vtbl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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_SKIP_CRC_CHECK_FLG)
  494. vol->skip_check = 1;
  495. if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
  496. /* Auto re-size flag may be set only for one volume */
  497. if (ubi->autoresize_vol_id != -1) {
  498. ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
  499. ubi->autoresize_vol_id, i);
  500. kfree(vol);
  501. return -EINVAL;
  502. }
  503. ubi->autoresize_vol_id = i;
  504. }
  505. ubi_assert(!ubi->volumes[i]);
  506. ubi->volumes[i] = vol;
  507. ubi->vol_count += 1;
  508. vol->ubi = ubi;
  509. reserved_pebs += vol->reserved_pebs;
  510. /*
  511. * We use ubi->peb_count and not vol->reserved_pebs because
  512. * we want to keep the code simple. Otherwise we'd have to
  513. * resize/check the bitmap upon volume resize too.
  514. * Allocating a few bytes more does not hurt.
  515. */
  516. err = ubi_fastmap_init_checkmap(vol, ubi->peb_count);
  517. if (err)
  518. return err;
  519. /*
  520. * In case of dynamic volume UBI knows nothing about how many
  521. * data is stored there. So assume the whole volume is used.
  522. */
  523. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  524. vol->used_ebs = vol->reserved_pebs;
  525. vol->last_eb_bytes = vol->usable_leb_size;
  526. vol->used_bytes =
  527. (long long)vol->used_ebs * vol->usable_leb_size;
  528. continue;
  529. }
  530. /* Static volumes only */
  531. av = ubi_find_av(ai, i);
  532. if (!av || !av->leb_count) {
  533. /*
  534. * No eraseblocks belonging to this volume found. We
  535. * don't actually know whether this static volume is
  536. * completely corrupted or just contains no data. And
  537. * we cannot know this as long as data size is not
  538. * stored on flash. So we just assume the volume is
  539. * empty. FIXME: this should be handled.
  540. */
  541. continue;
  542. }
  543. if (av->leb_count != av->used_ebs) {
  544. /*
  545. * We found a static volume which misses several
  546. * eraseblocks. Treat it as corrupted.
  547. */
  548. ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
  549. av->vol_id, av->used_ebs - av->leb_count);
  550. vol->corrupted = 1;
  551. continue;
  552. }
  553. vol->used_ebs = av->used_ebs;
  554. vol->used_bytes =
  555. (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
  556. vol->used_bytes += av->last_data_size;
  557. vol->last_eb_bytes = av->last_data_size;
  558. }
  559. /* And add the layout volume */
  560. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  561. if (!vol)
  562. return -ENOMEM;
  563. vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
  564. vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
  565. vol->vol_type = UBI_DYNAMIC_VOLUME;
  566. vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
  567. memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
  568. vol->usable_leb_size = ubi->leb_size;
  569. vol->used_ebs = vol->reserved_pebs;
  570. vol->last_eb_bytes = vol->reserved_pebs;
  571. vol->used_bytes =
  572. (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
  573. vol->vol_id = UBI_LAYOUT_VOLUME_ID;
  574. vol->ref_count = 1;
  575. ubi_assert(!ubi->volumes[i]);
  576. ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
  577. reserved_pebs += vol->reserved_pebs;
  578. ubi->vol_count += 1;
  579. vol->ubi = ubi;
  580. err = ubi_fastmap_init_checkmap(vol, UBI_LAYOUT_VOLUME_EBS);
  581. if (err)
  582. return err;
  583. if (reserved_pebs > ubi->avail_pebs) {
  584. ubi_err(ubi, "not enough PEBs, required %d, available %d",
  585. reserved_pebs, ubi->avail_pebs);
  586. if (ubi->corr_peb_count)
  587. ubi_err(ubi, "%d PEBs are corrupted and not used",
  588. ubi->corr_peb_count);
  589. return -ENOSPC;
  590. }
  591. ubi->rsvd_pebs += reserved_pebs;
  592. ubi->avail_pebs -= reserved_pebs;
  593. return 0;
  594. }
  595. /**
  596. * check_av - check volume attaching information.
  597. * @vol: UBI volume description object
  598. * @av: volume attaching information
  599. *
  600. * This function returns zero if the volume attaching information is consistent
  601. * to the data read from the volume tabla, and %-EINVAL if not.
  602. */
  603. static int check_av(const struct ubi_volume *vol,
  604. const struct ubi_ainf_volume *av)
  605. {
  606. int err;
  607. if (av->highest_lnum >= vol->reserved_pebs) {
  608. err = 1;
  609. goto bad;
  610. }
  611. if (av->leb_count > vol->reserved_pebs) {
  612. err = 2;
  613. goto bad;
  614. }
  615. if (av->vol_type != vol->vol_type) {
  616. err = 3;
  617. goto bad;
  618. }
  619. if (av->used_ebs > vol->reserved_pebs) {
  620. err = 4;
  621. goto bad;
  622. }
  623. if (av->data_pad != vol->data_pad) {
  624. err = 5;
  625. goto bad;
  626. }
  627. return 0;
  628. bad:
  629. ubi_err(vol->ubi, "bad attaching information, error %d", err);
  630. ubi_dump_av(av);
  631. ubi_dump_vol_info(vol);
  632. return -EINVAL;
  633. }
  634. /**
  635. * check_attaching_info - check that attaching information.
  636. * @ubi: UBI device description object
  637. * @ai: attaching information
  638. *
  639. * Even though we protect on-flash data by CRC checksums, we still don't trust
  640. * the media. This function ensures that attaching information is consistent to
  641. * the information read from the volume table. Returns zero if the attaching
  642. * information is OK and %-EINVAL if it is not.
  643. */
  644. static int check_attaching_info(const struct ubi_device *ubi,
  645. struct ubi_attach_info *ai)
  646. {
  647. int err, i;
  648. struct ubi_ainf_volume *av;
  649. struct ubi_volume *vol;
  650. if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
  651. ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
  652. ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
  653. return -EINVAL;
  654. }
  655. if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
  656. ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
  657. ubi_err(ubi, "too large volume ID %d found",
  658. ai->highest_vol_id);
  659. return -EINVAL;
  660. }
  661. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  662. cond_resched();
  663. av = ubi_find_av(ai, i);
  664. vol = ubi->volumes[i];
  665. if (!vol) {
  666. if (av)
  667. ubi_remove_av(ai, av);
  668. continue;
  669. }
  670. if (vol->reserved_pebs == 0) {
  671. ubi_assert(i < ubi->vtbl_slots);
  672. if (!av)
  673. continue;
  674. /*
  675. * During attaching we found a volume which does not
  676. * exist according to the information in the volume
  677. * table. This must have happened due to an unclean
  678. * reboot while the volume was being removed. Discard
  679. * these eraseblocks.
  680. */
  681. ubi_msg(ubi, "finish volume %d removal", av->vol_id);
  682. ubi_remove_av(ai, av);
  683. } else if (av) {
  684. err = check_av(vol, av);
  685. if (err)
  686. return err;
  687. }
  688. }
  689. return 0;
  690. }
  691. /**
  692. * ubi_read_volume_table - read the volume table.
  693. * @ubi: UBI device description object
  694. * @ai: attaching information
  695. *
  696. * This function reads volume table, checks it, recover from errors if needed,
  697. * or creates it if needed. Returns zero in case of success and a negative
  698. * error code in case of failure.
  699. */
  700. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
  701. {
  702. int i, err;
  703. struct ubi_ainf_volume *av;
  704. empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
  705. /*
  706. * The number of supported volumes is limited by the eraseblock size
  707. * and by the UBI_MAX_VOLUMES constant.
  708. */
  709. ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
  710. if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
  711. ubi->vtbl_slots = UBI_MAX_VOLUMES;
  712. ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
  713. ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
  714. av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
  715. if (!av) {
  716. /*
  717. * No logical eraseblocks belonging to the layout volume were
  718. * found. This could mean that the flash is just empty. In
  719. * this case we create empty layout volume.
  720. *
  721. * But if flash is not empty this must be a corruption or the
  722. * MTD device just contains garbage.
  723. */
  724. if (ai->is_empty) {
  725. ubi->vtbl = create_empty_lvol(ubi, ai);
  726. if (IS_ERR(ubi->vtbl))
  727. return PTR_ERR(ubi->vtbl);
  728. } else {
  729. ubi_err(ubi, "the layout volume was not found");
  730. return -EINVAL;
  731. }
  732. } else {
  733. if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
  734. /* This must not happen with proper UBI images */
  735. ubi_err(ubi, "too many LEBs (%d) in layout volume",
  736. av->leb_count);
  737. return -EINVAL;
  738. }
  739. ubi->vtbl = process_lvol(ubi, ai, av);
  740. if (IS_ERR(ubi->vtbl))
  741. return PTR_ERR(ubi->vtbl);
  742. }
  743. ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
  744. /*
  745. * The layout volume is OK, initialize the corresponding in-RAM data
  746. * structures.
  747. */
  748. err = init_volumes(ubi, ai, ubi->vtbl);
  749. if (err)
  750. goto out_free;
  751. /*
  752. * Make sure that the attaching information is consistent to the
  753. * information stored in the volume table.
  754. */
  755. err = check_attaching_info(ubi, ai);
  756. if (err)
  757. goto out_free;
  758. return 0;
  759. out_free:
  760. vfree(ubi->vtbl);
  761. for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  762. ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
  763. kfree(ubi->volumes[i]);
  764. ubi->volumes[i] = NULL;
  765. }
  766. return err;
  767. }
  768. /**
  769. * self_vtbl_check - check volume table.
  770. * @ubi: UBI device description object
  771. */
  772. static void self_vtbl_check(const struct ubi_device *ubi)
  773. {
  774. if (!ubi_dbg_chk_gen(ubi))
  775. return;
  776. if (vtbl_check(ubi, ubi->vtbl)) {
  777. ubi_err(ubi, "self-check failed");
  778. BUG();
  779. }
  780. }