38 #include <linux/module.h>
39 #include <linux/stat.h>
40 #include <linux/slab.h>
42 #include <linux/capability.h>
62 spin_lock(&vol->
ubi->volumes_lock);
74 spin_unlock(&vol->
ubi->volumes_lock);
88 spin_lock(&vol->
ubi->volumes_lock);
98 spin_unlock(&vol->
ubi->volumes_lock);
106 int vol_id = iminor(inode) - 1,
mode, ubi_num;
117 dbg_gen(
"open device %d, volume %d, mode %d",
118 ubi_num, vol_id, mode);
122 return PTR_ERR(desc);
128 static int vol_cdev_release(
struct inode *inode,
struct file *file)
133 dbg_gen(
"release device %d, volume %d, mode %d",
137 ubi_warn(
"update of volume %d not finished, volume is damaged",
143 dbg_gen(
"only %lld of %lld bytes received for atomic LEB change for volume %d:%d, cancel",
154 static loff_t vol_cdev_llseek(
struct file *file, loff_t
offset,
int origin)
180 if (new_offset < 0 || new_offset > vol->
used_bytes) {
181 ubi_err(
"bad seek %lld", new_offset);
185 dbg_gen(
"seek volume %d, offset %lld, origin %d, new offset %lld",
186 vol->
vol_id, offset, origin, new_offset);
188 file->
f_pos = new_offset;
192 static int vol_cdev_fsync(
struct file *file, loff_t
start, loff_t
end,
197 struct inode *inode = file->
f_path.dentry->d_inode;
206 static ssize_t vol_cdev_read(
struct file *file, __user
char *
buf,
size_t count,
212 int err, lnum, off,
len, tbuf_size;
213 size_t count_save =
count;
216 dbg_gen(
"read %zd bytes from offset %lld of volume %d",
217 count, *offp, vol->
vol_id);
224 ubi_err(
"damaged volume, update marker is set");
237 if (count < tbuf_size)
243 len = count > tbuf_size ? tbuf_size :
count;
272 len = count > tbuf_size ? tbuf_size :
count;
276 return err ? err : count_save -
count;
283 static ssize_t vol_cdev_direct_write(
struct file *file,
const char __user *buf,
284 size_t count, loff_t *offp)
289 int lnum, off, len, tbuf_size, err = 0;
290 size_t count_save =
count;
296 dbg_gen(
"requested: write %zd bytes to offset %lld of volume %u",
297 count, *offp, vol->
vol_id);
313 ubi_err(
"unaligned write length");
318 if (count < tbuf_size)
324 len = count > tbuf_size ? tbuf_size :
count;
351 len = count > tbuf_size ? tbuf_size :
count;
355 return err ? err : count_save -
count;
358 static ssize_t vol_cdev_write(
struct file *file,
const char __user *buf,
359 size_t count, loff_t *offp)
367 return vol_cdev_direct_write(file, buf, count, offp);
375 ubi_err(
"cannot accept more %zd bytes of data, error %d",
397 ubi_warn(
"volume %d on UBI device %d is corrupted",
409 static long vol_cdev_ioctl(
struct file *file,
unsigned int cmd,
422 int64_t
bytes, rsvd_bytes;
442 if (bytes < 0 || bytes > rsvd_bytes) {
447 err = get_exclusive(desc);
481 err = get_exclusive(desc);
575 switch (
req.property) {
578 desc->
vol->direct_writes = !!
req.value;
602 static int verify_mkvol_req(
const struct ubi_device *ubi,
647 ubi_err(
"bad volume creation request");
659 static int verify_rsvol_req(
const struct ubi_device *ubi,
681 static int rename_volumes(
struct ubi_device *ubi,
695 for (i = 0; i < req->
count; i++) {
696 if (req->
ents[i].vol_id < 0 ||
699 if (req->
ents[i].name_len < 0)
703 req->
ents[
i].name[req->
ents[
i].name_len] =
'\0';
705 if (n != req->
ents[i].name_len)
710 for (i = 0; i < req->
count - 1; i++) {
711 for (n = i + 1; n < req->
count; n++) {
712 if (req->
ents[i].vol_id == req->
ents[n].vol_id) {
713 ubi_err(
"duplicated volume id %d",
714 req->
ents[i].vol_id);
718 ubi_err(
"duplicated volume name \"%s\"",
726 INIT_LIST_HEAD(&rename_list);
727 for (i = 0; i < req->
count; i++) {
728 int vol_id = req->
ents[
i].vol_id;
739 if (IS_ERR(re->
desc)) {
740 err = PTR_ERR(re->
desc);
741 ubi_err(
"cannot open volume %d, error %d", vol_id, err);
747 if (re->
desc->vol->name_len == name_len &&
748 !
memcmp(re->
desc->vol->name, name, name_len)) {
757 dbg_gen(
"will rename volume %d from \"%s\" to \"%s\"",
758 vol_id, re->
desc->vol->name, name);
761 if (list_empty(&rename_list))
767 int no_remove_needed = 0;
778 re1->
desc->vol->name_len)) {
779 no_remove_needed = 1;
784 if (no_remove_needed)
800 ubi_err(
"cannot open volume \"%s\", error %d",
814 list_add(&re1->
list, &rename_list);
815 dbg_gen(
"will remove volume %d, name \"%s\"",
816 re1->
desc->vol->vol_id, re1->
desc->vol->name);
832 static long ubi_cdev_ioctl(
struct file *file,
unsigned int cmd,
860 err = verify_mkvol_req(ubi, &req);
921 err = verify_rsvol_req(ubi, &req);
931 pebs = div_u64(req.bytes + desc->
vol->usable_leb_size - 1,
932 desc->
vol->usable_leb_size);
960 err = rename_volumes(ubi, req);
974 static long ctrl_cdev_ioctl(
struct file *file,
unsigned int cmd,
997 if (req.mtd_num < 0 ||
1015 req.max_beb_per1024);
1052 #ifdef CONFIG_COMPAT
1056 unsigned long translated_arg = (
unsigned long)compat_ptr(arg);
1058 return vol_cdev_ioctl(file, cmd, translated_arg);
1064 unsigned long translated_arg = (
unsigned long)compat_ptr(arg);
1066 return ubi_cdev_ioctl(file, cmd, translated_arg);
1072 unsigned long translated_arg = (
unsigned long)compat_ptr(arg);
1074 return ctrl_cdev_ioctl(file, cmd, translated_arg);
1077 #define vol_cdev_compat_ioctl NULL
1078 #define ubi_cdev_compat_ioctl NULL
1079 #define ctrl_cdev_compat_ioctl NULL
1085 .open = vol_cdev_open,
1086 .release = vol_cdev_release,
1087 .llseek = vol_cdev_llseek,
1088 .read = vol_cdev_read,
1089 .write = vol_cdev_write,
1090 .fsync = vol_cdev_fsync,
1091 .unlocked_ioctl = vol_cdev_ioctl,
1099 .unlocked_ioctl = ubi_cdev_ioctl,
1106 .unlocked_ioctl = ctrl_cdev_ioctl,