wl.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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. #include "wl.h"
  105. /* Number of physical eraseblocks reserved for wear-leveling purposes */
  106. #define WL_RESERVED_PEBS 1
  107. /*
  108. * Maximum difference between two erase counters. If this threshold is
  109. * exceeded, the WL sub-system starts moving data from used physical
  110. * eraseblocks with low erase counter to free physical eraseblocks with high
  111. * erase counter.
  112. */
  113. #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
  114. /*
  115. * When a physical eraseblock is moved, the WL sub-system has to pick the target
  116. * physical eraseblock to move to. The simplest way would be just to pick the
  117. * one with the highest erase counter. But in certain workloads this could lead
  118. * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
  119. * situation when the picked physical eraseblock is constantly erased after the
  120. * data is written to it. So, we have a constant which limits the highest erase
  121. * counter of the free physical eraseblock to pick. Namely, the WL sub-system
  122. * does not pick eraseblocks with erase counter greater than the lowest erase
  123. * counter plus %WL_FREE_MAX_DIFF.
  124. */
  125. #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
  126. /*
  127. * Maximum number of consecutive background thread failures which is enough to
  128. * switch to read-only mode.
  129. */
  130. #define WL_MAX_FAILURES 32
  131. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
  132. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  133. struct ubi_wl_entry *e, struct rb_root *root);
  134. static int self_check_in_pq(const struct ubi_device *ubi,
  135. struct ubi_wl_entry *e);
  136. /**
  137. * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
  138. * @e: the wear-leveling entry to add
  139. * @root: the root of the tree
  140. *
  141. * Note, we use (erase counter, physical eraseblock number) pairs as keys in
  142. * the @ubi->used and @ubi->free RB-trees.
  143. */
  144. static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
  145. {
  146. struct rb_node **p, *parent = NULL;
  147. p = &root->rb_node;
  148. while (*p) {
  149. struct ubi_wl_entry *e1;
  150. parent = *p;
  151. e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
  152. if (e->ec < e1->ec)
  153. p = &(*p)->rb_left;
  154. else if (e->ec > e1->ec)
  155. p = &(*p)->rb_right;
  156. else {
  157. ubi_assert(e->pnum != e1->pnum);
  158. if (e->pnum < e1->pnum)
  159. p = &(*p)->rb_left;
  160. else
  161. p = &(*p)->rb_right;
  162. }
  163. }
  164. rb_link_node(&e->u.rb, parent, p);
  165. rb_insert_color(&e->u.rb, root);
  166. }
  167. /**
  168. * wl_tree_destroy - destroy a wear-leveling entry.
  169. * @ubi: UBI device description object
  170. * @e: the wear-leveling entry to add
  171. *
  172. * This function destroys a wear leveling entry and removes
  173. * the reference from the lookup table.
  174. */
  175. static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e)
  176. {
  177. ubi->lookuptbl[e->pnum] = NULL;
  178. kmem_cache_free(ubi_wl_entry_slab, e);
  179. }
  180. /**
  181. * do_work - do one pending work.
  182. * @ubi: UBI device description object
  183. *
  184. * This function returns zero in case of success and a negative error code in
  185. * case of failure.
  186. */
  187. static int do_work(struct ubi_device *ubi)
  188. {
  189. int err;
  190. struct ubi_work *wrk;
  191. cond_resched();
  192. /*
  193. * @ubi->work_sem is used to synchronize with the workers. Workers take
  194. * it in read mode, so many of them may be doing works at a time. But
  195. * the queue flush code has to be sure the whole queue of works is
  196. * done, and it takes the mutex in write mode.
  197. */
  198. down_read(&ubi->work_sem);
  199. spin_lock(&ubi->wl_lock);
  200. if (list_empty(&ubi->works)) {
  201. spin_unlock(&ubi->wl_lock);
  202. up_read(&ubi->work_sem);
  203. return 0;
  204. }
  205. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  206. list_del(&wrk->list);
  207. ubi->works_count -= 1;
  208. ubi_assert(ubi->works_count >= 0);
  209. spin_unlock(&ubi->wl_lock);
  210. /*
  211. * Call the worker function. Do not touch the work structure
  212. * after this call as it will have been freed or reused by that
  213. * time by the worker function.
  214. */
  215. err = wrk->func(ubi, wrk, 0);
  216. if (err)
  217. ubi_err(ubi, "work failed with error code %d", err);
  218. up_read(&ubi->work_sem);
  219. return err;
  220. }
  221. /**
  222. * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
  223. * @e: the wear-leveling entry to check
  224. * @root: the root of the tree
  225. *
  226. * This function returns non-zero if @e is in the @root RB-tree and zero if it
  227. * is not.
  228. */
  229. static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
  230. {
  231. struct rb_node *p;
  232. p = root->rb_node;
  233. while (p) {
  234. struct ubi_wl_entry *e1;
  235. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  236. if (e->pnum == e1->pnum) {
  237. ubi_assert(e == e1);
  238. return 1;
  239. }
  240. if (e->ec < e1->ec)
  241. p = p->rb_left;
  242. else if (e->ec > e1->ec)
  243. p = p->rb_right;
  244. else {
  245. ubi_assert(e->pnum != e1->pnum);
  246. if (e->pnum < e1->pnum)
  247. p = p->rb_left;
  248. else
  249. p = p->rb_right;
  250. }
  251. }
  252. return 0;
  253. }
  254. /**
  255. * prot_queue_add - add physical eraseblock to the protection queue.
  256. * @ubi: UBI device description object
  257. * @e: the physical eraseblock to add
  258. *
  259. * This function adds @e to the tail of the protection queue @ubi->pq, where
  260. * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
  261. * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
  262. * be locked.
  263. */
  264. static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
  265. {
  266. int pq_tail = ubi->pq_head - 1;
  267. if (pq_tail < 0)
  268. pq_tail = UBI_PROT_QUEUE_LEN - 1;
  269. ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
  270. list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
  271. dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
  272. }
  273. /**
  274. * find_wl_entry - find wear-leveling entry closest to certain erase counter.
  275. * @ubi: UBI device description object
  276. * @root: the RB-tree where to look for
  277. * @diff: maximum possible difference from the smallest erase counter
  278. *
  279. * This function looks for a wear leveling entry with erase counter closest to
  280. * min + @diff, where min is the smallest erase counter.
  281. */
  282. static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
  283. struct rb_root *root, int diff)
  284. {
  285. struct rb_node *p;
  286. struct ubi_wl_entry *e, *prev_e = NULL;
  287. int max;
  288. e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  289. max = e->ec + diff;
  290. p = root->rb_node;
  291. while (p) {
  292. struct ubi_wl_entry *e1;
  293. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  294. if (e1->ec >= max)
  295. p = p->rb_left;
  296. else {
  297. p = p->rb_right;
  298. prev_e = e;
  299. e = e1;
  300. }
  301. }
  302. /* If no fastmap has been written and this WL entry can be used
  303. * as anchor PEB, hold it back and return the second best WL entry
  304. * such that fastmap can use the anchor PEB later. */
  305. if (prev_e && !ubi->fm_disabled &&
  306. !ubi->fm && e->pnum < UBI_FM_MAX_START)
  307. return prev_e;
  308. return e;
  309. }
  310. /**
  311. * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
  312. * @ubi: UBI device description object
  313. * @root: the RB-tree where to look for
  314. *
  315. * This function looks for a wear leveling entry with medium erase counter,
  316. * but not greater or equivalent than the lowest erase counter plus
  317. * %WL_FREE_MAX_DIFF/2.
  318. */
  319. static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
  320. struct rb_root *root)
  321. {
  322. struct ubi_wl_entry *e, *first, *last;
  323. first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  324. last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
  325. if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
  326. e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
  327. /* If no fastmap has been written and this WL entry can be used
  328. * as anchor PEB, hold it back and return the second best
  329. * WL entry such that fastmap can use the anchor PEB later. */
  330. e = may_reserve_for_fm(ubi, e, root);
  331. } else
  332. e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
  333. return e;
  334. }
  335. /**
  336. * wl_get_wle - get a mean wl entry to be used by ubi_wl_get_peb() or
  337. * refill_wl_user_pool().
  338. * @ubi: UBI device description object
  339. *
  340. * This function returns a a wear leveling entry in case of success and
  341. * NULL in case of failure.
  342. */
  343. static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi)
  344. {
  345. struct ubi_wl_entry *e;
  346. e = find_mean_wl_entry(ubi, &ubi->free);
  347. if (!e) {
  348. ubi_err(ubi, "no free eraseblocks");
  349. return NULL;
  350. }
  351. self_check_in_wl_tree(ubi, e, &ubi->free);
  352. /*
  353. * Move the physical eraseblock to the protection queue where it will
  354. * be protected from being moved for some time.
  355. */
  356. rb_erase(&e->u.rb, &ubi->free);
  357. ubi->free_count--;
  358. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  359. return e;
  360. }
  361. /**
  362. * prot_queue_del - remove a physical eraseblock from the protection queue.
  363. * @ubi: UBI device description object
  364. * @pnum: the physical eraseblock to remove
  365. *
  366. * This function deletes PEB @pnum from the protection queue and returns zero
  367. * in case of success and %-ENODEV if the PEB was not found.
  368. */
  369. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  370. {
  371. struct ubi_wl_entry *e;
  372. e = ubi->lookuptbl[pnum];
  373. if (!e)
  374. return -ENODEV;
  375. if (self_check_in_pq(ubi, e))
  376. return -ENODEV;
  377. list_del(&e->u.list);
  378. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  379. return 0;
  380. }
  381. /**
  382. * sync_erase - synchronously erase a physical eraseblock.
  383. * @ubi: UBI device description object
  384. * @e: the the physical eraseblock to erase
  385. * @torture: if the physical eraseblock has to be tortured
  386. *
  387. * This function returns zero in case of success and a negative error code in
  388. * case of failure.
  389. */
  390. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  391. int torture)
  392. {
  393. int err;
  394. struct ubi_ec_hdr *ec_hdr;
  395. unsigned long long ec = e->ec;
  396. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  397. err = self_check_ec(ubi, e->pnum, e->ec);
  398. if (err)
  399. return -EINVAL;
  400. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  401. if (!ec_hdr)
  402. return -ENOMEM;
  403. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  404. if (err < 0)
  405. goto out_free;
  406. ec += err;
  407. if (ec > UBI_MAX_ERASECOUNTER) {
  408. /*
  409. * Erase counter overflow. Upgrade UBI and use 64-bit
  410. * erase counters internally.
  411. */
  412. ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
  413. e->pnum, ec);
  414. err = -EINVAL;
  415. goto out_free;
  416. }
  417. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  418. ec_hdr->ec = cpu_to_be64(ec);
  419. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  420. if (err)
  421. goto out_free;
  422. e->ec = ec;
  423. spin_lock(&ubi->wl_lock);
  424. if (e->ec > ubi->max_ec)
  425. ubi->max_ec = e->ec;
  426. spin_unlock(&ubi->wl_lock);
  427. out_free:
  428. kfree(ec_hdr);
  429. return err;
  430. }
  431. /**
  432. * serve_prot_queue - check if it is time to stop protecting PEBs.
  433. * @ubi: UBI device description object
  434. *
  435. * This function is called after each erase operation and removes PEBs from the
  436. * tail of the protection queue. These PEBs have been protected for long enough
  437. * and should be moved to the used tree.
  438. */
  439. static void serve_prot_queue(struct ubi_device *ubi)
  440. {
  441. struct ubi_wl_entry *e, *tmp;
  442. int count;
  443. /*
  444. * There may be several protected physical eraseblock to remove,
  445. * process them all.
  446. */
  447. repeat:
  448. count = 0;
  449. spin_lock(&ubi->wl_lock);
  450. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  451. dbg_wl("PEB %d EC %d protection over, move to used tree",
  452. e->pnum, e->ec);
  453. list_del(&e->u.list);
  454. wl_tree_add(e, &ubi->used);
  455. if (count++ > 32) {
  456. /*
  457. * Let's be nice and avoid holding the spinlock for
  458. * too long.
  459. */
  460. spin_unlock(&ubi->wl_lock);
  461. cond_resched();
  462. goto repeat;
  463. }
  464. }
  465. ubi->pq_head += 1;
  466. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  467. ubi->pq_head = 0;
  468. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  469. spin_unlock(&ubi->wl_lock);
  470. }
  471. /**
  472. * __schedule_ubi_work - schedule a work.
  473. * @ubi: UBI device description object
  474. * @wrk: the work to schedule
  475. *
  476. * This function adds a work defined by @wrk to the tail of the pending works
  477. * list. Can only be used if ubi->work_sem is already held in read mode!
  478. */
  479. static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  480. {
  481. spin_lock(&ubi->wl_lock);
  482. list_add_tail(&wrk->list, &ubi->works);
  483. ubi_assert(ubi->works_count >= 0);
  484. ubi->works_count += 1;
  485. if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
  486. wake_up_process(ubi->bgt_thread);
  487. spin_unlock(&ubi->wl_lock);
  488. }
  489. /**
  490. * schedule_ubi_work - schedule a work.
  491. * @ubi: UBI device description object
  492. * @wrk: the work to schedule
  493. *
  494. * This function adds a work defined by @wrk to the tail of the pending works
  495. * list.
  496. */
  497. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  498. {
  499. down_read(&ubi->work_sem);
  500. __schedule_ubi_work(ubi, wrk);
  501. up_read(&ubi->work_sem);
  502. }
  503. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  504. int shutdown);
  505. /**
  506. * schedule_erase - schedule an erase work.
  507. * @ubi: UBI device description object
  508. * @e: the WL entry of the physical eraseblock to erase
  509. * @vol_id: the volume ID that last used this PEB
  510. * @lnum: the last used logical eraseblock number for the PEB
  511. * @torture: if the physical eraseblock has to be tortured
  512. *
  513. * This function returns zero in case of success and a %-ENOMEM in case of
  514. * failure.
  515. */
  516. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  517. int vol_id, int lnum, int torture)
  518. {
  519. struct ubi_work *wl_wrk;
  520. ubi_assert(e);
  521. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  522. e->pnum, e->ec, torture);
  523. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  524. if (!wl_wrk)
  525. return -ENOMEM;
  526. wl_wrk->func = &erase_worker;
  527. wl_wrk->e = e;
  528. wl_wrk->vol_id = vol_id;
  529. wl_wrk->lnum = lnum;
  530. wl_wrk->torture = torture;
  531. schedule_ubi_work(ubi, wl_wrk);
  532. return 0;
  533. }
  534. static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk);
  535. /**
  536. * do_sync_erase - run the erase worker synchronously.
  537. * @ubi: UBI device description object
  538. * @e: the WL entry of the physical eraseblock to erase
  539. * @vol_id: the volume ID that last used this PEB
  540. * @lnum: the last used logical eraseblock number for the PEB
  541. * @torture: if the physical eraseblock has to be tortured
  542. *
  543. */
  544. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  545. int vol_id, int lnum, int torture)
  546. {
  547. struct ubi_work wl_wrk;
  548. dbg_wl("sync erase of PEB %i", e->pnum);
  549. wl_wrk.e = e;
  550. wl_wrk.vol_id = vol_id;
  551. wl_wrk.lnum = lnum;
  552. wl_wrk.torture = torture;
  553. return __erase_worker(ubi, &wl_wrk);
  554. }
  555. static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
  556. /**
  557. * wear_leveling_worker - wear-leveling worker function.
  558. * @ubi: UBI device description object
  559. * @wrk: the work object
  560. * @shutdown: non-zero if the worker has to free memory and exit
  561. * because the WL-subsystem is shutting down
  562. *
  563. * This function copies a more worn out physical eraseblock to a less worn out
  564. * one. Returns zero in case of success and a negative error code in case of
  565. * failure.
  566. */
  567. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  568. int shutdown)
  569. {
  570. int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  571. int vol_id = -1, lnum = -1;
  572. #ifdef CONFIG_MTD_UBI_FASTMAP
  573. int anchor = wrk->anchor;
  574. #endif
  575. struct ubi_wl_entry *e1, *e2;
  576. struct ubi_vid_hdr *vid_hdr;
  577. int dst_leb_clean = 0;
  578. kfree(wrk);
  579. if (shutdown)
  580. return 0;
  581. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  582. if (!vid_hdr)
  583. return -ENOMEM;
  584. mutex_lock(&ubi->move_mutex);
  585. spin_lock(&ubi->wl_lock);
  586. ubi_assert(!ubi->move_from && !ubi->move_to);
  587. ubi_assert(!ubi->move_to_put);
  588. if (!ubi->free.rb_node ||
  589. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  590. /*
  591. * No free physical eraseblocks? Well, they must be waiting in
  592. * the queue to be erased. Cancel movement - it will be
  593. * triggered again when a free physical eraseblock appears.
  594. *
  595. * No used physical eraseblocks? They must be temporarily
  596. * protected from being moved. They will be moved to the
  597. * @ubi->used tree later and the wear-leveling will be
  598. * triggered again.
  599. */
  600. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  601. !ubi->free.rb_node, !ubi->used.rb_node);
  602. goto out_cancel;
  603. }
  604. #ifdef CONFIG_MTD_UBI_FASTMAP
  605. /* Check whether we need to produce an anchor PEB */
  606. if (!anchor)
  607. anchor = !anchor_pebs_avalible(&ubi->free);
  608. if (anchor) {
  609. e1 = find_anchor_wl_entry(&ubi->used);
  610. if (!e1)
  611. goto out_cancel;
  612. e2 = get_peb_for_wl(ubi);
  613. if (!e2)
  614. goto out_cancel;
  615. self_check_in_wl_tree(ubi, e1, &ubi->used);
  616. rb_erase(&e1->u.rb, &ubi->used);
  617. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  618. } else if (!ubi->scrub.rb_node) {
  619. #else
  620. if (!ubi->scrub.rb_node) {
  621. #endif
  622. /*
  623. * Now pick the least worn-out used physical eraseblock and a
  624. * highly worn-out free physical eraseblock. If the erase
  625. * counters differ much enough, start wear-leveling.
  626. */
  627. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  628. e2 = get_peb_for_wl(ubi);
  629. if (!e2)
  630. goto out_cancel;
  631. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  632. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  633. e1->ec, e2->ec);
  634. /* Give the unused PEB back */
  635. wl_tree_add(e2, &ubi->free);
  636. ubi->free_count++;
  637. goto out_cancel;
  638. }
  639. self_check_in_wl_tree(ubi, e1, &ubi->used);
  640. rb_erase(&e1->u.rb, &ubi->used);
  641. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  642. e1->pnum, e1->ec, e2->pnum, e2->ec);
  643. } else {
  644. /* Perform scrubbing */
  645. scrubbing = 1;
  646. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  647. e2 = get_peb_for_wl(ubi);
  648. if (!e2)
  649. goto out_cancel;
  650. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  651. rb_erase(&e1->u.rb, &ubi->scrub);
  652. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  653. }
  654. ubi->move_from = e1;
  655. ubi->move_to = e2;
  656. spin_unlock(&ubi->wl_lock);
  657. /*
  658. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  659. * We so far do not know which logical eraseblock our physical
  660. * eraseblock (@e1) belongs to. We have to read the volume identifier
  661. * header first.
  662. *
  663. * Note, we are protected from this PEB being unmapped and erased. The
  664. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  665. * which is being moved was unmapped.
  666. */
  667. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  668. if (err && err != UBI_IO_BITFLIPS) {
  669. dst_leb_clean = 1;
  670. if (err == UBI_IO_FF) {
  671. /*
  672. * We are trying to move PEB without a VID header. UBI
  673. * always write VID headers shortly after the PEB was
  674. * given, so we have a situation when it has not yet
  675. * had a chance to write it, because it was preempted.
  676. * So add this PEB to the protection queue so far,
  677. * because presumably more data will be written there
  678. * (including the missing VID header), and then we'll
  679. * move it.
  680. */
  681. dbg_wl("PEB %d has no VID header", e1->pnum);
  682. protect = 1;
  683. goto out_not_moved;
  684. } else if (err == UBI_IO_FF_BITFLIPS) {
  685. /*
  686. * The same situation as %UBI_IO_FF, but bit-flips were
  687. * detected. It is better to schedule this PEB for
  688. * scrubbing.
  689. */
  690. dbg_wl("PEB %d has no VID header but has bit-flips",
  691. e1->pnum);
  692. scrubbing = 1;
  693. goto out_not_moved;
  694. }
  695. ubi_err(ubi, "error %d while reading VID header from PEB %d",
  696. err, e1->pnum);
  697. goto out_error;
  698. }
  699. vol_id = be32_to_cpu(vid_hdr->vol_id);
  700. lnum = be32_to_cpu(vid_hdr->lnum);
  701. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  702. if (err) {
  703. if (err == MOVE_CANCEL_RACE) {
  704. /*
  705. * The LEB has not been moved because the volume is
  706. * being deleted or the PEB has been put meanwhile. We
  707. * should prevent this PEB from being selected for
  708. * wear-leveling movement again, so put it to the
  709. * protection queue.
  710. */
  711. protect = 1;
  712. dst_leb_clean = 1;
  713. goto out_not_moved;
  714. }
  715. if (err == MOVE_RETRY) {
  716. scrubbing = 1;
  717. dst_leb_clean = 1;
  718. goto out_not_moved;
  719. }
  720. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  721. err == MOVE_TARGET_RD_ERR) {
  722. /*
  723. * Target PEB had bit-flips or write error - torture it.
  724. */
  725. torture = 1;
  726. goto out_not_moved;
  727. }
  728. if (err == MOVE_SOURCE_RD_ERR) {
  729. /*
  730. * An error happened while reading the source PEB. Do
  731. * not switch to R/O mode in this case, and give the
  732. * upper layers a possibility to recover from this,
  733. * e.g. by unmapping corresponding LEB. Instead, just
  734. * put this PEB to the @ubi->erroneous list to prevent
  735. * UBI from trying to move it over and over again.
  736. */
  737. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  738. ubi_err(ubi, "too many erroneous eraseblocks (%d)",
  739. ubi->erroneous_peb_count);
  740. goto out_error;
  741. }
  742. dst_leb_clean = 1;
  743. erroneous = 1;
  744. goto out_not_moved;
  745. }
  746. if (err < 0)
  747. goto out_error;
  748. ubi_assert(0);
  749. }
  750. /* The PEB has been successfully moved */
  751. if (scrubbing)
  752. ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  753. e1->pnum, vol_id, lnum, e2->pnum);
  754. ubi_free_vid_hdr(ubi, vid_hdr);
  755. spin_lock(&ubi->wl_lock);
  756. if (!ubi->move_to_put) {
  757. wl_tree_add(e2, &ubi->used);
  758. e2 = NULL;
  759. }
  760. ubi->move_from = ubi->move_to = NULL;
  761. ubi->move_to_put = ubi->wl_scheduled = 0;
  762. spin_unlock(&ubi->wl_lock);
  763. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  764. if (err) {
  765. if (e2)
  766. wl_entry_destroy(ubi, e2);
  767. goto out_ro;
  768. }
  769. if (e2) {
  770. /*
  771. * Well, the target PEB was put meanwhile, schedule it for
  772. * erasure.
  773. */
  774. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  775. e2->pnum, vol_id, lnum);
  776. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  777. if (err)
  778. goto out_ro;
  779. }
  780. dbg_wl("done");
  781. mutex_unlock(&ubi->move_mutex);
  782. return 0;
  783. /*
  784. * For some reasons the LEB was not moved, might be an error, might be
  785. * something else. @e1 was not changed, so return it back. @e2 might
  786. * have been changed, schedule it for erasure.
  787. */
  788. out_not_moved:
  789. if (vol_id != -1)
  790. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  791. e1->pnum, vol_id, lnum, e2->pnum, err);
  792. else
  793. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  794. e1->pnum, e2->pnum, err);
  795. spin_lock(&ubi->wl_lock);
  796. if (protect)
  797. prot_queue_add(ubi, e1);
  798. else if (erroneous) {
  799. wl_tree_add(e1, &ubi->erroneous);
  800. ubi->erroneous_peb_count += 1;
  801. } else if (scrubbing)
  802. wl_tree_add(e1, &ubi->scrub);
  803. else
  804. wl_tree_add(e1, &ubi->used);
  805. if (dst_leb_clean) {
  806. wl_tree_add(e2, &ubi->free);
  807. ubi->free_count++;
  808. }
  809. ubi_assert(!ubi->move_to_put);
  810. ubi->move_from = ubi->move_to = NULL;
  811. ubi->wl_scheduled = 0;
  812. spin_unlock(&ubi->wl_lock);
  813. ubi_free_vid_hdr(ubi, vid_hdr);
  814. if (dst_leb_clean) {
  815. ensure_wear_leveling(ubi, 1);
  816. } else {
  817. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  818. if (err)
  819. goto out_ro;
  820. }
  821. mutex_unlock(&ubi->move_mutex);
  822. return 0;
  823. out_error:
  824. if (vol_id != -1)
  825. ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
  826. err, e1->pnum, e2->pnum);
  827. else
  828. ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  829. err, e1->pnum, vol_id, lnum, e2->pnum);
  830. spin_lock(&ubi->wl_lock);
  831. ubi->move_from = ubi->move_to = NULL;
  832. ubi->move_to_put = ubi->wl_scheduled = 0;
  833. spin_unlock(&ubi->wl_lock);
  834. ubi_free_vid_hdr(ubi, vid_hdr);
  835. wl_entry_destroy(ubi, e1);
  836. wl_entry_destroy(ubi, e2);
  837. out_ro:
  838. ubi_ro_mode(ubi);
  839. mutex_unlock(&ubi->move_mutex);
  840. ubi_assert(err != 0);
  841. return err < 0 ? err : -EIO;
  842. out_cancel:
  843. ubi->wl_scheduled = 0;
  844. spin_unlock(&ubi->wl_lock);
  845. mutex_unlock(&ubi->move_mutex);
  846. ubi_free_vid_hdr(ubi, vid_hdr);
  847. return 0;
  848. }
  849. /**
  850. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  851. * @ubi: UBI device description object
  852. * @nested: set to non-zero if this function is called from UBI worker
  853. *
  854. * This function checks if it is time to start wear-leveling and schedules it
  855. * if yes. This function returns zero in case of success and a negative error
  856. * code in case of failure.
  857. */
  858. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  859. {
  860. int err = 0;
  861. struct ubi_wl_entry *e1;
  862. struct ubi_wl_entry *e2;
  863. struct ubi_work *wrk;
  864. spin_lock(&ubi->wl_lock);
  865. if (ubi->wl_scheduled)
  866. /* Wear-leveling is already in the work queue */
  867. goto out_unlock;
  868. /*
  869. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  870. * the WL worker has to be scheduled anyway.
  871. */
  872. if (!ubi->scrub.rb_node) {
  873. if (!ubi->used.rb_node || !ubi->free.rb_node)
  874. /* No physical eraseblocks - no deal */
  875. goto out_unlock;
  876. /*
  877. * We schedule wear-leveling only if the difference between the
  878. * lowest erase counter of used physical eraseblocks and a high
  879. * erase counter of free physical eraseblocks is greater than
  880. * %UBI_WL_THRESHOLD.
  881. */
  882. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  883. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  884. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  885. goto out_unlock;
  886. dbg_wl("schedule wear-leveling");
  887. } else
  888. dbg_wl("schedule scrubbing");
  889. ubi->wl_scheduled = 1;
  890. spin_unlock(&ubi->wl_lock);
  891. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  892. if (!wrk) {
  893. err = -ENOMEM;
  894. goto out_cancel;
  895. }
  896. wrk->anchor = 0;
  897. wrk->func = &wear_leveling_worker;
  898. if (nested)
  899. __schedule_ubi_work(ubi, wrk);
  900. else
  901. schedule_ubi_work(ubi, wrk);
  902. return err;
  903. out_cancel:
  904. spin_lock(&ubi->wl_lock);
  905. ubi->wl_scheduled = 0;
  906. out_unlock:
  907. spin_unlock(&ubi->wl_lock);
  908. return err;
  909. }
  910. /**
  911. * __erase_worker - physical eraseblock erase worker function.
  912. * @ubi: UBI device description object
  913. * @wl_wrk: the work object
  914. * @shutdown: non-zero if the worker has to free memory and exit
  915. * because the WL sub-system is shutting down
  916. *
  917. * This function erases a physical eraseblock and perform torture testing if
  918. * needed. It also takes care about marking the physical eraseblock bad if
  919. * needed. Returns zero in case of success and a negative error code in case of
  920. * failure.
  921. */
  922. static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
  923. {
  924. struct ubi_wl_entry *e = wl_wrk->e;
  925. int pnum = e->pnum;
  926. int vol_id = wl_wrk->vol_id;
  927. int lnum = wl_wrk->lnum;
  928. int err, available_consumed = 0;
  929. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  930. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  931. err = sync_erase(ubi, e, wl_wrk->torture);
  932. if (!err) {
  933. spin_lock(&ubi->wl_lock);
  934. wl_tree_add(e, &ubi->free);
  935. ubi->free_count++;
  936. spin_unlock(&ubi->wl_lock);
  937. /*
  938. * One more erase operation has happened, take care about
  939. * protected physical eraseblocks.
  940. */
  941. serve_prot_queue(ubi);
  942. /* And take care about wear-leveling */
  943. err = ensure_wear_leveling(ubi, 1);
  944. return err;
  945. }
  946. ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
  947. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  948. err == -EBUSY) {
  949. int err1;
  950. /* Re-schedule the LEB for erasure */
  951. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  952. if (err1) {
  953. wl_entry_destroy(ubi, e);
  954. err = err1;
  955. goto out_ro;
  956. }
  957. return err;
  958. }
  959. wl_entry_destroy(ubi, e);
  960. if (err != -EIO)
  961. /*
  962. * If this is not %-EIO, we have no idea what to do. Scheduling
  963. * this physical eraseblock for erasure again would cause
  964. * errors again and again. Well, lets switch to R/O mode.
  965. */
  966. goto out_ro;
  967. /* It is %-EIO, the PEB went bad */
  968. if (!ubi->bad_allowed) {
  969. ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
  970. goto out_ro;
  971. }
  972. spin_lock(&ubi->volumes_lock);
  973. if (ubi->beb_rsvd_pebs == 0) {
  974. if (ubi->avail_pebs == 0) {
  975. spin_unlock(&ubi->volumes_lock);
  976. ubi_err(ubi, "no reserved/available physical eraseblocks");
  977. goto out_ro;
  978. }
  979. ubi->avail_pebs -= 1;
  980. available_consumed = 1;
  981. }
  982. spin_unlock(&ubi->volumes_lock);
  983. ubi_msg(ubi, "mark PEB %d as bad", pnum);
  984. err = ubi_io_mark_bad(ubi, pnum);
  985. if (err)
  986. goto out_ro;
  987. spin_lock(&ubi->volumes_lock);
  988. if (ubi->beb_rsvd_pebs > 0) {
  989. if (available_consumed) {
  990. /*
  991. * The amount of reserved PEBs increased since we last
  992. * checked.
  993. */
  994. ubi->avail_pebs += 1;
  995. available_consumed = 0;
  996. }
  997. ubi->beb_rsvd_pebs -= 1;
  998. }
  999. ubi->bad_peb_count += 1;
  1000. ubi->good_peb_count -= 1;
  1001. ubi_calculate_reserved(ubi);
  1002. if (available_consumed)
  1003. ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
  1004. else if (ubi->beb_rsvd_pebs)
  1005. ubi_msg(ubi, "%d PEBs left in the reserve",
  1006. ubi->beb_rsvd_pebs);
  1007. else
  1008. ubi_warn(ubi, "last PEB from the reserve was used");
  1009. spin_unlock(&ubi->volumes_lock);
  1010. return err;
  1011. out_ro:
  1012. if (available_consumed) {
  1013. spin_lock(&ubi->volumes_lock);
  1014. ubi->avail_pebs += 1;
  1015. spin_unlock(&ubi->volumes_lock);
  1016. }
  1017. ubi_ro_mode(ubi);
  1018. return err;
  1019. }
  1020. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1021. int shutdown)
  1022. {
  1023. int ret;
  1024. if (shutdown) {
  1025. struct ubi_wl_entry *e = wl_wrk->e;
  1026. dbg_wl("cancel erasure of PEB %d EC %d", e->pnum, e->ec);
  1027. kfree(wl_wrk);
  1028. wl_entry_destroy(ubi, e);
  1029. return 0;
  1030. }
  1031. ret = __erase_worker(ubi, wl_wrk);
  1032. kfree(wl_wrk);
  1033. return ret;
  1034. }
  1035. /**
  1036. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1037. * @ubi: UBI device description object
  1038. * @vol_id: the volume ID that last used this PEB
  1039. * @lnum: the last used logical eraseblock number for the PEB
  1040. * @pnum: physical eraseblock to return
  1041. * @torture: if this physical eraseblock has to be tortured
  1042. *
  1043. * This function is called to return physical eraseblock @pnum to the pool of
  1044. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1045. * occurred to this @pnum and it has to be tested. This function returns zero
  1046. * in case of success, and a negative error code in case of failure.
  1047. */
  1048. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1049. int pnum, int torture)
  1050. {
  1051. int err;
  1052. struct ubi_wl_entry *e;
  1053. dbg_wl("PEB %d", pnum);
  1054. ubi_assert(pnum >= 0);
  1055. ubi_assert(pnum < ubi->peb_count);
  1056. down_read(&ubi->fm_protect);
  1057. retry:
  1058. spin_lock(&ubi->wl_lock);
  1059. e = ubi->lookuptbl[pnum];
  1060. if (e == ubi->move_from) {
  1061. /*
  1062. * User is putting the physical eraseblock which was selected to
  1063. * be moved. It will be scheduled for erasure in the
  1064. * wear-leveling worker.
  1065. */
  1066. dbg_wl("PEB %d is being moved, wait", pnum);
  1067. spin_unlock(&ubi->wl_lock);
  1068. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1069. mutex_lock(&ubi->move_mutex);
  1070. mutex_unlock(&ubi->move_mutex);
  1071. goto retry;
  1072. } else if (e == ubi->move_to) {
  1073. /*
  1074. * User is putting the physical eraseblock which was selected
  1075. * as the target the data is moved to. It may happen if the EBA
  1076. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1077. * but the WL sub-system has not put the PEB to the "used" tree
  1078. * yet, but it is about to do this. So we just set a flag which
  1079. * will tell the WL worker that the PEB is not needed anymore
  1080. * and should be scheduled for erasure.
  1081. */
  1082. dbg_wl("PEB %d is the target of data moving", pnum);
  1083. ubi_assert(!ubi->move_to_put);
  1084. ubi->move_to_put = 1;
  1085. spin_unlock(&ubi->wl_lock);
  1086. up_read(&ubi->fm_protect);
  1087. return 0;
  1088. } else {
  1089. if (in_wl_tree(e, &ubi->used)) {
  1090. self_check_in_wl_tree(ubi, e, &ubi->used);
  1091. rb_erase(&e->u.rb, &ubi->used);
  1092. } else if (in_wl_tree(e, &ubi->scrub)) {
  1093. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1094. rb_erase(&e->u.rb, &ubi->scrub);
  1095. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1096. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1097. rb_erase(&e->u.rb, &ubi->erroneous);
  1098. ubi->erroneous_peb_count -= 1;
  1099. ubi_assert(ubi->erroneous_peb_count >= 0);
  1100. /* Erroneous PEBs should be tortured */
  1101. torture = 1;
  1102. } else {
  1103. err = prot_queue_del(ubi, e->pnum);
  1104. if (err) {
  1105. ubi_err(ubi, "PEB %d not found", pnum);
  1106. ubi_ro_mode(ubi);
  1107. spin_unlock(&ubi->wl_lock);
  1108. up_read(&ubi->fm_protect);
  1109. return err;
  1110. }
  1111. }
  1112. }
  1113. spin_unlock(&ubi->wl_lock);
  1114. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1115. if (err) {
  1116. spin_lock(&ubi->wl_lock);
  1117. wl_tree_add(e, &ubi->used);
  1118. spin_unlock(&ubi->wl_lock);
  1119. }
  1120. up_read(&ubi->fm_protect);
  1121. return err;
  1122. }
  1123. /**
  1124. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1125. * @ubi: UBI device description object
  1126. * @pnum: the physical eraseblock to schedule
  1127. *
  1128. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1129. * needs scrubbing. This function schedules a physical eraseblock for
  1130. * scrubbing which is done in background. This function returns zero in case of
  1131. * success and a negative error code in case of failure.
  1132. */
  1133. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1134. {
  1135. struct ubi_wl_entry *e;
  1136. ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
  1137. retry:
  1138. spin_lock(&ubi->wl_lock);
  1139. e = ubi->lookuptbl[pnum];
  1140. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1141. in_wl_tree(e, &ubi->erroneous)) {
  1142. spin_unlock(&ubi->wl_lock);
  1143. return 0;
  1144. }
  1145. if (e == ubi->move_to) {
  1146. /*
  1147. * This physical eraseblock was used to move data to. The data
  1148. * was moved but the PEB was not yet inserted to the proper
  1149. * tree. We should just wait a little and let the WL worker
  1150. * proceed.
  1151. */
  1152. spin_unlock(&ubi->wl_lock);
  1153. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1154. yield();
  1155. goto retry;
  1156. }
  1157. if (in_wl_tree(e, &ubi->used)) {
  1158. self_check_in_wl_tree(ubi, e, &ubi->used);
  1159. rb_erase(&e->u.rb, &ubi->used);
  1160. } else {
  1161. int err;
  1162. err = prot_queue_del(ubi, e->pnum);
  1163. if (err) {
  1164. ubi_err(ubi, "PEB %d not found", pnum);
  1165. ubi_ro_mode(ubi);
  1166. spin_unlock(&ubi->wl_lock);
  1167. return err;
  1168. }
  1169. }
  1170. wl_tree_add(e, &ubi->scrub);
  1171. spin_unlock(&ubi->wl_lock);
  1172. /*
  1173. * Technically scrubbing is the same as wear-leveling, so it is done
  1174. * by the WL worker.
  1175. */
  1176. return ensure_wear_leveling(ubi, 0);
  1177. }
  1178. /**
  1179. * ubi_wl_flush - flush all pending works.
  1180. * @ubi: UBI device description object
  1181. * @vol_id: the volume id to flush for
  1182. * @lnum: the logical eraseblock number to flush for
  1183. *
  1184. * This function executes all pending works for a particular volume id /
  1185. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1186. * acts as a wildcard for all of the corresponding volume numbers or logical
  1187. * eraseblock numbers. It returns zero in case of success and a negative error
  1188. * code in case of failure.
  1189. */
  1190. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1191. {
  1192. int err = 0;
  1193. int found = 1;
  1194. /*
  1195. * Erase while the pending works queue is not empty, but not more than
  1196. * the number of currently pending works.
  1197. */
  1198. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1199. vol_id, lnum, ubi->works_count);
  1200. while (found) {
  1201. struct ubi_work *wrk, *tmp;
  1202. found = 0;
  1203. down_read(&ubi->work_sem);
  1204. spin_lock(&ubi->wl_lock);
  1205. list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
  1206. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1207. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1208. list_del(&wrk->list);
  1209. ubi->works_count -= 1;
  1210. ubi_assert(ubi->works_count >= 0);
  1211. spin_unlock(&ubi->wl_lock);
  1212. err = wrk->func(ubi, wrk, 0);
  1213. if (err) {
  1214. up_read(&ubi->work_sem);
  1215. return err;
  1216. }
  1217. spin_lock(&ubi->wl_lock);
  1218. found = 1;
  1219. break;
  1220. }
  1221. }
  1222. spin_unlock(&ubi->wl_lock);
  1223. up_read(&ubi->work_sem);
  1224. }
  1225. /*
  1226. * Make sure all the works which have been done in parallel are
  1227. * finished.
  1228. */
  1229. down_write(&ubi->work_sem);
  1230. up_write(&ubi->work_sem);
  1231. return err;
  1232. }
  1233. /**
  1234. * tree_destroy - destroy an RB-tree.
  1235. * @ubi: UBI device description object
  1236. * @root: the root of the tree to destroy
  1237. */
  1238. static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
  1239. {
  1240. struct rb_node *rb;
  1241. struct ubi_wl_entry *e;
  1242. rb = root->rb_node;
  1243. while (rb) {
  1244. if (rb->rb_left)
  1245. rb = rb->rb_left;
  1246. else if (rb->rb_right)
  1247. rb = rb->rb_right;
  1248. else {
  1249. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1250. rb = rb_parent(rb);
  1251. if (rb) {
  1252. if (rb->rb_left == &e->u.rb)
  1253. rb->rb_left = NULL;
  1254. else
  1255. rb->rb_right = NULL;
  1256. }
  1257. wl_entry_destroy(ubi, e);
  1258. }
  1259. }
  1260. }
  1261. /**
  1262. * ubi_thread - UBI background thread.
  1263. * @u: the UBI device description object pointer
  1264. */
  1265. int ubi_thread(void *u)
  1266. {
  1267. int failures = 0;
  1268. struct ubi_device *ubi = u;
  1269. ubi_msg(ubi, "background thread \"%s\" started, PID %d",
  1270. ubi->bgt_name, task_pid_nr(current));
  1271. set_freezable();
  1272. for (;;) {
  1273. int err;
  1274. if (kthread_should_stop())
  1275. break;
  1276. if (try_to_freeze())
  1277. continue;
  1278. spin_lock(&ubi->wl_lock);
  1279. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1280. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1281. set_current_state(TASK_INTERRUPTIBLE);
  1282. spin_unlock(&ubi->wl_lock);
  1283. schedule();
  1284. continue;
  1285. }
  1286. spin_unlock(&ubi->wl_lock);
  1287. err = do_work(ubi);
  1288. if (err) {
  1289. ubi_err(ubi, "%s: work failed with error code %d",
  1290. ubi->bgt_name, err);
  1291. if (failures++ > WL_MAX_FAILURES) {
  1292. /*
  1293. * Too many failures, disable the thread and
  1294. * switch to read-only mode.
  1295. */
  1296. ubi_msg(ubi, "%s: %d consecutive failures",
  1297. ubi->bgt_name, WL_MAX_FAILURES);
  1298. ubi_ro_mode(ubi);
  1299. ubi->thread_enabled = 0;
  1300. continue;
  1301. }
  1302. } else
  1303. failures = 0;
  1304. cond_resched();
  1305. }
  1306. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1307. return 0;
  1308. }
  1309. /**
  1310. * shutdown_work - shutdown all pending works.
  1311. * @ubi: UBI device description object
  1312. */
  1313. static void shutdown_work(struct ubi_device *ubi)
  1314. {
  1315. #ifdef CONFIG_MTD_UBI_FASTMAP
  1316. flush_work(&ubi->fm_work);
  1317. #endif
  1318. while (!list_empty(&ubi->works)) {
  1319. struct ubi_work *wrk;
  1320. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1321. list_del(&wrk->list);
  1322. wrk->func(ubi, wrk, 1);
  1323. ubi->works_count -= 1;
  1324. ubi_assert(ubi->works_count >= 0);
  1325. }
  1326. }
  1327. /**
  1328. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1329. * @ubi: UBI device description object
  1330. * @ai: attaching information
  1331. *
  1332. * This function returns zero in case of success, and a negative error code in
  1333. * case of failure.
  1334. */
  1335. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1336. {
  1337. int err, i, reserved_pebs, found_pebs = 0;
  1338. struct rb_node *rb1, *rb2;
  1339. struct ubi_ainf_volume *av;
  1340. struct ubi_ainf_peb *aeb, *tmp;
  1341. struct ubi_wl_entry *e;
  1342. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1343. spin_lock_init(&ubi->wl_lock);
  1344. mutex_init(&ubi->move_mutex);
  1345. init_rwsem(&ubi->work_sem);
  1346. ubi->max_ec = ai->max_ec;
  1347. INIT_LIST_HEAD(&ubi->works);
  1348. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1349. err = -ENOMEM;
  1350. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1351. if (!ubi->lookuptbl)
  1352. return err;
  1353. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1354. INIT_LIST_HEAD(&ubi->pq[i]);
  1355. ubi->pq_head = 0;
  1356. ubi->free_count = 0;
  1357. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1358. cond_resched();
  1359. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1360. if (!e)
  1361. goto out_free;
  1362. e->pnum = aeb->pnum;
  1363. e->ec = aeb->ec;
  1364. ubi->lookuptbl[e->pnum] = e;
  1365. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1366. wl_entry_destroy(ubi, e);
  1367. goto out_free;
  1368. }
  1369. found_pebs++;
  1370. }
  1371. list_for_each_entry(aeb, &ai->free, u.list) {
  1372. cond_resched();
  1373. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1374. if (!e)
  1375. goto out_free;
  1376. e->pnum = aeb->pnum;
  1377. e->ec = aeb->ec;
  1378. ubi_assert(e->ec >= 0);
  1379. wl_tree_add(e, &ubi->free);
  1380. ubi->free_count++;
  1381. ubi->lookuptbl[e->pnum] = e;
  1382. found_pebs++;
  1383. }
  1384. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1385. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1386. cond_resched();
  1387. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1388. if (!e)
  1389. goto out_free;
  1390. e->pnum = aeb->pnum;
  1391. e->ec = aeb->ec;
  1392. ubi->lookuptbl[e->pnum] = e;
  1393. if (!aeb->scrub) {
  1394. dbg_wl("add PEB %d EC %d to the used tree",
  1395. e->pnum, e->ec);
  1396. wl_tree_add(e, &ubi->used);
  1397. } else {
  1398. dbg_wl("add PEB %d EC %d to the scrub tree",
  1399. e->pnum, e->ec);
  1400. wl_tree_add(e, &ubi->scrub);
  1401. }
  1402. found_pebs++;
  1403. }
  1404. }
  1405. list_for_each_entry(aeb, &ai->fastmap, u.list) {
  1406. cond_resched();
  1407. e = ubi_find_fm_block(ubi, aeb->pnum);
  1408. if (e) {
  1409. ubi_assert(!ubi->lookuptbl[e->pnum]);
  1410. ubi->lookuptbl[e->pnum] = e;
  1411. } else {
  1412. /*
  1413. * Usually old Fastmap PEBs are scheduled for erasure
  1414. * and we don't have to care about them but if we face
  1415. * an power cut before scheduling them we need to
  1416. * take care of them here.
  1417. */
  1418. if (ubi->lookuptbl[aeb->pnum])
  1419. continue;
  1420. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1421. if (!e)
  1422. goto out_free;
  1423. e->pnum = aeb->pnum;
  1424. e->ec = aeb->ec;
  1425. ubi_assert(!ubi->lookuptbl[e->pnum]);
  1426. ubi->lookuptbl[e->pnum] = e;
  1427. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1428. wl_entry_destroy(ubi, e);
  1429. goto out_free;
  1430. }
  1431. }
  1432. found_pebs++;
  1433. }
  1434. dbg_wl("found %i PEBs", found_pebs);
  1435. ubi_assert(ubi->good_peb_count == found_pebs);
  1436. reserved_pebs = WL_RESERVED_PEBS;
  1437. ubi_fastmap_init(ubi, &reserved_pebs);
  1438. if (ubi->avail_pebs < reserved_pebs) {
  1439. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1440. ubi->avail_pebs, reserved_pebs);
  1441. if (ubi->corr_peb_count)
  1442. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1443. ubi->corr_peb_count);
  1444. err = -ENOSPC;
  1445. goto out_free;
  1446. }
  1447. ubi->avail_pebs -= reserved_pebs;
  1448. ubi->rsvd_pebs += reserved_pebs;
  1449. /* Schedule wear-leveling if needed */
  1450. err = ensure_wear_leveling(ubi, 0);
  1451. if (err)
  1452. goto out_free;
  1453. return 0;
  1454. out_free:
  1455. shutdown_work(ubi);
  1456. tree_destroy(ubi, &ubi->used);
  1457. tree_destroy(ubi, &ubi->free);
  1458. tree_destroy(ubi, &ubi->scrub);
  1459. kfree(ubi->lookuptbl);
  1460. return err;
  1461. }
  1462. /**
  1463. * protection_queue_destroy - destroy the protection queue.
  1464. * @ubi: UBI device description object
  1465. */
  1466. static void protection_queue_destroy(struct ubi_device *ubi)
  1467. {
  1468. int i;
  1469. struct ubi_wl_entry *e, *tmp;
  1470. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1471. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1472. list_del(&e->u.list);
  1473. wl_entry_destroy(ubi, e);
  1474. }
  1475. }
  1476. }
  1477. /**
  1478. * ubi_wl_close - close the wear-leveling sub-system.
  1479. * @ubi: UBI device description object
  1480. */
  1481. void ubi_wl_close(struct ubi_device *ubi)
  1482. {
  1483. dbg_wl("close the WL sub-system");
  1484. ubi_fastmap_close(ubi);
  1485. shutdown_work(ubi);
  1486. protection_queue_destroy(ubi);
  1487. tree_destroy(ubi, &ubi->used);
  1488. tree_destroy(ubi, &ubi->erroneous);
  1489. tree_destroy(ubi, &ubi->free);
  1490. tree_destroy(ubi, &ubi->scrub);
  1491. kfree(ubi->lookuptbl);
  1492. }
  1493. /**
  1494. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1495. * @ubi: UBI device description object
  1496. * @pnum: the physical eraseblock number to check
  1497. * @ec: the erase counter to check
  1498. *
  1499. * This function returns zero if the erase counter of physical eraseblock @pnum
  1500. * is equivalent to @ec, and a negative error code if not or if an error
  1501. * occurred.
  1502. */
  1503. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1504. {
  1505. int err;
  1506. long long read_ec;
  1507. struct ubi_ec_hdr *ec_hdr;
  1508. if (!ubi_dbg_chk_gen(ubi))
  1509. return 0;
  1510. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1511. if (!ec_hdr)
  1512. return -ENOMEM;
  1513. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1514. if (err && err != UBI_IO_BITFLIPS) {
  1515. /* The header does not have to exist */
  1516. err = 0;
  1517. goto out_free;
  1518. }
  1519. read_ec = be64_to_cpu(ec_hdr->ec);
  1520. if (ec != read_ec && read_ec - ec > 1) {
  1521. ubi_err(ubi, "self-check failed for PEB %d", pnum);
  1522. ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
  1523. dump_stack();
  1524. err = 1;
  1525. } else
  1526. err = 0;
  1527. out_free:
  1528. kfree(ec_hdr);
  1529. return err;
  1530. }
  1531. /**
  1532. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1533. * @ubi: UBI device description object
  1534. * @e: the wear-leveling entry to check
  1535. * @root: the root of the tree
  1536. *
  1537. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1538. * is not.
  1539. */
  1540. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1541. struct ubi_wl_entry *e, struct rb_root *root)
  1542. {
  1543. if (!ubi_dbg_chk_gen(ubi))
  1544. return 0;
  1545. if (in_wl_tree(e, root))
  1546. return 0;
  1547. ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
  1548. e->pnum, e->ec, root);
  1549. dump_stack();
  1550. return -EINVAL;
  1551. }
  1552. /**
  1553. * self_check_in_pq - check if wear-leveling entry is in the protection
  1554. * queue.
  1555. * @ubi: UBI device description object
  1556. * @e: the wear-leveling entry to check
  1557. *
  1558. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1559. */
  1560. static int self_check_in_pq(const struct ubi_device *ubi,
  1561. struct ubi_wl_entry *e)
  1562. {
  1563. struct ubi_wl_entry *p;
  1564. int i;
  1565. if (!ubi_dbg_chk_gen(ubi))
  1566. return 0;
  1567. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1568. list_for_each_entry(p, &ubi->pq[i], u.list)
  1569. if (p == e)
  1570. return 0;
  1571. ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
  1572. e->pnum, e->ec);
  1573. dump_stack();
  1574. return -EINVAL;
  1575. }
  1576. #ifndef CONFIG_MTD_UBI_FASTMAP
  1577. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  1578. {
  1579. struct ubi_wl_entry *e;
  1580. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1581. self_check_in_wl_tree(ubi, e, &ubi->free);
  1582. ubi->free_count--;
  1583. ubi_assert(ubi->free_count >= 0);
  1584. rb_erase(&e->u.rb, &ubi->free);
  1585. return e;
  1586. }
  1587. /**
  1588. * produce_free_peb - produce a free physical eraseblock.
  1589. * @ubi: UBI device description object
  1590. *
  1591. * This function tries to make a free PEB by means of synchronous execution of
  1592. * pending works. This may be needed if, for example the background thread is
  1593. * disabled. Returns zero in case of success and a negative error code in case
  1594. * of failure.
  1595. */
  1596. static int produce_free_peb(struct ubi_device *ubi)
  1597. {
  1598. int err;
  1599. while (!ubi->free.rb_node && ubi->works_count) {
  1600. spin_unlock(&ubi->wl_lock);
  1601. dbg_wl("do one work synchronously");
  1602. err = do_work(ubi);
  1603. spin_lock(&ubi->wl_lock);
  1604. if (err)
  1605. return err;
  1606. }
  1607. return 0;
  1608. }
  1609. /**
  1610. * ubi_wl_get_peb - get a physical eraseblock.
  1611. * @ubi: UBI device description object
  1612. *
  1613. * This function returns a physical eraseblock in case of success and a
  1614. * negative error code in case of failure.
  1615. * Returns with ubi->fm_eba_sem held in read mode!
  1616. */
  1617. int ubi_wl_get_peb(struct ubi_device *ubi)
  1618. {
  1619. int err;
  1620. struct ubi_wl_entry *e;
  1621. retry:
  1622. down_read(&ubi->fm_eba_sem);
  1623. spin_lock(&ubi->wl_lock);
  1624. if (!ubi->free.rb_node) {
  1625. if (ubi->works_count == 0) {
  1626. ubi_err(ubi, "no free eraseblocks");
  1627. ubi_assert(list_empty(&ubi->works));
  1628. spin_unlock(&ubi->wl_lock);
  1629. return -ENOSPC;
  1630. }
  1631. err = produce_free_peb(ubi);
  1632. if (err < 0) {
  1633. spin_unlock(&ubi->wl_lock);
  1634. return err;
  1635. }
  1636. spin_unlock(&ubi->wl_lock);
  1637. up_read(&ubi->fm_eba_sem);
  1638. goto retry;
  1639. }
  1640. e = wl_get_wle(ubi);
  1641. prot_queue_add(ubi, e);
  1642. spin_unlock(&ubi->wl_lock);
  1643. err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
  1644. ubi->peb_size - ubi->vid_hdr_aloffset);
  1645. if (err) {
  1646. ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);
  1647. return err;
  1648. }
  1649. return e->pnum;
  1650. }
  1651. #else
  1652. #include "fastmap-wl.c"
  1653. #endif