vtbl.c 23 KB

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