wl.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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. * If the WL sub-system fails to erase a physical eraseblock, it marks it as
  43. * bad.
  44. *
  45. * This sub-system is also responsible for scrubbing. If a bit-flip is detected
  46. * in a physical eraseblock, it has to be moved. Technically this is the same
  47. * as moving it for wear-leveling reasons.
  48. *
  49. * As it was said, for the UBI sub-system all physical eraseblocks are either
  50. * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
  51. * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
  52. * RB-trees, as well as (temporarily) in the @wl->pq queue.
  53. *
  54. * When the WL sub-system returns a physical eraseblock, the physical
  55. * eraseblock is protected from being moved for some "time". For this reason,
  56. * the physical eraseblock is not directly moved from the @wl->free tree to the
  57. * @wl->used tree. There is a protection queue in between where this
  58. * physical eraseblock is temporarily stored (@wl->pq).
  59. *
  60. * All this protection stuff is needed because:
  61. * o we don't want to move physical eraseblocks just after we have given them
  62. * to the user; instead, we first want to let users fill them up with data;
  63. *
  64. * o there is a chance that the user will put the physical eraseblock very
  65. * soon, so it makes sense not to move it for some time, but wait.
  66. *
  67. * Physical eraseblocks stay protected only for limited time. But the "time" is
  68. * measured in erase cycles in this case. This is implemented with help of the
  69. * protection queue. Eraseblocks are put to the tail of this queue when they
  70. * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
  71. * head of the queue on each erase operation (for any eraseblock). So the
  72. * length of the queue defines how may (global) erase cycles PEBs are protected.
  73. *
  74. * To put it differently, each physical eraseblock has 2 main states: free and
  75. * used. The former state corresponds to the @wl->free tree. The latter state
  76. * is split up on several sub-states:
  77. * o the WL movement is allowed (@wl->used tree);
  78. * o the WL movement is disallowed (@wl->erroneous) because the PEB is
  79. * erroneous - e.g., there was a read error;
  80. * o the WL movement is temporarily prohibited (@wl->pq queue);
  81. * o scrubbing is needed (@wl->scrub tree).
  82. *
  83. * Depending on the sub-state, wear-leveling entries of the used physical
  84. * eraseblocks may be kept in one of those structures.
  85. *
  86. * Note, in this implementation, we keep a small in-RAM object for each physical
  87. * eraseblock. This is surely not a scalable solution. But it appears to be good
  88. * enough for moderately large flashes and it is simple. In future, one may
  89. * re-work this sub-system and make it more scalable.
  90. *
  91. * At the moment this sub-system does not utilize the sequence number, which
  92. * was introduced relatively recently. But it would be wise to do this because
  93. * the sequence number of a logical eraseblock characterizes how old is it. For
  94. * example, when we move a PEB with low erase counter, and we need to pick the
  95. * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
  96. * pick target PEB with an average EC if our PEB is not very "old". This is a
  97. * room for future re-works of the WL sub-system.
  98. */
  99. #include <linux/slab.h>
  100. #include <linux/crc32.h>
  101. #include <linux/freezer.h>
  102. #include <linux/kthread.h>
  103. #include "ubi.h"
  104. /* Number of physical eraseblocks reserved for wear-leveling purposes */
  105. #define WL_RESERVED_PEBS 1
  106. /*
  107. * Maximum difference between two erase counters. If this threshold is
  108. * exceeded, the WL sub-system starts moving data from used physical
  109. * eraseblocks with low erase counter to free physical eraseblocks with high
  110. * erase counter.
  111. */
  112. #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
  113. /*
  114. * When a physical eraseblock is moved, the WL sub-system has to pick the target
  115. * physical eraseblock to move to. The simplest way would be just to pick the
  116. * one with the highest erase counter. But in certain workloads this could lead
  117. * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
  118. * situation when the picked physical eraseblock is constantly erased after the
  119. * data is written to it. So, we have a constant which limits the highest erase
  120. * counter of the free physical eraseblock to pick. Namely, the WL sub-system
  121. * does not pick eraseblocks with erase counter greater than the lowest erase
  122. * counter plus %WL_FREE_MAX_DIFF.
  123. */
  124. #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
  125. /*
  126. * Maximum number of consecutive background thread failures which is enough to
  127. * switch to read-only mode.
  128. */
  129. #define WL_MAX_FAILURES 32
  130. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
  131. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  132. struct ubi_wl_entry *e, struct rb_root *root);
  133. static int self_check_in_pq(const struct ubi_device *ubi,
  134. struct ubi_wl_entry *e);
  135. #ifdef CONFIG_MTD_UBI_FASTMAP
  136. /**
  137. * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
  138. * @wrk: the work description object
  139. */
  140. static void update_fastmap_work_fn(struct work_struct *wrk)
  141. {
  142. struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
  143. ubi_update_fastmap(ubi);
  144. }
  145. /**
  146. * ubi_ubi_is_fm_block - returns 1 if a PEB is currently used in a fastmap.
  147. * @ubi: UBI device description object
  148. * @pnum: the to be checked PEB
  149. */
  150. static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
  151. {
  152. int i;
  153. if (!ubi->fm)
  154. return 0;
  155. for (i = 0; i < ubi->fm->used_blocks; i++)
  156. if (ubi->fm->e[i]->pnum == pnum)
  157. return 1;
  158. return 0;
  159. }
  160. #else
  161. static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
  162. {
  163. return 0;
  164. }
  165. #endif
  166. /**
  167. * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
  168. * @e: the wear-leveling entry to add
  169. * @root: the root of the tree
  170. *
  171. * Note, we use (erase counter, physical eraseblock number) pairs as keys in
  172. * the @ubi->used and @ubi->free RB-trees.
  173. */
  174. static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
  175. {
  176. struct rb_node **p, *parent = NULL;
  177. p = &root->rb_node;
  178. while (*p) {
  179. struct ubi_wl_entry *e1;
  180. parent = *p;
  181. e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
  182. if (e->ec < e1->ec)
  183. p = &(*p)->rb_left;
  184. else if (e->ec > e1->ec)
  185. p = &(*p)->rb_right;
  186. else {
  187. ubi_assert(e->pnum != e1->pnum);
  188. if (e->pnum < e1->pnum)
  189. p = &(*p)->rb_left;
  190. else
  191. p = &(*p)->rb_right;
  192. }
  193. }
  194. rb_link_node(&e->u.rb, parent, p);
  195. rb_insert_color(&e->u.rb, root);
  196. }
  197. /**
  198. * do_work - do one pending work.
  199. * @ubi: UBI device description object
  200. *
  201. * This function returns zero in case of success and a negative error code in
  202. * case of failure.
  203. */
  204. static int do_work(struct ubi_device *ubi)
  205. {
  206. int err;
  207. struct ubi_work *wrk;
  208. cond_resched();
  209. /*
  210. * @ubi->work_sem is used to synchronize with the workers. Workers take
  211. * it in read mode, so many of them may be doing works at a time. But
  212. * the queue flush code has to be sure the whole queue of works is
  213. * done, and it takes the mutex in write mode.
  214. */
  215. down_read(&ubi->work_sem);
  216. spin_lock(&ubi->wl_lock);
  217. if (list_empty(&ubi->works)) {
  218. spin_unlock(&ubi->wl_lock);
  219. up_read(&ubi->work_sem);
  220. return 0;
  221. }
  222. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  223. list_del(&wrk->list);
  224. ubi->works_count -= 1;
  225. ubi_assert(ubi->works_count >= 0);
  226. spin_unlock(&ubi->wl_lock);
  227. /*
  228. * Call the worker function. Do not touch the work structure
  229. * after this call as it will have been freed or reused by that
  230. * time by the worker function.
  231. */
  232. err = wrk->func(ubi, wrk, 0);
  233. if (err)
  234. ubi_err("work failed with error code %d", err);
  235. up_read(&ubi->work_sem);
  236. return err;
  237. }
  238. /**
  239. * produce_free_peb - produce a free physical eraseblock.
  240. * @ubi: UBI device description object
  241. *
  242. * This function tries to make a free PEB by means of synchronous execution of
  243. * pending works. This may be needed if, for example the background thread is
  244. * disabled. Returns zero in case of success and a negative error code in case
  245. * of failure.
  246. */
  247. static int produce_free_peb(struct ubi_device *ubi)
  248. {
  249. int err;
  250. while (!ubi->free.rb_node && ubi->works_count) {
  251. spin_unlock(&ubi->wl_lock);
  252. dbg_wl("do one work synchronously");
  253. err = do_work(ubi);
  254. spin_lock(&ubi->wl_lock);
  255. if (err)
  256. return err;
  257. }
  258. return 0;
  259. }
  260. /**
  261. * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
  262. * @e: the wear-leveling entry to check
  263. * @root: the root of the tree
  264. *
  265. * This function returns non-zero if @e is in the @root RB-tree and zero if it
  266. * is not.
  267. */
  268. static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
  269. {
  270. struct rb_node *p;
  271. p = root->rb_node;
  272. while (p) {
  273. struct ubi_wl_entry *e1;
  274. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  275. if (e->pnum == e1->pnum) {
  276. ubi_assert(e == e1);
  277. return 1;
  278. }
  279. if (e->ec < e1->ec)
  280. p = p->rb_left;
  281. else if (e->ec > e1->ec)
  282. p = p->rb_right;
  283. else {
  284. ubi_assert(e->pnum != e1->pnum);
  285. if (e->pnum < e1->pnum)
  286. p = p->rb_left;
  287. else
  288. p = p->rb_right;
  289. }
  290. }
  291. return 0;
  292. }
  293. /**
  294. * prot_queue_add - add physical eraseblock to the protection queue.
  295. * @ubi: UBI device description object
  296. * @e: the physical eraseblock to add
  297. *
  298. * This function adds @e to the tail of the protection queue @ubi->pq, where
  299. * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
  300. * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
  301. * be locked.
  302. */
  303. static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
  304. {
  305. int pq_tail = ubi->pq_head - 1;
  306. if (pq_tail < 0)
  307. pq_tail = UBI_PROT_QUEUE_LEN - 1;
  308. ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
  309. list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
  310. dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
  311. }
  312. /**
  313. * find_wl_entry - find wear-leveling entry closest to certain erase counter.
  314. * @ubi: UBI device description object
  315. * @root: the RB-tree where to look for
  316. * @diff: maximum possible difference from the smallest erase counter
  317. *
  318. * This function looks for a wear leveling entry with erase counter closest to
  319. * min + @diff, where min is the smallest erase counter.
  320. */
  321. static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
  322. struct rb_root *root, int diff)
  323. {
  324. struct rb_node *p;
  325. struct ubi_wl_entry *e, *prev_e = NULL;
  326. int max;
  327. e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  328. max = e->ec + diff;
  329. p = root->rb_node;
  330. while (p) {
  331. struct ubi_wl_entry *e1;
  332. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  333. if (e1->ec >= max)
  334. p = p->rb_left;
  335. else {
  336. p = p->rb_right;
  337. prev_e = e;
  338. e = e1;
  339. }
  340. }
  341. /* If no fastmap has been written and this WL entry can be used
  342. * as anchor PEB, hold it back and return the second best WL entry
  343. * such that fastmap can use the anchor PEB later. */
  344. if (prev_e && !ubi->fm_disabled &&
  345. !ubi->fm && e->pnum < UBI_FM_MAX_START)
  346. return prev_e;
  347. return e;
  348. }
  349. /**
  350. * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
  351. * @ubi: UBI device description object
  352. * @root: the RB-tree where to look for
  353. *
  354. * This function looks for a wear leveling entry with medium erase counter,
  355. * but not greater or equivalent than the lowest erase counter plus
  356. * %WL_FREE_MAX_DIFF/2.
  357. */
  358. static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
  359. struct rb_root *root)
  360. {
  361. struct ubi_wl_entry *e, *first, *last;
  362. first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  363. last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
  364. if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
  365. e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
  366. #ifdef CONFIG_MTD_UBI_FASTMAP
  367. /* If no fastmap has been written and this WL entry can be used
  368. * as anchor PEB, hold it back and return the second best
  369. * WL entry such that fastmap can use the anchor PEB later. */
  370. if (e && !ubi->fm_disabled && !ubi->fm &&
  371. e->pnum < UBI_FM_MAX_START)
  372. e = rb_entry(rb_next(root->rb_node),
  373. struct ubi_wl_entry, u.rb);
  374. #endif
  375. } else
  376. e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
  377. return e;
  378. }
  379. #ifdef CONFIG_MTD_UBI_FASTMAP
  380. /**
  381. * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
  382. * @root: the RB-tree where to look for
  383. */
  384. static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
  385. {
  386. struct rb_node *p;
  387. struct ubi_wl_entry *e, *victim = NULL;
  388. int max_ec = UBI_MAX_ERASECOUNTER;
  389. ubi_rb_for_each_entry(p, e, root, u.rb) {
  390. if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
  391. victim = e;
  392. max_ec = e->ec;
  393. }
  394. }
  395. return victim;
  396. }
  397. static int anchor_pebs_avalible(struct rb_root *root)
  398. {
  399. struct rb_node *p;
  400. struct ubi_wl_entry *e;
  401. ubi_rb_for_each_entry(p, e, root, u.rb)
  402. if (e->pnum < UBI_FM_MAX_START)
  403. return 1;
  404. return 0;
  405. }
  406. /**
  407. * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
  408. * @ubi: UBI device description object
  409. * @anchor: This PEB will be used as anchor PEB by fastmap
  410. *
  411. * The function returns a physical erase block with a given maximal number
  412. * and removes it from the wl subsystem.
  413. * Must be called with wl_lock held!
  414. */
  415. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
  416. {
  417. struct ubi_wl_entry *e = NULL;
  418. if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
  419. goto out;
  420. if (anchor)
  421. e = find_anchor_wl_entry(&ubi->free);
  422. else
  423. e = find_mean_wl_entry(ubi, &ubi->free);
  424. if (!e)
  425. goto out;
  426. self_check_in_wl_tree(ubi, e, &ubi->free);
  427. /* remove it from the free list,
  428. * the wl subsystem does no longer know this erase block */
  429. rb_erase(&e->u.rb, &ubi->free);
  430. ubi->free_count--;
  431. out:
  432. return e;
  433. }
  434. #endif
  435. /**
  436. * __wl_get_peb - get a physical eraseblock.
  437. * @ubi: UBI device description object
  438. *
  439. * This function returns a physical eraseblock in case of success and a
  440. * negative error code in case of failure.
  441. */
  442. static int __wl_get_peb(struct ubi_device *ubi)
  443. {
  444. int err;
  445. struct ubi_wl_entry *e;
  446. retry:
  447. if (!ubi->free.rb_node) {
  448. if (ubi->works_count == 0) {
  449. ubi_err("no free eraseblocks");
  450. ubi_assert(list_empty(&ubi->works));
  451. return -ENOSPC;
  452. }
  453. err = produce_free_peb(ubi);
  454. if (err < 0)
  455. return err;
  456. goto retry;
  457. }
  458. e = find_mean_wl_entry(ubi, &ubi->free);
  459. if (!e) {
  460. ubi_err("no free eraseblocks");
  461. return -ENOSPC;
  462. }
  463. self_check_in_wl_tree(ubi, e, &ubi->free);
  464. /*
  465. * Move the physical eraseblock to the protection queue where it will
  466. * be protected from being moved for some time.
  467. */
  468. rb_erase(&e->u.rb, &ubi->free);
  469. ubi->free_count--;
  470. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  471. #ifndef CONFIG_MTD_UBI_FASTMAP
  472. /* We have to enqueue e only if fastmap is disabled,
  473. * is fastmap enabled prot_queue_add() will be called by
  474. * ubi_wl_get_peb() after removing e from the pool. */
  475. prot_queue_add(ubi, e);
  476. #endif
  477. return e->pnum;
  478. }
  479. #ifdef CONFIG_MTD_UBI_FASTMAP
  480. /**
  481. * return_unused_pool_pebs - returns unused PEB to the free tree.
  482. * @ubi: UBI device description object
  483. * @pool: fastmap pool description object
  484. */
  485. static void return_unused_pool_pebs(struct ubi_device *ubi,
  486. struct ubi_fm_pool *pool)
  487. {
  488. int i;
  489. struct ubi_wl_entry *e;
  490. for (i = pool->used; i < pool->size; i++) {
  491. e = ubi->lookuptbl[pool->pebs[i]];
  492. wl_tree_add(e, &ubi->free);
  493. ubi->free_count++;
  494. }
  495. }
  496. /**
  497. * refill_wl_pool - refills all the fastmap pool used by the
  498. * WL sub-system.
  499. * @ubi: UBI device description object
  500. */
  501. static void refill_wl_pool(struct ubi_device *ubi)
  502. {
  503. struct ubi_wl_entry *e;
  504. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  505. return_unused_pool_pebs(ubi, pool);
  506. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  507. if (!ubi->free.rb_node ||
  508. (ubi->free_count - ubi->beb_rsvd_pebs < 5))
  509. break;
  510. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  511. self_check_in_wl_tree(ubi, e, &ubi->free);
  512. rb_erase(&e->u.rb, &ubi->free);
  513. ubi->free_count--;
  514. pool->pebs[pool->size] = e->pnum;
  515. }
  516. pool->used = 0;
  517. }
  518. /**
  519. * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
  520. * @ubi: UBI device description object
  521. */
  522. static void refill_wl_user_pool(struct ubi_device *ubi)
  523. {
  524. struct ubi_fm_pool *pool = &ubi->fm_pool;
  525. return_unused_pool_pebs(ubi, pool);
  526. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  527. pool->pebs[pool->size] = __wl_get_peb(ubi);
  528. if (pool->pebs[pool->size] < 0)
  529. break;
  530. }
  531. pool->used = 0;
  532. }
  533. /**
  534. * ubi_refill_pools - refills all fastmap PEB pools.
  535. * @ubi: UBI device description object
  536. */
  537. void ubi_refill_pools(struct ubi_device *ubi)
  538. {
  539. spin_lock(&ubi->wl_lock);
  540. refill_wl_pool(ubi);
  541. refill_wl_user_pool(ubi);
  542. spin_unlock(&ubi->wl_lock);
  543. }
  544. /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
  545. * the fastmap pool.
  546. */
  547. int ubi_wl_get_peb(struct ubi_device *ubi)
  548. {
  549. int ret;
  550. struct ubi_fm_pool *pool = &ubi->fm_pool;
  551. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  552. if (!pool->size || !wl_pool->size || pool->used == pool->size ||
  553. wl_pool->used == wl_pool->size)
  554. ubi_update_fastmap(ubi);
  555. /* we got not a single free PEB */
  556. if (!pool->size)
  557. ret = -ENOSPC;
  558. else {
  559. spin_lock(&ubi->wl_lock);
  560. ret = pool->pebs[pool->used++];
  561. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  562. spin_unlock(&ubi->wl_lock);
  563. }
  564. return ret;
  565. }
  566. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  567. *
  568. * @ubi: UBI device description object
  569. */
  570. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  571. {
  572. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  573. int pnum;
  574. if (pool->used == pool->size || !pool->size) {
  575. /* We cannot update the fastmap here because this
  576. * function is called in atomic context.
  577. * Let's fail here and refill/update it as soon as possible. */
  578. schedule_work(&ubi->fm_work);
  579. return NULL;
  580. } else {
  581. pnum = pool->pebs[pool->used++];
  582. return ubi->lookuptbl[pnum];
  583. }
  584. }
  585. #else
  586. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  587. {
  588. struct ubi_wl_entry *e;
  589. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  590. self_check_in_wl_tree(ubi, e, &ubi->free);
  591. ubi->free_count--;
  592. ubi_assert(ubi->free_count >= 0);
  593. rb_erase(&e->u.rb, &ubi->free);
  594. return e;
  595. }
  596. int ubi_wl_get_peb(struct ubi_device *ubi)
  597. {
  598. int peb, err;
  599. spin_lock(&ubi->wl_lock);
  600. peb = __wl_get_peb(ubi);
  601. spin_unlock(&ubi->wl_lock);
  602. if (peb < 0)
  603. return peb;
  604. err = ubi_self_check_all_ff(ubi, peb, ubi->vid_hdr_aloffset,
  605. ubi->peb_size - ubi->vid_hdr_aloffset);
  606. if (err) {
  607. ubi_err("new PEB %d does not contain all 0xFF bytes", peb);
  608. return err;
  609. }
  610. return peb;
  611. }
  612. #endif
  613. /**
  614. * prot_queue_del - remove a physical eraseblock from the protection queue.
  615. * @ubi: UBI device description object
  616. * @pnum: the physical eraseblock to remove
  617. *
  618. * This function deletes PEB @pnum from the protection queue and returns zero
  619. * in case of success and %-ENODEV if the PEB was not found.
  620. */
  621. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  622. {
  623. struct ubi_wl_entry *e;
  624. e = ubi->lookuptbl[pnum];
  625. if (!e)
  626. return -ENODEV;
  627. if (self_check_in_pq(ubi, e))
  628. return -ENODEV;
  629. list_del(&e->u.list);
  630. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  631. return 0;
  632. }
  633. /**
  634. * sync_erase - synchronously erase a physical eraseblock.
  635. * @ubi: UBI device description object
  636. * @e: the the physical eraseblock to erase
  637. * @torture: if the physical eraseblock has to be tortured
  638. *
  639. * This function returns zero in case of success and a negative error code in
  640. * case of failure.
  641. */
  642. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  643. int torture)
  644. {
  645. int err;
  646. struct ubi_ec_hdr *ec_hdr;
  647. unsigned long long ec = e->ec;
  648. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  649. err = self_check_ec(ubi, e->pnum, e->ec);
  650. if (err)
  651. return -EINVAL;
  652. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  653. if (!ec_hdr)
  654. return -ENOMEM;
  655. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  656. if (err < 0)
  657. goto out_free;
  658. ec += err;
  659. if (ec > UBI_MAX_ERASECOUNTER) {
  660. /*
  661. * Erase counter overflow. Upgrade UBI and use 64-bit
  662. * erase counters internally.
  663. */
  664. ubi_err("erase counter overflow at PEB %d, EC %llu",
  665. e->pnum, ec);
  666. err = -EINVAL;
  667. goto out_free;
  668. }
  669. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  670. ec_hdr->ec = cpu_to_be64(ec);
  671. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  672. if (err)
  673. goto out_free;
  674. e->ec = ec;
  675. spin_lock(&ubi->wl_lock);
  676. if (e->ec > ubi->max_ec)
  677. ubi->max_ec = e->ec;
  678. spin_unlock(&ubi->wl_lock);
  679. out_free:
  680. kfree(ec_hdr);
  681. return err;
  682. }
  683. /**
  684. * serve_prot_queue - check if it is time to stop protecting PEBs.
  685. * @ubi: UBI device description object
  686. *
  687. * This function is called after each erase operation and removes PEBs from the
  688. * tail of the protection queue. These PEBs have been protected for long enough
  689. * and should be moved to the used tree.
  690. */
  691. static void serve_prot_queue(struct ubi_device *ubi)
  692. {
  693. struct ubi_wl_entry *e, *tmp;
  694. int count;
  695. /*
  696. * There may be several protected physical eraseblock to remove,
  697. * process them all.
  698. */
  699. repeat:
  700. count = 0;
  701. spin_lock(&ubi->wl_lock);
  702. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  703. dbg_wl("PEB %d EC %d protection over, move to used tree",
  704. e->pnum, e->ec);
  705. list_del(&e->u.list);
  706. wl_tree_add(e, &ubi->used);
  707. if (count++ > 32) {
  708. /*
  709. * Let's be nice and avoid holding the spinlock for
  710. * too long.
  711. */
  712. spin_unlock(&ubi->wl_lock);
  713. cond_resched();
  714. goto repeat;
  715. }
  716. }
  717. ubi->pq_head += 1;
  718. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  719. ubi->pq_head = 0;
  720. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  721. spin_unlock(&ubi->wl_lock);
  722. }
  723. /**
  724. * __schedule_ubi_work - schedule a work.
  725. * @ubi: UBI device description object
  726. * @wrk: the work to schedule
  727. *
  728. * This function adds a work defined by @wrk to the tail of the pending works
  729. * list. Can only be used if ubi->work_sem is already held in read mode!
  730. */
  731. static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  732. {
  733. spin_lock(&ubi->wl_lock);
  734. list_add_tail(&wrk->list, &ubi->works);
  735. ubi_assert(ubi->works_count >= 0);
  736. ubi->works_count += 1;
  737. if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
  738. wake_up_process(ubi->bgt_thread);
  739. spin_unlock(&ubi->wl_lock);
  740. }
  741. /**
  742. * schedule_ubi_work - schedule a work.
  743. * @ubi: UBI device description object
  744. * @wrk: the work to schedule
  745. *
  746. * This function adds a work defined by @wrk to the tail of the pending works
  747. * list.
  748. */
  749. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  750. {
  751. down_read(&ubi->work_sem);
  752. __schedule_ubi_work(ubi, wrk);
  753. up_read(&ubi->work_sem);
  754. }
  755. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  756. int shutdown);
  757. #ifdef CONFIG_MTD_UBI_FASTMAP
  758. /**
  759. * ubi_is_erase_work - checks whether a work is erase work.
  760. * @wrk: The work object to be checked
  761. */
  762. int ubi_is_erase_work(struct ubi_work *wrk)
  763. {
  764. return wrk->func == erase_worker;
  765. }
  766. #endif
  767. /**
  768. * schedule_erase - schedule an erase work.
  769. * @ubi: UBI device description object
  770. * @e: the WL entry of the physical eraseblock to erase
  771. * @vol_id: the volume ID that last used this PEB
  772. * @lnum: the last used logical eraseblock number for the PEB
  773. * @torture: if the physical eraseblock has to be tortured
  774. *
  775. * This function returns zero in case of success and a %-ENOMEM in case of
  776. * failure.
  777. */
  778. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  779. int vol_id, int lnum, int torture)
  780. {
  781. struct ubi_work *wl_wrk;
  782. ubi_assert(e);
  783. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  784. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  785. e->pnum, e->ec, torture);
  786. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  787. if (!wl_wrk)
  788. return -ENOMEM;
  789. wl_wrk->func = &erase_worker;
  790. wl_wrk->e = e;
  791. wl_wrk->vol_id = vol_id;
  792. wl_wrk->lnum = lnum;
  793. wl_wrk->torture = torture;
  794. schedule_ubi_work(ubi, wl_wrk);
  795. return 0;
  796. }
  797. /**
  798. * do_sync_erase - run the erase worker synchronously.
  799. * @ubi: UBI device description object
  800. * @e: the WL entry of the physical eraseblock to erase
  801. * @vol_id: the volume ID that last used this PEB
  802. * @lnum: the last used logical eraseblock number for the PEB
  803. * @torture: if the physical eraseblock has to be tortured
  804. *
  805. */
  806. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  807. int vol_id, int lnum, int torture)
  808. {
  809. struct ubi_work *wl_wrk;
  810. dbg_wl("sync erase of PEB %i", e->pnum);
  811. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  812. if (!wl_wrk)
  813. return -ENOMEM;
  814. wl_wrk->e = e;
  815. wl_wrk->vol_id = vol_id;
  816. wl_wrk->lnum = lnum;
  817. wl_wrk->torture = torture;
  818. return erase_worker(ubi, wl_wrk, 0);
  819. }
  820. #ifdef CONFIG_MTD_UBI_FASTMAP
  821. /**
  822. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  823. * sub-system.
  824. * see: ubi_wl_put_peb()
  825. *
  826. * @ubi: UBI device description object
  827. * @fm_e: physical eraseblock to return
  828. * @lnum: the last used logical eraseblock number for the PEB
  829. * @torture: if this physical eraseblock has to be tortured
  830. */
  831. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  832. int lnum, int torture)
  833. {
  834. struct ubi_wl_entry *e;
  835. int vol_id, pnum = fm_e->pnum;
  836. dbg_wl("PEB %d", pnum);
  837. ubi_assert(pnum >= 0);
  838. ubi_assert(pnum < ubi->peb_count);
  839. spin_lock(&ubi->wl_lock);
  840. e = ubi->lookuptbl[pnum];
  841. /* This can happen if we recovered from a fastmap the very
  842. * first time and writing now a new one. In this case the wl system
  843. * has never seen any PEB used by the original fastmap.
  844. */
  845. if (!e) {
  846. e = fm_e;
  847. ubi_assert(e->ec >= 0);
  848. ubi->lookuptbl[pnum] = e;
  849. } else {
  850. e->ec = fm_e->ec;
  851. kfree(fm_e);
  852. }
  853. spin_unlock(&ubi->wl_lock);
  854. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  855. return schedule_erase(ubi, e, vol_id, lnum, torture);
  856. }
  857. #endif
  858. /**
  859. * wear_leveling_worker - wear-leveling worker function.
  860. * @ubi: UBI device description object
  861. * @wrk: the work object
  862. * @shutdown: non-zero if the worker has to free memory and exit
  863. * because the WL-subsystem is shutting down
  864. *
  865. * This function copies a more worn out physical eraseblock to a less worn out
  866. * one. Returns zero in case of success and a negative error code in case of
  867. * failure.
  868. */
  869. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  870. int shutdown)
  871. {
  872. int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  873. int vol_id = -1, uninitialized_var(lnum);
  874. #ifdef CONFIG_MTD_UBI_FASTMAP
  875. int anchor = wrk->anchor;
  876. #endif
  877. struct ubi_wl_entry *e1, *e2;
  878. struct ubi_vid_hdr *vid_hdr;
  879. kfree(wrk);
  880. if (shutdown)
  881. return 0;
  882. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  883. if (!vid_hdr)
  884. return -ENOMEM;
  885. mutex_lock(&ubi->move_mutex);
  886. spin_lock(&ubi->wl_lock);
  887. ubi_assert(!ubi->move_from && !ubi->move_to);
  888. ubi_assert(!ubi->move_to_put);
  889. if (!ubi->free.rb_node ||
  890. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  891. /*
  892. * No free physical eraseblocks? Well, they must be waiting in
  893. * the queue to be erased. Cancel movement - it will be
  894. * triggered again when a free physical eraseblock appears.
  895. *
  896. * No used physical eraseblocks? They must be temporarily
  897. * protected from being moved. They will be moved to the
  898. * @ubi->used tree later and the wear-leveling will be
  899. * triggered again.
  900. */
  901. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  902. !ubi->free.rb_node, !ubi->used.rb_node);
  903. goto out_cancel;
  904. }
  905. #ifdef CONFIG_MTD_UBI_FASTMAP
  906. /* Check whether we need to produce an anchor PEB */
  907. if (!anchor)
  908. anchor = !anchor_pebs_avalible(&ubi->free);
  909. if (anchor) {
  910. e1 = find_anchor_wl_entry(&ubi->used);
  911. if (!e1)
  912. goto out_cancel;
  913. e2 = get_peb_for_wl(ubi);
  914. if (!e2)
  915. goto out_cancel;
  916. self_check_in_wl_tree(ubi, e1, &ubi->used);
  917. rb_erase(&e1->u.rb, &ubi->used);
  918. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  919. } else if (!ubi->scrub.rb_node) {
  920. #else
  921. if (!ubi->scrub.rb_node) {
  922. #endif
  923. /*
  924. * Now pick the least worn-out used physical eraseblock and a
  925. * highly worn-out free physical eraseblock. If the erase
  926. * counters differ much enough, start wear-leveling.
  927. */
  928. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  929. e2 = get_peb_for_wl(ubi);
  930. if (!e2)
  931. goto out_cancel;
  932. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  933. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  934. e1->ec, e2->ec);
  935. /* Give the unused PEB back */
  936. wl_tree_add(e2, &ubi->free);
  937. ubi->free_count++;
  938. goto out_cancel;
  939. }
  940. self_check_in_wl_tree(ubi, e1, &ubi->used);
  941. rb_erase(&e1->u.rb, &ubi->used);
  942. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  943. e1->pnum, e1->ec, e2->pnum, e2->ec);
  944. } else {
  945. /* Perform scrubbing */
  946. scrubbing = 1;
  947. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  948. e2 = get_peb_for_wl(ubi);
  949. if (!e2)
  950. goto out_cancel;
  951. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  952. rb_erase(&e1->u.rb, &ubi->scrub);
  953. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  954. }
  955. ubi->move_from = e1;
  956. ubi->move_to = e2;
  957. spin_unlock(&ubi->wl_lock);
  958. /*
  959. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  960. * We so far do not know which logical eraseblock our physical
  961. * eraseblock (@e1) belongs to. We have to read the volume identifier
  962. * header first.
  963. *
  964. * Note, we are protected from this PEB being unmapped and erased. The
  965. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  966. * which is being moved was unmapped.
  967. */
  968. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  969. if (err && err != UBI_IO_BITFLIPS) {
  970. if (err == UBI_IO_FF) {
  971. /*
  972. * We are trying to move PEB without a VID header. UBI
  973. * always write VID headers shortly after the PEB was
  974. * given, so we have a situation when it has not yet
  975. * had a chance to write it, because it was preempted.
  976. * So add this PEB to the protection queue so far,
  977. * because presumably more data will be written there
  978. * (including the missing VID header), and then we'll
  979. * move it.
  980. */
  981. dbg_wl("PEB %d has no VID header", e1->pnum);
  982. protect = 1;
  983. goto out_not_moved;
  984. } else if (err == UBI_IO_FF_BITFLIPS) {
  985. /*
  986. * The same situation as %UBI_IO_FF, but bit-flips were
  987. * detected. It is better to schedule this PEB for
  988. * scrubbing.
  989. */
  990. dbg_wl("PEB %d has no VID header but has bit-flips",
  991. e1->pnum);
  992. scrubbing = 1;
  993. goto out_not_moved;
  994. }
  995. ubi_err("error %d while reading VID header from PEB %d",
  996. err, e1->pnum);
  997. goto out_error;
  998. }
  999. vol_id = be32_to_cpu(vid_hdr->vol_id);
  1000. lnum = be32_to_cpu(vid_hdr->lnum);
  1001. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  1002. if (err) {
  1003. if (err == MOVE_CANCEL_RACE) {
  1004. /*
  1005. * The LEB has not been moved because the volume is
  1006. * being deleted or the PEB has been put meanwhile. We
  1007. * should prevent this PEB from being selected for
  1008. * wear-leveling movement again, so put it to the
  1009. * protection queue.
  1010. */
  1011. protect = 1;
  1012. goto out_not_moved;
  1013. }
  1014. if (err == MOVE_RETRY) {
  1015. scrubbing = 1;
  1016. goto out_not_moved;
  1017. }
  1018. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  1019. err == MOVE_TARGET_RD_ERR) {
  1020. /*
  1021. * Target PEB had bit-flips or write error - torture it.
  1022. */
  1023. torture = 1;
  1024. goto out_not_moved;
  1025. }
  1026. if (err == MOVE_SOURCE_RD_ERR) {
  1027. /*
  1028. * An error happened while reading the source PEB. Do
  1029. * not switch to R/O mode in this case, and give the
  1030. * upper layers a possibility to recover from this,
  1031. * e.g. by unmapping corresponding LEB. Instead, just
  1032. * put this PEB to the @ubi->erroneous list to prevent
  1033. * UBI from trying to move it over and over again.
  1034. */
  1035. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  1036. ubi_err("too many erroneous eraseblocks (%d)",
  1037. ubi->erroneous_peb_count);
  1038. goto out_error;
  1039. }
  1040. erroneous = 1;
  1041. goto out_not_moved;
  1042. }
  1043. if (err < 0)
  1044. goto out_error;
  1045. ubi_assert(0);
  1046. }
  1047. /* The PEB has been successfully moved */
  1048. if (scrubbing)
  1049. ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  1050. e1->pnum, vol_id, lnum, e2->pnum);
  1051. ubi_free_vid_hdr(ubi, vid_hdr);
  1052. spin_lock(&ubi->wl_lock);
  1053. if (!ubi->move_to_put) {
  1054. wl_tree_add(e2, &ubi->used);
  1055. e2 = NULL;
  1056. }
  1057. ubi->move_from = ubi->move_to = NULL;
  1058. ubi->move_to_put = ubi->wl_scheduled = 0;
  1059. spin_unlock(&ubi->wl_lock);
  1060. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  1061. if (err) {
  1062. kmem_cache_free(ubi_wl_entry_slab, e1);
  1063. if (e2)
  1064. kmem_cache_free(ubi_wl_entry_slab, e2);
  1065. goto out_ro;
  1066. }
  1067. if (e2) {
  1068. /*
  1069. * Well, the target PEB was put meanwhile, schedule it for
  1070. * erasure.
  1071. */
  1072. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  1073. e2->pnum, vol_id, lnum);
  1074. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  1075. if (err) {
  1076. kmem_cache_free(ubi_wl_entry_slab, e2);
  1077. goto out_ro;
  1078. }
  1079. }
  1080. dbg_wl("done");
  1081. mutex_unlock(&ubi->move_mutex);
  1082. return 0;
  1083. /*
  1084. * For some reasons the LEB was not moved, might be an error, might be
  1085. * something else. @e1 was not changed, so return it back. @e2 might
  1086. * have been changed, schedule it for erasure.
  1087. */
  1088. out_not_moved:
  1089. if (vol_id != -1)
  1090. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  1091. e1->pnum, vol_id, lnum, e2->pnum, err);
  1092. else
  1093. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  1094. e1->pnum, e2->pnum, err);
  1095. spin_lock(&ubi->wl_lock);
  1096. if (protect)
  1097. prot_queue_add(ubi, e1);
  1098. else if (erroneous) {
  1099. wl_tree_add(e1, &ubi->erroneous);
  1100. ubi->erroneous_peb_count += 1;
  1101. } else if (scrubbing)
  1102. wl_tree_add(e1, &ubi->scrub);
  1103. else
  1104. wl_tree_add(e1, &ubi->used);
  1105. ubi_assert(!ubi->move_to_put);
  1106. ubi->move_from = ubi->move_to = NULL;
  1107. ubi->wl_scheduled = 0;
  1108. spin_unlock(&ubi->wl_lock);
  1109. ubi_free_vid_hdr(ubi, vid_hdr);
  1110. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  1111. if (err) {
  1112. kmem_cache_free(ubi_wl_entry_slab, e2);
  1113. goto out_ro;
  1114. }
  1115. mutex_unlock(&ubi->move_mutex);
  1116. return 0;
  1117. out_error:
  1118. if (vol_id != -1)
  1119. ubi_err("error %d while moving PEB %d to PEB %d",
  1120. err, e1->pnum, e2->pnum);
  1121. else
  1122. ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  1123. err, e1->pnum, vol_id, lnum, e2->pnum);
  1124. spin_lock(&ubi->wl_lock);
  1125. ubi->move_from = ubi->move_to = NULL;
  1126. ubi->move_to_put = ubi->wl_scheduled = 0;
  1127. spin_unlock(&ubi->wl_lock);
  1128. ubi_free_vid_hdr(ubi, vid_hdr);
  1129. kmem_cache_free(ubi_wl_entry_slab, e1);
  1130. kmem_cache_free(ubi_wl_entry_slab, e2);
  1131. out_ro:
  1132. ubi_ro_mode(ubi);
  1133. mutex_unlock(&ubi->move_mutex);
  1134. ubi_assert(err != 0);
  1135. return err < 0 ? err : -EIO;
  1136. out_cancel:
  1137. ubi->wl_scheduled = 0;
  1138. spin_unlock(&ubi->wl_lock);
  1139. mutex_unlock(&ubi->move_mutex);
  1140. ubi_free_vid_hdr(ubi, vid_hdr);
  1141. return 0;
  1142. }
  1143. /**
  1144. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  1145. * @ubi: UBI device description object
  1146. * @nested: set to non-zero if this function is called from UBI worker
  1147. *
  1148. * This function checks if it is time to start wear-leveling and schedules it
  1149. * if yes. This function returns zero in case of success and a negative error
  1150. * code in case of failure.
  1151. */
  1152. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  1153. {
  1154. int err = 0;
  1155. struct ubi_wl_entry *e1;
  1156. struct ubi_wl_entry *e2;
  1157. struct ubi_work *wrk;
  1158. spin_lock(&ubi->wl_lock);
  1159. if (ubi->wl_scheduled)
  1160. /* Wear-leveling is already in the work queue */
  1161. goto out_unlock;
  1162. /*
  1163. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  1164. * the WL worker has to be scheduled anyway.
  1165. */
  1166. if (!ubi->scrub.rb_node) {
  1167. if (!ubi->used.rb_node || !ubi->free.rb_node)
  1168. /* No physical eraseblocks - no deal */
  1169. goto out_unlock;
  1170. /*
  1171. * We schedule wear-leveling only if the difference between the
  1172. * lowest erase counter of used physical eraseblocks and a high
  1173. * erase counter of free physical eraseblocks is greater than
  1174. * %UBI_WL_THRESHOLD.
  1175. */
  1176. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  1177. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1178. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  1179. goto out_unlock;
  1180. dbg_wl("schedule wear-leveling");
  1181. } else
  1182. dbg_wl("schedule scrubbing");
  1183. ubi->wl_scheduled = 1;
  1184. spin_unlock(&ubi->wl_lock);
  1185. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1186. if (!wrk) {
  1187. err = -ENOMEM;
  1188. goto out_cancel;
  1189. }
  1190. wrk->anchor = 0;
  1191. wrk->func = &wear_leveling_worker;
  1192. if (nested)
  1193. __schedule_ubi_work(ubi, wrk);
  1194. else
  1195. schedule_ubi_work(ubi, wrk);
  1196. return err;
  1197. out_cancel:
  1198. spin_lock(&ubi->wl_lock);
  1199. ubi->wl_scheduled = 0;
  1200. out_unlock:
  1201. spin_unlock(&ubi->wl_lock);
  1202. return err;
  1203. }
  1204. #ifdef CONFIG_MTD_UBI_FASTMAP
  1205. /**
  1206. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  1207. * @ubi: UBI device description object
  1208. */
  1209. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  1210. {
  1211. struct ubi_work *wrk;
  1212. spin_lock(&ubi->wl_lock);
  1213. if (ubi->wl_scheduled) {
  1214. spin_unlock(&ubi->wl_lock);
  1215. return 0;
  1216. }
  1217. ubi->wl_scheduled = 1;
  1218. spin_unlock(&ubi->wl_lock);
  1219. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1220. if (!wrk) {
  1221. spin_lock(&ubi->wl_lock);
  1222. ubi->wl_scheduled = 0;
  1223. spin_unlock(&ubi->wl_lock);
  1224. return -ENOMEM;
  1225. }
  1226. wrk->anchor = 1;
  1227. wrk->func = &wear_leveling_worker;
  1228. schedule_ubi_work(ubi, wrk);
  1229. return 0;
  1230. }
  1231. #endif
  1232. /**
  1233. * erase_worker - physical eraseblock erase worker function.
  1234. * @ubi: UBI device description object
  1235. * @wl_wrk: the work object
  1236. * @shutdown: non-zero if the worker has to free memory and exit
  1237. * because the WL sub-system is shutting down
  1238. *
  1239. * This function erases a physical eraseblock and perform torture testing if
  1240. * needed. It also takes care about marking the physical eraseblock bad if
  1241. * needed. Returns zero in case of success and a negative error code in case of
  1242. * failure.
  1243. */
  1244. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1245. int shutdown)
  1246. {
  1247. struct ubi_wl_entry *e = wl_wrk->e;
  1248. int pnum = e->pnum;
  1249. int vol_id = wl_wrk->vol_id;
  1250. int lnum = wl_wrk->lnum;
  1251. int err, available_consumed = 0;
  1252. if (shutdown) {
  1253. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  1254. kfree(wl_wrk);
  1255. kmem_cache_free(ubi_wl_entry_slab, e);
  1256. return 0;
  1257. }
  1258. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  1259. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  1260. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1261. err = sync_erase(ubi, e, wl_wrk->torture);
  1262. if (!err) {
  1263. /* Fine, we've erased it successfully */
  1264. kfree(wl_wrk);
  1265. spin_lock(&ubi->wl_lock);
  1266. wl_tree_add(e, &ubi->free);
  1267. ubi->free_count++;
  1268. spin_unlock(&ubi->wl_lock);
  1269. /*
  1270. * One more erase operation has happened, take care about
  1271. * protected physical eraseblocks.
  1272. */
  1273. serve_prot_queue(ubi);
  1274. /* And take care about wear-leveling */
  1275. err = ensure_wear_leveling(ubi, 1);
  1276. return err;
  1277. }
  1278. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  1279. kfree(wl_wrk);
  1280. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  1281. err == -EBUSY) {
  1282. int err1;
  1283. /* Re-schedule the LEB for erasure */
  1284. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  1285. if (err1) {
  1286. err = err1;
  1287. goto out_ro;
  1288. }
  1289. return err;
  1290. }
  1291. kmem_cache_free(ubi_wl_entry_slab, e);
  1292. if (err != -EIO)
  1293. /*
  1294. * If this is not %-EIO, we have no idea what to do. Scheduling
  1295. * this physical eraseblock for erasure again would cause
  1296. * errors again and again. Well, lets switch to R/O mode.
  1297. */
  1298. goto out_ro;
  1299. /* It is %-EIO, the PEB went bad */
  1300. if (!ubi->bad_allowed) {
  1301. ubi_err("bad physical eraseblock %d detected", pnum);
  1302. goto out_ro;
  1303. }
  1304. spin_lock(&ubi->volumes_lock);
  1305. if (ubi->beb_rsvd_pebs == 0) {
  1306. if (ubi->avail_pebs == 0) {
  1307. spin_unlock(&ubi->volumes_lock);
  1308. ubi_err("no reserved/available physical eraseblocks");
  1309. goto out_ro;
  1310. }
  1311. ubi->avail_pebs -= 1;
  1312. available_consumed = 1;
  1313. }
  1314. spin_unlock(&ubi->volumes_lock);
  1315. ubi_msg("mark PEB %d as bad", pnum);
  1316. err = ubi_io_mark_bad(ubi, pnum);
  1317. if (err)
  1318. goto out_ro;
  1319. spin_lock(&ubi->volumes_lock);
  1320. if (ubi->beb_rsvd_pebs > 0) {
  1321. if (available_consumed) {
  1322. /*
  1323. * The amount of reserved PEBs increased since we last
  1324. * checked.
  1325. */
  1326. ubi->avail_pebs += 1;
  1327. available_consumed = 0;
  1328. }
  1329. ubi->beb_rsvd_pebs -= 1;
  1330. }
  1331. ubi->bad_peb_count += 1;
  1332. ubi->good_peb_count -= 1;
  1333. ubi_calculate_reserved(ubi);
  1334. if (available_consumed)
  1335. ubi_warn("no PEBs in the reserved pool, used an available PEB");
  1336. else if (ubi->beb_rsvd_pebs)
  1337. ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
  1338. else
  1339. ubi_warn("last PEB from the reserve was used");
  1340. spin_unlock(&ubi->volumes_lock);
  1341. return err;
  1342. out_ro:
  1343. if (available_consumed) {
  1344. spin_lock(&ubi->volumes_lock);
  1345. ubi->avail_pebs += 1;
  1346. spin_unlock(&ubi->volumes_lock);
  1347. }
  1348. ubi_ro_mode(ubi);
  1349. return err;
  1350. }
  1351. /**
  1352. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1353. * @ubi: UBI device description object
  1354. * @vol_id: the volume ID that last used this PEB
  1355. * @lnum: the last used logical eraseblock number for the PEB
  1356. * @pnum: physical eraseblock to return
  1357. * @torture: if this physical eraseblock has to be tortured
  1358. *
  1359. * This function is called to return physical eraseblock @pnum to the pool of
  1360. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1361. * occurred to this @pnum and it has to be tested. This function returns zero
  1362. * in case of success, and a negative error code in case of failure.
  1363. */
  1364. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1365. int pnum, int torture)
  1366. {
  1367. int err;
  1368. struct ubi_wl_entry *e;
  1369. dbg_wl("PEB %d", pnum);
  1370. ubi_assert(pnum >= 0);
  1371. ubi_assert(pnum < ubi->peb_count);
  1372. retry:
  1373. spin_lock(&ubi->wl_lock);
  1374. e = ubi->lookuptbl[pnum];
  1375. if (e == ubi->move_from) {
  1376. /*
  1377. * User is putting the physical eraseblock which was selected to
  1378. * be moved. It will be scheduled for erasure in the
  1379. * wear-leveling worker.
  1380. */
  1381. dbg_wl("PEB %d is being moved, wait", pnum);
  1382. spin_unlock(&ubi->wl_lock);
  1383. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1384. mutex_lock(&ubi->move_mutex);
  1385. mutex_unlock(&ubi->move_mutex);
  1386. goto retry;
  1387. } else if (e == ubi->move_to) {
  1388. /*
  1389. * User is putting the physical eraseblock which was selected
  1390. * as the target the data is moved to. It may happen if the EBA
  1391. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1392. * but the WL sub-system has not put the PEB to the "used" tree
  1393. * yet, but it is about to do this. So we just set a flag which
  1394. * will tell the WL worker that the PEB is not needed anymore
  1395. * and should be scheduled for erasure.
  1396. */
  1397. dbg_wl("PEB %d is the target of data moving", pnum);
  1398. ubi_assert(!ubi->move_to_put);
  1399. ubi->move_to_put = 1;
  1400. spin_unlock(&ubi->wl_lock);
  1401. return 0;
  1402. } else {
  1403. if (in_wl_tree(e, &ubi->used)) {
  1404. self_check_in_wl_tree(ubi, e, &ubi->used);
  1405. rb_erase(&e->u.rb, &ubi->used);
  1406. } else if (in_wl_tree(e, &ubi->scrub)) {
  1407. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1408. rb_erase(&e->u.rb, &ubi->scrub);
  1409. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1410. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1411. rb_erase(&e->u.rb, &ubi->erroneous);
  1412. ubi->erroneous_peb_count -= 1;
  1413. ubi_assert(ubi->erroneous_peb_count >= 0);
  1414. /* Erroneous PEBs should be tortured */
  1415. torture = 1;
  1416. } else {
  1417. err = prot_queue_del(ubi, e->pnum);
  1418. if (err) {
  1419. ubi_err("PEB %d not found", pnum);
  1420. ubi_ro_mode(ubi);
  1421. spin_unlock(&ubi->wl_lock);
  1422. return err;
  1423. }
  1424. }
  1425. }
  1426. spin_unlock(&ubi->wl_lock);
  1427. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1428. if (err) {
  1429. spin_lock(&ubi->wl_lock);
  1430. wl_tree_add(e, &ubi->used);
  1431. spin_unlock(&ubi->wl_lock);
  1432. }
  1433. return err;
  1434. }
  1435. /**
  1436. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1437. * @ubi: UBI device description object
  1438. * @pnum: the physical eraseblock to schedule
  1439. *
  1440. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1441. * needs scrubbing. This function schedules a physical eraseblock for
  1442. * scrubbing which is done in background. This function returns zero in case of
  1443. * success and a negative error code in case of failure.
  1444. */
  1445. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1446. {
  1447. struct ubi_wl_entry *e;
  1448. ubi_msg("schedule PEB %d for scrubbing", pnum);
  1449. retry:
  1450. spin_lock(&ubi->wl_lock);
  1451. e = ubi->lookuptbl[pnum];
  1452. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1453. in_wl_tree(e, &ubi->erroneous)) {
  1454. spin_unlock(&ubi->wl_lock);
  1455. return 0;
  1456. }
  1457. if (e == ubi->move_to) {
  1458. /*
  1459. * This physical eraseblock was used to move data to. The data
  1460. * was moved but the PEB was not yet inserted to the proper
  1461. * tree. We should just wait a little and let the WL worker
  1462. * proceed.
  1463. */
  1464. spin_unlock(&ubi->wl_lock);
  1465. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1466. yield();
  1467. goto retry;
  1468. }
  1469. if (in_wl_tree(e, &ubi->used)) {
  1470. self_check_in_wl_tree(ubi, e, &ubi->used);
  1471. rb_erase(&e->u.rb, &ubi->used);
  1472. } else {
  1473. int err;
  1474. err = prot_queue_del(ubi, e->pnum);
  1475. if (err) {
  1476. ubi_err("PEB %d not found", pnum);
  1477. ubi_ro_mode(ubi);
  1478. spin_unlock(&ubi->wl_lock);
  1479. return err;
  1480. }
  1481. }
  1482. wl_tree_add(e, &ubi->scrub);
  1483. spin_unlock(&ubi->wl_lock);
  1484. /*
  1485. * Technically scrubbing is the same as wear-leveling, so it is done
  1486. * by the WL worker.
  1487. */
  1488. return ensure_wear_leveling(ubi, 0);
  1489. }
  1490. /**
  1491. * ubi_wl_flush - flush all pending works.
  1492. * @ubi: UBI device description object
  1493. * @vol_id: the volume id to flush for
  1494. * @lnum: the logical eraseblock number to flush for
  1495. *
  1496. * This function executes all pending works for a particular volume id /
  1497. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1498. * acts as a wildcard for all of the corresponding volume numbers or logical
  1499. * eraseblock numbers. It returns zero in case of success and a negative error
  1500. * code in case of failure.
  1501. */
  1502. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1503. {
  1504. int err = 0;
  1505. int found = 1;
  1506. /*
  1507. * Erase while the pending works queue is not empty, but not more than
  1508. * the number of currently pending works.
  1509. */
  1510. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1511. vol_id, lnum, ubi->works_count);
  1512. while (found) {
  1513. struct ubi_work *wrk, *tmp;
  1514. found = 0;
  1515. down_read(&ubi->work_sem);
  1516. spin_lock(&ubi->wl_lock);
  1517. list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
  1518. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1519. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1520. list_del(&wrk->list);
  1521. ubi->works_count -= 1;
  1522. ubi_assert(ubi->works_count >= 0);
  1523. spin_unlock(&ubi->wl_lock);
  1524. err = wrk->func(ubi, wrk, 0);
  1525. if (err) {
  1526. up_read(&ubi->work_sem);
  1527. return err;
  1528. }
  1529. spin_lock(&ubi->wl_lock);
  1530. found = 1;
  1531. break;
  1532. }
  1533. }
  1534. spin_unlock(&ubi->wl_lock);
  1535. up_read(&ubi->work_sem);
  1536. }
  1537. /*
  1538. * Make sure all the works which have been done in parallel are
  1539. * finished.
  1540. */
  1541. down_write(&ubi->work_sem);
  1542. up_write(&ubi->work_sem);
  1543. return err;
  1544. }
  1545. /**
  1546. * tree_destroy - destroy an RB-tree.
  1547. * @root: the root of the tree to destroy
  1548. */
  1549. static void tree_destroy(struct rb_root *root)
  1550. {
  1551. struct rb_node *rb;
  1552. struct ubi_wl_entry *e;
  1553. rb = root->rb_node;
  1554. while (rb) {
  1555. if (rb->rb_left)
  1556. rb = rb->rb_left;
  1557. else if (rb->rb_right)
  1558. rb = rb->rb_right;
  1559. else {
  1560. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1561. rb = rb_parent(rb);
  1562. if (rb) {
  1563. if (rb->rb_left == &e->u.rb)
  1564. rb->rb_left = NULL;
  1565. else
  1566. rb->rb_right = NULL;
  1567. }
  1568. kmem_cache_free(ubi_wl_entry_slab, e);
  1569. }
  1570. }
  1571. }
  1572. /**
  1573. * ubi_thread - UBI background thread.
  1574. * @u: the UBI device description object pointer
  1575. */
  1576. int ubi_thread(void *u)
  1577. {
  1578. int failures = 0;
  1579. struct ubi_device *ubi = u;
  1580. ubi_msg("background thread \"%s\" started, PID %d",
  1581. ubi->bgt_name, task_pid_nr(current));
  1582. set_freezable();
  1583. for (;;) {
  1584. int err;
  1585. if (kthread_should_stop())
  1586. break;
  1587. if (try_to_freeze())
  1588. continue;
  1589. spin_lock(&ubi->wl_lock);
  1590. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1591. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1592. set_current_state(TASK_INTERRUPTIBLE);
  1593. spin_unlock(&ubi->wl_lock);
  1594. schedule();
  1595. continue;
  1596. }
  1597. spin_unlock(&ubi->wl_lock);
  1598. err = do_work(ubi);
  1599. if (err) {
  1600. ubi_err("%s: work failed with error code %d",
  1601. ubi->bgt_name, err);
  1602. if (failures++ > WL_MAX_FAILURES) {
  1603. /*
  1604. * Too many failures, disable the thread and
  1605. * switch to read-only mode.
  1606. */
  1607. ubi_msg("%s: %d consecutive failures",
  1608. ubi->bgt_name, WL_MAX_FAILURES);
  1609. ubi_ro_mode(ubi);
  1610. ubi->thread_enabled = 0;
  1611. continue;
  1612. }
  1613. } else
  1614. failures = 0;
  1615. cond_resched();
  1616. }
  1617. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1618. return 0;
  1619. }
  1620. /**
  1621. * shutdown_work - shutdown all pending works.
  1622. * @ubi: UBI device description object
  1623. */
  1624. static void shutdown_work(struct ubi_device *ubi)
  1625. {
  1626. while (!list_empty(&ubi->works)) {
  1627. struct ubi_work *wrk;
  1628. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1629. list_del(&wrk->list);
  1630. wrk->func(ubi, wrk, 1);
  1631. ubi->works_count -= 1;
  1632. ubi_assert(ubi->works_count >= 0);
  1633. }
  1634. }
  1635. /**
  1636. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1637. * @ubi: UBI device description object
  1638. * @ai: attaching information
  1639. *
  1640. * This function returns zero in case of success, and a negative error code in
  1641. * case of failure.
  1642. */
  1643. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1644. {
  1645. int err, i, reserved_pebs, found_pebs = 0;
  1646. struct rb_node *rb1, *rb2;
  1647. struct ubi_ainf_volume *av;
  1648. struct ubi_ainf_peb *aeb, *tmp;
  1649. struct ubi_wl_entry *e;
  1650. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1651. spin_lock_init(&ubi->wl_lock);
  1652. mutex_init(&ubi->move_mutex);
  1653. init_rwsem(&ubi->work_sem);
  1654. ubi->max_ec = ai->max_ec;
  1655. INIT_LIST_HEAD(&ubi->works);
  1656. #ifdef CONFIG_MTD_UBI_FASTMAP
  1657. INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
  1658. #endif
  1659. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1660. err = -ENOMEM;
  1661. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1662. if (!ubi->lookuptbl)
  1663. return err;
  1664. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1665. INIT_LIST_HEAD(&ubi->pq[i]);
  1666. ubi->pq_head = 0;
  1667. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1668. cond_resched();
  1669. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1670. if (!e)
  1671. goto out_free;
  1672. e->pnum = aeb->pnum;
  1673. e->ec = aeb->ec;
  1674. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1675. ubi->lookuptbl[e->pnum] = e;
  1676. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1677. kmem_cache_free(ubi_wl_entry_slab, e);
  1678. goto out_free;
  1679. }
  1680. found_pebs++;
  1681. }
  1682. ubi->free_count = 0;
  1683. list_for_each_entry(aeb, &ai->free, u.list) {
  1684. cond_resched();
  1685. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1686. if (!e)
  1687. goto out_free;
  1688. e->pnum = aeb->pnum;
  1689. e->ec = aeb->ec;
  1690. ubi_assert(e->ec >= 0);
  1691. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1692. wl_tree_add(e, &ubi->free);
  1693. ubi->free_count++;
  1694. ubi->lookuptbl[e->pnum] = e;
  1695. found_pebs++;
  1696. }
  1697. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1698. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1699. cond_resched();
  1700. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1701. if (!e)
  1702. goto out_free;
  1703. e->pnum = aeb->pnum;
  1704. e->ec = aeb->ec;
  1705. ubi->lookuptbl[e->pnum] = e;
  1706. if (!aeb->scrub) {
  1707. dbg_wl("add PEB %d EC %d to the used tree",
  1708. e->pnum, e->ec);
  1709. wl_tree_add(e, &ubi->used);
  1710. } else {
  1711. dbg_wl("add PEB %d EC %d to the scrub tree",
  1712. e->pnum, e->ec);
  1713. wl_tree_add(e, &ubi->scrub);
  1714. }
  1715. found_pebs++;
  1716. }
  1717. }
  1718. dbg_wl("found %i PEBs", found_pebs);
  1719. if (ubi->fm)
  1720. ubi_assert(ubi->good_peb_count == \
  1721. found_pebs + ubi->fm->used_blocks);
  1722. else
  1723. ubi_assert(ubi->good_peb_count == found_pebs);
  1724. reserved_pebs = WL_RESERVED_PEBS;
  1725. #ifdef CONFIG_MTD_UBI_FASTMAP
  1726. /* Reserve enough LEBs to store two fastmaps. */
  1727. reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
  1728. #endif
  1729. if (ubi->avail_pebs < reserved_pebs) {
  1730. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1731. ubi->avail_pebs, reserved_pebs);
  1732. if (ubi->corr_peb_count)
  1733. ubi_err("%d PEBs are corrupted and not used",
  1734. ubi->corr_peb_count);
  1735. goto out_free;
  1736. }
  1737. ubi->avail_pebs -= reserved_pebs;
  1738. ubi->rsvd_pebs += reserved_pebs;
  1739. /* Schedule wear-leveling if needed */
  1740. err = ensure_wear_leveling(ubi, 0);
  1741. if (err)
  1742. goto out_free;
  1743. return 0;
  1744. out_free:
  1745. shutdown_work(ubi);
  1746. tree_destroy(&ubi->used);
  1747. tree_destroy(&ubi->free);
  1748. tree_destroy(&ubi->scrub);
  1749. kfree(ubi->lookuptbl);
  1750. return err;
  1751. }
  1752. /**
  1753. * protection_queue_destroy - destroy the protection queue.
  1754. * @ubi: UBI device description object
  1755. */
  1756. static void protection_queue_destroy(struct ubi_device *ubi)
  1757. {
  1758. int i;
  1759. struct ubi_wl_entry *e, *tmp;
  1760. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1761. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1762. list_del(&e->u.list);
  1763. kmem_cache_free(ubi_wl_entry_slab, e);
  1764. }
  1765. }
  1766. }
  1767. /**
  1768. * ubi_wl_close - close the wear-leveling sub-system.
  1769. * @ubi: UBI device description object
  1770. */
  1771. void ubi_wl_close(struct ubi_device *ubi)
  1772. {
  1773. dbg_wl("close the WL sub-system");
  1774. shutdown_work(ubi);
  1775. protection_queue_destroy(ubi);
  1776. tree_destroy(&ubi->used);
  1777. tree_destroy(&ubi->erroneous);
  1778. tree_destroy(&ubi->free);
  1779. tree_destroy(&ubi->scrub);
  1780. kfree(ubi->lookuptbl);
  1781. }
  1782. /**
  1783. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1784. * @ubi: UBI device description object
  1785. * @pnum: the physical eraseblock number to check
  1786. * @ec: the erase counter to check
  1787. *
  1788. * This function returns zero if the erase counter of physical eraseblock @pnum
  1789. * is equivalent to @ec, and a negative error code if not or if an error
  1790. * occurred.
  1791. */
  1792. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1793. {
  1794. int err;
  1795. long long read_ec;
  1796. struct ubi_ec_hdr *ec_hdr;
  1797. if (!ubi_dbg_chk_gen(ubi))
  1798. return 0;
  1799. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1800. if (!ec_hdr)
  1801. return -ENOMEM;
  1802. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1803. if (err && err != UBI_IO_BITFLIPS) {
  1804. /* The header does not have to exist */
  1805. err = 0;
  1806. goto out_free;
  1807. }
  1808. read_ec = be64_to_cpu(ec_hdr->ec);
  1809. if (ec != read_ec && read_ec - ec > 1) {
  1810. ubi_err("self-check failed for PEB %d", pnum);
  1811. ubi_err("read EC is %lld, should be %d", read_ec, ec);
  1812. dump_stack();
  1813. err = 1;
  1814. } else
  1815. err = 0;
  1816. out_free:
  1817. kfree(ec_hdr);
  1818. return err;
  1819. }
  1820. /**
  1821. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1822. * @ubi: UBI device description object
  1823. * @e: the wear-leveling entry to check
  1824. * @root: the root of the tree
  1825. *
  1826. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1827. * is not.
  1828. */
  1829. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1830. struct ubi_wl_entry *e, struct rb_root *root)
  1831. {
  1832. if (!ubi_dbg_chk_gen(ubi))
  1833. return 0;
  1834. if (in_wl_tree(e, root))
  1835. return 0;
  1836. ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
  1837. e->pnum, e->ec, root);
  1838. dump_stack();
  1839. return -EINVAL;
  1840. }
  1841. /**
  1842. * self_check_in_pq - check if wear-leveling entry is in the protection
  1843. * queue.
  1844. * @ubi: UBI device description object
  1845. * @e: the wear-leveling entry to check
  1846. *
  1847. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1848. */
  1849. static int self_check_in_pq(const struct ubi_device *ubi,
  1850. struct ubi_wl_entry *e)
  1851. {
  1852. struct ubi_wl_entry *p;
  1853. int i;
  1854. if (!ubi_dbg_chk_gen(ubi))
  1855. return 0;
  1856. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1857. list_for_each_entry(p, &ubi->pq[i], u.list)
  1858. if (p == e)
  1859. return 0;
  1860. ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
  1861. e->pnum, e->ec);
  1862. dump_stack();
  1863. return -EINVAL;
  1864. }