vtbl.c 24 KB

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