wl.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
  19. */
  20. /*
  21. * UBI wear-leveling unit.
  22. *
  23. * This unit is responsible for wear-leveling. It works in terms of physical
  24. * eraseblocks and erase counters and knows nothing about logical eraseblocks,
  25. * volumes, etc. From this unit's perspective all physical eraseblocks are of
  26. * two types - used and free. Used physical eraseblocks are those that were
  27. * "get" by the 'ubi_wl_get_peb()' function, and free physical eraseblocks are
  28. * those that were put by the 'ubi_wl_put_peb()' function.
  29. *
  30. * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
  31. * header. The rest of the physical eraseblock contains only 0xFF bytes.
  32. *
  33. * When physical eraseblocks are returned to the WL unit by means of the
  34. * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
  35. * done asynchronously in context of the per-UBI device background thread,
  36. * which is also managed by the WL unit.
  37. *
  38. * The wear-leveling is ensured by means of moving the contents of used
  39. * physical eraseblocks with low erase counter to free physical eraseblocks
  40. * with high erase counter.
  41. *
  42. * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick
  43. * an "optimal" physical eraseblock. For example, when it is known that the
  44. * physical eraseblock will be "put" soon because it contains short-term data,
  45. * the WL unit may pick a free physical eraseblock with low erase counter, and
  46. * so forth.
  47. *
  48. * If the WL unit fails to erase a physical eraseblock, it marks it as bad.
  49. *
  50. * This unit is also responsible for scrubbing. If a bit-flip is detected in a
  51. * physical eraseblock, it has to be moved. Technically this is the same as
  52. * moving it for wear-leveling reasons.
  53. *
  54. * As it was said, for the UBI unit all physical eraseblocks are either "free"
  55. * or "used". Free eraseblock are kept in the @wl->free RB-tree, while used
  56. * eraseblocks are kept in a set of different RB-trees: @wl->used,
  57. * @wl->prot.pnum, @wl->prot.aec, and @wl->scrub.
  58. *
  59. * Note, in this implementation, we keep a small in-RAM object for each physical
  60. * eraseblock. This is surely not a scalable solution. But it appears to be good
  61. * enough for moderately large flashes and it is simple. In future, one may
  62. * re-work this unit and make it more scalable.
  63. *
  64. * At the moment this unit does not utilize the sequence number, which was
  65. * introduced relatively recently. But it would be wise to do this because the
  66. * sequence number of a logical eraseblock characterizes how old is it. For
  67. * example, when we move a PEB with low erase counter, and we need to pick the
  68. * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
  69. * pick target PEB with an average EC if our PEB is not very "old". This is a
  70. * room for future re-works of the WL unit.
  71. *
  72. * FIXME: looks too complex, should be simplified (later).
  73. */
  74. #ifdef UBI_LINUX
  75. #include <linux/slab.h>
  76. #include <linux/crc32.h>
  77. #include <linux/freezer.h>
  78. #include <linux/kthread.h>
  79. #endif
  80. #include <ubi_uboot.h>
  81. #include "ubi.h"
  82. /* Number of physical eraseblocks reserved for wear-leveling purposes */
  83. #define WL_RESERVED_PEBS 1
  84. /*
  85. * How many erase cycles are short term, unknown, and long term physical
  86. * eraseblocks protected.
  87. */
  88. #define ST_PROTECTION 16
  89. #define U_PROTECTION 10
  90. #define LT_PROTECTION 4
  91. /*
  92. * Maximum difference between two erase counters. If this threshold is
  93. * exceeded, the WL unit starts moving data from used physical eraseblocks with
  94. * low erase counter to free physical eraseblocks with high erase counter.
  95. */
  96. #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
  97. /*
  98. * When a physical eraseblock is moved, the WL unit has to pick the target
  99. * physical eraseblock to move to. The simplest way would be just to pick the
  100. * one with the highest erase counter. But in certain workloads this could lead
  101. * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
  102. * situation when the picked physical eraseblock is constantly erased after the
  103. * data is written to it. So, we have a constant which limits the highest erase
  104. * counter of the free physical eraseblock to pick. Namely, the WL unit does
  105. * not pick eraseblocks with erase counter greater then the lowest erase
  106. * counter plus %WL_FREE_MAX_DIFF.
  107. */
  108. #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
  109. /*
  110. * Maximum number of consecutive background thread failures which is enough to
  111. * switch to read-only mode.
  112. */
  113. #define WL_MAX_FAILURES 32
  114. /**
  115. * struct ubi_wl_prot_entry - PEB protection entry.
  116. * @rb_pnum: link in the @wl->prot.pnum RB-tree
  117. * @rb_aec: link in the @wl->prot.aec RB-tree
  118. * @abs_ec: the absolute erase counter value when the protection ends
  119. * @e: the wear-leveling entry of the physical eraseblock under protection
  120. *
  121. * When the WL unit returns a physical eraseblock, the physical eraseblock is
  122. * protected from being moved for some "time". For this reason, the physical
  123. * eraseblock is not directly moved from the @wl->free tree to the @wl->used
  124. * tree. There is one more tree in between where this physical eraseblock is
  125. * temporarily stored (@wl->prot).
  126. *
  127. * All this protection stuff is needed because:
  128. * o we don't want to move physical eraseblocks just after we have given them
  129. * to the user; instead, we first want to let users fill them up with data;
  130. *
  131. * o there is a chance that the user will put the physical eraseblock very
  132. * soon, so it makes sense not to move it for some time, but wait; this is
  133. * especially important in case of "short term" physical eraseblocks.
  134. *
  135. * Physical eraseblocks stay protected only for limited time. But the "time" is
  136. * measured in erase cycles in this case. This is implemented with help of the
  137. * absolute erase counter (@wl->abs_ec). When it reaches certain value, the
  138. * physical eraseblocks are moved from the protection trees (@wl->prot.*) to
  139. * the @wl->used tree.
  140. *
  141. * Protected physical eraseblocks are searched by physical eraseblock number
  142. * (when they are put) and by the absolute erase counter (to check if it is
  143. * time to move them to the @wl->used tree). So there are actually 2 RB-trees
  144. * storing the protected physical eraseblocks: @wl->prot.pnum and
  145. * @wl->prot.aec. They are referred to as the "protection" trees. The
  146. * first one is indexed by the physical eraseblock number. The second one is
  147. * indexed by the absolute erase counter. Both trees store
  148. * &struct ubi_wl_prot_entry objects.
  149. *
  150. * Each physical eraseblock has 2 main states: free and used. The former state
  151. * corresponds to the @wl->free tree. The latter state is split up on several
  152. * sub-states:
  153. * o the WL movement is allowed (@wl->used tree);
  154. * o the WL movement is temporarily prohibited (@wl->prot.pnum and
  155. * @wl->prot.aec trees);
  156. * o scrubbing is needed (@wl->scrub tree).
  157. *
  158. * Depending on the sub-state, wear-leveling entries of the used physical
  159. * eraseblocks may be kept in one of those trees.
  160. */
  161. struct ubi_wl_prot_entry {
  162. struct rb_node rb_pnum;
  163. struct rb_node rb_aec;
  164. unsigned long long abs_ec;
  165. struct ubi_wl_entry *e;
  166. };
  167. /**
  168. * struct ubi_work - UBI work description data structure.
  169. * @list: a link in the list of pending works
  170. * @func: worker function
  171. * @priv: private data of the worker function
  172. *
  173. * @e: physical eraseblock to erase
  174. * @torture: if the physical eraseblock has to be tortured
  175. *
  176. * The @func pointer points to the worker function. If the @cancel argument is
  177. * not zero, the worker has to free the resources and exit immediately. The
  178. * worker has to return zero in case of success and a negative error code in
  179. * case of failure.
  180. */
  181. struct ubi_work {
  182. struct list_head list;
  183. int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
  184. /* The below fields are only relevant to erasure works */
  185. struct ubi_wl_entry *e;
  186. int torture;
  187. };
  188. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  189. static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
  190. static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
  191. struct rb_root *root);
  192. #else
  193. #define paranoid_check_ec(ubi, pnum, ec) 0
  194. #define paranoid_check_in_wl_tree(e, root)
  195. #endif
  196. /**
  197. * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
  198. * @e: the wear-leveling entry to add
  199. * @root: the root of the tree
  200. *
  201. * Note, we use (erase counter, physical eraseblock number) pairs as keys in
  202. * the @ubi->used and @ubi->free RB-trees.
  203. */
  204. static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
  205. {
  206. struct rb_node **p, *parent = NULL;
  207. p = &root->rb_node;
  208. while (*p) {
  209. struct ubi_wl_entry *e1;
  210. parent = *p;
  211. e1 = rb_entry(parent, struct ubi_wl_entry, rb);
  212. if (e->ec < e1->ec)
  213. p = &(*p)->rb_left;
  214. else if (e->ec > e1->ec)
  215. p = &(*p)->rb_right;
  216. else {
  217. ubi_assert(e->pnum != e1->pnum);
  218. if (e->pnum < e1->pnum)
  219. p = &(*p)->rb_left;
  220. else
  221. p = &(*p)->rb_right;
  222. }
  223. }
  224. rb_link_node(&e->rb, parent, p);
  225. rb_insert_color(&e->rb, root);
  226. }
  227. /**
  228. * do_work - do one pending work.
  229. * @ubi: UBI device description object
  230. *
  231. * This function returns zero in case of success and a negative error code in
  232. * case of failure.
  233. */
  234. static int do_work(struct ubi_device *ubi)
  235. {
  236. int err;
  237. struct ubi_work *wrk;
  238. cond_resched();
  239. /*
  240. * @ubi->work_sem is used to synchronize with the workers. Workers take
  241. * it in read mode, so many of them may be doing works at a time. But
  242. * the queue flush code has to be sure the whole queue of works is
  243. * done, and it takes the mutex in write mode.
  244. */
  245. down_read(&ubi->work_sem);
  246. spin_lock(&ubi->wl_lock);
  247. if (list_empty(&ubi->works)) {
  248. spin_unlock(&ubi->wl_lock);
  249. up_read(&ubi->work_sem);
  250. return 0;
  251. }
  252. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  253. list_del(&wrk->list);
  254. ubi->works_count -= 1;
  255. ubi_assert(ubi->works_count >= 0);
  256. spin_unlock(&ubi->wl_lock);
  257. /*
  258. * Call the worker function. Do not touch the work structure
  259. * after this call as it will have been freed or reused by that
  260. * time by the worker function.
  261. */
  262. err = wrk->func(ubi, wrk, 0);
  263. if (err)
  264. ubi_err("work failed with error code %d", err);
  265. up_read(&ubi->work_sem);
  266. return err;
  267. }
  268. /**
  269. * produce_free_peb - produce a free physical eraseblock.
  270. * @ubi: UBI device description object
  271. *
  272. * This function tries to make a free PEB by means of synchronous execution of
  273. * pending works. This may be needed if, for example the background thread is
  274. * disabled. Returns zero in case of success and a negative error code in case
  275. * of failure.
  276. */
  277. static int produce_free_peb(struct ubi_device *ubi)
  278. {
  279. int err;
  280. spin_lock(&ubi->wl_lock);
  281. while (!ubi->free.rb_node) {
  282. spin_unlock(&ubi->wl_lock);
  283. dbg_wl("do one work synchronously");
  284. err = do_work(ubi);
  285. if (err)
  286. return err;
  287. spin_lock(&ubi->wl_lock);
  288. }
  289. spin_unlock(&ubi->wl_lock);
  290. return 0;
  291. }
  292. /**
  293. * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
  294. * @e: the wear-leveling entry to check
  295. * @root: the root of the tree
  296. *
  297. * This function returns non-zero if @e is in the @root RB-tree and zero if it
  298. * is not.
  299. */
  300. static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
  301. {
  302. struct rb_node *p;
  303. p = root->rb_node;
  304. while (p) {
  305. struct ubi_wl_entry *e1;
  306. e1 = rb_entry(p, struct ubi_wl_entry, rb);
  307. if (e->pnum == e1->pnum) {
  308. ubi_assert(e == e1);
  309. return 1;
  310. }
  311. if (e->ec < e1->ec)
  312. p = p->rb_left;
  313. else if (e->ec > e1->ec)
  314. p = p->rb_right;
  315. else {
  316. ubi_assert(e->pnum != e1->pnum);
  317. if (e->pnum < e1->pnum)
  318. p = p->rb_left;
  319. else
  320. p = p->rb_right;
  321. }
  322. }
  323. return 0;
  324. }
  325. /**
  326. * prot_tree_add - add physical eraseblock to protection trees.
  327. * @ubi: UBI device description object
  328. * @e: the physical eraseblock to add
  329. * @pe: protection entry object to use
  330. * @abs_ec: absolute erase counter value when this physical eraseblock has
  331. * to be removed from the protection trees.
  332. *
  333. * @wl->lock has to be locked.
  334. */
  335. static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e,
  336. struct ubi_wl_prot_entry *pe, int abs_ec)
  337. {
  338. struct rb_node **p, *parent = NULL;
  339. struct ubi_wl_prot_entry *pe1;
  340. pe->e = e;
  341. pe->abs_ec = ubi->abs_ec + abs_ec;
  342. p = &ubi->prot.pnum.rb_node;
  343. while (*p) {
  344. parent = *p;
  345. pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum);
  346. if (e->pnum < pe1->e->pnum)
  347. p = &(*p)->rb_left;
  348. else
  349. p = &(*p)->rb_right;
  350. }
  351. rb_link_node(&pe->rb_pnum, parent, p);
  352. rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum);
  353. p = &ubi->prot.aec.rb_node;
  354. parent = NULL;
  355. while (*p) {
  356. parent = *p;
  357. pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec);
  358. if (pe->abs_ec < pe1->abs_ec)
  359. p = &(*p)->rb_left;
  360. else
  361. p = &(*p)->rb_right;
  362. }
  363. rb_link_node(&pe->rb_aec, parent, p);
  364. rb_insert_color(&pe->rb_aec, &ubi->prot.aec);
  365. }
  366. /**
  367. * find_wl_entry - find wear-leveling entry closest to certain erase counter.
  368. * @root: the RB-tree where to look for
  369. * @max: highest possible erase counter
  370. *
  371. * This function looks for a wear leveling entry with erase counter closest to
  372. * @max and less then @max.
  373. */
  374. static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
  375. {
  376. struct rb_node *p;
  377. struct ubi_wl_entry *e;
  378. e = rb_entry(rb_first(root), struct ubi_wl_entry, rb);
  379. max += e->ec;
  380. p = root->rb_node;
  381. while (p) {
  382. struct ubi_wl_entry *e1;
  383. e1 = rb_entry(p, struct ubi_wl_entry, rb);
  384. if (e1->ec >= max)
  385. p = p->rb_left;
  386. else {
  387. p = p->rb_right;
  388. e = e1;
  389. }
  390. }
  391. return e;
  392. }
  393. /**
  394. * ubi_wl_get_peb - get a physical eraseblock.
  395. * @ubi: UBI device description object
  396. * @dtype: type of data which will be stored in this physical eraseblock
  397. *
  398. * This function returns a physical eraseblock in case of success and a
  399. * negative error code in case of failure. Might sleep.
  400. */
  401. int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
  402. {
  403. int err, protect, medium_ec;
  404. struct ubi_wl_entry *e, *first, *last;
  405. struct ubi_wl_prot_entry *pe;
  406. ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
  407. dtype == UBI_UNKNOWN);
  408. pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
  409. if (!pe)
  410. return -ENOMEM;
  411. retry:
  412. spin_lock(&ubi->wl_lock);
  413. if (!ubi->free.rb_node) {
  414. if (ubi->works_count == 0) {
  415. ubi_assert(list_empty(&ubi->works));
  416. ubi_err("no free eraseblocks");
  417. spin_unlock(&ubi->wl_lock);
  418. kfree(pe);
  419. return -ENOSPC;
  420. }
  421. spin_unlock(&ubi->wl_lock);
  422. err = produce_free_peb(ubi);
  423. if (err < 0) {
  424. kfree(pe);
  425. return err;
  426. }
  427. goto retry;
  428. }
  429. switch (dtype) {
  430. case UBI_LONGTERM:
  431. /*
  432. * For long term data we pick a physical eraseblock
  433. * with high erase counter. But the highest erase
  434. * counter we can pick is bounded by the the lowest
  435. * erase counter plus %WL_FREE_MAX_DIFF.
  436. */
  437. e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  438. protect = LT_PROTECTION;
  439. break;
  440. case UBI_UNKNOWN:
  441. /*
  442. * For unknown data we pick a physical eraseblock with
  443. * medium erase counter. But we by no means can pick a
  444. * physical eraseblock with erase counter greater or
  445. * equivalent than the lowest erase counter plus
  446. * %WL_FREE_MAX_DIFF.
  447. */
  448. first = rb_entry(rb_first(&ubi->free),
  449. struct ubi_wl_entry, rb);
  450. last = rb_entry(rb_last(&ubi->free),
  451. struct ubi_wl_entry, rb);
  452. if (last->ec - first->ec < WL_FREE_MAX_DIFF)
  453. e = rb_entry(ubi->free.rb_node,
  454. struct ubi_wl_entry, rb);
  455. else {
  456. medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
  457. e = find_wl_entry(&ubi->free, medium_ec);
  458. }
  459. protect = U_PROTECTION;
  460. break;
  461. case UBI_SHORTTERM:
  462. /*
  463. * For short term data we pick a physical eraseblock
  464. * with the lowest erase counter as we expect it will
  465. * be erased soon.
  466. */
  467. e = rb_entry(rb_first(&ubi->free),
  468. struct ubi_wl_entry, rb);
  469. protect = ST_PROTECTION;
  470. break;
  471. default:
  472. protect = 0;
  473. e = NULL;
  474. BUG();
  475. }
  476. /*
  477. * Move the physical eraseblock to the protection trees where it will
  478. * be protected from being moved for some time.
  479. */
  480. paranoid_check_in_wl_tree(e, &ubi->free);
  481. rb_erase(&e->rb, &ubi->free);
  482. prot_tree_add(ubi, e, pe, protect);
  483. dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect);
  484. spin_unlock(&ubi->wl_lock);
  485. return e->pnum;
  486. }
  487. /**
  488. * prot_tree_del - remove a physical eraseblock from the protection trees
  489. * @ubi: UBI device description object
  490. * @pnum: the physical eraseblock to remove
  491. *
  492. * This function returns PEB @pnum from the protection trees and returns zero
  493. * in case of success and %-ENODEV if the PEB was not found in the protection
  494. * trees.
  495. */
  496. static int prot_tree_del(struct ubi_device *ubi, int pnum)
  497. {
  498. struct rb_node *p;
  499. struct ubi_wl_prot_entry *pe = NULL;
  500. p = ubi->prot.pnum.rb_node;
  501. while (p) {
  502. pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum);
  503. if (pnum == pe->e->pnum)
  504. goto found;
  505. if (pnum < pe->e->pnum)
  506. p = p->rb_left;
  507. else
  508. p = p->rb_right;
  509. }
  510. return -ENODEV;
  511. found:
  512. ubi_assert(pe->e->pnum == pnum);
  513. rb_erase(&pe->rb_aec, &ubi->prot.aec);
  514. rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
  515. kfree(pe);
  516. return 0;
  517. }
  518. /**
  519. * sync_erase - synchronously erase a physical eraseblock.
  520. * @ubi: UBI device description object
  521. * @e: the the physical eraseblock to erase
  522. * @torture: if the physical eraseblock has to be tortured
  523. *
  524. * This function returns zero in case of success and a negative error code in
  525. * case of failure.
  526. */
  527. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture)
  528. {
  529. int err;
  530. struct ubi_ec_hdr *ec_hdr;
  531. unsigned long long ec = e->ec;
  532. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  533. err = paranoid_check_ec(ubi, e->pnum, e->ec);
  534. if (err > 0)
  535. return -EINVAL;
  536. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  537. if (!ec_hdr)
  538. return -ENOMEM;
  539. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  540. if (err < 0)
  541. goto out_free;
  542. ec += err;
  543. if (ec > UBI_MAX_ERASECOUNTER) {
  544. /*
  545. * Erase counter overflow. Upgrade UBI and use 64-bit
  546. * erase counters internally.
  547. */
  548. ubi_err("erase counter overflow at PEB %d, EC %llu",
  549. e->pnum, ec);
  550. err = -EINVAL;
  551. goto out_free;
  552. }
  553. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  554. ec_hdr->ec = cpu_to_be64(ec);
  555. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  556. if (err)
  557. goto out_free;
  558. e->ec = ec;
  559. spin_lock(&ubi->wl_lock);
  560. if (e->ec > ubi->max_ec)
  561. ubi->max_ec = e->ec;
  562. spin_unlock(&ubi->wl_lock);
  563. out_free:
  564. kfree(ec_hdr);
  565. return err;
  566. }
  567. /**
  568. * check_protection_over - check if it is time to stop protecting some
  569. * physical eraseblocks.
  570. * @ubi: UBI device description object
  571. *
  572. * This function is called after each erase operation, when the absolute erase
  573. * counter is incremented, to check if some physical eraseblock have not to be
  574. * protected any longer. These physical eraseblocks are moved from the
  575. * protection trees to the used tree.
  576. */
  577. static void check_protection_over(struct ubi_device *ubi)
  578. {
  579. struct ubi_wl_prot_entry *pe;
  580. /*
  581. * There may be several protected physical eraseblock to remove,
  582. * process them all.
  583. */
  584. while (1) {
  585. spin_lock(&ubi->wl_lock);
  586. if (!ubi->prot.aec.rb_node) {
  587. spin_unlock(&ubi->wl_lock);
  588. break;
  589. }
  590. pe = rb_entry(rb_first(&ubi->prot.aec),
  591. struct ubi_wl_prot_entry, rb_aec);
  592. if (pe->abs_ec > ubi->abs_ec) {
  593. spin_unlock(&ubi->wl_lock);
  594. break;
  595. }
  596. dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu",
  597. pe->e->pnum, ubi->abs_ec, pe->abs_ec);
  598. rb_erase(&pe->rb_aec, &ubi->prot.aec);
  599. rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
  600. wl_tree_add(pe->e, &ubi->used);
  601. spin_unlock(&ubi->wl_lock);
  602. kfree(pe);
  603. cond_resched();
  604. }
  605. }
  606. /**
  607. * schedule_ubi_work - schedule a work.
  608. * @ubi: UBI device description object
  609. * @wrk: the work to schedule
  610. *
  611. * This function enqueues a work defined by @wrk to the tail of the pending
  612. * works list.
  613. */
  614. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  615. {
  616. spin_lock(&ubi->wl_lock);
  617. list_add_tail(&wrk->list, &ubi->works);
  618. ubi_assert(ubi->works_count >= 0);
  619. ubi->works_count += 1;
  620. /*
  621. * U-Boot special: We have no bgt_thread in U-Boot!
  622. * So just call do_work() here directly.
  623. */
  624. do_work(ubi);
  625. spin_unlock(&ubi->wl_lock);
  626. }
  627. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  628. int cancel);
  629. /**
  630. * schedule_erase - schedule an erase work.
  631. * @ubi: UBI device description object
  632. * @e: the WL entry of the physical eraseblock to erase
  633. * @torture: if the physical eraseblock has to be tortured
  634. *
  635. * This function returns zero in case of success and a %-ENOMEM in case of
  636. * failure.
  637. */
  638. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  639. int torture)
  640. {
  641. struct ubi_work *wl_wrk;
  642. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  643. e->pnum, e->ec, torture);
  644. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  645. if (!wl_wrk)
  646. return -ENOMEM;
  647. wl_wrk->func = &erase_worker;
  648. wl_wrk->e = e;
  649. wl_wrk->torture = torture;
  650. schedule_ubi_work(ubi, wl_wrk);
  651. return 0;
  652. }
  653. /**
  654. * wear_leveling_worker - wear-leveling worker function.
  655. * @ubi: UBI device description object
  656. * @wrk: the work object
  657. * @cancel: non-zero if the worker has to free memory and exit
  658. *
  659. * This function copies a more worn out physical eraseblock to a less worn out
  660. * one. Returns zero in case of success and a negative error code in case of
  661. * failure.
  662. */
  663. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  664. int cancel)
  665. {
  666. int err, put = 0, scrubbing = 0, protect = 0;
  667. struct ubi_wl_prot_entry *uninitialized_var(pe);
  668. struct ubi_wl_entry *e1, *e2;
  669. struct ubi_vid_hdr *vid_hdr;
  670. kfree(wrk);
  671. if (cancel)
  672. return 0;
  673. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  674. if (!vid_hdr)
  675. return -ENOMEM;
  676. mutex_lock(&ubi->move_mutex);
  677. spin_lock(&ubi->wl_lock);
  678. ubi_assert(!ubi->move_from && !ubi->move_to);
  679. ubi_assert(!ubi->move_to_put);
  680. if (!ubi->free.rb_node ||
  681. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  682. /*
  683. * No free physical eraseblocks? Well, they must be waiting in
  684. * the queue to be erased. Cancel movement - it will be
  685. * triggered again when a free physical eraseblock appears.
  686. *
  687. * No used physical eraseblocks? They must be temporarily
  688. * protected from being moved. They will be moved to the
  689. * @ubi->used tree later and the wear-leveling will be
  690. * triggered again.
  691. */
  692. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  693. !ubi->free.rb_node, !ubi->used.rb_node);
  694. goto out_cancel;
  695. }
  696. if (!ubi->scrub.rb_node) {
  697. /*
  698. * Now pick the least worn-out used physical eraseblock and a
  699. * highly worn-out free physical eraseblock. If the erase
  700. * counters differ much enough, start wear-leveling.
  701. */
  702. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
  703. e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  704. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  705. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  706. e1->ec, e2->ec);
  707. goto out_cancel;
  708. }
  709. paranoid_check_in_wl_tree(e1, &ubi->used);
  710. rb_erase(&e1->rb, &ubi->used);
  711. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  712. e1->pnum, e1->ec, e2->pnum, e2->ec);
  713. } else {
  714. /* Perform scrubbing */
  715. scrubbing = 1;
  716. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb);
  717. e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  718. paranoid_check_in_wl_tree(e1, &ubi->scrub);
  719. rb_erase(&e1->rb, &ubi->scrub);
  720. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  721. }
  722. paranoid_check_in_wl_tree(e2, &ubi->free);
  723. rb_erase(&e2->rb, &ubi->free);
  724. ubi->move_from = e1;
  725. ubi->move_to = e2;
  726. spin_unlock(&ubi->wl_lock);
  727. /*
  728. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  729. * We so far do not know which logical eraseblock our physical
  730. * eraseblock (@e1) belongs to. We have to read the volume identifier
  731. * header first.
  732. *
  733. * Note, we are protected from this PEB being unmapped and erased. The
  734. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  735. * which is being moved was unmapped.
  736. */
  737. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  738. if (err && err != UBI_IO_BITFLIPS) {
  739. if (err == UBI_IO_PEB_FREE) {
  740. /*
  741. * We are trying to move PEB without a VID header. UBI
  742. * always write VID headers shortly after the PEB was
  743. * given, so we have a situation when it did not have
  744. * chance to write it down because it was preempted.
  745. * Just re-schedule the work, so that next time it will
  746. * likely have the VID header in place.
  747. */
  748. dbg_wl("PEB %d has no VID header", e1->pnum);
  749. goto out_not_moved;
  750. }
  751. ubi_err("error %d while reading VID header from PEB %d",
  752. err, e1->pnum);
  753. if (err > 0)
  754. err = -EIO;
  755. goto out_error;
  756. }
  757. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  758. if (err) {
  759. if (err < 0)
  760. goto out_error;
  761. if (err == 1)
  762. goto out_not_moved;
  763. /*
  764. * For some reason the LEB was not moved - it might be because
  765. * the volume is being deleted. We should prevent this PEB from
  766. * being selected for wear-levelling movement for some "time",
  767. * so put it to the protection tree.
  768. */
  769. dbg_wl("cancelled moving PEB %d", e1->pnum);
  770. pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
  771. if (!pe) {
  772. err = -ENOMEM;
  773. goto out_error;
  774. }
  775. protect = 1;
  776. }
  777. ubi_free_vid_hdr(ubi, vid_hdr);
  778. spin_lock(&ubi->wl_lock);
  779. if (protect)
  780. prot_tree_add(ubi, e1, pe, protect);
  781. if (!ubi->move_to_put)
  782. wl_tree_add(e2, &ubi->used);
  783. else
  784. put = 1;
  785. ubi->move_from = ubi->move_to = NULL;
  786. ubi->move_to_put = ubi->wl_scheduled = 0;
  787. spin_unlock(&ubi->wl_lock);
  788. if (put) {
  789. /*
  790. * Well, the target PEB was put meanwhile, schedule it for
  791. * erasure.
  792. */
  793. dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
  794. err = schedule_erase(ubi, e2, 0);
  795. if (err)
  796. goto out_error;
  797. }
  798. if (!protect) {
  799. err = schedule_erase(ubi, e1, 0);
  800. if (err)
  801. goto out_error;
  802. }
  803. dbg_wl("done");
  804. mutex_unlock(&ubi->move_mutex);
  805. return 0;
  806. /*
  807. * For some reasons the LEB was not moved, might be an error, might be
  808. * something else. @e1 was not changed, so return it back. @e2 might
  809. * be changed, schedule it for erasure.
  810. */
  811. out_not_moved:
  812. ubi_free_vid_hdr(ubi, vid_hdr);
  813. spin_lock(&ubi->wl_lock);
  814. if (scrubbing)
  815. wl_tree_add(e1, &ubi->scrub);
  816. else
  817. wl_tree_add(e1, &ubi->used);
  818. ubi->move_from = ubi->move_to = NULL;
  819. ubi->move_to_put = ubi->wl_scheduled = 0;
  820. spin_unlock(&ubi->wl_lock);
  821. err = schedule_erase(ubi, e2, 0);
  822. if (err)
  823. goto out_error;
  824. mutex_unlock(&ubi->move_mutex);
  825. return 0;
  826. out_error:
  827. ubi_err("error %d while moving PEB %d to PEB %d",
  828. err, e1->pnum, e2->pnum);
  829. ubi_free_vid_hdr(ubi, vid_hdr);
  830. spin_lock(&ubi->wl_lock);
  831. ubi->move_from = ubi->move_to = NULL;
  832. ubi->move_to_put = ubi->wl_scheduled = 0;
  833. spin_unlock(&ubi->wl_lock);
  834. kmem_cache_free(ubi_wl_entry_slab, e1);
  835. kmem_cache_free(ubi_wl_entry_slab, e2);
  836. ubi_ro_mode(ubi);
  837. mutex_unlock(&ubi->move_mutex);
  838. return err;
  839. out_cancel:
  840. ubi->wl_scheduled = 0;
  841. spin_unlock(&ubi->wl_lock);
  842. mutex_unlock(&ubi->move_mutex);
  843. ubi_free_vid_hdr(ubi, vid_hdr);
  844. return 0;
  845. }
  846. /**
  847. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  848. * @ubi: UBI device description object
  849. *
  850. * This function checks if it is time to start wear-leveling and schedules it
  851. * if yes. This function returns zero in case of success and a negative error
  852. * code in case of failure.
  853. */
  854. static int ensure_wear_leveling(struct ubi_device *ubi)
  855. {
  856. int err = 0;
  857. struct ubi_wl_entry *e1;
  858. struct ubi_wl_entry *e2;
  859. struct ubi_work *wrk;
  860. spin_lock(&ubi->wl_lock);
  861. if (ubi->wl_scheduled)
  862. /* Wear-leveling is already in the work queue */
  863. goto out_unlock;
  864. /*
  865. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  866. * the WL worker has to be scheduled anyway.
  867. */
  868. if (!ubi->scrub.rb_node) {
  869. if (!ubi->used.rb_node || !ubi->free.rb_node)
  870. /* No physical eraseblocks - no deal */
  871. goto out_unlock;
  872. /*
  873. * We schedule wear-leveling only if the difference between the
  874. * lowest erase counter of used physical eraseblocks and a high
  875. * erase counter of free physical eraseblocks is greater then
  876. * %UBI_WL_THRESHOLD.
  877. */
  878. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
  879. e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  880. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  881. goto out_unlock;
  882. dbg_wl("schedule wear-leveling");
  883. } else
  884. dbg_wl("schedule scrubbing");
  885. ubi->wl_scheduled = 1;
  886. spin_unlock(&ubi->wl_lock);
  887. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  888. if (!wrk) {
  889. err = -ENOMEM;
  890. goto out_cancel;
  891. }
  892. wrk->func = &wear_leveling_worker;
  893. schedule_ubi_work(ubi, wrk);
  894. return err;
  895. out_cancel:
  896. spin_lock(&ubi->wl_lock);
  897. ubi->wl_scheduled = 0;
  898. out_unlock:
  899. spin_unlock(&ubi->wl_lock);
  900. return err;
  901. }
  902. /**
  903. * erase_worker - physical eraseblock erase worker function.
  904. * @ubi: UBI device description object
  905. * @wl_wrk: the work object
  906. * @cancel: non-zero if the worker has to free memory and exit
  907. *
  908. * This function erases a physical eraseblock and perform torture testing if
  909. * needed. It also takes care about marking the physical eraseblock bad if
  910. * needed. Returns zero in case of success and a negative error code in case of
  911. * failure.
  912. */
  913. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  914. int cancel)
  915. {
  916. struct ubi_wl_entry *e = wl_wrk->e;
  917. int pnum = e->pnum, err, need;
  918. if (cancel) {
  919. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  920. kfree(wl_wrk);
  921. kmem_cache_free(ubi_wl_entry_slab, e);
  922. return 0;
  923. }
  924. dbg_wl("erase PEB %d EC %d", pnum, e->ec);
  925. err = sync_erase(ubi, e, wl_wrk->torture);
  926. if (!err) {
  927. /* Fine, we've erased it successfully */
  928. kfree(wl_wrk);
  929. spin_lock(&ubi->wl_lock);
  930. ubi->abs_ec += 1;
  931. wl_tree_add(e, &ubi->free);
  932. spin_unlock(&ubi->wl_lock);
  933. /*
  934. * One more erase operation has happened, take care about protected
  935. * physical eraseblocks.
  936. */
  937. check_protection_over(ubi);
  938. /* And take care about wear-leveling */
  939. err = ensure_wear_leveling(ubi);
  940. return err;
  941. }
  942. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  943. kfree(wl_wrk);
  944. kmem_cache_free(ubi_wl_entry_slab, e);
  945. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  946. err == -EBUSY) {
  947. int err1;
  948. /* Re-schedule the LEB for erasure */
  949. err1 = schedule_erase(ubi, e, 0);
  950. if (err1) {
  951. err = err1;
  952. goto out_ro;
  953. }
  954. return err;
  955. } else if (err != -EIO) {
  956. /*
  957. * If this is not %-EIO, we have no idea what to do. Scheduling
  958. * this physical eraseblock for erasure again would cause
  959. * errors again and again. Well, lets switch to RO mode.
  960. */
  961. goto out_ro;
  962. }
  963. /* It is %-EIO, the PEB went bad */
  964. if (!ubi->bad_allowed) {
  965. ubi_err("bad physical eraseblock %d detected", pnum);
  966. goto out_ro;
  967. }
  968. spin_lock(&ubi->volumes_lock);
  969. need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
  970. if (need > 0) {
  971. need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
  972. ubi->avail_pebs -= need;
  973. ubi->rsvd_pebs += need;
  974. ubi->beb_rsvd_pebs += need;
  975. if (need > 0)
  976. ubi_msg("reserve more %d PEBs", need);
  977. }
  978. if (ubi->beb_rsvd_pebs == 0) {
  979. spin_unlock(&ubi->volumes_lock);
  980. ubi_err("no reserved physical eraseblocks");
  981. goto out_ro;
  982. }
  983. spin_unlock(&ubi->volumes_lock);
  984. ubi_msg("mark PEB %d as bad", pnum);
  985. err = ubi_io_mark_bad(ubi, pnum);
  986. if (err)
  987. goto out_ro;
  988. spin_lock(&ubi->volumes_lock);
  989. ubi->beb_rsvd_pebs -= 1;
  990. ubi->bad_peb_count += 1;
  991. ubi->good_peb_count -= 1;
  992. ubi_calculate_reserved(ubi);
  993. if (ubi->beb_rsvd_pebs == 0)
  994. ubi_warn("last PEB from the reserved pool was used");
  995. spin_unlock(&ubi->volumes_lock);
  996. return err;
  997. out_ro:
  998. ubi_ro_mode(ubi);
  999. return err;
  1000. }
  1001. /**
  1002. * ubi_wl_put_peb - return a physical eraseblock to the wear-leveling unit.
  1003. * @ubi: UBI device description object
  1004. * @pnum: physical eraseblock to return
  1005. * @torture: if this physical eraseblock has to be tortured
  1006. *
  1007. * This function is called to return physical eraseblock @pnum to the pool of
  1008. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1009. * occurred to this @pnum and it has to be tested. This function returns zero
  1010. * in case of success, and a negative error code in case of failure.
  1011. */
  1012. int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
  1013. {
  1014. int err;
  1015. struct ubi_wl_entry *e;
  1016. dbg_wl("PEB %d", pnum);
  1017. ubi_assert(pnum >= 0);
  1018. ubi_assert(pnum < ubi->peb_count);
  1019. retry:
  1020. spin_lock(&ubi->wl_lock);
  1021. e = ubi->lookuptbl[pnum];
  1022. if (e == ubi->move_from) {
  1023. /*
  1024. * User is putting the physical eraseblock which was selected to
  1025. * be moved. It will be scheduled for erasure in the
  1026. * wear-leveling worker.
  1027. */
  1028. dbg_wl("PEB %d is being moved, wait", pnum);
  1029. spin_unlock(&ubi->wl_lock);
  1030. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1031. mutex_lock(&ubi->move_mutex);
  1032. mutex_unlock(&ubi->move_mutex);
  1033. goto retry;
  1034. } else if (e == ubi->move_to) {
  1035. /*
  1036. * User is putting the physical eraseblock which was selected
  1037. * as the target the data is moved to. It may happen if the EBA
  1038. * unit already re-mapped the LEB in 'ubi_eba_copy_leb()' but
  1039. * the WL unit has not put the PEB to the "used" tree yet, but
  1040. * it is about to do this. So we just set a flag which will
  1041. * tell the WL worker that the PEB is not needed anymore and
  1042. * should be scheduled for erasure.
  1043. */
  1044. dbg_wl("PEB %d is the target of data moving", pnum);
  1045. ubi_assert(!ubi->move_to_put);
  1046. ubi->move_to_put = 1;
  1047. spin_unlock(&ubi->wl_lock);
  1048. return 0;
  1049. } else {
  1050. if (in_wl_tree(e, &ubi->used)) {
  1051. paranoid_check_in_wl_tree(e, &ubi->used);
  1052. rb_erase(&e->rb, &ubi->used);
  1053. } else if (in_wl_tree(e, &ubi->scrub)) {
  1054. paranoid_check_in_wl_tree(e, &ubi->scrub);
  1055. rb_erase(&e->rb, &ubi->scrub);
  1056. } else {
  1057. err = prot_tree_del(ubi, e->pnum);
  1058. if (err) {
  1059. ubi_err("PEB %d not found", pnum);
  1060. ubi_ro_mode(ubi);
  1061. spin_unlock(&ubi->wl_lock);
  1062. return err;
  1063. }
  1064. }
  1065. }
  1066. spin_unlock(&ubi->wl_lock);
  1067. err = schedule_erase(ubi, e, torture);
  1068. if (err) {
  1069. spin_lock(&ubi->wl_lock);
  1070. wl_tree_add(e, &ubi->used);
  1071. spin_unlock(&ubi->wl_lock);
  1072. }
  1073. return err;
  1074. }
  1075. /**
  1076. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1077. * @ubi: UBI device description object
  1078. * @pnum: the physical eraseblock to schedule
  1079. *
  1080. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1081. * needs scrubbing. This function schedules a physical eraseblock for
  1082. * scrubbing which is done in background. This function returns zero in case of
  1083. * success and a negative error code in case of failure.
  1084. */
  1085. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1086. {
  1087. struct ubi_wl_entry *e;
  1088. ubi_msg("schedule PEB %d for scrubbing", pnum);
  1089. retry:
  1090. spin_lock(&ubi->wl_lock);
  1091. e = ubi->lookuptbl[pnum];
  1092. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
  1093. spin_unlock(&ubi->wl_lock);
  1094. return 0;
  1095. }
  1096. if (e == ubi->move_to) {
  1097. /*
  1098. * This physical eraseblock was used to move data to. The data
  1099. * was moved but the PEB was not yet inserted to the proper
  1100. * tree. We should just wait a little and let the WL worker
  1101. * proceed.
  1102. */
  1103. spin_unlock(&ubi->wl_lock);
  1104. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1105. yield();
  1106. goto retry;
  1107. }
  1108. if (in_wl_tree(e, &ubi->used)) {
  1109. paranoid_check_in_wl_tree(e, &ubi->used);
  1110. rb_erase(&e->rb, &ubi->used);
  1111. } else {
  1112. int err;
  1113. err = prot_tree_del(ubi, e->pnum);
  1114. if (err) {
  1115. ubi_err("PEB %d not found", pnum);
  1116. ubi_ro_mode(ubi);
  1117. spin_unlock(&ubi->wl_lock);
  1118. return err;
  1119. }
  1120. }
  1121. wl_tree_add(e, &ubi->scrub);
  1122. spin_unlock(&ubi->wl_lock);
  1123. /*
  1124. * Technically scrubbing is the same as wear-leveling, so it is done
  1125. * by the WL worker.
  1126. */
  1127. return ensure_wear_leveling(ubi);
  1128. }
  1129. /**
  1130. * ubi_wl_flush - flush all pending works.
  1131. * @ubi: UBI device description object
  1132. *
  1133. * This function returns zero in case of success and a negative error code in
  1134. * case of failure.
  1135. */
  1136. int ubi_wl_flush(struct ubi_device *ubi)
  1137. {
  1138. int err;
  1139. /*
  1140. * Erase while the pending works queue is not empty, but not more then
  1141. * the number of currently pending works.
  1142. */
  1143. dbg_wl("flush (%d pending works)", ubi->works_count);
  1144. while (ubi->works_count) {
  1145. err = do_work(ubi);
  1146. if (err)
  1147. return err;
  1148. }
  1149. /*
  1150. * Make sure all the works which have been done in parallel are
  1151. * finished.
  1152. */
  1153. down_write(&ubi->work_sem);
  1154. up_write(&ubi->work_sem);
  1155. /*
  1156. * And in case last was the WL worker and it cancelled the LEB
  1157. * movement, flush again.
  1158. */
  1159. while (ubi->works_count) {
  1160. dbg_wl("flush more (%d pending works)", ubi->works_count);
  1161. err = do_work(ubi);
  1162. if (err)
  1163. return err;
  1164. }
  1165. return 0;
  1166. }
  1167. /**
  1168. * tree_destroy - destroy an RB-tree.
  1169. * @root: the root of the tree to destroy
  1170. */
  1171. static void tree_destroy(struct rb_root *root)
  1172. {
  1173. struct rb_node *rb;
  1174. struct ubi_wl_entry *e;
  1175. rb = root->rb_node;
  1176. while (rb) {
  1177. if (rb->rb_left)
  1178. rb = rb->rb_left;
  1179. else if (rb->rb_right)
  1180. rb = rb->rb_right;
  1181. else {
  1182. e = rb_entry(rb, struct ubi_wl_entry, rb);
  1183. rb = rb_parent(rb);
  1184. if (rb) {
  1185. if (rb->rb_left == &e->rb)
  1186. rb->rb_left = NULL;
  1187. else
  1188. rb->rb_right = NULL;
  1189. }
  1190. kmem_cache_free(ubi_wl_entry_slab, e);
  1191. }
  1192. }
  1193. }
  1194. /**
  1195. * ubi_thread - UBI background thread.
  1196. * @u: the UBI device description object pointer
  1197. */
  1198. int ubi_thread(void *u)
  1199. {
  1200. int failures = 0;
  1201. struct ubi_device *ubi = u;
  1202. ubi_msg("background thread \"%s\" started, PID %d",
  1203. ubi->bgt_name, task_pid_nr(current));
  1204. set_freezable();
  1205. for (;;) {
  1206. int err;
  1207. if (kthread_should_stop())
  1208. break;
  1209. if (try_to_freeze())
  1210. continue;
  1211. spin_lock(&ubi->wl_lock);
  1212. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1213. !ubi->thread_enabled) {
  1214. set_current_state(TASK_INTERRUPTIBLE);
  1215. spin_unlock(&ubi->wl_lock);
  1216. schedule();
  1217. continue;
  1218. }
  1219. spin_unlock(&ubi->wl_lock);
  1220. err = do_work(ubi);
  1221. if (err) {
  1222. ubi_err("%s: work failed with error code %d",
  1223. ubi->bgt_name, err);
  1224. if (failures++ > WL_MAX_FAILURES) {
  1225. /*
  1226. * Too many failures, disable the thread and
  1227. * switch to read-only mode.
  1228. */
  1229. ubi_msg("%s: %d consecutive failures",
  1230. ubi->bgt_name, WL_MAX_FAILURES);
  1231. ubi_ro_mode(ubi);
  1232. break;
  1233. }
  1234. } else
  1235. failures = 0;
  1236. cond_resched();
  1237. }
  1238. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1239. return 0;
  1240. }
  1241. /**
  1242. * cancel_pending - cancel all pending works.
  1243. * @ubi: UBI device description object
  1244. */
  1245. static void cancel_pending(struct ubi_device *ubi)
  1246. {
  1247. while (!list_empty(&ubi->works)) {
  1248. struct ubi_work *wrk;
  1249. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1250. list_del(&wrk->list);
  1251. wrk->func(ubi, wrk, 1);
  1252. ubi->works_count -= 1;
  1253. ubi_assert(ubi->works_count >= 0);
  1254. }
  1255. }
  1256. /**
  1257. * ubi_wl_init_scan - initialize the wear-leveling unit using scanning
  1258. * information.
  1259. * @ubi: UBI device description object
  1260. * @si: scanning information
  1261. *
  1262. * This function returns zero in case of success, and a negative error code in
  1263. * case of failure.
  1264. */
  1265. int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
  1266. {
  1267. int err;
  1268. struct rb_node *rb1, *rb2;
  1269. struct ubi_scan_volume *sv;
  1270. struct ubi_scan_leb *seb, *tmp;
  1271. struct ubi_wl_entry *e;
  1272. ubi->used = ubi->free = ubi->scrub = RB_ROOT;
  1273. ubi->prot.pnum = ubi->prot.aec = RB_ROOT;
  1274. spin_lock_init(&ubi->wl_lock);
  1275. mutex_init(&ubi->move_mutex);
  1276. init_rwsem(&ubi->work_sem);
  1277. ubi->max_ec = si->max_ec;
  1278. INIT_LIST_HEAD(&ubi->works);
  1279. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1280. err = -ENOMEM;
  1281. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1282. if (!ubi->lookuptbl)
  1283. return err;
  1284. list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
  1285. cond_resched();
  1286. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1287. if (!e)
  1288. goto out_free;
  1289. e->pnum = seb->pnum;
  1290. e->ec = seb->ec;
  1291. ubi->lookuptbl[e->pnum] = e;
  1292. if (schedule_erase(ubi, e, 0)) {
  1293. kmem_cache_free(ubi_wl_entry_slab, e);
  1294. goto out_free;
  1295. }
  1296. }
  1297. list_for_each_entry(seb, &si->free, u.list) {
  1298. cond_resched();
  1299. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1300. if (!e)
  1301. goto out_free;
  1302. e->pnum = seb->pnum;
  1303. e->ec = seb->ec;
  1304. ubi_assert(e->ec >= 0);
  1305. wl_tree_add(e, &ubi->free);
  1306. ubi->lookuptbl[e->pnum] = e;
  1307. }
  1308. list_for_each_entry(seb, &si->corr, u.list) {
  1309. cond_resched();
  1310. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1311. if (!e)
  1312. goto out_free;
  1313. e->pnum = seb->pnum;
  1314. e->ec = seb->ec;
  1315. ubi->lookuptbl[e->pnum] = e;
  1316. if (schedule_erase(ubi, e, 0)) {
  1317. kmem_cache_free(ubi_wl_entry_slab, e);
  1318. goto out_free;
  1319. }
  1320. }
  1321. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1322. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1323. cond_resched();
  1324. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1325. if (!e)
  1326. goto out_free;
  1327. e->pnum = seb->pnum;
  1328. e->ec = seb->ec;
  1329. ubi->lookuptbl[e->pnum] = e;
  1330. if (!seb->scrub) {
  1331. dbg_wl("add PEB %d EC %d to the used tree",
  1332. e->pnum, e->ec);
  1333. wl_tree_add(e, &ubi->used);
  1334. } else {
  1335. dbg_wl("add PEB %d EC %d to the scrub tree",
  1336. e->pnum, e->ec);
  1337. wl_tree_add(e, &ubi->scrub);
  1338. }
  1339. }
  1340. }
  1341. if (ubi->avail_pebs < WL_RESERVED_PEBS) {
  1342. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1343. ubi->avail_pebs, WL_RESERVED_PEBS);
  1344. err = -ENOSPC;
  1345. goto out_free;
  1346. }
  1347. ubi->avail_pebs -= WL_RESERVED_PEBS;
  1348. ubi->rsvd_pebs += WL_RESERVED_PEBS;
  1349. /* Schedule wear-leveling if needed */
  1350. err = ensure_wear_leveling(ubi);
  1351. if (err)
  1352. goto out_free;
  1353. return 0;
  1354. out_free:
  1355. cancel_pending(ubi);
  1356. tree_destroy(&ubi->used);
  1357. tree_destroy(&ubi->free);
  1358. tree_destroy(&ubi->scrub);
  1359. kfree(ubi->lookuptbl);
  1360. return err;
  1361. }
  1362. /**
  1363. * protection_trees_destroy - destroy the protection RB-trees.
  1364. * @ubi: UBI device description object
  1365. */
  1366. static void protection_trees_destroy(struct ubi_device *ubi)
  1367. {
  1368. struct rb_node *rb;
  1369. struct ubi_wl_prot_entry *pe;
  1370. rb = ubi->prot.aec.rb_node;
  1371. while (rb) {
  1372. if (rb->rb_left)
  1373. rb = rb->rb_left;
  1374. else if (rb->rb_right)
  1375. rb = rb->rb_right;
  1376. else {
  1377. pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec);
  1378. rb = rb_parent(rb);
  1379. if (rb) {
  1380. if (rb->rb_left == &pe->rb_aec)
  1381. rb->rb_left = NULL;
  1382. else
  1383. rb->rb_right = NULL;
  1384. }
  1385. kmem_cache_free(ubi_wl_entry_slab, pe->e);
  1386. kfree(pe);
  1387. }
  1388. }
  1389. }
  1390. /**
  1391. * ubi_wl_close - close the wear-leveling unit.
  1392. * @ubi: UBI device description object
  1393. */
  1394. void ubi_wl_close(struct ubi_device *ubi)
  1395. {
  1396. dbg_wl("close the UBI wear-leveling unit");
  1397. cancel_pending(ubi);
  1398. protection_trees_destroy(ubi);
  1399. tree_destroy(&ubi->used);
  1400. tree_destroy(&ubi->free);
  1401. tree_destroy(&ubi->scrub);
  1402. kfree(ubi->lookuptbl);
  1403. }
  1404. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  1405. /**
  1406. * paranoid_check_ec - make sure that the erase counter of a physical eraseblock
  1407. * is correct.
  1408. * @ubi: UBI device description object
  1409. * @pnum: the physical eraseblock number to check
  1410. * @ec: the erase counter to check
  1411. *
  1412. * This function returns zero if the erase counter of physical eraseblock @pnum
  1413. * is equivalent to @ec, %1 if not, and a negative error code if an error
  1414. * occurred.
  1415. */
  1416. static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1417. {
  1418. int err;
  1419. long long read_ec;
  1420. struct ubi_ec_hdr *ec_hdr;
  1421. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1422. if (!ec_hdr)
  1423. return -ENOMEM;
  1424. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1425. if (err && err != UBI_IO_BITFLIPS) {
  1426. /* The header does not have to exist */
  1427. err = 0;
  1428. goto out_free;
  1429. }
  1430. read_ec = be64_to_cpu(ec_hdr->ec);
  1431. if (ec != read_ec) {
  1432. ubi_err("paranoid check failed for PEB %d", pnum);
  1433. ubi_err("read EC is %lld, should be %d", read_ec, ec);
  1434. ubi_dbg_dump_stack();
  1435. err = 1;
  1436. } else
  1437. err = 0;
  1438. out_free:
  1439. kfree(ec_hdr);
  1440. return err;
  1441. }
  1442. /**
  1443. * paranoid_check_in_wl_tree - make sure that a wear-leveling entry is present
  1444. * in a WL RB-tree.
  1445. * @e: the wear-leveling entry to check
  1446. * @root: the root of the tree
  1447. *
  1448. * This function returns zero if @e is in the @root RB-tree and %1 if it
  1449. * is not.
  1450. */
  1451. static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
  1452. struct rb_root *root)
  1453. {
  1454. if (in_wl_tree(e, root))
  1455. return 0;
  1456. ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
  1457. e->pnum, e->ec, root);
  1458. ubi_dbg_dump_stack();
  1459. return 1;
  1460. }
  1461. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */