Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This file includes implementation of UBI character device operations. |
| 23 | * |
| 24 | * There are two kinds of character devices in UBI: UBI character devices and |
| 25 | * UBI volume character devices. UBI character devices allow users to |
| 26 | * manipulate whole volumes: create, remove, and re-size them. Volume character |
| 27 | * devices provide volume I/O capabilities. |
| 28 | * |
| 29 | * Major and minor numbers are assigned dynamically to both UBI and volume |
| 30 | * character devices. |
| 31 | */ |
| 32 | |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/stat.h> |
| 35 | #include <linux/ioctl.h> |
| 36 | #include <linux/capability.h> |
| 37 | #include <mtd/ubi-user.h> |
| 38 | #include <asm/uaccess.h> |
| 39 | #include <asm/div64.h> |
| 40 | #include "ubi.h" |
| 41 | |
| 42 | /* |
| 43 | * Maximum sequence numbers of UBI and volume character device IOCTLs (direct |
| 44 | * logical eraseblock erase is a debug-only feature). |
| 45 | */ |
| 46 | #define UBI_CDEV_IOC_MAX_SEQ 2 |
| 47 | #ifndef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO |
| 48 | #define VOL_CDEV_IOC_MAX_SEQ 1 |
| 49 | #else |
| 50 | #define VOL_CDEV_IOC_MAX_SEQ 2 |
| 51 | #endif |
| 52 | |
| 53 | /** |
| 54 | * major_to_device - get UBI device object by character device major number. |
| 55 | * @major: major number |
| 56 | * |
| 57 | * This function returns a pointer to the UBI device object. |
| 58 | */ |
| 59 | static struct ubi_device *major_to_device(int major) |
| 60 | { |
| 61 | int i; |
| 62 | |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 63 | for (i = 0; i < UBI_MAX_DEVICES; i++) |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 64 | if (ubi_devices[i] && MAJOR(ubi_devices[i]->cdev.dev) == major) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 65 | return ubi_devices[i]; |
| 66 | BUG(); |
Paul Mundt | add0b43 | 2007-07-18 18:39:45 +0900 | [diff] [blame] | 67 | return NULL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * get_exclusive - get exclusive access to an UBI volume. |
| 72 | * @desc: volume descriptor |
| 73 | * |
| 74 | * This function changes UBI volume open mode to "exclusive". Returns previous |
| 75 | * mode value (positive integer) in case of success and a negative error code |
| 76 | * in case of failure. |
| 77 | */ |
| 78 | static int get_exclusive(struct ubi_volume_desc *desc) |
| 79 | { |
| 80 | int users, err; |
| 81 | struct ubi_volume *vol = desc->vol; |
| 82 | |
| 83 | spin_lock(&vol->ubi->volumes_lock); |
| 84 | users = vol->readers + vol->writers + vol->exclusive; |
| 85 | ubi_assert(users > 0); |
| 86 | if (users > 1) { |
| 87 | dbg_err("%d users for volume %d", users, vol->vol_id); |
| 88 | err = -EBUSY; |
| 89 | } else { |
| 90 | vol->readers = vol->writers = 0; |
| 91 | vol->exclusive = 1; |
| 92 | err = desc->mode; |
| 93 | desc->mode = UBI_EXCLUSIVE; |
| 94 | } |
| 95 | spin_unlock(&vol->ubi->volumes_lock); |
| 96 | |
| 97 | return err; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * revoke_exclusive - revoke exclusive mode. |
| 102 | * @desc: volume descriptor |
| 103 | * @mode: new mode to switch to |
| 104 | */ |
| 105 | static void revoke_exclusive(struct ubi_volume_desc *desc, int mode) |
| 106 | { |
| 107 | struct ubi_volume *vol = desc->vol; |
| 108 | |
| 109 | spin_lock(&vol->ubi->volumes_lock); |
| 110 | ubi_assert(vol->readers == 0 && vol->writers == 0); |
| 111 | ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE); |
| 112 | vol->exclusive = 0; |
| 113 | if (mode == UBI_READONLY) |
| 114 | vol->readers = 1; |
| 115 | else if (mode == UBI_READWRITE) |
| 116 | vol->writers = 1; |
| 117 | else |
| 118 | vol->exclusive = 1; |
| 119 | spin_unlock(&vol->ubi->volumes_lock); |
| 120 | |
| 121 | desc->mode = mode; |
| 122 | } |
| 123 | |
| 124 | static int vol_cdev_open(struct inode *inode, struct file *file) |
| 125 | { |
| 126 | struct ubi_volume_desc *desc; |
| 127 | const struct ubi_device *ubi = major_to_device(imajor(inode)); |
| 128 | int vol_id = iminor(inode) - 1; |
| 129 | int mode; |
| 130 | |
| 131 | if (file->f_mode & FMODE_WRITE) |
| 132 | mode = UBI_READWRITE; |
| 133 | else |
| 134 | mode = UBI_READONLY; |
| 135 | |
| 136 | dbg_msg("open volume %d, mode %d", vol_id, mode); |
| 137 | |
| 138 | desc = ubi_open_volume(ubi->ubi_num, vol_id, mode); |
| 139 | if (IS_ERR(desc)) |
| 140 | return PTR_ERR(desc); |
| 141 | |
| 142 | file->private_data = desc; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int vol_cdev_release(struct inode *inode, struct file *file) |
| 147 | { |
| 148 | struct ubi_volume_desc *desc = file->private_data; |
| 149 | struct ubi_volume *vol = desc->vol; |
| 150 | |
| 151 | dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode); |
| 152 | |
| 153 | if (vol->updating) { |
| 154 | ubi_warn("update of volume %d not finished, volume is damaged", |
| 155 | vol->vol_id); |
| 156 | vol->updating = 0; |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 157 | vfree(vol->upd_buf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | ubi_close_volume(desc); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin) |
| 165 | { |
| 166 | struct ubi_volume_desc *desc = file->private_data; |
| 167 | struct ubi_volume *vol = desc->vol; |
| 168 | loff_t new_offset; |
| 169 | |
| 170 | if (vol->updating) { |
| 171 | /* Update is in progress, seeking is prohibited */ |
| 172 | dbg_err("updating"); |
| 173 | return -EBUSY; |
| 174 | } |
| 175 | |
| 176 | switch (origin) { |
| 177 | case 0: /* SEEK_SET */ |
| 178 | new_offset = offset; |
| 179 | break; |
| 180 | case 1: /* SEEK_CUR */ |
| 181 | new_offset = file->f_pos + offset; |
| 182 | break; |
| 183 | case 2: /* SEEK_END */ |
| 184 | new_offset = vol->used_bytes + offset; |
| 185 | break; |
| 186 | default: |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
| 190 | if (new_offset < 0 || new_offset > vol->used_bytes) { |
| 191 | dbg_err("bad seek %lld", new_offset); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld", |
| 196 | vol->vol_id, offset, origin, new_offset); |
| 197 | |
| 198 | file->f_pos = new_offset; |
| 199 | return new_offset; |
| 200 | } |
| 201 | |
| 202 | static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count, |
| 203 | loff_t *offp) |
| 204 | { |
| 205 | struct ubi_volume_desc *desc = file->private_data; |
| 206 | struct ubi_volume *vol = desc->vol; |
| 207 | struct ubi_device *ubi = vol->ubi; |
| 208 | int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size; |
| 209 | size_t count_save = count; |
| 210 | void *tbuf; |
| 211 | uint64_t tmp; |
| 212 | |
| 213 | dbg_msg("read %zd bytes from offset %lld of volume %d", |
| 214 | count, *offp, vol_id); |
| 215 | |
| 216 | if (vol->updating) { |
| 217 | dbg_err("updating"); |
| 218 | return -EBUSY; |
| 219 | } |
| 220 | if (vol->upd_marker) { |
| 221 | dbg_err("damaged volume, update marker is set"); |
| 222 | return -EBADF; |
| 223 | } |
| 224 | if (*offp == vol->used_bytes || count == 0) |
| 225 | return 0; |
| 226 | |
| 227 | if (vol->corrupted) |
| 228 | dbg_msg("read from corrupted volume %d", vol_id); |
| 229 | |
| 230 | if (*offp + count > vol->used_bytes) |
| 231 | count_save = count = vol->used_bytes - *offp; |
| 232 | |
| 233 | tbuf_size = vol->usable_leb_size; |
| 234 | if (count < tbuf_size) |
| 235 | tbuf_size = ALIGN(count, ubi->min_io_size); |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 236 | tbuf = vmalloc(tbuf_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 237 | if (!tbuf) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | len = count > tbuf_size ? tbuf_size : count; |
| 241 | |
| 242 | tmp = *offp; |
| 243 | off = do_div(tmp, vol->usable_leb_size); |
| 244 | lnum = tmp; |
| 245 | |
| 246 | do { |
| 247 | cond_resched(); |
| 248 | |
| 249 | if (off + len >= vol->usable_leb_size) |
| 250 | len = vol->usable_leb_size - off; |
| 251 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 252 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 253 | if (err) |
| 254 | break; |
| 255 | |
| 256 | off += len; |
| 257 | if (off == vol->usable_leb_size) { |
| 258 | lnum += 1; |
| 259 | off -= vol->usable_leb_size; |
| 260 | } |
| 261 | |
| 262 | count -= len; |
| 263 | *offp += len; |
| 264 | |
| 265 | err = copy_to_user(buf, tbuf, len); |
| 266 | if (err) { |
| 267 | err = -EFAULT; |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | buf += len; |
| 272 | len = count > tbuf_size ? tbuf_size : count; |
| 273 | } while (count); |
| 274 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 275 | vfree(tbuf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 276 | return err ? err : count_save - count; |
| 277 | } |
| 278 | |
| 279 | #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO |
| 280 | |
| 281 | /* |
| 282 | * This function allows to directly write to dynamic UBI volumes, without |
| 283 | * issuing the volume update operation. Available only as a debugging feature. |
| 284 | * Very useful for testing UBI. |
| 285 | */ |
| 286 | static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf, |
| 287 | size_t count, loff_t *offp) |
| 288 | { |
| 289 | struct ubi_volume_desc *desc = file->private_data; |
| 290 | struct ubi_volume *vol = desc->vol; |
| 291 | struct ubi_device *ubi = vol->ubi; |
| 292 | int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err = 0; |
| 293 | size_t count_save = count; |
| 294 | char *tbuf; |
| 295 | uint64_t tmp; |
| 296 | |
| 297 | dbg_msg("requested: write %zd bytes to offset %lld of volume %u", |
| 298 | count, *offp, desc->vol->vol_id); |
| 299 | |
| 300 | if (vol->vol_type == UBI_STATIC_VOLUME) |
| 301 | return -EROFS; |
| 302 | |
| 303 | tmp = *offp; |
| 304 | off = do_div(tmp, vol->usable_leb_size); |
| 305 | lnum = tmp; |
| 306 | |
| 307 | if (off % ubi->min_io_size) { |
| 308 | dbg_err("unaligned position"); |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | |
| 312 | if (*offp + count > vol->used_bytes) |
| 313 | count_save = count = vol->used_bytes - *offp; |
| 314 | |
| 315 | /* We can write only in fractions of the minimum I/O unit */ |
| 316 | if (count % ubi->min_io_size) { |
| 317 | dbg_err("unaligned write length"); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | tbuf_size = vol->usable_leb_size; |
| 322 | if (count < tbuf_size) |
| 323 | tbuf_size = ALIGN(count, ubi->min_io_size); |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 324 | tbuf = vmalloc(tbuf_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 325 | if (!tbuf) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | len = count > tbuf_size ? tbuf_size : count; |
| 329 | |
| 330 | while (count) { |
| 331 | cond_resched(); |
| 332 | |
| 333 | if (off + len >= vol->usable_leb_size) |
| 334 | len = vol->usable_leb_size - off; |
| 335 | |
| 336 | err = copy_from_user(tbuf, buf, len); |
| 337 | if (err) { |
| 338 | err = -EFAULT; |
| 339 | break; |
| 340 | } |
| 341 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 342 | err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 343 | UBI_UNKNOWN); |
| 344 | if (err) |
| 345 | break; |
| 346 | |
| 347 | off += len; |
| 348 | if (off == vol->usable_leb_size) { |
| 349 | lnum += 1; |
| 350 | off -= vol->usable_leb_size; |
| 351 | } |
| 352 | |
| 353 | count -= len; |
| 354 | *offp += len; |
| 355 | buf += len; |
| 356 | len = count > tbuf_size ? tbuf_size : count; |
| 357 | } |
| 358 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 359 | vfree(tbuf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 360 | return err ? err : count_save - count; |
| 361 | } |
| 362 | |
| 363 | #else |
| 364 | #define vol_cdev_direct_write(file, buf, count, offp) -EPERM |
| 365 | #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */ |
| 366 | |
| 367 | static ssize_t vol_cdev_write(struct file *file, const char __user *buf, |
| 368 | size_t count, loff_t *offp) |
| 369 | { |
| 370 | int err = 0; |
| 371 | struct ubi_volume_desc *desc = file->private_data; |
| 372 | struct ubi_volume *vol = desc->vol; |
| 373 | struct ubi_device *ubi = vol->ubi; |
| 374 | |
| 375 | if (!vol->updating) |
| 376 | return vol_cdev_direct_write(file, buf, count, offp); |
| 377 | |
| 378 | err = ubi_more_update_data(ubi, vol->vol_id, buf, count); |
| 379 | if (err < 0) { |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 380 | ubi_err("cannot write %zd bytes of update data, error %d", |
| 381 | count, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 382 | return err; |
| 383 | } |
| 384 | |
| 385 | if (err) { |
| 386 | /* |
| 387 | * Update is finished, @err contains number of actually written |
| 388 | * bytes now. |
| 389 | */ |
| 390 | count = err; |
| 391 | |
| 392 | err = ubi_check_volume(ubi, vol->vol_id); |
| 393 | if (err < 0) |
| 394 | return err; |
| 395 | |
| 396 | if (err) { |
| 397 | ubi_warn("volume %d on UBI device %d is corrupted", |
| 398 | vol->vol_id, ubi->ubi_num); |
| 399 | vol->corrupted = 1; |
| 400 | } |
| 401 | vol->checked = 1; |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 402 | ubi_gluebi_updated(vol); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 403 | revoke_exclusive(desc, UBI_READWRITE); |
| 404 | } |
| 405 | |
| 406 | *offp += count; |
| 407 | return count; |
| 408 | } |
| 409 | |
| 410 | static int vol_cdev_ioctl(struct inode *inode, struct file *file, |
| 411 | unsigned int cmd, unsigned long arg) |
| 412 | { |
| 413 | int err = 0; |
| 414 | struct ubi_volume_desc *desc = file->private_data; |
| 415 | struct ubi_volume *vol = desc->vol; |
| 416 | struct ubi_device *ubi = vol->ubi; |
| 417 | void __user *argp = (void __user *)arg; |
| 418 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 419 | switch (cmd) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 420 | /* Volume update command */ |
| 421 | case UBI_IOCVOLUP: |
| 422 | { |
| 423 | int64_t bytes, rsvd_bytes; |
| 424 | |
| 425 | if (!capable(CAP_SYS_RESOURCE)) { |
| 426 | err = -EPERM; |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | err = copy_from_user(&bytes, argp, sizeof(int64_t)); |
| 431 | if (err) { |
| 432 | err = -EFAULT; |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | if (desc->mode == UBI_READONLY) { |
| 437 | err = -EROFS; |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad); |
| 442 | if (bytes < 0 || bytes > rsvd_bytes) { |
| 443 | err = -EINVAL; |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | err = get_exclusive(desc); |
| 448 | if (err < 0) |
| 449 | break; |
| 450 | |
| 451 | err = ubi_start_update(ubi, vol->vol_id, bytes); |
| 452 | if (bytes == 0) |
| 453 | revoke_exclusive(desc, UBI_READWRITE); |
| 454 | |
| 455 | file->f_pos = 0; |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO |
| 460 | /* Logical eraseblock erasure command */ |
| 461 | case UBI_IOCEBER: |
| 462 | { |
| 463 | int32_t lnum; |
| 464 | |
Christoph Hellwig | bf07803 | 2007-05-17 16:32:10 +0200 | [diff] [blame] | 465 | err = get_user(lnum, (__user int32_t *)argp); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 466 | if (err) { |
| 467 | err = -EFAULT; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | if (desc->mode == UBI_READONLY) { |
| 472 | err = -EROFS; |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | if (lnum < 0 || lnum >= vol->reserved_pebs) { |
| 477 | err = -EINVAL; |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | if (vol->vol_type != UBI_DYNAMIC_VOLUME) { |
| 482 | err = -EROFS; |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | dbg_msg("erase LEB %d:%d", vol->vol_id, lnum); |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 487 | err = ubi_eba_unmap_leb(ubi, vol, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 488 | if (err) |
| 489 | break; |
| 490 | |
| 491 | err = ubi_wl_flush(ubi); |
| 492 | break; |
| 493 | } |
| 494 | #endif |
| 495 | |
| 496 | default: |
| 497 | err = -ENOTTY; |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | return err; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * verify_mkvol_req - verify volume creation request. |
| 506 | * @ubi: UBI device description object |
| 507 | * @req: the request to check |
| 508 | * |
| 509 | * This function zero if the request is correct, and %-EINVAL if not. |
| 510 | */ |
| 511 | static int verify_mkvol_req(const struct ubi_device *ubi, |
| 512 | const struct ubi_mkvol_req *req) |
| 513 | { |
| 514 | int n, err = -EINVAL; |
| 515 | |
| 516 | if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 || |
| 517 | req->name_len < 0) |
| 518 | goto bad; |
| 519 | |
| 520 | if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) && |
| 521 | req->vol_id != UBI_VOL_NUM_AUTO) |
| 522 | goto bad; |
| 523 | |
| 524 | if (req->alignment == 0) |
| 525 | goto bad; |
| 526 | |
| 527 | if (req->bytes == 0) |
| 528 | goto bad; |
| 529 | |
| 530 | if (req->vol_type != UBI_DYNAMIC_VOLUME && |
| 531 | req->vol_type != UBI_STATIC_VOLUME) |
| 532 | goto bad; |
| 533 | |
| 534 | if (req->alignment > ubi->leb_size) |
| 535 | goto bad; |
| 536 | |
| 537 | n = req->alignment % ubi->min_io_size; |
| 538 | if (req->alignment != 1 && n) |
| 539 | goto bad; |
| 540 | |
| 541 | if (req->name_len > UBI_VOL_NAME_MAX) { |
| 542 | err = -ENAMETOOLONG; |
| 543 | goto bad; |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | |
| 548 | bad: |
| 549 | dbg_err("bad volume creation request"); |
| 550 | ubi_dbg_dump_mkvol_req(req); |
| 551 | return err; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * verify_rsvol_req - verify volume re-size request. |
| 556 | * @ubi: UBI device description object |
| 557 | * @req: the request to check |
| 558 | * |
| 559 | * This function returns zero if the request is correct, and %-EINVAL if not. |
| 560 | */ |
| 561 | static int verify_rsvol_req(const struct ubi_device *ubi, |
| 562 | const struct ubi_rsvol_req *req) |
| 563 | { |
| 564 | if (req->bytes <= 0) |
| 565 | return -EINVAL; |
| 566 | |
| 567 | if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) |
| 568 | return -EINVAL; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int ubi_cdev_ioctl(struct inode *inode, struct file *file, |
| 574 | unsigned int cmd, unsigned long arg) |
| 575 | { |
| 576 | int err = 0; |
| 577 | struct ubi_device *ubi; |
| 578 | struct ubi_volume_desc *desc; |
| 579 | void __user *argp = (void __user *)arg; |
| 580 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 581 | if (!capable(CAP_SYS_RESOURCE)) |
| 582 | return -EPERM; |
| 583 | |
| 584 | ubi = major_to_device(imajor(inode)); |
| 585 | if (IS_ERR(ubi)) |
| 586 | return PTR_ERR(ubi); |
| 587 | |
| 588 | switch (cmd) { |
| 589 | /* Create volume command */ |
| 590 | case UBI_IOCMKVOL: |
| 591 | { |
| 592 | struct ubi_mkvol_req req; |
| 593 | |
| 594 | dbg_msg("create volume"); |
Christoph Hellwig | bf07803 | 2007-05-17 16:32:10 +0200 | [diff] [blame] | 595 | err = copy_from_user(&req, argp, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 596 | sizeof(struct ubi_mkvol_req)); |
| 597 | if (err) { |
| 598 | err = -EFAULT; |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | err = verify_mkvol_req(ubi, &req); |
| 603 | if (err) |
| 604 | break; |
| 605 | |
| 606 | req.name[req.name_len] = '\0'; |
| 607 | |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 608 | mutex_lock(&ubi->volumes_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 609 | err = ubi_create_volume(ubi, &req); |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 610 | mutex_unlock(&ubi->volumes_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 611 | if (err) |
| 612 | break; |
| 613 | |
Christoph Hellwig | bf07803 | 2007-05-17 16:32:10 +0200 | [diff] [blame] | 614 | err = put_user(req.vol_id, (__user int32_t *)argp); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 615 | if (err) |
| 616 | err = -EFAULT; |
| 617 | |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | /* Remove volume command */ |
| 622 | case UBI_IOCRMVOL: |
| 623 | { |
| 624 | int vol_id; |
| 625 | |
| 626 | dbg_msg("remove volume"); |
Christoph Hellwig | bf07803 | 2007-05-17 16:32:10 +0200 | [diff] [blame] | 627 | err = get_user(vol_id, (__user int32_t *)argp); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 628 | if (err) { |
| 629 | err = -EFAULT; |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE); |
| 634 | if (IS_ERR(desc)) { |
| 635 | err = PTR_ERR(desc); |
| 636 | break; |
| 637 | } |
| 638 | |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 639 | mutex_lock(&ubi->volumes_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 640 | err = ubi_remove_volume(desc); |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 641 | mutex_unlock(&ubi->volumes_mutex); |
| 642 | |
Artem Bityutskiy | 450f872 | 2007-12-17 13:09:09 +0200 | [diff] [blame] | 643 | /* |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 644 | * The volume is deleted (unless an error occurred), and the |
| 645 | * 'struct ubi_volume' object will be freed when |
| 646 | * 'ubi_close_volume()' will call 'put_device()'. |
Artem Bityutskiy | 450f872 | 2007-12-17 13:09:09 +0200 | [diff] [blame] | 647 | */ |
| 648 | ubi_close_volume(desc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | |
| 652 | /* Re-size volume command */ |
| 653 | case UBI_IOCRSVOL: |
| 654 | { |
| 655 | int pebs; |
| 656 | uint64_t tmp; |
| 657 | struct ubi_rsvol_req req; |
| 658 | |
| 659 | dbg_msg("re-size volume"); |
Christoph Hellwig | bf07803 | 2007-05-17 16:32:10 +0200 | [diff] [blame] | 660 | err = copy_from_user(&req, argp, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 661 | sizeof(struct ubi_rsvol_req)); |
| 662 | if (err) { |
| 663 | err = -EFAULT; |
| 664 | break; |
| 665 | } |
| 666 | |
| 667 | err = verify_rsvol_req(ubi, &req); |
| 668 | if (err) |
| 669 | break; |
| 670 | |
| 671 | desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE); |
| 672 | if (IS_ERR(desc)) { |
| 673 | err = PTR_ERR(desc); |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | tmp = req.bytes; |
| 678 | pebs = !!do_div(tmp, desc->vol->usable_leb_size); |
| 679 | pebs += tmp; |
| 680 | |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 681 | mutex_lock(&ubi->volumes_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 682 | err = ubi_resize_volume(desc, pebs); |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame^] | 683 | mutex_unlock(&ubi->volumes_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 684 | ubi_close_volume(desc); |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | default: |
| 689 | err = -ENOTTY; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | /* UBI character device operations */ |
| 697 | struct file_operations ubi_cdev_operations = { |
| 698 | .owner = THIS_MODULE, |
| 699 | .ioctl = ubi_cdev_ioctl, |
Artem Bityutskiy | f800f09 | 2007-05-06 16:45:43 +0300 | [diff] [blame] | 700 | .llseek = no_llseek, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | /* UBI volume character device operations */ |
| 704 | struct file_operations ubi_vol_cdev_operations = { |
| 705 | .owner = THIS_MODULE, |
| 706 | .open = vol_cdev_open, |
| 707 | .release = vol_cdev_release, |
| 708 | .llseek = vol_cdev_llseek, |
| 709 | .read = vol_cdev_read, |
| 710 | .write = vol_cdev_write, |
Artem Bityutskiy | f800f09 | 2007-05-06 16:45:43 +0300 | [diff] [blame] | 711 | .ioctl = vol_cdev_ioctl, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 712 | }; |