wl.c 57 KB

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