wl.c 45 KB

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