[PATCH] xfs: revert to double-buffering readdir

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Christoph Hellwig
Date: Sunday, November 25, 2007 - 9:30 am

The current readdir implementation deadlocks on a btree buffers locks
because nfsd calls back into ->lookup from the filldir callback.  The
only short-term fix for this is to revert to the old inefficient
double-buffering scheme.

This patch does exactly that and reverts xfs_file_readdir to what's
basically the 2.6.23 version minus the uio and vnops junk.

I'll try to find something more optimal for 2.6.25 or at least find a
way to use the proper version for local access.


Signed-off-by: Christoph Hellwig <hch@lst.de>

Index: linux-2.6/fs/xfs/linux-2.6/xfs_file.c
===================================================================
--- linux-2.6.orig/fs/xfs/linux-2.6/xfs_file.c	2007-11-25 11:41:20.000000000 +0100
+++ linux-2.6/fs/xfs/linux-2.6/xfs_file.c	2007-11-25 17:14:27.000000000 +0100
@@ -218,6 +218,15 @@
 }
 #endif /* CONFIG_XFS_DMAPI */
 
+/*
+ * Unfortunately we can't just use the clean and simple readdir implementation
+ * below, because nfs might call back into ->lookup from the filldir callback
+ * and that will deadlock the low-level btree code.
+ *
+ * Hopefully we'll find a better workaround that allows to use the optimal
+ * version at least for local readdirs for 2.6.25.
+ */
+#if 0
 STATIC int
 xfs_file_readdir(
 	struct file	*filp,
@@ -249,6 +258,121 @@
 		return -error;
 	return 0;
 }
+#else
+
+struct hack_dirent {
+	int		namlen;
+	loff_t		offset;
+	u64		ino;
+	unsigned int	d_type;
+	char		name[];
+};
+
+struct hack_callback {
+	char		*dirent;
+	size_t		len;
+	size_t		used;
+};
+
+STATIC int
+xfs_hack_filldir(
+	void		*__buf,
+	const char	*name,
+	int		namlen,
+	loff_t		offset,
+	u64		ino,
+	unsigned int	d_type)
+{
+	struct hack_callback *buf = __buf;
+	struct hack_dirent *de = (struct hack_dirent *)(buf->dirent + buf->used);
+
+	if (buf->used + sizeof(struct hack_dirent) + namlen > buf->len)
+		return -EINVAL;
+
+	de->namlen = namlen;
+	de->offset = offset;
+	de->ino = ino;
+	de->d_type = d_type;
+	memcpy(de->name, name, namlen);
+	buf->used += sizeof(struct hack_dirent) + namlen;
+	return 0;
+}
+
+STATIC int
+xfs_file_readdir(
+	struct file	*filp,
+	void		*dirent,
+	filldir_t	filldir)
+{
+	struct inode	*inode = filp->f_path.dentry->d_inode;
+	xfs_inode_t	*ip = XFS_I(inode);
+	struct hack_callback buf;
+	struct hack_dirent *de;
+	int		error;
+	loff_t		size;
+	int		eof = 0;
+	xfs_off_t       start_offset, curr_offset, offset;
+
+	/*
+	 * Try fairly hard to get memory
+	 */
+	buf.len = PAGE_CACHE_SIZE;
+	do {
+		buf.dirent = kmalloc(buf.len, GFP_KERNEL);
+		if (buf.dirent)
+			break;
+		buf.len >>= 1;
+	} while (buf.len >= 1024);
+
+	if (!buf.dirent)
+		return -ENOMEM;
+
+	curr_offset = filp->f_pos;
+	if (curr_offset == 0x7fffffff)
+		offset = 0xffffffff;
+	else
+		offset = filp->f_pos;
+
+	while (!eof) {
+		int reclen;
+		start_offset = offset;
+
+		buf.used = 0;
+		error = -xfs_readdir(ip, &buf, buf.len, &offset,
+				     xfs_hack_filldir);
+		if (error || offset == start_offset) {
+			size = 0;
+			break;
+		}
+
+		size = buf.used;
+		de = (struct hack_dirent *)buf.dirent;
+		while (size > 0) {
+			if (filldir(dirent, de->name, de->namlen,
+					curr_offset & 0x7fffffff,
+					de->ino, de->d_type)) {
+				goto done;
+			}
+
+			reclen = sizeof(struct hack_dirent) + de->namlen;
+			size -= reclen;
+			curr_offset = de->offset /* & 0x7fffffff */;
+			de = (struct hack_dirent *)((char *)de + reclen);
+		}
+	}
+
+ done:
+ 	if (!error) {
+		if (size == 0)
+			filp->f_pos = offset & 0x7fffffff;
+		else if (de)
+			filp->f_pos = curr_offset;
+	}
+
+	kfree(buf.dirent);
+	return error;
+}
+#endif
 
 STATIC int
 xfs_file_mmap(
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
2.6.24-rc2 XFS nfsd hang, Chris Wedgwood, (Wed Nov 14, 12:04 am)
Re: 2.6.24-rc2 XFS nfsd hang, Benny Halevy, (Wed Nov 14, 12:43 am)
2.6.24-rc2 XFS nfsd hang --- filldir change responsible?, Chris Wedgwood, (Wed Nov 14, 4:49 am)
Re: 2.6.24-rc2 XFS nfsd hang, J. Bruce Fields, (Wed Nov 14, 5:59 am)
Re: 2.6.24-rc2 XFS nfsd hang, Christoph Hellwig, (Wed Nov 14, 8:29 am)
Re: 2.6.24-rc2 XFS nfsd hang, J. Bruce Fields, (Wed Nov 14, 10:39 am)
Re: 2.6.24-rc2 XFS nfsd hang, Christoph Hellwig, (Wed Nov 14, 10:44 am)
Re: 2.6.24-rc2 XFS nfsd hang, J. Bruce Fields, (Wed Nov 14, 10:53 am)
Re: 2.6.24-rc2 XFS nfsd hang, Christoph Hellwig, (Wed Nov 14, 11:02 am)
Re: 2.6.24-rc2 XFS nfsd hang, J. Bruce Fields, (Wed Nov 14, 11:08 am)
Re: 2.6.24-rc2 XFS nfsd hang, Christian Kujau, (Wed Nov 14, 3:31 pm)
Re: 2.6.24-rc2 XFS nfsd hang --- filldir change responsible?, Christian Kujau, (Wed Nov 14, 3:48 pm)
Re: 2.6.24-rc2 XFS nfsd hang / smbd too, Christian Kujau, (Thu Nov 15, 12:51 am)
Re: 2.6.24-rc2 XFS nfsd hang / smbd too, Christian Kujau, (Thu Nov 15, 7:44 am)
Re: 2.6.24-rc2 XFS nfsd hang / smbd too, Christian Kujau, (Thu Nov 15, 3:01 pm)
Re: 2.6.24-rc2 XFS nfsd hang / smbd too, Chris Wedgwood, (Thu Nov 15, 5:34 pm)
Re: 2.6.24-rc2 XFS nfsd hang, Christian Kujau, (Fri Nov 16, 2:17 am)
Re: 2.6.24-rc2 XFS nfsd hang, Chris Wedgwood, (Fri Nov 16, 4:03 am)
Re: 2.6.24-rc2 XFS nfsd hang, Trond Myklebust, (Fri Nov 16, 7:19 am)
Re: 2.6.24-rc2 XFS nfsd hang, Chris Wedgwood, (Fri Nov 16, 2:43 pm)
Re: 2.6.24-rc2 XFS nfsd hang, Justin Piszcz, (Sun Nov 18, 8:31 am)
Re: 2.6.24-rc2 XFS nfsd hang, Christian Kujau, (Sun Nov 18, 3:07 pm)
Re: 2.6.24-rc2 XFS nfsd hang, Christoph Hellwig, (Wed Nov 21, 8:07 am)
Re: 2.6.24-rc2 XFS nfsd hang, J. Bruce Fields, (Wed Nov 21, 12:03 pm)
[PATCH] xfs: revert to double-buffering readdir, Christoph Hellwig, (Sun Nov 25, 9:30 am)
Re: [PATCH] xfs: revert to double-buffering readdir, Chris Wedgwood, (Tue Nov 27, 12:43 pm)
Re: [PATCH] xfs: revert to double-buffering readdir, Timothy Shimmin, (Fri Nov 30, 12:22 am)
Re: [PATCH] xfs: revert to double-buffering readdir, David Chinner, (Fri Nov 30, 12:47 am)
Re: [PATCH] xfs: revert to double-buffering readdir, Stephen Lord, (Fri Nov 30, 3:36 pm)
Re: [PATCH] xfs: revert to double-buffering readdir, Chris Wedgwood, (Fri Nov 30, 4:04 pm)
Re: [PATCH] xfs: revert to double-buffering readdir, Stephen Lord, (Sat Dec 1, 6:04 am)
Re: [PATCH] xfs: revert to double-buffering readdir, Christoph Hellwig, (Mon Dec 3, 8:09 am)
Re: [PATCH] xfs: revert to double-buffering readdir, Christoph Hellwig, (Mon Dec 3, 8:11 am)