vmt.c 21.7 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>
28
#include <linux/slab.h>
29
#include <linux/export.h>
A
Artem B. Bityutskiy 已提交
30 31
#include "ubi.h"

32
static int self_check_volumes(struct ubi_device *ubi);
A
Artem B. Bityutskiy 已提交
33 34 35 36 37

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 已提交
38
static struct device_attribute attr_vol_reserved_ebs =
A
Artem B. Bityutskiy 已提交
39
	__ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
40
static struct device_attribute attr_vol_type =
A
Artem B. Bityutskiy 已提交
41
	__ATTR(type, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
42
static struct device_attribute attr_vol_name =
A
Artem B. Bityutskiy 已提交
43
	__ATTR(name, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
44
static struct device_attribute attr_vol_corrupted =
A
Artem B. Bityutskiy 已提交
45
	__ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
46
static struct device_attribute attr_vol_alignment =
A
Artem B. Bityutskiy 已提交
47
	__ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
48
static struct device_attribute attr_vol_usable_eb_size =
A
Artem B. Bityutskiy 已提交
49
	__ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
50
static struct device_attribute attr_vol_data_bytes =
A
Artem B. Bityutskiy 已提交
51
	__ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
A
Artem Bityutskiy 已提交
52
static struct device_attribute attr_vol_upd_marker =
A
Artem B. Bityutskiy 已提交
53 54 55 56 57 58 59 60 61 62 63
	__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;
 *
64 65
 * 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 已提交
66 67 68 69
 */
static ssize_t vol_attribute_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
70
	int ret;
A
Artem B. Bityutskiy 已提交
71
	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
72 73 74 75 76
	struct ubi_device *ubi;

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

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

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

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

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

123 124 125 126 127 128 129 130 131 132 133 134 135
static struct attribute *volume_dev_attrs[] = {
	&attr_vol_reserved_ebs.attr,
	&attr_vol_type.attr,
	&attr_vol_name.attr,
	&attr_vol_corrupted.attr,
	&attr_vol_alignment.attr,
	&attr_vol_usable_eb_size.attr,
	&attr_vol_data_bytes.attr,
	&attr_vol_upd_marker.attr,
	NULL
};
ATTRIBUTE_GROUPS(volume_dev);

A
Artem B. Bityutskiy 已提交
136 137 138 139
/* 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 已提交
140

B
Boris Brezillon 已提交
141
	ubi_eba_replace_table(vol, NULL);
142
	ubi_fastmap_destroy_checkmap(vol);
A
Artem B. Bityutskiy 已提交
143 144 145 146 147 148 149 150 151
	kfree(vol);
}

/**
 * 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
152
 * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
A
Artem B. Bityutskiy 已提交
153
 * and saves it in @req->vol_id. Returns zero in case of success and a negative
154
 * error code in case of failure. Note, the caller has to have the
155
 * @ubi->device_mutex locked.
A
Artem B. Bityutskiy 已提交
156 157 158
 */
int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
{
159
	int i, err, vol_id = req->vol_id;
A
Artem B. Bityutskiy 已提交
160 161
	struct ubi_volume *vol;
	struct ubi_vtbl_record vtbl_rec;
B
Boris Brezillon 已提交
162
	struct ubi_eba_table *eba_tbl = NULL;
A
Artem B. Bityutskiy 已提交
163 164 165 166 167 168 169 170

	if (ubi->ro_mode)
		return -EROFS;

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

171 172 173 174 175 176
	device_initialize(&vol->dev);
	vol->dev.release = vol_release;
	vol->dev.parent = &ubi->dev;
	vol->dev.class = &ubi_class;
	vol->dev.groups = volume_dev_groups;

A
Artem B. Bityutskiy 已提交
177 178 179
	spin_lock(&ubi->volumes_lock);
	if (vol_id == UBI_VOL_NUM_AUTO) {
		/* Find unused volume ID */
180
		dbg_gen("search for vacant volume ID");
A
Artem B. Bityutskiy 已提交
181 182 183 184 185 186 187
		for (i = 0; i < ubi->vtbl_slots; i++)
			if (!ubi->volumes[i]) {
				vol_id = i;
				break;
			}

		if (vol_id == UBI_VOL_NUM_AUTO) {
188
			ubi_err(ubi, "out of volume IDs");
A
Artem B. Bityutskiy 已提交
189 190 191 192 193 194
			err = -ENFILE;
			goto out_unlock;
		}
		req->vol_id = vol_id;
	}

195 196
	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 已提交
197 198 199 200 201
		(int)req->vol_type, req->name);

	/* Ensure that this volume does not exist */
	err = -EEXIST;
	if (ubi->volumes[vol_id]) {
202
		ubi_err(ubi, "volume %d already exists", vol_id);
A
Artem B. Bityutskiy 已提交
203 204 205 206 207 208 209
		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 已提交
210
		    !strcmp(ubi->volumes[i]->name, req->name)) {
211 212
			ubi_err(ubi, "volume \"%s\" exists (ID %d)",
				req->name, i);
A
Artem B. Bityutskiy 已提交
213 214 215
			goto out_unlock;
		}

216
	/* Calculate how many eraseblocks are requested */
A
Artem B. Bityutskiy 已提交
217
	vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
218 219
	vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
				     vol->usable_leb_size);
A
Artem B. Bityutskiy 已提交
220 221 222

	/* Reserve physical eraseblocks */
	if (vol->reserved_pebs > ubi->avail_pebs) {
223 224
		ubi_err(ubi, "not enough PEBs, only %d available",
			ubi->avail_pebs);
A
Artem Bityutskiy 已提交
225
		if (ubi->corr_peb_count)
226
			ubi_err(ubi, "%d PEBs are corrupted and not used",
A
Artem Bityutskiy 已提交
227
				ubi->corr_peb_count);
A
Artem B. Bityutskiy 已提交
228 229 230 231 232
		err = -ENOSPC;
		goto out_unlock;
	}
	ubi->avail_pebs -= vol->reserved_pebs;
	ubi->rsvd_pebs += vol->reserved_pebs;
233
	spin_unlock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
234 235 236 237 238 239

	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;
240
	memcpy(vol->name, req->name, vol->name_len);
A
Artem B. Bityutskiy 已提交
241 242 243 244 245 246
	vol->ubi = ubi;

	/*
	 * Finish all pending erases because there may be some LEBs belonging
	 * to the same volume ID.
	 */
247
	err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
A
Artem B. Bityutskiy 已提交
248 249 250
	if (err)
		goto out_acc;

B
Boris Brezillon 已提交
251 252 253
	eba_tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
	if (IS_ERR(eba_tbl)) {
		err = PTR_ERR(eba_tbl);
A
Artem B. Bityutskiy 已提交
254 255 256
		goto out_acc;
	}

B
Boris Brezillon 已提交
257
	ubi_eba_replace_table(vol, eba_tbl);
A
Artem B. Bityutskiy 已提交
258 259 260 261

	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
		vol->used_ebs = vol->reserved_pebs;
		vol->last_eb_bytes = vol->usable_leb_size;
V
Vinit Agnihotri 已提交
262 263
		vol->used_bytes =
			(long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
264
	} else {
A
Artem Bityutskiy 已提交
265 266 267 268
		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 已提交
269 270 271 272 273
			vol->used_ebs += 1;
		else
			vol->last_eb_bytes = vol->usable_leb_size;
	}

274 275 276 277 278 279
	/* Make volume "available" before it becomes accessible via sysfs */
	spin_lock(&ubi->volumes_lock);
	ubi->volumes[vol_id] = vol;
	ubi->vol_count += 1;
	spin_unlock(&ubi->volumes_lock);

A
Artem B. Bityutskiy 已提交
280 281 282
	/* Register character device for the volume */
	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
	vol->cdev.owner = THIS_MODULE;
A
Artem Bityutskiy 已提交
283

284
	vol->dev.devt = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
285
	dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
286
	err = cdev_device_add(&vol->cdev, &vol->dev);
A
Artem Bityutskiy 已提交
287
	if (err) {
288 289
		ubi_err(ubi, "cannot add device");
		goto out_mapping;
A
Artem Bityutskiy 已提交
290
	}
A
Artem B. Bityutskiy 已提交
291 292 293

	/* Fill volume table record */
	memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
294 295 296 297
	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 已提交
298 299 300 301
	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
		vtbl_rec.vol_type = UBI_VID_DYNAMIC;
	else
		vtbl_rec.vol_type = UBI_VID_STATIC;
302
	memcpy(vtbl_rec.name, vol->name, vol->name_len);
A
Artem B. Bityutskiy 已提交
303 304 305 306 307

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

D
Dmitry Pervushin 已提交
308
	ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
309
	self_check_volumes(ubi);
310
	return err;
A
Artem B. Bityutskiy 已提交
311

A
Artem Bityutskiy 已提交
312 313
out_sysfs:
	/*
A
Artem Bityutskiy 已提交
314
	 * We have registered our device, we should not free the volume
A
Artem Bityutskiy 已提交
315 316 317
	 * description object in this function in case of an error - it is
	 * freed by the release function.
	 */
318
	cdev_device_del(&vol->cdev, &vol->dev);
A
Artem B. Bityutskiy 已提交
319
out_mapping:
320 321 322 323
	spin_lock(&ubi->volumes_lock);
	ubi->volumes[vol_id] = NULL;
	ubi->vol_count -= 1;
	spin_unlock(&ubi->volumes_lock);
324
	ubi_eba_destroy_table(eba_tbl);
A
Artem B. Bityutskiy 已提交
325 326 327 328 329 330
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);
331
	put_device(&vol->dev);
332
	ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
A
Artem B. Bityutskiy 已提交
333 334 335 336 337 338
	return err;
}

/**
 * ubi_remove_volume - remove volume.
 * @desc: volume descriptor
339
 * @no_vtbl: do not change volume table if not zero
A
Artem B. Bityutskiy 已提交
340 341 342
 *
 * 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
343
 * code in case of failure. The caller has to have the @ubi->device_mutex
344
 * locked.
A
Artem B. Bityutskiy 已提交
345
 */
346
int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
A
Artem B. Bityutskiy 已提交
347 348 349 350 351
{
	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;

352
	dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
A
Artem B. Bityutskiy 已提交
353 354 355 356 357 358
	ubi_assert(desc->mode == UBI_EXCLUSIVE);
	ubi_assert(vol == ubi->volumes[vol_id]);

	if (ubi->ro_mode)
		return -EROFS;

359 360 361 362 363 364 365 366 367 368 369 370
	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);

371 372 373 374 375
	if (!no_vtbl) {
		err = ubi_change_vtbl_record(ubi, vol_id, NULL);
		if (err)
			goto out_err;
	}
A
Artem B. Bityutskiy 已提交
376 377

	for (i = 0; i < vol->reserved_pebs; i++) {
378
		err = ubi_eba_unmap_leb(ubi, vol, i);
A
Artem B. Bityutskiy 已提交
379
		if (err)
380
			goto out_err;
A
Artem B. Bityutskiy 已提交
381 382
	}

383 384
	cdev_device_del(&vol->cdev, &vol->dev);
	put_device(&vol->dev);
A
Artem B. Bityutskiy 已提交
385 386 387 388

	spin_lock(&ubi->volumes_lock);
	ubi->rsvd_pebs -= reserved_pebs;
	ubi->avail_pebs += reserved_pebs;
389
	ubi_update_reserved(ubi);
A
Artem B. Bityutskiy 已提交
390 391 392
	ubi->vol_count -= 1;
	spin_unlock(&ubi->volumes_lock);

D
Dmitry Pervushin 已提交
393
	ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
394 395
	if (!no_vtbl)
		self_check_volumes(ubi);
396

397
	return 0;
398 399

out_err:
400
	ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
401 402 403 404
	spin_lock(&ubi->volumes_lock);
	ubi->volumes[vol_id] = vol;
out_unlock:
	spin_unlock(&ubi->volumes_lock);
A
Artem Bityutskiy 已提交
405
	return err;
A
Artem B. Bityutskiy 已提交
406 407 408 409 410 411 412
}

/**
 * ubi_resize_volume - re-size volume.
 * @desc: volume descriptor
 * @reserved_pebs: new size in physical eraseblocks
 *
413 414
 * 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
415
 * @ubi->device_mutex locked.
A
Artem B. Bityutskiy 已提交
416 417 418
 */
int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
{
B
Boris Brezillon 已提交
419
	int i, err, pebs;
A
Artem B. Bityutskiy 已提交
420 421 422
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	struct ubi_vtbl_record vtbl_rec;
B
Boris Brezillon 已提交
423
	struct ubi_eba_table *new_eba_tbl = NULL;
A
Artem B. Bityutskiy 已提交
424 425 426 427 428
	int vol_id = vol->vol_id;

	if (ubi->ro_mode)
		return -EROFS;

429 430
	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 已提交
431 432 433

	if (vol->vol_type == UBI_STATIC_VOLUME &&
	    reserved_pebs < vol->used_ebs) {
434
		ubi_err(ubi, "too small size %d, %d LEBs contain data",
A
Artem B. Bityutskiy 已提交
435 436 437 438 439 440 441 442
			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;

B
Boris Brezillon 已提交
443 444 445
	new_eba_tbl = ubi_eba_create_table(vol, reserved_pebs);
	if (IS_ERR(new_eba_tbl))
		return PTR_ERR(new_eba_tbl);
A
Artem B. Bityutskiy 已提交
446

447 448 449 450 451 452 453 454 455
	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 已提交
456 457 458 459
	pebs = reserved_pebs - vol->reserved_pebs;
	if (pebs > 0) {
		spin_lock(&ubi->volumes_lock);
		if (pebs > ubi->avail_pebs) {
460
			ubi_err(ubi, "not enough PEBs: requested %d, available %d",
A
Artem B. Bityutskiy 已提交
461
				pebs, ubi->avail_pebs);
A
Artem Bityutskiy 已提交
462
			if (ubi->corr_peb_count)
463
				ubi_err(ubi, "%d PEBs are corrupted and not used",
A
Artem Bityutskiy 已提交
464
					ubi->corr_peb_count);
A
Artem B. Bityutskiy 已提交
465 466 467 468 469 470
			spin_unlock(&ubi->volumes_lock);
			err = -ENOSPC;
			goto out_free;
		}
		ubi->avail_pebs -= pebs;
		ubi->rsvd_pebs += pebs;
B
Boris Brezillon 已提交
471 472
		ubi_eba_copy_table(vol, new_eba_tbl, vol->reserved_pebs);
		ubi_eba_replace_table(vol, new_eba_tbl);
A
Artem B. Bityutskiy 已提交
473 474 475 476 477
		spin_unlock(&ubi->volumes_lock);
	}

	if (pebs < 0) {
		for (i = 0; i < -pebs; i++) {
478
			err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
A
Artem B. Bityutskiy 已提交
479 480 481 482 483 484
			if (err)
				goto out_acc;
		}
		spin_lock(&ubi->volumes_lock);
		ubi->rsvd_pebs += pebs;
		ubi->avail_pebs -= pebs;
485
		ubi_update_reserved(ubi);
B
Boris Brezillon 已提交
486 487
		ubi_eba_copy_table(vol, new_eba_tbl, reserved_pebs);
		ubi_eba_replace_table(vol, new_eba_tbl);
A
Artem B. Bityutskiy 已提交
488 489 490
		spin_unlock(&ubi->volumes_lock);
	}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	/*
	 * When we shrink a volume we have to flush all pending (erase) work.
	 * Otherwise it can happen that upon next attach UBI finds a LEB with
	 * lnum > highest_lnum and refuses to attach.
	 */
	if (pebs < 0) {
		err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
		if (err)
			goto out_acc;
	}

	/* Change volume table record */
	vtbl_rec = ubi->vtbl[vol_id];
	vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
	err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
	if (err)
		goto out_acc;

A
Artem B. Bityutskiy 已提交
509 510 511 512
	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 已提交
513 514
		vol->used_bytes =
			(long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
515 516
	}

D
Dmitry Pervushin 已提交
517
	ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
518
	self_check_volumes(ubi);
519
	return err;
A
Artem B. Bityutskiy 已提交
520 521 522 523 524 525 526 527 528

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:
B
Boris Brezillon 已提交
529
	kfree(new_eba_tbl);
A
Artem B. Bityutskiy 已提交
530 531 532
	return err;
}

533 534 535
/**
 * ubi_rename_volumes - re-name UBI volumes.
 * @ubi: UBI device description object
536
 * @rename_list: list of &struct ubi_rename_entry objects
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
 *
 * 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);
D
Dmitry Pervushin 已提交
563
			ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
564 565 566
		}
	}

567 568
	if (!err)
		self_check_volumes(ubi);
569 570 571
	return err;
}

A
Artem B. Bityutskiy 已提交
572 573 574
/**
 * ubi_add_volume - add volume.
 * @ubi: UBI device description object
575
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
576
 *
577 578
 * 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 已提交
579 580
 * case of failure.
 */
581
int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
A
Artem B. Bityutskiy 已提交
582
{
583
	int err, vol_id = vol->vol_id;
A
Artem Bityutskiy 已提交
584
	dev_t dev;
A
Artem B. Bityutskiy 已提交
585

586
	dbg_gen("add volume %d", vol_id);
A
Artem B. Bityutskiy 已提交
587 588 589 590

	/* Register character device for the volume */
	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
	vol->cdev.owner = THIS_MODULE;
A
Artem Bityutskiy 已提交
591 592
	dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
	err = cdev_add(&vol->cdev, dev, 1);
A
Artem B. Bityutskiy 已提交
593
	if (err) {
594
		ubi_err(ubi, "cannot add character device for volume %d, error %d",
A
Artem Bityutskiy 已提交
595
			vol_id, err);
A
Artem B. Bityutskiy 已提交
596 597 598 599 600
		return err;
	}

	vol->dev.release = vol_release;
	vol->dev.parent = &ubi->dev;
A
Artem Bityutskiy 已提交
601
	vol->dev.devt = dev;
602 603
	vol->dev.class = &ubi_class;
	vol->dev.groups = volume_dev_groups;
604
	dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
A
Artem B. Bityutskiy 已提交
605 606
	err = device_register(&vol->dev);
	if (err)
D
Dmitry Pervushin 已提交
607
		goto out_cdev;
A
Artem B. Bityutskiy 已提交
608

609
	self_check_volumes(ubi);
610
	return err;
A
Artem B. Bityutskiy 已提交
611 612 613 614 615 616 617 618 619

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

/**
 * ubi_free_volume - free volume.
 * @ubi: UBI device description object
620
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
621
 *
622
 * This function frees all resources for volume @vol but does not remove it.
A
Artem B. Bityutskiy 已提交
623 624
 * Used only when the UBI device is detached.
 */
625
void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
A
Artem B. Bityutskiy 已提交
626
{
627
	dbg_gen("free volume %d", vol->vol_id);
A
Artem B. Bityutskiy 已提交
628

629
	ubi->volumes[vol->vol_id] = NULL;
A
Artem B. Bityutskiy 已提交
630
	cdev_del(&vol->cdev);
631
	device_unregister(&vol->dev);
A
Artem B. Bityutskiy 已提交
632 633 634
}

/**
635
 * self_check_volume - check volume information.
A
Artem B. Bityutskiy 已提交
636 637
 * @ubi: UBI device description object
 * @vol_id: volume ID
638 639
 *
 * Returns zero if volume is all right and a a negative error code if not.
A
Artem B. Bityutskiy 已提交
640
 */
641
static int self_check_volume(struct ubi_device *ubi, int vol_id)
A
Artem B. Bityutskiy 已提交
642 643 644
{
	int idx = vol_id2idx(ubi, vol_id);
	int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
A
Artem Bityutskiy 已提交
645
	const struct ubi_volume *vol;
A
Artem B. Bityutskiy 已提交
646 647 648
	long long n;
	const char *name;

A
Artem Bityutskiy 已提交
649
	spin_lock(&ubi->volumes_lock);
650
	reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
A
Artem Bityutskiy 已提交
651
	vol = ubi->volumes[idx];
A
Artem B. Bityutskiy 已提交
652 653 654

	if (!vol) {
		if (reserved_pebs) {
655
			ubi_err(ubi, "no volume info, but volume exists");
A
Artem B. Bityutskiy 已提交
656 657
			goto fail;
		}
A
Artem Bityutskiy 已提交
658
		spin_unlock(&ubi->volumes_lock);
659
		return 0;
A
Artem B. Bityutskiy 已提交
660 661 662 663
	}

	if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
	    vol->name_len < 0) {
664
		ubi_err(ubi, "negative values");
A
Artem B. Bityutskiy 已提交
665 666 667
		goto fail;
	}
	if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
668
		ubi_err(ubi, "bad alignment");
A
Artem B. Bityutskiy 已提交
669 670 671
		goto fail;
	}

672
	n = vol->alignment & (ubi->min_io_size - 1);
A
Artem B. Bityutskiy 已提交
673
	if (vol->alignment != 1 && n) {
674
		ubi_err(ubi, "alignment is not multiple of min I/O unit");
A
Artem B. Bityutskiy 已提交
675 676 677 678 679
		goto fail;
	}

	n = ubi->leb_size % vol->alignment;
	if (vol->data_pad != n) {
680
		ubi_err(ubi, "bad data_pad, has to be %lld", n);
A
Artem B. Bityutskiy 已提交
681 682 683 684 685
		goto fail;
	}

	if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
	    vol->vol_type != UBI_STATIC_VOLUME) {
686
		ubi_err(ubi, "bad vol_type");
A
Artem B. Bityutskiy 已提交
687 688 689 690
		goto fail;
	}

	if (vol->upd_marker && vol->corrupted) {
691
		ubi_err(ubi, "update marker and corrupted simultaneously");
A
Artem B. Bityutskiy 已提交
692 693 694 695
		goto fail;
	}

	if (vol->reserved_pebs > ubi->good_peb_count) {
696
		ubi_err(ubi, "too large reserved_pebs");
A
Artem B. Bityutskiy 已提交
697 698 699 700 701
		goto fail;
	}

	n = ubi->leb_size - vol->data_pad;
	if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
702
		ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
A
Artem B. Bityutskiy 已提交
703 704 705 706
		goto fail;
	}

	if (vol->name_len > UBI_VOL_NAME_MAX) {
707 708
		ubi_err(ubi, "too long volume name, max is %d",
			UBI_VOL_NAME_MAX);
A
Artem B. Bityutskiy 已提交
709 710 711 712 713
		goto fail;
	}

	n = strnlen(vol->name, vol->name_len + 1);
	if (n != vol->name_len) {
714
		ubi_err(ubi, "bad name_len %lld", n);
A
Artem B. Bityutskiy 已提交
715 716 717
		goto fail;
	}

V
Vinit Agnihotri 已提交
718
	n = (long long)vol->used_ebs * vol->usable_leb_size;
A
Artem B. Bityutskiy 已提交
719
	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
A
Artem Bityutskiy 已提交
720
		if (vol->corrupted) {
721
			ubi_err(ubi, "corrupted dynamic volume");
A
Artem B. Bityutskiy 已提交
722 723 724
			goto fail;
		}
		if (vol->used_ebs != vol->reserved_pebs) {
725
			ubi_err(ubi, "bad used_ebs");
A
Artem B. Bityutskiy 已提交
726 727 728
			goto fail;
		}
		if (vol->last_eb_bytes != vol->usable_leb_size) {
729
			ubi_err(ubi, "bad last_eb_bytes");
A
Artem B. Bityutskiy 已提交
730 731 732
			goto fail;
		}
		if (vol->used_bytes != n) {
733
			ubi_err(ubi, "bad used_bytes");
A
Artem B. Bityutskiy 已提交
734 735 736 737
			goto fail;
		}
	} else {
		if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
738
			ubi_err(ubi, "bad used_ebs");
A
Artem B. Bityutskiy 已提交
739 740 741 742
			goto fail;
		}
		if (vol->last_eb_bytes < 0 ||
		    vol->last_eb_bytes > vol->usable_leb_size) {
743
			ubi_err(ubi, "bad last_eb_bytes");
A
Artem B. Bityutskiy 已提交
744 745 746 747
			goto fail;
		}
		if (vol->used_bytes < 0 || vol->used_bytes > n ||
		    vol->used_bytes < n - vol->usable_leb_size) {
748
			ubi_err(ubi, "bad used_bytes");
A
Artem B. Bityutskiy 已提交
749 750 751 752
			goto fail;
		}
	}

753 754 755
	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 已提交
756 757 758 759 760 761 762 763 764
	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 ||
765
	    name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
766
		ubi_err(ubi, "volume info is different");
A
Artem B. Bityutskiy 已提交
767 768 769
		goto fail;
	}

A
Artem Bityutskiy 已提交
770
	spin_unlock(&ubi->volumes_lock);
771
	return 0;
A
Artem B. Bityutskiy 已提交
772 773

fail:
774
	ubi_err(ubi, "self-check failed for volume %d", vol_id);
775
	if (vol)
776
		ubi_dump_vol_info(vol);
777
	ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
778
	dump_stack();
A
Artem Bityutskiy 已提交
779
	spin_unlock(&ubi->volumes_lock);
780
	return -EINVAL;
A
Artem B. Bityutskiy 已提交
781 782 783
}

/**
784
 * self_check_volumes - check information about all volumes.
A
Artem B. Bityutskiy 已提交
785
 * @ubi: UBI device description object
786 787
 *
 * Returns zero if volumes are all right and a a negative error code if not.
A
Artem B. Bityutskiy 已提交
788
 */
789
static int self_check_volumes(struct ubi_device *ubi)
A
Artem B. Bityutskiy 已提交
790
{
791
	int i, err = 0;
A
Artem B. Bityutskiy 已提交
792

793
	if (!ubi_dbg_chk_gen(ubi))
A
Artem Bityutskiy 已提交
794 795
		return 0;

796
	for (i = 0; i < ubi->vtbl_slots; i++) {
797
		err = self_check_volume(ubi, i);
798 799 800 801 802
		if (err)
			break;
	}

	return err;
A
Artem B. Bityutskiy 已提交
803
}