wl.c 56 KB

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