wl.c 45 KB

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