xref: /openbmc/linux/fs/nfs/dir.c (revision 63519fbc)
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 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
301da177e4SLinus Torvalds #include <linux/nfs_fs.h>
311da177e4SLinus Torvalds #include <linux/nfs_mount.h>
321da177e4SLinus Torvalds #include <linux/pagemap.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
36a0b8cab3SMel Gorman #include <linux/swap.h>
37e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "delegation.h"
4291d5b470SChuck Lever #include "iostat.h"
434c30d56eSAdrian Bunk #include "internal.h"
44cd9a1c0eSTrond Myklebust #include "fscache.h"
451da177e4SLinus Torvalds 
46f4ce1299STrond Myklebust #include "nfstrace.h"
47f4ce1299STrond Myklebust 
481da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
51480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5223db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
54f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5511de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
561da177e4SLinus Torvalds 
574b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
58f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
591da177e4SLinus Torvalds 	.read		= generic_read_dir,
609ac3d3e8SAl Viro 	.iterate_shared	= nfs_readdir,
611da177e4SLinus Torvalds 	.open		= nfs_opendir,
62480c2006SBryan Schumaker 	.release	= nfs_closedir,
631da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
6611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6711de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
68d1bacf9eSBryan Schumaker };
69d1bacf9eSBryan Schumaker 
700c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
71480c2006SBryan Schumaker {
72311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
73480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
74480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
75480c2006SBryan Schumaker 	if (ctx != NULL) {
768ef2ce3eSBryan Schumaker 		ctx->duped = 0;
77311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
78480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
798ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
80480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
81311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
82311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
83311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
84480c2006SBryan Schumaker 		return ctx;
85480c2006SBryan Schumaker 	}
860c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
870c030806STrond Myklebust }
88480c2006SBryan Schumaker 
89311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
90480c2006SBryan Schumaker {
91311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
92311324adSTrond Myklebust 	list_del(&ctx->list);
93311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
94480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
95480c2006SBryan Schumaker 	kfree(ctx);
96480c2006SBryan Schumaker }
97480c2006SBryan Schumaker 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Open file
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds static int
1021da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1031da177e4SLinus Torvalds {
104480c2006SBryan Schumaker 	int res = 0;
105480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
106480c2006SBryan Schumaker 	struct rpc_cred *cred;
1071da177e4SLinus Torvalds 
1086de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
109cc0dd2d1SChuck Lever 
110cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1111e7cb3dcSChuck Lever 
112480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
113480c2006SBryan Schumaker 	if (IS_ERR(cred))
114480c2006SBryan Schumaker 		return PTR_ERR(cred);
1150c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
116480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
117480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
118480c2006SBryan Schumaker 		goto out;
119480c2006SBryan Schumaker 	}
120480c2006SBryan Schumaker 	filp->private_data = ctx;
121f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
122f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
123f5a73672SNeil Brown 		 * have been called, so we need to refresh the
124f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
125f5a73672SNeil Brown 		 */
126f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
127f5a73672SNeil Brown 	}
128480c2006SBryan Schumaker out:
129480c2006SBryan Schumaker 	put_rpccred(cred);
1301da177e4SLinus Torvalds 	return res;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133480c2006SBryan Schumaker static int
134480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
135480c2006SBryan Schumaker {
136a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
137480c2006SBryan Schumaker 	return 0;
138480c2006SBryan Schumaker }
139480c2006SBryan Schumaker 
140d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
141d1bacf9eSBryan Schumaker 	u64 cookie;
142d1bacf9eSBryan Schumaker 	u64 ino;
143d1bacf9eSBryan Schumaker 	struct qstr string;
1440b26a0bfSTrond Myklebust 	unsigned char d_type;
145d1bacf9eSBryan Schumaker };
146d1bacf9eSBryan Schumaker 
147d1bacf9eSBryan Schumaker struct nfs_cache_array {
1489ac3d3e8SAl Viro 	atomic_t refcount;
14988b8e133SChuck Lever 	int size;
150d1bacf9eSBryan Schumaker 	int eof_index;
151d1bacf9eSBryan Schumaker 	u64 last_cookie;
152d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
153d1bacf9eSBryan Schumaker };
154d1bacf9eSBryan Schumaker 
155573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1561da177e4SLinus Torvalds typedef struct {
1571da177e4SLinus Torvalds 	struct file	*file;
1581da177e4SLinus Torvalds 	struct page	*page;
15923db8620SAl Viro 	struct dir_context *ctx;
1601da177e4SLinus Torvalds 	unsigned long	page_index;
161f0dd2136STrond Myklebust 	u64		*dir_cookie;
1620aded708STrond Myklebust 	u64		last_cookie;
163f0dd2136STrond Myklebust 	loff_t		current_index;
1641da177e4SLinus Torvalds 	decode_dirent_t	decode;
165d1bacf9eSBryan Schumaker 
1661f4eab7eSNeil Brown 	unsigned long	timestamp;
1674704f0e2STrond Myklebust 	unsigned long	gencount;
168d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
169d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
170d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1711da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1721da177e4SLinus Torvalds 
173d1bacf9eSBryan Schumaker /*
174d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1751da177e4SLinus Torvalds  */
1761da177e4SLinus Torvalds static
177d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1781da177e4SLinus Torvalds {
1798cd51a0cSTrond Myklebust 	void *ptr;
180d1bacf9eSBryan Schumaker 	if (page == NULL)
181d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1828cd51a0cSTrond Myklebust 	ptr = kmap(page);
1838cd51a0cSTrond Myklebust 	if (ptr == NULL)
1848cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1858cd51a0cSTrond Myklebust 	return ptr;
186d1bacf9eSBryan Schumaker }
187d1bacf9eSBryan Schumaker 
188d1bacf9eSBryan Schumaker static
189d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
190d1bacf9eSBryan Schumaker {
191d1bacf9eSBryan Schumaker 	kunmap(page);
192d1bacf9eSBryan Schumaker }
193d1bacf9eSBryan Schumaker 
194d1bacf9eSBryan Schumaker /*
195d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
196d1bacf9eSBryan Schumaker  */
197d1bacf9eSBryan Schumaker static
19811de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
199d1bacf9eSBryan Schumaker {
20011de3b11STrond Myklebust 	struct nfs_cache_array *array;
201d1bacf9eSBryan Schumaker 	int i;
2028cd51a0cSTrond Myklebust 
2032b86ce2dSCong Wang 	array = kmap_atomic(page);
2049ac3d3e8SAl Viro 	if (atomic_dec_and_test(&array->refcount))
205d1bacf9eSBryan Schumaker 		for (i = 0; i < array->size; i++)
206d1bacf9eSBryan Schumaker 			kfree(array->array[i].string.name);
2072b86ce2dSCong Wang 	kunmap_atomic(array);
208d1bacf9eSBryan Schumaker }
209d1bacf9eSBryan Schumaker 
2109ac3d3e8SAl Viro static bool grab_page(struct page *page)
2119ac3d3e8SAl Viro {
2129ac3d3e8SAl Viro 	struct nfs_cache_array *array = kmap_atomic(page);
2139ac3d3e8SAl Viro 	bool res = atomic_inc_not_zero(&array->refcount);
2149ac3d3e8SAl Viro 	kunmap_atomic(array);
2159ac3d3e8SAl Viro 	return res;
2169ac3d3e8SAl Viro }
2179ac3d3e8SAl Viro 
218d1bacf9eSBryan Schumaker /*
219d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
220d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
221d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
222d1bacf9eSBryan Schumaker  */
223d1bacf9eSBryan Schumaker static
2244a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
225d1bacf9eSBryan Schumaker {
226d1bacf9eSBryan Schumaker 	string->len = len;
227d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2284a201d6eSTrond Myklebust 	if (string->name == NULL)
2294a201d6eSTrond Myklebust 		return -ENOMEM;
23004e4bd1cSCatalin Marinas 	/*
23104e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
23204e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
23304e4bd1cSCatalin Marinas 	 */
23404e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2358387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2364a201d6eSTrond Myklebust 	return 0;
237d1bacf9eSBryan Schumaker }
238d1bacf9eSBryan Schumaker 
239d1bacf9eSBryan Schumaker static
240d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
241d1bacf9eSBryan Schumaker {
242d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2434a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2444a201d6eSTrond Myklebust 	int ret;
2454a201d6eSTrond Myklebust 
246d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
247d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
248d1bacf9eSBryan Schumaker 
2494a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2503020093fSTrond Myklebust 
2513020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2523020093fSTrond Myklebust 	ret = -ENOSPC;
2533020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2543020093fSTrond Myklebust 		goto out;
2553020093fSTrond Myklebust 
2564a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2574a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2580b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2594a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2604a201d6eSTrond Myklebust 	if (ret)
2614a201d6eSTrond Myklebust 		goto out;
262d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2638cd51a0cSTrond Myklebust 	array->size++;
26447c716cbSTrond Myklebust 	if (entry->eof != 0)
265d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2664a201d6eSTrond Myklebust out:
267d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2684a201d6eSTrond Myklebust 	return ret;
269d1bacf9eSBryan Schumaker }
270d1bacf9eSBryan Schumaker 
271d1bacf9eSBryan Schumaker static
272d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
273d1bacf9eSBryan Schumaker {
27423db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
275d1bacf9eSBryan Schumaker 	unsigned int index;
276d1bacf9eSBryan Schumaker 
277d1bacf9eSBryan Schumaker 	if (diff < 0)
278d1bacf9eSBryan Schumaker 		goto out_eof;
279d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2808cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
281d1bacf9eSBryan Schumaker 			goto out_eof;
282d1bacf9eSBryan Schumaker 		return -EAGAIN;
283d1bacf9eSBryan Schumaker 	}
284d1bacf9eSBryan Schumaker 
285d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
286d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
287d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
288d1bacf9eSBryan Schumaker 	return 0;
289d1bacf9eSBryan Schumaker out_eof:
290d1bacf9eSBryan Schumaker 	desc->eof = 1;
291d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
292d1bacf9eSBryan Schumaker }
293d1bacf9eSBryan Schumaker 
2944db72b40SJeff Layton static bool
2954db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2964db72b40SJeff Layton {
2974db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2984db72b40SJeff Layton 		return false;
2994db72b40SJeff Layton 	smp_rmb();
3004db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
3014db72b40SJeff Layton }
3024db72b40SJeff Layton 
303d1bacf9eSBryan Schumaker static
304d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
305d1bacf9eSBryan Schumaker {
306d1bacf9eSBryan Schumaker 	int i;
3078ef2ce3eSBryan Schumaker 	loff_t new_pos;
308d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
309d1bacf9eSBryan Schumaker 
310d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3118cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
312496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
3130c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
3140c030806STrond Myklebust 
3158ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3164db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
3174db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
3180c030806STrond Myklebust 				ctx->duped = 0;
3190c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
32023db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
3210c030806STrond Myklebust 				if (ctx->duped > 0
3220c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
3230c030806STrond Myklebust 					if (printk_ratelimit()) {
3246de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
3250c030806STrond Myklebust 								"Please contact your server vendor.  "
3269581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
3279581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
3289581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
3290c030806STrond Myklebust 					}
3300c030806STrond Myklebust 					status = -ELOOP;
3310c030806STrond Myklebust 					goto out;
3320c030806STrond Myklebust 				}
3338ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3340c030806STrond Myklebust 				ctx->duped = -1;
3358ef2ce3eSBryan Schumaker 			}
33623db8620SAl Viro 			desc->ctx->pos = new_pos;
3378cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
33847c716cbSTrond Myklebust 			return 0;
3398cd51a0cSTrond Myklebust 		}
3408cd51a0cSTrond Myklebust 	}
34147c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
342d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
34318fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
34418fb5fe4STrond Myklebust 			desc->eof = 1;
345d1bacf9eSBryan Schumaker 	}
3460c030806STrond Myklebust out:
347d1bacf9eSBryan Schumaker 	return status;
348d1bacf9eSBryan Schumaker }
349d1bacf9eSBryan Schumaker 
350d1bacf9eSBryan Schumaker static
351d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
352d1bacf9eSBryan Schumaker {
353d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
35447c716cbSTrond Myklebust 	int status;
355d1bacf9eSBryan Schumaker 
356d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
357d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
358d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
359d1bacf9eSBryan Schumaker 		goto out;
360d1bacf9eSBryan Schumaker 	}
361d1bacf9eSBryan Schumaker 
362d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
363d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
364d1bacf9eSBryan Schumaker 	else
365d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
366d1bacf9eSBryan Schumaker 
36747c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3680aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
369e47c085aSTrond Myklebust 		desc->current_index += array->size;
37047c716cbSTrond Myklebust 		desc->page_index++;
37147c716cbSTrond Myklebust 	}
372d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
373d1bacf9eSBryan Schumaker out:
374d1bacf9eSBryan Schumaker 	return status;
375d1bacf9eSBryan Schumaker }
376d1bacf9eSBryan Schumaker 
377d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
378d1bacf9eSBryan Schumaker static
37956e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
380d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
381d1bacf9eSBryan Schumaker {
382480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
383480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3844704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3851da177e4SLinus Torvalds 	int		error;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds  again:
3881da177e4SLinus Torvalds 	timestamp = jiffies;
3894704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
390be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3911da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3921da177e4SLinus Torvalds 	if (error < 0) {
3931da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3941da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3951da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3963a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3971da177e4SLinus Torvalds 			desc->plus = 0;
3981da177e4SLinus Torvalds 			goto again;
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 		goto error;
4011da177e4SLinus Torvalds 	}
4021f4eab7eSNeil Brown 	desc->timestamp = timestamp;
4034704f0e2STrond Myklebust 	desc->gencount = gencount;
404d1bacf9eSBryan Schumaker error:
405d1bacf9eSBryan Schumaker 	return error;
406d1bacf9eSBryan Schumaker }
407d1bacf9eSBryan Schumaker 
408573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
409573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
410d1bacf9eSBryan Schumaker {
411573c4e1eSChuck Lever 	int error;
412d1bacf9eSBryan Schumaker 
413573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
414573c4e1eSChuck Lever 	if (error)
415573c4e1eSChuck Lever 		return error;
416d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
417d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
418d1bacf9eSBryan Schumaker 	return 0;
419d1bacf9eSBryan Schumaker }
420d1bacf9eSBryan Schumaker 
421fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
422fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
423fa923369STrond Myklebust  */
424d39ab9deSBryan Schumaker static
425d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
426d39ab9deSBryan Schumaker {
427d8fdb47fSTrond Myklebust 	struct inode *inode;
428fa923369STrond Myklebust 	struct nfs_inode *nfsi;
429fa923369STrond Myklebust 
4302b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
4312b0143b5SDavid Howells 		return 0;
432fa923369STrond Myklebust 
433d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
434d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
435d8fdb47fSTrond Myklebust 		return 0;
436d8fdb47fSTrond Myklebust 
437d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
4387dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
439d39ab9deSBryan Schumaker 		return 0;
4407dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
4417dc72d5fSTrond Myklebust 		return 0;
4427dc72d5fSTrond Myklebust 	return 1;
443d39ab9deSBryan Schumaker }
444d39ab9deSBryan Schumaker 
445d39ab9deSBryan Schumaker static
44623db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
447d69ee9b8STrond Myklebust {
448d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
449d69ee9b8STrond Myklebust 		return false;
450d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
451d69ee9b8STrond Myklebust 		return true;
45223db8620SAl Viro 	if (ctx->pos == 0)
453d69ee9b8STrond Myklebust 		return true;
454d69ee9b8STrond Myklebust 	return false;
455d69ee9b8STrond Myklebust }
456d69ee9b8STrond Myklebust 
457d69ee9b8STrond Myklebust /*
45863519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
45963519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
460d69ee9b8STrond Myklebust  * directory.
461d69ee9b8STrond Myklebust  */
462d69ee9b8STrond Myklebust static
463d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
464d69ee9b8STrond Myklebust {
46563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
46663519fbcSTrond Myklebust 
46763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
46863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
46963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
470d69ee9b8STrond Myklebust }
471d69ee9b8STrond Myklebust 
472311324adSTrond Myklebust /*
473311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
474311324adSTrond Myklebust  *
475311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
476311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
477311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
478311324adSTrond Myklebust  * cache flush.
479311324adSTrond Myklebust  */
480311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
481311324adSTrond Myklebust {
48263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
48363519fbcSTrond Myklebust 
48463519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
48563519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
48663519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
48779f687a3STrond Myklebust 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
488311324adSTrond Myklebust 	}
489311324adSTrond Myklebust }
490311324adSTrond Myklebust 
491d69ee9b8STrond Myklebust static
492d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
493d39ab9deSBryan Schumaker {
49426fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4959ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4964a201d6eSTrond Myklebust 	struct dentry *dentry;
4974a201d6eSTrond Myklebust 	struct dentry *alias;
4982b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
499d39ab9deSBryan Schumaker 	struct inode *inode;
500aa9c2669SDavid Quigley 	int status;
501d39ab9deSBryan Schumaker 
502fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
503fa923369STrond Myklebust 		return;
5046c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
5056c441c25STrond Myklebust 		return;
50678d04af4STrond Myklebust 	if (filename.len == 0)
50778d04af4STrond Myklebust 		return;
50878d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
50978d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
51078d04af4STrond Myklebust 		return;
51178d04af4STrond Myklebust 	/* ...or '/' */
51278d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
51378d04af4STrond Myklebust 		return;
5144a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
5154a201d6eSTrond Myklebust 		if (filename.len == 1)
5164a201d6eSTrond Myklebust 			return;
5174a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
5184a201d6eSTrond Myklebust 			return;
5194a201d6eSTrond Myklebust 	}
5208387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
521d39ab9deSBryan Schumaker 
5224a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
5239ac3d3e8SAl Viro again:
5249ac3d3e8SAl Viro 	if (!dentry) {
5259ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
5269ac3d3e8SAl Viro 		if (IS_ERR(dentry))
5279ac3d3e8SAl Viro 			return;
5289ac3d3e8SAl Viro 	}
5299ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
5306c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
5316c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
5326c441c25STrond Myklebust 					&entry->fattr->fsid))
5336c441c25STrond Myklebust 			goto out;
534d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
5357dc72d5fSTrond Myklebust 			if (!entry->fh->size)
5367dc72d5fSTrond Myklebust 				goto out;
537cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
5382b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
539aa9c2669SDavid Quigley 			if (!status)
5402b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
541d39ab9deSBryan Schumaker 			goto out;
542d39ab9deSBryan Schumaker 		} else {
5435542aa2fSEric W. Biederman 			d_invalidate(dentry);
544d39ab9deSBryan Schumaker 			dput(dentry);
5459ac3d3e8SAl Viro 			dentry = NULL;
5469ac3d3e8SAl Viro 			goto again;
547d39ab9deSBryan Schumaker 		}
548d39ab9deSBryan Schumaker 	}
5497dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5507dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5517dc72d5fSTrond Myklebust 		goto out;
5527dc72d5fSTrond Myklebust 	}
553d39ab9deSBryan Schumaker 
5541775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
55541d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5569ac3d3e8SAl Viro 	d_lookup_done(dentry);
5579ac3d3e8SAl Viro 	if (alias) {
558d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
559d39ab9deSBryan Schumaker 			goto out;
5609ac3d3e8SAl Viro 		dput(dentry);
5619ac3d3e8SAl Viro 		dentry = alias;
5629ac3d3e8SAl Viro 	}
563d39ab9deSBryan Schumaker 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
564d39ab9deSBryan Schumaker out:
565d39ab9deSBryan Schumaker 	dput(dentry);
566d39ab9deSBryan Schumaker }
567d39ab9deSBryan Schumaker 
568d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
569d1bacf9eSBryan Schumaker static
5708cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5716650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
572d1bacf9eSBryan Schumaker {
573babddc72SBryan Schumaker 	struct xdr_stream stream;
574f7da7a12SBenny Halevy 	struct xdr_buf buf;
5756650239aSTrond Myklebust 	struct page *scratch;
57699424380SBryan Schumaker 	struct nfs_cache_array *array;
5775c346854STrond Myklebust 	unsigned int count = 0;
5785c346854STrond Myklebust 	int status;
579babddc72SBryan Schumaker 
5806650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5816650239aSTrond Myklebust 	if (scratch == NULL)
5826650239aSTrond Myklebust 		return -ENOMEM;
583babddc72SBryan Schumaker 
584ce85cfbeSBenjamin Coddington 	if (buflen == 0)
585ce85cfbeSBenjamin Coddington 		goto out_nopages;
586ce85cfbeSBenjamin Coddington 
587f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5886650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
58999424380SBryan Schumaker 
59099424380SBryan Schumaker 	do {
59199424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5928cd51a0cSTrond Myklebust 		if (status != 0) {
5938cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5948cd51a0cSTrond Myklebust 				status = 0;
59599424380SBryan Schumaker 			break;
5968cd51a0cSTrond Myklebust 		}
59799424380SBryan Schumaker 
5985c346854STrond Myklebust 		count++;
5995c346854STrond Myklebust 
60047c716cbSTrond Myklebust 		if (desc->plus != 0)
601be62a1a8SMiklos Szeredi 			nfs_prime_dcache(file_dentry(desc->file), entry);
6028cd51a0cSTrond Myklebust 
6038cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
6048cd51a0cSTrond Myklebust 		if (status != 0)
6058cd51a0cSTrond Myklebust 			break;
60699424380SBryan Schumaker 	} while (!entry->eof);
60799424380SBryan Schumaker 
608ce85cfbeSBenjamin Coddington out_nopages:
60947c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
61099424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
6118cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
6128cd51a0cSTrond Myklebust 			array->eof_index = array->size;
61399424380SBryan Schumaker 			status = 0;
61499424380SBryan Schumaker 			nfs_readdir_release_array(page);
6155c346854STrond Myklebust 		} else
6165c346854STrond Myklebust 			status = PTR_ERR(array);
61756e4ebf8SBryan Schumaker 	}
6186650239aSTrond Myklebust 
6196650239aSTrond Myklebust 	put_page(scratch);
6208cd51a0cSTrond Myklebust 	return status;
6218cd51a0cSTrond Myklebust }
62256e4ebf8SBryan Schumaker 
62356e4ebf8SBryan Schumaker static
624c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
62556e4ebf8SBryan Schumaker {
62656e4ebf8SBryan Schumaker 	unsigned int i;
62756e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
62856e4ebf8SBryan Schumaker 		put_page(pages[i]);
62956e4ebf8SBryan Schumaker }
63056e4ebf8SBryan Schumaker 
63156e4ebf8SBryan Schumaker /*
63256e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
6330b936e37SAnna Schumaker  * to nfs_readdir_free_pagearray
63456e4ebf8SBryan Schumaker  */
63556e4ebf8SBryan Schumaker static
636c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
63756e4ebf8SBryan Schumaker {
63856e4ebf8SBryan Schumaker 	unsigned int i;
63956e4ebf8SBryan Schumaker 
64056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
64156e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
64256e4ebf8SBryan Schumaker 		if (page == NULL)
64356e4ebf8SBryan Schumaker 			goto out_freepages;
64456e4ebf8SBryan Schumaker 		pages[i] = page;
64556e4ebf8SBryan Schumaker 	}
6466650239aSTrond Myklebust 	return 0;
64756e4ebf8SBryan Schumaker 
64856e4ebf8SBryan Schumaker out_freepages:
649c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6506650239aSTrond Myklebust 	return -ENOMEM;
651d1bacf9eSBryan Schumaker }
652d1bacf9eSBryan Schumaker 
653d1bacf9eSBryan Schumaker static
654d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
655d1bacf9eSBryan Schumaker {
65656e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
657d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
658d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
659d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6608cd51a0cSTrond Myklebust 	int status = -ENOMEM;
66156e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
662d1bacf9eSBryan Schumaker 
663d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6640aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
665d1bacf9eSBryan Schumaker 	entry.eof = 0;
666d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
667d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
668573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
669d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
670d1bacf9eSBryan Schumaker 		goto out;
671d1bacf9eSBryan Schumaker 
67214c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
67314c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
67414c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
67514c43f76SDavid Quigley 		goto out;
67614c43f76SDavid Quigley 	}
67714c43f76SDavid Quigley 
678d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6798cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6808cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
68114c43f76SDavid Quigley 		goto out_label_free;
6828cd51a0cSTrond Myklebust 	}
683d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
6849ac3d3e8SAl Viro 	atomic_set(&array->refcount, 1);
685d1bacf9eSBryan Schumaker 	array->eof_index = -1;
686d1bacf9eSBryan Schumaker 
687c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
6886650239aSTrond Myklebust 	if (status < 0)
689d1bacf9eSBryan Schumaker 		goto out_release_array;
690d1bacf9eSBryan Schumaker 	do {
691ac396128STrond Myklebust 		unsigned int pglen;
69256e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
693babddc72SBryan Schumaker 
694d1bacf9eSBryan Schumaker 		if (status < 0)
695d1bacf9eSBryan Schumaker 			break;
696ac396128STrond Myklebust 		pglen = status;
6976650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6988cd51a0cSTrond Myklebust 		if (status < 0) {
6998cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
7008cd51a0cSTrond Myklebust 				status = 0;
7018cd51a0cSTrond Myklebust 			break;
7028cd51a0cSTrond Myklebust 		}
7038cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
704d1bacf9eSBryan Schumaker 
705c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
706d1bacf9eSBryan Schumaker out_release_array:
707d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
70814c43f76SDavid Quigley out_label_free:
70914c43f76SDavid Quigley 	nfs4_label_free(entry.label);
710d1bacf9eSBryan Schumaker out:
711d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
712d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
713d1bacf9eSBryan Schumaker 	return status;
714d1bacf9eSBryan Schumaker }
715d1bacf9eSBryan Schumaker 
716d1bacf9eSBryan Schumaker /*
717d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
718d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
719d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
720d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
7211da177e4SLinus Torvalds  */
722d1bacf9eSBryan Schumaker static
723d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
724d1bacf9eSBryan Schumaker {
725496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
7268cd51a0cSTrond Myklebust 	int ret;
727d1bacf9eSBryan Schumaker 
7288cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
7298cd51a0cSTrond Myklebust 	if (ret < 0)
730d1bacf9eSBryan Schumaker 		goto error;
731d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
732d1bacf9eSBryan Schumaker 
7332aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
734cd9ae2b6STrond Myklebust 		/* Should never happen */
735cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
736cd9ae2b6STrond Myklebust 	}
7371da177e4SLinus Torvalds 	unlock_page(page);
7381da177e4SLinus Torvalds 	return 0;
7391da177e4SLinus Torvalds  error:
7401da177e4SLinus Torvalds 	unlock_page(page);
7418cd51a0cSTrond Myklebust 	return ret;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744d1bacf9eSBryan Schumaker static
745d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7461da177e4SLinus Torvalds {
74711de3b11STrond Myklebust 	nfs_readdir_clear_array(desc->page);
74809cbfeafSKirill A. Shutemov 	put_page(desc->page);
7491da177e4SLinus Torvalds 	desc->page = NULL;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
752d1bacf9eSBryan Schumaker static
753d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7541da177e4SLinus Torvalds {
7559ac3d3e8SAl Viro 	struct page *page;
7569ac3d3e8SAl Viro 
7579ac3d3e8SAl Viro 	for (;;) {
75893c76a3dSAl Viro 		page = read_cache_page(desc->file->f_mapping,
759d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
7609ac3d3e8SAl Viro 		if (IS_ERR(page) || grab_page(page))
7619ac3d3e8SAl Viro 			break;
7629ac3d3e8SAl Viro 		put_page(page);
7639ac3d3e8SAl Viro 	}
7649ac3d3e8SAl Viro 	return page;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds /*
768d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7691da177e4SLinus Torvalds  */
770d1bacf9eSBryan Schumaker static
771d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
772d1bacf9eSBryan Schumaker {
773d1bacf9eSBryan Schumaker 	int res;
774d1bacf9eSBryan Schumaker 
775d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
776d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
777d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
778d1bacf9eSBryan Schumaker 
779d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
78047c716cbSTrond Myklebust 	if (res != 0)
781d1bacf9eSBryan Schumaker 		cache_page_release(desc);
782d1bacf9eSBryan Schumaker 	return res;
783d1bacf9eSBryan Schumaker }
784d1bacf9eSBryan Schumaker 
785d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7861da177e4SLinus Torvalds static inline
7871da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7881da177e4SLinus Torvalds {
7898cd51a0cSTrond Myklebust 	int res;
790d1bacf9eSBryan Schumaker 
7910aded708STrond Myklebust 	if (desc->page_index == 0) {
7928cd51a0cSTrond Myklebust 		desc->current_index = 0;
7930aded708STrond Myklebust 		desc->last_cookie = 0;
7940aded708STrond Myklebust 	}
79547c716cbSTrond Myklebust 	do {
796d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
79747c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7981da177e4SLinus Torvalds 	return res;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds /*
8021da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds static
80523db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	struct file	*file = desc->file;
808d1bacf9eSBryan Schumaker 	int i = 0;
809d1bacf9eSBryan Schumaker 	int res = 0;
810d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8118ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
8128ef2ce3eSBryan Schumaker 
813d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
814e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
815e7c58e97STrond Myklebust 		res = PTR_ERR(array);
816e7c58e97STrond Myklebust 		goto out;
817e7c58e97STrond Myklebust 	}
8181da177e4SLinus Torvalds 
819d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
820ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8211da177e4SLinus Torvalds 
822ece0b423STrond Myklebust 		ent = &array->array[i];
82323db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
82423db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
825ece0b423STrond Myklebust 			desc->eof = 1;
8261da177e4SLinus Torvalds 			break;
827ece0b423STrond Myklebust 		}
82823db8620SAl Viro 		desc->ctx->pos++;
829d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
830d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
831d1bacf9eSBryan Schumaker 		else
832d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
8330c030806STrond Myklebust 		if (ctx->duped != 0)
8340c030806STrond Myklebust 			ctx->duped = 1;
8358cd51a0cSTrond Myklebust 	}
83647c716cbSTrond Myklebust 	if (array->eof_index >= 0)
837d1bacf9eSBryan Schumaker 		desc->eof = 1;
838d1bacf9eSBryan Schumaker 
839d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
840e7c58e97STrond Myklebust out:
841d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8421e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8431e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
8441da177e4SLinus Torvalds 	return res;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds /*
8481da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8491da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8501da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8511da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8521da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8531da177e4SLinus Torvalds  *
8541da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8551da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8561da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8571da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8581da177e4SLinus Torvalds  */
8591da177e4SLinus Torvalds static inline
86023db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	struct page	*page = NULL;
8631da177e4SLinus Torvalds 	int		status;
864496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8650c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8661da177e4SLinus Torvalds 
8671e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8681e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8711da177e4SLinus Torvalds 	if (!page) {
8721da177e4SLinus Torvalds 		status = -ENOMEM;
8731da177e4SLinus Torvalds 		goto out;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
8767a8e1dc3STrond Myklebust 	desc->page_index = 0;
8770aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8787a8e1dc3STrond Myklebust 	desc->page = page;
8790c030806STrond Myklebust 	ctx->duped = 0;
8807a8e1dc3STrond Myklebust 
88185f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
88285f8607eSTrond Myklebust 	if (status < 0)
883d1bacf9eSBryan Schumaker 		goto out_release;
884d1bacf9eSBryan Schumaker 
88523db8620SAl Viro 	status = nfs_do_filldir(desc);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds  out:
8881e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8893110ff80SHarvey Harrison 			__func__, status);
8901da177e4SLinus Torvalds 	return status;
8911da177e4SLinus Torvalds  out_release:
892d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8931da177e4SLinus Torvalds 	goto out;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
89600a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
89700a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
89800a92642SOlivier Galibert    whole directory.
8991da177e4SLinus Torvalds  */
90023db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
9011da177e4SLinus Torvalds {
902be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
9032b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
9041da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
9051da177e4SLinus Torvalds 			*desc = &my_desc;
90623db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
90707b5ce8eSScott Mayhew 	int res = 0;
9081da177e4SLinus Torvalds 
9096de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9106de1472fSAl Viro 			file, (long long)ctx->pos);
91191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
91291d5b470SChuck Lever 
9131da177e4SLinus Torvalds 	/*
91423db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
915f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
91600a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
91700a92642SOlivier Galibert 	 * revalidate the cookie.
9181da177e4SLinus Torvalds 	 */
9191da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
9201da177e4SLinus Torvalds 
92123db8620SAl Viro 	desc->file = file;
92223db8620SAl Viro 	desc->ctx = ctx;
923480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
9241da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
92523db8620SAl Viro 	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
9261da177e4SLinus Torvalds 
92779f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
92823db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
929fccca7fcSTrond Myklebust 	if (res < 0)
930fccca7fcSTrond Myklebust 		goto out;
931fccca7fcSTrond Myklebust 
93247c716cbSTrond Myklebust 	do {
9331da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
93400a92642SOlivier Galibert 
9351da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
936ece0b423STrond Myklebust 			res = 0;
9371da177e4SLinus Torvalds 			/* This means either end of directory */
938d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
9391da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
94023db8620SAl Viro 				res = uncached_readdir(desc);
941ece0b423STrond Myklebust 				if (res == 0)
9421da177e4SLinus Torvalds 					continue;
9431da177e4SLinus Torvalds 			}
9441da177e4SLinus Torvalds 			break;
9451da177e4SLinus Torvalds 		}
9461da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9473a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9481da177e4SLinus Torvalds 			nfs_zap_caches(inode);
949baf57a09STrond Myklebust 			desc->page_index = 0;
9501da177e4SLinus Torvalds 			desc->plus = 0;
951d1bacf9eSBryan Schumaker 			desc->eof = 0;
9521da177e4SLinus Torvalds 			continue;
9531da177e4SLinus Torvalds 		}
9541da177e4SLinus Torvalds 		if (res < 0)
9551da177e4SLinus Torvalds 			break;
9561da177e4SLinus Torvalds 
95723db8620SAl Viro 		res = nfs_do_filldir(desc);
958ece0b423STrond Myklebust 		if (res < 0)
9591da177e4SLinus Torvalds 			break;
96047c716cbSTrond Myklebust 	} while (!desc->eof);
961fccca7fcSTrond Myklebust out:
9621e7cb3dcSChuck Lever 	if (res > 0)
9631e7cb3dcSChuck Lever 		res = 0;
9646de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9651da177e4SLinus Torvalds 	return res;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
968965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
969f0dd2136STrond Myklebust {
970480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
971b84e06c5SChuck Lever 
9726de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9736de1472fSAl Viro 			filp, offset, whence);
974b84e06c5SChuck Lever 
975965c8e59SAndrew Morton 	switch (whence) {
976f0dd2136STrond Myklebust 		case 1:
977f0dd2136STrond Myklebust 			offset += filp->f_pos;
978f0dd2136STrond Myklebust 		case 0:
979f0dd2136STrond Myklebust 			if (offset >= 0)
980f0dd2136STrond Myklebust 				break;
981f0dd2136STrond Myklebust 		default:
9829ac3d3e8SAl Viro 			return -EINVAL;
983f0dd2136STrond Myklebust 	}
984f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
985f0dd2136STrond Myklebust 		filp->f_pos = offset;
986480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9878ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
988f0dd2136STrond Myklebust 	}
989f0dd2136STrond Myklebust 	return offset;
990f0dd2136STrond Myklebust }
991f0dd2136STrond Myklebust 
9921da177e4SLinus Torvalds /*
9931da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9941da177e4SLinus Torvalds  * is a dummy operation.
9951da177e4SLinus Torvalds  */
99602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
99702c24a82SJosef Bacik 			 int datasync)
9981da177e4SLinus Torvalds {
9996de1472fSAl Viro 	struct inode *inode = file_inode(filp);
10007ea80859SChristoph Hellwig 
10016de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10021e7cb3dcSChuck Lever 
10035955102cSAl Viro 	inode_lock(inode);
10046de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
10055955102cSAl Viro 	inode_unlock(inode);
10061da177e4SLinus Torvalds 	return 0;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
1009bfc69a45STrond Myklebust /**
1010bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1011bfc69a45STrond Myklebust  * @dir - pointer to directory inode
1012bfc69a45STrond Myklebust  *
1013bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1014bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1015bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1016bfc69a45STrond Myklebust  *
1017bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1018bfc69a45STrond Myklebust  */
1019bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1020bfc69a45STrond Myklebust {
1021011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
1022bfc69a45STrond Myklebust }
102389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1024bfc69a45STrond Myklebust 
10251da177e4SLinus Torvalds /*
10261da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
10271da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
10281da177e4SLinus Torvalds  * and may need to be looked up again.
1029912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
10301da177e4SLinus Torvalds  */
1031912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1032912a108dSNeilBrown 			      int rcu_walk)
10331da177e4SLinus Torvalds {
1034912a108dSNeilBrown 	int ret;
1035912a108dSNeilBrown 
10361da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
10371da177e4SLinus Torvalds 		return 1;
10384eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
10394eec952eSTrond Myklebust 		return 0;
1040f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10416ecc5e8fSTrond Myklebust 		return 0;
1042f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1043912a108dSNeilBrown 	if (rcu_walk)
1044912a108dSNeilBrown 		ret = nfs_revalidate_inode_rcu(NFS_SERVER(dir), dir);
1045912a108dSNeilBrown 	else
1046912a108dSNeilBrown 		ret = nfs_revalidate_inode(NFS_SERVER(dir), dir);
1047912a108dSNeilBrown 	if (ret < 0)
1048f2c77f4eSTrond Myklebust 		return 0;
1049f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1050f2c77f4eSTrond Myklebust 		return 0;
1051f2c77f4eSTrond Myklebust 	return 1;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds /*
1055a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1056a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1057a12802caSTrond Myklebust  */
1058fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1059a12802caSTrond Myklebust {
1060a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1061a12802caSTrond Myklebust 		return 0;
1062fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1063a12802caSTrond Myklebust }
1064a12802caSTrond Myklebust 
1065a12802caSTrond Myklebust /*
10661d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10671d6757fbSTrond Myklebust  *
10681d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10691d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10701d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10711d6757fbSTrond Myklebust  *
10721d6757fbSTrond Myklebust  */
107365a0c149STrond Myklebust static
1074fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
107765a0c149STrond Myklebust 	int ret;
10781da177e4SLinus Torvalds 
107936d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10804e99a1ffSTrond Myklebust 		return 0;
10811da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1082fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10831da177e4SLinus Torvalds 		goto out_force;
10841da177e4SLinus Torvalds 	/* This is an open(2) */
1085fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1086fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10871da177e4SLinus Torvalds 		goto out_force;
108865a0c149STrond Myklebust out:
108965a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10901da177e4SLinus Torvalds out_force:
10911fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
10921fa1e384SNeilBrown 		return -ECHILD;
109365a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
109465a0c149STrond Myklebust 	if (ret != 0)
109565a0c149STrond Myklebust 		return ret;
109665a0c149STrond Myklebust 	goto out;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds /*
11001da177e4SLinus Torvalds  * We judge how long we want to trust negative
11011da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
11021da177e4SLinus Torvalds  *
11031da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
11041da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1105912a108dSNeilBrown  *
1106912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1107912a108dSNeilBrown  * suggesting a reval is needed.
11081da177e4SLinus Torvalds  */
11091da177e4SLinus Torvalds static inline
11101da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1111fa3c56bbSAl Viro 		       unsigned int flags)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1114fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
11151da177e4SLinus Torvalds 		return 0;
11164eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
11174eec952eSTrond Myklebust 		return 1;
1118912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds /*
11221da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
11231da177e4SLinus Torvalds  * and we should check whether we can really trust that
11241da177e4SLinus Torvalds  * lookup.
11251da177e4SLinus Torvalds  *
11261da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
11271da177e4SLinus Torvalds  * we have an inode!
11281da177e4SLinus Torvalds  *
11291da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
11301da177e4SLinus Torvalds  * cached dentry and do a new lookup.
11311da177e4SLinus Torvalds  */
11320b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	struct inode *dir;
11351da177e4SLinus Torvalds 	struct inode *inode;
11361da177e4SLinus Torvalds 	struct dentry *parent;
1137e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1138e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
11391775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
11401da177e4SLinus Torvalds 	int error;
11411da177e4SLinus Torvalds 
1142d51ac1a8SNeilBrown 	if (flags & LOOKUP_RCU) {
114350d77739SNeilBrown 		parent = ACCESS_ONCE(dentry->d_parent);
11442b0143b5SDavid Howells 		dir = d_inode_rcu(parent);
1145d51ac1a8SNeilBrown 		if (!dir)
114634286d66SNick Piggin 			return -ECHILD;
1147d51ac1a8SNeilBrown 	} else {
11481da177e4SLinus Torvalds 		parent = dget_parent(dentry);
11492b0143b5SDavid Howells 		dir = d_inode(parent);
1150d51ac1a8SNeilBrown 	}
115191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
11522b0143b5SDavid Howells 	inode = d_inode(dentry);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	if (!inode) {
1155912a108dSNeilBrown 		if (nfs_neg_need_reval(dir, dentry, flags)) {
1156d51ac1a8SNeilBrown 			if (flags & LOOKUP_RCU)
1157d51ac1a8SNeilBrown 				return -ECHILD;
11581da177e4SLinus Torvalds 			goto out_bad;
1159912a108dSNeilBrown 		}
116063519fbcSTrond Myklebust 		goto out_valid;
11611da177e4SLinus Torvalds 	}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
1164d51ac1a8SNeilBrown 		if (flags & LOOKUP_RCU)
1165d51ac1a8SNeilBrown 			return -ECHILD;
11666de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
11676de1472fSAl Viro 				__func__, dentry);
11681da177e4SLinus Torvalds 		goto out_bad;
11691da177e4SLinus Torvalds 	}
11701da177e4SLinus Torvalds 
1171011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
117215860ab1STrond Myklebust 		goto out_set_verifier;
117315860ab1STrond Myklebust 
1174912a108dSNeilBrown 	/* Force a full look up iff the parent directory has changed */
1175912a108dSNeilBrown 	if (!nfs_is_exclusive_create(dir, flags) &&
1176912a108dSNeilBrown 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1177912a108dSNeilBrown 
11781fa1e384SNeilBrown 		if (nfs_lookup_verify_inode(inode, flags)) {
1179d51ac1a8SNeilBrown 			if (flags & LOOKUP_RCU)
1180d51ac1a8SNeilBrown 				return -ECHILD;
11811da177e4SLinus Torvalds 			goto out_zap_parent;
11821fa1e384SNeilBrown 		}
118363519fbcSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
11841da177e4SLinus Torvalds 		goto out_valid;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 
1187912a108dSNeilBrown 	if (flags & LOOKUP_RCU)
1188912a108dSNeilBrown 		return -ECHILD;
1189912a108dSNeilBrown 
11901da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11911da177e4SLinus Torvalds 		goto out_bad;
11921da177e4SLinus Torvalds 
1193e1fb4d05STrond Myklebust 	error = -ENOMEM;
1194e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1195e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1196e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1197e1fb4d05STrond Myklebust 		goto out_error;
1198e1fb4d05STrond Myklebust 
119914c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
120014c43f76SDavid Quigley 	if (IS_ERR(label))
120114c43f76SDavid Quigley 		goto out_error;
120214c43f76SDavid Quigley 
12036e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
12041775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
12056e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
12061da177e4SLinus Torvalds 	if (error)
12071da177e4SLinus Torvalds 		goto out_bad;
1208e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
12091da177e4SLinus Torvalds 		goto out_bad;
1210e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
12111da177e4SLinus Torvalds 		goto out_bad;
12121da177e4SLinus Torvalds 
1213aa9c2669SDavid Quigley 	nfs_setsecurity(inode, fattr, label);
1214aa9c2669SDavid Quigley 
1215e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1216e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
121714c43f76SDavid Quigley 	nfs4_label_free(label);
121814c43f76SDavid Quigley 
121963519fbcSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
122063519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
122163519fbcSTrond Myklebust 
122215860ab1STrond Myklebust out_set_verifier:
1223cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12241da177e4SLinus Torvalds  out_valid:
1225d51ac1a8SNeilBrown 	if (flags & LOOKUP_RCU) {
122650d77739SNeilBrown 		if (parent != ACCESS_ONCE(dentry->d_parent))
1227d51ac1a8SNeilBrown 			return -ECHILD;
1228d51ac1a8SNeilBrown 	} else
12291da177e4SLinus Torvalds 		dput(parent);
12306de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
12316de1472fSAl Viro 			__func__, dentry);
12321da177e4SLinus Torvalds 	return 1;
12331da177e4SLinus Torvalds out_zap_parent:
12341da177e4SLinus Torvalds 	nfs_zap_caches(dir);
12351da177e4SLinus Torvalds  out_bad:
1236d51ac1a8SNeilBrown 	WARN_ON(flags & LOOKUP_RCU);
1237c44600c9SAl Viro 	nfs_free_fattr(fattr);
1238c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
123914c43f76SDavid Quigley 	nfs4_label_free(label);
1240a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
12411da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
12421da177e4SLinus Torvalds 		/* Purge readdir caches. */
12431da177e4SLinus Torvalds 		nfs_zap_caches(inode);
1244a3f432bfSJ. Bruce Fields 		/*
1245a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1246a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1247a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1248a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1249a3f432bfSJ. Bruce Fields 		 */
1250a3f432bfSJ. Bruce Fields 		if (IS_ROOT(dentry))
1251d9e80b7dSAl Viro 			goto out_valid;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 	dput(parent);
12546de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
12556de1472fSAl Viro 			__func__, dentry);
12561da177e4SLinus Torvalds 	return 0;
1257e1fb4d05STrond Myklebust out_error:
1258d51ac1a8SNeilBrown 	WARN_ON(flags & LOOKUP_RCU);
1259e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1260e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
126114c43f76SDavid Quigley 	nfs4_label_free(label);
1262e1fb4d05STrond Myklebust 	dput(parent);
12636de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
12646de1472fSAl Viro 			__func__, dentry, error);
1265e1fb4d05STrond Myklebust 	return error;
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds /*
12692b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1270ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1271ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1272ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1273ecf3d1f1SJeff Layton  *
1274ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1275ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1276ecf3d1f1SJeff Layton  */
1277ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1278ecf3d1f1SJeff Layton {
1279ecf3d1f1SJeff Layton 	int error;
12802b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
1281ecf3d1f1SJeff Layton 
1282ecf3d1f1SJeff Layton 	/*
1283ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1284ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1285ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1286ecf3d1f1SJeff Layton 	 */
1287ecf3d1f1SJeff Layton 	if (!inode) {
12886de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
12896de1472fSAl Viro 				__func__, dentry);
1290ecf3d1f1SJeff Layton 		return 1;
1291ecf3d1f1SJeff Layton 	}
1292ecf3d1f1SJeff Layton 
1293ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
12946de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12956de1472fSAl Viro 				__func__, dentry);
1296ecf3d1f1SJeff Layton 		return 0;
1297ecf3d1f1SJeff Layton 	}
1298ecf3d1f1SJeff Layton 
1299ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1300ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1301ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1302ecf3d1f1SJeff Layton 	return !error;
1303ecf3d1f1SJeff Layton }
1304ecf3d1f1SJeff Layton 
1305ecf3d1f1SJeff Layton /*
13061da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
13071da177e4SLinus Torvalds  */
1308fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
13091da177e4SLinus Torvalds {
13106de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
13116de1472fSAl Viro 		dentry, dentry->d_flags);
13121da177e4SLinus Torvalds 
131377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
13142b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
131577f11192STrond Myklebust 		return 1;
131677f11192STrond Myklebust 
13171da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
13181da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
13191da177e4SLinus Torvalds 		return 1;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
13221da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
13231da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
13241da177e4SLinus Torvalds 		return 1;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 	return 0;
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13301f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
13311b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
13321b83d707STrond Myklebust {
13331b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
13341f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
13351f018458STrond Myklebust 	if (inode->i_nlink == 1)
13361f018458STrond Myklebust 		clear_nlink(inode);
13371f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
13381b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
13391b83d707STrond Myklebust }
13401b83d707STrond Myklebust 
13411da177e4SLinus Torvalds /*
13421da177e4SLinus Torvalds  * Called when the dentry loses inode.
13431da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
13441da177e4SLinus Torvalds  */
13451da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
13461da177e4SLinus Torvalds {
134783672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
134883672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
134983672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
135083672d39SNeil Brown 
13511da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1352e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
13531f018458STrond Myklebust 		nfs_drop_nlink(inode);
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds 	iput(inode);
13561da177e4SLinus Torvalds }
13571da177e4SLinus Torvalds 
1358b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1359b1942c5fSAl Viro {
1360b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1361b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1362b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1363b1942c5fSAl Viro 			WARN_ON(1);
1364b1942c5fSAl Viro 		else
1365b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1366b1942c5fSAl Viro 	}
1367b1942c5fSAl Viro }
1368b1942c5fSAl Viro 
1369f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
13701da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1371ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
13721da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13731da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
137436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1375b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13761da177e4SLinus Torvalds };
1377ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
13781da177e4SLinus Torvalds 
1379597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	struct dentry *res;
13821da177e4SLinus Torvalds 	struct inode *inode = NULL;
1383e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1384e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
13851775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
13861da177e4SLinus Torvalds 	int error;
13871da177e4SLinus Torvalds 
13886de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
138991d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
13901da177e4SLinus Torvalds 
1391130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1392130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
13931da177e4SLinus Torvalds 
1394fd684071STrond Myklebust 	/*
1395fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1396fd684071STrond Myklebust 	 * but don't hash the dentry.
1397fd684071STrond Myklebust 	 */
1398130f9ab7SAl Viro 	if (nfs_is_exclusive_create(dir, flags))
1399130f9ab7SAl Viro 		return NULL;
14001da177e4SLinus Torvalds 
1401e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1402e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1403e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1404e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1405e1fb4d05STrond Myklebust 		goto out;
1406e1fb4d05STrond Myklebust 
140714c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
140814c43f76SDavid Quigley 	if (IS_ERR(label))
140914c43f76SDavid Quigley 		goto out;
141014c43f76SDavid Quigley 
14116e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
14121775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
14131da177e4SLinus Torvalds 	if (error == -ENOENT)
14141da177e4SLinus Torvalds 		goto no_entry;
14151da177e4SLinus Torvalds 	if (error < 0) {
14161da177e4SLinus Torvalds 		res = ERR_PTR(error);
1417bf130914SAl Viro 		goto out_label;
14181da177e4SLinus Torvalds 	}
14191775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1420bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
142103f28e3aSTrond Myklebust 	if (IS_ERR(res))
1422bf130914SAl Viro 		goto out_label;
142354ceac45SDavid Howells 
142463519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
142563519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1426d69ee9b8STrond Myklebust 
14271da177e4SLinus Torvalds no_entry:
142841d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
14299eaef27bSTrond Myklebust 	if (res != NULL) {
14309eaef27bSTrond Myklebust 		if (IS_ERR(res))
1431bf130914SAl Viro 			goto out_label;
14321da177e4SLinus Torvalds 		dentry = res;
14339eaef27bSTrond Myklebust 	}
14341da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1435bf130914SAl Viro out_label:
14366e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
143714c43f76SDavid Quigley 	nfs4_label_free(label);
14381da177e4SLinus Torvalds out:
1439e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1440e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
14411da177e4SLinus Torvalds 	return res;
14421da177e4SLinus Torvalds }
1443ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
14441da177e4SLinus Torvalds 
144589d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
14460b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
14471da177e4SLinus Torvalds 
1448f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
14490ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
14501da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
14511da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
145236d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1453b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14541da177e4SLinus Torvalds };
145589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
14561da177e4SLinus Torvalds 
14578a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
14588a5e929dSAl Viro {
14598a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
14608a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
14618a5e929dSAl Viro 		res |= FMODE_READ;
14628a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
14638a5e929dSAl Viro 		res |= FMODE_WRITE;
14648a5e929dSAl Viro 	return res;
14658a5e929dSAl Viro }
14668a5e929dSAl Viro 
1467532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1468cd9a1c0eSTrond Myklebust {
1469532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1470cd9a1c0eSTrond Myklebust }
1471cd9a1c0eSTrond Myklebust 
1472cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1473cd9a1c0eSTrond Myklebust {
1474f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1475cd9a1c0eSTrond Myklebust 	return 0;
1476cd9a1c0eSTrond Myklebust }
1477cd9a1c0eSTrond Myklebust 
1478d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
14790dd2b474SMiklos Szeredi 			   struct dentry *dentry,
148030d90494SAl Viro 			   struct file *file, unsigned open_flags,
148147237687SAl Viro 			   int *opened)
1482cd9a1c0eSTrond Myklebust {
14830dd2b474SMiklos Szeredi 	int err;
14840dd2b474SMiklos Szeredi 
148530d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
148630d90494SAl Viro 	if (err)
1487d9585277SAl Viro 		goto out;
148830d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
14890dd2b474SMiklos Szeredi 
1490cd9a1c0eSTrond Myklebust out:
1491d9585277SAl Viro 	return err;
1492cd9a1c0eSTrond Myklebust }
1493cd9a1c0eSTrond Myklebust 
149473a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
149530d90494SAl Viro 		    struct file *file, unsigned open_flags,
149647237687SAl Viro 		    umode_t mode, int *opened)
14971da177e4SLinus Torvalds {
1498c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1499cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
15000dd2b474SMiklos Szeredi 	struct dentry *res;
15010dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1502f46e0bd3STrond Myklebust 	struct inode *inode;
15031472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1504c94c0953SAl Viro 	bool switched = false;
1505898f635cSTrond Myklebust 	int err;
15061da177e4SLinus Torvalds 
15070dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
15082b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
15090dd2b474SMiklos Szeredi 
15101e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
15116de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
15121e7cb3dcSChuck Lever 
15139597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
15149597c13bSJeff Layton 	if (err)
15159597c13bSJeff Layton 		return err;
15169597c13bSJeff Layton 
15170dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
15180dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
151900699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
15200dd2b474SMiklos Szeredi 			/*
15210dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
15220dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
15230dd2b474SMiklos Szeredi 			 * again
15240dd2b474SMiklos Szeredi 			 */
1525d9585277SAl Viro 			return -ENOENT;
15260dd2b474SMiklos Szeredi 		}
15271472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
15281da177e4SLinus Torvalds 		goto no_open;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15310dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1532d9585277SAl Viro 		return -ENAMETOOLONG;
15331da177e4SLinus Torvalds 
15340dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1535536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
15360dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
15370dd2b474SMiklos Szeredi 	}
1538536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1539536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1540536e43d1STrond Myklebust 		attr.ia_size = 0;
1541cd9a1c0eSTrond Myklebust 	}
1542cd9a1c0eSTrond Myklebust 
1543c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1544c94c0953SAl Viro 		d_drop(dentry);
1545c94c0953SAl Viro 		switched = true;
1546c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1547c94c0953SAl Viro 					  &dentry->d_name, &wq);
1548c94c0953SAl Viro 		if (IS_ERR(dentry))
1549c94c0953SAl Viro 			return PTR_ERR(dentry);
1550c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1551c94c0953SAl Viro 			return finish_no_open(file, dentry);
1552c94c0953SAl Viro 	}
1553c94c0953SAl Viro 
1554532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
15550dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
15560dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1557d9585277SAl Viro 		goto out;
15580dd2b474SMiklos Szeredi 
15596e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
15605bc2afc2STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1561275bb307STrond Myklebust 	if (IS_ERR(inode)) {
15620dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
15636e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15642d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1565d20cb71dSAl Viro 		d_drop(dentry);
15660dd2b474SMiklos Szeredi 		switch (err) {
15671da177e4SLinus Torvalds 		case -ENOENT:
1568f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
1569809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15700dd2b474SMiklos Szeredi 			break;
15711788ea6eSJeff Layton 		case -EISDIR:
15726f926b5bSTrond Myklebust 		case -ENOTDIR:
15736f926b5bSTrond Myklebust 			goto no_open;
15741da177e4SLinus Torvalds 		case -ELOOP:
15750dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
15761da177e4SLinus Torvalds 				goto no_open;
15770dd2b474SMiklos Szeredi 			break;
15781da177e4SLinus Torvalds 			/* case -EINVAL: */
15791da177e4SLinus Torvalds 		default:
15800dd2b474SMiklos Szeredi 			break;
15811da177e4SLinus Torvalds 		}
15821da177e4SLinus Torvalds 		goto out;
15831da177e4SLinus Torvalds 	}
15840dd2b474SMiklos Szeredi 
1585275bb307STrond Myklebust 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
15866e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15872d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1588d9585277SAl Viro out:
1589c94c0953SAl Viro 	if (unlikely(switched)) {
1590c94c0953SAl Viro 		d_lookup_done(dentry);
1591c94c0953SAl Viro 		dput(dentry);
1592c94c0953SAl Viro 	}
1593d9585277SAl Viro 	return err;
15940dd2b474SMiklos Szeredi 
15951da177e4SLinus Torvalds no_open:
15961472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1597c94c0953SAl Viro 	if (switched) {
1598c94c0953SAl Viro 		d_lookup_done(dentry);
1599c94c0953SAl Viro 		if (!res)
1600c94c0953SAl Viro 			res = dentry;
1601c94c0953SAl Viro 		else
1602c94c0953SAl Viro 			dput(dentry);
1603c94c0953SAl Viro 	}
16040dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1605c94c0953SAl Viro 		return PTR_ERR(res);
1606e45198a6SAl Viro 	return finish_no_open(file, res);
16071da177e4SLinus Torvalds }
160889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
16091da177e4SLinus Torvalds 
16100b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
16111da177e4SLinus Torvalds {
1612657e94b6SNick Piggin 	struct inode *inode;
161350de348cSMiklos Szeredi 	int ret = 0;
16141da177e4SLinus Torvalds 
1615fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1616eda72afbSMiklos Szeredi 		goto no_open;
1617eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
16185584c306STrond Myklebust 		goto no_open;
161949f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
162049f9a0faSTrond Myklebust 		goto no_open;
16212b484297STrond Myklebust 
16222b0143b5SDavid Howells 	inode = d_inode(dentry);
16232b484297STrond Myklebust 
16241da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
16251da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
16261da177e4SLinus Torvalds 	 */
1627216d5d06STrond Myklebust 	if (inode == NULL) {
162849317a7fSNeilBrown 		struct dentry *parent;
162949317a7fSNeilBrown 		struct inode *dir;
163049317a7fSNeilBrown 
1631912a108dSNeilBrown 		if (flags & LOOKUP_RCU) {
163250d77739SNeilBrown 			parent = ACCESS_ONCE(dentry->d_parent);
16332b0143b5SDavid Howells 			dir = d_inode_rcu(parent);
1634912a108dSNeilBrown 			if (!dir)
1635d51ac1a8SNeilBrown 				return -ECHILD;
1636912a108dSNeilBrown 		} else {
163749317a7fSNeilBrown 			parent = dget_parent(dentry);
16382b0143b5SDavid Howells 			dir = d_inode(parent);
1639912a108dSNeilBrown 		}
1640fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1641216d5d06STrond Myklebust 			ret = 1;
1642912a108dSNeilBrown 		else if (flags & LOOKUP_RCU)
1643912a108dSNeilBrown 			ret = -ECHILD;
1644912a108dSNeilBrown 		if (!(flags & LOOKUP_RCU))
164549317a7fSNeilBrown 			dput(parent);
164650d77739SNeilBrown 		else if (parent != ACCESS_ONCE(dentry->d_parent))
1647912a108dSNeilBrown 			return -ECHILD;
16481da177e4SLinus Torvalds 		goto out;
1649216d5d06STrond Myklebust 	}
1650216d5d06STrond Myklebust 
16511da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
16521da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
165349317a7fSNeilBrown 		goto no_open;
16541da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1655fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
165649317a7fSNeilBrown 		goto no_open;
16571da177e4SLinus Torvalds 
16580ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1659898f635cSTrond Myklebust 	ret = 1;
16600ef97dcfSMiklos Szeredi 
16611da177e4SLinus Torvalds out:
16621da177e4SLinus Torvalds 	return ret;
1663535918f1STrond Myklebust 
16645584c306STrond Myklebust no_open:
16650b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1666c0204fd2STrond Myklebust }
1667c0204fd2STrond Myklebust 
16681da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds /*
16711da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
16721da177e4SLinus Torvalds  */
16731da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
16741775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
16751775fd3eSDavid Quigley 				struct nfs4_label *label)
16761da177e4SLinus Torvalds {
1677fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
16782b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
16791da177e4SLinus Torvalds 	struct inode *inode;
16801da177e4SLinus Torvalds 	int error = -EACCES;
16811da177e4SLinus Torvalds 
1682fab728e1STrond Myklebust 	d_drop(dentry);
1683fab728e1STrond Myklebust 
16841da177e4SLinus Torvalds 	/* We may have been initialized further down */
16852b0143b5SDavid Howells 	if (d_really_is_positive(dentry))
1686fab728e1STrond Myklebust 		goto out;
16871da177e4SLinus Torvalds 	if (fhandle->size == 0) {
16881775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
16891da177e4SLinus Torvalds 		if (error)
1690fab728e1STrond Myklebust 			goto out_error;
16911da177e4SLinus Torvalds 	}
16925724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16931da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
16941da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
16951775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
16961da177e4SLinus Torvalds 		if (error < 0)
1697fab728e1STrond Myklebust 			goto out_error;
16981da177e4SLinus Torvalds 	}
16991775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
170003f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
170103f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1702fab728e1STrond Myklebust 		goto out_error;
1703fab728e1STrond Myklebust 	d_add(dentry, inode);
1704fab728e1STrond Myklebust out:
1705fab728e1STrond Myklebust 	dput(parent);
17061da177e4SLinus Torvalds 	return 0;
1707fab728e1STrond Myklebust out_error:
1708fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1709fab728e1STrond Myklebust 	dput(parent);
1710fab728e1STrond Myklebust 	return error;
17111da177e4SLinus Torvalds }
1712ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds /*
17151da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
17161da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
17171da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
17181da177e4SLinus Torvalds  * reply path made it appear to have failed.
17191da177e4SLinus Torvalds  */
1720597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1721ebfc3b49SAl Viro 		umode_t mode, bool excl)
17221da177e4SLinus Torvalds {
17231da177e4SLinus Torvalds 	struct iattr attr;
1724ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
17251da177e4SLinus Torvalds 	int error;
17261da177e4SLinus Torvalds 
17271e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
17286de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	attr.ia_mode = mode;
17311da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17321da177e4SLinus Torvalds 
17338b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
17348867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
17358b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
17361da177e4SLinus Torvalds 	if (error != 0)
17371da177e4SLinus Torvalds 		goto out_err;
17381da177e4SLinus Torvalds 	return 0;
17391da177e4SLinus Torvalds out_err:
17401da177e4SLinus Torvalds 	d_drop(dentry);
17411da177e4SLinus Torvalds 	return error;
17421da177e4SLinus Torvalds }
1743ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds /*
17461da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17471da177e4SLinus Torvalds  */
1748597d9289SBryan Schumaker int
17491a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
17501da177e4SLinus Torvalds {
17511da177e4SLinus Torvalds 	struct iattr attr;
17521da177e4SLinus Torvalds 	int status;
17531da177e4SLinus Torvalds 
17541e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
17556de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	attr.ia_mode = mode;
17581da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17591da177e4SLinus Torvalds 
17601ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
17611da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
17621ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
17631da177e4SLinus Torvalds 	if (status != 0)
17641da177e4SLinus Torvalds 		goto out_err;
17651da177e4SLinus Torvalds 	return 0;
17661da177e4SLinus Torvalds out_err:
17671da177e4SLinus Torvalds 	d_drop(dentry);
17681da177e4SLinus Torvalds 	return status;
17691da177e4SLinus Torvalds }
1770ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds /*
17731da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17741da177e4SLinus Torvalds  */
1775597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	struct iattr attr;
17781da177e4SLinus Torvalds 	int error;
17791da177e4SLinus Torvalds 
17801e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
17816de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17841da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
17851da177e4SLinus Torvalds 
17861ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
17871da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
17881ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
17891da177e4SLinus Torvalds 	if (error != 0)
17901da177e4SLinus Torvalds 		goto out_err;
17911da177e4SLinus Torvalds 	return 0;
17921da177e4SLinus Torvalds out_err:
17931da177e4SLinus Torvalds 	d_drop(dentry);
17941da177e4SLinus Torvalds 	return error;
17951da177e4SLinus Torvalds }
1796ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
17971da177e4SLinus Torvalds 
1798d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1799d45b9d8bSTrond Myklebust {
1800dc3f4198SAl Viro 	if (simple_positive(dentry))
1801d45b9d8bSTrond Myklebust 		d_delete(dentry);
1802d45b9d8bSTrond Myklebust }
1803d45b9d8bSTrond Myklebust 
1804597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
18051da177e4SLinus Torvalds {
18061da177e4SLinus Torvalds 	int error;
18071da177e4SLinus Torvalds 
18081e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
18096de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18101da177e4SLinus Torvalds 
18111ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
18122b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1813884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
18141da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18151da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1816ba6c0592STrond Myklebust 		switch (error) {
1817ba6c0592STrond Myklebust 		case 0:
18182b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1819ba6c0592STrond Myklebust 			break;
1820ba6c0592STrond Myklebust 		case -ENOENT:
1821d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1822ba6c0592STrond Myklebust 		}
1823884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1824ba6c0592STrond Myklebust 	} else
1825ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18261ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	return error;
18291da177e4SLinus Torvalds }
1830ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds /*
18331da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
18341da177e4SLinus Torvalds  * and after checking that the file has only one user.
18351da177e4SLinus Torvalds  *
18361da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
18371da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
18381da177e4SLinus Torvalds  */
18391da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
18401da177e4SLinus Torvalds {
18412b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
18422b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
18431da177e4SLinus Torvalds 	int error = -EBUSY;
18441da177e4SLinus Torvalds 
18456de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
18481da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
18491da177e4SLinus Torvalds 		error = 0;
18501da177e4SLinus Torvalds 		goto out;
18511da177e4SLinus Torvalds 	}
18521da177e4SLinus Torvalds 
18531ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
18541da177e4SLinus Torvalds 	if (inode != NULL) {
185557ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
18561da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
18571da177e4SLinus Torvalds 		if (error == 0)
18581b83d707STrond Myklebust 			nfs_drop_nlink(inode);
18591da177e4SLinus Torvalds 	} else
18601da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1861d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1862d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
18631ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
18641da177e4SLinus Torvalds out:
18651da177e4SLinus Torvalds 	return error;
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
18691da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
18701da177e4SLinus Torvalds  *
18711da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
18721da177e4SLinus Torvalds  */
1873597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds 	int error;
18761da177e4SLinus Torvalds 	int need_rehash = 0;
18771da177e4SLinus Torvalds 
18781e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
18796de1472fSAl Viro 		dir->i_ino, dentry);
18801da177e4SLinus Torvalds 
18811ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
18821da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
188384d08fa8SAl Viro 	if (d_count(dentry) > 1) {
18841da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1885ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
18862b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
18871da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
18881ca42382STrond Myklebust 		goto out;
18891da177e4SLinus Torvalds 	}
18901da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
18911da177e4SLinus Torvalds 		__d_drop(dentry);
18921da177e4SLinus Torvalds 		need_rehash = 1;
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
18951da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1896d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
18971da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18981da177e4SLinus Torvalds 	} else if (need_rehash)
18991da177e4SLinus Torvalds 		d_rehash(dentry);
19001ca42382STrond Myklebust out:
19011ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
19021da177e4SLinus Torvalds 	return error;
19031da177e4SLinus Torvalds }
1904ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
19051da177e4SLinus Torvalds 
1906873101b3SChuck Lever /*
1907873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1908873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1909873101b3SChuck Lever  * using prepare_write/commit_write.
1910873101b3SChuck Lever  *
1911873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1912873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1913873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1914873101b3SChuck Lever  * symlink request has completed on the server.
1915873101b3SChuck Lever  *
1916873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1917873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1918873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1919873101b3SChuck Lever  * and move the raw page into its mapping.
1920873101b3SChuck Lever  */
1921597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19221da177e4SLinus Torvalds {
1923873101b3SChuck Lever 	struct page *page;
1924873101b3SChuck Lever 	char *kaddr;
19251da177e4SLinus Torvalds 	struct iattr attr;
1926873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
19271da177e4SLinus Torvalds 	int error;
19281da177e4SLinus Torvalds 
19291e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
19306de1472fSAl Viro 		dir->i_ino, dentry, symname);
19311da177e4SLinus Torvalds 
1932873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1933873101b3SChuck Lever 		return -ENAMETOOLONG;
19341da177e4SLinus Torvalds 
1935873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1936873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
19371da177e4SLinus Torvalds 
1938e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
193976566991STrond Myklebust 	if (!page)
1940873101b3SChuck Lever 		return -ENOMEM;
1941873101b3SChuck Lever 
1942e8ecde25SAl Viro 	kaddr = page_address(page);
1943873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1944873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1945873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1946873101b3SChuck Lever 
19471ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
194894a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
19491ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1950873101b3SChuck Lever 	if (error != 0) {
19511e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1952873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
19536de1472fSAl Viro 			dentry, symname, error);
19541da177e4SLinus Torvalds 		d_drop(dentry);
1955873101b3SChuck Lever 		__free_page(page);
19561da177e4SLinus Torvalds 		return error;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 
1959873101b3SChuck Lever 	/*
1960873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1961873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1962873101b3SChuck Lever 	 */
19632b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
1964873101b3SChuck Lever 							GFP_KERNEL)) {
1965873101b3SChuck Lever 		SetPageUptodate(page);
1966873101b3SChuck Lever 		unlock_page(page);
1967a0b54addSRafael Aquini 		/*
1968a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
1969a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
1970a0b54addSRafael Aquini 		 */
197109cbfeafSKirill A. Shutemov 		put_page(page);
1972873101b3SChuck Lever 	} else
1973873101b3SChuck Lever 		__free_page(page);
1974873101b3SChuck Lever 
1975873101b3SChuck Lever 	return 0;
1976873101b3SChuck Lever }
1977ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1978873101b3SChuck Lever 
1979597d9289SBryan Schumaker int
19801da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
19811da177e4SLinus Torvalds {
19822b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
19831da177e4SLinus Torvalds 	int error;
19841da177e4SLinus Torvalds 
19856de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
19866de1472fSAl Viro 		old_dentry, dentry);
19871da177e4SLinus Torvalds 
19881fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
198957ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
19909a3936aaSTrond Myklebust 
19919697d234STrond Myklebust 	d_drop(dentry);
19921da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1993cf809556STrond Myklebust 	if (error == 0) {
19947de9c6eeSAl Viro 		ihold(inode);
19959697d234STrond Myklebust 		d_add(dentry, inode);
1996cf809556STrond Myklebust 	}
19971fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
19981da177e4SLinus Torvalds 	return error;
19991da177e4SLinus Torvalds }
2000ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds /*
20031da177e4SLinus Torvalds  * RENAME
20041da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
20051da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
20061da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
20071da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
20081da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
20091da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
20101da177e4SLinus Torvalds  *
20111da177e4SLinus Torvalds  * FIXED.
20121da177e4SLinus Torvalds  *
20131da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
20141da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
20151da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
20161da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
20171da177e4SLinus Torvalds  * using the inode layer
20181da177e4SLinus Torvalds  *
20191da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
20201da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
20211da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
20221da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
20231da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
20241da177e4SLinus Torvalds  * the rename.
20251da177e4SLinus Torvalds  */
2026597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
20271cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
20281cd66c93SMiklos Szeredi 	       unsigned int flags)
20291da177e4SLinus Torvalds {
20302b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
20312b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
20321da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
203380a491fdSJeff Layton 	struct rpc_task *task;
20341da177e4SLinus Torvalds 	int error = -EBUSY;
20351da177e4SLinus Torvalds 
20361cd66c93SMiklos Szeredi 	if (flags)
20371cd66c93SMiklos Szeredi 		return -EINVAL;
20381cd66c93SMiklos Szeredi 
20396de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
20406de1472fSAl Viro 		 old_dentry, new_dentry,
204184d08fa8SAl Viro 		 d_count(new_dentry));
20421da177e4SLinus Torvalds 
204370ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
20441da177e4SLinus Torvalds 	/*
204528f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
204628f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
204728f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
204828f79a1aSMiklos Szeredi 	 * the new target.
20491da177e4SLinus Torvalds 	 */
205027226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
205127226104SMiklos Szeredi 		/*
205227226104SMiklos Szeredi 		 * To prevent any new references to the target during the
205327226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
205427226104SMiklos Szeredi 		 */
205527226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
205627226104SMiklos Szeredi 			d_drop(new_dentry);
205727226104SMiklos Szeredi 			rehash = new_dentry;
205827226104SMiklos Szeredi 		}
205927226104SMiklos Szeredi 
206084d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
20611da177e4SLinus Torvalds 			int err;
206227226104SMiklos Szeredi 
20631da177e4SLinus Torvalds 			/* copy the target dentry's name */
20641da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
20651da177e4SLinus Torvalds 					 &new_dentry->d_name);
20661da177e4SLinus Torvalds 			if (!dentry)
20671da177e4SLinus Torvalds 				goto out;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
20701da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
207124e93025SMiklos Szeredi 			if (err)
20721da177e4SLinus Torvalds 				goto out;
207324e93025SMiklos Szeredi 
207424e93025SMiklos Szeredi 			new_dentry = dentry;
207556335936SOGAWA Hirofumi 			rehash = NULL;
207624e93025SMiklos Szeredi 			new_inode = NULL;
2077b1e4adf4STrond Myklebust 		}
207827226104SMiklos Szeredi 	}
20791da177e4SLinus Torvalds 
208057ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
2081b1e4adf4STrond Myklebust 	if (new_inode != NULL)
208257ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
20831da177e4SLinus Torvalds 
208480a491fdSJeff Layton 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
208580a491fdSJeff Layton 	if (IS_ERR(task)) {
208680a491fdSJeff Layton 		error = PTR_ERR(task);
208780a491fdSJeff Layton 		goto out;
208880a491fdSJeff Layton 	}
208980a491fdSJeff Layton 
209080a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
209180a491fdSJeff Layton 	if (error == 0)
209280a491fdSJeff Layton 		error = task->tk_status;
209380a491fdSJeff Layton 	rpc_put_task(task);
20945ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
20951da177e4SLinus Torvalds out:
20961da177e4SLinus Torvalds 	if (rehash)
20971da177e4SLinus Torvalds 		d_rehash(rehash);
209870ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
209970ded201STrond Myklebust 			new_dir, new_dentry, error);
21001da177e4SLinus Torvalds 	if (!error) {
2101b1e4adf4STrond Myklebust 		if (new_inode != NULL)
2102b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
21031da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
21048fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
21058fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
2106d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
2107d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	/* new dentry created? */
21101da177e4SLinus Torvalds 	if (dentry)
21111da177e4SLinus Torvalds 		dput(dentry);
21121da177e4SLinus Torvalds 	return error;
21131da177e4SLinus Torvalds }
2114ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
21151da177e4SLinus Torvalds 
2116cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2117cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2118cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2119cfcea3e8STrond Myklebust 
21203a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
21213a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
21223a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
21233a505845STrond Myklebust 
21241c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
21251c3c07e9STrond Myklebust {
21261c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
2127f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
21284e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2129cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
21304e857c58SPeter Zijlstra 	smp_mb__after_atomic();
21311c3c07e9STrond Myklebust }
21321c3c07e9STrond Myklebust 
21331a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
21341a81bb8aSTrond Myklebust {
21351a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
21361a81bb8aSTrond Myklebust 
21371a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
21381a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
21391a81bb8aSTrond Myklebust 		list_del(&cache->lru);
21401a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
21411a81bb8aSTrond Myklebust 	}
21421a81bb8aSTrond Myklebust }
21431a81bb8aSTrond Myklebust 
21443a505845STrond Myklebust static unsigned long
21453a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2146979df72eSTrond Myklebust {
2147979df72eSTrond Myklebust 	LIST_HEAD(head);
2148aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2149979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
21501ab6c499SDave Chinner 	long freed = 0;
2151979df72eSTrond Myklebust 
2152a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2153aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2154979df72eSTrond Myklebust 		struct inode *inode;
2155979df72eSTrond Myklebust 
2156979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2157979df72eSTrond Myklebust 			break;
21589c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2159979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2160979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2161979df72eSTrond Myklebust 			goto remove_lru_entry;
2162979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2163979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2164979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2165979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
21661ab6c499SDave Chinner 		freed++;
2167979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2168979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2169979df72eSTrond Myklebust 					&nfs_access_lru_list);
2170979df72eSTrond Myklebust 		else {
2171979df72eSTrond Myklebust remove_lru_entry:
2172979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
21734e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2174979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
21754e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2176979df72eSTrond Myklebust 		}
217759844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2178979df72eSTrond Myklebust 	}
2179979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
21801a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
21811ab6c499SDave Chinner 	return freed;
21821ab6c499SDave Chinner }
21831ab6c499SDave Chinner 
21841ab6c499SDave Chinner unsigned long
21853a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
21863a505845STrond Myklebust {
21873a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
21883a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
21893a505845STrond Myklebust 
21903a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
21913a505845STrond Myklebust 		return SHRINK_STOP;
21923a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
21933a505845STrond Myklebust }
21943a505845STrond Myklebust 
21953a505845STrond Myklebust 
21963a505845STrond Myklebust unsigned long
21971ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
21981ab6c499SDave Chinner {
219955f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2200979df72eSTrond Myklebust }
2201979df72eSTrond Myklebust 
22023a505845STrond Myklebust static void
22033a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
22043a505845STrond Myklebust {
22053a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
22063a505845STrond Myklebust 	unsigned long diff;
22073a505845STrond Myklebust 	unsigned int nr_to_scan;
22083a505845STrond Myklebust 
22093a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
22103a505845STrond Myklebust 		return;
22113a505845STrond Myklebust 	nr_to_scan = 100;
22123a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
22133a505845STrond Myklebust 	if (diff < nr_to_scan)
22143a505845STrond Myklebust 		nr_to_scan = diff;
22153a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
22163a505845STrond Myklebust }
22173a505845STrond Myklebust 
22181a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
22191c3c07e9STrond Myklebust {
22201c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
22211a81bb8aSTrond Myklebust 	struct rb_node *n;
22221c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
22231c3c07e9STrond Myklebust 
22241c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
22251c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
22261c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
22271c3c07e9STrond Myklebust 		rb_erase(n, root_node);
22281a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
22291c3c07e9STrond Myklebust 	}
22301c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
22311c3c07e9STrond Myklebust }
22321c3c07e9STrond Myklebust 
22331c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
22341c3c07e9STrond Myklebust {
22351a81bb8aSTrond Myklebust 	LIST_HEAD(head);
22361a81bb8aSTrond Myklebust 
22371a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
22381a81bb8aSTrond Myklebust 		return;
2239cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2240cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
22411a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2242cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2243cfcea3e8STrond Myklebust 
22441c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
22451a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
22461a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
22471a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22481a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22491c3c07e9STrond Myklebust }
22501c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
22511c3c07e9STrond Myklebust 
22521c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
22531c3c07e9STrond Myklebust {
22541c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
22551c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
22561c3c07e9STrond Myklebust 
22571c3c07e9STrond Myklebust 	while (n != NULL) {
22581c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
22591c3c07e9STrond Myklebust 
22601c3c07e9STrond Myklebust 		if (cred < entry->cred)
22611c3c07e9STrond Myklebust 			n = n->rb_left;
22621c3c07e9STrond Myklebust 		else if (cred > entry->cred)
22631c3c07e9STrond Myklebust 			n = n->rb_right;
22641c3c07e9STrond Myklebust 		else
22651c3c07e9STrond Myklebust 			return entry;
22661c3c07e9STrond Myklebust 	}
22671c3c07e9STrond Myklebust 	return NULL;
22681c3c07e9STrond Myklebust }
22691c3c07e9STrond Myklebust 
227057b69181STrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block)
22711da177e4SLinus Torvalds {
227255296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
22731c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
227457b69181STrond Myklebust 	bool retry = true;
227557b69181STrond Myklebust 	int err;
22761da177e4SLinus Torvalds 
22771c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
227857b69181STrond Myklebust 	for(;;) {
22791c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
22801c3c07e9STrond Myklebust 			goto out_zap;
22811c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
228257b69181STrond Myklebust 		err = -ENOENT;
22831c3c07e9STrond Myklebust 		if (cache == NULL)
22841c3c07e9STrond Myklebust 			goto out;
228557b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
228657b69181STrond Myklebust 		if (!nfs_attribute_cache_expired(inode) &&
228757b69181STrond Myklebust 		    !(nfsi->cache_validity & NFS_INO_INVALID_ATTR))
228857b69181STrond Myklebust 			break;
228957b69181STrond Myklebust 		err = -ECHILD;
229057b69181STrond Myklebust 		if (!may_block)
229157b69181STrond Myklebust 			goto out;
229257b69181STrond Myklebust 		if (!retry)
229357b69181STrond Myklebust 			goto out_zap;
229457b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
229557b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
229657b69181STrond Myklebust 		if (err)
229757b69181STrond Myklebust 			return err;
229857b69181STrond Myklebust 		spin_lock(&inode->i_lock);
229957b69181STrond Myklebust 		retry = false;
230057b69181STrond Myklebust 	}
23011c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
23021c3c07e9STrond Myklebust 	res->cred = cache->cred;
23031c3c07e9STrond Myklebust 	res->mask = cache->mask;
2304cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
23051c3c07e9STrond Myklebust 	err = 0;
23061c3c07e9STrond Myklebust out:
23071c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23081c3c07e9STrond Myklebust 	return err;
23091c3c07e9STrond Myklebust out_zap:
23101a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
23111a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
23121c3c07e9STrond Myklebust 	return -ENOENT;
23131c3c07e9STrond Myklebust }
23141c3c07e9STrond Myklebust 
2315f682a398SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2316f682a398SNeilBrown {
2317f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2318f682a398SNeilBrown 	 * but do it without locking.
2319f682a398SNeilBrown 	 */
2320f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2321f682a398SNeilBrown 	struct nfs_access_entry *cache;
2322f682a398SNeilBrown 	int err = -ECHILD;
2323f682a398SNeilBrown 	struct list_head *lh;
2324f682a398SNeilBrown 
2325f682a398SNeilBrown 	rcu_read_lock();
2326f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2327f682a398SNeilBrown 		goto out;
2328f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2329f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2330f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2331f682a398SNeilBrown 	    cred != cache->cred)
2332f682a398SNeilBrown 		cache = NULL;
2333f682a398SNeilBrown 	if (cache == NULL)
2334f682a398SNeilBrown 		goto out;
233557b69181STrond Myklebust 	err = nfs_revalidate_inode_rcu(NFS_SERVER(inode), inode);
233657b69181STrond Myklebust 	if (err)
2337f682a398SNeilBrown 		goto out;
2338f682a398SNeilBrown 	res->jiffies = cache->jiffies;
2339f682a398SNeilBrown 	res->cred = cache->cred;
2340f682a398SNeilBrown 	res->mask = cache->mask;
2341f682a398SNeilBrown out:
2342f682a398SNeilBrown 	rcu_read_unlock();
2343f682a398SNeilBrown 	return err;
2344f682a398SNeilBrown }
2345f682a398SNeilBrown 
23461c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
23471c3c07e9STrond Myklebust {
2348cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2349cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
23501c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
23511c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
23521c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
23531c3c07e9STrond Myklebust 
23541c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
23551c3c07e9STrond Myklebust 	while (*p != NULL) {
23561c3c07e9STrond Myklebust 		parent = *p;
23571c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
23581c3c07e9STrond Myklebust 
23591c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
23601c3c07e9STrond Myklebust 			p = &parent->rb_left;
23611c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
23621c3c07e9STrond Myklebust 			p = &parent->rb_right;
23631c3c07e9STrond Myklebust 		else
23641c3c07e9STrond Myklebust 			goto found;
23651c3c07e9STrond Myklebust 	}
23661c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
23671c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2368cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
23691c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23701c3c07e9STrond Myklebust 	return;
23711c3c07e9STrond Myklebust found:
23721c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2373cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2374cfcea3e8STrond Myklebust 	list_del(&entry->lru);
23751c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23761c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
23771da177e4SLinus Torvalds }
23781da177e4SLinus Torvalds 
23796168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
23801da177e4SLinus Torvalds {
23811c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
23821c3c07e9STrond Myklebust 	if (cache == NULL)
23831c3c07e9STrond Myklebust 		return;
23841c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
23851da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
23861c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
23871da177e4SLinus Torvalds 	cache->mask = set->mask;
23881c3c07e9STrond Myklebust 
2389f682a398SNeilBrown 	/* The above field assignments must be visible
2390f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2391f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2392f682a398SNeilBrown 	 */
2393f682a398SNeilBrown 	smp_wmb();
23941c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2395cfcea3e8STrond Myklebust 
2396cfcea3e8STrond Myklebust 	/* Update accounting */
23974e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2398cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
23994e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2400cfcea3e8STrond Myklebust 
2401cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
24021a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2403cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
24041a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
24051a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
24061a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2407cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2408cfcea3e8STrond Myklebust 	}
24093a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
24101da177e4SLinus Torvalds }
24116168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
24126168f62cSWeston Andros Adamson 
24136168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
24146168f62cSWeston Andros Adamson {
24156168f62cSWeston Andros Adamson 	entry->mask = 0;
24166168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
24176168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
24186168f62cSWeston Andros Adamson 	if (access_result &
24196168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
24206168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
24216168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
24226168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
24236168f62cSWeston Andros Adamson }
24246168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
24271da177e4SLinus Torvalds {
24281da177e4SLinus Torvalds 	struct nfs_access_entry cache;
242957b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
24301da177e4SLinus Torvalds 	int status;
24311da177e4SLinus Torvalds 
2432f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2433f4ce1299STrond Myklebust 
2434f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2435f682a398SNeilBrown 	if (status != 0)
243657b69181STrond Myklebust 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
24371da177e4SLinus Torvalds 	if (status == 0)
2438f4ce1299STrond Myklebust 		goto out_cached;
24391da177e4SLinus Torvalds 
2440f3324a2aSNeilBrown 	status = -ECHILD;
244157b69181STrond Myklebust 	if (!may_block)
2442f3324a2aSNeilBrown 		goto out;
2443f3324a2aSNeilBrown 
24441da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
24451da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
24461da177e4SLinus Torvalds 	cache.cred = cred;
24471da177e4SLinus Torvalds 	cache.jiffies = jiffies;
24481da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2449a71ee337SSuresh Jayaraman 	if (status != 0) {
2450a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2451a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2452a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2453a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2454a71ee337SSuresh Jayaraman 		}
2455f4ce1299STrond Myklebust 		goto out;
2456a71ee337SSuresh Jayaraman 	}
24571da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2458f4ce1299STrond Myklebust out_cached:
2459f4ce1299STrond Myklebust 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2460f4ce1299STrond Myklebust 		status = -EACCES;
24611da177e4SLinus Torvalds out:
2462f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2463f4ce1299STrond Myklebust 	return status;
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
2466af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2467af22f94aSTrond Myklebust {
2468af22f94aSTrond Myklebust 	int mask = 0;
2469af22f94aSTrond Myklebust 
2470f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2471f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2472f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2473f8d9a897SWeston Andros Adamson 	} else {
24748a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2475af22f94aSTrond Myklebust 			mask |= MAY_READ;
24768a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2477af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2478f8d9a897SWeston Andros Adamson 	}
2479f8d9a897SWeston Andros Adamson 
2480af22f94aSTrond Myklebust 	return mask;
2481af22f94aSTrond Myklebust }
2482af22f94aSTrond Myklebust 
2483af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2484af22f94aSTrond Myklebust {
2485af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2486af22f94aSTrond Myklebust }
248789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2488af22f94aSTrond Myklebust 
24895c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
24905c5fc09aSTrond Myklebust {
24915c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
24925c5fc09aSTrond Myklebust 	int ret;
24935c5fc09aSTrond Myklebust 
24945c5fc09aSTrond Myklebust 	if (mask & MAY_NOT_BLOCK)
24955c5fc09aSTrond Myklebust 		ret = nfs_revalidate_inode_rcu(server, inode);
24965c5fc09aSTrond Myklebust 	else
24975c5fc09aSTrond Myklebust 		ret = nfs_revalidate_inode(server, inode);
24985c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
24995c5fc09aSTrond Myklebust 		ret = -EACCES;
25005c5fc09aSTrond Myklebust 	return ret;
25015c5fc09aSTrond Myklebust }
25025c5fc09aSTrond Myklebust 
250310556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
25041da177e4SLinus Torvalds {
25051da177e4SLinus Torvalds 	struct rpc_cred *cred;
25061da177e4SLinus Torvalds 	int res = 0;
25071da177e4SLinus Torvalds 
250891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
250991d5b470SChuck Lever 
2510e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
25111da177e4SLinus Torvalds 		goto out;
25121da177e4SLinus Torvalds 	/* Is this sys_access() ? */
25139cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
25141da177e4SLinus Torvalds 		goto force_lookup;
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
25171da177e4SLinus Torvalds 		case S_IFLNK:
25181da177e4SLinus Torvalds 			goto out;
25191da177e4SLinus Torvalds 		case S_IFREG:
2520762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2521762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2522762674f8STrond Myklebust 				return 0;
25231da177e4SLinus Torvalds 			break;
25241da177e4SLinus Torvalds 		case S_IFDIR:
25251da177e4SLinus Torvalds 			/*
25261da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
25271da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
25281da177e4SLinus Torvalds 			 */
25291da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
25301da177e4SLinus Torvalds 				goto out;
25311da177e4SLinus Torvalds 	}
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds force_lookup:
25341da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
25351da177e4SLinus Torvalds 		goto out_notsup;
25361da177e4SLinus Torvalds 
2537f3324a2aSNeilBrown 	/* Always try fast lookups first */
2538f3324a2aSNeilBrown 	rcu_read_lock();
2539f3324a2aSNeilBrown 	cred = rpc_lookup_cred_nonblock();
2540f3324a2aSNeilBrown 	if (!IS_ERR(cred))
2541f3324a2aSNeilBrown 		res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2542f3324a2aSNeilBrown 	else
2543f3324a2aSNeilBrown 		res = PTR_ERR(cred);
2544f3324a2aSNeilBrown 	rcu_read_unlock();
2545f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2546f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
254798a8e323STrond Myklebust 		cred = rpc_lookup_cred();
25481da177e4SLinus Torvalds 		if (!IS_ERR(cred)) {
25491da177e4SLinus Torvalds 			res = nfs_do_access(inode, cred, mask);
25501da177e4SLinus Torvalds 			put_rpccred(cred);
25511da177e4SLinus Torvalds 		} else
25521da177e4SLinus Torvalds 			res = PTR_ERR(cred);
2553f3324a2aSNeilBrown 	}
25541da177e4SLinus Torvalds out:
25555c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
25565c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2557f696a365SMiklos Szeredi 
25581e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
25591e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
25601da177e4SLinus Torvalds 	return res;
25611da177e4SLinus Torvalds out_notsup:
2562d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2563d51ac1a8SNeilBrown 		return -ECHILD;
2564d51ac1a8SNeilBrown 
25651da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
25661da177e4SLinus Torvalds 	if (res == 0)
25672830ba7fSAl Viro 		res = generic_permission(inode, mask);
25681e7cb3dcSChuck Lever 	goto out;
25691da177e4SLinus Torvalds }
2570ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds /*
25731da177e4SLinus Torvalds  * Local variables:
25741da177e4SLinus Torvalds  *  version-control: t
25751da177e4SLinus Torvalds  *  kept-new-versions: 5
25761da177e4SLinus Torvalds  * End:
25771da177e4SLinus Torvalds  */
2578