vtbl.c 23.9 KB
Newer Older
A
Artem B. Bityutskiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * Copyright (c) International Business Machines Corp., 2006
 * Copyright (c) Nokia Corporation, 2006, 2007
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Author: Artem Bityutskiy (Битюцкий Артём)
 */

/*
 * This file includes volume table manipulation code. The volume table is an
 * on-flash table containing volume meta-data like name, number of reserved
 * physical eraseblocks, type, etc. The volume table is stored in the so-called
 * "layout volume".
 *
 * The layout volume is an internal volume which is organized as follows. It
 * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
 * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
 * other. This redundancy guarantees robustness to unclean reboots. The volume
 * table is basically an array of volume table records. Each record contains
 * full information about the volume and protected by a CRC checksum.
 *
 * The volume table is changed, it is first changed in RAM. Then LEB 0 is
 * erased, and the updated volume table is written back to LEB 0. Then same for
 * LEB 1. This scheme guarantees recoverability from unclean reboots.
 *
 * In this UBI implementation the on-flash volume table does not contain any
40
 * information about how much data static volumes contain.
A
Artem B. Bityutskiy 已提交
41 42 43 44
 *
 * But it would still be beneficial to store this information in the volume
 * table. For example, suppose we have a static volume X, and all its physical
 * eraseblocks became bad for some reasons. Suppose we are attaching the
45
 * corresponding MTD device, for some reason we find no logical eraseblocks
A
Artem B. Bityutskiy 已提交
46 47
 * corresponding to the volume X. According to the volume table volume X does
 * exist. So we don't know whether it is just empty or all its physical
48
 * eraseblocks went bad. So we cannot alarm the user properly.
A
Artem B. Bityutskiy 已提交
49 50 51 52 53 54 55 56 57 58 59
 *
 * The volume table also stores so-called "update marker", which is used for
 * volume updates. Before updating the volume, the update marker is set, and
 * after the update operation is finished, the update marker is cleared. So if
 * the update operation was interrupted (e.g. by an unclean reboot) - the
 * update marker is still there and we know that the volume's contents is
 * damaged.
 */

#include <linux/crc32.h>
#include <linux/err.h>
60
#include <linux/slab.h>
A
Artem B. Bityutskiy 已提交
61 62 63
#include <asm/div64.h>
#include "ubi.h"

64
static void self_vtbl_check(const struct ubi_device *ubi);
A
Artem B. Bityutskiy 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

/* Empty volume table record */
static struct ubi_vtbl_record empty_vtbl_record;

/**
 * ubi_change_vtbl_record - change volume table record.
 * @ubi: UBI device description object
 * @idx: table index to change
 * @vtbl_rec: new volume table record
 *
 * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
 * volume table record is written. The caller does not have to calculate CRC of
 * the record as it is done by this function. Returns zero in case of success
 * and a negative error code in case of failure.
 */
int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
			   struct ubi_vtbl_record *vtbl_rec)
{
	int i, err;
	uint32_t crc;
85
	struct ubi_volume *layout_vol;
A
Artem B. Bityutskiy 已提交
86 87

	ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
88
	layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
A
Artem B. Bityutskiy 已提交
89 90 91 92 93

	if (!vtbl_rec)
		vtbl_rec = &empty_vtbl_record;
	else {
		crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
94
		vtbl_rec->crc = cpu_to_be32(crc);
A
Artem B. Bityutskiy 已提交
95 96 97 98
	}

	memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
99
		err = ubi_eba_unmap_leb(ubi, layout_vol, i);
A
Artem Bityutskiy 已提交
100
		if (err)
A
Artem B. Bityutskiy 已提交
101
			return err;
A
Artem Bityutskiy 已提交
102

103
		err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
R
Richard Weinberger 已提交
104
					ubi->vtbl_size);
A
Artem Bityutskiy 已提交
105
		if (err)
A
Artem B. Bityutskiy 已提交
106 107 108
			return err;
	}

109
	self_vtbl_check(ubi);
110
	return 0;
A
Artem B. Bityutskiy 已提交
111 112
}

113 114 115
/**
 * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
 * @ubi: UBI device description object
116
 * @rename_list: list of &struct ubi_rename_entry objects
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
 *
 * This function re-names multiple volumes specified in @req in the volume
 * table. Returns zero in case of success and a negative error code in case of
 * failure.
 */
int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
			    struct list_head *rename_list)
{
	int i, err;
	struct ubi_rename_entry *re;
	struct ubi_volume *layout_vol;

	list_for_each_entry(re, rename_list, list) {
		uint32_t crc;
		struct ubi_volume *vol = re->desc->vol;
		struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];

		if (re->remove) {
			memcpy(vtbl_rec, &empty_vtbl_record,
			       sizeof(struct ubi_vtbl_record));
			continue;
		}

		vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
		memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
		memset(vtbl_rec->name + re->new_name_len, 0,
		       UBI_VOL_NAME_MAX + 1 - re->new_name_len);
		crc = crc32(UBI_CRC32_INIT, vtbl_rec,
			    UBI_VTBL_RECORD_SIZE_CRC);
		vtbl_rec->crc = cpu_to_be32(crc);
	}

	layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
		err = ubi_eba_unmap_leb(ubi, layout_vol, i);
		if (err)
			return err;

		err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
R
Richard Weinberger 已提交
156
					ubi->vtbl_size);
157 158 159 160 161 162 163
		if (err)
			return err;
	}

	return 0;
}

A
Artem B. Bityutskiy 已提交
164
/**
165
 * vtbl_check - check if volume table is not corrupted and sensible.
A
Artem B. Bityutskiy 已提交
166 167 168 169 170 171 172 173 174 175
 * @ubi: UBI device description object
 * @vtbl: volume table
 *
 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
 * and %-EINVAL if it contains inconsistent data.
 */
static int vtbl_check(const struct ubi_device *ubi,
		      const struct ubi_vtbl_record *vtbl)
{
	int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
A
Artem Bityutskiy 已提交
176
	int upd_marker, err;
A
Artem B. Bityutskiy 已提交
177 178 179 180 181 182
	uint32_t crc;
	const char *name;

	for (i = 0; i < ubi->vtbl_slots; i++) {
		cond_resched();

183 184 185
		reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
		alignment = be32_to_cpu(vtbl[i].alignment);
		data_pad = be32_to_cpu(vtbl[i].data_pad);
A
Artem B. Bityutskiy 已提交
186 187
		upd_marker = vtbl[i].upd_marker;
		vol_type = vtbl[i].vol_type;
188
		name_len = be16_to_cpu(vtbl[i].name_len);
A
Artem B. Bityutskiy 已提交
189 190 191
		name = &vtbl[i].name[0];

		crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
192
		if (be32_to_cpu(vtbl[i].crc) != crc) {
193
			ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
194
				 i, crc, be32_to_cpu(vtbl[i].crc));
195
			ubi_dump_vtbl_record(&vtbl[i], i);
A
Artem B. Bityutskiy 已提交
196 197 198 199 200 201
			return 1;
		}

		if (reserved_pebs == 0) {
			if (memcmp(&vtbl[i], &empty_vtbl_record,
						UBI_VTBL_RECORD_SIZE)) {
A
Artem Bityutskiy 已提交
202
				err = 2;
A
Artem B. Bityutskiy 已提交
203 204 205 206 207 208 209
				goto bad;
			}
			continue;
		}

		if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
		    name_len < 0) {
A
Artem Bityutskiy 已提交
210
			err = 3;
A
Artem B. Bityutskiy 已提交
211 212 213 214
			goto bad;
		}

		if (alignment > ubi->leb_size || alignment == 0) {
A
Artem Bityutskiy 已提交
215
			err = 4;
A
Artem B. Bityutskiy 已提交
216 217 218
			goto bad;
		}

219
		n = alignment & (ubi->min_io_size - 1);
A
Artem B. Bityutskiy 已提交
220
		if (alignment != 1 && n) {
A
Artem Bityutskiy 已提交
221
			err = 5;
A
Artem B. Bityutskiy 已提交
222 223 224 225 226
			goto bad;
		}

		n = ubi->leb_size % alignment;
		if (data_pad != n) {
227
			ubi_err(ubi, "bad data_pad, has to be %d", n);
A
Artem Bityutskiy 已提交
228
			err = 6;
A
Artem B. Bityutskiy 已提交
229 230 231 232
			goto bad;
		}

		if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
A
Artem Bityutskiy 已提交
233
			err = 7;
A
Artem B. Bityutskiy 已提交
234 235 236 237
			goto bad;
		}

		if (upd_marker != 0 && upd_marker != 1) {
A
Artem Bityutskiy 已提交
238
			err = 8;
A
Artem B. Bityutskiy 已提交
239 240 241 242
			goto bad;
		}

		if (reserved_pebs > ubi->good_peb_count) {
243
			ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
244
				reserved_pebs, ubi->good_peb_count);
A
Artem Bityutskiy 已提交
245
			err = 9;
A
Artem B. Bityutskiy 已提交
246 247 248 249
			goto bad;
		}

		if (name_len > UBI_VOL_NAME_MAX) {
A
Artem Bityutskiy 已提交
250
			err = 10;
A
Artem B. Bityutskiy 已提交
251 252 253 254
			goto bad;
		}

		if (name[0] == '\0') {
A
Artem Bityutskiy 已提交
255
			err = 11;
A
Artem B. Bityutskiy 已提交
256 257 258 259
			goto bad;
		}

		if (name_len != strnlen(name, name_len + 1)) {
A
Artem Bityutskiy 已提交
260
			err = 12;
A
Artem B. Bityutskiy 已提交
261 262 263 264 265 266 267
			goto bad;
		}
	}

	/* Checks that all names are unique */
	for (i = 0; i < ubi->vtbl_slots - 1; i++) {
		for (n = i + 1; n < ubi->vtbl_slots; n++) {
268 269
			int len1 = be16_to_cpu(vtbl[i].name_len);
			int len2 = be16_to_cpu(vtbl[n].name_len);
A
Artem B. Bityutskiy 已提交
270 271 272

			if (len1 > 0 && len1 == len2 &&
			    !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
273
				ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
A
Artem Bityutskiy 已提交
274
					i, n, vtbl[i].name);
275 276
				ubi_dump_vtbl_record(&vtbl[i], i);
				ubi_dump_vtbl_record(&vtbl[n], n);
A
Artem B. Bityutskiy 已提交
277 278 279 280 281 282 283 284
				return -EINVAL;
			}
		}
	}

	return 0;

bad:
285
	ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
286
	ubi_dump_vtbl_record(&vtbl[i], i);
A
Artem B. Bityutskiy 已提交
287 288 289 290 291 292
	return -EINVAL;
}

/**
 * create_vtbl - create a copy of volume table.
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
293
 * @ai: attaching information
A
Artem B. Bityutskiy 已提交
294 295 296 297 298 299
 * @copy: number of the volume table copy
 * @vtbl: contents of the volume table
 *
 * This function returns zero in case of success and a negative error code in
 * case of failure.
 */
A
Artem Bityutskiy 已提交
300
static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
A
Artem B. Bityutskiy 已提交
301 302 303
		       int copy, void *vtbl)
{
	int err, tries = 0;
304
	struct ubi_vid_hdr *vid_hdr;
A
Artem Bityutskiy 已提交
305
	struct ubi_ainf_peb *new_aeb;
A
Artem B. Bityutskiy 已提交
306

A
Artem Bityutskiy 已提交
307
	dbg_gen("create volume table (copy #%d)", copy + 1);
A
Artem B. Bityutskiy 已提交
308

309
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
A
Artem B. Bityutskiy 已提交
310 311 312 313
	if (!vid_hdr)
		return -ENOMEM;

retry:
314
	new_aeb = ubi_early_get_peb(ubi, ai);
A
Artem Bityutskiy 已提交
315 316
	if (IS_ERR(new_aeb)) {
		err = PTR_ERR(new_aeb);
A
Artem B. Bityutskiy 已提交
317 318 319
		goto out_free;
	}

320
	vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
321
	vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
A
Artem B. Bityutskiy 已提交
322 323
	vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
	vid_hdr->data_size = vid_hdr->used_ebs =
324 325
			     vid_hdr->data_pad = cpu_to_be32(0);
	vid_hdr->lnum = cpu_to_be32(copy);
A
Artem Bityutskiy 已提交
326
	vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
A
Artem B. Bityutskiy 已提交
327 328

	/* The EC header is already there, write the VID header */
A
Artem Bityutskiy 已提交
329
	err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr);
A
Artem B. Bityutskiy 已提交
330 331 332 333
	if (err)
		goto write_error;

	/* Write the layout volume contents */
A
Artem Bityutskiy 已提交
334
	err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
A
Artem B. Bityutskiy 已提交
335 336 337 338
	if (err)
		goto write_error;

	/*
A
Artem Bityutskiy 已提交
339
	 * And add it to the attaching information. Don't delete the old version
A
Artem Bityutskiy 已提交
340
	 * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
A
Artem B. Bityutskiy 已提交
341
	 */
A
Artem Bityutskiy 已提交
342
	err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
343
	kmem_cache_free(ai->aeb_slab_cache, new_aeb);
A
Artem B. Bityutskiy 已提交
344 345 346 347
	ubi_free_vid_hdr(ubi, vid_hdr);
	return err;

write_error:
348 349 350 351 352
	if (err == -EIO && ++tries <= 5) {
		/*
		 * Probably this physical eraseblock went bad, try to pick
		 * another one.
		 */
A
Artem Bityutskiy 已提交
353
		list_add(&new_aeb->u.list, &ai->erase);
F
Florin Malita 已提交
354
		goto retry;
355
	}
356
	kmem_cache_free(ai->aeb_slab_cache, new_aeb);
A
Artem B. Bityutskiy 已提交
357 358 359 360 361 362 363 364 365
out_free:
	ubi_free_vid_hdr(ubi, vid_hdr);
	return err;

}

/**
 * process_lvol - process the layout volume.
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
366
 * @ai: attaching information
A
Artem Bityutskiy 已提交
367
 * @av: layout volume attaching information
A
Artem B. Bityutskiy 已提交
368 369 370 371 372
 *
 * This function is responsible for reading the layout volume, ensuring it is
 * not corrupted, and recovering from corruptions if needed. Returns volume
 * table in case of success and a negative error code in case of failure.
 */
373
static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
A
Artem Bityutskiy 已提交
374
					    struct ubi_attach_info *ai,
A
Artem Bityutskiy 已提交
375
					    struct ubi_ainf_volume *av)
A
Artem B. Bityutskiy 已提交
376 377 378
{
	int err;
	struct rb_node *rb;
A
Artem Bityutskiy 已提交
379
	struct ubi_ainf_peb *aeb;
A
Artem B. Bityutskiy 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
	int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};

	/*
	 * UBI goes through the following steps when it changes the layout
	 * volume:
	 * a. erase LEB 0;
	 * b. write new data to LEB 0;
	 * c. erase LEB 1;
	 * d. write new data to LEB 1.
	 *
	 * Before the change, both LEBs contain the same data.
	 *
	 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
	 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
	 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
	 * finally, unclean reboots may result in a situation when neither LEB
	 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
	 * 0 contains more recent information.
	 *
	 * So the plan is to first check LEB 0. Then
S
Shinya Kuribayashi 已提交
401
	 * a. if LEB 0 is OK, it must be containing the most recent data; then
A
Artem B. Bityutskiy 已提交
402 403 404 405 406 407
	 *    we compare it with LEB 1, and if they are different, we copy LEB
	 *    0 to LEB 1;
	 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
	 *    to LEB 0.
	 */

408
	dbg_gen("check layout volume");
A
Artem B. Bityutskiy 已提交
409 410

	/* Read both LEB 0 and LEB 1 into memory */
A
Artem Bityutskiy 已提交
411
	ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
A
Artem Bityutskiy 已提交
412 413
		leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
		if (!leb[aeb->lnum]) {
A
Artem B. Bityutskiy 已提交
414 415 416 417
			err = -ENOMEM;
			goto out_free;
		}

A
Artem Bityutskiy 已提交
418
		err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
A
Artem B. Bityutskiy 已提交
419
				       ubi->vtbl_size);
420
		if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
A
Artem Bityutskiy 已提交
421 422 423 424 425
			/*
			 * Scrub the PEB later. Note, -EBADMSG indicates an
			 * uncorrectable ECC error, but we have our own CRC and
			 * the data will be checked later. If the data is OK,
			 * the PEB will be scrubbed (because we set
A
Artem Bityutskiy 已提交
426
			 * aeb->scrub). If the data is not OK, the contents of
A
Artem Bityutskiy 已提交
427
			 * the PEB will be recovered from the second copy, and
A
Artem Bityutskiy 已提交
428
			 * aeb->scrub will be cleared in
A
Artem Bityutskiy 已提交
429
			 * 'ubi_add_to_av()'.
A
Artem Bityutskiy 已提交
430
			 */
A
Artem Bityutskiy 已提交
431
			aeb->scrub = 1;
A
Artem B. Bityutskiy 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445
		else if (err)
			goto out_free;
	}

	err = -EINVAL;
	if (leb[0]) {
		leb_corrupted[0] = vtbl_check(ubi, leb[0]);
		if (leb_corrupted[0] < 0)
			goto out_free;
	}

	if (!leb_corrupted[0]) {
		/* LEB 0 is OK */
		if (leb[1])
446 447
			leb_corrupted[1] = memcmp(leb[0], leb[1],
						  ubi->vtbl_size);
A
Artem B. Bityutskiy 已提交
448
		if (leb_corrupted[1]) {
449
			ubi_warn(ubi, "volume table copy #2 is corrupted");
A
Artem Bityutskiy 已提交
450
			err = create_vtbl(ubi, ai, 1, leb[0]);
A
Artem B. Bityutskiy 已提交
451 452
			if (err)
				goto out_free;
453
			ubi_msg(ubi, "volume table was restored");
A
Artem B. Bityutskiy 已提交
454 455 456
		}

		/* Both LEB 1 and LEB 2 are OK and consistent */
457
		vfree(leb[1]);
A
Artem B. Bityutskiy 已提交
458 459 460 461 462 463 464 465 466 467
		return leb[0];
	} else {
		/* LEB 0 is corrupted or does not exist */
		if (leb[1]) {
			leb_corrupted[1] = vtbl_check(ubi, leb[1]);
			if (leb_corrupted[1] < 0)
				goto out_free;
		}
		if (leb_corrupted[1]) {
			/* Both LEB 0 and LEB 1 are corrupted */
468
			ubi_err(ubi, "both volume tables are corrupted");
A
Artem B. Bityutskiy 已提交
469 470 471
			goto out_free;
		}

472
		ubi_warn(ubi, "volume table copy #1 is corrupted");
A
Artem Bityutskiy 已提交
473
		err = create_vtbl(ubi, ai, 0, leb[1]);
A
Artem B. Bityutskiy 已提交
474 475
		if (err)
			goto out_free;
476
		ubi_msg(ubi, "volume table was restored");
A
Artem B. Bityutskiy 已提交
477

478
		vfree(leb[0]);
A
Artem B. Bityutskiy 已提交
479 480 481 482
		return leb[1];
	}

out_free:
483 484
	vfree(leb[0]);
	vfree(leb[1]);
A
Artem B. Bityutskiy 已提交
485 486 487 488 489 490
	return ERR_PTR(err);
}

/**
 * create_empty_lvol - create empty layout volume.
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
491
 * @ai: attaching information
A
Artem B. Bityutskiy 已提交
492 493 494 495
 *
 * This function returns volume table contents in case of success and a
 * negative error code in case of failure.
 */
496
static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
A
Artem Bityutskiy 已提交
497
						 struct ubi_attach_info *ai)
A
Artem B. Bityutskiy 已提交
498 499 500 501
{
	int i;
	struct ubi_vtbl_record *vtbl;

J
Joe Perches 已提交
502
	vtbl = vzalloc(ubi->vtbl_size);
A
Artem B. Bityutskiy 已提交
503 504 505 506 507 508 509 510 511
	if (!vtbl)
		return ERR_PTR(-ENOMEM);

	for (i = 0; i < ubi->vtbl_slots; i++)
		memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);

	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
		int err;

A
Artem Bityutskiy 已提交
512
		err = create_vtbl(ubi, ai, i, vtbl);
A
Artem B. Bityutskiy 已提交
513
		if (err) {
514
			vfree(vtbl);
A
Artem B. Bityutskiy 已提交
515 516 517 518 519 520 521 522 523 524
			return ERR_PTR(err);
		}
	}

	return vtbl;
}

/**
 * init_volumes - initialize volume information for existing volumes.
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
525
 * @ai: scanning information
A
Artem B. Bityutskiy 已提交
526 527 528 529 530 531
 * @vtbl: volume table
 *
 * This function allocates volume description objects for existing volumes.
 * Returns zero in case of success and a negative error code in case of
 * failure.
 */
532
static int init_volumes(struct ubi_device *ubi,
A
Artem Bityutskiy 已提交
533
			const struct ubi_attach_info *ai,
A
Artem B. Bityutskiy 已提交
534 535 536
			const struct ubi_vtbl_record *vtbl)
{
	int i, reserved_pebs = 0;
A
Artem Bityutskiy 已提交
537
	struct ubi_ainf_volume *av;
A
Artem B. Bityutskiy 已提交
538 539 540 541 542
	struct ubi_volume *vol;

	for (i = 0; i < ubi->vtbl_slots; i++) {
		cond_resched();

543
		if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
A
Artem B. Bityutskiy 已提交
544 545 546 547 548 549
			continue; /* Empty record */

		vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
		if (!vol)
			return -ENOMEM;

550 551 552
		vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
		vol->alignment = be32_to_cpu(vtbl[i].alignment);
		vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
P
Peter Horton 已提交
553
		vol->upd_marker = vtbl[i].upd_marker;
A
Artem B. Bityutskiy 已提交
554 555
		vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
					UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
556
		vol->name_len = be16_to_cpu(vtbl[i].name_len);
A
Artem B. Bityutskiy 已提交
557 558 559 560 561
		vol->usable_leb_size = ubi->leb_size - vol->data_pad;
		memcpy(vol->name, vtbl[i].name, vol->name_len);
		vol->name[vol->name_len] = '\0';
		vol->vol_id = i;

A
Artem Bityutskiy 已提交
562 563 564
		if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
			/* Auto re-size flag may be set only for one volume */
			if (ubi->autoresize_vol_id != -1) {
565
				ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
A
Artem Bityutskiy 已提交
566
					ubi->autoresize_vol_id, i);
567
				kfree(vol);
A
Artem Bityutskiy 已提交
568 569 570 571 572 573
				return -EINVAL;
			}

			ubi->autoresize_vol_id = i;
		}

A
Artem B. Bityutskiy 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586
		ubi_assert(!ubi->volumes[i]);
		ubi->volumes[i] = vol;
		ubi->vol_count += 1;
		vol->ubi = ubi;
		reserved_pebs += vol->reserved_pebs;

		/*
		 * In case of dynamic volume UBI knows nothing about how many
		 * data is stored there. So assume the whole volume is used.
		 */
		if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
			vol->used_ebs = vol->reserved_pebs;
			vol->last_eb_bytes = vol->usable_leb_size;
V
Vinit Agnihotri 已提交
587 588
			vol->used_bytes =
				(long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
589 590 591 592
			continue;
		}

		/* Static volumes only */
A
Artem Bityutskiy 已提交
593
		av = ubi_find_av(ai, i);
594
		if (!av || !av->leb_count) {
A
Artem B. Bityutskiy 已提交
595 596 597 598 599 600 601 602 603 604 605
			/*
			 * No eraseblocks belonging to this volume found. We
			 * don't actually know whether this static volume is
			 * completely corrupted or just contains no data. And
			 * we cannot know this as long as data size is not
			 * stored on flash. So we just assume the volume is
			 * empty. FIXME: this should be handled.
			 */
			continue;
		}

A
Artem Bityutskiy 已提交
606
		if (av->leb_count != av->used_ebs) {
A
Artem B. Bityutskiy 已提交
607 608 609 610
			/*
			 * We found a static volume which misses several
			 * eraseblocks. Treat it as corrupted.
			 */
611
			ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
A
Artem Bityutskiy 已提交
612
				 av->vol_id, av->used_ebs - av->leb_count);
A
Artem B. Bityutskiy 已提交
613 614 615 616
			vol->corrupted = 1;
			continue;
		}

A
Artem Bityutskiy 已提交
617
		vol->used_ebs = av->used_ebs;
V
Vinit Agnihotri 已提交
618 619
		vol->used_bytes =
			(long long)(vol->used_ebs - 1) * vol->usable_leb_size;
A
Artem Bityutskiy 已提交
620 621
		vol->used_bytes += av->last_data_size;
		vol->last_eb_bytes = av->last_data_size;
A
Artem B. Bityutskiy 已提交
622 623
	}

624
	/* And add the layout volume */
A
Artem B. Bityutskiy 已提交
625 626 627 628 629
	vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
	if (!vol)
		return -ENOMEM;

	vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
630
	vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
A
Artem B. Bityutskiy 已提交
631 632 633 634 635 636
	vol->vol_type = UBI_DYNAMIC_VOLUME;
	vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
	memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
	vol->usable_leb_size = ubi->leb_size;
	vol->used_ebs = vol->reserved_pebs;
	vol->last_eb_bytes = vol->reserved_pebs;
V
Vinit Agnihotri 已提交
637 638
	vol->used_bytes =
		(long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
639
	vol->vol_id = UBI_LAYOUT_VOLUME_ID;
640
	vol->ref_count = 1;
A
Artem B. Bityutskiy 已提交
641 642 643 644 645 646 647

	ubi_assert(!ubi->volumes[i]);
	ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
	reserved_pebs += vol->reserved_pebs;
	ubi->vol_count += 1;
	vol->ubi = ubi;

A
Artem Bityutskiy 已提交
648
	if (reserved_pebs > ubi->avail_pebs) {
649
		ubi_err(ubi, "not enough PEBs, required %d, available %d",
A
Artem B. Bityutskiy 已提交
650
			reserved_pebs, ubi->avail_pebs);
A
Artem Bityutskiy 已提交
651
		if (ubi->corr_peb_count)
652
			ubi_err(ubi, "%d PEBs are corrupted and not used",
A
Artem Bityutskiy 已提交
653 654
				ubi->corr_peb_count);
	}
A
Artem B. Bityutskiy 已提交
655 656 657 658 659 660 661
	ubi->rsvd_pebs += reserved_pebs;
	ubi->avail_pebs -= reserved_pebs;

	return 0;
}

/**
A
Artem Bityutskiy 已提交
662
 * check_av - check volume attaching information.
663
 * @ubi: UBI device description object
A
Artem B. Bityutskiy 已提交
664
 * @vol: UBI volume description object
A
Artem Bityutskiy 已提交
665
 * @av: volume attaching information
A
Artem B. Bityutskiy 已提交
666
 *
A
Artem Bityutskiy 已提交
667
 * This function returns zero if the volume attaching information is consistent
A
Artem B. Bityutskiy 已提交
668 669
 * to the data read from the volume tabla, and %-EINVAL if not.
 */
670
static int check_av(const struct ubi_device *ubi, const struct ubi_volume *vol,
A
Artem Bityutskiy 已提交
671
		    const struct ubi_ainf_volume *av)
A
Artem B. Bityutskiy 已提交
672
{
A
Artem Bityutskiy 已提交
673 674
	int err;

A
Artem Bityutskiy 已提交
675
	if (av->highest_lnum >= vol->reserved_pebs) {
A
Artem Bityutskiy 已提交
676
		err = 1;
A
Artem B. Bityutskiy 已提交
677 678
		goto bad;
	}
A
Artem Bityutskiy 已提交
679
	if (av->leb_count > vol->reserved_pebs) {
A
Artem Bityutskiy 已提交
680
		err = 2;
A
Artem B. Bityutskiy 已提交
681 682
		goto bad;
	}
A
Artem Bityutskiy 已提交
683
	if (av->vol_type != vol->vol_type) {
A
Artem Bityutskiy 已提交
684
		err = 3;
A
Artem B. Bityutskiy 已提交
685 686
		goto bad;
	}
A
Artem Bityutskiy 已提交
687
	if (av->used_ebs > vol->reserved_pebs) {
A
Artem Bityutskiy 已提交
688
		err = 4;
A
Artem B. Bityutskiy 已提交
689 690
		goto bad;
	}
A
Artem Bityutskiy 已提交
691
	if (av->data_pad != vol->data_pad) {
A
Artem Bityutskiy 已提交
692
		err = 5;
A
Artem B. Bityutskiy 已提交
693 694 695 696 697
		goto bad;
	}
	return 0;

bad:
698
	ubi_err(ubi, "bad attaching information, error %d", err);
A
Artem Bityutskiy 已提交
699
	ubi_dump_av(av);
700
	ubi_dump_vol_info(vol);
A
Artem B. Bityutskiy 已提交
701 702 703 704
	return -EINVAL;
}

/**
705
 * check_attaching_info - check that attaching information.
A
Artem B. Bityutskiy 已提交
706
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
707
 * @ai: attaching information
A
Artem B. Bityutskiy 已提交
708 709
 *
 * Even though we protect on-flash data by CRC checksums, we still don't trust
A
Artem Bityutskiy 已提交
710
 * the media. This function ensures that attaching information is consistent to
711
 * the information read from the volume table. Returns zero if the attaching
A
Artem B. Bityutskiy 已提交
712 713
 * information is OK and %-EINVAL if it is not.
 */
714
static int check_attaching_info(const struct ubi_device *ubi,
A
Artem Bityutskiy 已提交
715
			       struct ubi_attach_info *ai)
A
Artem B. Bityutskiy 已提交
716 717
{
	int err, i;
A
Artem Bityutskiy 已提交
718
	struct ubi_ainf_volume *av;
A
Artem B. Bityutskiy 已提交
719 720
	struct ubi_volume *vol;

A
Artem Bityutskiy 已提交
721
	if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
722
		ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
A
Artem Bityutskiy 已提交
723
			ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
A
Artem B. Bityutskiy 已提交
724 725 726
		return -EINVAL;
	}

A
Artem Bityutskiy 已提交
727 728
	if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
	    ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
729 730
		ubi_err(ubi, "too large volume ID %d found",
			ai->highest_vol_id);
A
Artem B. Bityutskiy 已提交
731 732 733 734 735 736
		return -EINVAL;
	}

	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
		cond_resched();

A
Artem Bityutskiy 已提交
737
		av = ubi_find_av(ai, i);
A
Artem B. Bityutskiy 已提交
738 739
		vol = ubi->volumes[i];
		if (!vol) {
A
Artem Bityutskiy 已提交
740
			if (av)
741
				ubi_remove_av(ai, av);
A
Artem B. Bityutskiy 已提交
742 743 744 745 746 747
			continue;
		}

		if (vol->reserved_pebs == 0) {
			ubi_assert(i < ubi->vtbl_slots);

A
Artem Bityutskiy 已提交
748
			if (!av)
A
Artem B. Bityutskiy 已提交
749 750 751
				continue;

			/*
752
			 * During attaching we found a volume which does not
A
Artem B. Bityutskiy 已提交
753 754 755 756 757
			 * exist according to the information in the volume
			 * table. This must have happened due to an unclean
			 * reboot while the volume was being removed. Discard
			 * these eraseblocks.
			 */
758
			ubi_msg(ubi, "finish volume %d removal", av->vol_id);
759
			ubi_remove_av(ai, av);
A
Artem Bityutskiy 已提交
760
		} else if (av) {
761
			err = check_av(ubi, vol, av);
A
Artem B. Bityutskiy 已提交
762 763 764 765 766 767 768 769 770
			if (err)
				return err;
		}
	}

	return 0;
}

/**
771
 * ubi_read_volume_table - read the volume table.
A
Artem B. Bityutskiy 已提交
772
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
773
 * @ai: attaching information
A
Artem B. Bityutskiy 已提交
774 775 776 777 778
 *
 * This function reads volume table, checks it, recover from errors if needed,
 * or creates it if needed. Returns zero in case of success and a negative
 * error code in case of failure.
 */
A
Artem Bityutskiy 已提交
779
int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
A
Artem B. Bityutskiy 已提交
780 781
{
	int i, err;
A
Artem Bityutskiy 已提交
782
	struct ubi_ainf_volume *av;
A
Artem B. Bityutskiy 已提交
783

784
	empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
A
Artem B. Bityutskiy 已提交
785 786 787 788 789 790 791 792 793 794 795 796

	/*
	 * The number of supported volumes is limited by the eraseblock size
	 * and by the UBI_MAX_VOLUMES constant.
	 */
	ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
	if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
		ubi->vtbl_slots = UBI_MAX_VOLUMES;

	ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
	ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);

A
Artem Bityutskiy 已提交
797
	av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
A
Artem Bityutskiy 已提交
798
	if (!av) {
A
Artem B. Bityutskiy 已提交
799 800 801 802 803 804 805 806
		/*
		 * No logical eraseblocks belonging to the layout volume were
		 * found. This could mean that the flash is just empty. In
		 * this case we create empty layout volume.
		 *
		 * But if flash is not empty this must be a corruption or the
		 * MTD device just contains garbage.
		 */
A
Artem Bityutskiy 已提交
807 808
		if (ai->is_empty) {
			ubi->vtbl = create_empty_lvol(ubi, ai);
A
Artem B. Bityutskiy 已提交
809 810 811
			if (IS_ERR(ubi->vtbl))
				return PTR_ERR(ubi->vtbl);
		} else {
812
			ubi_err(ubi, "the layout volume was not found");
A
Artem B. Bityutskiy 已提交
813 814 815
			return -EINVAL;
		}
	} else {
A
Artem Bityutskiy 已提交
816
		if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
A
Artem B. Bityutskiy 已提交
817
			/* This must not happen with proper UBI images */
818
			ubi_err(ubi, "too many LEBs (%d) in layout volume",
A
Artem Bityutskiy 已提交
819
				av->leb_count);
A
Artem B. Bityutskiy 已提交
820 821 822
			return -EINVAL;
		}

A
Artem Bityutskiy 已提交
823
		ubi->vtbl = process_lvol(ubi, ai, av);
A
Artem B. Bityutskiy 已提交
824 825 826 827
		if (IS_ERR(ubi->vtbl))
			return PTR_ERR(ubi->vtbl);
	}

A
Artem Bityutskiy 已提交
828
	ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
A
Artem B. Bityutskiy 已提交
829 830 831 832 833

	/*
	 * The layout volume is OK, initialize the corresponding in-RAM data
	 * structures.
	 */
A
Artem Bityutskiy 已提交
834
	err = init_volumes(ubi, ai, ubi->vtbl);
A
Artem B. Bityutskiy 已提交
835 836 837 838
	if (err)
		goto out_free;

	/*
A
Artem Bityutskiy 已提交
839
	 * Make sure that the attaching information is consistent to the
A
Artem B. Bityutskiy 已提交
840 841
	 * information stored in the volume table.
	 */
842
	err = check_attaching_info(ubi, ai);
A
Artem B. Bityutskiy 已提交
843 844 845 846 847 848
	if (err)
		goto out_free;

	return 0;

out_free:
849
	vfree(ubi->vtbl);
850 851 852 853
	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
		kfree(ubi->volumes[i]);
		ubi->volumes[i] = NULL;
	}
A
Artem B. Bityutskiy 已提交
854 855 856 857
	return err;
}

/**
858
 * self_vtbl_check - check volume table.
A
Artem B. Bityutskiy 已提交
859 860
 * @ubi: UBI device description object
 */
861
static void self_vtbl_check(const struct ubi_device *ubi)
A
Artem B. Bityutskiy 已提交
862
{
863
	if (!ubi_dbg_chk_gen(ubi))
A
Artem Bityutskiy 已提交
864 865
		return;

A
Artem B. Bityutskiy 已提交
866
	if (vtbl_check(ubi, ubi->vtbl)) {
867
		ubi_err(ubi, "self-check failed");
A
Artem B. Bityutskiy 已提交
868 869 870
		BUG();
	}
}