History

vfs: Create fs_context-aware mount_bdev() replacement

Create a function, get_tree_bdev(), that is fs_context-aware and a ->get_tree() counterpart of mount_bdev(). It caches the block device pointer in the fs_context struct so that this information can be passed into sget_fc()'s test and set functions.

David Howells committed 7 years ago

Commit fe62c3a1 parent 533770c2 files · +99 −0Browse files

Unified · Split

2 files
  1. fs/super.c +94 −0
  2. include/linux/fs_context.h +5 −0
fs/super.c+94−0
@@ -1222,6 +1222,7 @@ int get_tree_keyed(struct fs_context *fc, · 1221 unmodified lines
12221222 EXPORT_SYMBOL(get_tree_keyed);
12231223
12241224 #ifdef CONFIG_BLOCK
1225+
12251226 static int set_bdev_super(struct super_block *s, void *data)
12261227 {
12271228 s->s_bdev = data;
@@ -1231,6 +1232,99 @@ static int set_bdev_super(struct super_block *s, void *data) · 3 unmodified lines
12311232 return 0;
12321233 }
12331234
1235+static int set_bdev_super_fc(struct super_block *s, struct fs_context *fc)
1236+{
1237+ return set_bdev_super(s, fc->sget_key);
1238+}
1239+
1240+static int test_bdev_super_fc(struct super_block *s, struct fs_context *fc)
1241+{
1242+ return s->s_bdev == fc->sget_key;
1243+}
1244+
1245+/**
1246+ * get_tree_bdev - Get a superblock based on a single block device
1247+ * @fc: The filesystem context holding the parameters
1248+ * @fill_super: Helper to initialise a new superblock
1249+ */
1250+int get_tree_bdev(struct fs_context *fc,
1251+ int (*fill_super)(struct super_block *,
1252+ struct fs_context *))
1253+{
1254+ struct block_device *bdev;
1255+ struct super_block *s;
1256+ fmode_t mode = FMODE_READ | FMODE_EXCL;
1257+ int error = 0;
1258+
1259+ if (!(fc->sb_flags & SB_RDONLY))
1260+ mode |= FMODE_WRITE;
1261+
1262+ if (!fc->source)
1263+ return invalf(fc, "No source specified");
1264+
1265+ bdev = blkdev_get_by_path(fc->source, mode, fc->fs_type);
1266+ if (IS_ERR(bdev)) {
1267+ errorf(fc, "%s: Can't open blockdev", fc->source);
1268+ return PTR_ERR(bdev);
1269+ }
1270+
1271+ /* Once the superblock is inserted into the list by sget_fc(), s_umount
1272+ * will protect the lockfs code from trying to start a snapshot while
1273+ * we are mounting
1274+ */
1275+ mutex_lock(&bdev->bd_fsfreeze_mutex);
1276+ if (bdev->bd_fsfreeze_count > 0) {
1277+ mutex_unlock(&bdev->bd_fsfreeze_mutex);
1278+ warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
1279+ return -EBUSY;
1280+ }
1281+
1282+ fc->sb_flags |= SB_NOSEC;
1283+ fc->sget_key = bdev;
1284+ s = sget_fc(fc, test_bdev_super_fc, set_bdev_super_fc);
1285+ mutex_unlock(&bdev->bd_fsfreeze_mutex);
1286+ if (IS_ERR(s))
1287+ return PTR_ERR(s);
1288+
1289+ if (s->s_root) {
1290+ /* Don't summarily change the RO/RW state. */
1291+ if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
1292+ warnf(fc, "%pg: Can't mount, would change RO state", bdev);
1293+ deactivate_locked_super(s);
1294+ blkdev_put(bdev, mode);
1295+ return -EBUSY;
1296+ }
1297+
1298+ /*
1299+ * s_umount nests inside bd_mutex during
1300+ * __invalidate_device(). blkdev_put() acquires
1301+ * bd_mutex and can't be called under s_umount. Drop
1302+ * s_umount temporarily. This is safe as we're
1303+ * holding an active reference.
1304+ */
1305+ up_write(&s->s_umount);
1306+ blkdev_put(bdev, mode);
1307+ down_write(&s->s_umount);
1308+ } else {
1309+ s->s_mode = mode;
1310+ snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1311+ sb_set_blocksize(s, block_size(bdev));
1312+ error = fill_super(s, fc);
1313+ if (error) {
1314+ deactivate_locked_super(s);
1315+ return error;
1316+ }
1317+
1318+ s->s_flags |= SB_ACTIVE;
1319+ bdev->bd_super = s;
1320+ }
1321+
1322+ BUG_ON(fc->root);
1323+ fc->root = dget(s->s_root);
1324+ return 0;
1325+}
1326+EXPORT_SYMBOL(get_tree_bdev);
1327+
12341328 static int test_bdev_super(struct super_block *s, void *data)
12351329 {
12361330 return (void *)s->s_bdev == data;
include/linux/fs_context.h+5−0
@@ -88,6 +88,7 @@ struct fs_context { · 87 unmodified lines
8888 struct mutex uapi_mutex; /* Userspace access mutex */
8989 struct file_system_type *fs_type;
9090 void *fs_private; /* The filesystem's context */
91+ void *sget_key;
9192 struct dentry *root; /* The root and superblock */
9293 struct user_namespace *user_ns; /* The user namespace for this mount */
9394 struct net *net_ns; /* The network namespace for this mount */
@@ -159,6 +160,10 @@ extern int get_tree_keyed(struct fs_context *fc, · 65 unmodified lines
159160 struct fs_context *fc),
160161 void *key);
161162
163+extern int get_tree_bdev(struct fs_context *fc,
164+ int (*fill_super)(struct super_block *sb,
165+ struct fs_context *fc));
166+
162167 extern const struct file_operations fscontext_fops;
163168
164169 /*

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