vtbl.c 23 KB

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