vtbl.c 24 KB

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