wl.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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_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(ubi, "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. dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
  311. }
  312. /**
  313. * find_wl_entry - find wear-leveling entry closest to certain erase counter.
  314. * @ubi: UBI device description object
  315. * @root: the RB-tree where to look for
  316. * @diff: maximum possible difference from the smallest erase counter
  317. *
  318. * This function looks for a wear leveling entry with erase counter closest to
  319. * min + @diff, where min is the smallest erase counter.
  320. */
  321. static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
  322. struct rb_root *root, int diff)
  323. {
  324. struct rb_node *p;
  325. struct ubi_wl_entry *e, *prev_e = NULL;
  326. int max;
  327. e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  328. max = e->ec + diff;
  329. p = root->rb_node;
  330. while (p) {
  331. struct ubi_wl_entry *e1;
  332. e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
  333. if (e1->ec >= max)
  334. p = p->rb_left;
  335. else {
  336. p = p->rb_right;
  337. prev_e = e;
  338. e = e1;
  339. }
  340. }
  341. /* If no fastmap has been written and this WL entry can be used
  342. * as anchor PEB, hold it back and return the second best WL entry
  343. * such that fastmap can use the anchor PEB later. */
  344. if (prev_e && !ubi->fm_disabled &&
  345. !ubi->fm && e->pnum < UBI_FM_MAX_START)
  346. return prev_e;
  347. return e;
  348. }
  349. /**
  350. * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
  351. * @ubi: UBI device description object
  352. * @root: the RB-tree where to look for
  353. *
  354. * This function looks for a wear leveling entry with medium erase counter,
  355. * but not greater or equivalent than the lowest erase counter plus
  356. * %WL_FREE_MAX_DIFF/2.
  357. */
  358. static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
  359. struct rb_root *root)
  360. {
  361. struct ubi_wl_entry *e, *first, *last;
  362. first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
  363. last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
  364. if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
  365. e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
  366. #ifdef CONFIG_MTD_UBI_FASTMAP
  367. /* If no fastmap has been written and this WL entry can be used
  368. * as anchor PEB, hold it back and return the second best
  369. * WL entry such that fastmap can use the anchor PEB later. */
  370. if (e && !ubi->fm_disabled && !ubi->fm &&
  371. e->pnum < UBI_FM_MAX_START)
  372. e = rb_entry(rb_next(root->rb_node),
  373. struct ubi_wl_entry, u.rb);
  374. #endif
  375. } else
  376. e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
  377. return e;
  378. }
  379. #ifdef CONFIG_MTD_UBI_FASTMAP
  380. /**
  381. * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
  382. * @root: the RB-tree where to look for
  383. */
  384. static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
  385. {
  386. struct rb_node *p;
  387. struct ubi_wl_entry *e, *victim = NULL;
  388. int max_ec = UBI_MAX_ERASECOUNTER;
  389. ubi_rb_for_each_entry(p, e, root, u.rb) {
  390. if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
  391. victim = e;
  392. max_ec = e->ec;
  393. }
  394. }
  395. return victim;
  396. }
  397. static int anchor_pebs_avalible(struct rb_root *root)
  398. {
  399. struct rb_node *p;
  400. struct ubi_wl_entry *e;
  401. ubi_rb_for_each_entry(p, e, root, u.rb)
  402. if (e->pnum < UBI_FM_MAX_START)
  403. return 1;
  404. return 0;
  405. }
  406. /**
  407. * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
  408. * @ubi: UBI device description object
  409. * @anchor: This PEB will be used as anchor PEB by fastmap
  410. *
  411. * The function returns a physical erase block with a given maximal number
  412. * and removes it from the wl subsystem.
  413. * Must be called with wl_lock held!
  414. */
  415. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
  416. {
  417. struct ubi_wl_entry *e = NULL;
  418. if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
  419. goto out;
  420. if (anchor)
  421. e = find_anchor_wl_entry(&ubi->free);
  422. else
  423. e = find_mean_wl_entry(ubi, &ubi->free);
  424. if (!e)
  425. goto out;
  426. self_check_in_wl_tree(ubi, e, &ubi->free);
  427. /* remove it from the free list,
  428. * the wl subsystem does no longer know this erase block */
  429. rb_erase(&e->u.rb, &ubi->free);
  430. ubi->free_count--;
  431. out:
  432. return e;
  433. }
  434. #endif
  435. /**
  436. * __wl_get_peb - get a physical eraseblock.
  437. * @ubi: UBI device description object
  438. *
  439. * This function returns a physical eraseblock in case of success and a
  440. * negative error code in case of failure.
  441. */
  442. static int __wl_get_peb(struct ubi_device *ubi)
  443. {
  444. int err;
  445. struct ubi_wl_entry *e;
  446. retry:
  447. if (!ubi->free.rb_node) {
  448. if (ubi->works_count == 0) {
  449. ubi_err(ubi, "no free eraseblocks");
  450. ubi_assert(list_empty(&ubi->works));
  451. return -ENOSPC;
  452. }
  453. err = produce_free_peb(ubi);
  454. if (err < 0)
  455. return err;
  456. goto retry;
  457. }
  458. e = find_mean_wl_entry(ubi, &ubi->free);
  459. if (!e) {
  460. ubi_err(ubi, "no free eraseblocks");
  461. return -ENOSPC;
  462. }
  463. self_check_in_wl_tree(ubi, e, &ubi->free);
  464. /*
  465. * Move the physical eraseblock to the protection queue where it will
  466. * be protected from being moved for some time.
  467. */
  468. rb_erase(&e->u.rb, &ubi->free);
  469. ubi->free_count--;
  470. dbg_wl("PEB %d EC %d", e->pnum, e->ec);
  471. #ifndef CONFIG_MTD_UBI_FASTMAP
  472. /* We have to enqueue e only if fastmap is disabled,
  473. * is fastmap enabled prot_queue_add() will be called by
  474. * ubi_wl_get_peb() after removing e from the pool. */
  475. prot_queue_add(ubi, e);
  476. #endif
  477. return e->pnum;
  478. }
  479. #ifdef CONFIG_MTD_UBI_FASTMAP
  480. /**
  481. * return_unused_pool_pebs - returns unused PEB to the free tree.
  482. * @ubi: UBI device description object
  483. * @pool: fastmap pool description object
  484. */
  485. static void return_unused_pool_pebs(struct ubi_device *ubi,
  486. struct ubi_fm_pool *pool)
  487. {
  488. int i;
  489. struct ubi_wl_entry *e;
  490. for (i = pool->used; i < pool->size; i++) {
  491. e = ubi->lookuptbl[pool->pebs[i]];
  492. wl_tree_add(e, &ubi->free);
  493. ubi->free_count++;
  494. }
  495. }
  496. /**
  497. * refill_wl_pool - refills all the fastmap pool used by the
  498. * WL sub-system.
  499. * @ubi: UBI device description object
  500. */
  501. static void refill_wl_pool(struct ubi_device *ubi)
  502. {
  503. struct ubi_wl_entry *e;
  504. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  505. return_unused_pool_pebs(ubi, pool);
  506. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  507. if (!ubi->free.rb_node ||
  508. (ubi->free_count - ubi->beb_rsvd_pebs < 5))
  509. break;
  510. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  511. self_check_in_wl_tree(ubi, e, &ubi->free);
  512. rb_erase(&e->u.rb, &ubi->free);
  513. ubi->free_count--;
  514. pool->pebs[pool->size] = e->pnum;
  515. }
  516. pool->used = 0;
  517. }
  518. /**
  519. * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
  520. * @ubi: UBI device description object
  521. */
  522. static void refill_wl_user_pool(struct ubi_device *ubi)
  523. {
  524. struct ubi_fm_pool *pool = &ubi->fm_pool;
  525. return_unused_pool_pebs(ubi, pool);
  526. for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
  527. pool->pebs[pool->size] = __wl_get_peb(ubi);
  528. if (pool->pebs[pool->size] < 0)
  529. break;
  530. }
  531. pool->used = 0;
  532. }
  533. /**
  534. * ubi_refill_pools - refills all fastmap PEB pools.
  535. * @ubi: UBI device description object
  536. */
  537. void ubi_refill_pools(struct ubi_device *ubi)
  538. {
  539. spin_lock(&ubi->wl_lock);
  540. refill_wl_pool(ubi);
  541. refill_wl_user_pool(ubi);
  542. spin_unlock(&ubi->wl_lock);
  543. }
  544. /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
  545. * the fastmap pool.
  546. */
  547. int ubi_wl_get_peb(struct ubi_device *ubi)
  548. {
  549. int ret;
  550. struct ubi_fm_pool *pool = &ubi->fm_pool;
  551. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  552. if (!pool->size || !wl_pool->size || pool->used == pool->size ||
  553. wl_pool->used == wl_pool->size)
  554. ubi_update_fastmap(ubi);
  555. /* we got not a single free PEB */
  556. if (!pool->size)
  557. ret = -ENOSPC;
  558. else {
  559. spin_lock(&ubi->wl_lock);
  560. ret = pool->pebs[pool->used++];
  561. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  562. spin_unlock(&ubi->wl_lock);
  563. }
  564. return ret;
  565. }
  566. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  567. *
  568. * @ubi: UBI device description object
  569. */
  570. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  571. {
  572. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  573. int pnum;
  574. if (pool->used == pool->size || !pool->size) {
  575. /* We cannot update the fastmap here because this
  576. * function is called in atomic context.
  577. * Let's fail here and refill/update it as soon as possible. */
  578. schedule_work(&ubi->fm_work);
  579. return NULL;
  580. } else {
  581. pnum = pool->pebs[pool->used++];
  582. return ubi->lookuptbl[pnum];
  583. }
  584. }
  585. #else
  586. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  587. {
  588. struct ubi_wl_entry *e;
  589. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  590. self_check_in_wl_tree(ubi, e, &ubi->free);
  591. ubi->free_count--;
  592. ubi_assert(ubi->free_count >= 0);
  593. rb_erase(&e->u.rb, &ubi->free);
  594. return e;
  595. }
  596. int ubi_wl_get_peb(struct ubi_device *ubi)
  597. {
  598. int peb, err;
  599. spin_lock(&ubi->wl_lock);
  600. peb = __wl_get_peb(ubi);
  601. spin_unlock(&ubi->wl_lock);
  602. if (peb < 0)
  603. return peb;
  604. err = ubi_self_check_all_ff(ubi, peb, ubi->vid_hdr_aloffset,
  605. ubi->peb_size - ubi->vid_hdr_aloffset);
  606. if (err) {
  607. ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes",
  608. peb);
  609. return err;
  610. }
  611. return peb;
  612. }
  613. #endif
  614. /**
  615. * prot_queue_del - remove a physical eraseblock from the protection queue.
  616. * @ubi: UBI device description object
  617. * @pnum: the physical eraseblock to remove
  618. *
  619. * This function deletes PEB @pnum from the protection queue and returns zero
  620. * in case of success and %-ENODEV if the PEB was not found.
  621. */
  622. static int prot_queue_del(struct ubi_device *ubi, int pnum)
  623. {
  624. struct ubi_wl_entry *e;
  625. e = ubi->lookuptbl[pnum];
  626. if (!e)
  627. return -ENODEV;
  628. if (self_check_in_pq(ubi, e))
  629. return -ENODEV;
  630. list_del(&e->u.list);
  631. dbg_wl("deleted PEB %d from the protection queue", e->pnum);
  632. return 0;
  633. }
  634. /**
  635. * sync_erase - synchronously erase a physical eraseblock.
  636. * @ubi: UBI device description object
  637. * @e: the the physical eraseblock to erase
  638. * @torture: if the physical eraseblock has to be tortured
  639. *
  640. * This function returns zero in case of success and a negative error code in
  641. * case of failure.
  642. */
  643. static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  644. int torture)
  645. {
  646. int err;
  647. struct ubi_ec_hdr *ec_hdr;
  648. unsigned long long ec = e->ec;
  649. dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
  650. err = self_check_ec(ubi, e->pnum, e->ec);
  651. if (err)
  652. return -EINVAL;
  653. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  654. if (!ec_hdr)
  655. return -ENOMEM;
  656. err = ubi_io_sync_erase(ubi, e->pnum, torture);
  657. if (err < 0)
  658. goto out_free;
  659. ec += err;
  660. if (ec > UBI_MAX_ERASECOUNTER) {
  661. /*
  662. * Erase counter overflow. Upgrade UBI and use 64-bit
  663. * erase counters internally.
  664. */
  665. ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
  666. e->pnum, ec);
  667. err = -EINVAL;
  668. goto out_free;
  669. }
  670. dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
  671. ec_hdr->ec = cpu_to_be64(ec);
  672. err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
  673. if (err)
  674. goto out_free;
  675. e->ec = ec;
  676. spin_lock(&ubi->wl_lock);
  677. if (e->ec > ubi->max_ec)
  678. ubi->max_ec = e->ec;
  679. spin_unlock(&ubi->wl_lock);
  680. out_free:
  681. kfree(ec_hdr);
  682. return err;
  683. }
  684. /**
  685. * serve_prot_queue - check if it is time to stop protecting PEBs.
  686. * @ubi: UBI device description object
  687. *
  688. * This function is called after each erase operation and removes PEBs from the
  689. * tail of the protection queue. These PEBs have been protected for long enough
  690. * and should be moved to the used tree.
  691. */
  692. static void serve_prot_queue(struct ubi_device *ubi)
  693. {
  694. struct ubi_wl_entry *e, *tmp;
  695. int count;
  696. /*
  697. * There may be several protected physical eraseblock to remove,
  698. * process them all.
  699. */
  700. repeat:
  701. count = 0;
  702. spin_lock(&ubi->wl_lock);
  703. list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
  704. dbg_wl("PEB %d EC %d protection over, move to used tree",
  705. e->pnum, e->ec);
  706. list_del(&e->u.list);
  707. wl_tree_add(e, &ubi->used);
  708. if (count++ > 32) {
  709. /*
  710. * Let's be nice and avoid holding the spinlock for
  711. * too long.
  712. */
  713. spin_unlock(&ubi->wl_lock);
  714. cond_resched();
  715. goto repeat;
  716. }
  717. }
  718. ubi->pq_head += 1;
  719. if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
  720. ubi->pq_head = 0;
  721. ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
  722. spin_unlock(&ubi->wl_lock);
  723. }
  724. /**
  725. * __schedule_ubi_work - schedule a work.
  726. * @ubi: UBI device description object
  727. * @wrk: the work to schedule
  728. *
  729. * This function adds a work defined by @wrk to the tail of the pending works
  730. * list. Can only be used if ubi->work_sem is already held in read mode!
  731. */
  732. static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  733. {
  734. spin_lock(&ubi->wl_lock);
  735. list_add_tail(&wrk->list, &ubi->works);
  736. ubi_assert(ubi->works_count >= 0);
  737. ubi->works_count += 1;
  738. if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
  739. wake_up_process(ubi->bgt_thread);
  740. spin_unlock(&ubi->wl_lock);
  741. }
  742. /**
  743. * schedule_ubi_work - schedule a work.
  744. * @ubi: UBI device description object
  745. * @wrk: the work to schedule
  746. *
  747. * This function adds a work defined by @wrk to the tail of the pending works
  748. * list.
  749. */
  750. static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
  751. {
  752. down_read(&ubi->work_sem);
  753. __schedule_ubi_work(ubi, wrk);
  754. up_read(&ubi->work_sem);
  755. }
  756. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  757. int shutdown);
  758. #ifdef CONFIG_MTD_UBI_FASTMAP
  759. /**
  760. * ubi_is_erase_work - checks whether a work is erase work.
  761. * @wrk: The work object to be checked
  762. */
  763. int ubi_is_erase_work(struct ubi_work *wrk)
  764. {
  765. return wrk->func == erase_worker;
  766. }
  767. #endif
  768. /**
  769. * schedule_erase - schedule an erase work.
  770. * @ubi: UBI device description object
  771. * @e: the WL entry of the physical eraseblock to erase
  772. * @vol_id: the volume ID that last used this PEB
  773. * @lnum: the last used logical eraseblock number for the PEB
  774. * @torture: if the physical eraseblock has to be tortured
  775. *
  776. * This function returns zero in case of success and a %-ENOMEM in case of
  777. * failure.
  778. */
  779. static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  780. int vol_id, int lnum, int torture)
  781. {
  782. struct ubi_work *wl_wrk;
  783. ubi_assert(e);
  784. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  785. dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
  786. e->pnum, e->ec, torture);
  787. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  788. if (!wl_wrk)
  789. return -ENOMEM;
  790. wl_wrk->func = &erase_worker;
  791. wl_wrk->e = e;
  792. wl_wrk->vol_id = vol_id;
  793. wl_wrk->lnum = lnum;
  794. wl_wrk->torture = torture;
  795. schedule_ubi_work(ubi, wl_wrk);
  796. return 0;
  797. }
  798. /**
  799. * do_sync_erase - run the erase worker synchronously.
  800. * @ubi: UBI device description object
  801. * @e: the WL entry of the physical eraseblock to erase
  802. * @vol_id: the volume ID that last used this PEB
  803. * @lnum: the last used logical eraseblock number for the PEB
  804. * @torture: if the physical eraseblock has to be tortured
  805. *
  806. */
  807. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  808. int vol_id, int lnum, int torture)
  809. {
  810. struct ubi_work *wl_wrk;
  811. dbg_wl("sync erase of PEB %i", e->pnum);
  812. wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  813. if (!wl_wrk)
  814. return -ENOMEM;
  815. wl_wrk->e = e;
  816. wl_wrk->vol_id = vol_id;
  817. wl_wrk->lnum = lnum;
  818. wl_wrk->torture = torture;
  819. return erase_worker(ubi, wl_wrk, 0);
  820. }
  821. #ifdef CONFIG_MTD_UBI_FASTMAP
  822. /**
  823. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  824. * sub-system.
  825. * see: ubi_wl_put_peb()
  826. *
  827. * @ubi: UBI device description object
  828. * @fm_e: physical eraseblock to return
  829. * @lnum: the last used logical eraseblock number for the PEB
  830. * @torture: if this physical eraseblock has to be tortured
  831. */
  832. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  833. int lnum, int torture)
  834. {
  835. struct ubi_wl_entry *e;
  836. int vol_id, pnum = fm_e->pnum;
  837. dbg_wl("PEB %d", pnum);
  838. ubi_assert(pnum >= 0);
  839. ubi_assert(pnum < ubi->peb_count);
  840. spin_lock(&ubi->wl_lock);
  841. e = ubi->lookuptbl[pnum];
  842. /* This can happen if we recovered from a fastmap the very
  843. * first time and writing now a new one. In this case the wl system
  844. * has never seen any PEB used by the original fastmap.
  845. */
  846. if (!e) {
  847. e = fm_e;
  848. ubi_assert(e->ec >= 0);
  849. ubi->lookuptbl[pnum] = e;
  850. } else {
  851. e->ec = fm_e->ec;
  852. kfree(fm_e);
  853. }
  854. spin_unlock(&ubi->wl_lock);
  855. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  856. return schedule_erase(ubi, e, vol_id, lnum, torture);
  857. }
  858. #endif
  859. /**
  860. * wear_leveling_worker - wear-leveling worker function.
  861. * @ubi: UBI device description object
  862. * @wrk: the work object
  863. * @shutdown: non-zero if the worker has to free memory and exit
  864. * because the WL-subsystem is shutting down
  865. *
  866. * This function copies a more worn out physical eraseblock to a less worn out
  867. * one. Returns zero in case of success and a negative error code in case of
  868. * failure.
  869. */
  870. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  871. int shutdown)
  872. {
  873. int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  874. int vol_id = -1, uninitialized_var(lnum);
  875. #ifdef CONFIG_MTD_UBI_FASTMAP
  876. int anchor = wrk->anchor;
  877. #endif
  878. struct ubi_wl_entry *e1, *e2;
  879. struct ubi_vid_hdr *vid_hdr;
  880. kfree(wrk);
  881. if (shutdown)
  882. return 0;
  883. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  884. if (!vid_hdr)
  885. return -ENOMEM;
  886. mutex_lock(&ubi->move_mutex);
  887. spin_lock(&ubi->wl_lock);
  888. ubi_assert(!ubi->move_from && !ubi->move_to);
  889. ubi_assert(!ubi->move_to_put);
  890. if (!ubi->free.rb_node ||
  891. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  892. /*
  893. * No free physical eraseblocks? Well, they must be waiting in
  894. * the queue to be erased. Cancel movement - it will be
  895. * triggered again when a free physical eraseblock appears.
  896. *
  897. * No used physical eraseblocks? They must be temporarily
  898. * protected from being moved. They will be moved to the
  899. * @ubi->used tree later and the wear-leveling will be
  900. * triggered again.
  901. */
  902. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  903. !ubi->free.rb_node, !ubi->used.rb_node);
  904. goto out_cancel;
  905. }
  906. #ifdef CONFIG_MTD_UBI_FASTMAP
  907. /* Check whether we need to produce an anchor PEB */
  908. if (!anchor)
  909. anchor = !anchor_pebs_avalible(&ubi->free);
  910. if (anchor) {
  911. e1 = find_anchor_wl_entry(&ubi->used);
  912. if (!e1)
  913. goto out_cancel;
  914. e2 = get_peb_for_wl(ubi);
  915. if (!e2)
  916. goto out_cancel;
  917. self_check_in_wl_tree(ubi, e1, &ubi->used);
  918. rb_erase(&e1->u.rb, &ubi->used);
  919. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  920. } else if (!ubi->scrub.rb_node) {
  921. #else
  922. if (!ubi->scrub.rb_node) {
  923. #endif
  924. /*
  925. * Now pick the least worn-out used physical eraseblock and a
  926. * highly worn-out free physical eraseblock. If the erase
  927. * counters differ much enough, start wear-leveling.
  928. */
  929. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  930. e2 = get_peb_for_wl(ubi);
  931. if (!e2)
  932. goto out_cancel;
  933. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  934. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  935. e1->ec, e2->ec);
  936. /* Give the unused PEB back */
  937. wl_tree_add(e2, &ubi->free);
  938. ubi->free_count++;
  939. goto out_cancel;
  940. }
  941. self_check_in_wl_tree(ubi, e1, &ubi->used);
  942. rb_erase(&e1->u.rb, &ubi->used);
  943. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  944. e1->pnum, e1->ec, e2->pnum, e2->ec);
  945. } else {
  946. /* Perform scrubbing */
  947. scrubbing = 1;
  948. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  949. e2 = get_peb_for_wl(ubi);
  950. if (!e2)
  951. goto out_cancel;
  952. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  953. rb_erase(&e1->u.rb, &ubi->scrub);
  954. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  955. }
  956. ubi->move_from = e1;
  957. ubi->move_to = e2;
  958. spin_unlock(&ubi->wl_lock);
  959. /*
  960. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  961. * We so far do not know which logical eraseblock our physical
  962. * eraseblock (@e1) belongs to. We have to read the volume identifier
  963. * header first.
  964. *
  965. * Note, we are protected from this PEB being unmapped and erased. The
  966. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  967. * which is being moved was unmapped.
  968. */
  969. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  970. if (err && err != UBI_IO_BITFLIPS) {
  971. if (err == UBI_IO_FF) {
  972. /*
  973. * We are trying to move PEB without a VID header. UBI
  974. * always write VID headers shortly after the PEB was
  975. * given, so we have a situation when it has not yet
  976. * had a chance to write it, because it was preempted.
  977. * So add this PEB to the protection queue so far,
  978. * because presumably more data will be written there
  979. * (including the missing VID header), and then we'll
  980. * move it.
  981. */
  982. dbg_wl("PEB %d has no VID header", e1->pnum);
  983. protect = 1;
  984. goto out_not_moved;
  985. } else if (err == UBI_IO_FF_BITFLIPS) {
  986. /*
  987. * The same situation as %UBI_IO_FF, but bit-flips were
  988. * detected. It is better to schedule this PEB for
  989. * scrubbing.
  990. */
  991. dbg_wl("PEB %d has no VID header but has bit-flips",
  992. e1->pnum);
  993. scrubbing = 1;
  994. goto out_not_moved;
  995. }
  996. ubi_err(ubi, "error %d while reading VID header from PEB %d",
  997. err, e1->pnum);
  998. goto out_error;
  999. }
  1000. vol_id = be32_to_cpu(vid_hdr->vol_id);
  1001. lnum = be32_to_cpu(vid_hdr->lnum);
  1002. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  1003. if (err) {
  1004. if (err == MOVE_CANCEL_RACE) {
  1005. /*
  1006. * The LEB has not been moved because the volume is
  1007. * being deleted or the PEB has been put meanwhile. We
  1008. * should prevent this PEB from being selected for
  1009. * wear-leveling movement again, so put it to the
  1010. * protection queue.
  1011. */
  1012. protect = 1;
  1013. goto out_not_moved;
  1014. }
  1015. if (err == MOVE_RETRY) {
  1016. scrubbing = 1;
  1017. goto out_not_moved;
  1018. }
  1019. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  1020. err == MOVE_TARGET_RD_ERR) {
  1021. /*
  1022. * Target PEB had bit-flips or write error - torture it.
  1023. */
  1024. torture = 1;
  1025. goto out_not_moved;
  1026. }
  1027. if (err == MOVE_SOURCE_RD_ERR) {
  1028. /*
  1029. * An error happened while reading the source PEB. Do
  1030. * not switch to R/O mode in this case, and give the
  1031. * upper layers a possibility to recover from this,
  1032. * e.g. by unmapping corresponding LEB. Instead, just
  1033. * put this PEB to the @ubi->erroneous list to prevent
  1034. * UBI from trying to move it over and over again.
  1035. */
  1036. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  1037. ubi_err(ubi, "too many erroneous eraseblocks (%d)",
  1038. ubi->erroneous_peb_count);
  1039. goto out_error;
  1040. }
  1041. erroneous = 1;
  1042. goto out_not_moved;
  1043. }
  1044. if (err < 0)
  1045. goto out_error;
  1046. ubi_assert(0);
  1047. }
  1048. /* The PEB has been successfully moved */
  1049. if (scrubbing)
  1050. ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  1051. e1->pnum, vol_id, lnum, e2->pnum);
  1052. ubi_free_vid_hdr(ubi, vid_hdr);
  1053. spin_lock(&ubi->wl_lock);
  1054. if (!ubi->move_to_put) {
  1055. wl_tree_add(e2, &ubi->used);
  1056. e2 = NULL;
  1057. }
  1058. ubi->move_from = ubi->move_to = NULL;
  1059. ubi->move_to_put = ubi->wl_scheduled = 0;
  1060. spin_unlock(&ubi->wl_lock);
  1061. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  1062. if (err) {
  1063. if (e2)
  1064. kmem_cache_free(ubi_wl_entry_slab, e2);
  1065. goto out_ro;
  1066. }
  1067. if (e2) {
  1068. /*
  1069. * Well, the target PEB was put meanwhile, schedule it for
  1070. * erasure.
  1071. */
  1072. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  1073. e2->pnum, vol_id, lnum);
  1074. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  1075. if (err)
  1076. goto out_ro;
  1077. }
  1078. dbg_wl("done");
  1079. mutex_unlock(&ubi->move_mutex);
  1080. return 0;
  1081. /*
  1082. * For some reasons the LEB was not moved, might be an error, might be
  1083. * something else. @e1 was not changed, so return it back. @e2 might
  1084. * have been changed, schedule it for erasure.
  1085. */
  1086. out_not_moved:
  1087. if (vol_id != -1)
  1088. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  1089. e1->pnum, vol_id, lnum, e2->pnum, err);
  1090. else
  1091. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  1092. e1->pnum, e2->pnum, err);
  1093. spin_lock(&ubi->wl_lock);
  1094. if (protect)
  1095. prot_queue_add(ubi, e1);
  1096. else if (erroneous) {
  1097. wl_tree_add(e1, &ubi->erroneous);
  1098. ubi->erroneous_peb_count += 1;
  1099. } else if (scrubbing)
  1100. wl_tree_add(e1, &ubi->scrub);
  1101. else
  1102. wl_tree_add(e1, &ubi->used);
  1103. ubi_assert(!ubi->move_to_put);
  1104. ubi->move_from = ubi->move_to = NULL;
  1105. ubi->wl_scheduled = 0;
  1106. spin_unlock(&ubi->wl_lock);
  1107. ubi_free_vid_hdr(ubi, vid_hdr);
  1108. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  1109. if (err)
  1110. goto out_ro;
  1111. mutex_unlock(&ubi->move_mutex);
  1112. return 0;
  1113. out_error:
  1114. if (vol_id != -1)
  1115. ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
  1116. err, e1->pnum, e2->pnum);
  1117. else
  1118. ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  1119. err, e1->pnum, vol_id, lnum, e2->pnum);
  1120. spin_lock(&ubi->wl_lock);
  1121. ubi->move_from = ubi->move_to = NULL;
  1122. ubi->move_to_put = ubi->wl_scheduled = 0;
  1123. spin_unlock(&ubi->wl_lock);
  1124. ubi_free_vid_hdr(ubi, vid_hdr);
  1125. kmem_cache_free(ubi_wl_entry_slab, e1);
  1126. kmem_cache_free(ubi_wl_entry_slab, e2);
  1127. out_ro:
  1128. ubi_ro_mode(ubi);
  1129. mutex_unlock(&ubi->move_mutex);
  1130. ubi_assert(err != 0);
  1131. return err < 0 ? err : -EIO;
  1132. out_cancel:
  1133. ubi->wl_scheduled = 0;
  1134. spin_unlock(&ubi->wl_lock);
  1135. mutex_unlock(&ubi->move_mutex);
  1136. ubi_free_vid_hdr(ubi, vid_hdr);
  1137. return 0;
  1138. }
  1139. /**
  1140. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  1141. * @ubi: UBI device description object
  1142. * @nested: set to non-zero if this function is called from UBI worker
  1143. *
  1144. * This function checks if it is time to start wear-leveling and schedules it
  1145. * if yes. This function returns zero in case of success and a negative error
  1146. * code in case of failure.
  1147. */
  1148. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  1149. {
  1150. int err = 0;
  1151. struct ubi_wl_entry *e1;
  1152. struct ubi_wl_entry *e2;
  1153. struct ubi_work *wrk;
  1154. spin_lock(&ubi->wl_lock);
  1155. if (ubi->wl_scheduled)
  1156. /* Wear-leveling is already in the work queue */
  1157. goto out_unlock;
  1158. /*
  1159. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  1160. * the WL worker has to be scheduled anyway.
  1161. */
  1162. if (!ubi->scrub.rb_node) {
  1163. if (!ubi->used.rb_node || !ubi->free.rb_node)
  1164. /* No physical eraseblocks - no deal */
  1165. goto out_unlock;
  1166. /*
  1167. * We schedule wear-leveling only if the difference between the
  1168. * lowest erase counter of used physical eraseblocks and a high
  1169. * erase counter of free physical eraseblocks is greater than
  1170. * %UBI_WL_THRESHOLD.
  1171. */
  1172. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  1173. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1174. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  1175. goto out_unlock;
  1176. dbg_wl("schedule wear-leveling");
  1177. } else
  1178. dbg_wl("schedule scrubbing");
  1179. ubi->wl_scheduled = 1;
  1180. spin_unlock(&ubi->wl_lock);
  1181. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1182. if (!wrk) {
  1183. err = -ENOMEM;
  1184. goto out_cancel;
  1185. }
  1186. wrk->anchor = 0;
  1187. wrk->func = &wear_leveling_worker;
  1188. if (nested)
  1189. __schedule_ubi_work(ubi, wrk);
  1190. else
  1191. schedule_ubi_work(ubi, wrk);
  1192. return err;
  1193. out_cancel:
  1194. spin_lock(&ubi->wl_lock);
  1195. ubi->wl_scheduled = 0;
  1196. out_unlock:
  1197. spin_unlock(&ubi->wl_lock);
  1198. return err;
  1199. }
  1200. #ifdef CONFIG_MTD_UBI_FASTMAP
  1201. /**
  1202. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  1203. * @ubi: UBI device description object
  1204. */
  1205. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  1206. {
  1207. struct ubi_work *wrk;
  1208. spin_lock(&ubi->wl_lock);
  1209. if (ubi->wl_scheduled) {
  1210. spin_unlock(&ubi->wl_lock);
  1211. return 0;
  1212. }
  1213. ubi->wl_scheduled = 1;
  1214. spin_unlock(&ubi->wl_lock);
  1215. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  1216. if (!wrk) {
  1217. spin_lock(&ubi->wl_lock);
  1218. ubi->wl_scheduled = 0;
  1219. spin_unlock(&ubi->wl_lock);
  1220. return -ENOMEM;
  1221. }
  1222. wrk->anchor = 1;
  1223. wrk->func = &wear_leveling_worker;
  1224. schedule_ubi_work(ubi, wrk);
  1225. return 0;
  1226. }
  1227. #endif
  1228. /**
  1229. * erase_worker - physical eraseblock erase worker function.
  1230. * @ubi: UBI device description object
  1231. * @wl_wrk: the work object
  1232. * @shutdown: non-zero if the worker has to free memory and exit
  1233. * because the WL sub-system is shutting down
  1234. *
  1235. * This function erases a physical eraseblock and perform torture testing if
  1236. * needed. It also takes care about marking the physical eraseblock bad if
  1237. * needed. Returns zero in case of success and a negative error code in case of
  1238. * failure.
  1239. */
  1240. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1241. int shutdown)
  1242. {
  1243. struct ubi_wl_entry *e = wl_wrk->e;
  1244. int pnum = e->pnum;
  1245. int vol_id = wl_wrk->vol_id;
  1246. int lnum = wl_wrk->lnum;
  1247. int err, available_consumed = 0;
  1248. if (shutdown) {
  1249. dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
  1250. kfree(wl_wrk);
  1251. kmem_cache_free(ubi_wl_entry_slab, e);
  1252. return 0;
  1253. }
  1254. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  1255. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  1256. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1257. err = sync_erase(ubi, e, wl_wrk->torture);
  1258. if (!err) {
  1259. /* Fine, we've erased it successfully */
  1260. kfree(wl_wrk);
  1261. spin_lock(&ubi->wl_lock);
  1262. wl_tree_add(e, &ubi->free);
  1263. ubi->free_count++;
  1264. spin_unlock(&ubi->wl_lock);
  1265. /*
  1266. * One more erase operation has happened, take care about
  1267. * protected physical eraseblocks.
  1268. */
  1269. serve_prot_queue(ubi);
  1270. /* And take care about wear-leveling */
  1271. err = ensure_wear_leveling(ubi, 1);
  1272. return err;
  1273. }
  1274. ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
  1275. kfree(wl_wrk);
  1276. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  1277. err == -EBUSY) {
  1278. int err1;
  1279. /* Re-schedule the LEB for erasure */
  1280. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  1281. if (err1) {
  1282. err = err1;
  1283. goto out_ro;
  1284. }
  1285. return err;
  1286. }
  1287. kmem_cache_free(ubi_wl_entry_slab, e);
  1288. if (err != -EIO)
  1289. /*
  1290. * If this is not %-EIO, we have no idea what to do. Scheduling
  1291. * this physical eraseblock for erasure again would cause
  1292. * errors again and again. Well, lets switch to R/O mode.
  1293. */
  1294. goto out_ro;
  1295. /* It is %-EIO, the PEB went bad */
  1296. if (!ubi->bad_allowed) {
  1297. ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
  1298. goto out_ro;
  1299. }
  1300. spin_lock(&ubi->volumes_lock);
  1301. if (ubi->beb_rsvd_pebs == 0) {
  1302. if (ubi->avail_pebs == 0) {
  1303. spin_unlock(&ubi->volumes_lock);
  1304. ubi_err(ubi, "no reserved/available physical eraseblocks");
  1305. goto out_ro;
  1306. }
  1307. ubi->avail_pebs -= 1;
  1308. available_consumed = 1;
  1309. }
  1310. spin_unlock(&ubi->volumes_lock);
  1311. ubi_msg(ubi, "mark PEB %d as bad", pnum);
  1312. err = ubi_io_mark_bad(ubi, pnum);
  1313. if (err)
  1314. goto out_ro;
  1315. spin_lock(&ubi->volumes_lock);
  1316. if (ubi->beb_rsvd_pebs > 0) {
  1317. if (available_consumed) {
  1318. /*
  1319. * The amount of reserved PEBs increased since we last
  1320. * checked.
  1321. */
  1322. ubi->avail_pebs += 1;
  1323. available_consumed = 0;
  1324. }
  1325. ubi->beb_rsvd_pebs -= 1;
  1326. }
  1327. ubi->bad_peb_count += 1;
  1328. ubi->good_peb_count -= 1;
  1329. ubi_calculate_reserved(ubi);
  1330. if (available_consumed)
  1331. ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
  1332. else if (ubi->beb_rsvd_pebs)
  1333. ubi_msg(ubi, "%d PEBs left in the reserve",
  1334. ubi->beb_rsvd_pebs);
  1335. else
  1336. ubi_warn(ubi, "last PEB from the reserve was used");
  1337. spin_unlock(&ubi->volumes_lock);
  1338. return err;
  1339. out_ro:
  1340. if (available_consumed) {
  1341. spin_lock(&ubi->volumes_lock);
  1342. ubi->avail_pebs += 1;
  1343. spin_unlock(&ubi->volumes_lock);
  1344. }
  1345. ubi_ro_mode(ubi);
  1346. return err;
  1347. }
  1348. /**
  1349. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1350. * @ubi: UBI device description object
  1351. * @vol_id: the volume ID that last used this PEB
  1352. * @lnum: the last used logical eraseblock number for the PEB
  1353. * @pnum: physical eraseblock to return
  1354. * @torture: if this physical eraseblock has to be tortured
  1355. *
  1356. * This function is called to return physical eraseblock @pnum to the pool of
  1357. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1358. * occurred to this @pnum and it has to be tested. This function returns zero
  1359. * in case of success, and a negative error code in case of failure.
  1360. */
  1361. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1362. int pnum, int torture)
  1363. {
  1364. int err;
  1365. struct ubi_wl_entry *e;
  1366. dbg_wl("PEB %d", pnum);
  1367. ubi_assert(pnum >= 0);
  1368. ubi_assert(pnum < ubi->peb_count);
  1369. retry:
  1370. spin_lock(&ubi->wl_lock);
  1371. e = ubi->lookuptbl[pnum];
  1372. if (e == ubi->move_from) {
  1373. /*
  1374. * User is putting the physical eraseblock which was selected to
  1375. * be moved. It will be scheduled for erasure in the
  1376. * wear-leveling worker.
  1377. */
  1378. dbg_wl("PEB %d is being moved, wait", pnum);
  1379. spin_unlock(&ubi->wl_lock);
  1380. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1381. mutex_lock(&ubi->move_mutex);
  1382. mutex_unlock(&ubi->move_mutex);
  1383. goto retry;
  1384. } else if (e == ubi->move_to) {
  1385. /*
  1386. * User is putting the physical eraseblock which was selected
  1387. * as the target the data is moved to. It may happen if the EBA
  1388. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1389. * but the WL sub-system has not put the PEB to the "used" tree
  1390. * yet, but it is about to do this. So we just set a flag which
  1391. * will tell the WL worker that the PEB is not needed anymore
  1392. * and should be scheduled for erasure.
  1393. */
  1394. dbg_wl("PEB %d is the target of data moving", pnum);
  1395. ubi_assert(!ubi->move_to_put);
  1396. ubi->move_to_put = 1;
  1397. spin_unlock(&ubi->wl_lock);
  1398. return 0;
  1399. } else {
  1400. if (in_wl_tree(e, &ubi->used)) {
  1401. self_check_in_wl_tree(ubi, e, &ubi->used);
  1402. rb_erase(&e->u.rb, &ubi->used);
  1403. } else if (in_wl_tree(e, &ubi->scrub)) {
  1404. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1405. rb_erase(&e->u.rb, &ubi->scrub);
  1406. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1407. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1408. rb_erase(&e->u.rb, &ubi->erroneous);
  1409. ubi->erroneous_peb_count -= 1;
  1410. ubi_assert(ubi->erroneous_peb_count >= 0);
  1411. /* Erroneous PEBs should be tortured */
  1412. torture = 1;
  1413. } else {
  1414. err = prot_queue_del(ubi, e->pnum);
  1415. if (err) {
  1416. ubi_err(ubi, "PEB %d not found", pnum);
  1417. ubi_ro_mode(ubi);
  1418. spin_unlock(&ubi->wl_lock);
  1419. return err;
  1420. }
  1421. }
  1422. }
  1423. spin_unlock(&ubi->wl_lock);
  1424. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1425. if (err) {
  1426. spin_lock(&ubi->wl_lock);
  1427. wl_tree_add(e, &ubi->used);
  1428. spin_unlock(&ubi->wl_lock);
  1429. }
  1430. return err;
  1431. }
  1432. /**
  1433. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1434. * @ubi: UBI device description object
  1435. * @pnum: the physical eraseblock to schedule
  1436. *
  1437. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1438. * needs scrubbing. This function schedules a physical eraseblock for
  1439. * scrubbing which is done in background. This function returns zero in case of
  1440. * success and a negative error code in case of failure.
  1441. */
  1442. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1443. {
  1444. struct ubi_wl_entry *e;
  1445. ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
  1446. retry:
  1447. spin_lock(&ubi->wl_lock);
  1448. e = ubi->lookuptbl[pnum];
  1449. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1450. in_wl_tree(e, &ubi->erroneous)) {
  1451. spin_unlock(&ubi->wl_lock);
  1452. return 0;
  1453. }
  1454. if (e == ubi->move_to) {
  1455. /*
  1456. * This physical eraseblock was used to move data to. The data
  1457. * was moved but the PEB was not yet inserted to the proper
  1458. * tree. We should just wait a little and let the WL worker
  1459. * proceed.
  1460. */
  1461. spin_unlock(&ubi->wl_lock);
  1462. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1463. yield();
  1464. goto retry;
  1465. }
  1466. if (in_wl_tree(e, &ubi->used)) {
  1467. self_check_in_wl_tree(ubi, e, &ubi->used);
  1468. rb_erase(&e->u.rb, &ubi->used);
  1469. } else {
  1470. int err;
  1471. err = prot_queue_del(ubi, e->pnum);
  1472. if (err) {
  1473. ubi_err(ubi, "PEB %d not found", pnum);
  1474. ubi_ro_mode(ubi);
  1475. spin_unlock(&ubi->wl_lock);
  1476. return err;
  1477. }
  1478. }
  1479. wl_tree_add(e, &ubi->scrub);
  1480. spin_unlock(&ubi->wl_lock);
  1481. /*
  1482. * Technically scrubbing is the same as wear-leveling, so it is done
  1483. * by the WL worker.
  1484. */
  1485. return ensure_wear_leveling(ubi, 0);
  1486. }
  1487. /**
  1488. * ubi_wl_flush - flush all pending works.
  1489. * @ubi: UBI device description object
  1490. * @vol_id: the volume id to flush for
  1491. * @lnum: the logical eraseblock number to flush for
  1492. *
  1493. * This function executes all pending works for a particular volume id /
  1494. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1495. * acts as a wildcard for all of the corresponding volume numbers or logical
  1496. * eraseblock numbers. It returns zero in case of success and a negative error
  1497. * code in case of failure.
  1498. */
  1499. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1500. {
  1501. int err = 0;
  1502. int found = 1;
  1503. /*
  1504. * Erase while the pending works queue is not empty, but not more than
  1505. * the number of currently pending works.
  1506. */
  1507. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1508. vol_id, lnum, ubi->works_count);
  1509. while (found) {
  1510. struct ubi_work *wrk, *tmp;
  1511. found = 0;
  1512. down_read(&ubi->work_sem);
  1513. spin_lock(&ubi->wl_lock);
  1514. list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
  1515. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1516. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1517. list_del(&wrk->list);
  1518. ubi->works_count -= 1;
  1519. ubi_assert(ubi->works_count >= 0);
  1520. spin_unlock(&ubi->wl_lock);
  1521. err = wrk->func(ubi, wrk, 0);
  1522. if (err) {
  1523. up_read(&ubi->work_sem);
  1524. return err;
  1525. }
  1526. spin_lock(&ubi->wl_lock);
  1527. found = 1;
  1528. break;
  1529. }
  1530. }
  1531. spin_unlock(&ubi->wl_lock);
  1532. up_read(&ubi->work_sem);
  1533. }
  1534. /*
  1535. * Make sure all the works which have been done in parallel are
  1536. * finished.
  1537. */
  1538. down_write(&ubi->work_sem);
  1539. up_write(&ubi->work_sem);
  1540. return err;
  1541. }
  1542. /**
  1543. * tree_destroy - destroy an RB-tree.
  1544. * @root: the root of the tree to destroy
  1545. */
  1546. static void tree_destroy(struct rb_root *root)
  1547. {
  1548. struct rb_node *rb;
  1549. struct ubi_wl_entry *e;
  1550. rb = root->rb_node;
  1551. while (rb) {
  1552. if (rb->rb_left)
  1553. rb = rb->rb_left;
  1554. else if (rb->rb_right)
  1555. rb = rb->rb_right;
  1556. else {
  1557. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1558. rb = rb_parent(rb);
  1559. if (rb) {
  1560. if (rb->rb_left == &e->u.rb)
  1561. rb->rb_left = NULL;
  1562. else
  1563. rb->rb_right = NULL;
  1564. }
  1565. kmem_cache_free(ubi_wl_entry_slab, e);
  1566. }
  1567. }
  1568. }
  1569. /**
  1570. * ubi_thread - UBI background thread.
  1571. * @u: the UBI device description object pointer
  1572. */
  1573. int ubi_thread(void *u)
  1574. {
  1575. int failures = 0;
  1576. struct ubi_device *ubi = u;
  1577. ubi_msg(ubi, "background thread \"%s\" started, PID %d",
  1578. ubi->bgt_name, task_pid_nr(current));
  1579. set_freezable();
  1580. for (;;) {
  1581. int err;
  1582. if (kthread_should_stop())
  1583. break;
  1584. if (try_to_freeze())
  1585. continue;
  1586. spin_lock(&ubi->wl_lock);
  1587. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1588. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1589. set_current_state(TASK_INTERRUPTIBLE);
  1590. spin_unlock(&ubi->wl_lock);
  1591. schedule();
  1592. continue;
  1593. }
  1594. spin_unlock(&ubi->wl_lock);
  1595. err = do_work(ubi);
  1596. if (err) {
  1597. ubi_err(ubi, "%s: work failed with error code %d",
  1598. ubi->bgt_name, err);
  1599. if (failures++ > WL_MAX_FAILURES) {
  1600. /*
  1601. * Too many failures, disable the thread and
  1602. * switch to read-only mode.
  1603. */
  1604. ubi_msg(ubi, "%s: %d consecutive failures",
  1605. ubi->bgt_name, WL_MAX_FAILURES);
  1606. ubi_ro_mode(ubi);
  1607. ubi->thread_enabled = 0;
  1608. continue;
  1609. }
  1610. } else
  1611. failures = 0;
  1612. cond_resched();
  1613. }
  1614. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1615. return 0;
  1616. }
  1617. /**
  1618. * shutdown_work - shutdown all pending works.
  1619. * @ubi: UBI device description object
  1620. */
  1621. static void shutdown_work(struct ubi_device *ubi)
  1622. {
  1623. while (!list_empty(&ubi->works)) {
  1624. struct ubi_work *wrk;
  1625. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1626. list_del(&wrk->list);
  1627. wrk->func(ubi, wrk, 1);
  1628. ubi->works_count -= 1;
  1629. ubi_assert(ubi->works_count >= 0);
  1630. }
  1631. }
  1632. /**
  1633. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1634. * @ubi: UBI device description object
  1635. * @ai: attaching information
  1636. *
  1637. * This function returns zero in case of success, and a negative error code in
  1638. * case of failure.
  1639. */
  1640. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1641. {
  1642. int err, i, reserved_pebs, found_pebs = 0;
  1643. struct rb_node *rb1, *rb2;
  1644. struct ubi_ainf_volume *av;
  1645. struct ubi_ainf_peb *aeb, *tmp;
  1646. struct ubi_wl_entry *e;
  1647. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1648. spin_lock_init(&ubi->wl_lock);
  1649. mutex_init(&ubi->move_mutex);
  1650. init_rwsem(&ubi->work_sem);
  1651. ubi->max_ec = ai->max_ec;
  1652. INIT_LIST_HEAD(&ubi->works);
  1653. #ifdef CONFIG_MTD_UBI_FASTMAP
  1654. INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
  1655. #endif
  1656. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1657. err = -ENOMEM;
  1658. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1659. if (!ubi->lookuptbl)
  1660. return err;
  1661. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1662. INIT_LIST_HEAD(&ubi->pq[i]);
  1663. ubi->pq_head = 0;
  1664. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1665. cond_resched();
  1666. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1667. if (!e)
  1668. goto out_free;
  1669. e->pnum = aeb->pnum;
  1670. e->ec = aeb->ec;
  1671. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1672. ubi->lookuptbl[e->pnum] = e;
  1673. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1674. kmem_cache_free(ubi_wl_entry_slab, e);
  1675. goto out_free;
  1676. }
  1677. found_pebs++;
  1678. }
  1679. ubi->free_count = 0;
  1680. list_for_each_entry(aeb, &ai->free, u.list) {
  1681. cond_resched();
  1682. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1683. if (!e)
  1684. goto out_free;
  1685. e->pnum = aeb->pnum;
  1686. e->ec = aeb->ec;
  1687. ubi_assert(e->ec >= 0);
  1688. ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
  1689. wl_tree_add(e, &ubi->free);
  1690. ubi->free_count++;
  1691. ubi->lookuptbl[e->pnum] = e;
  1692. found_pebs++;
  1693. }
  1694. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1695. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1696. cond_resched();
  1697. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1698. if (!e)
  1699. goto out_free;
  1700. e->pnum = aeb->pnum;
  1701. e->ec = aeb->ec;
  1702. ubi->lookuptbl[e->pnum] = e;
  1703. if (!aeb->scrub) {
  1704. dbg_wl("add PEB %d EC %d to the used tree",
  1705. e->pnum, e->ec);
  1706. wl_tree_add(e, &ubi->used);
  1707. } else {
  1708. dbg_wl("add PEB %d EC %d to the scrub tree",
  1709. e->pnum, e->ec);
  1710. wl_tree_add(e, &ubi->scrub);
  1711. }
  1712. found_pebs++;
  1713. }
  1714. }
  1715. dbg_wl("found %i PEBs", found_pebs);
  1716. if (ubi->fm)
  1717. ubi_assert(ubi->good_peb_count == \
  1718. found_pebs + ubi->fm->used_blocks);
  1719. else
  1720. ubi_assert(ubi->good_peb_count == found_pebs);
  1721. reserved_pebs = WL_RESERVED_PEBS;
  1722. #ifdef CONFIG_MTD_UBI_FASTMAP
  1723. /* Reserve enough LEBs to store two fastmaps. */
  1724. reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
  1725. #endif
  1726. if (ubi->avail_pebs < reserved_pebs) {
  1727. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1728. ubi->avail_pebs, reserved_pebs);
  1729. if (ubi->corr_peb_count)
  1730. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1731. ubi->corr_peb_count);
  1732. goto out_free;
  1733. }
  1734. ubi->avail_pebs -= reserved_pebs;
  1735. ubi->rsvd_pebs += reserved_pebs;
  1736. /* Schedule wear-leveling if needed */
  1737. err = ensure_wear_leveling(ubi, 0);
  1738. if (err)
  1739. goto out_free;
  1740. return 0;
  1741. out_free:
  1742. shutdown_work(ubi);
  1743. tree_destroy(&ubi->used);
  1744. tree_destroy(&ubi->free);
  1745. tree_destroy(&ubi->scrub);
  1746. kfree(ubi->lookuptbl);
  1747. return err;
  1748. }
  1749. /**
  1750. * protection_queue_destroy - destroy the protection queue.
  1751. * @ubi: UBI device description object
  1752. */
  1753. static void protection_queue_destroy(struct ubi_device *ubi)
  1754. {
  1755. int i;
  1756. struct ubi_wl_entry *e, *tmp;
  1757. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1758. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1759. list_del(&e->u.list);
  1760. kmem_cache_free(ubi_wl_entry_slab, e);
  1761. }
  1762. }
  1763. }
  1764. /**
  1765. * ubi_wl_close - close the wear-leveling sub-system.
  1766. * @ubi: UBI device description object
  1767. */
  1768. void ubi_wl_close(struct ubi_device *ubi)
  1769. {
  1770. dbg_wl("close the WL sub-system");
  1771. shutdown_work(ubi);
  1772. protection_queue_destroy(ubi);
  1773. tree_destroy(&ubi->used);
  1774. tree_destroy(&ubi->erroneous);
  1775. tree_destroy(&ubi->free);
  1776. tree_destroy(&ubi->scrub);
  1777. kfree(ubi->lookuptbl);
  1778. }
  1779. /**
  1780. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1781. * @ubi: UBI device description object
  1782. * @pnum: the physical eraseblock number to check
  1783. * @ec: the erase counter to check
  1784. *
  1785. * This function returns zero if the erase counter of physical eraseblock @pnum
  1786. * is equivalent to @ec, and a negative error code if not or if an error
  1787. * occurred.
  1788. */
  1789. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1790. {
  1791. int err;
  1792. long long read_ec;
  1793. struct ubi_ec_hdr *ec_hdr;
  1794. if (!ubi_dbg_chk_gen(ubi))
  1795. return 0;
  1796. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1797. if (!ec_hdr)
  1798. return -ENOMEM;
  1799. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1800. if (err && err != UBI_IO_BITFLIPS) {
  1801. /* The header does not have to exist */
  1802. err = 0;
  1803. goto out_free;
  1804. }
  1805. read_ec = be64_to_cpu(ec_hdr->ec);
  1806. if (ec != read_ec && read_ec - ec > 1) {
  1807. ubi_err(ubi, "self-check failed for PEB %d", pnum);
  1808. ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
  1809. dump_stack();
  1810. err = 1;
  1811. } else
  1812. err = 0;
  1813. out_free:
  1814. kfree(ec_hdr);
  1815. return err;
  1816. }
  1817. /**
  1818. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1819. * @ubi: UBI device description object
  1820. * @e: the wear-leveling entry to check
  1821. * @root: the root of the tree
  1822. *
  1823. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1824. * is not.
  1825. */
  1826. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1827. struct ubi_wl_entry *e, struct rb_root *root)
  1828. {
  1829. if (!ubi_dbg_chk_gen(ubi))
  1830. return 0;
  1831. if (in_wl_tree(e, root))
  1832. return 0;
  1833. ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
  1834. e->pnum, e->ec, root);
  1835. dump_stack();
  1836. return -EINVAL;
  1837. }
  1838. /**
  1839. * self_check_in_pq - check if wear-leveling entry is in the protection
  1840. * queue.
  1841. * @ubi: UBI device description object
  1842. * @e: the wear-leveling entry to check
  1843. *
  1844. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1845. */
  1846. static int self_check_in_pq(const struct ubi_device *ubi,
  1847. struct ubi_wl_entry *e)
  1848. {
  1849. struct ubi_wl_entry *p;
  1850. int i;
  1851. if (!ubi_dbg_chk_gen(ubi))
  1852. return 0;
  1853. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1854. list_for_each_entry(p, &ubi->pq[i], u.list)
  1855. if (p == e)
  1856. return 0;
  1857. ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
  1858. e->pnum, e->ec);
  1859. dump_stack();
  1860. return -EINVAL;
  1861. }