wl.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  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. * If the WL sub-system fails to erase a physical eraseblock, it marks it as
  43. * bad.
  44. *
  45. * This sub-system is also responsible for scrubbing. If a bit-flip is detected
  46. * in a physical eraseblock, it has to be moved. Technically this is the same
  47. * as moving it for wear-leveling reasons.
  48. *
  49. * As it was said, for the UBI sub-system all physical eraseblocks are either
  50. * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
  51. * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
  52. * RB-trees, as well as (temporarily) in the @wl->pq queue.
  53. *
  54. * When the WL sub-system returns a physical eraseblock, the physical
  55. * eraseblock is protected from being moved for some "time". For this reason,
  56. * the physical eraseblock is not directly moved from the @wl->free tree to the
  57. * @wl->used tree. There is a protection queue in between where this
  58. * physical eraseblock is temporarily stored (@wl->pq).
  59. *
  60. * All this protection stuff is needed because:
  61. * o we don't want to move physical eraseblocks just after we have given them
  62. * to the user; instead, we first want to let users fill them up with data;
  63. *
  64. * o there is a chance that the user will put the physical eraseblock very
  65. * soon, so it makes sense not to move it for some time, but wait.
  66. *
  67. * Physical eraseblocks stay protected only for limited time. But the "time" is
  68. * measured in erase cycles in this case. This is implemented with help of the
  69. * protection queue. Eraseblocks are put to the tail of this queue when they
  70. * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
  71. * head of the queue on each erase operation (for any eraseblock). So the
  72. * length of the queue defines how may (global) erase cycles PEBs are protected.
  73. *
  74. * To put it differently, each physical eraseblock has 2 main states: free and
  75. * used. The former state corresponds to the @wl->free tree. The latter state
  76. * is split up on several sub-states:
  77. * o the WL movement is allowed (@wl->used tree);
  78. * o the WL movement is disallowed (@wl->erroneous) because the PEB is
  79. * erroneous - e.g., there was a read error;
  80. * o the WL movement is temporarily prohibited (@wl->pq queue);
  81. * o scrubbing is needed (@wl->scrub tree).
  82. *
  83. * Depending on the sub-state, wear-leveling entries of the used physical
  84. * eraseblocks may be kept in one of those structures.
  85. *
  86. * Note, in this implementation, we keep a small in-RAM object for each physical
  87. * eraseblock. This is surely not a scalable solution. But it appears to be good
  88. * enough for moderately large flashes and it is simple. In future, one may
  89. * re-work this sub-system and make it more scalable.
  90. *
  91. * At the moment this sub-system does not utilize the sequence number, which
  92. * was introduced relatively recently. But it would be wise to do this because
  93. * the sequence number of a logical eraseblock characterizes how old is it. For
  94. * example, when we move a PEB with low erase counter, and we need to pick the
  95. * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
  96. * pick target PEB with an average EC if our PEB is not very "old". This is a
  97. * room for future re-works of the WL sub-system.
  98. */
  99. #include <linux/slab.h>
  100. #include <linux/crc32.h>
  101. #include <linux/freezer.h>
  102. #include <linux/kthread.h>
  103. #include "ubi.h"
  104. /* Number of physical eraseblocks reserved for wear-leveling purposes */
  105. #define WL_RESERVED_PEBS 1
  106. /*
  107. * Maximum difference between two erase counters. If this threshold is
  108. * exceeded, the WL sub-system starts moving data from used physical
  109. * eraseblocks with low erase counter to free physical eraseblocks with high
  110. * erase counter.
  111. */
  112. #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
  113. /*
  114. * When a physical eraseblock is moved, the WL sub-system has to pick the target
  115. * physical eraseblock to move to. The simplest way would be just to pick the
  116. * one with the highest erase counter. But in certain workloads this could lead
  117. * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
  118. * situation when the picked physical eraseblock is constantly erased after the
  119. * data is written to it. So, we have a constant which limits the highest erase
  120. * counter of the free physical eraseblock to pick. Namely, the WL sub-system
  121. * does not pick eraseblocks with erase counter greater than the lowest erase
  122. * counter plus %WL_FREE_MAX_DIFF.
  123. */
  124. #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
  125. /*
  126. * Maximum number of consecutive background thread failures which is enough to
  127. * switch to read-only mode.
  128. */
  129. #define WL_MAX_FAILURES 32
  130. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
  131. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  132. struct ubi_wl_entry *e, struct rb_root *root);
  133. static int self_check_in_pq(const struct ubi_device *ubi,
  134. struct ubi_wl_entry *e);
  135. #ifdef CONFIG_MTD_UBI_FASTMAP
  136. /**
  137. * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
  138. * @wrk: the work description object
  139. */
  140. static void update_fastmap_work_fn(struct work_struct *wrk)
  141. {
  142. struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
  143. ubi_update_fastmap(ubi);
  144. }
  145. /**
  146. * ubi_ubi_is_fm_block - returns 1 if a PEB is currently used in a fastmap.
  147. * @ubi: UBI device description object
  148. * @pnum: the to be checked PEB
  149. */
  150. static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
  151. {
  152. int i;
  153. if (!ubi->fm)
  154. return 0;
  155. for (i = 0; i < ubi->fm->used_blocks; i++)
  156. if (ubi->fm->e[i]->pnum == pnum)
  157. return 1;
  158. return 0;
  159. }
  160. #else
  161. static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
  162. {
  163. return 0;
  164. }
  165. #endif
  166. /**
  167. * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
  168. * @e: the wear-leveling entry to add
  169. * @root: the root of the tree
  170. *
  171. * Note, we use (erase counter, physical eraseblock number) pairs as keys in
  172. * the @ubi->used and @ubi->free RB-trees.
  173. */
  174. static void wl_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e, struct rb_root *root)
  175. {
  176. struct rb_node **p, *parent = NULL;
  177. p = &root->rb_node;
  178. while (*p) {
  179. struct ubi_wl_entry *e1;
  180. parent = *p;
  181. e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
  182. if (e->ec < e1->ec)
  183. p = &(*p)->rb_left;
  184. else if (e->ec > e1->ec)
  185. p = &(*p)->rb_right;
  186. else {
  187. ubi_assert(e->pnum != e1->pnum);
  188. if (e->pnum < e1->pnum)
  189. p = &(*p)->rb_left;
  190. else
  191. p = &(*p)->rb_right;
  192. }
  193. }
  194. rb_link_node(&e->u.rb, parent, p);
  195. rb_insert_color(&e->u.rb, root);
  196. }
  197. /**
  198. * do_work - do one pending work.
  199. * @ubi: UBI device description object
  200. *
  201. * This function returns zero in case of success and a negative error code in
  202. * case of failure.
  203. */
  204. static int do_work(struct ubi_device *ubi)
  205. {
  206. int err;
  207. struct ubi_work *wrk;
  208. cond_resched();
  209. /*
  210. * @ubi->work_sem is used to synchronize with the workers. Workers take
  211. * it in read mode, so many of them may be doing works at a time. But
  212. * the queue flush code has to be sure the whole queue of works is
  213. * done, and it takes the mutex in write mode.
  214. */
  215. down_read(&ubi->work_sem);
  216. spin_lock(&ubi->wl_lock);
  217. if (list_empty(&ubi->works)) {
  218. spin_unlock(&ubi->wl_lock);
  219. up_read(&ubi->work_sem);
  220. return 0;
  221. }
  222. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  223. list_del(&wrk->list);
  224. ubi->works_count -= 1;
  225. ubi_assert(ubi->works_count >= 0);
  226. spin_unlock(&ubi->wl_lock);
  227. /*
  228. * Call the worker function. Do not touch the work structure
  229. * after this call as it will have been freed or reused by that
  230. * time by the worker function.
  231. */
  232. err = wrk->func(ubi, wrk, 0);
  233. if (err)
  234. ubi_err("work failed with error code %d", err);
  235. up_read(&ubi->work_sem);
  236. return err;
  237. }
  238. /**
  239. * produce_free_peb - produce a free physical eraseblock.
  240. * @ubi: UBI device description object
  241. *
  242. * This function tries to make a free PEB by means of synchronous execution of
  243. * pending works. This may be needed if, for example the background thread is
  244. * disabled. Returns zero in case of success and a negative error code in case
  245. * of failure.
  246. */
  247. static int produce_free_peb(struct ubi_device *ubi)
  248. {
  249. int err;
  250. while (!ubi->free.rb_node && ubi->works_count) {
  251. spin_unlock(&ubi->wl_lock);
  252. dbg_wl("do one work synchronously");
  253. err = do_work(ubi);
  254. spin_lock(&ubi->wl_lock);
  255. if (err)
  256. return err;
  257. }
  258. return 0;
  259. }
  260. /**
  261. * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
  262. * @e: the wear-leveling entry to check
  263. * @root: the root of the tree
  264. *
  265. * This function returns non-zero if @e is in the @root RB-tree and zero if it
  266. * is not.
  267. */
  268. static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
  269. {
  270. struct rb_node *p;
  271. p = root->rb_node;
  272. while (p) {
  273. struct ubi_wl_entry *e1;
  274. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  275. if (e->pnum == e1->pnum) {
  276. ubi_assert(e == e1);
  277. return 1;
  278. }
  279. if (e->ec < e1->ec)
  280. p = p->rb_left;
  281. else if (e->ec > e1->ec)
  282. p = p->rb_right;
  283. else {
  284. ubi_assert(e->pnum != e1->pnum);
  285. if (e->pnum < e1->pnum)
  286. p = p->rb_left;
  287. else
  288. p = p->rb_right;
  289. }
  290. }
  291. return 0;
  292. }
  293. /**
  294. * prot_queue_add - add physical eraseblock to the protection queue.
  295. * @ubi: UBI device description object
  296. * @e: the physical eraseblock to add
  297. *
  298. * This function adds @e to the tail of the protection queue @ubi->pq, where
  299. * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
  300. * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
  301. * be locked.
  302. */
  303. static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
  304. {
  305. int pq_tail = ubi->pq_head - 1;
  306. if (pq_tail < 0)
  307. pq_tail = UBI_PROT_QUEUE_LEN - 1;
  308. ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
  309. list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
  310. e->type = WL_LIST_PQ;
  311. dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
  312. }
  313. /**
  314. * find_wl_entry - find wear-leveling entry closest to certain erase counter.
  315. * @ubi: UBI device description object
  316. * @root: the RB-tree where to look for
  317. * @diff: maximum possible difference from the smallest erase counter
  318. *
  319. * This function looks for a wear leveling entry with erase counter closest to
  320. * min + @diff, where min is the smallest erase counter.
  321. */
  322. static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
  323. struct rb_root *root, int diff)
  324. {
  325. struct rb_node *p;
  326. struct ubi_wl_entry *e, *prev_e = NULL;
  327. int max;
  328. e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  329. max = e->ec + diff;
  330. p = root->rb_node;
  331. while (p) {
  332. struct ubi_wl_entry *e1;
  333. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  334. if (e1->ec >= max)
  335. p = p->rb_left;
  336. else {
  337. p = p->rb_right;
  338. prev_e = e;
  339. e = e1;
  340. }
  341. }
  342. /* If no fastmap has been written and this WL entry can be used
  343. * as anchor PEB, hold it back and return the second best WL entry
  344. * such that fastmap can use the anchor PEB later. */
  345. if (prev_e && !ubi->fm_disabled &&
  346. !ubi->fm && e->pnum < UBI_FM_MAX_START)
  347. return prev_e;
  348. return e;
  349. }
  350. /**
  351. * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
  352. * @ubi: UBI device description object
  353. * @root: the RB-tree where to look for
  354. *
  355. * This function looks for a wear leveling entry with medium erase counter,
  356. * but not greater or equivalent than the lowest erase counter plus
  357. * %WL_FREE_MAX_DIFF/2.
  358. */
  359. static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
  360. struct rb_root *root)
  361. {
  362. struct ubi_wl_entry *e, *first, *last;
  363. first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  364. last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
  365. if (last->ec - first->ec < ubi->wl_th*2) {
  366. e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
  367. #ifdef CONFIG_MTD_UBI_FASTMAP
  368. /* If no fastmap has been written and this WL entry can be used
  369. * as anchor PEB, hold it back and return the second best
  370. * WL entry such that fastmap can use the anchor PEB later. */
  371. if (e && !ubi->fm_disabled && !ubi->fm &&
  372. e->pnum < UBI_FM_MAX_START)
  373. e = rb_entry(rb_next(root->rb_node),
  374. struct ubi_wl_entry, u.rb);
  375. #endif
  376. } else
  377. e = find_wl_entry(ubi, root, ubi->wl_th);
  378. return e;
  379. }
  380. #ifdef CONFIG_MTD_UBI_FASTMAP
  381. /**
  382. * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
  383. * @root: the RB-tree where to look for
  384. */
  385. static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
  386. {
  387. struct rb_node *p;
  388. struct ubi_wl_entry *e, *victim = NULL;
  389. int max_ec = UBI_MAX_ERASECOUNTER;
  390. ubi_rb_for_each_entry(p, e, root, u.rb) {
  391. if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
  392. victim = e;
  393. max_ec = e->ec;
  394. }
  395. }
  396. return victim;
  397. }
  398. static int anchor_pebs_avalible(struct rb_root *root)
  399. {
  400. struct rb_node *p;
  401. struct ubi_wl_entry *e;
  402. ubi_rb_for_each_entry(p, e, root, u.rb)
  403. if (e->pnum < UBI_FM_MAX_START)
  404. return 1;
  405. return 0;
  406. }
  407. /**
  408. * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
  409. * @ubi: UBI device description object
  410. * @anchor: This PEB will be used as anchor PEB by fastmap
  411. *
  412. * The function returns a physical erase block with a given maximal number
  413. * and removes it from the wl subsystem.
  414. * Must be called with wl_lock held!
  415. */
  416. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
  417. {
  418. struct ubi_wl_entry *e = NULL;
  419. if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
  420. goto out;
  421. if (anchor)
  422. e = find_anchor_wl_entry(&ubi->free);
  423. else
  424. e = find_mean_wl_entry(ubi, &ubi->free);
  425. if (!e)
  426. goto out;
  427. self_check_in_wl_tree(ubi, e, &ubi->free);
  428. /* remove it from the free list,
  429. * the wl subsystem does no longer know this erase block */
  430. rb_erase(&e->u.rb, &ubi->free);
  431. ubi->free_count--;
  432. out:
  433. return e;
  434. }
  435. #endif
  436. /**
  437. * __wl_get_peb - get a physical eraseblock.
  438. * @ubi: UBI device description object
  439. *
  440. * This function returns a physical eraseblock in case of success and a
  441. * negative error code in case of failure.
  442. */
  443. static int __wl_get_peb(struct ubi_device *ubi)
  444. {
  445. int err;
  446. struct ubi_wl_entry *e;
  447. retry:
  448. if (!ubi->free.rb_node) {
  449. if (ubi->works_count == 0) {
  450. ubi_err("1 no free eraseblocks");
  451. ubi_err("free count:%d", ubi->free_count);
  452. ubi_assert(list_empty(&ubi->works));
  453. return -ENOSPC;
  454. }
  455. err = produce_free_peb(ubi);
  456. if (err < 0)
  457. return err;
  458. goto retry;
  459. }
  460. e = find_mean_wl_entry(ubi, &ubi->free);
  461. if (!e) {
  462. ubi_err("2 no free eraseblocks");
  463. ubi_err("free count:%d", ubi->free_count);
  464. return -ENOSPC;
  465. }
  466. self_check_in_wl_tree(ubi, e, &ubi->free);
  467. /*
  468. * Move the physical eraseblock to the protection queue where it will
  469. * be protected from being moved for some time.
  470. */
  471. rb_erase(&e->u.rb, &ubi->free);
  472. ubi->free_count--;
  473. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  474. #ifndef CONFIG_MTD_UBI_FASTMAP
  475. /* We have to enqueue e only if fastmap is disabled,
  476. * is fastmap enabled prot_queue_add() will be called by
  477. * ubi_wl_get_peb() after removing e from the pool. */
  478. prot_queue_add(ubi, e);
  479. #endif
  480. return e->pnum;
  481. }
  482. #ifdef CONFIG_MTD_UBI_FASTMAP
  483. /**
  484. * return_unused_pool_pebs - returns unused PEB to the free tree.
  485. * @ubi: UBI device description object
  486. * @pool: fastmap pool description object
  487. */
  488. static void return_unused_pool_pebs(struct ubi_device *ubi,
  489. struct ubi_fm_pool *pool)
  490. {
  491. int i;
  492. struct ubi_wl_entry *e;
  493. for (i = pool->used; i < pool->size; i++) {
  494. e = ubi->lookuptbl[pool->pebs[i]];
  495. wl_tree_add(ubi, e, &ubi->free);
  496. ubi->free_count++;
  497. }
  498. }
  499. /**
  500. * refill_wl_pool - refills all the fastmap pool used by the
  501. * WL sub-system.
  502. * @ubi: UBI device description object
  503. */
  504. static void refill_wl_pool(struct ubi_device *ubi)
  505. {
  506. struct ubi_wl_entry *e;
  507. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  508. return_unused_pool_pebs(ubi, pool);
  509. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  510. if (!ubi->free.rb_node ||
  511. (ubi->free_count - ubi->beb_rsvd_pebs < 5))
  512. break;
  513. e = find_wl_entry(ubi, &ubi->free, ubi->wl_th*2);
  514. self_check_in_wl_tree(ubi, e, &ubi->free);
  515. rb_erase(&e->u.rb, &ubi->free);
  516. ubi->free_count--;
  517. pool->pebs[pool->size] = e->pnum;
  518. }
  519. pool->used = 0;
  520. }
  521. /**
  522. * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
  523. * @ubi: UBI device description object
  524. */
  525. static void refill_wl_user_pool(struct ubi_device *ubi)
  526. {
  527. struct ubi_fm_pool *pool = &ubi->fm_pool;
  528. return_unused_pool_pebs(ubi, pool);
  529. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  530. pool->pebs[pool->size] = __wl_get_peb(ubi);
  531. if (pool->pebs[pool->size] < 0)
  532. break;
  533. }
  534. pool->used = 0;
  535. }
  536. /**
  537. * ubi_refill_pools - refills all fastmap PEB pools.
  538. * @ubi: UBI device description object
  539. */
  540. void ubi_refill_pools(struct ubi_device *ubi)
  541. {
  542. spin_lock(&ubi->wl_lock);
  543. refill_wl_pool(ubi);
  544. refill_wl_user_pool(ubi);
  545. spin_unlock(&ubi->wl_lock);
  546. }
  547. /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
  548. * the fastmap pool.
  549. */
  550. int ubi_wl_get_peb(struct ubi_device *ubi)
  551. {
  552. int ret;
  553. struct ubi_fm_pool *pool = &ubi->fm_pool;
  554. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  555. if (!pool->size || !wl_pool->size || pool->used == pool->size ||
  556. wl_pool->used == wl_pool->size)
  557. ubi_update_fastmap(ubi);
  558. /* we got not a single free PEB */
  559. if (!pool->size)
  560. ret = -ENOSPC;
  561. else {
  562. spin_lock(&ubi->wl_lock);
  563. ret = pool->pebs[pool->used++];
  564. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  565. spin_unlock(&ubi->wl_lock);
  566. }
  567. return ret;
  568. }
  569. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  570. *
  571. * @ubi: UBI device description object
  572. */
  573. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  574. {
  575. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  576. int pnum;
  577. if (pool->used == pool->size || !pool->size) {
  578. /* We cannot update the fastmap here because this
  579. * function is called in atomic context.
  580. * Let's fail here and refill/update it as soon as possible. */
  581. schedule_work(&ubi->fm_work);
  582. return NULL;
  583. }
  584. pnum = pool->pebs[pool->used++];
  585. return ubi->lookuptbl[pnum];
  586. }
  587. #else
  588. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  589. {
  590. struct ubi_wl_entry *e;
  591. e = find_wl_entry(ubi, &ubi->free, ubi->wl_th*2);
  592. self_check_in_wl_tree(ubi, e, &ubi->free);
  593. ubi->free_count--;
  594. ubi_assert(ubi->free_count >= 0);
  595. rb_erase(&e->u.rb, &ubi->free);
  596. return e;
  597. }
  598. int ubi_wl_get_peb(struct ubi_device *ubi)
  599. {
  600. int peb, err;
  601. spin_lock(&ubi->wl_lock);
  602. peb = __wl_get_peb(ubi);
  603. spin_unlock(&ubi->wl_lock);
  604. if (peb < 0)
  605. return peb;
  606. err = ubi_self_check_all_ff(ubi, peb, ubi->vid_hdr_aloffset,
  607. ubi->peb_size - ubi->vid_hdr_aloffset);
  608. if (err) {
  609. ubi_err("new PEB %d does not contain all 0xFF bytes", peb);
  610. return err;
  611. }
  612. return peb;
  613. }
  614. #endif
  615. /**
  616. * prot_queue_del - remove a physical eraseblock from the protection queue.
  617. * @ubi: UBI device description object
  618. * @pnum: the physical eraseblock to remove
  619. *
  620. * This function deletes PEB @pnum from the protection queue and returns zero
  621. * in case of success and %-ENODEV if the PEB was not found.
  622. */
  623. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  624. {
  625. struct ubi_wl_entry *e;
  626. e = ubi->lookuptbl[pnum];
  627. if (!e)
  628. return -ENODEV;
  629. if (self_check_in_pq(ubi, e))
  630. return -ENODEV;
  631. list_del(&e->u.list);
  632. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  633. return 0;
  634. }
  635. /**
  636. * sync_erase - synchronously erase a physical eraseblock.
  637. * @ubi: UBI device description object
  638. * @e: the the physical eraseblock to erase
  639. * @torture: if the physical eraseblock has to be tortured
  640. *
  641. * This function returns zero in case of success and a negative error code in
  642. * case of failure.
  643. */
  644. int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  645. int torture)
  646. {
  647. int err;
  648. struct ubi_ec_hdr *ec_hdr;
  649. unsigned long long old_ec = e->ec, ec = e->ec; /*MTK: old_ec*/
  650. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  651. err = self_check_ec(ubi, e->pnum, e->ec);
  652. if (err)
  653. return -EINVAL;
  654. ec_hdr = vzalloc(ubi->ec_hdr_alsize);
  655. if (!ec_hdr)
  656. return -ENOMEM;
  657. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  658. if (err < 0)
  659. goto out_free;
  660. ec += err;
  661. if (ec > UBI_MAX_ERASECOUNTER) {
  662. /*
  663. * Erase counter overflow. Upgrade UBI and use 64-bit
  664. * erase counters internally.
  665. */
  666. ubi_err("erase counter overflow at PEB %d, EC %llu",
  667. e->pnum, ec);
  668. err = -EINVAL;
  669. goto out_free;
  670. }
  671. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  672. ec_hdr->ec = cpu_to_be64(ec);
  673. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  674. if (err)
  675. goto out_free;
  676. e->ec = ec;
  677. spin_lock(&ubi->wl_lock);
  678. if (e->ec > ubi->max_ec)
  679. ubi->max_ec = e->ec;
  680. /*MTK start: the incresing of ec > 1 is doing by torture*/
  681. if (ec - old_ec > 1)
  682. ubi->torture += (ec - old_ec);
  683. ubi->ec_sum += (ec - old_ec);
  684. ubi->mean_ec = div_u64(ubi->ec_sum, ubi->rsvd_pebs);
  685. /*MTK end*/
  686. spin_unlock(&ubi->wl_lock);
  687. out_free:
  688. vfree(ec_hdr);
  689. return err;
  690. }
  691. /**
  692. * serve_prot_queue - check if it is time to stop protecting PEBs.
  693. * @ubi: UBI device description object
  694. *
  695. * This function is called after each erase operation and removes PEBs from the
  696. * tail of the protection queue. These PEBs have been protected for long enough
  697. * and should be moved to the used tree.
  698. */
  699. static void serve_prot_queue(struct ubi_device *ubi)
  700. {
  701. struct ubi_wl_entry *e, *tmp;
  702. int count;
  703. /*
  704. * There may be several protected physical eraseblock to remove,
  705. * process them all.
  706. */
  707. repeat:
  708. count = 0;
  709. spin_lock(&ubi->wl_lock);
  710. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  711. dbg_wl("PEB %d EC %d protection over, move to used tree",
  712. e->pnum, e->ec);
  713. list_del(&e->u.list);
  714. wl_tree_add(ubi, e, &ubi->used);
  715. if (count++ > 32) {
  716. /*
  717. * Let's be nice and avoid holding the spinlock for
  718. * too long.
  719. */
  720. spin_unlock(&ubi->wl_lock);
  721. cond_resched();
  722. goto repeat;
  723. }
  724. }
  725. ubi->pq_head += 1;
  726. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  727. ubi->pq_head = 0;
  728. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  729. spin_unlock(&ubi->wl_lock);
  730. }
  731. /**
  732. * __schedule_ubi_work - schedule a work.
  733. * @ubi: UBI device description object
  734. * @wrk: the work to schedule
  735. *
  736. * This function adds a work defined by @wrk to the tail of the pending works
  737. * list. Can only be used if ubi->work_sem is already held in read mode!
  738. */
  739. static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  740. {
  741. spin_lock(&ubi->wl_lock);
  742. list_add_tail(&wrk->list, &ubi->works);
  743. ubi_assert(ubi->works_count >= 0);
  744. ubi->works_count += 1;
  745. if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
  746. wake_up_process(ubi->bgt_thread);
  747. spin_unlock(&ubi->wl_lock);
  748. }
  749. /**
  750. * schedule_ubi_work - schedule a work.
  751. * @ubi: UBI device description object
  752. * @wrk: the work to schedule
  753. *
  754. * This function adds a work defined by @wrk to the tail of the pending works
  755. * list.
  756. */
  757. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  758. {
  759. down_read(&ubi->work_sem);
  760. __schedule_ubi_work(ubi, wrk);
  761. up_read(&ubi->work_sem);
  762. }
  763. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  764. int shutdown);
  765. #ifdef CONFIG_MTD_UBI_FASTMAP
  766. /**
  767. * ubi_is_erase_work - checks whether a work is erase work.
  768. * @wrk: The work object to be checked
  769. */
  770. int ubi_is_erase_work(struct ubi_work *wrk)
  771. {
  772. return wrk->func == erase_worker;
  773. }
  774. #endif
  775. /**
  776. * schedule_erase - schedule an erase work.
  777. * @ubi: UBI device description object
  778. * @e: the WL entry of the physical eraseblock to erase
  779. * @vol_id: the volume ID that last used this PEB
  780. * @lnum: the last used logical eraseblock number for the PEB
  781. * @torture: if the physical eraseblock has to be tortured
  782. *
  783. * This function returns zero in case of success and a %-ENOMEM in case of
  784. * failure.
  785. */
  786. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  787. int vol_id, int lnum, int torture)
  788. {
  789. struct ubi_work *wl_wrk;
  790. ubi_assert(e);
  791. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  792. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  793. e->pnum, e->ec, torture);
  794. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  795. if (!wl_wrk)
  796. return -ENOMEM;
  797. wl_wrk->func = &erase_worker;
  798. wl_wrk->e = e;
  799. wl_wrk->vol_id = vol_id;
  800. wl_wrk->lnum = lnum;
  801. wl_wrk->torture = torture;
  802. schedule_ubi_work(ubi, wl_wrk);
  803. return 0;
  804. }
  805. /**
  806. * do_sync_erase - run the erase worker synchronously.
  807. * @ubi: UBI device description object
  808. * @e: the WL entry of the physical eraseblock to erase
  809. * @vol_id: the volume ID that last used this PEB
  810. * @lnum: the last used logical eraseblock number for the PEB
  811. * @torture: if the physical eraseblock has to be tortured
  812. *
  813. */
  814. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  815. int vol_id, int lnum, int torture)
  816. {
  817. struct ubi_work *wl_wrk;
  818. dbg_wl("sync erase of PEB %i", e->pnum);
  819. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  820. if (!wl_wrk)
  821. return -ENOMEM;
  822. wl_wrk->e = e;
  823. wl_wrk->vol_id = vol_id;
  824. wl_wrk->lnum = lnum;
  825. wl_wrk->torture = torture;
  826. return erase_worker(ubi, wl_wrk, 0);
  827. }
  828. #ifdef CONFIG_MTD_UBI_FASTMAP
  829. /**
  830. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  831. * sub-system.
  832. * see: ubi_wl_put_peb()
  833. *
  834. * @ubi: UBI device description object
  835. * @fm_e: physical eraseblock to return
  836. * @lnum: the last used logical eraseblock number for the PEB
  837. * @torture: if this physical eraseblock has to be tortured
  838. */
  839. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  840. int lnum, int torture)
  841. {
  842. struct ubi_wl_entry *e;
  843. int vol_id, pnum = fm_e->pnum;
  844. dbg_wl("PEB %d", pnum);
  845. ubi_assert(pnum >= 0);
  846. ubi_assert(pnum < ubi->peb_count);
  847. spin_lock(&ubi->wl_lock);
  848. e = ubi->lookuptbl[pnum];
  849. /* This can happen if we recovered from a fastmap the very
  850. * first time and writing now a new one. In this case the wl system
  851. * has never seen any PEB used by the original fastmap.
  852. */
  853. if (!e) {
  854. e = fm_e;
  855. ubi_assert(e->ec >= 0);
  856. ubi->lookuptbl[pnum] = e;
  857. } else {
  858. e->ec = fm_e->ec;
  859. kfree(fm_e);
  860. }
  861. spin_unlock(&ubi->wl_lock);
  862. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  863. return schedule_erase(ubi, e, vol_id, lnum, torture);
  864. }
  865. #endif
  866. /**
  867. * wear_leveling_worker - wear-leveling worker function.
  868. * @ubi: UBI device description object
  869. * @wrk: the work object
  870. * @shutdown: non-zero if the worker has to free memory and exit
  871. * because the WL-subsystem is shutting down
  872. *
  873. * This function copies a more worn out physical eraseblock to a less worn out
  874. * one. Returns zero in case of success and a negative error code in case of
  875. * failure.
  876. */
  877. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  878. int shutdown)
  879. {
  880. int erase_e2 = 1, err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  881. int vol_id = -1, lnum = -1;
  882. #ifdef CONFIG_MTD_UBI_FASTMAP
  883. int anchor = wrk->anchor;
  884. #endif
  885. struct ubi_wl_entry *e1, *e2;
  886. struct ubi_vid_hdr *vid_hdr;
  887. int do_wl = 0; /*MTK:wl or not, 1 for wl, 2 for scrubbing*/
  888. kfree(wrk);
  889. if (shutdown)
  890. return 0;
  891. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  892. if (!vid_hdr)
  893. return -ENOMEM;
  894. mutex_lock(&ubi->move_mutex);
  895. spin_lock(&ubi->wl_lock);
  896. ubi_assert(!ubi->move_from && !ubi->move_to);
  897. ubi_assert(!ubi->move_to_put);
  898. if (!ubi->free.rb_node ||
  899. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  900. /*
  901. * No free physical eraseblocks? Well, they must be waiting in
  902. * the queue to be erased. Cancel movement - it will be
  903. * triggered again when a free physical eraseblock appears.
  904. *
  905. * No used physical eraseblocks? They must be temporarily
  906. * protected from being moved. They will be moved to the
  907. * @ubi->used tree later and the wear-leveling will be
  908. * triggered again.
  909. */
  910. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  911. !ubi->free.rb_node, !ubi->used.rb_node);
  912. goto out_cancel;
  913. }
  914. #ifdef CONFIG_MTD_UBI_FASTMAP
  915. /* Check whether we need to produce an anchor PEB */
  916. if (!anchor)
  917. anchor = !anchor_pebs_avalible(&ubi->free);
  918. if (anchor) {
  919. e1 = find_anchor_wl_entry(&ubi->used);
  920. if (!e1)
  921. goto out_cancel;
  922. e2 = get_peb_for_wl(ubi);
  923. if (!e2)
  924. goto out_cancel;
  925. self_check_in_wl_tree(ubi, e1, &ubi->used);
  926. rb_erase(&e1->u.rb, &ubi->used);
  927. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  928. } else if (!ubi->scrub.rb_node) {
  929. #else
  930. if (!ubi->scrub.rb_node) {
  931. #endif
  932. /*
  933. * Now pick the least worn-out used physical eraseblock and a
  934. * highly worn-out free physical eraseblock. If the erase
  935. * counters differ much enough, start wear-leveling.
  936. */
  937. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  938. e2 = get_peb_for_wl(ubi);
  939. if (!e2)
  940. goto out_cancel;
  941. if (!(e2->ec - e1->ec >= ubi->wl_th)) {
  942. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  943. e1->ec, e2->ec);
  944. /* Give the unused PEB back */
  945. wl_tree_add(ubi, e2, &ubi->free);
  946. ubi->free_count++;
  947. goto out_cancel;
  948. }
  949. self_check_in_wl_tree(ubi, e1, &ubi->used);
  950. rb_erase(&e1->u.rb, &ubi->used);
  951. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  952. e1->pnum, e1->ec, e2->pnum, e2->ec);
  953. do_wl = 1; /*MTK*/
  954. } else {
  955. /* Perform scrubbing */
  956. scrubbing = 1;
  957. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  958. e2 = get_peb_for_wl(ubi);
  959. if (!e2)
  960. goto out_cancel;
  961. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  962. rb_erase(&e1->u.rb, &ubi->scrub);
  963. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  964. do_wl = 2; /*MTK*/
  965. }
  966. ubi->move_from = e1;
  967. ubi->move_to = e2;
  968. spin_unlock(&ubi->wl_lock);
  969. /*
  970. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  971. * We so far do not know which logical eraseblock our physical
  972. * eraseblock (@e1) belongs to. We have to read the volume identifier
  973. * header first.
  974. *
  975. * Note, we are protected from this PEB being unmapped and erased. The
  976. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  977. * which is being moved was unmapped.
  978. */
  979. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  980. if (err && err != UBI_IO_BITFLIPS) {
  981. if (err == UBI_IO_FF) {
  982. /*
  983. * We are trying to move PEB without a VID header. UBI
  984. * always write VID headers shortly after the PEB was
  985. * given, so we have a situation when it has not yet
  986. * had a chance to write it, because it was preempted.
  987. * So add this PEB to the protection queue so far,
  988. * because presumably more data will be written there
  989. * (including the missing VID header), and then we'll
  990. * move it.
  991. */
  992. dbg_wl("PEB %d has no VID header", e1->pnum);
  993. protect = 1;
  994. erase_e2 = 0; /*MTK*/
  995. goto out_not_moved;
  996. } else if (err == UBI_IO_FF_BITFLIPS) {
  997. /*
  998. * The same situation as %UBI_IO_FF, but bit-flips were
  999. * detected. It is better to schedule this PEB for
  1000. * scrubbing.
  1001. */
  1002. dbg_wl("PEB %d has no VID header but has bit-flips",
  1003. e1->pnum);
  1004. scrubbing = 1;
  1005. erase_e2 = 0; /*MTK*/
  1006. goto out_not_moved;
  1007. }
  1008. ubi_err("error %d while reading VID header from PEB %d",
  1009. err, e1->pnum);
  1010. goto out_error;
  1011. }
  1012. vol_id = be32_to_cpu(vid_hdr->vol_id);
  1013. lnum = be32_to_cpu(vid_hdr->lnum);
  1014. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr, do_wl); /*MTK: pass do_wl*/
  1015. if (err) {
  1016. if (err == MOVE_CANCEL_RACE) {
  1017. /*
  1018. * The LEB has not been moved because the volume is
  1019. * being deleted or the PEB has been put meanwhile. We
  1020. * should prevent this PEB from being selected for
  1021. * wear-leveling movement again, so put it to the
  1022. * protection queue.
  1023. */
  1024. protect = 1;
  1025. erase_e2 = 0; /*MTK*/
  1026. goto out_not_moved;
  1027. }
  1028. if (err == MOVE_RETRY) {
  1029. scrubbing = 1;
  1030. atomic_inc(&ubi->move_retry); /*MTK*/
  1031. erase_e2 = 0; /*MTK*/
  1032. goto out_not_moved;
  1033. }
  1034. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  1035. err == MOVE_TARGET_RD_ERR) {
  1036. /*
  1037. * Target PEB had bit-flips or write error - torture it.
  1038. */
  1039. torture = 1;
  1040. goto out_not_moved;
  1041. }
  1042. if (err == MOVE_SOURCE_RD_ERR) {
  1043. /*
  1044. * An error happened while reading the source PEB. Do
  1045. * not switch to R/O mode in this case, and give the
  1046. * upper layers a possibility to recover from this,
  1047. * e.g. by unmapping corresponding LEB. Instead, just
  1048. * put this PEB to the @ubi->erroneous list to prevent
  1049. * UBI from trying to move it over and over again.
  1050. */
  1051. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  1052. ubi_err("too many erroneous eraseblocks (%d)",
  1053. ubi->erroneous_peb_count);
  1054. goto out_error;
  1055. }
  1056. erroneous = 1;
  1057. goto out_not_moved;
  1058. }
  1059. if (err < 0)
  1060. goto out_error;
  1061. ubi_assert(0);
  1062. }
  1063. /* The PEB has been successfully moved */
  1064. if (scrubbing)
  1065. ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  1066. e1->pnum, vol_id, lnum, e2->pnum);
  1067. ubi_free_vid_hdr(ubi, vid_hdr);
  1068. spin_lock(&ubi->wl_lock);
  1069. if (!ubi->move_to_put) {
  1070. wl_tree_add(ubi, e2, &ubi->used);
  1071. e2 = NULL;
  1072. }
  1073. ubi->move_from = ubi->move_to = NULL;
  1074. ubi->move_to_put = ubi->wl_scheduled = 0;
  1075. spin_unlock(&ubi->wl_lock);
  1076. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  1077. if (err) {
  1078. if (e2)
  1079. kmem_cache_free(ubi_wl_entry_slab, e2);
  1080. goto out_ro;
  1081. }
  1082. if (e2) {
  1083. /*
  1084. * Well, the target PEB was put meanwhile, schedule it for
  1085. * erasure.
  1086. */
  1087. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  1088. e2->pnum, vol_id, lnum);
  1089. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  1090. if (err)
  1091. goto out_ro;
  1092. }
  1093. dbg_wl("done");
  1094. mutex_unlock(&ubi->move_mutex);
  1095. return 0;
  1096. /*
  1097. * For some reasons the LEB was not moved, might be an error, might be
  1098. * something else. @e1 was not changed, so return it back. @e2 might
  1099. * have been changed, schedule it for erasure.
  1100. */
  1101. out_not_moved:
  1102. if (vol_id != -1)
  1103. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  1104. e1->pnum, vol_id, lnum, e2->pnum, err);
  1105. else
  1106. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  1107. e1->pnum, e2->pnum, err);
  1108. spin_lock(&ubi->wl_lock);
  1109. if (protect)
  1110. prot_queue_add(ubi, e1);
  1111. else if (erroneous) {
  1112. wl_tree_add(ubi, e1, &ubi->erroneous);
  1113. ubi->erroneous_peb_count += 1;
  1114. } else if (scrubbing)
  1115. wl_tree_add(ubi, e1, &ubi->scrub);
  1116. else
  1117. wl_tree_add(ubi, e1, &ubi->used);
  1118. ubi_assert(!ubi->move_to_put);
  1119. ubi->move_from = ubi->move_to = NULL;
  1120. ubi->wl_scheduled = 0;
  1121. spin_unlock(&ubi->wl_lock);
  1122. ubi_free_vid_hdr(ubi, vid_hdr);
  1123. /*MTK start*/
  1124. if (erase_e2 == 1) {
  1125. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  1126. if (err) {
  1127. kmem_cache_free(ubi_wl_entry_slab, e2);
  1128. goto out_ro;
  1129. }
  1130. } else {
  1131. spin_lock(&ubi->wl_lock);
  1132. wl_tree_add(ubi, e2, &ubi->free);
  1133. spin_unlock(&ubi->wl_lock);
  1134. }
  1135. /*MTK end*/
  1136. mutex_unlock(&ubi->move_mutex);
  1137. return 0;
  1138. out_error:
  1139. if (vol_id != -1)
  1140. ubi_err("error %d while moving PEB %d to PEB %d",
  1141. err, e1->pnum, e2->pnum);
  1142. else
  1143. ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  1144. err, e1->pnum, vol_id, lnum, e2->pnum);
  1145. spin_lock(&ubi->wl_lock);
  1146. ubi->move_from = ubi->move_to = NULL;
  1147. ubi->move_to_put = ubi->wl_scheduled = 0;
  1148. spin_unlock(&ubi->wl_lock);
  1149. ubi_free_vid_hdr(ubi, vid_hdr);
  1150. kmem_cache_free(ubi_wl_entry_slab, e1);
  1151. kmem_cache_free(ubi_wl_entry_slab, e2);
  1152. out_ro:
  1153. ubi_ro_mode(ubi);
  1154. mutex_unlock(&ubi->move_mutex);
  1155. ubi_assert(err != 0);
  1156. return err < 0 ? err : -EIO;
  1157. out_cancel:
  1158. ubi->wl_scheduled = 0;
  1159. spin_unlock(&ubi->wl_lock);
  1160. mutex_unlock(&ubi->move_mutex);
  1161. ubi_free_vid_hdr(ubi, vid_hdr);
  1162. return 0;
  1163. }
  1164. /**
  1165. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  1166. * @ubi: UBI device description object
  1167. * @nested: set to non-zero if this function is called from UBI worker
  1168. *
  1169. * This function checks if it is time to start wear-leveling and schedules it
  1170. * if yes. This function returns zero in case of success and a negative error
  1171. * code in case of failure.
  1172. */
  1173. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  1174. {
  1175. int err = 0;
  1176. struct ubi_wl_entry *e1;
  1177. struct ubi_wl_entry *e2;
  1178. struct ubi_work *wrk;
  1179. spin_lock(&ubi->wl_lock);
  1180. /*
  1181. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  1182. * the WL worker has to be scheduled anyway.
  1183. */
  1184. if (!ubi->scrub.rb_node) {
  1185. if (!ubi->used.rb_node || !ubi->free.rb_node)
  1186. /* No physical eraseblocks - no deal */
  1187. goto out_unlock;
  1188. /*
  1189. * We schedule wear-leveling only if the difference between the
  1190. * lowest erase counter of used physical eraseblocks and a high
  1191. * erase counter of free physical eraseblocks is greater than
  1192. * %UBI_WL_THRESHOLD.
  1193. */
  1194. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  1195. e2 = find_wl_entry(ubi, &ubi->free, ubi->wl_th*2);
  1196. if (!(e2->ec - e1->ec >= ubi->wl_th))
  1197. goto out_unlock;
  1198. dbg_wl("schedule wear-leveling");
  1199. } else
  1200. dbg_wl("schedule scrubbing");
  1201. spin_unlock(&ubi->wl_lock);
  1202. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1203. if (!wrk) {
  1204. err = -ENOMEM;
  1205. clear_bit(0, &ubi->wl_scheduled);
  1206. goto out_cancel;
  1207. }
  1208. wrk->anchor = 0;
  1209. wrk->func = &wear_leveling_worker;
  1210. if (nested)
  1211. __schedule_ubi_work(ubi, wrk);
  1212. else
  1213. schedule_ubi_work(ubi, wrk);
  1214. return err;
  1215. out_cancel:
  1216. spin_lock(&ubi->wl_lock);
  1217. out_unlock:
  1218. spin_unlock(&ubi->wl_lock);
  1219. return err;
  1220. }
  1221. #ifdef CONFIG_MTD_UBI_FASTMAP
  1222. /**
  1223. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  1224. * @ubi: UBI device description object
  1225. */
  1226. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  1227. {
  1228. struct ubi_work *wrk;
  1229. spin_lock(&ubi->wl_lock);
  1230. if (ubi->wl_scheduled) {
  1231. spin_unlock(&ubi->wl_lock);
  1232. return 0;
  1233. }
  1234. ubi->wl_scheduled = 1;
  1235. spin_unlock(&ubi->wl_lock);
  1236. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1237. if (!wrk) {
  1238. spin_lock(&ubi->wl_lock);
  1239. ubi->wl_scheduled = 0;
  1240. spin_unlock(&ubi->wl_lock);
  1241. return -ENOMEM;
  1242. }
  1243. wrk->anchor = 1;
  1244. wrk->func = &wear_leveling_worker;
  1245. schedule_ubi_work(ubi, wrk);
  1246. return 0;
  1247. }
  1248. #endif
  1249. /**
  1250. * erase_worker - physical eraseblock erase worker function.
  1251. * @ubi: UBI device description object
  1252. * @wl_wrk: the work object
  1253. * @shutdown: non-zero if the worker has to free memory and exit
  1254. * because the WL sub-system is shutting down
  1255. *
  1256. * This function erases a physical eraseblock and perform torture testing if
  1257. * needed. It also takes care about marking the physical eraseblock bad if
  1258. * needed. Returns zero in case of success and a negative error code in case of
  1259. * failure.
  1260. */
  1261. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1262. int shutdown)
  1263. {
  1264. struct ubi_wl_entry *e = wl_wrk->e;
  1265. int pnum = e->pnum;
  1266. int vol_id = wl_wrk->vol_id;
  1267. int lnum = wl_wrk->lnum;
  1268. int err, available_consumed = 0;
  1269. if (shutdown) {
  1270. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  1271. kfree(wl_wrk);
  1272. kmem_cache_free(ubi_wl_entry_slab, e);
  1273. return 0;
  1274. }
  1275. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  1276. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  1277. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1278. err = sync_erase(ubi, e, wl_wrk->torture);
  1279. if (!err) {
  1280. /* Fine, we've erased it successfully */
  1281. kfree(wl_wrk);
  1282. spin_lock(&ubi->wl_lock);
  1283. { /* for SLC block erase worker */
  1284. wl_tree_add(ubi, e, &ubi->free);
  1285. ubi->free_count++;
  1286. }
  1287. spin_unlock(&ubi->wl_lock);
  1288. /*
  1289. * One more erase operation has happened, take care about
  1290. * protected physical eraseblocks.
  1291. */
  1292. serve_prot_queue(ubi);
  1293. /* And take care about wear-leveling */
  1294. err = ensure_wear_leveling(ubi, 1);
  1295. return err;
  1296. }
  1297. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  1298. kfree(wl_wrk);
  1299. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  1300. err == -EBUSY) {
  1301. int err1;
  1302. /* Re-schedule the LEB for erasure */
  1303. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  1304. if (err1) {
  1305. err = err1;
  1306. goto out_ro;
  1307. }
  1308. return err;
  1309. }
  1310. kmem_cache_free(ubi_wl_entry_slab, e);
  1311. if (err != -EIO)
  1312. /*
  1313. * If this is not %-EIO, we have no idea what to do. Scheduling
  1314. * this physical eraseblock for erasure again would cause
  1315. * errors again and again. Well, lets switch to R/O mode.
  1316. */
  1317. goto out_ro;
  1318. /* It is %-EIO, the PEB went bad */
  1319. if (!ubi->bad_allowed) {
  1320. ubi_err("bad physical eraseblock %d detected", pnum);
  1321. goto out_ro;
  1322. }
  1323. spin_lock(&ubi->volumes_lock);
  1324. if (ubi->beb_rsvd_pebs == 0) {
  1325. if (ubi->avail_pebs == 0) {
  1326. spin_unlock(&ubi->volumes_lock);
  1327. ubi_err("no reserved/available physical eraseblocks");
  1328. goto out_ro;
  1329. }
  1330. ubi->avail_pebs -= 1;
  1331. available_consumed = 1;
  1332. }
  1333. spin_unlock(&ubi->volumes_lock);
  1334. dbg_wl("mark PEB %d as bad", pnum);
  1335. err = ubi_io_mark_bad(ubi, pnum);
  1336. if (err)
  1337. goto out_ro;
  1338. spin_lock(&ubi->volumes_lock);
  1339. if (ubi->beb_rsvd_pebs > 0) {
  1340. if (available_consumed) {
  1341. /*
  1342. * The amount of reserved PEBs increased since we last
  1343. * checked.
  1344. */
  1345. ubi->avail_pebs += 1;
  1346. available_consumed = 0;
  1347. }
  1348. ubi->beb_rsvd_pebs -= 1;
  1349. }
  1350. ubi->bad_peb_count += 1;
  1351. ubi->good_peb_count -= 1;
  1352. ubi_calculate_reserved(ubi);
  1353. if (available_consumed)
  1354. ubi_warn("no PEBs in the reserved pool, used an available PEB");
  1355. else if (ubi->beb_rsvd_pebs)
  1356. dbg_wl("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
  1357. else
  1358. ubi_warn("last PEB from the reserve was used");
  1359. spin_unlock(&ubi->volumes_lock);
  1360. return err;
  1361. out_ro:
  1362. if (available_consumed) {
  1363. spin_lock(&ubi->volumes_lock);
  1364. ubi->avail_pebs += 1;
  1365. spin_unlock(&ubi->volumes_lock);
  1366. }
  1367. ubi_ro_mode(ubi);
  1368. return err;
  1369. }
  1370. /**
  1371. * ubifs_erase_peb - erase physical eraseblock for mtk.
  1372. * @ubi: UBI device description object
  1373. * @wl_wrk: the work object
  1374. * @cancel: non-zero if the worker has to free memory and exit
  1375. *
  1376. * This function erases a physical eraseblock and perform torture testing if
  1377. * needed. It also takes care about marking the physical eraseblock bad if
  1378. * needed. Returns zero in case of success and a negative error code in case of
  1379. * failure.
  1380. */
  1381. static int ubi_erase_peb(struct ubi_device *ubi, struct ubi_wl_entry *e,
  1382. int torture)
  1383. {
  1384. int pnum = e->pnum, err, need;
  1385. int retry = 0;
  1386. retry_erase:
  1387. retry++;
  1388. err = sync_erase(ubi, e, torture);
  1389. if (!err) {
  1390. /* Fine, we've erased it successfully */
  1391. spin_lock(&ubi->wl_lock);
  1392. { /* for SLC block erase worker */
  1393. wl_tree_add(ubi, e, &ubi->free);
  1394. ubi->free_count++;
  1395. }
  1396. spin_unlock(&ubi->wl_lock);
  1397. /*
  1398. * One more erase operation has happened, take care about
  1399. * protected physical eraseblocks.
  1400. */
  1401. serve_prot_queue(ubi);
  1402. /* And take care about wear-leveling */
  1403. err = ensure_wear_leveling(ubi, 1);
  1404. return err;
  1405. }
  1406. ubi_err("failed to erase PEB %d, error %d", pnum, err);
  1407. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  1408. err == -EBUSY) {
  1409. if (retry < 4)
  1410. goto retry_erase;
  1411. else
  1412. goto out_ro;
  1413. }
  1414. kmem_cache_free(ubi_wl_entry_slab, e);
  1415. if (err != -EIO)
  1416. /*
  1417. * If this is not %-EIO, we have no idea what to do. Scheduling
  1418. * this physical eraseblock for erasure again would cause
  1419. * errors again and again. Well, lets switch to R/O mode.
  1420. */
  1421. goto out_ro;
  1422. /* It is %-EIO, the PEB went bad */
  1423. if (!ubi->bad_allowed) {
  1424. ubi_err("bad physical eraseblock %d detected", pnum);
  1425. goto out_ro;
  1426. }
  1427. spin_lock(&ubi->volumes_lock);
  1428. need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
  1429. if (need > 0) {
  1430. need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
  1431. ubi->avail_pebs -= need;
  1432. ubi->rsvd_pebs += need;
  1433. ubi->beb_rsvd_pebs += need;
  1434. if (need > 0)
  1435. dbg_wl("reserve more %d PEBs", need);
  1436. }
  1437. if (ubi->beb_rsvd_pebs == 0) {
  1438. spin_unlock(&ubi->volumes_lock);
  1439. ubi_err("no reserved physical eraseblocks");
  1440. goto out_ro;
  1441. }
  1442. spin_unlock(&ubi->volumes_lock);
  1443. dbg_wl("mark PEB %d as bad", pnum);
  1444. err = ubi_io_mark_bad(ubi, pnum);
  1445. if (err)
  1446. goto out_ro;
  1447. spin_lock(&ubi->volumes_lock);
  1448. ubi->beb_rsvd_pebs -= 1;
  1449. ubi->bad_peb_count += 1;
  1450. ubi->good_peb_count -= 1;
  1451. ubi_calculate_reserved(ubi);
  1452. if (ubi->beb_rsvd_pebs)
  1453. dbg_wl("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
  1454. else
  1455. ubi_warn("last PEB from the reserved pool was used");
  1456. spin_unlock(&ubi->volumes_lock);
  1457. return err;
  1458. out_ro:
  1459. ubi_ro_mode(ubi);
  1460. return err;
  1461. }
  1462. /**
  1463. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1464. * @ubi: UBI device description object
  1465. * @vol_id: the volume ID that last used this PEB
  1466. * @lnum: the last used logical eraseblock number for the PEB
  1467. * @pnum: physical eraseblock to return
  1468. * @torture: if this physical eraseblock has to be tortured
  1469. *
  1470. * This function is called to return physical eraseblock @pnum to the pool of
  1471. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1472. * occurred to this @pnum and it has to be tested. This function returns zero
  1473. * in case of success, and a negative error code in case of failure.
  1474. */
  1475. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1476. int pnum, int torture)
  1477. {
  1478. int err;
  1479. struct ubi_wl_entry *e;
  1480. dbg_wl("PEB %d", pnum);
  1481. ubi_assert(pnum >= 0);
  1482. ubi_assert(pnum < ubi->peb_count);
  1483. retry:
  1484. spin_lock(&ubi->wl_lock);
  1485. e = ubi->lookuptbl[pnum];
  1486. if (e == ubi->move_from) {
  1487. /*
  1488. * User is putting the physical eraseblock which was selected to
  1489. * be moved. It will be scheduled for erasure in the
  1490. * wear-leveling worker.
  1491. */
  1492. dbg_wl("PEB %d is being moved, wait", pnum);
  1493. spin_unlock(&ubi->wl_lock);
  1494. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1495. mutex_lock(&ubi->move_mutex);
  1496. mutex_unlock(&ubi->move_mutex);
  1497. goto retry;
  1498. } else if (e == ubi->move_to) {
  1499. /*
  1500. * User is putting the physical eraseblock which was selected
  1501. * as the target the data is moved to. It may happen if the EBA
  1502. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1503. * but the WL sub-system has not put the PEB to the "used" tree
  1504. * yet, but it is about to do this. So we just set a flag which
  1505. * will tell the WL worker that the PEB is not needed anymore
  1506. * and should be scheduled for erasure.
  1507. */
  1508. dbg_wl("PEB %d is the target of data moving", pnum);
  1509. ubi_assert(!ubi->move_to_put);
  1510. ubi->move_to_put = 1;
  1511. spin_unlock(&ubi->wl_lock);
  1512. return 0;
  1513. }
  1514. if (in_wl_tree(e, &ubi->used)) {
  1515. self_check_in_wl_tree(ubi, e, &ubi->used);
  1516. rb_erase(&e->u.rb, &ubi->used);
  1517. } else if (in_wl_tree(e, &ubi->scrub)) {
  1518. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1519. rb_erase(&e->u.rb, &ubi->scrub);
  1520. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1521. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1522. rb_erase(&e->u.rb, &ubi->erroneous);
  1523. ubi->erroneous_peb_count -= 1;
  1524. ubi_assert(ubi->erroneous_peb_count >= 0);
  1525. /* Erroneous PEBs should be tortured */
  1526. torture = 1;
  1527. } else {
  1528. err = prot_queue_del(ubi, e->pnum);
  1529. if (err) {
  1530. ubi_err("PEB %d not found", pnum);
  1531. ubi_ro_mode(ubi);
  1532. spin_unlock(&ubi->wl_lock);
  1533. return err;
  1534. }
  1535. }
  1536. spin_unlock(&ubi->wl_lock);
  1537. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1538. if (err) {
  1539. spin_lock(&ubi->wl_lock);
  1540. wl_tree_add(ubi, e, &ubi->used);
  1541. spin_unlock(&ubi->wl_lock);
  1542. }
  1543. return err;
  1544. }
  1545. /**
  1546. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1547. * @ubi: UBI device description object
  1548. * @pnum: the physical eraseblock to schedule
  1549. *
  1550. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1551. * needs scrubbing. This function schedules a physical eraseblock for
  1552. * scrubbing which is done in background. This function returns zero in case of
  1553. * success and a negative error code in case of failure.
  1554. */
  1555. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1556. {
  1557. struct ubi_wl_entry *e;
  1558. dbg_wl("schedule PEB %d for scrubbing", pnum);
  1559. retry:
  1560. spin_lock(&ubi->wl_lock);
  1561. e = ubi->lookuptbl[pnum];
  1562. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1563. in_wl_tree(e, &ubi->erroneous)) {
  1564. spin_unlock(&ubi->wl_lock);
  1565. return 0;
  1566. }
  1567. if (e == ubi->move_to) {
  1568. /*
  1569. * This physical eraseblock was used to move data to. The data
  1570. * was moved but the PEB was not yet inserted to the proper
  1571. * tree. We should just wait a little and let the WL worker
  1572. * proceed.
  1573. */
  1574. spin_unlock(&ubi->wl_lock);
  1575. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1576. /* yield(); */
  1577. goto retry;
  1578. }
  1579. if (in_wl_tree(e, &ubi->used)) {
  1580. self_check_in_wl_tree(ubi, e, &ubi->used);
  1581. rb_erase(&e->u.rb, &ubi->used);
  1582. } else {
  1583. int err;
  1584. err = prot_queue_del(ubi, e->pnum);
  1585. if (err) {
  1586. ubi_err("PEB %d not found", pnum);
  1587. ubi_ro_mode(ubi);
  1588. spin_unlock(&ubi->wl_lock);
  1589. return err;
  1590. }
  1591. }
  1592. wl_tree_add(ubi, e, &ubi->scrub);
  1593. spin_unlock(&ubi->wl_lock);
  1594. /*
  1595. * Technically scrubbing is the same as wear-leveling, so it is done
  1596. * by the WL worker.
  1597. */
  1598. return ensure_wear_leveling(ubi, 0);
  1599. }
  1600. /**
  1601. * ubi_wl_flush - flush all pending works.
  1602. * @ubi: UBI device description object
  1603. * @vol_id: the volume id to flush for
  1604. * @lnum: the logical eraseblock number to flush for
  1605. *
  1606. * This function executes all pending works for a particular volume id /
  1607. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1608. * acts as a wildcard for all of the corresponding volume numbers or logical
  1609. * eraseblock numbers. It returns zero in case of success and a negative error
  1610. * code in case of failure.
  1611. */
  1612. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1613. {
  1614. int err = 0;
  1615. int found = 1;
  1616. /*
  1617. * Erase while the pending works queue is not empty, but not more than
  1618. * the number of currently pending works.
  1619. */
  1620. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1621. vol_id, lnum, ubi->works_count);
  1622. while (found) {
  1623. struct ubi_work *wrk, *tmp;
  1624. found = 0;
  1625. down_read(&ubi->work_sem);
  1626. spin_lock(&ubi->wl_lock);
  1627. list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
  1628. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1629. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1630. list_del(&wrk->list);
  1631. ubi->works_count -= 1;
  1632. ubi_assert(ubi->works_count >= 0);
  1633. spin_unlock(&ubi->wl_lock);
  1634. err = wrk->func(ubi, wrk, 0);
  1635. if (err) {
  1636. up_read(&ubi->work_sem);
  1637. return err;
  1638. }
  1639. spin_lock(&ubi->wl_lock);
  1640. found = 1;
  1641. break;
  1642. }
  1643. }
  1644. spin_unlock(&ubi->wl_lock);
  1645. up_read(&ubi->work_sem);
  1646. }
  1647. /*
  1648. * Make sure all the works which have been done in parallel are
  1649. * finished.
  1650. */
  1651. down_write(&ubi->work_sem);
  1652. up_write(&ubi->work_sem);
  1653. return err;
  1654. }
  1655. /**
  1656. * tree_destroy - destroy an RB-tree.
  1657. * @root: the root of the tree to destroy
  1658. */
  1659. static void tree_destroy(struct rb_root *root)
  1660. {
  1661. struct rb_node *rb;
  1662. struct ubi_wl_entry *e;
  1663. rb = root->rb_node;
  1664. while (rb) {
  1665. if (rb->rb_left)
  1666. rb = rb->rb_left;
  1667. else if (rb->rb_right)
  1668. rb = rb->rb_right;
  1669. else {
  1670. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1671. rb = rb_parent(rb);
  1672. if (rb) {
  1673. if (rb->rb_left == &e->u.rb)
  1674. rb->rb_left = NULL;
  1675. else
  1676. rb->rb_right = NULL;
  1677. }
  1678. kmem_cache_free(ubi_wl_entry_slab, e);
  1679. }
  1680. }
  1681. }
  1682. /**
  1683. * ubi_thread - UBI background thread.
  1684. * @u: the UBI device description object pointer
  1685. */
  1686. int ubi_thread(void *u)
  1687. {
  1688. int failures = 0;
  1689. struct ubi_device *ubi = u;
  1690. ubi_msg("background thread \"%s\" started, PID %d",
  1691. ubi->bgt_name, task_pid_nr(current));
  1692. set_freezable();
  1693. for (;;) {
  1694. int err;
  1695. if (kthread_should_stop())
  1696. break;
  1697. if (try_to_freeze())
  1698. continue;
  1699. spin_lock(&ubi->wl_lock);
  1700. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1701. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1702. set_current_state(TASK_INTERRUPTIBLE);
  1703. spin_unlock(&ubi->wl_lock);
  1704. schedule();
  1705. continue;
  1706. }
  1707. spin_unlock(&ubi->wl_lock);
  1708. err = do_work(ubi);
  1709. if (err) {
  1710. ubi_err("%s: work failed with error code %d",
  1711. ubi->bgt_name, err);
  1712. if (failures++ > WL_MAX_FAILURES) {
  1713. /*
  1714. * Too many failures, disable the thread and
  1715. * switch to read-only mode.
  1716. */
  1717. ubi_msg("%s: %d consecutive failures",
  1718. ubi->bgt_name, WL_MAX_FAILURES);
  1719. ubi_ro_mode(ubi);
  1720. ubi->thread_enabled = 0;
  1721. continue;
  1722. }
  1723. } else
  1724. failures = 0;
  1725. cond_resched();
  1726. }
  1727. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1728. return 0;
  1729. }
  1730. /**
  1731. * shutdown_work - shutdown all pending works.
  1732. * @ubi: UBI device description object
  1733. */
  1734. static void shutdown_work(struct ubi_device *ubi)
  1735. {
  1736. while (!list_empty(&ubi->works)) {
  1737. struct ubi_work *wrk;
  1738. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1739. list_del(&wrk->list);
  1740. wrk->func(ubi, wrk, 1);
  1741. ubi->works_count -= 1;
  1742. ubi_assert(ubi->works_count >= 0);
  1743. }
  1744. }
  1745. /**
  1746. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1747. * @ubi: UBI device description object
  1748. * @ai: attaching information
  1749. *
  1750. * This function returns zero in case of success, and a negative error code in
  1751. * case of failure.
  1752. */
  1753. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1754. {
  1755. int err, i, reserved_pebs, found_pebs = 0;
  1756. struct rb_node *rb1, *rb2;
  1757. struct ubi_ainf_volume *av;
  1758. struct ubi_ainf_peb *aeb, *tmp;
  1759. struct ubi_wl_entry *e;
  1760. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1761. spin_lock_init(&ubi->wl_lock);
  1762. mutex_init(&ubi->move_mutex);
  1763. init_rwsem(&ubi->work_sem);
  1764. ubi->max_ec = ai->max_ec;
  1765. INIT_LIST_HEAD(&ubi->works);
  1766. #ifdef CONFIG_MTD_UBI_FASTMAP
  1767. INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
  1768. #endif
  1769. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1770. err = -ENOMEM;
  1771. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1772. if (!ubi->lookuptbl)
  1773. return err;
  1774. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1775. INIT_LIST_HEAD(&ubi->pq[i]);
  1776. ubi->pq_head = 0;
  1777. ubi->free_count = 0;
  1778. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1779. cond_resched();
  1780. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1781. if (!e)
  1782. goto out_free;
  1783. e->pnum = aeb->pnum;
  1784. e->ec = aeb->ec;
  1785. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1786. ubi->lookuptbl[e->pnum] = e;
  1787. #if 1
  1788. if (!ubi->ro_mode) {
  1789. if (ubi_erase_peb(ubi, e, 0)) {
  1790. kmem_cache_free(ubi_wl_entry_slab, e);
  1791. goto out_free;
  1792. }
  1793. }
  1794. #else
  1795. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1796. kmem_cache_free(ubi_wl_entry_slab, e);
  1797. goto out_free;
  1798. }
  1799. #endif
  1800. found_pebs++;
  1801. }
  1802. list_for_each_entry(aeb, &ai->free, u.list) {
  1803. cond_resched();
  1804. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1805. if (!e)
  1806. goto out_free;
  1807. e->pnum = aeb->pnum;
  1808. e->ec = aeb->ec;
  1809. ubi_assert(e->ec >= 0);
  1810. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1811. {
  1812. wl_tree_add(ubi, e, &ubi->free);
  1813. ubi->free_count++;
  1814. }
  1815. ubi->lookuptbl[e->pnum] = e;
  1816. found_pebs++;
  1817. }
  1818. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1819. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1820. cond_resched();
  1821. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1822. if (!e)
  1823. goto out_free;
  1824. e->pnum = aeb->pnum;
  1825. e->ec = aeb->ec;
  1826. ubi->lookuptbl[e->pnum] = e;
  1827. if (!aeb->scrub) {
  1828. dbg_wl("add PEB %d EC %d to the used tree",
  1829. e->pnum, e->ec);
  1830. wl_tree_add(ubi, e, &ubi->used);
  1831. } else {
  1832. dbg_wl("add PEB %d EC %d to the scrub tree",
  1833. e->pnum, e->ec);
  1834. wl_tree_add(ubi, e, &ubi->scrub);
  1835. }
  1836. found_pebs++;
  1837. }
  1838. }
  1839. dbg_wl("found %i PEBs", found_pebs);
  1840. if (ubi->fm)
  1841. ubi_assert(ubi->good_peb_count == found_pebs + ubi->fm->used_blocks);
  1842. else
  1843. ubi_assert(ubi->good_peb_count == found_pebs);
  1844. reserved_pebs = WL_RESERVED_PEBS;
  1845. #ifdef CONFIG_MTD_UBI_FASTMAP
  1846. /* Reserve enough LEBs to store two fastmaps. */
  1847. reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
  1848. #endif
  1849. if (ubi->avail_pebs < reserved_pebs) {
  1850. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1851. ubi->avail_pebs, reserved_pebs);
  1852. if (ubi->corr_peb_count)
  1853. ubi_err("%d PEBs are corrupted and not used",
  1854. ubi->corr_peb_count);
  1855. goto out_free;
  1856. }
  1857. ubi->avail_pebs -= reserved_pebs;
  1858. ubi->rsvd_pebs += reserved_pebs;
  1859. /* Schedule wear-leveling if needed */
  1860. err = ensure_wear_leveling(ubi, 0);
  1861. if (err)
  1862. goto out_free;
  1863. return 0;
  1864. out_free:
  1865. shutdown_work(ubi);
  1866. tree_destroy(&ubi->used);
  1867. tree_destroy(&ubi->free);
  1868. tree_destroy(&ubi->scrub);
  1869. kfree(ubi->lookuptbl);
  1870. return err;
  1871. }
  1872. /**
  1873. * protection_queue_destroy - destroy the protection queue.
  1874. * @ubi: UBI device description object
  1875. */
  1876. static void protection_queue_destroy(struct ubi_device *ubi)
  1877. {
  1878. int i;
  1879. struct ubi_wl_entry *e, *tmp;
  1880. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1881. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1882. list_del(&e->u.list);
  1883. kmem_cache_free(ubi_wl_entry_slab, e);
  1884. }
  1885. }
  1886. }
  1887. /**
  1888. * ubi_wl_close - close the wear-leveling sub-system.
  1889. * @ubi: UBI device description object
  1890. */
  1891. void ubi_wl_close(struct ubi_device *ubi)
  1892. {
  1893. dbg_wl("close the WL sub-system");
  1894. shutdown_work(ubi);
  1895. protection_queue_destroy(ubi);
  1896. tree_destroy(&ubi->used);
  1897. tree_destroy(&ubi->erroneous);
  1898. tree_destroy(&ubi->free);
  1899. tree_destroy(&ubi->scrub);
  1900. kfree(ubi->lookuptbl);
  1901. }
  1902. #ifdef CONFIG_MTK_HIBERNATION
  1903. void ubi_wl_move_pg_to_used(struct ubi_device *ubi, int pnum)
  1904. {
  1905. struct ubi_wl_entry *e;
  1906. e = ubi->lookuptbl[pnum];
  1907. if (in_wl_tree(e, &ubi->used) == 0) {
  1908. prot_queue_del(ubi, e->pnum);
  1909. wl_tree_add(ubi, e, &ubi->used);
  1910. }
  1911. }
  1912. #endif
  1913. /**
  1914. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1915. * @ubi: UBI device description object
  1916. * @pnum: the physical eraseblock number to check
  1917. * @ec: the erase counter to check
  1918. *
  1919. * This function returns zero if the erase counter of physical eraseblock @pnum
  1920. * is equivalent to @ec, and a negative error code if not or if an error
  1921. * occurred.
  1922. */
  1923. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1924. {
  1925. int err;
  1926. long long read_ec;
  1927. struct ubi_ec_hdr *ec_hdr;
  1928. if (!ubi_dbg_chk_gen(ubi))
  1929. return 0;
  1930. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1931. if (!ec_hdr)
  1932. return -ENOMEM;
  1933. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1934. if (err && err != UBI_IO_BITFLIPS) {
  1935. /* The header does not have to exist */
  1936. err = 0;
  1937. goto out_free;
  1938. }
  1939. read_ec = be64_to_cpu(ec_hdr->ec);
  1940. if (ec != read_ec && read_ec - ec > 1) {
  1941. ubi_err("self-check failed for PEB %d", pnum);
  1942. ubi_err("read EC is %lld, should be %d", read_ec, ec);
  1943. dump_stack();
  1944. err = 1;
  1945. } else
  1946. err = 0;
  1947. out_free:
  1948. kfree(ec_hdr);
  1949. return err;
  1950. }
  1951. /**
  1952. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1953. * @ubi: UBI device description object
  1954. * @e: the wear-leveling entry to check
  1955. * @root: the root of the tree
  1956. *
  1957. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1958. * is not.
  1959. */
  1960. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1961. struct ubi_wl_entry *e, struct rb_root *root)
  1962. {
  1963. if (!ubi_dbg_chk_gen(ubi))
  1964. return 0;
  1965. if (in_wl_tree(e, root))
  1966. return 0;
  1967. ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
  1968. e->pnum, e->ec, root);
  1969. dump_stack();
  1970. return -EINVAL;
  1971. }
  1972. /**
  1973. * self_check_in_pq - check if wear-leveling entry is in the protection
  1974. * queue.
  1975. * @ubi: UBI device description object
  1976. * @e: the wear-leveling entry to check
  1977. *
  1978. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1979. */
  1980. static int self_check_in_pq(const struct ubi_device *ubi,
  1981. struct ubi_wl_entry *e)
  1982. {
  1983. struct ubi_wl_entry *p;
  1984. int i;
  1985. if (!ubi_dbg_chk_gen(ubi))
  1986. return 0;
  1987. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1988. list_for_each_entry(p, &ubi->pq[i], u.list)
  1989. if (p == e)
  1990. return 0;
  1991. ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
  1992. e->pnum, e->ec);
  1993. dump_stack();
  1994. return -EINVAL;
  1995. }