vtbl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) International Business Machines Corp., 2006
  4. * Copyright (c) Nokia Corporation, 2006, 2007
  5. *
  6. * Author: Artem Bityutskiy (Битюцкий Артём)
  7. */
  8. /*
  9. * This file includes volume table manipulation code. The volume table is an
  10. * on-flash table containing volume meta-data like name, number of reserved
  11. * physical eraseblocks, type, etc. The volume table is stored in the so-called
  12. * "layout volume".
  13. *
  14. * The layout volume is an internal volume which is organized as follows. It
  15. * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
  16. * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
  17. * other. This redundancy guarantees robustness to unclean reboots. The volume
  18. * table is basically an array of volume table records. Each record contains
  19. * full information about the volume and protected by a CRC checksum. Note,
  20. * nowadays we use the atomic LEB change operation when updating the volume
  21. * table, so we do not really need 2 LEBs anymore, but we preserve the older
  22. * design for the backward compatibility reasons.
  23. *
  24. * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
  25. * erased, and the updated volume table is written back to LEB 0. Then same for
  26. * LEB 1. This scheme guarantees recoverability from unclean reboots.
  27. *
  28. * In this UBI implementation the on-flash volume table does not contain any
  29. * information about how much data static volumes contain.
  30. *
  31. * But it would still be beneficial to store this information in the volume
  32. * table. For example, suppose we have a static volume X, and all its physical
  33. * eraseblocks became bad for some reasons. Suppose we are attaching the
  34. * corresponding MTD device, for some reason we find no logical eraseblocks
  35. * corresponding to the volume X. According to the volume table volume X does
  36. * exist. So we don't know whether it is just empty or all its physical
  37. * eraseblocks went bad. So we cannot alarm the user properly.
  38. *
  39. * The volume table also stores so-called "update marker", which is used for
  40. * volume updates. Before updating the volume, the update marker is set, and
  41. * after the update operation is finished, the update marker is cleared. So if
  42. * the update operation was interrupted (e.g. by an unclean reboot) - the
  43. * update marker is still there and we know that the volume's contents is
  44. * damaged.
  45. */
  46. #ifndef __UBOOT__
  47. #include <linux/crc32.h>
  48. #include <linux/err.h>
  49. #include <linux/slab.h>
  50. #include <asm/div64.h>
  51. #include <u-boot/crc.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_SKIP_CRC_CHECK_FLG)
  489. vol->skip_check = 1;
  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(ubi, "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 || !av->leb_count) {
  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(ubi, "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(ubi, "not enough PEBs, required %d, available %d",
  568. reserved_pebs, ubi->avail_pebs);
  569. if (ubi->corr_peb_count)
  570. ubi_err(ubi, "%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(vol->ubi, "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(ubi, "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(ubi, "too large volume ID %d found",
  640. ai->highest_vol_id);
  641. return -EINVAL;
  642. }
  643. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  644. cond_resched();
  645. av = ubi_find_av(ai, i);
  646. vol = ubi->volumes[i];
  647. if (!vol) {
  648. if (av)
  649. ubi_remove_av(ai, av);
  650. continue;
  651. }
  652. if (vol->reserved_pebs == 0) {
  653. ubi_assert(i < ubi->vtbl_slots);
  654. if (!av)
  655. continue;
  656. /*
  657. * During attaching we found a volume which does not
  658. * exist according to the information in the volume
  659. * table. This must have happened due to an unclean
  660. * reboot while the volume was being removed. Discard
  661. * these eraseblocks.
  662. */
  663. ubi_msg(ubi, "finish volume %d removal", av->vol_id);
  664. ubi_remove_av(ai, av);
  665. } else if (av) {
  666. err = check_av(vol, av);
  667. if (err)
  668. return err;
  669. }
  670. }
  671. return 0;
  672. }
  673. /**
  674. * ubi_read_volume_table - read the volume table.
  675. * @ubi: UBI device description object
  676. * @ai: attaching information
  677. *
  678. * This function reads volume table, checks it, recover from errors if needed,
  679. * or creates it if needed. Returns zero in case of success and a negative
  680. * error code in case of failure.
  681. */
  682. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
  683. {
  684. int i, err;
  685. struct ubi_ainf_volume *av;
  686. empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
  687. /*
  688. * The number of supported volumes is limited by the eraseblock size
  689. * and by the UBI_MAX_VOLUMES constant.
  690. */
  691. ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
  692. if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
  693. ubi->vtbl_slots = UBI_MAX_VOLUMES;
  694. ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
  695. ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
  696. av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
  697. if (!av) {
  698. /*
  699. * No logical eraseblocks belonging to the layout volume were
  700. * found. This could mean that the flash is just empty. In
  701. * this case we create empty layout volume.
  702. *
  703. * But if flash is not empty this must be a corruption or the
  704. * MTD device just contains garbage.
  705. */
  706. if (ai->is_empty) {
  707. ubi->vtbl = create_empty_lvol(ubi, ai);
  708. if (IS_ERR(ubi->vtbl))
  709. return PTR_ERR(ubi->vtbl);
  710. } else {
  711. ubi_err(ubi, "the layout volume was not found");
  712. return -EINVAL;
  713. }
  714. } else {
  715. if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
  716. /* This must not happen with proper UBI images */
  717. ubi_err(ubi, "too many LEBs (%d) in layout volume",
  718. av->leb_count);
  719. return -EINVAL;
  720. }
  721. ubi->vtbl = process_lvol(ubi, ai, av);
  722. if (IS_ERR(ubi->vtbl))
  723. return PTR_ERR(ubi->vtbl);
  724. }
  725. ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
  726. /*
  727. * The layout volume is OK, initialize the corresponding in-RAM data
  728. * structures.
  729. */
  730. err = init_volumes(ubi, ai, ubi->vtbl);
  731. if (err)
  732. goto out_free;
  733. /*
  734. * Make sure that the attaching information is consistent to the
  735. * information stored in the volume table.
  736. */
  737. err = check_attaching_info(ubi, ai);
  738. if (err)
  739. goto out_free;
  740. return 0;
  741. out_free:
  742. vfree(ubi->vtbl);
  743. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  744. kfree(ubi->volumes[i]);
  745. ubi->volumes[i] = NULL;
  746. }
  747. return err;
  748. }
  749. /**
  750. * self_vtbl_check - check volume table.
  751. * @ubi: UBI device description object
  752. */
  753. static void self_vtbl_check(const struct ubi_device *ubi)
  754. {
  755. if (!ubi_dbg_chk_gen(ubi))
  756. return;
  757. if (vtbl_check(ubi, ubi->vtbl)) {
  758. ubi_err(ubi, "self-check failed");
  759. BUG();
  760. }
  761. }