NetBSD-5.0.2/external/gpl2/libdevmapper/dist/patches/linux-2.4.21-VFS-lock.patch

diff -ru linux-2.4.21/drivers/md/lvm.c linux/drivers/md/lvm.c
--- linux-2.4.21/drivers/md/lvm.c	Mon Jun  2 14:02:18 2003
+++ linux/drivers/md/lvm.c	Mon Jun  2 16:39:50 2003
@@ -229,9 +229,6 @@
 #define DEVICE_OFF(device)
 #define LOCAL_END_REQUEST
 
-/* lvm_do_lv_create calls fsync_dev_lockfs()/unlockfs() */
-/* #define	LVM_VFS_ENHANCEMENT */
-
 #include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
@@ -2171,12 +2168,8 @@
 	if (lv_ptr->lv_access & LV_SNAPSHOT) {
 		lv_t *org = lv_ptr->lv_snapshot_org, *last;
 
-		/* sync the original logical volume */
-		fsync_dev(org->lv_dev);
-#ifdef	LVM_VFS_ENHANCEMENT
 		/* VFS function call to sync and lock the filesystem */
 		fsync_dev_lockfs(org->lv_dev);
-#endif
 
 		down_write(&org->lv_lock);
 		org->lv_access |= LV_SNAPSHOT_ORG;
@@ -2201,11 +2194,9 @@
 	else
 		set_device_ro(lv_ptr->lv_dev, 1);
 
-#ifdef	LVM_VFS_ENHANCEMENT
 /* VFS function call to unlock the filesystem */
 	if (lv_ptr->lv_access & LV_SNAPSHOT)
 		unlockfs(lv_ptr->lv_snapshot_org->lv_dev);
-#endif
 
 	lvm_gendisk.part[MINOR(lv_ptr->lv_dev)].de =
 	    lvm_fs_create_lv(vg_ptr, lv_ptr);
diff -ru linux-2.4.21/fs/buffer.c linux/fs/buffer.c
--- linux-2.4.21/fs/buffer.c	Mon Jun  2 14:02:13 2003
+++ linux/fs/buffer.c	Mon Jun  2 17:20:56 2003
@@ -363,6 +363,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 -ru linux-2.4.21/fs/reiserfs/super.c linux/fs/reiserfs/super.c
--- linux-2.4.21/fs/reiserfs/super.c	Mon Jun  2 14:01:37 2003
+++ linux/fs/reiserfs/super.c	Mon Jun  2 16:39:50 2003
@@ -45,7 +45,7 @@
     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
     reiserfs_block_writes(&th) ;
-    journal_end(&th, s, 1) ;
+    journal_end_sync(&th, s, 1) ;
   }
   s->s_dirt = dirty;
   unlock_kernel() ;
diff -ru linux-2.4.21/fs/super.c linux/fs/super.c
--- linux-2.4.21/fs/super.c	Mon Jun  2 14:01:34 2003
+++ linux/fs/super.c	Mon Jun  2 18:54:30 2003
@@ -37,6 +37,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:
@@ -433,6 +440,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);
@@ -480,6 +507,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
@@ -699,6 +759,7 @@
 		goto out1;
 
 	error = -EBUSY;
+	down(&lockfs_sem);
 restart:
 	spin_lock(&sb_lock);
 
@@ -710,6 +771,7 @@
 		    ((flags ^ old->s_flags) & MS_RDONLY)) {
 			spin_unlock(&sb_lock);
 			destroy_super(s);
+			up(&lockfs_sem);
 			goto out1;
 		}
 		if (!grab_super(old))
@@ -717,12 +779,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;
@@ -830,7 +894,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)
diff -ru linux-2.4.21/include/linux/fs.h linux/include/linux/fs.h
--- linux-2.4.21/include/linux/fs.h	Mon Jun  2 14:00:34 2003
+++ linux/include/linux/fs.h	Mon Jun  2 16:41:40 2003
@@ -1255,6 +1255,7 @@
 extern int sync_buffers(kdev_t, int);
 extern void sync_dev(kdev_t);
 extern int fsync_dev(kdev_t);
+extern int fsync_dev_lockfs(kdev_t);
 extern int fsync_super(struct super_block *);
 extern int fsync_no_super(kdev_t);
 extern void sync_inodes_sb(struct super_block *);
@@ -1271,6 +1272,8 @@
 extern int filemap_fdatasync(struct address_space *);
 extern int filemap_fdatawait(struct address_space *);
 extern void sync_supers(kdev_t dev, int wait);
+extern void sync_supers_lockfs(kdev_t);
+extern void unlockfs(kdev_t);
 extern int bmap(struct inode *, int);
 extern int notify_change(struct dentry *, struct iattr *);
 extern int permission(struct inode *, int);
diff -ru linux-2.4.21/kernel/ksyms.c linux/kernel/ksyms.c
--- linux-2.4.21/kernel/ksyms.c	Mon Jun  2 13:59:22 2003
+++ linux/kernel/ksyms.c	Mon Jun  2 16:39:50 2003
@@ -186,6 +186,8 @@
 EXPORT_SYMBOL(invalidate_inode_pages);
 EXPORT_SYMBOL(truncate_inode_pages);
 EXPORT_SYMBOL(fsync_dev);
+EXPORT_SYMBOL(fsync_dev_lockfs);
+EXPORT_SYMBOL(unlockfs);
 EXPORT_SYMBOL(fsync_no_super);
 EXPORT_SYMBOL(permission);
 EXPORT_SYMBOL(vfs_permission);
diff -ruN linux-2.4.21/drivers/md/dm-snapshot.c linux/drivers/md/dm-snapshot.c
--- linux-2.4.21/drivers/md/dm-snapshot.c	Wed Jun 18 22:07:15 2003
+++ linux/drivers/md/dm-snapshot.c	Wed Jun 18 22:06:13 2003
@@ -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: