wl.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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. * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick
  43. * an "optimal" physical eraseblock. For example, when it is known that the
  44. * physical eraseblock will be "put" soon because it contains short-term data,
  45. * the WL sub-system may pick a free physical eraseblock with low erase
  46. * counter, and so forth.
  47. *
  48. * If the WL sub-system fails to erase a physical eraseblock, it marks it as
  49. * bad.
  50. *
  51. * This sub-system is also responsible for scrubbing. If a bit-flip is detected
  52. * in a physical eraseblock, it has to be moved. Technically this is the same
  53. * as moving it for wear-leveling reasons.
  54. *
  55. * As it was said, for the UBI sub-system all physical eraseblocks are either
  56. * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
  57. * used eraseblocks are kept in @wl->used or @wl->scrub RB-trees, or
  58. * (temporarily) in the @wl->pq queue.
  59. *
  60. * When the WL sub-system returns a physical eraseblock, the physical
  61. * eraseblock is protected from being moved for some "time". For this reason,
  62. * the physical eraseblock is not directly moved from the @wl->free tree to the
  63. * @wl->used tree. There is a protection queue in between where this
  64. * physical eraseblock is temporarily stored (@wl->pq).
  65. *
  66. * All this protection stuff is needed because:
  67. * o we don't want to move physical eraseblocks just after we have given them
  68. * to the user; instead, we first want to let users fill them up with data;
  69. *
  70. * o there is a chance that the user will put the physical eraseblock very
  71. * soon, so it makes sense not to move it for some time, but wait; this is
  72. * especially important in case of "short term" physical eraseblocks.
  73. *
  74. * Physical eraseblocks stay protected only for limited time. But the "time" is
  75. * measured in erase cycles in this case. This is implemented with help of the
  76. * protection queue. Eraseblocks are put to the tail of this queue when they
  77. * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
  78. * head of the queue on each erase operation (for any eraseblock). So the
  79. * length of the queue defines how may (global) erase cycles PEBs are protected.
  80. *
  81. * To put it differently, each physical eraseblock has 2 main states: free and
  82. * used. The former state corresponds to the @wl->free tree. The latter state
  83. * is split up on several sub-states:
  84. * o the WL movement is allowed (@wl->used tree);
  85. * o the WL movement is temporarily prohibited (@wl->pq queue);
  86. * o scrubbing is needed (@wl->scrub tree).
  87. *
  88. * Depending on the sub-state, wear-leveling entries of the used physical
  89. * eraseblocks may be kept in one of those structures.
  90. *
  91. * Note, in this implementation, we keep a small in-RAM object for each physical
  92. * eraseblock. This is surely not a scalable solution. But it appears to be good
  93. * enough for moderately large flashes and it is simple. In future, one may
  94. * re-work this sub-system and make it more scalable.
  95. *
  96. * At the moment this sub-system does not utilize the sequence number, which
  97. * was introduced relatively recently. But it would be wise to do this because
  98. * the sequence number of a logical eraseblock characterizes how old is it. For
  99. * example, when we move a PEB with low erase counter, and we need to pick the
  100. * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
  101. * pick target PEB with an average EC if our PEB is not very "old". This is a
  102. * room for future re-works of the WL sub-system.
  103. */
  104. #include <linux/slab.h>
  105. #include <linux/crc32.h>
  106. #include <linux/freezer.h>
  107. #include <linux/kthread.h>
  108. #include "ubi.h"
  109. /* Number of physical eraseblocks reserved for wear-leveling purposes */
  110. #define WL_RESERVED_PEBS 1
  111. /*
  112. * Maximum difference between two erase counters. If this threshold is
  113. * exceeded, the WL sub-system starts moving data from used physical
  114. * eraseblocks with low erase counter to free physical eraseblocks with high
  115. * erase counter.
  116. */
  117. #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
  118. /*
  119. * When a physical eraseblock is moved, the WL sub-system has to pick the target
  120. * physical eraseblock to move to. The simplest way would be just to pick the
  121. * one with the highest erase counter. But in certain workloads this could lead
  122. * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
  123. * situation when the picked physical eraseblock is constantly erased after the
  124. * data is written to it. So, we have a constant which limits the highest erase
  125. * counter of the free physical eraseblock to pick. Namely, the WL sub-system
  126. * does not pick eraseblocks with erase counter greater then the lowest erase
  127. * counter plus %WL_FREE_MAX_DIFF.
  128. */
  129. #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
  130. /*
  131. * Maximum number of consecutive background thread failures which is enough to
  132. * switch to read-only mode.
  133. */
  134. #define WL_MAX_FAILURES 32
  135. /**
  136. * struct ubi_work - UBI work description data structure.
  137. * @list: a link in the list of pending works
  138. * @func: worker function
  139. * @e: physical eraseblock to erase
  140. * @torture: if the physical eraseblock has to be tortured
  141. *
  142. * The @func pointer points to the worker function. If the @cancel argument is
  143. * not zero, the worker has to free the resources and exit immediately. The
  144. * worker has to return zero in case of success and a negative error code in
  145. * case of failure.
  146. */
  147. struct ubi_work {
  148. struct list_head list;
  149. int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
  150. /* The below fields are only relevant to erasure works */
  151. struct ubi_wl_entry *e;
  152. int torture;
  153. };
  154. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  155. static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
  156. static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
  157. struct rb_root *root);
  158. static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e);
  159. #else
  160. #define paranoid_check_ec(ubi, pnum, ec) 0
  161. #define paranoid_check_in_wl_tree(e, root)
  162. #define paranoid_check_in_pq(ubi, e) 0
  163. #endif
  164. /**
  165. * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
  166. * @e: the wear-leveling entry to add
  167. * @root: the root of the tree
  168. *
  169. * Note, we use (erase counter, physical eraseblock number) pairs as keys in
  170. * the @ubi->used and @ubi->free RB-trees.
  171. */
  172. static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
  173. {
  174. struct rb_node **p, *parent = NULL;
  175. p = &root->rb_node;
  176. while (*p) {
  177. struct ubi_wl_entry *e1;
  178. parent = *p;
  179. e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
  180. if (e->ec < e1->ec)
  181. p = &(*p)->rb_left;
  182. else if (e->ec > e1->ec)
  183. p = &(*p)->rb_right;
  184. else {
  185. ubi_assert(e->pnum != e1->pnum);
  186. if (e->pnum < e1->pnum)
  187. p = &(*p)->rb_left;
  188. else
  189. p = &(*p)->rb_right;
  190. }
  191. }
  192. rb_link_node(&e->u.rb, parent, p);
  193. rb_insert_color(&e->u.rb, root);
  194. }
  195. /**
  196. * do_work - do one pending work.
  197. * @ubi: UBI device description object
  198. *
  199. * This function returns zero in case of success and a negative error code in
  200. * case of failure.
  201. */
  202. static int do_work(struct ubi_device *ubi)
  203. {
  204. int err;
  205. struct ubi_work *wrk;
  206. cond_resched();
  207. /*
  208. * @ubi->work_sem is used to synchronize with the workers. Workers take
  209. * it in read mode, so many of them may be doing works at a time. But
  210. * the queue flush code has to be sure the whole queue of works is
  211. * done, and it takes the mutex in write mode.
  212. */
  213. down_read(&ubi->work_sem);
  214. spin_lock(&ubi->wl_lock);
  215. if (list_empty(&ubi->works)) {
  216. spin_unlock(&ubi->wl_lock);
  217. up_read(&ubi->work_sem);
  218. return 0;
  219. }
  220. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  221. list_del(&wrk->list);
  222. ubi->works_count -= 1;
  223. ubi_assert(ubi->works_count >= 0);
  224. spin_unlock(&ubi->wl_lock);
  225. /*
  226. * Call the worker function. Do not touch the work structure
  227. * after this call as it will have been freed or reused by that
  228. * time by the worker function.
  229. */
  230. err = wrk->func(ubi, wrk, 0);
  231. if (err)
  232. ubi_err("work failed with error code %d", err);
  233. up_read(&ubi->work_sem);
  234. return err;
  235. }
  236. /**
  237. * produce_free_peb - produce a free physical eraseblock.
  238. * @ubi: UBI device description object
  239. *
  240. * This function tries to make a free PEB by means of synchronous execution of
  241. * pending works. This may be needed if, for example the background thread is
  242. * disabled. Returns zero in case of success and a negative error code in case
  243. * of failure.
  244. */
  245. static int produce_free_peb(struct ubi_device *ubi)
  246. {
  247. int err;
  248. spin_lock(&ubi->wl_lock);
  249. while (!ubi->free.rb_node) {
  250. spin_unlock(&ubi->wl_lock);
  251. dbg_wl("do one work synchronously");
  252. err = do_work(ubi);
  253. if (err)
  254. return err;
  255. spin_lock(&ubi->wl_lock);
  256. }
  257. spin_unlock(&ubi->wl_lock);
  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. * @root: the RB-tree where to look for
  315. * @max: highest possible erase counter
  316. *
  317. * This function looks for a wear leveling entry with erase counter closest to
  318. * @max and less then @max.
  319. */
  320. static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
  321. {
  322. struct rb_node *p;
  323. struct ubi_wl_entry *e;
  324. e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  325. max += e->ec;
  326. p = root->rb_node;
  327. while (p) {
  328. struct ubi_wl_entry *e1;
  329. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  330. if (e1->ec >= max)
  331. p = p->rb_left;
  332. else {
  333. p = p->rb_right;
  334. e = e1;
  335. }
  336. }
  337. return e;
  338. }
  339. /**
  340. * ubi_wl_get_peb - get a physical eraseblock.
  341. * @ubi: UBI device description object
  342. * @dtype: type of data which will be stored in this physical eraseblock
  343. *
  344. * This function returns a physical eraseblock in case of success and a
  345. * negative error code in case of failure. Might sleep.
  346. */
  347. int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
  348. {
  349. int err, medium_ec;
  350. struct ubi_wl_entry *e, *first, *last;
  351. ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
  352. dtype == UBI_UNKNOWN);
  353. retry:
  354. spin_lock(&ubi->wl_lock);
  355. if (!ubi->free.rb_node) {
  356. if (ubi->works_count == 0) {
  357. ubi_assert(list_empty(&ubi->works));
  358. ubi_err("no free eraseblocks");
  359. spin_unlock(&ubi->wl_lock);
  360. return -ENOSPC;
  361. }
  362. spin_unlock(&ubi->wl_lock);
  363. err = produce_free_peb(ubi);
  364. if (err < 0)
  365. return err;
  366. goto retry;
  367. }
  368. switch (dtype) {
  369. case UBI_LONGTERM:
  370. /*
  371. * For long term data we pick a physical eraseblock with high
  372. * erase counter. But the highest erase counter we can pick is
  373. * bounded by the the lowest erase counter plus
  374. * %WL_FREE_MAX_DIFF.
  375. */
  376. e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  377. break;
  378. case UBI_UNKNOWN:
  379. /*
  380. * For unknown data we pick a physical eraseblock with medium
  381. * erase counter. But we by no means can pick a physical
  382. * eraseblock with erase counter greater or equivalent than the
  383. * lowest erase counter plus %WL_FREE_MAX_DIFF.
  384. */
  385. first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry,
  386. u.rb);
  387. last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
  388. if (last->ec - first->ec < WL_FREE_MAX_DIFF)
  389. e = rb_entry(ubi->free.rb_node,
  390. struct ubi_wl_entry, u.rb);
  391. else {
  392. medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
  393. e = find_wl_entry(&ubi->free, medium_ec);
  394. }
  395. break;
  396. case UBI_SHORTTERM:
  397. /*
  398. * For short term data we pick a physical eraseblock with the
  399. * lowest erase counter as we expect it will be erased soon.
  400. */
  401. e = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
  402. break;
  403. default:
  404. BUG();
  405. }
  406. paranoid_check_in_wl_tree(e, &ubi->free);
  407. /*
  408. * Move the physical eraseblock to the protection queue where it will
  409. * be protected from being moved for some time.
  410. */
  411. rb_erase(&e->u.rb, &ubi->free);
  412. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  413. prot_queue_add(ubi, e);
  414. spin_unlock(&ubi->wl_lock);
  415. return e->pnum;
  416. }
  417. /**
  418. * prot_queue_del - remove a physical eraseblock from the protection queue.
  419. * @ubi: UBI device description object
  420. * @pnum: the physical eraseblock to remove
  421. *
  422. * This function deletes PEB @pnum from the protection queue and returns zero
  423. * in case of success and %-ENODEV if the PEB was not found.
  424. */
  425. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  426. {
  427. struct ubi_wl_entry *e;
  428. e = ubi->lookuptbl[pnum];
  429. if (!e)
  430. return -ENODEV;
  431. if (paranoid_check_in_pq(ubi, e))
  432. return -ENODEV;
  433. list_del(&e->u.list);
  434. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  435. return 0;
  436. }
  437. /**
  438. * sync_erase - synchronously erase a physical eraseblock.
  439. * @ubi: UBI device description object
  440. * @e: the the physical eraseblock to erase
  441. * @torture: if the physical eraseblock has to be tortured
  442. *
  443. * This function returns zero in case of success and a negative error code in
  444. * case of failure.
  445. */
  446. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  447. int torture)
  448. {
  449. int err;
  450. struct ubi_ec_hdr *ec_hdr;
  451. unsigned long long ec = e->ec;
  452. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  453. err = paranoid_check_ec(ubi, e->pnum, e->ec);
  454. if (err > 0)
  455. return -EINVAL;
  456. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  457. if (!ec_hdr)
  458. return -ENOMEM;
  459. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  460. if (err < 0)
  461. goto out_free;
  462. ec += err;
  463. if (ec > UBI_MAX_ERASECOUNTER) {
  464. /*
  465. * Erase counter overflow. Upgrade UBI and use 64-bit
  466. * erase counters internally.
  467. */
  468. ubi_err("erase counter overflow at PEB %d, EC %llu",
  469. e->pnum, ec);
  470. err = -EINVAL;
  471. goto out_free;
  472. }
  473. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  474. ec_hdr->ec = cpu_to_be64(ec);
  475. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  476. if (err)
  477. goto out_free;
  478. e->ec = ec;
  479. spin_lock(&ubi->wl_lock);
  480. if (e->ec > ubi->max_ec)
  481. ubi->max_ec = e->ec;
  482. spin_unlock(&ubi->wl_lock);
  483. out_free:
  484. kfree(ec_hdr);
  485. return err;
  486. }
  487. /**
  488. * serve_prot_queue - check if it is time to stop protecting PEBs.
  489. * @ubi: UBI device description object
  490. *
  491. * This function is called after each erase operation and removes PEBs from the
  492. * tail of the protection queue. These PEBs have been protected for long enough
  493. * and should be moved to the used tree.
  494. */
  495. static void serve_prot_queue(struct ubi_device *ubi)
  496. {
  497. struct ubi_wl_entry *e, *tmp;
  498. int count;
  499. /*
  500. * There may be several protected physical eraseblock to remove,
  501. * process them all.
  502. */
  503. repeat:
  504. count = 0;
  505. spin_lock(&ubi->wl_lock);
  506. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  507. dbg_wl("PEB %d EC %d protection over, move to used tree",
  508. e->pnum, e->ec);
  509. list_del(&e->u.list);
  510. wl_tree_add(e, &ubi->used);
  511. if (count++ > 32) {
  512. /*
  513. * Let's be nice and avoid holding the spinlock for
  514. * too long.
  515. */
  516. spin_unlock(&ubi->wl_lock);
  517. cond_resched();
  518. goto repeat;
  519. }
  520. }
  521. ubi->pq_head += 1;
  522. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  523. ubi->pq_head = 0;
  524. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  525. spin_unlock(&ubi->wl_lock);
  526. }
  527. /**
  528. * schedule_ubi_work - schedule a work.
  529. * @ubi: UBI device description object
  530. * @wrk: the work to schedule
  531. *
  532. * This function adds a work defined by @wrk to the tail of the pending works
  533. * list.
  534. */
  535. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  536. {
  537. spin_lock(&ubi->wl_lock);
  538. list_add_tail(&wrk->list, &ubi->works);
  539. ubi_assert(ubi->works_count >= 0);
  540. ubi->works_count += 1;
  541. if (ubi->thread_enabled)
  542. wake_up_process(ubi->bgt_thread);
  543. spin_unlock(&ubi->wl_lock);
  544. }
  545. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  546. int cancel);
  547. /**
  548. * schedule_erase - schedule an erase work.
  549. * @ubi: UBI device description object
  550. * @e: the WL entry of the physical eraseblock to erase
  551. * @torture: if the physical eraseblock has to be tortured
  552. *
  553. * This function returns zero in case of success and a %-ENOMEM in case of
  554. * failure.
  555. */
  556. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  557. int torture)
  558. {
  559. struct ubi_work *wl_wrk;
  560. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  561. e->pnum, e->ec, torture);
  562. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  563. if (!wl_wrk)
  564. return -ENOMEM;
  565. wl_wrk->func = &erase_worker;
  566. wl_wrk->e = e;
  567. wl_wrk->torture = torture;
  568. schedule_ubi_work(ubi, wl_wrk);
  569. return 0;
  570. }
  571. /**
  572. * wear_leveling_worker - wear-leveling worker function.
  573. * @ubi: UBI device description object
  574. * @wrk: the work object
  575. * @cancel: non-zero if the worker has to free memory and exit
  576. *
  577. * This function copies a more worn out physical eraseblock to a less worn out
  578. * one. Returns zero in case of success and a negative error code in case of
  579. * failure.
  580. */
  581. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  582. int cancel)
  583. {
  584. int err, scrubbing = 0, torture = 0;
  585. struct ubi_wl_entry *e1, *e2;
  586. struct ubi_vid_hdr *vid_hdr;
  587. kfree(wrk);
  588. if (cancel)
  589. return 0;
  590. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  591. if (!vid_hdr)
  592. return -ENOMEM;
  593. mutex_lock(&ubi->move_mutex);
  594. spin_lock(&ubi->wl_lock);
  595. ubi_assert(!ubi->move_from && !ubi->move_to);
  596. ubi_assert(!ubi->move_to_put);
  597. if (!ubi->free.rb_node ||
  598. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  599. /*
  600. * No free physical eraseblocks? Well, they must be waiting in
  601. * the queue to be erased. Cancel movement - it will be
  602. * triggered again when a free physical eraseblock appears.
  603. *
  604. * No used physical eraseblocks? They must be temporarily
  605. * protected from being moved. They will be moved to the
  606. * @ubi->used tree later and the wear-leveling will be
  607. * triggered again.
  608. */
  609. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  610. !ubi->free.rb_node, !ubi->used.rb_node);
  611. goto out_cancel;
  612. }
  613. if (!ubi->scrub.rb_node) {
  614. /*
  615. * Now pick the least worn-out used physical eraseblock and a
  616. * highly worn-out free physical eraseblock. If the erase
  617. * counters differ much enough, start wear-leveling.
  618. */
  619. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  620. e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  621. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  622. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  623. e1->ec, e2->ec);
  624. goto out_cancel;
  625. }
  626. paranoid_check_in_wl_tree(e1, &ubi->used);
  627. rb_erase(&e1->u.rb, &ubi->used);
  628. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  629. e1->pnum, e1->ec, e2->pnum, e2->ec);
  630. } else {
  631. /* Perform scrubbing */
  632. scrubbing = 1;
  633. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  634. e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  635. paranoid_check_in_wl_tree(e1, &ubi->scrub);
  636. rb_erase(&e1->u.rb, &ubi->scrub);
  637. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  638. }
  639. paranoid_check_in_wl_tree(e2, &ubi->free);
  640. rb_erase(&e2->u.rb, &ubi->free);
  641. ubi->move_from = e1;
  642. ubi->move_to = e2;
  643. spin_unlock(&ubi->wl_lock);
  644. /*
  645. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  646. * We so far do not know which logical eraseblock our physical
  647. * eraseblock (@e1) belongs to. We have to read the volume identifier
  648. * header first.
  649. *
  650. * Note, we are protected from this PEB being unmapped and erased. The
  651. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  652. * which is being moved was unmapped.
  653. */
  654. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  655. if (err && err != UBI_IO_BITFLIPS) {
  656. if (err == UBI_IO_PEB_FREE) {
  657. /*
  658. * We are trying to move PEB without a VID header. UBI
  659. * always write VID headers shortly after the PEB was
  660. * given, so we have a situation when it did not have
  661. * chance to write it down because it was preempted.
  662. * Just re-schedule the work, so that next time it will
  663. * likely have the VID header in place.
  664. */
  665. dbg_wl("PEB %d has no VID header", e1->pnum);
  666. goto out_not_moved;
  667. }
  668. ubi_err("error %d while reading VID header from PEB %d",
  669. err, e1->pnum);
  670. if (err > 0)
  671. err = -EIO;
  672. goto out_error;
  673. }
  674. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  675. if (err) {
  676. if (err == -EAGAIN)
  677. goto out_not_moved;
  678. if (err < 0)
  679. goto out_error;
  680. if (err == 2) {
  681. /* Target PEB write error, torture it */
  682. torture = 1;
  683. goto out_not_moved;
  684. }
  685. /*
  686. * The LEB has not been moved because the volume is being
  687. * deleted or the PEB has been put meanwhile. We should prevent
  688. * this PEB from being selected for wear-leveling movement
  689. * again, so put it to the protection queue.
  690. */
  691. dbg_wl("canceled moving PEB %d", e1->pnum);
  692. ubi_assert(err == 1);
  693. ubi_free_vid_hdr(ubi, vid_hdr);
  694. vid_hdr = NULL;
  695. spin_lock(&ubi->wl_lock);
  696. prot_queue_add(ubi, e1);
  697. ubi_assert(!ubi->move_to_put);
  698. ubi->move_from = ubi->move_to = NULL;
  699. ubi->wl_scheduled = 0;
  700. spin_unlock(&ubi->wl_lock);
  701. e1 = NULL;
  702. err = schedule_erase(ubi, e2, 0);
  703. if (err)
  704. goto out_error;
  705. mutex_unlock(&ubi->move_mutex);
  706. return 0;
  707. }
  708. /* The PEB has been successfully moved */
  709. ubi_free_vid_hdr(ubi, vid_hdr);
  710. vid_hdr = NULL;
  711. if (scrubbing)
  712. ubi_msg("scrubbed PEB %d, data moved to PEB %d",
  713. e1->pnum, e2->pnum);
  714. spin_lock(&ubi->wl_lock);
  715. if (!ubi->move_to_put) {
  716. wl_tree_add(e2, &ubi->used);
  717. e2 = NULL;
  718. }
  719. ubi->move_from = ubi->move_to = NULL;
  720. ubi->move_to_put = ubi->wl_scheduled = 0;
  721. spin_unlock(&ubi->wl_lock);
  722. err = schedule_erase(ubi, e1, 0);
  723. if (err) {
  724. e1 = NULL;
  725. goto out_error;
  726. }
  727. if (e2) {
  728. /*
  729. * Well, the target PEB was put meanwhile, schedule it for
  730. * erasure.
  731. */
  732. dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
  733. err = schedule_erase(ubi, e2, 0);
  734. if (err)
  735. goto out_error;
  736. }
  737. dbg_wl("done");
  738. mutex_unlock(&ubi->move_mutex);
  739. return 0;
  740. /*
  741. * For some reasons the LEB was not moved, might be an error, might be
  742. * something else. @e1 was not changed, so return it back. @e2 might
  743. * have been changed, schedule it for erasure.
  744. */
  745. out_not_moved:
  746. dbg_wl("canceled moving PEB %d", e1->pnum);
  747. ubi_free_vid_hdr(ubi, vid_hdr);
  748. vid_hdr = NULL;
  749. spin_lock(&ubi->wl_lock);
  750. if (scrubbing)
  751. wl_tree_add(e1, &ubi->scrub);
  752. else
  753. wl_tree_add(e1, &ubi->used);
  754. ubi_assert(!ubi->move_to_put);
  755. ubi->move_from = ubi->move_to = NULL;
  756. ubi->wl_scheduled = 0;
  757. spin_unlock(&ubi->wl_lock);
  758. e1 = NULL;
  759. err = schedule_erase(ubi, e2, torture);
  760. if (err)
  761. goto out_error;
  762. mutex_unlock(&ubi->move_mutex);
  763. return 0;
  764. out_error:
  765. ubi_err("error %d while moving PEB %d to PEB %d",
  766. err, e1->pnum, e2->pnum);
  767. ubi_free_vid_hdr(ubi, vid_hdr);
  768. spin_lock(&ubi->wl_lock);
  769. ubi->move_from = ubi->move_to = NULL;
  770. ubi->move_to_put = ubi->wl_scheduled = 0;
  771. spin_unlock(&ubi->wl_lock);
  772. if (e1)
  773. kmem_cache_free(ubi_wl_entry_slab, e1);
  774. if (e2)
  775. kmem_cache_free(ubi_wl_entry_slab, e2);
  776. ubi_ro_mode(ubi);
  777. mutex_unlock(&ubi->move_mutex);
  778. return err;
  779. out_cancel:
  780. ubi->wl_scheduled = 0;
  781. spin_unlock(&ubi->wl_lock);
  782. mutex_unlock(&ubi->move_mutex);
  783. ubi_free_vid_hdr(ubi, vid_hdr);
  784. return 0;
  785. }
  786. /**
  787. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  788. * @ubi: UBI device description object
  789. *
  790. * This function checks if it is time to start wear-leveling and schedules it
  791. * if yes. This function returns zero in case of success and a negative error
  792. * code in case of failure.
  793. */
  794. static int ensure_wear_leveling(struct ubi_device *ubi)
  795. {
  796. int err = 0;
  797. struct ubi_wl_entry *e1;
  798. struct ubi_wl_entry *e2;
  799. struct ubi_work *wrk;
  800. spin_lock(&ubi->wl_lock);
  801. if (ubi->wl_scheduled)
  802. /* Wear-leveling is already in the work queue */
  803. goto out_unlock;
  804. /*
  805. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  806. * the WL worker has to be scheduled anyway.
  807. */
  808. if (!ubi->scrub.rb_node) {
  809. if (!ubi->used.rb_node || !ubi->free.rb_node)
  810. /* No physical eraseblocks - no deal */
  811. goto out_unlock;
  812. /*
  813. * We schedule wear-leveling only if the difference between the
  814. * lowest erase counter of used physical eraseblocks and a high
  815. * erase counter of free physical eraseblocks is greater then
  816. * %UBI_WL_THRESHOLD.
  817. */
  818. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  819. e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
  820. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  821. goto out_unlock;
  822. dbg_wl("schedule wear-leveling");
  823. } else
  824. dbg_wl("schedule scrubbing");
  825. ubi->wl_scheduled = 1;
  826. spin_unlock(&ubi->wl_lock);
  827. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  828. if (!wrk) {
  829. err = -ENOMEM;
  830. goto out_cancel;
  831. }
  832. wrk->func = &wear_leveling_worker;
  833. schedule_ubi_work(ubi, wrk);
  834. return err;
  835. out_cancel:
  836. spin_lock(&ubi->wl_lock);
  837. ubi->wl_scheduled = 0;
  838. out_unlock:
  839. spin_unlock(&ubi->wl_lock);
  840. return err;
  841. }
  842. /**
  843. * erase_worker - physical eraseblock erase worker function.
  844. * @ubi: UBI device description object
  845. * @wl_wrk: the work object
  846. * @cancel: non-zero if the worker has to free memory and exit
  847. *
  848. * This function erases a physical eraseblock and perform torture testing if
  849. * needed. It also takes care about marking the physical eraseblock bad if
  850. * needed. Returns zero in case of success and a negative error code in case of
  851. * failure.
  852. */
  853. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  854. int cancel)
  855. {
  856. struct ubi_wl_entry *e = wl_wrk->e;
  857. int pnum = e->pnum, err, need;
  858. if (cancel) {
  859. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  860. kfree(wl_wrk);
  861. kmem_cache_free(ubi_wl_entry_slab, e);
  862. return 0;
  863. }
  864. dbg_wl("erase PEB %d EC %d", pnum, e->ec);
  865. err = sync_erase(ubi, e, wl_wrk->torture);
  866. if (!err) {
  867. /* Fine, we've erased it successfully */
  868. kfree(wl_wrk);
  869. spin_lock(&ubi->wl_lock);
  870. wl_tree_add(e, &ubi->free);
  871. spin_unlock(&ubi->wl_lock);
  872. /*
  873. * One more erase operation has happened, take care about
  874. * protected physical eraseblocks.
  875. */
  876. serve_prot_queue(ubi);
  877. /* And take care about wear-leveling */
  878. err = ensure_wear_leveling(ubi);
  879. return err;
  880. }
  881. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  882. kfree(wl_wrk);
  883. kmem_cache_free(ubi_wl_entry_slab, e);
  884. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  885. err == -EBUSY) {
  886. int err1;
  887. /* Re-schedule the LEB for erasure */
  888. err1 = schedule_erase(ubi, e, 0);
  889. if (err1) {
  890. err = err1;
  891. goto out_ro;
  892. }
  893. return err;
  894. } else if (err != -EIO) {
  895. /*
  896. * If this is not %-EIO, we have no idea what to do. Scheduling
  897. * this physical eraseblock for erasure again would cause
  898. * errors again and again. Well, lets switch to RO mode.
  899. */
  900. goto out_ro;
  901. }
  902. /* It is %-EIO, the PEB went bad */
  903. if (!ubi->bad_allowed) {
  904. ubi_err("bad physical eraseblock %d detected", pnum);
  905. goto out_ro;
  906. }
  907. spin_lock(&ubi->volumes_lock);
  908. need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
  909. if (need > 0) {
  910. need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
  911. ubi->avail_pebs -= need;
  912. ubi->rsvd_pebs += need;
  913. ubi->beb_rsvd_pebs += need;
  914. if (need > 0)
  915. ubi_msg("reserve more %d PEBs", need);
  916. }
  917. if (ubi->beb_rsvd_pebs == 0) {
  918. spin_unlock(&ubi->volumes_lock);
  919. ubi_err("no reserved physical eraseblocks");
  920. goto out_ro;
  921. }
  922. spin_unlock(&ubi->volumes_lock);
  923. ubi_msg("mark PEB %d as bad", pnum);
  924. err = ubi_io_mark_bad(ubi, pnum);
  925. if (err)
  926. goto out_ro;
  927. spin_lock(&ubi->volumes_lock);
  928. ubi->beb_rsvd_pebs -= 1;
  929. ubi->bad_peb_count += 1;
  930. ubi->good_peb_count -= 1;
  931. ubi_calculate_reserved(ubi);
  932. if (ubi->beb_rsvd_pebs == 0)
  933. ubi_warn("last PEB from the reserved pool was used");
  934. spin_unlock(&ubi->volumes_lock);
  935. return err;
  936. out_ro:
  937. ubi_ro_mode(ubi);
  938. return err;
  939. }
  940. /**
  941. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  942. * @ubi: UBI device description object
  943. * @pnum: physical eraseblock to return
  944. * @torture: if this physical eraseblock has to be tortured
  945. *
  946. * This function is called to return physical eraseblock @pnum to the pool of
  947. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  948. * occurred to this @pnum and it has to be tested. This function returns zero
  949. * in case of success, and a negative error code in case of failure.
  950. */
  951. int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
  952. {
  953. int err;
  954. struct ubi_wl_entry *e;
  955. dbg_wl("PEB %d", pnum);
  956. ubi_assert(pnum >= 0);
  957. ubi_assert(pnum < ubi->peb_count);
  958. retry:
  959. spin_lock(&ubi->wl_lock);
  960. e = ubi->lookuptbl[pnum];
  961. if (e == ubi->move_from) {
  962. /*
  963. * User is putting the physical eraseblock which was selected to
  964. * be moved. It will be scheduled for erasure in the
  965. * wear-leveling worker.
  966. */
  967. dbg_wl("PEB %d is being moved, wait", pnum);
  968. spin_unlock(&ubi->wl_lock);
  969. /* Wait for the WL worker by taking the @ubi->move_mutex */
  970. mutex_lock(&ubi->move_mutex);
  971. mutex_unlock(&ubi->move_mutex);
  972. goto retry;
  973. } else if (e == ubi->move_to) {
  974. /*
  975. * User is putting the physical eraseblock which was selected
  976. * as the target the data is moved to. It may happen if the EBA
  977. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  978. * but the WL sub-system has not put the PEB to the "used" tree
  979. * yet, but it is about to do this. So we just set a flag which
  980. * will tell the WL worker that the PEB is not needed anymore
  981. * and should be scheduled for erasure.
  982. */
  983. dbg_wl("PEB %d is the target of data moving", pnum);
  984. ubi_assert(!ubi->move_to_put);
  985. ubi->move_to_put = 1;
  986. spin_unlock(&ubi->wl_lock);
  987. return 0;
  988. } else {
  989. if (in_wl_tree(e, &ubi->used)) {
  990. paranoid_check_in_wl_tree(e, &ubi->used);
  991. rb_erase(&e->u.rb, &ubi->used);
  992. } else if (in_wl_tree(e, &ubi->scrub)) {
  993. paranoid_check_in_wl_tree(e, &ubi->scrub);
  994. rb_erase(&e->u.rb, &ubi->scrub);
  995. } else {
  996. err = prot_queue_del(ubi, e->pnum);
  997. if (err) {
  998. ubi_err("PEB %d not found", pnum);
  999. ubi_ro_mode(ubi);
  1000. spin_unlock(&ubi->wl_lock);
  1001. return err;
  1002. }
  1003. }
  1004. }
  1005. spin_unlock(&ubi->wl_lock);
  1006. err = schedule_erase(ubi, e, torture);
  1007. if (err) {
  1008. spin_lock(&ubi->wl_lock);
  1009. wl_tree_add(e, &ubi->used);
  1010. spin_unlock(&ubi->wl_lock);
  1011. }
  1012. return err;
  1013. }
  1014. /**
  1015. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1016. * @ubi: UBI device description object
  1017. * @pnum: the physical eraseblock to schedule
  1018. *
  1019. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1020. * needs scrubbing. This function schedules a physical eraseblock for
  1021. * scrubbing which is done in background. This function returns zero in case of
  1022. * success and a negative error code in case of failure.
  1023. */
  1024. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1025. {
  1026. struct ubi_wl_entry *e;
  1027. dbg_msg("schedule PEB %d for scrubbing", pnum);
  1028. retry:
  1029. spin_lock(&ubi->wl_lock);
  1030. e = ubi->lookuptbl[pnum];
  1031. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
  1032. spin_unlock(&ubi->wl_lock);
  1033. return 0;
  1034. }
  1035. if (e == ubi->move_to) {
  1036. /*
  1037. * This physical eraseblock was used to move data to. The data
  1038. * was moved but the PEB was not yet inserted to the proper
  1039. * tree. We should just wait a little and let the WL worker
  1040. * proceed.
  1041. */
  1042. spin_unlock(&ubi->wl_lock);
  1043. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1044. yield();
  1045. goto retry;
  1046. }
  1047. if (in_wl_tree(e, &ubi->used)) {
  1048. paranoid_check_in_wl_tree(e, &ubi->used);
  1049. rb_erase(&e->u.rb, &ubi->used);
  1050. } else {
  1051. int err;
  1052. err = prot_queue_del(ubi, e->pnum);
  1053. if (err) {
  1054. ubi_err("PEB %d not found", pnum);
  1055. ubi_ro_mode(ubi);
  1056. spin_unlock(&ubi->wl_lock);
  1057. return err;
  1058. }
  1059. }
  1060. wl_tree_add(e, &ubi->scrub);
  1061. spin_unlock(&ubi->wl_lock);
  1062. /*
  1063. * Technically scrubbing is the same as wear-leveling, so it is done
  1064. * by the WL worker.
  1065. */
  1066. return ensure_wear_leveling(ubi);
  1067. }
  1068. /**
  1069. * ubi_wl_flush - flush all pending works.
  1070. * @ubi: UBI device description object
  1071. *
  1072. * This function returns zero in case of success and a negative error code in
  1073. * case of failure.
  1074. */
  1075. int ubi_wl_flush(struct ubi_device *ubi)
  1076. {
  1077. int err;
  1078. /*
  1079. * Erase while the pending works queue is not empty, but not more than
  1080. * the number of currently pending works.
  1081. */
  1082. dbg_wl("flush (%d pending works)", ubi->works_count);
  1083. while (ubi->works_count) {
  1084. err = do_work(ubi);
  1085. if (err)
  1086. return err;
  1087. }
  1088. /*
  1089. * Make sure all the works which have been done in parallel are
  1090. * finished.
  1091. */
  1092. down_write(&ubi->work_sem);
  1093. up_write(&ubi->work_sem);
  1094. /*
  1095. * And in case last was the WL worker and it canceled the LEB
  1096. * movement, flush again.
  1097. */
  1098. while (ubi->works_count) {
  1099. dbg_wl("flush more (%d pending works)", ubi->works_count);
  1100. err = do_work(ubi);
  1101. if (err)
  1102. return err;
  1103. }
  1104. return 0;
  1105. }
  1106. /**
  1107. * tree_destroy - destroy an RB-tree.
  1108. * @root: the root of the tree to destroy
  1109. */
  1110. static void tree_destroy(struct rb_root *root)
  1111. {
  1112. struct rb_node *rb;
  1113. struct ubi_wl_entry *e;
  1114. rb = root->rb_node;
  1115. while (rb) {
  1116. if (rb->rb_left)
  1117. rb = rb->rb_left;
  1118. else if (rb->rb_right)
  1119. rb = rb->rb_right;
  1120. else {
  1121. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1122. rb = rb_parent(rb);
  1123. if (rb) {
  1124. if (rb->rb_left == &e->u.rb)
  1125. rb->rb_left = NULL;
  1126. else
  1127. rb->rb_right = NULL;
  1128. }
  1129. kmem_cache_free(ubi_wl_entry_slab, e);
  1130. }
  1131. }
  1132. }
  1133. /**
  1134. * ubi_thread - UBI background thread.
  1135. * @u: the UBI device description object pointer
  1136. */
  1137. int ubi_thread(void *u)
  1138. {
  1139. int failures = 0;
  1140. struct ubi_device *ubi = u;
  1141. ubi_msg("background thread \"%s\" started, PID %d",
  1142. ubi->bgt_name, task_pid_nr(current));
  1143. set_freezable();
  1144. for (;;) {
  1145. int err;
  1146. if (kthread_should_stop())
  1147. break;
  1148. if (try_to_freeze())
  1149. continue;
  1150. spin_lock(&ubi->wl_lock);
  1151. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1152. !ubi->thread_enabled) {
  1153. set_current_state(TASK_INTERRUPTIBLE);
  1154. spin_unlock(&ubi->wl_lock);
  1155. schedule();
  1156. continue;
  1157. }
  1158. spin_unlock(&ubi->wl_lock);
  1159. err = do_work(ubi);
  1160. if (err) {
  1161. ubi_err("%s: work failed with error code %d",
  1162. ubi->bgt_name, err);
  1163. if (failures++ > WL_MAX_FAILURES) {
  1164. /*
  1165. * Too many failures, disable the thread and
  1166. * switch to read-only mode.
  1167. */
  1168. ubi_msg("%s: %d consecutive failures",
  1169. ubi->bgt_name, WL_MAX_FAILURES);
  1170. ubi_ro_mode(ubi);
  1171. ubi->thread_enabled = 0;
  1172. continue;
  1173. }
  1174. } else
  1175. failures = 0;
  1176. cond_resched();
  1177. }
  1178. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1179. return 0;
  1180. }
  1181. /**
  1182. * cancel_pending - cancel all pending works.
  1183. * @ubi: UBI device description object
  1184. */
  1185. static void cancel_pending(struct ubi_device *ubi)
  1186. {
  1187. while (!list_empty(&ubi->works)) {
  1188. struct ubi_work *wrk;
  1189. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1190. list_del(&wrk->list);
  1191. wrk->func(ubi, wrk, 1);
  1192. ubi->works_count -= 1;
  1193. ubi_assert(ubi->works_count >= 0);
  1194. }
  1195. }
  1196. /**
  1197. * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
  1198. * @ubi: UBI device description object
  1199. * @si: scanning information
  1200. *
  1201. * This function returns zero in case of success, and a negative error code in
  1202. * case of failure.
  1203. */
  1204. int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
  1205. {
  1206. int err, i;
  1207. struct rb_node *rb1, *rb2;
  1208. struct ubi_scan_volume *sv;
  1209. struct ubi_scan_leb *seb, *tmp;
  1210. struct ubi_wl_entry *e;
  1211. ubi->used = ubi->free = ubi->scrub = RB_ROOT;
  1212. spin_lock_init(&ubi->wl_lock);
  1213. mutex_init(&ubi->move_mutex);
  1214. init_rwsem(&ubi->work_sem);
  1215. ubi->max_ec = si->max_ec;
  1216. INIT_LIST_HEAD(&ubi->works);
  1217. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1218. err = -ENOMEM;
  1219. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1220. if (!ubi->lookuptbl)
  1221. return err;
  1222. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1223. INIT_LIST_HEAD(&ubi->pq[i]);
  1224. ubi->pq_head = 0;
  1225. list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
  1226. cond_resched();
  1227. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1228. if (!e)
  1229. goto out_free;
  1230. e->pnum = seb->pnum;
  1231. e->ec = seb->ec;
  1232. ubi->lookuptbl[e->pnum] = e;
  1233. if (schedule_erase(ubi, e, 0)) {
  1234. kmem_cache_free(ubi_wl_entry_slab, e);
  1235. goto out_free;
  1236. }
  1237. }
  1238. list_for_each_entry(seb, &si->free, u.list) {
  1239. cond_resched();
  1240. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1241. if (!e)
  1242. goto out_free;
  1243. e->pnum = seb->pnum;
  1244. e->ec = seb->ec;
  1245. ubi_assert(e->ec >= 0);
  1246. wl_tree_add(e, &ubi->free);
  1247. ubi->lookuptbl[e->pnum] = e;
  1248. }
  1249. list_for_each_entry(seb, &si->corr, u.list) {
  1250. cond_resched();
  1251. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1252. if (!e)
  1253. goto out_free;
  1254. e->pnum = seb->pnum;
  1255. e->ec = seb->ec;
  1256. ubi->lookuptbl[e->pnum] = e;
  1257. if (schedule_erase(ubi, e, 0)) {
  1258. kmem_cache_free(ubi_wl_entry_slab, e);
  1259. goto out_free;
  1260. }
  1261. }
  1262. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1263. ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
  1264. cond_resched();
  1265. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1266. if (!e)
  1267. goto out_free;
  1268. e->pnum = seb->pnum;
  1269. e->ec = seb->ec;
  1270. ubi->lookuptbl[e->pnum] = e;
  1271. if (!seb->scrub) {
  1272. dbg_wl("add PEB %d EC %d to the used tree",
  1273. e->pnum, e->ec);
  1274. wl_tree_add(e, &ubi->used);
  1275. } else {
  1276. dbg_wl("add PEB %d EC %d to the scrub tree",
  1277. e->pnum, e->ec);
  1278. wl_tree_add(e, &ubi->scrub);
  1279. }
  1280. }
  1281. }
  1282. if (ubi->avail_pebs < WL_RESERVED_PEBS) {
  1283. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1284. ubi->avail_pebs, WL_RESERVED_PEBS);
  1285. goto out_free;
  1286. }
  1287. ubi->avail_pebs -= WL_RESERVED_PEBS;
  1288. ubi->rsvd_pebs += WL_RESERVED_PEBS;
  1289. /* Schedule wear-leveling if needed */
  1290. err = ensure_wear_leveling(ubi);
  1291. if (err)
  1292. goto out_free;
  1293. return 0;
  1294. out_free:
  1295. cancel_pending(ubi);
  1296. tree_destroy(&ubi->used);
  1297. tree_destroy(&ubi->free);
  1298. tree_destroy(&ubi->scrub);
  1299. kfree(ubi->lookuptbl);
  1300. return err;
  1301. }
  1302. /**
  1303. * protection_queue_destroy - destroy the protection queue.
  1304. * @ubi: UBI device description object
  1305. */
  1306. static void protection_queue_destroy(struct ubi_device *ubi)
  1307. {
  1308. int i;
  1309. struct ubi_wl_entry *e, *tmp;
  1310. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1311. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1312. list_del(&e->u.list);
  1313. kmem_cache_free(ubi_wl_entry_slab, e);
  1314. }
  1315. }
  1316. }
  1317. /**
  1318. * ubi_wl_close - close the wear-leveling sub-system.
  1319. * @ubi: UBI device description object
  1320. */
  1321. void ubi_wl_close(struct ubi_device *ubi)
  1322. {
  1323. dbg_wl("close the WL sub-system");
  1324. cancel_pending(ubi);
  1325. protection_queue_destroy(ubi);
  1326. tree_destroy(&ubi->used);
  1327. tree_destroy(&ubi->free);
  1328. tree_destroy(&ubi->scrub);
  1329. kfree(ubi->lookuptbl);
  1330. }
  1331. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  1332. /**
  1333. * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
  1334. * @ubi: UBI device description object
  1335. * @pnum: the physical eraseblock number to check
  1336. * @ec: the erase counter to check
  1337. *
  1338. * This function returns zero if the erase counter of physical eraseblock @pnum
  1339. * is equivalent to @ec, %1 if not, and a negative error code if an error
  1340. * occurred.
  1341. */
  1342. static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1343. {
  1344. int err;
  1345. long long read_ec;
  1346. struct ubi_ec_hdr *ec_hdr;
  1347. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1348. if (!ec_hdr)
  1349. return -ENOMEM;
  1350. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1351. if (err && err != UBI_IO_BITFLIPS) {
  1352. /* The header does not have to exist */
  1353. err = 0;
  1354. goto out_free;
  1355. }
  1356. read_ec = be64_to_cpu(ec_hdr->ec);
  1357. if (ec != read_ec) {
  1358. ubi_err("paranoid check failed for PEB %d", pnum);
  1359. ubi_err("read EC is %lld, should be %d", read_ec, ec);
  1360. ubi_dbg_dump_stack();
  1361. err = 1;
  1362. } else
  1363. err = 0;
  1364. out_free:
  1365. kfree(ec_hdr);
  1366. return err;
  1367. }
  1368. /**
  1369. * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1370. * @e: the wear-leveling entry to check
  1371. * @root: the root of the tree
  1372. *
  1373. * This function returns zero if @e is in the @root RB-tree and %1 if it is
  1374. * not.
  1375. */
  1376. static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
  1377. struct rb_root *root)
  1378. {
  1379. if (in_wl_tree(e, root))
  1380. return 0;
  1381. ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
  1382. e->pnum, e->ec, root);
  1383. ubi_dbg_dump_stack();
  1384. return 1;
  1385. }
  1386. /**
  1387. * paranoid_check_in_pq - check if wear-leveling entry is in the protection
  1388. * queue.
  1389. * @ubi: UBI device description object
  1390. * @e: the wear-leveling entry to check
  1391. *
  1392. * This function returns zero if @e is in @ubi->pq and %1 if it is not.
  1393. */
  1394. static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e)
  1395. {
  1396. struct ubi_wl_entry *p;
  1397. int i;
  1398. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1399. list_for_each_entry(p, &ubi->pq[i], u.list)
  1400. if (p == e)
  1401. return 0;
  1402. ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue",
  1403. e->pnum, e->ec);
  1404. ubi_dbg_dump_stack();
  1405. return 1;
  1406. }
  1407. #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */