xref: /openbmc/linux/fs/nfs/dir.c (revision 4c30d56edcaaa0446370189e8ab5c5393dc20ca3)
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>
36e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
371da177e4SLinus Torvalds 
384ce79717STrond Myklebust #include "nfs4_fs.h"
391da177e4SLinus Torvalds #include "delegation.h"
4091d5b470SChuck Lever #include "iostat.h"
41*4c30d56eSAdrian Bunk #include "internal.h"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
461da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
471da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
481da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
491da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
501da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
511da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
521da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
531da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
541da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
551da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
561da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
571da177e4SLinus Torvalds static int nfs_fsync_dir(struct file *, struct dentry *, int);
58f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
591da177e4SLinus Torvalds 
604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
61f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
621da177e4SLinus Torvalds 	.read		= generic_read_dir,
631da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
641da177e4SLinus Torvalds 	.open		= nfs_opendir,
651da177e4SLinus Torvalds 	.release	= nfs_release,
661da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
6992e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
701da177e4SLinus Torvalds 	.create		= nfs_create,
711da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
721da177e4SLinus Torvalds 	.link		= nfs_link,
731da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
741da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
751da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
761da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
771da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
781da177e4SLinus Torvalds 	.rename		= nfs_rename,
791da177e4SLinus Torvalds 	.permission	= nfs_permission,
801da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
811da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
84b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
8592e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
86b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
87b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
88b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
89b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
90b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
91b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
92b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
93b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
94b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
95b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
96b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
97b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
98b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
99b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
100b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
101b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
102b7fa0554SAndreas Gruenbacher };
103b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
104b7fa0554SAndreas Gruenbacher 
1051da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
10892e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
1091da177e4SLinus Torvalds 	.create		= nfs_create,
1101da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
1111da177e4SLinus Torvalds 	.link		= nfs_link,
1121da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1131da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1141da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1151da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1161da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1171da177e4SLinus Torvalds 	.rename		= nfs_rename,
1181da177e4SLinus Torvalds 	.permission	= nfs_permission,
1191da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1201da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
1216b3b5496SJ. Bruce Fields 	.getxattr       = nfs4_getxattr,
1226b3b5496SJ. Bruce Fields 	.setxattr       = nfs4_setxattr,
1236b3b5496SJ. Bruce Fields 	.listxattr      = nfs4_listxattr,
1241da177e4SLinus Torvalds };
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * Open file
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds static int
1321da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1331da177e4SLinus Torvalds {
1347451c4f0SCarsten Otte 	int res;
1351da177e4SLinus Torvalds 
1361e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
1371e7cb3dcSChuck Lever 			inode->i_sb->s_id, inode->i_ino);
1381e7cb3dcSChuck Lever 
1391da177e4SLinus Torvalds 	lock_kernel();
1401da177e4SLinus Torvalds 	/* Call generic open code in order to cache credentials */
1411da177e4SLinus Torvalds 	res = nfs_open(inode, filp);
1421da177e4SLinus Torvalds 	unlock_kernel();
1431da177e4SLinus Torvalds 	return res;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1460dbb4c67SAl Viro typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
1471da177e4SLinus Torvalds typedef struct {
1481da177e4SLinus Torvalds 	struct file	*file;
1491da177e4SLinus Torvalds 	struct page	*page;
1501da177e4SLinus Torvalds 	unsigned long	page_index;
1510dbb4c67SAl Viro 	__be32		*ptr;
152f0dd2136STrond Myklebust 	u64		*dir_cookie;
153f0dd2136STrond Myklebust 	loff_t		current_index;
1541da177e4SLinus Torvalds 	struct nfs_entry *entry;
1551da177e4SLinus Torvalds 	decode_dirent_t	decode;
1561da177e4SLinus Torvalds 	int		plus;
1571da177e4SLinus Torvalds 	int		error;
1581f4eab7eSNeil Brown 	unsigned long	timestamp;
1591f4eab7eSNeil Brown 	int		timestamp_valid;
1601da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent
1631da177e4SLinus Torvalds  * data directly in the page cache.
1641da177e4SLinus Torvalds  *
1651da177e4SLinus Torvalds  * Inode invalidation due to refresh etc. takes care of
1661da177e4SLinus Torvalds  * _everything_, no sloppy entry flushing logic, no extraneous
1671da177e4SLinus Torvalds  * copying, network direct to page cache, the way it was meant
1681da177e4SLinus Torvalds  * to be.
1691da177e4SLinus Torvalds  *
1701da177e4SLinus Torvalds  * NOTE: Dirent information verification is done always by the
1711da177e4SLinus Torvalds  *	 page-in of the RPC reply, nowhere else, this simplies
1721da177e4SLinus Torvalds  *	 things substantially.
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds static
1751da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct file	*file = desc->file;
17801cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
1791da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
1801da177e4SLinus Torvalds 	unsigned long	timestamp;
1811da177e4SLinus Torvalds 	int		error;
1821da177e4SLinus Torvalds 
1831e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
1841e7cb3dcSChuck Lever 			__FUNCTION__, (long long)desc->entry->cookie,
1851e7cb3dcSChuck Lever 			page->index);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds  again:
1881da177e4SLinus Torvalds 	timestamp = jiffies;
18901cce933SJosef "Jeff" Sipek 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
1901da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
1911da177e4SLinus Torvalds 	if (error < 0) {
1921da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
1931da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
1941da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
195412d582eSChuck Lever 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
1961da177e4SLinus Torvalds 			desc->plus = 0;
1971da177e4SLinus Torvalds 			goto again;
1981da177e4SLinus Torvalds 		}
1991da177e4SLinus Torvalds 		goto error;
2001da177e4SLinus Torvalds 	}
2011f4eab7eSNeil Brown 	desc->timestamp = timestamp;
2021f4eab7eSNeil Brown 	desc->timestamp_valid = 1;
2031da177e4SLinus Torvalds 	SetPageUptodate(page);
2041da177e4SLinus Torvalds 	/* Ensure consistent page alignment of the data.
2051da177e4SLinus Torvalds 	 * Note: assumes we have exclusive access to this mapping either
2061b1dcc1bSJes Sorensen 	 *	 through inode->i_mutex or some other mechanism.
2071da177e4SLinus Torvalds 	 */
208cd9ae2b6STrond Myklebust 	if (page->index == 0 && invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1) < 0) {
209cd9ae2b6STrond Myklebust 		/* Should never happen */
210cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
211cd9ae2b6STrond Myklebust 	}
2121da177e4SLinus Torvalds 	unlock_page(page);
2131da177e4SLinus Torvalds 	return 0;
2141da177e4SLinus Torvalds  error:
2151da177e4SLinus Torvalds 	unlock_page(page);
2161da177e4SLinus Torvalds 	desc->error = error;
2171da177e4SLinus Torvalds 	return -EIO;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static inline
2211da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc)
2221da177e4SLinus Torvalds {
2230dbb4c67SAl Viro 	__be32	*p = desc->ptr;
2241da177e4SLinus Torvalds 	p = desc->decode(p, desc->entry, desc->plus);
2251da177e4SLinus Torvalds 	if (IS_ERR(p))
2261da177e4SLinus Torvalds 		return PTR_ERR(p);
2271da177e4SLinus Torvalds 	desc->ptr = p;
2281f4eab7eSNeil Brown 	if (desc->timestamp_valid)
2291f4eab7eSNeil Brown 		desc->entry->fattr->time_start = desc->timestamp;
2301f4eab7eSNeil Brown 	else
2311f4eab7eSNeil Brown 		desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
2321da177e4SLinus Torvalds 	return 0;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds static inline
2361da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	kunmap(desc->page);
2391da177e4SLinus Torvalds 	page_cache_release(desc->page);
2401da177e4SLinus Torvalds 	desc->page = NULL;
2411da177e4SLinus Torvalds 	desc->ptr = NULL;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /*
2451da177e4SLinus Torvalds  * Given a pointer to a buffer that has already been filled by a call
246f0dd2136STrond Myklebust  * to readdir, find the next entry with cookie '*desc->dir_cookie'.
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  * If the end of the buffer has been reached, return -EAGAIN, if not,
2491da177e4SLinus Torvalds  * return the offset within the buffer of the next entry to be
2501da177e4SLinus Torvalds  * read.
2511da177e4SLinus Torvalds  */
2521da177e4SLinus Torvalds static inline
25300a92642SOlivier Galibert int find_dirent(nfs_readdir_descriptor_t *desc)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
2561da177e4SLinus Torvalds 	int		loop_count = 0,
2571da177e4SLinus Torvalds 			status;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	while((status = dir_decode(desc)) == 0) {
2601e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
2611e7cb3dcSChuck Lever 				__FUNCTION__, (unsigned long long)entry->cookie);
262f0dd2136STrond Myklebust 		if (entry->prev_cookie == *desc->dir_cookie)
2631da177e4SLinus Torvalds 			break;
2641da177e4SLinus Torvalds 		if (loop_count++ > 200) {
2651da177e4SLinus Torvalds 			loop_count = 0;
2661da177e4SLinus Torvalds 			schedule();
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 	return status;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /*
27300a92642SOlivier Galibert  * Given a pointer to a buffer that has already been filled by a call
274f0dd2136STrond Myklebust  * to readdir, find the entry at offset 'desc->file->f_pos'.
27500a92642SOlivier Galibert  *
27600a92642SOlivier Galibert  * If the end of the buffer has been reached, return -EAGAIN, if not,
27700a92642SOlivier Galibert  * return the offset within the buffer of the next entry to be
27800a92642SOlivier Galibert  * read.
27900a92642SOlivier Galibert  */
28000a92642SOlivier Galibert static inline
28100a92642SOlivier Galibert int find_dirent_index(nfs_readdir_descriptor_t *desc)
28200a92642SOlivier Galibert {
28300a92642SOlivier Galibert 	struct nfs_entry *entry = desc->entry;
28400a92642SOlivier Galibert 	int		loop_count = 0,
28500a92642SOlivier Galibert 			status;
28600a92642SOlivier Galibert 
28700a92642SOlivier Galibert 	for(;;) {
28800a92642SOlivier Galibert 		status = dir_decode(desc);
28900a92642SOlivier Galibert 		if (status)
29000a92642SOlivier Galibert 			break;
29100a92642SOlivier Galibert 
2921e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
2931e7cb3dcSChuck Lever 				(unsigned long long)entry->cookie, desc->current_index);
29400a92642SOlivier Galibert 
295f0dd2136STrond Myklebust 		if (desc->file->f_pos == desc->current_index) {
296f0dd2136STrond Myklebust 			*desc->dir_cookie = entry->cookie;
29700a92642SOlivier Galibert 			break;
29800a92642SOlivier Galibert 		}
29900a92642SOlivier Galibert 		desc->current_index++;
30000a92642SOlivier Galibert 		if (loop_count++ > 200) {
30100a92642SOlivier Galibert 			loop_count = 0;
30200a92642SOlivier Galibert 			schedule();
30300a92642SOlivier Galibert 		}
30400a92642SOlivier Galibert 	}
30500a92642SOlivier Galibert 	return status;
30600a92642SOlivier Galibert }
30700a92642SOlivier Galibert 
30800a92642SOlivier Galibert /*
30900a92642SOlivier Galibert  * Find the given page, and call find_dirent() or find_dirent_index in
31000a92642SOlivier Galibert  * order to try to return the next entry.
3111da177e4SLinus Torvalds  */
3121da177e4SLinus Torvalds static inline
3131da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc)
3141da177e4SLinus Torvalds {
31501cce933SJosef "Jeff" Sipek 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
3161da177e4SLinus Torvalds 	struct page	*page;
3171da177e4SLinus Torvalds 	int		status;
3181da177e4SLinus Torvalds 
3191e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
3201e7cb3dcSChuck Lever 			__FUNCTION__, desc->page_index,
3211e7cb3dcSChuck Lever 			(long long) *desc->dir_cookie);
3221da177e4SLinus Torvalds 
3231f4eab7eSNeil Brown 	/* If we find the page in the page_cache, we cannot be sure
3241f4eab7eSNeil Brown 	 * how fresh the data is, so we will ignore readdir_plus attributes.
3251f4eab7eSNeil Brown 	 */
3261f4eab7eSNeil Brown 	desc->timestamp_valid = 0;
3271da177e4SLinus Torvalds 	page = read_cache_page(inode->i_mapping, desc->page_index,
3281da177e4SLinus Torvalds 			       (filler_t *)nfs_readdir_filler, desc);
3291da177e4SLinus Torvalds 	if (IS_ERR(page)) {
3301da177e4SLinus Torvalds 		status = PTR_ERR(page);
3311da177e4SLinus Torvalds 		goto out;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* NOTE: Someone else may have changed the READDIRPLUS flag */
3351da177e4SLinus Torvalds 	desc->page = page;
3361da177e4SLinus Torvalds 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
337f0dd2136STrond Myklebust 	if (*desc->dir_cookie != 0)
33800a92642SOlivier Galibert 		status = find_dirent(desc);
33900a92642SOlivier Galibert 	else
34000a92642SOlivier Galibert 		status = find_dirent_index(desc);
3411da177e4SLinus Torvalds 	if (status < 0)
3421da177e4SLinus Torvalds 		dir_page_release(desc);
3431da177e4SLinus Torvalds  out:
3441e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
3451da177e4SLinus Torvalds 	return status;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /*
3491da177e4SLinus Torvalds  * Recurse through the page cache pages, and return a
3501da177e4SLinus Torvalds  * filled nfs_entry structure of the next directory entry if possible.
3511da177e4SLinus Torvalds  *
352f0dd2136STrond Myklebust  * The target for the search is '*desc->dir_cookie' if non-0,
353f0dd2136STrond Myklebust  * 'desc->file->f_pos' otherwise
3541da177e4SLinus Torvalds  */
3551da177e4SLinus Torvalds static inline
3561da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
3571da177e4SLinus Torvalds {
3581da177e4SLinus Torvalds 	int		loop_count = 0;
3591da177e4SLinus Torvalds 	int		res;
3601da177e4SLinus Torvalds 
36100a92642SOlivier Galibert 	/* Always search-by-index from the beginning of the cache */
362f0dd2136STrond Myklebust 	if (*desc->dir_cookie == 0) {
3631e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
3641e7cb3dcSChuck Lever 				(long long)desc->file->f_pos);
36500a92642SOlivier Galibert 		desc->page_index = 0;
36600a92642SOlivier Galibert 		desc->entry->cookie = desc->entry->prev_cookie = 0;
36700a92642SOlivier Galibert 		desc->entry->eof = 0;
36800a92642SOlivier Galibert 		desc->current_index = 0;
369f0dd2136STrond Myklebust 	} else
3701e7cb3dcSChuck Lever 		dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
3711e7cb3dcSChuck Lever 				(unsigned long long)*desc->dir_cookie);
37200a92642SOlivier Galibert 
3731da177e4SLinus Torvalds 	for (;;) {
3741da177e4SLinus Torvalds 		res = find_dirent_page(desc);
3751da177e4SLinus Torvalds 		if (res != -EAGAIN)
3761da177e4SLinus Torvalds 			break;
3771da177e4SLinus Torvalds 		/* Align to beginning of next page */
3781da177e4SLinus Torvalds 		desc->page_index ++;
3791da177e4SLinus Torvalds 		if (loop_count++ > 200) {
3801da177e4SLinus Torvalds 			loop_count = 0;
3811da177e4SLinus Torvalds 			schedule();
3821da177e4SLinus Torvalds 		}
3831da177e4SLinus Torvalds 	}
3841e7cb3dcSChuck Lever 
3851e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
3861da177e4SLinus Torvalds 	return res;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /*
3971da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
3981da177e4SLinus Torvalds  */
3991da177e4SLinus Torvalds static
4001da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
4011da177e4SLinus Torvalds 		   filldir_t filldir)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct file	*file = desc->file;
4041da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
4051da177e4SLinus Torvalds 	struct dentry	*dentry = NULL;
4064e769b93SPeter Staubach 	u64		fileid;
4071da177e4SLinus Torvalds 	int		loop_count = 0,
4081da177e4SLinus Torvalds 			res;
4091da177e4SLinus Torvalds 
4101e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
4111e7cb3dcSChuck Lever 			(unsigned long long)entry->cookie);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	for(;;) {
4141da177e4SLinus Torvalds 		unsigned d_type = DT_UNKNOWN;
4151da177e4SLinus Torvalds 		/* Note: entry->prev_cookie contains the cookie for
4161da177e4SLinus Torvalds 		 *	 retrieving the current dirent on the server */
4174e769b93SPeter Staubach 		fileid = entry->ino;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		/* Get a dentry if we have one */
4201da177e4SLinus Torvalds 		if (dentry != NULL)
4211da177e4SLinus Torvalds 			dput(dentry);
4221da177e4SLinus Torvalds 		dentry = nfs_readdir_lookup(desc);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		/* Use readdirplus info */
4251da177e4SLinus Torvalds 		if (dentry != NULL && dentry->d_inode != NULL) {
4261da177e4SLinus Torvalds 			d_type = dt_type(dentry->d_inode);
4274e769b93SPeter Staubach 			fileid = NFS_FILEID(dentry->d_inode);
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 		res = filldir(dirent, entry->name, entry->len,
431f43bf0beSTrond Myklebust 			      file->f_pos, nfs_compat_user_ino64(fileid),
432f43bf0beSTrond Myklebust 			      d_type);
4331da177e4SLinus Torvalds 		if (res < 0)
4341da177e4SLinus Torvalds 			break;
43500a92642SOlivier Galibert 		file->f_pos++;
436f0dd2136STrond Myklebust 		*desc->dir_cookie = entry->cookie;
4371da177e4SLinus Torvalds 		if (dir_decode(desc) != 0) {
4381da177e4SLinus Torvalds 			desc->page_index ++;
4391da177e4SLinus Torvalds 			break;
4401da177e4SLinus Torvalds 		}
4411da177e4SLinus Torvalds 		if (loop_count++ > 200) {
4421da177e4SLinus Torvalds 			loop_count = 0;
4431da177e4SLinus Torvalds 			schedule();
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 	dir_page_release(desc);
4471da177e4SLinus Torvalds 	if (dentry != NULL)
4481da177e4SLinus Torvalds 		dput(dentry);
4491e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
4501e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
4511da177e4SLinus Torvalds 	return res;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /*
4551da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
4561da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
4571da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
4581da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
4591da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
4601da177e4SLinus Torvalds  *
4611da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
4621da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
4631da177e4SLinus Torvalds  *	 we should already have a complete representation of the
4641da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds static inline
4671da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
4681da177e4SLinus Torvalds 		     filldir_t filldir)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct file	*file = desc->file;
47101cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
4721da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
4731da177e4SLinus Torvalds 	struct page	*page = NULL;
4741da177e4SLinus Torvalds 	int		status;
4751f4eab7eSNeil Brown 	unsigned long	timestamp;
4761da177e4SLinus Torvalds 
4771e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
4781e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
4811da177e4SLinus Torvalds 	if (!page) {
4821da177e4SLinus Torvalds 		status = -ENOMEM;
4831da177e4SLinus Torvalds 		goto out;
4841da177e4SLinus Torvalds 	}
4851f4eab7eSNeil Brown 	timestamp = jiffies;
48601cce933SJosef "Jeff" Sipek 	desc->error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, *desc->dir_cookie,
4871da177e4SLinus Torvalds 						page,
4881da177e4SLinus Torvalds 						NFS_SERVER(inode)->dtsize,
4891da177e4SLinus Torvalds 						desc->plus);
4901da177e4SLinus Torvalds 	desc->page = page;
4911da177e4SLinus Torvalds 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
4921da177e4SLinus Torvalds 	if (desc->error >= 0) {
4931f4eab7eSNeil Brown 		desc->timestamp = timestamp;
4941f4eab7eSNeil Brown 		desc->timestamp_valid = 1;
4951da177e4SLinus Torvalds 		if ((status = dir_decode(desc)) == 0)
496f0dd2136STrond Myklebust 			desc->entry->prev_cookie = *desc->dir_cookie;
4971da177e4SLinus Torvalds 	} else
4981da177e4SLinus Torvalds 		status = -EIO;
4991da177e4SLinus Torvalds 	if (status < 0)
5001da177e4SLinus Torvalds 		goto out_release;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* Reset read descriptor so it searches the page cache from
5051da177e4SLinus Torvalds 	 * the start upon the next call to readdir_search_pagecache() */
5061da177e4SLinus Torvalds 	desc->page_index = 0;
5071da177e4SLinus Torvalds 	desc->entry->cookie = desc->entry->prev_cookie = 0;
5081da177e4SLinus Torvalds 	desc->entry->eof = 0;
5091da177e4SLinus Torvalds  out:
5101e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
5111e7cb3dcSChuck Lever 			__FUNCTION__, status);
5121da177e4SLinus Torvalds 	return status;
5131da177e4SLinus Torvalds  out_release:
5141da177e4SLinus Torvalds 	dir_page_release(desc);
5151da177e4SLinus Torvalds 	goto out;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
51800a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
51900a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
52000a92642SOlivier Galibert    whole directory.
5211da177e4SLinus Torvalds  */
5221da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
5231da177e4SLinus Torvalds {
52401cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
5251da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
5261da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
5271da177e4SLinus Torvalds 			*desc = &my_desc;
5281da177e4SLinus Torvalds 	struct nfs_entry my_entry;
5291da177e4SLinus Torvalds 	struct nfs_fh	 fh;
5301da177e4SLinus Torvalds 	struct nfs_fattr fattr;
5311da177e4SLinus Torvalds 	long		res;
5321da177e4SLinus Torvalds 
5331e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
5341e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
5351e7cb3dcSChuck Lever 			(long long)filp->f_pos);
53691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
53791d5b470SChuck Lever 
5381da177e4SLinus Torvalds 	lock_kernel();
5391da177e4SLinus Torvalds 
540717d44e8STrond Myklebust 	res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
5411da177e4SLinus Torvalds 	if (res < 0) {
5421da177e4SLinus Torvalds 		unlock_kernel();
5431da177e4SLinus Torvalds 		return res;
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/*
54700a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
548f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
54900a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
55000a92642SOlivier Galibert 	 * revalidate the cookie.
5511da177e4SLinus Torvalds 	 */
5521da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	desc->file = filp;
555cd3758e3STrond Myklebust 	desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
5561da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
5571da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	my_entry.cookie = my_entry.prev_cookie = 0;
5601da177e4SLinus Torvalds 	my_entry.eof = 0;
5611da177e4SLinus Torvalds 	my_entry.fh = &fh;
5621da177e4SLinus Torvalds 	my_entry.fattr = &fattr;
5630e574af1STrond Myklebust 	nfs_fattr_init(&fattr);
5641da177e4SLinus Torvalds 	desc->entry = &my_entry;
5651da177e4SLinus Torvalds 
566565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
5671da177e4SLinus Torvalds 	while(!desc->entry->eof) {
5681da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
56900a92642SOlivier Galibert 
5701da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
5711da177e4SLinus Torvalds 			/* This means either end of directory */
572f0dd2136STrond Myklebust 			if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
5731da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
5741da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
5751da177e4SLinus Torvalds 				if (res >= 0)
5761da177e4SLinus Torvalds 					continue;
5771da177e4SLinus Torvalds 			}
5781da177e4SLinus Torvalds 			res = 0;
5791da177e4SLinus Torvalds 			break;
5801da177e4SLinus Torvalds 		}
5811da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
582412d582eSChuck Lever 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
5831da177e4SLinus Torvalds 			nfs_zap_caches(inode);
5841da177e4SLinus Torvalds 			desc->plus = 0;
5851da177e4SLinus Torvalds 			desc->entry->eof = 0;
5861da177e4SLinus Torvalds 			continue;
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 		if (res < 0)
5891da177e4SLinus Torvalds 			break;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
5921da177e4SLinus Torvalds 		if (res < 0) {
5931da177e4SLinus Torvalds 			res = 0;
5941da177e4SLinus Torvalds 			break;
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds 	}
597565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
5981da177e4SLinus Torvalds 	unlock_kernel();
5991e7cb3dcSChuck Lever 	if (res > 0)
6001e7cb3dcSChuck Lever 		res = 0;
6011e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
6021e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
6031e7cb3dcSChuck Lever 			res);
6041da177e4SLinus Torvalds 	return res;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
60710afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
608f0dd2136STrond Myklebust {
60901cce933SJosef "Jeff" Sipek 	mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
610f0dd2136STrond Myklebust 	switch (origin) {
611f0dd2136STrond Myklebust 		case 1:
612f0dd2136STrond Myklebust 			offset += filp->f_pos;
613f0dd2136STrond Myklebust 		case 0:
614f0dd2136STrond Myklebust 			if (offset >= 0)
615f0dd2136STrond Myklebust 				break;
616f0dd2136STrond Myklebust 		default:
617f0dd2136STrond Myklebust 			offset = -EINVAL;
618f0dd2136STrond Myklebust 			goto out;
619f0dd2136STrond Myklebust 	}
620f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
621f0dd2136STrond Myklebust 		filp->f_pos = offset;
622cd3758e3STrond Myklebust 		nfs_file_open_context(filp)->dir_cookie = 0;
623f0dd2136STrond Myklebust 	}
624f0dd2136STrond Myklebust out:
62501cce933SJosef "Jeff" Sipek 	mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
626f0dd2136STrond Myklebust 	return offset;
627f0dd2136STrond Myklebust }
628f0dd2136STrond Myklebust 
6291da177e4SLinus Torvalds /*
6301da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
6311da177e4SLinus Torvalds  * is a dummy operation.
6321da177e4SLinus Torvalds  */
63310afec90STrond Myklebust static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
6341da177e4SLinus Torvalds {
6351e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
6361e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
6371e7cb3dcSChuck Lever 			datasync);
6381e7cb3dcSChuck Lever 
6391da177e4SLinus Torvalds 	return 0;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds /*
6431da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
6441da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
6451da177e4SLinus Torvalds  * and may need to be looked up again.
6461da177e4SLinus Torvalds  */
647c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
6501da177e4SLinus Torvalds 		return 1;
651f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
6526ecc5e8fSTrond Myklebust 		return 0;
653f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
654f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
655f2c77f4eSTrond Myklebust 		return 0;
656f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
657f2c77f4eSTrond Myklebust 		return 0;
658f2c77f4eSTrond Myklebust 	return 1;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
6621d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
6631d6757fbSTrond Myklebust  *
6641d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
6651d6757fbSTrond Myklebust  * component of the path.
6661d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
6671d6757fbSTrond Myklebust  */
6681d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
6691d6757fbSTrond Myklebust {
6701d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
6711d6757fbSTrond Myklebust 		return 0;
6721d6757fbSTrond Myklebust 	return nd->flags & mask;
6731d6757fbSTrond Myklebust }
6741d6757fbSTrond Myklebust 
6751d6757fbSTrond Myklebust /*
676a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
677a12802caSTrond Myklebust  * an O_EXCL create using this path component.
678a12802caSTrond Myklebust  */
679a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
680a12802caSTrond Myklebust {
681a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
682a12802caSTrond Myklebust 		return 0;
683a12802caSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
684a12802caSTrond Myklebust 		return 0;
685a12802caSTrond Myklebust 	return (nd->intent.open.flags & O_EXCL) != 0;
686a12802caSTrond Myklebust }
687a12802caSTrond Myklebust 
688a12802caSTrond Myklebust /*
6891d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
6901d6757fbSTrond Myklebust  *
6911d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
6921d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
6931d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
6941d6757fbSTrond Myklebust  *
6951d6757fbSTrond Myklebust  */
6961da177e4SLinus Torvalds static inline
6971da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (nd != NULL) {
7021da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
7031d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
7041da177e4SLinus Torvalds 			goto out_force;
7051da177e4SLinus Torvalds 		/* This is an open(2) */
7061d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
7074e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
7084e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
7094e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
7101da177e4SLinus Torvalds 			goto out_force;
7114f48af45STrond Myklebust 		return 0;
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
7141da177e4SLinus Torvalds out_force:
7151da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /*
7191da177e4SLinus Torvalds  * We judge how long we want to trust negative
7201da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
7211da177e4SLinus Torvalds  *
7221da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
7231da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
7241da177e4SLinus Torvalds  */
7251da177e4SLinus Torvalds static inline
7261da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
7271da177e4SLinus Torvalds 		       struct nameidata *nd)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
7301d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
7311da177e4SLinus Torvalds 		return 0;
7321da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds /*
7361da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
7371da177e4SLinus Torvalds  * and we should check whether we can really trust that
7381da177e4SLinus Torvalds  * lookup.
7391da177e4SLinus Torvalds  *
7401da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
7411da177e4SLinus Torvalds  * we have an inode!
7421da177e4SLinus Torvalds  *
7431da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
7441da177e4SLinus Torvalds  * cached dentry and do a new lookup.
7451da177e4SLinus Torvalds  */
7461da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	struct inode *dir;
7491da177e4SLinus Torvalds 	struct inode *inode;
7501da177e4SLinus Torvalds 	struct dentry *parent;
7511da177e4SLinus Torvalds 	int error;
7521da177e4SLinus Torvalds 	struct nfs_fh fhandle;
7531da177e4SLinus Torvalds 	struct nfs_fattr fattr;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	parent = dget_parent(dentry);
7561da177e4SLinus Torvalds 	lock_kernel();
7571da177e4SLinus Torvalds 	dir = parent->d_inode;
75891d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
7591da177e4SLinus Torvalds 	inode = dentry->d_inode;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (!inode) {
7621da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
7631da177e4SLinus Torvalds 			goto out_bad;
7641da177e4SLinus Torvalds 		goto out_valid;
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
7681e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
7691e7cb3dcSChuck Lever 				__FUNCTION__, dentry->d_parent->d_name.name,
7701e7cb3dcSChuck Lever 				dentry->d_name.name);
7711da177e4SLinus Torvalds 		goto out_bad;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
775a12802caSTrond Myklebust 	if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
7761da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
7771da177e4SLinus Torvalds 			goto out_zap_parent;
7781da177e4SLinus Torvalds 		goto out_valid;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (NFS_STALE(inode))
7821da177e4SLinus Torvalds 		goto out_bad;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
7851da177e4SLinus Torvalds 	if (error)
7861da177e4SLinus Torvalds 		goto out_bad;
7871da177e4SLinus Torvalds 	if (nfs_compare_fh(NFS_FH(inode), &fhandle))
7881da177e4SLinus Torvalds 		goto out_bad;
7891da177e4SLinus Torvalds 	if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
7901da177e4SLinus Torvalds 		goto out_bad;
7911da177e4SLinus Torvalds 
792cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
7931da177e4SLinus Torvalds  out_valid:
7941da177e4SLinus Torvalds 	unlock_kernel();
7951da177e4SLinus Torvalds 	dput(parent);
7961e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
7971e7cb3dcSChuck Lever 			__FUNCTION__, dentry->d_parent->d_name.name,
7981e7cb3dcSChuck Lever 			dentry->d_name.name);
7991da177e4SLinus Torvalds 	return 1;
8001da177e4SLinus Torvalds out_zap_parent:
8011da177e4SLinus Torvalds 	nfs_zap_caches(dir);
8021da177e4SLinus Torvalds  out_bad:
803a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
8041da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
8051da177e4SLinus Torvalds 		/* Purge readdir caches. */
8061da177e4SLinus Torvalds 		nfs_zap_caches(inode);
8071da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
8081da177e4SLinus Torvalds 		if (have_submounts(dentry))
8091da177e4SLinus Torvalds 			goto out_valid;
8101da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 	d_drop(dentry);
8131da177e4SLinus Torvalds 	unlock_kernel();
8141da177e4SLinus Torvalds 	dput(parent);
8151e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
8161e7cb3dcSChuck Lever 			__FUNCTION__, dentry->d_parent->d_name.name,
8171e7cb3dcSChuck Lever 			dentry->d_name.name);
8181da177e4SLinus Torvalds 	return 0;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds /*
8221da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
8231da177e4SLinus Torvalds  */
8241da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry)
8251da177e4SLinus Torvalds {
8261da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
8271da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
8281da177e4SLinus Torvalds 		dentry->d_flags);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
8311da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
8321da177e4SLinus Torvalds 		return 1;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
8351da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
8361da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
8371da177e4SLinus Torvalds 		return 1;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 	return 0;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds /*
8441da177e4SLinus Torvalds  * Called when the dentry loses inode.
8451da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
8461da177e4SLinus Torvalds  */
8471da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
8481da177e4SLinus Torvalds {
849cae7a073STrond Myklebust 	nfs_inode_return_delegation(inode);
85083672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
85183672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
85283672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
85383672d39SNeil Brown 
8541da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
8551da177e4SLinus Torvalds 		lock_kernel();
8569a53c3a7SDave Hansen 		drop_nlink(inode);
857e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
8581da177e4SLinus Torvalds 		unlock_kernel();
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 	iput(inode);
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds struct dentry_operations nfs_dentry_operations = {
8641da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
8651da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
8661da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
8671da177e4SLinus Torvalds };
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	struct dentry *res;
872565277f6STrond Myklebust 	struct dentry *parent;
8731da177e4SLinus Torvalds 	struct inode *inode = NULL;
8741da177e4SLinus Torvalds 	int error;
8751da177e4SLinus Torvalds 	struct nfs_fh fhandle;
8761da177e4SLinus Torvalds 	struct nfs_fattr fattr;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
8791da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
88091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
8831da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
8841da177e4SLinus Torvalds 		goto out;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	res = ERR_PTR(-ENOMEM);
8871da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	lock_kernel();
8901da177e4SLinus Torvalds 
891fd684071STrond Myklebust 	/*
892fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
893fd684071STrond Myklebust 	 * but don't hash the dentry.
894fd684071STrond Myklebust 	 */
895fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
896fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
897fd684071STrond Myklebust 		res = NULL;
898fd684071STrond Myklebust 		goto out_unlock;
899fd684071STrond Myklebust 	}
9001da177e4SLinus Torvalds 
901565277f6STrond Myklebust 	parent = dentry->d_parent;
902565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
903565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
9041da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
9051da177e4SLinus Torvalds 	if (error == -ENOENT)
9061da177e4SLinus Torvalds 		goto no_entry;
9071da177e4SLinus Torvalds 	if (error < 0) {
9081da177e4SLinus Torvalds 		res = ERR_PTR(error);
909565277f6STrond Myklebust 		goto out_unblock_sillyrename;
9101da177e4SLinus Torvalds 	}
9111da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
91203f28e3aSTrond Myklebust 	res = (struct dentry *)inode;
91303f28e3aSTrond Myklebust 	if (IS_ERR(res))
914565277f6STrond Myklebust 		goto out_unblock_sillyrename;
91554ceac45SDavid Howells 
9161da177e4SLinus Torvalds no_entry:
91754ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
9189eaef27bSTrond Myklebust 	if (res != NULL) {
9199eaef27bSTrond Myklebust 		if (IS_ERR(res))
920565277f6STrond Myklebust 			goto out_unblock_sillyrename;
9211da177e4SLinus Torvalds 		dentry = res;
9229eaef27bSTrond Myklebust 	}
9231da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
924565277f6STrond Myklebust out_unblock_sillyrename:
925565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
9261da177e4SLinus Torvalds out_unlock:
9271da177e4SLinus Torvalds 	unlock_kernel();
9281da177e4SLinus Torvalds out:
9291da177e4SLinus Torvalds 	return res;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
9331da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds struct dentry_operations nfs4_dentry_operations = {
9361da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
9371da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
9381da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
9391da177e4SLinus Torvalds };
9401da177e4SLinus Torvalds 
9411d6757fbSTrond Myklebust /*
9421d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
9431d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
9441d6757fbSTrond Myklebust  */
9451da177e4SLinus Torvalds static int is_atomic_open(struct inode *dir, struct nameidata *nd)
9461da177e4SLinus Torvalds {
9471d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
9481da177e4SLinus Torvalds 		return 0;
9491da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
9501da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
9511da177e4SLinus Torvalds 		return 0;
9521da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
9531da177e4SLinus Torvalds 	if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
9541da177e4SLinus Torvalds 		return 0;
9551da177e4SLinus Torvalds 	return 1;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	struct dentry *res = NULL;
9611da177e4SLinus Torvalds 	int error;
9621da177e4SLinus Torvalds 
9631e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
9641e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
9651e7cb3dcSChuck Lever 
9661da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
9671da177e4SLinus Torvalds 	if (!is_atomic_open(dir, nd))
9681da177e4SLinus Torvalds 		goto no_open;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
9711da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
9721da177e4SLinus Torvalds 		goto out;
9731da177e4SLinus Torvalds 	}
9741da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
9751da177e4SLinus Torvalds 
976d4d9cdcbSTrond Myklebust 	/* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
977d4d9cdcbSTrond Myklebust 	 * the dentry. */
97802a913a7STrond Myklebust 	if (nd->intent.open.flags & O_EXCL) {
979d4d9cdcbSTrond Myklebust 		d_instantiate(dentry, NULL);
98002a913a7STrond Myklebust 		goto out;
98102a913a7STrond Myklebust 	}
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	/* Open the file on the server */
9841da177e4SLinus Torvalds 	lock_kernel();
98502a913a7STrond Myklebust 	res = nfs4_atomic_open(dir, dentry, nd);
9861da177e4SLinus Torvalds 	unlock_kernel();
98702a913a7STrond Myklebust 	if (IS_ERR(res)) {
98802a913a7STrond Myklebust 		error = PTR_ERR(res);
9891da177e4SLinus Torvalds 		switch (error) {
9901da177e4SLinus Torvalds 			/* Make a negative dentry */
9911da177e4SLinus Torvalds 			case -ENOENT:
99202a913a7STrond Myklebust 				res = NULL;
99302a913a7STrond Myklebust 				goto out;
9941da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
9956f926b5bSTrond Myklebust 			case -EISDIR:
9966f926b5bSTrond Myklebust 			case -ENOTDIR:
9976f926b5bSTrond Myklebust 				goto no_open;
9981da177e4SLinus Torvalds 			case -ELOOP:
9991da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
10001da177e4SLinus Torvalds 					goto no_open;
10011da177e4SLinus Torvalds 			/* case -EINVAL: */
10021da177e4SLinus Torvalds 			default:
10031da177e4SLinus Torvalds 				goto out;
10041da177e4SLinus Torvalds 		}
100502a913a7STrond Myklebust 	} else if (res != NULL)
10061da177e4SLinus Torvalds 		dentry = res;
10071da177e4SLinus Torvalds out:
10081da177e4SLinus Torvalds 	return res;
10091da177e4SLinus Torvalds no_open:
10101da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	struct dentry *parent = NULL;
10161da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
10171da177e4SLinus Torvalds 	struct inode *dir;
10181da177e4SLinus Torvalds 	int openflags, ret = 0;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10211da177e4SLinus Torvalds 	dir = parent->d_inode;
10221da177e4SLinus Torvalds 	if (!is_atomic_open(dir, nd))
10231da177e4SLinus Torvalds 		goto no_open;
10241da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
10251da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
10261da177e4SLinus Torvalds 	 */
1027216d5d06STrond Myklebust 	if (inode == NULL) {
1028216d5d06STrond Myklebust 		if (!nfs_neg_need_reval(dir, dentry, nd))
1029216d5d06STrond Myklebust 			ret = 1;
10301da177e4SLinus Torvalds 		goto out;
1031216d5d06STrond Myklebust 	}
1032216d5d06STrond Myklebust 
10331da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
10341da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
10351da177e4SLinus Torvalds 		goto no_open;
10361da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
10371da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
10381da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
10391da177e4SLinus Torvalds 		goto no_open;
10401da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
10411da177e4SLinus Torvalds 	openflags &= ~(O_CREAT|O_TRUNC);
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/*
10441b1dcc1bSJes Sorensen 	 * Note: we're not holding inode->i_mutex and so may be racing with
10451da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
10461da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
10471da177e4SLinus Torvalds 	 */
10481da177e4SLinus Torvalds 	lock_kernel();
104902a913a7STrond Myklebust 	ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
10501da177e4SLinus Torvalds 	unlock_kernel();
10511da177e4SLinus Torvalds out:
10521da177e4SLinus Torvalds 	dput(parent);
10531da177e4SLinus Torvalds 	if (!ret)
10541da177e4SLinus Torvalds 		d_drop(dentry);
10551da177e4SLinus Torvalds 	return ret;
10561da177e4SLinus Torvalds no_open:
10571da177e4SLinus Torvalds 	dput(parent);
10581da177e4SLinus Torvalds 	if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
10591da177e4SLinus Torvalds 		return 1;
10601da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
10651da177e4SLinus Torvalds {
106601cce933SJosef "Jeff" Sipek 	struct dentry *parent = desc->file->f_path.dentry;
10671da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
10681da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
10691da177e4SLinus Torvalds 	struct dentry *dentry, *alias;
10701da177e4SLinus Torvalds 	struct qstr name = {
10711da177e4SLinus Torvalds 		.name = entry->name,
10721da177e4SLinus Torvalds 		.len = entry->len,
10731da177e4SLinus Torvalds 	};
10741da177e4SLinus Torvalds 	struct inode *inode;
107557fa76f2STrond Myklebust 	unsigned long verf = nfs_save_change_attribute(dir);
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	switch (name.len) {
10781da177e4SLinus Torvalds 		case 2:
10791da177e4SLinus Torvalds 			if (name.name[0] == '.' && name.name[1] == '.')
10801da177e4SLinus Torvalds 				return dget_parent(parent);
10811da177e4SLinus Torvalds 			break;
10821da177e4SLinus Torvalds 		case 1:
10831da177e4SLinus Torvalds 			if (name.name[0] == '.')
10841da177e4SLinus Torvalds 				return dget(parent);
10851da177e4SLinus Torvalds 	}
108657fa76f2STrond Myklebust 
108757fa76f2STrond Myklebust 	spin_lock(&dir->i_lock);
108857fa76f2STrond Myklebust 	if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
108957fa76f2STrond Myklebust 		spin_unlock(&dir->i_lock);
109057fa76f2STrond Myklebust 		return NULL;
109157fa76f2STrond Myklebust 	}
109257fa76f2STrond Myklebust 	spin_unlock(&dir->i_lock);
109357fa76f2STrond Myklebust 
10941da177e4SLinus Torvalds 	name.hash = full_name_hash(name.name, name.len);
10951da177e4SLinus Torvalds 	dentry = d_lookup(parent, &name);
1096df1d5d23STrond Myklebust 	if (dentry != NULL) {
1097ef75c797STrond Myklebust 		/* Is this a positive dentry that matches the readdir info? */
1098ef75c797STrond Myklebust 		if (dentry->d_inode != NULL &&
1099ef75c797STrond Myklebust 				(NFS_FILEID(dentry->d_inode) == entry->ino ||
1100ef75c797STrond Myklebust 				d_mountpoint(dentry))) {
1101ef75c797STrond Myklebust 			if (!desc->plus || entry->fh->size == 0)
11021da177e4SLinus Torvalds 				return dentry;
1103ef75c797STrond Myklebust 			if (nfs_compare_fh(NFS_FH(dentry->d_inode),
1104ef75c797STrond Myklebust 						entry->fh) == 0)
1105ef75c797STrond Myklebust 				goto out_renew;
1106ef75c797STrond Myklebust 		}
1107df1d5d23STrond Myklebust 		/* No, so d_drop to allow one to be created */
1108df1d5d23STrond Myklebust 		d_drop(dentry);
1109df1d5d23STrond Myklebust 		dput(dentry);
1110df1d5d23STrond Myklebust 	}
11111da177e4SLinus Torvalds 	if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
11121da177e4SLinus Torvalds 		return NULL;
111354af3bb5STrond Myklebust 	if (name.len > NFS_SERVER(dir)->namelen)
111454af3bb5STrond Myklebust 		return NULL;
11151b1dcc1bSJes Sorensen 	/* Note: caller is already holding the dir->i_mutex! */
11161da177e4SLinus Torvalds 	dentry = d_alloc(parent, &name);
11171da177e4SLinus Torvalds 	if (dentry == NULL)
11181da177e4SLinus Torvalds 		return NULL;
11191da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
11201da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
112103f28e3aSTrond Myklebust 	if (IS_ERR(inode)) {
11221da177e4SLinus Torvalds 		dput(dentry);
11231da177e4SLinus Torvalds 		return NULL;
11241da177e4SLinus Torvalds 	}
112554ceac45SDavid Howells 
112654ceac45SDavid Howells 	alias = d_materialise_unique(dentry, inode);
11271da177e4SLinus Torvalds 	if (alias != NULL) {
11281da177e4SLinus Torvalds 		dput(dentry);
11299eaef27bSTrond Myklebust 		if (IS_ERR(alias))
11309eaef27bSTrond Myklebust 			return NULL;
11311da177e4SLinus Torvalds 		dentry = alias;
11321da177e4SLinus Torvalds 	}
113354ceac45SDavid Howells 
1134c79ba787STrond Myklebust out_renew:
113557fa76f2STrond Myklebust 	nfs_set_verifier(dentry, verf);
1136c79ba787STrond Myklebust 	return dentry;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds /*
11401da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
11411da177e4SLinus Torvalds  */
11421da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
11431da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
11441da177e4SLinus Torvalds {
1145fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1146fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
11471da177e4SLinus Torvalds 	struct inode *inode;
11481da177e4SLinus Torvalds 	int error = -EACCES;
11491da177e4SLinus Torvalds 
1150fab728e1STrond Myklebust 	d_drop(dentry);
1151fab728e1STrond Myklebust 
11521da177e4SLinus Torvalds 	/* We may have been initialized further down */
11531da177e4SLinus Torvalds 	if (dentry->d_inode)
1154fab728e1STrond Myklebust 		goto out;
11551da177e4SLinus Torvalds 	if (fhandle->size == 0) {
11561da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
11571da177e4SLinus Torvalds 		if (error)
1158fab728e1STrond Myklebust 			goto out_error;
11591da177e4SLinus Torvalds 	}
11605724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11611da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
11621da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
11638fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
11641da177e4SLinus Torvalds 		if (error < 0)
1165fab728e1STrond Myklebust 			goto out_error;
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
116803f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
116903f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1170fab728e1STrond Myklebust 		goto out_error;
1171fab728e1STrond Myklebust 	d_add(dentry, inode);
1172fab728e1STrond Myklebust out:
1173fab728e1STrond Myklebust 	dput(parent);
11741da177e4SLinus Torvalds 	return 0;
1175fab728e1STrond Myklebust out_error:
1176fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1177fab728e1STrond Myklebust 	dput(parent);
1178fab728e1STrond Myklebust 	return error;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds /*
11821da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
11831da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
11841da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
11851da177e4SLinus Torvalds  * reply path made it appear to have failed.
11861da177e4SLinus Torvalds  */
11871da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
11881da177e4SLinus Torvalds 		struct nameidata *nd)
11891da177e4SLinus Torvalds {
11901da177e4SLinus Torvalds 	struct iattr attr;
11911da177e4SLinus Torvalds 	int error;
11921da177e4SLinus Torvalds 	int open_flags = 0;
11931da177e4SLinus Torvalds 
11941e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
11951e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	attr.ia_mode = mode;
11981da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
11991da177e4SLinus Torvalds 
1200ad389da7STrond Myklebust 	if ((nd->flags & LOOKUP_CREATE) != 0)
12011da177e4SLinus Torvalds 		open_flags = nd->intent.open.flags;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	lock_kernel();
120402a913a7STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
12051da177e4SLinus Torvalds 	if (error != 0)
12061da177e4SLinus Torvalds 		goto out_err;
12071da177e4SLinus Torvalds 	unlock_kernel();
12081da177e4SLinus Torvalds 	return 0;
12091da177e4SLinus Torvalds out_err:
12101da177e4SLinus Torvalds 	unlock_kernel();
12111da177e4SLinus Torvalds 	d_drop(dentry);
12121da177e4SLinus Torvalds 	return error;
12131da177e4SLinus Torvalds }
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds /*
12161da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
12171da177e4SLinus Torvalds  */
12181da177e4SLinus Torvalds static int
12191da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	struct iattr attr;
12221da177e4SLinus Torvalds 	int status;
12231da177e4SLinus Torvalds 
12241e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
12251e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
12281da177e4SLinus Torvalds 		return -EINVAL;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	attr.ia_mode = mode;
12311da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	lock_kernel();
12341da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
12351da177e4SLinus Torvalds 	if (status != 0)
12361da177e4SLinus Torvalds 		goto out_err;
12371da177e4SLinus Torvalds 	unlock_kernel();
12381da177e4SLinus Torvalds 	return 0;
12391da177e4SLinus Torvalds out_err:
12401da177e4SLinus Torvalds 	unlock_kernel();
12411da177e4SLinus Torvalds 	d_drop(dentry);
12421da177e4SLinus Torvalds 	return status;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds /*
12461da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
12471da177e4SLinus Torvalds  */
12481da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
12491da177e4SLinus Torvalds {
12501da177e4SLinus Torvalds 	struct iattr attr;
12511da177e4SLinus Torvalds 	int error;
12521da177e4SLinus Torvalds 
12531e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
12541e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
12571da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	lock_kernel();
12601da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
12611da177e4SLinus Torvalds 	if (error != 0)
12621da177e4SLinus Torvalds 		goto out_err;
12631da177e4SLinus Torvalds 	unlock_kernel();
12641da177e4SLinus Torvalds 	return 0;
12651da177e4SLinus Torvalds out_err:
12661da177e4SLinus Torvalds 	d_drop(dentry);
12671da177e4SLinus Torvalds 	unlock_kernel();
12681da177e4SLinus Torvalds 	return error;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
12721da177e4SLinus Torvalds {
12731da177e4SLinus Torvalds 	int error;
12741da177e4SLinus Torvalds 
12751e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
12761e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	lock_kernel();
12791da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
12801da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
12811da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1282ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
12831da177e4SLinus Torvalds 	unlock_kernel();
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	return error;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	static unsigned int sillycounter;
12914e769b93SPeter Staubach 	const int      fileidsize  = sizeof(NFS_FILEID(dentry->d_inode))*2;
12921da177e4SLinus Torvalds 	const int      countersize = sizeof(sillycounter)*2;
12934e769b93SPeter Staubach 	const int      slen        = sizeof(".nfs")+fileidsize+countersize-1;
12941da177e4SLinus Torvalds 	char           silly[slen+1];
12951da177e4SLinus Torvalds 	struct qstr    qsilly;
12961da177e4SLinus Torvalds 	struct dentry *sdentry;
12971da177e4SLinus Torvalds 	int            error = -EIO;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
13001da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
13011da177e4SLinus Torvalds 		atomic_read(&dentry->d_count));
130291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	/*
13051da177e4SLinus Torvalds 	 * We don't allow a dentry to be silly-renamed twice.
13061da177e4SLinus Torvalds 	 */
13071da177e4SLinus Torvalds 	error = -EBUSY;
13081da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
13091da177e4SLinus Torvalds 		goto out;
13101da177e4SLinus Torvalds 
13114e769b93SPeter Staubach 	sprintf(silly, ".nfs%*.*Lx",
13124e769b93SPeter Staubach 		fileidsize, fileidsize,
13134e769b93SPeter Staubach 		(unsigned long long)NFS_FILEID(dentry->d_inode));
13141da177e4SLinus Torvalds 
131534ea8188STrond Myklebust 	/* Return delegation in anticipation of the rename */
131634ea8188STrond Myklebust 	nfs_inode_return_delegation(dentry->d_inode);
131734ea8188STrond Myklebust 
13181da177e4SLinus Torvalds 	sdentry = NULL;
13191da177e4SLinus Torvalds 	do {
13201da177e4SLinus Torvalds 		char *suffix = silly + slen - countersize;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 		dput(sdentry);
13231da177e4SLinus Torvalds 		sillycounter++;
13241da177e4SLinus Torvalds 		sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
13251da177e4SLinus Torvalds 
13261e7cb3dcSChuck Lever 		dfprintk(VFS, "NFS: trying to rename %s to %s\n",
13271da177e4SLinus Torvalds 				dentry->d_name.name, silly);
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
13301da177e4SLinus Torvalds 		/*
13311da177e4SLinus Torvalds 		 * N.B. Better to return EBUSY here ... it could be
13321da177e4SLinus Torvalds 		 * dangerous to delete the file while it's in use.
13331da177e4SLinus Torvalds 		 */
13341da177e4SLinus Torvalds 		if (IS_ERR(sdentry))
13351da177e4SLinus Torvalds 			goto out;
13361da177e4SLinus Torvalds 	} while(sdentry->d_inode != NULL); /* need negative lookup */
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	qsilly.name = silly;
13391da177e4SLinus Torvalds 	qsilly.len  = strlen(silly);
13401da177e4SLinus Torvalds 	if (dentry->d_inode) {
13411da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
13421da177e4SLinus Torvalds 				dir, &qsilly);
13435ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(dentry->d_inode);
13441da177e4SLinus Torvalds 	} else
13451da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
13461da177e4SLinus Torvalds 				dir, &qsilly);
13471da177e4SLinus Torvalds 	if (!error) {
13481da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
13491da177e4SLinus Torvalds 		d_move(dentry, sdentry);
1350e4eff1a6STrond Myklebust 		error = nfs_async_unlink(dir, dentry);
13511da177e4SLinus Torvalds  		/* If we return 0 we don't unlink */
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 	dput(sdentry);
13541da177e4SLinus Torvalds out:
13551da177e4SLinus Torvalds 	return error;
13561da177e4SLinus Torvalds }
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds /*
13591da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
13601da177e4SLinus Torvalds  * and after checking that the file has only one user.
13611da177e4SLinus Torvalds  *
13621da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
13631da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
13641da177e4SLinus Torvalds  */
13651da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
13681da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
13691da177e4SLinus Torvalds 	int error = -EBUSY;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
13721da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
13751da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
13761da177e4SLinus Torvalds 		error = 0;
13771da177e4SLinus Torvalds 		goto out;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	if (inode != NULL) {
1381cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
13821da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
13831da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
13841da177e4SLinus Torvalds 		if (error == 0)
13859a53c3a7SDave Hansen 			drop_nlink(inode);
13865ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
13871da177e4SLinus Torvalds 	} else
13881da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
13891da177e4SLinus Torvalds out:
13901da177e4SLinus Torvalds 	return error;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
13941da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
13951da177e4SLinus Torvalds  *
13961da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
13971da177e4SLinus Torvalds  */
13981da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
13991da177e4SLinus Torvalds {
14001da177e4SLinus Torvalds 	int error;
14011da177e4SLinus Torvalds 	int need_rehash = 0;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
14041da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	lock_kernel();
14071da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
14081da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
14091da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) > 1) {
14101da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
14111da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
1412ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1413ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
14141da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
14151da177e4SLinus Torvalds 		unlock_kernel();
14161da177e4SLinus Torvalds 		return error;
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
14191da177e4SLinus Torvalds 		__d_drop(dentry);
14201da177e4SLinus Torvalds 		need_rehash = 1;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
14231da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
14241da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
14251da177e4SLinus Torvalds 	if (!error) {
14261da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14271da177e4SLinus Torvalds 	} else if (need_rehash)
14281da177e4SLinus Torvalds 		d_rehash(dentry);
14291da177e4SLinus Torvalds 	unlock_kernel();
14301da177e4SLinus Torvalds 	return error;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
1433873101b3SChuck Lever /*
1434873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1435873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1436873101b3SChuck Lever  * using prepare_write/commit_write.
1437873101b3SChuck Lever  *
1438873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1439873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1440873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1441873101b3SChuck Lever  * symlink request has completed on the server.
1442873101b3SChuck Lever  *
1443873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1444873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1445873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1446873101b3SChuck Lever  * and move the raw page into its mapping.
1447873101b3SChuck Lever  */
1448873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
14491da177e4SLinus Torvalds {
1450873101b3SChuck Lever 	struct pagevec lru_pvec;
1451873101b3SChuck Lever 	struct page *page;
1452873101b3SChuck Lever 	char *kaddr;
14531da177e4SLinus Torvalds 	struct iattr attr;
1454873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
14551da177e4SLinus Torvalds 	int error;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
14581da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
14591da177e4SLinus Torvalds 
1460873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1461873101b3SChuck Lever 		return -ENAMETOOLONG;
14621da177e4SLinus Torvalds 
1463873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1464873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	lock_kernel();
1467873101b3SChuck Lever 
146883d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
1469873101b3SChuck Lever 	if (!page) {
1470873101b3SChuck Lever 		unlock_kernel();
1471873101b3SChuck Lever 		return -ENOMEM;
1472873101b3SChuck Lever 	}
1473873101b3SChuck Lever 
1474873101b3SChuck Lever 	kaddr = kmap_atomic(page, KM_USER0);
1475873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1476873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1477873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1478873101b3SChuck Lever 	kunmap_atomic(kaddr, KM_USER0);
1479873101b3SChuck Lever 
148094a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1481873101b3SChuck Lever 	if (error != 0) {
1482873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1483873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1484873101b3SChuck Lever 			dentry->d_name.name, symname, error);
14851da177e4SLinus Torvalds 		d_drop(dentry);
1486873101b3SChuck Lever 		__free_page(page);
14871da177e4SLinus Torvalds 		unlock_kernel();
14881da177e4SLinus Torvalds 		return error;
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
1491873101b3SChuck Lever 	/*
1492873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1493873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1494873101b3SChuck Lever 	 */
1495873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1496873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1497873101b3SChuck Lever 							GFP_KERNEL)) {
149839cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
149939cf8a13SChuck Lever 		pagevec_lru_add(&lru_pvec);
1500873101b3SChuck Lever 		SetPageUptodate(page);
1501873101b3SChuck Lever 		unlock_page(page);
1502873101b3SChuck Lever 	} else
1503873101b3SChuck Lever 		__free_page(page);
1504873101b3SChuck Lever 
1505873101b3SChuck Lever 	unlock_kernel();
1506873101b3SChuck Lever 	return 0;
1507873101b3SChuck Lever }
1508873101b3SChuck Lever 
15091da177e4SLinus Torvalds static int
15101da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
15131da177e4SLinus Torvalds 	int error;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
15161da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
15171da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	lock_kernel();
15209697d234STrond Myklebust 	d_drop(dentry);
15211da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1522cf809556STrond Myklebust 	if (error == 0) {
1523cf809556STrond Myklebust 		atomic_inc(&inode->i_count);
15249697d234STrond Myklebust 		d_add(dentry, inode);
1525cf809556STrond Myklebust 	}
15261da177e4SLinus Torvalds 	unlock_kernel();
15271da177e4SLinus Torvalds 	return error;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /*
15311da177e4SLinus Torvalds  * RENAME
15321da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
15331da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
15341da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
15351da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
15361da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
15371da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
15381da177e4SLinus Torvalds  *
15391da177e4SLinus Torvalds  * FIXED.
15401da177e4SLinus Torvalds  *
15411da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
15421da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
15431da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
15441da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
15451da177e4SLinus Torvalds  * using the inode layer
15461da177e4SLinus Torvalds  *
15471da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
15481da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
15491da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
15501da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
15511da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
15521da177e4SLinus Torvalds  * the rename.
15531da177e4SLinus Torvalds  */
15541da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
15551da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
15581da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
15591da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
15601da177e4SLinus Torvalds 	int error = -EBUSY;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/*
15631da177e4SLinus Torvalds 	 * To prevent any new references to the target during the rename,
15641da177e4SLinus Torvalds 	 * we unhash the dentry and free the inode in advance.
15651da177e4SLinus Torvalds 	 */
15661da177e4SLinus Torvalds 	lock_kernel();
15671da177e4SLinus Torvalds 	if (!d_unhashed(new_dentry)) {
15681da177e4SLinus Torvalds 		d_drop(new_dentry);
15691da177e4SLinus Torvalds 		rehash = new_dentry;
15701da177e4SLinus Torvalds 	}
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
15731da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
15741da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
15751da177e4SLinus Torvalds 		 atomic_read(&new_dentry->d_count));
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 * First check whether the target is busy ... we can't
15791da177e4SLinus Torvalds 	 * safely do _any_ rename if the target is in use.
15801da177e4SLinus Torvalds 	 *
15811da177e4SLinus Torvalds 	 * For files, make a copy of the dentry and then do a
15821da177e4SLinus Torvalds 	 * silly-rename. If the silly-rename succeeds, the
15831da177e4SLinus Torvalds 	 * copied dentry is hashed and becomes the new target.
15841da177e4SLinus Torvalds 	 */
15851da177e4SLinus Torvalds 	if (!new_inode)
15861da177e4SLinus Torvalds 		goto go_ahead;
15876fe43f9eSTrond Myklebust 	if (S_ISDIR(new_inode->i_mode)) {
15886fe43f9eSTrond Myklebust 		error = -EISDIR;
15896fe43f9eSTrond Myklebust 		if (!S_ISDIR(old_inode->i_mode))
15901da177e4SLinus Torvalds 			goto out;
15916fe43f9eSTrond Myklebust 	} else if (atomic_read(&new_dentry->d_count) > 2) {
15921da177e4SLinus Torvalds 		int err;
15931da177e4SLinus Torvalds 		/* copy the target dentry's name */
15941da177e4SLinus Torvalds 		dentry = d_alloc(new_dentry->d_parent,
15951da177e4SLinus Torvalds 				 &new_dentry->d_name);
15961da177e4SLinus Torvalds 		if (!dentry)
15971da177e4SLinus Torvalds 			goto out;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 		/* silly-rename the existing target ... */
16001da177e4SLinus Torvalds 		err = nfs_sillyrename(new_dir, new_dentry);
16011da177e4SLinus Torvalds 		if (!err) {
16021da177e4SLinus Torvalds 			new_dentry = rehash = dentry;
16031da177e4SLinus Torvalds 			new_inode = NULL;
16041da177e4SLinus Torvalds 			/* instantiate the replacement target */
16051da177e4SLinus Torvalds 			d_instantiate(new_dentry, NULL);
16067a13e932SJesper Juhl 		} else if (atomic_read(&new_dentry->d_count) > 1)
16071da177e4SLinus Torvalds 			/* dentry still busy? */
16081da177e4SLinus Torvalds 			goto out;
160920509f1bSTrond Myklebust 	} else
16109a53c3a7SDave Hansen 		drop_nlink(new_inode);
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds go_ahead:
16131da177e4SLinus Torvalds 	/*
16141da177e4SLinus Torvalds 	 * ... prune child dentries and writebacks if needed.
16151da177e4SLinus Torvalds 	 */
16161da177e4SLinus Torvalds 	if (atomic_read(&old_dentry->d_count) > 1) {
1617e1552e19STrond Myklebust 		if (S_ISREG(old_inode->i_mode))
16181da177e4SLinus Torvalds 			nfs_wb_all(old_inode);
16191da177e4SLinus Torvalds 		shrink_dcache_parent(old_dentry);
16201da177e4SLinus Torvalds 	}
1621cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
16221da177e4SLinus Torvalds 
162324174119STrond Myklebust 	if (new_inode != NULL) {
162424174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
16251da177e4SLinus Torvalds 		d_delete(new_dentry);
162624174119STrond Myklebust 	}
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
16291da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
16305ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
16311da177e4SLinus Torvalds out:
16321da177e4SLinus Torvalds 	if (rehash)
16331da177e4SLinus Torvalds 		d_rehash(rehash);
16341da177e4SLinus Torvalds 	if (!error) {
16351da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
16368fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
16378fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 	/* new dentry created? */
16411da177e4SLinus Torvalds 	if (dentry)
16421da177e4SLinus Torvalds 		dput(dentry);
16431da177e4SLinus Torvalds 	unlock_kernel();
16441da177e4SLinus Torvalds 	return error;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
1647cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1648cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1649cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1650cfcea3e8STrond Myklebust 
16511c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
16521c3c07e9STrond Myklebust {
16531c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
16541c3c07e9STrond Myklebust 	kfree(entry);
1655cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1656cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1657cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
16581c3c07e9STrond Myklebust }
16591c3c07e9STrond Myklebust 
1660979df72eSTrond Myklebust int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
1661979df72eSTrond Myklebust {
1662979df72eSTrond Myklebust 	LIST_HEAD(head);
1663979df72eSTrond Myklebust 	struct nfs_inode *nfsi;
1664979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
1665979df72eSTrond Myklebust 
1666979df72eSTrond Myklebust restart:
1667a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1668979df72eSTrond Myklebust 	list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
1669979df72eSTrond Myklebust 		struct inode *inode;
1670979df72eSTrond Myklebust 
1671979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1672979df72eSTrond Myklebust 			break;
1673979df72eSTrond Myklebust 		inode = igrab(&nfsi->vfs_inode);
1674979df72eSTrond Myklebust 		if (inode == NULL)
1675979df72eSTrond Myklebust 			continue;
1676979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1677979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1678979df72eSTrond Myklebust 			goto remove_lru_entry;
1679979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1680979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1681979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1682979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1683979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1684979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1685979df72eSTrond Myklebust 					&nfs_access_lru_list);
1686979df72eSTrond Myklebust 		else {
1687979df72eSTrond Myklebust remove_lru_entry:
1688979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
1689979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
1690979df72eSTrond Myklebust 		}
1691979df72eSTrond Myklebust 		spin_unlock(&inode->i_lock);
1692a50f7951STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
1693979df72eSTrond Myklebust 		iput(inode);
1694979df72eSTrond Myklebust 		goto restart;
1695979df72eSTrond Myklebust 	}
1696979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
1697979df72eSTrond Myklebust 	while (!list_empty(&head)) {
1698979df72eSTrond Myklebust 		cache = list_entry(head.next, struct nfs_access_entry, lru);
1699979df72eSTrond Myklebust 		list_del(&cache->lru);
1700979df72eSTrond Myklebust 		nfs_access_free_entry(cache);
1701979df72eSTrond Myklebust 	}
1702979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1703979df72eSTrond Myklebust }
1704979df72eSTrond Myklebust 
17051c3c07e9STrond Myklebust static void __nfs_access_zap_cache(struct inode *inode)
17061c3c07e9STrond Myklebust {
17071c3c07e9STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
17081c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
17091c3c07e9STrond Myklebust 	struct rb_node *n, *dispose = NULL;
17101c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
17111c3c07e9STrond Myklebust 
17121c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
17131c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
17141c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
17151c3c07e9STrond Myklebust 		rb_erase(n, root_node);
1716cfcea3e8STrond Myklebust 		list_del(&entry->lru);
17171c3c07e9STrond Myklebust 		n->rb_left = dispose;
17181c3c07e9STrond Myklebust 		dispose = n;
17191c3c07e9STrond Myklebust 	}
17201c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
17211c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
17221c3c07e9STrond Myklebust 
17231c3c07e9STrond Myklebust 	/* Now kill them all! */
17241c3c07e9STrond Myklebust 	while (dispose != NULL) {
17251c3c07e9STrond Myklebust 		n = dispose;
17261c3c07e9STrond Myklebust 		dispose = n->rb_left;
17271c3c07e9STrond Myklebust 		nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
17281c3c07e9STrond Myklebust 	}
17291c3c07e9STrond Myklebust }
17301c3c07e9STrond Myklebust 
17311c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
17321c3c07e9STrond Myklebust {
1733cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
1734cfcea3e8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1735cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
1736cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1737cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
1738cfcea3e8STrond Myklebust 	}
1739cfcea3e8STrond Myklebust 
17401c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
17411c3c07e9STrond Myklebust 	/* This will release the spinlock */
17421c3c07e9STrond Myklebust 	__nfs_access_zap_cache(inode);
17431c3c07e9STrond Myklebust }
17441c3c07e9STrond Myklebust 
17451c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
17461c3c07e9STrond Myklebust {
17471c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
17481c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
17491c3c07e9STrond Myklebust 
17501c3c07e9STrond Myklebust 	while (n != NULL) {
17511c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
17521c3c07e9STrond Myklebust 
17531c3c07e9STrond Myklebust 		if (cred < entry->cred)
17541c3c07e9STrond Myklebust 			n = n->rb_left;
17551c3c07e9STrond Myklebust 		else if (cred > entry->cred)
17561c3c07e9STrond Myklebust 			n = n->rb_right;
17571c3c07e9STrond Myklebust 		else
17581c3c07e9STrond Myklebust 			return entry;
17591c3c07e9STrond Myklebust 	}
17601c3c07e9STrond Myklebust 	return NULL;
17611c3c07e9STrond Myklebust }
17621c3c07e9STrond Myklebust 
1763af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
17641da177e4SLinus Torvalds {
176555296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
17661c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
17671c3c07e9STrond Myklebust 	int err = -ENOENT;
17681da177e4SLinus Torvalds 
17691c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
17701c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
17711c3c07e9STrond Myklebust 		goto out_zap;
17721c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
17731c3c07e9STrond Myklebust 	if (cache == NULL)
17741c3c07e9STrond Myklebust 		goto out;
1775c7c20973STrond Myklebust 	if (!time_in_range(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
17761c3c07e9STrond Myklebust 		goto out_stale;
17771c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
17781c3c07e9STrond Myklebust 	res->cred = cache->cred;
17791c3c07e9STrond Myklebust 	res->mask = cache->mask;
1780cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
17811c3c07e9STrond Myklebust 	err = 0;
17821c3c07e9STrond Myklebust out:
17831c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
17841c3c07e9STrond Myklebust 	return err;
17851c3c07e9STrond Myklebust out_stale:
17861c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
1787cfcea3e8STrond Myklebust 	list_del(&cache->lru);
17881c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
17891c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
17901da177e4SLinus Torvalds 	return -ENOENT;
17911c3c07e9STrond Myklebust out_zap:
17921c3c07e9STrond Myklebust 	/* This will release the spinlock */
17931c3c07e9STrond Myklebust 	__nfs_access_zap_cache(inode);
17941c3c07e9STrond Myklebust 	return -ENOENT;
17951c3c07e9STrond Myklebust }
17961c3c07e9STrond Myklebust 
17971c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
17981c3c07e9STrond Myklebust {
1799cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1800cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
18011c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
18021c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
18031c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
18041c3c07e9STrond Myklebust 
18051c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
18061c3c07e9STrond Myklebust 	while (*p != NULL) {
18071c3c07e9STrond Myklebust 		parent = *p;
18081c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
18091c3c07e9STrond Myklebust 
18101c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
18111c3c07e9STrond Myklebust 			p = &parent->rb_left;
18121c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
18131c3c07e9STrond Myklebust 			p = &parent->rb_right;
18141c3c07e9STrond Myklebust 		else
18151c3c07e9STrond Myklebust 			goto found;
18161c3c07e9STrond Myklebust 	}
18171c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
18181c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
1819cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
18201c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
18211c3c07e9STrond Myklebust 	return;
18221c3c07e9STrond Myklebust found:
18231c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
1824cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
1825cfcea3e8STrond Myklebust 	list_del(&entry->lru);
18261c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
18271c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
1830af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
18311da177e4SLinus Torvalds {
18321c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
18331c3c07e9STrond Myklebust 	if (cache == NULL)
18341c3c07e9STrond Myklebust 		return;
18351c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
18361da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
18371c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
18381da177e4SLinus Torvalds 	cache->mask = set->mask;
18391c3c07e9STrond Myklebust 
18401c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
1841cfcea3e8STrond Myklebust 
1842cfcea3e8STrond Myklebust 	/* Update accounting */
1843cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
1844cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
1845cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
1846cfcea3e8STrond Myklebust 
1847cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
1848cfcea3e8STrond Myklebust 	if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
1849cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
1850cfcea3e8STrond Myklebust 		list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
1851cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
1852cfcea3e8STrond Myklebust 	}
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds 	struct nfs_access_entry cache;
18581da177e4SLinus Torvalds 	int status;
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
18611da177e4SLinus Torvalds 	if (status == 0)
18621da177e4SLinus Torvalds 		goto out;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
18651da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
18661da177e4SLinus Torvalds 	cache.cred = cred;
18671da177e4SLinus Torvalds 	cache.jiffies = jiffies;
18681da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
18691da177e4SLinus Torvalds 	if (status != 0)
18701da177e4SLinus Torvalds 		return status;
18711da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
18721da177e4SLinus Torvalds out:
18731da177e4SLinus Torvalds 	if ((cache.mask & mask) == mask)
18741da177e4SLinus Torvalds 		return 0;
18751da177e4SLinus Torvalds 	return -EACCES;
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
1878af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
1879af22f94aSTrond Myklebust {
1880af22f94aSTrond Myklebust 	int mask = 0;
1881af22f94aSTrond Myklebust 
1882af22f94aSTrond Myklebust 	if (openflags & FMODE_READ)
1883af22f94aSTrond Myklebust 		mask |= MAY_READ;
1884af22f94aSTrond Myklebust 	if (openflags & FMODE_WRITE)
1885af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
1886af22f94aSTrond Myklebust 	if (openflags & FMODE_EXEC)
1887af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
1888af22f94aSTrond Myklebust 	return mask;
1889af22f94aSTrond Myklebust }
1890af22f94aSTrond Myklebust 
1891af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
1892af22f94aSTrond Myklebust {
1893af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
1894af22f94aSTrond Myklebust }
1895af22f94aSTrond Myklebust 
18961da177e4SLinus Torvalds int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
18971da177e4SLinus Torvalds {
18981da177e4SLinus Torvalds 	struct rpc_cred *cred;
18991da177e4SLinus Torvalds 	int res = 0;
19001da177e4SLinus Torvalds 
190191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
190291d5b470SChuck Lever 
19031da177e4SLinus Torvalds 	if (mask == 0)
19041da177e4SLinus Torvalds 		goto out;
19051da177e4SLinus Torvalds 	/* Is this sys_access() ? */
19061da177e4SLinus Torvalds 	if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
19071da177e4SLinus Torvalds 		goto force_lookup;
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
19101da177e4SLinus Torvalds 		case S_IFLNK:
19111da177e4SLinus Torvalds 			goto out;
19121da177e4SLinus Torvalds 		case S_IFREG:
19131da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
19141da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
19151da177e4SLinus Torvalds 					&& nd != NULL
19161da177e4SLinus Torvalds 					&& (nd->flags & LOOKUP_OPEN))
19171da177e4SLinus Torvalds 				goto out;
19181da177e4SLinus Torvalds 			break;
19191da177e4SLinus Torvalds 		case S_IFDIR:
19201da177e4SLinus Torvalds 			/*
19211da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
19221da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
19231da177e4SLinus Torvalds 			 */
19241da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
19251da177e4SLinus Torvalds 				goto out;
19261da177e4SLinus Torvalds 	}
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds force_lookup:
19291da177e4SLinus Torvalds 	lock_kernel();
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
19321da177e4SLinus Torvalds 		goto out_notsup;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
19351da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
19361da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
19371da177e4SLinus Torvalds 		put_rpccred(cred);
19381da177e4SLinus Torvalds 	} else
19391da177e4SLinus Torvalds 		res = PTR_ERR(cred);
19401da177e4SLinus Torvalds 	unlock_kernel();
19411da177e4SLinus Torvalds out:
19421e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
19431e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
19441da177e4SLinus Torvalds 	return res;
19451da177e4SLinus Torvalds out_notsup:
19461da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
19471da177e4SLinus Torvalds 	if (res == 0)
19481da177e4SLinus Torvalds 		res = generic_permission(inode, mask, NULL);
19491da177e4SLinus Torvalds 	unlock_kernel();
19501e7cb3dcSChuck Lever 	goto out;
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds /*
19541da177e4SLinus Torvalds  * Local variables:
19551da177e4SLinus Torvalds  *  version-control: t
19561da177e4SLinus Torvalds  *  kept-new-versions: 5
19571da177e4SLinus Torvalds  * End:
19581da177e4SLinus Torvalds  */
1959