wl.c 43 KB

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