hpfs: convert hpfs to use the new mount api

Convert the hpfs filesystem to use the new mount API.
Tested by comparing random mount & remount options before and after
the change.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Link: https://lore.kernel.org/r/0a066bbb-59ad-17b0-e413-190569f2fea9@redhat.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Eric Sandeen 2024-09-25 15:17:06 +02:00 committed by Christian Brauner
parent 945be8ca81
commit c323cbf720
No known key found for this signature in database
GPG Key ID: 91C61BC06578DCA2

View File

@ -9,7 +9,8 @@
#include "hpfs_fn.h" #include "hpfs_fn.h"
#include <linux/module.h> #include <linux/module.h>
#include <linux/parser.h> #include <linux/fs_context.h>
#include <linux/fs_parser.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/statfs.h> #include <linux/statfs.h>
#include <linux/magic.h> #include <linux/magic.h>
@ -90,7 +91,7 @@ void hpfs_error(struct super_block *s, const char *fmt, ...)
hpfs_sb(s)->sb_was_error = 1; hpfs_sb(s)->sb_was_error = 1;
} }
/* /*
* A little trick to detect cycles in many hpfs structures and don't let the * A little trick to detect cycles in many hpfs structures and don't let the
* kernel crash on corrupted filesystem. When first called, set c2 to 0. * kernel crash on corrupted filesystem. When first called, set c2 to 0.
* *
@ -272,146 +273,70 @@ static void destroy_inodecache(void)
kmem_cache_destroy(hpfs_inode_cachep); kmem_cache_destroy(hpfs_inode_cachep);
} }
/*
* A tiny parser for option strings, stolen from dosfs.
* Stolen again from read-only hpfs.
* And updated for table-driven option parsing.
*/
enum { enum {
Opt_help, Opt_uid, Opt_gid, Opt_umask, Opt_case_lower, Opt_case_asis, Opt_help, Opt_uid, Opt_gid, Opt_umask, Opt_case,
Opt_check_none, Opt_check_normal, Opt_check_strict, Opt_check, Opt_err, Opt_eas, Opt_chkdsk, Opt_timeshift,
Opt_err_cont, Opt_err_ro, Opt_err_panic,
Opt_eas_no, Opt_eas_ro, Opt_eas_rw,
Opt_chkdsk_no, Opt_chkdsk_errors, Opt_chkdsk_always,
Opt_timeshift, Opt_err,
}; };
static const match_table_t tokens = { static const struct constant_table hpfs_param_case[] = {
{Opt_help, "help"}, {"asis", 0},
{Opt_uid, "uid=%u"}, {"lower", 1},
{Opt_gid, "gid=%u"}, {}
{Opt_umask, "umask=%o"},
{Opt_case_lower, "case=lower"},
{Opt_case_asis, "case=asis"},
{Opt_check_none, "check=none"},
{Opt_check_normal, "check=normal"},
{Opt_check_strict, "check=strict"},
{Opt_err_cont, "errors=continue"},
{Opt_err_ro, "errors=remount-ro"},
{Opt_err_panic, "errors=panic"},
{Opt_eas_no, "eas=no"},
{Opt_eas_ro, "eas=ro"},
{Opt_eas_rw, "eas=rw"},
{Opt_chkdsk_no, "chkdsk=no"},
{Opt_chkdsk_errors, "chkdsk=errors"},
{Opt_chkdsk_always, "chkdsk=always"},
{Opt_timeshift, "timeshift=%d"},
{Opt_err, NULL},
}; };
static int parse_opts(char *opts, kuid_t *uid, kgid_t *gid, umode_t *umask, static const struct constant_table hpfs_param_check[] = {
int *lowercase, int *eas, int *chk, int *errs, {"none", 0},
int *chkdsk, int *timeshift) {"normal", 1},
{ {"strict", 2},
char *p; {}
int option; };
if (!opts) static const struct constant_table hpfs_param_err[] = {
return 1; {"continue", 0},
{"remount-ro", 1},
{"panic", 2},
{}
};
/*pr_info("Parsing opts: '%s'\n",opts);*/ static const struct constant_table hpfs_param_eas[] = {
{"no", 0},
{"ro", 1},
{"rw", 2},
{}
};
while ((p = strsep(&opts, ",")) != NULL) { static const struct constant_table hpfs_param_chkdsk[] = {
substring_t args[MAX_OPT_ARGS]; {"no", 0},
int token; {"errors", 1},
if (!*p) {"always", 2},
continue; {}
};
token = match_token(p, tokens, args); static const struct fs_parameter_spec hpfs_param_spec[] = {
switch (token) { fsparam_flag ("help", Opt_help),
case Opt_help: fsparam_uid ("uid", Opt_uid),
return 2; fsparam_gid ("gid", Opt_gid),
case Opt_uid: fsparam_u32oct ("umask", Opt_umask),
if (match_int(args, &option)) fsparam_enum ("case", Opt_case, hpfs_param_case),
return 0; fsparam_enum ("check", Opt_check, hpfs_param_check),
*uid = make_kuid(current_user_ns(), option); fsparam_enum ("errors", Opt_err, hpfs_param_err),
if (!uid_valid(*uid)) fsparam_enum ("eas", Opt_eas, hpfs_param_eas),
return 0; fsparam_enum ("chkdsk", Opt_chkdsk, hpfs_param_chkdsk),
break; fsparam_s32 ("timeshift", Opt_timeshift),
case Opt_gid: {}
if (match_int(args, &option)) };
return 0;
*gid = make_kgid(current_user_ns(), option); struct hpfs_fc_context {
if (!gid_valid(*gid)) kuid_t uid;
return 0; kgid_t gid;
break; umode_t umask;
case Opt_umask: int lowercase;
if (match_octal(args, &option)) int eas;
return 0; int chk;
*umask = option; int errs;
break; int chkdsk;
case Opt_case_lower: int timeshift;
*lowercase = 1; };
break;
case Opt_case_asis:
*lowercase = 0;
break;
case Opt_check_none:
*chk = 0;
break;
case Opt_check_normal:
*chk = 1;
break;
case Opt_check_strict:
*chk = 2;
break;
case Opt_err_cont:
*errs = 0;
break;
case Opt_err_ro:
*errs = 1;
break;
case Opt_err_panic:
*errs = 2;
break;
case Opt_eas_no:
*eas = 0;
break;
case Opt_eas_ro:
*eas = 1;
break;
case Opt_eas_rw:
*eas = 2;
break;
case Opt_chkdsk_no:
*chkdsk = 0;
break;
case Opt_chkdsk_errors:
*chkdsk = 1;
break;
case Opt_chkdsk_always:
*chkdsk = 2;
break;
case Opt_timeshift:
{
int m = 1;
char *rhs = args[0].from;
if (!rhs || !*rhs)
return 0;
if (*rhs == '-') m = -1;
if (*rhs == '+' || *rhs == '-') rhs++;
*timeshift = simple_strtoul(rhs, &rhs, 0) * m;
if (*rhs)
return 0;
break;
}
default:
return 0;
}
}
return 1;
}
static inline void hpfs_help(void) static inline void hpfs_help(void)
{ {
@ -439,49 +364,92 @@ HPFS filesystem options:\n\
\n"); \n");
} }
static int hpfs_remount_fs(struct super_block *s, int *flags, char *data) static int hpfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
{ {
kuid_t uid; struct hpfs_fc_context *ctx = fc->fs_private;
kgid_t gid; struct fs_parse_result result;
umode_t umask; int opt;
int lowercase, eas, chk, errs, chkdsk, timeshift;
int o; opt = fs_parse(fc, hpfs_param_spec, param, &result);
if (opt < 0)
return opt;
switch (opt) {
case Opt_help:
hpfs_help();
return -EINVAL;
case Opt_uid:
ctx->uid = result.uid;
break;
case Opt_gid:
ctx->gid = result.gid;
break;
case Opt_umask:
ctx->umask = result.uint_32;
break;
case Opt_case:
ctx->lowercase = result.uint_32;
break;
case Opt_check:
ctx->chk = result.uint_32;
break;
case Opt_err:
ctx->errs = result.uint_32;
break;
case Opt_eas:
ctx->eas = result.uint_32;
break;
case Opt_chkdsk:
ctx->chkdsk = result.uint_32;
break;
case Opt_timeshift:
{
int m = 1;
char *rhs = param->string;
int timeshift;
if (*rhs == '-') m = -1;
if (*rhs == '+' || *rhs == '-') rhs++;
timeshift = simple_strtoul(rhs, &rhs, 0) * m;
if (*rhs)
return -EINVAL;
ctx->timeshift = timeshift;
break;
}
default:
return -EINVAL;
}
return 0;
}
static int hpfs_reconfigure(struct fs_context *fc)
{
struct hpfs_fc_context *ctx = fc->fs_private;
struct super_block *s = fc->root->d_sb;
struct hpfs_sb_info *sbi = hpfs_sb(s); struct hpfs_sb_info *sbi = hpfs_sb(s);
sync_filesystem(s); sync_filesystem(s);
*flags |= SB_NOATIME; fc->sb_flags |= SB_NOATIME;
hpfs_lock(s); hpfs_lock(s);
uid = sbi->sb_uid; gid = sbi->sb_gid;
umask = 0777 & ~sbi->sb_mode;
lowercase = sbi->sb_lowercase;
eas = sbi->sb_eas; chk = sbi->sb_chk; chkdsk = sbi->sb_chkdsk;
errs = sbi->sb_err; timeshift = sbi->sb_timeshift;
if (!(o = parse_opts(data, &uid, &gid, &umask, &lowercase, if (ctx->timeshift != sbi->sb_timeshift) {
&eas, &chk, &errs, &chkdsk, &timeshift))) {
pr_err("bad mount options.\n");
goto out_err;
}
if (o == 2) {
hpfs_help();
goto out_err;
}
if (timeshift != sbi->sb_timeshift) {
pr_err("timeshift can't be changed using remount.\n"); pr_err("timeshift can't be changed using remount.\n");
goto out_err; goto out_err;
} }
unmark_dirty(s); unmark_dirty(s);
sbi->sb_uid = uid; sbi->sb_gid = gid; sbi->sb_uid = ctx->uid; sbi->sb_gid = ctx->gid;
sbi->sb_mode = 0777 & ~umask; sbi->sb_mode = 0777 & ~ctx->umask;
sbi->sb_lowercase = lowercase; sbi->sb_lowercase = ctx->lowercase;
sbi->sb_eas = eas; sbi->sb_chk = chk; sbi->sb_chkdsk = chkdsk; sbi->sb_eas = ctx->eas; sbi->sb_chk = ctx->chk;
sbi->sb_err = errs; sbi->sb_timeshift = timeshift; sbi->sb_chkdsk = ctx->chkdsk;
sbi->sb_err = ctx->errs; sbi->sb_timeshift = ctx->timeshift;
if (!(*flags & SB_RDONLY)) mark_dirty(s, 1); if (!(fc->sb_flags & SB_RDONLY)) mark_dirty(s, 1);
hpfs_unlock(s); hpfs_unlock(s);
return 0; return 0;
@ -530,30 +498,24 @@ static const struct super_operations hpfs_sops =
.evict_inode = hpfs_evict_inode, .evict_inode = hpfs_evict_inode,
.put_super = hpfs_put_super, .put_super = hpfs_put_super,
.statfs = hpfs_statfs, .statfs = hpfs_statfs,
.remount_fs = hpfs_remount_fs,
.show_options = hpfs_show_options, .show_options = hpfs_show_options,
}; };
static int hpfs_fill_super(struct super_block *s, void *options, int silent) static int hpfs_fill_super(struct super_block *s, struct fs_context *fc)
{ {
struct hpfs_fc_context *ctx = fc->fs_private;
struct buffer_head *bh0, *bh1, *bh2; struct buffer_head *bh0, *bh1, *bh2;
struct hpfs_boot_block *bootblock; struct hpfs_boot_block *bootblock;
struct hpfs_super_block *superblock; struct hpfs_super_block *superblock;
struct hpfs_spare_block *spareblock; struct hpfs_spare_block *spareblock;
struct hpfs_sb_info *sbi; struct hpfs_sb_info *sbi;
struct inode *root; struct inode *root;
int silent = fc->sb_flags & SB_SILENT;
kuid_t uid;
kgid_t gid;
umode_t umask;
int lowercase, eas, chk, errs, chkdsk, timeshift;
dnode_secno root_dno; dnode_secno root_dno;
struct hpfs_dirent *de = NULL; struct hpfs_dirent *de = NULL;
struct quad_buffer_head qbh; struct quad_buffer_head qbh;
int o;
sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
if (!sbi) { if (!sbi) {
return -ENOMEM; return -ENOMEM;
@ -563,26 +525,6 @@ static int hpfs_fill_super(struct super_block *s, void *options, int silent)
mutex_init(&sbi->hpfs_mutex); mutex_init(&sbi->hpfs_mutex);
hpfs_lock(s); hpfs_lock(s);
uid = current_uid();
gid = current_gid();
umask = current_umask();
lowercase = 0;
eas = 2;
chk = 1;
errs = 1;
chkdsk = 1;
timeshift = 0;
if (!(o = parse_opts(options, &uid, &gid, &umask, &lowercase,
&eas, &chk, &errs, &chkdsk, &timeshift))) {
pr_err("bad mount options.\n");
goto bail0;
}
if (o==2) {
hpfs_help();
goto bail0;
}
/*sbi->sb_mounting = 1;*/ /*sbi->sb_mounting = 1;*/
sb_set_blocksize(s, 512); sb_set_blocksize(s, 512);
sbi->sb_fs_size = -1; sbi->sb_fs_size = -1;
@ -622,17 +564,17 @@ static int hpfs_fill_super(struct super_block *s, void *options, int silent)
sbi->sb_dirband_start = le32_to_cpu(superblock->dir_band_start); sbi->sb_dirband_start = le32_to_cpu(superblock->dir_band_start);
sbi->sb_dirband_size = le32_to_cpu(superblock->n_dir_band); sbi->sb_dirband_size = le32_to_cpu(superblock->n_dir_band);
sbi->sb_dmap = le32_to_cpu(superblock->dir_band_bitmap); sbi->sb_dmap = le32_to_cpu(superblock->dir_band_bitmap);
sbi->sb_uid = uid; sbi->sb_uid = ctx->uid;
sbi->sb_gid = gid; sbi->sb_gid = ctx->gid;
sbi->sb_mode = 0777 & ~umask; sbi->sb_mode = 0777 & ~ctx->umask;
sbi->sb_n_free = -1; sbi->sb_n_free = -1;
sbi->sb_n_free_dnodes = -1; sbi->sb_n_free_dnodes = -1;
sbi->sb_lowercase = lowercase; sbi->sb_lowercase = ctx->lowercase;
sbi->sb_eas = eas; sbi->sb_eas = ctx->eas;
sbi->sb_chk = chk; sbi->sb_chk = ctx->chk;
sbi->sb_chkdsk = chkdsk; sbi->sb_chkdsk = ctx->chkdsk;
sbi->sb_err = errs; sbi->sb_err = ctx->errs;
sbi->sb_timeshift = timeshift; sbi->sb_timeshift = ctx->timeshift;
sbi->sb_was_error = 0; sbi->sb_was_error = 0;
sbi->sb_cp_table = NULL; sbi->sb_cp_table = NULL;
sbi->sb_c_bitmap = -1; sbi->sb_c_bitmap = -1;
@ -653,7 +595,7 @@ static int hpfs_fill_super(struct super_block *s, void *options, int silent)
/* Check for general fs errors*/ /* Check for general fs errors*/
if (spareblock->dirty && !spareblock->old_wrote) { if (spareblock->dirty && !spareblock->old_wrote) {
if (errs == 2) { if (sbi->sb_err == 2) {
pr_err("Improperly stopped, not mounted\n"); pr_err("Improperly stopped, not mounted\n");
goto bail4; goto bail4;
} }
@ -667,16 +609,16 @@ static int hpfs_fill_super(struct super_block *s, void *options, int silent)
} }
if (le32_to_cpu(spareblock->n_dnode_spares) != le32_to_cpu(spareblock->n_dnode_spares_free)) { if (le32_to_cpu(spareblock->n_dnode_spares) != le32_to_cpu(spareblock->n_dnode_spares_free)) {
if (errs >= 2) { if (sbi->sb_err >= 2) {
pr_err("Spare dnodes used, try chkdsk\n"); pr_err("Spare dnodes used, try chkdsk\n");
mark_dirty(s, 0); mark_dirty(s, 0);
goto bail4; goto bail4;
} }
hpfs_error(s, "warning: spare dnodes used, try chkdsk"); hpfs_error(s, "warning: spare dnodes used, try chkdsk");
if (errs == 0) if (sbi->sb_err == 0)
pr_err("Proceeding, but your filesystem could be corrupted if you delete files or directories\n"); pr_err("Proceeding, but your filesystem could be corrupted if you delete files or directories\n");
} }
if (chk) { if (sbi->sb_chk) {
unsigned a; unsigned a;
if (le32_to_cpu(superblock->dir_band_end) - le32_to_cpu(superblock->dir_band_start) + 1 != le32_to_cpu(superblock->n_dir_band) || if (le32_to_cpu(superblock->dir_band_end) - le32_to_cpu(superblock->dir_band_start) + 1 != le32_to_cpu(superblock->n_dir_band) ||
le32_to_cpu(superblock->dir_band_end) < le32_to_cpu(superblock->dir_band_start) || le32_to_cpu(superblock->n_dir_band) > 0x4000) { le32_to_cpu(superblock->dir_band_end) < le32_to_cpu(superblock->dir_band_start) || le32_to_cpu(superblock->n_dir_band) > 0x4000) {
@ -755,18 +697,70 @@ bail0:
return -EINVAL; return -EINVAL;
} }
static struct dentry *hpfs_mount(struct file_system_type *fs_type, static int hpfs_get_tree(struct fs_context *fc)
int flags, const char *dev_name, void *data)
{ {
return mount_bdev(fs_type, flags, dev_name, data, hpfs_fill_super); return get_tree_bdev(fc, hpfs_fill_super);
} }
static void hpfs_free_fc(struct fs_context *fc)
{
kfree(fc->fs_private);
}
static const struct fs_context_operations hpfs_fc_context_ops = {
.parse_param = hpfs_parse_param,
.get_tree = hpfs_get_tree,
.reconfigure = hpfs_reconfigure,
.free = hpfs_free_fc,
};
static int hpfs_init_fs_context(struct fs_context *fc)
{
struct hpfs_fc_context *ctx;
ctx = kzalloc(sizeof(struct hpfs_fc_context), GFP_KERNEL);
if (!ctx)
return -ENOMEM;
if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
struct super_block *sb = fc->root->d_sb;
struct hpfs_sb_info *sbi = hpfs_sb(sb);
ctx->uid = sbi->sb_uid;
ctx->gid = sbi->sb_gid;
ctx->umask = 0777 & ~sbi->sb_mode;
ctx->lowercase = sbi->sb_lowercase;
ctx->eas = sbi->sb_eas;
ctx->chk = sbi->sb_chk;
ctx->chkdsk = sbi->sb_chkdsk;
ctx->errs = sbi->sb_err;
ctx->timeshift = sbi->sb_timeshift;
} else {
ctx->uid = current_uid();
ctx->gid = current_gid();
ctx->umask = current_umask();
ctx->lowercase = 0;
ctx->eas = 2;
ctx->chk = 1;
ctx->errs = 1;
ctx->chkdsk = 1;
ctx->timeshift = 0;
}
fc->fs_private = ctx;
fc->ops = &hpfs_fc_context_ops;
return 0;
};
static struct file_system_type hpfs_fs_type = { static struct file_system_type hpfs_fs_type = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.name = "hpfs", .name = "hpfs",
.mount = hpfs_mount,
.kill_sb = kill_block_super, .kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV, .fs_flags = FS_REQUIRES_DEV,
.init_fs_context = hpfs_init_fs_context,
.parameters = hpfs_param_spec,
}; };
MODULE_ALIAS_FS("hpfs"); MODULE_ALIAS_FS("hpfs");