eba.c 46 KB

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