vtbl.c 22 KB

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