wl.c 43 KB

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