wl.c 51 KB

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