wl.c 44 KB

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