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
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
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>
48 * Maximum sequence numbers of UBI and volume character device IOCTLs (direct
49 * logical eraseblock erase is a debug-only feature).
51 #define UBI_CDEV_IOC_MAX_SEQ 2
52 #ifndef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
53 #define VOL_CDEV_IOC_MAX_SEQ 1
55 #define VOL_CDEV_IOC_MAX_SEQ 2
59 * major_to_device - get UBI device object by character device major number.
60 * @major: major number
62 * This function returns a pointer to the UBI device object.
64 static struct ubi_device *major_to_device(int major)
68 for (i = 0; i < UBI_MAX_DEVICES; i++)
69 if (ubi_devices[i] && MAJOR(ubi_devices[i]->cdev.dev) == major)
70 return ubi_devices[i];
76 * get_exclusive - get exclusive access to an UBI volume.
77 * @desc: volume descriptor
79 * This function changes UBI volume open mode to "exclusive". Returns previous
80 * mode value (positive integer) in case of success and a negative error code
83 static int get_exclusive(struct ubi_volume_desc *desc)
86 struct ubi_volume *vol = desc->vol;
88 spin_lock(&vol->ubi->volumes_lock);
89 users = vol->readers + vol->writers + vol->exclusive;
90 ubi_assert(users > 0);
92 dbg_err("%d users for volume %d", users, vol->vol_id);
95 vol->readers = vol->writers = 0;
98 desc->mode = UBI_EXCLUSIVE;
100 spin_unlock(&vol->ubi->volumes_lock);
106 * revoke_exclusive - revoke exclusive mode.
107 * @desc: volume descriptor
108 * @mode: new mode to switch to
110 static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
112 struct ubi_volume *vol = desc->vol;
114 spin_lock(&vol->ubi->volumes_lock);
115 ubi_assert(vol->readers == 0 && vol->writers == 0);
116 ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
118 if (mode == UBI_READONLY)
120 else if (mode == UBI_READWRITE)
124 spin_unlock(&vol->ubi->volumes_lock);
129 static int vol_cdev_open(struct inode *inode, struct file *file)
131 struct ubi_volume_desc *desc;
132 const struct ubi_device *ubi = major_to_device(imajor(inode));
133 int vol_id = iminor(inode) - 1;
136 if (file->f_mode & FMODE_WRITE)
137 mode = UBI_READWRITE;
141 dbg_msg("open volume %d, mode %d", vol_id, mode);
143 desc = ubi_open_volume(ubi->ubi_num, vol_id, mode);
145 return PTR_ERR(desc);
147 file->private_data = desc;
151 static int vol_cdev_release(struct inode *inode, struct file *file)
153 struct ubi_volume_desc *desc = file->private_data;
154 struct ubi_volume *vol = desc->vol;
156 dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
159 ubi_warn("update of volume %d not finished, volume is damaged",
165 ubi_close_volume(desc);
169 static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
171 struct ubi_volume_desc *desc = file->private_data;
172 struct ubi_volume *vol = desc->vol;
176 /* Update is in progress, seeking is prohibited */
182 case 0: /* SEEK_SET */
185 case 1: /* SEEK_CUR */
186 new_offset = file->f_pos + offset;
188 case 2: /* SEEK_END */
189 new_offset = vol->used_bytes + offset;
195 if (new_offset < 0 || new_offset > vol->used_bytes) {
196 dbg_err("bad seek %lld", new_offset);
200 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
201 vol->vol_id, offset, origin, new_offset);
203 file->f_pos = new_offset;
207 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
210 struct ubi_volume_desc *desc = file->private_data;
211 struct ubi_volume *vol = desc->vol;
212 struct ubi_device *ubi = vol->ubi;
213 int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size;
214 size_t count_save = count;
218 dbg_msg("read %zd bytes from offset %lld of volume %d",
219 count, *offp, vol_id);
225 if (vol->upd_marker) {
226 dbg_err("damaged volume, update marker is set");
229 if (*offp == vol->used_bytes || count == 0)
233 dbg_msg("read from corrupted volume %d", vol_id);
235 if (*offp + count > vol->used_bytes)
236 count_save = count = vol->used_bytes - *offp;
238 tbuf_size = vol->usable_leb_size;
239 if (count < tbuf_size)
240 tbuf_size = ALIGN(count, ubi->min_io_size);
241 tbuf = vmalloc(tbuf_size);
245 len = count > tbuf_size ? tbuf_size : count;
248 off = do_div(tmp, vol->usable_leb_size);
254 if (off + len >= vol->usable_leb_size)
255 len = vol->usable_leb_size - off;
257 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
262 if (off == vol->usable_leb_size) {
264 off -= vol->usable_leb_size;
270 err = copy_to_user(buf, tbuf, len);
277 len = count > tbuf_size ? tbuf_size : count;
281 return err ? err : count_save - count;
284 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
287 * This function allows to directly write to dynamic UBI volumes, without
288 * issuing the volume update operation. Available only as a debugging feature.
289 * Very useful for testing UBI.
291 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
292 size_t count, loff_t *offp)
294 struct ubi_volume_desc *desc = file->private_data;
295 struct ubi_volume *vol = desc->vol;
296 struct ubi_device *ubi = vol->ubi;
297 int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err = 0;
298 size_t count_save = count;
302 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
303 count, *offp, desc->vol->vol_id);
305 if (vol->vol_type == UBI_STATIC_VOLUME)
309 off = do_div(tmp, vol->usable_leb_size);
312 if (off % ubi->min_io_size) {
313 dbg_err("unaligned position");
317 if (*offp + count > vol->used_bytes)
318 count_save = count = vol->used_bytes - *offp;
320 /* We can write only in fractions of the minimum I/O unit */
321 if (count % ubi->min_io_size) {
322 dbg_err("unaligned write length");
326 tbuf_size = vol->usable_leb_size;
327 if (count < tbuf_size)
328 tbuf_size = ALIGN(count, ubi->min_io_size);
329 tbuf = vmalloc(tbuf_size);
333 len = count > tbuf_size ? tbuf_size : count;
338 if (off + len >= vol->usable_leb_size)
339 len = vol->usable_leb_size - off;
341 err = copy_from_user(tbuf, buf, len);
347 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
353 if (off == vol->usable_leb_size) {
355 off -= vol->usable_leb_size;
361 len = count > tbuf_size ? tbuf_size : count;
365 return err ? err : count_save - count;
369 #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
370 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
372 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
373 size_t count, loff_t *offp)
376 struct ubi_volume_desc *desc = file->private_data;
377 struct ubi_volume *vol = desc->vol;
378 struct ubi_device *ubi = vol->ubi;
381 return vol_cdev_direct_write(file, buf, count, offp);
383 err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
385 ubi_err("cannot write %zd bytes of update data, error %d",
392 * Update is finished, @err contains number of actually written
397 err = ubi_check_volume(ubi, vol->vol_id);
402 ubi_warn("volume %d on UBI device %d is corrupted",
403 vol->vol_id, ubi->ubi_num);
407 ubi_gluebi_updated(vol);
408 revoke_exclusive(desc, UBI_READWRITE);
415 static int vol_cdev_ioctl(struct inode *inode, struct file *file,
416 unsigned int cmd, unsigned long arg)
419 struct ubi_volume_desc *desc = file->private_data;
420 struct ubi_volume *vol = desc->vol;
421 struct ubi_device *ubi = vol->ubi;
422 void __user *argp = (void __user *)arg;
425 /* Volume update command */
428 int64_t bytes, rsvd_bytes;
430 if (!capable(CAP_SYS_RESOURCE)) {
435 err = copy_from_user(&bytes, argp, sizeof(int64_t));
441 if (desc->mode == UBI_READONLY) {
446 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
447 if (bytes < 0 || bytes > rsvd_bytes) {
452 err = get_exclusive(desc);
456 err = ubi_start_update(ubi, vol->vol_id, bytes);
458 revoke_exclusive(desc, UBI_READWRITE);
464 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
465 /* Logical eraseblock erasure command */
470 err = get_user(lnum, (__user int32_t *)argp);
476 if (desc->mode == UBI_READONLY) {
481 if (lnum < 0 || lnum >= vol->reserved_pebs) {
486 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
491 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
492 err = ubi_eba_unmap_leb(ubi, vol, lnum);
496 err = ubi_wl_flush(ubi);
510 * verify_mkvol_req - verify volume creation request.
511 * @ubi: UBI device description object
512 * @req: the request to check
514 * This function zero if the request is correct, and %-EINVAL if not.
516 static int verify_mkvol_req(const struct ubi_device *ubi,
517 const struct ubi_mkvol_req *req)
519 int n, err = -EINVAL;
521 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
525 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
526 req->vol_id != UBI_VOL_NUM_AUTO)
529 if (req->alignment == 0)
535 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
536 req->vol_type != UBI_STATIC_VOLUME)
539 if (req->alignment > ubi->leb_size)
542 n = req->alignment % ubi->min_io_size;
543 if (req->alignment != 1 && n)
546 if (req->name_len > UBI_VOL_NAME_MAX) {
554 dbg_err("bad volume creation request");
555 ubi_dbg_dump_mkvol_req(req);
560 * verify_rsvol_req - verify volume re-size request.
561 * @ubi: UBI device description object
562 * @req: the request to check
564 * This function returns zero if the request is correct, and %-EINVAL if not.
566 static int verify_rsvol_req(const struct ubi_device *ubi,
567 const struct ubi_rsvol_req *req)
572 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
578 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
579 unsigned int cmd, unsigned long arg)
582 struct ubi_device *ubi;
583 struct ubi_volume_desc *desc;
584 void __user *argp = (void __user *)arg;
586 if (!capable(CAP_SYS_RESOURCE))
589 ubi = major_to_device(imajor(inode));
594 /* Create volume command */
597 struct ubi_mkvol_req req;
599 dbg_msg("create volume");
600 err = copy_from_user(&req, argp,
601 sizeof(struct ubi_mkvol_req));
607 err = verify_mkvol_req(ubi, &req);
611 req.name[req.name_len] = '\0';
613 mutex_lock(&ubi->volumes_mutex);
614 err = ubi_create_volume(ubi, &req);
615 mutex_unlock(&ubi->volumes_mutex);
619 err = put_user(req.vol_id, (__user int32_t *)argp);
626 /* Remove volume command */
631 dbg_msg("remove volume");
632 err = get_user(vol_id, (__user int32_t *)argp);
638 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
644 mutex_lock(&ubi->volumes_mutex);
645 err = ubi_remove_volume(desc);
646 mutex_unlock(&ubi->volumes_mutex);
649 * The volume is deleted (unless an error occurred), and the
650 * 'struct ubi_volume' object will be freed when
651 * 'ubi_close_volume()' will call 'put_device()'.
653 ubi_close_volume(desc);
657 /* Re-size volume command */
662 struct ubi_rsvol_req req;
664 dbg_msg("re-size volume");
665 err = copy_from_user(&req, argp,
666 sizeof(struct ubi_rsvol_req));
672 err = verify_rsvol_req(ubi, &req);
676 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
683 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
686 mutex_lock(&ubi->volumes_mutex);
687 err = ubi_resize_volume(desc, pebs);
688 mutex_unlock(&ubi->volumes_mutex);
689 ubi_close_volume(desc);
701 /* UBI control character device operations */
702 struct file_operations ubi_ctrl_cdev_operations = {
703 .owner = THIS_MODULE,
706 /* UBI character device operations */
707 struct file_operations ubi_cdev_operations = {
708 .owner = THIS_MODULE,
709 .ioctl = ubi_cdev_ioctl,
713 /* UBI volume character device operations */
714 struct file_operations ubi_vol_cdev_operations = {
715 .owner = THIS_MODULE,
716 .open = vol_cdev_open,
717 .release = vol_cdev_release,
718 .llseek = vol_cdev_llseek,
719 .read = vol_cdev_read,
720 .write = vol_cdev_write,
721 .ioctl = vol_cdev_ioctl,