1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) International Business Machines Corp., 2006
5 * Author: Artem Bityutskiy (Битюцкий Артём)
9 * This file includes implementation of UBI character device operations.
11 * There are two kinds of character devices in UBI: UBI character devices and
12 * UBI volume character devices. UBI character devices allow users to
13 * manipulate whole volumes: create, remove, and re-size them. Volume character
14 * devices provide volume I/O capabilities.
16 * Major and minor numbers are assigned dynamically to both UBI and volume
19 * Well, there is the third kind of character devices - the UBI control
20 * character device, which allows to manipulate by UBI devices - create and
21 * delete them. In other words, it is used for attaching and detaching MTD
25 #include <linux/module.h>
26 #include <linux/stat.h>
27 #include <linux/slab.h>
28 #include <linux/ioctl.h>
29 #include <linux/capability.h>
30 #include <linux/uaccess.h>
31 #include <linux/compat.h>
32 #include <linux/math64.h>
33 #include <mtd/ubi-user.h>
37 * get_exclusive - get exclusive access to an UBI volume.
38 * @desc: volume descriptor
40 * This function changes UBI volume open mode to "exclusive". Returns previous
41 * mode value (positive integer) in case of success and a negative error code
44 static int get_exclusive(struct ubi_volume_desc *desc)
47 struct ubi_volume *vol = desc->vol;
49 spin_lock(&vol->ubi->volumes_lock);
50 users = vol->readers + vol->writers + vol->exclusive + vol->metaonly;
51 ubi_assert(users > 0);
53 ubi_err(vol->ubi, "%d users for volume %d", users, vol->vol_id);
56 vol->readers = vol->writers = vol->metaonly = 0;
59 desc->mode = UBI_EXCLUSIVE;
61 spin_unlock(&vol->ubi->volumes_lock);
67 * revoke_exclusive - revoke exclusive mode.
68 * @desc: volume descriptor
69 * @mode: new mode to switch to
71 static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
73 struct ubi_volume *vol = desc->vol;
75 spin_lock(&vol->ubi->volumes_lock);
76 ubi_assert(vol->readers == 0 && vol->writers == 0 && vol->metaonly == 0);
77 ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
79 if (mode == UBI_READONLY)
81 else if (mode == UBI_READWRITE)
83 else if (mode == UBI_METAONLY)
87 spin_unlock(&vol->ubi->volumes_lock);
92 static int vol_cdev_open(struct inode *inode, struct file *file)
94 struct ubi_volume_desc *desc;
95 int vol_id = iminor(inode) - 1, mode, ubi_num;
97 ubi_num = ubi_major2num(imajor(inode));
101 if (file->f_mode & FMODE_WRITE)
102 mode = UBI_READWRITE;
106 dbg_gen("open device %d, volume %d, mode %d",
107 ubi_num, vol_id, mode);
109 desc = ubi_open_volume(ubi_num, vol_id, mode);
111 return PTR_ERR(desc);
113 file->private_data = desc;
117 static int vol_cdev_release(struct inode *inode, struct file *file)
119 struct ubi_volume_desc *desc = file->private_data;
120 struct ubi_volume *vol = desc->vol;
122 dbg_gen("release device %d, volume %d, mode %d",
123 vol->ubi->ubi_num, vol->vol_id, desc->mode);
126 ubi_warn(vol->ubi, "update of volume %d not finished, volume is damaged",
128 ubi_assert(!vol->changing_leb);
131 } else if (vol->changing_leb) {
132 dbg_gen("only %lld of %lld bytes received for atomic LEB change for volume %d:%d, cancel",
133 vol->upd_received, vol->upd_bytes, vol->ubi->ubi_num,
135 vol->changing_leb = 0;
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;
149 /* Update is in progress, seeking is prohibited */
150 ubi_err(vol->ubi, "updating");
154 return fixed_size_llseek(file, offset, origin, vol->used_bytes);
157 static int vol_cdev_fsync(struct file *file, loff_t start, loff_t end,
160 struct ubi_volume_desc *desc = file->private_data;
161 struct ubi_device *ubi = desc->vol->ubi;
162 struct inode *inode = file_inode(file);
165 err = ubi_sync(ubi->ubi_num);
171 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
174 struct ubi_volume_desc *desc = file->private_data;
175 struct ubi_volume *vol = desc->vol;
176 struct ubi_device *ubi = vol->ubi;
177 int err, lnum, off, len, tbuf_size;
178 size_t count_save = count;
181 dbg_gen("read %zd bytes from offset %lld of volume %d",
182 count, *offp, vol->vol_id);
185 ubi_err(vol->ubi, "updating");
188 if (vol->upd_marker) {
189 ubi_err(vol->ubi, "damaged volume, update marker is set");
192 if (*offp == vol->used_bytes || count == 0)
196 dbg_gen("read from corrupted volume %d", vol->vol_id);
198 if (*offp + count > vol->used_bytes)
199 count_save = count = vol->used_bytes - *offp;
201 tbuf_size = vol->usable_leb_size;
202 if (count < tbuf_size)
203 tbuf_size = ALIGN(count, ubi->min_io_size);
204 tbuf = vmalloc(tbuf_size);
208 len = count > tbuf_size ? tbuf_size : count;
209 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
214 if (off + len >= vol->usable_leb_size)
215 len = vol->usable_leb_size - off;
217 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
222 if (off == vol->usable_leb_size) {
224 off -= vol->usable_leb_size;
230 err = copy_to_user(buf, tbuf, len);
237 len = count > tbuf_size ? tbuf_size : count;
241 return err ? err : count_save - count;
245 * This function allows to directly write to dynamic UBI volumes, without
246 * issuing the volume update operation.
248 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
249 size_t count, loff_t *offp)
251 struct ubi_volume_desc *desc = file->private_data;
252 struct ubi_volume *vol = desc->vol;
253 struct ubi_device *ubi = vol->ubi;
254 int lnum, off, len, tbuf_size, err = 0;
255 size_t count_save = count;
258 if (!vol->direct_writes)
261 dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
262 count, *offp, vol->vol_id);
264 if (vol->vol_type == UBI_STATIC_VOLUME)
267 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
268 if (off & (ubi->min_io_size - 1)) {
269 ubi_err(ubi, "unaligned position");
273 if (*offp + count > vol->used_bytes)
274 count_save = count = vol->used_bytes - *offp;
276 /* We can write only in fractions of the minimum I/O unit */
277 if (count & (ubi->min_io_size - 1)) {
278 ubi_err(ubi, "unaligned write length");
282 tbuf_size = vol->usable_leb_size;
283 if (count < tbuf_size)
284 tbuf_size = ALIGN(count, ubi->min_io_size);
285 tbuf = vmalloc(tbuf_size);
289 len = count > tbuf_size ? tbuf_size : count;
294 if (off + len >= vol->usable_leb_size)
295 len = vol->usable_leb_size - off;
297 err = copy_from_user(tbuf, buf, len);
303 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len);
308 if (off == vol->usable_leb_size) {
310 off -= vol->usable_leb_size;
316 len = count > tbuf_size ? tbuf_size : count;
320 return err ? err : count_save - count;
323 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
324 size_t count, loff_t *offp)
327 struct ubi_volume_desc *desc = file->private_data;
328 struct ubi_volume *vol = desc->vol;
329 struct ubi_device *ubi = vol->ubi;
331 if (!vol->updating && !vol->changing_leb)
332 return vol_cdev_direct_write(file, buf, count, offp);
335 err = ubi_more_update_data(ubi, vol, buf, count);
337 err = ubi_more_leb_change_data(ubi, vol, buf, count);
340 ubi_err(ubi, "cannot accept more %zd bytes of data, error %d",
347 * The operation is finished, @err contains number of actually
352 if (vol->changing_leb) {
353 revoke_exclusive(desc, UBI_READWRITE);
358 * We voluntarily do not take into account the skip_check flag
359 * as we want to make sure what we wrote was correctly written.
361 err = ubi_check_volume(ubi, vol->vol_id);
366 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
367 vol->vol_id, ubi->ubi_num);
371 ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
372 revoke_exclusive(desc, UBI_READWRITE);
378 static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
382 struct ubi_volume_desc *desc = file->private_data;
383 struct ubi_volume *vol = desc->vol;
384 struct ubi_device *ubi = vol->ubi;
385 void __user *argp = (void __user *)arg;
388 /* Volume update command */
391 int64_t bytes, rsvd_bytes;
393 if (!capable(CAP_SYS_RESOURCE)) {
398 err = copy_from_user(&bytes, argp, sizeof(int64_t));
404 if (desc->mode == UBI_READONLY) {
409 rsvd_bytes = (long long)vol->reserved_pebs *
410 vol->usable_leb_size;
411 if (bytes < 0 || bytes > rsvd_bytes) {
416 err = get_exclusive(desc);
420 err = ubi_start_update(ubi, vol, bytes);
422 ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
423 revoke_exclusive(desc, UBI_READWRITE);
428 /* Atomic logical eraseblock change command */
431 struct ubi_leb_change_req req;
433 err = copy_from_user(&req, argp,
434 sizeof(struct ubi_leb_change_req));
440 if (desc->mode == UBI_READONLY ||
441 vol->vol_type == UBI_STATIC_VOLUME) {
446 /* Validate the request */
448 if (!ubi_leb_valid(vol, req.lnum) ||
449 req.bytes < 0 || req.bytes > vol->usable_leb_size)
452 err = get_exclusive(desc);
456 err = ubi_start_leb_change(ubi, vol, &req);
458 revoke_exclusive(desc, UBI_READWRITE);
462 /* Logical eraseblock erasure command */
467 err = get_user(lnum, (__user int32_t *)argp);
473 if (desc->mode == UBI_READONLY ||
474 vol->vol_type == UBI_STATIC_VOLUME) {
479 if (!ubi_leb_valid(vol, lnum)) {
484 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
485 err = ubi_eba_unmap_leb(ubi, vol, lnum);
489 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
493 /* Logical eraseblock map command */
496 struct ubi_map_req req;
498 err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
503 err = ubi_leb_map(desc, req.lnum);
507 /* Logical eraseblock un-map command */
512 err = get_user(lnum, (__user int32_t *)argp);
517 err = ubi_leb_unmap(desc, lnum);
521 /* Check if logical eraseblock is mapped command */
526 err = get_user(lnum, (__user int32_t *)argp);
531 err = ubi_is_mapped(desc, lnum);
535 /* Set volume property command */
536 case UBI_IOCSETVOLPROP:
538 struct ubi_set_vol_prop_req req;
540 err = copy_from_user(&req, argp,
541 sizeof(struct ubi_set_vol_prop_req));
546 switch (req.property) {
547 case UBI_VOL_PROP_DIRECT_WRITE:
548 mutex_lock(&ubi->device_mutex);
549 desc->vol->direct_writes = !!req.value;
550 mutex_unlock(&ubi->device_mutex);
559 /* Create a R/O block device on top of the UBI volume */
560 case UBI_IOCVOLCRBLK:
562 struct ubi_volume_info vi;
564 ubi_get_volume_info(desc, &vi);
565 err = ubiblock_create(&vi);
569 /* Remove the R/O block device */
570 case UBI_IOCVOLRMBLK:
572 struct ubi_volume_info vi;
574 ubi_get_volume_info(desc, &vi);
575 err = ubiblock_remove(&vi);
587 * verify_mkvol_req - verify volume creation request.
588 * @ubi: UBI device description object
589 * @req: the request to check
591 * This function zero if the request is correct, and %-EINVAL if not.
593 static int verify_mkvol_req(const struct ubi_device *ubi,
594 const struct ubi_mkvol_req *req)
596 int n, err = -EINVAL;
598 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
602 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
603 req->vol_id != UBI_VOL_NUM_AUTO)
606 if (req->alignment == 0)
612 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
613 req->vol_type != UBI_STATIC_VOLUME)
616 if (req->flags & ~UBI_VOL_VALID_FLGS)
619 if (req->flags & UBI_VOL_SKIP_CRC_CHECK_FLG &&
620 req->vol_type != UBI_STATIC_VOLUME)
623 if (req->alignment > ubi->leb_size)
626 n = req->alignment & (ubi->min_io_size - 1);
627 if (req->alignment != 1 && n)
630 if (!req->name[0] || !req->name_len)
633 if (req->name_len > UBI_VOL_NAME_MAX) {
638 n = strnlen(req->name, req->name_len + 1);
639 if (n != req->name_len)
645 ubi_err(ubi, "bad volume creation request");
646 ubi_dump_mkvol_req(req);
651 * verify_rsvol_req - verify volume re-size request.
652 * @ubi: UBI device description object
653 * @req: the request to check
655 * This function returns zero if the request is correct, and %-EINVAL if not.
657 static int verify_rsvol_req(const struct ubi_device *ubi,
658 const struct ubi_rsvol_req *req)
663 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
670 * rename_volumes - rename UBI volumes.
671 * @ubi: UBI device description object
672 * @req: volumes re-name request
674 * This is a helper function for the volume re-name IOCTL which validates the
675 * the request, opens the volume and calls corresponding volumes management
676 * function. Returns zero in case of success and a negative error code in case
679 static int rename_volumes(struct ubi_device *ubi,
680 struct ubi_rnvol_req *req)
683 struct list_head rename_list;
684 struct ubi_rename_entry *re, *re1;
686 if (req->count < 0 || req->count > UBI_MAX_RNVOL)
692 /* Validate volume IDs and names in the request */
693 for (i = 0; i < req->count; i++) {
694 if (req->ents[i].vol_id < 0 ||
695 req->ents[i].vol_id >= ubi->vtbl_slots)
697 if (req->ents[i].name_len < 0)
699 if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
700 return -ENAMETOOLONG;
701 req->ents[i].name[req->ents[i].name_len] = '\0';
702 n = strlen(req->ents[i].name);
703 if (n != req->ents[i].name_len)
707 /* Make sure volume IDs and names are unique */
708 for (i = 0; i < req->count - 1; i++) {
709 for (n = i + 1; n < req->count; n++) {
710 if (req->ents[i].vol_id == req->ents[n].vol_id) {
711 ubi_err(ubi, "duplicated volume id %d",
712 req->ents[i].vol_id);
715 if (!strcmp(req->ents[i].name, req->ents[n].name)) {
716 ubi_err(ubi, "duplicated volume name \"%s\"",
723 /* Create the re-name list */
724 INIT_LIST_HEAD(&rename_list);
725 for (i = 0; i < req->count; i++) {
726 int vol_id = req->ents[i].vol_id;
727 int name_len = req->ents[i].name_len;
728 const char *name = req->ents[i].name;
730 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
736 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_METAONLY);
737 if (IS_ERR(re->desc)) {
738 err = PTR_ERR(re->desc);
739 ubi_err(ubi, "cannot open volume %d, error %d",
745 /* Skip this re-naming if the name does not really change */
746 if (re->desc->vol->name_len == name_len &&
747 !memcmp(re->desc->vol->name, name, name_len)) {
748 ubi_close_volume(re->desc);
753 re->new_name_len = name_len;
754 memcpy(re->new_name, name, name_len);
755 list_add_tail(&re->list, &rename_list);
756 dbg_gen("will rename volume %d from \"%s\" to \"%s\"",
757 vol_id, re->desc->vol->name, name);
760 if (list_empty(&rename_list))
763 /* Find out the volumes which have to be removed */
764 list_for_each_entry(re, &rename_list, list) {
765 struct ubi_volume_desc *desc;
766 int no_remove_needed = 0;
769 * Volume @re->vol_id is going to be re-named to
770 * @re->new_name, while its current name is @name. If a volume
771 * with name @re->new_name currently exists, it has to be
772 * removed, unless it is also re-named in the request (@req).
774 list_for_each_entry(re1, &rename_list, list) {
775 if (re->new_name_len == re1->desc->vol->name_len &&
776 !memcmp(re->new_name, re1->desc->vol->name,
777 re1->desc->vol->name_len)) {
778 no_remove_needed = 1;
783 if (no_remove_needed)
787 * It seems we need to remove volume with name @re->new_name,
790 desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
795 /* Re-naming into a non-existing volume name */
798 /* The volume exists but busy, or an error occurred */
799 ubi_err(ubi, "cannot open volume \"%s\", error %d",
804 re1 = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
807 ubi_close_volume(desc);
813 list_add(&re1->list, &rename_list);
814 dbg_gen("will remove volume %d, name \"%s\"",
815 re1->desc->vol->vol_id, re1->desc->vol->name);
818 mutex_lock(&ubi->device_mutex);
819 err = ubi_rename_volumes(ubi, &rename_list);
820 mutex_unlock(&ubi->device_mutex);
823 list_for_each_entry_safe(re, re1, &rename_list, list) {
824 ubi_close_volume(re->desc);
831 static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
835 struct ubi_device *ubi;
836 struct ubi_volume_desc *desc;
837 void __user *argp = (void __user *)arg;
839 if (!capable(CAP_SYS_RESOURCE))
842 ubi = ubi_get_by_major(imajor(file->f_mapping->host));
847 /* Create volume command */
850 struct ubi_mkvol_req req;
852 dbg_gen("create volume");
853 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
859 err = verify_mkvol_req(ubi, &req);
863 mutex_lock(&ubi->device_mutex);
864 err = ubi_create_volume(ubi, &req);
865 mutex_unlock(&ubi->device_mutex);
869 err = put_user(req.vol_id, (__user int32_t *)argp);
876 /* Remove volume command */
881 dbg_gen("remove volume");
882 err = get_user(vol_id, (__user int32_t *)argp);
888 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
894 mutex_lock(&ubi->device_mutex);
895 err = ubi_remove_volume(desc, 0);
896 mutex_unlock(&ubi->device_mutex);
899 * The volume is deleted (unless an error occurred), and the
900 * 'struct ubi_volume' object will be freed when
901 * 'ubi_close_volume()' will call 'put_device()'.
903 ubi_close_volume(desc);
907 /* Re-size volume command */
911 struct ubi_rsvol_req req;
913 dbg_gen("re-size volume");
914 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
920 err = verify_rsvol_req(ubi, &req);
924 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
930 pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
931 desc->vol->usable_leb_size);
933 mutex_lock(&ubi->device_mutex);
934 err = ubi_resize_volume(desc, pebs);
935 mutex_unlock(&ubi->device_mutex);
936 ubi_close_volume(desc);
940 /* Re-name volumes command */
943 struct ubi_rnvol_req *req;
945 dbg_gen("re-name volumes");
946 req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
952 err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
959 err = rename_volumes(ubi, req);
964 /* Check a specific PEB for bitflips and scrub it if needed */
969 err = get_user(pnum, (__user int32_t *)argp);
975 err = ubi_bitflip_check(ubi, pnum, 0);
979 /* Force scrubbing for a specific PEB */
984 err = get_user(pnum, (__user int32_t *)argp);
990 err = ubi_bitflip_check(ubi, pnum, 1);
1003 static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
1007 void __user *argp = (void __user *)arg;
1009 if (!capable(CAP_SYS_RESOURCE))
1013 /* Attach an MTD device command */
1016 struct ubi_attach_req req;
1017 struct mtd_info *mtd;
1019 dbg_gen("attach MTD device");
1020 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
1026 if (req.mtd_num < 0 ||
1027 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
1032 mtd = get_mtd_device(NULL, req.mtd_num);
1039 * Note, further request verification is done by
1040 * 'ubi_attach_mtd_dev()'.
1042 mutex_lock(&ubi_devices_mutex);
1043 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
1044 req.max_beb_per1024);
1045 mutex_unlock(&ubi_devices_mutex);
1047 put_mtd_device(mtd);
1049 /* @err contains UBI device number */
1050 err = put_user(err, (__user int32_t *)argp);
1055 /* Detach an MTD device command */
1060 dbg_gen("detach MTD device");
1061 err = get_user(ubi_num, (__user int32_t *)argp);
1067 mutex_lock(&ubi_devices_mutex);
1068 err = ubi_detach_mtd_dev(ubi_num, 0);
1069 mutex_unlock(&ubi_devices_mutex);
1081 /* UBI volume character device operations */
1082 const struct file_operations ubi_vol_cdev_operations = {
1083 .owner = THIS_MODULE,
1084 .open = vol_cdev_open,
1085 .release = vol_cdev_release,
1086 .llseek = vol_cdev_llseek,
1087 .read = vol_cdev_read,
1088 .write = vol_cdev_write,
1089 .fsync = vol_cdev_fsync,
1090 .unlocked_ioctl = vol_cdev_ioctl,
1091 .compat_ioctl = compat_ptr_ioctl,
1094 /* UBI character device operations */
1095 const struct file_operations ubi_cdev_operations = {
1096 .owner = THIS_MODULE,
1097 .llseek = no_llseek,
1098 .unlocked_ioctl = ubi_cdev_ioctl,
1099 .compat_ioctl = compat_ptr_ioctl,
1102 /* UBI control character device operations */
1103 const struct file_operations ubi_ctrl_cdev_operations = {
1104 .owner = THIS_MODULE,
1105 .unlocked_ioctl = ctrl_cdev_ioctl,
1106 .compat_ioctl = compat_ptr_ioctl,
1107 .llseek = no_llseek,