History

convenience helpers: vfs_get_super() and sget_fc()

the former is an analogue of mount_{single,nodev} for use in ->get_tree() instances, the latter - analogue of sget() for the same. These are fairly similar to the originals, but the callback signature for sget_fc() is different from sget() ones, so getting bits and pieces shared would be too convoluted; we might get around to that later, but for now let's just remember to keep them in sync. They do live next to each other, and changes in either won't be hard to spot.

Al Viro committed 7 years ago

Commit cb50b341 parent 3e1aeb03 files · +190 −0Browse files

Unified · Split

3 files
  1. fs/super.c +171 −0
  2. include/linux/fs.h +4 −0
  3. include/linux/fs_context.h +15 −0
fs/super.c+171−0
@@ -476,6 +476,94 @@ void generic_shutdown_super(struct super_block *sb) · 475 unmodified lines
476 476
477EXPORT_SYMBOL(generic_shutdown_super);477EXPORT_SYMBOL(generic_shutdown_super);
478 478
479/**
480 * sget_fc - Find or create a superblock
481 * @fc: Filesystem context.
482 * @test: Comparison callback
483 * @set: Setup callback
484 *
485 * Find or create a superblock using the parameters stored in the filesystem
486 * context and the two callback functions.
487 *
488 * If an extant superblock is matched, then that will be returned with an
489 * elevated reference count that the caller must transfer or discard.
490 *
491 * If no match is made, a new superblock will be allocated and basic
492 * initialisation will be performed (s_type, s_fs_info and s_id will be set and
493 * the set() callback will be invoked), the superblock will be published and it
494 * will be returned in a partially constructed state with SB_BORN and SB_ACTIVE
495 * as yet unset.
496 */
497struct super_block *sget_fc(struct fs_context *fc,
498 int (*test)(struct super_block *, struct fs_context *),
499 int (*set)(struct super_block *, struct fs_context *))
500{
501 struct super_block *s = NULL;
502 struct super_block *old;
503 struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns;
504 int err;
505
506 if (!(fc->sb_flags & SB_KERNMOUNT) &&
507 fc->purpose != FS_CONTEXT_FOR_SUBMOUNT) {
508 /* Don't allow mounting unless the caller has CAP_SYS_ADMIN
509 * over the namespace.
510 */
511 if (!(fc->fs_type->fs_flags & FS_USERNS_MOUNT)) {
512 if (!capable(CAP_SYS_ADMIN))
513 return ERR_PTR(-EPERM);
514 } else {
515 if (!ns_capable(fc->user_ns, CAP_SYS_ADMIN))
516 return ERR_PTR(-EPERM);
517 }
518 }
519
520retry:
521 spin_lock(&sb_lock);
522 if (test) {
523 hlist_for_each_entry(old, &fc->fs_type->fs_supers, s_instances) {
524 if (test(old, fc))
525 goto share_extant_sb;
526 }
527 }
528 if (!s) {
529 spin_unlock(&sb_lock);
530 s = alloc_super(fc->fs_type, fc->sb_flags, user_ns);
531 if (!s)
532 return ERR_PTR(-ENOMEM);
533 goto retry;
534 }
535
536 s->s_fs_info = fc->s_fs_info;
537 err = set(s, fc);
538 if (err) {
539 s->s_fs_info = NULL;
540 spin_unlock(&sb_lock);
541 destroy_unused_super(s);
542 return ERR_PTR(err);
543 }
544 fc->s_fs_info = NULL;
545 s->s_type = fc->fs_type;
546 strlcpy(s->s_id, s->s_type->name, sizeof(s->s_id));
547 list_add_tail(&s->s_list, &super_blocks);
548 hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
549 spin_unlock(&sb_lock);
550 get_filesystem(s->s_type);
551 register_shrinker_prepared(&s->s_shrink);
552 return s;
553
554share_extant_sb:
555 if (user_ns != old->s_user_ns) {
556 spin_unlock(&sb_lock);
557 destroy_unused_super(s);
558 return ERR_PTR(-EBUSY);
559 }
560 if (!grab_super(old))
561 goto retry;
562 destroy_unused_super(s);
563 return old;
564}
565EXPORT_SYMBOL(sget_fc);
566
479/**567/**
480 * sget_userns - find or create a superblock568 * sget_userns - find or create a superblock
481 * @type: filesystem type superblock should belong to569 * @type: filesystem type superblock should belong to
@@ -1103,6 +1191,89 @@ struct dentry *mount_ns(struct file_system_type *fs_type, · 621 unmodified lines
1103 1191
1104EXPORT_SYMBOL(mount_ns);1192EXPORT_SYMBOL(mount_ns);
1105 1193
1194int set_anon_super_fc(struct super_block *sb, struct fs_context *fc)
1195{
1196 return set_anon_super(sb, NULL);
1197}
1198EXPORT_SYMBOL(set_anon_super_fc);
1199
1200static int test_keyed_super(struct super_block *sb, struct fs_context *fc)
1201{
1202 return sb->s_fs_info == fc->s_fs_info;
1203}
1204
1205static int test_single_super(struct super_block *s, struct fs_context *fc)
1206{
1207 return 1;
1208}
1209
1210/**
1211 * vfs_get_super - Get a superblock with a search key set in s_fs_info.
1212 * @fc: The filesystem context holding the parameters
1213 * @keying: How to distinguish superblocks
1214 * @fill_super: Helper to initialise a new superblock
1215 *
1216 * Search for a superblock and create a new one if not found. The search
1217 * criterion is controlled by @keying. If the search fails, a new superblock
1218 * is created and @fill_super() is called to initialise it.
1219 *
1220 * @keying can take one of a number of values:
1221 *
1222 * (1) vfs_get_single_super - Only one superblock of this type may exist on the
1223 * system. This is typically used for special system filesystems.
1224 *
1225 * (2) vfs_get_keyed_super - Multiple superblocks may exist, but they must have
1226 * distinct keys (where the key is in s_fs_info). Searching for the same
1227 * key again will turn up the superblock for that key.
1228 *
1229 * (3) vfs_get_independent_super - Multiple superblocks may exist and are
1230 * unkeyed. Each call will get a new superblock.
1231 *
1232 * A permissions check is made by sget_fc() unless we're getting a superblock
1233 * for a kernel-internal mount or a submount.
1234 */
1235int vfs_get_super(struct fs_context *fc,
1236 enum vfs_get_super_keying keying,
1237 int (*fill_super)(struct super_block *sb,
1238 struct fs_context *fc))
1239{
1240 int (*test)(struct super_block *, struct fs_context *);
1241 struct super_block *sb;
1242
1243 switch (keying) {
1244 case vfs_get_single_super:
1245 test = test_single_super;
1246 break;
1247 case vfs_get_keyed_super:
1248 test = test_keyed_super;
1249 break;
1250 case vfs_get_independent_super:
1251 test = NULL;
1252 break;
1253 default:
1254 BUG();
1255 }
1256
1257 sb = sget_fc(fc, test, set_anon_super_fc);
1258 if (IS_ERR(sb))
1259 return PTR_ERR(sb);
1260
1261 if (!sb->s_root) {
1262 int err = fill_super(sb, fc);
1263 if (err) {
1264 deactivate_locked_super(sb);
1265 return err;
1266 }
1267
1268 sb->s_flags |= SB_ACTIVE;
1269 }
1270
1271 BUG_ON(fc->root);
1272 fc->root = dget(sb->s_root);
1273 return 0;
1274}
1275EXPORT_SYMBOL(vfs_get_super);
1276
1106#ifdef CONFIG_BLOCK1277#ifdef CONFIG_BLOCK
1107static int set_bdev_super(struct super_block *s, void *data)1278static int set_bdev_super(struct super_block *s, void *data)
1108{1279{
include/linux/fs.h+4−0
@@ -2232,8 +2232,12 @@ void kill_litter_super(struct super_block *sb); · 2231 unmodified lines
2232void deactivate_super(struct super_block *sb);2232void deactivate_super(struct super_block *sb);
2233void deactivate_locked_super(struct super_block *sb);2233void deactivate_locked_super(struct super_block *sb);
2234int set_anon_super(struct super_block *s, void *data);2234int set_anon_super(struct super_block *s, void *data);
2235int set_anon_super_fc(struct super_block *s, struct fs_context *fc);
2235int get_anon_bdev(dev_t *);2236int get_anon_bdev(dev_t *);
2236void free_anon_bdev(dev_t);2237void free_anon_bdev(dev_t);
2238struct super_block *sget_fc(struct fs_context *fc,
2239 int (*test)(struct super_block *, struct fs_context *),
2240 int (*set)(struct super_block *, struct fs_context *));
2237struct super_block *sget_userns(struct file_system_type *type,2241struct super_block *sget_userns(struct file_system_type *type,
2238 int (*test)(struct super_block *,void *),2242 int (*test)(struct super_block *,void *),
2239 int (*set)(struct super_block *,void *),2243 int (*set)(struct super_block *,void *),
include/linux/fs_context.h+15−0
@@ -83,11 +83,13 @@ struct fs_context { · 82 unmodified lines
83 const char *source; /* The source name (eg. dev path) */83 const char *source; /* The source name (eg. dev path) */
84 const char *subtype; /* The subtype to set on the superblock */84 const char *subtype; /* The subtype to set on the superblock */
85 void *security; /* Linux S&M options */85 void *security; /* Linux S&M options */
86 void *s_fs_info; /* Proposed s_fs_info */
86 unsigned int sb_flags; /* Proposed superblock flags (SB_*) */87 unsigned int sb_flags; /* Proposed superblock flags (SB_*) */
87 unsigned int sb_flags_mask; /* Superblock flags that were changed */88 unsigned int sb_flags_mask; /* Superblock flags that were changed */
88 unsigned int lsm_flags; /* Information flags from the fs to the LSM */89 unsigned int lsm_flags; /* Information flags from the fs to the LSM */
89 enum fs_context_purpose purpose:8;90 enum fs_context_purpose purpose:8;
90 bool need_free:1; /* Need to call ops->free() */91 bool need_free:1; /* Need to call ops->free() */
92 bool global:1; /* Goes into &init_user_ns */
91};93};
92 94
93struct fs_context_operations {95struct fs_context_operations {
@@ -116,6 +118,19 @@ extern int generic_parse_monolithic(struct fs_context *fc, void *data); · 22 unmodified lines
116extern int vfs_get_tree(struct fs_context *fc);118extern int vfs_get_tree(struct fs_context *fc);
117extern void put_fs_context(struct fs_context *fc);119extern void put_fs_context(struct fs_context *fc);
118 120
121/*
122 * sget() wrapper to be called from the ->get_tree() op.
123 */
124enum vfs_get_super_keying {
125 vfs_get_single_super, /* Only one such superblock may exist */
126 vfs_get_keyed_super, /* Superblocks with different s_fs_info keys may exist */
127 vfs_get_independent_super, /* Multiple independent superblocks may exist */
128};
129extern int vfs_get_super(struct fs_context *fc,
130 enum vfs_get_super_keying keying,
131 int (*fill_super)(struct super_block *sb,
132 struct fs_context *fc));
133
119#define logfc(FC, FMT, ...) pr_notice(FMT, ## __VA_ARGS__)134#define logfc(FC, FMT, ...) pr_notice(FMT, ## __VA_ARGS__)
120 135
121/**136/**

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t