xref: /openbmc/linux/fs/nfs/dir.c (revision 30d90494)
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>
32873101b3SChuck Lever #include <linux/pagevec.h>
331da177e4SLinus Torvalds #include <linux/namei.h>
3454ceac45SDavid Howells #include <linux/mount.h>
35e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3604e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3764c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include "delegation.h"
4091d5b470SChuck Lever #include "iostat.h"
414c30d56eSAdrian Bunk #include "internal.h"
42cd9a1c0eSTrond Myklebust #include "fscache.h"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
47480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
481da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
491da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
504acdaf27SAl Viro static int nfs_create(struct inode *, struct dentry *, umode_t, struct nameidata *);
5118bb1db3SAl Viro static int nfs_mkdir(struct inode *, struct dentry *, umode_t);
521da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
531da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
541da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
551da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
561a67aafbSAl Viro static int nfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
571da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
581da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
5902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
60f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
6111de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
621da177e4SLinus Torvalds 
634b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
64f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
651da177e4SLinus Torvalds 	.read		= generic_read_dir,
661da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
671da177e4SLinus Torvalds 	.open		= nfs_opendir,
68480c2006SBryan Schumaker 	.release	= nfs_closedir,
691da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
7292e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
731da177e4SLinus Torvalds 	.create		= nfs_create,
741da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
751da177e4SLinus Torvalds 	.link		= nfs_link,
761da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
771da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
781da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
791da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
801da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
811da177e4SLinus Torvalds 	.rename		= nfs_rename,
821da177e4SLinus Torvalds 	.permission	= nfs_permission,
831da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
841da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
8711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
8811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
89d1bacf9eSBryan Schumaker };
90d1bacf9eSBryan Schumaker 
91b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
9292e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
93b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
94b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
95b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
96b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
97b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
98b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
99b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
100b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
101b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
102b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
103b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
104b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
105b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
106b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
107b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
108b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
109b7fa0554SAndreas Gruenbacher };
110b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
111b7fa0554SAndreas Gruenbacher 
1121da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1131da177e4SLinus Torvalds 
114d9585277SAl Viro static int nfs_atomic_open(struct inode *, struct dentry *,
11530d90494SAl Viro 			   struct file *, unsigned, umode_t,
11647237687SAl Viro 			   int *);
11792e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
1188867fe58SMiklos Szeredi 	.create		= nfs_create,
1190dd2b474SMiklos Szeredi 	.lookup		= nfs_lookup,
1200dd2b474SMiklos Szeredi 	.atomic_open	= nfs_atomic_open,
1211da177e4SLinus Torvalds 	.link		= nfs_link,
1221da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1231da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1241da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1251da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1261da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1271da177e4SLinus Torvalds 	.rename		= nfs_rename,
1281da177e4SLinus Torvalds 	.permission	= nfs_permission,
1291da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1301da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
13164c2ce8bSAneesh Kumar K.V 	.getxattr	= generic_getxattr,
13264c2ce8bSAneesh Kumar K.V 	.setxattr	= generic_setxattr,
13364c2ce8bSAneesh Kumar K.V 	.listxattr	= generic_listxattr,
13464c2ce8bSAneesh Kumar K.V 	.removexattr	= generic_removexattr,
1351da177e4SLinus Torvalds };
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1381da177e4SLinus Torvalds 
1390c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
140480c2006SBryan Schumaker {
141480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
142480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
143480c2006SBryan Schumaker 	if (ctx != NULL) {
1448ef2ce3eSBryan Schumaker 		ctx->duped = 0;
1450c030806STrond Myklebust 		ctx->attr_gencount = NFS_I(dir)->attr_gencount;
146480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
1478ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
148480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
149480c2006SBryan Schumaker 		return ctx;
150480c2006SBryan Schumaker 	}
1510c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
1520c030806STrond Myklebust }
153480c2006SBryan Schumaker 
154480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
155480c2006SBryan Schumaker {
156480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
157480c2006SBryan Schumaker 	kfree(ctx);
158480c2006SBryan Schumaker }
159480c2006SBryan Schumaker 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  * Open file
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds static int
1641da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1651da177e4SLinus Torvalds {
166480c2006SBryan Schumaker 	int res = 0;
167480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
168480c2006SBryan Schumaker 	struct rpc_cred *cred;
1691da177e4SLinus Torvalds 
1706da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
171cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
172cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
173cc0dd2d1SChuck Lever 
174cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1751e7cb3dcSChuck Lever 
176480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
177480c2006SBryan Schumaker 	if (IS_ERR(cred))
178480c2006SBryan Schumaker 		return PTR_ERR(cred);
1790c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
180480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
181480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
182480c2006SBryan Schumaker 		goto out;
183480c2006SBryan Schumaker 	}
184480c2006SBryan Schumaker 	filp->private_data = ctx;
185f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
186f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
187f5a73672SNeil Brown 		 * have been called, so we need to refresh the
188f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
189f5a73672SNeil Brown 		 */
190f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
191f5a73672SNeil Brown 	}
192480c2006SBryan Schumaker out:
193480c2006SBryan Schumaker 	put_rpccred(cred);
1941da177e4SLinus Torvalds 	return res;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
197480c2006SBryan Schumaker static int
198480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
199480c2006SBryan Schumaker {
200480c2006SBryan Schumaker 	put_nfs_open_dir_context(filp->private_data);
201480c2006SBryan Schumaker 	return 0;
202480c2006SBryan Schumaker }
203480c2006SBryan Schumaker 
204d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
205d1bacf9eSBryan Schumaker 	u64 cookie;
206d1bacf9eSBryan Schumaker 	u64 ino;
207d1bacf9eSBryan Schumaker 	struct qstr string;
2080b26a0bfSTrond Myklebust 	unsigned char d_type;
209d1bacf9eSBryan Schumaker };
210d1bacf9eSBryan Schumaker 
211d1bacf9eSBryan Schumaker struct nfs_cache_array {
21288b8e133SChuck Lever 	int size;
213d1bacf9eSBryan Schumaker 	int eof_index;
214d1bacf9eSBryan Schumaker 	u64 last_cookie;
215d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
216d1bacf9eSBryan Schumaker };
217d1bacf9eSBryan Schumaker 
218573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
2191da177e4SLinus Torvalds typedef struct {
2201da177e4SLinus Torvalds 	struct file	*file;
2211da177e4SLinus Torvalds 	struct page	*page;
2221da177e4SLinus Torvalds 	unsigned long	page_index;
223f0dd2136STrond Myklebust 	u64		*dir_cookie;
2240aded708STrond Myklebust 	u64		last_cookie;
225f0dd2136STrond Myklebust 	loff_t		current_index;
2261da177e4SLinus Torvalds 	decode_dirent_t	decode;
227d1bacf9eSBryan Schumaker 
2281f4eab7eSNeil Brown 	unsigned long	timestamp;
2294704f0e2STrond Myklebust 	unsigned long	gencount;
230d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
231d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
232d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
2331da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
2341da177e4SLinus Torvalds 
235d1bacf9eSBryan Schumaker /*
236d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
2371da177e4SLinus Torvalds  */
2381da177e4SLinus Torvalds static
239d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
2401da177e4SLinus Torvalds {
2418cd51a0cSTrond Myklebust 	void *ptr;
242d1bacf9eSBryan Schumaker 	if (page == NULL)
243d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
2448cd51a0cSTrond Myklebust 	ptr = kmap(page);
2458cd51a0cSTrond Myklebust 	if (ptr == NULL)
2468cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
2478cd51a0cSTrond Myklebust 	return ptr;
248d1bacf9eSBryan Schumaker }
249d1bacf9eSBryan Schumaker 
250d1bacf9eSBryan Schumaker static
251d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
252d1bacf9eSBryan Schumaker {
253d1bacf9eSBryan Schumaker 	kunmap(page);
254d1bacf9eSBryan Schumaker }
255d1bacf9eSBryan Schumaker 
256d1bacf9eSBryan Schumaker /*
257d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
258d1bacf9eSBryan Schumaker  */
259d1bacf9eSBryan Schumaker static
26011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
261d1bacf9eSBryan Schumaker {
26211de3b11STrond Myklebust 	struct nfs_cache_array *array;
263d1bacf9eSBryan Schumaker 	int i;
2648cd51a0cSTrond Myklebust 
2652b86ce2dSCong Wang 	array = kmap_atomic(page);
266d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
267d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
2682b86ce2dSCong Wang 	kunmap_atomic(array);
269d1bacf9eSBryan Schumaker }
270d1bacf9eSBryan Schumaker 
271d1bacf9eSBryan Schumaker /*
272d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
273d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
274d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
275d1bacf9eSBryan Schumaker  */
276d1bacf9eSBryan Schumaker static
2774a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
278d1bacf9eSBryan Schumaker {
279d1bacf9eSBryan Schumaker 	string->len = len;
280d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2814a201d6eSTrond Myklebust 	if (string->name == NULL)
2824a201d6eSTrond Myklebust 		return -ENOMEM;
28304e4bd1cSCatalin Marinas 	/*
28404e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
28504e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
28604e4bd1cSCatalin Marinas 	 */
28704e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2884a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2894a201d6eSTrond Myklebust 	return 0;
290d1bacf9eSBryan Schumaker }
291d1bacf9eSBryan Schumaker 
292d1bacf9eSBryan Schumaker static
293d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
294d1bacf9eSBryan Schumaker {
295d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2964a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2974a201d6eSTrond Myklebust 	int ret;
2984a201d6eSTrond Myklebust 
299d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
300d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
301d1bacf9eSBryan Schumaker 
3024a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
3033020093fSTrond Myklebust 
3043020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
3053020093fSTrond Myklebust 	ret = -ENOSPC;
3063020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
3073020093fSTrond Myklebust 		goto out;
3083020093fSTrond Myklebust 
3094a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
3104a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
3110b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
3124a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
3134a201d6eSTrond Myklebust 	if (ret)
3144a201d6eSTrond Myklebust 		goto out;
315d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
3168cd51a0cSTrond Myklebust 	array->size++;
31747c716cbSTrond Myklebust 	if (entry->eof != 0)
318d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
3194a201d6eSTrond Myklebust out:
320d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
3214a201d6eSTrond Myklebust 	return ret;
322d1bacf9eSBryan Schumaker }
323d1bacf9eSBryan Schumaker 
324d1bacf9eSBryan Schumaker static
325d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
326d1bacf9eSBryan Schumaker {
327d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
328d1bacf9eSBryan Schumaker 	unsigned int index;
329d1bacf9eSBryan Schumaker 
330d1bacf9eSBryan Schumaker 	if (diff < 0)
331d1bacf9eSBryan Schumaker 		goto out_eof;
332d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
3338cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
334d1bacf9eSBryan Schumaker 			goto out_eof;
335d1bacf9eSBryan Schumaker 		return -EAGAIN;
336d1bacf9eSBryan Schumaker 	}
337d1bacf9eSBryan Schumaker 
338d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
339d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
340d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
341d1bacf9eSBryan Schumaker 	return 0;
342d1bacf9eSBryan Schumaker out_eof:
343d1bacf9eSBryan Schumaker 	desc->eof = 1;
344d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
345d1bacf9eSBryan Schumaker }
346d1bacf9eSBryan Schumaker 
347d1bacf9eSBryan Schumaker static
348d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
349d1bacf9eSBryan Schumaker {
350d1bacf9eSBryan Schumaker 	int i;
3518ef2ce3eSBryan Schumaker 	loff_t new_pos;
352d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
353d1bacf9eSBryan Schumaker 
354d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3558cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
3560c030806STrond Myklebust 			struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode);
3570c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
3580c030806STrond Myklebust 
3598ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3600c030806STrond Myklebust 			if (ctx->attr_gencount != nfsi->attr_gencount
3610c030806STrond Myklebust 			    || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
3620c030806STrond Myklebust 				ctx->duped = 0;
3630c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
3640c030806STrond Myklebust 			} else if (new_pos < desc->file->f_pos) {
3650c030806STrond Myklebust 				if (ctx->duped > 0
3660c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
3670c030806STrond Myklebust 					if (printk_ratelimit()) {
3680c030806STrond Myklebust 						pr_notice("NFS: directory %s/%s contains a readdir loop."
3690c030806STrond Myklebust 								"Please contact your server vendor.  "
370374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
3710c030806STrond Myklebust 								desc->file->f_dentry->d_parent->d_name.name,
3720c030806STrond Myklebust 								desc->file->f_dentry->d_name.name,
373374e4e3eSBryan Schumaker 								array->array[i].string.name,
3740c030806STrond Myklebust 								*desc->dir_cookie);
3750c030806STrond Myklebust 					}
3760c030806STrond Myklebust 					status = -ELOOP;
3770c030806STrond Myklebust 					goto out;
3780c030806STrond Myklebust 				}
3798ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3800c030806STrond Myklebust 				ctx->duped = -1;
3818ef2ce3eSBryan Schumaker 			}
3828ef2ce3eSBryan Schumaker 			desc->file->f_pos = new_pos;
3838cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
38447c716cbSTrond Myklebust 			return 0;
3858cd51a0cSTrond Myklebust 		}
3868cd51a0cSTrond Myklebust 	}
38747c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
388d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
38918fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
39018fb5fe4STrond Myklebust 			desc->eof = 1;
391d1bacf9eSBryan Schumaker 	}
3920c030806STrond Myklebust out:
393d1bacf9eSBryan Schumaker 	return status;
394d1bacf9eSBryan Schumaker }
395d1bacf9eSBryan Schumaker 
396d1bacf9eSBryan Schumaker static
397d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
398d1bacf9eSBryan Schumaker {
399d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
40047c716cbSTrond Myklebust 	int status;
401d1bacf9eSBryan Schumaker 
402d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
403d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
404d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
405d1bacf9eSBryan Schumaker 		goto out;
406d1bacf9eSBryan Schumaker 	}
407d1bacf9eSBryan Schumaker 
408d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
409d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
410d1bacf9eSBryan Schumaker 	else
411d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
412d1bacf9eSBryan Schumaker 
41347c716cbSTrond Myklebust 	if (status == -EAGAIN) {
4140aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
415e47c085aSTrond Myklebust 		desc->current_index += array->size;
41647c716cbSTrond Myklebust 		desc->page_index++;
41747c716cbSTrond Myklebust 	}
418d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
419d1bacf9eSBryan Schumaker out:
420d1bacf9eSBryan Schumaker 	return status;
421d1bacf9eSBryan Schumaker }
422d1bacf9eSBryan Schumaker 
423d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
424d1bacf9eSBryan Schumaker static
42556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
426d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
427d1bacf9eSBryan Schumaker {
428480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
429480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
4304704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
4311da177e4SLinus Torvalds 	int		error;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds  again:
4341da177e4SLinus Torvalds 	timestamp = jiffies;
4354704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
43656e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
4371da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
4381da177e4SLinus Torvalds 	if (error < 0) {
4391da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
4401da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
4411da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
4423a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
4431da177e4SLinus Torvalds 			desc->plus = 0;
4441da177e4SLinus Torvalds 			goto again;
4451da177e4SLinus Torvalds 		}
4461da177e4SLinus Torvalds 		goto error;
4471da177e4SLinus Torvalds 	}
4481f4eab7eSNeil Brown 	desc->timestamp = timestamp;
4494704f0e2STrond Myklebust 	desc->gencount = gencount;
450d1bacf9eSBryan Schumaker error:
451d1bacf9eSBryan Schumaker 	return error;
452d1bacf9eSBryan Schumaker }
453d1bacf9eSBryan Schumaker 
454573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
455573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
456d1bacf9eSBryan Schumaker {
457573c4e1eSChuck Lever 	int error;
458d1bacf9eSBryan Schumaker 
459573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
460573c4e1eSChuck Lever 	if (error)
461573c4e1eSChuck Lever 		return error;
462d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
463d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
464d1bacf9eSBryan Schumaker 	return 0;
465d1bacf9eSBryan Schumaker }
466d1bacf9eSBryan Schumaker 
467d39ab9deSBryan Schumaker static
468d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
469d39ab9deSBryan Schumaker {
470d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
471d39ab9deSBryan Schumaker 		goto different;
47237a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
473d39ab9deSBryan Schumaker 		goto different;
474d39ab9deSBryan Schumaker 	return 1;
475d39ab9deSBryan Schumaker different:
476d39ab9deSBryan Schumaker 	return 0;
477d39ab9deSBryan Schumaker }
478d39ab9deSBryan Schumaker 
479d39ab9deSBryan Schumaker static
480d69ee9b8STrond Myklebust bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
481d69ee9b8STrond Myklebust {
482d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
483d69ee9b8STrond Myklebust 		return false;
484d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
485d69ee9b8STrond Myklebust 		return true;
486d69ee9b8STrond Myklebust 	if (filp->f_pos == 0)
487d69ee9b8STrond Myklebust 		return true;
488d69ee9b8STrond Myklebust 	return false;
489d69ee9b8STrond Myklebust }
490d69ee9b8STrond Myklebust 
491d69ee9b8STrond Myklebust /*
492d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
493d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
494d69ee9b8STrond Myklebust  * directory.
495d69ee9b8STrond Myklebust  */
496d69ee9b8STrond Myklebust static
497d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
498d69ee9b8STrond Myklebust {
499d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
500d69ee9b8STrond Myklebust }
501d69ee9b8STrond Myklebust 
502d69ee9b8STrond Myklebust static
503d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
504d39ab9deSBryan Schumaker {
50526fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
5064a201d6eSTrond Myklebust 	struct dentry *dentry;
5074a201d6eSTrond Myklebust 	struct dentry *alias;
508d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
509d39ab9deSBryan Schumaker 	struct inode *inode;
510d39ab9deSBryan Schumaker 
5114a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
5124a201d6eSTrond Myklebust 		if (filename.len == 1)
5134a201d6eSTrond Myklebust 			return;
5144a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
5154a201d6eSTrond Myklebust 			return;
5164a201d6eSTrond Myklebust 	}
5174a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
518d39ab9deSBryan Schumaker 
5194a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
520d39ab9deSBryan Schumaker 	if (dentry != NULL) {
521d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
522d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
523d39ab9deSBryan Schumaker 			goto out;
524d39ab9deSBryan Schumaker 		} else {
525d39ab9deSBryan Schumaker 			d_drop(dentry);
526d39ab9deSBryan Schumaker 			dput(dentry);
527d39ab9deSBryan Schumaker 		}
528d39ab9deSBryan Schumaker 	}
529d39ab9deSBryan Schumaker 
530d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
5314a201d6eSTrond Myklebust 	if (dentry == NULL)
5324a201d6eSTrond Myklebust 		return;
5334a201d6eSTrond Myklebust 
534d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
535d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
536d39ab9deSBryan Schumaker 		goto out;
537d39ab9deSBryan Schumaker 
538d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
539d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
540d39ab9deSBryan Schumaker 		goto out;
541d39ab9deSBryan Schumaker 	else if (alias) {
542d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
543d39ab9deSBryan Schumaker 		dput(alias);
544d39ab9deSBryan Schumaker 	} else
545d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
546d39ab9deSBryan Schumaker 
547d39ab9deSBryan Schumaker out:
548d39ab9deSBryan Schumaker 	dput(dentry);
549d39ab9deSBryan Schumaker }
550d39ab9deSBryan Schumaker 
551d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
552d1bacf9eSBryan Schumaker static
5538cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5546650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
555d1bacf9eSBryan Schumaker {
556babddc72SBryan Schumaker 	struct xdr_stream stream;
557f7da7a12SBenny Halevy 	struct xdr_buf buf;
5586650239aSTrond Myklebust 	struct page *scratch;
55999424380SBryan Schumaker 	struct nfs_cache_array *array;
5605c346854STrond Myklebust 	unsigned int count = 0;
5615c346854STrond Myklebust 	int status;
562babddc72SBryan Schumaker 
5636650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5646650239aSTrond Myklebust 	if (scratch == NULL)
5656650239aSTrond Myklebust 		return -ENOMEM;
566babddc72SBryan Schumaker 
567f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5686650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
56999424380SBryan Schumaker 
57099424380SBryan Schumaker 	do {
57199424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5728cd51a0cSTrond Myklebust 		if (status != 0) {
5738cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5748cd51a0cSTrond Myklebust 				status = 0;
57599424380SBryan Schumaker 			break;
5768cd51a0cSTrond Myklebust 		}
57799424380SBryan Schumaker 
5785c346854STrond Myklebust 		count++;
5795c346854STrond Myklebust 
58047c716cbSTrond Myklebust 		if (desc->plus != 0)
581d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5828cd51a0cSTrond Myklebust 
5838cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5848cd51a0cSTrond Myklebust 		if (status != 0)
5858cd51a0cSTrond Myklebust 			break;
58699424380SBryan Schumaker 	} while (!entry->eof);
58799424380SBryan Schumaker 
58847c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
58999424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5908cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5918cd51a0cSTrond Myklebust 			array->eof_index = array->size;
59299424380SBryan Schumaker 			status = 0;
59399424380SBryan Schumaker 			nfs_readdir_release_array(page);
5945c346854STrond Myklebust 		} else
5955c346854STrond Myklebust 			status = PTR_ERR(array);
59656e4ebf8SBryan Schumaker 	}
5976650239aSTrond Myklebust 
5986650239aSTrond Myklebust 	put_page(scratch);
5998cd51a0cSTrond Myklebust 	return status;
6008cd51a0cSTrond Myklebust }
60156e4ebf8SBryan Schumaker 
60256e4ebf8SBryan Schumaker static
60356e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
60456e4ebf8SBryan Schumaker {
60556e4ebf8SBryan Schumaker 	unsigned int i;
60656e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
60756e4ebf8SBryan Schumaker 		put_page(pages[i]);
60856e4ebf8SBryan Schumaker }
60956e4ebf8SBryan Schumaker 
61056e4ebf8SBryan Schumaker static
61156e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
61256e4ebf8SBryan Schumaker 		unsigned int npages)
61356e4ebf8SBryan Schumaker {
61456e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
61556e4ebf8SBryan Schumaker }
61656e4ebf8SBryan Schumaker 
61756e4ebf8SBryan Schumaker /*
61856e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
61956e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
62056e4ebf8SBryan Schumaker  */
62156e4ebf8SBryan Schumaker static
6226650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
62356e4ebf8SBryan Schumaker {
62456e4ebf8SBryan Schumaker 	unsigned int i;
62556e4ebf8SBryan Schumaker 
62656e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
62756e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
62856e4ebf8SBryan Schumaker 		if (page == NULL)
62956e4ebf8SBryan Schumaker 			goto out_freepages;
63056e4ebf8SBryan Schumaker 		pages[i] = page;
63156e4ebf8SBryan Schumaker 	}
6326650239aSTrond Myklebust 	return 0;
63356e4ebf8SBryan Schumaker 
63456e4ebf8SBryan Schumaker out_freepages:
63556e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
6366650239aSTrond Myklebust 	return -ENOMEM;
637d1bacf9eSBryan Schumaker }
638d1bacf9eSBryan Schumaker 
639d1bacf9eSBryan Schumaker static
640d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
641d1bacf9eSBryan Schumaker {
64256e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
64356e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
644d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
645d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
646d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6478cd51a0cSTrond Myklebust 	int status = -ENOMEM;
64856e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
649d1bacf9eSBryan Schumaker 
650d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6510aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
652d1bacf9eSBryan Schumaker 	entry.eof = 0;
653d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
654d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
655573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
656d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
657d1bacf9eSBryan Schumaker 		goto out;
658d1bacf9eSBryan Schumaker 
659d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6608cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6618cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
6628cd51a0cSTrond Myklebust 		goto out;
6638cd51a0cSTrond Myklebust 	}
664d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
665d1bacf9eSBryan Schumaker 	array->eof_index = -1;
666d1bacf9eSBryan Schumaker 
6676650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6686650239aSTrond Myklebust 	if (status < 0)
669d1bacf9eSBryan Schumaker 		goto out_release_array;
670d1bacf9eSBryan Schumaker 	do {
671ac396128STrond Myklebust 		unsigned int pglen;
67256e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
673babddc72SBryan Schumaker 
674d1bacf9eSBryan Schumaker 		if (status < 0)
675d1bacf9eSBryan Schumaker 			break;
676ac396128STrond Myklebust 		pglen = status;
6776650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6788cd51a0cSTrond Myklebust 		if (status < 0) {
6798cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6808cd51a0cSTrond Myklebust 				status = 0;
6818cd51a0cSTrond Myklebust 			break;
6828cd51a0cSTrond Myklebust 		}
6838cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
684d1bacf9eSBryan Schumaker 
68556e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
686d1bacf9eSBryan Schumaker out_release_array:
687d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
688d1bacf9eSBryan Schumaker out:
689d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
690d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
691d1bacf9eSBryan Schumaker 	return status;
692d1bacf9eSBryan Schumaker }
693d1bacf9eSBryan Schumaker 
694d1bacf9eSBryan Schumaker /*
695d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
696d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
697d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
698d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6991da177e4SLinus Torvalds  */
700d1bacf9eSBryan Schumaker static
701d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
702d1bacf9eSBryan Schumaker {
703d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
7048cd51a0cSTrond Myklebust 	int ret;
705d1bacf9eSBryan Schumaker 
7068cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
7078cd51a0cSTrond Myklebust 	if (ret < 0)
708d1bacf9eSBryan Schumaker 		goto error;
709d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
710d1bacf9eSBryan Schumaker 
7112aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
712cd9ae2b6STrond Myklebust 		/* Should never happen */
713cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
714cd9ae2b6STrond Myklebust 	}
7151da177e4SLinus Torvalds 	unlock_page(page);
7161da177e4SLinus Torvalds 	return 0;
7171da177e4SLinus Torvalds  error:
7181da177e4SLinus Torvalds 	unlock_page(page);
7198cd51a0cSTrond Myklebust 	return ret;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
722d1bacf9eSBryan Schumaker static
723d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7241da177e4SLinus Torvalds {
72511de3b11STrond Myklebust 	if (!desc->page->mapping)
72611de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
7271da177e4SLinus Torvalds 	page_cache_release(desc->page);
7281da177e4SLinus Torvalds 	desc->page = NULL;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
731d1bacf9eSBryan Schumaker static
732d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7331da177e4SLinus Torvalds {
7348cd51a0cSTrond Myklebust 	return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
735d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /*
739d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7401da177e4SLinus Torvalds  */
741d1bacf9eSBryan Schumaker static
742d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
743d1bacf9eSBryan Schumaker {
744d1bacf9eSBryan Schumaker 	int res;
745d1bacf9eSBryan Schumaker 
746d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
747d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
748d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
749d1bacf9eSBryan Schumaker 
750d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
75147c716cbSTrond Myklebust 	if (res != 0)
752d1bacf9eSBryan Schumaker 		cache_page_release(desc);
753d1bacf9eSBryan Schumaker 	return res;
754d1bacf9eSBryan Schumaker }
755d1bacf9eSBryan Schumaker 
756d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7571da177e4SLinus Torvalds static inline
7581da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7591da177e4SLinus Torvalds {
7608cd51a0cSTrond Myklebust 	int res;
761d1bacf9eSBryan Schumaker 
7620aded708STrond Myklebust 	if (desc->page_index == 0) {
7638cd51a0cSTrond Myklebust 		desc->current_index = 0;
7640aded708STrond Myklebust 		desc->last_cookie = 0;
7650aded708STrond Myklebust 	}
76647c716cbSTrond Myklebust 	do {
767d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
76847c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7691da177e4SLinus Torvalds 	return res;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /*
7731da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7741da177e4SLinus Torvalds  */
7751da177e4SLinus Torvalds static
7761da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
7771da177e4SLinus Torvalds 		   filldir_t filldir)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	struct file	*file = desc->file;
780d1bacf9eSBryan Schumaker 	int i = 0;
781d1bacf9eSBryan Schumaker 	int res = 0;
782d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7838ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7848ef2ce3eSBryan Schumaker 
785d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
786e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
787e7c58e97STrond Myklebust 		res = PTR_ERR(array);
788e7c58e97STrond Myklebust 		goto out;
789e7c58e97STrond Myklebust 	}
7901da177e4SLinus Torvalds 
791d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
792ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7931da177e4SLinus Torvalds 
794ece0b423STrond Myklebust 		ent = &array->array[i];
795ece0b423STrond Myklebust 		if (filldir(dirent, ent->string.name, ent->string.len,
7960b26a0bfSTrond Myklebust 		    file->f_pos, nfs_compat_user_ino64(ent->ino),
7970b26a0bfSTrond Myklebust 		    ent->d_type) < 0) {
798ece0b423STrond Myklebust 			desc->eof = 1;
7991da177e4SLinus Torvalds 			break;
800ece0b423STrond Myklebust 		}
80100a92642SOlivier Galibert 		file->f_pos++;
802d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
803d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
804d1bacf9eSBryan Schumaker 		else
805d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
8060c030806STrond Myklebust 		if (ctx->duped != 0)
8070c030806STrond Myklebust 			ctx->duped = 1;
8088cd51a0cSTrond Myklebust 	}
80947c716cbSTrond Myklebust 	if (array->eof_index >= 0)
810d1bacf9eSBryan Schumaker 		desc->eof = 1;
811d1bacf9eSBryan Schumaker 
812d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
813e7c58e97STrond Myklebust out:
814d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8151e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8161e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
8171da177e4SLinus Torvalds 	return res;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds /*
8211da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8221da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8231da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8241da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8251da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8261da177e4SLinus Torvalds  *
8271da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8281da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8291da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8301da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8311da177e4SLinus Torvalds  */
8321da177e4SLinus Torvalds static inline
8331da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
8341da177e4SLinus Torvalds 		     filldir_t filldir)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	struct page	*page = NULL;
8371da177e4SLinus Torvalds 	int		status;
838d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
8390c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8401da177e4SLinus Torvalds 
8411e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8421e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8451da177e4SLinus Torvalds 	if (!page) {
8461da177e4SLinus Torvalds 		status = -ENOMEM;
8471da177e4SLinus Torvalds 		goto out;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds 
8507a8e1dc3STrond Myklebust 	desc->page_index = 0;
8510aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8527a8e1dc3STrond Myklebust 	desc->page = page;
8530c030806STrond Myklebust 	ctx->duped = 0;
8547a8e1dc3STrond Myklebust 
85585f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
85685f8607eSTrond Myklebust 	if (status < 0)
857d1bacf9eSBryan Schumaker 		goto out_release;
858d1bacf9eSBryan Schumaker 
8591da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds  out:
8621e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8633110ff80SHarvey Harrison 			__func__, status);
8641da177e4SLinus Torvalds 	return status;
8651da177e4SLinus Torvalds  out_release:
866d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8671da177e4SLinus Torvalds 	goto out;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
87000a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
87100a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
87200a92642SOlivier Galibert    whole directory.
8731da177e4SLinus Torvalds  */
8741da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
8751da177e4SLinus Torvalds {
87601cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
8771da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8781da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8791da177e4SLinus Torvalds 			*desc = &my_desc;
880480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
88147c716cbSTrond Myklebust 	int res;
8821da177e4SLinus Torvalds 
8836da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8841e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8851e7cb3dcSChuck Lever 			(long long)filp->f_pos);
88691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
88791d5b470SChuck Lever 
8881da177e4SLinus Torvalds 	/*
88900a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
890f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
89100a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
89200a92642SOlivier Galibert 	 * revalidate the cookie.
8931da177e4SLinus Torvalds 	 */
8941da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	desc->file = filp;
897480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8981da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
899d69ee9b8STrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
9001da177e4SLinus Torvalds 
901565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
9021cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
903fccca7fcSTrond Myklebust 	if (res < 0)
904fccca7fcSTrond Myklebust 		goto out;
905fccca7fcSTrond Myklebust 
90647c716cbSTrond Myklebust 	do {
9071da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
90800a92642SOlivier Galibert 
9091da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
910ece0b423STrond Myklebust 			res = 0;
9111da177e4SLinus Torvalds 			/* This means either end of directory */
912d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
9131da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
9141da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
915ece0b423STrond Myklebust 				if (res == 0)
9161da177e4SLinus Torvalds 					continue;
9171da177e4SLinus Torvalds 			}
9181da177e4SLinus Torvalds 			break;
9191da177e4SLinus Torvalds 		}
9201da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9213a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9221da177e4SLinus Torvalds 			nfs_zap_caches(inode);
923baf57a09STrond Myklebust 			desc->page_index = 0;
9241da177e4SLinus Torvalds 			desc->plus = 0;
925d1bacf9eSBryan Schumaker 			desc->eof = 0;
9261da177e4SLinus Torvalds 			continue;
9271da177e4SLinus Torvalds 		}
9281da177e4SLinus Torvalds 		if (res < 0)
9291da177e4SLinus Torvalds 			break;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
932ece0b423STrond Myklebust 		if (res < 0)
9331da177e4SLinus Torvalds 			break;
93447c716cbSTrond Myklebust 	} while (!desc->eof);
935fccca7fcSTrond Myklebust out:
936565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
9371e7cb3dcSChuck Lever 	if (res > 0)
9381e7cb3dcSChuck Lever 		res = 0;
939aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
9401e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9411e7cb3dcSChuck Lever 			res);
9421da177e4SLinus Torvalds 	return res;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
94510afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
946f0dd2136STrond Myklebust {
947b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
948b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
949480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
950b84e06c5SChuck Lever 
9516da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
952b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
953b84e06c5SChuck Lever 			dentry->d_name.name,
954b84e06c5SChuck Lever 			offset, origin);
955b84e06c5SChuck Lever 
956b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
957f0dd2136STrond Myklebust 	switch (origin) {
958f0dd2136STrond Myklebust 		case 1:
959f0dd2136STrond Myklebust 			offset += filp->f_pos;
960f0dd2136STrond Myklebust 		case 0:
961f0dd2136STrond Myklebust 			if (offset >= 0)
962f0dd2136STrond Myklebust 				break;
963f0dd2136STrond Myklebust 		default:
964f0dd2136STrond Myklebust 			offset = -EINVAL;
965f0dd2136STrond Myklebust 			goto out;
966f0dd2136STrond Myklebust 	}
967f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
968f0dd2136STrond Myklebust 		filp->f_pos = offset;
969480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9708ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
971f0dd2136STrond Myklebust 	}
972f0dd2136STrond Myklebust out:
973b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
974f0dd2136STrond Myklebust 	return offset;
975f0dd2136STrond Myklebust }
976f0dd2136STrond Myklebust 
9771da177e4SLinus Torvalds /*
9781da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9791da177e4SLinus Torvalds  * is a dummy operation.
9801da177e4SLinus Torvalds  */
98102c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
98202c24a82SJosef Bacik 			 int datasync)
9831da177e4SLinus Torvalds {
9847ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
98502c24a82SJosef Bacik 	struct inode *inode = dentry->d_inode;
9867ea80859SChristoph Hellwig 
9876da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9881e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9891e7cb3dcSChuck Lever 			datasync);
9901e7cb3dcSChuck Lever 
99102c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
99254917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
99302c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9941da177e4SLinus Torvalds 	return 0;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
997bfc69a45STrond Myklebust /**
998bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
999bfc69a45STrond Myklebust  * @dir - pointer to directory inode
1000bfc69a45STrond Myklebust  *
1001bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1002bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1003bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1004bfc69a45STrond Myklebust  *
1005bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1006bfc69a45STrond Myklebust  */
1007bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1008bfc69a45STrond Myklebust {
1009011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
1010bfc69a45STrond Myklebust }
1011bfc69a45STrond Myklebust 
10121da177e4SLinus Torvalds /*
10131da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
10141da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
10151da177e4SLinus Torvalds  * and may need to be looked up again.
10161da177e4SLinus Torvalds  */
1017c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
10201da177e4SLinus Torvalds 		return 1;
10214eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
10224eec952eSTrond Myklebust 		return 0;
1023f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10246ecc5e8fSTrond Myklebust 		return 0;
1025f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1026f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1027f2c77f4eSTrond Myklebust 		return 0;
1028f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1029f2c77f4eSTrond Myklebust 		return 0;
1030f2c77f4eSTrond Myklebust 	return 1;
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /*
10341d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
10351d6757fbSTrond Myklebust  *
10361d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
10378aeb376cSAl Viro  * component of the path and none of them is set before that last
10388aeb376cSAl Viro  * component.
10391d6757fbSTrond Myklebust  */
104034286d66SNick Piggin static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
104134286d66SNick Piggin 						unsigned int mask)
10421d6757fbSTrond Myklebust {
10431d6757fbSTrond Myklebust 	return nd->flags & mask;
10441d6757fbSTrond Myklebust }
10451d6757fbSTrond Myklebust 
10461d6757fbSTrond Myklebust /*
1047a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1048a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1049a12802caSTrond Myklebust  */
1050a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
1051a12802caSTrond Myklebust {
1052a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1053a12802caSTrond Myklebust 		return 0;
10543516586aSAl Viro 	return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
1055a12802caSTrond Myklebust }
1056a12802caSTrond Myklebust 
1057a12802caSTrond Myklebust /*
10581d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10591d6757fbSTrond Myklebust  *
10601d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10611d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10621d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10631d6757fbSTrond Myklebust  *
10641d6757fbSTrond Myklebust  */
10651da177e4SLinus Torvalds static inline
10661da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
10691da177e4SLinus Torvalds 
107036d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10714e99a1ffSTrond Myklebust 		return 0;
10721da177e4SLinus Torvalds 	if (nd != NULL) {
10731da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
10741d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
10751da177e4SLinus Torvalds 			goto out_force;
10761da177e4SLinus Torvalds 		/* This is an open(2) */
10771d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
10784e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
10794e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
10804e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
10811da177e4SLinus Torvalds 			goto out_force;
10824f48af45STrond Myklebust 		return 0;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
10851da177e4SLinus Torvalds out_force:
10861da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds /*
10901da177e4SLinus Torvalds  * We judge how long we want to trust negative
10911da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10921da177e4SLinus Torvalds  *
10931da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10941da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10951da177e4SLinus Torvalds  */
10961da177e4SLinus Torvalds static inline
10971da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
10981da177e4SLinus Torvalds 		       struct nameidata *nd)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
11011d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
11021da177e4SLinus Torvalds 		return 0;
11034eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
11044eec952eSTrond Myklebust 		return 1;
11051da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds /*
11091da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
11101da177e4SLinus Torvalds  * and we should check whether we can really trust that
11111da177e4SLinus Torvalds  * lookup.
11121da177e4SLinus Torvalds  *
11131da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
11141da177e4SLinus Torvalds  * we have an inode!
11151da177e4SLinus Torvalds  *
11161da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
11171da177e4SLinus Torvalds  * cached dentry and do a new lookup.
11181da177e4SLinus Torvalds  */
11191da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
11201da177e4SLinus Torvalds {
11211da177e4SLinus Torvalds 	struct inode *dir;
11221da177e4SLinus Torvalds 	struct inode *inode;
11231da177e4SLinus Torvalds 	struct dentry *parent;
1124e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1125e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
11261da177e4SLinus Torvalds 	int error;
11271da177e4SLinus Torvalds 
112834286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
112934286d66SNick Piggin 		return -ECHILD;
113034286d66SNick Piggin 
11311da177e4SLinus Torvalds 	parent = dget_parent(dentry);
11321da177e4SLinus Torvalds 	dir = parent->d_inode;
113391d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
11341da177e4SLinus Torvalds 	inode = dentry->d_inode;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	if (!inode) {
11371da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
11381da177e4SLinus Torvalds 			goto out_bad;
1139d69ee9b8STrond Myklebust 		goto out_valid_noent;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
11431e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
11443110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
11451e7cb3dcSChuck Lever 				dentry->d_name.name);
11461da177e4SLinus Torvalds 		goto out_bad;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
114915860ab1STrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
115015860ab1STrond Myklebust 		goto out_set_verifier;
115115860ab1STrond Myklebust 
11521da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1153a12802caSTrond Myklebust 	if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
11541da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
11551da177e4SLinus Torvalds 			goto out_zap_parent;
11561da177e4SLinus Torvalds 		goto out_valid;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11601da177e4SLinus Torvalds 		goto out_bad;
11611da177e4SLinus Torvalds 
1162e1fb4d05STrond Myklebust 	error = -ENOMEM;
1163e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1164e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1165e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1166e1fb4d05STrond Myklebust 		goto out_error;
1167e1fb4d05STrond Myklebust 
116880a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
11691da177e4SLinus Torvalds 	if (error)
11701da177e4SLinus Torvalds 		goto out_bad;
1171e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11721da177e4SLinus Torvalds 		goto out_bad;
1173e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11741da177e4SLinus Torvalds 		goto out_bad;
11751da177e4SLinus Torvalds 
1176e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1177e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
117815860ab1STrond Myklebust out_set_verifier:
1179cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11801da177e4SLinus Torvalds  out_valid:
1181d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1182d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1183d69ee9b8STrond Myklebust  out_valid_noent:
11841da177e4SLinus Torvalds 	dput(parent);
11851e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
11863110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11871e7cb3dcSChuck Lever 			dentry->d_name.name);
11881da177e4SLinus Torvalds 	return 1;
11891da177e4SLinus Torvalds out_zap_parent:
11901da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11911da177e4SLinus Torvalds  out_bad:
1192a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11931da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11941da177e4SLinus Torvalds 		/* Purge readdir caches. */
11951da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11961da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11971da177e4SLinus Torvalds 		if (have_submounts(dentry))
11981da177e4SLinus Torvalds 			goto out_valid;
1199d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1200d9e80b7dSAl Viro 			goto out_valid;
12011da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 	d_drop(dentry);
1204e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1205e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
12061da177e4SLinus Torvalds 	dput(parent);
12071e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
12083110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
12091e7cb3dcSChuck Lever 			dentry->d_name.name);
12101da177e4SLinus Torvalds 	return 0;
1211e1fb4d05STrond Myklebust out_error:
1212e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1213e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1214e1fb4d05STrond Myklebust 	dput(parent);
1215e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1216e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1217e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1218e1fb4d05STrond Myklebust 	return error;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds /*
12221da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
12231da177e4SLinus Torvalds  */
1224fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
12251da177e4SLinus Torvalds {
12261da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
12271da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
12281da177e4SLinus Torvalds 		dentry->d_flags);
12291da177e4SLinus Torvalds 
123077f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
123177f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
123277f11192STrond Myklebust 		return 1;
123377f11192STrond Myklebust 
12341da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12351da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12361da177e4SLinus Torvalds 		return 1;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12391da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12401da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12411da177e4SLinus Torvalds 		return 1;
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 	return 0;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
12471b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12481b83d707STrond Myklebust {
12491b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12501b83d707STrond Myklebust 	if (inode->i_nlink > 0)
12511b83d707STrond Myklebust 		drop_nlink(inode);
12521b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12531b83d707STrond Myklebust }
12541b83d707STrond Myklebust 
12551da177e4SLinus Torvalds /*
12561da177e4SLinus Torvalds  * Called when the dentry loses inode.
12571da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12581da177e4SLinus Torvalds  */
12591da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12601da177e4SLinus Torvalds {
126183672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
126283672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
126383672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
126483672d39SNeil Brown 
12651da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12669a53c3a7SDave Hansen 		drop_nlink(inode);
1267e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12681da177e4SLinus Torvalds 	}
12691da177e4SLinus Torvalds 	iput(inode);
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds 
1272b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1273b1942c5fSAl Viro {
1274b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1275b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1276b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1277b1942c5fSAl Viro 			WARN_ON(1);
1278b1942c5fSAl Viro 		else
1279b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1280b1942c5fSAl Viro 	}
1281b1942c5fSAl Viro }
1282b1942c5fSAl Viro 
1283f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12841da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
12851da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12861da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
128736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1288b1942c5fSAl Viro 	.d_release	= nfs_d_release,
12891da177e4SLinus Torvalds };
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
12921da177e4SLinus Torvalds {
12931da177e4SLinus Torvalds 	struct dentry *res;
1294565277f6STrond Myklebust 	struct dentry *parent;
12951da177e4SLinus Torvalds 	struct inode *inode = NULL;
1296e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1297e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12981da177e4SLinus Torvalds 	int error;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
13011da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
130291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
13051da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
13061da177e4SLinus Torvalds 		goto out;
13071da177e4SLinus Torvalds 
1308fd684071STrond Myklebust 	/*
1309fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1310fd684071STrond Myklebust 	 * but don't hash the dentry.
1311fd684071STrond Myklebust 	 */
1312fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
1313fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1314fd684071STrond Myklebust 		res = NULL;
1315fc0f684cSTrond Myklebust 		goto out;
1316fd684071STrond Myklebust 	}
13171da177e4SLinus Torvalds 
1318e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1319e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1320e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1321e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1322e1fb4d05STrond Myklebust 		goto out;
1323e1fb4d05STrond Myklebust 
1324565277f6STrond Myklebust 	parent = dentry->d_parent;
1325565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1326565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
132780a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
13281da177e4SLinus Torvalds 	if (error == -ENOENT)
13291da177e4SLinus Torvalds 		goto no_entry;
13301da177e4SLinus Torvalds 	if (error < 0) {
13311da177e4SLinus Torvalds 		res = ERR_PTR(error);
1332565277f6STrond Myklebust 		goto out_unblock_sillyrename;
13331da177e4SLinus Torvalds 	}
1334e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1335bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
133603f28e3aSTrond Myklebust 	if (IS_ERR(res))
1337565277f6STrond Myklebust 		goto out_unblock_sillyrename;
133854ceac45SDavid Howells 
1339d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1340d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1341d69ee9b8STrond Myklebust 
13421da177e4SLinus Torvalds no_entry:
134354ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13449eaef27bSTrond Myklebust 	if (res != NULL) {
13459eaef27bSTrond Myklebust 		if (IS_ERR(res))
1346565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13471da177e4SLinus Torvalds 		dentry = res;
13489eaef27bSTrond Myklebust 	}
13491da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1350565277f6STrond Myklebust out_unblock_sillyrename:
1351565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
13521da177e4SLinus Torvalds out:
1353e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1354e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13551da177e4SLinus Torvalds 	return res;
13561da177e4SLinus Torvalds }
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
13590ef97dcfSMiklos Szeredi static int nfs4_lookup_revalidate(struct dentry *, struct nameidata *);
13601da177e4SLinus Torvalds 
1361f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13620ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13631da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13641da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
136536d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1366b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13671da177e4SLinus Torvalds };
13681da177e4SLinus Torvalds 
13698a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13708a5e929dSAl Viro {
13718a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13728a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
13738a5e929dSAl Viro 		res |= FMODE_READ;
13748a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
13758a5e929dSAl Viro 		res |= FMODE_WRITE;
13768a5e929dSAl Viro 	return res;
13778a5e929dSAl Viro }
13788a5e929dSAl Viro 
137951141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1380cd9a1c0eSTrond Myklebust {
13815ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1382cd9a1c0eSTrond Myklebust }
1383cd9a1c0eSTrond Myklebust 
1384cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1385cd9a1c0eSTrond Myklebust {
1386cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1387cd9a1c0eSTrond Myklebust 	return 0;
1388cd9a1c0eSTrond Myklebust }
1389cd9a1c0eSTrond Myklebust 
1390d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13910dd2b474SMiklos Szeredi 			   struct dentry *dentry,
139230d90494SAl Viro 			   struct file *file, unsigned open_flags,
139347237687SAl Viro 			   int *opened)
1394cd9a1c0eSTrond Myklebust {
13950dd2b474SMiklos Szeredi 	int err;
13960dd2b474SMiklos Szeredi 
13970dd2b474SMiklos Szeredi 	if (ctx->dentry != dentry) {
13980dd2b474SMiklos Szeredi 		dput(ctx->dentry);
13990dd2b474SMiklos Szeredi 		ctx->dentry = dget(dentry);
14000dd2b474SMiklos Szeredi 	}
1401cd9a1c0eSTrond Myklebust 
1402cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1403cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
14040dd2b474SMiklos Szeredi 		err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1405d9585277SAl Viro 		if (err < 0)
1406cd9a1c0eSTrond Myklebust 			goto out;
1407cd9a1c0eSTrond Myklebust 	}
14080dd2b474SMiklos Szeredi 
140930d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
141030d90494SAl Viro 	if (err)
1411d9585277SAl Viro 		goto out;
141230d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
14130dd2b474SMiklos Szeredi 
1414cd9a1c0eSTrond Myklebust out:
1415cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1416d9585277SAl Viro 	return err;
1417cd9a1c0eSTrond Myklebust }
1418cd9a1c0eSTrond Myklebust 
1419d9585277SAl Viro static int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
142030d90494SAl Viro 			    struct file *file, unsigned open_flags,
142147237687SAl Viro 			    umode_t mode, int *opened)
14221da177e4SLinus Torvalds {
1423cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
14240dd2b474SMiklos Szeredi 	struct dentry *res;
14250dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1426f46e0bd3STrond Myklebust 	struct inode *inode;
1427898f635cSTrond Myklebust 	int err;
14281da177e4SLinus Torvalds 
14290dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14300dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
14310dd2b474SMiklos Szeredi 
14320dd2b474SMiklos Szeredi 	dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
14331e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
14341e7cb3dcSChuck Lever 
14350dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14360dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14370dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14380dd2b474SMiklos Szeredi 			/*
14390dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14400dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14410dd2b474SMiklos Szeredi 			 * again
14420dd2b474SMiklos Szeredi 			 */
1443d9585277SAl Viro 			return -ENOENT;
14440dd2b474SMiklos Szeredi 		}
14451da177e4SLinus Torvalds 		goto no_open;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14480dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1449d9585277SAl Viro 		return -ENAMETOOLONG;
14501da177e4SLinus Torvalds 
14510dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1452536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14530dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14540dd2b474SMiklos Szeredi 	}
1455536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1456536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1457536e43d1STrond Myklebust 		attr.ia_size = 0;
1458cd9a1c0eSTrond Myklebust 	}
1459cd9a1c0eSTrond Myklebust 
14600dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14610dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14620dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1463d9585277SAl Viro 		goto out;
14640dd2b474SMiklos Szeredi 
1465f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14662b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
14670dd2b474SMiklos Szeredi 	d_drop(dentry);
1468f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1469f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1470cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
14710dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14720dd2b474SMiklos Szeredi 		switch (err) {
14731da177e4SLinus Torvalds 		case -ENOENT:
1474f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
14750dd2b474SMiklos Szeredi 			break;
14761788ea6eSJeff Layton 		case -EISDIR:
14776f926b5bSTrond Myklebust 		case -ENOTDIR:
14786f926b5bSTrond Myklebust 			goto no_open;
14791da177e4SLinus Torvalds 		case -ELOOP:
14800dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
14811da177e4SLinus Torvalds 				goto no_open;
14820dd2b474SMiklos Szeredi 			break;
14831da177e4SLinus Torvalds 			/* case -EINVAL: */
14841da177e4SLinus Torvalds 		default:
14850dd2b474SMiklos Szeredi 			break;
14861da177e4SLinus Torvalds 		}
1487d9585277SAl Viro 		goto out;
1488cd9a1c0eSTrond Myklebust 	}
1489f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1490898f635cSTrond Myklebust 	if (res != NULL)
14910dd2b474SMiklos Szeredi 		dentry = res;
14920dd2b474SMiklos Szeredi 
14930dd2b474SMiklos Szeredi 	nfs_unblock_sillyrename(dentry->d_parent);
1494f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14950dd2b474SMiklos Szeredi 
149630d90494SAl Viro 	err = nfs_finish_open(ctx, dentry, file, open_flags, opened);
14970dd2b474SMiklos Szeredi 
14980dd2b474SMiklos Szeredi 	dput(res);
1499d9585277SAl Viro out:
1500d9585277SAl Viro 	return err;
15010dd2b474SMiklos Szeredi 
15021da177e4SLinus Torvalds no_open:
15030dd2b474SMiklos Szeredi 	res = nfs_lookup(dir, dentry, NULL);
15040dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
15050dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1506d9585277SAl Viro 		goto out;
15070dd2b474SMiklos Szeredi 
150830d90494SAl Viro 	finish_no_open(file, res);
1509d9585277SAl Viro 	return 1;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15120ef97dcfSMiklos Szeredi static int nfs4_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
15131da177e4SLinus Torvalds {
15141da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1515657e94b6SNick Piggin 	struct inode *inode;
15161da177e4SLinus Torvalds 	struct inode *dir;
151750de348cSMiklos Szeredi 	int ret = 0;
15181da177e4SLinus Torvalds 
1519657e94b6SNick Piggin 	if (nd->flags & LOOKUP_RCU)
1520657e94b6SNick Piggin 		return -ECHILD;
1521657e94b6SNick Piggin 
1522eda72afbSMiklos Szeredi 	if (!(nd->flags & LOOKUP_OPEN) || (nd->flags & LOOKUP_DIRECTORY))
1523eda72afbSMiklos Szeredi 		goto no_open;
1524eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15255584c306STrond Myklebust 		goto no_open;
15262b484297STrond Myklebust 
1527eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
15281da177e4SLinus Torvalds 	parent = dget_parent(dentry);
15291da177e4SLinus Torvalds 	dir = parent->d_inode;
15302b484297STrond Myklebust 
15311da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15321da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15331da177e4SLinus Torvalds 	 */
1534216d5d06STrond Myklebust 	if (inode == NULL) {
1535216d5d06STrond Myklebust 		if (!nfs_neg_need_reval(dir, dentry, nd))
1536216d5d06STrond Myklebust 			ret = 1;
15371da177e4SLinus Torvalds 		goto out;
1538216d5d06STrond Myklebust 	}
1539216d5d06STrond Myklebust 
15401da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
15411da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15425584c306STrond Myklebust 		goto no_open_dput;
15431da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
154450de348cSMiklos Szeredi 	if (nd && nd->flags & LOOKUP_EXCL)
15455584c306STrond Myklebust 		goto no_open_dput;
15461da177e4SLinus Torvalds 
15470ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1548898f635cSTrond Myklebust 	ret = 1;
15490ef97dcfSMiklos Szeredi 
15501da177e4SLinus Torvalds out:
15511da177e4SLinus Torvalds 	dput(parent);
15521da177e4SLinus Torvalds 	return ret;
1553535918f1STrond Myklebust 
15545584c306STrond Myklebust no_open_dput:
15551da177e4SLinus Torvalds 	dput(parent);
15565584c306STrond Myklebust no_open:
15571da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
15581da177e4SLinus Torvalds }
1559c0204fd2STrond Myklebust 
15601da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds /*
15631da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15641da177e4SLinus Torvalds  */
15651da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15661da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
15671da177e4SLinus Torvalds {
1568fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1569fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15701da177e4SLinus Torvalds 	struct inode *inode;
15711da177e4SLinus Torvalds 	int error = -EACCES;
15721da177e4SLinus Torvalds 
1573fab728e1STrond Myklebust 	d_drop(dentry);
1574fab728e1STrond Myklebust 
15751da177e4SLinus Torvalds 	/* We may have been initialized further down */
15761da177e4SLinus Torvalds 	if (dentry->d_inode)
1577fab728e1STrond Myklebust 		goto out;
15781da177e4SLinus Torvalds 	if (fhandle->size == 0) {
157980a16b21SBryan Schumaker 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
15801da177e4SLinus Torvalds 		if (error)
1581fab728e1STrond Myklebust 			goto out_error;
15821da177e4SLinus Torvalds 	}
15835724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15841da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15851da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15868fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
15871da177e4SLinus Torvalds 		if (error < 0)
1588fab728e1STrond Myklebust 			goto out_error;
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
159103f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
159203f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1593fab728e1STrond Myklebust 		goto out_error;
1594fab728e1STrond Myklebust 	d_add(dentry, inode);
1595fab728e1STrond Myklebust out:
1596fab728e1STrond Myklebust 	dput(parent);
15971da177e4SLinus Torvalds 	return 0;
1598fab728e1STrond Myklebust out_error:
1599fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1600fab728e1STrond Myklebust 	dput(parent);
1601fab728e1STrond Myklebust 	return error;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds /*
16051da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
16061da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
16071da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
16081da177e4SLinus Torvalds  * reply path made it appear to have failed.
16091da177e4SLinus Torvalds  */
16104acdaf27SAl Viro static int nfs_create(struct inode *dir, struct dentry *dentry,
16114acdaf27SAl Viro 		umode_t mode, struct nameidata *nd)
16121da177e4SLinus Torvalds {
16131da177e4SLinus Torvalds 	struct iattr attr;
16141da177e4SLinus Torvalds 	int error;
16158a5e929dSAl Viro 	int open_flags = O_CREAT|O_EXCL;
16161da177e4SLinus Torvalds 
16171e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
16181e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	attr.ia_mode = mode;
16211da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16221da177e4SLinus Torvalds 
162350de348cSMiklos Szeredi 	if (nd && !(nd->flags & LOOKUP_EXCL))
162450de348cSMiklos Szeredi 		open_flags = O_CREAT;
16258a0eebf6STrond Myklebust 
16268867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16271da177e4SLinus Torvalds 	if (error != 0)
16281da177e4SLinus Torvalds 		goto out_err;
16291da177e4SLinus Torvalds 	return 0;
16301da177e4SLinus Torvalds out_err:
16311da177e4SLinus Torvalds 	d_drop(dentry);
16321da177e4SLinus Torvalds 	return error;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds /*
16361da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16371da177e4SLinus Torvalds  */
16381da177e4SLinus Torvalds static int
16391a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 	struct iattr attr;
16421da177e4SLinus Torvalds 	int status;
16431da177e4SLinus Torvalds 
16441e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
16451e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16481da177e4SLinus Torvalds 		return -EINVAL;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	attr.ia_mode = mode;
16511da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16541da177e4SLinus Torvalds 	if (status != 0)
16551da177e4SLinus Torvalds 		goto out_err;
16561da177e4SLinus Torvalds 	return 0;
16571da177e4SLinus Torvalds out_err:
16581da177e4SLinus Torvalds 	d_drop(dentry);
16591da177e4SLinus Torvalds 	return status;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds /*
16631da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16641da177e4SLinus Torvalds  */
166518bb1db3SAl Viro static int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	struct iattr attr;
16681da177e4SLinus Torvalds 	int error;
16691da177e4SLinus Torvalds 
16701e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16711e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16741da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16771da177e4SLinus Torvalds 	if (error != 0)
16781da177e4SLinus Torvalds 		goto out_err;
16791da177e4SLinus Torvalds 	return 0;
16801da177e4SLinus Torvalds out_err:
16811da177e4SLinus Torvalds 	d_drop(dentry);
16821da177e4SLinus Torvalds 	return error;
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
1685d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1686d45b9d8bSTrond Myklebust {
1687d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1688d45b9d8bSTrond Myklebust 		d_delete(dentry);
1689d45b9d8bSTrond Myklebust }
1690d45b9d8bSTrond Myklebust 
16911da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	int error;
16941da177e4SLinus Torvalds 
16951e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16961e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16991da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
17001da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1701ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1702d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1703d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	return error;
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds /*
17091da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17101da177e4SLinus Torvalds  * and after checking that the file has only one user.
17111da177e4SLinus Torvalds  *
17121da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17131da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17141da177e4SLinus Torvalds  */
17151da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17181da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17191da177e4SLinus Torvalds 	int error = -EBUSY;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
17221da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
17251da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17261da177e4SLinus Torvalds 		error = 0;
17271da177e4SLinus Torvalds 		goto out;
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	if (inode != NULL) {
1731cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
17321da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17331da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
17341da177e4SLinus Torvalds 		if (error == 0)
17351b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17365ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
17371da177e4SLinus Torvalds 	} else
17381da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1739d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1740d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17411da177e4SLinus Torvalds out:
17421da177e4SLinus Torvalds 	return error;
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17461da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17471da177e4SLinus Torvalds  *
17481da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17491da177e4SLinus Torvalds  */
17501da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
17511da177e4SLinus Torvalds {
17521da177e4SLinus Torvalds 	int error;
17531da177e4SLinus Torvalds 	int need_rehash = 0;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17561da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
1759b7ab39f6SNick Piggin 	if (dentry->d_count > 1) {
17601da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1761ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1762ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17631da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17641da177e4SLinus Torvalds 		return error;
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17671da177e4SLinus Torvalds 		__d_drop(dentry);
17681da177e4SLinus Torvalds 		need_rehash = 1;
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17711da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1772d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17731da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17741da177e4SLinus Torvalds 	} else if (need_rehash)
17751da177e4SLinus Torvalds 		d_rehash(dentry);
17761da177e4SLinus Torvalds 	return error;
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
1779873101b3SChuck Lever /*
1780873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1781873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1782873101b3SChuck Lever  * using prepare_write/commit_write.
1783873101b3SChuck Lever  *
1784873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1785873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1786873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1787873101b3SChuck Lever  * symlink request has completed on the server.
1788873101b3SChuck Lever  *
1789873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1790873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1791873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1792873101b3SChuck Lever  * and move the raw page into its mapping.
1793873101b3SChuck Lever  */
1794873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17951da177e4SLinus Torvalds {
1796873101b3SChuck Lever 	struct pagevec lru_pvec;
1797873101b3SChuck Lever 	struct page *page;
1798873101b3SChuck Lever 	char *kaddr;
17991da177e4SLinus Torvalds 	struct iattr attr;
1800873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
18011da177e4SLinus Torvalds 	int error;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
18041da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
18051da177e4SLinus Torvalds 
1806873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1807873101b3SChuck Lever 		return -ENAMETOOLONG;
18081da177e4SLinus Torvalds 
1809873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1810873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18111da177e4SLinus Torvalds 
181283d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
181376566991STrond Myklebust 	if (!page)
1814873101b3SChuck Lever 		return -ENOMEM;
1815873101b3SChuck Lever 
18162b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1817873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1818873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1819873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
18202b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1821873101b3SChuck Lever 
182294a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1823873101b3SChuck Lever 	if (error != 0) {
1824873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1825873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1826873101b3SChuck Lever 			dentry->d_name.name, symname, error);
18271da177e4SLinus Torvalds 		d_drop(dentry);
1828873101b3SChuck Lever 		__free_page(page);
18291da177e4SLinus Torvalds 		return error;
18301da177e4SLinus Torvalds 	}
18311da177e4SLinus Torvalds 
1832873101b3SChuck Lever 	/*
1833873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1834873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1835873101b3SChuck Lever 	 */
1836873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1837873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1838873101b3SChuck Lever 							GFP_KERNEL)) {
183939cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
18404f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1841873101b3SChuck Lever 		SetPageUptodate(page);
1842873101b3SChuck Lever 		unlock_page(page);
1843873101b3SChuck Lever 	} else
1844873101b3SChuck Lever 		__free_page(page);
1845873101b3SChuck Lever 
1846873101b3SChuck Lever 	return 0;
1847873101b3SChuck Lever }
1848873101b3SChuck Lever 
18491da177e4SLinus Torvalds static int
18501da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18511da177e4SLinus Torvalds {
18521da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18531da177e4SLinus Torvalds 	int error;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18561da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18571da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18581da177e4SLinus Torvalds 
18599a3936aaSTrond Myklebust 	nfs_inode_return_delegation(inode);
18609a3936aaSTrond Myklebust 
18619697d234STrond Myklebust 	d_drop(dentry);
18621da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1863cf809556STrond Myklebust 	if (error == 0) {
18647de9c6eeSAl Viro 		ihold(inode);
18659697d234STrond Myklebust 		d_add(dentry, inode);
1866cf809556STrond Myklebust 	}
18671da177e4SLinus Torvalds 	return error;
18681da177e4SLinus Torvalds }
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds /*
18711da177e4SLinus Torvalds  * RENAME
18721da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18731da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18741da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18751da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18761da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18771da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  * FIXED.
18801da177e4SLinus Torvalds  *
18811da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18821da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18831da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18841da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18851da177e4SLinus Torvalds  * using the inode layer
18861da177e4SLinus Torvalds  *
18871da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18881da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18891da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18901da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18911da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18921da177e4SLinus Torvalds  * the rename.
18931da177e4SLinus Torvalds  */
18941da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18951da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18981da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18991da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
19001da177e4SLinus Torvalds 	int error = -EBUSY;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
19031da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
19041da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1905b7ab39f6SNick Piggin 		 new_dentry->d_count);
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	/*
190828f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
190928f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
191028f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
191128f79a1aSMiklos Szeredi 	 * the new target.
19121da177e4SLinus Torvalds 	 */
191327226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
191427226104SMiklos Szeredi 		/*
191527226104SMiklos Szeredi 		 * To prevent any new references to the target during the
191627226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
191727226104SMiklos Szeredi 		 */
191827226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
191927226104SMiklos Szeredi 			d_drop(new_dentry);
192027226104SMiklos Szeredi 			rehash = new_dentry;
192127226104SMiklos Szeredi 		}
192227226104SMiklos Szeredi 
1923b7ab39f6SNick Piggin 		if (new_dentry->d_count > 2) {
19241da177e4SLinus Torvalds 			int err;
192527226104SMiklos Szeredi 
19261da177e4SLinus Torvalds 			/* copy the target dentry's name */
19271da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19281da177e4SLinus Torvalds 					 &new_dentry->d_name);
19291da177e4SLinus Torvalds 			if (!dentry)
19301da177e4SLinus Torvalds 				goto out;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19331da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
193424e93025SMiklos Szeredi 			if (err)
19351da177e4SLinus Torvalds 				goto out;
193624e93025SMiklos Szeredi 
193724e93025SMiklos Szeredi 			new_dentry = dentry;
193856335936SOGAWA Hirofumi 			rehash = NULL;
193924e93025SMiklos Szeredi 			new_inode = NULL;
1940b1e4adf4STrond Myklebust 		}
194127226104SMiklos Szeredi 	}
19421da177e4SLinus Torvalds 
1943cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
1944b1e4adf4STrond Myklebust 	if (new_inode != NULL)
194524174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19481da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19495ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19501da177e4SLinus Torvalds out:
19511da177e4SLinus Torvalds 	if (rehash)
19521da177e4SLinus Torvalds 		d_rehash(rehash);
19531da177e4SLinus Torvalds 	if (!error) {
1954b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1955b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19561da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19578fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19588fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1959d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1960d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	/* new dentry created? */
19631da177e4SLinus Torvalds 	if (dentry)
19641da177e4SLinus Torvalds 		dput(dentry);
19651da177e4SLinus Torvalds 	return error;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
1968cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1969cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1970cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1971cfcea3e8STrond Myklebust 
19721c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19731c3c07e9STrond Myklebust {
19741c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19751c3c07e9STrond Myklebust 	kfree(entry);
1976cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1977cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1978cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19791c3c07e9STrond Myklebust }
19801c3c07e9STrond Myklebust 
19811a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19821a81bb8aSTrond Myklebust {
19831a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19841a81bb8aSTrond Myklebust 
19851a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19861a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19871a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19881a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19891a81bb8aSTrond Myklebust 	}
19901a81bb8aSTrond Myklebust }
19911a81bb8aSTrond Myklebust 
19921495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink,
19931495f230SYing Han 			      struct shrink_control *sc)
1994979df72eSTrond Myklebust {
1995979df72eSTrond Myklebust 	LIST_HEAD(head);
1996aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1997979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
19981495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
19991495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
2000979df72eSTrond Myklebust 
200161d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
200261d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
20039c7e7e23STrond Myklebust 
2004a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2005aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2006979df72eSTrond Myklebust 		struct inode *inode;
2007979df72eSTrond Myklebust 
2008979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2009979df72eSTrond Myklebust 			break;
20109c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2011979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2012979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2013979df72eSTrond Myklebust 			goto remove_lru_entry;
2014979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2015979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2016979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2017979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
2018979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2019979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2020979df72eSTrond Myklebust 					&nfs_access_lru_list);
2021979df72eSTrond Myklebust 		else {
2022979df72eSTrond Myklebust remove_lru_entry:
2023979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
20249c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
2025979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
20269c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
2027979df72eSTrond Myklebust 		}
202859844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2029979df72eSTrond Myklebust 	}
2030979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20311a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
2032979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2033979df72eSTrond Myklebust }
2034979df72eSTrond Myklebust 
20351a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20361c3c07e9STrond Myklebust {
20371c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20381a81bb8aSTrond Myklebust 	struct rb_node *n;
20391c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20401c3c07e9STrond Myklebust 
20411c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20421c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20431c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20441c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20451a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20461c3c07e9STrond Myklebust 	}
20471c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20481c3c07e9STrond Myklebust }
20491c3c07e9STrond Myklebust 
20501c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20511c3c07e9STrond Myklebust {
20521a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20531a81bb8aSTrond Myklebust 
20541a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20551a81bb8aSTrond Myklebust 		return;
2056cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2057cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20581a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2059cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2060cfcea3e8STrond Myklebust 
20611c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20621a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20631a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20641a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20651a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20661c3c07e9STrond Myklebust }
20671c3c07e9STrond Myklebust 
20681c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20691c3c07e9STrond Myklebust {
20701c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20711c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20721c3c07e9STrond Myklebust 
20731c3c07e9STrond Myklebust 	while (n != NULL) {
20741c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20751c3c07e9STrond Myklebust 
20761c3c07e9STrond Myklebust 		if (cred < entry->cred)
20771c3c07e9STrond Myklebust 			n = n->rb_left;
20781c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20791c3c07e9STrond Myklebust 			n = n->rb_right;
20801c3c07e9STrond Myklebust 		else
20811c3c07e9STrond Myklebust 			return entry;
20821c3c07e9STrond Myklebust 	}
20831c3c07e9STrond Myklebust 	return NULL;
20841c3c07e9STrond Myklebust }
20851c3c07e9STrond Myklebust 
2086af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20871da177e4SLinus Torvalds {
208855296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
20891c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
20901c3c07e9STrond Myklebust 	int err = -ENOENT;
20911da177e4SLinus Torvalds 
20921c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20931c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
20941c3c07e9STrond Myklebust 		goto out_zap;
20951c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20961c3c07e9STrond Myklebust 	if (cache == NULL)
20971c3c07e9STrond Myklebust 		goto out;
2098b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
209964672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
21001c3c07e9STrond Myklebust 		goto out_stale;
21011c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
21021c3c07e9STrond Myklebust 	res->cred = cache->cred;
21031c3c07e9STrond Myklebust 	res->mask = cache->mask;
2104cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
21051c3c07e9STrond Myklebust 	err = 0;
21061c3c07e9STrond Myklebust out:
21071c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21081c3c07e9STrond Myklebust 	return err;
21091c3c07e9STrond Myklebust out_stale:
21101c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2111cfcea3e8STrond Myklebust 	list_del(&cache->lru);
21121c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21131c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
21141da177e4SLinus Torvalds 	return -ENOENT;
21151c3c07e9STrond Myklebust out_zap:
21161a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21171a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
21181c3c07e9STrond Myklebust 	return -ENOENT;
21191c3c07e9STrond Myklebust }
21201c3c07e9STrond Myklebust 
21211c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
21221c3c07e9STrond Myklebust {
2123cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2124cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21251c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
21261c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
21271c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21281c3c07e9STrond Myklebust 
21291c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21301c3c07e9STrond Myklebust 	while (*p != NULL) {
21311c3c07e9STrond Myklebust 		parent = *p;
21321c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21331c3c07e9STrond Myklebust 
21341c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21351c3c07e9STrond Myklebust 			p = &parent->rb_left;
21361c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21371c3c07e9STrond Myklebust 			p = &parent->rb_right;
21381c3c07e9STrond Myklebust 		else
21391c3c07e9STrond Myklebust 			goto found;
21401c3c07e9STrond Myklebust 	}
21411c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21421c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2143cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21441c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21451c3c07e9STrond Myklebust 	return;
21461c3c07e9STrond Myklebust found:
21471c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2148cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2149cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21501c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21511c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
2154af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21551da177e4SLinus Torvalds {
21561c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21571c3c07e9STrond Myklebust 	if (cache == NULL)
21581c3c07e9STrond Myklebust 		return;
21591c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21601da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21611c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21621da177e4SLinus Torvalds 	cache->mask = set->mask;
21631c3c07e9STrond Myklebust 
21641c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2165cfcea3e8STrond Myklebust 
2166cfcea3e8STrond Myklebust 	/* Update accounting */
2167cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2168cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2169cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2170cfcea3e8STrond Myklebust 
2171cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21721a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2173cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21741a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21751a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21761a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2177cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2178cfcea3e8STrond Myklebust 	}
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21821da177e4SLinus Torvalds {
21831da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21841da177e4SLinus Torvalds 	int status;
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21871da177e4SLinus Torvalds 	if (status == 0)
21881da177e4SLinus Torvalds 		goto out;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
21911da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
21921da177e4SLinus Torvalds 	cache.cred = cred;
21931da177e4SLinus Torvalds 	cache.jiffies = jiffies;
21941da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2195a71ee337SSuresh Jayaraman 	if (status != 0) {
2196a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2197a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2198a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2199a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2200a71ee337SSuresh Jayaraman 		}
22011da177e4SLinus Torvalds 		return status;
2202a71ee337SSuresh Jayaraman 	}
22031da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
22041da177e4SLinus Torvalds out:
2205e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22061da177e4SLinus Torvalds 		return 0;
22071da177e4SLinus Torvalds 	return -EACCES;
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
2210af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2211af22f94aSTrond Myklebust {
2212af22f94aSTrond Myklebust 	int mask = 0;
2213af22f94aSTrond Myklebust 
22148a5e929dSAl Viro 	if ((openflags & O_ACCMODE) != O_WRONLY)
2215af22f94aSTrond Myklebust 		mask |= MAY_READ;
22168a5e929dSAl Viro 	if ((openflags & O_ACCMODE) != O_RDONLY)
2217af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
22188a5e929dSAl Viro 	if (openflags & __FMODE_EXEC)
2219af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2220af22f94aSTrond Myklebust 	return mask;
2221af22f94aSTrond Myklebust }
2222af22f94aSTrond Myklebust 
2223af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2224af22f94aSTrond Myklebust {
2225af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2226af22f94aSTrond Myklebust }
2227af22f94aSTrond Myklebust 
222810556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
22291da177e4SLinus Torvalds {
22301da177e4SLinus Torvalds 	struct rpc_cred *cred;
22311da177e4SLinus Torvalds 	int res = 0;
22321da177e4SLinus Torvalds 
223310556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2234b74c79e9SNick Piggin 		return -ECHILD;
2235b74c79e9SNick Piggin 
223691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
223791d5b470SChuck Lever 
2238e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22391da177e4SLinus Torvalds 		goto out;
22401da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22419cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22421da177e4SLinus Torvalds 		goto force_lookup;
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22451da177e4SLinus Torvalds 		case S_IFLNK:
22461da177e4SLinus Torvalds 			goto out;
22471da177e4SLinus Torvalds 		case S_IFREG:
22481da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
22491da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
22507ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
22517ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
22521da177e4SLinus Torvalds 				goto out;
22531da177e4SLinus Torvalds 			break;
22541da177e4SLinus Torvalds 		case S_IFDIR:
22551da177e4SLinus Torvalds 			/*
22561da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22571da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22581da177e4SLinus Torvalds 			 */
22591da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22601da177e4SLinus Torvalds 				goto out;
22611da177e4SLinus Torvalds 	}
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds force_lookup:
22641da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22651da177e4SLinus Torvalds 		goto out_notsup;
22661da177e4SLinus Torvalds 
226798a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22681da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22691da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
22701da177e4SLinus Torvalds 		put_rpccred(cred);
22711da177e4SLinus Torvalds 	} else
22721da177e4SLinus Torvalds 		res = PTR_ERR(cred);
22731da177e4SLinus Torvalds out:
2274f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2275f696a365SMiklos Szeredi 		res = -EACCES;
2276f696a365SMiklos Szeredi 
22771e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
22781e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22791da177e4SLinus Torvalds 	return res;
22801da177e4SLinus Torvalds out_notsup:
22811da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22821da177e4SLinus Torvalds 	if (res == 0)
22832830ba7fSAl Viro 		res = generic_permission(inode, mask);
22841e7cb3dcSChuck Lever 	goto out;
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds /*
22881da177e4SLinus Torvalds  * Local variables:
22891da177e4SLinus Torvalds  *  version-control: t
22901da177e4SLinus Torvalds  *  kept-new-versions: 5
22911da177e4SLinus Torvalds  * End:
22921da177e4SLinus Torvalds  */
2293