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