vtbl.c 24 KB

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