eba.c 38 KB

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