vtbl.c 24 KB

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