vmt.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
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * 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 contains implementation of volume creation, deletion, updating and
 * resizing.
 */

#include <linux/err.h>
A
Artem Bityutskiy 已提交
27
#include <linux/math64.h>
A
Artem B. Bityutskiy 已提交
28 29 30
#include "ubi.h"

#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
31
static int paranoid_check_volumes(struct ubi_device *ubi);
A
Artem B. Bityutskiy 已提交
32
#else
33
#define paranoid_check_volumes(ubi) 0
A
Artem B. Bityutskiy 已提交
34 35 36 37 38 39
#endif

static ssize_t vol_attribute_show(struct device *dev,
				  struct device_attribute *attr, char *buf);

/* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
A
Artem Bityutskiy 已提交
40
static struct device_attribute attr_vol_reserved_ebs =
A
Artem B. Bityutskiy 已提交
41
	__ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
42
static struct device_attribute attr_vol_type =
A
Artem B. Bityutskiy 已提交
43
	__ATTR(type, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
44
static struct device_attribute attr_vol_name =
A
Artem B. Bityutskiy 已提交
45
	__ATTR(name, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
46
static struct device_attribute attr_vol_corrupted =
A
Artem B. Bityutskiy 已提交
47
	__ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
48
static struct device_attribute attr_vol_alignment =
A
Artem B. Bityutskiy 已提交
49
	__ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
50
static struct device_attribute attr_vol_usable_eb_size =
A
Artem B. Bityutskiy 已提交
51
	__ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
52
static struct device_attribute attr_vol_data_bytes =
A
Artem B. Bityutskiy 已提交
53
	__ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
54
static struct device_attribute attr_vol_upd_marker =
A
Artem B. Bityutskiy 已提交
55 56 57 58 59 60 61 62 63 64 65
	__ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);

/*
 * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
 *
 * Consider a situation:
 * A. process 1 opens a sysfs file related to volume Y, say
 *    /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
 * B. process 2 removes volume Y;
 * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
 *
66 67
 * In this situation, this function will return %-ENODEV because it will find
 * out that the volume was removed from the @ubi->volumes array.
A
Artem B. Bityutskiy 已提交
68 69 70 71
 */
static ssize_t vol_attribute_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
72
	int ret;
A
Artem B. Bityutskiy 已提交
73
	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
74 75 76 77 78
	struct ubi_device *ubi;

	ubi = ubi_get_device(vol->ubi->ubi_num);
	if (!ubi)
		return -ENODEV;
A
Artem B. Bityutskiy 已提交
79

80 81 82
	spin_lock(&ubi->volumes_lock);
	if (!ubi->volumes[vol->vol_id]) {
		spin_unlock(&ubi->volumes_lock);
83
		ubi_put_device(ubi);
84
		return -ENODEV;
A
Artem B. Bityutskiy 已提交
85
	}
86 87 88
	/* Take a reference to prevent volume removal */
	vol->ref_count += 1;
	spin_unlock(&ubi->volumes_lock);
A
Artem Bityutskiy 已提交
89

A
Artem Bityutskiy 已提交
90
	if (attr == &attr_vol_reserved_ebs)
A
Artem B. Bityutskiy 已提交
91
		ret = sprintf(buf, "%d\n", vol->reserved_pebs);
A
Artem Bityutskiy 已提交
92
	else if (attr == &attr_vol_type) {
A
Artem B. Bityutskiy 已提交
93
		const char *tp;
A
Artem Bityutskiy 已提交
94 95 96 97 98

		if (vol->vol_type == UBI_DYNAMIC_VOLUME)
			tp = "dynamic";
		else
			tp = "static";
A
Artem B. Bityutskiy 已提交
99
		ret = sprintf(buf, "%s\n", tp);
A
Artem Bityutskiy 已提交
100
	} else if (attr == &attr_vol_name)
A
Artem B. Bityutskiy 已提交
101
		ret = sprintf(buf, "%s\n", vol->name);
A
Artem Bityutskiy 已提交
102
	else if (attr == &attr_vol_corrupted)
A
Artem B. Bityutskiy 已提交
103
		ret = sprintf(buf, "%d\n", vol->corrupted);
A
Artem Bityutskiy 已提交
104
	else if (attr == &attr_vol_alignment)
A
Artem B. Bityutskiy 已提交
105
		ret = sprintf(buf, "%d\n", vol->alignment);
A
Artem Bityutskiy 已提交
106
	else if (attr == &attr_vol_usable_eb_size)
A
Artem B. Bityutskiy 已提交
107
		ret = sprintf(buf, "%d\n", vol->usable_leb_size);
A
Artem Bityutskiy 已提交
108
	else if (attr == &attr_vol_data_bytes)
A
Artem B. Bityutskiy 已提交
109
		ret = sprintf(buf, "%lld\n", vol->used_bytes);
A
Artem Bityutskiy 已提交
110
	else if (attr == &attr_vol_upd_marker)
A
Artem B. Bityutskiy 已提交
111 112
		ret = sprintf(buf, "%d\n", vol->upd_marker);
	else
113 114 115
		/* This must be a bug */
		ret = -EINVAL;

116
	/* We've done the operation, drop volume and UBI device references */
117 118 119 120
	spin_lock(&ubi->volumes_lock);
	vol->ref_count -= 1;
	ubi_assert(vol->ref_count >= 0);
	spin_unlock(&ubi->volumes_lock);
121
	ubi_put_device(ubi);
A
Artem B. Bityutskiy 已提交
122 123 124 125 126 127 128
	return ret;
}

/* Release method for volume devices */
static void vol_release(struct device *dev)
{
	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
A
Artem Bityutskiy 已提交
129

A
Artem Bityutskiy 已提交
130
	kfree(vol->eba_tbl);
A
Artem B. Bityutskiy 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	kfree(vol);
}

/**
 * volume_sysfs_init - initialize sysfs for new volume.
 * @ubi: UBI device description object
 * @vol: volume description object
 *
 * This function returns zero in case of success and a negative error code in
 * case of failure.
 *
 * Note, this function does not free allocated resources in case of failure -
 * the caller does it. This is because this would cause release() here and the
 * caller would oops.
 */
static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
{
	int err;

A
Artem Bityutskiy 已提交
150
	err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
A
Artem B. Bityutskiy 已提交
151 152
	if (err)
		return err;
A
Artem Bityutskiy 已提交
153
	err = device_create_file(&vol->dev, &attr_vol_type);
A
Artem B. Bityutskiy 已提交
154 155
	if (err)
		return err;
A
Artem Bityutskiy 已提交
156
	err = device_create_file(&vol->dev, &attr_vol_name);
A
Artem B. Bityutskiy 已提交
157 158
	if (err)
		return err;
A
Artem Bityutskiy 已提交
159
	err = device_create_file(&vol->dev, &attr_vol_corrupted);
A
Artem B. Bityutskiy 已提交
160 161
	if (err)
		return err;
A
Artem Bityutskiy 已提交
162
	err = device_create_file(&vol->dev, &attr_vol_alignment);
A
Artem B. Bityutskiy 已提交
163 164
	if (err)
		return err;
A
Artem Bityutskiy 已提交
165
	err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
A
Artem B. Bityutskiy 已提交
166 167
	if (err)
		return err;
A
Artem Bityutskiy 已提交
168
	err = device_create_file(&vol->dev, &attr_vol_data_bytes);
A
Artem B. Bityutskiy 已提交
169 170
	if (err)
		return err;
A
Artem Bityutskiy 已提交
171
	err = device_create_file(&vol->dev, &attr_vol_upd_marker);
A
Artem Bityutskiy 已提交
172
	return err;
A
Artem B. Bityutskiy 已提交
173 174 175 176 177 178 179 180
}

/**
 * volume_sysfs_close - close sysfs for a volume.
 * @vol: volume description object
 */
static void volume_sysfs_close(struct ubi_volume *vol)
{
A
Artem Bityutskiy 已提交
181 182 183 184 185 186 187 188
	device_remove_file(&vol->dev, &attr_vol_upd_marker);
	device_remove_file(&vol->dev, &attr_vol_data_bytes);
	device_remove_file(&vol->dev, &attr_vol_usable_eb_size);
	device_remove_file(&vol->dev, &attr_vol_alignment);
	device_remove_file(&vol->dev, &attr_vol_corrupted);
	device_remove_file(&vol->dev, &attr_vol_name);
	device_remove_file(&vol->dev, &attr_vol_type);
	device_remove_file(&vol->dev, &attr_vol_reserved_ebs);
A
Artem B. Bityutskiy 已提交
189 190 191 192 193 194 195 196 197
	device_unregister(&vol->dev);
}

/**
 * ubi_create_volume - create volume.
 * @ubi: UBI device description object
 * @req: volume creation request
 *
 * This function creates volume described by @req. If @req->vol_id id
198
 * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
A
Artem B. Bityutskiy 已提交
199
 * and saves it in @req->vol_id. Returns zero in case of success and a negative
200
 * error code in case of failure. Note, the caller has to have the
201
 * @ubi->device_mutex locked.
A
Artem B. Bityutskiy 已提交
202 203 204
 */
int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
{
A
Artem Bityutskiy 已提交
205
	int i, err, vol_id = req->vol_id, do_free = 1;
A
Artem B. Bityutskiy 已提交
206 207
	struct ubi_volume *vol;
	struct ubi_vtbl_record vtbl_rec;
A
Artem Bityutskiy 已提交
208
	dev_t dev;
A
Artem B. Bityutskiy 已提交
209 210 211 212 213 214 215 216 217 218 219

	if (ubi->ro_mode)
		return -EROFS;

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

	spin_lock(&ubi->volumes_lock);
	if (vol_id == UBI_VOL_NUM_AUTO) {
		/* Find unused volume ID */
220
		dbg_gen("search for vacant volume ID");
A
Artem B. Bityutskiy 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234
		for (i = 0; i < ubi->vtbl_slots; i++)
			if (!ubi->volumes[i]) {
				vol_id = i;
				break;
			}

		if (vol_id == UBI_VOL_NUM_AUTO) {
			dbg_err("out of volume IDs");
			err = -ENFILE;
			goto out_unlock;
		}
		req->vol_id = vol_id;
	}

235 236
	dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
		ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
A
Artem B. Bityutskiy 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249
		(int)req->vol_type, req->name);

	/* Ensure that this volume does not exist */
	err = -EEXIST;
	if (ubi->volumes[vol_id]) {
		dbg_err("volume %d already exists", vol_id);
		goto out_unlock;
	}

	/* Ensure that the name is unique */
	for (i = 0; i < ubi->vtbl_slots; i++)
		if (ubi->volumes[i] &&
		    ubi->volumes[i]->name_len == req->name_len &&
A
Artem Bityutskiy 已提交
250
		    !strcmp(ubi->volumes[i]->name, req->name)) {
A
Artem B. Bityutskiy 已提交
251 252 253 254
			dbg_err("volume \"%s\" exists (ID %d)", req->name, i);
			goto out_unlock;
		}

255
	/* Calculate how many eraseblocks are requested */
A
Artem B. Bityutskiy 已提交
256
	vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
A
Artem Bityutskiy 已提交
257 258
	vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
				      vol->usable_leb_size);
A
Artem B. Bityutskiy 已提交
259 260 261 262 263 264 265 266 267

	/* Reserve physical eraseblocks */
	if (vol->reserved_pebs > ubi->avail_pebs) {
		dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
		err = -ENOSPC;
		goto out_unlock;
	}
	ubi->avail_pebs -= vol->reserved_pebs;
	ubi->rsvd_pebs += vol->reserved_pebs;
268
	spin_unlock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
269 270 271 272 273 274

	vol->vol_id    = vol_id;
	vol->alignment = req->alignment;
	vol->data_pad  = ubi->leb_size % vol->alignment;
	vol->vol_type  = req->vol_type;
	vol->name_len  = req->name_len;
275
	memcpy(vol->name, req->name, vol->name_len);
A
Artem B. Bityutskiy 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	vol->ubi = ubi;

	/*
	 * Finish all pending erases because there may be some LEBs belonging
	 * to the same volume ID.
	 */
	err = ubi_wl_flush(ubi);
	if (err)
		goto out_acc;

	vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
	if (!vol->eba_tbl) {
		err = -ENOMEM;
		goto out_acc;
	}

	for (i = 0; i < vol->reserved_pebs; i++)
		vol->eba_tbl[i] = UBI_LEB_UNMAPPED;

	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
		vol->used_ebs = vol->reserved_pebs;
		vol->last_eb_bytes = vol->usable_leb_size;
V
Vinit Agnihotri 已提交
298 299
		vol->used_bytes =
			(long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
300
	} else {
A
Artem Bityutskiy 已提交
301 302 303 304
		vol->used_ebs = div_u64_rem(vol->used_bytes,
					    vol->usable_leb_size,
					    &vol->last_eb_bytes);
		if (vol->last_eb_bytes != 0)
A
Artem B. Bityutskiy 已提交
305 306 307 308 309 310 311 312
			vol->used_ebs += 1;
		else
			vol->last_eb_bytes = vol->usable_leb_size;
	}

	/* Register character device for the volume */
	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
	vol->cdev.owner = THIS_MODULE;
A
Artem Bityutskiy 已提交
313 314
	dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
	err = cdev_add(&vol->cdev, dev, 1);
A
Artem B. Bityutskiy 已提交
315
	if (err) {
A
Artem Bityutskiy 已提交
316
		ubi_err("cannot add character device");
A
Artem B. Bityutskiy 已提交
317 318 319 320 321 322 323 324 325
		goto out_mapping;
	}

	err = ubi_create_gluebi(ubi, vol);
	if (err)
		goto out_cdev;

	vol->dev.release = vol_release;
	vol->dev.parent = &ubi->dev;
A
Artem Bityutskiy 已提交
326
	vol->dev.devt = dev;
A
Artem B. Bityutskiy 已提交
327
	vol->dev.class = ubi_class;
A
Artem Bityutskiy 已提交
328

329
	dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
A
Artem B. Bityutskiy 已提交
330
	err = device_register(&vol->dev);
A
Artem Bityutskiy 已提交
331 332
	if (err) {
		ubi_err("cannot register device");
A
Artem B. Bityutskiy 已提交
333
		goto out_gluebi;
A
Artem Bityutskiy 已提交
334
	}
A
Artem B. Bityutskiy 已提交
335 336 337 338 339 340 341

	err = volume_sysfs_init(ubi, vol);
	if (err)
		goto out_sysfs;

	/* Fill volume table record */
	memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
342 343 344 345
	vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
	vtbl_rec.alignment     = cpu_to_be32(vol->alignment);
	vtbl_rec.data_pad      = cpu_to_be32(vol->data_pad);
	vtbl_rec.name_len      = cpu_to_be16(vol->name_len);
A
Artem B. Bityutskiy 已提交
346 347 348 349
	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
		vtbl_rec.vol_type = UBI_VID_DYNAMIC;
	else
		vtbl_rec.vol_type = UBI_VID_STATIC;
350
	memcpy(vtbl_rec.name, vol->name, vol->name_len);
A
Artem B. Bityutskiy 已提交
351 352 353 354 355 356

	err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
	if (err)
		goto out_sysfs;

	spin_lock(&ubi->volumes_lock);
357
	ubi->volumes[vol_id] = vol;
358
	ubi->vol_count += 1;
A
Artem B. Bityutskiy 已提交
359 360
	spin_unlock(&ubi->volumes_lock);

361 362
	if (paranoid_check_volumes(ubi))
		dbg_err("check failed while creating volume %d", vol_id);
363
	return err;
A
Artem B. Bityutskiy 已提交
364

A
Artem Bityutskiy 已提交
365 366
out_sysfs:
	/*
A
Artem Bityutskiy 已提交
367
	 * We have registered our device, we should not free the volume
A
Artem Bityutskiy 已提交
368 369 370 371 372 373
	 * description object in this function in case of an error - it is
	 * freed by the release function.
	 *
	 * Get device reference to prevent the release function from being
	 * called just after sysfs has been closed.
	 */
A
Artem Bityutskiy 已提交
374
	do_free = 0;
A
Artem Bityutskiy 已提交
375 376
	get_device(&vol->dev);
	volume_sysfs_close(vol);
A
Artem B. Bityutskiy 已提交
377
out_gluebi:
S
S.Çağlar Onur 已提交
378 379 380
	if (ubi_destroy_gluebi(vol))
		dbg_err("cannot destroy gluebi for volume %d:%d",
			ubi->ubi_num, vol_id);
A
Artem B. Bityutskiy 已提交
381 382 383
out_cdev:
	cdev_del(&vol->cdev);
out_mapping:
A
Artem Bityutskiy 已提交
384 385
	if (do_free)
		kfree(vol->eba_tbl);
A
Artem B. Bityutskiy 已提交
386 387 388 389 390 391
out_acc:
	spin_lock(&ubi->volumes_lock);
	ubi->rsvd_pebs -= vol->reserved_pebs;
	ubi->avail_pebs += vol->reserved_pebs;
out_unlock:
	spin_unlock(&ubi->volumes_lock);
A
Artem Bityutskiy 已提交
392
	if (do_free)
A
Artem Bityutskiy 已提交
393
		kfree(vol);
A
Artem Bityutskiy 已提交
394 395
	else
		put_device(&vol->dev);
A
Artem Bityutskiy 已提交
396
	ubi_err("cannot create volume %d, error %d", vol_id, err);
A
Artem B. Bityutskiy 已提交
397 398 399 400 401 402
	return err;
}

/**
 * ubi_remove_volume - remove volume.
 * @desc: volume descriptor
403
 * @no_vtbl: do not change volume table if not zero
A
Artem B. Bityutskiy 已提交
404 405 406
 *
 * This function removes volume described by @desc. The volume has to be opened
 * in "exclusive" mode. Returns zero in case of success and a negative error
407
 * code in case of failure. The caller has to have the @ubi->device_mutex
408
 * locked.
A
Artem B. Bityutskiy 已提交
409
 */
410
int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
A
Artem B. Bityutskiy 已提交
411 412 413 414 415
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;

416
	dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
A
Artem B. Bityutskiy 已提交
417 418 419 420 421 422
	ubi_assert(desc->mode == UBI_EXCLUSIVE);
	ubi_assert(vol == ubi->volumes[vol_id]);

	if (ubi->ro_mode)
		return -EROFS;

423 424 425 426 427 428 429 430 431 432 433 434
	spin_lock(&ubi->volumes_lock);
	if (vol->ref_count > 1) {
		/*
		 * The volume is busy, probably someone is reading one of its
		 * sysfs files.
		 */
		err = -EBUSY;
		goto out_unlock;
	}
	ubi->volumes[vol_id] = NULL;
	spin_unlock(&ubi->volumes_lock);

A
Artem B. Bityutskiy 已提交
435 436
	err = ubi_destroy_gluebi(vol);
	if (err)
437
		goto out_err;
A
Artem B. Bityutskiy 已提交
438

439 440 441 442 443
	if (!no_vtbl) {
		err = ubi_change_vtbl_record(ubi, vol_id, NULL);
		if (err)
			goto out_err;
	}
A
Artem B. Bityutskiy 已提交
444 445

	for (i = 0; i < vol->reserved_pebs; i++) {
446
		err = ubi_eba_unmap_leb(ubi, vol, i);
A
Artem B. Bityutskiy 已提交
447
		if (err)
448
			goto out_err;
A
Artem B. Bityutskiy 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	}

	cdev_del(&vol->cdev);
	volume_sysfs_close(vol);

	spin_lock(&ubi->volumes_lock);
	ubi->rsvd_pebs -= reserved_pebs;
	ubi->avail_pebs += reserved_pebs;
	i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
	if (i > 0) {
		i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
		ubi->avail_pebs -= i;
		ubi->rsvd_pebs += i;
		ubi->beb_rsvd_pebs += i;
		if (i > 0)
			ubi_msg("reserve more %d PEBs", i);
	}
	ubi->vol_count -= 1;
	spin_unlock(&ubi->volumes_lock);

469 470 471
	if (!no_vtbl && paranoid_check_volumes(ubi))
		dbg_err("check failed while removing volume %d", vol_id);

472
	return err;
473 474 475 476 477 478 479

out_err:
	ubi_err("cannot remove volume %d, error %d", vol_id, err);
	spin_lock(&ubi->volumes_lock);
	ubi->volumes[vol_id] = vol;
out_unlock:
	spin_unlock(&ubi->volumes_lock);
A
Artem Bityutskiy 已提交
480
	return err;
A
Artem B. Bityutskiy 已提交
481 482 483 484 485 486 487
}

/**
 * ubi_resize_volume - re-size volume.
 * @desc: volume descriptor
 * @reserved_pebs: new size in physical eraseblocks
 *
488 489
 * This function re-sizes the volume and returns zero in case of success, and a
 * negative error code in case of failure. The caller has to have the
490
 * @ubi->device_mutex locked.
A
Artem B. Bityutskiy 已提交
491 492 493 494 495 496 497 498 499 500 501 502
 */
int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
{
	int i, err, pebs, *new_mapping;
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	struct ubi_vtbl_record vtbl_rec;
	int vol_id = vol->vol_id;

	if (ubi->ro_mode)
		return -EROFS;

503 504
	dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
		ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
A
Artem B. Bityutskiy 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

	if (vol->vol_type == UBI_STATIC_VOLUME &&
	    reserved_pebs < vol->used_ebs) {
		dbg_err("too small size %d, %d LEBs contain data",
			reserved_pebs, vol->used_ebs);
		return -EINVAL;
	}

	/* If the size is the same, we have nothing to do */
	if (reserved_pebs == vol->reserved_pebs)
		return 0;

	new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
	if (!new_mapping)
		return -ENOMEM;

	for (i = 0; i < reserved_pebs; i++)
		new_mapping[i] = UBI_LEB_UNMAPPED;

524 525 526 527 528 529 530 531 532
	spin_lock(&ubi->volumes_lock);
	if (vol->ref_count > 1) {
		spin_unlock(&ubi->volumes_lock);
		err = -EBUSY;
		goto out_free;
	}
	spin_unlock(&ubi->volumes_lock);

	/* Reserve physical eraseblocks */
A
Artem B. Bityutskiy 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	pebs = reserved_pebs - vol->reserved_pebs;
	if (pebs > 0) {
		spin_lock(&ubi->volumes_lock);
		if (pebs > ubi->avail_pebs) {
			dbg_err("not enough PEBs: requested %d, available %d",
				pebs, ubi->avail_pebs);
			spin_unlock(&ubi->volumes_lock);
			err = -ENOSPC;
			goto out_free;
		}
		ubi->avail_pebs -= pebs;
		ubi->rsvd_pebs += pebs;
		for (i = 0; i < vol->reserved_pebs; i++)
			new_mapping[i] = vol->eba_tbl[i];
		kfree(vol->eba_tbl);
		vol->eba_tbl = new_mapping;
		spin_unlock(&ubi->volumes_lock);
	}

	/* Change volume table record */
	memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
554
	vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
A
Artem B. Bityutskiy 已提交
555 556 557 558 559 560
	err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
	if (err)
		goto out_acc;

	if (pebs < 0) {
		for (i = 0; i < -pebs; i++) {
561
			err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
A
Artem B. Bityutskiy 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
			if (err)
				goto out_acc;
		}
		spin_lock(&ubi->volumes_lock);
		ubi->rsvd_pebs += pebs;
		ubi->avail_pebs -= pebs;
		pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
		if (pebs > 0) {
			pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
			ubi->avail_pebs -= pebs;
			ubi->rsvd_pebs += pebs;
			ubi->beb_rsvd_pebs += pebs;
			if (pebs > 0)
				ubi_msg("reserve more %d PEBs", pebs);
		}
		for (i = 0; i < reserved_pebs; i++)
			new_mapping[i] = vol->eba_tbl[i];
		kfree(vol->eba_tbl);
		vol->eba_tbl = new_mapping;
		spin_unlock(&ubi->volumes_lock);
	}

	vol->reserved_pebs = reserved_pebs;
	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
		vol->used_ebs = reserved_pebs;
		vol->last_eb_bytes = vol->usable_leb_size;
V
Vinit Agnihotri 已提交
588 589
		vol->used_bytes =
			(long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
590 591
	}

592 593
	if (paranoid_check_volumes(ubi))
		dbg_err("check failed while re-sizing volume %d", vol_id);
594
	return err;
A
Artem B. Bityutskiy 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607

out_acc:
	if (pebs > 0) {
		spin_lock(&ubi->volumes_lock);
		ubi->rsvd_pebs -= pebs;
		ubi->avail_pebs += pebs;
		spin_unlock(&ubi->volumes_lock);
	}
out_free:
	kfree(new_mapping);
	return err;
}

608 609 610
/**
 * ubi_rename_volumes - re-name UBI volumes.
 * @ubi: UBI device description object
611
 * @rename_list: list of &struct ubi_rename_entry objects
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
 *
 * This function re-names or removes volumes specified in the re-name list.
 * Returns zero in case of success and a negative error code in case of
 * failure.
 */
int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
{
	int err;
	struct ubi_rename_entry *re;

	err = ubi_vtbl_rename_volumes(ubi, rename_list);
	if (err)
		return err;

	list_for_each_entry(re, rename_list, list) {
		if (re->remove) {
			err = ubi_remove_volume(re->desc, 1);
			if (err)
				break;
		} else {
			struct ubi_volume *vol = re->desc->vol;

			spin_lock(&ubi->volumes_lock);
			vol->name_len = re->new_name_len;
			memcpy(vol->name, re->new_name, re->new_name_len + 1);
			spin_unlock(&ubi->volumes_lock);
		}
	}

641 642
	if (!err && paranoid_check_volumes(ubi))
		;
643 644 645
	return err;
}

A
Artem B. Bityutskiy 已提交
646 647 648
/**
 * ubi_add_volume - add volume.
 * @ubi: UBI device description object
649
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
650
 *
651 652
 * This function adds an existing volume and initializes all its data
 * structures. Returns zero in case of success and a negative error code in
A
Artem B. Bityutskiy 已提交
653 654
 * case of failure.
 */
655
int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
A
Artem B. Bityutskiy 已提交
656
{
657
	int err, vol_id = vol->vol_id;
A
Artem Bityutskiy 已提交
658
	dev_t dev;
A
Artem B. Bityutskiy 已提交
659

660
	dbg_gen("add volume %d", vol_id);
A
Artem B. Bityutskiy 已提交
661 662 663 664

	/* Register character device for the volume */
	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
	vol->cdev.owner = THIS_MODULE;
A
Artem Bityutskiy 已提交
665 666
	dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
	err = cdev_add(&vol->cdev, dev, 1);
A
Artem B. Bityutskiy 已提交
667
	if (err) {
A
Artem Bityutskiy 已提交
668 669
		ubi_err("cannot add character device for volume %d, error %d",
			vol_id, err);
A
Artem B. Bityutskiy 已提交
670 671 672 673 674 675 676 677 678
		return err;
	}

	err = ubi_create_gluebi(ubi, vol);
	if (err)
		goto out_cdev;

	vol->dev.release = vol_release;
	vol->dev.parent = &ubi->dev;
A
Artem Bityutskiy 已提交
679
	vol->dev.devt = dev;
A
Artem B. Bityutskiy 已提交
680
	vol->dev.class = ubi_class;
681
	dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
A
Artem B. Bityutskiy 已提交
682 683 684 685 686 687 688 689 690 691 692 693
	err = device_register(&vol->dev);
	if (err)
		goto out_gluebi;

	err = volume_sysfs_init(ubi, vol);
	if (err) {
		cdev_del(&vol->cdev);
		err = ubi_destroy_gluebi(vol);
		volume_sysfs_close(vol);
		return err;
	}

694 695
	if (paranoid_check_volumes(ubi))
		dbg_err("check failed while adding volume %d", vol_id);
696
	return err;
A
Artem B. Bityutskiy 已提交
697 698 699 700 701 702 703 704 705 706 707

out_gluebi:
	err = ubi_destroy_gluebi(vol);
out_cdev:
	cdev_del(&vol->cdev);
	return err;
}

/**
 * ubi_free_volume - free volume.
 * @ubi: UBI device description object
708
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
709
 *
710
 * This function frees all resources for volume @vol but does not remove it.
A
Artem B. Bityutskiy 已提交
711 712
 * Used only when the UBI device is detached.
 */
713
void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
A
Artem B. Bityutskiy 已提交
714 715 716
{
	int err;

717
	dbg_gen("free volume %d", vol->vol_id);
A
Artem B. Bityutskiy 已提交
718

719
	ubi->volumes[vol->vol_id] = NULL;
720
	err = ubi_destroy_gluebi(vol);
A
Artem B. Bityutskiy 已提交
721 722 723 724 725 726 727 728 729 730
	cdev_del(&vol->cdev);
	volume_sysfs_close(vol);
}

#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID

/**
 * paranoid_check_volume - check volume information.
 * @ubi: UBI device description object
 * @vol_id: volume ID
731 732
 *
 * Returns zero if volume is all right and a a negative error code if not.
A
Artem B. Bityutskiy 已提交
733
 */
734
static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
A
Artem B. Bityutskiy 已提交
735 736 737
{
	int idx = vol_id2idx(ubi, vol_id);
	int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
A
Artem Bityutskiy 已提交
738
	const struct ubi_volume *vol;
A
Artem B. Bityutskiy 已提交
739 740 741
	long long n;
	const char *name;

A
Artem Bityutskiy 已提交
742
	spin_lock(&ubi->volumes_lock);
743
	reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
A
Artem Bityutskiy 已提交
744
	vol = ubi->volumes[idx];
A
Artem B. Bityutskiy 已提交
745 746 747 748 749 750

	if (!vol) {
		if (reserved_pebs) {
			ubi_err("no volume info, but volume exists");
			goto fail;
		}
A
Artem Bityutskiy 已提交
751
		spin_unlock(&ubi->volumes_lock);
752
		return 0;
A
Artem B. Bityutskiy 已提交
753 754 755 756 757 758 759 760 761 762 763 764
	}

	if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
	    vol->name_len < 0) {
		ubi_err("negative values");
		goto fail;
	}
	if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
		ubi_err("bad alignment");
		goto fail;
	}

765
	n = vol->alignment & (ubi->min_io_size - 1);
A
Artem B. Bityutskiy 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
	if (vol->alignment != 1 && n) {
		ubi_err("alignment is not multiple of min I/O unit");
		goto fail;
	}

	n = ubi->leb_size % vol->alignment;
	if (vol->data_pad != n) {
		ubi_err("bad data_pad, has to be %lld", n);
		goto fail;
	}

	if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
	    vol->vol_type != UBI_STATIC_VOLUME) {
		ubi_err("bad vol_type");
		goto fail;
	}

	if (vol->upd_marker && vol->corrupted) {
		dbg_err("update marker and corrupted simultaneously");
		goto fail;
	}

	if (vol->reserved_pebs > ubi->good_peb_count) {
		ubi_err("too large reserved_pebs");
		goto fail;
	}

	n = ubi->leb_size - vol->data_pad;
	if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
		ubi_err("bad usable_leb_size, has to be %lld", n);
		goto fail;
	}

	if (vol->name_len > UBI_VOL_NAME_MAX) {
		ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
		goto fail;
	}

	if (!vol->name) {
		ubi_err("NULL volume name");
		goto fail;
	}

	n = strnlen(vol->name, vol->name_len + 1);
	if (n != vol->name_len) {
		ubi_err("bad name_len %lld", n);
		goto fail;
	}

V
Vinit Agnihotri 已提交
815
	n = (long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
816
	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
A
Artem Bityutskiy 已提交
817
		if (vol->corrupted) {
A
Artem B. Bityutskiy 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
			ubi_err("corrupted dynamic volume");
			goto fail;
		}
		if (vol->used_ebs != vol->reserved_pebs) {
			ubi_err("bad used_ebs");
			goto fail;
		}
		if (vol->last_eb_bytes != vol->usable_leb_size) {
			ubi_err("bad last_eb_bytes");
			goto fail;
		}
		if (vol->used_bytes != n) {
			ubi_err("bad used_bytes");
			goto fail;
		}
	} else {
		if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
			ubi_err("bad used_ebs");
			goto fail;
		}
		if (vol->last_eb_bytes < 0 ||
		    vol->last_eb_bytes > vol->usable_leb_size) {
			ubi_err("bad last_eb_bytes");
			goto fail;
		}
		if (vol->used_bytes < 0 || vol->used_bytes > n ||
		    vol->used_bytes < n - vol->usable_leb_size) {
			ubi_err("bad used_bytes");
			goto fail;
		}
	}

850 851 852
	alignment  = be32_to_cpu(ubi->vtbl[vol_id].alignment);
	data_pad   = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
	name_len   = be16_to_cpu(ubi->vtbl[vol_id].name_len);
A
Artem B. Bityutskiy 已提交
853 854 855 856 857 858 859 860 861
	upd_marker = ubi->vtbl[vol_id].upd_marker;
	name       = &ubi->vtbl[vol_id].name[0];
	if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
		vol_type = UBI_DYNAMIC_VOLUME;
	else
		vol_type = UBI_STATIC_VOLUME;

	if (alignment != vol->alignment || data_pad != vol->data_pad ||
	    upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
862
	    name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
A
Artem B. Bityutskiy 已提交
863 864 865 866
		ubi_err("volume info is different");
		goto fail;
	}

A
Artem Bityutskiy 已提交
867
	spin_unlock(&ubi->volumes_lock);
868
	return 0;
A
Artem B. Bityutskiy 已提交
869 870

fail:
A
Artem Bityutskiy 已提交
871
	ubi_err("paranoid check failed for volume %d", vol_id);
872
	if (vol)
873
		ubi_dbg_dump_vol_info(vol);
874
	ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
875
	dump_stack();
A
Artem Bityutskiy 已提交
876
	spin_unlock(&ubi->volumes_lock);
877
	return -EINVAL;
A
Artem B. Bityutskiy 已提交
878 879 880 881 882
}

/**
 * paranoid_check_volumes - check information about all volumes.
 * @ubi: UBI device description object
883 884
 *
 * Returns zero if volumes are all right and a a negative error code if not.
A
Artem B. Bityutskiy 已提交
885
 */
886
static int paranoid_check_volumes(struct ubi_device *ubi)
A
Artem B. Bityutskiy 已提交
887
{
888
	int i, err = 0;
A
Artem B. Bityutskiy 已提交
889

890 891 892 893 894 895 896
	for (i = 0; i < ubi->vtbl_slots; i++) {
		err = paranoid_check_volume(ubi, i);
		if (err)
			break;
	}

	return err;
A
Artem B. Bityutskiy 已提交
897 898
}
#endif