wl.c 56 KB

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