wl.c 43 KB

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