wl.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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.
  441. */
  442. static int __wl_get_peb(struct ubi_device *ubi)
  443. {
  444. int err;
  445. struct ubi_wl_entry *e;
  446. retry:
  447. if (!ubi->free.rb_node) {
  448. if (ubi->works_count == 0) {
  449. ubi_err("no free eraseblocks");
  450. ubi_assert(list_empty(&ubi->works));
  451. return -ENOSPC;
  452. }
  453. err = produce_free_peb(ubi);
  454. if (err < 0)
  455. return err;
  456. goto retry;
  457. }
  458. e = find_mean_wl_entry(ubi, &ubi->free);
  459. if (!e) {
  460. ubi_err("no free eraseblocks");
  461. return -ENOSPC;
  462. }
  463. self_check_in_wl_tree(ubi, e, &ubi->free);
  464. /*
  465. * Move the physical eraseblock to the protection queue where it will
  466. * be protected from being moved for some time.
  467. */
  468. rb_erase(&e->u.rb, &ubi->free);
  469. ubi->free_count--;
  470. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  471. #ifndef CONFIG_MTD_UBI_FASTMAP
  472. /* We have to enqueue e only if fastmap is disabled,
  473. * is fastmap enabled prot_queue_add() will be called by
  474. * ubi_wl_get_peb() after removing e from the pool. */
  475. prot_queue_add(ubi, e);
  476. #endif
  477. return e->pnum;
  478. }
  479. #ifdef CONFIG_MTD_UBI_FASTMAP
  480. /**
  481. * return_unused_pool_pebs - returns unused PEB to the free tree.
  482. * @ubi: UBI device description object
  483. * @pool: fastmap pool description object
  484. */
  485. static void return_unused_pool_pebs(struct ubi_device *ubi,
  486. struct ubi_fm_pool *pool)
  487. {
  488. int i;
  489. struct ubi_wl_entry *e;
  490. for (i = pool->used; i < pool->size; i++) {
  491. e = ubi->lookuptbl[pool->pebs[i]];
  492. wl_tree_add(e, &ubi->free);
  493. ubi->free_count++;
  494. }
  495. }
  496. /**
  497. * refill_wl_pool - refills all the fastmap pool used by the
  498. * WL sub-system.
  499. * @ubi: UBI device description object
  500. */
  501. static void refill_wl_pool(struct ubi_device *ubi)
  502. {
  503. struct ubi_wl_entry *e;
  504. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  505. return_unused_pool_pebs(ubi, pool);
  506. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  507. if (!ubi->free.rb_node ||
  508. (ubi->free_count - ubi->beb_rsvd_pebs < 5))
  509. break;
  510. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  511. self_check_in_wl_tree(ubi, e, &ubi->free);
  512. rb_erase(&e->u.rb, &ubi->free);
  513. ubi->free_count--;
  514. pool->pebs[pool->size] = e->pnum;
  515. }
  516. pool->used = 0;
  517. }
  518. /**
  519. * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
  520. * @ubi: UBI device description object
  521. */
  522. static void refill_wl_user_pool(struct ubi_device *ubi)
  523. {
  524. struct ubi_fm_pool *pool = &ubi->fm_pool;
  525. return_unused_pool_pebs(ubi, pool);
  526. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  527. if (!ubi->free.rb_node ||
  528. (ubi->free_count - ubi->beb_rsvd_pebs < 1))
  529. break;
  530. pool->pebs[pool->size] = __wl_get_peb(ubi);
  531. if (pool->pebs[pool->size] < 0)
  532. break;
  533. }
  534. pool->used = 0;
  535. }
  536. /**
  537. * ubi_refill_pools - refills all fastmap PEB pools.
  538. * @ubi: UBI device description object
  539. */
  540. void ubi_refill_pools(struct ubi_device *ubi)
  541. {
  542. spin_lock(&ubi->wl_lock);
  543. refill_wl_pool(ubi);
  544. refill_wl_user_pool(ubi);
  545. spin_unlock(&ubi->wl_lock);
  546. }
  547. /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
  548. * the fastmap pool.
  549. */
  550. int ubi_wl_get_peb(struct ubi_device *ubi)
  551. {
  552. int ret;
  553. struct ubi_fm_pool *pool = &ubi->fm_pool;
  554. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  555. if (!pool->size || !wl_pool->size || pool->used == pool->size ||
  556. wl_pool->used == wl_pool->size)
  557. ubi_update_fastmap(ubi);
  558. /* we got not a single free PEB */
  559. if (!pool->size)
  560. ret = -ENOSPC;
  561. else {
  562. spin_lock(&ubi->wl_lock);
  563. ret = pool->pebs[pool->used++];
  564. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  565. spin_unlock(&ubi->wl_lock);
  566. }
  567. return ret;
  568. }
  569. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  570. *
  571. * @ubi: UBI device description object
  572. */
  573. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  574. {
  575. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  576. int pnum;
  577. if (pool->used == pool->size || !pool->size) {
  578. /* We cannot update the fastmap here because this
  579. * function is called in atomic context.
  580. * Let's fail here and refill/update it as soon as possible. */
  581. schedule_work(&ubi->fm_work);
  582. return NULL;
  583. } else {
  584. pnum = pool->pebs[pool->used++];
  585. return ubi->lookuptbl[pnum];
  586. }
  587. }
  588. #else
  589. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  590. {
  591. struct ubi_wl_entry *e;
  592. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  593. self_check_in_wl_tree(ubi, e, &ubi->free);
  594. rb_erase(&e->u.rb, &ubi->free);
  595. return e;
  596. }
  597. int ubi_wl_get_peb(struct ubi_device *ubi)
  598. {
  599. int peb, err;
  600. spin_lock(&ubi->wl_lock);
  601. peb = __wl_get_peb(ubi);
  602. spin_unlock(&ubi->wl_lock);
  603. err = ubi_self_check_all_ff(ubi, peb, ubi->vid_hdr_aloffset,
  604. ubi->peb_size - ubi->vid_hdr_aloffset);
  605. if (err) {
  606. ubi_err("new PEB %d does not contain all 0xFF bytes", peb);
  607. return err;
  608. }
  609. return peb;
  610. }
  611. #endif
  612. /**
  613. * prot_queue_del - remove a physical eraseblock from the protection queue.
  614. * @ubi: UBI device description object
  615. * @pnum: the physical eraseblock to remove
  616. *
  617. * This function deletes PEB @pnum from the protection queue and returns zero
  618. * in case of success and %-ENODEV if the PEB was not found.
  619. */
  620. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  621. {
  622. struct ubi_wl_entry *e;
  623. e = ubi->lookuptbl[pnum];
  624. if (!e)
  625. return -ENODEV;
  626. if (self_check_in_pq(ubi, e))
  627. return -ENODEV;
  628. list_del(&e->u.list);
  629. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  630. return 0;
  631. }
  632. /**
  633. * sync_erase - synchronously erase a physical eraseblock.
  634. * @ubi: UBI device description object
  635. * @e: the the physical eraseblock to erase
  636. * @torture: if the physical eraseblock has to be tortured
  637. *
  638. * This function returns zero in case of success and a negative error code in
  639. * case of failure.
  640. */
  641. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  642. int torture)
  643. {
  644. int err;
  645. struct ubi_ec_hdr *ec_hdr;
  646. unsigned long long ec = e->ec;
  647. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  648. err = self_check_ec(ubi, e->pnum, e->ec);
  649. if (err)
  650. return -EINVAL;
  651. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  652. if (!ec_hdr)
  653. return -ENOMEM;
  654. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  655. if (err < 0)
  656. goto out_free;
  657. ec += err;
  658. if (ec > UBI_MAX_ERASECOUNTER) {
  659. /*
  660. * Erase counter overflow. Upgrade UBI and use 64-bit
  661. * erase counters internally.
  662. */
  663. ubi_err("erase counter overflow at PEB %d, EC %llu",
  664. e->pnum, ec);
  665. err = -EINVAL;
  666. goto out_free;
  667. }
  668. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  669. ec_hdr->ec = cpu_to_be64(ec);
  670. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  671. if (err)
  672. goto out_free;
  673. e->ec = ec;
  674. spin_lock(&ubi->wl_lock);
  675. if (e->ec > ubi->max_ec)
  676. ubi->max_ec = e->ec;
  677. spin_unlock(&ubi->wl_lock);
  678. out_free:
  679. kfree(ec_hdr);
  680. return err;
  681. }
  682. /**
  683. * serve_prot_queue - check if it is time to stop protecting PEBs.
  684. * @ubi: UBI device description object
  685. *
  686. * This function is called after each erase operation and removes PEBs from the
  687. * tail of the protection queue. These PEBs have been protected for long enough
  688. * and should be moved to the used tree.
  689. */
  690. static void serve_prot_queue(struct ubi_device *ubi)
  691. {
  692. struct ubi_wl_entry *e, *tmp;
  693. int count;
  694. /*
  695. * There may be several protected physical eraseblock to remove,
  696. * process them all.
  697. */
  698. repeat:
  699. count = 0;
  700. spin_lock(&ubi->wl_lock);
  701. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  702. dbg_wl("PEB %d EC %d protection over, move to used tree",
  703. e->pnum, e->ec);
  704. list_del(&e->u.list);
  705. wl_tree_add(e, &ubi->used);
  706. if (count++ > 32) {
  707. /*
  708. * Let's be nice and avoid holding the spinlock for
  709. * too long.
  710. */
  711. spin_unlock(&ubi->wl_lock);
  712. cond_resched();
  713. goto repeat;
  714. }
  715. }
  716. ubi->pq_head += 1;
  717. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  718. ubi->pq_head = 0;
  719. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  720. spin_unlock(&ubi->wl_lock);
  721. }
  722. /**
  723. * __schedule_ubi_work - schedule a work.
  724. * @ubi: UBI device description object
  725. * @wrk: the work to schedule
  726. *
  727. * This function adds a work defined by @wrk to the tail of the pending works
  728. * list. Can only be used of ubi->work_sem is already held in read mode!
  729. */
  730. static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  731. {
  732. spin_lock(&ubi->wl_lock);
  733. list_add_tail(&wrk->list, &ubi->works);
  734. ubi_assert(ubi->works_count >= 0);
  735. ubi->works_count += 1;
  736. if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
  737. wake_up_process(ubi->bgt_thread);
  738. spin_unlock(&ubi->wl_lock);
  739. }
  740. /**
  741. * schedule_ubi_work - schedule a work.
  742. * @ubi: UBI device description object
  743. * @wrk: the work to schedule
  744. *
  745. * This function adds a work defined by @wrk to the tail of the pending works
  746. * list.
  747. */
  748. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  749. {
  750. down_read(&ubi->work_sem);
  751. __schedule_ubi_work(ubi, wrk);
  752. up_read(&ubi->work_sem);
  753. }
  754. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  755. int cancel);
  756. #ifdef CONFIG_MTD_UBI_FASTMAP
  757. /**
  758. * ubi_is_erase_work - checks whether a work is erase work.
  759. * @wrk: The work object to be checked
  760. */
  761. int ubi_is_erase_work(struct ubi_work *wrk)
  762. {
  763. return wrk->func == erase_worker;
  764. }
  765. #endif
  766. /**
  767. * schedule_erase - schedule an erase work.
  768. * @ubi: UBI device description object
  769. * @e: the WL entry of the physical eraseblock to erase
  770. * @vol_id: the volume ID that last used this PEB
  771. * @lnum: the last used logical eraseblock number for the PEB
  772. * @torture: if the physical eraseblock has to be tortured
  773. *
  774. * This function returns zero in case of success and a %-ENOMEM in case of
  775. * failure.
  776. */
  777. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  778. int vol_id, int lnum, int torture)
  779. {
  780. struct ubi_work *wl_wrk;
  781. ubi_assert(e);
  782. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  783. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  784. e->pnum, e->ec, torture);
  785. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  786. if (!wl_wrk)
  787. return -ENOMEM;
  788. wl_wrk->func = &erase_worker;
  789. wl_wrk->e = e;
  790. wl_wrk->vol_id = vol_id;
  791. wl_wrk->lnum = lnum;
  792. wl_wrk->torture = torture;
  793. schedule_ubi_work(ubi, wl_wrk);
  794. return 0;
  795. }
  796. /**
  797. * do_sync_erase - run the erase worker synchronously.
  798. * @ubi: UBI device description object
  799. * @e: the WL entry of the physical eraseblock to erase
  800. * @vol_id: the volume ID that last used this PEB
  801. * @lnum: the last used logical eraseblock number for the PEB
  802. * @torture: if the physical eraseblock has to be tortured
  803. *
  804. */
  805. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  806. int vol_id, int lnum, int torture)
  807. {
  808. struct ubi_work *wl_wrk;
  809. dbg_wl("sync erase of PEB %i", e->pnum);
  810. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  811. if (!wl_wrk)
  812. return -ENOMEM;
  813. wl_wrk->e = e;
  814. wl_wrk->vol_id = vol_id;
  815. wl_wrk->lnum = lnum;
  816. wl_wrk->torture = torture;
  817. return erase_worker(ubi, wl_wrk, 0);
  818. }
  819. #ifdef CONFIG_MTD_UBI_FASTMAP
  820. /**
  821. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  822. * sub-system.
  823. * see: ubi_wl_put_peb()
  824. *
  825. * @ubi: UBI device description object
  826. * @fm_e: physical eraseblock to return
  827. * @lnum: the last used logical eraseblock number for the PEB
  828. * @torture: if this physical eraseblock has to be tortured
  829. */
  830. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  831. int lnum, int torture)
  832. {
  833. struct ubi_wl_entry *e;
  834. int vol_id, pnum = fm_e->pnum;
  835. dbg_wl("PEB %d", pnum);
  836. ubi_assert(pnum >= 0);
  837. ubi_assert(pnum < ubi->peb_count);
  838. spin_lock(&ubi->wl_lock);
  839. e = ubi->lookuptbl[pnum];
  840. /* This can happen if we recovered from a fastmap the very
  841. * first time and writing now a new one. In this case the wl system
  842. * has never seen any PEB used by the original fastmap.
  843. */
  844. if (!e) {
  845. e = fm_e;
  846. ubi_assert(e->ec >= 0);
  847. ubi->lookuptbl[pnum] = e;
  848. } else {
  849. e->ec = fm_e->ec;
  850. kfree(fm_e);
  851. }
  852. spin_unlock(&ubi->wl_lock);
  853. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  854. return schedule_erase(ubi, e, vol_id, lnum, torture);
  855. }
  856. #endif
  857. /**
  858. * wear_leveling_worker - wear-leveling worker function.
  859. * @ubi: UBI device description object
  860. * @wrk: the work object
  861. * @cancel: non-zero if the worker has to free memory and exit
  862. *
  863. * This function copies a more worn out physical eraseblock to a less worn out
  864. * one. Returns zero in case of success and a negative error code in case of
  865. * failure.
  866. */
  867. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  868. int cancel)
  869. {
  870. int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  871. int vol_id = -1, uninitialized_var(lnum);
  872. #ifdef CONFIG_MTD_UBI_FASTMAP
  873. int anchor = wrk->anchor;
  874. #endif
  875. struct ubi_wl_entry *e1, *e2;
  876. struct ubi_vid_hdr *vid_hdr;
  877. kfree(wrk);
  878. if (cancel)
  879. return 0;
  880. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  881. if (!vid_hdr)
  882. return -ENOMEM;
  883. mutex_lock(&ubi->move_mutex);
  884. spin_lock(&ubi->wl_lock);
  885. ubi_assert(!ubi->move_from && !ubi->move_to);
  886. ubi_assert(!ubi->move_to_put);
  887. if (!ubi->free.rb_node ||
  888. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  889. /*
  890. * No free physical eraseblocks? Well, they must be waiting in
  891. * the queue to be erased. Cancel movement - it will be
  892. * triggered again when a free physical eraseblock appears.
  893. *
  894. * No used physical eraseblocks? They must be temporarily
  895. * protected from being moved. They will be moved to the
  896. * @ubi->used tree later and the wear-leveling will be
  897. * triggered again.
  898. */
  899. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  900. !ubi->free.rb_node, !ubi->used.rb_node);
  901. goto out_cancel;
  902. }
  903. #ifdef CONFIG_MTD_UBI_FASTMAP
  904. /* Check whether we need to produce an anchor PEB */
  905. if (!anchor)
  906. anchor = !anchor_pebs_avalible(&ubi->free);
  907. if (anchor) {
  908. e1 = find_anchor_wl_entry(&ubi->used);
  909. if (!e1)
  910. goto out_cancel;
  911. e2 = get_peb_for_wl(ubi);
  912. if (!e2)
  913. goto out_cancel;
  914. self_check_in_wl_tree(ubi, e1, &ubi->used);
  915. rb_erase(&e1->u.rb, &ubi->used);
  916. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  917. } else if (!ubi->scrub.rb_node) {
  918. #else
  919. if (!ubi->scrub.rb_node) {
  920. #endif
  921. /*
  922. * Now pick the least worn-out used physical eraseblock and a
  923. * highly worn-out free physical eraseblock. If the erase
  924. * counters differ much enough, start wear-leveling.
  925. */
  926. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  927. e2 = get_peb_for_wl(ubi);
  928. if (!e2)
  929. goto out_cancel;
  930. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  931. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  932. e1->ec, e2->ec);
  933. goto out_cancel;
  934. }
  935. self_check_in_wl_tree(ubi, e1, &ubi->used);
  936. rb_erase(&e1->u.rb, &ubi->used);
  937. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  938. e1->pnum, e1->ec, e2->pnum, e2->ec);
  939. } else {
  940. /* Perform scrubbing */
  941. scrubbing = 1;
  942. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  943. e2 = get_peb_for_wl(ubi);
  944. if (!e2)
  945. goto out_cancel;
  946. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  947. rb_erase(&e1->u.rb, &ubi->scrub);
  948. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  949. }
  950. ubi->move_from = e1;
  951. ubi->move_to = e2;
  952. spin_unlock(&ubi->wl_lock);
  953. /*
  954. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  955. * We so far do not know which logical eraseblock our physical
  956. * eraseblock (@e1) belongs to. We have to read the volume identifier
  957. * header first.
  958. *
  959. * Note, we are protected from this PEB being unmapped and erased. The
  960. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  961. * which is being moved was unmapped.
  962. */
  963. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  964. if (err && err != UBI_IO_BITFLIPS) {
  965. if (err == UBI_IO_FF) {
  966. /*
  967. * We are trying to move PEB without a VID header. UBI
  968. * always write VID headers shortly after the PEB was
  969. * given, so we have a situation when it has not yet
  970. * had a chance to write it, because it was preempted.
  971. * So add this PEB to the protection queue so far,
  972. * because presumably more data will be written there
  973. * (including the missing VID header), and then we'll
  974. * move it.
  975. */
  976. dbg_wl("PEB %d has no VID header", e1->pnum);
  977. protect = 1;
  978. goto out_not_moved;
  979. } else if (err == UBI_IO_FF_BITFLIPS) {
  980. /*
  981. * The same situation as %UBI_IO_FF, but bit-flips were
  982. * detected. It is better to schedule this PEB for
  983. * scrubbing.
  984. */
  985. dbg_wl("PEB %d has no VID header but has bit-flips",
  986. e1->pnum);
  987. scrubbing = 1;
  988. goto out_not_moved;
  989. }
  990. ubi_err("error %d while reading VID header from PEB %d",
  991. err, e1->pnum);
  992. goto out_error;
  993. }
  994. vol_id = be32_to_cpu(vid_hdr->vol_id);
  995. lnum = be32_to_cpu(vid_hdr->lnum);
  996. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  997. if (err) {
  998. if (err == MOVE_CANCEL_RACE) {
  999. /*
  1000. * The LEB has not been moved because the volume is
  1001. * being deleted or the PEB has been put meanwhile. We
  1002. * should prevent this PEB from being selected for
  1003. * wear-leveling movement again, so put it to the
  1004. * protection queue.
  1005. */
  1006. protect = 1;
  1007. goto out_not_moved;
  1008. }
  1009. if (err == MOVE_RETRY) {
  1010. scrubbing = 1;
  1011. goto out_not_moved;
  1012. }
  1013. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  1014. err == MOVE_TARGET_RD_ERR) {
  1015. /*
  1016. * Target PEB had bit-flips or write error - torture it.
  1017. */
  1018. torture = 1;
  1019. goto out_not_moved;
  1020. }
  1021. if (err == MOVE_SOURCE_RD_ERR) {
  1022. /*
  1023. * An error happened while reading the source PEB. Do
  1024. * not switch to R/O mode in this case, and give the
  1025. * upper layers a possibility to recover from this,
  1026. * e.g. by unmapping corresponding LEB. Instead, just
  1027. * put this PEB to the @ubi->erroneous list to prevent
  1028. * UBI from trying to move it over and over again.
  1029. */
  1030. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  1031. ubi_err("too many erroneous eraseblocks (%d)",
  1032. ubi->erroneous_peb_count);
  1033. goto out_error;
  1034. }
  1035. erroneous = 1;
  1036. goto out_not_moved;
  1037. }
  1038. if (err < 0)
  1039. goto out_error;
  1040. ubi_assert(0);
  1041. }
  1042. /* The PEB has been successfully moved */
  1043. if (scrubbing)
  1044. ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  1045. e1->pnum, vol_id, lnum, e2->pnum);
  1046. ubi_free_vid_hdr(ubi, vid_hdr);
  1047. spin_lock(&ubi->wl_lock);
  1048. if (!ubi->move_to_put) {
  1049. wl_tree_add(e2, &ubi->used);
  1050. e2 = NULL;
  1051. }
  1052. ubi->move_from = ubi->move_to = NULL;
  1053. ubi->move_to_put = ubi->wl_scheduled = 0;
  1054. spin_unlock(&ubi->wl_lock);
  1055. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  1056. if (err) {
  1057. kmem_cache_free(ubi_wl_entry_slab, e1);
  1058. if (e2)
  1059. kmem_cache_free(ubi_wl_entry_slab, e2);
  1060. goto out_ro;
  1061. }
  1062. if (e2) {
  1063. /*
  1064. * Well, the target PEB was put meanwhile, schedule it for
  1065. * erasure.
  1066. */
  1067. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  1068. e2->pnum, vol_id, lnum);
  1069. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  1070. if (err) {
  1071. kmem_cache_free(ubi_wl_entry_slab, e2);
  1072. goto out_ro;
  1073. }
  1074. }
  1075. dbg_wl("done");
  1076. mutex_unlock(&ubi->move_mutex);
  1077. return 0;
  1078. /*
  1079. * For some reasons the LEB was not moved, might be an error, might be
  1080. * something else. @e1 was not changed, so return it back. @e2 might
  1081. * have been changed, schedule it for erasure.
  1082. */
  1083. out_not_moved:
  1084. if (vol_id != -1)
  1085. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  1086. e1->pnum, vol_id, lnum, e2->pnum, err);
  1087. else
  1088. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  1089. e1->pnum, e2->pnum, err);
  1090. spin_lock(&ubi->wl_lock);
  1091. if (protect)
  1092. prot_queue_add(ubi, e1);
  1093. else if (erroneous) {
  1094. wl_tree_add(e1, &ubi->erroneous);
  1095. ubi->erroneous_peb_count += 1;
  1096. } else if (scrubbing)
  1097. wl_tree_add(e1, &ubi->scrub);
  1098. else
  1099. wl_tree_add(e1, &ubi->used);
  1100. ubi_assert(!ubi->move_to_put);
  1101. ubi->move_from = ubi->move_to = NULL;
  1102. ubi->wl_scheduled = 0;
  1103. spin_unlock(&ubi->wl_lock);
  1104. ubi_free_vid_hdr(ubi, vid_hdr);
  1105. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  1106. if (err) {
  1107. kmem_cache_free(ubi_wl_entry_slab, e2);
  1108. goto out_ro;
  1109. }
  1110. mutex_unlock(&ubi->move_mutex);
  1111. return 0;
  1112. out_error:
  1113. if (vol_id != -1)
  1114. ubi_err("error %d while moving PEB %d to PEB %d",
  1115. err, e1->pnum, e2->pnum);
  1116. else
  1117. ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  1118. err, e1->pnum, vol_id, lnum, e2->pnum);
  1119. spin_lock(&ubi->wl_lock);
  1120. ubi->move_from = ubi->move_to = NULL;
  1121. ubi->move_to_put = ubi->wl_scheduled = 0;
  1122. spin_unlock(&ubi->wl_lock);
  1123. ubi_free_vid_hdr(ubi, vid_hdr);
  1124. kmem_cache_free(ubi_wl_entry_slab, e1);
  1125. kmem_cache_free(ubi_wl_entry_slab, e2);
  1126. out_ro:
  1127. ubi_ro_mode(ubi);
  1128. mutex_unlock(&ubi->move_mutex);
  1129. ubi_assert(err != 0);
  1130. return err < 0 ? err : -EIO;
  1131. out_cancel:
  1132. ubi->wl_scheduled = 0;
  1133. spin_unlock(&ubi->wl_lock);
  1134. mutex_unlock(&ubi->move_mutex);
  1135. ubi_free_vid_hdr(ubi, vid_hdr);
  1136. return 0;
  1137. }
  1138. /**
  1139. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  1140. * @ubi: UBI device description object
  1141. * @nested: set to non-zero if this function is called from UBI worker
  1142. *
  1143. * This function checks if it is time to start wear-leveling and schedules it
  1144. * if yes. This function returns zero in case of success and a negative error
  1145. * code in case of failure.
  1146. */
  1147. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  1148. {
  1149. int err = 0;
  1150. struct ubi_wl_entry *e1;
  1151. struct ubi_wl_entry *e2;
  1152. struct ubi_work *wrk;
  1153. spin_lock(&ubi->wl_lock);
  1154. if (ubi->wl_scheduled)
  1155. /* Wear-leveling is already in the work queue */
  1156. goto out_unlock;
  1157. /*
  1158. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  1159. * the WL worker has to be scheduled anyway.
  1160. */
  1161. if (!ubi->scrub.rb_node) {
  1162. if (!ubi->used.rb_node || !ubi->free.rb_node)
  1163. /* No physical eraseblocks - no deal */
  1164. goto out_unlock;
  1165. /*
  1166. * We schedule wear-leveling only if the difference between the
  1167. * lowest erase counter of used physical eraseblocks and a high
  1168. * erase counter of free physical eraseblocks is greater than
  1169. * %UBI_WL_THRESHOLD.
  1170. */
  1171. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  1172. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1173. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  1174. goto out_unlock;
  1175. dbg_wl("schedule wear-leveling");
  1176. } else
  1177. dbg_wl("schedule scrubbing");
  1178. ubi->wl_scheduled = 1;
  1179. spin_unlock(&ubi->wl_lock);
  1180. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1181. if (!wrk) {
  1182. err = -ENOMEM;
  1183. goto out_cancel;
  1184. }
  1185. wrk->anchor = 0;
  1186. wrk->func = &wear_leveling_worker;
  1187. if (nested)
  1188. __schedule_ubi_work(ubi, wrk);
  1189. else
  1190. schedule_ubi_work(ubi, wrk);
  1191. return err;
  1192. out_cancel:
  1193. spin_lock(&ubi->wl_lock);
  1194. ubi->wl_scheduled = 0;
  1195. out_unlock:
  1196. spin_unlock(&ubi->wl_lock);
  1197. return err;
  1198. }
  1199. #ifdef CONFIG_MTD_UBI_FASTMAP
  1200. /**
  1201. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  1202. * @ubi: UBI device description object
  1203. */
  1204. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  1205. {
  1206. struct ubi_work *wrk;
  1207. spin_lock(&ubi->wl_lock);
  1208. if (ubi->wl_scheduled) {
  1209. spin_unlock(&ubi->wl_lock);
  1210. return 0;
  1211. }
  1212. ubi->wl_scheduled = 1;
  1213. spin_unlock(&ubi->wl_lock);
  1214. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1215. if (!wrk) {
  1216. spin_lock(&ubi->wl_lock);
  1217. ubi->wl_scheduled = 0;
  1218. spin_unlock(&ubi->wl_lock);
  1219. return -ENOMEM;
  1220. }
  1221. wrk->anchor = 1;
  1222. wrk->func = &wear_leveling_worker;
  1223. schedule_ubi_work(ubi, wrk);
  1224. return 0;
  1225. }
  1226. #endif
  1227. /**
  1228. * erase_worker - physical eraseblock erase worker function.
  1229. * @ubi: UBI device description object
  1230. * @wl_wrk: the work object
  1231. * @cancel: non-zero if the worker has to free memory and exit
  1232. *
  1233. * This function erases a physical eraseblock and perform torture testing if
  1234. * needed. It also takes care about marking the physical eraseblock bad if
  1235. * needed. Returns zero in case of success and a negative error code in case of
  1236. * failure.
  1237. */
  1238. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1239. int cancel)
  1240. {
  1241. struct ubi_wl_entry *e = wl_wrk->e;
  1242. int pnum = e->pnum;
  1243. int vol_id = wl_wrk->vol_id;
  1244. int lnum = wl_wrk->lnum;
  1245. int err, available_consumed = 0;
  1246. if (cancel) {
  1247. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  1248. kfree(wl_wrk);
  1249. kmem_cache_free(ubi_wl_entry_slab, e);
  1250. return 0;
  1251. }
  1252. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  1253. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  1254. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1255. err = sync_erase(ubi, e, wl_wrk->torture);
  1256. if (!err) {
  1257. /* Fine, we've erased it successfully */
  1258. kfree(wl_wrk);
  1259. spin_lock(&ubi->wl_lock);
  1260. wl_tree_add(e, &ubi->free);
  1261. ubi->free_count++;
  1262. spin_unlock(&ubi->wl_lock);
  1263. /*
  1264. * One more erase operation has happened, take care about
  1265. * protected physical eraseblocks.
  1266. */
  1267. serve_prot_queue(ubi);
  1268. /* And take care about wear-leveling */
  1269. err = ensure_wear_leveling(ubi, 1);
  1270. return err;
  1271. }
  1272. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  1273. kfree(wl_wrk);
  1274. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  1275. err == -EBUSY) {
  1276. int err1;
  1277. /* Re-schedule the LEB for erasure */
  1278. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  1279. if (err1) {
  1280. err = err1;
  1281. goto out_ro;
  1282. }
  1283. return err;
  1284. }
  1285. kmem_cache_free(ubi_wl_entry_slab, e);
  1286. if (err != -EIO)
  1287. /*
  1288. * If this is not %-EIO, we have no idea what to do. Scheduling
  1289. * this physical eraseblock for erasure again would cause
  1290. * errors again and again. Well, lets switch to R/O mode.
  1291. */
  1292. goto out_ro;
  1293. /* It is %-EIO, the PEB went bad */
  1294. if (!ubi->bad_allowed) {
  1295. ubi_err("bad physical eraseblock %d detected", pnum);
  1296. goto out_ro;
  1297. }
  1298. spin_lock(&ubi->volumes_lock);
  1299. if (ubi->beb_rsvd_pebs == 0) {
  1300. if (ubi->avail_pebs == 0) {
  1301. spin_unlock(&ubi->volumes_lock);
  1302. ubi_err("no reserved/available physical eraseblocks");
  1303. goto out_ro;
  1304. }
  1305. ubi->avail_pebs -= 1;
  1306. available_consumed = 1;
  1307. }
  1308. spin_unlock(&ubi->volumes_lock);
  1309. ubi_msg("mark PEB %d as bad", pnum);
  1310. err = ubi_io_mark_bad(ubi, pnum);
  1311. if (err)
  1312. goto out_ro;
  1313. spin_lock(&ubi->volumes_lock);
  1314. if (ubi->beb_rsvd_pebs > 0) {
  1315. if (available_consumed) {
  1316. /*
  1317. * The amount of reserved PEBs increased since we last
  1318. * checked.
  1319. */
  1320. ubi->avail_pebs += 1;
  1321. available_consumed = 0;
  1322. }
  1323. ubi->beb_rsvd_pebs -= 1;
  1324. }
  1325. ubi->bad_peb_count += 1;
  1326. ubi->good_peb_count -= 1;
  1327. ubi_calculate_reserved(ubi);
  1328. if (available_consumed)
  1329. ubi_warn("no PEBs in the reserved pool, used an available PEB");
  1330. else if (ubi->beb_rsvd_pebs)
  1331. ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
  1332. else
  1333. ubi_warn("last PEB from the reserve was used");
  1334. spin_unlock(&ubi->volumes_lock);
  1335. return err;
  1336. out_ro:
  1337. if (available_consumed) {
  1338. spin_lock(&ubi->volumes_lock);
  1339. ubi->avail_pebs += 1;
  1340. spin_unlock(&ubi->volumes_lock);
  1341. }
  1342. ubi_ro_mode(ubi);
  1343. return err;
  1344. }
  1345. /**
  1346. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1347. * @ubi: UBI device description object
  1348. * @vol_id: the volume ID that last used this PEB
  1349. * @lnum: the last used logical eraseblock number for the PEB
  1350. * @pnum: physical eraseblock to return
  1351. * @torture: if this physical eraseblock has to be tortured
  1352. *
  1353. * This function is called to return physical eraseblock @pnum to the pool of
  1354. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1355. * occurred to this @pnum and it has to be tested. This function returns zero
  1356. * in case of success, and a negative error code in case of failure.
  1357. */
  1358. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1359. int pnum, int torture)
  1360. {
  1361. int err;
  1362. struct ubi_wl_entry *e;
  1363. dbg_wl("PEB %d", pnum);
  1364. ubi_assert(pnum >= 0);
  1365. ubi_assert(pnum < ubi->peb_count);
  1366. retry:
  1367. spin_lock(&ubi->wl_lock);
  1368. e = ubi->lookuptbl[pnum];
  1369. if (e == ubi->move_from) {
  1370. /*
  1371. * User is putting the physical eraseblock which was selected to
  1372. * be moved. It will be scheduled for erasure in the
  1373. * wear-leveling worker.
  1374. */
  1375. dbg_wl("PEB %d is being moved, wait", pnum);
  1376. spin_unlock(&ubi->wl_lock);
  1377. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1378. mutex_lock(&ubi->move_mutex);
  1379. mutex_unlock(&ubi->move_mutex);
  1380. goto retry;
  1381. } else if (e == ubi->move_to) {
  1382. /*
  1383. * User is putting the physical eraseblock which was selected
  1384. * as the target the data is moved to. It may happen if the EBA
  1385. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1386. * but the WL sub-system has not put the PEB to the "used" tree
  1387. * yet, but it is about to do this. So we just set a flag which
  1388. * will tell the WL worker that the PEB is not needed anymore
  1389. * and should be scheduled for erasure.
  1390. */
  1391. dbg_wl("PEB %d is the target of data moving", pnum);
  1392. ubi_assert(!ubi->move_to_put);
  1393. ubi->move_to_put = 1;
  1394. spin_unlock(&ubi->wl_lock);
  1395. return 0;
  1396. } else {
  1397. if (in_wl_tree(e, &ubi->used)) {
  1398. self_check_in_wl_tree(ubi, e, &ubi->used);
  1399. rb_erase(&e->u.rb, &ubi->used);
  1400. } else if (in_wl_tree(e, &ubi->scrub)) {
  1401. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1402. rb_erase(&e->u.rb, &ubi->scrub);
  1403. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1404. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1405. rb_erase(&e->u.rb, &ubi->erroneous);
  1406. ubi->erroneous_peb_count -= 1;
  1407. ubi_assert(ubi->erroneous_peb_count >= 0);
  1408. /* Erroneous PEBs should be tortured */
  1409. torture = 1;
  1410. } else {
  1411. err = prot_queue_del(ubi, e->pnum);
  1412. if (err) {
  1413. ubi_err("PEB %d not found", pnum);
  1414. ubi_ro_mode(ubi);
  1415. spin_unlock(&ubi->wl_lock);
  1416. return err;
  1417. }
  1418. }
  1419. }
  1420. spin_unlock(&ubi->wl_lock);
  1421. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1422. if (err) {
  1423. spin_lock(&ubi->wl_lock);
  1424. wl_tree_add(e, &ubi->used);
  1425. spin_unlock(&ubi->wl_lock);
  1426. }
  1427. return err;
  1428. }
  1429. /**
  1430. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1431. * @ubi: UBI device description object
  1432. * @pnum: the physical eraseblock to schedule
  1433. *
  1434. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1435. * needs scrubbing. This function schedules a physical eraseblock for
  1436. * scrubbing which is done in background. This function returns zero in case of
  1437. * success and a negative error code in case of failure.
  1438. */
  1439. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1440. {
  1441. struct ubi_wl_entry *e;
  1442. ubi_msg("schedule PEB %d for scrubbing", pnum);
  1443. retry:
  1444. spin_lock(&ubi->wl_lock);
  1445. e = ubi->lookuptbl[pnum];
  1446. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1447. in_wl_tree(e, &ubi->erroneous)) {
  1448. spin_unlock(&ubi->wl_lock);
  1449. return 0;
  1450. }
  1451. if (e == ubi->move_to) {
  1452. /*
  1453. * This physical eraseblock was used to move data to. The data
  1454. * was moved but the PEB was not yet inserted to the proper
  1455. * tree. We should just wait a little and let the WL worker
  1456. * proceed.
  1457. */
  1458. spin_unlock(&ubi->wl_lock);
  1459. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1460. yield();
  1461. goto retry;
  1462. }
  1463. if (in_wl_tree(e, &ubi->used)) {
  1464. self_check_in_wl_tree(ubi, e, &ubi->used);
  1465. rb_erase(&e->u.rb, &ubi->used);
  1466. } else {
  1467. int err;
  1468. err = prot_queue_del(ubi, e->pnum);
  1469. if (err) {
  1470. ubi_err("PEB %d not found", pnum);
  1471. ubi_ro_mode(ubi);
  1472. spin_unlock(&ubi->wl_lock);
  1473. return err;
  1474. }
  1475. }
  1476. wl_tree_add(e, &ubi->scrub);
  1477. spin_unlock(&ubi->wl_lock);
  1478. /*
  1479. * Technically scrubbing is the same as wear-leveling, so it is done
  1480. * by the WL worker.
  1481. */
  1482. return ensure_wear_leveling(ubi, 0);
  1483. }
  1484. /**
  1485. * ubi_wl_flush - flush all pending works.
  1486. * @ubi: UBI device description object
  1487. * @vol_id: the volume id to flush for
  1488. * @lnum: the logical eraseblock number to flush for
  1489. *
  1490. * This function executes all pending works for a particular volume id /
  1491. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1492. * acts as a wildcard for all of the corresponding volume numbers or logical
  1493. * eraseblock numbers. It returns zero in case of success and a negative error
  1494. * code in case of failure.
  1495. */
  1496. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1497. {
  1498. int err = 0;
  1499. int found = 1;
  1500. /*
  1501. * Erase while the pending works queue is not empty, but not more than
  1502. * the number of currently pending works.
  1503. */
  1504. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1505. vol_id, lnum, ubi->works_count);
  1506. while (found) {
  1507. struct ubi_work *wrk;
  1508. found = 0;
  1509. down_read(&ubi->work_sem);
  1510. spin_lock(&ubi->wl_lock);
  1511. list_for_each_entry(wrk, &ubi->works, list) {
  1512. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1513. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1514. list_del(&wrk->list);
  1515. ubi->works_count -= 1;
  1516. ubi_assert(ubi->works_count >= 0);
  1517. spin_unlock(&ubi->wl_lock);
  1518. err = wrk->func(ubi, wrk, 0);
  1519. if (err) {
  1520. up_read(&ubi->work_sem);
  1521. return err;
  1522. }
  1523. spin_lock(&ubi->wl_lock);
  1524. found = 1;
  1525. break;
  1526. }
  1527. }
  1528. spin_unlock(&ubi->wl_lock);
  1529. up_read(&ubi->work_sem);
  1530. }
  1531. /*
  1532. * Make sure all the works which have been done in parallel are
  1533. * finished.
  1534. */
  1535. down_write(&ubi->work_sem);
  1536. up_write(&ubi->work_sem);
  1537. return err;
  1538. }
  1539. /**
  1540. * tree_destroy - destroy an RB-tree.
  1541. * @root: the root of the tree to destroy
  1542. */
  1543. static void tree_destroy(struct rb_root *root)
  1544. {
  1545. struct rb_node *rb;
  1546. struct ubi_wl_entry *e;
  1547. rb = root->rb_node;
  1548. while (rb) {
  1549. if (rb->rb_left)
  1550. rb = rb->rb_left;
  1551. else if (rb->rb_right)
  1552. rb = rb->rb_right;
  1553. else {
  1554. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1555. rb = rb_parent(rb);
  1556. if (rb) {
  1557. if (rb->rb_left == &e->u.rb)
  1558. rb->rb_left = NULL;
  1559. else
  1560. rb->rb_right = NULL;
  1561. }
  1562. kmem_cache_free(ubi_wl_entry_slab, e);
  1563. }
  1564. }
  1565. }
  1566. /**
  1567. * ubi_thread - UBI background thread.
  1568. * @u: the UBI device description object pointer
  1569. */
  1570. int ubi_thread(void *u)
  1571. {
  1572. int failures = 0;
  1573. struct ubi_device *ubi = u;
  1574. ubi_msg("background thread \"%s\" started, PID %d",
  1575. ubi->bgt_name, task_pid_nr(current));
  1576. set_freezable();
  1577. for (;;) {
  1578. int err;
  1579. if (kthread_should_stop())
  1580. break;
  1581. if (try_to_freeze())
  1582. continue;
  1583. spin_lock(&ubi->wl_lock);
  1584. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1585. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1586. set_current_state(TASK_INTERRUPTIBLE);
  1587. spin_unlock(&ubi->wl_lock);
  1588. schedule();
  1589. continue;
  1590. }
  1591. spin_unlock(&ubi->wl_lock);
  1592. err = do_work(ubi);
  1593. if (err) {
  1594. ubi_err("%s: work failed with error code %d",
  1595. ubi->bgt_name, err);
  1596. if (failures++ > WL_MAX_FAILURES) {
  1597. /*
  1598. * Too many failures, disable the thread and
  1599. * switch to read-only mode.
  1600. */
  1601. ubi_msg("%s: %d consecutive failures",
  1602. ubi->bgt_name, WL_MAX_FAILURES);
  1603. ubi_ro_mode(ubi);
  1604. ubi->thread_enabled = 0;
  1605. continue;
  1606. }
  1607. } else
  1608. failures = 0;
  1609. cond_resched();
  1610. }
  1611. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1612. return 0;
  1613. }
  1614. /**
  1615. * cancel_pending - cancel all pending works.
  1616. * @ubi: UBI device description object
  1617. */
  1618. static void cancel_pending(struct ubi_device *ubi)
  1619. {
  1620. while (!list_empty(&ubi->works)) {
  1621. struct ubi_work *wrk;
  1622. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1623. list_del(&wrk->list);
  1624. wrk->func(ubi, wrk, 1);
  1625. ubi->works_count -= 1;
  1626. ubi_assert(ubi->works_count >= 0);
  1627. }
  1628. }
  1629. /**
  1630. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1631. * @ubi: UBI device description object
  1632. * @ai: attaching information
  1633. *
  1634. * This function returns zero in case of success, and a negative error code in
  1635. * case of failure.
  1636. */
  1637. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1638. {
  1639. int err, i, reserved_pebs, found_pebs = 0;
  1640. struct rb_node *rb1, *rb2;
  1641. struct ubi_ainf_volume *av;
  1642. struct ubi_ainf_peb *aeb, *tmp;
  1643. struct ubi_wl_entry *e;
  1644. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1645. spin_lock_init(&ubi->wl_lock);
  1646. mutex_init(&ubi->move_mutex);
  1647. init_rwsem(&ubi->work_sem);
  1648. ubi->max_ec = ai->max_ec;
  1649. INIT_LIST_HEAD(&ubi->works);
  1650. #ifdef CONFIG_MTD_UBI_FASTMAP
  1651. INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
  1652. #endif
  1653. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1654. err = -ENOMEM;
  1655. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1656. if (!ubi->lookuptbl)
  1657. return err;
  1658. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1659. INIT_LIST_HEAD(&ubi->pq[i]);
  1660. ubi->pq_head = 0;
  1661. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1662. cond_resched();
  1663. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1664. if (!e)
  1665. goto out_free;
  1666. e->pnum = aeb->pnum;
  1667. e->ec = aeb->ec;
  1668. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1669. ubi->lookuptbl[e->pnum] = e;
  1670. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1671. kmem_cache_free(ubi_wl_entry_slab, e);
  1672. goto out_free;
  1673. }
  1674. found_pebs++;
  1675. }
  1676. ubi->free_count = 0;
  1677. list_for_each_entry(aeb, &ai->free, u.list) {
  1678. cond_resched();
  1679. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1680. if (!e)
  1681. goto out_free;
  1682. e->pnum = aeb->pnum;
  1683. e->ec = aeb->ec;
  1684. ubi_assert(e->ec >= 0);
  1685. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1686. wl_tree_add(e, &ubi->free);
  1687. ubi->free_count++;
  1688. ubi->lookuptbl[e->pnum] = e;
  1689. found_pebs++;
  1690. }
  1691. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1692. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1693. cond_resched();
  1694. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1695. if (!e)
  1696. goto out_free;
  1697. e->pnum = aeb->pnum;
  1698. e->ec = aeb->ec;
  1699. ubi->lookuptbl[e->pnum] = e;
  1700. if (!aeb->scrub) {
  1701. dbg_wl("add PEB %d EC %d to the used tree",
  1702. e->pnum, e->ec);
  1703. wl_tree_add(e, &ubi->used);
  1704. } else {
  1705. dbg_wl("add PEB %d EC %d to the scrub tree",
  1706. e->pnum, e->ec);
  1707. wl_tree_add(e, &ubi->scrub);
  1708. }
  1709. found_pebs++;
  1710. }
  1711. }
  1712. dbg_wl("found %i PEBs", found_pebs);
  1713. if (ubi->fm)
  1714. ubi_assert(ubi->good_peb_count == \
  1715. found_pebs + ubi->fm->used_blocks);
  1716. else
  1717. ubi_assert(ubi->good_peb_count == found_pebs);
  1718. reserved_pebs = WL_RESERVED_PEBS;
  1719. #ifdef CONFIG_MTD_UBI_FASTMAP
  1720. /* Reserve enough LEBs to store two fastmaps. */
  1721. reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
  1722. #endif
  1723. if (ubi->avail_pebs < reserved_pebs) {
  1724. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1725. ubi->avail_pebs, reserved_pebs);
  1726. if (ubi->corr_peb_count)
  1727. ubi_err("%d PEBs are corrupted and not used",
  1728. ubi->corr_peb_count);
  1729. goto out_free;
  1730. }
  1731. ubi->avail_pebs -= reserved_pebs;
  1732. ubi->rsvd_pebs += reserved_pebs;
  1733. /* Schedule wear-leveling if needed */
  1734. err = ensure_wear_leveling(ubi, 0);
  1735. if (err)
  1736. goto out_free;
  1737. return 0;
  1738. out_free:
  1739. cancel_pending(ubi);
  1740. tree_destroy(&ubi->used);
  1741. tree_destroy(&ubi->free);
  1742. tree_destroy(&ubi->scrub);
  1743. kfree(ubi->lookuptbl);
  1744. return err;
  1745. }
  1746. /**
  1747. * protection_queue_destroy - destroy the protection queue.
  1748. * @ubi: UBI device description object
  1749. */
  1750. static void protection_queue_destroy(struct ubi_device *ubi)
  1751. {
  1752. int i;
  1753. struct ubi_wl_entry *e, *tmp;
  1754. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1755. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1756. list_del(&e->u.list);
  1757. kmem_cache_free(ubi_wl_entry_slab, e);
  1758. }
  1759. }
  1760. }
  1761. /**
  1762. * ubi_wl_close - close the wear-leveling sub-system.
  1763. * @ubi: UBI device description object
  1764. */
  1765. void ubi_wl_close(struct ubi_device *ubi)
  1766. {
  1767. dbg_wl("close the WL sub-system");
  1768. cancel_pending(ubi);
  1769. protection_queue_destroy(ubi);
  1770. tree_destroy(&ubi->used);
  1771. tree_destroy(&ubi->erroneous);
  1772. tree_destroy(&ubi->free);
  1773. tree_destroy(&ubi->scrub);
  1774. kfree(ubi->lookuptbl);
  1775. }
  1776. /**
  1777. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1778. * @ubi: UBI device description object
  1779. * @pnum: the physical eraseblock number to check
  1780. * @ec: the erase counter to check
  1781. *
  1782. * This function returns zero if the erase counter of physical eraseblock @pnum
  1783. * is equivalent to @ec, and a negative error code if not or if an error
  1784. * occurred.
  1785. */
  1786. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1787. {
  1788. int err;
  1789. long long read_ec;
  1790. struct ubi_ec_hdr *ec_hdr;
  1791. if (!ubi_dbg_chk_gen(ubi))
  1792. return 0;
  1793. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1794. if (!ec_hdr)
  1795. return -ENOMEM;
  1796. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1797. if (err && err != UBI_IO_BITFLIPS) {
  1798. /* The header does not have to exist */
  1799. err = 0;
  1800. goto out_free;
  1801. }
  1802. read_ec = be64_to_cpu(ec_hdr->ec);
  1803. if (ec != read_ec && read_ec - ec > 1) {
  1804. ubi_err("self-check failed for PEB %d", pnum);
  1805. ubi_err("read EC is %lld, should be %d", read_ec, ec);
  1806. dump_stack();
  1807. err = 1;
  1808. } else
  1809. err = 0;
  1810. out_free:
  1811. kfree(ec_hdr);
  1812. return err;
  1813. }
  1814. /**
  1815. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1816. * @ubi: UBI device description object
  1817. * @e: the wear-leveling entry to check
  1818. * @root: the root of the tree
  1819. *
  1820. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1821. * is not.
  1822. */
  1823. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1824. struct ubi_wl_entry *e, struct rb_root *root)
  1825. {
  1826. if (!ubi_dbg_chk_gen(ubi))
  1827. return 0;
  1828. if (in_wl_tree(e, root))
  1829. return 0;
  1830. ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
  1831. e->pnum, e->ec, root);
  1832. dump_stack();
  1833. return -EINVAL;
  1834. }
  1835. /**
  1836. * self_check_in_pq - check if wear-leveling entry is in the protection
  1837. * queue.
  1838. * @ubi: UBI device description object
  1839. * @e: the wear-leveling entry to check
  1840. *
  1841. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1842. */
  1843. static int self_check_in_pq(const struct ubi_device *ubi,
  1844. struct ubi_wl_entry *e)
  1845. {
  1846. struct ubi_wl_entry *p;
  1847. int i;
  1848. if (!ubi_dbg_chk_gen(ubi))
  1849. return 0;
  1850. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1851. list_for_each_entry(p, &ubi->pq[i], u.list)
  1852. if (p == e)
  1853. return 0;
  1854. ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
  1855. e->pnum, e->ec);
  1856. dump_stack();
  1857. return -EINVAL;
  1858. }