wl.c 45 KB

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