wl.c 45 KB

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