wl.c 45 KB

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