Lines Matching +full:ubi +full:- +full:volume +full:-

1 // SPDX-License-Identifier: GPL-2.0-or-later
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
33 #include <mtd/ubi-user.h>
34 #include "ubi.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
47 struct ubi_volume *vol = desc->vol;
49 spin_lock(&vol->ubi->volumes_lock);
50 users = vol->readers + vol->writers + vol->exclusive + vol->metaonly;
53 ubi_err(vol->ubi, "%d users for volume %d", users, vol->vol_id);
54 err = -EBUSY;
56 vol->readers = vol->writers = vol->metaonly = 0;
57 vol->exclusive = 1;
58 err = desc->mode;
59 desc->mode = UBI_EXCLUSIVE;
61 spin_unlock(&vol->ubi->volumes_lock);
67 * revoke_exclusive - revoke exclusive mode.
68 * @desc: volume descriptor
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);
78 vol->exclusive = 0;
80 vol->readers = 1;
82 vol->writers = 1;
84 vol->metaonly = 1;
86 vol->exclusive = 1;
87 spin_unlock(&vol->ubi->volumes_lock);
89 desc->mode = mode;
95 int vol_id = iminor(inode) - 1, mode, ubi_num;
101 if (file->f_mode & FMODE_WRITE)
106 dbg_gen("open device %d, volume %d, mode %d",
113 file->private_data = desc;
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);
125 if (vol->updating) {
126 ubi_warn(vol->ubi, "update of volume %d not finished, volume is damaged",
127 vol->vol_id);
128 ubi_assert(!vol->changing_leb);
129 vol->updating = 0;
130 vfree(vol->upd_buf);
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,
134 vol->vol_id);
135 vol->changing_leb = 0;
136 vfree(vol->upd_buf);
145 struct ubi_volume_desc *desc = file->private_data;
146 struct ubi_volume *vol = desc->vol;
148 if (vol->updating) {
150 ubi_err(vol->ubi, "updating");
151 return -EBUSY;
154 return fixed_size_llseek(file, offset, origin, vol->used_bytes);
160 struct ubi_volume_desc *desc = file->private_data;
161 struct ubi_device *ubi = desc->vol->ubi;
165 err = ubi_sync(ubi->ubi_num);
174 struct ubi_volume_desc *desc = file->private_data;
175 struct ubi_volume *vol = desc->vol;
176 struct ubi_device *ubi = vol->ubi;
181 dbg_gen("read %zd bytes from offset %lld of volume %d",
182 count, *offp, vol->vol_id);
184 if (vol->updating) {
185 ubi_err(vol->ubi, "updating");
186 return -EBUSY;
188 if (vol->upd_marker) {
189 ubi_err(vol->ubi, "damaged volume, update marker is set");
190 return -EBADF;
192 if (*offp == vol->used_bytes || count == 0)
195 if (vol->corrupted)
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;
203 tbuf_size = ALIGN(count, ubi->min_io_size);
206 return -ENOMEM;
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;
227 count -= len;
232 err = -EFAULT;
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.
251 struct ubi_volume_desc *desc = file->private_data;
252 struct ubi_volume *vol = desc->vol;
253 struct ubi_device *ubi = vol->ubi;
258 if (!vol->direct_writes)
259 return -EPERM;
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)
265 return -EROFS;
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");
270 return -EINVAL;
273 if (*offp + count > vol->used_bytes)
274 count_save = count = vol->used_bytes - *offp;
277 if (count & (ubi->min_io_size - 1)) {
278 ubi_err(ubi, "unaligned write length");
279 return -EINVAL;
282 tbuf_size = vol->usable_leb_size;
284 tbuf_size = ALIGN(count, ubi->min_io_size);
287 return -ENOMEM;
294 if (off + len >= vol->usable_leb_size)
295 len = vol->usable_leb_size - off;
299 err = -EFAULT;
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;
313 count -= len;
320 return err ? err : count_save - count;
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)
334 if (vol->updating)
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",
352 if (vol->changing_leb) {
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);
368 vol->corrupted = 1;
370 vol->checked = 1;
371 ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
382 struct ubi_volume_desc *desc = file->private_data;
383 struct ubi_volume *vol = desc->vol;
384 struct ubi_device *ubi = vol->ubi;
388 /* Volume update command */
394 err = -EPERM;
400 err = -EFAULT;
404 if (desc->mode == UBI_READONLY) {
405 err = -EROFS;
409 rsvd_bytes = (long long)vol->reserved_pebs *
410 vol->usable_leb_size;
412 err = -EINVAL;
420 err = ubi_start_update(ubi, vol, bytes);
422 ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
436 err = -EFAULT;
440 if (desc->mode == UBI_READONLY ||
441 vol->vol_type == UBI_STATIC_VOLUME) {
442 err = -EROFS;
447 err = -EINVAL;
449 req.bytes < 0 || req.bytes > vol->usable_leb_size)
456 err = ubi_start_leb_change(ubi, vol, &req);
469 err = -EFAULT;
473 if (desc->mode == UBI_READONLY ||
474 vol->vol_type == UBI_STATIC_VOLUME) {
475 err = -EROFS;
480 err = -EINVAL;
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);
500 err = -EFAULT;
507 /* Logical eraseblock un-map command */
514 err = -EFAULT;
528 err = -EFAULT;
535 /* Set volume property command */
543 err = -EFAULT;
548 mutex_lock(&ubi->device_mutex);
549 desc->vol->direct_writes = !!req.value;
550 mutex_unlock(&ubi->device_mutex);
553 err = -EINVAL;
559 /* Create a R/O block device on top of the UBI volume */
580 err = -ENOTTY;
587 * verify_mkvol_req - verify volume creation request.
588 * @ubi: UBI device description object
591 * This function zero if the request is correct, and %-EINVAL if not.
593 static int verify_mkvol_req(const struct ubi_device *ubi,
596 int n, err = -EINVAL;
598 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
599 req->name_len < 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)
609 if (req->bytes == 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) {
634 err = -ENAMETOOLONG;
638 n = strnlen(req->name, req->name_len + 1);
639 if (n != req->name_len)
645 ubi_err(ubi, "bad volume creation request");
651 * verify_rsvol_req - verify volume re-size request.
652 * @ubi: UBI device description object
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,
660 if (req->bytes <= 0)
661 return -EINVAL;
663 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
664 return -EINVAL;
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 * request, opens the volume and calls corresponding volumes management
679 static int rename_volumes(struct ubi_device *ubi,
686 if (req->count < 0 || req->count > UBI_MAX_RNVOL)
687 return -EINVAL;
689 if (req->count == 0)
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)
696 return -EINVAL;
697 if (req->ents[i].name_len < 0)
698 return -EINVAL;
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)
704 return -EINVAL;
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);
713 return -EINVAL;
715 if (!strcmp(req->ents[i].name, req->ents[n].name)) {
716 ubi_err(ubi, "duplicated volume name \"%s\"",
717 req->ents[i].name);
718 return -EINVAL;
723 /* Create the re-name 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;
732 err = -ENOMEM;
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);
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).
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)) {
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,
794 if (err == -ENODEV)
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",
800 re->new_name, err);
806 err = -ENOMEM;
811 re1->remove = 1;
812 re1->desc = 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);
824 ubi_close_volume(re->desc);
825 list_del(&re->list);
831 static int ubi_get_ec_info(struct ubi_device *ubi, struct ubi_ecinfo_req __user *ureq)
841 return -EFAULT;
844 if (req.length <= 0 || req.start < 0 || req.start >= ubi->peb_count)
845 return -EINVAL;
848 return -EINVAL;
850 if (end_peb > ubi->peb_count)
851 end_peb = ubi->peb_count;
854 if (!access_ok((void __user *)ureq->erase_counters,
855 (end_peb-req.start) * sizeof(int32_t)))
856 return -EFAULT;
863 if (ubi_io_is_bad(ubi, peb)) {
864 if (__put_user(UBI_UNKNOWN, ureq->erase_counters+read_cnt))
865 return -EFAULT;
870 spin_lock(&ubi->wl_lock);
872 wl = ubi->lookuptbl[peb];
874 ec = wl->ec;
878 spin_unlock(&ubi->wl_lock);
880 if (__put_user(ec, ureq->erase_counters+read_cnt))
881 return -EFAULT;
890 return -EFAULT;
899 struct ubi_device *ubi;
904 return -EPERM;
906 ubi = ubi_get_by_major(imajor(file->f_mapping->host));
907 if (!ubi)
908 return -ENODEV;
911 /* Create volume command */
916 dbg_gen("create volume");
919 err = -EFAULT;
923 err = verify_mkvol_req(ubi, &req);
927 mutex_lock(&ubi->device_mutex);
928 err = ubi_create_volume(ubi, &req);
929 mutex_unlock(&ubi->device_mutex);
935 err = -EFAULT;
940 /* Remove volume command */
945 dbg_gen("remove volume");
948 err = -EFAULT;
952 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
958 mutex_lock(&ubi->device_mutex);
960 mutex_unlock(&ubi->device_mutex);
963 * The volume is deleted (unless an error occurred), and the
971 /* Re-size volume command */
977 dbg_gen("re-size volume");
980 err = -EFAULT;
984 err = verify_rsvol_req(ubi, &req);
988 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
994 pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
995 desc->vol->usable_leb_size);
997 mutex_lock(&ubi->device_mutex);
999 mutex_unlock(&ubi->device_mutex);
1004 /* Re-name volumes command */
1009 dbg_gen("re-name volumes");
1012 err = -ENOMEM;
1018 err = -EFAULT;
1023 err = rename_volumes(ubi, req);
1035 err = -EFAULT;
1039 err = ubi_bitflip_check(ubi, pnum, 0);
1050 err = -EFAULT;
1054 err = ubi_bitflip_check(ubi, pnum, 1);
1060 err = ubi_get_ec_info(ubi, argp);
1065 err = -ENOTTY;
1069 ubi_put_device(ubi);
1080 return -EPERM;
1092 err = -EFAULT;
1098 err = -EINVAL;
1120 /* @err contains UBI device number */
1134 err = -EFAULT;
1145 err = -ENOTTY;
1152 /* UBI volume character device operations */
1165 /* UBI character device operations */
1172 /* UBI control character device operations */