eba.c 47 KB

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