wl.c 43 KB

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