Skip to content

Commit 18e9e51

Browse files
Josef BacikAl Viro
authored andcommitted
Introduce freeze_super and thaw_super for the fsfreeze ioctl
Currently the way we do freezing is by passing sb>s_bdev to freeze_bdev and then letting it do all the work. But freezing is more of an fs thing, and doesn't really have much to do with the bdev at all, all the work gets done with the super. In btrfs we do not populate s_bdev, since we can have multiple bdev's for one fs and setting s_bdev makes removing devices from a pool kind of tricky. This means that freezing a btrfs filesystem fails, which causes us to corrupt with things like tux-on-ice which use the fsfreeze mechanism. So instead of populating sb->s_bdev with a random bdev in our pool, I've broken the actual fs freezing stuff into freeze_super and thaw_super. These just take the super_block that we're freezing and does the appropriate work. It's basically just copy and pasted from freeze_bdev. I've then converted freeze_bdev over to use the new super helpers. I've tested this with ext4 and btrfs and verified everything continues to work the same as before. The only new gotcha is multiple calls to the fsfreeze ioctl will return EBUSY if the fs is already frozen. I thought this was a better solution than adding a freeze counter to the super_block, but if everybody hates this idea I'm open to suggestions. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent e1e46bf commit 18e9e51

4 files changed

Lines changed: 110 additions & 71 deletions

File tree

‎fs/block_dev.c‎

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -245,38 +245,14 @@ struct super_block *freeze_bdev(struct block_device *bdev)
245245
sb = get_active_super(bdev);
246246
if (!sb)
247247
goto out;
248-
down_write(&sb->s_umount);
249-
if (sb->s_flags & MS_RDONLY) {
250-
sb->s_frozen = SB_FREEZE_TRANS;
251-
up_write(&sb->s_umount);
248+
error = freeze_super(sb);
249+
if (error) {
250+
deactivate_super(sb);
251+
bdev->bd_fsfreeze_count--;
252252
mutex_unlock(&bdev->bd_fsfreeze_mutex);
253-
return sb;
254-
}
255-
256-
sb->s_frozen = SB_FREEZE_WRITE;
257-
smp_wmb();
258-
259-
sync_filesystem(sb);
260-
261-
sb->s_frozen = SB_FREEZE_TRANS;
262-
smp_wmb();
263-
264-
sync_blockdev(sb->s_bdev);
265-
266-
if (sb->s_op->freeze_fs) {
267-
error = sb->s_op->freeze_fs(sb);
268-
if (error) {
269-
printk(KERN_ERR
270-
"VFS:Filesystem freeze failed\n");
271-
sb->s_frozen = SB_UNFROZEN;
272-
deactivate_locked_super(sb);
273-
bdev->bd_fsfreeze_count--;
274-
mutex_unlock(&bdev->bd_fsfreeze_mutex);
275-
return ERR_PTR(error);
276-
}
253+
return ERR_PTR(error);
277254
}
278-
up_write(&sb->s_umount);
279-
255+
deactivate_super(sb);
280256
out:
281257
sync_blockdev(bdev);
282258
mutex_unlock(&bdev->bd_fsfreeze_mutex);
@@ -297,40 +273,22 @@ int thaw_bdev(struct block_device *bdev, struct super_block *sb)
297273

298274
mutex_lock(&bdev->bd_fsfreeze_mutex);
299275
if (!bdev->bd_fsfreeze_count)
300-
goto out_unlock;
276+
goto out;
301277

302278
error = 0;
303279
if (--bdev->bd_fsfreeze_count > 0)
304-
goto out_unlock;
280+
goto out;
305281

306282
if (!sb)
307-
goto out_unlock;
308-
309-
BUG_ON(sb->s_bdev != bdev);
310-
down_write(&sb->s_umount);
311-
if (sb->s_flags & MS_RDONLY)
312-
goto out_unfrozen;
313-
314-
if (sb->s_op->unfreeze_fs) {
315-
error = sb->s_op->unfreeze_fs(sb);
316-
if (error) {
317-
printk(KERN_ERR
318-
"VFS:Filesystem thaw failed\n");
319-
sb->s_frozen = SB_FREEZE_TRANS;
320-
bdev->bd_fsfreeze_count++;
321-
mutex_unlock(&bdev->bd_fsfreeze_mutex);
322-
return error;
323-
}
324-
}
325-
326-
out_unfrozen:
327-
sb->s_frozen = SB_UNFROZEN;
328-
smp_wmb();
329-
wake_up(&sb->s_wait_unfrozen);
283+
goto out;
330284

331-
if (sb)
332-
deactivate_locked_super(sb);
333-
out_unlock:
285+
error = thaw_super(sb);
286+
if (error) {
287+
bdev->bd_fsfreeze_count++;
288+
mutex_unlock(&bdev->bd_fsfreeze_mutex);
289+
return error;
290+
}
291+
out:
334292
mutex_unlock(&bdev->bd_fsfreeze_mutex);
335293
return 0;
336294
}

‎fs/ioctl.c‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,8 @@ static int ioctl_fsfreeze(struct file *filp)
525525
if (sb->s_op->freeze_fs == NULL)
526526
return -EOPNOTSUPP;
527527

528-
/* If a blockdevice-backed filesystem isn't specified, return. */
529-
if (sb->s_bdev == NULL)
530-
return -EINVAL;
531-
532528
/* Freeze */
533-
sb = freeze_bdev(sb->s_bdev);
534-
if (IS_ERR(sb))
535-
return PTR_ERR(sb);
536-
return 0;
529+
return freeze_super(sb);
537530
}
538531

539532
static int ioctl_fsthaw(struct file *filp)
@@ -543,12 +536,8 @@ static int ioctl_fsthaw(struct file *filp)
543536
if (!capable(CAP_SYS_ADMIN))
544537
return -EPERM;
545538

546-
/* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
547-
if (sb->s_bdev == NULL)
548-
return -EINVAL;
549-
550539
/* Thaw */
551-
return thaw_bdev(sb->s_bdev, sb);
540+
return thaw_super(sb);
552541
}
553542

554543
/*

‎fs/super.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,96 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
944944

945945
EXPORT_SYMBOL_GPL(vfs_kern_mount);
946946

947+
/**
948+
* freeze_super -- lock the filesystem and force it into a consistent state
949+
* @super: the super to lock
950+
*
951+
* Syncs the super to make sure the filesystem is consistent and calls the fs's
952+
* freeze_fs. Subsequent calls to this without first thawing the fs will return
953+
* -EBUSY.
954+
*/
955+
int freeze_super(struct super_block *sb)
956+
{
957+
int ret;
958+
959+
atomic_inc(&sb->s_active);
960+
down_write(&sb->s_umount);
961+
if (sb->s_frozen) {
962+
deactivate_locked_super(sb);
963+
return -EBUSY;
964+
}
965+
966+
if (sb->s_flags & MS_RDONLY) {
967+
sb->s_frozen = SB_FREEZE_TRANS;
968+
smp_wmb();
969+
up_write(&sb->s_umount);
970+
return 0;
971+
}
972+
973+
sb->s_frozen = SB_FREEZE_WRITE;
974+
smp_wmb();
975+
976+
sync_filesystem(sb);
977+
978+
sb->s_frozen = SB_FREEZE_TRANS;
979+
smp_wmb();
980+
981+
sync_blockdev(sb->s_bdev);
982+
if (sb->s_op->freeze_fs) {
983+
ret = sb->s_op->freeze_fs(sb);
984+
if (ret) {
985+
printk(KERN_ERR
986+
"VFS:Filesystem freeze failed\n");
987+
sb->s_frozen = SB_UNFROZEN;
988+
deactivate_locked_super(sb);
989+
return ret;
990+
}
991+
}
992+
up_write(&sb->s_umount);
993+
return 0;
994+
}
995+
EXPORT_SYMBOL(freeze_super);
996+
997+
/**
998+
* thaw_super -- unlock filesystem
999+
* @sb: the super to thaw
1000+
*
1001+
* Unlocks the filesystem and marks it writeable again after freeze_super().
1002+
*/
1003+
int thaw_super(struct super_block *sb)
1004+
{
1005+
int error;
1006+
1007+
down_write(&sb->s_umount);
1008+
if (sb->s_frozen == SB_UNFROZEN) {
1009+
up_write(&sb->s_umount);
1010+
return -EINVAL;
1011+
}
1012+
1013+
if (sb->s_flags & MS_RDONLY)
1014+
goto out;
1015+
1016+
if (sb->s_op->unfreeze_fs) {
1017+
error = sb->s_op->unfreeze_fs(sb);
1018+
if (error) {
1019+
printk(KERN_ERR
1020+
"VFS:Filesystem thaw failed\n");
1021+
sb->s_frozen = SB_FREEZE_TRANS;
1022+
up_write(&sb->s_umount);
1023+
return error;
1024+
}
1025+
}
1026+
1027+
out:
1028+
sb->s_frozen = SB_UNFROZEN;
1029+
smp_wmb();
1030+
wake_up(&sb->s_wait_unfrozen);
1031+
deactivate_locked_super(sb);
1032+
1033+
return 0;
1034+
}
1035+
EXPORT_SYMBOL(thaw_super);
1036+
9471037
static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
9481038
{
9491039
int err;

‎include/linux/fs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,8 @@ extern void drop_collected_mounts(struct vfsmount *);
17971797
extern int iterate_mounts(int (*)(struct vfsmount *, void *), void *,
17981798
struct vfsmount *);
17991799
extern int vfs_statfs(struct dentry *, struct kstatfs *);
1800+
extern int freeze_super(struct super_block *super);
1801+
extern int thaw_super(struct super_block *super);
18001802

18011803
extern int current_umask(void);
18021804

0 commit comments

Comments
 (0)