wl.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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)
  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. schedule_ubi_work(ubi, wl_wrk);
  532. return 0;
  533. }
  534. static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk);
  535. /**
  536. * do_sync_erase - run the erase worker synchronously.
  537. * @ubi: UBI device description object
  538. * @e: the WL entry of the physical eraseblock to erase
  539. * @vol_id: the volume ID that last used this PEB
  540. * @lnum: the last used logical eraseblock number for the PEB
  541. * @torture: if the physical eraseblock has to be tortured
  542. *
  543. */
  544. static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
  545. int vol_id, int lnum, int torture)
  546. {
  547. struct ubi_work wl_wrk;
  548. dbg_wl("sync erase of PEB %i", e->pnum);
  549. wl_wrk.e = e;
  550. wl_wrk.vol_id = vol_id;
  551. wl_wrk.lnum = lnum;
  552. wl_wrk.torture = torture;
  553. return __erase_worker(ubi, &wl_wrk);
  554. }
  555. static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
  556. /**
  557. * wear_leveling_worker - wear-leveling worker function.
  558. * @ubi: UBI device description object
  559. * @wrk: the work object
  560. * @shutdown: non-zero if the worker has to free memory and exit
  561. * because the WL-subsystem is shutting down
  562. *
  563. * This function copies a more worn out physical eraseblock to a less worn out
  564. * one. Returns zero in case of success and a negative error code in case of
  565. * failure.
  566. */
  567. static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
  568. int shutdown)
  569. {
  570. int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
  571. int vol_id = -1, lnum = -1;
  572. #ifdef CONFIG_MTD_UBI_FASTMAP
  573. int anchor = wrk->anchor;
  574. #endif
  575. struct ubi_wl_entry *e1, *e2;
  576. struct ubi_vid_hdr *vid_hdr;
  577. int dst_leb_clean = 0;
  578. kfree(wrk);
  579. if (shutdown)
  580. return 0;
  581. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  582. if (!vid_hdr)
  583. return -ENOMEM;
  584. mutex_lock(&ubi->move_mutex);
  585. spin_lock(&ubi->wl_lock);
  586. ubi_assert(!ubi->move_from && !ubi->move_to);
  587. ubi_assert(!ubi->move_to_put);
  588. if (!ubi->free.rb_node ||
  589. (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
  590. /*
  591. * No free physical eraseblocks? Well, they must be waiting in
  592. * the queue to be erased. Cancel movement - it will be
  593. * triggered again when a free physical eraseblock appears.
  594. *
  595. * No used physical eraseblocks? They must be temporarily
  596. * protected from being moved. They will be moved to the
  597. * @ubi->used tree later and the wear-leveling will be
  598. * triggered again.
  599. */
  600. dbg_wl("cancel WL, a list is empty: free %d, used %d",
  601. !ubi->free.rb_node, !ubi->used.rb_node);
  602. goto out_cancel;
  603. }
  604. #ifdef CONFIG_MTD_UBI_FASTMAP
  605. /* Check whether we need to produce an anchor PEB */
  606. if (!anchor)
  607. anchor = !anchor_pebs_avalible(&ubi->free);
  608. if (anchor) {
  609. e1 = find_anchor_wl_entry(&ubi->used);
  610. if (!e1)
  611. goto out_cancel;
  612. e2 = get_peb_for_wl(ubi);
  613. if (!e2)
  614. goto out_cancel;
  615. self_check_in_wl_tree(ubi, e1, &ubi->used);
  616. rb_erase(&e1->u.rb, &ubi->used);
  617. dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
  618. } else if (!ubi->scrub.rb_node) {
  619. #else
  620. if (!ubi->scrub.rb_node) {
  621. #endif
  622. /*
  623. * Now pick the least worn-out used physical eraseblock and a
  624. * highly worn-out free physical eraseblock. If the erase
  625. * counters differ much enough, start wear-leveling.
  626. */
  627. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  628. e2 = get_peb_for_wl(ubi);
  629. if (!e2)
  630. goto out_cancel;
  631. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
  632. dbg_wl("no WL needed: min used EC %d, max free EC %d",
  633. e1->ec, e2->ec);
  634. /* Give the unused PEB back */
  635. wl_tree_add(e2, &ubi->free);
  636. ubi->free_count++;
  637. goto out_cancel;
  638. }
  639. self_check_in_wl_tree(ubi, e1, &ubi->used);
  640. rb_erase(&e1->u.rb, &ubi->used);
  641. dbg_wl("move PEB %d EC %d to PEB %d EC %d",
  642. e1->pnum, e1->ec, e2->pnum, e2->ec);
  643. } else {
  644. /* Perform scrubbing */
  645. scrubbing = 1;
  646. e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
  647. e2 = get_peb_for_wl(ubi);
  648. if (!e2)
  649. goto out_cancel;
  650. self_check_in_wl_tree(ubi, e1, &ubi->scrub);
  651. rb_erase(&e1->u.rb, &ubi->scrub);
  652. dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
  653. }
  654. ubi->move_from = e1;
  655. ubi->move_to = e2;
  656. spin_unlock(&ubi->wl_lock);
  657. /*
  658. * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
  659. * We so far do not know which logical eraseblock our physical
  660. * eraseblock (@e1) belongs to. We have to read the volume identifier
  661. * header first.
  662. *
  663. * Note, we are protected from this PEB being unmapped and erased. The
  664. * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
  665. * which is being moved was unmapped.
  666. */
  667. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  668. if (err && err != UBI_IO_BITFLIPS) {
  669. dst_leb_clean = 1;
  670. if (err == UBI_IO_FF) {
  671. /*
  672. * We are trying to move PEB without a VID header. UBI
  673. * always write VID headers shortly after the PEB was
  674. * given, so we have a situation when it has not yet
  675. * had a chance to write it, because it was preempted.
  676. * So add this PEB to the protection queue so far,
  677. * because presumably more data will be written there
  678. * (including the missing VID header), and then we'll
  679. * move it.
  680. */
  681. dbg_wl("PEB %d has no VID header", e1->pnum);
  682. protect = 1;
  683. goto out_not_moved;
  684. } else if (err == UBI_IO_FF_BITFLIPS) {
  685. /*
  686. * The same situation as %UBI_IO_FF, but bit-flips were
  687. * detected. It is better to schedule this PEB for
  688. * scrubbing.
  689. */
  690. dbg_wl("PEB %d has no VID header but has bit-flips",
  691. e1->pnum);
  692. scrubbing = 1;
  693. goto out_not_moved;
  694. }
  695. ubi_err(ubi, "error %d while reading VID header from PEB %d",
  696. err, e1->pnum);
  697. goto out_error;
  698. }
  699. vol_id = be32_to_cpu(vid_hdr->vol_id);
  700. lnum = be32_to_cpu(vid_hdr->lnum);
  701. err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
  702. if (err) {
  703. if (err == MOVE_CANCEL_RACE) {
  704. /*
  705. * The LEB has not been moved because the volume is
  706. * being deleted or the PEB has been put meanwhile. We
  707. * should prevent this PEB from being selected for
  708. * wear-leveling movement again, so put it to the
  709. * protection queue.
  710. */
  711. protect = 1;
  712. dst_leb_clean = 1;
  713. goto out_not_moved;
  714. }
  715. if (err == MOVE_RETRY) {
  716. scrubbing = 1;
  717. dst_leb_clean = 1;
  718. goto out_not_moved;
  719. }
  720. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  721. err == MOVE_TARGET_RD_ERR) {
  722. /*
  723. * Target PEB had bit-flips or write error - torture it.
  724. */
  725. torture = 1;
  726. goto out_not_moved;
  727. }
  728. if (err == MOVE_SOURCE_RD_ERR) {
  729. /*
  730. * An error happened while reading the source PEB. Do
  731. * not switch to R/O mode in this case, and give the
  732. * upper layers a possibility to recover from this,
  733. * e.g. by unmapping corresponding LEB. Instead, just
  734. * put this PEB to the @ubi->erroneous list to prevent
  735. * UBI from trying to move it over and over again.
  736. */
  737. if (ubi->erroneous_peb_count > ubi->max_erroneous) {
  738. ubi_err(ubi, "too many erroneous eraseblocks (%d)",
  739. ubi->erroneous_peb_count);
  740. goto out_error;
  741. }
  742. dst_leb_clean = 1;
  743. erroneous = 1;
  744. goto out_not_moved;
  745. }
  746. if (err < 0)
  747. goto out_error;
  748. ubi_assert(0);
  749. }
  750. /* The PEB has been successfully moved */
  751. if (scrubbing)
  752. ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
  753. e1->pnum, vol_id, lnum, e2->pnum);
  754. ubi_free_vid_hdr(ubi, vid_hdr);
  755. spin_lock(&ubi->wl_lock);
  756. if (!ubi->move_to_put) {
  757. wl_tree_add(e2, &ubi->used);
  758. e2 = NULL;
  759. }
  760. ubi->move_from = ubi->move_to = NULL;
  761. ubi->move_to_put = ubi->wl_scheduled = 0;
  762. spin_unlock(&ubi->wl_lock);
  763. err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
  764. if (err) {
  765. if (e2)
  766. wl_entry_destroy(ubi, e2);
  767. goto out_ro;
  768. }
  769. if (e2) {
  770. /*
  771. * Well, the target PEB was put meanwhile, schedule it for
  772. * erasure.
  773. */
  774. dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
  775. e2->pnum, vol_id, lnum);
  776. err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
  777. if (err)
  778. goto out_ro;
  779. }
  780. dbg_wl("done");
  781. mutex_unlock(&ubi->move_mutex);
  782. return 0;
  783. /*
  784. * For some reasons the LEB was not moved, might be an error, might be
  785. * something else. @e1 was not changed, so return it back. @e2 might
  786. * have been changed, schedule it for erasure.
  787. */
  788. out_not_moved:
  789. if (vol_id != -1)
  790. dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
  791. e1->pnum, vol_id, lnum, e2->pnum, err);
  792. else
  793. dbg_wl("cancel moving PEB %d to PEB %d (%d)",
  794. e1->pnum, e2->pnum, err);
  795. spin_lock(&ubi->wl_lock);
  796. if (protect)
  797. prot_queue_add(ubi, e1);
  798. else if (erroneous) {
  799. wl_tree_add(e1, &ubi->erroneous);
  800. ubi->erroneous_peb_count += 1;
  801. } else if (scrubbing)
  802. wl_tree_add(e1, &ubi->scrub);
  803. else
  804. wl_tree_add(e1, &ubi->used);
  805. if (dst_leb_clean) {
  806. wl_tree_add(e2, &ubi->free);
  807. ubi->free_count++;
  808. }
  809. ubi_assert(!ubi->move_to_put);
  810. ubi->move_from = ubi->move_to = NULL;
  811. ubi->wl_scheduled = 0;
  812. spin_unlock(&ubi->wl_lock);
  813. ubi_free_vid_hdr(ubi, vid_hdr);
  814. if (dst_leb_clean) {
  815. ensure_wear_leveling(ubi, 1);
  816. } else {
  817. err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  818. if (err)
  819. goto out_ro;
  820. }
  821. mutex_unlock(&ubi->move_mutex);
  822. return 0;
  823. out_error:
  824. if (vol_id != -1)
  825. ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
  826. err, e1->pnum, e2->pnum);
  827. else
  828. ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
  829. err, e1->pnum, vol_id, lnum, e2->pnum);
  830. spin_lock(&ubi->wl_lock);
  831. ubi->move_from = ubi->move_to = NULL;
  832. ubi->move_to_put = ubi->wl_scheduled = 0;
  833. spin_unlock(&ubi->wl_lock);
  834. ubi_free_vid_hdr(ubi, vid_hdr);
  835. wl_entry_destroy(ubi, e1);
  836. wl_entry_destroy(ubi, e2);
  837. out_ro:
  838. ubi_ro_mode(ubi);
  839. mutex_unlock(&ubi->move_mutex);
  840. ubi_assert(err != 0);
  841. return err < 0 ? err : -EIO;
  842. out_cancel:
  843. ubi->wl_scheduled = 0;
  844. spin_unlock(&ubi->wl_lock);
  845. mutex_unlock(&ubi->move_mutex);
  846. ubi_free_vid_hdr(ubi, vid_hdr);
  847. return 0;
  848. }
  849. /**
  850. * ensure_wear_leveling - schedule wear-leveling if it is needed.
  851. * @ubi: UBI device description object
  852. * @nested: set to non-zero if this function is called from UBI worker
  853. *
  854. * This function checks if it is time to start wear-leveling and schedules it
  855. * if yes. This function returns zero in case of success and a negative error
  856. * code in case of failure.
  857. */
  858. static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
  859. {
  860. int err = 0;
  861. struct ubi_wl_entry *e1;
  862. struct ubi_wl_entry *e2;
  863. struct ubi_work *wrk;
  864. spin_lock(&ubi->wl_lock);
  865. if (ubi->wl_scheduled)
  866. /* Wear-leveling is already in the work queue */
  867. goto out_unlock;
  868. /*
  869. * If the ubi->scrub tree is not empty, scrubbing is needed, and the
  870. * the WL worker has to be scheduled anyway.
  871. */
  872. if (!ubi->scrub.rb_node) {
  873. if (!ubi->used.rb_node || !ubi->free.rb_node)
  874. /* No physical eraseblocks - no deal */
  875. goto out_unlock;
  876. /*
  877. * We schedule wear-leveling only if the difference between the
  878. * lowest erase counter of used physical eraseblocks and a high
  879. * erase counter of free physical eraseblocks is greater than
  880. * %UBI_WL_THRESHOLD.
  881. */
  882. e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  883. e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  884. if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
  885. goto out_unlock;
  886. dbg_wl("schedule wear-leveling");
  887. } else
  888. dbg_wl("schedule scrubbing");
  889. ubi->wl_scheduled = 1;
  890. spin_unlock(&ubi->wl_lock);
  891. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  892. if (!wrk) {
  893. err = -ENOMEM;
  894. goto out_cancel;
  895. }
  896. wrk->anchor = 0;
  897. wrk->func = &wear_leveling_worker;
  898. if (nested)
  899. __schedule_ubi_work(ubi, wrk);
  900. else
  901. schedule_ubi_work(ubi, wrk);
  902. return err;
  903. out_cancel:
  904. spin_lock(&ubi->wl_lock);
  905. ubi->wl_scheduled = 0;
  906. out_unlock:
  907. spin_unlock(&ubi->wl_lock);
  908. return err;
  909. }
  910. /**
  911. * __erase_worker - physical eraseblock erase worker function.
  912. * @ubi: UBI device description object
  913. * @wl_wrk: the work object
  914. * @shutdown: non-zero if the worker has to free memory and exit
  915. * because the WL sub-system is shutting down
  916. *
  917. * This function erases a physical eraseblock and perform torture testing if
  918. * needed. It also takes care about marking the physical eraseblock bad if
  919. * needed. Returns zero in case of success and a negative error code in case of
  920. * failure.
  921. */
  922. static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
  923. {
  924. struct ubi_wl_entry *e = wl_wrk->e;
  925. int pnum = e->pnum;
  926. int vol_id = wl_wrk->vol_id;
  927. int lnum = wl_wrk->lnum;
  928. int err, available_consumed = 0;
  929. dbg_wl("erase PEB %d EC %d LEB %d:%d",
  930. pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
  931. err = sync_erase(ubi, e, wl_wrk->torture);
  932. if (!err) {
  933. spin_lock(&ubi->wl_lock);
  934. wl_tree_add(e, &ubi->free);
  935. ubi->free_count++;
  936. spin_unlock(&ubi->wl_lock);
  937. /*
  938. * One more erase operation has happened, take care about
  939. * protected physical eraseblocks.
  940. */
  941. serve_prot_queue(ubi);
  942. /* And take care about wear-leveling */
  943. err = ensure_wear_leveling(ubi, 1);
  944. return err;
  945. }
  946. ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
  947. if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
  948. err == -EBUSY) {
  949. int err1;
  950. /* Re-schedule the LEB for erasure */
  951. err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
  952. if (err1) {
  953. wl_entry_destroy(ubi, e);
  954. err = err1;
  955. goto out_ro;
  956. }
  957. return err;
  958. }
  959. wl_entry_destroy(ubi, e);
  960. if (err != -EIO)
  961. /*
  962. * If this is not %-EIO, we have no idea what to do. Scheduling
  963. * this physical eraseblock for erasure again would cause
  964. * errors again and again. Well, lets switch to R/O mode.
  965. */
  966. goto out_ro;
  967. /* It is %-EIO, the PEB went bad */
  968. if (!ubi->bad_allowed) {
  969. ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
  970. goto out_ro;
  971. }
  972. spin_lock(&ubi->volumes_lock);
  973. if (ubi->beb_rsvd_pebs == 0) {
  974. if (ubi->avail_pebs == 0) {
  975. spin_unlock(&ubi->volumes_lock);
  976. ubi_err(ubi, "no reserved/available physical eraseblocks");
  977. goto out_ro;
  978. }
  979. ubi->avail_pebs -= 1;
  980. available_consumed = 1;
  981. }
  982. spin_unlock(&ubi->volumes_lock);
  983. ubi_msg(ubi, "mark PEB %d as bad", pnum);
  984. err = ubi_io_mark_bad(ubi, pnum);
  985. if (err)
  986. goto out_ro;
  987. spin_lock(&ubi->volumes_lock);
  988. if (ubi->beb_rsvd_pebs > 0) {
  989. if (available_consumed) {
  990. /*
  991. * The amount of reserved PEBs increased since we last
  992. * checked.
  993. */
  994. ubi->avail_pebs += 1;
  995. available_consumed = 0;
  996. }
  997. ubi->beb_rsvd_pebs -= 1;
  998. }
  999. ubi->bad_peb_count += 1;
  1000. ubi->good_peb_count -= 1;
  1001. ubi_calculate_reserved(ubi);
  1002. if (available_consumed)
  1003. ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
  1004. else if (ubi->beb_rsvd_pebs)
  1005. ubi_msg(ubi, "%d PEBs left in the reserve",
  1006. ubi->beb_rsvd_pebs);
  1007. else
  1008. ubi_warn(ubi, "last PEB from the reserve was used");
  1009. spin_unlock(&ubi->volumes_lock);
  1010. return err;
  1011. out_ro:
  1012. if (available_consumed) {
  1013. spin_lock(&ubi->volumes_lock);
  1014. ubi->avail_pebs += 1;
  1015. spin_unlock(&ubi->volumes_lock);
  1016. }
  1017. ubi_ro_mode(ubi);
  1018. return err;
  1019. }
  1020. static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  1021. int shutdown)
  1022. {
  1023. int ret;
  1024. if (shutdown) {
  1025. struct ubi_wl_entry *e = wl_wrk->e;
  1026. dbg_wl("cancel erasure of PEB %d EC %d", e->pnum, e->ec);
  1027. kfree(wl_wrk);
  1028. wl_entry_destroy(ubi, e);
  1029. return 0;
  1030. }
  1031. ret = __erase_worker(ubi, wl_wrk);
  1032. kfree(wl_wrk);
  1033. return ret;
  1034. }
  1035. /**
  1036. * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  1037. * @ubi: UBI device description object
  1038. * @vol_id: the volume ID that last used this PEB
  1039. * @lnum: the last used logical eraseblock number for the PEB
  1040. * @pnum: physical eraseblock to return
  1041. * @torture: if this physical eraseblock has to be tortured
  1042. *
  1043. * This function is called to return physical eraseblock @pnum to the pool of
  1044. * free physical eraseblocks. The @torture flag has to be set if an I/O error
  1045. * occurred to this @pnum and it has to be tested. This function returns zero
  1046. * in case of success, and a negative error code in case of failure.
  1047. */
  1048. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  1049. int pnum, int torture)
  1050. {
  1051. int err;
  1052. struct ubi_wl_entry *e;
  1053. dbg_wl("PEB %d", pnum);
  1054. ubi_assert(pnum >= 0);
  1055. ubi_assert(pnum < ubi->peb_count);
  1056. down_read(&ubi->fm_protect);
  1057. retry:
  1058. spin_lock(&ubi->wl_lock);
  1059. e = ubi->lookuptbl[pnum];
  1060. if (e == ubi->move_from) {
  1061. /*
  1062. * User is putting the physical eraseblock which was selected to
  1063. * be moved. It will be scheduled for erasure in the
  1064. * wear-leveling worker.
  1065. */
  1066. dbg_wl("PEB %d is being moved, wait", pnum);
  1067. spin_unlock(&ubi->wl_lock);
  1068. /* Wait for the WL worker by taking the @ubi->move_mutex */
  1069. mutex_lock(&ubi->move_mutex);
  1070. mutex_unlock(&ubi->move_mutex);
  1071. goto retry;
  1072. } else if (e == ubi->move_to) {
  1073. /*
  1074. * User is putting the physical eraseblock which was selected
  1075. * as the target the data is moved to. It may happen if the EBA
  1076. * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
  1077. * but the WL sub-system has not put the PEB to the "used" tree
  1078. * yet, but it is about to do this. So we just set a flag which
  1079. * will tell the WL worker that the PEB is not needed anymore
  1080. * and should be scheduled for erasure.
  1081. */
  1082. dbg_wl("PEB %d is the target of data moving", pnum);
  1083. ubi_assert(!ubi->move_to_put);
  1084. ubi->move_to_put = 1;
  1085. spin_unlock(&ubi->wl_lock);
  1086. up_read(&ubi->fm_protect);
  1087. return 0;
  1088. } else {
  1089. if (in_wl_tree(e, &ubi->used)) {
  1090. self_check_in_wl_tree(ubi, e, &ubi->used);
  1091. rb_erase(&e->u.rb, &ubi->used);
  1092. } else if (in_wl_tree(e, &ubi->scrub)) {
  1093. self_check_in_wl_tree(ubi, e, &ubi->scrub);
  1094. rb_erase(&e->u.rb, &ubi->scrub);
  1095. } else if (in_wl_tree(e, &ubi->erroneous)) {
  1096. self_check_in_wl_tree(ubi, e, &ubi->erroneous);
  1097. rb_erase(&e->u.rb, &ubi->erroneous);
  1098. ubi->erroneous_peb_count -= 1;
  1099. ubi_assert(ubi->erroneous_peb_count >= 0);
  1100. /* Erroneous PEBs should be tortured */
  1101. torture = 1;
  1102. } else {
  1103. err = prot_queue_del(ubi, e->pnum);
  1104. if (err) {
  1105. ubi_err(ubi, "PEB %d not found", pnum);
  1106. ubi_ro_mode(ubi);
  1107. spin_unlock(&ubi->wl_lock);
  1108. up_read(&ubi->fm_protect);
  1109. return err;
  1110. }
  1111. }
  1112. }
  1113. spin_unlock(&ubi->wl_lock);
  1114. err = schedule_erase(ubi, e, vol_id, lnum, torture);
  1115. if (err) {
  1116. spin_lock(&ubi->wl_lock);
  1117. wl_tree_add(e, &ubi->used);
  1118. spin_unlock(&ubi->wl_lock);
  1119. }
  1120. up_read(&ubi->fm_protect);
  1121. return err;
  1122. }
  1123. /**
  1124. * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
  1125. * @ubi: UBI device description object
  1126. * @pnum: the physical eraseblock to schedule
  1127. *
  1128. * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
  1129. * needs scrubbing. This function schedules a physical eraseblock for
  1130. * scrubbing which is done in background. This function returns zero in case of
  1131. * success and a negative error code in case of failure.
  1132. */
  1133. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
  1134. {
  1135. struct ubi_wl_entry *e;
  1136. ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
  1137. retry:
  1138. spin_lock(&ubi->wl_lock);
  1139. e = ubi->lookuptbl[pnum];
  1140. if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
  1141. in_wl_tree(e, &ubi->erroneous)) {
  1142. spin_unlock(&ubi->wl_lock);
  1143. return 0;
  1144. }
  1145. if (e == ubi->move_to) {
  1146. /*
  1147. * This physical eraseblock was used to move data to. The data
  1148. * was moved but the PEB was not yet inserted to the proper
  1149. * tree. We should just wait a little and let the WL worker
  1150. * proceed.
  1151. */
  1152. spin_unlock(&ubi->wl_lock);
  1153. dbg_wl("the PEB %d is not in proper tree, retry", pnum);
  1154. yield();
  1155. goto retry;
  1156. }
  1157. if (in_wl_tree(e, &ubi->used)) {
  1158. self_check_in_wl_tree(ubi, e, &ubi->used);
  1159. rb_erase(&e->u.rb, &ubi->used);
  1160. } else {
  1161. int err;
  1162. err = prot_queue_del(ubi, e->pnum);
  1163. if (err) {
  1164. ubi_err(ubi, "PEB %d not found", pnum);
  1165. ubi_ro_mode(ubi);
  1166. spin_unlock(&ubi->wl_lock);
  1167. return err;
  1168. }
  1169. }
  1170. wl_tree_add(e, &ubi->scrub);
  1171. spin_unlock(&ubi->wl_lock);
  1172. /*
  1173. * Technically scrubbing is the same as wear-leveling, so it is done
  1174. * by the WL worker.
  1175. */
  1176. return ensure_wear_leveling(ubi, 0);
  1177. }
  1178. /**
  1179. * ubi_wl_flush - flush all pending works.
  1180. * @ubi: UBI device description object
  1181. * @vol_id: the volume id to flush for
  1182. * @lnum: the logical eraseblock number to flush for
  1183. *
  1184. * This function executes all pending works for a particular volume id /
  1185. * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
  1186. * acts as a wildcard for all of the corresponding volume numbers or logical
  1187. * eraseblock numbers. It returns zero in case of success and a negative error
  1188. * code in case of failure.
  1189. */
  1190. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
  1191. {
  1192. int err = 0;
  1193. int found = 1;
  1194. /*
  1195. * Erase while the pending works queue is not empty, but not more than
  1196. * the number of currently pending works.
  1197. */
  1198. dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
  1199. vol_id, lnum, ubi->works_count);
  1200. while (found) {
  1201. struct ubi_work *wrk, *tmp;
  1202. found = 0;
  1203. down_read(&ubi->work_sem);
  1204. spin_lock(&ubi->wl_lock);
  1205. list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
  1206. if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
  1207. (lnum == UBI_ALL || wrk->lnum == lnum)) {
  1208. list_del(&wrk->list);
  1209. ubi->works_count -= 1;
  1210. ubi_assert(ubi->works_count >= 0);
  1211. spin_unlock(&ubi->wl_lock);
  1212. err = wrk->func(ubi, wrk, 0);
  1213. if (err) {
  1214. up_read(&ubi->work_sem);
  1215. return err;
  1216. }
  1217. spin_lock(&ubi->wl_lock);
  1218. found = 1;
  1219. break;
  1220. }
  1221. }
  1222. spin_unlock(&ubi->wl_lock);
  1223. up_read(&ubi->work_sem);
  1224. }
  1225. /*
  1226. * Make sure all the works which have been done in parallel are
  1227. * finished.
  1228. */
  1229. down_write(&ubi->work_sem);
  1230. up_write(&ubi->work_sem);
  1231. return err;
  1232. }
  1233. /**
  1234. * tree_destroy - destroy an RB-tree.
  1235. * @ubi: UBI device description object
  1236. * @root: the root of the tree to destroy
  1237. */
  1238. static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
  1239. {
  1240. struct rb_node *rb;
  1241. struct ubi_wl_entry *e;
  1242. rb = root->rb_node;
  1243. while (rb) {
  1244. if (rb->rb_left)
  1245. rb = rb->rb_left;
  1246. else if (rb->rb_right)
  1247. rb = rb->rb_right;
  1248. else {
  1249. e = rb_entry(rb, struct ubi_wl_entry, u.rb);
  1250. rb = rb_parent(rb);
  1251. if (rb) {
  1252. if (rb->rb_left == &e->u.rb)
  1253. rb->rb_left = NULL;
  1254. else
  1255. rb->rb_right = NULL;
  1256. }
  1257. wl_entry_destroy(ubi, e);
  1258. }
  1259. }
  1260. }
  1261. /**
  1262. * ubi_thread - UBI background thread.
  1263. * @u: the UBI device description object pointer
  1264. */
  1265. int ubi_thread(void *u)
  1266. {
  1267. int failures = 0;
  1268. struct ubi_device *ubi = u;
  1269. ubi_msg(ubi, "background thread \"%s\" started, PID %d",
  1270. ubi->bgt_name, task_pid_nr(current));
  1271. set_freezable();
  1272. for (;;) {
  1273. int err;
  1274. if (kthread_should_stop())
  1275. break;
  1276. if (try_to_freeze())
  1277. continue;
  1278. spin_lock(&ubi->wl_lock);
  1279. if (list_empty(&ubi->works) || ubi->ro_mode ||
  1280. !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
  1281. set_current_state(TASK_INTERRUPTIBLE);
  1282. spin_unlock(&ubi->wl_lock);
  1283. schedule();
  1284. continue;
  1285. }
  1286. spin_unlock(&ubi->wl_lock);
  1287. err = do_work(ubi);
  1288. if (err) {
  1289. ubi_err(ubi, "%s: work failed with error code %d",
  1290. ubi->bgt_name, err);
  1291. if (failures++ > WL_MAX_FAILURES) {
  1292. /*
  1293. * Too many failures, disable the thread and
  1294. * switch to read-only mode.
  1295. */
  1296. ubi_msg(ubi, "%s: %d consecutive failures",
  1297. ubi->bgt_name, WL_MAX_FAILURES);
  1298. ubi_ro_mode(ubi);
  1299. ubi->thread_enabled = 0;
  1300. continue;
  1301. }
  1302. } else
  1303. failures = 0;
  1304. cond_resched();
  1305. }
  1306. dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
  1307. return 0;
  1308. }
  1309. /**
  1310. * shutdown_work - shutdown all pending works.
  1311. * @ubi: UBI device description object
  1312. */
  1313. static void shutdown_work(struct ubi_device *ubi)
  1314. {
  1315. #ifdef CONFIG_MTD_UBI_FASTMAP
  1316. flush_work(&ubi->fm_work);
  1317. #endif
  1318. while (!list_empty(&ubi->works)) {
  1319. struct ubi_work *wrk;
  1320. wrk = list_entry(ubi->works.next, struct ubi_work, list);
  1321. list_del(&wrk->list);
  1322. wrk->func(ubi, wrk, 1);
  1323. ubi->works_count -= 1;
  1324. ubi_assert(ubi->works_count >= 0);
  1325. }
  1326. }
  1327. /**
  1328. * ubi_wl_init - initialize the WL sub-system using attaching information.
  1329. * @ubi: UBI device description object
  1330. * @ai: attaching information
  1331. *
  1332. * This function returns zero in case of success, and a negative error code in
  1333. * case of failure.
  1334. */
  1335. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1336. {
  1337. int err, i, reserved_pebs, found_pebs = 0;
  1338. struct rb_node *rb1, *rb2;
  1339. struct ubi_ainf_volume *av;
  1340. struct ubi_ainf_peb *aeb, *tmp;
  1341. struct ubi_wl_entry *e;
  1342. ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
  1343. spin_lock_init(&ubi->wl_lock);
  1344. mutex_init(&ubi->move_mutex);
  1345. init_rwsem(&ubi->work_sem);
  1346. ubi->max_ec = ai->max_ec;
  1347. INIT_LIST_HEAD(&ubi->works);
  1348. sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
  1349. err = -ENOMEM;
  1350. ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
  1351. if (!ubi->lookuptbl)
  1352. return err;
  1353. for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
  1354. INIT_LIST_HEAD(&ubi->pq[i]);
  1355. ubi->pq_head = 0;
  1356. list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
  1357. cond_resched();
  1358. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1359. if (!e)
  1360. goto out_free;
  1361. e->pnum = aeb->pnum;
  1362. e->ec = aeb->ec;
  1363. ubi->lookuptbl[e->pnum] = e;
  1364. if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
  1365. wl_entry_destroy(ubi, e);
  1366. goto out_free;
  1367. }
  1368. found_pebs++;
  1369. }
  1370. ubi->free_count = 0;
  1371. list_for_each_entry(aeb, &ai->free, u.list) {
  1372. cond_resched();
  1373. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1374. if (!e)
  1375. goto out_free;
  1376. e->pnum = aeb->pnum;
  1377. e->ec = aeb->ec;
  1378. ubi_assert(e->ec >= 0);
  1379. wl_tree_add(e, &ubi->free);
  1380. ubi->free_count++;
  1381. ubi->lookuptbl[e->pnum] = e;
  1382. found_pebs++;
  1383. }
  1384. ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
  1385. ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
  1386. cond_resched();
  1387. e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
  1388. if (!e)
  1389. goto out_free;
  1390. e->pnum = aeb->pnum;
  1391. e->ec = aeb->ec;
  1392. ubi->lookuptbl[e->pnum] = e;
  1393. if (!aeb->scrub) {
  1394. dbg_wl("add PEB %d EC %d to the used tree",
  1395. e->pnum, e->ec);
  1396. wl_tree_add(e, &ubi->used);
  1397. } else {
  1398. dbg_wl("add PEB %d EC %d to the scrub tree",
  1399. e->pnum, e->ec);
  1400. wl_tree_add(e, &ubi->scrub);
  1401. }
  1402. found_pebs++;
  1403. }
  1404. }
  1405. dbg_wl("found %i PEBs", found_pebs);
  1406. if (ubi->fm) {
  1407. ubi_assert(ubi->good_peb_count ==
  1408. found_pebs + ubi->fm->used_blocks);
  1409. for (i = 0; i < ubi->fm->used_blocks; i++) {
  1410. e = ubi->fm->e[i];
  1411. ubi->lookuptbl[e->pnum] = e;
  1412. }
  1413. }
  1414. else
  1415. ubi_assert(ubi->good_peb_count == found_pebs);
  1416. reserved_pebs = WL_RESERVED_PEBS;
  1417. ubi_fastmap_init(ubi, &reserved_pebs);
  1418. if (ubi->avail_pebs < reserved_pebs) {
  1419. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1420. ubi->avail_pebs, reserved_pebs);
  1421. if (ubi->corr_peb_count)
  1422. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1423. ubi->corr_peb_count);
  1424. err = -ENOSPC;
  1425. goto out_free;
  1426. }
  1427. ubi->avail_pebs -= reserved_pebs;
  1428. ubi->rsvd_pebs += reserved_pebs;
  1429. /* Schedule wear-leveling if needed */
  1430. err = ensure_wear_leveling(ubi, 0);
  1431. if (err)
  1432. goto out_free;
  1433. return 0;
  1434. out_free:
  1435. shutdown_work(ubi);
  1436. tree_destroy(ubi, &ubi->used);
  1437. tree_destroy(ubi, &ubi->free);
  1438. tree_destroy(ubi, &ubi->scrub);
  1439. kfree(ubi->lookuptbl);
  1440. return err;
  1441. }
  1442. /**
  1443. * protection_queue_destroy - destroy the protection queue.
  1444. * @ubi: UBI device description object
  1445. */
  1446. static void protection_queue_destroy(struct ubi_device *ubi)
  1447. {
  1448. int i;
  1449. struct ubi_wl_entry *e, *tmp;
  1450. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
  1451. list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
  1452. list_del(&e->u.list);
  1453. wl_entry_destroy(ubi, e);
  1454. }
  1455. }
  1456. }
  1457. /**
  1458. * ubi_wl_close - close the wear-leveling sub-system.
  1459. * @ubi: UBI device description object
  1460. */
  1461. void ubi_wl_close(struct ubi_device *ubi)
  1462. {
  1463. dbg_wl("close the WL sub-system");
  1464. ubi_fastmap_close(ubi);
  1465. shutdown_work(ubi);
  1466. protection_queue_destroy(ubi);
  1467. tree_destroy(ubi, &ubi->used);
  1468. tree_destroy(ubi, &ubi->erroneous);
  1469. tree_destroy(ubi, &ubi->free);
  1470. tree_destroy(ubi, &ubi->scrub);
  1471. kfree(ubi->lookuptbl);
  1472. }
  1473. /**
  1474. * self_check_ec - make sure that the erase counter of a PEB is correct.
  1475. * @ubi: UBI device description object
  1476. * @pnum: the physical eraseblock number to check
  1477. * @ec: the erase counter to check
  1478. *
  1479. * This function returns zero if the erase counter of physical eraseblock @pnum
  1480. * is equivalent to @ec, and a negative error code if not or if an error
  1481. * occurred.
  1482. */
  1483. static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
  1484. {
  1485. int err;
  1486. long long read_ec;
  1487. struct ubi_ec_hdr *ec_hdr;
  1488. if (!ubi_dbg_chk_gen(ubi))
  1489. return 0;
  1490. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
  1491. if (!ec_hdr)
  1492. return -ENOMEM;
  1493. err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
  1494. if (err && err != UBI_IO_BITFLIPS) {
  1495. /* The header does not have to exist */
  1496. err = 0;
  1497. goto out_free;
  1498. }
  1499. read_ec = be64_to_cpu(ec_hdr->ec);
  1500. if (ec != read_ec && read_ec - ec > 1) {
  1501. ubi_err(ubi, "self-check failed for PEB %d", pnum);
  1502. ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
  1503. dump_stack();
  1504. err = 1;
  1505. } else
  1506. err = 0;
  1507. out_free:
  1508. kfree(ec_hdr);
  1509. return err;
  1510. }
  1511. /**
  1512. * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
  1513. * @ubi: UBI device description object
  1514. * @e: the wear-leveling entry to check
  1515. * @root: the root of the tree
  1516. *
  1517. * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
  1518. * is not.
  1519. */
  1520. static int self_check_in_wl_tree(const struct ubi_device *ubi,
  1521. struct ubi_wl_entry *e, struct rb_root *root)
  1522. {
  1523. if (!ubi_dbg_chk_gen(ubi))
  1524. return 0;
  1525. if (in_wl_tree(e, root))
  1526. return 0;
  1527. ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
  1528. e->pnum, e->ec, root);
  1529. dump_stack();
  1530. return -EINVAL;
  1531. }
  1532. /**
  1533. * self_check_in_pq - check if wear-leveling entry is in the protection
  1534. * queue.
  1535. * @ubi: UBI device description object
  1536. * @e: the wear-leveling entry to check
  1537. *
  1538. * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
  1539. */
  1540. static int self_check_in_pq(const struct ubi_device *ubi,
  1541. struct ubi_wl_entry *e)
  1542. {
  1543. struct ubi_wl_entry *p;
  1544. int i;
  1545. if (!ubi_dbg_chk_gen(ubi))
  1546. return 0;
  1547. for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
  1548. list_for_each_entry(p, &ubi->pq[i], u.list)
  1549. if (p == e)
  1550. return 0;
  1551. ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
  1552. e->pnum, e->ec);
  1553. dump_stack();
  1554. return -EINVAL;
  1555. }
  1556. #ifndef CONFIG_MTD_UBI_FASTMAP
  1557. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  1558. {
  1559. struct ubi_wl_entry *e;
  1560. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  1561. self_check_in_wl_tree(ubi, e, &ubi->free);
  1562. ubi->free_count--;
  1563. ubi_assert(ubi->free_count >= 0);
  1564. rb_erase(&e->u.rb, &ubi->free);
  1565. return e;
  1566. }
  1567. /**
  1568. * produce_free_peb - produce a free physical eraseblock.
  1569. * @ubi: UBI device description object
  1570. *
  1571. * This function tries to make a free PEB by means of synchronous execution of
  1572. * pending works. This may be needed if, for example the background thread is
  1573. * disabled. Returns zero in case of success and a negative error code in case
  1574. * of failure.
  1575. */
  1576. static int produce_free_peb(struct ubi_device *ubi)
  1577. {
  1578. int err;
  1579. while (!ubi->free.rb_node && ubi->works_count) {
  1580. spin_unlock(&ubi->wl_lock);
  1581. dbg_wl("do one work synchronously");
  1582. err = do_work(ubi);
  1583. spin_lock(&ubi->wl_lock);
  1584. if (err)
  1585. return err;
  1586. }
  1587. return 0;
  1588. }
  1589. /**
  1590. * ubi_wl_get_peb - get a physical eraseblock.
  1591. * @ubi: UBI device description object
  1592. *
  1593. * This function returns a physical eraseblock in case of success and a
  1594. * negative error code in case of failure.
  1595. * Returns with ubi->fm_eba_sem held in read mode!
  1596. */
  1597. int ubi_wl_get_peb(struct ubi_device *ubi)
  1598. {
  1599. int err;
  1600. struct ubi_wl_entry *e;
  1601. retry:
  1602. down_read(&ubi->fm_eba_sem);
  1603. spin_lock(&ubi->wl_lock);
  1604. if (!ubi->free.rb_node) {
  1605. if (ubi->works_count == 0) {
  1606. ubi_err(ubi, "no free eraseblocks");
  1607. ubi_assert(list_empty(&ubi->works));
  1608. spin_unlock(&ubi->wl_lock);
  1609. return -ENOSPC;
  1610. }
  1611. err = produce_free_peb(ubi);
  1612. if (err < 0) {
  1613. spin_unlock(&ubi->wl_lock);
  1614. return err;
  1615. }
  1616. spin_unlock(&ubi->wl_lock);
  1617. up_read(&ubi->fm_eba_sem);
  1618. goto retry;
  1619. }
  1620. e = wl_get_wle(ubi);
  1621. prot_queue_add(ubi, e);
  1622. spin_unlock(&ubi->wl_lock);
  1623. err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
  1624. ubi->peb_size - ubi->vid_hdr_aloffset);
  1625. if (err) {
  1626. ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);
  1627. return err;
  1628. }
  1629. return e->pnum;
  1630. }
  1631. #else
  1632. #include "fastmap-wl.c"
  1633. #endif