wl.c 44 KB

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