wl.c 44 KB

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