wl.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  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. /* Give the unused PEB back */
  934. wl_tree_add(e2, &ubi->free);
  935. goto out_cancel;
  936. }
  937. self_check_in_wl_tree(ubi, e1, &ubi->used);
  938. rb_erase(&e1->u.rb, &ubi->used);
  939. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  940. e1->pnum, e1->ec, e2->pnum, e2->ec);
  941. } else {
  942. /* Perform scrubbing */
  943. scrubbing = 1;
  944. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  945. e2 = get_peb_for_wl(ubi);
  946. if (!e2)
  947. goto out_cancel;
  948. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  949. rb_erase(&e1->u.rb, &ubi->scrub);
  950. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  951. }
  952. ubi->move_from = e1;
  953. ubi->move_to = e2;
  954. spin_unlock(&ubi->wl_lock);
  955. /*
  956. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  957. * We so far do not know which logical eraseblock our physical
  958. * eraseblock (@e1) belongs to. We have to read the volume identifier
  959. * header first.
  960. *
  961. * Note, we are protected from this PEB being unmapped and erased. The
  962. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  963. * which is being moved was unmapped.
  964. */
  965. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  966. if (err && err != UBI_IO_BITFLIPS) {
  967. if (err == UBI_IO_FF) {
  968. /*
  969. * We are trying to move PEB without a VID header. UBI
  970. * always write VID headers shortly after the PEB was
  971. * given, so we have a situation when it has not yet
  972. * had a chance to write it, because it was preempted.
  973. * So add this PEB to the protection queue so far,
  974. * because presumably more data will be written there
  975. * (including the missing VID header), and then we'll
  976. * move it.
  977. */
  978. dbg_wl("PEB %d has no VID header", e1->pnum);
  979. protect = 1;
  980. goto out_not_moved;
  981. } else if (err == UBI_IO_FF_BITFLIPS) {
  982. /*
  983. * The same situation as %UBI_IO_FF, but bit-flips were
  984. * detected. It is better to schedule this PEB for
  985. * scrubbing.
  986. */
  987. dbg_wl("PEB %d has no VID header but has bit-flips",
  988. e1->pnum);
  989. scrubbing = 1;
  990. goto out_not_moved;
  991. }
  992. ubi_err("error %d while reading VID header from PEB %d",
  993. err, e1->pnum);
  994. goto out_error;
  995. }
  996. vol_id = be32_to_cpu(vid_hdr->vol_id);
  997. lnum = be32_to_cpu(vid_hdr->lnum);
  998. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  999. if (err) {
  1000. if (err == MOVE_CANCEL_RACE) {
  1001. /*
  1002. * The LEB has not been moved because the volume is
  1003. * being deleted or the PEB has been put meanwhile. We
  1004. * should prevent this PEB from being selected for
  1005. * wear-leveling movement again, so put it to the
  1006. * protection queue.
  1007. */
  1008. protect = 1;
  1009. goto out_not_moved;
  1010. }
  1011. if (err == MOVE_RETRY) {
  1012. scrubbing = 1;
  1013. goto out_not_moved;
  1014. }
  1015. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  1016. err == MOVE_TARGET_RD_ERR) {
  1017. /*
  1018. * Target PEB had bit-flips or write error - torture it.
  1019. */
  1020. torture = 1;
  1021. goto out_not_moved;
  1022. }
  1023. if (err == MOVE_SOURCE_RD_ERR) {
  1024. /*
  1025. * An error happened while reading the source PEB. Do
  1026. * not switch to R/O mode in this case, and give the
  1027. * upper layers a possibility to recover from this,
  1028. * e.g. by unmapping corresponding LEB. Instead, just
  1029. * put this PEB to the @ubi->erroneous list to prevent
  1030. * UBI from trying to move it over and over again.
  1031. */
  1032. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  1033. ubi_err("too many erroneous eraseblocks (%d)",
  1034. ubi->erroneous_peb_count);
  1035. goto out_error;
  1036. }
  1037. erroneous = 1;
  1038. goto out_not_moved;
  1039. }
  1040. if (err < 0)
  1041. goto out_error;
  1042. ubi_assert(0);
  1043. }
  1044. /* The PEB has been successfully moved */
  1045. if (scrubbing)
  1046. ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  1047. e1->pnum, vol_id, lnum, e2->pnum);
  1048. ubi_free_vid_hdr(ubi, vid_hdr);
  1049. spin_lock(&ubi->wl_lock);
  1050. if (!ubi->move_to_put) {
  1051. wl_tree_add(e2, &ubi->used);
  1052. e2 = NULL;
  1053. }
  1054. ubi->move_from = ubi->move_to = NULL;
  1055. ubi->move_to_put = ubi->wl_scheduled = 0;
  1056. spin_unlock(&ubi->wl_lock);
  1057. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  1058. if (err) {
  1059. kmem_cache_free(ubi_wl_entry_slab, e1);
  1060. if (e2)
  1061. kmem_cache_free(ubi_wl_entry_slab, e2);
  1062. goto out_ro;
  1063. }
  1064. if (e2) {
  1065. /*
  1066. * Well, the target PEB was put meanwhile, schedule it for
  1067. * erasure.
  1068. */
  1069. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  1070. e2->pnum, vol_id, lnum);
  1071. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  1072. if (err) {
  1073. kmem_cache_free(ubi_wl_entry_slab, e2);
  1074. goto out_ro;
  1075. }
  1076. }
  1077. dbg_wl("done");
  1078. mutex_unlock(&ubi->move_mutex);
  1079. return 0;
  1080. /*
  1081. * For some reasons the LEB was not moved, might be an error, might be
  1082. * something else. @e1 was not changed, so return it back. @e2 might
  1083. * have been changed, schedule it for erasure.
  1084. */
  1085. out_not_moved:
  1086. if (vol_id != -1)
  1087. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  1088. e1->pnum, vol_id, lnum, e2->pnum, err);
  1089. else
  1090. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  1091. e1->pnum, e2->pnum, err);
  1092. spin_lock(&ubi->wl_lock);
  1093. if (protect)
  1094. prot_queue_add(ubi, e1);
  1095. else if (erroneous) {
  1096. wl_tree_add(e1, &ubi->erroneous);
  1097. ubi->erroneous_peb_count += 1;
  1098. } else if (scrubbing)
  1099. wl_tree_add(e1, &ubi->scrub);
  1100. else
  1101. wl_tree_add(e1, &ubi->used);
  1102. ubi_assert(!ubi->move_to_put);
  1103. ubi->move_from = ubi->move_to = NULL;
  1104. ubi->wl_scheduled = 0;
  1105. spin_unlock(&ubi->wl_lock);
  1106. ubi_free_vid_hdr(ubi, vid_hdr);
  1107. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  1108. if (err) {
  1109. kmem_cache_free(ubi_wl_entry_slab, e2);
  1110. goto out_ro;
  1111. }
  1112. mutex_unlock(&ubi->move_mutex);
  1113. return 0;
  1114. out_error:
  1115. if (vol_id != -1)
  1116. ubi_err("error %d while moving PEB %d to PEB %d",
  1117. err, e1->pnum, e2->pnum);
  1118. else
  1119. ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  1120. err, e1->pnum, vol_id, lnum, e2->pnum);
  1121. spin_lock(&ubi->wl_lock);
  1122. ubi->move_from = ubi->move_to = NULL;
  1123. ubi->move_to_put = ubi->wl_scheduled = 0;
  1124. spin_unlock(&ubi->wl_lock);
  1125. ubi_free_vid_hdr(ubi, vid_hdr);
  1126. kmem_cache_free(ubi_wl_entry_slab, e1);
  1127. kmem_cache_free(ubi_wl_entry_slab, e2);
  1128. out_ro:
  1129. ubi_ro_mode(ubi);
  1130. mutex_unlock(&ubi->move_mutex);
  1131. ubi_assert(err != 0);
  1132. return err < 0 ? err : -EIO;
  1133. out_cancel:
  1134. ubi->wl_scheduled = 0;
  1135. spin_unlock(&ubi->wl_lock);
  1136. mutex_unlock(&ubi->move_mutex);
  1137. ubi_free_vid_hdr(ubi, vid_hdr);
  1138. return 0;
  1139. }
  1140. /**
  1141. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  1142. * @ubi: UBI device description object
  1143. * @nested: set to non-zero if this function is called from UBI worker
  1144. *
  1145. * This function checks if it is time to start wear-leveling and schedules it
  1146. * if yes. This function returns zero in case of success and a negative error
  1147. * code in case of failure.
  1148. */
  1149. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  1150. {
  1151. int err = 0;
  1152. struct ubi_wl_entry *e1;
  1153. struct ubi_wl_entry *e2;
  1154. struct ubi_work *wrk;
  1155. spin_lock(&ubi->wl_lock);
  1156. if (ubi->wl_scheduled)
  1157. /* Wear-leveling is already in the work queue */
  1158. goto out_unlock;
  1159. /*
  1160. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  1161. * the WL worker has to be scheduled anyway.
  1162. */
  1163. if (!ubi->scrub.rb_node) {
  1164. if (!ubi->used.rb_node || !ubi->free.rb_node)
  1165. /* No physical eraseblocks - no deal */
  1166. goto out_unlock;
  1167. /*
  1168. * We schedule wear-leveling only if the difference between the
  1169. * lowest erase counter of used physical eraseblocks and a high
  1170. * erase counter of free physical eraseblocks is greater than
  1171. * %UBI_WL_THRESHOLD.
  1172. */
  1173. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  1174. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1175. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  1176. goto out_unlock;
  1177. dbg_wl("schedule wear-leveling");
  1178. } else
  1179. dbg_wl("schedule scrubbing");
  1180. ubi->wl_scheduled = 1;
  1181. spin_unlock(&ubi->wl_lock);
  1182. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1183. if (!wrk) {
  1184. err = -ENOMEM;
  1185. goto out_cancel;
  1186. }
  1187. wrk->anchor = 0;
  1188. wrk->func = &wear_leveling_worker;
  1189. if (nested)
  1190. __schedule_ubi_work(ubi, wrk);
  1191. else
  1192. schedule_ubi_work(ubi, wrk);
  1193. return err;
  1194. out_cancel:
  1195. spin_lock(&ubi->wl_lock);
  1196. ubi->wl_scheduled = 0;
  1197. out_unlock:
  1198. spin_unlock(&ubi->wl_lock);
  1199. return err;
  1200. }
  1201. #ifdef CONFIG_MTD_UBI_FASTMAP
  1202. /**
  1203. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  1204. * @ubi: UBI device description object
  1205. */
  1206. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  1207. {
  1208. struct ubi_work *wrk;
  1209. spin_lock(&ubi->wl_lock);
  1210. if (ubi->wl_scheduled) {
  1211. spin_unlock(&ubi->wl_lock);
  1212. return 0;
  1213. }
  1214. ubi->wl_scheduled = 1;
  1215. spin_unlock(&ubi->wl_lock);
  1216. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1217. if (!wrk) {
  1218. spin_lock(&ubi->wl_lock);
  1219. ubi->wl_scheduled = 0;
  1220. spin_unlock(&ubi->wl_lock);
  1221. return -ENOMEM;
  1222. }
  1223. wrk->anchor = 1;
  1224. wrk->func = &wear_leveling_worker;
  1225. schedule_ubi_work(ubi, wrk);
  1226. return 0;
  1227. }
  1228. #endif
  1229. /**
  1230. * erase_worker - physical eraseblock erase worker function.
  1231. * @ubi: UBI device description object
  1232. * @wl_wrk: the work object
  1233. * @cancel: non-zero if the worker has to free memory and exit
  1234. *
  1235. * This function erases a physical eraseblock and perform torture testing if
  1236. * needed. It also takes care about marking the physical eraseblock bad if
  1237. * needed. Returns zero in case of success and a negative error code in case of
  1238. * failure.
  1239. */
  1240. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1241. int cancel)
  1242. {
  1243. struct ubi_wl_entry *e = wl_wrk->e;
  1244. int pnum = e->pnum;
  1245. int vol_id = wl_wrk->vol_id;
  1246. int lnum = wl_wrk->lnum;
  1247. int err, available_consumed = 0;
  1248. if (cancel) {
  1249. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  1250. kfree(wl_wrk);
  1251. kmem_cache_free(ubi_wl_entry_slab, e);
  1252. return 0;
  1253. }
  1254. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  1255. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  1256. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1257. err = sync_erase(ubi, e, wl_wrk->torture);
  1258. if (!err) {
  1259. /* Fine, we've erased it successfully */
  1260. kfree(wl_wrk);
  1261. spin_lock(&ubi->wl_lock);
  1262. wl_tree_add(e, &ubi->free);
  1263. ubi->free_count++;
  1264. spin_unlock(&ubi->wl_lock);
  1265. /*
  1266. * One more erase operation has happened, take care about
  1267. * protected physical eraseblocks.
  1268. */
  1269. serve_prot_queue(ubi);
  1270. /* And take care about wear-leveling */
  1271. err = ensure_wear_leveling(ubi, 1);
  1272. return err;
  1273. }
  1274. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  1275. kfree(wl_wrk);
  1276. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  1277. err == -EBUSY) {
  1278. int err1;
  1279. /* Re-schedule the LEB for erasure */
  1280. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  1281. if (err1) {
  1282. err = err1;
  1283. goto out_ro;
  1284. }
  1285. return err;
  1286. }
  1287. kmem_cache_free(ubi_wl_entry_slab, e);
  1288. if (err != -EIO)
  1289. /*
  1290. * If this is not %-EIO, we have no idea what to do. Scheduling
  1291. * this physical eraseblock for erasure again would cause
  1292. * errors again and again. Well, lets switch to R/O mode.
  1293. */
  1294. goto out_ro;
  1295. /* It is %-EIO, the PEB went bad */
  1296. if (!ubi->bad_allowed) {
  1297. ubi_err("bad physical eraseblock %d detected", pnum);
  1298. goto out_ro;
  1299. }
  1300. spin_lock(&ubi->volumes_lock);
  1301. if (ubi->beb_rsvd_pebs == 0) {
  1302. if (ubi->avail_pebs == 0) {
  1303. spin_unlock(&ubi->volumes_lock);
  1304. ubi_err("no reserved/available physical eraseblocks");
  1305. goto out_ro;
  1306. }
  1307. ubi->avail_pebs -= 1;
  1308. available_consumed = 1;
  1309. }
  1310. spin_unlock(&ubi->volumes_lock);
  1311. ubi_msg("mark PEB %d as bad", pnum);
  1312. err = ubi_io_mark_bad(ubi, pnum);
  1313. if (err)
  1314. goto out_ro;
  1315. spin_lock(&ubi->volumes_lock);
  1316. if (ubi->beb_rsvd_pebs > 0) {
  1317. if (available_consumed) {
  1318. /*
  1319. * The amount of reserved PEBs increased since we last
  1320. * checked.
  1321. */
  1322. ubi->avail_pebs += 1;
  1323. available_consumed = 0;
  1324. }
  1325. ubi->beb_rsvd_pebs -= 1;
  1326. }
  1327. ubi->bad_peb_count += 1;
  1328. ubi->good_peb_count -= 1;
  1329. ubi_calculate_reserved(ubi);
  1330. if (available_consumed)
  1331. ubi_warn("no PEBs in the reserved pool, used an available PEB");
  1332. else if (ubi->beb_rsvd_pebs)
  1333. ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
  1334. else
  1335. ubi_warn("last PEB from the reserve was used");
  1336. spin_unlock(&ubi->volumes_lock);
  1337. return err;
  1338. out_ro:
  1339. if (available_consumed) {
  1340. spin_lock(&ubi->volumes_lock);
  1341. ubi->avail_pebs += 1;
  1342. spin_unlock(&ubi->volumes_lock);
  1343. }
  1344. ubi_ro_mode(ubi);
  1345. return err;
  1346. }
  1347. /**
  1348. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1349. * @ubi: UBI device description object
  1350. * @vol_id: the volume ID that last used this PEB
  1351. * @lnum: the last used logical eraseblock number for the PEB
  1352. * @pnum: physical eraseblock to return
  1353. * @torture: if this physical eraseblock has to be tortured
  1354. *
  1355. * This function is called to return physical eraseblock @pnum to the pool of
  1356. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1357. * occurred to this @pnum and it has to be tested. This function returns zero
  1358. * in case of success, and a negative error code in case of failure.
  1359. */
  1360. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1361. int pnum, int torture)
  1362. {
  1363. int err;
  1364. struct ubi_wl_entry *e;
  1365. dbg_wl("PEB %d", pnum);
  1366. ubi_assert(pnum >= 0);
  1367. ubi_assert(pnum < ubi->peb_count);
  1368. retry:
  1369. spin_lock(&ubi->wl_lock);
  1370. e = ubi->lookuptbl[pnum];
  1371. if (e == ubi->move_from) {
  1372. /*
  1373. * User is putting the physical eraseblock which was selected to
  1374. * be moved. It will be scheduled for erasure in the
  1375. * wear-leveling worker.
  1376. */
  1377. dbg_wl("PEB %d is being moved, wait", pnum);
  1378. spin_unlock(&ubi->wl_lock);
  1379. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1380. mutex_lock(&ubi->move_mutex);
  1381. mutex_unlock(&ubi->move_mutex);
  1382. goto retry;
  1383. } else if (e == ubi->move_to) {
  1384. /*
  1385. * User is putting the physical eraseblock which was selected
  1386. * as the target the data is moved to. It may happen if the EBA
  1387. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1388. * but the WL sub-system has not put the PEB to the "used" tree
  1389. * yet, but it is about to do this. So we just set a flag which
  1390. * will tell the WL worker that the PEB is not needed anymore
  1391. * and should be scheduled for erasure.
  1392. */
  1393. dbg_wl("PEB %d is the target of data moving", pnum);
  1394. ubi_assert(!ubi->move_to_put);
  1395. ubi->move_to_put = 1;
  1396. spin_unlock(&ubi->wl_lock);
  1397. return 0;
  1398. } else {
  1399. if (in_wl_tree(e, &ubi->used)) {
  1400. self_check_in_wl_tree(ubi, e, &ubi->used);
  1401. rb_erase(&e->u.rb, &ubi->used);
  1402. } else if (in_wl_tree(e, &ubi->scrub)) {
  1403. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1404. rb_erase(&e->u.rb, &ubi->scrub);
  1405. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1406. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1407. rb_erase(&e->u.rb, &ubi->erroneous);
  1408. ubi->erroneous_peb_count -= 1;
  1409. ubi_assert(ubi->erroneous_peb_count >= 0);
  1410. /* Erroneous PEBs should be tortured */
  1411. torture = 1;
  1412. } else {
  1413. err = prot_queue_del(ubi, e->pnum);
  1414. if (err) {
  1415. ubi_err("PEB %d not found", pnum);
  1416. ubi_ro_mode(ubi);
  1417. spin_unlock(&ubi->wl_lock);
  1418. return err;
  1419. }
  1420. }
  1421. }
  1422. spin_unlock(&ubi->wl_lock);
  1423. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1424. if (err) {
  1425. spin_lock(&ubi->wl_lock);
  1426. wl_tree_add(e, &ubi->used);
  1427. spin_unlock(&ubi->wl_lock);
  1428. }
  1429. return err;
  1430. }
  1431. /**
  1432. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1433. * @ubi: UBI device description object
  1434. * @pnum: the physical eraseblock to schedule
  1435. *
  1436. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1437. * needs scrubbing. This function schedules a physical eraseblock for
  1438. * scrubbing which is done in background. This function returns zero in case of
  1439. * success and a negative error code in case of failure.
  1440. */
  1441. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1442. {
  1443. struct ubi_wl_entry *e;
  1444. ubi_msg("schedule PEB %d for scrubbing", pnum);
  1445. retry:
  1446. spin_lock(&ubi->wl_lock);
  1447. e = ubi->lookuptbl[pnum];
  1448. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1449. in_wl_tree(e, &ubi->erroneous)) {
  1450. spin_unlock(&ubi->wl_lock);
  1451. return 0;
  1452. }
  1453. if (e == ubi->move_to) {
  1454. /*
  1455. * This physical eraseblock was used to move data to. The data
  1456. * was moved but the PEB was not yet inserted to the proper
  1457. * tree. We should just wait a little and let the WL worker
  1458. * proceed.
  1459. */
  1460. spin_unlock(&ubi->wl_lock);
  1461. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1462. yield();
  1463. goto retry;
  1464. }
  1465. if (in_wl_tree(e, &ubi->used)) {
  1466. self_check_in_wl_tree(ubi, e, &ubi->used);
  1467. rb_erase(&e->u.rb, &ubi->used);
  1468. } else {
  1469. int err;
  1470. err = prot_queue_del(ubi, e->pnum);
  1471. if (err) {
  1472. ubi_err("PEB %d not found", pnum);
  1473. ubi_ro_mode(ubi);
  1474. spin_unlock(&ubi->wl_lock);
  1475. return err;
  1476. }
  1477. }
  1478. wl_tree_add(e, &ubi->scrub);
  1479. spin_unlock(&ubi->wl_lock);
  1480. /*
  1481. * Technically scrubbing is the same as wear-leveling, so it is done
  1482. * by the WL worker.
  1483. */
  1484. return ensure_wear_leveling(ubi, 0);
  1485. }
  1486. /**
  1487. * ubi_wl_flush - flush all pending works.
  1488. * @ubi: UBI device description object
  1489. * @vol_id: the volume id to flush for
  1490. * @lnum: the logical eraseblock number to flush for
  1491. *
  1492. * This function executes all pending works for a particular volume id /
  1493. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1494. * acts as a wildcard for all of the corresponding volume numbers or logical
  1495. * eraseblock numbers. It returns zero in case of success and a negative error
  1496. * code in case of failure.
  1497. */
  1498. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1499. {
  1500. int err = 0;
  1501. int found = 1;
  1502. /*
  1503. * Erase while the pending works queue is not empty, but not more than
  1504. * the number of currently pending works.
  1505. */
  1506. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1507. vol_id, lnum, ubi->works_count);
  1508. while (found) {
  1509. struct ubi_work *wrk;
  1510. found = 0;
  1511. down_read(&ubi->work_sem);
  1512. spin_lock(&ubi->wl_lock);
  1513. list_for_each_entry(wrk, &ubi->works, list) {
  1514. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1515. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1516. list_del(&wrk->list);
  1517. ubi->works_count -= 1;
  1518. ubi_assert(ubi->works_count >= 0);
  1519. spin_unlock(&ubi->wl_lock);
  1520. err = wrk->func(ubi, wrk, 0);
  1521. if (err) {
  1522. up_read(&ubi->work_sem);
  1523. return err;
  1524. }
  1525. spin_lock(&ubi->wl_lock);
  1526. found = 1;
  1527. break;
  1528. }
  1529. }
  1530. spin_unlock(&ubi->wl_lock);
  1531. up_read(&ubi->work_sem);
  1532. }
  1533. /*
  1534. * Make sure all the works which have been done in parallel are
  1535. * finished.
  1536. */
  1537. down_write(&ubi->work_sem);
  1538. up_write(&ubi->work_sem);
  1539. return err;
  1540. }
  1541. /**
  1542. * tree_destroy - destroy an RB-tree.
  1543. * @root: the root of the tree to destroy
  1544. */
  1545. static void tree_destroy(struct rb_root *root)
  1546. {
  1547. struct rb_node *rb;
  1548. struct ubi_wl_entry *e;
  1549. rb = root->rb_node;
  1550. while (rb) {
  1551. if (rb->rb_left)
  1552. rb = rb->rb_left;
  1553. else if (rb->rb_right)
  1554. rb = rb->rb_right;
  1555. else {
  1556. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1557. rb = rb_parent(rb);
  1558. if (rb) {
  1559. if (rb->rb_left == &e->u.rb)
  1560. rb->rb_left = NULL;
  1561. else
  1562. rb->rb_right = NULL;
  1563. }
  1564. kmem_cache_free(ubi_wl_entry_slab, e);
  1565. }
  1566. }
  1567. }
  1568. /**
  1569. * ubi_thread - UBI background thread.
  1570. * @u: the UBI device description object pointer
  1571. */
  1572. int ubi_thread(void *u)
  1573. {
  1574. int failures = 0;
  1575. struct ubi_device *ubi = u;
  1576. ubi_msg("background thread \"%s\" started, PID %d",
  1577. ubi->bgt_name, task_pid_nr(current));
  1578. set_freezable();
  1579. for (;;) {
  1580. int err;
  1581. if (kthread_should_stop())
  1582. break;
  1583. if (try_to_freeze())
  1584. continue;
  1585. spin_lock(&ubi->wl_lock);
  1586. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1587. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1588. set_current_state(TASK_INTERRUPTIBLE);
  1589. spin_unlock(&ubi->wl_lock);
  1590. schedule();
  1591. continue;
  1592. }
  1593. spin_unlock(&ubi->wl_lock);
  1594. err = do_work(ubi);
  1595. if (err) {
  1596. ubi_err("%s: work failed with error code %d",
  1597. ubi->bgt_name, err);
  1598. if (failures++ > WL_MAX_FAILURES) {
  1599. /*
  1600. * Too many failures, disable the thread and
  1601. * switch to read-only mode.
  1602. */
  1603. ubi_msg("%s: %d consecutive failures",
  1604. ubi->bgt_name, WL_MAX_FAILURES);
  1605. ubi_ro_mode(ubi);
  1606. ubi->thread_enabled = 0;
  1607. continue;
  1608. }
  1609. } else
  1610. failures = 0;
  1611. cond_resched();
  1612. }
  1613. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1614. return 0;
  1615. }
  1616. /**
  1617. * cancel_pending - cancel all pending works.
  1618. * @ubi: UBI device description object
  1619. */
  1620. static void cancel_pending(struct ubi_device *ubi)
  1621. {
  1622. while (!list_empty(&ubi->works)) {
  1623. struct ubi_work *wrk;
  1624. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1625. list_del(&wrk->list);
  1626. wrk->func(ubi, wrk, 1);
  1627. ubi->works_count -= 1;
  1628. ubi_assert(ubi->works_count >= 0);
  1629. }
  1630. }
  1631. /**
  1632. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1633. * @ubi: UBI device description object
  1634. * @ai: attaching information
  1635. *
  1636. * This function returns zero in case of success, and a negative error code in
  1637. * case of failure.
  1638. */
  1639. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1640. {
  1641. int err, i, reserved_pebs, found_pebs = 0;
  1642. struct rb_node *rb1, *rb2;
  1643. struct ubi_ainf_volume *av;
  1644. struct ubi_ainf_peb *aeb, *tmp;
  1645. struct ubi_wl_entry *e;
  1646. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1647. spin_lock_init(&ubi->wl_lock);
  1648. mutex_init(&ubi->move_mutex);
  1649. init_rwsem(&ubi->work_sem);
  1650. ubi->max_ec = ai->max_ec;
  1651. INIT_LIST_HEAD(&ubi->works);
  1652. #ifdef CONFIG_MTD_UBI_FASTMAP
  1653. INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
  1654. #endif
  1655. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1656. err = -ENOMEM;
  1657. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1658. if (!ubi->lookuptbl)
  1659. return err;
  1660. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1661. INIT_LIST_HEAD(&ubi->pq[i]);
  1662. ubi->pq_head = 0;
  1663. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1664. cond_resched();
  1665. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1666. if (!e)
  1667. goto out_free;
  1668. e->pnum = aeb->pnum;
  1669. e->ec = aeb->ec;
  1670. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1671. ubi->lookuptbl[e->pnum] = e;
  1672. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1673. kmem_cache_free(ubi_wl_entry_slab, e);
  1674. goto out_free;
  1675. }
  1676. found_pebs++;
  1677. }
  1678. ubi->free_count = 0;
  1679. list_for_each_entry(aeb, &ai->free, u.list) {
  1680. cond_resched();
  1681. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1682. if (!e)
  1683. goto out_free;
  1684. e->pnum = aeb->pnum;
  1685. e->ec = aeb->ec;
  1686. ubi_assert(e->ec >= 0);
  1687. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1688. wl_tree_add(e, &ubi->free);
  1689. ubi->free_count++;
  1690. ubi->lookuptbl[e->pnum] = e;
  1691. found_pebs++;
  1692. }
  1693. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1694. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1695. cond_resched();
  1696. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1697. if (!e)
  1698. goto out_free;
  1699. e->pnum = aeb->pnum;
  1700. e->ec = aeb->ec;
  1701. ubi->lookuptbl[e->pnum] = e;
  1702. if (!aeb->scrub) {
  1703. dbg_wl("add PEB %d EC %d to the used tree",
  1704. e->pnum, e->ec);
  1705. wl_tree_add(e, &ubi->used);
  1706. } else {
  1707. dbg_wl("add PEB %d EC %d to the scrub tree",
  1708. e->pnum, e->ec);
  1709. wl_tree_add(e, &ubi->scrub);
  1710. }
  1711. found_pebs++;
  1712. }
  1713. }
  1714. dbg_wl("found %i PEBs", found_pebs);
  1715. if (ubi->fm)
  1716. ubi_assert(ubi->good_peb_count == \
  1717. found_pebs + ubi->fm->used_blocks);
  1718. else
  1719. ubi_assert(ubi->good_peb_count == found_pebs);
  1720. reserved_pebs = WL_RESERVED_PEBS;
  1721. #ifdef CONFIG_MTD_UBI_FASTMAP
  1722. /* Reserve enough LEBs to store two fastmaps. */
  1723. reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
  1724. #endif
  1725. if (ubi->avail_pebs < reserved_pebs) {
  1726. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1727. ubi->avail_pebs, reserved_pebs);
  1728. if (ubi->corr_peb_count)
  1729. ubi_err("%d PEBs are corrupted and not used",
  1730. ubi->corr_peb_count);
  1731. goto out_free;
  1732. }
  1733. ubi->avail_pebs -= reserved_pebs;
  1734. ubi->rsvd_pebs += reserved_pebs;
  1735. /* Schedule wear-leveling if needed */
  1736. err = ensure_wear_leveling(ubi, 0);
  1737. if (err)
  1738. goto out_free;
  1739. return 0;
  1740. out_free:
  1741. cancel_pending(ubi);
  1742. tree_destroy(&ubi->used);
  1743. tree_destroy(&ubi->free);
  1744. tree_destroy(&ubi->scrub);
  1745. kfree(ubi->lookuptbl);
  1746. return err;
  1747. }
  1748. /**
  1749. * protection_queue_destroy - destroy the protection queue.
  1750. * @ubi: UBI device description object
  1751. */
  1752. static void protection_queue_destroy(struct ubi_device *ubi)
  1753. {
  1754. int i;
  1755. struct ubi_wl_entry *e, *tmp;
  1756. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1757. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1758. list_del(&e->u.list);
  1759. kmem_cache_free(ubi_wl_entry_slab, e);
  1760. }
  1761. }
  1762. }
  1763. /**
  1764. * ubi_wl_close - close the wear-leveling sub-system.
  1765. * @ubi: UBI device description object
  1766. */
  1767. void ubi_wl_close(struct ubi_device *ubi)
  1768. {
  1769. dbg_wl("close the WL sub-system");
  1770. cancel_pending(ubi);
  1771. protection_queue_destroy(ubi);
  1772. tree_destroy(&ubi->used);
  1773. tree_destroy(&ubi->erroneous);
  1774. tree_destroy(&ubi->free);
  1775. tree_destroy(&ubi->scrub);
  1776. kfree(ubi->lookuptbl);
  1777. }
  1778. /**
  1779. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1780. * @ubi: UBI device description object
  1781. * @pnum: the physical eraseblock number to check
  1782. * @ec: the erase counter to check
  1783. *
  1784. * This function returns zero if the erase counter of physical eraseblock @pnum
  1785. * is equivalent to @ec, and a negative error code if not or if an error
  1786. * occurred.
  1787. */
  1788. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1789. {
  1790. int err;
  1791. long long read_ec;
  1792. struct ubi_ec_hdr *ec_hdr;
  1793. if (!ubi_dbg_chk_gen(ubi))
  1794. return 0;
  1795. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1796. if (!ec_hdr)
  1797. return -ENOMEM;
  1798. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1799. if (err && err != UBI_IO_BITFLIPS) {
  1800. /* The header does not have to exist */
  1801. err = 0;
  1802. goto out_free;
  1803. }
  1804. read_ec = be64_to_cpu(ec_hdr->ec);
  1805. if (ec != read_ec && read_ec - ec > 1) {
  1806. ubi_err("self-check failed for PEB %d", pnum);
  1807. ubi_err("read EC is %lld, should be %d", read_ec, ec);
  1808. dump_stack();
  1809. err = 1;
  1810. } else
  1811. err = 0;
  1812. out_free:
  1813. kfree(ec_hdr);
  1814. return err;
  1815. }
  1816. /**
  1817. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1818. * @ubi: UBI device description object
  1819. * @e: the wear-leveling entry to check
  1820. * @root: the root of the tree
  1821. *
  1822. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1823. * is not.
  1824. */
  1825. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1826. struct ubi_wl_entry *e, struct rb_root *root)
  1827. {
  1828. if (!ubi_dbg_chk_gen(ubi))
  1829. return 0;
  1830. if (in_wl_tree(e, root))
  1831. return 0;
  1832. ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
  1833. e->pnum, e->ec, root);
  1834. dump_stack();
  1835. return -EINVAL;
  1836. }
  1837. /**
  1838. * self_check_in_pq - check if wear-leveling entry is in the protection
  1839. * queue.
  1840. * @ubi: UBI device description object
  1841. * @e: the wear-leveling entry to check
  1842. *
  1843. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1844. */
  1845. static int self_check_in_pq(const struct ubi_device *ubi,
  1846. struct ubi_wl_entry *e)
  1847. {
  1848. struct ubi_wl_entry *p;
  1849. int i;
  1850. if (!ubi_dbg_chk_gen(ubi))
  1851. return 0;
  1852. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1853. list_for_each_entry(p, &ubi->pq[i], u.list)
  1854. if (p == e)
  1855. return 0;
  1856. ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
  1857. e->pnum, e->ec);
  1858. dump_stack();
  1859. return -EINVAL;
  1860. }