Skip to contentSkip to conversation

Implement generic freeze feature

PATCH In review Takashi Sato 2 participants 3 messages Sep 8, 2008 – Sep 11, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Implement generic freeze feature

The ioctls for the generic freeze feature are below.
o Freeze the filesystem
int ioctl(int fd, int FIFREEZE, arg)
fd: The file descriptor of the mountpoint
FIFREEZE: request code for the freeze
arg: Ignored
Return value: 0 if the operation succeeds. Otherwise, -1

o Unfreeze the filesystem
int ioctl(int fd, int FITHAW, arg)
fd: The file descriptor of the mountpoint
FITHAW: request code for unfreeze
arg: Ignored
Return value: 0 if the operation succeeds. Otherwise, -1

---
 fs/block_dev.c              |    2 +
 fs/buffer.c                 |   34 ++++++++++++++++++++++++++++++-
 fs/ioctl.c                  |   47 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/buffer_head.h |    2 -
 include/linux/fs.h          |    7 ++++++
 5 files changed, 90 insertions(+), 2 deletions(-)

diff -uprN -X linux-2.6.27-rc5.org/Documentation/dontdiff linux-2.6.27-rc5.org/fs/block_dev.c linux-2.6.27-rc5-freeze/fs /block_dev.c

fs/block_dev.c +2 -0
@@ -285,6 +285,8 @@ static void init_once(void *foo)
 	INIT_LIST_HEAD(&bdev->bd_holder_list);
 #endif
 	inode_init_once(&ei->vfs_inode);
+	/* Initialize mutex for freeze. */
+	mutex_init(&bdev->bd_fsfreeze_mutex);
 }
 
 static inline void __bd_forget(struct inode *inode)

diff -uprN -X linux-2.6.27-rc5.org/Documentation/dontdiff linux-2.6.27-rc5.org/fs/buffer.c linux-2.6.27-rc5-freeze/fs/bu ffer.c

fs/buffer.c +33 -1
@@ -196,11 +196,25 @@ int fsync_bdev(struct block_device *bdev
  * happen on bdev until thaw_bdev() is called.
  * If a superblock is found on this device, we take the s_umount semaphore
  * on it to make sure nobody unmounts until the snapshot creation is done.
+ * The reference counter (bd_fsfreeze_count) guarantees that only the last
+ * unfreeze process can unfreeze the frozen filesystem actually when multiple
+ * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
+ * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
+ * actually.
  */
 struct super_block *freeze_bdev(struct block_device *bdev)
 {
 	struct super_block *sb;
 
+	mutex_lock(&bdev->bd_fsfreeze_mutex);
+	if (bdev->bd_fsfreeze_count > 0) {
+		bdev->bd_fsfreeze_count++;
+		sb = get_super(bdev);
+		mutex_unlock(&bdev->bd_fsfreeze_mutex);
+		return sb;
+	}
+	bdev->bd_fsfreeze_count++;
+
 	down(&bdev->bd_mount_sem);
 	sb = get_super(bdev);
 	if (sb && !(sb->s_flags & MS_RDONLY)) {
@@ -219,6 +233,8 @@ struct super_block *freeze_bdev(struct b
 	}
 
 	sync_blockdev(bdev);
+	mutex_unlock(&bdev->bd_fsfreeze_mutex);
+
 	return sb;	/* thaw_bdev releases s->s_umount and bd_mount_sem */
 }
 EXPORT_SYMBOL(freeze_bdev);
@@ -230,8 +246,22 @@ EXPORT_SYMBOL(freeze_bdev);
  *
  * Unlocks the filesystem and marks it writeable again after freeze_bdev().
  */
-void thaw_bdev(struct block_device *bdev, struct super_block *sb)
+int thaw_bdev(struct block_device *bdev, struct super_block *sb)
 {
+	mutex_lock(&bdev->bd_fsfreeze_mutex);
+	if (!bdev->bd_fsfreeze_count) {
+		mutex_unlock(&bdev->bd_fsfreeze_mutex);
+		return 0;
+	}
+
+	bdev->bd_fsfreeze_count--;
+	if (bdev->bd_fsfreeze_count > 0) {
+		if (sb)
+			drop_super(sb);
+		mutex_unlock(&bdev->bd_fsfreeze_mutex);
+		return 0;
+	}
+
 	if (sb) {
 		BUG_ON(sb->s_bdev != bdev);
 
@@ -244,6 +274,8 @@ void thaw_bdev(struct block_device *bdev
 	}
 
 	up(&bdev->bd_mount_sem);
+	mutex_unlock(&bdev->bd_fsfreeze_mutex);
+	return 0;
 }
 EXPORT_SYMBOL(thaw_bdev);
 

diff -uprN -X linux-2.6.27-rc5.org/Documentation/dontdiff linux-2.6.27-rc5.org/fs/ioctl.c linux-2.6.27-rc5-freeze/fs/ioc tl.c

fs/ioctl.c +47 -0

65 lines not on this page · raw message

diff -uprN -X linux-2.6.27-rc5.org/Documentation/dontdiff linux-2.6.27-rc5.org/include/linux/buffer_head.h linux-2.6.27- rc5-freeze/include/linux/buffer_head.h

include/linux/buffer_head.h +1 -1
@@ -170,7 +170,7 @@ void __wait_on_buffer(struct buffer_head
 wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
 int fsync_bdev(struct block_device *);
 struct super_block *freeze_bdev(struct block_device *);
-void thaw_bdev(struct block_device *, struct super_block *);
+int thaw_bdev(struct block_device *, struct super_block *);
 int fsync_super(struct super_block *);
 int fsync_no_super(struct block_device *);
 struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,

diff -uprN -X linux-2.6.27-rc5.org/Documentation/dontdiff linux-2.6.27-rc5.org/include/linux/fs.h linux-2.6.27-rc5-freez e/include/linux/fs.h

include/linux/fs.h +7 -0
@@ -226,6 +226,8 @@ extern int dir_notify_enable;
 #define BMAP_IOCTL 1		/* obsolete - kept for compatibility */
 #define FIBMAP	   _IO(0x00,1)	/* bmap access */
 #define FIGETBSZ   _IO(0x00,2)	/* get the block size used for bmap */
+#define FIFREEZE	_IOWR('X', 119, int)	/* Freeze */
+#define FITHAW		_IOWR('X', 120, int)	/* Thaw */
 
 #define	FS_IOC_GETFLAGS			_IOR('f', 1, long)
 #define	FS_IOC_SETFLAGS			_IOW('f', 2, long)
@@ -574,6 +576,11 @@ struct block_device {
 	 * care to not mess up bd_private for that case.
 	 */
 	unsigned long		bd_private;
+
+	/* The counter of freeze processes */
+	int			bd_fsfreeze_count;
+	/* Mutex for freeze */
+	struct mutex		bd_fsfreeze_mutex;
 };
 
 /*
#1 lore raw

fs/block_dev.c static void init_once(void *foo) · line 289

Quoted 10 lines · Takashi Sato

diff -uprN -X linux-2.6.27-rc5.org/Documentation/dontdiff linux-2.6.27-rc5.org/fs/block_dev.c linux-2.6.27-rc5-freeze/fs /block_dev.c

fs/block_dev.c +2 -0
@@ -285,6 +285,8 @@ static void init_once(void *foo)
 	INIT_LIST_HEAD(&bdev->bd_holder_list);
 #endif
 	inode_init_once(&ei->vfs_inode);
+	/* Initialize mutex for freeze. */
+	mutex_init(&bdev->bd_fsfreeze_mutex);

Why not just freeze_mutex?

Quoted 4 lines
fs/block_dev.c +0 -0
 struct super_block *freeze_bdev(struct block_device *bdev)
 {
 	struct super_block *sb;
 

fs/block_dev.c static void init_once(void *foo)

Quoted 10 lines
fs/block_dev.c +9 -0
+	mutex_lock(&bdev->bd_fsfreeze_mutex);
+	if (bdev->bd_fsfreeze_count > 0) {
+		bdev->bd_fsfreeze_count++;
+		sb = get_super(bdev);
+		mutex_unlock(&bdev->bd_fsfreeze_mutex);
+		return sb;
+	}
+	bdev->bd_fsfreeze_count++;
+
 	down(&bdev->bd_mount_sem);

Note that we still have duplication with the bd_mount_sem. I think you should look into getting rid of it and instead do a check of the freeze_count under proper freeze_mutex protection.

Quoted 20 lines
fs/block_dev.c +15 -0
+int thaw_bdev(struct block_device *bdev, struct super_block *sb)
 {
+	mutex_lock(&bdev->bd_fsfreeze_mutex);
+	if (!bdev->bd_fsfreeze_count) {
+		mutex_unlock(&bdev->bd_fsfreeze_mutex);
+		return 0;
+	}
+
+	bdev->bd_fsfreeze_count--;
+	if (bdev->bd_fsfreeze_count > 0) {
+		if (sb)
+			drop_super(sb);
+		mutex_unlock(&bdev->bd_fsfreeze_mutex);
+		return 0;
+	}
+
 	if (sb) {
 		BUG_ON(sb->s_bdev != bdev);

fs/block_dev.c void thaw_bdev(struct block_device *bdev · line 278

Quoted 5 lines
fs/block_dev.c +2 -0
@@ -244,6 +274,8 @@ void thaw_bdev(struct block_device *bdev
 	}
 
 	up(&bdev->bd_mount_sem);
+	mutex_unlock(&bdev->bd_fsfreeze_mutex);
+	return 0;

Why do you add a return value here if we always return 0 anyway?

#2 lore raw

Hi,

Quoted 10 lines · Christoph Hellwig

fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 static void init_once(void *foo) · line 289

Quoted 8 lines
fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 renamed from fs/block_dev.c 2008-08-29 07:52:02.000000000 +0900 +2 -0
@@ -285,6 +285,8 @@ static void init_once(void *foo)
     INIT_LIST_HEAD(&bdev->bd_holder_list);
 #endif
     inode_init_once(&ei->vfs_inode);
+     /* Initialize mutex for freeze. */
+     mutex_init(&bdev->bd_fsfreeze_mutex);

Why not just freeze_mutex?

The Linux kernel has already had the name of "freezer" in the part of power-management. So I named the above mutex "fsfreeze" (filesystem freeze) to distinguish it from the existent "freezer". Andrew pointed it out.

Quoted 15 lines
Quoted 4 lines
fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 renamed from fs/block_dev.c 2008-08-29 07:52:02.000000000 +0900 +0 -0
 struct super_block *freeze_bdev(struct block_device *bdev)
 {
      struct super_block *sb;
 

fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 static void init_once(void *foo)

Quoted 10 lines
fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 renamed from fs/block_dev.c 2008-08-29 07:52:02.000000000 +0900 +9 -0
+    mutex_lock(&bdev->bd_fsfreeze_mutex);
+    if (bdev->bd_fsfreeze_count > 0) {
+        bdev->bd_fsfreeze_count++;
+        sb = get_super(bdev);
+        mutex_unlock(&bdev->bd_fsfreeze_mutex);
+        return sb;
+    }
+    bdev->bd_fsfreeze_count++;
+
     down(&bdev->bd_mount_sem);
Quoted 3 lines

Note that we still have duplication with the bd_mount_sem. I think you should look into getting rid of it and instead do a check of the freeze_count under proper freeze_mutex protection.

In the original implementation,
while the filesystem is frozen, subsequent mounts wait for unfreeze with the semaphore (bd_mount_sem).
But if we replace the semphore with the check of the freeze_count, subsequent mounts will abort.
I think the original behavior shouldn't be changed, so the existing bd_mount_sem is better.

Quoted 8 lines

fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 void thaw_bdev(struct block_device *bdev · line 278

Quoted 6 lines
fs/block_dev.c 2008-09-05 20:00:29.000000000 +0900 renamed from fs/block_dev.c 2008-08-29 07:52:02.000000000 +0900 +2 -0
@@ -244,6 +274,8 @@ void thaw_bdev(struct block_device *bdev
 }
 
       up(&bdev->bd_mount_sem);
+     mutex_unlock(&bdev->bd_fsfreeze_mutex);
+     return 0;

Why do you add a return value here if we always return 0 anyway?

I forgot to remove the unneeded return value in above old patch. But I need to implement a return value in the new patch because thaw_bdev() needs to return an IO error which occurs in unlockfs().
Eric pointed it out.

Cheers, Takashi