wl.c 44 KB

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