vtbl.c 24 KB

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