tools: Introduce f2fslabel

commit 6afd3e9df0
category: bugfix
issue: #I6VAS0
CVE: NA

Signed-off-by: DongSenhao <dongsenhao2@huawei.com>
---------------------------------------

Although many other filesystems provide a tool for changing volume
label, e.g. e2label for ext filesystem, but f2fs has no way to change
volume label except set it while formatting with mkfs.f2fs.

This introduces f2fslabel, simple tool for changing label of f2fs
volume.

Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: dongsenhao <dongsenhao2@huawei.com>

 create mode 100644 man/f2fslabel.8

Signed-off-by: dongsenhao <dongsenhao2@huawei.com>
This commit is contained in:
Dongwoo Lee 2021-05-21 19:32:38 +09:00 committed by dongsenhao
parent 0c15655e55
commit b0ca57aa78
5 changed files with 116 additions and 1 deletions

View File

@ -18,3 +18,4 @@ install-data-hook:
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/defrag.f2fs
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/resize.f2fs
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/sload.f2fs
ln -sf fsck.f2fs $(DESTDIR)/$(sbindir)/f2fslabel

View File

@ -155,6 +155,14 @@ void sload_usage()
exit(1);
}
void label_usage()
{
MSG(0, "\nUsage: f2fslabel [options] device [volume-label]\n");
MSG(0, "[options]:\n");
MSG(0, " -V print the version number and exit\n");
exit(1);
}
static int is_digits(char *optarg)
{
unsigned int i;
@ -177,6 +185,8 @@ static void error_out(char *prog)
resize_usage();
else if (!strcmp("sload.f2fs", prog))
sload_usage();
else if (!strcmp("f2fslabel", prog))
label_usage();
else
MSG(0, "\nWrong program.\n");
}
@ -722,6 +732,39 @@ void f2fs_parse_options(int argc, char *argv[])
}
}
#endif /* WITH_SLOAD */
} else if (!strcmp("f2fslabel", prog)) {
#ifdef WITH_LABEL
const char *option_string = "V";
c.func = LABEL;
while ((option = getopt(argc, argv, option_string)) != EOF) {
switch (option) {
case 'V':
show_version(prog);
exit(0);
default:
err = EUNKNOWN_OPT;
break;
}
if (err != NOERROR)
break;
}
if (argc > (optind + 2)) { /* unknown argument(s) is(are) passed */
optind += 2;
err = EUNKNOWN_ARG;
} else if (argc == (optind + 2)) { /* change label */
c.vol_label = argv[optind + 1];
argc--;
} else { /* print label */
/*
* Since vol_label was initialized as "", in order to
* distinguish between clear label and print, set
* vol_label as NULL for print case
*/
c.vol_label = NULL;
}
#endif /* WITH_LABEL */
}
if (err == NOERROR) {
@ -971,6 +1014,36 @@ static int do_sload(struct f2fs_sb_info *sbi)
}
#endif
#ifdef WITH_LABEL
static int do_label(struct f2fs_sb_info *sbi)
{
struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
if (!c.vol_label) {
char label[MAX_VOLUME_NAME];
utf16_to_utf8(label, sb->volume_name,
MAX_VOLUME_NAME, MAX_VOLUME_NAME);
MSG(0, "Info: volume label = %s\n", label);
return 0;
}
if (strlen(c.vol_label) > MAX_VOLUME_NAME) {
ERR_MSG("Label should not exceed %d characters\n", MAX_VOLUME_NAME);
return -1;
}
utf8_to_utf16(sb->volume_name, (const char *)c.vol_label,
MAX_VOLUME_NAME, strlen(c.vol_label));
update_superblock(sb, SB_MASK_ALL);
MSG(0, "Info: volume label is changed to %s\n", c.vol_label);
return 0;
}
#endif
#if defined(__APPLE__)
static u64 get_boottime_ns()
{
@ -1084,6 +1157,12 @@ fsck_again:
c.func = FSCK;
c.fix_on = 1;
goto fsck_again;
#endif
#ifdef WITH_LABEL
case LABEL:
if (do_label(sbi))
goto out_err;
break;
#endif
default:
ERR_MSG("Wrong program name\n");

View File

@ -35,6 +35,7 @@
#define WITH_DEFRAG
#define WITH_RESIZE
#define WITH_SLOAD
#define WITH_LABEL
#endif
#include <inttypes.h>
@ -343,6 +344,7 @@ enum f2fs_config_func {
DEFRAG,
RESIZE,
SLOAD,
LABEL,
};
enum default_set {

View File

@ -1,3 +1,3 @@
## Makefile.am
dist_man_MANS = mkfs.f2fs.8 fsck.f2fs.8 dump.f2fs.8 defrag.f2fs.8 resize.f2fs.8 sload.f2fs.8 f2fs_io.8
dist_man_MANS = mkfs.f2fs.8 fsck.f2fs.8 dump.f2fs.8 defrag.f2fs.8 resize.f2fs.8 sload.f2fs.8 f2fs_io.8 f2fslabel.8

33
man/f2fslabel.8 Normal file
View File

@ -0,0 +1,33 @@
.\" Copyright (c) 2021 Samsung Electronics Co., Ltd.
.\"
.TH F2FSLABEL 8
.SH NAME
f2fslabel \- Change the label on an f2fs volume
.SH SYNOPSIS
.B f2fslabel
.I device
[
.I volume-label
]
.SH DESCRIPTION
.B f2fslabel
will display or change the volume label on the f2fs located on
.I device.
.PP
If the optional argument
.I volume-label
is present, then
.B f2fslabel
will set the volume label to be
.IR volume-label .
.PP
Otherwise,
.B f2fslabel
will simply show the current label.
.PP
.SH AUTHOR
.B f2fslabel
was written by Dongwoo Lee (dwoo08.lee@samsung.com).
.SH AVAILABILITY
.B f2fslabel
is available from git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs-tools.git.