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_MAX_DEVICES; i++)
64 if (ubi_devices[i] && MAJOR(ubi_devices[i]->cdev.dev) == major)
65 return ubi_devices[i];
71 * get_exclusive - get exclusive access to an UBI volume.
72 * @desc: volume descriptor
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
78 static int get_exclusive(struct ubi_volume_desc *desc)
81 struct ubi_volume *vol = desc->vol;
83 spin_lock(&vol->ubi->volumes_lock);
84 users = vol->readers + vol->writers + vol->exclusive;
85 ubi_assert(users > 0);
87 dbg_err("%d users for volume %d", users, vol->vol_id);
90 vol->readers = vol->writers = 0;
93 desc->mode = UBI_EXCLUSIVE;
95 spin_unlock(&vol->ubi->volumes_lock);
101 * revoke_exclusive - revoke exclusive mode.
102 * @desc: volume descriptor
103 * @mode: new mode to switch to
105 static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
107 struct ubi_volume *vol = desc->vol;
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);
113 if (mode == UBI_READONLY)
115 else if (mode == UBI_READWRITE)
119 spin_unlock(&vol->ubi->volumes_lock);
124 static int vol_cdev_open(struct inode *inode, struct file *file)
126 struct ubi_volume_desc *desc;
127 const struct ubi_device *ubi = major_to_device(imajor(inode));
128 int vol_id = iminor(inode) - 1;
131 if (file->f_mode & FMODE_WRITE)
132 mode = UBI_READWRITE;
136 dbg_msg("open volume %d, mode %d", vol_id, mode);
138 desc = ubi_open_volume(ubi->ubi_num, vol_id, mode);
140 return PTR_ERR(desc);
142 file->private_data = desc;
146 static int vol_cdev_release(struct inode *inode, struct file *file)
148 struct ubi_volume_desc *desc = file->private_data;
149 struct ubi_volume *vol = desc->vol;
151 dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
154 ubi_warn("update of volume %d not finished, volume is damaged",
160 ubi_close_volume(desc);
164 static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
166 struct ubi_volume_desc *desc = file->private_data;
167 struct ubi_volume *vol = desc->vol;
171 /* Update is in progress, seeking is prohibited */
177 case 0: /* SEEK_SET */
180 case 1: /* SEEK_CUR */
181 new_offset = file->f_pos + offset;
183 case 2: /* SEEK_END */
184 new_offset = vol->used_bytes + offset;
190 if (new_offset < 0 || new_offset > vol->used_bytes) {
191 dbg_err("bad seek %lld", new_offset);
195 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
196 vol->vol_id, offset, origin, new_offset);
198 file->f_pos = new_offset;
202 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
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;
213 dbg_msg("read %zd bytes from offset %lld of volume %d",
214 count, *offp, vol_id);
220 if (vol->upd_marker) {
221 dbg_err("damaged volume, update marker is set");
224 if (*offp == vol->used_bytes || count == 0)
228 dbg_msg("read from corrupted volume %d", vol_id);
230 if (*offp + count > vol->used_bytes)
231 count_save = count = vol->used_bytes - *offp;
233 tbuf_size = vol->usable_leb_size;
234 if (count < tbuf_size)
235 tbuf_size = ALIGN(count, ubi->min_io_size);
236 tbuf = vmalloc(tbuf_size);
240 len = count > tbuf_size ? tbuf_size : count;
243 off = do_div(tmp, vol->usable_leb_size);
249 if (off + len >= vol->usable_leb_size)
250 len = vol->usable_leb_size - off;
252 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
257 if (off == vol->usable_leb_size) {
259 off -= vol->usable_leb_size;
265 err = copy_to_user(buf, tbuf, len);
272 len = count > tbuf_size ? tbuf_size : count;
276 return err ? err : count_save - count;
279 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
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.
286 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
287 size_t count, loff_t *offp)
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;
297 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
298 count, *offp, desc->vol->vol_id);
300 if (vol->vol_type == UBI_STATIC_VOLUME)
304 off = do_div(tmp, vol->usable_leb_size);
307 if (off % ubi->min_io_size) {
308 dbg_err("unaligned position");
312 if (*offp + count > vol->used_bytes)
313 count_save = count = vol->used_bytes - *offp;
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");
321 tbuf_size = vol->usable_leb_size;
322 if (count < tbuf_size)
323 tbuf_size = ALIGN(count, ubi->min_io_size);
324 tbuf = vmalloc(tbuf_size);
328 len = count > tbuf_size ? tbuf_size : count;
333 if (off + len >= vol->usable_leb_size)
334 len = vol->usable_leb_size - off;
336 err = copy_from_user(tbuf, buf, len);
342 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
348 if (off == vol->usable_leb_size) {
350 off -= vol->usable_leb_size;
356 len = count > tbuf_size ? tbuf_size : count;
360 return err ? err : count_save - count;
364 #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
365 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
367 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
368 size_t count, loff_t *offp)
371 struct ubi_volume_desc *desc = file->private_data;
372 struct ubi_volume *vol = desc->vol;
373 struct ubi_device *ubi = vol->ubi;
376 return vol_cdev_direct_write(file, buf, count, offp);
378 err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
380 ubi_err("cannot write %zd bytes of update data, error %d",
387 * Update is finished, @err contains number of actually written
392 err = ubi_check_volume(ubi, vol->vol_id);
397 ubi_warn("volume %d on UBI device %d is corrupted",
398 vol->vol_id, ubi->ubi_num);
402 ubi_gluebi_updated(vol);
403 revoke_exclusive(desc, UBI_READWRITE);
410 static int vol_cdev_ioctl(struct inode *inode, struct file *file,
411 unsigned int cmd, unsigned long arg)
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;
420 /* Volume update command */
423 int64_t bytes, rsvd_bytes;
425 if (!capable(CAP_SYS_RESOURCE)) {
430 err = copy_from_user(&bytes, argp, sizeof(int64_t));
436 if (desc->mode == UBI_READONLY) {
441 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
442 if (bytes < 0 || bytes > rsvd_bytes) {
447 err = get_exclusive(desc);
451 err = ubi_start_update(ubi, vol->vol_id, bytes);
453 revoke_exclusive(desc, UBI_READWRITE);
459 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
460 /* Logical eraseblock erasure command */
465 err = get_user(lnum, (__user int32_t *)argp);
471 if (desc->mode == UBI_READONLY) {
476 if (lnum < 0 || lnum >= vol->reserved_pebs) {
481 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
486 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
487 err = ubi_eba_unmap_leb(ubi, vol, lnum);
491 err = ubi_wl_flush(ubi);
505 * verify_mkvol_req - verify volume creation request.
506 * @ubi: UBI device description object
507 * @req: the request to check
509 * This function zero if the request is correct, and %-EINVAL if not.
511 static int verify_mkvol_req(const struct ubi_device *ubi,
512 const struct ubi_mkvol_req *req)
514 int n, err = -EINVAL;
516 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
520 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
521 req->vol_id != UBI_VOL_NUM_AUTO)
524 if (req->alignment == 0)
530 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
531 req->vol_type != UBI_STATIC_VOLUME)
534 if (req->alignment > ubi->leb_size)
537 n = req->alignment % ubi->min_io_size;
538 if (req->alignment != 1 && n)
541 if (req->name_len > UBI_VOL_NAME_MAX) {
549 dbg_err("bad volume creation request");
550 ubi_dbg_dump_mkvol_req(req);
555 * verify_rsvol_req - verify volume re-size request.
556 * @ubi: UBI device description object
557 * @req: the request to check
559 * This function returns zero if the request is correct, and %-EINVAL if not.
561 static int verify_rsvol_req(const struct ubi_device *ubi,
562 const struct ubi_rsvol_req *req)
567 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
573 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
574 unsigned int cmd, unsigned long arg)
577 struct ubi_device *ubi;
578 struct ubi_volume_desc *desc;
579 void __user *argp = (void __user *)arg;
581 if (!capable(CAP_SYS_RESOURCE))
584 ubi = major_to_device(imajor(inode));
589 /* Create volume command */
592 struct ubi_mkvol_req req;
594 dbg_msg("create volume");
595 err = copy_from_user(&req, argp,
596 sizeof(struct ubi_mkvol_req));
602 err = verify_mkvol_req(ubi, &req);
606 req.name[req.name_len] = '\0';
608 mutex_lock(&ubi->volumes_mutex);
609 err = ubi_create_volume(ubi, &req);
610 mutex_unlock(&ubi->volumes_mutex);
614 err = put_user(req.vol_id, (__user int32_t *)argp);
621 /* Remove volume command */
626 dbg_msg("remove volume");
627 err = get_user(vol_id, (__user int32_t *)argp);
633 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
639 mutex_lock(&ubi->volumes_mutex);
640 err = ubi_remove_volume(desc);
641 mutex_unlock(&ubi->volumes_mutex);
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()'.
648 ubi_close_volume(desc);
652 /* Re-size volume command */
657 struct ubi_rsvol_req req;
659 dbg_msg("re-size volume");
660 err = copy_from_user(&req, argp,
661 sizeof(struct ubi_rsvol_req));
667 err = verify_rsvol_req(ubi, &req);
671 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
678 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
681 mutex_lock(&ubi->volumes_mutex);
682 err = ubi_resize_volume(desc, pebs);
683 mutex_unlock(&ubi->volumes_mutex);
684 ubi_close_volume(desc);
696 /* UBI character device operations */
697 struct file_operations ubi_cdev_operations = {
698 .owner = THIS_MODULE,
699 .ioctl = ubi_cdev_ioctl,
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,
711 .ioctl = vol_cdev_ioctl,