vtbl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2006, 2007
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Author: Artem Bityutskiy (Битюцкий Артём)
  8. */
  9. /*
  10. * This file includes volume table manipulation code. The volume table is an
  11. * on-flash table containing volume meta-data like name, number of reserved
  12. * physical eraseblocks, type, etc. The volume table is stored in the so-called
  13. * "layout volume".
  14. *
  15. * The layout volume is an internal volume which is organized as follows. It
  16. * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
  17. * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
  18. * other. This redundancy guarantees robustness to unclean reboots. The volume
  19. * table is basically an array of volume table records. Each record contains
  20. * full information about the volume and protected by a CRC checksum.
  21. *
  22. * The volume table is changed, it is first changed in RAM. Then LEB 0 is
  23. * erased, and the updated volume table is written back to LEB 0. Then same for
  24. * LEB 1. This scheme guarantees recoverability from unclean reboots.
  25. *
  26. * In this UBI implementation the on-flash volume table does not contain any
  27. * information about how much data static volumes contain.
  28. *
  29. * But it would still be beneficial to store this information in the volume
  30. * table. For example, suppose we have a static volume X, and all its physical
  31. * eraseblocks became bad for some reasons. Suppose we are attaching the
  32. * corresponding MTD device, for some reason we find no logical eraseblocks
  33. * corresponding to the volume X. According to the volume table volume X does
  34. * exist. So we don't know whether it is just empty or all its physical
  35. * eraseblocks went bad. So we cannot alarm the user properly.
  36. *
  37. * The volume table also stores so-called "update marker", which is used for
  38. * volume updates. Before updating the volume, the update marker is set, and
  39. * after the update operation is finished, the update marker is cleared. So if
  40. * the update operation was interrupted (e.g. by an unclean reboot) - the
  41. * update marker is still there and we know that the volume's contents is
  42. * damaged.
  43. */
  44. #ifndef __UBOOT__
  45. #include <linux/crc32.h>
  46. #include <linux/err.h>
  47. #include <linux/slab.h>
  48. #include <asm/div64.h>
  49. #else
  50. #include <ubi_uboot.h>
  51. #endif
  52. #include <linux/err.h>
  53. #include "ubi.h"
  54. static void self_vtbl_check(const struct ubi_device *ubi);
  55. /* Empty volume table record */
  56. static struct ubi_vtbl_record empty_vtbl_record;
  57. /**
  58. * ubi_change_vtbl_record - change volume table record.
  59. * @ubi: UBI device description object
  60. * @idx: table index to change
  61. * @vtbl_rec: new volume table record
  62. *
  63. * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
  64. * volume table record is written. The caller does not have to calculate CRC of
  65. * the record as it is done by this function. Returns zero in case of success
  66. * and a negative error code in case of failure.
  67. */
  68. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  69. struct ubi_vtbl_record *vtbl_rec)
  70. {
  71. int i, err;
  72. uint32_t crc;
  73. struct ubi_volume *layout_vol;
  74. ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
  75. layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
  76. if (!vtbl_rec)
  77. vtbl_rec = &empty_vtbl_record;
  78. else {
  79. crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
  80. vtbl_rec->crc = cpu_to_be32(crc);
  81. }
  82. memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
  83. for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
  84. err = ubi_eba_unmap_leb(ubi, layout_vol, i);
  85. if (err)
  86. return err;
  87. err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
  88. ubi->vtbl_size);
  89. if (err)
  90. return err;
  91. }
  92. self_vtbl_check(ubi);
  93. return 0;
  94. }
  95. /**
  96. * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
  97. * @ubi: UBI device description object
  98. * @rename_list: list of &struct ubi_rename_entry objects
  99. *
  100. * This function re-names multiple volumes specified in @req in the volume
  101. * table. Returns zero in case of success and a negative error code in case of
  102. * failure.
  103. */
  104. int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
  105. struct list_head *rename_list)
  106. {
  107. int i, err;
  108. struct ubi_rename_entry *re;
  109. struct ubi_volume *layout_vol;
  110. list_for_each_entry(re, rename_list, list) {
  111. uint32_t crc;
  112. struct ubi_volume *vol = re->desc->vol;
  113. struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
  114. if (re->remove) {
  115. memcpy(vtbl_rec, &empty_vtbl_record,
  116. sizeof(struct ubi_vtbl_record));
  117. continue;
  118. }
  119. vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
  120. memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
  121. memset(vtbl_rec->name + re->new_name_len, 0,
  122. UBI_VOL_NAME_MAX + 1 - re->new_name_len);
  123. crc = crc32(UBI_CRC32_INIT, vtbl_rec,
  124. UBI_VTBL_RECORD_SIZE_CRC);
  125. vtbl_rec->crc = cpu_to_be32(crc);
  126. }
  127. layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
  128. for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
  129. err = ubi_eba_unmap_leb(ubi, layout_vol, i);
  130. if (err)
  131. return err;
  132. err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
  133. ubi->vtbl_size);
  134. if (err)
  135. return err;
  136. }
  137. return 0;
  138. }
  139. /**
  140. * vtbl_check - check if volume table is not corrupted and sensible.
  141. * @ubi: UBI device description object
  142. * @vtbl: volume table
  143. *
  144. * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
  145. * and %-EINVAL if it contains inconsistent data.
  146. */
  147. static int vtbl_check(const struct ubi_device *ubi,
  148. const struct ubi_vtbl_record *vtbl)
  149. {
  150. int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
  151. int upd_marker, err;
  152. uint32_t crc;
  153. const char *name;
  154. for (i = 0; i < ubi->vtbl_slots; i++) {
  155. cond_resched();
  156. reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
  157. alignment = be32_to_cpu(vtbl[i].alignment);
  158. data_pad = be32_to_cpu(vtbl[i].data_pad);
  159. upd_marker = vtbl[i].upd_marker;
  160. vol_type = vtbl[i].vol_type;
  161. name_len = be16_to_cpu(vtbl[i].name_len);
  162. name = &vtbl[i].name[0];
  163. crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
  164. if (be32_to_cpu(vtbl[i].crc) != crc) {
  165. ubi_err("bad CRC at record %u: %#08x, not %#08x",
  166. i, crc, be32_to_cpu(vtbl[i].crc));
  167. ubi_dump_vtbl_record(&vtbl[i], i);
  168. return 1;
  169. }
  170. if (reserved_pebs == 0) {
  171. if (memcmp(&vtbl[i], &empty_vtbl_record,
  172. UBI_VTBL_RECORD_SIZE)) {
  173. err = 2;
  174. goto bad;
  175. }
  176. continue;
  177. }
  178. if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
  179. name_len < 0) {
  180. err = 3;
  181. goto bad;
  182. }
  183. if (alignment > ubi->leb_size || alignment == 0) {
  184. err = 4;
  185. goto bad;
  186. }
  187. n = alignment & (ubi->min_io_size - 1);
  188. if (alignment != 1 && n) {
  189. err = 5;
  190. goto bad;
  191. }
  192. n = ubi->leb_size % alignment;
  193. if (data_pad != n) {
  194. ubi_err("bad data_pad, has to be %d", n);
  195. err = 6;
  196. goto bad;
  197. }
  198. if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
  199. err = 7;
  200. goto bad;
  201. }
  202. if (upd_marker != 0 && upd_marker != 1) {
  203. err = 8;
  204. goto bad;
  205. }
  206. if (reserved_pebs > ubi->good_peb_count) {
  207. ubi_err("too large reserved_pebs %d, good PEBs %d",
  208. reserved_pebs, ubi->good_peb_count);
  209. err = 9;
  210. goto bad;
  211. }
  212. if (name_len > UBI_VOL_NAME_MAX) {
  213. err = 10;
  214. goto bad;
  215. }
  216. if (name[0] == '\0') {
  217. err = 11;
  218. goto bad;
  219. }
  220. if (name_len != strnlen(name, name_len + 1)) {
  221. err = 12;
  222. goto bad;
  223. }
  224. }
  225. /* Checks that all names are unique */
  226. for (i = 0; i < ubi->vtbl_slots - 1; i++) {
  227. for (n = i + 1; n < ubi->vtbl_slots; n++) {
  228. int len1 = be16_to_cpu(vtbl[i].name_len);
  229. int len2 = be16_to_cpu(vtbl[n].name_len);
  230. if (len1 > 0 && len1 == len2 &&
  231. #ifndef __UBOOT__
  232. !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
  233. #else
  234. !strncmp((char *)vtbl[i].name, vtbl[n].name, len1)) {
  235. #endif
  236. ubi_err("volumes %d and %d have the same name \"%s\"",
  237. i, n, vtbl[i].name);
  238. ubi_dump_vtbl_record(&vtbl[i], i);
  239. ubi_dump_vtbl_record(&vtbl[n], n);
  240. return -EINVAL;
  241. }
  242. }
  243. }
  244. return 0;
  245. bad:
  246. ubi_err("volume table check failed: record %d, error %d", i, err);
  247. ubi_dump_vtbl_record(&vtbl[i], i);
  248. return -EINVAL;
  249. }
  250. /**
  251. * create_vtbl - create a copy of volume table.
  252. * @ubi: UBI device description object
  253. * @ai: attaching information
  254. * @copy: number of the volume table copy
  255. * @vtbl: contents of the volume table
  256. *
  257. * This function returns zero in case of success and a negative error code in
  258. * case of failure.
  259. */
  260. static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
  261. int copy, void *vtbl)
  262. {
  263. int err, tries = 0;
  264. struct ubi_vid_hdr *vid_hdr;
  265. struct ubi_ainf_peb *new_aeb;
  266. dbg_gen("create volume table (copy #%d)", copy + 1);
  267. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  268. if (!vid_hdr)
  269. return -ENOMEM;
  270. retry:
  271. new_aeb = ubi_early_get_peb(ubi, ai);
  272. if (IS_ERR(new_aeb)) {
  273. err = PTR_ERR(new_aeb);
  274. goto out_free;
  275. }
  276. vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
  277. vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
  278. vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
  279. vid_hdr->data_size = vid_hdr->used_ebs =
  280. vid_hdr->data_pad = cpu_to_be32(0);
  281. vid_hdr->lnum = cpu_to_be32(copy);
  282. vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
  283. /* The EC header is already there, write the VID header */
  284. err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr);
  285. if (err)
  286. goto write_error;
  287. /* Write the layout volume contents */
  288. err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
  289. if (err)
  290. goto write_error;
  291. /*
  292. * And add it to the attaching information. Don't delete the old version
  293. * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
  294. */
  295. err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
  296. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  297. ubi_free_vid_hdr(ubi, vid_hdr);
  298. return err;
  299. write_error:
  300. if (err == -EIO && ++tries <= 5) {
  301. /*
  302. * Probably this physical eraseblock went bad, try to pick
  303. * another one.
  304. */
  305. list_add(&new_aeb->u.list, &ai->erase);
  306. goto retry;
  307. }
  308. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  309. out_free:
  310. ubi_free_vid_hdr(ubi, vid_hdr);
  311. return err;
  312. }
  313. /**
  314. * process_lvol - process the layout volume.
  315. * @ubi: UBI device description object
  316. * @ai: attaching information
  317. * @av: layout volume attaching information
  318. *
  319. * This function is responsible for reading the layout volume, ensuring it is
  320. * not corrupted, and recovering from corruptions if needed. Returns volume
  321. * table in case of success and a negative error code in case of failure.
  322. */
  323. static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
  324. struct ubi_attach_info *ai,
  325. struct ubi_ainf_volume *av)
  326. {
  327. int err;
  328. struct rb_node *rb;
  329. struct ubi_ainf_peb *aeb;
  330. struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
  331. int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
  332. /*
  333. * UBI goes through the following steps when it changes the layout
  334. * volume:
  335. * a. erase LEB 0;
  336. * b. write new data to LEB 0;
  337. * c. erase LEB 1;
  338. * d. write new data to LEB 1.
  339. *
  340. * Before the change, both LEBs contain the same data.
  341. *
  342. * Due to unclean reboots, the contents of LEB 0 may be lost, but there
  343. * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
  344. * Similarly, LEB 1 may be lost, but there should be LEB 0. And
  345. * finally, unclean reboots may result in a situation when neither LEB
  346. * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
  347. * 0 contains more recent information.
  348. *
  349. * So the plan is to first check LEB 0. Then
  350. * a. if LEB 0 is OK, it must be containing the most recent data; then
  351. * we compare it with LEB 1, and if they are different, we copy LEB
  352. * 0 to LEB 1;
  353. * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
  354. * to LEB 0.
  355. */
  356. dbg_gen("check layout volume");
  357. /* Read both LEB 0 and LEB 1 into memory */
  358. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
  359. leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
  360. if (!leb[aeb->lnum]) {
  361. err = -ENOMEM;
  362. goto out_free;
  363. }
  364. err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
  365. ubi->vtbl_size);
  366. if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
  367. /*
  368. * Scrub the PEB later. Note, -EBADMSG indicates an
  369. * uncorrectable ECC error, but we have our own CRC and
  370. * the data will be checked later. If the data is OK,
  371. * the PEB will be scrubbed (because we set
  372. * aeb->scrub). If the data is not OK, the contents of
  373. * the PEB will be recovered from the second copy, and
  374. * aeb->scrub will be cleared in
  375. * 'ubi_add_to_av()'.
  376. */
  377. aeb->scrub = 1;
  378. else if (err)
  379. goto out_free;
  380. }
  381. err = -EINVAL;
  382. if (leb[0]) {
  383. leb_corrupted[0] = vtbl_check(ubi, leb[0]);
  384. if (leb_corrupted[0] < 0)
  385. goto out_free;
  386. }
  387. if (!leb_corrupted[0]) {
  388. /* LEB 0 is OK */
  389. if (leb[1])
  390. leb_corrupted[1] = memcmp(leb[0], leb[1],
  391. ubi->vtbl_size);
  392. if (leb_corrupted[1]) {
  393. ubi_warn("volume table copy #2 is corrupted");
  394. err = create_vtbl(ubi, ai, 1, leb[0]);
  395. if (err)
  396. goto out_free;
  397. ubi_msg("volume table was restored");
  398. }
  399. /* Both LEB 1 and LEB 2 are OK and consistent */
  400. vfree(leb[1]);
  401. return leb[0];
  402. } else {
  403. /* LEB 0 is corrupted or does not exist */
  404. if (leb[1]) {
  405. leb_corrupted[1] = vtbl_check(ubi, leb[1]);
  406. if (leb_corrupted[1] < 0)
  407. goto out_free;
  408. }
  409. if (leb_corrupted[1]) {
  410. /* Both LEB 0 and LEB 1 are corrupted */
  411. ubi_err("both volume tables are corrupted");
  412. goto out_free;
  413. }
  414. ubi_warn("volume table copy #1 is corrupted");
  415. err = create_vtbl(ubi, ai, 0, leb[1]);
  416. if (err)
  417. goto out_free;
  418. ubi_msg("volume table was restored");
  419. vfree(leb[0]);
  420. return leb[1];
  421. }
  422. out_free:
  423. vfree(leb[0]);
  424. vfree(leb[1]);
  425. return ERR_PTR(err);
  426. }
  427. /**
  428. * create_empty_lvol - create empty layout volume.
  429. * @ubi: UBI device description object
  430. * @ai: attaching information
  431. *
  432. * This function returns volume table contents in case of success and a
  433. * negative error code in case of failure.
  434. */
  435. static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
  436. struct ubi_attach_info *ai)
  437. {
  438. int i;
  439. struct ubi_vtbl_record *vtbl;
  440. vtbl = vzalloc(ubi->vtbl_size);
  441. if (!vtbl)
  442. return ERR_PTR(-ENOMEM);
  443. for (i = 0; i < ubi->vtbl_slots; i++)
  444. memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
  445. for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
  446. int err;
  447. err = create_vtbl(ubi, ai, i, vtbl);
  448. if (err) {
  449. vfree(vtbl);
  450. return ERR_PTR(err);
  451. }
  452. }
  453. return vtbl;
  454. }
  455. /**
  456. * init_volumes - initialize volume information for existing volumes.
  457. * @ubi: UBI device description object
  458. * @ai: scanning information
  459. * @vtbl: volume table
  460. *
  461. * This function allocates volume description objects for existing volumes.
  462. * Returns zero in case of success and a negative error code in case of
  463. * failure.
  464. */
  465. static int init_volumes(struct ubi_device *ubi,
  466. const struct ubi_attach_info *ai,
  467. const struct ubi_vtbl_record *vtbl)
  468. {
  469. int i, reserved_pebs = 0;
  470. struct ubi_ainf_volume *av;
  471. struct ubi_volume *vol;
  472. for (i = 0; i < ubi->vtbl_slots; i++) {
  473. cond_resched();
  474. if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
  475. continue; /* Empty record */
  476. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  477. if (!vol)
  478. return -ENOMEM;
  479. vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
  480. vol->alignment = be32_to_cpu(vtbl[i].alignment);
  481. vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
  482. vol->upd_marker = vtbl[i].upd_marker;
  483. vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
  484. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  485. vol->name_len = be16_to_cpu(vtbl[i].name_len);
  486. vol->usable_leb_size = ubi->leb_size - vol->data_pad;
  487. memcpy(vol->name, vtbl[i].name, vol->name_len);
  488. vol->name[vol->name_len] = '\0';
  489. vol->vol_id = i;
  490. if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
  491. /* Auto re-size flag may be set only for one volume */
  492. if (ubi->autoresize_vol_id != -1) {
  493. ubi_err("more than one auto-resize volume (%d and %d)",
  494. ubi->autoresize_vol_id, i);
  495. kfree(vol);
  496. return -EINVAL;
  497. }
  498. ubi->autoresize_vol_id = i;
  499. }
  500. ubi_assert(!ubi->volumes[i]);
  501. ubi->volumes[i] = vol;
  502. ubi->vol_count += 1;
  503. vol->ubi = ubi;
  504. reserved_pebs += vol->reserved_pebs;
  505. /*
  506. * In case of dynamic volume UBI knows nothing about how many
  507. * data is stored there. So assume the whole volume is used.
  508. */
  509. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  510. vol->used_ebs = vol->reserved_pebs;
  511. vol->last_eb_bytes = vol->usable_leb_size;
  512. vol->used_bytes =
  513. (long long)vol->used_ebs * vol->usable_leb_size;
  514. continue;
  515. }
  516. /* Static volumes only */
  517. av = ubi_find_av(ai, i);
  518. if (!av) {
  519. /*
  520. * No eraseblocks belonging to this volume found. We
  521. * don't actually know whether this static volume is
  522. * completely corrupted or just contains no data. And
  523. * we cannot know this as long as data size is not
  524. * stored on flash. So we just assume the volume is
  525. * empty. FIXME: this should be handled.
  526. */
  527. continue;
  528. }
  529. if (av->leb_count != av->used_ebs) {
  530. /*
  531. * We found a static volume which misses several
  532. * eraseblocks. Treat it as corrupted.
  533. */
  534. ubi_warn("static volume %d misses %d LEBs - corrupted",
  535. av->vol_id, av->used_ebs - av->leb_count);
  536. vol->corrupted = 1;
  537. continue;
  538. }
  539. vol->used_ebs = av->used_ebs;
  540. vol->used_bytes =
  541. (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
  542. vol->used_bytes += av->last_data_size;
  543. vol->last_eb_bytes = av->last_data_size;
  544. }
  545. /* And add the layout volume */
  546. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  547. if (!vol)
  548. return -ENOMEM;
  549. vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
  550. vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
  551. vol->vol_type = UBI_DYNAMIC_VOLUME;
  552. vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
  553. memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
  554. vol->usable_leb_size = ubi->leb_size;
  555. vol->used_ebs = vol->reserved_pebs;
  556. vol->last_eb_bytes = vol->reserved_pebs;
  557. vol->used_bytes =
  558. (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
  559. vol->vol_id = UBI_LAYOUT_VOLUME_ID;
  560. vol->ref_count = 1;
  561. ubi_assert(!ubi->volumes[i]);
  562. ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
  563. reserved_pebs += vol->reserved_pebs;
  564. ubi->vol_count += 1;
  565. vol->ubi = ubi;
  566. if (reserved_pebs > ubi->avail_pebs) {
  567. ubi_err("not enough PEBs, required %d, available %d",
  568. reserved_pebs, ubi->avail_pebs);
  569. if (ubi->corr_peb_count)
  570. ubi_err("%d PEBs are corrupted and not used",
  571. ubi->corr_peb_count);
  572. }
  573. ubi->rsvd_pebs += reserved_pebs;
  574. ubi->avail_pebs -= reserved_pebs;
  575. return 0;
  576. }
  577. /**
  578. * check_av - check volume attaching information.
  579. * @vol: UBI volume description object
  580. * @av: volume attaching information
  581. *
  582. * This function returns zero if the volume attaching information is consistent
  583. * to the data read from the volume tabla, and %-EINVAL if not.
  584. */
  585. static int check_av(const struct ubi_volume *vol,
  586. const struct ubi_ainf_volume *av)
  587. {
  588. int err;
  589. if (av->highest_lnum >= vol->reserved_pebs) {
  590. err = 1;
  591. goto bad;
  592. }
  593. if (av->leb_count > vol->reserved_pebs) {
  594. err = 2;
  595. goto bad;
  596. }
  597. if (av->vol_type != vol->vol_type) {
  598. err = 3;
  599. goto bad;
  600. }
  601. if (av->used_ebs > vol->reserved_pebs) {
  602. err = 4;
  603. goto bad;
  604. }
  605. if (av->data_pad != vol->data_pad) {
  606. err = 5;
  607. goto bad;
  608. }
  609. return 0;
  610. bad:
  611. ubi_err("bad attaching information, error %d", err);
  612. ubi_dump_av(av);
  613. ubi_dump_vol_info(vol);
  614. return -EINVAL;
  615. }
  616. /**
  617. * check_attaching_info - check that attaching information.
  618. * @ubi: UBI device description object
  619. * @ai: attaching information
  620. *
  621. * Even though we protect on-flash data by CRC checksums, we still don't trust
  622. * the media. This function ensures that attaching information is consistent to
  623. * the information read from the volume table. Returns zero if the attaching
  624. * information is OK and %-EINVAL if it is not.
  625. */
  626. static int check_attaching_info(const struct ubi_device *ubi,
  627. struct ubi_attach_info *ai)
  628. {
  629. int err, i;
  630. struct ubi_ainf_volume *av;
  631. struct ubi_volume *vol;
  632. if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
  633. ubi_err("found %d volumes while attaching, maximum is %d + %d",
  634. ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
  635. return -EINVAL;
  636. }
  637. if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
  638. ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
  639. ubi_err("too large volume ID %d found", ai->highest_vol_id);
  640. return -EINVAL;
  641. }
  642. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  643. cond_resched();
  644. av = ubi_find_av(ai, i);
  645. vol = ubi->volumes[i];
  646. if (!vol) {
  647. if (av)
  648. ubi_remove_av(ai, av);
  649. continue;
  650. }
  651. if (vol->reserved_pebs == 0) {
  652. ubi_assert(i < ubi->vtbl_slots);
  653. if (!av)
  654. continue;
  655. /*
  656. * During attaching we found a volume which does not
  657. * exist according to the information in the volume
  658. * table. This must have happened due to an unclean
  659. * reboot while the volume was being removed. Discard
  660. * these eraseblocks.
  661. */
  662. ubi_msg("finish volume %d removal", av->vol_id);
  663. ubi_remove_av(ai, av);
  664. } else if (av) {
  665. err = check_av(vol, av);
  666. if (err)
  667. return err;
  668. }
  669. }
  670. return 0;
  671. }
  672. /**
  673. * ubi_read_volume_table - read the volume table.
  674. * @ubi: UBI device description object
  675. * @ai: attaching information
  676. *
  677. * This function reads volume table, checks it, recover from errors if needed,
  678. * or creates it if needed. Returns zero in case of success and a negative
  679. * error code in case of failure.
  680. */
  681. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
  682. {
  683. int i, err;
  684. struct ubi_ainf_volume *av;
  685. empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
  686. /*
  687. * The number of supported volumes is limited by the eraseblock size
  688. * and by the UBI_MAX_VOLUMES constant.
  689. */
  690. ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
  691. if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
  692. ubi->vtbl_slots = UBI_MAX_VOLUMES;
  693. ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
  694. ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
  695. av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
  696. if (!av) {
  697. /*
  698. * No logical eraseblocks belonging to the layout volume were
  699. * found. This could mean that the flash is just empty. In
  700. * this case we create empty layout volume.
  701. *
  702. * But if flash is not empty this must be a corruption or the
  703. * MTD device just contains garbage.
  704. */
  705. if (ai->is_empty) {
  706. ubi->vtbl = create_empty_lvol(ubi, ai);
  707. if (IS_ERR(ubi->vtbl))
  708. return PTR_ERR(ubi->vtbl);
  709. } else {
  710. ubi_err("the layout volume was not found");
  711. return -EINVAL;
  712. }
  713. } else {
  714. if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
  715. /* This must not happen with proper UBI images */
  716. ubi_err("too many LEBs (%d) in layout volume",
  717. av->leb_count);
  718. return -EINVAL;
  719. }
  720. ubi->vtbl = process_lvol(ubi, ai, av);
  721. if (IS_ERR(ubi->vtbl))
  722. return PTR_ERR(ubi->vtbl);
  723. }
  724. ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
  725. /*
  726. * The layout volume is OK, initialize the corresponding in-RAM data
  727. * structures.
  728. */
  729. err = init_volumes(ubi, ai, ubi->vtbl);
  730. if (err)
  731. goto out_free;
  732. /*
  733. * Make sure that the attaching information is consistent to the
  734. * information stored in the volume table.
  735. */
  736. err = check_attaching_info(ubi, ai);
  737. if (err)
  738. goto out_free;
  739. return 0;
  740. out_free:
  741. vfree(ubi->vtbl);
  742. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  743. kfree(ubi->volumes[i]);
  744. ubi->volumes[i] = NULL;
  745. }
  746. return err;
  747. }
  748. /**
  749. * self_vtbl_check - check volume table.
  750. * @ubi: UBI device description object
  751. */
  752. static void self_vtbl_check(const struct ubi_device *ubi)
  753. {
  754. if (!ubi_dbg_chk_gen(ubi))
  755. return;
  756. if (vtbl_check(ubi, ubi->vtbl)) {
  757. ubi_err("self-check failed");
  758. BUG();
  759. }
  760. }