wl.c 44 KB

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