aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTakashi Sato <t-sato@yk.jp.nec.com>2009-01-09 19:40:59 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2009-01-09 19:54:42 -0500
commitfcccf502540e3d752d33b2d8e976034dee81f9f7 (patch)
tree401be4ad2fc3c535a2fc77e413b1be4a736f320c
parentc4be0c1dc4cdc37b175579be1460f15ac6495e9a (diff)
filesystem freeze: 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 Error number: If the filesystem has already been unfrozen, errno is set to EINVAL. [akpm@linux-foundation.org: fix CONFIG_BLOCK=n] Signed-off-by: Takashi Sato <t-sato@yk.jp.nec.com> Signed-off-by: Masayuki Hamaguchi <m-hamaguchi@ys.jp.nec.com> Cc: <xfs-masters@oss.sgi.com> Cc: <linux-ext4@vger.kernel.org> Cc: Christoph Hellwig <hch@lst.de> Cc: Dave Kleikamp <shaggy@austin.ibm.com> Cc: Dave Chinner <david@fromorbit.com> Cc: Alasdair G Kergon <agk@redhat.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--fs/block_dev.c2
-rw-r--r--fs/buffer.c74
-rw-r--r--fs/ioctl.c46
-rw-r--r--include/linux/buffer_head.h11
-rw-r--r--include/linux/fs.h7
5 files changed, 130 insertions, 10 deletions
diff --git a/fs/block_dev.c b/fs/block_dev.c
index ac7031f12ea5..b3c1efff5e1d 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -285,6 +285,8 @@ static void init_once(void *foo)
285 INIT_LIST_HEAD(&bdev->bd_holder_list); 285 INIT_LIST_HEAD(&bdev->bd_holder_list);
286#endif 286#endif
287 inode_init_once(&ei->vfs_inode); 287 inode_init_once(&ei->vfs_inode);
288 /* Initialize mutex for freeze. */
289 mutex_init(&bdev->bd_fsfreeze_mutex);
288} 290}
289 291
290static inline void __bd_forget(struct inode *inode) 292static inline void __bd_forget(struct inode *inode)
diff --git a/fs/buffer.c b/fs/buffer.c
index 87f9e537b8c3..b6e8b8632e2f 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -203,10 +203,25 @@ int fsync_bdev(struct block_device *bdev)
203 * happen on bdev until thaw_bdev() is called. 203 * happen on bdev until thaw_bdev() is called.
204 * If a superblock is found on this device, we take the s_umount semaphore 204 * If a superblock is found on this device, we take the s_umount semaphore
205 * on it to make sure nobody unmounts until the snapshot creation is done. 205 * on it to make sure nobody unmounts until the snapshot creation is done.
206 * The reference counter (bd_fsfreeze_count) guarantees that only the last
207 * unfreeze process can unfreeze the frozen filesystem actually when multiple
208 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
209 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
210 * actually.
206 */ 211 */
207struct super_block *freeze_bdev(struct block_device *bdev) 212struct super_block *freeze_bdev(struct block_device *bdev)
208{ 213{
209 struct super_block *sb; 214 struct super_block *sb;
215 int error = 0;
216
217 mutex_lock(&bdev->bd_fsfreeze_mutex);
218 if (bdev->bd_fsfreeze_count > 0) {
219 bdev->bd_fsfreeze_count++;
220 sb = get_super(bdev);
221 mutex_unlock(&bdev->bd_fsfreeze_mutex);
222 return sb;
223 }
224 bdev->bd_fsfreeze_count++;
210 225
211 down(&bdev->bd_mount_sem); 226 down(&bdev->bd_mount_sem);
212 sb = get_super(bdev); 227 sb = get_super(bdev);
@@ -221,11 +236,24 @@ struct super_block *freeze_bdev(struct block_device *bdev)
221 236
222 sync_blockdev(sb->s_bdev); 237 sync_blockdev(sb->s_bdev);
223 238
224 if (sb->s_op->freeze_fs) 239 if (sb->s_op->freeze_fs) {
225 sb->s_op->freeze_fs(sb); 240 error = sb->s_op->freeze_fs(sb);
241 if (error) {
242 printk(KERN_ERR
243 "VFS:Filesystem freeze failed\n");
244 sb->s_frozen = SB_UNFROZEN;
245 drop_super(sb);
246 up(&bdev->bd_mount_sem);
247 bdev->bd_fsfreeze_count--;
248 mutex_unlock(&bdev->bd_fsfreeze_mutex);
249 return ERR_PTR(error);
250 }
251 }
226 } 252 }
227 253
228 sync_blockdev(bdev); 254 sync_blockdev(bdev);
255 mutex_unlock(&bdev->bd_fsfreeze_mutex);
256
229 return sb; /* thaw_bdev releases s->s_umount and bd_mount_sem */ 257 return sb; /* thaw_bdev releases s->s_umount and bd_mount_sem */
230} 258}
231EXPORT_SYMBOL(freeze_bdev); 259EXPORT_SYMBOL(freeze_bdev);
@@ -237,20 +265,48 @@ EXPORT_SYMBOL(freeze_bdev);
237 * 265 *
238 * Unlocks the filesystem and marks it writeable again after freeze_bdev(). 266 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
239 */ 267 */
240void thaw_bdev(struct block_device *bdev, struct super_block *sb) 268int thaw_bdev(struct block_device *bdev, struct super_block *sb)
241{ 269{
270 int error = 0;
271
272 mutex_lock(&bdev->bd_fsfreeze_mutex);
273 if (!bdev->bd_fsfreeze_count) {
274 mutex_unlock(&bdev->bd_fsfreeze_mutex);
275 return -EINVAL;
276 }
277
278 bdev->bd_fsfreeze_count--;
279 if (bdev->bd_fsfreeze_count > 0) {
280 if (sb)
281 drop_super(sb);
282 mutex_unlock(&bdev->bd_fsfreeze_mutex);
283 return 0;
284 }
285
242 if (sb) { 286 if (sb) {
243 BUG_ON(sb->s_bdev != bdev); 287 BUG_ON(sb->s_bdev != bdev);
244 288 if (!(sb->s_flags & MS_RDONLY)) {
245 if (sb->s_op->unfreeze_fs) 289 if (sb->s_op->unfreeze_fs) {
246 sb->s_op->unfreeze_fs(sb); 290 error = sb->s_op->unfreeze_fs(sb);
247 sb->s_frozen = SB_UNFROZEN; 291 if (error) {
248 smp_wmb(); 292 printk(KERN_ERR
249 wake_up(&sb->s_wait_unfrozen); 293 "VFS:Filesystem thaw failed\n");
294 sb->s_frozen = SB_FREEZE_TRANS;
295 bdev->bd_fsfreeze_count++;
296 mutex_unlock(&bdev->bd_fsfreeze_mutex);
297 return error;
298 }
299 }
300 sb->s_frozen = SB_UNFROZEN;
301 smp_wmb();
302 wake_up(&sb->s_wait_unfrozen);
303 }
250 drop_super(sb); 304 drop_super(sb);
251 } 305 }
252 306
253 up(&bdev->bd_mount_sem); 307 up(&bdev->bd_mount_sem);
308 mutex_unlock(&bdev->bd_fsfreeze_mutex);
309 return 0;
254} 310}
255EXPORT_SYMBOL(thaw_bdev); 311EXPORT_SYMBOL(thaw_bdev);
256 312
diff --git a/fs/ioctl.c b/fs/ioctl.c
index cc3f1aa1cf7b..20b0a8a24c6b 100644
--- a/fs/ioctl.c
+++ b/fs/ioctl.c
@@ -439,6 +439,43 @@ static int ioctl_fioasync(unsigned int fd, struct file *filp,
439 return error; 439 return error;
440} 440}
441 441
442static int ioctl_fsfreeze(struct file *filp)
443{
444 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
445
446 if (!capable(CAP_SYS_ADMIN))
447 return -EPERM;
448
449 /* If filesystem doesn't support freeze feature, return. */
450 if (sb->s_op->freeze_fs == NULL)
451 return -EOPNOTSUPP;
452
453 /* If a blockdevice-backed filesystem isn't specified, return. */
454 if (sb->s_bdev == NULL)
455 return -EINVAL;
456
457 /* Freeze */
458 sb = freeze_bdev(sb->s_bdev);
459 if (IS_ERR(sb))
460 return PTR_ERR(sb);
461 return 0;
462}
463
464static int ioctl_fsthaw(struct file *filp)
465{
466 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
467
468 if (!capable(CAP_SYS_ADMIN))
469 return -EPERM;
470
471 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
472 if (sb->s_bdev == NULL)
473 return -EINVAL;
474
475 /* Thaw */
476 return thaw_bdev(sb->s_bdev, sb);
477}
478
442/* 479/*
443 * When you add any new common ioctls to the switches above and below 480 * When you add any new common ioctls to the switches above and below
444 * please update compat_sys_ioctl() too. 481 * please update compat_sys_ioctl() too.
@@ -486,6 +523,15 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
486 } else 523 } else
487 error = -ENOTTY; 524 error = -ENOTTY;
488 break; 525 break;
526
527 case FIFREEZE:
528 error = ioctl_fsfreeze(filp);
529 break;
530
531 case FITHAW:
532 error = ioctl_fsthaw(filp);
533 break;
534
489 default: 535 default:
490 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode)) 536 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
491 error = file_ioctl(filp, cmd, arg); 537 error = file_ioctl(filp, cmd, arg);
diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
index 8605f8a74df9..bd7ac793be19 100644
--- a/include/linux/buffer_head.h
+++ b/include/linux/buffer_head.h
@@ -171,7 +171,7 @@ void __wait_on_buffer(struct buffer_head *);
171wait_queue_head_t *bh_waitq_head(struct buffer_head *bh); 171wait_queue_head_t *bh_waitq_head(struct buffer_head *bh);
172int fsync_bdev(struct block_device *); 172int fsync_bdev(struct block_device *);
173struct super_block *freeze_bdev(struct block_device *); 173struct super_block *freeze_bdev(struct block_device *);
174void thaw_bdev(struct block_device *, struct super_block *); 174int thaw_bdev(struct block_device *, struct super_block *);
175int fsync_super(struct super_block *); 175int fsync_super(struct super_block *);
176int fsync_no_super(struct block_device *); 176int fsync_no_super(struct block_device *);
177struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block, 177struct buffer_head *__find_get_block(struct block_device *bdev, sector_t block,
@@ -346,6 +346,15 @@ static inline int remove_inode_buffers(struct inode *inode) { return 1; }
346static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; } 346static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; }
347static inline void invalidate_bdev(struct block_device *bdev) {} 347static inline void invalidate_bdev(struct block_device *bdev) {}
348 348
349static inline struct super_block *freeze_bdev(struct block_device *sb)
350{
351 return NULL;
352}
353
354static inline int thaw_bdev(struct block_device *bdev, struct super_block *sb)
355{
356 return 0;
357}
349 358
350#endif /* CONFIG_BLOCK */ 359#endif /* CONFIG_BLOCK */
351#endif /* _LINUX_BUFFER_HEAD_H */ 360#endif /* _LINUX_BUFFER_HEAD_H */
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3e59182de9df..6022f44043f2 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -234,6 +234,8 @@ struct inodes_stat_t {
234#define BMAP_IOCTL 1 /* obsolete - kept for compatibility */ 234#define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
235#define FIBMAP _IO(0x00,1) /* bmap access */ 235#define FIBMAP _IO(0x00,1) /* bmap access */
236#define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */ 236#define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */
237#define FIFREEZE _IOWR('X', 119, int) /* Freeze */
238#define FITHAW _IOWR('X', 120, int) /* Thaw */
237 239
238#define FS_IOC_GETFLAGS _IOR('f', 1, long) 240#define FS_IOC_GETFLAGS _IOR('f', 1, long)
239#define FS_IOC_SETFLAGS _IOW('f', 2, long) 241#define FS_IOC_SETFLAGS _IOW('f', 2, long)
@@ -591,6 +593,11 @@ struct block_device {
591 * care to not mess up bd_private for that case. 593 * care to not mess up bd_private for that case.
592 */ 594 */
593 unsigned long bd_private; 595 unsigned long bd_private;
596
597 /* The counter of freeze processes */
598 int bd_fsfreeze_count;
599 /* Mutex for freeze */
600 struct mutex bd_fsfreeze_mutex;
594}; 601};
595 602
596/* 603/*