vtbl.c 23 KB

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