| @@ -476,6 +476,94 @@ void generic_shutdown_super(struct super_block *sb) · 475 unmodified lines |
| 476 | 476 | |
| 477 | 477 | EXPORT_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 | + */ |
| 497 | +struct 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 | + |
| 520 | +retry: |
| 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 | + |
| 554 | +share_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 | +} |
| 565 | +EXPORT_SYMBOL(sget_fc); |
| 566 | + |
| 479 | 567 | /** |
| 480 | 568 | * sget_userns - find or create a superblock |
| 481 | 569 | * @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 | |
| 1104 | 1192 | EXPORT_SYMBOL(mount_ns); |
| 1105 | 1193 | |
| 1194 | +int set_anon_super_fc(struct super_block *sb, struct fs_context *fc) |
| 1195 | +{ |
| 1196 | + return set_anon_super(sb, NULL); |
| 1197 | +} |
| 1198 | +EXPORT_SYMBOL(set_anon_super_fc); |
| 1199 | + |
| 1200 | +static 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 | + |
| 1205 | +static 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 | + */ |
| 1235 | +int 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 | +} |
| 1275 | +EXPORT_SYMBOL(vfs_get_super); |
| 1276 | + |
| 1106 | 1277 | #ifdef CONFIG_BLOCK |
| 1107 | 1278 | static int set_bdev_super(struct super_block *s, void *data) |
| 1108 | 1279 | { |