2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём)
22 * This file includes implementation of UBI character device operations.
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.
29 * Major and minor numbers are assigned dynamically to both UBI and volume
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>
43 * Maximum sequence numbers of UBI and volume character device IOCTLs (direct
44 * logical eraseblock erase is a debug-only feature).
46 #define UBI_CDEV_IOC_MAX_SEQ 2
47 #ifndef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
48 #define VOL_CDEV_IOC_MAX_SEQ 1
50 #define VOL_CDEV_IOC_MAX_SEQ 2
54 * major_to_device - get UBI device object by character device major number.
55 * @major: major number
57 * This function returns a pointer to the UBI device object.
59 static struct ubi_device
*major_to_device(int major
)
63 for (i
= 0; i
< ubi_devices_cnt
; i
++)
64 if (ubi_devices
[i
] && ubi_devices
[i
]->major
== major
)
65 return ubi_devices
[i
];
70 * get_exclusive - get exclusive access to an UBI volume.
71 * @desc: volume descriptor
73 * This function changes UBI volume open mode to "exclusive". Returns previous
74 * mode value (positive integer) in case of success and a negative error code
77 static int get_exclusive(struct ubi_volume_desc
*desc
)
80 struct ubi_volume
*vol
= desc
->vol
;
82 spin_lock(&vol
->ubi
->volumes_lock
);
83 users
= vol
->readers
+ vol
->writers
+ vol
->exclusive
;
84 ubi_assert(users
> 0);
86 dbg_err("%d users for volume %d", users
, vol
->vol_id
);
89 vol
->readers
= vol
->writers
= 0;
92 desc
->mode
= UBI_EXCLUSIVE
;
94 spin_unlock(&vol
->ubi
->volumes_lock
);
100 * revoke_exclusive - revoke exclusive mode.
101 * @desc: volume descriptor
102 * @mode: new mode to switch to
104 static void revoke_exclusive(struct ubi_volume_desc
*desc
, int mode
)
106 struct ubi_volume
*vol
= desc
->vol
;
108 spin_lock(&vol
->ubi
->volumes_lock
);
109 ubi_assert(vol
->readers
== 0 && vol
->writers
== 0);
110 ubi_assert(vol
->exclusive
== 1 && desc
->mode
== UBI_EXCLUSIVE
);
112 if (mode
== UBI_READONLY
)
114 else if (mode
== UBI_READWRITE
)
118 spin_unlock(&vol
->ubi
->volumes_lock
);
123 static int vol_cdev_open(struct inode
*inode
, struct file
*file
)
125 struct ubi_volume_desc
*desc
;
126 const struct ubi_device
*ubi
= major_to_device(imajor(inode
));
127 int vol_id
= iminor(inode
) - 1;
130 if (file
->f_mode
& FMODE_WRITE
)
131 mode
= UBI_READWRITE
;
135 dbg_msg("open volume %d, mode %d", vol_id
, mode
);
137 desc
= ubi_open_volume(ubi
->ubi_num
, vol_id
, mode
);
139 return PTR_ERR(desc
);
141 file
->private_data
= desc
;
145 static int vol_cdev_release(struct inode
*inode
, struct file
*file
)
147 struct ubi_volume_desc
*desc
= file
->private_data
;
148 struct ubi_volume
*vol
= desc
->vol
;
150 dbg_msg("release volume %d, mode %d", vol
->vol_id
, desc
->mode
);
153 ubi_warn("update of volume %d not finished, volume is damaged",
159 ubi_close_volume(desc
);
163 static loff_t
vol_cdev_llseek(struct file
*file
, loff_t offset
, int origin
)
165 struct ubi_volume_desc
*desc
= file
->private_data
;
166 struct ubi_volume
*vol
= desc
->vol
;
170 /* Update is in progress, seeking is prohibited */
176 case 0: /* SEEK_SET */
179 case 1: /* SEEK_CUR */
180 new_offset
= file
->f_pos
+ offset
;
182 case 2: /* SEEK_END */
183 new_offset
= vol
->used_bytes
+ offset
;
189 if (new_offset
< 0 || new_offset
> vol
->used_bytes
) {
190 dbg_err("bad seek %lld", new_offset
);
194 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
195 vol
->vol_id
, offset
, origin
, new_offset
);
197 file
->f_pos
= new_offset
;
201 static ssize_t
vol_cdev_read(struct file
*file
, __user
char *buf
, size_t count
,
204 struct ubi_volume_desc
*desc
= file
->private_data
;
205 struct ubi_volume
*vol
= desc
->vol
;
206 struct ubi_device
*ubi
= vol
->ubi
;
207 int err
, lnum
, off
, len
, vol_id
= desc
->vol
->vol_id
, tbuf_size
;
208 size_t count_save
= count
;
212 dbg_msg("read %zd bytes from offset %lld of volume %d",
213 count
, *offp
, vol_id
);
219 if (vol
->upd_marker
) {
220 dbg_err("damaged volume, update marker is set");
223 if (*offp
== vol
->used_bytes
|| count
== 0)
227 dbg_msg("read from corrupted volume %d", vol_id
);
229 if (*offp
+ count
> vol
->used_bytes
)
230 count_save
= count
= vol
->used_bytes
- *offp
;
232 tbuf_size
= vol
->usable_leb_size
;
233 if (count
< tbuf_size
)
234 tbuf_size
= ALIGN(count
, ubi
->min_io_size
);
235 tbuf
= vmalloc(tbuf_size
);
239 len
= count
> tbuf_size
? tbuf_size
: count
;
242 off
= do_div(tmp
, vol
->usable_leb_size
);
248 if (off
+ len
>= vol
->usable_leb_size
)
249 len
= vol
->usable_leb_size
- off
;
251 err
= ubi_eba_read_leb(ubi
, vol_id
, lnum
, tbuf
, off
, len
, 0);
256 if (off
== vol
->usable_leb_size
) {
258 off
-= vol
->usable_leb_size
;
264 err
= copy_to_user(buf
, tbuf
, len
);
271 len
= count
> tbuf_size
? tbuf_size
: count
;
275 return err
? err
: count_save
- count
;
278 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
281 * This function allows to directly write to dynamic UBI volumes, without
282 * issuing the volume update operation. Available only as a debugging feature.
283 * Very useful for testing UBI.
285 static ssize_t
vol_cdev_direct_write(struct file
*file
, const char __user
*buf
,
286 size_t count
, loff_t
*offp
)
288 struct ubi_volume_desc
*desc
= file
->private_data
;
289 struct ubi_volume
*vol
= desc
->vol
;
290 struct ubi_device
*ubi
= vol
->ubi
;
291 int lnum
, off
, len
, tbuf_size
, vol_id
= vol
->vol_id
, err
= 0;
292 size_t count_save
= count
;
296 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
297 count
, *offp
, desc
->vol
->vol_id
);
299 if (vol
->vol_type
== UBI_STATIC_VOLUME
)
303 off
= do_div(tmp
, vol
->usable_leb_size
);
306 if (off
% ubi
->min_io_size
) {
307 dbg_err("unaligned position");
311 if (*offp
+ count
> vol
->used_bytes
)
312 count_save
= count
= vol
->used_bytes
- *offp
;
314 /* We can write only in fractions of the minimum I/O unit */
315 if (count
% ubi
->min_io_size
) {
316 dbg_err("unaligned write length");
320 tbuf_size
= vol
->usable_leb_size
;
321 if (count
< tbuf_size
)
322 tbuf_size
= ALIGN(count
, ubi
->min_io_size
);
323 tbuf
= vmalloc(tbuf_size
);
327 len
= count
> tbuf_size
? tbuf_size
: count
;
332 if (off
+ len
>= vol
->usable_leb_size
)
333 len
= vol
->usable_leb_size
- off
;
335 err
= copy_from_user(tbuf
, buf
, len
);
341 err
= ubi_eba_write_leb(ubi
, vol_id
, lnum
, tbuf
, off
, len
,
347 if (off
== vol
->usable_leb_size
) {
349 off
-= vol
->usable_leb_size
;
355 len
= count
> tbuf_size
? tbuf_size
: count
;
359 return err
? err
: count_save
- count
;
363 #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
364 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
366 static ssize_t
vol_cdev_write(struct file
*file
, const char __user
*buf
,
367 size_t count
, loff_t
*offp
)
370 struct ubi_volume_desc
*desc
= file
->private_data
;
371 struct ubi_volume
*vol
= desc
->vol
;
372 struct ubi_device
*ubi
= vol
->ubi
;
375 return vol_cdev_direct_write(file
, buf
, count
, offp
);
377 err
= ubi_more_update_data(ubi
, vol
->vol_id
, buf
, count
);
379 ubi_err("cannot write %zd bytes of update data", count
);
385 * Update is finished, @err contains number of actually written
390 err
= ubi_check_volume(ubi
, vol
->vol_id
);
395 ubi_warn("volume %d on UBI device %d is corrupted",
396 vol
->vol_id
, ubi
->ubi_num
);
400 ubi_gluebi_updated(vol
);
401 revoke_exclusive(desc
, UBI_READWRITE
);
408 static int vol_cdev_ioctl(struct inode
*inode
, struct file
*file
,
409 unsigned int cmd
, unsigned long arg
)
412 struct ubi_volume_desc
*desc
= file
->private_data
;
413 struct ubi_volume
*vol
= desc
->vol
;
414 struct ubi_device
*ubi
= vol
->ubi
;
415 void __user
*argp
= (void __user
*)arg
;
417 if (_IOC_NR(cmd
) > VOL_CDEV_IOC_MAX_SEQ
||
418 _IOC_TYPE(cmd
) != UBI_VOL_IOC_MAGIC
)
421 if (_IOC_DIR(cmd
) && _IOC_READ
)
422 err
= !access_ok(VERIFY_WRITE
, argp
, _IOC_SIZE(cmd
));
423 else if (_IOC_DIR(cmd
) && _IOC_WRITE
)
424 err
= !access_ok(VERIFY_READ
, argp
, _IOC_SIZE(cmd
));
430 /* Volume update command */
433 int64_t bytes
, rsvd_bytes
;
435 if (!capable(CAP_SYS_RESOURCE
)) {
440 err
= copy_from_user(&bytes
, argp
, sizeof(int64_t));
446 if (desc
->mode
== UBI_READONLY
) {
451 rsvd_bytes
= vol
->reserved_pebs
* (ubi
->leb_size
-vol
->data_pad
);
452 if (bytes
< 0 || bytes
> rsvd_bytes
) {
457 err
= get_exclusive(desc
);
461 err
= ubi_start_update(ubi
, vol
->vol_id
, bytes
);
463 revoke_exclusive(desc
, UBI_READWRITE
);
469 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
470 /* Logical eraseblock erasure command */
475 err
= __get_user(lnum
, (__user
int32_t *)argp
);
481 if (desc
->mode
== UBI_READONLY
) {
486 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
) {
491 if (vol
->vol_type
!= UBI_DYNAMIC_VOLUME
) {
496 dbg_msg("erase LEB %d:%d", vol
->vol_id
, lnum
);
497 err
= ubi_eba_unmap_leb(ubi
, vol
->vol_id
, lnum
);
501 err
= ubi_wl_flush(ubi
);
515 * verify_mkvol_req - verify volume creation request.
516 * @ubi: UBI device description object
517 * @req: the request to check
519 * This function zero if the request is correct, and %-EINVAL if not.
521 static int verify_mkvol_req(const struct ubi_device
*ubi
,
522 const struct ubi_mkvol_req
*req
)
524 int n
, err
= -EINVAL
;
526 if (req
->bytes
< 0 || req
->alignment
< 0 || req
->vol_type
< 0 ||
530 if ((req
->vol_id
< 0 || req
->vol_id
>= ubi
->vtbl_slots
) &&
531 req
->vol_id
!= UBI_VOL_NUM_AUTO
)
534 if (req
->alignment
== 0)
540 if (req
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
541 req
->vol_type
!= UBI_STATIC_VOLUME
)
544 if (req
->alignment
> ubi
->leb_size
)
547 n
= req
->alignment
% ubi
->min_io_size
;
548 if (req
->alignment
!= 1 && n
)
551 if (req
->name_len
> UBI_VOL_NAME_MAX
) {
559 dbg_err("bad volume creation request");
560 ubi_dbg_dump_mkvol_req(req
);
565 * verify_rsvol_req - verify volume re-size request.
566 * @ubi: UBI device description object
567 * @req: the request to check
569 * This function returns zero if the request is correct, and %-EINVAL if not.
571 static int verify_rsvol_req(const struct ubi_device
*ubi
,
572 const struct ubi_rsvol_req
*req
)
577 if (req
->vol_id
< 0 || req
->vol_id
>= ubi
->vtbl_slots
)
583 static int ubi_cdev_ioctl(struct inode
*inode
, struct file
*file
,
584 unsigned int cmd
, unsigned long arg
)
587 struct ubi_device
*ubi
;
588 struct ubi_volume_desc
*desc
;
589 void __user
*argp
= (void __user
*)arg
;
591 if (_IOC_NR(cmd
) > UBI_CDEV_IOC_MAX_SEQ
||
592 _IOC_TYPE(cmd
) != UBI_IOC_MAGIC
)
595 if (_IOC_DIR(cmd
) && _IOC_READ
)
596 err
= !access_ok(VERIFY_WRITE
, argp
, _IOC_SIZE(cmd
));
597 else if (_IOC_DIR(cmd
) && _IOC_WRITE
)
598 err
= !access_ok(VERIFY_READ
, argp
, _IOC_SIZE(cmd
));
602 if (!capable(CAP_SYS_RESOURCE
))
605 ubi
= major_to_device(imajor(inode
));
610 /* Create volume command */
613 struct ubi_mkvol_req req
;
615 dbg_msg("create volume");
616 err
= __copy_from_user(&req
, argp
,
617 sizeof(struct ubi_mkvol_req
));
623 err
= verify_mkvol_req(ubi
, &req
);
627 req
.name
[req
.name_len
] = '\0';
629 err
= ubi_create_volume(ubi
, &req
);
633 err
= __put_user(req
.vol_id
, (__user
int32_t *)argp
);
640 /* Remove volume command */
645 dbg_msg("remove volume");
646 err
= __get_user(vol_id
, (__user
int32_t *)argp
);
652 desc
= ubi_open_volume(ubi
->ubi_num
, vol_id
, UBI_EXCLUSIVE
);
658 err
= ubi_remove_volume(desc
);
660 ubi_close_volume(desc
);
665 /* Re-size volume command */
670 struct ubi_rsvol_req req
;
672 dbg_msg("re-size volume");
673 err
= __copy_from_user(&req
, argp
,
674 sizeof(struct ubi_rsvol_req
));
680 err
= verify_rsvol_req(ubi
, &req
);
684 desc
= ubi_open_volume(ubi
->ubi_num
, req
.vol_id
, UBI_EXCLUSIVE
);
691 pebs
= !!do_div(tmp
, desc
->vol
->usable_leb_size
);
694 err
= ubi_resize_volume(desc
, pebs
);
695 ubi_close_volume(desc
);
707 /* UBI character device operations */
708 struct file_operations ubi_cdev_operations
= {
709 .owner
= THIS_MODULE
,
710 .ioctl
= ubi_cdev_ioctl
,
714 /* UBI volume character device operations */
715 struct file_operations ubi_vol_cdev_operations
= {
716 .owner
= THIS_MODULE
,
717 .open
= vol_cdev_open
,
718 .release
= vol_cdev_release
,
719 .llseek
= vol_cdev_llseek
,
720 .read
= vol_cdev_read
,
721 .write
= vol_cdev_write
,
722 .ioctl
= vol_cdev_ioctl