eba.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) International Business Machines Corp., 2006
  4. *
  5. * Author: Artem Bityutskiy (Битюцкий Артём)
  6. */
  7. /*
  8. * The UBI Eraseblock Association (EBA) sub-system.
  9. *
  10. * This sub-system is responsible for I/O to/from logical eraseblock.
  11. *
  12. * Although in this implementation the EBA table is fully kept and managed in
  13. * RAM, which assumes poor scalability, it might be (partially) maintained on
  14. * flash in future implementations.
  15. *
  16. * The EBA sub-system implements per-logical eraseblock locking. Before
  17. * accessing a logical eraseblock it is locked for reading or writing. The
  18. * per-logical eraseblock locking is implemented by means of the lock tree. The
  19. * lock tree is an RB-tree which refers all the currently locked logical
  20. * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
  21. * They are indexed by (@vol_id, @lnum) pairs.
  22. *
  23. * EBA also maintains the global sequence counter which is incremented each
  24. * time a logical eraseblock is mapped to a physical eraseblock and it is
  25. * stored in the volume identifier header. This means that each VID header has
  26. * a unique sequence number. The sequence number is only increased an we assume
  27. * 64 bits is enough to never overflow.
  28. */
  29. #include <linux/slab.h>
  30. #include <linux/crc32.h>
  31. #include <linux/err.h>
  32. #include "ubi.h"
  33. /**
  34. * struct ubi_eba_entry - structure encoding a single LEB -> PEB association
  35. * @pnum: the physical eraseblock number attached to the LEB
  36. *
  37. * This structure is encoding a LEB -> PEB association. Note that the LEB
  38. * number is not stored here, because it is the index used to access the
  39. * entries table.
  40. */
  41. struct ubi_eba_entry {
  42. int pnum;
  43. };
  44. /**
  45. * struct ubi_eba_table - LEB -> PEB association information
  46. * @entries: the LEB to PEB mapping (one entry per LEB).
  47. *
  48. * This structure is private to the EBA logic and should be kept here.
  49. * It is encoding the LEB to PEB association table, and is subject to
  50. * changes.
  51. */
  52. struct ubi_eba_table {
  53. struct ubi_eba_entry *entries;
  54. };
  55. /**
  56. * ubi_next_sqnum - get next sequence number.
  57. * @ubi: UBI device description object
  58. *
  59. * This function returns next sequence number to use, which is just the current
  60. * global sequence counter value. It also increases the global sequence
  61. * counter.
  62. */
  63. unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
  64. {
  65. unsigned long long sqnum;
  66. spin_lock(&ubi->ltree_lock);
  67. sqnum = ubi->global_sqnum++;
  68. spin_unlock(&ubi->ltree_lock);
  69. return sqnum;
  70. }
  71. /**
  72. * ubi_get_compat - get compatibility flags of a volume.
  73. * @ubi: UBI device description object
  74. * @vol_id: volume ID
  75. *
  76. * This function returns compatibility flags for an internal volume. User
  77. * volumes have no compatibility flags, so %0 is returned.
  78. */
  79. static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
  80. {
  81. if (vol_id == UBI_LAYOUT_VOLUME_ID)
  82. return UBI_LAYOUT_VOLUME_COMPAT;
  83. return 0;
  84. }
  85. /**
  86. * ubi_eba_get_ldesc - get information about a LEB
  87. * @vol: volume description object
  88. * @lnum: logical eraseblock number
  89. * @ldesc: the LEB descriptor to fill
  90. *
  91. * Used to query information about a specific LEB.
  92. * It is currently only returning the physical position of the LEB, but will be
  93. * extended to provide more information.
  94. */
  95. void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
  96. struct ubi_eba_leb_desc *ldesc)
  97. {
  98. ldesc->lnum = lnum;
  99. ldesc->pnum = vol->eba_tbl->entries[lnum].pnum;
  100. }
  101. /**
  102. * ubi_eba_create_table - allocate a new EBA table and initialize it with all
  103. * LEBs unmapped
  104. * @vol: volume containing the EBA table to copy
  105. * @nentries: number of entries in the table
  106. *
  107. * Allocate a new EBA table and initialize it with all LEBs unmapped.
  108. * Returns a valid pointer if it succeed, an ERR_PTR() otherwise.
  109. */
  110. struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
  111. int nentries)
  112. {
  113. struct ubi_eba_table *tbl;
  114. int err = -ENOMEM;
  115. int i;
  116. tbl = kzalloc_obj(*tbl);
  117. if (!tbl)
  118. return ERR_PTR(-ENOMEM);
  119. tbl->entries = kmalloc_objs(*tbl->entries, nentries);
  120. if (!tbl->entries)
  121. goto err;
  122. for (i = 0; i < nentries; i++)
  123. tbl->entries[i].pnum = UBI_LEB_UNMAPPED;
  124. return tbl;
  125. err:
  126. kfree(tbl);
  127. return ERR_PTR(err);
  128. }
  129. /**
  130. * ubi_eba_destroy_table - destroy an EBA table
  131. * @tbl: the table to destroy
  132. *
  133. * Destroy an EBA table.
  134. */
  135. void ubi_eba_destroy_table(struct ubi_eba_table *tbl)
  136. {
  137. if (!tbl)
  138. return;
  139. kfree(tbl->entries);
  140. kfree(tbl);
  141. }
  142. /**
  143. * ubi_eba_copy_table - copy the EBA table attached to vol into another table
  144. * @vol: volume containing the EBA table to copy
  145. * @dst: destination
  146. * @nentries: number of entries to copy
  147. *
  148. * Copy the EBA table stored in vol into the one pointed by dst.
  149. */
  150. void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
  151. int nentries)
  152. {
  153. struct ubi_eba_table *src;
  154. int i;
  155. ubi_assert(dst && vol && vol->eba_tbl);
  156. src = vol->eba_tbl;
  157. for (i = 0; i < nentries; i++)
  158. dst->entries[i].pnum = src->entries[i].pnum;
  159. }
  160. /**
  161. * ubi_eba_replace_table - assign a new EBA table to a volume
  162. * @vol: volume containing the EBA table to copy
  163. * @tbl: new EBA table
  164. *
  165. * Assign a new EBA table to the volume and release the old one.
  166. */
  167. void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl)
  168. {
  169. ubi_eba_destroy_table(vol->eba_tbl);
  170. vol->eba_tbl = tbl;
  171. }
  172. /**
  173. * ltree_lookup - look up the lock tree.
  174. * @ubi: UBI device description object
  175. * @vol_id: volume ID
  176. * @lnum: logical eraseblock number
  177. *
  178. * This function returns a pointer to the corresponding &struct ubi_ltree_entry
  179. * object if the logical eraseblock is locked and %NULL if it is not.
  180. * @ubi->ltree_lock has to be locked.
  181. */
  182. static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
  183. int lnum)
  184. {
  185. struct rb_node *p;
  186. p = ubi->ltree.rb_node;
  187. while (p) {
  188. struct ubi_ltree_entry *le;
  189. le = rb_entry(p, struct ubi_ltree_entry, rb);
  190. if (vol_id < le->vol_id)
  191. p = p->rb_left;
  192. else if (vol_id > le->vol_id)
  193. p = p->rb_right;
  194. else {
  195. if (lnum < le->lnum)
  196. p = p->rb_left;
  197. else if (lnum > le->lnum)
  198. p = p->rb_right;
  199. else
  200. return le;
  201. }
  202. }
  203. return NULL;
  204. }
  205. /**
  206. * ltree_add_entry - add new entry to the lock tree.
  207. * @ubi: UBI device description object
  208. * @vol_id: volume ID
  209. * @lnum: logical eraseblock number
  210. *
  211. * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
  212. * lock tree. If such entry is already there, its usage counter is increased.
  213. * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
  214. * failed.
  215. */
  216. static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
  217. int vol_id, int lnum)
  218. {
  219. struct ubi_ltree_entry *le, *le1, *le_free;
  220. le = kmalloc_obj(struct ubi_ltree_entry, GFP_NOFS);
  221. if (!le)
  222. return ERR_PTR(-ENOMEM);
  223. le->users = 0;
  224. init_rwsem(&le->mutex);
  225. le->vol_id = vol_id;
  226. le->lnum = lnum;
  227. spin_lock(&ubi->ltree_lock);
  228. le1 = ltree_lookup(ubi, vol_id, lnum);
  229. if (le1) {
  230. /*
  231. * This logical eraseblock is already locked. The newly
  232. * allocated lock entry is not needed.
  233. */
  234. le_free = le;
  235. le = le1;
  236. } else {
  237. struct rb_node **p, *parent = NULL;
  238. /*
  239. * No lock entry, add the newly allocated one to the
  240. * @ubi->ltree RB-tree.
  241. */
  242. le_free = NULL;
  243. p = &ubi->ltree.rb_node;
  244. while (*p) {
  245. parent = *p;
  246. le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
  247. if (vol_id < le1->vol_id)
  248. p = &(*p)->rb_left;
  249. else if (vol_id > le1->vol_id)
  250. p = &(*p)->rb_right;
  251. else {
  252. ubi_assert(lnum != le1->lnum);
  253. if (lnum < le1->lnum)
  254. p = &(*p)->rb_left;
  255. else
  256. p = &(*p)->rb_right;
  257. }
  258. }
  259. rb_link_node(&le->rb, parent, p);
  260. rb_insert_color(&le->rb, &ubi->ltree);
  261. }
  262. le->users += 1;
  263. spin_unlock(&ubi->ltree_lock);
  264. kfree(le_free);
  265. return le;
  266. }
  267. /**
  268. * leb_read_lock - lock logical eraseblock for reading.
  269. * @ubi: UBI device description object
  270. * @vol_id: volume ID
  271. * @lnum: logical eraseblock number
  272. *
  273. * This function locks a logical eraseblock for reading. Returns zero in case
  274. * of success and a negative error code in case of failure.
  275. */
  276. static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
  277. {
  278. struct ubi_ltree_entry *le;
  279. le = ltree_add_entry(ubi, vol_id, lnum);
  280. if (IS_ERR(le))
  281. return PTR_ERR(le);
  282. down_read(&le->mutex);
  283. return 0;
  284. }
  285. /**
  286. * leb_read_unlock - unlock logical eraseblock.
  287. * @ubi: UBI device description object
  288. * @vol_id: volume ID
  289. * @lnum: logical eraseblock number
  290. */
  291. static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  292. {
  293. struct ubi_ltree_entry *le;
  294. spin_lock(&ubi->ltree_lock);
  295. le = ltree_lookup(ubi, vol_id, lnum);
  296. le->users -= 1;
  297. ubi_assert(le->users >= 0);
  298. up_read(&le->mutex);
  299. if (le->users == 0) {
  300. rb_erase(&le->rb, &ubi->ltree);
  301. kfree(le);
  302. }
  303. spin_unlock(&ubi->ltree_lock);
  304. }
  305. /**
  306. * leb_write_lock - lock logical eraseblock for writing.
  307. * @ubi: UBI device description object
  308. * @vol_id: volume ID
  309. * @lnum: logical eraseblock number
  310. *
  311. * This function locks a logical eraseblock for writing. Returns zero in case
  312. * of success and a negative error code in case of failure.
  313. */
  314. static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
  315. {
  316. struct ubi_ltree_entry *le;
  317. le = ltree_add_entry(ubi, vol_id, lnum);
  318. if (IS_ERR(le))
  319. return PTR_ERR(le);
  320. down_write(&le->mutex);
  321. return 0;
  322. }
  323. /**
  324. * leb_write_trylock - try to lock logical eraseblock for writing.
  325. * @ubi: UBI device description object
  326. * @vol_id: volume ID
  327. * @lnum: logical eraseblock number
  328. *
  329. * This function locks a logical eraseblock for writing if there is no
  330. * contention and does nothing if there is contention. Returns %0 in case of
  331. * success, %1 in case of contention, and a negative error code in case of
  332. * failure.
  333. */
  334. static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
  335. {
  336. struct ubi_ltree_entry *le;
  337. le = ltree_add_entry(ubi, vol_id, lnum);
  338. if (IS_ERR(le))
  339. return PTR_ERR(le);
  340. if (down_write_trylock(&le->mutex))
  341. return 0;
  342. /* Contention, cancel */
  343. spin_lock(&ubi->ltree_lock);
  344. le->users -= 1;
  345. ubi_assert(le->users >= 0);
  346. if (le->users == 0) {
  347. rb_erase(&le->rb, &ubi->ltree);
  348. kfree(le);
  349. }
  350. spin_unlock(&ubi->ltree_lock);
  351. return 1;
  352. }
  353. /**
  354. * leb_write_unlock - unlock logical eraseblock.
  355. * @ubi: UBI device description object
  356. * @vol_id: volume ID
  357. * @lnum: logical eraseblock number
  358. */
  359. static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  360. {
  361. struct ubi_ltree_entry *le;
  362. spin_lock(&ubi->ltree_lock);
  363. le = ltree_lookup(ubi, vol_id, lnum);
  364. le->users -= 1;
  365. ubi_assert(le->users >= 0);
  366. up_write(&le->mutex);
  367. if (le->users == 0) {
  368. rb_erase(&le->rb, &ubi->ltree);
  369. kfree(le);
  370. }
  371. spin_unlock(&ubi->ltree_lock);
  372. }
  373. /**
  374. * ubi_eba_is_mapped - check if a LEB is mapped.
  375. * @vol: volume description object
  376. * @lnum: logical eraseblock number
  377. *
  378. * This function returns true if the LEB is mapped, false otherwise.
  379. */
  380. bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum)
  381. {
  382. return vol->eba_tbl->entries[lnum].pnum >= 0;
  383. }
  384. /**
  385. * ubi_eba_unmap_leb - un-map logical eraseblock.
  386. * @ubi: UBI device description object
  387. * @vol: volume description object
  388. * @lnum: logical eraseblock number
  389. *
  390. * This function un-maps logical eraseblock @lnum and schedules corresponding
  391. * physical eraseblock for erasure. Returns zero in case of success and a
  392. * negative error code in case of failure.
  393. */
  394. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  395. int lnum)
  396. {
  397. int err, pnum, vol_id = vol->vol_id;
  398. if (ubi->ro_mode)
  399. return -EROFS;
  400. err = leb_write_lock(ubi, vol_id, lnum);
  401. if (err)
  402. return err;
  403. pnum = vol->eba_tbl->entries[lnum].pnum;
  404. if (pnum < 0)
  405. /* This logical eraseblock is already unmapped */
  406. goto out_unlock;
  407. dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
  408. down_read(&ubi->fm_eba_sem);
  409. vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
  410. up_read(&ubi->fm_eba_sem);
  411. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
  412. out_unlock:
  413. leb_write_unlock(ubi, vol_id, lnum);
  414. return err;
  415. }
  416. #ifdef CONFIG_MTD_UBI_FASTMAP
  417. /**
  418. * check_mapping - check and fixup a mapping
  419. * @ubi: UBI device description object
  420. * @vol: volume description object
  421. * @lnum: logical eraseblock number
  422. * @pnum: physical eraseblock number
  423. *
  424. * Checks whether a given mapping is valid. Fastmap cannot track LEB unmap
  425. * operations, if such an operation is interrupted the mapping still looks
  426. * good, but upon first read an ECC is reported to the upper layer.
  427. * Normaly during the full-scan at attach time this is fixed, for Fastmap
  428. * we have to deal with it while reading.
  429. * If the PEB behind a LEB shows this symthom we change the mapping to
  430. * %UBI_LEB_UNMAPPED and schedule the PEB for erasure.
  431. *
  432. * Returns 0 on success, negative error code in case of failure.
  433. */
  434. static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  435. int *pnum)
  436. {
  437. int err;
  438. struct ubi_vid_io_buf *vidb;
  439. struct ubi_vid_hdr *vid_hdr;
  440. if (!ubi->fast_attach)
  441. return 0;
  442. if (!vol->checkmap || test_bit(lnum, vol->checkmap))
  443. return 0;
  444. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  445. if (!vidb)
  446. return -ENOMEM;
  447. err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0);
  448. if (err > 0 && err != UBI_IO_BITFLIPS) {
  449. int torture = 0;
  450. switch (err) {
  451. case UBI_IO_FF:
  452. case UBI_IO_FF_BITFLIPS:
  453. case UBI_IO_BAD_HDR:
  454. case UBI_IO_BAD_HDR_EBADMSG:
  455. break;
  456. default:
  457. ubi_assert(0);
  458. }
  459. if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_FF_BITFLIPS)
  460. torture = 1;
  461. down_read(&ubi->fm_eba_sem);
  462. vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
  463. up_read(&ubi->fm_eba_sem);
  464. ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture);
  465. *pnum = UBI_LEB_UNMAPPED;
  466. } else if (err < 0) {
  467. ubi_err(ubi, "unable to read VID header back from PEB %i: %i",
  468. *pnum, err);
  469. goto out_free;
  470. } else {
  471. int found_vol_id, found_lnum;
  472. ubi_assert(err == 0 || err == UBI_IO_BITFLIPS);
  473. vid_hdr = ubi_get_vid_hdr(vidb);
  474. found_vol_id = be32_to_cpu(vid_hdr->vol_id);
  475. found_lnum = be32_to_cpu(vid_hdr->lnum);
  476. if (found_lnum != lnum || found_vol_id != vol->vol_id) {
  477. ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i",
  478. *pnum, found_vol_id, found_lnum, vol->vol_id, lnum);
  479. ubi_ro_mode(ubi);
  480. err = -EINVAL;
  481. goto out_free;
  482. }
  483. }
  484. set_bit(lnum, vol->checkmap);
  485. err = 0;
  486. out_free:
  487. ubi_free_vid_buf(vidb);
  488. return err;
  489. }
  490. #else
  491. static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  492. int *pnum)
  493. {
  494. return 0;
  495. }
  496. #endif
  497. /**
  498. * ubi_eba_read_leb - read data.
  499. * @ubi: UBI device description object
  500. * @vol: volume description object
  501. * @lnum: logical eraseblock number
  502. * @buf: buffer to store the read data
  503. * @offset: offset from where to read
  504. * @len: how many bytes to read
  505. * @check: data CRC check flag
  506. *
  507. * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
  508. * bytes. The @check flag only makes sense for static volumes and forces
  509. * eraseblock data CRC checking.
  510. *
  511. * In case of success this function returns zero. In case of a static volume,
  512. * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
  513. * returned for any volume type if an ECC error was detected by the MTD device
  514. * driver. Other negative error cored may be returned in case of other errors.
  515. */
  516. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  517. void *buf, int offset, int len, int check)
  518. {
  519. int err, pnum, scrub = 0, vol_id = vol->vol_id;
  520. struct ubi_vid_io_buf *vidb;
  521. struct ubi_vid_hdr *vid_hdr;
  522. uint32_t crc;
  523. err = leb_read_lock(ubi, vol_id, lnum);
  524. if (err)
  525. return err;
  526. pnum = vol->eba_tbl->entries[lnum].pnum;
  527. if (pnum >= 0) {
  528. err = check_mapping(ubi, vol, lnum, &pnum);
  529. if (err < 0)
  530. goto out_unlock;
  531. }
  532. if (pnum == UBI_LEB_UNMAPPED) {
  533. /*
  534. * The logical eraseblock is not mapped, fill the whole buffer
  535. * with 0xFF bytes. The exception is static volumes for which
  536. * it is an error to read unmapped logical eraseblocks.
  537. */
  538. dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
  539. len, offset, vol_id, lnum);
  540. leb_read_unlock(ubi, vol_id, lnum);
  541. ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
  542. memset(buf, 0xFF, len);
  543. return 0;
  544. }
  545. dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
  546. len, offset, vol_id, lnum, pnum);
  547. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  548. check = 0;
  549. retry:
  550. if (check) {
  551. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  552. if (!vidb) {
  553. err = -ENOMEM;
  554. goto out_unlock;
  555. }
  556. vid_hdr = ubi_get_vid_hdr(vidb);
  557. err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
  558. if (err && err != UBI_IO_BITFLIPS) {
  559. if (err > 0) {
  560. /*
  561. * The header is either absent or corrupted.
  562. * The former case means there is a bug -
  563. * switch to read-only mode just in case.
  564. * The latter case means a real corruption - we
  565. * may try to recover data. FIXME: but this is
  566. * not implemented.
  567. */
  568. if (err == UBI_IO_BAD_HDR_EBADMSG ||
  569. err == UBI_IO_BAD_HDR) {
  570. ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
  571. pnum, vol_id, lnum);
  572. err = -EBADMSG;
  573. } else {
  574. /*
  575. * Ending up here in the non-Fastmap case
  576. * is a clear bug as the VID header had to
  577. * be present at scan time to have it referenced.
  578. * With fastmap the story is more complicated.
  579. * Fastmap has the mapping info without the need
  580. * of a full scan. So the LEB could have been
  581. * unmapped, Fastmap cannot know this and keeps
  582. * the LEB referenced.
  583. * This is valid and works as the layer above UBI
  584. * has to do bookkeeping about used/referenced
  585. * LEBs in any case.
  586. */
  587. if (ubi->fast_attach) {
  588. err = -EBADMSG;
  589. } else {
  590. err = -EINVAL;
  591. ubi_ro_mode(ubi);
  592. }
  593. }
  594. }
  595. goto out_free;
  596. } else if (err == UBI_IO_BITFLIPS)
  597. scrub = 1;
  598. ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
  599. ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
  600. crc = be32_to_cpu(vid_hdr->data_crc);
  601. ubi_free_vid_buf(vidb);
  602. }
  603. err = ubi_io_read_data(ubi, buf, pnum, offset, len);
  604. if (err) {
  605. if (err == UBI_IO_BITFLIPS)
  606. scrub = 1;
  607. else if (mtd_is_eccerr(err)) {
  608. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  609. goto out_unlock;
  610. scrub = 1;
  611. if (!check) {
  612. ubi_msg(ubi, "force data checking");
  613. check = 1;
  614. goto retry;
  615. }
  616. } else
  617. goto out_unlock;
  618. }
  619. if (check) {
  620. uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
  621. if (crc1 != crc) {
  622. ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
  623. crc1, crc);
  624. err = -EBADMSG;
  625. goto out_unlock;
  626. }
  627. }
  628. if (scrub)
  629. err = ubi_wl_scrub_peb(ubi, pnum);
  630. leb_read_unlock(ubi, vol_id, lnum);
  631. return err;
  632. out_free:
  633. ubi_free_vid_buf(vidb);
  634. out_unlock:
  635. leb_read_unlock(ubi, vol_id, lnum);
  636. return err;
  637. }
  638. /**
  639. * ubi_eba_read_leb_sg - read data into a scatter gather list.
  640. * @ubi: UBI device description object
  641. * @vol: volume description object
  642. * @lnum: logical eraseblock number
  643. * @sgl: UBI scatter gather list to store the read data
  644. * @offset: offset from where to read
  645. * @len: how many bytes to read
  646. * @check: data CRC check flag
  647. *
  648. * This function works exactly like ubi_eba_read_leb(). But instead of
  649. * storing the read data into a buffer it writes to an UBI scatter gather
  650. * list.
  651. */
  652. int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
  653. struct ubi_sgl *sgl, int lnum, int offset, int len,
  654. int check)
  655. {
  656. int to_read;
  657. int ret;
  658. struct scatterlist *sg;
  659. for (;;) {
  660. ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
  661. sg = &sgl->sg[sgl->list_pos];
  662. if (len < sg->length - sgl->page_pos)
  663. to_read = len;
  664. else
  665. to_read = sg->length - sgl->page_pos;
  666. ret = ubi_eba_read_leb(ubi, vol, lnum,
  667. sg_virt(sg) + sgl->page_pos, offset,
  668. to_read, check);
  669. if (ret < 0)
  670. return ret;
  671. offset += to_read;
  672. len -= to_read;
  673. if (!len) {
  674. sgl->page_pos += to_read;
  675. if (sgl->page_pos == sg->length) {
  676. sgl->list_pos++;
  677. sgl->page_pos = 0;
  678. }
  679. break;
  680. }
  681. sgl->list_pos++;
  682. sgl->page_pos = 0;
  683. }
  684. return ret;
  685. }
  686. /**
  687. * try_recover_peb - try to recover from write failure.
  688. * @vol: volume description object
  689. * @pnum: the physical eraseblock to recover
  690. * @lnum: logical eraseblock number
  691. * @buf: data which was not written because of the write failure
  692. * @offset: offset of the failed write
  693. * @len: how many bytes should have been written
  694. * @vidb: VID buffer
  695. * @retry: whether the caller should retry in case of failure
  696. *
  697. * This function is called in case of a write failure and moves all good data
  698. * from the potentially bad physical eraseblock to a good physical eraseblock.
  699. * This function also writes the data which was not written due to the failure.
  700. * Returns 0 in case of success, and a negative error code in case of failure.
  701. * In case of failure, the %retry parameter is set to false if this is a fatal
  702. * error (retrying won't help), and true otherwise.
  703. */
  704. static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
  705. const void *buf, int offset, int len,
  706. struct ubi_vid_io_buf *vidb, bool *retry)
  707. {
  708. struct ubi_device *ubi = vol->ubi;
  709. struct ubi_vid_hdr *vid_hdr;
  710. int new_pnum, err, vol_id = vol->vol_id, data_size;
  711. uint32_t crc;
  712. *retry = false;
  713. new_pnum = ubi_wl_get_peb(ubi);
  714. if (new_pnum < 0) {
  715. err = new_pnum;
  716. goto out_put;
  717. }
  718. ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
  719. pnum, new_pnum);
  720. err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
  721. if (err && err != UBI_IO_BITFLIPS) {
  722. if (err > 0)
  723. err = -EIO;
  724. goto out_put;
  725. }
  726. vid_hdr = ubi_get_vid_hdr(vidb);
  727. ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC);
  728. mutex_lock(&ubi->buf_mutex);
  729. memset(ubi->peb_buf + offset, 0xFF, len);
  730. /* Read everything before the area where the write failure happened */
  731. if (offset > 0) {
  732. err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
  733. if (err && err != UBI_IO_BITFLIPS)
  734. goto out_unlock;
  735. }
  736. *retry = true;
  737. memcpy(ubi->peb_buf + offset, buf, len);
  738. data_size = offset + len;
  739. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
  740. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  741. vid_hdr->copy_flag = 1;
  742. vid_hdr->data_size = cpu_to_be32(data_size);
  743. vid_hdr->data_crc = cpu_to_be32(crc);
  744. err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb);
  745. if (err)
  746. goto out_unlock;
  747. err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
  748. out_unlock:
  749. mutex_unlock(&ubi->buf_mutex);
  750. if (!err)
  751. vol->eba_tbl->entries[lnum].pnum = new_pnum;
  752. out_put:
  753. up_read(&ubi->fm_eba_sem);
  754. if (!err) {
  755. ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  756. ubi_msg(ubi, "data was successfully recovered");
  757. } else if (new_pnum >= 0) {
  758. /*
  759. * Bad luck? This physical eraseblock is bad too? Crud. Let's
  760. * try to get another one.
  761. */
  762. ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
  763. ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
  764. }
  765. return err;
  766. }
  767. /**
  768. * recover_peb - recover from write failure.
  769. * @ubi: UBI device description object
  770. * @pnum: the physical eraseblock to recover
  771. * @vol_id: volume ID
  772. * @lnum: logical eraseblock number
  773. * @buf: data which was not written because of the write failure
  774. * @offset: offset of the failed write
  775. * @len: how many bytes should have been written
  776. *
  777. * This function is called in case of a write failure and moves all good data
  778. * from the potentially bad physical eraseblock to a good physical eraseblock.
  779. * This function also writes the data which was not written due to the failure.
  780. * Returns 0 in case of success, and a negative error code in case of failure.
  781. * This function tries %UBI_IO_RETRIES before giving up.
  782. */
  783. static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
  784. const void *buf, int offset, int len)
  785. {
  786. int err, idx = vol_id2idx(ubi, vol_id), tries;
  787. struct ubi_volume *vol = ubi->volumes[idx];
  788. struct ubi_vid_io_buf *vidb;
  789. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  790. if (!vidb)
  791. return -ENOMEM;
  792. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  793. bool retry;
  794. err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb,
  795. &retry);
  796. if (!err || !retry)
  797. break;
  798. ubi_msg(ubi, "try again");
  799. }
  800. ubi_free_vid_buf(vidb);
  801. return err;
  802. }
  803. /**
  804. * try_write_vid_and_data - try to write VID header and data to a new PEB.
  805. * @vol: volume description object
  806. * @lnum: logical eraseblock number
  807. * @vidb: the VID buffer to write
  808. * @buf: buffer containing the data
  809. * @offset: where to start writing data
  810. * @len: how many bytes should be written
  811. *
  812. * This function tries to write VID header and data belonging to logical
  813. * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero
  814. * in case of success and a negative error code in case of failure.
  815. * In case of error, it is possible that something was still written to the
  816. * flash media, but may be some garbage.
  817. */
  818. static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
  819. struct ubi_vid_io_buf *vidb, const void *buf,
  820. int offset, int len)
  821. {
  822. struct ubi_device *ubi = vol->ubi;
  823. int pnum, opnum, err, err2, vol_id = vol->vol_id;
  824. pnum = ubi_wl_get_peb(ubi);
  825. if (pnum < 0) {
  826. err = pnum;
  827. goto out_put;
  828. }
  829. opnum = vol->eba_tbl->entries[lnum].pnum;
  830. dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
  831. len, offset, vol_id, lnum, pnum);
  832. err = ubi_io_write_vid_hdr(ubi, pnum, vidb);
  833. if (err) {
  834. ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
  835. vol_id, lnum, pnum);
  836. goto out_put;
  837. }
  838. if (len) {
  839. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  840. if (err) {
  841. ubi_warn(ubi,
  842. "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
  843. len, offset, vol_id, lnum, pnum);
  844. goto out_put;
  845. }
  846. }
  847. vol->eba_tbl->entries[lnum].pnum = pnum;
  848. out_put:
  849. up_read(&ubi->fm_eba_sem);
  850. if (err && pnum >= 0) {
  851. err2 = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  852. if (err2) {
  853. ubi_warn(ubi, "failed to return physical eraseblock %d, error %d",
  854. pnum, err2);
  855. }
  856. } else if (!err && opnum >= 0) {
  857. err2 = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0);
  858. if (err2) {
  859. ubi_warn(ubi, "failed to return physical eraseblock %d, error %d",
  860. opnum, err2);
  861. }
  862. }
  863. return err;
  864. }
  865. /**
  866. * ubi_eba_write_leb - write data to dynamic volume.
  867. * @ubi: UBI device description object
  868. * @vol: volume description object
  869. * @lnum: logical eraseblock number
  870. * @buf: the data to write
  871. * @offset: offset within the logical eraseblock where to write
  872. * @len: how many bytes to write
  873. *
  874. * This function writes data to logical eraseblock @lnum of a dynamic volume
  875. * @vol. Returns zero in case of success and a negative error code in case
  876. * of failure. In case of error, it is possible that something was still
  877. * written to the flash media, but may be some garbage.
  878. * This function retries %UBI_IO_RETRIES times before giving up.
  879. */
  880. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  881. const void *buf, int offset, int len)
  882. {
  883. int err, pnum, tries, vol_id = vol->vol_id;
  884. struct ubi_vid_io_buf *vidb;
  885. struct ubi_vid_hdr *vid_hdr;
  886. if (ubi->ro_mode)
  887. return -EROFS;
  888. err = leb_write_lock(ubi, vol_id, lnum);
  889. if (err)
  890. return err;
  891. pnum = vol->eba_tbl->entries[lnum].pnum;
  892. if (pnum >= 0) {
  893. err = check_mapping(ubi, vol, lnum, &pnum);
  894. if (err < 0)
  895. goto out;
  896. }
  897. if (pnum >= 0) {
  898. dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
  899. len, offset, vol_id, lnum, pnum);
  900. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  901. if (err) {
  902. ubi_warn(ubi, "failed to write data to PEB %d", pnum);
  903. if (err == -EIO && ubi->bad_allowed)
  904. err = recover_peb(ubi, pnum, vol_id, lnum, buf,
  905. offset, len);
  906. }
  907. goto out;
  908. }
  909. /*
  910. * The logical eraseblock is not mapped. We have to get a free physical
  911. * eraseblock and write the volume identifier header there first.
  912. */
  913. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  914. if (!vidb) {
  915. leb_write_unlock(ubi, vol_id, lnum);
  916. return -ENOMEM;
  917. }
  918. vid_hdr = ubi_get_vid_hdr(vidb);
  919. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  920. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  921. vid_hdr->vol_id = cpu_to_be32(vol_id);
  922. vid_hdr->lnum = cpu_to_be32(lnum);
  923. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  924. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  925. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  926. err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len);
  927. if (err != -EIO || !ubi->bad_allowed)
  928. break;
  929. /*
  930. * Fortunately, this is the first write operation to this
  931. * physical eraseblock, so just put it and request a new one.
  932. * We assume that if this physical eraseblock went bad, the
  933. * erase code will handle that.
  934. */
  935. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  936. ubi_msg(ubi, "try another PEB");
  937. }
  938. ubi_free_vid_buf(vidb);
  939. out:
  940. if (err)
  941. ubi_ro_mode(ubi);
  942. leb_write_unlock(ubi, vol_id, lnum);
  943. return err;
  944. }
  945. /**
  946. * ubi_eba_write_leb_st - write data to static volume.
  947. * @ubi: UBI device description object
  948. * @vol: volume description object
  949. * @lnum: logical eraseblock number
  950. * @buf: data to write
  951. * @len: how many bytes to write
  952. * @used_ebs: how many logical eraseblocks will this volume contain
  953. *
  954. * This function writes data to logical eraseblock @lnum of static volume
  955. * @vol. The @used_ebs argument should contain total number of logical
  956. * eraseblock in this static volume.
  957. *
  958. * When writing to the last logical eraseblock, the @len argument doesn't have
  959. * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
  960. * to the real data size, although the @buf buffer has to contain the
  961. * alignment. In all other cases, @len has to be aligned.
  962. *
  963. * It is prohibited to write more than once to logical eraseblocks of static
  964. * volumes. This function returns zero in case of success and a negative error
  965. * code in case of failure.
  966. */
  967. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  968. int lnum, const void *buf, int len, int used_ebs)
  969. {
  970. int err, tries, data_size = len, vol_id = vol->vol_id;
  971. struct ubi_vid_io_buf *vidb;
  972. struct ubi_vid_hdr *vid_hdr;
  973. uint32_t crc;
  974. if (ubi->ro_mode)
  975. return -EROFS;
  976. if (lnum == used_ebs - 1)
  977. /* If this is the last LEB @len may be unaligned */
  978. len = ALIGN(data_size, ubi->min_io_size);
  979. else
  980. ubi_assert(!(len & (ubi->min_io_size - 1)));
  981. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  982. if (!vidb)
  983. return -ENOMEM;
  984. vid_hdr = ubi_get_vid_hdr(vidb);
  985. err = leb_write_lock(ubi, vol_id, lnum);
  986. if (err)
  987. goto out;
  988. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  989. vid_hdr->vol_id = cpu_to_be32(vol_id);
  990. vid_hdr->lnum = cpu_to_be32(lnum);
  991. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  992. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  993. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  994. vid_hdr->vol_type = UBI_VID_STATIC;
  995. vid_hdr->data_size = cpu_to_be32(data_size);
  996. vid_hdr->used_ebs = cpu_to_be32(used_ebs);
  997. vid_hdr->data_crc = cpu_to_be32(crc);
  998. ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
  999. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  1000. err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
  1001. if (err != -EIO || !ubi->bad_allowed)
  1002. break;
  1003. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1004. ubi_msg(ubi, "try another PEB");
  1005. }
  1006. if (err)
  1007. ubi_ro_mode(ubi);
  1008. leb_write_unlock(ubi, vol_id, lnum);
  1009. out:
  1010. ubi_free_vid_buf(vidb);
  1011. return err;
  1012. }
  1013. /*
  1014. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  1015. * @ubi: UBI device description object
  1016. * @vol: volume description object
  1017. * @lnum: logical eraseblock number
  1018. * @buf: data to write
  1019. * @len: how many bytes to write
  1020. *
  1021. * This function changes the contents of a logical eraseblock atomically. @buf
  1022. * has to contain new logical eraseblock data, and @len - the length of the
  1023. * data, which has to be aligned. This function guarantees that in case of an
  1024. * unclean reboot the old contents is preserved. Returns zero in case of
  1025. * success and a negative error code in case of failure.
  1026. *
  1027. * UBI reserves one LEB for the "atomic LEB change" operation, so only one
  1028. * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
  1029. */
  1030. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  1031. int lnum, const void *buf, int len)
  1032. {
  1033. int err, tries, vol_id = vol->vol_id;
  1034. struct ubi_vid_io_buf *vidb;
  1035. struct ubi_vid_hdr *vid_hdr;
  1036. uint32_t crc;
  1037. if (ubi->ro_mode)
  1038. return -EROFS;
  1039. if (len == 0) {
  1040. /*
  1041. * Special case when data length is zero. In this case the LEB
  1042. * has to be unmapped and mapped somewhere else.
  1043. */
  1044. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  1045. if (err)
  1046. return err;
  1047. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
  1048. }
  1049. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  1050. if (!vidb)
  1051. return -ENOMEM;
  1052. vid_hdr = ubi_get_vid_hdr(vidb);
  1053. mutex_lock(&ubi->alc_mutex);
  1054. err = leb_write_lock(ubi, vol_id, lnum);
  1055. if (err)
  1056. goto out_mutex;
  1057. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1058. vid_hdr->vol_id = cpu_to_be32(vol_id);
  1059. vid_hdr->lnum = cpu_to_be32(lnum);
  1060. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  1061. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  1062. crc = crc32(UBI_CRC32_INIT, buf, len);
  1063. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  1064. vid_hdr->data_size = cpu_to_be32(len);
  1065. vid_hdr->copy_flag = 1;
  1066. vid_hdr->data_crc = cpu_to_be32(crc);
  1067. dbg_eba("change LEB %d:%d", vol_id, lnum);
  1068. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  1069. err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
  1070. if (err != -EIO || !ubi->bad_allowed)
  1071. break;
  1072. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1073. ubi_msg(ubi, "try another PEB");
  1074. }
  1075. /*
  1076. * This flash device does not admit of bad eraseblocks or
  1077. * something nasty and unexpected happened. Switch to read-only
  1078. * mode just in case.
  1079. */
  1080. if (err)
  1081. ubi_ro_mode(ubi);
  1082. leb_write_unlock(ubi, vol_id, lnum);
  1083. out_mutex:
  1084. mutex_unlock(&ubi->alc_mutex);
  1085. ubi_free_vid_buf(vidb);
  1086. return err;
  1087. }
  1088. /**
  1089. * is_error_sane - check whether a read error is sane.
  1090. * @err: code of the error happened during reading
  1091. *
  1092. * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
  1093. * cannot read data from the target PEB (an error @err happened). If the error
  1094. * code is sane, then we treat this error as non-fatal. Otherwise the error is
  1095. * fatal and UBI will be switched to R/O mode later.
  1096. *
  1097. * The idea is that we try not to switch to R/O mode if the read error is
  1098. * something which suggests there was a real read problem. E.g., %-EIO. Or a
  1099. * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
  1100. * mode, simply because we do not know what happened at the MTD level, and we
  1101. * cannot handle this. E.g., the underlying driver may have become crazy, and
  1102. * it is safer to switch to R/O mode to preserve the data.
  1103. *
  1104. * And bear in mind, this is about reading from the target PEB, i.e. the PEB
  1105. * which we have just written.
  1106. */
  1107. static int is_error_sane(int err)
  1108. {
  1109. if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
  1110. err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
  1111. return 0;
  1112. return 1;
  1113. }
  1114. /**
  1115. * ubi_eba_copy_leb - copy logical eraseblock.
  1116. * @ubi: UBI device description object
  1117. * @from: physical eraseblock number from where to copy
  1118. * @to: physical eraseblock number where to copy
  1119. * @vidb: data structure from where the VID header is derived
  1120. *
  1121. * This function copies logical eraseblock from physical eraseblock @from to
  1122. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  1123. * function. Returns:
  1124. * o %0 in case of success;
  1125. * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
  1126. * o a negative error code in case of failure.
  1127. */
  1128. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  1129. struct ubi_vid_io_buf *vidb)
  1130. {
  1131. int err, vol_id, lnum, data_size, aldata_size, idx;
  1132. struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
  1133. struct ubi_volume *vol;
  1134. uint32_t crc;
  1135. ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
  1136. vol_id = be32_to_cpu(vid_hdr->vol_id);
  1137. lnum = be32_to_cpu(vid_hdr->lnum);
  1138. dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  1139. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  1140. data_size = be32_to_cpu(vid_hdr->data_size);
  1141. aldata_size = ALIGN(data_size, ubi->min_io_size);
  1142. } else
  1143. data_size = aldata_size =
  1144. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  1145. idx = vol_id2idx(ubi, vol_id);
  1146. spin_lock(&ubi->volumes_lock);
  1147. /*
  1148. * Note, we may race with volume deletion, which means that the volume
  1149. * this logical eraseblock belongs to might be being deleted. Since the
  1150. * volume deletion un-maps all the volume's logical eraseblocks, it will
  1151. * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
  1152. */
  1153. vol = ubi->volumes[idx];
  1154. spin_unlock(&ubi->volumes_lock);
  1155. if (!vol) {
  1156. /* No need to do further work, cancel */
  1157. dbg_wl("volume %d is being removed, cancel", vol_id);
  1158. return MOVE_CANCEL_RACE;
  1159. }
  1160. /*
  1161. * We do not want anybody to write to this logical eraseblock while we
  1162. * are moving it, so lock it.
  1163. *
  1164. * Note, we are using non-waiting locking here, because we cannot sleep
  1165. * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
  1166. * unmapping the LEB which is mapped to the PEB we are going to move
  1167. * (@from). This task locks the LEB and goes sleep in the
  1168. * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
  1169. * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
  1170. * LEB is already locked, we just do not move it and return
  1171. * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
  1172. * we do not know the reasons of the contention - it may be just a
  1173. * normal I/O on this LEB, so we want to re-try.
  1174. */
  1175. err = leb_write_trylock(ubi, vol_id, lnum);
  1176. if (err) {
  1177. dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
  1178. return MOVE_RETRY;
  1179. }
  1180. /*
  1181. * The LEB might have been put meanwhile, and the task which put it is
  1182. * probably waiting on @ubi->move_mutex. No need to continue the work,
  1183. * cancel it.
  1184. */
  1185. if (vol->eba_tbl->entries[lnum].pnum != from) {
  1186. dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
  1187. vol_id, lnum, from, vol->eba_tbl->entries[lnum].pnum);
  1188. err = MOVE_CANCEL_RACE;
  1189. goto out_unlock_leb;
  1190. }
  1191. /*
  1192. * OK, now the LEB is locked and we can safely start moving it. Since
  1193. * this function utilizes the @ubi->peb_buf buffer which is shared
  1194. * with some other functions - we lock the buffer by taking the
  1195. * @ubi->buf_mutex.
  1196. */
  1197. mutex_lock(&ubi->buf_mutex);
  1198. dbg_wl("read %d bytes of data", aldata_size);
  1199. err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
  1200. if (err && err != UBI_IO_BITFLIPS) {
  1201. ubi_warn(ubi, "error %d while reading data from PEB %d",
  1202. err, from);
  1203. err = MOVE_SOURCE_RD_ERR;
  1204. goto out_unlock_buf;
  1205. }
  1206. /*
  1207. * Now we have got to calculate how much data we have to copy. In
  1208. * case of a static volume it is fairly easy - the VID header contains
  1209. * the data size. In case of a dynamic volume it is more difficult - we
  1210. * have to read the contents, cut 0xFF bytes from the end and copy only
  1211. * the first part. We must do this to avoid writing 0xFF bytes as it
  1212. * may have some side-effects. And not only this. It is important not
  1213. * to include those 0xFFs to CRC because later the they may be filled
  1214. * by data.
  1215. */
  1216. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  1217. aldata_size = data_size =
  1218. ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
  1219. cond_resched();
  1220. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
  1221. cond_resched();
  1222. /*
  1223. * It may turn out to be that the whole @from physical eraseblock
  1224. * contains only 0xFF bytes. Then we have to only write the VID header
  1225. * and do not write any data. This also means we should not set
  1226. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  1227. */
  1228. if (data_size > 0) {
  1229. vid_hdr->copy_flag = 1;
  1230. vid_hdr->data_size = cpu_to_be32(data_size);
  1231. vid_hdr->data_crc = cpu_to_be32(crc);
  1232. }
  1233. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1234. err = ubi_io_write_vid_hdr(ubi, to, vidb);
  1235. if (err) {
  1236. if (err == -EIO)
  1237. err = MOVE_TARGET_WR_ERR;
  1238. goto out_unlock_buf;
  1239. }
  1240. cond_resched();
  1241. /* Read the VID header back and check if it was written correctly */
  1242. err = ubi_io_read_vid_hdr(ubi, to, vidb, 1);
  1243. if (err) {
  1244. if (err != UBI_IO_BITFLIPS) {
  1245. ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
  1246. err, to);
  1247. if (is_error_sane(err))
  1248. err = MOVE_TARGET_RD_ERR;
  1249. } else
  1250. err = MOVE_TARGET_BITFLIPS;
  1251. goto out_unlock_buf;
  1252. }
  1253. if (data_size > 0) {
  1254. err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
  1255. if (err) {
  1256. if (err == -EIO)
  1257. err = MOVE_TARGET_WR_ERR;
  1258. goto out_unlock_buf;
  1259. }
  1260. cond_resched();
  1261. }
  1262. ubi_assert(vol->eba_tbl->entries[lnum].pnum == from);
  1263. /**
  1264. * The volumes_lock lock is needed here to prevent the expired old eba_tbl
  1265. * being updated when the eba_tbl is copied in the ubi_resize_volume() process.
  1266. */
  1267. spin_lock(&ubi->volumes_lock);
  1268. vol->eba_tbl->entries[lnum].pnum = to;
  1269. spin_unlock(&ubi->volumes_lock);
  1270. out_unlock_buf:
  1271. mutex_unlock(&ubi->buf_mutex);
  1272. out_unlock_leb:
  1273. leb_write_unlock(ubi, vol_id, lnum);
  1274. return err;
  1275. }
  1276. /**
  1277. * print_rsvd_warning - warn about not having enough reserved PEBs.
  1278. * @ubi: UBI device description object
  1279. * @ai: UBI attach info object
  1280. *
  1281. * This is a helper function for 'ubi_eba_init()' which is called when UBI
  1282. * cannot reserve enough PEBs for bad block handling. This function makes a
  1283. * decision whether we have to print a warning or not. The algorithm is as
  1284. * follows:
  1285. * o if this is a new UBI image, then just print the warning
  1286. * o if this is an UBI image which has already been used for some time, print
  1287. * a warning only if we can reserve less than 10% of the expected amount of
  1288. * the reserved PEB.
  1289. *
  1290. * The idea is that when UBI is used, PEBs become bad, and the reserved pool
  1291. * of PEBs becomes smaller, which is normal and we do not want to scare users
  1292. * with a warning every time they attach the MTD device. This was an issue
  1293. * reported by real users.
  1294. */
  1295. static void print_rsvd_warning(struct ubi_device *ubi,
  1296. struct ubi_attach_info *ai)
  1297. {
  1298. /*
  1299. * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
  1300. * large number to distinguish between newly flashed and used images.
  1301. */
  1302. if (ai->max_sqnum > (1 << 18)) {
  1303. int min = ubi->beb_rsvd_level / 10;
  1304. if (!min)
  1305. min = 1;
  1306. if (ubi->beb_rsvd_pebs > min)
  1307. return;
  1308. }
  1309. ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
  1310. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1311. if (ubi->corr_peb_count)
  1312. ubi_warn(ubi, "%d PEBs are corrupted and not used",
  1313. ubi->corr_peb_count);
  1314. }
  1315. /**
  1316. * self_check_eba - run a self check on the EBA table constructed by fastmap.
  1317. * @ubi: UBI device description object
  1318. * @ai_fastmap: UBI attach info object created by fastmap
  1319. * @ai_scan: UBI attach info object created by scanning
  1320. *
  1321. * Returns < 0 in case of an internal error, 0 otherwise.
  1322. * If a bad EBA table entry was found it will be printed out and
  1323. * ubi_assert() triggers.
  1324. */
  1325. int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
  1326. struct ubi_attach_info *ai_scan)
  1327. {
  1328. int i, j, num_volumes, ret = 0;
  1329. int **scan_eba, **fm_eba;
  1330. struct ubi_ainf_volume *av;
  1331. struct ubi_volume *vol;
  1332. struct ubi_ainf_peb *aeb;
  1333. struct rb_node *rb;
  1334. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1335. scan_eba = kmalloc_objs(*scan_eba, num_volumes);
  1336. if (!scan_eba)
  1337. return -ENOMEM;
  1338. fm_eba = kmalloc_objs(*fm_eba, num_volumes);
  1339. if (!fm_eba) {
  1340. kfree(scan_eba);
  1341. return -ENOMEM;
  1342. }
  1343. for (i = 0; i < num_volumes; i++) {
  1344. vol = ubi->volumes[i];
  1345. if (!vol)
  1346. continue;
  1347. scan_eba[i] = kmalloc_objs(**scan_eba, vol->reserved_pebs);
  1348. if (!scan_eba[i]) {
  1349. ret = -ENOMEM;
  1350. goto out_free;
  1351. }
  1352. fm_eba[i] = kmalloc_objs(**fm_eba, vol->reserved_pebs);
  1353. if (!fm_eba[i]) {
  1354. ret = -ENOMEM;
  1355. kfree(scan_eba[i]);
  1356. goto out_free;
  1357. }
  1358. for (j = 0; j < vol->reserved_pebs; j++)
  1359. scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
  1360. av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
  1361. if (!av)
  1362. continue;
  1363. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
  1364. scan_eba[i][aeb->lnum] = aeb->pnum;
  1365. av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
  1366. if (!av)
  1367. continue;
  1368. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
  1369. fm_eba[i][aeb->lnum] = aeb->pnum;
  1370. for (j = 0; j < vol->reserved_pebs; j++) {
  1371. if (scan_eba[i][j] != fm_eba[i][j]) {
  1372. if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
  1373. fm_eba[i][j] == UBI_LEB_UNMAPPED)
  1374. continue;
  1375. ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
  1376. vol->vol_id, j, fm_eba[i][j],
  1377. scan_eba[i][j]);
  1378. ubi_assert(0);
  1379. }
  1380. }
  1381. }
  1382. out_free:
  1383. while (--i >= 0) {
  1384. if (!ubi->volumes[i])
  1385. continue;
  1386. kfree(scan_eba[i]);
  1387. kfree(fm_eba[i]);
  1388. }
  1389. kfree(scan_eba);
  1390. kfree(fm_eba);
  1391. return ret;
  1392. }
  1393. /**
  1394. * ubi_eba_init - initialize the EBA sub-system using attaching information.
  1395. * @ubi: UBI device description object
  1396. * @ai: attaching information
  1397. *
  1398. * This function returns zero in case of success and a negative error code in
  1399. * case of failure.
  1400. */
  1401. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1402. {
  1403. int i, err, num_volumes;
  1404. struct ubi_ainf_volume *av;
  1405. struct ubi_volume *vol;
  1406. struct ubi_ainf_peb *aeb;
  1407. struct rb_node *rb;
  1408. dbg_eba("initialize EBA sub-system");
  1409. spin_lock_init(&ubi->ltree_lock);
  1410. mutex_init(&ubi->alc_mutex);
  1411. ubi->ltree = RB_ROOT;
  1412. ubi->global_sqnum = ai->max_sqnum + 1;
  1413. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1414. for (i = 0; i < num_volumes; i++) {
  1415. struct ubi_eba_table *tbl;
  1416. vol = ubi->volumes[i];
  1417. if (!vol)
  1418. continue;
  1419. cond_resched();
  1420. tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
  1421. if (IS_ERR(tbl)) {
  1422. err = PTR_ERR(tbl);
  1423. goto out_free;
  1424. }
  1425. ubi_eba_replace_table(vol, tbl);
  1426. av = ubi_find_av(ai, idx2vol_id(ubi, i));
  1427. if (!av)
  1428. continue;
  1429. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
  1430. if (aeb->lnum >= vol->reserved_pebs) {
  1431. /*
  1432. * This may happen in case of an unclean reboot
  1433. * during re-size.
  1434. */
  1435. ubi_move_aeb_to_list(av, aeb, &ai->erase);
  1436. } else {
  1437. struct ubi_eba_entry *entry;
  1438. entry = &vol->eba_tbl->entries[aeb->lnum];
  1439. entry->pnum = aeb->pnum;
  1440. }
  1441. }
  1442. }
  1443. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1444. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1445. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1446. if (ubi->corr_peb_count)
  1447. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1448. ubi->corr_peb_count);
  1449. err = -ENOSPC;
  1450. goto out_free;
  1451. }
  1452. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1453. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1454. if (ubi->bad_allowed) {
  1455. ubi_calculate_reserved(ubi);
  1456. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1457. /* No enough free physical eraseblocks */
  1458. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1459. print_rsvd_warning(ubi, ai);
  1460. } else
  1461. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1462. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1463. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1464. }
  1465. dbg_eba("EBA sub-system is initialized");
  1466. return 0;
  1467. out_free:
  1468. for (i = 0; i < num_volumes; i++) {
  1469. if (!ubi->volumes[i])
  1470. continue;
  1471. ubi_eba_replace_table(ubi->volumes[i], NULL);
  1472. }
  1473. return err;
  1474. }