wl.c 52 KB

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