patch-2.3.50 linux/fs/readdir.c

Next file: linux/fs/smbfs/dir.c
Previous file: linux/fs/proc/root.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.49/linux/fs/readdir.c linux/fs/readdir.c
@@ -13,6 +13,21 @@
 
 #include <asm/uaccess.h>
 
+int vfs_readdir(struct file *file,
+		int (*filler)(void *,const char *,int,off_t,ino_t),
+		void *buf)
+{
+	struct inode *inode = file->f_dentry->d_inode;
+	int res = -ENOTDIR;
+	if (!file->f_op || !file->f_op->readdir)
+		goto out;
+	down(&inode->i_sem);
+	res = file->f_op->readdir(file, buf, filler);
+	up(&inode->i_sem);
+out:
+	return res;
+}
+
 /*
  * Traditional linux readdir() handling..
  *
@@ -57,46 +72,24 @@
 {
 	int error;
 	struct file * file;
-	struct dentry * dentry;
-	struct inode * inode;
 	struct readdir_callback buf;
 
-	lock_kernel();
 	error = -EBADF;
 	file = fget(fd);
 	if (!file)
 		goto out;
 
-	dentry = file->f_dentry;
-	if (!dentry)
-		goto out_putf;
-
-	inode = dentry->d_inode;
-	if (!inode)
-		goto out_putf;
-
 	buf.count = 0;
 	buf.dirent = dirent;
 
-	error = -ENOTDIR;
-	if (!file->f_op || !file->f_op->readdir)
-		goto out_putf;
-
-	/*
-	 * Get the inode's semaphore to prevent changes
-	 * to the directory while we read it.
-	 */
-	down(&inode->i_sem);
-	error = file->f_op->readdir(file, &buf, fillonedir);
-	up(&inode->i_sem);
-	if (error < 0)
-		goto out_putf;
-	error = buf.count;
+	lock_kernel();
+	error = vfs_readdir(file, fillonedir, &buf);
+	if (error >= 0)
+		error = buf.count;
+	unlock_kernel();
 
-out_putf:
 	fput(file);
 out:
-	unlock_kernel();
 	return error;
 }
 
@@ -145,42 +138,22 @@
 asmlinkage long sys_getdents(unsigned int fd, void * dirent, unsigned int count)
 {
 	struct file * file;
-	struct dentry * dentry;
-	struct inode * inode;
 	struct linux_dirent * lastdirent;
 	struct getdents_callback buf;
 	int error;
 
-	lock_kernel();
 	error = -EBADF;
 	file = fget(fd);
 	if (!file)
 		goto out;
 
-	dentry = file->f_dentry;
-	if (!dentry)
-		goto out_putf;
-
-	inode = dentry->d_inode;
-	if (!inode)
-		goto out_putf;
-
 	buf.current_dir = (struct linux_dirent *) dirent;
 	buf.previous = NULL;
 	buf.count = count;
 	buf.error = 0;
 
-	error = -ENOTDIR;
-	if (!file->f_op || !file->f_op->readdir)
-		goto out_putf;
-
-	/*
-	 * Get the inode's semaphore to prevent changes
-	 * to the directory while we read it.
-	 */
-	down(&inode->i_sem);
-	error = file->f_op->readdir(file, &buf, filldir);
-	up(&inode->i_sem);
+	lock_kernel();
+	error = vfs_readdir(file, filldir, &buf);
 	if (error < 0)
 		goto out_putf;
 	error = buf.error;
@@ -191,8 +164,8 @@
 	}
 
 out_putf:
+	unlock_kernel();
 	fput(file);
 out:
-	unlock_kernel();
 	return error;
 }

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)