blob: 0c4044d6cae05fee38d59abefd6ca0500eabdede [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
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.
Artem Bityutskiy9f961b52007-12-16 16:59:31 +020031 *
32 * Well, there is the third kind of character devices - the UBI control
33 * character device, which allows to manipulate by UBI devices - create and
34 * delete them. In other words, it is used for attaching and detaching MTD
35 * devices.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040036 */
37
38#include <linux/module.h>
39#include <linux/stat.h>
40#include <linux/ioctl.h>
41#include <linux/capability.h>
42#include <mtd/ubi-user.h>
43#include <asm/uaccess.h>
44#include <asm/div64.h>
45#include "ubi.h"
46
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040047/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040048 * get_exclusive - get exclusive access to an UBI volume.
49 * @desc: volume descriptor
50 *
51 * This function changes UBI volume open mode to "exclusive". Returns previous
52 * mode value (positive integer) in case of success and a negative error code
53 * in case of failure.
54 */
55static int get_exclusive(struct ubi_volume_desc *desc)
56{
57 int users, err;
58 struct ubi_volume *vol = desc->vol;
59
60 spin_lock(&vol->ubi->volumes_lock);
61 users = vol->readers + vol->writers + vol->exclusive;
62 ubi_assert(users > 0);
63 if (users > 1) {
64 dbg_err("%d users for volume %d", users, vol->vol_id);
65 err = -EBUSY;
66 } else {
67 vol->readers = vol->writers = 0;
68 vol->exclusive = 1;
69 err = desc->mode;
70 desc->mode = UBI_EXCLUSIVE;
71 }
72 spin_unlock(&vol->ubi->volumes_lock);
73
74 return err;
75}
76
77/**
78 * revoke_exclusive - revoke exclusive mode.
79 * @desc: volume descriptor
80 * @mode: new mode to switch to
81 */
82static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
83{
84 struct ubi_volume *vol = desc->vol;
85
86 spin_lock(&vol->ubi->volumes_lock);
87 ubi_assert(vol->readers == 0 && vol->writers == 0);
88 ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
89 vol->exclusive = 0;
90 if (mode == UBI_READONLY)
91 vol->readers = 1;
92 else if (mode == UBI_READWRITE)
93 vol->writers = 1;
94 else
95 vol->exclusive = 1;
96 spin_unlock(&vol->ubi->volumes_lock);
97
98 desc->mode = mode;
99}
100
101static int vol_cdev_open(struct inode *inode, struct file *file)
102{
103 struct ubi_volume_desc *desc;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200104 int vol_id = iminor(inode) - 1, mode, ubi_num;
105
106 ubi_num = ubi_major2num(imajor(inode));
107 if (ubi_num < 0)
108 return ubi_num;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400109
110 if (file->f_mode & FMODE_WRITE)
111 mode = UBI_READWRITE;
112 else
113 mode = UBI_READONLY;
114
115 dbg_msg("open volume %d, mode %d", vol_id, mode);
116
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200117 desc = ubi_open_volume(ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400118 if (IS_ERR(desc))
119 return PTR_ERR(desc);
120
121 file->private_data = desc;
122 return 0;
123}
124
125static int vol_cdev_release(struct inode *inode, struct file *file)
126{
127 struct ubi_volume_desc *desc = file->private_data;
128 struct ubi_volume *vol = desc->vol;
129
130 dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
131
132 if (vol->updating) {
133 ubi_warn("update of volume %d not finished, volume is damaged",
134 vol->vol_id);
135 vol->updating = 0;
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300136 vfree(vol->upd_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400137 }
138
139 ubi_close_volume(desc);
140 return 0;
141}
142
143static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
144{
145 struct ubi_volume_desc *desc = file->private_data;
146 struct ubi_volume *vol = desc->vol;
147 loff_t new_offset;
148
149 if (vol->updating) {
150 /* Update is in progress, seeking is prohibited */
151 dbg_err("updating");
152 return -EBUSY;
153 }
154
155 switch (origin) {
156 case 0: /* SEEK_SET */
157 new_offset = offset;
158 break;
159 case 1: /* SEEK_CUR */
160 new_offset = file->f_pos + offset;
161 break;
162 case 2: /* SEEK_END */
163 new_offset = vol->used_bytes + offset;
164 break;
165 default:
166 return -EINVAL;
167 }
168
169 if (new_offset < 0 || new_offset > vol->used_bytes) {
170 dbg_err("bad seek %lld", new_offset);
171 return -EINVAL;
172 }
173
174 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
175 vol->vol_id, offset, origin, new_offset);
176
177 file->f_pos = new_offset;
178 return new_offset;
179}
180
181static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
182 loff_t *offp)
183{
184 struct ubi_volume_desc *desc = file->private_data;
185 struct ubi_volume *vol = desc->vol;
186 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200187 int err, lnum, off, len, tbuf_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400188 size_t count_save = count;
189 void *tbuf;
190 uint64_t tmp;
191
192 dbg_msg("read %zd bytes from offset %lld of volume %d",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200193 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400194
195 if (vol->updating) {
196 dbg_err("updating");
197 return -EBUSY;
198 }
199 if (vol->upd_marker) {
200 dbg_err("damaged volume, update marker is set");
201 return -EBADF;
202 }
203 if (*offp == vol->used_bytes || count == 0)
204 return 0;
205
206 if (vol->corrupted)
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200207 dbg_msg("read from corrupted volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208
209 if (*offp + count > vol->used_bytes)
210 count_save = count = vol->used_bytes - *offp;
211
212 tbuf_size = vol->usable_leb_size;
213 if (count < tbuf_size)
214 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300215 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400216 if (!tbuf)
217 return -ENOMEM;
218
219 len = count > tbuf_size ? tbuf_size : count;
220
221 tmp = *offp;
222 off = do_div(tmp, vol->usable_leb_size);
223 lnum = tmp;
224
225 do {
226 cond_resched();
227
228 if (off + len >= vol->usable_leb_size)
229 len = vol->usable_leb_size - off;
230
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200231 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400232 if (err)
233 break;
234
235 off += len;
236 if (off == vol->usable_leb_size) {
237 lnum += 1;
238 off -= vol->usable_leb_size;
239 }
240
241 count -= len;
242 *offp += len;
243
244 err = copy_to_user(buf, tbuf, len);
245 if (err) {
246 err = -EFAULT;
247 break;
248 }
249
250 buf += len;
251 len = count > tbuf_size ? tbuf_size : count;
252 } while (count);
253
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300254 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400255 return err ? err : count_save - count;
256}
257
258#ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
259
260/*
261 * This function allows to directly write to dynamic UBI volumes, without
262 * issuing the volume update operation. Available only as a debugging feature.
263 * Very useful for testing UBI.
264 */
265static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
266 size_t count, loff_t *offp)
267{
268 struct ubi_volume_desc *desc = file->private_data;
269 struct ubi_volume *vol = desc->vol;
270 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200271 int lnum, off, len, tbuf_size, err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272 size_t count_save = count;
273 char *tbuf;
274 uint64_t tmp;
275
276 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200277 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400278
279 if (vol->vol_type == UBI_STATIC_VOLUME)
280 return -EROFS;
281
282 tmp = *offp;
283 off = do_div(tmp, vol->usable_leb_size);
284 lnum = tmp;
285
286 if (off % ubi->min_io_size) {
287 dbg_err("unaligned position");
288 return -EINVAL;
289 }
290
291 if (*offp + count > vol->used_bytes)
292 count_save = count = vol->used_bytes - *offp;
293
294 /* We can write only in fractions of the minimum I/O unit */
295 if (count % ubi->min_io_size) {
296 dbg_err("unaligned write length");
297 return -EINVAL;
298 }
299
300 tbuf_size = vol->usable_leb_size;
301 if (count < tbuf_size)
302 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300303 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400304 if (!tbuf)
305 return -ENOMEM;
306
307 len = count > tbuf_size ? tbuf_size : count;
308
309 while (count) {
310 cond_resched();
311
312 if (off + len >= vol->usable_leb_size)
313 len = vol->usable_leb_size - off;
314
315 err = copy_from_user(tbuf, buf, len);
316 if (err) {
317 err = -EFAULT;
318 break;
319 }
320
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200321 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400322 UBI_UNKNOWN);
323 if (err)
324 break;
325
326 off += len;
327 if (off == vol->usable_leb_size) {
328 lnum += 1;
329 off -= vol->usable_leb_size;
330 }
331
332 count -= len;
333 *offp += len;
334 buf += len;
335 len = count > tbuf_size ? tbuf_size : count;
336 }
337
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300338 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400339 return err ? err : count_save - count;
340}
341
342#else
343#define vol_cdev_direct_write(file, buf, count, offp) -EPERM
344#endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
345
346static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
347 size_t count, loff_t *offp)
348{
349 int err = 0;
350 struct ubi_volume_desc *desc = file->private_data;
351 struct ubi_volume *vol = desc->vol;
352 struct ubi_device *ubi = vol->ubi;
353
354 if (!vol->updating)
355 return vol_cdev_direct_write(file, buf, count, offp);
356
Artem Bityutskiy1b68d0e2008-01-24 17:04:01 +0200357 err = ubi_more_update_data(ubi, vol, buf, count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400358 if (err < 0) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200359 ubi_err("cannot write %zd bytes of update data, error %d",
360 count, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400361 return err;
362 }
363
364 if (err) {
365 /*
366 * Update is finished, @err contains number of actually written
367 * bytes now.
368 */
369 count = err;
370
Artem Bityutskiy0411e732008-01-24 16:45:57 +0200371 vol->updating = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400372 err = ubi_check_volume(ubi, vol->vol_id);
373 if (err < 0)
374 return err;
375
376 if (err) {
377 ubi_warn("volume %d on UBI device %d is corrupted",
378 vol->vol_id, ubi->ubi_num);
379 vol->corrupted = 1;
380 }
381 vol->checked = 1;
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300382 ubi_gluebi_updated(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400383 revoke_exclusive(desc, UBI_READWRITE);
384 }
385
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400386 return count;
387}
388
389static int vol_cdev_ioctl(struct inode *inode, struct file *file,
390 unsigned int cmd, unsigned long arg)
391{
392 int err = 0;
393 struct ubi_volume_desc *desc = file->private_data;
394 struct ubi_volume *vol = desc->vol;
395 struct ubi_device *ubi = vol->ubi;
396 void __user *argp = (void __user *)arg;
397
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 switch (cmd) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400399 /* Volume update command */
400 case UBI_IOCVOLUP:
401 {
402 int64_t bytes, rsvd_bytes;
403
404 if (!capable(CAP_SYS_RESOURCE)) {
405 err = -EPERM;
406 break;
407 }
408
409 err = copy_from_user(&bytes, argp, sizeof(int64_t));
410 if (err) {
411 err = -EFAULT;
412 break;
413 }
414
415 if (desc->mode == UBI_READONLY) {
416 err = -EROFS;
417 break;
418 }
419
420 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
421 if (bytes < 0 || bytes > rsvd_bytes) {
422 err = -EINVAL;
423 break;
424 }
425
426 err = get_exclusive(desc);
427 if (err < 0)
428 break;
429
Artem Bityutskiy1b68d0e2008-01-24 17:04:01 +0200430 err = ubi_start_update(ubi, vol, bytes);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400431 if (bytes == 0)
432 revoke_exclusive(desc, UBI_READWRITE);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400433 break;
434 }
435
436#ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
437 /* Logical eraseblock erasure command */
438 case UBI_IOCEBER:
439 {
440 int32_t lnum;
441
Christoph Hellwigbf078032007-05-17 16:32:10 +0200442 err = get_user(lnum, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443 if (err) {
444 err = -EFAULT;
445 break;
446 }
447
448 if (desc->mode == UBI_READONLY) {
449 err = -EROFS;
450 break;
451 }
452
453 if (lnum < 0 || lnum >= vol->reserved_pebs) {
454 err = -EINVAL;
455 break;
456 }
457
458 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
459 err = -EROFS;
460 break;
461 }
462
463 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200464 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400465 if (err)
466 break;
467
468 err = ubi_wl_flush(ubi);
469 break;
470 }
471#endif
472
473 default:
474 err = -ENOTTY;
475 break;
476 }
477
478 return err;
479}
480
481/**
482 * verify_mkvol_req - verify volume creation request.
483 * @ubi: UBI device description object
484 * @req: the request to check
485 *
486 * This function zero if the request is correct, and %-EINVAL if not.
487 */
488static int verify_mkvol_req(const struct ubi_device *ubi,
489 const struct ubi_mkvol_req *req)
490{
491 int n, err = -EINVAL;
492
493 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
494 req->name_len < 0)
495 goto bad;
496
497 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
498 req->vol_id != UBI_VOL_NUM_AUTO)
499 goto bad;
500
501 if (req->alignment == 0)
502 goto bad;
503
504 if (req->bytes == 0)
505 goto bad;
506
507 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
508 req->vol_type != UBI_STATIC_VOLUME)
509 goto bad;
510
511 if (req->alignment > ubi->leb_size)
512 goto bad;
513
514 n = req->alignment % ubi->min_io_size;
515 if (req->alignment != 1 && n)
516 goto bad;
517
518 if (req->name_len > UBI_VOL_NAME_MAX) {
519 err = -ENAMETOOLONG;
520 goto bad;
521 }
522
523 return 0;
524
525bad:
526 dbg_err("bad volume creation request");
527 ubi_dbg_dump_mkvol_req(req);
528 return err;
529}
530
531/**
532 * verify_rsvol_req - verify volume re-size request.
533 * @ubi: UBI device description object
534 * @req: the request to check
535 *
536 * This function returns zero if the request is correct, and %-EINVAL if not.
537 */
538static int verify_rsvol_req(const struct ubi_device *ubi,
539 const struct ubi_rsvol_req *req)
540{
541 if (req->bytes <= 0)
542 return -EINVAL;
543
544 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
545 return -EINVAL;
546
547 return 0;
548}
549
550static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
551 unsigned int cmd, unsigned long arg)
552{
553 int err = 0;
554 struct ubi_device *ubi;
555 struct ubi_volume_desc *desc;
556 void __user *argp = (void __user *)arg;
557
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400558 if (!capable(CAP_SYS_RESOURCE))
559 return -EPERM;
560
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200561 ubi = ubi_get_by_major(imajor(inode));
562 if (!ubi)
563 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400564
565 switch (cmd) {
566 /* Create volume command */
567 case UBI_IOCMKVOL:
568 {
569 struct ubi_mkvol_req req;
570
571 dbg_msg("create volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200572 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400573 if (err) {
574 err = -EFAULT;
575 break;
576 }
577
578 err = verify_mkvol_req(ubi, &req);
579 if (err)
580 break;
581
582 req.name[req.name_len] = '\0';
583
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200584 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400585 err = ubi_create_volume(ubi, &req);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200586 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400587 if (err)
588 break;
589
Christoph Hellwigbf078032007-05-17 16:32:10 +0200590 err = put_user(req.vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400591 if (err)
592 err = -EFAULT;
593
594 break;
595 }
596
597 /* Remove volume command */
598 case UBI_IOCRMVOL:
599 {
600 int vol_id;
601
602 dbg_msg("remove volume");
Christoph Hellwigbf078032007-05-17 16:32:10 +0200603 err = get_user(vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400604 if (err) {
605 err = -EFAULT;
606 break;
607 }
608
609 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
610 if (IS_ERR(desc)) {
611 err = PTR_ERR(desc);
612 break;
613 }
614
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200615 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400616 err = ubi_remove_volume(desc);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200617 mutex_unlock(&ubi->volumes_mutex);
618
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200619 /*
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200620 * The volume is deleted (unless an error occurred), and the
621 * 'struct ubi_volume' object will be freed when
622 * 'ubi_close_volume()' will call 'put_device()'.
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200623 */
624 ubi_close_volume(desc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400625 break;
626 }
627
628 /* Re-size volume command */
629 case UBI_IOCRSVOL:
630 {
631 int pebs;
632 uint64_t tmp;
633 struct ubi_rsvol_req req;
634
635 dbg_msg("re-size volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200636 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400637 if (err) {
638 err = -EFAULT;
639 break;
640 }
641
642 err = verify_rsvol_req(ubi, &req);
643 if (err)
644 break;
645
646 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
647 if (IS_ERR(desc)) {
648 err = PTR_ERR(desc);
649 break;
650 }
651
652 tmp = req.bytes;
653 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
654 pebs += tmp;
655
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200656 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400657 err = ubi_resize_volume(desc, pebs);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200658 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400659 ubi_close_volume(desc);
660 break;
661 }
662
663 default:
664 err = -ENOTTY;
665 break;
666 }
667
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200668 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400669 return err;
670}
671
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200672static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
673 unsigned int cmd, unsigned long arg)
674{
675 int err = 0;
676 void __user *argp = (void __user *)arg;
677
678 if (!capable(CAP_SYS_RESOURCE))
679 return -EPERM;
680
681 switch (cmd) {
682 /* Attach an MTD device command */
683 case UBI_IOCATT:
684 {
685 struct ubi_attach_req req;
686 struct mtd_info *mtd;
687
688 dbg_msg("attach MTD device");
689 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
690 if (err) {
691 err = -EFAULT;
692 break;
693 }
694
695 if (req.mtd_num < 0 ||
696 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
697 err = -EINVAL;
698 break;
699 }
700
701 mtd = get_mtd_device(NULL, req.mtd_num);
702 if (IS_ERR(mtd)) {
703 err = PTR_ERR(mtd);
704 break;
705 }
706
707 /*
708 * Note, further request verification is done by
709 * 'ubi_attach_mtd_dev()'.
710 */
711 mutex_lock(&ubi_devices_mutex);
712 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
713 mutex_unlock(&ubi_devices_mutex);
714 if (err < 0)
715 put_mtd_device(mtd);
716 else
717 /* @err contains UBI device number */
718 err = put_user(err, (__user int32_t *)argp);
719
720 break;
721 }
722
723 /* Detach an MTD device command */
724 case UBI_IOCDET:
725 {
726 int ubi_num;
727
728 dbg_msg("dettach MTD device");
729 err = get_user(ubi_num, (__user int32_t *)argp);
730 if (err) {
731 err = -EFAULT;
732 break;
733 }
734
735 mutex_lock(&ubi_devices_mutex);
736 err = ubi_detach_mtd_dev(ubi_num, 0);
737 mutex_unlock(&ubi_devices_mutex);
738 break;
739 }
740
741 default:
742 err = -ENOTTY;
743 break;
744 }
745
746 return err;
747}
748
Artem Bityutskiy9f961b52007-12-16 16:59:31 +0200749/* UBI control character device operations */
750struct file_operations ubi_ctrl_cdev_operations = {
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200751 .ioctl = ctrl_cdev_ioctl,
Artem Bityutskiy9f961b52007-12-16 16:59:31 +0200752 .owner = THIS_MODULE,
753};
754
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400755/* UBI character device operations */
756struct file_operations ubi_cdev_operations = {
757 .owner = THIS_MODULE,
758 .ioctl = ubi_cdev_ioctl,
Artem Bityutskiyf800f092007-05-06 16:45:43 +0300759 .llseek = no_llseek,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400760};
761
762/* UBI volume character device operations */
763struct file_operations ubi_vol_cdev_operations = {
764 .owner = THIS_MODULE,
765 .open = vol_cdev_open,
766 .release = vol_cdev_release,
767 .llseek = vol_cdev_llseek,
768 .read = vol_cdev_read,
769 .write = vol_cdev_write,
Artem Bityutskiyf800f092007-05-06 16:45:43 +0300770 .ioctl = vol_cdev_ioctl,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400771};