vtbl.c 25 KB

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