wl.c 44 KB

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