vtbl.c 24 KB

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