wl.c 44 KB

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