wl.c 44 KB

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