hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/super.c
....@@ -36,6 +36,8 @@
3636 #include <linux/fsnotify.h>
3737 #include <linux/lockdep.h>
3838 #include <linux/user_namespace.h>
39
+#include <linux/fs_context.h>
40
+#include <uapi/linux/mount.h>
3941 #include "internal.h"
4042
4143 static int thaw_super_locked(struct super_block *sb);
....@@ -256,6 +258,8 @@
256258 s->s_maxbytes = MAX_NON_LFS;
257259 s->s_op = &default_op;
258260 s->s_time_gran = 1000000000;
261
+ s->s_time_min = TIME64_MIN;
262
+ s->s_time_max = TIME64_MAX;
259263 s->cleancache_poolid = CLEANCACHE_NO_POOL;
260264
261265 s->s_shrink.seeks = DEFAULT_SEEKS;
....@@ -289,7 +293,7 @@
289293 WARN_ON(s->s_inode_lru.node);
290294 WARN_ON(!list_empty(&s->s_mounts));
291295 security_sb_free(s);
292
- fscrypt_sb_free(s);
296
+ fscrypt_destroy_keyring(s);
293297 put_user_ns(s->s_user_ns);
294298 kfree(s->s_subtype);
295299 call_rcu(&s->rcu, destroy_super_rcu);
....@@ -357,7 +361,7 @@
357361 */
358362 void deactivate_super(struct super_block *s)
359363 {
360
- if (!atomic_add_unless(&s->s_active, -1, 1)) {
364
+ if (!atomic_add_unless(&s->s_active, -1, 1)) {
361365 down_write(&s->s_umount);
362366 deactivate_locked_super(s);
363367 }
....@@ -444,10 +448,13 @@
444448 sync_filesystem(sb);
445449 sb->s_flags &= ~SB_ACTIVE;
446450
447
- fsnotify_unmount_inodes(sb);
448451 cgroup_writeback_umount();
449452
453
+ /* evict all inodes with zero refcount */
450454 evict_inodes(sb);
455
+ /* only nonzero refcount inodes can have marks */
456
+ fsnotify_sb_delete(sb);
457
+ fscrypt_destroy_keyring(sb);
451458
452459 if (sb->s_dio_done_wq) {
453460 destroy_workqueue(sb->s_dio_done_wq);
....@@ -476,29 +483,115 @@
476483
477484 EXPORT_SYMBOL(generic_shutdown_super);
478485
486
+bool mount_capable(struct fs_context *fc)
487
+{
488
+ if (!(fc->fs_type->fs_flags & FS_USERNS_MOUNT))
489
+ return capable(CAP_SYS_ADMIN);
490
+ else
491
+ return ns_capable(fc->user_ns, CAP_SYS_ADMIN);
492
+}
493
+
479494 /**
480
- * sget_userns - find or create a superblock
481
- * @type: filesystem type superblock should belong to
482
- * @test: comparison callback
483
- * @set: setup callback
484
- * @flags: mount flags
485
- * @user_ns: User namespace for the super_block
486
- * @data: argument to each of them
495
+ * sget_fc - Find or create a superblock
496
+ * @fc: Filesystem context.
497
+ * @test: Comparison callback
498
+ * @set: Setup callback
499
+ *
500
+ * Find or create a superblock using the parameters stored in the filesystem
501
+ * context and the two callback functions.
502
+ *
503
+ * If an extant superblock is matched, then that will be returned with an
504
+ * elevated reference count that the caller must transfer or discard.
505
+ *
506
+ * If no match is made, a new superblock will be allocated and basic
507
+ * initialisation will be performed (s_type, s_fs_info and s_id will be set and
508
+ * the set() callback will be invoked), the superblock will be published and it
509
+ * will be returned in a partially constructed state with SB_BORN and SB_ACTIVE
510
+ * as yet unset.
487511 */
488
-struct super_block *sget_userns(struct file_system_type *type,
512
+struct super_block *sget_fc(struct fs_context *fc,
513
+ int (*test)(struct super_block *, struct fs_context *),
514
+ int (*set)(struct super_block *, struct fs_context *))
515
+{
516
+ struct super_block *s = NULL;
517
+ struct super_block *old;
518
+ struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns;
519
+ int err;
520
+
521
+retry:
522
+ spin_lock(&sb_lock);
523
+ if (test) {
524
+ hlist_for_each_entry(old, &fc->fs_type->fs_supers, s_instances) {
525
+ if (test(old, fc))
526
+ goto share_extant_sb;
527
+ }
528
+ }
529
+ if (!s) {
530
+ spin_unlock(&sb_lock);
531
+ s = alloc_super(fc->fs_type, fc->sb_flags, user_ns);
532
+ if (!s)
533
+ return ERR_PTR(-ENOMEM);
534
+ goto retry;
535
+ }
536
+
537
+ s->s_fs_info = fc->s_fs_info;
538
+ err = set(s, fc);
539
+ if (err) {
540
+ s->s_fs_info = NULL;
541
+ spin_unlock(&sb_lock);
542
+ destroy_unused_super(s);
543
+ return ERR_PTR(err);
544
+ }
545
+ fc->s_fs_info = NULL;
546
+ s->s_type = fc->fs_type;
547
+ s->s_iflags |= fc->s_iflags;
548
+ strlcpy(s->s_id, s->s_type->name, sizeof(s->s_id));
549
+ list_add_tail(&s->s_list, &super_blocks);
550
+ hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
551
+ spin_unlock(&sb_lock);
552
+ get_filesystem(s->s_type);
553
+ register_shrinker_prepared(&s->s_shrink);
554
+ return s;
555
+
556
+share_extant_sb:
557
+ if (user_ns != old->s_user_ns) {
558
+ spin_unlock(&sb_lock);
559
+ destroy_unused_super(s);
560
+ return ERR_PTR(-EBUSY);
561
+ }
562
+ if (!grab_super(old))
563
+ goto retry;
564
+ destroy_unused_super(s);
565
+ return old;
566
+}
567
+EXPORT_SYMBOL(sget_fc);
568
+
569
+/**
570
+ * sget - find or create a superblock
571
+ * @type: filesystem type superblock should belong to
572
+ * @test: comparison callback
573
+ * @set: setup callback
574
+ * @flags: mount flags
575
+ * @data: argument to each of them
576
+ */
577
+struct super_block *sget(struct file_system_type *type,
489578 int (*test)(struct super_block *,void *),
490579 int (*set)(struct super_block *,void *),
491
- int flags, struct user_namespace *user_ns,
580
+ int flags,
492581 void *data)
493582 {
583
+ struct user_namespace *user_ns = current_user_ns();
494584 struct super_block *s = NULL;
495585 struct super_block *old;
496586 int err;
497587
498
- if (!(flags & (SB_KERNMOUNT|SB_SUBMOUNT)) &&
499
- !(type->fs_flags & FS_USERNS_MOUNT) &&
500
- !capable(CAP_SYS_ADMIN))
501
- return ERR_PTR(-EPERM);
588
+ /* We don't yet pass the user namespace of the parent
589
+ * mount through to here so always use &init_user_ns
590
+ * until that changes.
591
+ */
592
+ if (flags & SB_SUBMOUNT)
593
+ user_ns = &init_user_ns;
594
+
502595 retry:
503596 spin_lock(&sb_lock);
504597 if (test) {
....@@ -539,39 +632,6 @@
539632 register_shrinker_prepared(&s->s_shrink);
540633 return s;
541634 }
542
-
543
-EXPORT_SYMBOL(sget_userns);
544
-
545
-/**
546
- * sget - find or create a superblock
547
- * @type: filesystem type superblock should belong to
548
- * @test: comparison callback
549
- * @set: setup callback
550
- * @flags: mount flags
551
- * @data: argument to each of them
552
- */
553
-struct super_block *sget(struct file_system_type *type,
554
- int (*test)(struct super_block *,void *),
555
- int (*set)(struct super_block *,void *),
556
- int flags,
557
- void *data)
558
-{
559
- struct user_namespace *user_ns = current_user_ns();
560
-
561
- /* We don't yet pass the user namespace of the parent
562
- * mount through to here so always use &init_user_ns
563
- * until that changes.
564
- */
565
- if (flags & SB_SUBMOUNT)
566
- user_ns = &init_user_ns;
567
-
568
- /* Ensure the requestor has permissions over the target filesystem */
569
- if (!(flags & (SB_KERNMOUNT|SB_SUBMOUNT)) && !ns_capable(user_ns, CAP_SYS_ADMIN))
570
- return ERR_PTR(-EPERM);
571
-
572
- return sget_userns(type, test, set, flags, user_ns, data);
573
-}
574
-
575635 EXPORT_SYMBOL(sget);
576636
577637 void drop_super(struct super_block *sb)
....@@ -836,29 +896,36 @@
836896 }
837897
838898 /**
839
- * do_remount_sb2 - asks filesystem to change mount options.
840
- * @mnt: mount we are looking at
841
- * @sb: superblock in question
842
- * @sb_flags: revised superblock flags
843
- * @data: the rest of options
844
- * @force: whether or not to force the change
899
+ * reconfigure_super - asks filesystem to change superblock parameters
900
+ * @fc: The superblock and configuration
845901 *
846
- * Alters the mount options of a mounted file system.
902
+ * Alters the configuration parameters of a live superblock.
847903 */
848
-int do_remount_sb2(struct vfsmount *mnt, struct super_block *sb, int sb_flags, void *data, int force)
904
+int reconfigure_super(struct fs_context *fc)
849905 {
906
+ struct super_block *sb = fc->root->d_sb;
850907 int retval;
851
- int remount_ro;
908
+ bool remount_ro = false;
909
+ bool remount_rw = false;
910
+ bool force = fc->sb_flags & SB_FORCE;
852911
912
+ if (fc->sb_flags_mask & ~MS_RMT_MASK)
913
+ return -EINVAL;
853914 if (sb->s_writers.frozen != SB_UNFROZEN)
854915 return -EBUSY;
855916
856
-#ifdef CONFIG_BLOCK
857
- if (!(sb_flags & SB_RDONLY) && bdev_read_only(sb->s_bdev))
858
- return -EACCES;
859
-#endif
917
+ retval = security_sb_remount(sb, fc->security);
918
+ if (retval)
919
+ return retval;
860920
861
- remount_ro = (sb_flags & SB_RDONLY) && !sb_rdonly(sb);
921
+ if (fc->sb_flags_mask & SB_RDONLY) {
922
+#ifdef CONFIG_BLOCK
923
+ if (!(fc->sb_flags & SB_RDONLY) && bdev_read_only(sb->s_bdev))
924
+ return -EACCES;
925
+#endif
926
+ remount_rw = !(fc->sb_flags & SB_RDONLY) && sb_rdonly(sb);
927
+ remount_ro = (fc->sb_flags & SB_RDONLY) && !sb_rdonly(sb);
928
+ }
862929
863930 if (remount_ro) {
864931 if (!hlist_empty(&sb->s_pins)) {
....@@ -869,13 +936,14 @@
869936 return 0;
870937 if (sb->s_writers.frozen != SB_UNFROZEN)
871938 return -EBUSY;
872
- remount_ro = (sb_flags & SB_RDONLY) && !sb_rdonly(sb);
939
+ remount_ro = !sb_rdonly(sb);
873940 }
874941 }
875942 shrink_dcache_sb(sb);
876943
877
- /* If we are remounting RDONLY and current sb is read/write,
878
- make sure there are no rw files opened */
944
+ /* If we are reconfiguring to RDONLY and current sb is read/write,
945
+ * make sure there are no files open for writing.
946
+ */
879947 if (remount_ro) {
880948 if (force) {
881949 sb->s_readonly_remount = 1;
....@@ -885,19 +953,18 @@
885953 if (retval)
886954 return retval;
887955 }
956
+ } else if (remount_rw) {
957
+ /*
958
+ * We set s_readonly_remount here to protect filesystem's
959
+ * reconfigure code from writes from userspace until
960
+ * reconfigure finishes.
961
+ */
962
+ sb->s_readonly_remount = 1;
963
+ smp_wmb();
888964 }
889965
890
- if (mnt && sb->s_op->remount_fs2) {
891
- retval = sb->s_op->remount_fs2(mnt, sb, &sb_flags, data);
892
- if (retval) {
893
- if (!force)
894
- goto cancel_readonly;
895
- /* If forced remount, go ahead despite any errors */
896
- WARN(1, "forced remount of a %s fs returned %i\n",
897
- sb->s_type->name, retval);
898
- }
899
- } else if (sb->s_op->remount_fs) {
900
- retval = sb->s_op->remount_fs(sb, &sb_flags, data);
966
+ if (fc->ops->reconfigure) {
967
+ retval = fc->ops->reconfigure(fc);
901968 if (retval) {
902969 if (!force)
903970 goto cancel_readonly;
....@@ -906,7 +973,9 @@
906973 sb->s_type->name, retval);
907974 }
908975 }
909
- sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (sb_flags & MS_RMT_MASK);
976
+
977
+ WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
978
+ (fc->sb_flags & fc->sb_flags_mask)));
910979 /* Needs to be ordered wrt mnt_is_readonly() */
911980 smp_wmb();
912981 sb->s_readonly_remount = 0;
....@@ -928,20 +997,20 @@
928997 return retval;
929998 }
930999
931
-int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
932
-{
933
- return do_remount_sb2(NULL, sb, flags, data, force);
934
-}
935
-
9361000 static void do_emergency_remount_callback(struct super_block *sb)
9371001 {
9381002 down_write(&sb->s_umount);
9391003 if (sb->s_root && sb->s_bdev && (sb->s_flags & SB_BORN) &&
9401004 !sb_rdonly(sb)) {
941
- /*
942
- * What lock protects sb->s_flags??
943
- */
944
- do_remount_sb(sb, SB_RDONLY, NULL, 1);
1005
+ struct fs_context *fc;
1006
+
1007
+ fc = fs_context_for_reconfigure(sb->s_root,
1008
+ SB_RDONLY | SB_FORCE, SB_RDONLY);
1009
+ if (!IS_ERR(fc)) {
1010
+ if (parse_monolithic_mount_data(fc, NULL) == 0)
1011
+ (void)reconfigure_super(fc);
1012
+ put_fs_context(fc);
1013
+ }
9451014 }
9461015 up_write(&sb->s_umount);
9471016 }
....@@ -1059,59 +1128,244 @@
10591128 }
10601129 EXPORT_SYMBOL(kill_litter_super);
10611130
1062
-static int ns_test_super(struct super_block *sb, void *data)
1131
+int set_anon_super_fc(struct super_block *sb, struct fs_context *fc)
10631132 {
1064
- return sb->s_fs_info == data;
1065
-}
1066
-
1067
-static int ns_set_super(struct super_block *sb, void *data)
1068
-{
1069
- sb->s_fs_info = data;
10701133 return set_anon_super(sb, NULL);
10711134 }
1135
+EXPORT_SYMBOL(set_anon_super_fc);
10721136
1073
-struct dentry *mount_ns(struct file_system_type *fs_type,
1074
- int flags, void *data, void *ns, struct user_namespace *user_ns,
1075
- int (*fill_super)(struct super_block *, void *, int))
1137
+static int test_keyed_super(struct super_block *sb, struct fs_context *fc)
10761138 {
1077
- struct super_block *sb;
1078
-
1079
- /* Don't allow mounting unless the caller has CAP_SYS_ADMIN
1080
- * over the namespace.
1081
- */
1082
- if (!(flags & SB_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
1083
- return ERR_PTR(-EPERM);
1084
-
1085
- sb = sget_userns(fs_type, ns_test_super, ns_set_super, flags,
1086
- user_ns, ns);
1087
- if (IS_ERR(sb))
1088
- return ERR_CAST(sb);
1089
-
1090
- if (!sb->s_root) {
1091
- int err;
1092
- err = fill_super(sb, data, flags & SB_SILENT ? 1 : 0);
1093
- if (err) {
1094
- deactivate_locked_super(sb);
1095
- return ERR_PTR(err);
1096
- }
1097
-
1098
- sb->s_flags |= SB_ACTIVE;
1099
- }
1100
-
1101
- return dget(sb->s_root);
1139
+ return sb->s_fs_info == fc->s_fs_info;
11021140 }
11031141
1104
-EXPORT_SYMBOL(mount_ns);
1142
+static int test_single_super(struct super_block *s, struct fs_context *fc)
1143
+{
1144
+ return 1;
1145
+}
1146
+
1147
+/**
1148
+ * vfs_get_super - Get a superblock with a search key set in s_fs_info.
1149
+ * @fc: The filesystem context holding the parameters
1150
+ * @keying: How to distinguish superblocks
1151
+ * @fill_super: Helper to initialise a new superblock
1152
+ *
1153
+ * Search for a superblock and create a new one if not found. The search
1154
+ * criterion is controlled by @keying. If the search fails, a new superblock
1155
+ * is created and @fill_super() is called to initialise it.
1156
+ *
1157
+ * @keying can take one of a number of values:
1158
+ *
1159
+ * (1) vfs_get_single_super - Only one superblock of this type may exist on the
1160
+ * system. This is typically used for special system filesystems.
1161
+ *
1162
+ * (2) vfs_get_keyed_super - Multiple superblocks may exist, but they must have
1163
+ * distinct keys (where the key is in s_fs_info). Searching for the same
1164
+ * key again will turn up the superblock for that key.
1165
+ *
1166
+ * (3) vfs_get_independent_super - Multiple superblocks may exist and are
1167
+ * unkeyed. Each call will get a new superblock.
1168
+ *
1169
+ * A permissions check is made by sget_fc() unless we're getting a superblock
1170
+ * for a kernel-internal mount or a submount.
1171
+ */
1172
+int vfs_get_super(struct fs_context *fc,
1173
+ enum vfs_get_super_keying keying,
1174
+ int (*fill_super)(struct super_block *sb,
1175
+ struct fs_context *fc))
1176
+{
1177
+ int (*test)(struct super_block *, struct fs_context *);
1178
+ struct super_block *sb;
1179
+ int err;
1180
+
1181
+ switch (keying) {
1182
+ case vfs_get_single_super:
1183
+ case vfs_get_single_reconf_super:
1184
+ test = test_single_super;
1185
+ break;
1186
+ case vfs_get_keyed_super:
1187
+ test = test_keyed_super;
1188
+ break;
1189
+ case vfs_get_independent_super:
1190
+ test = NULL;
1191
+ break;
1192
+ default:
1193
+ BUG();
1194
+ }
1195
+
1196
+ sb = sget_fc(fc, test, set_anon_super_fc);
1197
+ if (IS_ERR(sb))
1198
+ return PTR_ERR(sb);
1199
+
1200
+ if (!sb->s_root) {
1201
+ err = fill_super(sb, fc);
1202
+ if (err)
1203
+ goto error;
1204
+
1205
+ sb->s_flags |= SB_ACTIVE;
1206
+ fc->root = dget(sb->s_root);
1207
+ } else {
1208
+ fc->root = dget(sb->s_root);
1209
+ if (keying == vfs_get_single_reconf_super) {
1210
+ err = reconfigure_super(fc);
1211
+ if (err < 0) {
1212
+ dput(fc->root);
1213
+ fc->root = NULL;
1214
+ goto error;
1215
+ }
1216
+ }
1217
+ }
1218
+
1219
+ return 0;
1220
+
1221
+error:
1222
+ deactivate_locked_super(sb);
1223
+ return err;
1224
+}
1225
+EXPORT_SYMBOL(vfs_get_super);
1226
+
1227
+int get_tree_nodev(struct fs_context *fc,
1228
+ int (*fill_super)(struct super_block *sb,
1229
+ struct fs_context *fc))
1230
+{
1231
+ return vfs_get_super(fc, vfs_get_independent_super, fill_super);
1232
+}
1233
+EXPORT_SYMBOL(get_tree_nodev);
1234
+
1235
+int get_tree_single(struct fs_context *fc,
1236
+ int (*fill_super)(struct super_block *sb,
1237
+ struct fs_context *fc))
1238
+{
1239
+ return vfs_get_super(fc, vfs_get_single_super, fill_super);
1240
+}
1241
+EXPORT_SYMBOL(get_tree_single);
1242
+
1243
+int get_tree_single_reconf(struct fs_context *fc,
1244
+ int (*fill_super)(struct super_block *sb,
1245
+ struct fs_context *fc))
1246
+{
1247
+ return vfs_get_super(fc, vfs_get_single_reconf_super, fill_super);
1248
+}
1249
+EXPORT_SYMBOL(get_tree_single_reconf);
1250
+
1251
+int get_tree_keyed(struct fs_context *fc,
1252
+ int (*fill_super)(struct super_block *sb,
1253
+ struct fs_context *fc),
1254
+ void *key)
1255
+{
1256
+ fc->s_fs_info = key;
1257
+ return vfs_get_super(fc, vfs_get_keyed_super, fill_super);
1258
+}
1259
+EXPORT_SYMBOL(get_tree_keyed);
11051260
11061261 #ifdef CONFIG_BLOCK
1262
+
11071263 static int set_bdev_super(struct super_block *s, void *data)
11081264 {
11091265 s->s_bdev = data;
11101266 s->s_dev = s->s_bdev->bd_dev;
11111267 s->s_bdi = bdi_get(s->s_bdev->bd_bdi);
11121268
1269
+ if (blk_queue_stable_writes(s->s_bdev->bd_disk->queue))
1270
+ s->s_iflags |= SB_I_STABLE_WRITES;
11131271 return 0;
11141272 }
1273
+
1274
+static int set_bdev_super_fc(struct super_block *s, struct fs_context *fc)
1275
+{
1276
+ return set_bdev_super(s, fc->sget_key);
1277
+}
1278
+
1279
+static int test_bdev_super_fc(struct super_block *s, struct fs_context *fc)
1280
+{
1281
+ return s->s_bdev == fc->sget_key;
1282
+}
1283
+
1284
+/**
1285
+ * get_tree_bdev - Get a superblock based on a single block device
1286
+ * @fc: The filesystem context holding the parameters
1287
+ * @fill_super: Helper to initialise a new superblock
1288
+ */
1289
+int get_tree_bdev(struct fs_context *fc,
1290
+ int (*fill_super)(struct super_block *,
1291
+ struct fs_context *))
1292
+{
1293
+ struct block_device *bdev;
1294
+ struct super_block *s;
1295
+ fmode_t mode = FMODE_READ | FMODE_EXCL;
1296
+ int error = 0;
1297
+
1298
+ if (!(fc->sb_flags & SB_RDONLY))
1299
+ mode |= FMODE_WRITE;
1300
+
1301
+ if (!fc->source)
1302
+ return invalf(fc, "No source specified");
1303
+
1304
+ bdev = blkdev_get_by_path(fc->source, mode, fc->fs_type);
1305
+ if (IS_ERR(bdev)) {
1306
+ errorf(fc, "%s: Can't open blockdev", fc->source);
1307
+ return PTR_ERR(bdev);
1308
+ }
1309
+
1310
+ /* Once the superblock is inserted into the list by sget_fc(), s_umount
1311
+ * will protect the lockfs code from trying to start a snapshot while
1312
+ * we are mounting
1313
+ */
1314
+ mutex_lock(&bdev->bd_fsfreeze_mutex);
1315
+ if (bdev->bd_fsfreeze_count > 0) {
1316
+ mutex_unlock(&bdev->bd_fsfreeze_mutex);
1317
+ warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
1318
+ blkdev_put(bdev, mode);
1319
+ return -EBUSY;
1320
+ }
1321
+
1322
+ fc->sb_flags |= SB_NOSEC;
1323
+ fc->sget_key = bdev;
1324
+ s = sget_fc(fc, test_bdev_super_fc, set_bdev_super_fc);
1325
+ mutex_unlock(&bdev->bd_fsfreeze_mutex);
1326
+ if (IS_ERR(s)) {
1327
+ blkdev_put(bdev, mode);
1328
+ return PTR_ERR(s);
1329
+ }
1330
+
1331
+ if (s->s_root) {
1332
+ /* Don't summarily change the RO/RW state. */
1333
+ if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
1334
+ warnf(fc, "%pg: Can't mount, would change RO state", bdev);
1335
+ deactivate_locked_super(s);
1336
+ blkdev_put(bdev, mode);
1337
+ return -EBUSY;
1338
+ }
1339
+
1340
+ /*
1341
+ * s_umount nests inside bd_mutex during
1342
+ * __invalidate_device(). blkdev_put() acquires
1343
+ * bd_mutex and can't be called under s_umount. Drop
1344
+ * s_umount temporarily. This is safe as we're
1345
+ * holding an active reference.
1346
+ */
1347
+ up_write(&s->s_umount);
1348
+ blkdev_put(bdev, mode);
1349
+ down_write(&s->s_umount);
1350
+ } else {
1351
+ s->s_mode = mode;
1352
+ snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1353
+ sb_set_blocksize(s, block_size(bdev));
1354
+ error = fill_super(s, fc);
1355
+ if (error) {
1356
+ deactivate_locked_super(s);
1357
+ return error;
1358
+ }
1359
+
1360
+ s->s_flags |= SB_ACTIVE;
1361
+ bdev->bd_super = s;
1362
+ }
1363
+
1364
+ BUG_ON(fc->root);
1365
+ fc->root = dget(s->s_root);
1366
+ return 0;
1367
+}
1368
+EXPORT_SYMBOL(get_tree_bdev);
11151369
11161370 static int test_bdev_super(struct super_block *s, void *data)
11171371 {
....@@ -1191,7 +1445,7 @@
11911445 error:
11921446 return ERR_PTR(error);
11931447 }
1194
-EXPORT_SYMBOL(mount_bdev);
1448
+EXPORT_SYMBOL_NS(mount_bdev, ANDROID_GKI_VFS_EXPORT_ONLY);
11951449
11961450 void kill_block_super(struct super_block *sb)
11971451 {
....@@ -1205,7 +1459,7 @@
12051459 blkdev_put(bdev, mode | FMODE_EXCL);
12061460 }
12071461
1208
-EXPORT_SYMBOL(kill_block_super);
1462
+EXPORT_SYMBOL_NS(kill_block_super, ANDROID_GKI_VFS_EXPORT_ONLY);
12091463 #endif
12101464
12111465 struct dentry *mount_nodev(struct file_system_type *fs_type,
....@@ -1228,6 +1482,31 @@
12281482 }
12291483 EXPORT_SYMBOL(mount_nodev);
12301484
1485
+int reconfigure_single(struct super_block *s,
1486
+ int flags, void *data)
1487
+{
1488
+ struct fs_context *fc;
1489
+ int ret;
1490
+
1491
+ /* The caller really need to be passing fc down into mount_single(),
1492
+ * then a chunk of this can be removed. [Bollocks -- AV]
1493
+ * Better yet, reconfiguration shouldn't happen, but rather the second
1494
+ * mount should be rejected if the parameters are not compatible.
1495
+ */
1496
+ fc = fs_context_for_reconfigure(s->s_root, flags, MS_RMT_MASK);
1497
+ if (IS_ERR(fc))
1498
+ return PTR_ERR(fc);
1499
+
1500
+ ret = parse_monolithic_mount_data(fc, data);
1501
+ if (ret < 0)
1502
+ goto out;
1503
+
1504
+ ret = reconfigure_super(fc);
1505
+out:
1506
+ put_fs_context(fc);
1507
+ return ret;
1508
+}
1509
+
12311510 static int compare_single(struct super_block *s, void *p)
12321511 {
12331512 return 1;
....@@ -1245,46 +1524,52 @@
12451524 return ERR_CAST(s);
12461525 if (!s->s_root) {
12471526 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1248
- if (error) {
1249
- deactivate_locked_super(s);
1250
- return ERR_PTR(error);
1251
- }
1252
- s->s_flags |= SB_ACTIVE;
1527
+ if (!error)
1528
+ s->s_flags |= SB_ACTIVE;
12531529 } else {
1254
- do_remount_sb(s, flags, data, 0);
1530
+ error = reconfigure_single(s, flags, data);
1531
+ }
1532
+ if (unlikely(error)) {
1533
+ deactivate_locked_super(s);
1534
+ return ERR_PTR(error);
12551535 }
12561536 return dget(s->s_root);
12571537 }
12581538 EXPORT_SYMBOL(mount_single);
12591539
1260
-struct dentry *
1261
-mount_fs(struct file_system_type *type, int flags, const char *name, struct vfsmount *mnt, void *data)
1540
+/**
1541
+ * vfs_get_tree - Get the mountable root
1542
+ * @fc: The superblock configuration context.
1543
+ *
1544
+ * The filesystem is invoked to get or create a superblock which can then later
1545
+ * be used for mounting. The filesystem places a pointer to the root to be
1546
+ * used for mounting in @fc->root.
1547
+ */
1548
+int vfs_get_tree(struct fs_context *fc)
12621549 {
1263
- struct dentry *root;
12641550 struct super_block *sb;
1265
- char *secdata = NULL;
1266
- int error = -ENOMEM;
1551
+ int error;
12671552
1268
- if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
1269
- secdata = alloc_secdata();
1270
- if (!secdata)
1271
- goto out;
1553
+ if (fc->root)
1554
+ return -EBUSY;
12721555
1273
- error = security_sb_copy_data(data, secdata);
1274
- if (error)
1275
- goto out_free_secdata;
1556
+ /* Get the mountable root in fc->root, with a ref on the root and a ref
1557
+ * on the superblock.
1558
+ */
1559
+ error = fc->ops->get_tree(fc);
1560
+ if (error < 0)
1561
+ return error;
1562
+
1563
+ if (!fc->root) {
1564
+ pr_err("Filesystem %s get_tree() didn't set fc->root\n",
1565
+ fc->fs_type->name);
1566
+ /* We don't know what the locking state of the superblock is -
1567
+ * if there is a superblock.
1568
+ */
1569
+ BUG();
12761570 }
12771571
1278
- if (type->mount2)
1279
- root = type->mount2(mnt, type, flags, name, data);
1280
- else
1281
- root = type->mount(type, flags, name, data);
1282
- if (IS_ERR(root)) {
1283
- error = PTR_ERR(root);
1284
- goto out_free_secdata;
1285
- }
1286
- sb = root->d_sb;
1287
- BUG_ON(!sb);
1572
+ sb = fc->root->d_sb;
12881573 WARN_ON(!sb->s_bdi);
12891574
12901575 /*
....@@ -1296,9 +1581,11 @@
12961581 smp_wmb();
12971582 sb->s_flags |= SB_BORN;
12981583
1299
- error = security_sb_kern_mount(sb, flags, secdata);
1300
- if (error)
1301
- goto out_sb;
1584
+ error = security_sb_set_mnt_opts(sb, fc->security, 0, NULL);
1585
+ if (unlikely(error)) {
1586
+ fc_drop_locked(fc);
1587
+ return error;
1588
+ }
13021589
13031590 /*
13041591 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
....@@ -1307,19 +1594,11 @@
13071594 * violate this rule.
13081595 */
13091596 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1310
- "negative value (%lld)\n", type->name, sb->s_maxbytes);
1597
+ "negative value (%lld)\n", fc->fs_type->name, sb->s_maxbytes);
13111598
1312
- up_write(&sb->s_umount);
1313
- free_secdata(secdata);
1314
- return root;
1315
-out_sb:
1316
- dput(root);
1317
- deactivate_locked_super(sb);
1318
-out_free_secdata:
1319
- free_secdata(secdata);
1320
-out:
1321
- return ERR_PTR(error);
1599
+ return 0;
13221600 }
1601
+EXPORT_SYMBOL(vfs_get_tree);
13231602
13241603 /*
13251604 * Setup private BDI for given superblock. It gets automatically cleaned up
....@@ -1331,11 +1610,9 @@
13311610 int err;
13321611 va_list args;
13331612
1334
- bdi = bdi_alloc(GFP_KERNEL);
1613
+ bdi = bdi_alloc(NUMA_NO_NODE);
13351614 if (!bdi)
13361615 return -ENOMEM;
1337
-
1338
- bdi->name = sb->s_type->name;
13391616
13401617 va_start(args, fmt);
13411618 err = bdi_register_va(bdi, fmt, args);
....@@ -1363,30 +1640,6 @@
13631640 atomic_long_inc_return(&bdi_seq));
13641641 }
13651642 EXPORT_SYMBOL(super_setup_bdi);
1366
-
1367
-/*
1368
- * This is an internal function, please use sb_end_{write,pagefault,intwrite}
1369
- * instead.
1370
- */
1371
-void __sb_end_write(struct super_block *sb, int level)
1372
-{
1373
- percpu_up_read(sb->s_writers.rw_sem + level-1);
1374
-}
1375
-EXPORT_SYMBOL(__sb_end_write);
1376
-
1377
-/*
1378
- * This is an internal function, please use sb_start_{write,pagefault,intwrite}
1379
- * instead.
1380
- */
1381
-int __sb_start_write(struct super_block *sb, int level, bool wait)
1382
-{
1383
- if (!wait)
1384
- return percpu_down_read_trylock(sb->s_writers.rw_sem + level-1);
1385
-
1386
- percpu_down_read(sb->s_writers.rw_sem + level-1);
1387
- return 1;
1388
-}
1389
-EXPORT_SYMBOL(__sb_start_write);
13901643
13911644 /**
13921645 * sb_wait_write - wait until all writers to given file system finish