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 * get_exclusive - get exclusive access to an UBI volume.
49 * @desc: volume descriptor
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
55 static int get_exclusive(struct ubi_volume_desc *desc)
58 struct ubi_volume *vol = desc->vol;
60 spin_lock(&vol->ubi->volumes_lock);
61 users = vol->readers + vol->writers + vol->exclusive;
62 ubi_assert(users > 0);
64 dbg_err("%d users for volume %d", users, vol->vol_id);
67 vol->readers = vol->writers = 0;
70 desc->mode = UBI_EXCLUSIVE;
72 spin_unlock(&vol->ubi->volumes_lock);
78 * revoke_exclusive - revoke exclusive mode.
79 * @desc: volume descriptor
80 * @mode: new mode to switch to
82 static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
84 struct ubi_volume *vol = desc->vol;
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);
90 if (mode == UBI_READONLY)
92 else if (mode == UBI_READWRITE)
96 spin_unlock(&vol->ubi->volumes_lock);
101 static int vol_cdev_open(struct inode *inode, struct file *file)
103 struct ubi_volume_desc *desc;
104 int vol_id = iminor(inode) - 1, mode, ubi_num;
106 ubi_num = ubi_major2num(imajor(inode));
110 if (file->f_mode & FMODE_WRITE)
111 mode = UBI_READWRITE;
115 dbg_msg("open volume %d, mode %d", vol_id, mode);
117 desc = ubi_open_volume(ubi_num, vol_id, mode);
119 return PTR_ERR(desc);
121 file->private_data = desc;
125 static int vol_cdev_release(struct inode *inode, struct file *file)
127 struct ubi_volume_desc *desc = file->private_data;
128 struct ubi_volume *vol = desc->vol;
130 dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
133 ubi_warn("update of volume %d not finished, volume is damaged",
139 ubi_close_volume(desc);
143 static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
145 struct ubi_volume_desc *desc = file->private_data;
146 struct ubi_volume *vol = desc->vol;
150 /* Update is in progress, seeking is prohibited */
156 case 0: /* SEEK_SET */
159 case 1: /* SEEK_CUR */
160 new_offset = file->f_pos + offset;
162 case 2: /* SEEK_END */
163 new_offset = vol->used_bytes + offset;
169 if (new_offset < 0 || new_offset > vol->used_bytes) {
170 dbg_err("bad seek %lld", new_offset);
174 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
175 vol->vol_id, offset, origin, new_offset);
177 file->f_pos = new_offset;
181 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
184 struct ubi_volume_desc *desc = file->private_data;
185 struct ubi_volume *vol = desc->vol;
186 struct ubi_device *ubi = vol->ubi;
187 int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size;
188 size_t count_save = count;
192 dbg_msg("read %zd bytes from offset %lld of volume %d",
193 count, *offp, vol_id);
199 if (vol->upd_marker) {
200 dbg_err("damaged volume, update marker is set");
203 if (*offp == vol->used_bytes || count == 0)
207 dbg_msg("read from corrupted volume %d", vol_id);
209 if (*offp + count > vol->used_bytes)
210 count_save = count = vol->used_bytes - *offp;
212 tbuf_size = vol->usable_leb_size;
213 if (count < tbuf_size)
214 tbuf_size = ALIGN(count, ubi->min_io_size);
215 tbuf = vmalloc(tbuf_size);
219 len = count > tbuf_size ? tbuf_size : count;
222 off = do_div(tmp, vol->usable_leb_size);
228 if (off + len >= vol->usable_leb_size)
229 len = vol->usable_leb_size - off;
231 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
236 if (off == vol->usable_leb_size) {
238 off -= vol->usable_leb_size;
244 err = copy_to_user(buf, tbuf, len);
251 len = count > tbuf_size ? tbuf_size : count;
255 return err ? err : count_save - count;
258 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
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.
265 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
266 size_t count, loff_t *offp)
268 struct ubi_volume_desc *desc = file->private_data;
269 struct ubi_volume *vol = desc->vol;
270 struct ubi_device *ubi = vol->ubi;
271 int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err = 0;
272 size_t count_save = count;
276 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
277 count, *offp, desc->vol->vol_id);
279 if (vol->vol_type == UBI_STATIC_VOLUME)
283 off = do_div(tmp, vol->usable_leb_size);
286 if (off % ubi->min_io_size) {
287 dbg_err("unaligned position");
291 if (*offp + count > vol->used_bytes)
292 count_save = count = vol->used_bytes - *offp;
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");
300 tbuf_size = vol->usable_leb_size;
301 if (count < tbuf_size)
302 tbuf_size = ALIGN(count, ubi->min_io_size);
303 tbuf = vmalloc(tbuf_size);
307 len = count > tbuf_size ? tbuf_size : count;
312 if (off + len >= vol->usable_leb_size)
313 len = vol->usable_leb_size - off;
315 err = copy_from_user(tbuf, buf, len);
321 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
327 if (off == vol->usable_leb_size) {
329 off -= vol->usable_leb_size;
335 len = count > tbuf_size ? tbuf_size : count;
339 return err ? err : count_save - count;
343 #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
344 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
346 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
347 size_t count, loff_t *offp)
350 struct ubi_volume_desc *desc = file->private_data;
351 struct ubi_volume *vol = desc->vol;
352 struct ubi_device *ubi = vol->ubi;
355 return vol_cdev_direct_write(file, buf, count, offp);
357 err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
359 ubi_err("cannot write %zd bytes of update data, error %d",
366 * Update is finished, @err contains number of actually written
371 err = ubi_check_volume(ubi, vol->vol_id);
376 ubi_warn("volume %d on UBI device %d is corrupted",
377 vol->vol_id, ubi->ubi_num);
381 ubi_gluebi_updated(vol);
382 revoke_exclusive(desc, UBI_READWRITE);
389 static int vol_cdev_ioctl(struct inode *inode, struct file *file,
390 unsigned int cmd, unsigned long arg)
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;
399 /* Volume update command */
402 int64_t bytes, rsvd_bytes;
404 if (!capable(CAP_SYS_RESOURCE)) {
409 err = copy_from_user(&bytes, argp, sizeof(int64_t));
415 if (desc->mode == UBI_READONLY) {
420 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
421 if (bytes < 0 || bytes > rsvd_bytes) {
426 err = get_exclusive(desc);
430 err = ubi_start_update(ubi, vol->vol_id, bytes);
432 revoke_exclusive(desc, UBI_READWRITE);
438 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
439 /* Logical eraseblock erasure command */
444 err = get_user(lnum, (__user int32_t *)argp);
450 if (desc->mode == UBI_READONLY) {
455 if (lnum < 0 || lnum >= vol->reserved_pebs) {
460 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
465 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
466 err = ubi_eba_unmap_leb(ubi, vol, lnum);
470 err = ubi_wl_flush(ubi);
484 * verify_mkvol_req - verify volume creation request.
485 * @ubi: UBI device description object
486 * @req: the request to check
488 * This function zero if the request is correct, and %-EINVAL if not.
490 static int verify_mkvol_req(const struct ubi_device *ubi,
491 const struct ubi_mkvol_req *req)
493 int n, err = -EINVAL;
495 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
499 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
500 req->vol_id != UBI_VOL_NUM_AUTO)
503 if (req->alignment == 0)
509 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
510 req->vol_type != UBI_STATIC_VOLUME)
513 if (req->alignment > ubi->leb_size)
516 n = req->alignment % ubi->min_io_size;
517 if (req->alignment != 1 && n)
520 if (req->name_len > UBI_VOL_NAME_MAX) {
528 dbg_err("bad volume creation request");
529 ubi_dbg_dump_mkvol_req(req);
534 * verify_rsvol_req - verify volume re-size request.
535 * @ubi: UBI device description object
536 * @req: the request to check
538 * This function returns zero if the request is correct, and %-EINVAL if not.
540 static int verify_rsvol_req(const struct ubi_device *ubi,
541 const struct ubi_rsvol_req *req)
546 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
552 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
553 unsigned int cmd, unsigned long arg)
556 struct ubi_device *ubi;
557 struct ubi_volume_desc *desc;
558 void __user *argp = (void __user *)arg;
560 if (!capable(CAP_SYS_RESOURCE))
563 ubi = ubi_get_by_major(imajor(inode));
568 /* Create volume command */
571 struct ubi_mkvol_req req;
573 dbg_msg("create volume");
574 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
580 err = verify_mkvol_req(ubi, &req);
584 req.name[req.name_len] = '\0';
586 mutex_lock(&ubi->volumes_mutex);
587 err = ubi_create_volume(ubi, &req);
588 mutex_unlock(&ubi->volumes_mutex);
592 err = put_user(req.vol_id, (__user int32_t *)argp);
599 /* Remove volume command */
604 dbg_msg("remove volume");
605 err = get_user(vol_id, (__user int32_t *)argp);
611 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
617 mutex_lock(&ubi->volumes_mutex);
618 err = ubi_remove_volume(desc);
619 mutex_unlock(&ubi->volumes_mutex);
622 * The volume is deleted (unless an error occurred), and the
623 * 'struct ubi_volume' object will be freed when
624 * 'ubi_close_volume()' will call 'put_device()'.
626 ubi_close_volume(desc);
630 /* Re-size volume command */
635 struct ubi_rsvol_req req;
637 dbg_msg("re-size volume");
638 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
644 err = verify_rsvol_req(ubi, &req);
648 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
655 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
658 mutex_lock(&ubi->volumes_mutex);
659 err = ubi_resize_volume(desc, pebs);
660 mutex_unlock(&ubi->volumes_mutex);
661 ubi_close_volume(desc);
674 static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
675 unsigned int cmd, unsigned long arg)
678 void __user *argp = (void __user *)arg;
680 if (!capable(CAP_SYS_RESOURCE))
684 /* Attach an MTD device command */
687 struct ubi_attach_req req;
688 struct mtd_info *mtd;
690 dbg_msg("attach MTD device");
691 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
697 if (req.mtd_num < 0 ||
698 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
703 mtd = get_mtd_device(NULL, req.mtd_num);
710 * Note, further request verification is done by
711 * 'ubi_attach_mtd_dev()'.
713 mutex_lock(&ubi_devices_mutex);
714 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
715 mutex_unlock(&ubi_devices_mutex);
719 /* @err contains UBI device number */
720 err = put_user(err, (__user int32_t *)argp);
725 /* Detach an MTD device command */
730 dbg_msg("dettach MTD device");
731 err = get_user(ubi_num, (__user int32_t *)argp);
737 mutex_lock(&ubi_devices_mutex);
738 err = ubi_detach_mtd_dev(ubi_num, 0);
739 mutex_unlock(&ubi_devices_mutex);
751 /* UBI control character device operations */
752 struct file_operations ubi_ctrl_cdev_operations = {
753 .ioctl = ctrl_cdev_ioctl,
754 .owner = THIS_MODULE,
757 /* UBI character device operations */
758 struct file_operations ubi_cdev_operations = {
759 .owner = THIS_MODULE,
760 .ioctl = ubi_cdev_ioctl,
764 /* UBI volume character device operations */
765 struct file_operations ubi_vol_cdev_operations = {
766 .owner = THIS_MODULE,
767 .open = vol_cdev_open,
768 .release = vol_cdev_release,
769 .llseek = vol_cdev_llseek,
770 .read = vol_cdev_read,
771 .write = vol_cdev_write,
772 .ioctl = vol_cdev_ioctl,