wl.c 49 KB

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