vtbl.c 23 KB

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