Skip to content

Commit f40ac9c

Browse files
Artem BityutskiyArtem Bityutskiy
authored andcommitted
UBI: implement multiple volumes rename
Quite useful ioctl which allows to make atomic system upgrades. The idea belongs to Richard Titmuss <richard_titmuss@logitech.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent c856635 commit f40ac9c

6 files changed

Lines changed: 375 additions & 15 deletions

File tree

‎drivers/mtd/ubi/build.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
806806

807807
mutex_init(&ubi->buf_mutex);
808808
mutex_init(&ubi->ckvol_mutex);
809+
mutex_init(&ubi->mult_mutex);
809810
mutex_init(&ubi->volumes_mutex);
810811
spin_lock_init(&ubi->volumes_lock);
811812

‎drivers/mtd/ubi/cdev.c‎

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,166 @@ static int verify_rsvol_req(const struct ubi_device *ubi,
605605
return 0;
606606
}
607607

608+
/**
609+
* rename_volumes - rename UBI volumes.
610+
* @ubi: UBI device description object
611+
* @req: volumes re-name request
612+
*
613+
* This is a helper function for the volume re-name IOCTL which validates the
614+
* the request, opens the volume and calls corresponding volumes management
615+
* function. Returns zero in case of success and a negative error code in case
616+
* of failure.
617+
*/
618+
static int rename_volumes(struct ubi_device *ubi,
619+
struct ubi_rnvol_req *req)
620+
{
621+
int i, n, err;
622+
struct list_head rename_list;
623+
struct ubi_rename_entry *re, *re1;
624+
625+
if (req->count < 0 || req->count > UBI_MAX_RNVOL)
626+
return -EINVAL;
627+
628+
if (req->count == 0)
629+
return 0;
630+
631+
/* Validate volume IDs and names in the request */
632+
for (i = 0; i < req->count; i++) {
633+
if (req->ents[i].vol_id < 0 ||
634+
req->ents[i].vol_id >= ubi->vtbl_slots)
635+
return -EINVAL;
636+
if (req->ents[i].name_len < 0)
637+
return -EINVAL;
638+
if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
639+
return -ENAMETOOLONG;
640+
req->ents[i].name[req->ents[i].name_len] = '\0';
641+
n = strlen(req->ents[i].name);
642+
if (n != req->ents[i].name_len)
643+
err = -EINVAL;
644+
}
645+
646+
/* Make sure volume IDs and names are unique */
647+
for (i = 0; i < req->count - 1; i++) {
648+
for (n = i + 1; n < req->count; n++) {
649+
if (req->ents[i].vol_id == req->ents[n].vol_id) {
650+
dbg_err("duplicated volume id %d",
651+
req->ents[i].vol_id);
652+
return -EINVAL;
653+
}
654+
if (!strcmp(req->ents[i].name, req->ents[n].name)) {
655+
dbg_err("duplicated volume name \"%s\"",
656+
req->ents[i].name);
657+
return -EINVAL;
658+
}
659+
}
660+
}
661+
662+
/* Create the re-name list */
663+
INIT_LIST_HEAD(&rename_list);
664+
for (i = 0; i < req->count; i++) {
665+
int vol_id = req->ents[i].vol_id;
666+
int name_len = req->ents[i].name_len;
667+
const char *name = req->ents[i].name;
668+
669+
re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
670+
if (!re) {
671+
err = -ENOMEM;
672+
goto out_free;
673+
}
674+
675+
re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
676+
if (IS_ERR(re->desc)) {
677+
err = PTR_ERR(re->desc);
678+
dbg_err("cannot open volume %d, error %d", vol_id, err);
679+
kfree(re);
680+
goto out_free;
681+
}
682+
683+
/* Skip this re-naming if the name does not really change */
684+
if (re->desc->vol->name_len == name_len &&
685+
!memcmp(re->desc->vol->name, name, name_len)) {
686+
ubi_close_volume(re->desc);
687+
kfree(re);
688+
continue;
689+
}
690+
691+
re->new_name_len = name_len;
692+
memcpy(re->new_name, name, name_len);
693+
list_add_tail(&re->list, &rename_list);
694+
dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
695+
vol_id, re->desc->vol->name, name);
696+
}
697+
698+
if (list_empty(&rename_list))
699+
return 0;
700+
701+
/* Find out the volumes which have to be removed */
702+
list_for_each_entry(re, &rename_list, list) {
703+
struct ubi_volume_desc *desc;
704+
int no_remove_needed = 0;
705+
706+
/*
707+
* Volume @re->vol_id is going to be re-named to
708+
* @re->new_name, while its current name is @name. If a volume
709+
* with name @re->new_name currently exists, it has to be
710+
* removed, unless it is also re-named in the request (@req).
711+
*/
712+
list_for_each_entry(re1, &rename_list, list) {
713+
if (re->new_name_len == re1->desc->vol->name_len &&
714+
!memcmp(re->new_name, re1->desc->vol->name,
715+
re1->desc->vol->name_len)) {
716+
no_remove_needed = 1;
717+
break;
718+
}
719+
}
720+
721+
if (no_remove_needed)
722+
continue;
723+
724+
/*
725+
* It seems we need to remove volume with name @re->new_name,
726+
* if it exists.
727+
*/
728+
desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name, UBI_EXCLUSIVE);
729+
if (IS_ERR(desc)) {
730+
err = PTR_ERR(desc);
731+
if (err == -ENODEV)
732+
/* Re-naming into a non-existing volume name */
733+
continue;
734+
735+
/* The volume exists but busy, or an error occurred */
736+
dbg_err("cannot open volume \"%s\", error %d",
737+
re->new_name, err);
738+
goto out_free;
739+
}
740+
741+
re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
742+
if (!re) {
743+
err = -ENOMEM;
744+
ubi_close_volume(desc);
745+
goto out_free;
746+
}
747+
748+
re->remove = 1;
749+
re->desc = desc;
750+
list_add(&re->list, &rename_list);
751+
dbg_msg("will remove volume %d, name \"%s\"",
752+
re->desc->vol->vol_id, re->desc->vol->name);
753+
}
754+
755+
mutex_lock(&ubi->volumes_mutex);
756+
err = ubi_rename_volumes(ubi, &rename_list);
757+
mutex_unlock(&ubi->volumes_mutex);
758+
759+
out_free:
760+
list_for_each_entry_safe(re, re1, &rename_list, list) {
761+
ubi_close_volume(re->desc);
762+
list_del(&re->list);
763+
kfree(re);
764+
}
765+
return err;
766+
}
767+
608768
static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
609769
unsigned int cmd, unsigned long arg)
610770
{
@@ -670,7 +830,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
670830
}
671831

672832
mutex_lock(&ubi->volumes_mutex);
673-
err = ubi_remove_volume(desc);
833+
err = ubi_remove_volume(desc, 0);
674834
mutex_unlock(&ubi->volumes_mutex);
675835

676836
/*
@@ -717,6 +877,32 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
717877
break;
718878
}
719879

880+
/* Re-name volumes command */
881+
case UBI_IOCRNVOL:
882+
{
883+
struct ubi_rnvol_req *req;
884+
885+
dbg_msg("re-name volumes");
886+
req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
887+
if (!req) {
888+
err = -ENOMEM;
889+
break;
890+
};
891+
892+
err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
893+
if (err) {
894+
err = -EFAULT;
895+
kfree(req);
896+
break;
897+
}
898+
899+
mutex_lock(&ubi->mult_mutex);
900+
err = rename_volumes(ubi, req);
901+
mutex_unlock(&ubi->mult_mutex);
902+
kfree(req);
903+
break;
904+
}
905+
720906
default:
721907
err = -ENOTTY;
722908
break;

‎drivers/mtd/ubi/ubi.h‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ struct ubi_ltree_entry {
131131
struct rw_semaphore mutex;
132132
};
133133

134+
/**
135+
* struct ubi_rename_entry - volume re-name description data structure.
136+
* @new_name_len: new volume name length
137+
* @new_name: new volume name
138+
* @remove: if not zero, this volume should be removed, not re-named
139+
* @desc: descriptor of the volume
140+
* @list: links re-name entries into a list
141+
*
142+
* This data structure is utilized in the multiple volume re-name code. Namely,
143+
* UBI first creates a list of &struct ubi_rename_entry objects from the
144+
* &struct ubi_rnvol_req request object, and then utilizes this list to do all
145+
* the job.
146+
*/
147+
struct ubi_rename_entry {
148+
int new_name_len;
149+
char new_name[UBI_VOL_NAME_MAX + 1];
150+
int remove;
151+
struct ubi_volume_desc *desc;
152+
struct list_head list;
153+
};
154+
134155
struct ubi_volume_desc;
135156

136157
/**
@@ -206,7 +227,7 @@ struct ubi_volume {
206227
int alignment;
207228
int data_pad;
208229
int name_len;
209-
char name[UBI_VOL_NAME_MAX+1];
230+
char name[UBI_VOL_NAME_MAX + 1];
210231

211232
int upd_ebs;
212233
int ch_lnum;
@@ -272,7 +293,7 @@ struct ubi_wl_entry;
272293
* @vtbl_size: size of the volume table in bytes
273294
* @vtbl: in-RAM volume table copy
274295
* @volumes_mutex: protects on-flash volume table and serializes volume
275-
* changes, like creation, deletion, update, resize
296+
* changes, like creation, deletion, update, re-size and re-name
276297
*
277298
* @max_ec: current highest erase counter value
278299
* @mean_ec: current mean erase counter value
@@ -330,6 +351,8 @@ struct ubi_wl_entry;
330351
* @peb_buf1: a buffer of PEB size used for different purposes
331352
* @peb_buf2: another buffer of PEB size used for different purposes
332353
* @buf_mutex: proptects @peb_buf1 and @peb_buf2
354+
* @ckvol_mutex: serializes static volume checking when opening
355+
* @mult_mutex: serializes operations on multiple volumes, like re-nameing
333356
* @dbg_peb_buf: buffer of PEB size used for debugging
334357
* @dbg_buf_mutex: proptects @dbg_peb_buf
335358
*/
@@ -410,6 +433,7 @@ struct ubi_device {
410433
void *peb_buf2;
411434
struct mutex buf_mutex;
412435
struct mutex ckvol_mutex;
436+
struct mutex mult_mutex;
413437
#ifdef CONFIG_MTD_UBI_DEBUG
414438
void *dbg_peb_buf;
415439
struct mutex dbg_buf_mutex;
@@ -426,12 +450,15 @@ extern struct mutex ubi_devices_mutex;
426450
/* vtbl.c */
427451
int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
428452
struct ubi_vtbl_record *vtbl_rec);
453+
int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
454+
struct list_head *rename_list);
429455
int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
430456

431457
/* vmt.c */
432458
int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
433-
int ubi_remove_volume(struct ubi_volume_desc *desc);
459+
int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
434460
int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
461+
int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
435462
int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
436463
void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
437464

‎drivers/mtd/ubi/vmt.c‎

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,14 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
402402
/**
403403
* ubi_remove_volume - remove volume.
404404
* @desc: volume descriptor
405+
* @no_vtbl: do not change volume table if not zero
405406
*
406407
* This function removes volume described by @desc. The volume has to be opened
407408
* in "exclusive" mode. Returns zero in case of success and a negative error
408409
* code in case of failure. The caller has to have the @ubi->volumes_mutex
409410
* locked.
410411
*/
411-
int ubi_remove_volume(struct ubi_volume_desc *desc)
412+
int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
412413
{
413414
struct ubi_volume *vol = desc->vol;
414415
struct ubi_device *ubi = vol->ubi;
@@ -437,9 +438,11 @@ int ubi_remove_volume(struct ubi_volume_desc *desc)
437438
if (err)
438439
goto out_err;
439440

440-
err = ubi_change_vtbl_record(ubi, vol_id, NULL);
441-
if (err)
442-
goto out_err;
441+
if (!no_vtbl) {
442+
err = ubi_change_vtbl_record(ubi, vol_id, NULL);
443+
if (err)
444+
goto out_err;
445+
}
443446

444447
for (i = 0; i < vol->reserved_pebs; i++) {
445448
err = ubi_eba_unmap_leb(ubi, vol, i);
@@ -465,7 +468,8 @@ int ubi_remove_volume(struct ubi_volume_desc *desc)
465468
ubi->vol_count -= 1;
466469
spin_unlock(&ubi->volumes_lock);
467470

468-
err = paranoid_check_volumes(ubi);
471+
if (!no_vtbl)
472+
err = paranoid_check_volumes(ubi);
469473
return err;
470474

471475
out_err:
@@ -601,6 +605,44 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
601605
return err;
602606
}
603607

608+
/**
609+
* ubi_rename_volumes - re-name UBI volumes.
610+
* @ubi: UBI device description object
611+
* @renam_list: list of &struct ubi_rename_entry objects
612+
*
613+
* This function re-names or removes volumes specified in the re-name list.
614+
* Returns zero in case of success and a negative error code in case of
615+
* failure.
616+
*/
617+
int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
618+
{
619+
int err;
620+
struct ubi_rename_entry *re;
621+
622+
err = ubi_vtbl_rename_volumes(ubi, rename_list);
623+
if (err)
624+
return err;
625+
626+
list_for_each_entry(re, rename_list, list) {
627+
if (re->remove) {
628+
err = ubi_remove_volume(re->desc, 1);
629+
if (err)
630+
break;
631+
} else {
632+
struct ubi_volume *vol = re->desc->vol;
633+
634+
spin_lock(&ubi->volumes_lock);
635+
vol->name_len = re->new_name_len;
636+
memcpy(vol->name, re->new_name, re->new_name_len + 1);
637+
spin_unlock(&ubi->volumes_lock);
638+
}
639+
}
640+
641+
if (!err)
642+
paranoid_check_volumes(ubi);
643+
return err;
644+
}
645+
604646
/**
605647
* ubi_add_volume - add volume.
606648
* @ubi: UBI device description object
@@ -826,10 +868,9 @@ static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
826868

827869
fail:
828870
ubi_err("paranoid check failed for volume %d", vol_id);
829-
if (vol) {
871+
if (vol)
830872
ubi_dbg_dump_vol_info(vol);
831-
ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
832-
}
873+
ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
833874
spin_unlock(&ubi->volumes_lock);
834875
return -EINVAL;
835876
}

0 commit comments

Comments
 (0)