wl.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) International Business Machines Corp., 2006
  4. *
  5. * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
  6. */
  7. /*
  8. * UBI wear-leveling sub-system.
  9. *
  10. * This sub-system is responsible for wear-leveling. It works in terms of
  11. * physical eraseblocks and erase counters and knows nothing about logical
  12. * eraseblocks, volumes, etc. From this sub-system's perspective all physical
  13. * eraseblocks are of two types - used and free. Used physical eraseblocks are
  14. * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
  15. * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
  16. *
  17. * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
  18. * header. The rest of the physical eraseblock contains only %0xFF bytes.
  19. *
  20. * When physical eraseblocks are returned to the WL sub-system by means of the
  21. * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
  22. * done asynchronously in context of the per-UBI device background thread,
  23. * which is also managed by the WL sub-system.
  24. *
  25. * The wear-leveling is ensured by means of moving the contents of used
  26. * physical eraseblocks with low erase counter to free physical eraseblocks
  27. * with high erase counter.
  28. *
  29. * If the WL sub-system fails to erase a physical eraseblock, it marks it as
  30. * bad.
  31. *
  32. * This sub-system is also responsible for scrubbing. If a bit-flip is detected
  33. * in a physical eraseblock, it has to be moved. Technically this is the same
  34. * as moving it for wear-leveling reasons.
  35. *
  36. * As it was said, for the UBI sub-system all physical eraseblocks are either
  37. * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
  38. * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
  39. * RB-trees, as well as (temporarily) in the @wl->pq queue.
  40. *
  41. * When the WL sub-system returns a physical eraseblock, the physical
  42. * eraseblock is protected from being moved for some "time". For this reason,
  43. * the physical eraseblock is not directly moved from the @wl->free tree to the
  44. * @wl->used tree. There is a protection queue in between where this
  45. * physical eraseblock is temporarily stored (@wl->pq).
  46. *
  47. * All this protection stuff is needed because:
  48. * o we don't want to move physical eraseblocks just after we have given them
  49. * to the user; instead, we first want to let users fill them up with data;
  50. *
  51. * o there is a chance that the user will put the physical eraseblock very
  52. * soon, so it makes sense not to move it for some time, but wait.
  53. *
  54. * Physical eraseblocks stay protected only for limited time. But the "time" is
  55. * measured in erase cycles in this case. This is implemented with help of the
  56. * protection queue. Eraseblocks are put to the tail of this queue when they
  57. * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
  58. * head of the queue on each erase operation (for any eraseblock). So the
  59. * length of the queue defines how may (global) erase cycles PEBs are protected.
  60. *
  61. * To put it differently, each physical eraseblock has 2 main states: free and
  62. * used. The former state corresponds to the @wl->free tree. The latter state
  63. * is split up on several sub-states:
  64. * o the WL movement is allowed (@wl->used tree);
  65. * o the WL movement is disallowed (@wl->erroneous) because the PEB is
  66. * erroneous - e.g., there was a read error;
  67. * o the WL movement is temporarily prohibited (@wl->pq queue);
  68. * o scrubbing is needed (@wl->scrub tree).
  69. *
  70. * Depending on the sub-state, wear-leveling entries of the used physical
  71. * eraseblocks may be kept in one of those structures.
  72. *
  73. * Note, in this implementation, we keep a small in-RAM object for each physical
  74. * eraseblock. This is surely not a scalable solution. But it appears to be good
  75. * enough for moderately large flashes and it is simple. In future, one may
  76. * re-work this sub-system and make it more scalable.
  77. *
  78. * At the moment this sub-system does not utilize the sequence number, which
  79. * was introduced relatively recently. But it would be wise to do this because
  80. * the sequence number of a logical eraseblock characterizes how old is it. For
  81. * example, when we move a PEB with low erase counter, and we need to pick the
  82. * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
  83. * pick target PEB with an average EC if our PEB is not very "old". This is a
  84. * room for future re-works of the WL sub-system.
  85. */
  86. #include <linux/slab.h>
  87. #include <linux/crc32.h>
  88. #include <linux/freezer.h>
  89. #include <linux/kthread.h>
  90. #include "ubi.h"
  91. #include "wl.h"
  92. /* Number of physical eraseblocks reserved for wear-leveling purposes */
  93. #define WL_RESERVED_PEBS 1
  94. /*
  95. * Maximum difference between two erase counters. If this threshold is
  96. * exceeded, the WL sub-system starts moving data from used physical
  97. * eraseblocks with low erase counter to free physical eraseblocks with high
  98. * erase counter.
  99. */
  100. #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
  101. /*
  102. * When a physical eraseblock is moved, the WL sub-system has to pick the target
  103. * physical eraseblock to move to. The simplest way would be just to pick the
  104. * one with the highest erase counter. But in certain workloads this could lead
  105. * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
  106. * situation when the picked physical eraseblock is constantly erased after the
  107. * data is written to it. So, we have a constant which limits the highest erase
  108. * counter of the free physical eraseblock to pick. Namely, the WL sub-system
  109. * does not pick eraseblocks with erase counter greater than the lowest erase
  110. * counter plus %WL_FREE_MAX_DIFF.
  111. */
  112. #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
  113. /*
  114. * Maximum number of consecutive background thread failures which is enough to
  115. * switch to read-only mode.
  116. */
  117. #define WL_MAX_FAILURES 32
  118. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
  119. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  120. struct ubi_wl_entry *e, struct rb_root *root);
  121. static int self_check_in_pq(const struct ubi_device *ubi,
  122. struct ubi_wl_entry *e);
  123. /**
  124. * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
  125. * @e: the wear-leveling entry to add
  126. * @root: the root of the tree
  127. *
  128. * Note, we use (erase counter, physical eraseblock number) pairs as keys in
  129. * the @ubi->used and @ubi->free RB-trees.
  130. */
  131. static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
  132. {
  133. struct rb_node **p, *parent = NULL;
  134. p = &root->rb_node;
  135. while (*p) {
  136. struct ubi_wl_entry *e1;
  137. parent = *p;
  138. e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
  139. if (e->ec < e1->ec)
  140. p = &(*p)->rb_left;
  141. else if (e->ec > e1->ec)
  142. p = &(*p)->rb_right;
  143. else {
  144. ubi_assert(e->pnum != e1->pnum);
  145. if (e->pnum < e1->pnum)
  146. p = &(*p)->rb_left;
  147. else
  148. p = &(*p)->rb_right;
  149. }
  150. }
  151. rb_link_node(&e->u.rb, parent, p);
  152. rb_insert_color(&e->u.rb, root);
  153. }
  154. /**
  155. * wl_tree_destroy - destroy a wear-leveling entry.
  156. * @ubi: UBI device description object
  157. * @e: the wear-leveling entry to add
  158. *
  159. * This function destroys a wear leveling entry and removes
  160. * the reference from the lookup table.
  161. */
  162. static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e)
  163. {
  164. ubi->lookuptbl[e->pnum] = NULL;
  165. kmem_cache_free(ubi_wl_entry_slab, e);
  166. }
  167. /**
  168. * do_work - do one pending work.
  169. * @ubi: UBI device description object
  170. *
  171. * This function returns zero in case of success and a negative error code in
  172. * case of failure.
  173. */
  174. static int do_work(struct ubi_device *ubi)
  175. {
  176. int err;
  177. struct ubi_work *wrk;
  178. cond_resched();
  179. /*
  180. * @ubi->work_sem is used to synchronize with the workers. Workers take
  181. * it in read mode, so many of them may be doing works at a time. But
  182. * the queue flush code has to be sure the whole queue of works is
  183. * done, and it takes the mutex in write mode.
  184. */
  185. down_read(&ubi->work_sem);
  186. spin_lock(&ubi->wl_lock);
  187. if (list_empty(&ubi->works)) {
  188. spin_unlock(&ubi->wl_lock);
  189. up_read(&ubi->work_sem);
  190. return 0;
  191. }
  192. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  193. list_del(&wrk->list);
  194. ubi->works_count -= 1;
  195. ubi_assert(ubi->works_count >= 0);
  196. spin_unlock(&ubi->wl_lock);
  197. /*
  198. * Call the worker function. Do not touch the work structure
  199. * after this call as it will have been freed or reused by that
  200. * time by the worker function.
  201. */
  202. err = wrk->func(ubi, wrk, 0);
  203. if (err)
  204. ubi_err(ubi, "work failed with error code %d", err);
  205. up_read(&ubi->work_sem);
  206. return err;
  207. }
  208. /**
  209. * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
  210. * @e: the wear-leveling entry to check
  211. * @root: the root of the tree
  212. *
  213. * This function returns non-zero if @e is in the @root RB-tree and zero if it
  214. * is not.
  215. */
  216. static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
  217. {
  218. struct rb_node *p;
  219. p = root->rb_node;
  220. while (p) {
  221. struct ubi_wl_entry *e1;
  222. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  223. if (e->pnum == e1->pnum) {
  224. ubi_assert(e == e1);
  225. return 1;
  226. }
  227. if (e->ec < e1->ec)
  228. p = p->rb_left;
  229. else if (e->ec > e1->ec)
  230. p = p->rb_right;
  231. else {
  232. ubi_assert(e->pnum != e1->pnum);
  233. if (e->pnum < e1->pnum)
  234. p = p->rb_left;
  235. else
  236. p = p->rb_right;
  237. }
  238. }
  239. return 0;
  240. }
  241. /**
  242. * in_pq - check if a wear-leveling entry is present in the protection queue.
  243. * @ubi: UBI device description object
  244. * @e: the wear-leveling entry to check
  245. *
  246. * This function returns non-zero if @e is in the protection queue and zero
  247. * if it is not.
  248. */
  249. static inline int in_pq(const struct ubi_device *ubi, struct ubi_wl_entry *e)
  250. {
  251. struct ubi_wl_entry *p;
  252. int i;
  253. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  254. list_for_each_entry(p, &ubi->pq[i], u.list)
  255. if (p == e)
  256. return 1;
  257. return 0;
  258. }
  259. /**
  260. * prot_queue_add - add physical eraseblock to the protection queue.
  261. * @ubi: UBI device description object
  262. * @e: the physical eraseblock to add
  263. *
  264. * This function adds @e to the tail of the protection queue @ubi->pq, where
  265. * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
  266. * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
  267. * be locked.
  268. */
  269. static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
  270. {
  271. int pq_tail = ubi->pq_head - 1;
  272. if (pq_tail < 0)
  273. pq_tail = UBI_PROT_QUEUE_LEN - 1;
  274. ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
  275. list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
  276. dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
  277. }
  278. /**
  279. * find_wl_entry - find wear-leveling entry closest to certain erase counter.
  280. * @ubi: UBI device description object
  281. * @root: the RB-tree where to look for
  282. * @diff: maximum possible difference from the smallest erase counter
  283. *
  284. * This function looks for a wear leveling entry with erase counter closest to
  285. * min + @diff, where min is the smallest erase counter.
  286. */
  287. static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
  288. struct rb_root *root, int diff)
  289. {
  290. struct rb_node *p;
  291. struct ubi_wl_entry *e;
  292. int max;
  293. e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  294. max = e->ec + diff;
  295. p = root->rb_node;
  296. while (p) {
  297. struct ubi_wl_entry *e1;
  298. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  299. if (e1->ec >= max)
  300. p = p->rb_left;
  301. else {
  302. p = p->rb_right;
  303. e = e1;
  304. }
  305. }
  306. return e;
  307. }
  308. /**
  309. * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
  310. * @ubi: UBI device description object
  311. * @root: the RB-tree where to look for
  312. *
  313. * This function looks for a wear leveling entry with medium erase counter,
  314. * but not greater or equivalent than the lowest erase counter plus
  315. * %WL_FREE_MAX_DIFF/2.
  316. */
  317. static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
  318. struct rb_root *root)
  319. {
  320. struct ubi_wl_entry *e, *first, *last;
  321. first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  322. last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
  323. if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
  324. e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
  325. /* If no fastmap has been written and this WL entry can be used
  326. * as anchor PEB, hold it back and return the second best
  327. * WL entry such that fastmap can use the anchor PEB later. */
  328. e = may_reserve_for_fm(ubi, e, root);
  329. } else
  330. e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
  331. return e;
  332. }
  333. /**
  334. * wl_get_wle - get a mean wl entry to be used by ubi_wl_get_peb() or
  335. * refill_wl_user_pool().
  336. * @ubi: UBI device description object
  337. *
  338. * This function returns a wear leveling entry in case of success and
  339. * NULL in case of failure.
  340. */
  341. static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi)
  342. {
  343. struct ubi_wl_entry *e;
  344. e = find_mean_wl_entry(ubi, &ubi->free);
  345. if (!e) {
  346. ubi_err(ubi, "no free eraseblocks");
  347. return NULL;
  348. }
  349. self_check_in_wl_tree(ubi, e, &ubi->free);
  350. /*
  351. * Move the physical eraseblock to the protection queue where it will
  352. * be protected from being moved for some time.
  353. */
  354. rb_erase(&e->u.rb, &ubi->free);
  355. ubi->free_count--;
  356. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  357. return e;
  358. }
  359. /**
  360. * prot_queue_del - remove a physical eraseblock from the protection queue.
  361. * @ubi: UBI device description object
  362. * @pnum: the physical eraseblock to remove
  363. *
  364. * This function deletes PEB @pnum from the protection queue and returns zero
  365. * in case of success and %-ENODEV if the PEB was not found.
  366. */
  367. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  368. {
  369. struct ubi_wl_entry *e;
  370. e = ubi->lookuptbl[pnum];
  371. if (!e)
  372. return -ENODEV;
  373. if (self_check_in_pq(ubi, e))
  374. return -ENODEV;
  375. list_del(&e->u.list);
  376. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  377. return 0;
  378. }
  379. /**
  380. * sync_erase - synchronously erase a physical eraseblock.
  381. * @ubi: UBI device description object
  382. * @e: the physical eraseblock to erase
  383. * @torture: if the physical eraseblock has to be tortured
  384. *
  385. * This function returns zero in case of success and a negative error code in
  386. * case of failure.
  387. */
  388. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  389. int torture)
  390. {
  391. int err;
  392. struct ubi_ec_hdr *ec_hdr;
  393. unsigned long long ec = e->ec;
  394. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  395. err = self_check_ec(ubi, e->pnum, e->ec);
  396. if (err)
  397. return -EINVAL;
  398. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  399. if (!ec_hdr)
  400. return -ENOMEM;
  401. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  402. if (err < 0)
  403. goto out_free;
  404. ec += err;
  405. if (ec > UBI_MAX_ERASECOUNTER) {
  406. /*
  407. * Erase counter overflow. Upgrade UBI and use 64-bit
  408. * erase counters internally.
  409. */
  410. ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
  411. e->pnum, ec);
  412. err = -EINVAL;
  413. goto out_free;
  414. }
  415. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  416. ec_hdr->ec = cpu_to_be64(ec);
  417. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  418. if (err)
  419. goto out_free;
  420. e->ec = ec;
  421. spin_lock(&ubi->wl_lock);
  422. if (e->ec > ubi->max_ec)
  423. ubi->max_ec = e->ec;
  424. spin_unlock(&ubi->wl_lock);
  425. out_free:
  426. kfree(ec_hdr);
  427. return err;
  428. }
  429. /**
  430. * serve_prot_queue - check if it is time to stop protecting PEBs.
  431. * @ubi: UBI device description object
  432. *
  433. * This function is called after each erase operation and removes PEBs from the
  434. * tail of the protection queue. These PEBs have been protected for long enough
  435. * and should be moved to the used tree.
  436. */
  437. static void serve_prot_queue(struct ubi_device *ubi)
  438. {
  439. struct ubi_wl_entry *e, *tmp;
  440. int count;
  441. /*
  442. * There may be several protected physical eraseblock to remove,
  443. * process them all.
  444. */
  445. repeat:
  446. count = 0;
  447. spin_lock(&ubi->wl_lock);
  448. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  449. dbg_wl("PEB %d EC %d protection over, move to used tree",
  450. e->pnum, e->ec);
  451. list_del(&e->u.list);
  452. wl_tree_add(e, &ubi->used);
  453. if (count++ > 32) {
  454. /*
  455. * Let's be nice and avoid holding the spinlock for
  456. * too long.
  457. */
  458. spin_unlock(&ubi->wl_lock);
  459. cond_resched();
  460. goto repeat;
  461. }
  462. }
  463. ubi->pq_head += 1;
  464. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  465. ubi->pq_head = 0;
  466. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  467. spin_unlock(&ubi->wl_lock);
  468. }
  469. /**
  470. * __schedule_ubi_work - schedule a work.
  471. * @ubi: UBI device description object
  472. * @wrk: the work to schedule
  473. *
  474. * This function adds a work defined by @wrk to the tail of the pending works
  475. * list. Can only be used if ubi->work_sem is already held in read mode!
  476. */
  477. static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  478. {
  479. spin_lock(&ubi->wl_lock);
  480. list_add_tail(&wrk->list, &ubi->works);
  481. ubi_assert(ubi->works_count >= 0);
  482. ubi->works_count += 1;
  483. if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
  484. wake_up_process(ubi->bgt_thread);
  485. spin_unlock(&ubi->wl_lock);
  486. }
  487. /**
  488. * schedule_ubi_work - schedule a work.
  489. * @ubi: UBI device description object
  490. * @wrk: the work to schedule
  491. *
  492. * This function adds a work defined by @wrk to the tail of the pending works
  493. * list.
  494. */
  495. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  496. {
  497. down_read(&ubi->work_sem);
  498. __schedule_ubi_work(ubi, wrk);
  499. up_read(&ubi->work_sem);
  500. }
  501. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  502. int shutdown);
  503. /**
  504. * schedule_erase - schedule an erase work.
  505. * @ubi: UBI device description object
  506. * @e: the WL entry of the physical eraseblock to erase
  507. * @vol_id: the volume ID that last used this PEB
  508. * @lnum: the last used logical eraseblock number for the PEB
  509. * @torture: if the physical eraseblock has to be tortured
  510. * @nested: denotes whether the work_sem is already held
  511. *
  512. * This function returns zero in case of success and a %-ENOMEM in case of
  513. * failure.
  514. */
  515. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  516. int vol_id, int lnum, int torture, bool nested)
  517. {
  518. struct ubi_work *wl_wrk;
  519. ubi_assert(e);
  520. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  521. e->pnum, e->ec, torture);
  522. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  523. if (!wl_wrk)
  524. return -ENOMEM;
  525. wl_wrk->func = &erase_worker;
  526. wl_wrk->e = e;
  527. wl_wrk->vol_id = vol_id;
  528. wl_wrk->lnum = lnum;
  529. wl_wrk->torture = torture;
  530. if (nested)
  531. __schedule_ubi_work(ubi, wl_wrk);
  532. else
  533. schedule_ubi_work(ubi, wl_wrk);
  534. return 0;
  535. }
  536. static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk);
  537. /**
  538. * do_sync_erase - run the erase worker synchronously.
  539. * @ubi: UBI device description object
  540. * @e: the WL entry of the physical eraseblock to erase
  541. * @vol_id: the volume ID that last used this PEB
  542. * @lnum: the last used logical eraseblock number for the PEB
  543. * @torture: if the physical eraseblock has to be tortured
  544. *
  545. */
  546. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  547. int vol_id, int lnum, int torture)
  548. {
  549. struct ubi_work wl_wrk;
  550. dbg_wl("sync erase of PEB %i", e->pnum);
  551. wl_wrk.e = e;
  552. wl_wrk.vol_id = vol_id;
  553. wl_wrk.lnum = lnum;
  554. wl_wrk.torture = torture;
  555. return __erase_worker(ubi, &wl_wrk);
  556. }
  557. static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
  558. /**
  559. * wear_leveling_worker - wear-leveling worker function.
  560. * @ubi: UBI device description object
  561. * @wrk: the work object
  562. * @shutdown: non-zero if the worker has to free memory and exit
  563. * because the WL-subsystem is shutting down
  564. *
  565. * This function copies a more worn out physical eraseblock to a less worn out
  566. * one. Returns zero in case of success and a negative error code in case of
  567. * failure.
  568. */
  569. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  570. int shutdown)
  571. {
  572. int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  573. int erase = 0, keep = 0, vol_id = -1, lnum = -1;
  574. struct ubi_wl_entry *e1, *e2;
  575. struct ubi_vid_io_buf *vidb;
  576. struct ubi_vid_hdr *vid_hdr;
  577. int dst_leb_clean = 0;
  578. kfree(wrk);
  579. if (shutdown)
  580. return 0;
  581. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  582. if (!vidb)
  583. return -ENOMEM;
  584. vid_hdr = ubi_get_vid_hdr(vidb);
  585. down_read(&ubi->fm_eba_sem);
  586. mutex_lock(&ubi->move_mutex);
  587. spin_lock(&ubi->wl_lock);
  588. ubi_assert(!ubi->move_from && !ubi->move_to);
  589. ubi_assert(!ubi->move_to_put);
  590. #ifdef CONFIG_MTD_UBI_FASTMAP
  591. if (!next_peb_for_wl(ubi) ||
  592. #else
  593. if (!ubi->free.rb_node ||
  594. #endif
  595. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  596. /*
  597. * No free physical eraseblocks? Well, they must be waiting in
  598. * the queue to be erased. Cancel movement - it will be
  599. * triggered again when a free physical eraseblock appears.
  600. *
  601. * No used physical eraseblocks? They must be temporarily
  602. * protected from being moved. They will be moved to the
  603. * @ubi->used tree later and the wear-leveling will be
  604. * triggered again.
  605. */
  606. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  607. !ubi->free.rb_node, !ubi->used.rb_node);
  608. goto out_cancel;
  609. }
  610. #ifdef CONFIG_MTD_UBI_FASTMAP
  611. e1 = find_anchor_wl_entry(&ubi->used);
  612. if (e1 && ubi->fm_anchor &&
  613. (ubi->fm_anchor->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  614. ubi->fm_do_produce_anchor = 1;
  615. /*
  616. * fm_anchor is no longer considered a good anchor.
  617. * NULL assignment also prevents multiple wear level checks
  618. * of this PEB.
  619. */
  620. wl_tree_add(ubi->fm_anchor, &ubi->free);
  621. ubi->fm_anchor = NULL;
  622. ubi->free_count++;
  623. }
  624. if (ubi->fm_do_produce_anchor) {
  625. if (!e1)
  626. goto out_cancel;
  627. e2 = get_peb_for_wl(ubi);
  628. if (!e2)
  629. goto out_cancel;
  630. self_check_in_wl_tree(ubi, e1, &ubi->used);
  631. rb_erase(&e1->u.rb, &ubi->used);
  632. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  633. ubi->fm_do_produce_anchor = 0;
  634. } else if (!ubi->scrub.rb_node) {
  635. #else
  636. if (!ubi->scrub.rb_node) {
  637. #endif
  638. /*
  639. * Now pick the least worn-out used physical eraseblock and a
  640. * highly worn-out free physical eraseblock. If the erase
  641. * counters differ much enough, start wear-leveling.
  642. */
  643. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  644. e2 = get_peb_for_wl(ubi);
  645. if (!e2)
  646. goto out_cancel;
  647. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  648. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  649. e1->ec, e2->ec);
  650. /* Give the unused PEB back */
  651. wl_tree_add(e2, &ubi->free);
  652. ubi->free_count++;
  653. goto out_cancel;
  654. }
  655. self_check_in_wl_tree(ubi, e1, &ubi->used);
  656. rb_erase(&e1->u.rb, &ubi->used);
  657. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  658. e1->pnum, e1->ec, e2->pnum, e2->ec);
  659. } else {
  660. /* Perform scrubbing */
  661. scrubbing = 1;
  662. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  663. e2 = get_peb_for_wl(ubi);
  664. if (!e2)
  665. goto out_cancel;
  666. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  667. rb_erase(&e1->u.rb, &ubi->scrub);
  668. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  669. }
  670. ubi->move_from = e1;
  671. ubi->move_to = e2;
  672. spin_unlock(&ubi->wl_lock);
  673. /*
  674. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  675. * We so far do not know which logical eraseblock our physical
  676. * eraseblock (@e1) belongs to. We have to read the volume identifier
  677. * header first.
  678. *
  679. * Note, we are protected from this PEB being unmapped and erased. The
  680. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  681. * which is being moved was unmapped.
  682. */
  683. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vidb, 0);
  684. if (err && err != UBI_IO_BITFLIPS) {
  685. dst_leb_clean = 1;
  686. if (err == UBI_IO_FF) {
  687. /*
  688. * We are trying to move PEB without a VID header. UBI
  689. * always write VID headers shortly after the PEB was
  690. * given, so we have a situation when it has not yet
  691. * had a chance to write it, because it was preempted.
  692. * So add this PEB to the protection queue so far,
  693. * because presumably more data will be written there
  694. * (including the missing VID header), and then we'll
  695. * move it.
  696. */
  697. dbg_wl("PEB %d has no VID header", e1->pnum);
  698. protect = 1;
  699. goto out_not_moved;
  700. } else if (err == UBI_IO_FF_BITFLIPS) {
  701. /*
  702. * The same situation as %UBI_IO_FF, but bit-flips were
  703. * detected. It is better to schedule this PEB for
  704. * scrubbing.
  705. */
  706. dbg_wl("PEB %d has no VID header but has bit-flips",
  707. e1->pnum);
  708. scrubbing = 1;
  709. goto out_not_moved;
  710. } else if (ubi->fast_attach && err == UBI_IO_BAD_HDR_EBADMSG) {
  711. /*
  712. * While a full scan would detect interrupted erasures
  713. * at attach time we can face them here when attached from
  714. * Fastmap.
  715. */
  716. dbg_wl("PEB %d has ECC errors, maybe from an interrupted erasure",
  717. e1->pnum);
  718. erase = 1;
  719. goto out_not_moved;
  720. }
  721. ubi_err(ubi, "error %d while reading VID header from PEB %d",
  722. err, e1->pnum);
  723. goto out_error;
  724. }
  725. vol_id = be32_to_cpu(vid_hdr->vol_id);
  726. lnum = be32_to_cpu(vid_hdr->lnum);
  727. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vidb);
  728. if (err) {
  729. if (err == MOVE_CANCEL_RACE) {
  730. /*
  731. * The LEB has not been moved because the volume is
  732. * being deleted or the PEB has been put meanwhile. We
  733. * should prevent this PEB from being selected for
  734. * wear-leveling movement again, so put it to the
  735. * protection queue.
  736. */
  737. protect = 1;
  738. dst_leb_clean = 1;
  739. goto out_not_moved;
  740. }
  741. if (err == MOVE_RETRY) {
  742. scrubbing = 1;
  743. dst_leb_clean = 1;
  744. goto out_not_moved;
  745. }
  746. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  747. err == MOVE_TARGET_RD_ERR) {
  748. /*
  749. * Target PEB had bit-flips or write error - torture it.
  750. */
  751. torture = 1;
  752. keep = 1;
  753. goto out_not_moved;
  754. }
  755. if (err == MOVE_SOURCE_RD_ERR) {
  756. /*
  757. * An error happened while reading the source PEB. Do
  758. * not switch to R/O mode in this case, and give the
  759. * upper layers a possibility to recover from this,
  760. * e.g. by unmapping corresponding LEB. Instead, just
  761. * put this PEB to the @ubi->erroneous list to prevent
  762. * UBI from trying to move it over and over again.
  763. */
  764. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  765. ubi_err(ubi, "too many erroneous eraseblocks (%d)",
  766. ubi->erroneous_peb_count);
  767. goto out_error;
  768. }
  769. dst_leb_clean = 1;
  770. erroneous = 1;
  771. goto out_not_moved;
  772. }
  773. if (err < 0)
  774. goto out_error;
  775. ubi_assert(0);
  776. }
  777. /* The PEB has been successfully moved */
  778. if (scrubbing)
  779. ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  780. e1->pnum, vol_id, lnum, e2->pnum);
  781. ubi_free_vid_buf(vidb);
  782. spin_lock(&ubi->wl_lock);
  783. if (!ubi->move_to_put) {
  784. wl_tree_add(e2, &ubi->used);
  785. e2 = NULL;
  786. }
  787. ubi->move_from = ubi->move_to = NULL;
  788. ubi->move_to_put = ubi->wl_scheduled = 0;
  789. spin_unlock(&ubi->wl_lock);
  790. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  791. if (err) {
  792. if (e2) {
  793. spin_lock(&ubi->wl_lock);
  794. wl_entry_destroy(ubi, e2);
  795. spin_unlock(&ubi->wl_lock);
  796. }
  797. goto out_ro;
  798. }
  799. if (e2) {
  800. /*
  801. * Well, the target PEB was put meanwhile, schedule it for
  802. * erasure.
  803. */
  804. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  805. e2->pnum, vol_id, lnum);
  806. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  807. if (err)
  808. goto out_ro;
  809. }
  810. dbg_wl("done");
  811. mutex_unlock(&ubi->move_mutex);
  812. up_read(&ubi->fm_eba_sem);
  813. return 0;
  814. /*
  815. * For some reasons the LEB was not moved, might be an error, might be
  816. * something else. @e1 was not changed, so return it back. @e2 might
  817. * have been changed, schedule it for erasure.
  818. */
  819. out_not_moved:
  820. if (vol_id != -1)
  821. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  822. e1->pnum, vol_id, lnum, e2->pnum, err);
  823. else
  824. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  825. e1->pnum, e2->pnum, err);
  826. spin_lock(&ubi->wl_lock);
  827. if (protect)
  828. prot_queue_add(ubi, e1);
  829. else if (erroneous) {
  830. wl_tree_add(e1, &ubi->erroneous);
  831. ubi->erroneous_peb_count += 1;
  832. } else if (scrubbing)
  833. wl_tree_add(e1, &ubi->scrub);
  834. else if (keep)
  835. wl_tree_add(e1, &ubi->used);
  836. if (dst_leb_clean) {
  837. wl_tree_add(e2, &ubi->free);
  838. ubi->free_count++;
  839. }
  840. ubi_assert(!ubi->move_to_put);
  841. ubi->move_from = ubi->move_to = NULL;
  842. ubi->wl_scheduled = 0;
  843. spin_unlock(&ubi->wl_lock);
  844. ubi_free_vid_buf(vidb);
  845. if (dst_leb_clean) {
  846. ensure_wear_leveling(ubi, 1);
  847. } else {
  848. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  849. if (err)
  850. goto out_ro;
  851. }
  852. if (erase) {
  853. err = do_sync_erase(ubi, e1, vol_id, lnum, 1);
  854. if (err)
  855. goto out_ro;
  856. }
  857. mutex_unlock(&ubi->move_mutex);
  858. up_read(&ubi->fm_eba_sem);
  859. return 0;
  860. out_error:
  861. if (vol_id != -1)
  862. ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
  863. err, e1->pnum, e2->pnum);
  864. else
  865. ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  866. err, e1->pnum, vol_id, lnum, e2->pnum);
  867. spin_lock(&ubi->wl_lock);
  868. ubi->move_from = ubi->move_to = NULL;
  869. ubi->move_to_put = ubi->wl_scheduled = 0;
  870. wl_entry_destroy(ubi, e1);
  871. wl_entry_destroy(ubi, e2);
  872. spin_unlock(&ubi->wl_lock);
  873. ubi_free_vid_buf(vidb);
  874. out_ro:
  875. ubi_ro_mode(ubi);
  876. mutex_unlock(&ubi->move_mutex);
  877. up_read(&ubi->fm_eba_sem);
  878. ubi_assert(err != 0);
  879. return err < 0 ? err : -EIO;
  880. out_cancel:
  881. ubi->wl_scheduled = 0;
  882. spin_unlock(&ubi->wl_lock);
  883. mutex_unlock(&ubi->move_mutex);
  884. up_read(&ubi->fm_eba_sem);
  885. ubi_free_vid_buf(vidb);
  886. return 0;
  887. }
  888. /**
  889. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  890. * @ubi: UBI device description object
  891. * @nested: set to non-zero if this function is called from UBI worker
  892. *
  893. * This function checks if it is time to start wear-leveling and schedules it
  894. * if yes. This function returns zero in case of success and a negative error
  895. * code in case of failure.
  896. */
  897. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  898. {
  899. int err = 0;
  900. struct ubi_work *wrk;
  901. spin_lock(&ubi->wl_lock);
  902. if (ubi->wl_scheduled)
  903. /* Wear-leveling is already in the work queue */
  904. goto out_unlock;
  905. /*
  906. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  907. * WL worker has to be scheduled anyway.
  908. */
  909. if (!ubi->scrub.rb_node) {
  910. #ifdef CONFIG_MTD_UBI_FASTMAP
  911. if (!need_wear_leveling(ubi))
  912. goto out_unlock;
  913. #else
  914. struct ubi_wl_entry *e1;
  915. struct ubi_wl_entry *e2;
  916. if (!ubi->used.rb_node || !ubi->free.rb_node)
  917. /* No physical eraseblocks - no deal */
  918. goto out_unlock;
  919. /*
  920. * We schedule wear-leveling only if the difference between the
  921. * lowest erase counter of used physical eraseblocks and a high
  922. * erase counter of free physical eraseblocks is greater than
  923. * %UBI_WL_THRESHOLD.
  924. */
  925. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  926. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  927. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  928. goto out_unlock;
  929. #endif
  930. dbg_wl("schedule wear-leveling");
  931. } else
  932. dbg_wl("schedule scrubbing");
  933. ubi->wl_scheduled = 1;
  934. spin_unlock(&ubi->wl_lock);
  935. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  936. if (!wrk) {
  937. err = -ENOMEM;
  938. goto out_cancel;
  939. }
  940. wrk->func = &wear_leveling_worker;
  941. if (nested)
  942. __schedule_ubi_work(ubi, wrk);
  943. else
  944. schedule_ubi_work(ubi, wrk);
  945. return err;
  946. out_cancel:
  947. spin_lock(&ubi->wl_lock);
  948. ubi->wl_scheduled = 0;
  949. out_unlock:
  950. spin_unlock(&ubi->wl_lock);
  951. return err;
  952. }
  953. /**
  954. * __erase_worker - physical eraseblock erase worker function.
  955. * @ubi: UBI device description object
  956. * @wl_wrk: the work object
  957. *
  958. * This function erases a physical eraseblock and perform torture testing if
  959. * needed. It also takes care about marking the physical eraseblock bad if
  960. * needed. Returns zero in case of success and a negative error code in case of
  961. * failure.
  962. */
  963. static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
  964. {
  965. struct ubi_wl_entry *e = wl_wrk->e;
  966. int pnum = e->pnum;
  967. int vol_id = wl_wrk->vol_id;
  968. int lnum = wl_wrk->lnum;
  969. int err, available_consumed = 0;
  970. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  971. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  972. err = sync_erase(ubi, e, wl_wrk->torture);
  973. if (!err) {
  974. spin_lock(&ubi->wl_lock);
  975. if (!ubi->fm_disabled && !ubi->fm_anchor &&
  976. e->pnum < UBI_FM_MAX_START) {
  977. /*
  978. * Abort anchor production, if needed it will be
  979. * enabled again in the wear leveling started below.
  980. */
  981. ubi->fm_anchor = e;
  982. ubi->fm_do_produce_anchor = 0;
  983. } else {
  984. wl_tree_add(e, &ubi->free);
  985. ubi->free_count++;
  986. }
  987. spin_unlock(&ubi->wl_lock);
  988. /*
  989. * One more erase operation has happened, take care about
  990. * protected physical eraseblocks.
  991. */
  992. serve_prot_queue(ubi);
  993. /* And take care about wear-leveling */
  994. err = ensure_wear_leveling(ubi, 1);
  995. return err;
  996. }
  997. ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
  998. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  999. err == -EBUSY) {
  1000. int err1;
  1001. /* Re-schedule the LEB for erasure */
  1002. err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true);
  1003. if (err1) {
  1004. spin_lock(&ubi->wl_lock);
  1005. wl_entry_destroy(ubi, e);
  1006. spin_unlock(&ubi->wl_lock);
  1007. err = err1;
  1008. goto out_ro;
  1009. }
  1010. return err;
  1011. }
  1012. spin_lock(&ubi->wl_lock);
  1013. wl_entry_destroy(ubi, e);
  1014. spin_unlock(&ubi->wl_lock);
  1015. if (err != -EIO)
  1016. /*
  1017. * If this is not %-EIO, we have no idea what to do. Scheduling
  1018. * this physical eraseblock for erasure again would cause
  1019. * errors again and again. Well, lets switch to R/O mode.
  1020. */
  1021. goto out_ro;
  1022. /* It is %-EIO, the PEB went bad */
  1023. if (!ubi->bad_allowed) {
  1024. ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
  1025. goto out_ro;
  1026. }
  1027. spin_lock(&ubi->volumes_lock);
  1028. if (ubi->beb_rsvd_pebs == 0) {
  1029. if (ubi->avail_pebs == 0) {
  1030. spin_unlock(&ubi->volumes_lock);
  1031. ubi_err(ubi, "no reserved/available physical eraseblocks");
  1032. goto out_ro;
  1033. }
  1034. ubi->avail_pebs -= 1;
  1035. available_consumed = 1;
  1036. }
  1037. spin_unlock(&ubi->volumes_lock);
  1038. ubi_msg(ubi, "mark PEB %d as bad", pnum);
  1039. err = ubi_io_mark_bad(ubi, pnum);
  1040. if (err)
  1041. goto out_ro;
  1042. spin_lock(&ubi->volumes_lock);
  1043. if (ubi->beb_rsvd_pebs > 0) {
  1044. if (available_consumed) {
  1045. /*
  1046. * The amount of reserved PEBs increased since we last
  1047. * checked.
  1048. */
  1049. ubi->avail_pebs += 1;
  1050. available_consumed = 0;
  1051. }
  1052. ubi->beb_rsvd_pebs -= 1;
  1053. }
  1054. ubi->bad_peb_count += 1;
  1055. ubi->good_peb_count -= 1;
  1056. ubi_calculate_reserved(ubi);
  1057. if (available_consumed)
  1058. ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
  1059. else if (ubi->beb_rsvd_pebs)
  1060. ubi_msg(ubi, "%d PEBs left in the reserve",
  1061. ubi->beb_rsvd_pebs);
  1062. else
  1063. ubi_warn(ubi, "last PEB from the reserve was used");
  1064. spin_unlock(&ubi->volumes_lock);
  1065. return err;
  1066. out_ro:
  1067. if (available_consumed) {
  1068. spin_lock(&ubi->volumes_lock);
  1069. ubi->avail_pebs += 1;
  1070. spin_unlock(&ubi->volumes_lock);
  1071. }
  1072. ubi_ro_mode(ubi);
  1073. return err;
  1074. }
  1075. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1076. int shutdown)
  1077. {
  1078. int ret;
  1079. if (shutdown) {
  1080. struct ubi_wl_entry *e = wl_wrk->e;
  1081. dbg_wl("cancel erasure of PEB %d EC %d", e->pnum, e->ec);
  1082. kfree(wl_wrk);
  1083. wl_entry_destroy(ubi, e);
  1084. return 0;
  1085. }
  1086. ret = __erase_worker(ubi, wl_wrk);
  1087. kfree(wl_wrk);
  1088. return ret;
  1089. }
  1090. /**
  1091. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1092. * @ubi: UBI device description object
  1093. * @vol_id: the volume ID that last used this PEB
  1094. * @lnum: the last used logical eraseblock number for the PEB
  1095. * @pnum: physical eraseblock to return
  1096. * @torture: if this physical eraseblock has to be tortured
  1097. *
  1098. * This function is called to return physical eraseblock @pnum to the pool of
  1099. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1100. * occurred to this @pnum and it has to be tested. This function returns zero
  1101. * in case of success, and a negative error code in case of failure.
  1102. */
  1103. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1104. int pnum, int torture)
  1105. {
  1106. int err;
  1107. struct ubi_wl_entry *e;
  1108. dbg_wl("PEB %d", pnum);
  1109. ubi_assert(pnum >= 0);
  1110. ubi_assert(pnum < ubi->peb_count);
  1111. down_read(&ubi->fm_protect);
  1112. retry:
  1113. spin_lock(&ubi->wl_lock);
  1114. e = ubi->lookuptbl[pnum];
  1115. if (!e) {
  1116. /*
  1117. * This wl entry has been removed for some errors by other
  1118. * process (eg. wear leveling worker), corresponding process
  1119. * (except __erase_worker, which cannot concurrent with
  1120. * ubi_wl_put_peb) will set ubi ro_mode at the same time,
  1121. * just ignore this wl entry.
  1122. */
  1123. spin_unlock(&ubi->wl_lock);
  1124. up_read(&ubi->fm_protect);
  1125. return 0;
  1126. }
  1127. if (e == ubi->move_from) {
  1128. /*
  1129. * User is putting the physical eraseblock which was selected to
  1130. * be moved. It will be scheduled for erasure in the
  1131. * wear-leveling worker.
  1132. */
  1133. dbg_wl("PEB %d is being moved, wait", pnum);
  1134. spin_unlock(&ubi->wl_lock);
  1135. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1136. mutex_lock(&ubi->move_mutex);
  1137. mutex_unlock(&ubi->move_mutex);
  1138. goto retry;
  1139. } else if (e == ubi->move_to) {
  1140. /*
  1141. * User is putting the physical eraseblock which was selected
  1142. * as the target the data is moved to. It may happen if the EBA
  1143. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1144. * but the WL sub-system has not put the PEB to the "used" tree
  1145. * yet, but it is about to do this. So we just set a flag which
  1146. * will tell the WL worker that the PEB is not needed anymore
  1147. * and should be scheduled for erasure.
  1148. */
  1149. dbg_wl("PEB %d is the target of data moving", pnum);
  1150. ubi_assert(!ubi->move_to_put);
  1151. ubi->move_to_put = 1;
  1152. spin_unlock(&ubi->wl_lock);
  1153. up_read(&ubi->fm_protect);
  1154. return 0;
  1155. } else {
  1156. if (in_wl_tree(e, &ubi->used)) {
  1157. self_check_in_wl_tree(ubi, e, &ubi->used);
  1158. rb_erase(&e->u.rb, &ubi->used);
  1159. } else if (in_wl_tree(e, &ubi->scrub)) {
  1160. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1161. rb_erase(&e->u.rb, &ubi->scrub);
  1162. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1163. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1164. rb_erase(&e->u.rb, &ubi->erroneous);
  1165. ubi->erroneous_peb_count -= 1;
  1166. ubi_assert(ubi->erroneous_peb_count >= 0);
  1167. /* Erroneous PEBs should be tortured */
  1168. torture = 1;
  1169. } else {
  1170. err = prot_queue_del(ubi, e->pnum);
  1171. if (err) {
  1172. ubi_err(ubi, "PEB %d not found", pnum);
  1173. ubi_ro_mode(ubi);
  1174. spin_unlock(&ubi->wl_lock);
  1175. up_read(&ubi->fm_protect);
  1176. return err;
  1177. }
  1178. }
  1179. }
  1180. spin_unlock(&ubi->wl_lock);
  1181. err = schedule_erase(ubi, e, vol_id, lnum, torture, false);
  1182. if (err) {
  1183. spin_lock(&ubi->wl_lock);
  1184. wl_tree_add(e, &ubi->used);
  1185. spin_unlock(&ubi->wl_lock);
  1186. }
  1187. up_read(&ubi->fm_protect);
  1188. return err;
  1189. }
  1190. /**
  1191. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1192. * @ubi: UBI device description object
  1193. * @pnum: the physical eraseblock to schedule
  1194. *
  1195. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1196. * needs scrubbing. This function schedules a physical eraseblock for
  1197. * scrubbing which is done in background. This function returns zero in case of
  1198. * success and a negative error code in case of failure.
  1199. */
  1200. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1201. {
  1202. struct ubi_wl_entry *e;
  1203. ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
  1204. retry:
  1205. spin_lock(&ubi->wl_lock);
  1206. e = ubi->lookuptbl[pnum];
  1207. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1208. in_wl_tree(e, &ubi->erroneous)) {
  1209. spin_unlock(&ubi->wl_lock);
  1210. return 0;
  1211. }
  1212. if (e == ubi->move_to) {
  1213. /*
  1214. * This physical eraseblock was used to move data to. The data
  1215. * was moved but the PEB was not yet inserted to the proper
  1216. * tree. We should just wait a little and let the WL worker
  1217. * proceed.
  1218. */
  1219. spin_unlock(&ubi->wl_lock);
  1220. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1221. yield();
  1222. goto retry;
  1223. }
  1224. if (in_wl_tree(e, &ubi->used)) {
  1225. self_check_in_wl_tree(ubi, e, &ubi->used);
  1226. rb_erase(&e->u.rb, &ubi->used);
  1227. } else {
  1228. int err;
  1229. err = prot_queue_del(ubi, e->pnum);
  1230. if (err) {
  1231. ubi_err(ubi, "PEB %d not found", pnum);
  1232. ubi_ro_mode(ubi);
  1233. spin_unlock(&ubi->wl_lock);
  1234. return err;
  1235. }
  1236. }
  1237. wl_tree_add(e, &ubi->scrub);
  1238. spin_unlock(&ubi->wl_lock);
  1239. /*
  1240. * Technically scrubbing is the same as wear-leveling, so it is done
  1241. * by the WL worker.
  1242. */
  1243. return ensure_wear_leveling(ubi, 0);
  1244. }
  1245. /**
  1246. * ubi_wl_flush - flush all pending works.
  1247. * @ubi: UBI device description object
  1248. * @vol_id: the volume id to flush for
  1249. * @lnum: the logical eraseblock number to flush for
  1250. *
  1251. * This function executes all pending works for a particular volume id /
  1252. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1253. * acts as a wildcard for all of the corresponding volume numbers or logical
  1254. * eraseblock numbers. It returns zero in case of success and a negative error
  1255. * code in case of failure.
  1256. */
  1257. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1258. {
  1259. int err = 0;
  1260. int found = 1;
  1261. /*
  1262. * Erase while the pending works queue is not empty, but not more than
  1263. * the number of currently pending works.
  1264. */
  1265. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1266. vol_id, lnum, ubi->works_count);
  1267. while (found) {
  1268. struct ubi_work *wrk, *tmp;
  1269. found = 0;
  1270. down_read(&ubi->work_sem);
  1271. spin_lock(&ubi->wl_lock);
  1272. list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
  1273. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1274. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1275. list_del(&wrk->list);
  1276. ubi->works_count -= 1;
  1277. ubi_assert(ubi->works_count >= 0);
  1278. spin_unlock(&ubi->wl_lock);
  1279. err = wrk->func(ubi, wrk, 0);
  1280. if (err) {
  1281. up_read(&ubi->work_sem);
  1282. return err;
  1283. }
  1284. spin_lock(&ubi->wl_lock);
  1285. found = 1;
  1286. break;
  1287. }
  1288. }
  1289. spin_unlock(&ubi->wl_lock);
  1290. up_read(&ubi->work_sem);
  1291. }
  1292. /*
  1293. * Make sure all the works which have been done in parallel are
  1294. * finished.
  1295. */
  1296. down_write(&ubi->work_sem);
  1297. up_write(&ubi->work_sem);
  1298. return err;
  1299. }
  1300. static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e)
  1301. {
  1302. if (in_wl_tree(e, &ubi->scrub))
  1303. return false;
  1304. else if (in_wl_tree(e, &ubi->erroneous))
  1305. return false;
  1306. else if (ubi->move_from == e)
  1307. return false;
  1308. else if (ubi->move_to == e)
  1309. return false;
  1310. return true;
  1311. }
  1312. /**
  1313. * ubi_bitflip_check - Check an eraseblock for bitflips and scrub it if needed.
  1314. * @ubi: UBI device description object
  1315. * @pnum: the physical eraseblock to schedule
  1316. * @force: don't read the block, assume bitflips happened and take action.
  1317. *
  1318. * This function reads the given eraseblock and checks if bitflips occured.
  1319. * In case of bitflips, the eraseblock is scheduled for scrubbing.
  1320. * If scrubbing is forced with @force, the eraseblock is not read,
  1321. * but scheduled for scrubbing right away.
  1322. *
  1323. * Returns:
  1324. * %EINVAL, PEB is out of range
  1325. * %ENOENT, PEB is no longer used by UBI
  1326. * %EBUSY, PEB cannot be checked now or a check is currently running on it
  1327. * %EAGAIN, bit flips happened but scrubbing is currently not possible
  1328. * %EUCLEAN, bit flips happened and PEB is scheduled for scrubbing
  1329. * %0, no bit flips detected
  1330. */
  1331. int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force)
  1332. {
  1333. int err = 0;
  1334. struct ubi_wl_entry *e;
  1335. if (pnum < 0 || pnum >= ubi->peb_count) {
  1336. err = -EINVAL;
  1337. goto out;
  1338. }
  1339. /*
  1340. * Pause all parallel work, otherwise it can happen that the
  1341. * erase worker frees a wl entry under us.
  1342. */
  1343. down_write(&ubi->work_sem);
  1344. /*
  1345. * Make sure that the wl entry does not change state while
  1346. * inspecting it.
  1347. */
  1348. spin_lock(&ubi->wl_lock);
  1349. e = ubi->lookuptbl[pnum];
  1350. if (!e) {
  1351. spin_unlock(&ubi->wl_lock);
  1352. err = -ENOENT;
  1353. goto out_resume;
  1354. }
  1355. /*
  1356. * Does it make sense to check this PEB?
  1357. */
  1358. if (!scrub_possible(ubi, e)) {
  1359. spin_unlock(&ubi->wl_lock);
  1360. err = -EBUSY;
  1361. goto out_resume;
  1362. }
  1363. spin_unlock(&ubi->wl_lock);
  1364. if (!force) {
  1365. mutex_lock(&ubi->buf_mutex);
  1366. err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
  1367. mutex_unlock(&ubi->buf_mutex);
  1368. }
  1369. if (force || err == UBI_IO_BITFLIPS) {
  1370. /*
  1371. * Okay, bit flip happened, let's figure out what we can do.
  1372. */
  1373. spin_lock(&ubi->wl_lock);
  1374. /*
  1375. * Recheck. We released wl_lock, UBI might have killed the
  1376. * wl entry under us.
  1377. */
  1378. e = ubi->lookuptbl[pnum];
  1379. if (!e) {
  1380. spin_unlock(&ubi->wl_lock);
  1381. err = -ENOENT;
  1382. goto out_resume;
  1383. }
  1384. /*
  1385. * Need to re-check state
  1386. */
  1387. if (!scrub_possible(ubi, e)) {
  1388. spin_unlock(&ubi->wl_lock);
  1389. err = -EBUSY;
  1390. goto out_resume;
  1391. }
  1392. if (in_pq(ubi, e)) {
  1393. prot_queue_del(ubi, e->pnum);
  1394. wl_tree_add(e, &ubi->scrub);
  1395. spin_unlock(&ubi->wl_lock);
  1396. err = ensure_wear_leveling(ubi, 1);
  1397. } else if (in_wl_tree(e, &ubi->used)) {
  1398. rb_erase(&e->u.rb, &ubi->used);
  1399. wl_tree_add(e, &ubi->scrub);
  1400. spin_unlock(&ubi->wl_lock);
  1401. err = ensure_wear_leveling(ubi, 1);
  1402. } else if (in_wl_tree(e, &ubi->free)) {
  1403. rb_erase(&e->u.rb, &ubi->free);
  1404. ubi->free_count--;
  1405. spin_unlock(&ubi->wl_lock);
  1406. /*
  1407. * This PEB is empty we can schedule it for
  1408. * erasure right away. No wear leveling needed.
  1409. */
  1410. err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
  1411. force ? 0 : 1, true);
  1412. } else {
  1413. spin_unlock(&ubi->wl_lock);
  1414. err = -EAGAIN;
  1415. }
  1416. if (!err && !force)
  1417. err = -EUCLEAN;
  1418. } else {
  1419. err = 0;
  1420. }
  1421. out_resume:
  1422. up_write(&ubi->work_sem);
  1423. out:
  1424. return err;
  1425. }
  1426. /**
  1427. * tree_destroy - destroy an RB-tree.
  1428. * @ubi: UBI device description object
  1429. * @root: the root of the tree to destroy
  1430. */
  1431. static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
  1432. {
  1433. struct rb_node *rb;
  1434. struct ubi_wl_entry *e;
  1435. rb = root->rb_node;
  1436. while (rb) {
  1437. if (rb->rb_left)
  1438. rb = rb->rb_left;
  1439. else if (rb->rb_right)
  1440. rb = rb->rb_right;
  1441. else {
  1442. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1443. rb = rb_parent(rb);
  1444. if (rb) {
  1445. if (rb->rb_left == &e->u.rb)
  1446. rb->rb_left = NULL;
  1447. else
  1448. rb->rb_right = NULL;
  1449. }
  1450. wl_entry_destroy(ubi, e);
  1451. }
  1452. }
  1453. }
  1454. /**
  1455. * ubi_thread - UBI background thread.
  1456. * @u: the UBI device description object pointer
  1457. */
  1458. int ubi_thread(void *u)
  1459. {
  1460. int failures = 0;
  1461. struct ubi_device *ubi = u;
  1462. ubi_msg(ubi, "background thread \"%s\" started, PID %d",
  1463. ubi->bgt_name, task_pid_nr(current));
  1464. set_freezable();
  1465. for (;;) {
  1466. int err;
  1467. if (kthread_should_stop())
  1468. break;
  1469. if (try_to_freeze())
  1470. continue;
  1471. spin_lock(&ubi->wl_lock);
  1472. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1473. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1474. set_current_state(TASK_INTERRUPTIBLE);
  1475. spin_unlock(&ubi->wl_lock);
  1476. /*
  1477. * Check kthread_should_stop() after we set the task
  1478. * state to guarantee that we either see the stop bit
  1479. * and exit or the task state is reset to runnable such
  1480. * that it's not scheduled out indefinitely and detects
  1481. * the stop bit at kthread_should_stop().
  1482. */
  1483. if (kthread_should_stop()) {
  1484. set_current_state(TASK_RUNNING);
  1485. break;
  1486. }
  1487. schedule();
  1488. continue;
  1489. }
  1490. spin_unlock(&ubi->wl_lock);
  1491. err = do_work(ubi);
  1492. if (err) {
  1493. ubi_err(ubi, "%s: work failed with error code %d",
  1494. ubi->bgt_name, err);
  1495. if (failures++ > WL_MAX_FAILURES) {
  1496. /*
  1497. * Too many failures, disable the thread and
  1498. * switch to read-only mode.
  1499. */
  1500. ubi_msg(ubi, "%s: %d consecutive failures",
  1501. ubi->bgt_name, WL_MAX_FAILURES);
  1502. ubi_ro_mode(ubi);
  1503. ubi->thread_enabled = 0;
  1504. continue;
  1505. }
  1506. } else
  1507. failures = 0;
  1508. cond_resched();
  1509. }
  1510. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1511. ubi->thread_enabled = 0;
  1512. return 0;
  1513. }
  1514. /**
  1515. * shutdown_work - shutdown all pending works.
  1516. * @ubi: UBI device description object
  1517. */
  1518. static void shutdown_work(struct ubi_device *ubi)
  1519. {
  1520. while (!list_empty(&ubi->works)) {
  1521. struct ubi_work *wrk;
  1522. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1523. list_del(&wrk->list);
  1524. wrk->func(ubi, wrk, 1);
  1525. ubi->works_count -= 1;
  1526. ubi_assert(ubi->works_count >= 0);
  1527. }
  1528. }
  1529. /**
  1530. * erase_aeb - erase a PEB given in UBI attach info PEB
  1531. * @ubi: UBI device description object
  1532. * @aeb: UBI attach info PEB
  1533. * @sync: If true, erase synchronously. Otherwise schedule for erasure
  1534. */
  1535. static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync)
  1536. {
  1537. struct ubi_wl_entry *e;
  1538. int err;
  1539. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1540. if (!e)
  1541. return -ENOMEM;
  1542. e->pnum = aeb->pnum;
  1543. e->ec = aeb->ec;
  1544. ubi->lookuptbl[e->pnum] = e;
  1545. if (sync) {
  1546. err = sync_erase(ubi, e, false);
  1547. if (err)
  1548. goto out_free;
  1549. wl_tree_add(e, &ubi->free);
  1550. ubi->free_count++;
  1551. } else {
  1552. err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false);
  1553. if (err)
  1554. goto out_free;
  1555. }
  1556. return 0;
  1557. out_free:
  1558. wl_entry_destroy(ubi, e);
  1559. return err;
  1560. }
  1561. /**
  1562. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1563. * @ubi: UBI device description object
  1564. * @ai: attaching information
  1565. *
  1566. * This function returns zero in case of success, and a negative error code in
  1567. * case of failure.
  1568. */
  1569. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1570. {
  1571. int err, i, reserved_pebs, found_pebs = 0;
  1572. struct rb_node *rb1, *rb2;
  1573. struct ubi_ainf_volume *av;
  1574. struct ubi_ainf_peb *aeb, *tmp;
  1575. struct ubi_wl_entry *e;
  1576. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1577. spin_lock_init(&ubi->wl_lock);
  1578. mutex_init(&ubi->move_mutex);
  1579. init_rwsem(&ubi->work_sem);
  1580. ubi->max_ec = ai->max_ec;
  1581. INIT_LIST_HEAD(&ubi->works);
  1582. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1583. err = -ENOMEM;
  1584. ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL);
  1585. if (!ubi->lookuptbl)
  1586. return err;
  1587. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1588. INIT_LIST_HEAD(&ubi->pq[i]);
  1589. ubi->pq_head = 0;
  1590. ubi->free_count = 0;
  1591. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1592. cond_resched();
  1593. err = erase_aeb(ubi, aeb, false);
  1594. if (err)
  1595. goto out_free;
  1596. found_pebs++;
  1597. }
  1598. list_for_each_entry(aeb, &ai->free, u.list) {
  1599. cond_resched();
  1600. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1601. if (!e) {
  1602. err = -ENOMEM;
  1603. goto out_free;
  1604. }
  1605. e->pnum = aeb->pnum;
  1606. e->ec = aeb->ec;
  1607. ubi_assert(e->ec >= 0);
  1608. wl_tree_add(e, &ubi->free);
  1609. ubi->free_count++;
  1610. ubi->lookuptbl[e->pnum] = e;
  1611. found_pebs++;
  1612. }
  1613. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1614. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1615. cond_resched();
  1616. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1617. if (!e) {
  1618. err = -ENOMEM;
  1619. goto out_free;
  1620. }
  1621. e->pnum = aeb->pnum;
  1622. e->ec = aeb->ec;
  1623. ubi->lookuptbl[e->pnum] = e;
  1624. if (!aeb->scrub) {
  1625. dbg_wl("add PEB %d EC %d to the used tree",
  1626. e->pnum, e->ec);
  1627. wl_tree_add(e, &ubi->used);
  1628. } else {
  1629. dbg_wl("add PEB %d EC %d to the scrub tree",
  1630. e->pnum, e->ec);
  1631. wl_tree_add(e, &ubi->scrub);
  1632. }
  1633. found_pebs++;
  1634. }
  1635. }
  1636. list_for_each_entry(aeb, &ai->fastmap, u.list) {
  1637. cond_resched();
  1638. e = ubi_find_fm_block(ubi, aeb->pnum);
  1639. if (e) {
  1640. ubi_assert(!ubi->lookuptbl[e->pnum]);
  1641. ubi->lookuptbl[e->pnum] = e;
  1642. } else {
  1643. bool sync = false;
  1644. /*
  1645. * Usually old Fastmap PEBs are scheduled for erasure
  1646. * and we don't have to care about them but if we face
  1647. * an power cut before scheduling them we need to
  1648. * take care of them here.
  1649. */
  1650. if (ubi->lookuptbl[aeb->pnum])
  1651. continue;
  1652. /*
  1653. * The fastmap update code might not find a free PEB for
  1654. * writing the fastmap anchor to and then reuses the
  1655. * current fastmap anchor PEB. When this PEB gets erased
  1656. * and a power cut happens before it is written again we
  1657. * must make sure that the fastmap attach code doesn't
  1658. * find any outdated fastmap anchors, hence we erase the
  1659. * outdated fastmap anchor PEBs synchronously here.
  1660. */
  1661. if (aeb->vol_id == UBI_FM_SB_VOLUME_ID)
  1662. sync = true;
  1663. err = erase_aeb(ubi, aeb, sync);
  1664. if (err)
  1665. goto out_free;
  1666. }
  1667. found_pebs++;
  1668. }
  1669. dbg_wl("found %i PEBs", found_pebs);
  1670. ubi_assert(ubi->good_peb_count == found_pebs);
  1671. reserved_pebs = WL_RESERVED_PEBS;
  1672. ubi_fastmap_init(ubi, &reserved_pebs);
  1673. if (ubi->avail_pebs < reserved_pebs) {
  1674. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1675. ubi->avail_pebs, reserved_pebs);
  1676. if (ubi->corr_peb_count)
  1677. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1678. ubi->corr_peb_count);
  1679. err = -ENOSPC;
  1680. goto out_free;
  1681. }
  1682. ubi->avail_pebs -= reserved_pebs;
  1683. ubi->rsvd_pebs += reserved_pebs;
  1684. /* Schedule wear-leveling if needed */
  1685. err = ensure_wear_leveling(ubi, 0);
  1686. if (err)
  1687. goto out_free;
  1688. #ifdef CONFIG_MTD_UBI_FASTMAP
  1689. if (!ubi->ro_mode && !ubi->fm_disabled)
  1690. ubi_ensure_anchor_pebs(ubi);
  1691. #endif
  1692. return 0;
  1693. out_free:
  1694. shutdown_work(ubi);
  1695. tree_destroy(ubi, &ubi->used);
  1696. tree_destroy(ubi, &ubi->free);
  1697. tree_destroy(ubi, &ubi->scrub);
  1698. kfree(ubi->lookuptbl);
  1699. return err;
  1700. }
  1701. /**
  1702. * protection_queue_destroy - destroy the protection queue.
  1703. * @ubi: UBI device description object
  1704. */
  1705. static void protection_queue_destroy(struct ubi_device *ubi)
  1706. {
  1707. int i;
  1708. struct ubi_wl_entry *e, *tmp;
  1709. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1710. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1711. list_del(&e->u.list);
  1712. wl_entry_destroy(ubi, e);
  1713. }
  1714. }
  1715. }
  1716. /**
  1717. * ubi_wl_close - close the wear-leveling sub-system.
  1718. * @ubi: UBI device description object
  1719. */
  1720. void ubi_wl_close(struct ubi_device *ubi)
  1721. {
  1722. dbg_wl("close the WL sub-system");
  1723. ubi_fastmap_close(ubi);
  1724. shutdown_work(ubi);
  1725. protection_queue_destroy(ubi);
  1726. tree_destroy(ubi, &ubi->used);
  1727. tree_destroy(ubi, &ubi->erroneous);
  1728. tree_destroy(ubi, &ubi->free);
  1729. tree_destroy(ubi, &ubi->scrub);
  1730. kfree(ubi->lookuptbl);
  1731. }
  1732. /**
  1733. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1734. * @ubi: UBI device description object
  1735. * @pnum: the physical eraseblock number to check
  1736. * @ec: the erase counter to check
  1737. *
  1738. * This function returns zero if the erase counter of physical eraseblock @pnum
  1739. * is equivalent to @ec, and a negative error code if not or if an error
  1740. * occurred.
  1741. */
  1742. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1743. {
  1744. int err;
  1745. long long read_ec;
  1746. struct ubi_ec_hdr *ec_hdr;
  1747. if (!ubi_dbg_chk_gen(ubi))
  1748. return 0;
  1749. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1750. if (!ec_hdr)
  1751. return -ENOMEM;
  1752. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1753. if (err && err != UBI_IO_BITFLIPS) {
  1754. /* The header does not have to exist */
  1755. err = 0;
  1756. goto out_free;
  1757. }
  1758. read_ec = be64_to_cpu(ec_hdr->ec);
  1759. if (ec != read_ec && read_ec - ec > 1) {
  1760. ubi_err(ubi, "self-check failed for PEB %d", pnum);
  1761. ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
  1762. dump_stack();
  1763. err = 1;
  1764. } else
  1765. err = 0;
  1766. out_free:
  1767. kfree(ec_hdr);
  1768. return err;
  1769. }
  1770. /**
  1771. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1772. * @ubi: UBI device description object
  1773. * @e: the wear-leveling entry to check
  1774. * @root: the root of the tree
  1775. *
  1776. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1777. * is not.
  1778. */
  1779. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1780. struct ubi_wl_entry *e, struct rb_root *root)
  1781. {
  1782. if (!ubi_dbg_chk_gen(ubi))
  1783. return 0;
  1784. if (in_wl_tree(e, root))
  1785. return 0;
  1786. ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
  1787. e->pnum, e->ec, root);
  1788. dump_stack();
  1789. return -EINVAL;
  1790. }
  1791. /**
  1792. * self_check_in_pq - check if wear-leveling entry is in the protection
  1793. * queue.
  1794. * @ubi: UBI device description object
  1795. * @e: the wear-leveling entry to check
  1796. *
  1797. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1798. */
  1799. static int self_check_in_pq(const struct ubi_device *ubi,
  1800. struct ubi_wl_entry *e)
  1801. {
  1802. if (!ubi_dbg_chk_gen(ubi))
  1803. return 0;
  1804. if (in_pq(ubi, e))
  1805. return 0;
  1806. ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
  1807. e->pnum, e->ec);
  1808. dump_stack();
  1809. return -EINVAL;
  1810. }
  1811. #ifndef CONFIG_MTD_UBI_FASTMAP
  1812. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  1813. {
  1814. struct ubi_wl_entry *e;
  1815. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1816. self_check_in_wl_tree(ubi, e, &ubi->free);
  1817. ubi->free_count--;
  1818. ubi_assert(ubi->free_count >= 0);
  1819. rb_erase(&e->u.rb, &ubi->free);
  1820. return e;
  1821. }
  1822. /**
  1823. * produce_free_peb - produce a free physical eraseblock.
  1824. * @ubi: UBI device description object
  1825. *
  1826. * This function tries to make a free PEB by means of synchronous execution of
  1827. * pending works. This may be needed if, for example the background thread is
  1828. * disabled. Returns zero in case of success and a negative error code in case
  1829. * of failure.
  1830. */
  1831. static int produce_free_peb(struct ubi_device *ubi)
  1832. {
  1833. int err;
  1834. while (!ubi->free.rb_node && ubi->works_count) {
  1835. spin_unlock(&ubi->wl_lock);
  1836. dbg_wl("do one work synchronously");
  1837. err = do_work(ubi);
  1838. spin_lock(&ubi->wl_lock);
  1839. if (err)
  1840. return err;
  1841. }
  1842. return 0;
  1843. }
  1844. /**
  1845. * ubi_wl_get_peb - get a physical eraseblock.
  1846. * @ubi: UBI device description object
  1847. *
  1848. * This function returns a physical eraseblock in case of success and a
  1849. * negative error code in case of failure.
  1850. * Returns with ubi->fm_eba_sem held in read mode!
  1851. */
  1852. int ubi_wl_get_peb(struct ubi_device *ubi)
  1853. {
  1854. int err;
  1855. struct ubi_wl_entry *e;
  1856. retry:
  1857. down_read(&ubi->fm_eba_sem);
  1858. spin_lock(&ubi->wl_lock);
  1859. if (!ubi->free.rb_node) {
  1860. if (ubi->works_count == 0) {
  1861. ubi_err(ubi, "no free eraseblocks");
  1862. ubi_assert(list_empty(&ubi->works));
  1863. spin_unlock(&ubi->wl_lock);
  1864. return -ENOSPC;
  1865. }
  1866. err = produce_free_peb(ubi);
  1867. if (err < 0) {
  1868. spin_unlock(&ubi->wl_lock);
  1869. return err;
  1870. }
  1871. spin_unlock(&ubi->wl_lock);
  1872. up_read(&ubi->fm_eba_sem);
  1873. goto retry;
  1874. }
  1875. e = wl_get_wle(ubi);
  1876. prot_queue_add(ubi, e);
  1877. spin_unlock(&ubi->wl_lock);
  1878. err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
  1879. ubi->peb_size - ubi->vid_hdr_aloffset);
  1880. if (err) {
  1881. ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);
  1882. return err;
  1883. }
  1884. return e->pnum;
  1885. }
  1886. #else
  1887. #include "fastmap-wl.c"
  1888. #endif