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 <linux/uaccess.h>
43 #include <mtd/ubi-user.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_gen("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_gen("release volume %d, mode %d", vol->vol_id, desc->mode);
133 ubi_warn("update of volume %d not finished, volume is damaged",
135 ubi_assert(!vol->changing_leb);
138 } else if (vol->changing_leb) {
139 dbg_gen("only %lld of %lld bytes received for atomic LEB change"
140 " for volume %d:%d, cancel", vol->upd_received,
141 vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
142 vol->changing_leb = 0;
146 ubi_close_volume(desc);
150 static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
152 struct ubi_volume_desc *desc = file->private_data;
153 struct ubi_volume *vol = desc->vol;
157 /* Update is in progress, seeking is prohibited */
163 case 0: /* SEEK_SET */
166 case 1: /* SEEK_CUR */
167 new_offset = file->f_pos + offset;
169 case 2: /* SEEK_END */
170 new_offset = vol->used_bytes + offset;
176 if (new_offset < 0 || new_offset > vol->used_bytes) {
177 dbg_err("bad seek %lld", new_offset);
181 dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
182 vol->vol_id, offset, origin, new_offset);
184 file->f_pos = new_offset;
188 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
191 struct ubi_volume_desc *desc = file->private_data;
192 struct ubi_volume *vol = desc->vol;
193 struct ubi_device *ubi = vol->ubi;
194 int err, lnum, off, len, tbuf_size;
195 size_t count_save = count;
199 dbg_gen("read %zd bytes from offset %lld of volume %d",
200 count, *offp, vol->vol_id);
206 if (vol->upd_marker) {
207 dbg_err("damaged volume, update marker is set");
210 if (*offp == vol->used_bytes || count == 0)
214 dbg_gen("read from corrupted volume %d", vol->vol_id);
216 if (*offp + count > vol->used_bytes)
217 count_save = count = vol->used_bytes - *offp;
219 tbuf_size = vol->usable_leb_size;
220 if (count < tbuf_size)
221 tbuf_size = ALIGN(count, ubi->min_io_size);
222 tbuf = vmalloc(tbuf_size);
226 len = count > tbuf_size ? tbuf_size : count;
229 off = do_div(tmp, vol->usable_leb_size);
235 if (off + len >= vol->usable_leb_size)
236 len = vol->usable_leb_size - off;
238 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
243 if (off == vol->usable_leb_size) {
245 off -= vol->usable_leb_size;
251 err = copy_to_user(buf, tbuf, len);
258 len = count > tbuf_size ? tbuf_size : count;
262 return err ? err : count_save - count;
265 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
268 * This function allows to directly write to dynamic UBI volumes, without
269 * issuing the volume update operation. Available only as a debugging feature.
270 * Very useful for testing UBI.
272 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
273 size_t count, loff_t *offp)
275 struct ubi_volume_desc *desc = file->private_data;
276 struct ubi_volume *vol = desc->vol;
277 struct ubi_device *ubi = vol->ubi;
278 int lnum, off, len, tbuf_size, err = 0;
279 size_t count_save = count;
283 dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
284 count, *offp, vol->vol_id);
286 if (vol->vol_type == UBI_STATIC_VOLUME)
290 off = do_div(tmp, vol->usable_leb_size);
293 if (off & (ubi->min_io_size - 1)) {
294 dbg_err("unaligned position");
298 if (*offp + count > vol->used_bytes)
299 count_save = count = vol->used_bytes - *offp;
301 /* We can write only in fractions of the minimum I/O unit */
302 if (count & (ubi->min_io_size - 1)) {
303 dbg_err("unaligned write length");
307 tbuf_size = vol->usable_leb_size;
308 if (count < tbuf_size)
309 tbuf_size = ALIGN(count, ubi->min_io_size);
310 tbuf = vmalloc(tbuf_size);
314 len = count > tbuf_size ? tbuf_size : count;
319 if (off + len >= vol->usable_leb_size)
320 len = vol->usable_leb_size - off;
322 err = copy_from_user(tbuf, buf, len);
328 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
334 if (off == vol->usable_leb_size) {
336 off -= vol->usable_leb_size;
342 len = count > tbuf_size ? tbuf_size : count;
346 return err ? err : count_save - count;
350 #define vol_cdev_direct_write(file, buf, count, offp) (-EPERM)
351 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
353 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
354 size_t count, loff_t *offp)
357 struct ubi_volume_desc *desc = file->private_data;
358 struct ubi_volume *vol = desc->vol;
359 struct ubi_device *ubi = vol->ubi;
361 if (!vol->updating && !vol->changing_leb)
362 return vol_cdev_direct_write(file, buf, count, offp);
365 err = ubi_more_update_data(ubi, vol, buf, count);
367 err = ubi_more_leb_change_data(ubi, vol, buf, count);
370 ubi_err("cannot accept more %zd bytes of data, error %d",
377 * The operation is finished, @err contains number of actually
382 if (vol->changing_leb) {
383 revoke_exclusive(desc, UBI_READWRITE);
387 err = ubi_check_volume(ubi, vol->vol_id);
392 ubi_warn("volume %d on UBI device %d is corrupted",
393 vol->vol_id, ubi->ubi_num);
397 ubi_gluebi_updated(vol);
398 revoke_exclusive(desc, UBI_READWRITE);
404 static int vol_cdev_ioctl(struct inode *inode, struct file *file,
405 unsigned int cmd, unsigned long arg)
408 struct ubi_volume_desc *desc = file->private_data;
409 struct ubi_volume *vol = desc->vol;
410 struct ubi_device *ubi = vol->ubi;
411 void __user *argp = (void __user *)arg;
414 /* Volume update command */
417 int64_t bytes, rsvd_bytes;
419 if (!capable(CAP_SYS_RESOURCE)) {
424 err = copy_from_user(&bytes, argp, sizeof(int64_t));
430 if (desc->mode == UBI_READONLY) {
435 rsvd_bytes = (long long)vol->reserved_pebs *
436 ubi->leb_size-vol->data_pad;
437 if (bytes < 0 || bytes > rsvd_bytes) {
442 err = get_exclusive(desc);
446 err = ubi_start_update(ubi, vol, bytes);
448 revoke_exclusive(desc, UBI_READWRITE);
452 /* Atomic logical eraseblock change command */
455 struct ubi_leb_change_req req;
457 err = copy_from_user(&req, argp,
458 sizeof(struct ubi_leb_change_req));
464 if (desc->mode == UBI_READONLY ||
465 vol->vol_type == UBI_STATIC_VOLUME) {
470 /* Validate the request */
472 if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
473 req.bytes < 0 || req.lnum >= vol->usable_leb_size)
475 if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
476 req.dtype != UBI_UNKNOWN)
479 err = get_exclusive(desc);
483 err = ubi_start_leb_change(ubi, vol, &req);
485 revoke_exclusive(desc, UBI_READWRITE);
489 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
490 /* Logical eraseblock erasure command */
495 err = get_user(lnum, (__user int32_t *)argp);
501 if (desc->mode == UBI_READONLY ||
502 vol->vol_type == UBI_STATIC_VOLUME) {
507 if (lnum < 0 || lnum >= vol->reserved_pebs) {
512 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
513 err = ubi_eba_unmap_leb(ubi, vol, lnum);
517 err = ubi_wl_flush(ubi);
521 /* Logical eraseblock map command */
524 struct ubi_map_req req;
526 err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
531 err = ubi_leb_map(desc, req.lnum, req.dtype);
535 /* Logical eraseblock un-map command */
540 err = get_user(lnum, (__user int32_t *)argp);
545 err = ubi_leb_unmap(desc, lnum);
549 /* Check if logical eraseblock is mapped command */
554 err = get_user(lnum, (__user int32_t *)argp);
559 err = ubi_is_mapped(desc, lnum);
572 * verify_mkvol_req - verify volume creation request.
573 * @ubi: UBI device description object
574 * @req: the request to check
576 * This function zero if the request is correct, and %-EINVAL if not.
578 static int verify_mkvol_req(const struct ubi_device *ubi,
579 const struct ubi_mkvol_req *req)
581 int n, err = -EINVAL;
583 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
587 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
588 req->vol_id != UBI_VOL_NUM_AUTO)
591 if (req->alignment == 0)
597 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
598 req->vol_type != UBI_STATIC_VOLUME)
601 if (req->alignment > ubi->leb_size)
604 n = req->alignment & (ubi->min_io_size - 1);
605 if (req->alignment != 1 && n)
608 if (req->name_len > UBI_VOL_NAME_MAX) {
613 n = strnlen(req->name, req->name_len + 1);
614 if (n != req->name_len)
620 dbg_err("bad volume creation request");
621 ubi_dbg_dump_mkvol_req(req);
626 * verify_rsvol_req - verify volume re-size request.
627 * @ubi: UBI device description object
628 * @req: the request to check
630 * This function returns zero if the request is correct, and %-EINVAL if not.
632 static int verify_rsvol_req(const struct ubi_device *ubi,
633 const struct ubi_rsvol_req *req)
638 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
645 * rename_volumes - rename UBI volumes.
646 * @ubi: UBI device description object
647 * @req: volumes re-name request
649 * This is a helper function for the volume re-name IOCTL which validates the
650 * the request, opens the volume and calls corresponding volumes management
651 * function. Returns zero in case of success and a negative error code in case
654 static int rename_volumes(struct ubi_device *ubi,
655 struct ubi_rnvol_req *req)
658 struct list_head rename_list;
659 struct ubi_rename_entry *re, *re1;
661 if (req->count < 0 || req->count > UBI_MAX_RNVOL)
667 /* Validate volume IDs and names in the request */
668 for (i = 0; i < req->count; i++) {
669 if (req->ents[i].vol_id < 0 ||
670 req->ents[i].vol_id >= ubi->vtbl_slots)
672 if (req->ents[i].name_len < 0)
674 if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
675 return -ENAMETOOLONG;
676 req->ents[i].name[req->ents[i].name_len] = '\0';
677 n = strlen(req->ents[i].name);
678 if (n != req->ents[i].name_len)
682 /* Make sure volume IDs and names are unique */
683 for (i = 0; i < req->count - 1; i++) {
684 for (n = i + 1; n < req->count; n++) {
685 if (req->ents[i].vol_id == req->ents[n].vol_id) {
686 dbg_err("duplicated volume id %d",
687 req->ents[i].vol_id);
690 if (!strcmp(req->ents[i].name, req->ents[n].name)) {
691 dbg_err("duplicated volume name \"%s\"",
698 /* Create the re-name list */
699 INIT_LIST_HEAD(&rename_list);
700 for (i = 0; i < req->count; i++) {
701 int vol_id = req->ents[i].vol_id;
702 int name_len = req->ents[i].name_len;
703 const char *name = req->ents[i].name;
705 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
711 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
712 if (IS_ERR(re->desc)) {
713 err = PTR_ERR(re->desc);
714 dbg_err("cannot open volume %d, error %d", vol_id, err);
719 /* Skip this re-naming if the name does not really change */
720 if (re->desc->vol->name_len == name_len &&
721 !memcmp(re->desc->vol->name, name, name_len)) {
722 ubi_close_volume(re->desc);
727 re->new_name_len = name_len;
728 memcpy(re->new_name, name, name_len);
729 list_add_tail(&re->list, &rename_list);
730 dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
731 vol_id, re->desc->vol->name, name);
734 if (list_empty(&rename_list))
737 /* Find out the volumes which have to be removed */
738 list_for_each_entry(re, &rename_list, list) {
739 struct ubi_volume_desc *desc;
740 int no_remove_needed = 0;
743 * Volume @re->vol_id is going to be re-named to
744 * @re->new_name, while its current name is @name. If a volume
745 * with name @re->new_name currently exists, it has to be
746 * removed, unless it is also re-named in the request (@req).
748 list_for_each_entry(re1, &rename_list, list) {
749 if (re->new_name_len == re1->desc->vol->name_len &&
750 !memcmp(re->new_name, re1->desc->vol->name,
751 re1->desc->vol->name_len)) {
752 no_remove_needed = 1;
757 if (no_remove_needed)
761 * It seems we need to remove volume with name @re->new_name,
764 desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
769 /* Re-naming into a non-existing volume name */
772 /* The volume exists but busy, or an error occurred */
773 dbg_err("cannot open volume \"%s\", error %d",
778 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
781 ubi_close_volume(desc);
787 list_add(&re->list, &rename_list);
788 dbg_msg("will remove volume %d, name \"%s\"",
789 re->desc->vol->vol_id, re->desc->vol->name);
792 mutex_lock(&ubi->volumes_mutex);
793 err = ubi_rename_volumes(ubi, &rename_list);
794 mutex_unlock(&ubi->volumes_mutex);
797 list_for_each_entry_safe(re, re1, &rename_list, list) {
798 ubi_close_volume(re->desc);
805 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
806 unsigned int cmd, unsigned long arg)
809 struct ubi_device *ubi;
810 struct ubi_volume_desc *desc;
811 void __user *argp = (void __user *)arg;
813 if (!capable(CAP_SYS_RESOURCE))
816 ubi = ubi_get_by_major(imajor(inode));
821 /* Create volume command */
824 struct ubi_mkvol_req req;
826 dbg_gen("create volume");
827 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
833 req.name[req.name_len] = '\0';
834 err = verify_mkvol_req(ubi, &req);
838 mutex_lock(&ubi->volumes_mutex);
839 err = ubi_create_volume(ubi, &req);
840 mutex_unlock(&ubi->volumes_mutex);
844 err = put_user(req.vol_id, (__user int32_t *)argp);
851 /* Remove volume command */
856 dbg_gen("remove volume");
857 err = get_user(vol_id, (__user int32_t *)argp);
863 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
869 mutex_lock(&ubi->volumes_mutex);
870 err = ubi_remove_volume(desc, 0);
871 mutex_unlock(&ubi->volumes_mutex);
874 * The volume is deleted (unless an error occurred), and the
875 * 'struct ubi_volume' object will be freed when
876 * 'ubi_close_volume()' will call 'put_device()'.
878 ubi_close_volume(desc);
882 /* Re-size volume command */
887 struct ubi_rsvol_req req;
889 dbg_gen("re-size volume");
890 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
896 err = verify_rsvol_req(ubi, &req);
900 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
907 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
910 mutex_lock(&ubi->volumes_mutex);
911 err = ubi_resize_volume(desc, pebs);
912 mutex_unlock(&ubi->volumes_mutex);
913 ubi_close_volume(desc);
917 /* Re-name volumes command */
920 struct ubi_rnvol_req *req;
922 dbg_msg("re-name volumes");
923 req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
929 err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
936 mutex_lock(&ubi->mult_mutex);
937 err = rename_volumes(ubi, req);
938 mutex_unlock(&ubi->mult_mutex);
952 static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
953 unsigned int cmd, unsigned long arg)
956 void __user *argp = (void __user *)arg;
958 if (!capable(CAP_SYS_RESOURCE))
962 /* Attach an MTD device command */
965 struct ubi_attach_req req;
966 struct mtd_info *mtd;
968 dbg_gen("attach MTD device");
969 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
975 if (req.mtd_num < 0 ||
976 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
981 mtd = get_mtd_device(NULL, req.mtd_num);
988 * Note, further request verification is done by
989 * 'ubi_attach_mtd_dev()'.
991 mutex_lock(&ubi_devices_mutex);
992 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
993 mutex_unlock(&ubi_devices_mutex);
997 /* @err contains UBI device number */
998 err = put_user(err, (__user int32_t *)argp);
1003 /* Detach an MTD device command */
1008 dbg_gen("dettach MTD device");
1009 err = get_user(ubi_num, (__user int32_t *)argp);
1015 mutex_lock(&ubi_devices_mutex);
1016 err = ubi_detach_mtd_dev(ubi_num, 0);
1017 mutex_unlock(&ubi_devices_mutex);
1029 /* UBI control character device operations */
1030 struct file_operations ubi_ctrl_cdev_operations = {
1031 .ioctl = ctrl_cdev_ioctl,
1032 .owner = THIS_MODULE,
1035 /* UBI character device operations */
1036 struct file_operations ubi_cdev_operations = {
1037 .owner = THIS_MODULE,
1038 .ioctl = ubi_cdev_ioctl,
1039 .llseek = no_llseek,
1042 /* UBI volume character device operations */
1043 struct file_operations ubi_vol_cdev_operations = {
1044 .owner = THIS_MODULE,
1045 .open = vol_cdev_open,
1046 .release = vol_cdev_release,
1047 .llseek = vol_cdev_llseek,
1048 .read = vol_cdev_read,
1049 .write = vol_cdev_write,
1050 .ioctl = vol_cdev_ioctl,