vtbl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. Note,
  33. * nowadays we use the atomic LEB change operation when updating the volume
  34. * table, so we do not really need 2 LEBs anymore, but we preserve the older
  35. * design for the backward compatibility reasons.
  36. *
  37. * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
  38. * erased, and the updated volume table is written back to LEB 0. Then same for
  39. * LEB 1. This scheme guarantees recoverability from unclean reboots.
  40. *
  41. * In this UBI implementation the on-flash volume table does not contain any
  42. * information about how much data static volumes contain.
  43. *
  44. * But it would still be beneficial to store this information in the volume
  45. * table. For example, suppose we have a static volume X, and all its physical
  46. * eraseblocks became bad for some reasons. Suppose we are attaching the
  47. * corresponding MTD device, for some reason we find no logical eraseblocks
  48. * corresponding to the volume X. According to the volume table volume X does
  49. * exist. So we don't know whether it is just empty or all its physical
  50. * eraseblocks went bad. So we cannot alarm the user properly.
  51. *
  52. * The volume table also stores so-called "update marker", which is used for
  53. * volume updates. Before updating the volume, the update marker is set, and
  54. * after the update operation is finished, the update marker is cleared. So if
  55. * the update operation was interrupted (e.g. by an unclean reboot) - the
  56. * update marker is still there and we know that the volume's contents is
  57. * damaged.
  58. */
  59. #include <linux/crc32.h>
  60. #include <linux/err.h>
  61. #include <linux/slab.h>
  62. #include <asm/div64.h>
  63. #include "ubi.h"
  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_update_layout_vol - helper for updatting layout volumes on flash
  69. * @ubi: UBI device description object
  70. */
  71. static int ubi_update_layout_vol(struct ubi_device *ubi)
  72. {
  73. struct ubi_volume *layout_vol;
  74. int i, err;
  75. layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
  76. for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
  77. err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl,
  78. ubi->vtbl_size);
  79. if (err)
  80. return err;
  81. }
  82. return 0;
  83. }
  84. /**
  85. * ubi_change_vtbl_record - change volume table record.
  86. * @ubi: UBI device description object
  87. * @idx: table index to change
  88. * @vtbl_rec: new volume table record
  89. *
  90. * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
  91. * volume table record is written. The caller does not have to calculate CRC of
  92. * the record as it is done by this function. Returns zero in case of success
  93. * and a negative error code in case of failure.
  94. */
  95. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  96. struct ubi_vtbl_record *vtbl_rec)
  97. {
  98. int err;
  99. uint32_t crc;
  100. ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
  101. if (!vtbl_rec)
  102. vtbl_rec = &empty_vtbl_record;
  103. else {
  104. crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
  105. vtbl_rec->crc = cpu_to_be32(crc);
  106. }
  107. memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
  108. err = ubi_update_layout_vol(ubi);
  109. self_vtbl_check(ubi);
  110. return err ? err : 0;
  111. }
  112. /**
  113. * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
  114. * @ubi: UBI device description object
  115. * @rename_list: list of &struct ubi_rename_entry objects
  116. *
  117. * This function re-names multiple volumes specified in @req in the volume
  118. * table. Returns zero in case of success and a negative error code in case of
  119. * failure.
  120. */
  121. int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
  122. struct list_head *rename_list)
  123. {
  124. struct ubi_rename_entry *re;
  125. list_for_each_entry(re, rename_list, list) {
  126. uint32_t crc;
  127. struct ubi_volume *vol = re->desc->vol;
  128. struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
  129. if (re->remove) {
  130. memcpy(vtbl_rec, &empty_vtbl_record,
  131. sizeof(struct ubi_vtbl_record));
  132. continue;
  133. }
  134. vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
  135. memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
  136. memset(vtbl_rec->name + re->new_name_len, 0,
  137. UBI_VOL_NAME_MAX + 1 - re->new_name_len);
  138. crc = crc32(UBI_CRC32_INIT, vtbl_rec,
  139. UBI_VTBL_RECORD_SIZE_CRC);
  140. vtbl_rec->crc = cpu_to_be32(crc);
  141. }
  142. return ubi_update_layout_vol(ubi);
  143. }
  144. /**
  145. * vtbl_check - check if volume table is not corrupted and sensible.
  146. * @ubi: UBI device description object
  147. * @vtbl: volume table
  148. *
  149. * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
  150. * and %-EINVAL if it contains inconsistent data.
  151. */
  152. static int vtbl_check(const struct ubi_device *ubi,
  153. const struct ubi_vtbl_record *vtbl)
  154. {
  155. int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
  156. int upd_marker, err;
  157. uint32_t crc;
  158. const char *name;
  159. for (i = 0; i < ubi->vtbl_slots; i++) {
  160. cond_resched();
  161. reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
  162. alignment = be32_to_cpu(vtbl[i].alignment);
  163. data_pad = be32_to_cpu(vtbl[i].data_pad);
  164. upd_marker = vtbl[i].upd_marker;
  165. vol_type = vtbl[i].vol_type;
  166. name_len = be16_to_cpu(vtbl[i].name_len);
  167. name = &vtbl[i].name[0];
  168. crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
  169. if (be32_to_cpu(vtbl[i].crc) != crc) {
  170. ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
  171. i, crc, be32_to_cpu(vtbl[i].crc));
  172. ubi_dump_vtbl_record(&vtbl[i], i);
  173. return 1;
  174. }
  175. if (reserved_pebs == 0) {
  176. if (memcmp(&vtbl[i], &empty_vtbl_record,
  177. UBI_VTBL_RECORD_SIZE)) {
  178. err = 2;
  179. goto bad;
  180. }
  181. continue;
  182. }
  183. if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
  184. name_len < 0) {
  185. err = 3;
  186. goto bad;
  187. }
  188. if (alignment > ubi->leb_size || alignment == 0) {
  189. err = 4;
  190. goto bad;
  191. }
  192. n = alignment & (ubi->min_io_size - 1);
  193. if (alignment != 1 && n) {
  194. err = 5;
  195. goto bad;
  196. }
  197. n = ubi->leb_size % alignment;
  198. if (data_pad != n) {
  199. ubi_err(ubi, "bad data_pad, has to be %d", n);
  200. err = 6;
  201. goto bad;
  202. }
  203. if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
  204. err = 7;
  205. goto bad;
  206. }
  207. if (upd_marker != 0 && upd_marker != 1) {
  208. err = 8;
  209. goto bad;
  210. }
  211. if (reserved_pebs > ubi->good_peb_count) {
  212. ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
  213. reserved_pebs, ubi->good_peb_count);
  214. err = 9;
  215. goto bad;
  216. }
  217. if (name_len > UBI_VOL_NAME_MAX) {
  218. err = 10;
  219. goto bad;
  220. }
  221. if (name[0] == '\0') {
  222. err = 11;
  223. goto bad;
  224. }
  225. if (name_len != strnlen(name, name_len + 1)) {
  226. err = 12;
  227. goto bad;
  228. }
  229. }
  230. /* Checks that all names are unique */
  231. for (i = 0; i < ubi->vtbl_slots - 1; i++) {
  232. for (n = i + 1; n < ubi->vtbl_slots; n++) {
  233. int len1 = be16_to_cpu(vtbl[i].name_len);
  234. int len2 = be16_to_cpu(vtbl[n].name_len);
  235. if (len1 > 0 && len1 == len2 &&
  236. !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
  237. ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
  238. i, n, vtbl[i].name);
  239. ubi_dump_vtbl_record(&vtbl[i], i);
  240. ubi_dump_vtbl_record(&vtbl[n], n);
  241. return -EINVAL;
  242. }
  243. }
  244. }
  245. return 0;
  246. bad:
  247. ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
  248. ubi_dump_vtbl_record(&vtbl[i], i);
  249. return -EINVAL;
  250. }
  251. /**
  252. * create_vtbl - create a copy of volume table.
  253. * @ubi: UBI device description object
  254. * @ai: attaching information
  255. * @copy: number of the volume table copy
  256. * @vtbl: contents of the volume table
  257. *
  258. * This function returns zero in case of success and a negative error code in
  259. * case of failure.
  260. */
  261. static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
  262. int copy, void *vtbl)
  263. {
  264. int err, tries = 0;
  265. struct ubi_vid_io_buf *vidb;
  266. struct ubi_vid_hdr *vid_hdr;
  267. struct ubi_ainf_peb *new_aeb;
  268. dbg_gen("create volume table (copy #%d)", copy + 1);
  269. vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
  270. if (!vidb)
  271. return -ENOMEM;
  272. vid_hdr = ubi_get_vid_hdr(vidb);
  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, vidb);
  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. ubi_free_aeb(ai, new_aeb);
  300. ubi_free_vid_buf(vidb);
  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. ubi_free_aeb(ai, new_aeb);
  312. out_free:
  313. ubi_free_vid_buf(vidb);
  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(ubi, "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(ubi, "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(ubi, "both volume tables are corrupted");
  415. goto out_free;
  416. }
  417. ubi_warn(ubi, "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(ubi, "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, err, 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(ubi, "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. * We use ubi->peb_count and not vol->reserved_pebs because
  510. * we want to keep the code simple. Otherwise we'd have to
  511. * resize/check the bitmap upon volume resize too.
  512. * Allocating a few bytes more does not hurt.
  513. */
  514. err = ubi_fastmap_init_checkmap(vol, ubi->peb_count);
  515. if (err)
  516. return err;
  517. /*
  518. * In case of dynamic volume UBI knows nothing about how many
  519. * data is stored there. So assume the whole volume is used.
  520. */
  521. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  522. vol->used_ebs = vol->reserved_pebs;
  523. vol->last_eb_bytes = vol->usable_leb_size;
  524. vol->used_bytes =
  525. (long long)vol->used_ebs * vol->usable_leb_size;
  526. continue;
  527. }
  528. /* Static volumes only */
  529. av = ubi_find_av(ai, i);
  530. if (!av || !av->leb_count) {
  531. /*
  532. * No eraseblocks belonging to this volume found. We
  533. * don't actually know whether this static volume is
  534. * completely corrupted or just contains no data. And
  535. * we cannot know this as long as data size is not
  536. * stored on flash. So we just assume the volume is
  537. * empty. FIXME: this should be handled.
  538. */
  539. continue;
  540. }
  541. if (av->leb_count != av->used_ebs) {
  542. /*
  543. * We found a static volume which misses several
  544. * eraseblocks. Treat it as corrupted.
  545. */
  546. ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
  547. av->vol_id, av->used_ebs - av->leb_count);
  548. vol->corrupted = 1;
  549. continue;
  550. }
  551. vol->used_ebs = av->used_ebs;
  552. vol->used_bytes =
  553. (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
  554. vol->used_bytes += av->last_data_size;
  555. vol->last_eb_bytes = av->last_data_size;
  556. }
  557. /* And add the layout volume */
  558. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  559. if (!vol)
  560. return -ENOMEM;
  561. vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
  562. vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
  563. vol->vol_type = UBI_DYNAMIC_VOLUME;
  564. vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
  565. memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
  566. vol->usable_leb_size = ubi->leb_size;
  567. vol->used_ebs = vol->reserved_pebs;
  568. vol->last_eb_bytes = vol->reserved_pebs;
  569. vol->used_bytes =
  570. (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
  571. vol->vol_id = UBI_LAYOUT_VOLUME_ID;
  572. vol->ref_count = 1;
  573. ubi_assert(!ubi->volumes[i]);
  574. ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
  575. reserved_pebs += vol->reserved_pebs;
  576. ubi->vol_count += 1;
  577. vol->ubi = ubi;
  578. err = ubi_fastmap_init_checkmap(vol, UBI_LAYOUT_VOLUME_EBS);
  579. if (err)
  580. return err;
  581. if (reserved_pebs > ubi->avail_pebs) {
  582. ubi_err(ubi, "not enough PEBs, required %d, available %d",
  583. reserved_pebs, ubi->avail_pebs);
  584. if (ubi->corr_peb_count)
  585. ubi_err(ubi, "%d PEBs are corrupted and not used",
  586. ubi->corr_peb_count);
  587. return -ENOSPC;
  588. }
  589. ubi->rsvd_pebs += reserved_pebs;
  590. ubi->avail_pebs -= reserved_pebs;
  591. return 0;
  592. }
  593. /**
  594. * check_av - check volume attaching information.
  595. * @vol: UBI volume description object
  596. * @av: volume attaching information
  597. *
  598. * This function returns zero if the volume attaching information is consistent
  599. * to the data read from the volume tabla, and %-EINVAL if not.
  600. */
  601. static int check_av(const struct ubi_volume *vol,
  602. const struct ubi_ainf_volume *av)
  603. {
  604. int err;
  605. if (av->highest_lnum >= vol->reserved_pebs) {
  606. err = 1;
  607. goto bad;
  608. }
  609. if (av->leb_count > vol->reserved_pebs) {
  610. err = 2;
  611. goto bad;
  612. }
  613. if (av->vol_type != vol->vol_type) {
  614. err = 3;
  615. goto bad;
  616. }
  617. if (av->used_ebs > vol->reserved_pebs) {
  618. err = 4;
  619. goto bad;
  620. }
  621. if (av->data_pad != vol->data_pad) {
  622. err = 5;
  623. goto bad;
  624. }
  625. return 0;
  626. bad:
  627. ubi_err(vol->ubi, "bad attaching information, error %d", err);
  628. ubi_dump_av(av);
  629. ubi_dump_vol_info(vol);
  630. return -EINVAL;
  631. }
  632. /**
  633. * check_attaching_info - check that attaching information.
  634. * @ubi: UBI device description object
  635. * @ai: attaching information
  636. *
  637. * Even though we protect on-flash data by CRC checksums, we still don't trust
  638. * the media. This function ensures that attaching information is consistent to
  639. * the information read from the volume table. Returns zero if the attaching
  640. * information is OK and %-EINVAL if it is not.
  641. */
  642. static int check_attaching_info(const struct ubi_device *ubi,
  643. struct ubi_attach_info *ai)
  644. {
  645. int err, i;
  646. struct ubi_ainf_volume *av;
  647. struct ubi_volume *vol;
  648. if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
  649. ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
  650. ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
  651. return -EINVAL;
  652. }
  653. if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
  654. ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
  655. ubi_err(ubi, "too large volume ID %d found",
  656. ai->highest_vol_id);
  657. return -EINVAL;
  658. }
  659. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  660. cond_resched();
  661. av = ubi_find_av(ai, i);
  662. vol = ubi->volumes[i];
  663. if (!vol) {
  664. if (av)
  665. ubi_remove_av(ai, av);
  666. continue;
  667. }
  668. if (vol->reserved_pebs == 0) {
  669. ubi_assert(i < ubi->vtbl_slots);
  670. if (!av)
  671. continue;
  672. /*
  673. * During attaching we found a volume which does not
  674. * exist according to the information in the volume
  675. * table. This must have happened due to an unclean
  676. * reboot while the volume was being removed. Discard
  677. * these eraseblocks.
  678. */
  679. ubi_msg(ubi, "finish volume %d removal", av->vol_id);
  680. ubi_remove_av(ai, av);
  681. } else if (av) {
  682. err = check_av(vol, av);
  683. if (err)
  684. return err;
  685. }
  686. }
  687. return 0;
  688. }
  689. /**
  690. * ubi_read_volume_table - read the volume table.
  691. * @ubi: UBI device description object
  692. * @ai: attaching information
  693. *
  694. * This function reads volume table, checks it, recover from errors if needed,
  695. * or creates it if needed. Returns zero in case of success and a negative
  696. * error code in case of failure.
  697. */
  698. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
  699. {
  700. int i, err;
  701. struct ubi_ainf_volume *av;
  702. empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
  703. /*
  704. * The number of supported volumes is limited by the eraseblock size
  705. * and by the UBI_MAX_VOLUMES constant.
  706. */
  707. ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
  708. if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
  709. ubi->vtbl_slots = UBI_MAX_VOLUMES;
  710. ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
  711. ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
  712. av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
  713. if (!av) {
  714. /*
  715. * No logical eraseblocks belonging to the layout volume were
  716. * found. This could mean that the flash is just empty. In
  717. * this case we create empty layout volume.
  718. *
  719. * But if flash is not empty this must be a corruption or the
  720. * MTD device just contains garbage.
  721. */
  722. if (ai->is_empty) {
  723. ubi->vtbl = create_empty_lvol(ubi, ai);
  724. if (IS_ERR(ubi->vtbl))
  725. return PTR_ERR(ubi->vtbl);
  726. } else {
  727. ubi_err(ubi, "the layout volume was not found");
  728. return -EINVAL;
  729. }
  730. } else {
  731. if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
  732. /* This must not happen with proper UBI images */
  733. ubi_err(ubi, "too many LEBs (%d) in layout volume",
  734. av->leb_count);
  735. return -EINVAL;
  736. }
  737. ubi->vtbl = process_lvol(ubi, ai, av);
  738. if (IS_ERR(ubi->vtbl))
  739. return PTR_ERR(ubi->vtbl);
  740. }
  741. ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
  742. /*
  743. * The layout volume is OK, initialize the corresponding in-RAM data
  744. * structures.
  745. */
  746. err = init_volumes(ubi, ai, ubi->vtbl);
  747. if (err)
  748. goto out_free;
  749. /*
  750. * Make sure that the attaching information is consistent to the
  751. * information stored in the volume table.
  752. */
  753. err = check_attaching_info(ubi, ai);
  754. if (err)
  755. goto out_free;
  756. return 0;
  757. out_free:
  758. vfree(ubi->vtbl);
  759. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  760. ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
  761. kfree(ubi->volumes[i]);
  762. ubi->volumes[i] = NULL;
  763. }
  764. return err;
  765. }
  766. /**
  767. * self_vtbl_check - check volume table.
  768. * @ubi: UBI device description object
  769. */
  770. static void self_vtbl_check(const struct ubi_device *ubi)
  771. {
  772. if (!ubi_dbg_chk_gen(ubi))
  773. return;
  774. if (vtbl_check(ubi, ubi->vtbl)) {
  775. ubi_err(ubi, "self-check failed");
  776. BUG();
  777. }
  778. }