From patchwork Mon Oct 27 12:58:54 2008 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Sato X-Patchwork-Id: 5903 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by ozlabs.org (Postfix) with ESMTP id 0FDA3DDD0B for ; Mon, 27 Oct 2008 23:59:53 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752596AbYJ0M7l (ORCPT ); Mon, 27 Oct 2008 08:59:41 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752423AbYJ0M7l (ORCPT ); Mon, 27 Oct 2008 08:59:41 -0400 Received: from TYO202.gate.nec.co.jp ([202.32.8.206]:40166 "EHLO tyo202.gate.nec.co.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752368AbYJ0M7j (ORCPT ); Mon, 27 Oct 2008 08:59:39 -0400 Received: from mailgate3.nec.co.jp ([10.7.69.162]) by tyo202.gate.nec.co.jp (8.13.8/8.13.4) with ESMTP id m9RCwtjU011553; Mon, 27 Oct 2008 21:58:55 +0900 (JST) Received: (from root@localhost) by mailgate3.nec.co.jp (8.11.7/3.7W-MAILGATE-NEC) id m9RCwtt12670; Mon, 27 Oct 2008 21:58:55 +0900 (JST) Received: from togyo.jp.nec.com (togyo.jp.nec.com [10.26.220.4]) by mailsv.nec.co.jp (8.13.8/8.13.4) with ESMTP id m9RCwtkl021995; Mon, 27 Oct 2008 21:58:55 +0900 (JST) Received: from TNESB07336 ([10.64.168.65] [10.64.168.65]) by mail.jp.nec.com with ESMTP; Mon, 27 Oct 2008 21:58:54 +0900 To: Andrew Morton , Christoph Hellwig , "linux-fsdevel@vger.kernel.org" , "dm-devel@redhat.com" , "viro@ZenIV.linux.org.uk" , "linux-ext4@vger.kernel.org" , "xfs@oss.sgi.com" , "mtk.manpages@googlemail.com" , "axboe@kernel.dk" Cc: "linux-kernel@vger.kernel.org" Subject: [PATCH 2/3] Implement generic freeze feature Message-Id: <20081027215855t-sato@mail.jp.nec.com> Mime-Version: 1.0 X-Mailer: WeMail32[2.51] ID:1K0086 From: Takashi Sato Date: Mon, 27 Oct 2008 21:58:54 +0900 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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 Error number: If the filesystem has already been unfrozen, errno is set to EINVAL. Signed-off-by: Takashi Sato Signed-off-by: Masayuki Hamaguchi --- fs/block_dev.c | 2 + fs/buffer.c | 74 ++++++++++++++++++++++++++++++++++++++------ fs/ioctl.c | 46 +++++++++++++++++++++++++++ include/linux/buffer_head.h | 2 - include/linux/fs.h | 7 ++++ 5 files changed, 121 insertions(+), 10 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff -uprN -X linux-2.6.28-rc2-lockfs/Documentation/dontdiff linux-2.6.28-rc2-lockfs/fs/block_dev.c linux-2.6.28-rc2-fre eze/fs/block_dev.c --- linux-2.6.28-rc2-lockfs/fs/block_dev.c 2008-10-27 04:13:29.000000000 +0900 +++ linux-2.6.28-rc2-freeze/fs/block_dev.c 2008-10-27 10:12:47.000000000 +0900 @@ -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.28-rc2-lockfs/Documentation/dontdiff linux-2.6.28-rc2-lockfs/fs/buffer.c linux-2.6.28-rc2-freeze /fs/buffer.c --- linux-2.6.28-rc2-lockfs/fs/buffer.c 2008-10-27 08:58:36.000000000 +0900 +++ linux-2.6.28-rc2-freeze/fs/buffer.c 2008-10-27 10:12:47.000000000 +0900 @@ -195,10 +195,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; + int error = 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); sb = get_super(bdev); @@ -213,11 +228,24 @@ struct super_block *freeze_bdev(struct b sync_blockdev(sb->s_bdev); - if (sb->s_op->freeze_fs) - sb->s_op->freeze_fs(sb); + if (sb->s_op->freeze_fs) { + error = sb->s_op->freeze_fs(sb); + if (error) { + printk(KERN_ERR + "VFS:Filesystem freeze failed\n"); + sb->s_frozen = SB_UNFROZEN; + drop_super(sb); + up(&bdev->bd_mount_sem); + bdev->bd_fsfreeze_count--; + mutex_unlock(&bdev->bd_fsfreeze_mutex); + return ERR_PTR(error); + } + } } 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); @@ -229,20 +257,48 @@ 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) { + int error = 0; + + mutex_lock(&bdev->bd_fsfreeze_mutex); + if (!bdev->bd_fsfreeze_count) { + mutex_unlock(&bdev->bd_fsfreeze_mutex); + return -EINVAL; + } + + 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); - - if (sb->s_op->unfreeze_fs) - sb->s_op->unfreeze_fs(sb); - sb->s_frozen = SB_UNFROZEN; - smp_wmb(); - wake_up(&sb->s_wait_unfrozen); + if (!(sb->s_flags & MS_RDONLY)) { + if (sb->s_op->unfreeze_fs) { + error = sb->s_op->unfreeze_fs(sb); + if (error) { + printk(KERN_ERR + "VFS:Filesystem thaw failed\n"); + sb->s_frozen = SB_FREEZE_TRANS; + bdev->bd_fsfreeze_count++; + mutex_unlock(&bdev->bd_fsfreeze_mutex); + return error; + } + } + sb->s_frozen = SB_UNFROZEN; + smp_wmb(); + wake_up(&sb->s_wait_unfrozen); + } drop_super(sb); } up(&bdev->bd_mount_sem); + mutex_unlock(&bdev->bd_fsfreeze_mutex); + return 0; } EXPORT_SYMBOL(thaw_bdev); diff -uprN -X linux-2.6.28-rc2-lockfs/Documentation/dontdiff linux-2.6.28-rc2-lockfs/fs/ioctl.c linux-2.6.28-rc2-freeze/ fs/ioctl.c --- linux-2.6.28-rc2-lockfs/fs/ioctl.c 2008-10-27 04:13:29.000000000 +0900 +++ linux-2.6.28-rc2-freeze/fs/ioctl.c 2008-10-27 10:12:47.000000000 +0900 @@ -417,6 +417,43 @@ static int ioctl_fioasync(unsigned int f return error; } +static int ioctl_fsfreeze(struct file *filp) +{ + struct super_block *sb = filp->f_path.dentry->d_inode->i_sb; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + /* If filesystem doesn't support freeze feature, return. */ + if (sb->s_op->freeze_fs == NULL) + return -EOPNOTSUPP; + + /* If a blockdevice-backed filesystem isn't specified, return. */ + if (sb->s_bdev == NULL) + return -EINVAL; + + /* Freeze */ + sb = freeze_bdev(sb->s_bdev); + if (IS_ERR(sb)) + return PTR_ERR(sb); + return 0; +} + +static int ioctl_fsthaw(struct file *filp) +{ + struct super_block *sb = filp->f_path.dentry->d_inode->i_sb; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */ + if (sb->s_bdev == NULL) + return -EINVAL; + + /* Thaw */ + return thaw_bdev(sb->s_bdev, sb); +} + /* * When you add any new common ioctls to the switches above and below * please update compat_sys_ioctl() too. @@ -458,6 +495,15 @@ int do_vfs_ioctl(struct file *filp, unsi } else error = -ENOTTY; break; + + case FIFREEZE: + error = ioctl_fsfreeze(filp); + break; + + case FITHAW: + error = ioctl_fsthaw(filp); + break; + default: if (S_ISREG(filp->f_path.dentry->d_inode->i_mode)) error = file_ioctl(filp, cmd, arg); diff -uprN -X linux-2.6.28-rc2-lockfs/Documentation/dontdiff linux-2.6.28-rc2-lockfs/include/linux/buffer_head.h linux-2 .6.28-rc2-freeze/include/linux/buffer_head.h --- linux-2.6.28-rc2-lockfs/include/linux/buffer_head.h 2008-10-27 04:13:29.000000000 +0900 +++ linux-2.6.28-rc2-freeze/include/linux/buffer_head.h 2008-10-27 10:12:47.000000000 +0900 @@ -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.28-rc2-lockfs/Documentation/dontdiff linux-2.6.28-rc2-lockfs/include/linux/fs.h linux-2.6.28-rc2 -freeze/include/linux/fs.h --- linux-2.6.28-rc2-lockfs/include/linux/fs.h 2008-10-27 08:58:36.000000000 +0900 +++ linux-2.6.28-rc2-freeze/include/linux/fs.h 2008-10-27 10:12:47.000000000 +0900 @@ -234,6 +234,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) @@ -584,6 +586,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; }; /*