xref: /openbmc/linux/fs/nfs/dir.c (revision 1f4eab7e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/stat.h>
231da177e4SLinus Torvalds #include <linux/fcntl.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
291da177e4SLinus Torvalds #include <linux/nfs_fs.h>
301da177e4SLinus Torvalds #include <linux/nfs_mount.h>
311da177e4SLinus Torvalds #include <linux/pagemap.h>
321da177e4SLinus Torvalds #include <linux/smp_lock.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
361da177e4SLinus Torvalds 
374ce79717STrond Myklebust #include "nfs4_fs.h"
381da177e4SLinus Torvalds #include "delegation.h"
3991d5b470SChuck Lever #include "iostat.h"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define NFS_PARANOIA 1
421da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
451da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
461da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
471da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
481da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
491da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
501da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
511da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
521da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
531da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
541da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
551da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
561da177e4SLinus Torvalds static int nfs_fsync_dir(struct file *, struct dentry *, int);
57f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
581da177e4SLinus Torvalds 
594b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
60f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
611da177e4SLinus Torvalds 	.read		= generic_read_dir,
621da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
631da177e4SLinus Torvalds 	.open		= nfs_opendir,
641da177e4SLinus Torvalds 	.release	= nfs_release,
651da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
6892e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
691da177e4SLinus Torvalds 	.create		= nfs_create,
701da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
711da177e4SLinus Torvalds 	.link		= nfs_link,
721da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
731da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
741da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
751da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
761da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
771da177e4SLinus Torvalds 	.rename		= nfs_rename,
781da177e4SLinus Torvalds 	.permission	= nfs_permission,
791da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
801da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
83b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
8492e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
85b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
86b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
87b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
88b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
89b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
90b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
91b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
92b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
93b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
94b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
95b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
96b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
97b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
98b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
99b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
100b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
101b7fa0554SAndreas Gruenbacher };
102b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
103b7fa0554SAndreas Gruenbacher 
1041da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
10792e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
1081da177e4SLinus Torvalds 	.create		= nfs_create,
1091da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
1101da177e4SLinus Torvalds 	.link		= nfs_link,
1111da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1121da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1131da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1141da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1151da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1161da177e4SLinus Torvalds 	.rename		= nfs_rename,
1171da177e4SLinus Torvalds 	.permission	= nfs_permission,
1181da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1191da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
1206b3b5496SJ. Bruce Fields 	.getxattr       = nfs4_getxattr,
1216b3b5496SJ. Bruce Fields 	.setxattr       = nfs4_setxattr,
1226b3b5496SJ. Bruce Fields 	.listxattr      = nfs4_listxattr,
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /*
1281da177e4SLinus Torvalds  * Open file
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds static int
1311da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1321da177e4SLinus Torvalds {
1337451c4f0SCarsten Otte 	int res;
1341da177e4SLinus Torvalds 
1351e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
1361e7cb3dcSChuck Lever 			inode->i_sb->s_id, inode->i_ino);
1371e7cb3dcSChuck Lever 
1381da177e4SLinus Torvalds 	lock_kernel();
1391da177e4SLinus Torvalds 	/* Call generic open code in order to cache credentials */
1401da177e4SLinus Torvalds 	res = nfs_open(inode, filp);
1411da177e4SLinus Torvalds 	unlock_kernel();
1421da177e4SLinus Torvalds 	return res;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1450dbb4c67SAl Viro typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
1461da177e4SLinus Torvalds typedef struct {
1471da177e4SLinus Torvalds 	struct file	*file;
1481da177e4SLinus Torvalds 	struct page	*page;
1491da177e4SLinus Torvalds 	unsigned long	page_index;
1500dbb4c67SAl Viro 	__be32		*ptr;
151f0dd2136STrond Myklebust 	u64		*dir_cookie;
152f0dd2136STrond Myklebust 	loff_t		current_index;
1531da177e4SLinus Torvalds 	struct nfs_entry *entry;
1541da177e4SLinus Torvalds 	decode_dirent_t	decode;
1551da177e4SLinus Torvalds 	int		plus;
1561da177e4SLinus Torvalds 	int		error;
1571f4eab7eSNeil Brown 	unsigned long	timestamp;
1581f4eab7eSNeil Brown 	int		timestamp_valid;
1591da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent
1621da177e4SLinus Torvalds  * data directly in the page cache.
1631da177e4SLinus Torvalds  *
1641da177e4SLinus Torvalds  * Inode invalidation due to refresh etc. takes care of
1651da177e4SLinus Torvalds  * _everything_, no sloppy entry flushing logic, no extraneous
1661da177e4SLinus Torvalds  * copying, network direct to page cache, the way it was meant
1671da177e4SLinus Torvalds  * to be.
1681da177e4SLinus Torvalds  *
1691da177e4SLinus Torvalds  * NOTE: Dirent information verification is done always by the
1701da177e4SLinus Torvalds  *	 page-in of the RPC reply, nowhere else, this simplies
1711da177e4SLinus Torvalds  *	 things substantially.
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds static
1741da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct file	*file = desc->file;
17701cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
1781da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
1791da177e4SLinus Torvalds 	unsigned long	timestamp;
1801da177e4SLinus Torvalds 	int		error;
1811da177e4SLinus Torvalds 
1821e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
1831e7cb3dcSChuck Lever 			__FUNCTION__, (long long)desc->entry->cookie,
1841e7cb3dcSChuck Lever 			page->index);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds  again:
1871da177e4SLinus Torvalds 	timestamp = jiffies;
18801cce933SJosef "Jeff" Sipek 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
1891da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
1901da177e4SLinus Torvalds 	if (error < 0) {
1911da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
1921da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
1931da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
194412d582eSChuck Lever 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
1951da177e4SLinus Torvalds 			desc->plus = 0;
1961da177e4SLinus Torvalds 			goto again;
1971da177e4SLinus Torvalds 		}
1981da177e4SLinus Torvalds 		goto error;
1991da177e4SLinus Torvalds 	}
2001f4eab7eSNeil Brown 	desc->timestamp = timestamp;
2011f4eab7eSNeil Brown 	desc->timestamp_valid = 1;
2021da177e4SLinus Torvalds 	SetPageUptodate(page);
203dc59250cSChuck Lever 	spin_lock(&inode->i_lock);
20455296809SChuck Lever 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
205dc59250cSChuck Lever 	spin_unlock(&inode->i_lock);
2061da177e4SLinus Torvalds 	/* Ensure consistent page alignment of the data.
2071da177e4SLinus Torvalds 	 * Note: assumes we have exclusive access to this mapping either
2081b1dcc1bSJes Sorensen 	 *	 through inode->i_mutex or some other mechanism.
2091da177e4SLinus Torvalds 	 */
210cd9ae2b6STrond Myklebust 	if (page->index == 0 && invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1) < 0) {
211cd9ae2b6STrond Myklebust 		/* Should never happen */
212cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
213cd9ae2b6STrond Myklebust 	}
2141da177e4SLinus Torvalds 	unlock_page(page);
2151da177e4SLinus Torvalds 	return 0;
2161da177e4SLinus Torvalds  error:
2171da177e4SLinus Torvalds 	SetPageError(page);
2181da177e4SLinus Torvalds 	unlock_page(page);
2191da177e4SLinus Torvalds 	nfs_zap_caches(inode);
2201da177e4SLinus Torvalds 	desc->error = error;
2211da177e4SLinus Torvalds 	return -EIO;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static inline
2251da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc)
2261da177e4SLinus Torvalds {
2270dbb4c67SAl Viro 	__be32	*p = desc->ptr;
2281da177e4SLinus Torvalds 	p = desc->decode(p, desc->entry, desc->plus);
2291da177e4SLinus Torvalds 	if (IS_ERR(p))
2301da177e4SLinus Torvalds 		return PTR_ERR(p);
2311da177e4SLinus Torvalds 	desc->ptr = p;
2321f4eab7eSNeil Brown 	if (desc->timestamp_valid)
2331f4eab7eSNeil Brown 		desc->entry->fattr->time_start = desc->timestamp;
2341f4eab7eSNeil Brown 	else
2351f4eab7eSNeil Brown 		desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
2361da177e4SLinus Torvalds 	return 0;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static inline
2401da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	kunmap(desc->page);
2431da177e4SLinus Torvalds 	page_cache_release(desc->page);
2441da177e4SLinus Torvalds 	desc->page = NULL;
2451da177e4SLinus Torvalds 	desc->ptr = NULL;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /*
2491da177e4SLinus Torvalds  * Given a pointer to a buffer that has already been filled by a call
250f0dd2136STrond Myklebust  * to readdir, find the next entry with cookie '*desc->dir_cookie'.
2511da177e4SLinus Torvalds  *
2521da177e4SLinus Torvalds  * If the end of the buffer has been reached, return -EAGAIN, if not,
2531da177e4SLinus Torvalds  * return the offset within the buffer of the next entry to be
2541da177e4SLinus Torvalds  * read.
2551da177e4SLinus Torvalds  */
2561da177e4SLinus Torvalds static inline
25700a92642SOlivier Galibert int find_dirent(nfs_readdir_descriptor_t *desc)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
2601da177e4SLinus Torvalds 	int		loop_count = 0,
2611da177e4SLinus Torvalds 			status;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	while((status = dir_decode(desc)) == 0) {
2641e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
2651e7cb3dcSChuck Lever 				__FUNCTION__, (unsigned long long)entry->cookie);
266f0dd2136STrond Myklebust 		if (entry->prev_cookie == *desc->dir_cookie)
2671da177e4SLinus Torvalds 			break;
2681da177e4SLinus Torvalds 		if (loop_count++ > 200) {
2691da177e4SLinus Torvalds 			loop_count = 0;
2701da177e4SLinus Torvalds 			schedule();
2711da177e4SLinus Torvalds 		}
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 	return status;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds /*
27700a92642SOlivier Galibert  * Given a pointer to a buffer that has already been filled by a call
278f0dd2136STrond Myklebust  * to readdir, find the entry at offset 'desc->file->f_pos'.
27900a92642SOlivier Galibert  *
28000a92642SOlivier Galibert  * If the end of the buffer has been reached, return -EAGAIN, if not,
28100a92642SOlivier Galibert  * return the offset within the buffer of the next entry to be
28200a92642SOlivier Galibert  * read.
28300a92642SOlivier Galibert  */
28400a92642SOlivier Galibert static inline
28500a92642SOlivier Galibert int find_dirent_index(nfs_readdir_descriptor_t *desc)
28600a92642SOlivier Galibert {
28700a92642SOlivier Galibert 	struct nfs_entry *entry = desc->entry;
28800a92642SOlivier Galibert 	int		loop_count = 0,
28900a92642SOlivier Galibert 			status;
29000a92642SOlivier Galibert 
29100a92642SOlivier Galibert 	for(;;) {
29200a92642SOlivier Galibert 		status = dir_decode(desc);
29300a92642SOlivier Galibert 		if (status)
29400a92642SOlivier Galibert 			break;
29500a92642SOlivier Galibert 
2961e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
2971e7cb3dcSChuck Lever 				(unsigned long long)entry->cookie, desc->current_index);
29800a92642SOlivier Galibert 
299f0dd2136STrond Myklebust 		if (desc->file->f_pos == desc->current_index) {
300f0dd2136STrond Myklebust 			*desc->dir_cookie = entry->cookie;
30100a92642SOlivier Galibert 			break;
30200a92642SOlivier Galibert 		}
30300a92642SOlivier Galibert 		desc->current_index++;
30400a92642SOlivier Galibert 		if (loop_count++ > 200) {
30500a92642SOlivier Galibert 			loop_count = 0;
30600a92642SOlivier Galibert 			schedule();
30700a92642SOlivier Galibert 		}
30800a92642SOlivier Galibert 	}
30900a92642SOlivier Galibert 	return status;
31000a92642SOlivier Galibert }
31100a92642SOlivier Galibert 
31200a92642SOlivier Galibert /*
31300a92642SOlivier Galibert  * Find the given page, and call find_dirent() or find_dirent_index in
31400a92642SOlivier Galibert  * order to try to return the next entry.
3151da177e4SLinus Torvalds  */
3161da177e4SLinus Torvalds static inline
3171da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc)
3181da177e4SLinus Torvalds {
31901cce933SJosef "Jeff" Sipek 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
3201da177e4SLinus Torvalds 	struct page	*page;
3211da177e4SLinus Torvalds 	int		status;
3221da177e4SLinus Torvalds 
3231e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
3241e7cb3dcSChuck Lever 			__FUNCTION__, desc->page_index,
3251e7cb3dcSChuck Lever 			(long long) *desc->dir_cookie);
3261da177e4SLinus Torvalds 
3271f4eab7eSNeil Brown 	/* If we find the page in the page_cache, we cannot be sure
3281f4eab7eSNeil Brown 	 * how fresh the data is, so we will ignore readdir_plus attributes.
3291f4eab7eSNeil Brown 	 */
3301f4eab7eSNeil Brown 	desc->timestamp_valid = 0;
3311da177e4SLinus Torvalds 	page = read_cache_page(inode->i_mapping, desc->page_index,
3321da177e4SLinus Torvalds 			       (filler_t *)nfs_readdir_filler, desc);
3331da177e4SLinus Torvalds 	if (IS_ERR(page)) {
3341da177e4SLinus Torvalds 		status = PTR_ERR(page);
3351da177e4SLinus Torvalds 		goto out;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 	if (!PageUptodate(page))
3381da177e4SLinus Torvalds 		goto read_error;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/* NOTE: Someone else may have changed the READDIRPLUS flag */
3411da177e4SLinus Torvalds 	desc->page = page;
3421da177e4SLinus Torvalds 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
343f0dd2136STrond Myklebust 	if (*desc->dir_cookie != 0)
34400a92642SOlivier Galibert 		status = find_dirent(desc);
34500a92642SOlivier Galibert 	else
34600a92642SOlivier Galibert 		status = find_dirent_index(desc);
3471da177e4SLinus Torvalds 	if (status < 0)
3481da177e4SLinus Torvalds 		dir_page_release(desc);
3491da177e4SLinus Torvalds  out:
3501e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
3511da177e4SLinus Torvalds 	return status;
3521da177e4SLinus Torvalds  read_error:
3531da177e4SLinus Torvalds 	page_cache_release(page);
3541da177e4SLinus Torvalds 	return -EIO;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds /*
3581da177e4SLinus Torvalds  * Recurse through the page cache pages, and return a
3591da177e4SLinus Torvalds  * filled nfs_entry structure of the next directory entry if possible.
3601da177e4SLinus Torvalds  *
361f0dd2136STrond Myklebust  * The target for the search is '*desc->dir_cookie' if non-0,
362f0dd2136STrond Myklebust  * 'desc->file->f_pos' otherwise
3631da177e4SLinus Torvalds  */
3641da177e4SLinus Torvalds static inline
3651da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	int		loop_count = 0;
3681da177e4SLinus Torvalds 	int		res;
3691da177e4SLinus Torvalds 
37000a92642SOlivier Galibert 	/* Always search-by-index from the beginning of the cache */
371f0dd2136STrond Myklebust 	if (*desc->dir_cookie == 0) {
3721e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
3731e7cb3dcSChuck Lever 				(long long)desc->file->f_pos);
37400a92642SOlivier Galibert 		desc->page_index = 0;
37500a92642SOlivier Galibert 		desc->entry->cookie = desc->entry->prev_cookie = 0;
37600a92642SOlivier Galibert 		desc->entry->eof = 0;
37700a92642SOlivier Galibert 		desc->current_index = 0;
378f0dd2136STrond Myklebust 	} else
3791e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
3801e7cb3dcSChuck Lever 				(unsigned long long)*desc->dir_cookie);
38100a92642SOlivier Galibert 
3821da177e4SLinus Torvalds 	for (;;) {
3831da177e4SLinus Torvalds 		res = find_dirent_page(desc);
3841da177e4SLinus Torvalds 		if (res != -EAGAIN)
3851da177e4SLinus Torvalds 			break;
3861da177e4SLinus Torvalds 		/* Align to beginning of next page */
3871da177e4SLinus Torvalds 		desc->page_index ++;
3881da177e4SLinus Torvalds 		if (loop_count++ > 200) {
3891da177e4SLinus Torvalds 			loop_count = 0;
3901da177e4SLinus Torvalds 			schedule();
3911da177e4SLinus Torvalds 		}
3921da177e4SLinus Torvalds 	}
3931e7cb3dcSChuck Lever 
3941e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
3951da177e4SLinus Torvalds 	return res;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*
4061da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
4071da177e4SLinus Torvalds  */
4081da177e4SLinus Torvalds static
4091da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
4101da177e4SLinus Torvalds 		   filldir_t filldir)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	struct file	*file = desc->file;
4131da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
4141da177e4SLinus Torvalds 	struct dentry	*dentry = NULL;
4151da177e4SLinus Torvalds 	unsigned long	fileid;
4161da177e4SLinus Torvalds 	int		loop_count = 0,
4171da177e4SLinus Torvalds 			res;
4181da177e4SLinus Torvalds 
4191e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
4201e7cb3dcSChuck Lever 			(unsigned long long)entry->cookie);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	for(;;) {
4231da177e4SLinus Torvalds 		unsigned d_type = DT_UNKNOWN;
4241da177e4SLinus Torvalds 		/* Note: entry->prev_cookie contains the cookie for
4251da177e4SLinus Torvalds 		 *	 retrieving the current dirent on the server */
4261da177e4SLinus Torvalds 		fileid = nfs_fileid_to_ino_t(entry->ino);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 		/* Get a dentry if we have one */
4291da177e4SLinus Torvalds 		if (dentry != NULL)
4301da177e4SLinus Torvalds 			dput(dentry);
4311da177e4SLinus Torvalds 		dentry = nfs_readdir_lookup(desc);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		/* Use readdirplus info */
4341da177e4SLinus Torvalds 		if (dentry != NULL && dentry->d_inode != NULL) {
4351da177e4SLinus Torvalds 			d_type = dt_type(dentry->d_inode);
4361da177e4SLinus Torvalds 			fileid = dentry->d_inode->i_ino;
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 		res = filldir(dirent, entry->name, entry->len,
44000a92642SOlivier Galibert 			      file->f_pos, fileid, d_type);
4411da177e4SLinus Torvalds 		if (res < 0)
4421da177e4SLinus Torvalds 			break;
44300a92642SOlivier Galibert 		file->f_pos++;
444f0dd2136STrond Myklebust 		*desc->dir_cookie = entry->cookie;
4451da177e4SLinus Torvalds 		if (dir_decode(desc) != 0) {
4461da177e4SLinus Torvalds 			desc->page_index ++;
4471da177e4SLinus Torvalds 			break;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 		if (loop_count++ > 200) {
4501da177e4SLinus Torvalds 			loop_count = 0;
4511da177e4SLinus Torvalds 			schedule();
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds 	dir_page_release(desc);
4551da177e4SLinus Torvalds 	if (dentry != NULL)
4561da177e4SLinus Torvalds 		dput(dentry);
4571e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
4581e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
4591da177e4SLinus Torvalds 	return res;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /*
4631da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
4641da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
4651da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
4661da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
4671da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
4681da177e4SLinus Torvalds  *
4691da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
4701da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
4711da177e4SLinus Torvalds  *	 we should already have a complete representation of the
4721da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
4731da177e4SLinus Torvalds  */
4741da177e4SLinus Torvalds static inline
4751da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
4761da177e4SLinus Torvalds 		     filldir_t filldir)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct file	*file = desc->file;
47901cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
4801da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
4811da177e4SLinus Torvalds 	struct page	*page = NULL;
4821da177e4SLinus Torvalds 	int		status;
4831f4eab7eSNeil Brown 	unsigned long	timestamp;
4841da177e4SLinus Torvalds 
4851e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
4861e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
4891da177e4SLinus Torvalds 	if (!page) {
4901da177e4SLinus Torvalds 		status = -ENOMEM;
4911da177e4SLinus Torvalds 		goto out;
4921da177e4SLinus Torvalds 	}
4931f4eab7eSNeil Brown 	timestamp = jiffies;
49401cce933SJosef "Jeff" Sipek 	desc->error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, *desc->dir_cookie,
4951da177e4SLinus Torvalds 						page,
4961da177e4SLinus Torvalds 						NFS_SERVER(inode)->dtsize,
4971da177e4SLinus Torvalds 						desc->plus);
498dc59250cSChuck Lever 	spin_lock(&inode->i_lock);
49955296809SChuck Lever 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
500dc59250cSChuck Lever 	spin_unlock(&inode->i_lock);
5011da177e4SLinus Torvalds 	desc->page = page;
5021da177e4SLinus Torvalds 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
5031da177e4SLinus Torvalds 	if (desc->error >= 0) {
5041f4eab7eSNeil Brown 		desc->timestamp = timestamp;
5051f4eab7eSNeil Brown 		desc->timestamp_valid = 1;
5061da177e4SLinus Torvalds 		if ((status = dir_decode(desc)) == 0)
507f0dd2136STrond Myklebust 			desc->entry->prev_cookie = *desc->dir_cookie;
5081da177e4SLinus Torvalds 	} else
5091da177e4SLinus Torvalds 		status = -EIO;
5101da177e4SLinus Torvalds 	if (status < 0)
5111da177e4SLinus Torvalds 		goto out_release;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/* Reset read descriptor so it searches the page cache from
5161da177e4SLinus Torvalds 	 * the start upon the next call to readdir_search_pagecache() */
5171da177e4SLinus Torvalds 	desc->page_index = 0;
5181da177e4SLinus Torvalds 	desc->entry->cookie = desc->entry->prev_cookie = 0;
5191da177e4SLinus Torvalds 	desc->entry->eof = 0;
5201da177e4SLinus Torvalds  out:
5211e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
5221e7cb3dcSChuck Lever 			__FUNCTION__, status);
5231da177e4SLinus Torvalds 	return status;
5241da177e4SLinus Torvalds  out_release:
5251da177e4SLinus Torvalds 	dir_page_release(desc);
5261da177e4SLinus Torvalds 	goto out;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
52900a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
53000a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
53100a92642SOlivier Galibert    whole directory.
5321da177e4SLinus Torvalds  */
5331da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
5341da177e4SLinus Torvalds {
53501cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
5361da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
5371da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
5381da177e4SLinus Torvalds 			*desc = &my_desc;
5391da177e4SLinus Torvalds 	struct nfs_entry my_entry;
5401da177e4SLinus Torvalds 	struct nfs_fh	 fh;
5411da177e4SLinus Torvalds 	struct nfs_fattr fattr;
5421da177e4SLinus Torvalds 	long		res;
5431da177e4SLinus Torvalds 
5441e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
5451e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
5461e7cb3dcSChuck Lever 			(long long)filp->f_pos);
54791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
54891d5b470SChuck Lever 
5491da177e4SLinus Torvalds 	lock_kernel();
5501da177e4SLinus Torvalds 
551717d44e8STrond Myklebust 	res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
5521da177e4SLinus Torvalds 	if (res < 0) {
5531da177e4SLinus Torvalds 		unlock_kernel();
5541da177e4SLinus Torvalds 		return res;
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/*
55800a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
559f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
56000a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
56100a92642SOlivier Galibert 	 * revalidate the cookie.
5621da177e4SLinus Torvalds 	 */
5631da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	desc->file = filp;
566f0dd2136STrond Myklebust 	desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
5671da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
5681da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	my_entry.cookie = my_entry.prev_cookie = 0;
5711da177e4SLinus Torvalds 	my_entry.eof = 0;
5721da177e4SLinus Torvalds 	my_entry.fh = &fh;
5731da177e4SLinus Torvalds 	my_entry.fattr = &fattr;
5740e574af1STrond Myklebust 	nfs_fattr_init(&fattr);
5751da177e4SLinus Torvalds 	desc->entry = &my_entry;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	while(!desc->entry->eof) {
5781da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
57900a92642SOlivier Galibert 
5801da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
5811da177e4SLinus Torvalds 			/* This means either end of directory */
582f0dd2136STrond Myklebust 			if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
5831da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
5841da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
5851da177e4SLinus Torvalds 				if (res >= 0)
5861da177e4SLinus Torvalds 					continue;
5871da177e4SLinus Torvalds 			}
5881da177e4SLinus Torvalds 			res = 0;
5891da177e4SLinus Torvalds 			break;
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
592412d582eSChuck Lever 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
5931da177e4SLinus Torvalds 			nfs_zap_caches(inode);
5941da177e4SLinus Torvalds 			desc->plus = 0;
5951da177e4SLinus Torvalds 			desc->entry->eof = 0;
5961da177e4SLinus Torvalds 			continue;
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 		if (res < 0)
5991da177e4SLinus Torvalds 			break;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
6021da177e4SLinus Torvalds 		if (res < 0) {
6031da177e4SLinus Torvalds 			res = 0;
6041da177e4SLinus Torvalds 			break;
6051da177e4SLinus Torvalds 		}
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 	unlock_kernel();
6081e7cb3dcSChuck Lever 	if (res > 0)
6091e7cb3dcSChuck Lever 		res = 0;
6101e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
6111e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
6121e7cb3dcSChuck Lever 			res);
6131da177e4SLinus Torvalds 	return res;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
616f0dd2136STrond Myklebust loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
617f0dd2136STrond Myklebust {
61801cce933SJosef "Jeff" Sipek 	mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
619f0dd2136STrond Myklebust 	switch (origin) {
620f0dd2136STrond Myklebust 		case 1:
621f0dd2136STrond Myklebust 			offset += filp->f_pos;
622f0dd2136STrond Myklebust 		case 0:
623f0dd2136STrond Myklebust 			if (offset >= 0)
624f0dd2136STrond Myklebust 				break;
625f0dd2136STrond Myklebust 		default:
626f0dd2136STrond Myklebust 			offset = -EINVAL;
627f0dd2136STrond Myklebust 			goto out;
628f0dd2136STrond Myklebust 	}
629f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
630f0dd2136STrond Myklebust 		filp->f_pos = offset;
631f0dd2136STrond Myklebust 		((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
632f0dd2136STrond Myklebust 	}
633f0dd2136STrond Myklebust out:
63401cce933SJosef "Jeff" Sipek 	mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
635f0dd2136STrond Myklebust 	return offset;
636f0dd2136STrond Myklebust }
637f0dd2136STrond Myklebust 
6381da177e4SLinus Torvalds /*
6391da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
6401da177e4SLinus Torvalds  * is a dummy operation.
6411da177e4SLinus Torvalds  */
6421da177e4SLinus Torvalds int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
6431da177e4SLinus Torvalds {
6441e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
6451e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
6461e7cb3dcSChuck Lever 			datasync);
6471e7cb3dcSChuck Lever 
6481da177e4SLinus Torvalds 	return 0;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds /*
6521da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
6531da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
6541da177e4SLinus Torvalds  * and may need to be looked up again.
6551da177e4SLinus Torvalds  */
656c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
6591da177e4SLinus Torvalds 		return 1;
66055296809SChuck Lever 	if ((NFS_I(dir)->cache_validity & NFS_INO_INVALID_ATTR) != 0
6611da177e4SLinus Torvalds 			|| nfs_attribute_timeout(dir))
6621da177e4SLinus Torvalds 		return 0;
6631da177e4SLinus Torvalds 	return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)verf;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
671c79ba787STrond Myklebust static void nfs_refresh_verifier(struct dentry * dentry, unsigned long verf)
672c79ba787STrond Myklebust {
673c79ba787STrond Myklebust 	if (time_after(verf, (unsigned long)dentry->d_fsdata))
674c79ba787STrond Myklebust 		nfs_set_verifier(dentry, verf);
675c79ba787STrond Myklebust }
676c79ba787STrond Myklebust 
6771da177e4SLinus Torvalds /*
6781da177e4SLinus Torvalds  * Whenever an NFS operation succeeds, we know that the dentry
6791da177e4SLinus Torvalds  * is valid, so we update the revalidation timestamp.
6801da177e4SLinus Torvalds  */
6811da177e4SLinus Torvalds static inline void nfs_renew_times(struct dentry * dentry)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	dentry->d_time = jiffies;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861d6757fbSTrond Myklebust /*
6871d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
6881d6757fbSTrond Myklebust  *
6891d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
6901d6757fbSTrond Myklebust  * component of the path.
6911d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
6921d6757fbSTrond Myklebust  */
6931d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
6941d6757fbSTrond Myklebust {
6951d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
6961d6757fbSTrond Myklebust 		return 0;
6971d6757fbSTrond Myklebust 	return nd->flags & mask;
6981d6757fbSTrond Myklebust }
6991d6757fbSTrond Myklebust 
7001d6757fbSTrond Myklebust /*
7011d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
7021d6757fbSTrond Myklebust  *
7031d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
7041d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
7051d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
7061d6757fbSTrond Myklebust  *
7071d6757fbSTrond Myklebust  */
7081da177e4SLinus Torvalds static inline
7091da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (nd != NULL) {
7141da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
7151d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
7161da177e4SLinus Torvalds 			goto out_force;
7171da177e4SLinus Torvalds 		/* This is an open(2) */
7181d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
7194e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
7204e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
7214e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
7221da177e4SLinus Torvalds 			goto out_force;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
7251da177e4SLinus Torvalds out_force:
7261da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds /*
7301da177e4SLinus Torvalds  * We judge how long we want to trust negative
7311da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
7321da177e4SLinus Torvalds  *
7331da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
7341da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
7351da177e4SLinus Torvalds  */
7361da177e4SLinus Torvalds static inline
7371da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
7381da177e4SLinus Torvalds 		       struct nameidata *nd)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
7411d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
7421da177e4SLinus Torvalds 		return 0;
7431da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /*
7471da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
7481da177e4SLinus Torvalds  * and we should check whether we can really trust that
7491da177e4SLinus Torvalds  * lookup.
7501da177e4SLinus Torvalds  *
7511da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
7521da177e4SLinus Torvalds  * we have an inode!
7531da177e4SLinus Torvalds  *
7541da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
7551da177e4SLinus Torvalds  * cached dentry and do a new lookup.
7561da177e4SLinus Torvalds  */
7571da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct inode *dir;
7601da177e4SLinus Torvalds 	struct inode *inode;
7611da177e4SLinus Torvalds 	struct dentry *parent;
7621da177e4SLinus Torvalds 	int error;
7631da177e4SLinus Torvalds 	struct nfs_fh fhandle;
7641da177e4SLinus Torvalds 	struct nfs_fattr fattr;
7651da177e4SLinus Torvalds 	unsigned long verifier;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	parent = dget_parent(dentry);
7681da177e4SLinus Torvalds 	lock_kernel();
7691da177e4SLinus Torvalds 	dir = parent->d_inode;
77091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
7711da177e4SLinus Torvalds 	inode = dentry->d_inode;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	if (!inode) {
7741da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
7751da177e4SLinus Torvalds 			goto out_bad;
7761da177e4SLinus Torvalds 		goto out_valid;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
7801e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
7811e7cb3dcSChuck Lever 				__FUNCTION__, dentry->d_parent->d_name.name,
7821e7cb3dcSChuck Lever 				dentry->d_name.name);
7831da177e4SLinus Torvalds 		goto out_bad;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/* Revalidate parent directory attribute cache */
7871da177e4SLinus Torvalds 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
7881da177e4SLinus Torvalds 		goto out_zap_parent;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
7911da177e4SLinus Torvalds 	if (nfs_check_verifier(dir, dentry)) {
7921da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
7931da177e4SLinus Torvalds 			goto out_zap_parent;
7941da177e4SLinus Torvalds 		goto out_valid;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	if (NFS_STALE(inode))
7981da177e4SLinus Torvalds 		goto out_bad;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	verifier = nfs_save_change_attribute(dir);
8011da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
8021da177e4SLinus Torvalds 	if (error)
8031da177e4SLinus Torvalds 		goto out_bad;
8041da177e4SLinus Torvalds 	if (nfs_compare_fh(NFS_FH(inode), &fhandle))
8051da177e4SLinus Torvalds 		goto out_bad;
8061da177e4SLinus Torvalds 	if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
8071da177e4SLinus Torvalds 		goto out_bad;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	nfs_renew_times(dentry);
810c79ba787STrond Myklebust 	nfs_refresh_verifier(dentry, verifier);
8111da177e4SLinus Torvalds  out_valid:
8121da177e4SLinus Torvalds 	unlock_kernel();
8131da177e4SLinus Torvalds 	dput(parent);
8141e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
8151e7cb3dcSChuck Lever 			__FUNCTION__, dentry->d_parent->d_name.name,
8161e7cb3dcSChuck Lever 			dentry->d_name.name);
8171da177e4SLinus Torvalds 	return 1;
8181da177e4SLinus Torvalds out_zap_parent:
8191da177e4SLinus Torvalds 	nfs_zap_caches(dir);
8201da177e4SLinus Torvalds  out_bad:
8211da177e4SLinus Torvalds 	NFS_CACHEINV(dir);
8221da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
8231da177e4SLinus Torvalds 		/* Purge readdir caches. */
8241da177e4SLinus Torvalds 		nfs_zap_caches(inode);
8251da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
8261da177e4SLinus Torvalds 		if (have_submounts(dentry))
8271da177e4SLinus Torvalds 			goto out_valid;
8281da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 	d_drop(dentry);
8311da177e4SLinus Torvalds 	unlock_kernel();
8321da177e4SLinus Torvalds 	dput(parent);
8331e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
8341e7cb3dcSChuck Lever 			__FUNCTION__, dentry->d_parent->d_name.name,
8351e7cb3dcSChuck Lever 			dentry->d_name.name);
8361da177e4SLinus Torvalds 	return 0;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds /*
8401da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
8411da177e4SLinus Torvalds  */
8421da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
8451da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
8461da177e4SLinus Torvalds 		dentry->d_flags);
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
8491da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
8501da177e4SLinus Torvalds 		return 1;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
8531da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
8541da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
8551da177e4SLinus Torvalds 		return 1;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 	return 0;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /*
8621da177e4SLinus Torvalds  * Called when the dentry loses inode.
8631da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
8641da177e4SLinus Torvalds  */
8651da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
8661da177e4SLinus Torvalds {
867cae7a073STrond Myklebust 	nfs_inode_return_delegation(inode);
8681da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
8691da177e4SLinus Torvalds 		lock_kernel();
8709a53c3a7SDave Hansen 		drop_nlink(inode);
8711da177e4SLinus Torvalds 		nfs_complete_unlink(dentry);
8721da177e4SLinus Torvalds 		unlock_kernel();
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds 	/* When creating a negative dentry, we want to renew d_time */
8751da177e4SLinus Torvalds 	nfs_renew_times(dentry);
8761da177e4SLinus Torvalds 	iput(inode);
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds struct dentry_operations nfs_dentry_operations = {
8801da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
8811da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
8821da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
8831da177e4SLinus Torvalds };
8841da177e4SLinus Torvalds 
8851d6757fbSTrond Myklebust /*
8861d6757fbSTrond Myklebust  * Use intent information to check whether or not we're going to do
8871d6757fbSTrond Myklebust  * an O_EXCL create using this path component.
8881d6757fbSTrond Myklebust  */
8891da177e4SLinus Torvalds static inline
8901da177e4SLinus Torvalds int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	if (NFS_PROTO(dir)->version == 2)
8931da177e4SLinus Torvalds 		return 0;
8941d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
8951da177e4SLinus Torvalds 		return 0;
8961da177e4SLinus Torvalds 	return (nd->intent.open.flags & O_EXCL) != 0;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
89954ceac45SDavid Howells static inline int nfs_reval_fsid(struct vfsmount *mnt, struct inode *dir,
90055a97593STrond Myklebust 				 struct nfs_fh *fh, struct nfs_fattr *fattr)
90155a97593STrond Myklebust {
90255a97593STrond Myklebust 	struct nfs_server *server = NFS_SERVER(dir);
90355a97593STrond Myklebust 
90455a97593STrond Myklebust 	if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
90555a97593STrond Myklebust 		/* Revalidate fsid on root dir */
90654ceac45SDavid Howells 		return __nfs_revalidate_inode(server, mnt->mnt_root->d_inode);
90755a97593STrond Myklebust 	return 0;
90855a97593STrond Myklebust }
90955a97593STrond Myklebust 
9101da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	struct dentry *res;
9131da177e4SLinus Torvalds 	struct inode *inode = NULL;
9141da177e4SLinus Torvalds 	int error;
9151da177e4SLinus Torvalds 	struct nfs_fh fhandle;
9161da177e4SLinus Torvalds 	struct nfs_fattr fattr;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
9191da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
92091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
9231da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
9241da177e4SLinus Torvalds 		goto out;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	res = ERR_PTR(-ENOMEM);
9271da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	lock_kernel();
9301da177e4SLinus Torvalds 
931fd684071STrond Myklebust 	/*
932fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
933fd684071STrond Myklebust 	 * but don't hash the dentry.
934fd684071STrond Myklebust 	 */
935fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
936fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
937fd684071STrond Myklebust 		res = NULL;
938fd684071STrond Myklebust 		goto out_unlock;
939fd684071STrond Myklebust 	}
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
9421da177e4SLinus Torvalds 	if (error == -ENOENT)
9431da177e4SLinus Torvalds 		goto no_entry;
9441da177e4SLinus Torvalds 	if (error < 0) {
9451da177e4SLinus Torvalds 		res = ERR_PTR(error);
9461da177e4SLinus Torvalds 		goto out_unlock;
9471da177e4SLinus Torvalds 	}
94854ceac45SDavid Howells 	error = nfs_reval_fsid(nd->mnt, dir, &fhandle, &fattr);
94955a97593STrond Myklebust 	if (error < 0) {
95055a97593STrond Myklebust 		res = ERR_PTR(error);
95155a97593STrond Myklebust 		goto out_unlock;
95255a97593STrond Myklebust 	}
9531da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
95403f28e3aSTrond Myklebust 	res = (struct dentry *)inode;
95503f28e3aSTrond Myklebust 	if (IS_ERR(res))
9561da177e4SLinus Torvalds 		goto out_unlock;
95754ceac45SDavid Howells 
9581da177e4SLinus Torvalds no_entry:
95954ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
9609eaef27bSTrond Myklebust 	if (res != NULL) {
961fc22617eSTrond Myklebust 		struct dentry *parent;
9629eaef27bSTrond Myklebust 		if (IS_ERR(res))
9639eaef27bSTrond Myklebust 			goto out_unlock;
964fc22617eSTrond Myklebust 		/* Was a directory renamed! */
965fc22617eSTrond Myklebust 		parent = dget_parent(res);
966fc22617eSTrond Myklebust 		if (!IS_ROOT(parent))
967fc22617eSTrond Myklebust 			nfs_mark_for_revalidate(parent->d_inode);
968fc22617eSTrond Myklebust 		dput(parent);
9691da177e4SLinus Torvalds 		dentry = res;
9709eaef27bSTrond Myklebust 	}
9711da177e4SLinus Torvalds 	nfs_renew_times(dentry);
9721da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
9731da177e4SLinus Torvalds out_unlock:
9741da177e4SLinus Torvalds 	unlock_kernel();
9751da177e4SLinus Torvalds out:
9761da177e4SLinus Torvalds 	return res;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
9801da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds struct dentry_operations nfs4_dentry_operations = {
9831da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
9841da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
9851da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
9861da177e4SLinus Torvalds };
9871da177e4SLinus Torvalds 
9881d6757fbSTrond Myklebust /*
9891d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
9901d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
9911d6757fbSTrond Myklebust  */
9921da177e4SLinus Torvalds static int is_atomic_open(struct inode *dir, struct nameidata *nd)
9931da177e4SLinus Torvalds {
9941d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
9951da177e4SLinus Torvalds 		return 0;
9961da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
9971da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
9981da177e4SLinus Torvalds 		return 0;
9991da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
10001da177e4SLinus Torvalds 	if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
10011da177e4SLinus Torvalds 		return 0;
10021da177e4SLinus Torvalds 	return 1;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
10061da177e4SLinus Torvalds {
10071da177e4SLinus Torvalds 	struct dentry *res = NULL;
10081da177e4SLinus Torvalds 	int error;
10091da177e4SLinus Torvalds 
10101e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
10111e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
10121e7cb3dcSChuck Lever 
10131da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
10141da177e4SLinus Torvalds 	if (!is_atomic_open(dir, nd))
10151da177e4SLinus Torvalds 		goto no_open;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
10181da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
10191da177e4SLinus Torvalds 		goto out;
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	/* Let vfs_create() deal with O_EXCL */
102402a913a7STrond Myklebust 	if (nd->intent.open.flags & O_EXCL) {
102502a913a7STrond Myklebust 		d_add(dentry, NULL);
102602a913a7STrond Myklebust 		goto out;
102702a913a7STrond Myklebust 	}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	/* Open the file on the server */
10301da177e4SLinus Torvalds 	lock_kernel();
10311da177e4SLinus Torvalds 	/* Revalidate parent directory attribute cache */
10321da177e4SLinus Torvalds 	error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
10331da177e4SLinus Torvalds 	if (error < 0) {
10341da177e4SLinus Torvalds 		res = ERR_PTR(error);
103501c314a0SSteve Dickson 		unlock_kernel();
10361da177e4SLinus Torvalds 		goto out;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	if (nd->intent.open.flags & O_CREAT) {
10401da177e4SLinus Torvalds 		nfs_begin_data_update(dir);
104102a913a7STrond Myklebust 		res = nfs4_atomic_open(dir, dentry, nd);
10421da177e4SLinus Torvalds 		nfs_end_data_update(dir);
10431da177e4SLinus Torvalds 	} else
104402a913a7STrond Myklebust 		res = nfs4_atomic_open(dir, dentry, nd);
10451da177e4SLinus Torvalds 	unlock_kernel();
104602a913a7STrond Myklebust 	if (IS_ERR(res)) {
104702a913a7STrond Myklebust 		error = PTR_ERR(res);
10481da177e4SLinus Torvalds 		switch (error) {
10491da177e4SLinus Torvalds 			/* Make a negative dentry */
10501da177e4SLinus Torvalds 			case -ENOENT:
105102a913a7STrond Myklebust 				res = NULL;
105202a913a7STrond Myklebust 				goto out;
10531da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
10546f926b5bSTrond Myklebust 			case -EISDIR:
10556f926b5bSTrond Myklebust 			case -ENOTDIR:
10566f926b5bSTrond Myklebust 				goto no_open;
10571da177e4SLinus Torvalds 			case -ELOOP:
10581da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
10591da177e4SLinus Torvalds 					goto no_open;
10601da177e4SLinus Torvalds 			/* case -EINVAL: */
10611da177e4SLinus Torvalds 			default:
10621da177e4SLinus Torvalds 				goto out;
10631da177e4SLinus Torvalds 		}
106402a913a7STrond Myklebust 	} else if (res != NULL)
10651da177e4SLinus Torvalds 		dentry = res;
10661da177e4SLinus Torvalds 	nfs_renew_times(dentry);
10671da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10681da177e4SLinus Torvalds out:
10691da177e4SLinus Torvalds 	return res;
10701da177e4SLinus Torvalds no_open:
10711da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct dentry *parent = NULL;
10771da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
10781da177e4SLinus Torvalds 	struct inode *dir;
10791da177e4SLinus Torvalds 	unsigned long verifier;
10801da177e4SLinus Torvalds 	int openflags, ret = 0;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10831da177e4SLinus Torvalds 	dir = parent->d_inode;
10841da177e4SLinus Torvalds 	if (!is_atomic_open(dir, nd))
10851da177e4SLinus Torvalds 		goto no_open;
10861da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
10871da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
10881da177e4SLinus Torvalds 	 */
10891da177e4SLinus Torvalds 	if (inode == NULL)
10901da177e4SLinus Torvalds 		goto out;
10911da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
10921da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
10931da177e4SLinus Torvalds 		goto no_open;
10941da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
10951da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
10961da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
10971da177e4SLinus Torvalds 		goto no_open;
10981da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
10991da177e4SLinus Torvalds 	openflags &= ~(O_CREAT|O_TRUNC);
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	/*
11021b1dcc1bSJes Sorensen 	 * Note: we're not holding inode->i_mutex and so may be racing with
11031da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
11041da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
11051da177e4SLinus Torvalds 	 */
11061da177e4SLinus Torvalds 	lock_kernel();
11071da177e4SLinus Torvalds 	verifier = nfs_save_change_attribute(dir);
110802a913a7STrond Myklebust 	ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
11091da177e4SLinus Torvalds 	if (!ret)
1110c79ba787STrond Myklebust 		nfs_refresh_verifier(dentry, verifier);
11111da177e4SLinus Torvalds 	unlock_kernel();
11121da177e4SLinus Torvalds out:
11131da177e4SLinus Torvalds 	dput(parent);
11141da177e4SLinus Torvalds 	if (!ret)
11151da177e4SLinus Torvalds 		d_drop(dentry);
11161da177e4SLinus Torvalds 	return ret;
11171da177e4SLinus Torvalds no_open:
11181da177e4SLinus Torvalds 	dput(parent);
11191da177e4SLinus Torvalds 	if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
11201da177e4SLinus Torvalds 		return 1;
11211da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
11261da177e4SLinus Torvalds {
112701cce933SJosef "Jeff" Sipek 	struct dentry *parent = desc->file->f_path.dentry;
11281da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
11291da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
11301da177e4SLinus Torvalds 	struct dentry *dentry, *alias;
11311da177e4SLinus Torvalds 	struct qstr name = {
11321da177e4SLinus Torvalds 		.name = entry->name,
11331da177e4SLinus Torvalds 		.len = entry->len,
11341da177e4SLinus Torvalds 	};
11351da177e4SLinus Torvalds 	struct inode *inode;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	switch (name.len) {
11381da177e4SLinus Torvalds 		case 2:
11391da177e4SLinus Torvalds 			if (name.name[0] == '.' && name.name[1] == '.')
11401da177e4SLinus Torvalds 				return dget_parent(parent);
11411da177e4SLinus Torvalds 			break;
11421da177e4SLinus Torvalds 		case 1:
11431da177e4SLinus Torvalds 			if (name.name[0] == '.')
11441da177e4SLinus Torvalds 				return dget(parent);
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 	name.hash = full_name_hash(name.name, name.len);
11471da177e4SLinus Torvalds 	dentry = d_lookup(parent, &name);
1148df1d5d23STrond Myklebust 	if (dentry != NULL) {
1149ef75c797STrond Myklebust 		/* Is this a positive dentry that matches the readdir info? */
1150ef75c797STrond Myklebust 		if (dentry->d_inode != NULL &&
1151ef75c797STrond Myklebust 				(NFS_FILEID(dentry->d_inode) == entry->ino ||
1152ef75c797STrond Myklebust 				d_mountpoint(dentry))) {
1153ef75c797STrond Myklebust 			if (!desc->plus || entry->fh->size == 0)
11541da177e4SLinus Torvalds 				return dentry;
1155ef75c797STrond Myklebust 			if (nfs_compare_fh(NFS_FH(dentry->d_inode),
1156ef75c797STrond Myklebust 						entry->fh) == 0)
1157ef75c797STrond Myklebust 				goto out_renew;
1158ef75c797STrond Myklebust 		}
1159df1d5d23STrond Myklebust 		/* No, so d_drop to allow one to be created */
1160df1d5d23STrond Myklebust 		d_drop(dentry);
1161df1d5d23STrond Myklebust 		dput(dentry);
1162df1d5d23STrond Myklebust 	}
11631da177e4SLinus Torvalds 	if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
11641da177e4SLinus Torvalds 		return NULL;
11651b1dcc1bSJes Sorensen 	/* Note: caller is already holding the dir->i_mutex! */
11661da177e4SLinus Torvalds 	dentry = d_alloc(parent, &name);
11671da177e4SLinus Torvalds 	if (dentry == NULL)
11681da177e4SLinus Torvalds 		return NULL;
11691da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
11701da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
117103f28e3aSTrond Myklebust 	if (IS_ERR(inode)) {
11721da177e4SLinus Torvalds 		dput(dentry);
11731da177e4SLinus Torvalds 		return NULL;
11741da177e4SLinus Torvalds 	}
117554ceac45SDavid Howells 
117654ceac45SDavid Howells 	alias = d_materialise_unique(dentry, inode);
11771da177e4SLinus Torvalds 	if (alias != NULL) {
11781da177e4SLinus Torvalds 		dput(dentry);
11799eaef27bSTrond Myklebust 		if (IS_ERR(alias))
11809eaef27bSTrond Myklebust 			return NULL;
11811da177e4SLinus Torvalds 		dentry = alias;
11821da177e4SLinus Torvalds 	}
118354ceac45SDavid Howells 
11841da177e4SLinus Torvalds 	nfs_renew_times(dentry);
11851da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11861da177e4SLinus Torvalds 	return dentry;
1187c79ba787STrond Myklebust out_renew:
1188c79ba787STrond Myklebust 	nfs_renew_times(dentry);
1189c79ba787STrond Myklebust 	nfs_refresh_verifier(dentry, nfs_save_change_attribute(dir));
1190c79ba787STrond Myklebust 	return dentry;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds /*
11941da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
11951da177e4SLinus Torvalds  */
11961da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
11971da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
11981da177e4SLinus Torvalds {
11991da177e4SLinus Torvalds 	struct inode *inode;
12001da177e4SLinus Torvalds 	int error = -EACCES;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	/* We may have been initialized further down */
12031da177e4SLinus Torvalds 	if (dentry->d_inode)
12041da177e4SLinus Torvalds 		return 0;
12051da177e4SLinus Torvalds 	if (fhandle->size == 0) {
12061da177e4SLinus Torvalds 		struct inode *dir = dentry->d_parent->d_inode;
12071da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
12081da177e4SLinus Torvalds 		if (error)
12094f390c15SChuck Lever 			return error;
12101da177e4SLinus Torvalds 	}
12111da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
12121da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
12138fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
12141da177e4SLinus Torvalds 		if (error < 0)
12154f390c15SChuck Lever 			return error;
12161da177e4SLinus Torvalds 	}
12171da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
121803f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
121903f28e3aSTrond Myklebust 	if (IS_ERR(inode))
12204f390c15SChuck Lever 		return error;
12211da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
1222fd684071STrond Myklebust 	if (d_unhashed(dentry))
1223fd684071STrond Myklebust 		d_rehash(dentry);
12241da177e4SLinus Torvalds 	return 0;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds /*
12281da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
12291da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
12301da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
12311da177e4SLinus Torvalds  * reply path made it appear to have failed.
12321da177e4SLinus Torvalds  */
12331da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
12341da177e4SLinus Torvalds 		struct nameidata *nd)
12351da177e4SLinus Torvalds {
12361da177e4SLinus Torvalds 	struct iattr attr;
12371da177e4SLinus Torvalds 	int error;
12381da177e4SLinus Torvalds 	int open_flags = 0;
12391da177e4SLinus Torvalds 
12401e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
12411e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	attr.ia_mode = mode;
12441da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	if (nd && (nd->flags & LOOKUP_CREATE))
12471da177e4SLinus Torvalds 		open_flags = nd->intent.open.flags;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	lock_kernel();
12501da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
125102a913a7STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
12521da177e4SLinus Torvalds 	nfs_end_data_update(dir);
12531da177e4SLinus Torvalds 	if (error != 0)
12541da177e4SLinus Torvalds 		goto out_err;
12551da177e4SLinus Torvalds 	nfs_renew_times(dentry);
12561da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12571da177e4SLinus Torvalds 	unlock_kernel();
12581da177e4SLinus Torvalds 	return 0;
12591da177e4SLinus Torvalds out_err:
12601da177e4SLinus Torvalds 	unlock_kernel();
12611da177e4SLinus Torvalds 	d_drop(dentry);
12621da177e4SLinus Torvalds 	return error;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds /*
12661da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
12671da177e4SLinus Torvalds  */
12681da177e4SLinus Torvalds static int
12691da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
12701da177e4SLinus Torvalds {
12711da177e4SLinus Torvalds 	struct iattr attr;
12721da177e4SLinus Torvalds 	int status;
12731da177e4SLinus Torvalds 
12741e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
12751e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
12781da177e4SLinus Torvalds 		return -EINVAL;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	attr.ia_mode = mode;
12811da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	lock_kernel();
12841da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
12851da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
12861da177e4SLinus Torvalds 	nfs_end_data_update(dir);
12871da177e4SLinus Torvalds 	if (status != 0)
12881da177e4SLinus Torvalds 		goto out_err;
12891da177e4SLinus Torvalds 	nfs_renew_times(dentry);
12901da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12911da177e4SLinus Torvalds 	unlock_kernel();
12921da177e4SLinus Torvalds 	return 0;
12931da177e4SLinus Torvalds out_err:
12941da177e4SLinus Torvalds 	unlock_kernel();
12951da177e4SLinus Torvalds 	d_drop(dentry);
12961da177e4SLinus Torvalds 	return status;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds /*
13001da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
13011da177e4SLinus Torvalds  */
13021da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	struct iattr attr;
13051da177e4SLinus Torvalds 	int error;
13061da177e4SLinus Torvalds 
13071e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
13081e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
13111da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	lock_kernel();
13141da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
13151da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
13161da177e4SLinus Torvalds 	nfs_end_data_update(dir);
13171da177e4SLinus Torvalds 	if (error != 0)
13181da177e4SLinus Torvalds 		goto out_err;
13191da177e4SLinus Torvalds 	nfs_renew_times(dentry);
13201da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
13211da177e4SLinus Torvalds 	unlock_kernel();
13221da177e4SLinus Torvalds 	return 0;
13231da177e4SLinus Torvalds out_err:
13241da177e4SLinus Torvalds 	d_drop(dentry);
13251da177e4SLinus Torvalds 	unlock_kernel();
13261da177e4SLinus Torvalds 	return error;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds 	int error;
13321da177e4SLinus Torvalds 
13331e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
13341e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	lock_kernel();
13371da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
13381da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
13391da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
13401da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1341ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
13421da177e4SLinus Torvalds 	nfs_end_data_update(dir);
13431da177e4SLinus Torvalds 	unlock_kernel();
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	return error;
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	static unsigned int sillycounter;
13511da177e4SLinus Torvalds 	const int      i_inosize  = sizeof(dir->i_ino)*2;
13521da177e4SLinus Torvalds 	const int      countersize = sizeof(sillycounter)*2;
13531da177e4SLinus Torvalds 	const int      slen       = sizeof(".nfs") + i_inosize + countersize - 1;
13541da177e4SLinus Torvalds 	char           silly[slen+1];
13551da177e4SLinus Torvalds 	struct qstr    qsilly;
13561da177e4SLinus Torvalds 	struct dentry *sdentry;
13571da177e4SLinus Torvalds 	int            error = -EIO;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
13601da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
13611da177e4SLinus Torvalds 		atomic_read(&dentry->d_count));
136291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds #ifdef NFS_PARANOIA
13651da177e4SLinus Torvalds if (!dentry->d_inode)
13661da177e4SLinus Torvalds printk("NFS: silly-renaming %s/%s, negative dentry??\n",
13671da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name);
13681da177e4SLinus Torvalds #endif
13691da177e4SLinus Torvalds 	/*
13701da177e4SLinus Torvalds 	 * We don't allow a dentry to be silly-renamed twice.
13711da177e4SLinus Torvalds 	 */
13721da177e4SLinus Torvalds 	error = -EBUSY;
13731da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
13741da177e4SLinus Torvalds 		goto out;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	sprintf(silly, ".nfs%*.*lx",
13771da177e4SLinus Torvalds 		i_inosize, i_inosize, dentry->d_inode->i_ino);
13781da177e4SLinus Torvalds 
137934ea8188STrond Myklebust 	/* Return delegation in anticipation of the rename */
138034ea8188STrond Myklebust 	nfs_inode_return_delegation(dentry->d_inode);
138134ea8188STrond Myklebust 
13821da177e4SLinus Torvalds 	sdentry = NULL;
13831da177e4SLinus Torvalds 	do {
13841da177e4SLinus Torvalds 		char *suffix = silly + slen - countersize;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 		dput(sdentry);
13871da177e4SLinus Torvalds 		sillycounter++;
13881da177e4SLinus Torvalds 		sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
13891da177e4SLinus Torvalds 
13901e7cb3dcSChuck Lever 		dfprintk(VFS, "NFS: trying to rename %s to %s\n",
13911da177e4SLinus Torvalds 				dentry->d_name.name, silly);
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
13941da177e4SLinus Torvalds 		/*
13951da177e4SLinus Torvalds 		 * N.B. Better to return EBUSY here ... it could be
13961da177e4SLinus Torvalds 		 * dangerous to delete the file while it's in use.
13971da177e4SLinus Torvalds 		 */
13981da177e4SLinus Torvalds 		if (IS_ERR(sdentry))
13991da177e4SLinus Torvalds 			goto out;
14001da177e4SLinus Torvalds 	} while(sdentry->d_inode != NULL); /* need negative lookup */
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	qsilly.name = silly;
14031da177e4SLinus Torvalds 	qsilly.len  = strlen(silly);
14041da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
14051da177e4SLinus Torvalds 	if (dentry->d_inode) {
14061da177e4SLinus Torvalds 		nfs_begin_data_update(dentry->d_inode);
14071da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
14081da177e4SLinus Torvalds 				dir, &qsilly);
14095ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(dentry->d_inode);
14101da177e4SLinus Torvalds 		nfs_end_data_update(dentry->d_inode);
14111da177e4SLinus Torvalds 	} else
14121da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
14131da177e4SLinus Torvalds 				dir, &qsilly);
14141da177e4SLinus Torvalds 	nfs_end_data_update(dir);
14151da177e4SLinus Torvalds 	if (!error) {
14161da177e4SLinus Torvalds 		nfs_renew_times(dentry);
14171da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14181da177e4SLinus Torvalds 		d_move(dentry, sdentry);
14191da177e4SLinus Torvalds 		error = nfs_async_unlink(dentry);
14201da177e4SLinus Torvalds  		/* If we return 0 we don't unlink */
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 	dput(sdentry);
14231da177e4SLinus Torvalds out:
14241da177e4SLinus Torvalds 	return error;
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds /*
14281da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
14291da177e4SLinus Torvalds  * and after checking that the file has only one user.
14301da177e4SLinus Torvalds  *
14311da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
14321da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
14331da177e4SLinus Torvalds  */
14341da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
14351da177e4SLinus Torvalds {
14361da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
14371da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14381da177e4SLinus Torvalds 	int error = -EBUSY;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
14411da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
14441da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
14451da177e4SLinus Torvalds 		error = 0;
14461da177e4SLinus Torvalds 		goto out;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
14501da177e4SLinus Torvalds 	if (inode != NULL) {
1451cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
14521da177e4SLinus Torvalds 		nfs_begin_data_update(inode);
14531da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
14541da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
14551da177e4SLinus Torvalds 		if (error == 0)
14569a53c3a7SDave Hansen 			drop_nlink(inode);
14575ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
14581da177e4SLinus Torvalds 		nfs_end_data_update(inode);
14591da177e4SLinus Torvalds 	} else
14601da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
14611da177e4SLinus Torvalds 	nfs_end_data_update(dir);
14621da177e4SLinus Torvalds out:
14631da177e4SLinus Torvalds 	return error;
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
14671da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
14681da177e4SLinus Torvalds  *
14691da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
14701da177e4SLinus Torvalds  */
14711da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	int error;
14741da177e4SLinus Torvalds 	int need_rehash = 0;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
14771da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	lock_kernel();
14801da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
14811da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
14821da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) > 1) {
14831da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
14841da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
1485ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1486ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
14871da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
14881da177e4SLinus Torvalds 		unlock_kernel();
14891da177e4SLinus Torvalds 		return error;
14901da177e4SLinus Torvalds 	}
14911da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
14921da177e4SLinus Torvalds 		__d_drop(dentry);
14931da177e4SLinus Torvalds 		need_rehash = 1;
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
14961da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
14971da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
14981da177e4SLinus Torvalds 	if (!error) {
14991da177e4SLinus Torvalds 		nfs_renew_times(dentry);
15001da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15011da177e4SLinus Torvalds 	} else if (need_rehash)
15021da177e4SLinus Torvalds 		d_rehash(dentry);
15031da177e4SLinus Torvalds 	unlock_kernel();
15041da177e4SLinus Torvalds 	return error;
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds 
1507873101b3SChuck Lever /*
1508873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1509873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1510873101b3SChuck Lever  * using prepare_write/commit_write.
1511873101b3SChuck Lever  *
1512873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1513873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1514873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1515873101b3SChuck Lever  * symlink request has completed on the server.
1516873101b3SChuck Lever  *
1517873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1518873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1519873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1520873101b3SChuck Lever  * and move the raw page into its mapping.
1521873101b3SChuck Lever  */
1522873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
15231da177e4SLinus Torvalds {
1524873101b3SChuck Lever 	struct pagevec lru_pvec;
1525873101b3SChuck Lever 	struct page *page;
1526873101b3SChuck Lever 	char *kaddr;
15271da177e4SLinus Torvalds 	struct iattr attr;
1528873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
15291da177e4SLinus Torvalds 	int error;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
15321da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
15331da177e4SLinus Torvalds 
1534873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1535873101b3SChuck Lever 		return -ENAMETOOLONG;
15361da177e4SLinus Torvalds 
1537873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1538873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	lock_kernel();
1541873101b3SChuck Lever 
1542873101b3SChuck Lever 	page = alloc_page(GFP_KERNEL);
1543873101b3SChuck Lever 	if (!page) {
1544873101b3SChuck Lever 		unlock_kernel();
1545873101b3SChuck Lever 		return -ENOMEM;
1546873101b3SChuck Lever 	}
1547873101b3SChuck Lever 
1548873101b3SChuck Lever 	kaddr = kmap_atomic(page, KM_USER0);
1549873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1550873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1551873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1552873101b3SChuck Lever 	kunmap_atomic(kaddr, KM_USER0);
1553873101b3SChuck Lever 
15541da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
155594a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
15561da177e4SLinus Torvalds 	nfs_end_data_update(dir);
1557873101b3SChuck Lever 	if (error != 0) {
1558873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1559873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1560873101b3SChuck Lever 			dentry->d_name.name, symname, error);
15611da177e4SLinus Torvalds 		d_drop(dentry);
1562873101b3SChuck Lever 		__free_page(page);
15631da177e4SLinus Torvalds 		unlock_kernel();
15641da177e4SLinus Torvalds 		return error;
15651da177e4SLinus Torvalds 	}
15661da177e4SLinus Torvalds 
1567873101b3SChuck Lever 	/*
1568873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1569873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1570873101b3SChuck Lever 	 */
1571873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1572873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1573873101b3SChuck Lever 							GFP_KERNEL)) {
157439cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
157539cf8a13SChuck Lever 		pagevec_lru_add(&lru_pvec);
1576873101b3SChuck Lever 		SetPageUptodate(page);
1577873101b3SChuck Lever 		unlock_page(page);
1578873101b3SChuck Lever 	} else
1579873101b3SChuck Lever 		__free_page(page);
1580873101b3SChuck Lever 
1581873101b3SChuck Lever 	unlock_kernel();
1582873101b3SChuck Lever 	return 0;
1583873101b3SChuck Lever }
1584873101b3SChuck Lever 
15851da177e4SLinus Torvalds static int
15861da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
15871da177e4SLinus Torvalds {
15881da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
15891da177e4SLinus Torvalds 	int error;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
15921da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
15931da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	lock_kernel();
15961da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
15971da177e4SLinus Torvalds 	nfs_begin_data_update(inode);
15981da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1599cf809556STrond Myklebust 	if (error == 0) {
1600cf809556STrond Myklebust 		atomic_inc(&inode->i_count);
1601cf809556STrond Myklebust 		d_instantiate(dentry, inode);
1602cf809556STrond Myklebust 	}
16031da177e4SLinus Torvalds 	nfs_end_data_update(inode);
16041da177e4SLinus Torvalds 	nfs_end_data_update(dir);
16051da177e4SLinus Torvalds 	unlock_kernel();
16061da177e4SLinus Torvalds 	return error;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds /*
16101da177e4SLinus Torvalds  * RENAME
16111da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
16121da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
16131da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
16141da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
16151da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
16161da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
16171da177e4SLinus Torvalds  *
16181da177e4SLinus Torvalds  * FIXED.
16191da177e4SLinus Torvalds  *
16201da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
16211da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
16221da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
16231da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
16241da177e4SLinus Torvalds  * using the inode layer
16251da177e4SLinus Torvalds  *
16261da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
16271da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
16281da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
16291da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
16301da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
16311da177e4SLinus Torvalds  * the rename.
16321da177e4SLinus Torvalds  */
16331da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
16341da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
16371da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
16381da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
16391da177e4SLinus Torvalds 	int error = -EBUSY;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	/*
16421da177e4SLinus Torvalds 	 * To prevent any new references to the target during the rename,
16431da177e4SLinus Torvalds 	 * we unhash the dentry and free the inode in advance.
16441da177e4SLinus Torvalds 	 */
16451da177e4SLinus Torvalds 	lock_kernel();
16461da177e4SLinus Torvalds 	if (!d_unhashed(new_dentry)) {
16471da177e4SLinus Torvalds 		d_drop(new_dentry);
16481da177e4SLinus Torvalds 		rehash = new_dentry;
16491da177e4SLinus Torvalds 	}
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
16521da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
16531da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
16541da177e4SLinus Torvalds 		 atomic_read(&new_dentry->d_count));
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/*
16571da177e4SLinus Torvalds 	 * First check whether the target is busy ... we can't
16581da177e4SLinus Torvalds 	 * safely do _any_ rename if the target is in use.
16591da177e4SLinus Torvalds 	 *
16601da177e4SLinus Torvalds 	 * For files, make a copy of the dentry and then do a
16611da177e4SLinus Torvalds 	 * silly-rename. If the silly-rename succeeds, the
16621da177e4SLinus Torvalds 	 * copied dentry is hashed and becomes the new target.
16631da177e4SLinus Torvalds 	 */
16641da177e4SLinus Torvalds 	if (!new_inode)
16651da177e4SLinus Torvalds 		goto go_ahead;
16666fe43f9eSTrond Myklebust 	if (S_ISDIR(new_inode->i_mode)) {
16676fe43f9eSTrond Myklebust 		error = -EISDIR;
16686fe43f9eSTrond Myklebust 		if (!S_ISDIR(old_inode->i_mode))
16691da177e4SLinus Torvalds 			goto out;
16706fe43f9eSTrond Myklebust 	} else if (atomic_read(&new_dentry->d_count) > 2) {
16711da177e4SLinus Torvalds 		int err;
16721da177e4SLinus Torvalds 		/* copy the target dentry's name */
16731da177e4SLinus Torvalds 		dentry = d_alloc(new_dentry->d_parent,
16741da177e4SLinus Torvalds 				 &new_dentry->d_name);
16751da177e4SLinus Torvalds 		if (!dentry)
16761da177e4SLinus Torvalds 			goto out;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 		/* silly-rename the existing target ... */
16791da177e4SLinus Torvalds 		err = nfs_sillyrename(new_dir, new_dentry);
16801da177e4SLinus Torvalds 		if (!err) {
16811da177e4SLinus Torvalds 			new_dentry = rehash = dentry;
16821da177e4SLinus Torvalds 			new_inode = NULL;
16831da177e4SLinus Torvalds 			/* instantiate the replacement target */
16841da177e4SLinus Torvalds 			d_instantiate(new_dentry, NULL);
16851da177e4SLinus Torvalds 		} else if (atomic_read(&new_dentry->d_count) > 1) {
16861da177e4SLinus Torvalds 		/* dentry still busy? */
16871da177e4SLinus Torvalds #ifdef NFS_PARANOIA
16881da177e4SLinus Torvalds 			printk("nfs_rename: target %s/%s busy, d_count=%d\n",
16891da177e4SLinus Torvalds 			       new_dentry->d_parent->d_name.name,
16901da177e4SLinus Torvalds 			       new_dentry->d_name.name,
16911da177e4SLinus Torvalds 			       atomic_read(&new_dentry->d_count));
16921da177e4SLinus Torvalds #endif
16931da177e4SLinus Torvalds 			goto out;
16941da177e4SLinus Torvalds 		}
169520509f1bSTrond Myklebust 	} else
16969a53c3a7SDave Hansen 		drop_nlink(new_inode);
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds go_ahead:
16991da177e4SLinus Torvalds 	/*
17001da177e4SLinus Torvalds 	 * ... prune child dentries and writebacks if needed.
17011da177e4SLinus Torvalds 	 */
17021da177e4SLinus Torvalds 	if (atomic_read(&old_dentry->d_count) > 1) {
1703e1552e19STrond Myklebust 		if (S_ISREG(old_inode->i_mode))
17041da177e4SLinus Torvalds 			nfs_wb_all(old_inode);
17051da177e4SLinus Torvalds 		shrink_dcache_parent(old_dentry);
17061da177e4SLinus Torvalds 	}
1707cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
17081da177e4SLinus Torvalds 
170924174119STrond Myklebust 	if (new_inode != NULL) {
171024174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
17111da177e4SLinus Torvalds 		d_delete(new_dentry);
171224174119STrond Myklebust 	}
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	nfs_begin_data_update(old_dir);
17151da177e4SLinus Torvalds 	nfs_begin_data_update(new_dir);
17161da177e4SLinus Torvalds 	nfs_begin_data_update(old_inode);
17171da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
17181da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
17195ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
17201da177e4SLinus Torvalds 	nfs_end_data_update(old_inode);
17211da177e4SLinus Torvalds 	nfs_end_data_update(new_dir);
17221da177e4SLinus Torvalds 	nfs_end_data_update(old_dir);
17231da177e4SLinus Torvalds out:
17241da177e4SLinus Torvalds 	if (rehash)
17251da177e4SLinus Torvalds 		d_rehash(rehash);
17261da177e4SLinus Torvalds 	if (!error) {
17271da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
17281da177e4SLinus Torvalds 		nfs_renew_times(new_dentry);
1729c79ba787STrond Myklebust 		nfs_refresh_verifier(new_dentry, nfs_save_change_attribute(new_dir));
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	/* new dentry created? */
17331da177e4SLinus Torvalds 	if (dentry)
17341da177e4SLinus Torvalds 		dput(dentry);
17351da177e4SLinus Torvalds 	unlock_kernel();
17361da177e4SLinus Torvalds 	return error;
17371da177e4SLinus Torvalds }
17381da177e4SLinus Torvalds 
1739cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1740cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1741cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1742cfcea3e8STrond Myklebust 
17431c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
17441c3c07e9STrond Myklebust {
17451c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
17461c3c07e9STrond Myklebust 	kfree(entry);
1747cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1748cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1749cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
17501c3c07e9STrond Myklebust }
17511c3c07e9STrond Myklebust 
1752979df72eSTrond Myklebust int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
1753979df72eSTrond Myklebust {
1754979df72eSTrond Myklebust 	LIST_HEAD(head);
1755979df72eSTrond Myklebust 	struct nfs_inode *nfsi;
1756979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
1757979df72eSTrond Myklebust 
1758979df72eSTrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1759979df72eSTrond Myklebust restart:
1760979df72eSTrond Myklebust 	list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
1761979df72eSTrond Myklebust 		struct inode *inode;
1762979df72eSTrond Myklebust 
1763979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1764979df72eSTrond Myklebust 			break;
1765979df72eSTrond Myklebust 		inode = igrab(&nfsi->vfs_inode);
1766979df72eSTrond Myklebust 		if (inode == NULL)
1767979df72eSTrond Myklebust 			continue;
1768979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1769979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1770979df72eSTrond Myklebust 			goto remove_lru_entry;
1771979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1772979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1773979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1774979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1775979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1776979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1777979df72eSTrond Myklebust 					&nfs_access_lru_list);
1778979df72eSTrond Myklebust 		else {
1779979df72eSTrond Myklebust remove_lru_entry:
1780979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
1781979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
1782979df72eSTrond Myklebust 		}
1783979df72eSTrond Myklebust 		spin_unlock(&inode->i_lock);
1784979df72eSTrond Myklebust 		iput(inode);
1785979df72eSTrond Myklebust 		goto restart;
1786979df72eSTrond Myklebust 	}
1787979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
1788979df72eSTrond Myklebust 	while (!list_empty(&head)) {
1789979df72eSTrond Myklebust 		cache = list_entry(head.next, struct nfs_access_entry, lru);
1790979df72eSTrond Myklebust 		list_del(&cache->lru);
1791979df72eSTrond Myklebust 		nfs_access_free_entry(cache);
1792979df72eSTrond Myklebust 	}
1793979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1794979df72eSTrond Myklebust }
1795979df72eSTrond Myklebust 
17961c3c07e9STrond Myklebust static void __nfs_access_zap_cache(struct inode *inode)
17971c3c07e9STrond Myklebust {
17981c3c07e9STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
17991c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
18001c3c07e9STrond Myklebust 	struct rb_node *n, *dispose = NULL;
18011c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
18021c3c07e9STrond Myklebust 
18031c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
18041c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
18051c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
18061c3c07e9STrond Myklebust 		rb_erase(n, root_node);
1807cfcea3e8STrond Myklebust 		list_del(&entry->lru);
18081c3c07e9STrond Myklebust 		n->rb_left = dispose;
18091c3c07e9STrond Myklebust 		dispose = n;
18101c3c07e9STrond Myklebust 	}
18111c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
18121c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
18131c3c07e9STrond Myklebust 
18141c3c07e9STrond Myklebust 	/* Now kill them all! */
18151c3c07e9STrond Myklebust 	while (dispose != NULL) {
18161c3c07e9STrond Myklebust 		n = dispose;
18171c3c07e9STrond Myklebust 		dispose = n->rb_left;
18181c3c07e9STrond Myklebust 		nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
18191c3c07e9STrond Myklebust 	}
18201c3c07e9STrond Myklebust }
18211c3c07e9STrond Myklebust 
18221c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
18231c3c07e9STrond Myklebust {
1824cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
1825cfcea3e8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1826cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
1827cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1828cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
1829cfcea3e8STrond Myklebust 	}
1830cfcea3e8STrond Myklebust 
18311c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
18321c3c07e9STrond Myklebust 	/* This will release the spinlock */
18331c3c07e9STrond Myklebust 	__nfs_access_zap_cache(inode);
18341c3c07e9STrond Myklebust }
18351c3c07e9STrond Myklebust 
18361c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
18371c3c07e9STrond Myklebust {
18381c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
18391c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
18401c3c07e9STrond Myklebust 
18411c3c07e9STrond Myklebust 	while (n != NULL) {
18421c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
18431c3c07e9STrond Myklebust 
18441c3c07e9STrond Myklebust 		if (cred < entry->cred)
18451c3c07e9STrond Myklebust 			n = n->rb_left;
18461c3c07e9STrond Myklebust 		else if (cred > entry->cred)
18471c3c07e9STrond Myklebust 			n = n->rb_right;
18481c3c07e9STrond Myklebust 		else
18491c3c07e9STrond Myklebust 			return entry;
18501c3c07e9STrond Myklebust 	}
18511c3c07e9STrond Myklebust 	return NULL;
18521c3c07e9STrond Myklebust }
18531c3c07e9STrond Myklebust 
18541da177e4SLinus Torvalds int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
18551da177e4SLinus Torvalds {
185655296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
18571c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
18581c3c07e9STrond Myklebust 	int err = -ENOENT;
18591da177e4SLinus Torvalds 
18601c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
18611c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
18621c3c07e9STrond Myklebust 		goto out_zap;
18631c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
18641c3c07e9STrond Myklebust 	if (cache == NULL)
18651c3c07e9STrond Myklebust 		goto out;
18661c3c07e9STrond Myklebust 	if (time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
18671c3c07e9STrond Myklebust 		goto out_stale;
18681c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
18691c3c07e9STrond Myklebust 	res->cred = cache->cred;
18701c3c07e9STrond Myklebust 	res->mask = cache->mask;
1871cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
18721c3c07e9STrond Myklebust 	err = 0;
18731c3c07e9STrond Myklebust out:
18741c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
18751c3c07e9STrond Myklebust 	return err;
18761c3c07e9STrond Myklebust out_stale:
18771c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
1878cfcea3e8STrond Myklebust 	list_del(&cache->lru);
18791c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
18801c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
18811da177e4SLinus Torvalds 	return -ENOENT;
18821c3c07e9STrond Myklebust out_zap:
18831c3c07e9STrond Myklebust 	/* This will release the spinlock */
18841c3c07e9STrond Myklebust 	__nfs_access_zap_cache(inode);
18851c3c07e9STrond Myklebust 	return -ENOENT;
18861c3c07e9STrond Myklebust }
18871c3c07e9STrond Myklebust 
18881c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
18891c3c07e9STrond Myklebust {
1890cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1891cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
18921c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
18931c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
18941c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
18951c3c07e9STrond Myklebust 
18961c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
18971c3c07e9STrond Myklebust 	while (*p != NULL) {
18981c3c07e9STrond Myklebust 		parent = *p;
18991c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
19001c3c07e9STrond Myklebust 
19011c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
19021c3c07e9STrond Myklebust 			p = &parent->rb_left;
19031c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
19041c3c07e9STrond Myklebust 			p = &parent->rb_right;
19051c3c07e9STrond Myklebust 		else
19061c3c07e9STrond Myklebust 			goto found;
19071c3c07e9STrond Myklebust 	}
19081c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
19091c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
1910cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
19111c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
19121c3c07e9STrond Myklebust 	return;
19131c3c07e9STrond Myklebust found:
19141c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
1915cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
1916cfcea3e8STrond Myklebust 	list_del(&entry->lru);
19171c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
19181c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
19221da177e4SLinus Torvalds {
19231c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
19241c3c07e9STrond Myklebust 	if (cache == NULL)
19251c3c07e9STrond Myklebust 		return;
19261c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
19271da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
19281c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
19291da177e4SLinus Torvalds 	cache->mask = set->mask;
19301c3c07e9STrond Myklebust 
19311c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
1932cfcea3e8STrond Myklebust 
1933cfcea3e8STrond Myklebust 	/* Update accounting */
1934cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
1935cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
1936cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
1937cfcea3e8STrond Myklebust 
1938cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
1939cfcea3e8STrond Myklebust 	if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1940cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
1941cfcea3e8STrond Myklebust 		list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
1942cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
1943cfcea3e8STrond Myklebust 	}
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
19471da177e4SLinus Torvalds {
19481da177e4SLinus Torvalds 	struct nfs_access_entry cache;
19491da177e4SLinus Torvalds 	int status;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
19521da177e4SLinus Torvalds 	if (status == 0)
19531da177e4SLinus Torvalds 		goto out;
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
19561da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
19571da177e4SLinus Torvalds 	cache.cred = cred;
19581da177e4SLinus Torvalds 	cache.jiffies = jiffies;
19591da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
19601da177e4SLinus Torvalds 	if (status != 0)
19611da177e4SLinus Torvalds 		return status;
19621da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
19631da177e4SLinus Torvalds out:
19641da177e4SLinus Torvalds 	if ((cache.mask & mask) == mask)
19651da177e4SLinus Torvalds 		return 0;
19661da177e4SLinus Torvalds 	return -EACCES;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	struct rpc_cred *cred;
19721da177e4SLinus Torvalds 	int res = 0;
19731da177e4SLinus Torvalds 
197491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
197591d5b470SChuck Lever 
19761da177e4SLinus Torvalds 	if (mask == 0)
19771da177e4SLinus Torvalds 		goto out;
19781da177e4SLinus Torvalds 	/* Is this sys_access() ? */
19791da177e4SLinus Torvalds 	if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
19801da177e4SLinus Torvalds 		goto force_lookup;
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
19831da177e4SLinus Torvalds 		case S_IFLNK:
19841da177e4SLinus Torvalds 			goto out;
19851da177e4SLinus Torvalds 		case S_IFREG:
19861da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
19871da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
19881da177e4SLinus Torvalds 					&& nd != NULL
19891da177e4SLinus Torvalds 					&& (nd->flags & LOOKUP_OPEN))
19901da177e4SLinus Torvalds 				goto out;
19911da177e4SLinus Torvalds 			break;
19921da177e4SLinus Torvalds 		case S_IFDIR:
19931da177e4SLinus Torvalds 			/*
19941da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
19951da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
19961da177e4SLinus Torvalds 			 */
19971da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
19981da177e4SLinus Torvalds 				goto out;
19991da177e4SLinus Torvalds 	}
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds force_lookup:
20021da177e4SLinus Torvalds 	lock_kernel();
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
20051da177e4SLinus Torvalds 		goto out_notsup;
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
20081da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
20091da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
20101da177e4SLinus Torvalds 		put_rpccred(cred);
20111da177e4SLinus Torvalds 	} else
20121da177e4SLinus Torvalds 		res = PTR_ERR(cred);
20131da177e4SLinus Torvalds 	unlock_kernel();
20141da177e4SLinus Torvalds out:
20151e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
20161e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
20171da177e4SLinus Torvalds 	return res;
20181da177e4SLinus Torvalds out_notsup:
20191da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
20201da177e4SLinus Torvalds 	if (res == 0)
20211da177e4SLinus Torvalds 		res = generic_permission(inode, mask, NULL);
20221da177e4SLinus Torvalds 	unlock_kernel();
20231e7cb3dcSChuck Lever 	goto out;
20241da177e4SLinus Torvalds }
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds /*
20271da177e4SLinus Torvalds  * Local variables:
20281da177e4SLinus Torvalds  *  version-control: t
20291da177e4SLinus Torvalds  *  kept-new-versions: 5
20301da177e4SLinus Torvalds  * End:
20311da177e4SLinus Torvalds  */
2032