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. 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_hdr *vid_hdr;
  266. struct ubi_ainf_peb *new_aeb;
  267. dbg_gen("create volume table (copy #%d)", copy + 1);
  268. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  269. if (!vid_hdr)
  270. return -ENOMEM;
  271. retry:
  272. new_aeb = ubi_early_get_peb(ubi, ai);
  273. if (IS_ERR(new_aeb)) {
  274. err = PTR_ERR(new_aeb);
  275. goto out_free;
  276. }
  277. vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
  278. vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
  279. vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
  280. vid_hdr->data_size = vid_hdr->used_ebs =
  281. vid_hdr->data_pad = cpu_to_be32(0);
  282. vid_hdr->lnum = cpu_to_be32(copy);
  283. vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
  284. /* The EC header is already there, write the VID header */
  285. err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr);
  286. if (err)
  287. goto write_error;
  288. /* Write the layout volume contents */
  289. err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
  290. if (err)
  291. goto write_error;
  292. /*
  293. * And add it to the attaching information. Don't delete the old version
  294. * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
  295. */
  296. err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
  297. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  298. ubi_free_vid_hdr(ubi, vid_hdr);
  299. return err;
  300. write_error:
  301. if (err == -EIO && ++tries <= 5) {
  302. /*
  303. * Probably this physical eraseblock went bad, try to pick
  304. * another one.
  305. */
  306. list_add(&new_aeb->u.list, &ai->erase);
  307. goto retry;
  308. }
  309. kmem_cache_free(ai->aeb_slab_cache, new_aeb);
  310. out_free:
  311. ubi_free_vid_hdr(ubi, vid_hdr);
  312. return err;
  313. }
  314. /**
  315. * process_lvol - process the layout volume.
  316. * @ubi: UBI device description object
  317. * @ai: attaching information
  318. * @av: layout volume attaching information
  319. *
  320. * This function is responsible for reading the layout volume, ensuring it is
  321. * not corrupted, and recovering from corruptions if needed. Returns volume
  322. * table in case of success and a negative error code in case of failure.
  323. */
  324. static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
  325. struct ubi_attach_info *ai,
  326. struct ubi_ainf_volume *av)
  327. {
  328. int err;
  329. struct rb_node *rb;
  330. struct ubi_ainf_peb *aeb;
  331. struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
  332. int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
  333. /*
  334. * UBI goes through the following steps when it changes the layout
  335. * volume:
  336. * a. erase LEB 0;
  337. * b. write new data to LEB 0;
  338. * c. erase LEB 1;
  339. * d. write new data to LEB 1.
  340. *
  341. * Before the change, both LEBs contain the same data.
  342. *
  343. * Due to unclean reboots, the contents of LEB 0 may be lost, but there
  344. * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
  345. * Similarly, LEB 1 may be lost, but there should be LEB 0. And
  346. * finally, unclean reboots may result in a situation when neither LEB
  347. * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
  348. * 0 contains more recent information.
  349. *
  350. * So the plan is to first check LEB 0. Then
  351. * a. if LEB 0 is OK, it must be containing the most recent data; then
  352. * we compare it with LEB 1, and if they are different, we copy LEB
  353. * 0 to LEB 1;
  354. * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
  355. * to LEB 0.
  356. */
  357. dbg_gen("check layout volume");
  358. /* Read both LEB 0 and LEB 1 into memory */
  359. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
  360. leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
  361. if (!leb[aeb->lnum]) {
  362. err = -ENOMEM;
  363. goto out_free;
  364. }
  365. err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
  366. ubi->vtbl_size);
  367. if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
  368. /*
  369. * Scrub the PEB later. Note, -EBADMSG indicates an
  370. * uncorrectable ECC error, but we have our own CRC and
  371. * the data will be checked later. If the data is OK,
  372. * the PEB will be scrubbed (because we set
  373. * aeb->scrub). If the data is not OK, the contents of
  374. * the PEB will be recovered from the second copy, and
  375. * aeb->scrub will be cleared in
  376. * 'ubi_add_to_av()'.
  377. */
  378. aeb->scrub = 1;
  379. else if (err)
  380. goto out_free;
  381. }
  382. err = -EINVAL;
  383. if (leb[0]) {
  384. leb_corrupted[0] = vtbl_check(ubi, leb[0]);
  385. if (leb_corrupted[0] < 0)
  386. goto out_free;
  387. }
  388. if (!leb_corrupted[0]) {
  389. /* LEB 0 is OK */
  390. if (leb[1])
  391. leb_corrupted[1] = memcmp(leb[0], leb[1],
  392. ubi->vtbl_size);
  393. if (leb_corrupted[1]) {
  394. ubi_warn(ubi, "volume table copy #2 is corrupted");
  395. err = create_vtbl(ubi, ai, 1, leb[0]);
  396. if (err)
  397. goto out_free;
  398. ubi_msg(ubi, "volume table was restored");
  399. }
  400. /* Both LEB 1 and LEB 2 are OK and consistent */
  401. vfree(leb[1]);
  402. return leb[0];
  403. } else {
  404. /* LEB 0 is corrupted or does not exist */
  405. if (leb[1]) {
  406. leb_corrupted[1] = vtbl_check(ubi, leb[1]);
  407. if (leb_corrupted[1] < 0)
  408. goto out_free;
  409. }
  410. if (leb_corrupted[1]) {
  411. /* Both LEB 0 and LEB 1 are corrupted */
  412. ubi_err(ubi, "both volume tables are corrupted");
  413. goto out_free;
  414. }
  415. ubi_warn(ubi, "volume table copy #1 is corrupted");
  416. err = create_vtbl(ubi, ai, 0, leb[1]);
  417. if (err)
  418. goto out_free;
  419. ubi_msg(ubi, "volume table was restored");
  420. vfree(leb[0]);
  421. return leb[1];
  422. }
  423. out_free:
  424. vfree(leb[0]);
  425. vfree(leb[1]);
  426. return ERR_PTR(err);
  427. }
  428. /**
  429. * create_empty_lvol - create empty layout volume.
  430. * @ubi: UBI device description object
  431. * @ai: attaching information
  432. *
  433. * This function returns volume table contents in case of success and a
  434. * negative error code in case of failure.
  435. */
  436. static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
  437. struct ubi_attach_info *ai)
  438. {
  439. int i;
  440. struct ubi_vtbl_record *vtbl;
  441. vtbl = vzalloc(ubi->vtbl_size);
  442. if (!vtbl)
  443. return ERR_PTR(-ENOMEM);
  444. for (i = 0; i < ubi->vtbl_slots; i++)
  445. memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
  446. for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
  447. int err;
  448. err = create_vtbl(ubi, ai, i, vtbl);
  449. if (err) {
  450. vfree(vtbl);
  451. return ERR_PTR(err);
  452. }
  453. }
  454. return vtbl;
  455. }
  456. /**
  457. * init_volumes - initialize volume information for existing volumes.
  458. * @ubi: UBI device description object
  459. * @ai: scanning information
  460. * @vtbl: volume table
  461. *
  462. * This function allocates volume description objects for existing volumes.
  463. * Returns zero in case of success and a negative error code in case of
  464. * failure.
  465. */
  466. static int init_volumes(struct ubi_device *ubi,
  467. const struct ubi_attach_info *ai,
  468. const struct ubi_vtbl_record *vtbl)
  469. {
  470. int i, reserved_pebs = 0;
  471. struct ubi_ainf_volume *av;
  472. struct ubi_volume *vol;
  473. for (i = 0; i < ubi->vtbl_slots; i++) {
  474. cond_resched();
  475. if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
  476. continue; /* Empty record */
  477. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  478. if (!vol)
  479. return -ENOMEM;
  480. vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
  481. vol->alignment = be32_to_cpu(vtbl[i].alignment);
  482. vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
  483. vol->upd_marker = vtbl[i].upd_marker;
  484. vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
  485. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  486. vol->name_len = be16_to_cpu(vtbl[i].name_len);
  487. vol->usable_leb_size = ubi->leb_size - vol->data_pad;
  488. memcpy(vol->name, vtbl[i].name, vol->name_len);
  489. vol->name[vol->name_len] = '\0';
  490. vol->vol_id = i;
  491. if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
  492. /* Auto re-size flag may be set only for one volume */
  493. if (ubi->autoresize_vol_id != -1) {
  494. ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
  495. ubi->autoresize_vol_id, i);
  496. kfree(vol);
  497. return -EINVAL;
  498. }
  499. ubi->autoresize_vol_id = i;
  500. }
  501. ubi_assert(!ubi->volumes[i]);
  502. ubi->volumes[i] = vol;
  503. ubi->vol_count += 1;
  504. vol->ubi = ubi;
  505. reserved_pebs += vol->reserved_pebs;
  506. /*
  507. * In case of dynamic volume UBI knows nothing about how many
  508. * data is stored there. So assume the whole volume is used.
  509. */
  510. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  511. vol->used_ebs = vol->reserved_pebs;
  512. vol->last_eb_bytes = vol->usable_leb_size;
  513. vol->used_bytes =
  514. (long long)vol->used_ebs * vol->usable_leb_size;
  515. continue;
  516. }
  517. /* Static volumes only */
  518. av = ubi_find_av(ai, i);
  519. if (!av || !av->leb_count) {
  520. /*
  521. * No eraseblocks belonging to this volume found. We
  522. * don't actually know whether this static volume is
  523. * completely corrupted or just contains no data. And
  524. * we cannot know this as long as data size is not
  525. * stored on flash. So we just assume the volume is
  526. * empty. FIXME: this should be handled.
  527. */
  528. continue;
  529. }
  530. if (av->leb_count != av->used_ebs) {
  531. /*
  532. * We found a static volume which misses several
  533. * eraseblocks. Treat it as corrupted.
  534. */
  535. ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
  536. av->vol_id, av->used_ebs - av->leb_count);
  537. vol->corrupted = 1;
  538. continue;
  539. }
  540. vol->used_ebs = av->used_ebs;
  541. vol->used_bytes =
  542. (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
  543. vol->used_bytes += av->last_data_size;
  544. vol->last_eb_bytes = av->last_data_size;
  545. }
  546. /* And add the layout volume */
  547. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  548. if (!vol)
  549. return -ENOMEM;
  550. vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
  551. vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
  552. vol->vol_type = UBI_DYNAMIC_VOLUME;
  553. vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
  554. memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
  555. vol->usable_leb_size = ubi->leb_size;
  556. vol->used_ebs = vol->reserved_pebs;
  557. vol->last_eb_bytes = vol->reserved_pebs;
  558. vol->used_bytes =
  559. (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
  560. vol->vol_id = UBI_LAYOUT_VOLUME_ID;
  561. vol->ref_count = 1;
  562. ubi_assert(!ubi->volumes[i]);
  563. ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
  564. reserved_pebs += vol->reserved_pebs;
  565. ubi->vol_count += 1;
  566. vol->ubi = ubi;
  567. if (reserved_pebs > ubi->avail_pebs) {
  568. ubi_err(ubi, "not enough PEBs, required %d, available %d",
  569. reserved_pebs, ubi->avail_pebs);
  570. if (ubi->corr_peb_count)
  571. ubi_err(ubi, "%d PEBs are corrupted and not used",
  572. ubi->corr_peb_count);
  573. return -ENOSPC;
  574. }
  575. ubi->rsvd_pebs += reserved_pebs;
  576. ubi->avail_pebs -= reserved_pebs;
  577. return 0;
  578. }
  579. /**
  580. * check_av - check volume attaching information.
  581. * @vol: UBI volume description object
  582. * @av: volume attaching information
  583. *
  584. * This function returns zero if the volume attaching information is consistent
  585. * to the data read from the volume tabla, and %-EINVAL if not.
  586. */
  587. static int check_av(const struct ubi_volume *vol,
  588. const struct ubi_ainf_volume *av)
  589. {
  590. int err;
  591. if (av->highest_lnum >= vol->reserved_pebs) {
  592. err = 1;
  593. goto bad;
  594. }
  595. if (av->leb_count > vol->reserved_pebs) {
  596. err = 2;
  597. goto bad;
  598. }
  599. if (av->vol_type != vol->vol_type) {
  600. err = 3;
  601. goto bad;
  602. }
  603. if (av->used_ebs > vol->reserved_pebs) {
  604. err = 4;
  605. goto bad;
  606. }
  607. if (av->data_pad != vol->data_pad) {
  608. err = 5;
  609. goto bad;
  610. }
  611. return 0;
  612. bad:
  613. ubi_err(vol->ubi, "bad attaching information, error %d", err);
  614. ubi_dump_av(av);
  615. ubi_dump_vol_info(vol);
  616. return -EINVAL;
  617. }
  618. /**
  619. * check_attaching_info - check that attaching information.
  620. * @ubi: UBI device description object
  621. * @ai: attaching information
  622. *
  623. * Even though we protect on-flash data by CRC checksums, we still don't trust
  624. * the media. This function ensures that attaching information is consistent to
  625. * the information read from the volume table. Returns zero if the attaching
  626. * information is OK and %-EINVAL if it is not.
  627. */
  628. static int check_attaching_info(const struct ubi_device *ubi,
  629. struct ubi_attach_info *ai)
  630. {
  631. int err, i;
  632. struct ubi_ainf_volume *av;
  633. struct ubi_volume *vol;
  634. if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
  635. ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
  636. ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
  637. return -EINVAL;
  638. }
  639. if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
  640. ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
  641. ubi_err(ubi, "too large volume ID %d found",
  642. 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(ubi, "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(ubi, "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(ubi, "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(ubi, "self-check failed");
  761. BUG();
  762. }
  763. }