diff -NurP --minimal linux-2.4.22-dm/drivers/md/dm-snapshot.c linux-2.4.22-dm-vfs/drivers/md/dm-snapshot.c --- linux-2.4.22-dm/drivers/md/dm-snapshot.c 2003-07-29 23:37:42.000000000 +0200 +++ linux-2.4.22-dm-vfs/drivers/md/dm-snapshot.c 2003-07-29 23:40:29.000000000 +0200 @@ -525,7 +525,7 @@ } /* Flush IO to the origin device */ - fsync_dev(s->origin->dev); + fsync_dev_lockfs(s->origin->dev); /* Add snapshot to the list of snapshots for this origin */ if (register_snapshot(s)) { @@ -533,11 +533,13 @@ ti->error = "Cannot register snapshot origin"; goto bad6; } + unlockfs(s->origin->dev); ti->private = s; return 0; bad6: + unlockfs(s->origin->dev); kcopyd_client_destroy(s->kcopyd_client); bad5: diff -NurP --minimal linux-2.4.22-dm/fs/buffer.c linux-2.4.22-dm-vfs/fs/buffer.c --- linux-2.4.22-dm/fs/buffer.c 2003-07-29 23:37:42.000000000 +0200 +++ linux-2.4.22-dm-vfs/fs/buffer.c 2003-07-29 23:40:29.000000000 +0200 @@ -376,6 +376,38 @@ fsync_dev(dev); } +int fsync_dev_lockfs(kdev_t dev) +{ + /* you are not allowed to try locking all the filesystems + ** on the system, your chances of getting through without + ** total deadlock are slim to none. + */ + if (!dev) + return fsync_dev(dev); + + sync_buffers(dev, 0); + + lock_kernel(); + /* note, the FS might need to start transactions to + ** sync the inodes, or the quota, no locking until + ** after these are done + */ + sync_inodes(dev); +#ifdef DQUOT_SYNC_DEV + DQUOT_SYNC_DEV(dev); +#else + DQUOT_SYNC(dev); +#endif + /* if inodes or quotas could be dirtied during the + ** sync_supers_lockfs call, the FS is responsible for getting + ** them on disk, without deadlocking against the lock + */ + sync_supers_lockfs(dev); + unlock_kernel(); + + return sync_buffers(dev, 1); +} + asmlinkage long sys_sync(void) { fsync_dev(0); diff -NurP --minimal linux-2.4.22-dm/fs/super.c linux-2.4.22-dm-vfs/fs/super.c --- linux-2.4.22-dm/fs/super.c 2003-07-29 22:34:33.000000000 +0200 +++ linux-2.4.22-dm-vfs/fs/super.c 2003-07-29 23:40:29.000000000 +0200 @@ -38,6 +38,13 @@ LIST_HEAD(super_blocks); spinlock_t sb_lock = SPIN_LOCK_UNLOCKED; +/* + * lock/unlockfs grab a read lock on s_umount, but you need this lock to + * make sure no lockfs runs are in progress before inserting/removing + * supers from the list. + */ +static DECLARE_MUTEX(lockfs_sem); + /* * Handling of filesystem drivers list. * Rules: @@ -436,6 +443,26 @@ put_super(sb); } +static void write_super_lockfs(struct super_block *sb) +{ + lock_super(sb); + if (sb->s_root && sb->s_op) { + if (sb->s_dirt) { + if (sb->s_op->write_super) + sb->s_op->write_super(sb); + if (sb->s_op->sync_fs) { + unlock_super(sb); + sb->s_op->sync_fs(sb); + lock_super(sb); + } + } + if (sb->s_op->write_super_lockfs) { + sb->s_op->write_super_lockfs(sb); + } + } + unlock_super(sb); +} + static inline void write_super(struct super_block *sb) { lock_super(sb); @@ -483,6 +510,39 @@ spin_unlock(&sb_lock); } +/* + * Note: don't check the dirty flag before waiting, we want the lock + * to happen every time this is called. dev must be non-zero + */ +void sync_supers_lockfs(kdev_t dev) +{ + struct super_block * sb; + + down(&lockfs_sem) ; + if (dev) { + sb = get_super(dev); + if (sb) { + write_super_lockfs(sb); + drop_super(sb); + } + } +} + +void unlockfs(kdev_t dev) +{ + struct super_block * sb; + + if (dev) { + sb = get_super(dev); + if (sb) { + if (sb->s_op && sb->s_op->unlockfs) + sb->s_op->unlockfs(sb) ; + drop_super(sb); + } + } + up(&lockfs_sem) ; +} + /** * get_super - get the superblock of a device * @dev: device to get the superblock for @@ -702,6 +762,7 @@ goto out1; error = -EBUSY; + down(&lockfs_sem); restart: spin_lock(&sb_lock); @@ -713,6 +774,7 @@ ((flags ^ old->s_flags) & MS_RDONLY)) { spin_unlock(&sb_lock); destroy_super(s); + up(&lockfs_sem); goto out1; } if (!grab_super(old)) @@ -720,12 +782,14 @@ destroy_super(s); blkdev_put(bdev, BDEV_FS); path_release(&nd); + up(&lockfs_sem); return old; } s->s_dev = dev; s->s_bdev = bdev; s->s_flags = flags; insert_super(s, fs_type); + up(&lockfs_sem); if (!fs_type->read_super(s, data, flags & MS_VERBOSE ? 1 : 0)) goto Einval; s->s_flags |= MS_ACTIVE; @@ -833,7 +897,10 @@ if (!deactivate_super(sb)) return; + down(&lockfs_sem); down_write(&sb->s_umount); + up(&lockfs_sem); + sb->s_root = NULL; /* Need to clean after the sucker */ if (fs->fs_flags & FS_LITTER)