vtbl.c 24 KB

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