wl.c 44 KB

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