wl.c 43 KB

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