cdev.c 24.8 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
/*
 * 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 includes implementation of UBI character device operations.
 *
 * There are two kinds of character devices in UBI: UBI character devices and
 * UBI volume character devices. UBI character devices allow users to
 * manipulate whole volumes: create, remove, and re-size them. Volume character
 * devices provide volume I/O capabilities.
 *
 * Major and minor numbers are assigned dynamically to both UBI and volume
 * character devices.
A
Artem Bityutskiy 已提交
31 32 33 34 35
 *
 * Well, there is the third kind of character devices - the UBI control
 * character device, which allows to manipulate by UBI devices - create and
 * delete them. In other words, it is used for attaching and detaching MTD
 * devices.
A
Artem B. Bityutskiy 已提交
36 37 38 39
 */

#include <linux/module.h>
#include <linux/stat.h>
40
#include <linux/slab.h>
A
Artem B. Bityutskiy 已提交
41 42
#include <linux/ioctl.h>
#include <linux/capability.h>
43
#include <linux/uaccess.h>
A
Artem Bityutskiy 已提交
44
#include <linux/compat.h>
A
Artem Bityutskiy 已提交
45
#include <linux/math64.h>
A
Artem B. Bityutskiy 已提交
46 47 48 49 50 51 52 53 54 55 56
#include <mtd/ubi-user.h>
#include "ubi.h"

/**
 * get_exclusive - get exclusive access to an UBI volume.
 * @desc: volume descriptor
 *
 * This function changes UBI volume open mode to "exclusive". Returns previous
 * mode value (positive integer) in case of success and a negative error code
 * in case of failure.
 */
57
static int get_exclusive(struct ubi_volume_desc *desc)
A
Artem B. Bityutskiy 已提交
58 59 60 61 62
{
	int users, err;
	struct ubi_volume *vol = desc->vol;

	spin_lock(&vol->ubi->volumes_lock);
R
Richard Weinberger 已提交
63
	users = vol->readers + vol->writers + vol->exclusive + vol->metaonly;
A
Artem B. Bityutskiy 已提交
64 65
	ubi_assert(users > 0);
	if (users > 1) {
66
		ubi_err(vol->ubi, "%d users for volume %d", users, vol->vol_id);
A
Artem B. Bityutskiy 已提交
67 68
		err = -EBUSY;
	} else {
R
Richard Weinberger 已提交
69
		vol->readers = vol->writers = vol->metaonly = 0;
A
Artem B. Bityutskiy 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		vol->exclusive = 1;
		err = desc->mode;
		desc->mode = UBI_EXCLUSIVE;
	}
	spin_unlock(&vol->ubi->volumes_lock);

	return err;
}

/**
 * revoke_exclusive - revoke exclusive mode.
 * @desc: volume descriptor
 * @mode: new mode to switch to
 */
static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
{
	struct ubi_volume *vol = desc->vol;

	spin_lock(&vol->ubi->volumes_lock);
R
Richard Weinberger 已提交
89
	ubi_assert(vol->readers == 0 && vol->writers == 0 && vol->metaonly == 0);
A
Artem B. Bityutskiy 已提交
90 91 92 93 94 95
	ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
	vol->exclusive = 0;
	if (mode == UBI_READONLY)
		vol->readers = 1;
	else if (mode == UBI_READWRITE)
		vol->writers = 1;
R
Richard Weinberger 已提交
96 97
	else if (mode == UBI_METAONLY)
		vol->metaonly = 1;
A
Artem B. Bityutskiy 已提交
98 99 100 101 102 103 104 105 106 107
	else
		vol->exclusive = 1;
	spin_unlock(&vol->ubi->volumes_lock);

	desc->mode = mode;
}

static int vol_cdev_open(struct inode *inode, struct file *file)
{
	struct ubi_volume_desc *desc;
108 109 110
	int vol_id = iminor(inode) - 1, mode, ubi_num;

	ubi_num = ubi_major2num(imajor(inode));
A
Artem Bityutskiy 已提交
111
	if (ubi_num < 0)
112
		return ubi_num;
A
Artem B. Bityutskiy 已提交
113 114 115 116 117 118

	if (file->f_mode & FMODE_WRITE)
		mode = UBI_READWRITE;
	else
		mode = UBI_READONLY;

119
	dbg_gen("open device %d, volume %d, mode %d",
120
		ubi_num, vol_id, mode);
A
Artem B. Bityutskiy 已提交
121

122
	desc = ubi_open_volume(ubi_num, vol_id, mode);
A
Artem B. Bityutskiy 已提交
123 124 125 126 127 128 129 130 131 132 133 134
	if (IS_ERR(desc))
		return PTR_ERR(desc);

	file->private_data = desc;
	return 0;
}

static int vol_cdev_release(struct inode *inode, struct file *file)
{
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_volume *vol = desc->vol;

135 136
	dbg_gen("release device %d, volume %d, mode %d",
		vol->ubi->ubi_num, vol->vol_id, desc->mode);
A
Artem B. Bityutskiy 已提交
137 138

	if (vol->updating) {
139
		ubi_warn(vol->ubi, "update of volume %d not finished, volume is damaged",
A
Artem B. Bityutskiy 已提交
140
			 vol->vol_id);
141
		ubi_assert(!vol->changing_leb);
A
Artem B. Bityutskiy 已提交
142
		vol->updating = 0;
143
		vfree(vol->upd_buf);
144
	} else if (vol->changing_leb) {
A
Artem Bityutskiy 已提交
145 146 147
		dbg_gen("only %lld of %lld bytes received for atomic LEB change for volume %d:%d, cancel",
			vol->upd_received, vol->upd_bytes, vol->ubi->ubi_num,
			vol->vol_id);
148 149
		vol->changing_leb = 0;
		vfree(vol->upd_buf);
A
Artem B. Bityutskiy 已提交
150 151 152 153 154 155 156 157 158 159 160 161
	}

	ubi_close_volume(desc);
	return 0;
}

static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
{
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_volume *vol = desc->vol;

	if (vol->updating) {
162
		/* Update is in progress, seeking is prohibited */
163
		ubi_err(vol->ubi, "updating");
A
Artem B. Bityutskiy 已提交
164 165 166
		return -EBUSY;
	}

167
	return fixed_size_llseek(file, offset, origin, vol->used_bytes);
A
Artem B. Bityutskiy 已提交
168 169
}

A
Artem Bityutskiy 已提交
170 171
static int vol_cdev_fsync(struct file *file, loff_t start, loff_t end,
			  int datasync)
C
Corentin Chary 已提交
172 173 174
{
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_device *ubi = desc->vol->ubi;
A
Al Viro 已提交
175
	struct inode *inode = file_inode(file);
176 177 178 179 180
	int err;
	mutex_lock(&inode->i_mutex);
	err = ubi_sync(ubi->ubi_num);
	mutex_unlock(&inode->i_mutex);
	return err;
C
Corentin Chary 已提交
181 182 183
}


A
Artem B. Bityutskiy 已提交
184 185 186 187 188 189
static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
			     loff_t *offp)
{
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
A
Artem Bityutskiy 已提交
190
	int err, lnum, off, len,  tbuf_size;
A
Artem B. Bityutskiy 已提交
191 192 193
	size_t count_save = count;
	void *tbuf;

194
	dbg_gen("read %zd bytes from offset %lld of volume %d",
A
Artem Bityutskiy 已提交
195
		count, *offp, vol->vol_id);
A
Artem B. Bityutskiy 已提交
196 197

	if (vol->updating) {
198
		ubi_err(vol->ubi, "updating");
A
Artem B. Bityutskiy 已提交
199 200 201
		return -EBUSY;
	}
	if (vol->upd_marker) {
202
		ubi_err(vol->ubi, "damaged volume, update marker is set");
A
Artem B. Bityutskiy 已提交
203 204 205 206 207 208
		return -EBADF;
	}
	if (*offp == vol->used_bytes || count == 0)
		return 0;

	if (vol->corrupted)
209
		dbg_gen("read from corrupted volume %d", vol->vol_id);
A
Artem B. Bityutskiy 已提交
210 211 212 213 214 215 216

	if (*offp + count > vol->used_bytes)
		count_save = count = vol->used_bytes - *offp;

	tbuf_size = vol->usable_leb_size;
	if (count < tbuf_size)
		tbuf_size = ALIGN(count, ubi->min_io_size);
217
	tbuf = vmalloc(tbuf_size);
A
Artem B. Bityutskiy 已提交
218 219 220 221
	if (!tbuf)
		return -ENOMEM;

	len = count > tbuf_size ? tbuf_size : count;
A
Artem Bityutskiy 已提交
222
	lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
A
Artem B. Bityutskiy 已提交
223 224 225 226 227 228 229

	do {
		cond_resched();

		if (off + len >= vol->usable_leb_size)
			len = vol->usable_leb_size - off;

230
		err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
A
Artem B. Bityutskiy 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		if (err)
			break;

		off += len;
		if (off == vol->usable_leb_size) {
			lnum += 1;
			off -= vol->usable_leb_size;
		}

		count -= len;
		*offp += len;

		err = copy_to_user(buf, tbuf, len);
		if (err) {
			err = -EFAULT;
			break;
		}

		buf += len;
		len = count > tbuf_size ? tbuf_size : count;
	} while (count);

253
	vfree(tbuf);
A
Artem B. Bityutskiy 已提交
254 255 256 257 258
	return err ? err : count_save - count;
}

/*
 * This function allows to directly write to dynamic UBI volumes, without
S
Sidney Amani 已提交
259
 * issuing the volume update operation.
A
Artem B. Bityutskiy 已提交
260 261 262 263 264 265 266
 */
static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
				     size_t count, loff_t *offp)
{
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
A
Artem Bityutskiy 已提交
267
	int lnum, off, len, tbuf_size, err = 0;
A
Artem B. Bityutskiy 已提交
268 269 270
	size_t count_save = count;
	char *tbuf;

S
Sidney Amani 已提交
271 272 273
	if (!vol->direct_writes)
		return -EPERM;

274
	dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
A
Artem Bityutskiy 已提交
275
		count, *offp, vol->vol_id);
A
Artem B. Bityutskiy 已提交
276 277 278 279

	if (vol->vol_type == UBI_STATIC_VOLUME)
		return -EROFS;

A
Artem Bityutskiy 已提交
280
	lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
281
	if (off & (ubi->min_io_size - 1)) {
282
		ubi_err(ubi, "unaligned position");
A
Artem B. Bityutskiy 已提交
283 284 285 286 287 288 289
		return -EINVAL;
	}

	if (*offp + count > vol->used_bytes)
		count_save = count = vol->used_bytes - *offp;

	/* We can write only in fractions of the minimum I/O unit */
290
	if (count & (ubi->min_io_size - 1)) {
291
		ubi_err(ubi, "unaligned write length");
A
Artem B. Bityutskiy 已提交
292 293 294 295 296 297
		return -EINVAL;
	}

	tbuf_size = vol->usable_leb_size;
	if (count < tbuf_size)
		tbuf_size = ALIGN(count, ubi->min_io_size);
298
	tbuf = vmalloc(tbuf_size);
A
Artem B. Bityutskiy 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	if (!tbuf)
		return -ENOMEM;

	len = count > tbuf_size ? tbuf_size : count;

	while (count) {
		cond_resched();

		if (off + len >= vol->usable_leb_size)
			len = vol->usable_leb_size - off;

		err = copy_from_user(tbuf, buf, len);
		if (err) {
			err = -EFAULT;
			break;
		}

R
Richard Weinberger 已提交
316
		err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len);
A
Artem B. Bityutskiy 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		if (err)
			break;

		off += len;
		if (off == vol->usable_leb_size) {
			lnum += 1;
			off -= vol->usable_leb_size;
		}

		count -= len;
		*offp += len;
		buf += len;
		len = count > tbuf_size ? tbuf_size : count;
	}

332
	vfree(tbuf);
A
Artem B. Bityutskiy 已提交
333 334 335 336 337 338 339 340 341 342 343
	return err ? err : count_save - count;
}

static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
			      size_t count, loff_t *offp)
{
	int err = 0;
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;

344
	if (!vol->updating && !vol->changing_leb)
A
Artem B. Bityutskiy 已提交
345 346
		return vol_cdev_direct_write(file, buf, count, offp);

347 348 349 350 351
	if (vol->updating)
		err = ubi_more_update_data(ubi, vol, buf, count);
	else
		err = ubi_more_leb_change_data(ubi, vol, buf, count);

A
Artem B. Bityutskiy 已提交
352
	if (err < 0) {
353
		ubi_err(ubi, "cannot accept more %zd bytes of data, error %d",
A
Artem Bityutskiy 已提交
354
			count, err);
A
Artem B. Bityutskiy 已提交
355 356 357 358 359
		return err;
	}

	if (err) {
		/*
360 361
		 * The operation is finished, @err contains number of actually
		 * written bytes.
A
Artem B. Bityutskiy 已提交
362 363 364
		 */
		count = err;

365 366 367 368 369
		if (vol->changing_leb) {
			revoke_exclusive(desc, UBI_READWRITE);
			return count;
		}

A
Artem B. Bityutskiy 已提交
370 371 372 373 374
		err = ubi_check_volume(ubi, vol->vol_id);
		if (err < 0)
			return err;

		if (err) {
375
			ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
A
Artem B. Bityutskiy 已提交
376 377 378 379
				 vol->vol_id, ubi->ubi_num);
			vol->corrupted = 1;
		}
		vol->checked = 1;
D
Dmitry Pervushin 已提交
380
		ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
A
Artem B. Bityutskiy 已提交
381 382 383 384 385 386
		revoke_exclusive(desc, UBI_READWRITE);
	}

	return count;
}

A
Artem Bityutskiy 已提交
387 388
static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
			   unsigned long arg)
A
Artem B. Bityutskiy 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
{
	int err = 0;
	struct ubi_volume_desc *desc = file->private_data;
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	void __user *argp = (void __user *)arg;

	switch (cmd) {
	/* Volume update command */
	case UBI_IOCVOLUP:
	{
		int64_t bytes, rsvd_bytes;

		if (!capable(CAP_SYS_RESOURCE)) {
			err = -EPERM;
			break;
		}

		err = copy_from_user(&bytes, argp, sizeof(int64_t));
		if (err) {
			err = -EFAULT;
			break;
		}

		if (desc->mode == UBI_READONLY) {
			err = -EROFS;
			break;
		}

B
Bruce Leonard 已提交
418 419
		rsvd_bytes = (long long)vol->reserved_pebs *
					ubi->leb_size-vol->data_pad;
A
Artem B. Bityutskiy 已提交
420 421 422 423 424
		if (bytes < 0 || bytes > rsvd_bytes) {
			err = -EINVAL;
			break;
		}

425
		err = get_exclusive(desc);
A
Artem B. Bityutskiy 已提交
426 427 428
		if (err < 0)
			break;

429
		err = ubi_start_update(ubi, vol, bytes);
430 431
		if (bytes == 0) {
			ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
A
Artem B. Bityutskiy 已提交
432
			revoke_exclusive(desc, UBI_READWRITE);
433
		}
A
Artem B. Bityutskiy 已提交
434 435 436
		break;
	}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	/* Atomic logical eraseblock change command */
	case UBI_IOCEBCH:
	{
		struct ubi_leb_change_req req;

		err = copy_from_user(&req, argp,
				     sizeof(struct ubi_leb_change_req));
		if (err) {
			err = -EFAULT;
			break;
		}

		if (desc->mode == UBI_READONLY ||
		    vol->vol_type == UBI_STATIC_VOLUME) {
			err = -EROFS;
			break;
		}

		/* Validate the request */
		err = -EINVAL;
		if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
458
		    req.bytes < 0 || req.bytes > vol->usable_leb_size)
459 460
			break;

461
		err = get_exclusive(desc);
462 463 464 465 466 467 468 469 470
		if (err < 0)
			break;

		err = ubi_start_leb_change(ubi, vol, &req);
		if (req.bytes == 0)
			revoke_exclusive(desc, UBI_READWRITE);
		break;
	}

A
Artem B. Bityutskiy 已提交
471 472 473 474 475
	/* Logical eraseblock erasure command */
	case UBI_IOCEBER:
	{
		int32_t lnum;

C
Christoph Hellwig 已提交
476
		err = get_user(lnum, (__user int32_t *)argp);
A
Artem B. Bityutskiy 已提交
477 478 479 480 481
		if (err) {
			err = -EFAULT;
			break;
		}

482 483
		if (desc->mode == UBI_READONLY ||
		    vol->vol_type == UBI_STATIC_VOLUME) {
A
Artem B. Bityutskiy 已提交
484 485 486 487 488 489 490 491 492
			err = -EROFS;
			break;
		}

		if (lnum < 0 || lnum >= vol->reserved_pebs) {
			err = -EINVAL;
			break;
		}

493
		dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
494
		err = ubi_eba_unmap_leb(ubi, vol, lnum);
A
Artem B. Bityutskiy 已提交
495 496 497
		if (err)
			break;

498
		err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
A
Artem B. Bityutskiy 已提交
499 500
		break;
	}
501 502 503 504 505 506 507 508 509 510 511

	/* Logical eraseblock map command */
	case UBI_IOCEBMAP:
	{
		struct ubi_map_req req;

		err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
		if (err) {
			err = -EFAULT;
			break;
		}
R
Richard Weinberger 已提交
512
		err = ubi_leb_map(desc, req.lnum);
513 514
		break;
	}
515 516 517 518 519 520 521 522 523 524 525 526 527 528

	/* Logical eraseblock un-map command */
	case UBI_IOCEBUNMAP:
	{
		int32_t lnum;

		err = get_user(lnum, (__user int32_t *)argp);
		if (err) {
			err = -EFAULT;
			break;
		}
		err = ubi_leb_unmap(desc, lnum);
		break;
	}
529 530 531 532 533 534 535 536 537 538 539 540 541 542

	/* Check if logical eraseblock is mapped command */
	case UBI_IOCEBISMAP:
	{
		int32_t lnum;

		err = get_user(lnum, (__user int32_t *)argp);
		if (err) {
			err = -EFAULT;
			break;
		}
		err = ubi_is_mapped(desc, lnum);
		break;
	}
A
Artem B. Bityutskiy 已提交
543

544
	/* Set volume property command */
545
	case UBI_IOCSETVOLPROP:
S
Sidney Amani 已提交
546
	{
547
		struct ubi_set_vol_prop_req req;
S
Sidney Amani 已提交
548 549

		err = copy_from_user(&req, argp,
550
				     sizeof(struct ubi_set_vol_prop_req));
S
Sidney Amani 已提交
551 552 553 554 555
		if (err) {
			err = -EFAULT;
			break;
		}
		switch (req.property) {
556
		case UBI_VOL_PROP_DIRECT_WRITE:
557
			mutex_lock(&ubi->device_mutex);
S
Sidney Amani 已提交
558
			desc->vol->direct_writes = !!req.value;
559
			mutex_unlock(&ubi->device_mutex);
S
Sidney Amani 已提交
560 561 562 563 564 565 566 567
			break;
		default:
			err = -EINVAL;
			break;
		}
		break;
	}

568 569
	/* Create a R/O block device on top of the UBI volume */
	case UBI_IOCVOLCRBLK:
570 571 572 573
	{
		struct ubi_volume_info vi;

		ubi_get_volume_info(desc, &vi);
574
		err = ubiblock_create(&vi);
575 576 577
		break;
	}

578 579
	/* Remove the R/O block device */
	case UBI_IOCVOLRMBLK:
580 581 582 583
	{
		struct ubi_volume_info vi;

		ubi_get_volume_info(desc, &vi);
584
		err = ubiblock_remove(&vi);
585 586 587
		break;
	}

A
Artem B. Bityutskiy 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	default:
		err = -ENOTTY;
		break;
	}
	return err;
}

/**
 * verify_mkvol_req - verify volume creation request.
 * @ubi: UBI device description object
 * @req: the request to check
 *
 * This function zero if the request is correct, and %-EINVAL if not.
 */
static int verify_mkvol_req(const struct ubi_device *ubi,
			    const struct ubi_mkvol_req *req)
{
	int n, err = -EINVAL;

	if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
	    req->name_len < 0)
		goto bad;

	if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
	    req->vol_id != UBI_VOL_NUM_AUTO)
		goto bad;

	if (req->alignment == 0)
		goto bad;

	if (req->bytes == 0)
		goto bad;

	if (req->vol_type != UBI_DYNAMIC_VOLUME &&
	    req->vol_type != UBI_STATIC_VOLUME)
		goto bad;

	if (req->alignment > ubi->leb_size)
		goto bad;

628
	n = req->alignment & (ubi->min_io_size - 1);
A
Artem B. Bityutskiy 已提交
629 630 631
	if (req->alignment != 1 && n)
		goto bad;

632 633 634
	if (!req->name[0] || !req->name_len)
		goto bad;

A
Artem B. Bityutskiy 已提交
635 636 637 638 639
	if (req->name_len > UBI_VOL_NAME_MAX) {
		err = -ENAMETOOLONG;
		goto bad;
	}

640 641 642 643
	n = strnlen(req->name, req->name_len + 1);
	if (n != req->name_len)
		goto bad;

A
Artem B. Bityutskiy 已提交
644 645 646
	return 0;

bad:
647
	ubi_err(ubi, "bad volume creation request");
648
	ubi_dump_mkvol_req(req);
A
Artem B. Bityutskiy 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	return err;
}

/**
 * verify_rsvol_req - verify volume re-size request.
 * @ubi: UBI device description object
 * @req: the request to check
 *
 * This function returns zero if the request is correct, and %-EINVAL if not.
 */
static int verify_rsvol_req(const struct ubi_device *ubi,
			    const struct ubi_rsvol_req *req)
{
	if (req->bytes <= 0)
		return -EINVAL;

	if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
		return -EINVAL;

	return 0;
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
/**
 * rename_volumes - rename UBI volumes.
 * @ubi: UBI device description object
 * @req: volumes re-name request
 *
 * This is a helper function for the volume re-name IOCTL which validates the
 * the request, opens the volume and calls corresponding volumes management
 * function. Returns zero in case of success and a negative error code in case
 * of failure.
 */
static int rename_volumes(struct ubi_device *ubi,
			  struct ubi_rnvol_req *req)
{
	int i, n, err;
	struct list_head rename_list;
	struct ubi_rename_entry *re, *re1;

	if (req->count < 0 || req->count > UBI_MAX_RNVOL)
		return -EINVAL;

	if (req->count == 0)
		return 0;

	/* Validate volume IDs and names in the request */
	for (i = 0; i < req->count; i++) {
		if (req->ents[i].vol_id < 0 ||
		    req->ents[i].vol_id >= ubi->vtbl_slots)
			return -EINVAL;
		if (req->ents[i].name_len < 0)
			return -EINVAL;
		if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
			return -ENAMETOOLONG;
		req->ents[i].name[req->ents[i].name_len] = '\0';
		n = strlen(req->ents[i].name);
		if (n != req->ents[i].name_len)
706
			return -EINVAL;
707 708 709 710 711 712
	}

	/* Make sure volume IDs and names are unique */
	for (i = 0; i < req->count - 1; i++) {
		for (n = i + 1; n < req->count; n++) {
			if (req->ents[i].vol_id == req->ents[n].vol_id) {
713
				ubi_err(ubi, "duplicated volume id %d",
714 715 716 717
					req->ents[i].vol_id);
				return -EINVAL;
			}
			if (!strcmp(req->ents[i].name, req->ents[n].name)) {
718
				ubi_err(ubi, "duplicated volume name \"%s\"",
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
					req->ents[i].name);
				return -EINVAL;
			}
		}
	}

	/* Create the re-name list */
	INIT_LIST_HEAD(&rename_list);
	for (i = 0; i < req->count; i++) {
		int vol_id = req->ents[i].vol_id;
		int name_len = req->ents[i].name_len;
		const char *name = req->ents[i].name;

		re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
		if (!re) {
			err = -ENOMEM;
			goto out_free;
		}

738
		re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_METAONLY);
739 740
		if (IS_ERR(re->desc)) {
			err = PTR_ERR(re->desc);
741 742
			ubi_err(ubi, "cannot open volume %d, error %d",
				vol_id, err);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
			kfree(re);
			goto out_free;
		}

		/* Skip this re-naming if the name does not really change */
		if (re->desc->vol->name_len == name_len &&
		    !memcmp(re->desc->vol->name, name, name_len)) {
			ubi_close_volume(re->desc);
			kfree(re);
			continue;
		}

		re->new_name_len = name_len;
		memcpy(re->new_name, name, name_len);
		list_add_tail(&re->list, &rename_list);
A
Artem Bityutskiy 已提交
758
		dbg_gen("will rename volume %d from \"%s\" to \"%s\"",
759 760 761 762 763 764 765 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
			vol_id, re->desc->vol->name, name);
	}

	if (list_empty(&rename_list))
		return 0;

	/* Find out the volumes which have to be removed */
	list_for_each_entry(re, &rename_list, list) {
		struct ubi_volume_desc *desc;
		int no_remove_needed = 0;

		/*
		 * Volume @re->vol_id is going to be re-named to
		 * @re->new_name, while its current name is @name. If a volume
		 * with name @re->new_name currently exists, it has to be
		 * removed, unless it is also re-named in the request (@req).
		 */
		list_for_each_entry(re1, &rename_list, list) {
			if (re->new_name_len == re1->desc->vol->name_len &&
			    !memcmp(re->new_name, re1->desc->vol->name,
				    re1->desc->vol->name_len)) {
				no_remove_needed = 1;
				break;
			}
		}

		if (no_remove_needed)
			continue;

		/*
		 * It seems we need to remove volume with name @re->new_name,
		 * if it exists.
		 */
A
Artem Bityutskiy 已提交
792 793
		desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
					  UBI_EXCLUSIVE);
794 795 796 797 798 799 800
		if (IS_ERR(desc)) {
			err = PTR_ERR(desc);
			if (err == -ENODEV)
				/* Re-naming into a non-existing volume name */
				continue;

			/* The volume exists but busy, or an error occurred */
801
			ubi_err(ubi, "cannot open volume \"%s\", error %d",
802 803 804 805
				re->new_name, err);
			goto out_free;
		}

806 807
		re1 = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
		if (!re1) {
808 809 810 811 812
			err = -ENOMEM;
			ubi_close_volume(desc);
			goto out_free;
		}

813 814 815
		re1->remove = 1;
		re1->desc = desc;
		list_add(&re1->list, &rename_list);
A
Artem Bityutskiy 已提交
816
		dbg_gen("will remove volume %d, name \"%s\"",
817
			re1->desc->vol->vol_id, re1->desc->vol->name);
818 819
	}

820
	mutex_lock(&ubi->device_mutex);
821
	err = ubi_rename_volumes(ubi, &rename_list);
822
	mutex_unlock(&ubi->device_mutex);
823 824 825 826 827 828 829 830 831 832

out_free:
	list_for_each_entry_safe(re, re1, &rename_list, list) {
		ubi_close_volume(re->desc);
		list_del(&re->list);
		kfree(re);
	}
	return err;
}

A
Artem Bityutskiy 已提交
833 834
static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
			   unsigned long arg)
A
Artem B. Bityutskiy 已提交
835 836 837 838 839 840 841 842 843
{
	int err = 0;
	struct ubi_device *ubi;
	struct ubi_volume_desc *desc;
	void __user *argp = (void __user *)arg;

	if (!capable(CAP_SYS_RESOURCE))
		return -EPERM;

A
Artem Bityutskiy 已提交
844
	ubi = ubi_get_by_major(imajor(file->f_mapping->host));
845 846
	if (!ubi)
		return -ENODEV;
A
Artem B. Bityutskiy 已提交
847 848 849 850 851 852 853

	switch (cmd) {
	/* Create volume command */
	case UBI_IOCMKVOL:
	{
		struct ubi_mkvol_req req;

854
		dbg_gen("create volume");
A
Artem Bityutskiy 已提交
855
		err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
A
Artem B. Bityutskiy 已提交
856 857 858 859 860 861 862 863 864
		if (err) {
			err = -EFAULT;
			break;
		}

		err = verify_mkvol_req(ubi, &req);
		if (err)
			break;

865
		mutex_lock(&ubi->device_mutex);
A
Artem B. Bityutskiy 已提交
866
		err = ubi_create_volume(ubi, &req);
867
		mutex_unlock(&ubi->device_mutex);
A
Artem B. Bityutskiy 已提交
868 869 870
		if (err)
			break;

C
Christoph Hellwig 已提交
871
		err = put_user(req.vol_id, (__user int32_t *)argp);
A
Artem B. Bityutskiy 已提交
872 873 874 875 876 877 878 879 880 881 882
		if (err)
			err = -EFAULT;

		break;
	}

	/* Remove volume command */
	case UBI_IOCRMVOL:
	{
		int vol_id;

883
		dbg_gen("remove volume");
C
Christoph Hellwig 已提交
884
		err = get_user(vol_id, (__user int32_t *)argp);
A
Artem B. Bityutskiy 已提交
885 886 887 888 889 890 891 892 893 894 895
		if (err) {
			err = -EFAULT;
			break;
		}

		desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
		if (IS_ERR(desc)) {
			err = PTR_ERR(desc);
			break;
		}

896
		mutex_lock(&ubi->device_mutex);
897
		err = ubi_remove_volume(desc, 0);
898
		mutex_unlock(&ubi->device_mutex);
899

900
		/*
901 902 903
		 * The volume is deleted (unless an error occurred), and the
		 * 'struct ubi_volume' object will be freed when
		 * 'ubi_close_volume()' will call 'put_device()'.
904 905
		 */
		ubi_close_volume(desc);
A
Artem B. Bityutskiy 已提交
906 907 908 909 910 911 912 913 914
		break;
	}

	/* Re-size volume command */
	case UBI_IOCRSVOL:
	{
		int pebs;
		struct ubi_rsvol_req req;

915
		dbg_gen("re-size volume");
A
Artem Bityutskiy 已提交
916
		err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
A
Artem B. Bityutskiy 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
		if (err) {
			err = -EFAULT;
			break;
		}

		err = verify_rsvol_req(ubi, &req);
		if (err)
			break;

		desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
		if (IS_ERR(desc)) {
			err = PTR_ERR(desc);
			break;
		}

A
Artem Bityutskiy 已提交
932 933
		pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
			       desc->vol->usable_leb_size);
A
Artem B. Bityutskiy 已提交
934

935
		mutex_lock(&ubi->device_mutex);
A
Artem B. Bityutskiy 已提交
936
		err = ubi_resize_volume(desc, pebs);
937
		mutex_unlock(&ubi->device_mutex);
A
Artem B. Bityutskiy 已提交
938 939 940 941
		ubi_close_volume(desc);
		break;
	}

942 943 944 945 946
	/* Re-name volumes command */
	case UBI_IOCRNVOL:
	{
		struct ubi_rnvol_req *req;

A
Artem Bityutskiy 已提交
947
		dbg_gen("re-name volumes");
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
		req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
		if (!req) {
			err = -ENOMEM;
			break;
		};

		err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
		if (err) {
			err = -EFAULT;
			kfree(req);
			break;
		}

		err = rename_volumes(ubi, req);
		kfree(req);
		break;
	}

A
Artem B. Bityutskiy 已提交
966 967 968 969 970
	default:
		err = -ENOTTY;
		break;
	}

971
	ubi_put_device(ubi);
A
Artem B. Bityutskiy 已提交
972 973 974
	return err;
}

A
Artem Bityutskiy 已提交
975 976
static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
			    unsigned long arg)
A
Artem Bityutskiy 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990
{
	int err = 0;
	void __user *argp = (void __user *)arg;

	if (!capable(CAP_SYS_RESOURCE))
		return -EPERM;

	switch (cmd) {
	/* Attach an MTD device command */
	case UBI_IOCATT:
	{
		struct ubi_attach_req req;
		struct mtd_info *mtd;

991
		dbg_gen("attach MTD device");
A
Artem Bityutskiy 已提交
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
		err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
		if (err) {
			err = -EFAULT;
			break;
		}

		if (req.mtd_num < 0 ||
		    (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
			err = -EINVAL;
			break;
		}

		mtd = get_mtd_device(NULL, req.mtd_num);
		if (IS_ERR(mtd)) {
			err = PTR_ERR(mtd);
			break;
		}

		/*
		 * Note, further request verification is done by
		 * 'ubi_attach_mtd_dev()'.
		 */
		mutex_lock(&ubi_devices_mutex);
1015
		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
1016
					 req.max_beb_per1024);
A
Artem Bityutskiy 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
		mutex_unlock(&ubi_devices_mutex);
		if (err < 0)
			put_mtd_device(mtd);
		else
			/* @err contains UBI device number */
			err = put_user(err, (__user int32_t *)argp);

		break;
	}

	/* Detach an MTD device command */
	case UBI_IOCDET:
	{
		int ubi_num;

1032
		dbg_gen("detach MTD device");
A
Artem Bityutskiy 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		err = get_user(ubi_num, (__user int32_t *)argp);
		if (err) {
			err = -EFAULT;
			break;
		}

		mutex_lock(&ubi_devices_mutex);
		err = ubi_detach_mtd_dev(ubi_num, 0);
		mutex_unlock(&ubi_devices_mutex);
		break;
	}

	default:
		err = -ENOTTY;
		break;
	}

	return err;
}

A
Artem Bityutskiy 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
#ifdef CONFIG_COMPAT
static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
				  unsigned long arg)
{
	unsigned long translated_arg = (unsigned long)compat_ptr(arg);

	return vol_cdev_ioctl(file, cmd, translated_arg);
}

static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
				  unsigned long arg)
{
	unsigned long translated_arg = (unsigned long)compat_ptr(arg);

	return ubi_cdev_ioctl(file, cmd, translated_arg);
}

static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
				   unsigned long arg)
{
	unsigned long translated_arg = (unsigned long)compat_ptr(arg);

	return ctrl_cdev_ioctl(file, cmd, translated_arg);
}
#else
#define vol_cdev_compat_ioctl  NULL
#define ubi_cdev_compat_ioctl  NULL
#define ctrl_cdev_compat_ioctl NULL
#endif

/* UBI volume character device operations */
const struct file_operations ubi_vol_cdev_operations = {
	.owner          = THIS_MODULE,
	.open           = vol_cdev_open,
	.release        = vol_cdev_release,
	.llseek         = vol_cdev_llseek,
	.read           = vol_cdev_read,
	.write          = vol_cdev_write,
C
Corentin Chary 已提交
1091
	.fsync		= vol_cdev_fsync,
A
Artem Bityutskiy 已提交
1092 1093
	.unlocked_ioctl = vol_cdev_ioctl,
	.compat_ioctl   = vol_cdev_compat_ioctl,
A
Artem Bityutskiy 已提交
1094 1095
};

A
Artem B. Bityutskiy 已提交
1096
/* UBI character device operations */
J
Jan Engelhardt 已提交
1097
const struct file_operations ubi_cdev_operations = {
A
Artem Bityutskiy 已提交
1098 1099 1100 1101
	.owner          = THIS_MODULE,
	.llseek         = no_llseek,
	.unlocked_ioctl = ubi_cdev_ioctl,
	.compat_ioctl   = ubi_cdev_compat_ioctl,
A
Artem B. Bityutskiy 已提交
1102 1103
};

A
Artem Bityutskiy 已提交
1104 1105 1106 1107 1108
/* UBI control character device operations */
const struct file_operations ubi_ctrl_cdev_operations = {
	.owner          = THIS_MODULE,
	.unlocked_ioctl = ctrl_cdev_ioctl,
	.compat_ioctl   = ctrl_cdev_compat_ioctl,
1109
	.llseek		= no_llseek,
A
Artem B. Bityutskiy 已提交
1110
};