wl.c 45 KB

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