xref: /openbmc/linux/fs/nfs/dir.c (revision 5542aa2f)
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,
6023db8620SAl Viro 	.iterate	= 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 {
136311324adSTrond Myklebust 	put_nfs_open_dir_context(filp->f_path.dentry->d_inode, 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 {
14888b8e133SChuck Lever 	int size;
149d1bacf9eSBryan Schumaker 	int eof_index;
150d1bacf9eSBryan Schumaker 	u64 last_cookie;
151d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
152d1bacf9eSBryan Schumaker };
153d1bacf9eSBryan Schumaker 
154573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1551da177e4SLinus Torvalds typedef struct {
1561da177e4SLinus Torvalds 	struct file	*file;
1571da177e4SLinus Torvalds 	struct page	*page;
15823db8620SAl Viro 	struct dir_context *ctx;
1591da177e4SLinus Torvalds 	unsigned long	page_index;
160f0dd2136STrond Myklebust 	u64		*dir_cookie;
1610aded708STrond Myklebust 	u64		last_cookie;
162f0dd2136STrond Myklebust 	loff_t		current_index;
1631da177e4SLinus Torvalds 	decode_dirent_t	decode;
164d1bacf9eSBryan Schumaker 
1651f4eab7eSNeil Brown 	unsigned long	timestamp;
1664704f0e2STrond Myklebust 	unsigned long	gencount;
167d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
168d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
169d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1701da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1711da177e4SLinus Torvalds 
172d1bacf9eSBryan Schumaker /*
173d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds static
176d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1771da177e4SLinus Torvalds {
1788cd51a0cSTrond Myklebust 	void *ptr;
179d1bacf9eSBryan Schumaker 	if (page == NULL)
180d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1818cd51a0cSTrond Myklebust 	ptr = kmap(page);
1828cd51a0cSTrond Myklebust 	if (ptr == NULL)
1838cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1848cd51a0cSTrond Myklebust 	return ptr;
185d1bacf9eSBryan Schumaker }
186d1bacf9eSBryan Schumaker 
187d1bacf9eSBryan Schumaker static
188d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
189d1bacf9eSBryan Schumaker {
190d1bacf9eSBryan Schumaker 	kunmap(page);
191d1bacf9eSBryan Schumaker }
192d1bacf9eSBryan Schumaker 
193d1bacf9eSBryan Schumaker /*
194d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
195d1bacf9eSBryan Schumaker  */
196d1bacf9eSBryan Schumaker static
19711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
198d1bacf9eSBryan Schumaker {
19911de3b11STrond Myklebust 	struct nfs_cache_array *array;
200d1bacf9eSBryan Schumaker 	int i;
2018cd51a0cSTrond Myklebust 
2022b86ce2dSCong Wang 	array = kmap_atomic(page);
203d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
204d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
2052b86ce2dSCong Wang 	kunmap_atomic(array);
206d1bacf9eSBryan Schumaker }
207d1bacf9eSBryan Schumaker 
208d1bacf9eSBryan Schumaker /*
209d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
210d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
211d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
212d1bacf9eSBryan Schumaker  */
213d1bacf9eSBryan Schumaker static
2144a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
215d1bacf9eSBryan Schumaker {
216d1bacf9eSBryan Schumaker 	string->len = len;
217d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2184a201d6eSTrond Myklebust 	if (string->name == NULL)
2194a201d6eSTrond Myklebust 		return -ENOMEM;
22004e4bd1cSCatalin Marinas 	/*
22104e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
22204e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
22304e4bd1cSCatalin Marinas 	 */
22404e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2254a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2264a201d6eSTrond Myklebust 	return 0;
227d1bacf9eSBryan Schumaker }
228d1bacf9eSBryan Schumaker 
229d1bacf9eSBryan Schumaker static
230d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
231d1bacf9eSBryan Schumaker {
232d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2334a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2344a201d6eSTrond Myklebust 	int ret;
2354a201d6eSTrond Myklebust 
236d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
237d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
238d1bacf9eSBryan Schumaker 
2394a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2403020093fSTrond Myklebust 
2413020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2423020093fSTrond Myklebust 	ret = -ENOSPC;
2433020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2443020093fSTrond Myklebust 		goto out;
2453020093fSTrond Myklebust 
2464a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2474a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2480b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2494a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2504a201d6eSTrond Myklebust 	if (ret)
2514a201d6eSTrond Myklebust 		goto out;
252d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2538cd51a0cSTrond Myklebust 	array->size++;
25447c716cbSTrond Myklebust 	if (entry->eof != 0)
255d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2564a201d6eSTrond Myklebust out:
257d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2584a201d6eSTrond Myklebust 	return ret;
259d1bacf9eSBryan Schumaker }
260d1bacf9eSBryan Schumaker 
261d1bacf9eSBryan Schumaker static
262d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
263d1bacf9eSBryan Schumaker {
26423db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
265d1bacf9eSBryan Schumaker 	unsigned int index;
266d1bacf9eSBryan Schumaker 
267d1bacf9eSBryan Schumaker 	if (diff < 0)
268d1bacf9eSBryan Schumaker 		goto out_eof;
269d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2708cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
271d1bacf9eSBryan Schumaker 			goto out_eof;
272d1bacf9eSBryan Schumaker 		return -EAGAIN;
273d1bacf9eSBryan Schumaker 	}
274d1bacf9eSBryan Schumaker 
275d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
276d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
277d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
278d1bacf9eSBryan Schumaker 	return 0;
279d1bacf9eSBryan Schumaker out_eof:
280d1bacf9eSBryan Schumaker 	desc->eof = 1;
281d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
282d1bacf9eSBryan Schumaker }
283d1bacf9eSBryan Schumaker 
2844db72b40SJeff Layton static bool
2854db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2864db72b40SJeff Layton {
2874db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2884db72b40SJeff Layton 		return false;
2894db72b40SJeff Layton 	smp_rmb();
2904db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2914db72b40SJeff Layton }
2924db72b40SJeff Layton 
293d1bacf9eSBryan Schumaker static
294d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
295d1bacf9eSBryan Schumaker {
296d1bacf9eSBryan Schumaker 	int i;
2978ef2ce3eSBryan Schumaker 	loff_t new_pos;
298d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
299d1bacf9eSBryan Schumaker 
300d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3018cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
302496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
3030c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
3040c030806STrond Myklebust 
3058ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3064db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
3074db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
3080c030806STrond Myklebust 				ctx->duped = 0;
3090c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
31023db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
3110c030806STrond Myklebust 				if (ctx->duped > 0
3120c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
3130c030806STrond Myklebust 					if (printk_ratelimit()) {
3146de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
3150c030806STrond Myklebust 								"Please contact your server vendor.  "
3169581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
3179581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
3189581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
3190c030806STrond Myklebust 					}
3200c030806STrond Myklebust 					status = -ELOOP;
3210c030806STrond Myklebust 					goto out;
3220c030806STrond Myklebust 				}
3238ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3240c030806STrond Myklebust 				ctx->duped = -1;
3258ef2ce3eSBryan Schumaker 			}
32623db8620SAl Viro 			desc->ctx->pos = new_pos;
3278cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
32847c716cbSTrond Myklebust 			return 0;
3298cd51a0cSTrond Myklebust 		}
3308cd51a0cSTrond Myklebust 	}
33147c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
332d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
33318fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
33418fb5fe4STrond Myklebust 			desc->eof = 1;
335d1bacf9eSBryan Schumaker 	}
3360c030806STrond Myklebust out:
337d1bacf9eSBryan Schumaker 	return status;
338d1bacf9eSBryan Schumaker }
339d1bacf9eSBryan Schumaker 
340d1bacf9eSBryan Schumaker static
341d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
342d1bacf9eSBryan Schumaker {
343d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
34447c716cbSTrond Myklebust 	int status;
345d1bacf9eSBryan Schumaker 
346d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
347d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
348d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
349d1bacf9eSBryan Schumaker 		goto out;
350d1bacf9eSBryan Schumaker 	}
351d1bacf9eSBryan Schumaker 
352d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
353d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
354d1bacf9eSBryan Schumaker 	else
355d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
356d1bacf9eSBryan Schumaker 
35747c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3580aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
359e47c085aSTrond Myklebust 		desc->current_index += array->size;
36047c716cbSTrond Myklebust 		desc->page_index++;
36147c716cbSTrond Myklebust 	}
362d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
363d1bacf9eSBryan Schumaker out:
364d1bacf9eSBryan Schumaker 	return status;
365d1bacf9eSBryan Schumaker }
366d1bacf9eSBryan Schumaker 
367d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
368d1bacf9eSBryan Schumaker static
36956e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
370d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
371d1bacf9eSBryan Schumaker {
372480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
373480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3744704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3751da177e4SLinus Torvalds 	int		error;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds  again:
3781da177e4SLinus Torvalds 	timestamp = jiffies;
3794704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
38056e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3811da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3821da177e4SLinus Torvalds 	if (error < 0) {
3831da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3841da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3851da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3863a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3871da177e4SLinus Torvalds 			desc->plus = 0;
3881da177e4SLinus Torvalds 			goto again;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 		goto error;
3911da177e4SLinus Torvalds 	}
3921f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3934704f0e2STrond Myklebust 	desc->gencount = gencount;
394d1bacf9eSBryan Schumaker error:
395d1bacf9eSBryan Schumaker 	return error;
396d1bacf9eSBryan Schumaker }
397d1bacf9eSBryan Schumaker 
398573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
399573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
400d1bacf9eSBryan Schumaker {
401573c4e1eSChuck Lever 	int error;
402d1bacf9eSBryan Schumaker 
403573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
404573c4e1eSChuck Lever 	if (error)
405573c4e1eSChuck Lever 		return error;
406d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
407d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
408d1bacf9eSBryan Schumaker 	return 0;
409d1bacf9eSBryan Schumaker }
410d1bacf9eSBryan Schumaker 
411d39ab9deSBryan Schumaker static
412d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
413d39ab9deSBryan Schumaker {
414d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
415d39ab9deSBryan Schumaker 		goto different;
41637a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
417d39ab9deSBryan Schumaker 		goto different;
418d39ab9deSBryan Schumaker 	return 1;
419d39ab9deSBryan Schumaker different:
420d39ab9deSBryan Schumaker 	return 0;
421d39ab9deSBryan Schumaker }
422d39ab9deSBryan Schumaker 
423d39ab9deSBryan Schumaker static
42423db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
425d69ee9b8STrond Myklebust {
426d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
427d69ee9b8STrond Myklebust 		return false;
428d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
429d69ee9b8STrond Myklebust 		return true;
43023db8620SAl Viro 	if (ctx->pos == 0)
431d69ee9b8STrond Myklebust 		return true;
432d69ee9b8STrond Myklebust 	return false;
433d69ee9b8STrond Myklebust }
434d69ee9b8STrond Myklebust 
435d69ee9b8STrond Myklebust /*
436d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
437d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
438d69ee9b8STrond Myklebust  * directory.
439d69ee9b8STrond Myklebust  */
440d69ee9b8STrond Myklebust static
441d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
442d69ee9b8STrond Myklebust {
443d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
444d69ee9b8STrond Myklebust }
445d69ee9b8STrond Myklebust 
446311324adSTrond Myklebust /*
447311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
448311324adSTrond Myklebust  *
449311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
450311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
451311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
452311324adSTrond Myklebust  * cache flush.
453311324adSTrond Myklebust  */
454311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
455311324adSTrond Myklebust {
456311324adSTrond Myklebust 	if (!list_empty(&NFS_I(dir)->open_files)) {
457311324adSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
458311324adSTrond Myklebust 		nfs_zap_mapping(dir, dir->i_mapping);
459311324adSTrond Myklebust 	}
460311324adSTrond Myklebust }
461311324adSTrond Myklebust 
462d69ee9b8STrond Myklebust static
463d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
464d39ab9deSBryan Schumaker {
46526fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4664a201d6eSTrond Myklebust 	struct dentry *dentry;
4674a201d6eSTrond Myklebust 	struct dentry *alias;
468d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
469d39ab9deSBryan Schumaker 	struct inode *inode;
470aa9c2669SDavid Quigley 	int status;
471d39ab9deSBryan Schumaker 
4724a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4734a201d6eSTrond Myklebust 		if (filename.len == 1)
4744a201d6eSTrond Myklebust 			return;
4754a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4764a201d6eSTrond Myklebust 			return;
4774a201d6eSTrond Myklebust 	}
4784a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
479d39ab9deSBryan Schumaker 
4804a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
481d39ab9deSBryan Schumaker 	if (dentry != NULL) {
482d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
483cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
484aa9c2669SDavid Quigley 			status = nfs_refresh_inode(dentry->d_inode, entry->fattr);
485aa9c2669SDavid Quigley 			if (!status)
486aa9c2669SDavid Quigley 				nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label);
487d39ab9deSBryan Schumaker 			goto out;
488d39ab9deSBryan Schumaker 		} else {
4895542aa2fSEric W. Biederman 			d_invalidate(dentry);
490d39ab9deSBryan Schumaker 			dput(dentry);
491d39ab9deSBryan Schumaker 		}
492d39ab9deSBryan Schumaker 	}
493d39ab9deSBryan Schumaker 
494d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4954a201d6eSTrond Myklebust 	if (dentry == NULL)
4964a201d6eSTrond Myklebust 		return;
4974a201d6eSTrond Myklebust 
4981775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
499d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
500d39ab9deSBryan Schumaker 		goto out;
501d39ab9deSBryan Schumaker 
502d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
503d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
504d39ab9deSBryan Schumaker 		goto out;
505d39ab9deSBryan Schumaker 	else if (alias) {
506d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
507d39ab9deSBryan Schumaker 		dput(alias);
508d39ab9deSBryan Schumaker 	} else
509d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
510d39ab9deSBryan Schumaker 
511d39ab9deSBryan Schumaker out:
512d39ab9deSBryan Schumaker 	dput(dentry);
513d39ab9deSBryan Schumaker }
514d39ab9deSBryan Schumaker 
515d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
516d1bacf9eSBryan Schumaker static
5178cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5186650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
519d1bacf9eSBryan Schumaker {
520babddc72SBryan Schumaker 	struct xdr_stream stream;
521f7da7a12SBenny Halevy 	struct xdr_buf buf;
5226650239aSTrond Myklebust 	struct page *scratch;
52399424380SBryan Schumaker 	struct nfs_cache_array *array;
5245c346854STrond Myklebust 	unsigned int count = 0;
5255c346854STrond Myklebust 	int status;
526babddc72SBryan Schumaker 
5276650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5286650239aSTrond Myklebust 	if (scratch == NULL)
5296650239aSTrond Myklebust 		return -ENOMEM;
530babddc72SBryan Schumaker 
531f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5326650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
53399424380SBryan Schumaker 
53499424380SBryan Schumaker 	do {
53599424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5368cd51a0cSTrond Myklebust 		if (status != 0) {
5378cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5388cd51a0cSTrond Myklebust 				status = 0;
53999424380SBryan Schumaker 			break;
5408cd51a0cSTrond Myklebust 		}
54199424380SBryan Schumaker 
5425c346854STrond Myklebust 		count++;
5435c346854STrond Myklebust 
54447c716cbSTrond Myklebust 		if (desc->plus != 0)
545d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5468cd51a0cSTrond Myklebust 
5478cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5488cd51a0cSTrond Myklebust 		if (status != 0)
5498cd51a0cSTrond Myklebust 			break;
55099424380SBryan Schumaker 	} while (!entry->eof);
55199424380SBryan Schumaker 
55247c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
55399424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5548cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5558cd51a0cSTrond Myklebust 			array->eof_index = array->size;
55699424380SBryan Schumaker 			status = 0;
55799424380SBryan Schumaker 			nfs_readdir_release_array(page);
5585c346854STrond Myklebust 		} else
5595c346854STrond Myklebust 			status = PTR_ERR(array);
56056e4ebf8SBryan Schumaker 	}
5616650239aSTrond Myklebust 
5626650239aSTrond Myklebust 	put_page(scratch);
5638cd51a0cSTrond Myklebust 	return status;
5648cd51a0cSTrond Myklebust }
56556e4ebf8SBryan Schumaker 
56656e4ebf8SBryan Schumaker static
56756e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
56856e4ebf8SBryan Schumaker {
56956e4ebf8SBryan Schumaker 	unsigned int i;
57056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
57156e4ebf8SBryan Schumaker 		put_page(pages[i]);
57256e4ebf8SBryan Schumaker }
57356e4ebf8SBryan Schumaker 
57456e4ebf8SBryan Schumaker static
57556e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
57656e4ebf8SBryan Schumaker 		unsigned int npages)
57756e4ebf8SBryan Schumaker {
57856e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
57956e4ebf8SBryan Schumaker }
58056e4ebf8SBryan Schumaker 
58156e4ebf8SBryan Schumaker /*
58256e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
58356e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
58456e4ebf8SBryan Schumaker  */
58556e4ebf8SBryan Schumaker static
5866650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
58756e4ebf8SBryan Schumaker {
58856e4ebf8SBryan Schumaker 	unsigned int i;
58956e4ebf8SBryan Schumaker 
59056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
59156e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
59256e4ebf8SBryan Schumaker 		if (page == NULL)
59356e4ebf8SBryan Schumaker 			goto out_freepages;
59456e4ebf8SBryan Schumaker 		pages[i] = page;
59556e4ebf8SBryan Schumaker 	}
5966650239aSTrond Myklebust 	return 0;
59756e4ebf8SBryan Schumaker 
59856e4ebf8SBryan Schumaker out_freepages:
59956e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
6006650239aSTrond Myklebust 	return -ENOMEM;
601d1bacf9eSBryan Schumaker }
602d1bacf9eSBryan Schumaker 
603d1bacf9eSBryan Schumaker static
604d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
605d1bacf9eSBryan Schumaker {
60656e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
60756e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
608d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
609d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
610d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6118cd51a0cSTrond Myklebust 	int status = -ENOMEM;
61256e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
613d1bacf9eSBryan Schumaker 
614d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6150aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
616d1bacf9eSBryan Schumaker 	entry.eof = 0;
617d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
618d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
619573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
620d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
621d1bacf9eSBryan Schumaker 		goto out;
622d1bacf9eSBryan Schumaker 
62314c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
62414c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
62514c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
62614c43f76SDavid Quigley 		goto out;
62714c43f76SDavid Quigley 	}
62814c43f76SDavid Quigley 
629d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6308cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6318cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
63214c43f76SDavid Quigley 		goto out_label_free;
6338cd51a0cSTrond Myklebust 	}
634d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
635d1bacf9eSBryan Schumaker 	array->eof_index = -1;
636d1bacf9eSBryan Schumaker 
6376650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6386650239aSTrond Myklebust 	if (status < 0)
639d1bacf9eSBryan Schumaker 		goto out_release_array;
640d1bacf9eSBryan Schumaker 	do {
641ac396128STrond Myklebust 		unsigned int pglen;
64256e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
643babddc72SBryan Schumaker 
644d1bacf9eSBryan Schumaker 		if (status < 0)
645d1bacf9eSBryan Schumaker 			break;
646ac396128STrond Myklebust 		pglen = status;
6476650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6488cd51a0cSTrond Myklebust 		if (status < 0) {
6498cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6508cd51a0cSTrond Myklebust 				status = 0;
6518cd51a0cSTrond Myklebust 			break;
6528cd51a0cSTrond Myklebust 		}
6538cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
654d1bacf9eSBryan Schumaker 
65556e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
656d1bacf9eSBryan Schumaker out_release_array:
657d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
65814c43f76SDavid Quigley out_label_free:
65914c43f76SDavid Quigley 	nfs4_label_free(entry.label);
660d1bacf9eSBryan Schumaker out:
661d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
662d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
663d1bacf9eSBryan Schumaker 	return status;
664d1bacf9eSBryan Schumaker }
665d1bacf9eSBryan Schumaker 
666d1bacf9eSBryan Schumaker /*
667d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
668d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
669d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
670d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6711da177e4SLinus Torvalds  */
672d1bacf9eSBryan Schumaker static
673d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
674d1bacf9eSBryan Schumaker {
675496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6768cd51a0cSTrond Myklebust 	int ret;
677d1bacf9eSBryan Schumaker 
6788cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6798cd51a0cSTrond Myklebust 	if (ret < 0)
680d1bacf9eSBryan Schumaker 		goto error;
681d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
682d1bacf9eSBryan Schumaker 
6832aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
684cd9ae2b6STrond Myklebust 		/* Should never happen */
685cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
686cd9ae2b6STrond Myklebust 	}
6871da177e4SLinus Torvalds 	unlock_page(page);
6881da177e4SLinus Torvalds 	return 0;
6891da177e4SLinus Torvalds  error:
6901da177e4SLinus Torvalds 	unlock_page(page);
6918cd51a0cSTrond Myklebust 	return ret;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
694d1bacf9eSBryan Schumaker static
695d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6961da177e4SLinus Torvalds {
69711de3b11STrond Myklebust 	if (!desc->page->mapping)
69811de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6991da177e4SLinus Torvalds 	page_cache_release(desc->page);
7001da177e4SLinus Torvalds 	desc->page = NULL;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
703d1bacf9eSBryan Schumaker static
704d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7051da177e4SLinus Torvalds {
706496ad9aaSAl Viro 	return read_cache_page(file_inode(desc->file)->i_mapping,
707d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /*
711d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7121da177e4SLinus Torvalds  */
713d1bacf9eSBryan Schumaker static
714d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
715d1bacf9eSBryan Schumaker {
716d1bacf9eSBryan Schumaker 	int res;
717d1bacf9eSBryan Schumaker 
718d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
719d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
720d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
721d1bacf9eSBryan Schumaker 
722d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
72347c716cbSTrond Myklebust 	if (res != 0)
724d1bacf9eSBryan Schumaker 		cache_page_release(desc);
725d1bacf9eSBryan Schumaker 	return res;
726d1bacf9eSBryan Schumaker }
727d1bacf9eSBryan Schumaker 
728d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7291da177e4SLinus Torvalds static inline
7301da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7311da177e4SLinus Torvalds {
7328cd51a0cSTrond Myklebust 	int res;
733d1bacf9eSBryan Schumaker 
7340aded708STrond Myklebust 	if (desc->page_index == 0) {
7358cd51a0cSTrond Myklebust 		desc->current_index = 0;
7360aded708STrond Myklebust 		desc->last_cookie = 0;
7370aded708STrond Myklebust 	}
73847c716cbSTrond Myklebust 	do {
739d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
74047c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7411da177e4SLinus Torvalds 	return res;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /*
7451da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7461da177e4SLinus Torvalds  */
7471da177e4SLinus Torvalds static
74823db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct file	*file = desc->file;
751d1bacf9eSBryan Schumaker 	int i = 0;
752d1bacf9eSBryan Schumaker 	int res = 0;
753d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7548ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7558ef2ce3eSBryan Schumaker 
756d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
757e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
758e7c58e97STrond Myklebust 		res = PTR_ERR(array);
759e7c58e97STrond Myklebust 		goto out;
760e7c58e97STrond Myklebust 	}
7611da177e4SLinus Torvalds 
762d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
763ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7641da177e4SLinus Torvalds 
765ece0b423STrond Myklebust 		ent = &array->array[i];
76623db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
76723db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
768ece0b423STrond Myklebust 			desc->eof = 1;
7691da177e4SLinus Torvalds 			break;
770ece0b423STrond Myklebust 		}
77123db8620SAl Viro 		desc->ctx->pos++;
772d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
773d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
774d1bacf9eSBryan Schumaker 		else
775d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7760c030806STrond Myklebust 		if (ctx->duped != 0)
7770c030806STrond Myklebust 			ctx->duped = 1;
7788cd51a0cSTrond Myklebust 	}
77947c716cbSTrond Myklebust 	if (array->eof_index >= 0)
780d1bacf9eSBryan Schumaker 		desc->eof = 1;
781d1bacf9eSBryan Schumaker 
782d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
783e7c58e97STrond Myklebust out:
784d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7851e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7861e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7871da177e4SLinus Torvalds 	return res;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds /*
7911da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7921da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7931da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7941da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7951da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7961da177e4SLinus Torvalds  *
7971da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7981da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7991da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8001da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8011da177e4SLinus Torvalds  */
8021da177e4SLinus Torvalds static inline
80323db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	struct page	*page = NULL;
8061da177e4SLinus Torvalds 	int		status;
807496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8080c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8091da177e4SLinus Torvalds 
8101e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8111e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8141da177e4SLinus Torvalds 	if (!page) {
8151da177e4SLinus Torvalds 		status = -ENOMEM;
8161da177e4SLinus Torvalds 		goto out;
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8197a8e1dc3STrond Myklebust 	desc->page_index = 0;
8200aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8217a8e1dc3STrond Myklebust 	desc->page = page;
8220c030806STrond Myklebust 	ctx->duped = 0;
8237a8e1dc3STrond Myklebust 
82485f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
82585f8607eSTrond Myklebust 	if (status < 0)
826d1bacf9eSBryan Schumaker 		goto out_release;
827d1bacf9eSBryan Schumaker 
82823db8620SAl Viro 	status = nfs_do_filldir(desc);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds  out:
8311e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8323110ff80SHarvey Harrison 			__func__, status);
8331da177e4SLinus Torvalds 	return status;
8341da177e4SLinus Torvalds  out_release:
835d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8361da177e4SLinus Torvalds 	goto out;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
839311324adSTrond Myklebust static bool nfs_dir_mapping_need_revalidate(struct inode *dir)
840311324adSTrond Myklebust {
841311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
842311324adSTrond Myklebust 
843311324adSTrond Myklebust 	if (nfs_attribute_cache_expired(dir))
844311324adSTrond Myklebust 		return true;
845311324adSTrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
846311324adSTrond Myklebust 		return true;
847311324adSTrond Myklebust 	return false;
848311324adSTrond Myklebust }
849311324adSTrond Myklebust 
85000a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
85100a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
85200a92642SOlivier Galibert    whole directory.
8531da177e4SLinus Torvalds  */
85423db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8551da177e4SLinus Torvalds {
85623db8620SAl Viro 	struct dentry	*dentry = file->f_path.dentry;
8571da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8581da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8591da177e4SLinus Torvalds 			*desc = &my_desc;
86023db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
86107b5ce8eSScott Mayhew 	int res = 0;
8621da177e4SLinus Torvalds 
8636de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
8646de1472fSAl Viro 			file, (long long)ctx->pos);
86591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
86691d5b470SChuck Lever 
8671da177e4SLinus Torvalds 	/*
86823db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
869f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
87000a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
87100a92642SOlivier Galibert 	 * revalidate the cookie.
8721da177e4SLinus Torvalds 	 */
8731da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8741da177e4SLinus Torvalds 
87523db8620SAl Viro 	desc->file = file;
87623db8620SAl Viro 	desc->ctx = ctx;
877480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8781da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
87923db8620SAl Viro 	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
8801da177e4SLinus Torvalds 
881565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
882311324adSTrond Myklebust 	if (ctx->pos == 0 || nfs_dir_mapping_need_revalidate(inode))
88323db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
884fccca7fcSTrond Myklebust 	if (res < 0)
885fccca7fcSTrond Myklebust 		goto out;
886fccca7fcSTrond Myklebust 
88747c716cbSTrond Myklebust 	do {
8881da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
88900a92642SOlivier Galibert 
8901da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
891ece0b423STrond Myklebust 			res = 0;
8921da177e4SLinus Torvalds 			/* This means either end of directory */
893d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8941da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
89523db8620SAl Viro 				res = uncached_readdir(desc);
896ece0b423STrond Myklebust 				if (res == 0)
8971da177e4SLinus Torvalds 					continue;
8981da177e4SLinus Torvalds 			}
8991da177e4SLinus Torvalds 			break;
9001da177e4SLinus Torvalds 		}
9011da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9023a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9031da177e4SLinus Torvalds 			nfs_zap_caches(inode);
904baf57a09STrond Myklebust 			desc->page_index = 0;
9051da177e4SLinus Torvalds 			desc->plus = 0;
906d1bacf9eSBryan Schumaker 			desc->eof = 0;
9071da177e4SLinus Torvalds 			continue;
9081da177e4SLinus Torvalds 		}
9091da177e4SLinus Torvalds 		if (res < 0)
9101da177e4SLinus Torvalds 			break;
9111da177e4SLinus Torvalds 
91223db8620SAl Viro 		res = nfs_do_filldir(desc);
913ece0b423STrond Myklebust 		if (res < 0)
9141da177e4SLinus Torvalds 			break;
91547c716cbSTrond Myklebust 	} while (!desc->eof);
916fccca7fcSTrond Myklebust out:
917565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
9181e7cb3dcSChuck Lever 	if (res > 0)
9191e7cb3dcSChuck Lever 		res = 0;
9206de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9211da177e4SLinus Torvalds 	return res;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
925f0dd2136STrond Myklebust {
9266de1472fSAl Viro 	struct inode *inode = file_inode(filp);
927480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
928b84e06c5SChuck Lever 
9296de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9306de1472fSAl Viro 			filp, offset, whence);
931b84e06c5SChuck Lever 
932b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
933965c8e59SAndrew Morton 	switch (whence) {
934f0dd2136STrond Myklebust 		case 1:
935f0dd2136STrond Myklebust 			offset += filp->f_pos;
936f0dd2136STrond Myklebust 		case 0:
937f0dd2136STrond Myklebust 			if (offset >= 0)
938f0dd2136STrond Myklebust 				break;
939f0dd2136STrond Myklebust 		default:
940f0dd2136STrond Myklebust 			offset = -EINVAL;
941f0dd2136STrond Myklebust 			goto out;
942f0dd2136STrond Myklebust 	}
943f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
944f0dd2136STrond Myklebust 		filp->f_pos = offset;
945480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9468ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
947f0dd2136STrond Myklebust 	}
948f0dd2136STrond Myklebust out:
949b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
950f0dd2136STrond Myklebust 	return offset;
951f0dd2136STrond Myklebust }
952f0dd2136STrond Myklebust 
9531da177e4SLinus Torvalds /*
9541da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9551da177e4SLinus Torvalds  * is a dummy operation.
9561da177e4SLinus Torvalds  */
95702c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
95802c24a82SJosef Bacik 			 int datasync)
9591da177e4SLinus Torvalds {
9606de1472fSAl Viro 	struct inode *inode = file_inode(filp);
9617ea80859SChristoph Hellwig 
9626de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
9631e7cb3dcSChuck Lever 
96402c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
9656de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
96602c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9671da177e4SLinus Torvalds 	return 0;
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
970bfc69a45STrond Myklebust /**
971bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
972bfc69a45STrond Myklebust  * @dir - pointer to directory inode
973bfc69a45STrond Myklebust  *
974bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
975bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
976bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
977bfc69a45STrond Myklebust  *
978bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
979bfc69a45STrond Myklebust  */
980bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
981bfc69a45STrond Myklebust {
982011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
983bfc69a45STrond Myklebust }
98489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
985bfc69a45STrond Myklebust 
9861da177e4SLinus Torvalds /*
9871da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9881da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9891da177e4SLinus Torvalds  * and may need to be looked up again.
990912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
9911da177e4SLinus Torvalds  */
992912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
993912a108dSNeilBrown 			      int rcu_walk)
9941da177e4SLinus Torvalds {
995912a108dSNeilBrown 	int ret;
996912a108dSNeilBrown 
9971da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9981da177e4SLinus Torvalds 		return 1;
9994eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
10004eec952eSTrond Myklebust 		return 0;
1001f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10026ecc5e8fSTrond Myklebust 		return 0;
1003f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1004912a108dSNeilBrown 	if (rcu_walk)
1005912a108dSNeilBrown 		ret = nfs_revalidate_inode_rcu(NFS_SERVER(dir), dir);
1006912a108dSNeilBrown 	else
1007912a108dSNeilBrown 		ret = nfs_revalidate_inode(NFS_SERVER(dir), dir);
1008912a108dSNeilBrown 	if (ret < 0)
1009f2c77f4eSTrond Myklebust 		return 0;
1010f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1011f2c77f4eSTrond Myklebust 		return 0;
1012f2c77f4eSTrond Myklebust 	return 1;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /*
1016a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1017a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1018a12802caSTrond Myklebust  */
1019fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1020a12802caSTrond Myklebust {
1021a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1022a12802caSTrond Myklebust 		return 0;
1023fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1024a12802caSTrond Myklebust }
1025a12802caSTrond Myklebust 
1026a12802caSTrond Myklebust /*
10271d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10281d6757fbSTrond Myklebust  *
10291d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10301d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10311d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10321d6757fbSTrond Myklebust  *
10331d6757fbSTrond Myklebust  */
103465a0c149STrond Myklebust static
1035fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
103865a0c149STrond Myklebust 	int ret;
10391da177e4SLinus Torvalds 
104036d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10414e99a1ffSTrond Myklebust 		return 0;
10421da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1043fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10441da177e4SLinus Torvalds 		goto out_force;
10451da177e4SLinus Torvalds 	/* This is an open(2) */
1046fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1047fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10481da177e4SLinus Torvalds 		goto out_force;
104965a0c149STrond Myklebust out:
105065a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10511da177e4SLinus Torvalds out_force:
10521fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
10531fa1e384SNeilBrown 		return -ECHILD;
105465a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
105565a0c149STrond Myklebust 	if (ret != 0)
105665a0c149STrond Myklebust 		return ret;
105765a0c149STrond Myklebust 	goto out;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds /*
10611da177e4SLinus Torvalds  * We judge how long we want to trust negative
10621da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10651da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1066912a108dSNeilBrown  *
1067912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1068912a108dSNeilBrown  * suggesting a reval is needed.
10691da177e4SLinus Torvalds  */
10701da177e4SLinus Torvalds static inline
10711da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1072fa3c56bbSAl Viro 		       unsigned int flags)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1075fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10761da177e4SLinus Torvalds 		return 0;
10774eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10784eec952eSTrond Myklebust 		return 1;
1079912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds /*
10831da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10841da177e4SLinus Torvalds  * and we should check whether we can really trust that
10851da177e4SLinus Torvalds  * lookup.
10861da177e4SLinus Torvalds  *
10871da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10881da177e4SLinus Torvalds  * we have an inode!
10891da177e4SLinus Torvalds  *
10901da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10911da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10921da177e4SLinus Torvalds  */
10930b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	struct inode *dir;
10961da177e4SLinus Torvalds 	struct inode *inode;
10971da177e4SLinus Torvalds 	struct dentry *parent;
1098e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1099e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
11001775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
11011da177e4SLinus Torvalds 	int error;
11021da177e4SLinus Torvalds 
1103d51ac1a8SNeilBrown 	if (flags & LOOKUP_RCU) {
110450d77739SNeilBrown 		parent = ACCESS_ONCE(dentry->d_parent);
1105d51ac1a8SNeilBrown 		dir = ACCESS_ONCE(parent->d_inode);
1106d51ac1a8SNeilBrown 		if (!dir)
110734286d66SNick Piggin 			return -ECHILD;
1108d51ac1a8SNeilBrown 	} else {
11091da177e4SLinus Torvalds 		parent = dget_parent(dentry);
11101da177e4SLinus Torvalds 		dir = parent->d_inode;
1111d51ac1a8SNeilBrown 	}
111291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
11131da177e4SLinus Torvalds 	inode = dentry->d_inode;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	if (!inode) {
1116912a108dSNeilBrown 		if (nfs_neg_need_reval(dir, dentry, flags)) {
1117d51ac1a8SNeilBrown 			if (flags & LOOKUP_RCU)
1118d51ac1a8SNeilBrown 				return -ECHILD;
11191da177e4SLinus Torvalds 			goto out_bad;
1120912a108dSNeilBrown 		}
1121d69ee9b8STrond Myklebust 		goto out_valid_noent;
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
1125d51ac1a8SNeilBrown 		if (flags & LOOKUP_RCU)
1126d51ac1a8SNeilBrown 			return -ECHILD;
11276de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
11286de1472fSAl Viro 				__func__, dentry);
11291da177e4SLinus Torvalds 		goto out_bad;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
1132011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
113315860ab1STrond Myklebust 		goto out_set_verifier;
113415860ab1STrond Myklebust 
1135912a108dSNeilBrown 	/* Force a full look up iff the parent directory has changed */
1136912a108dSNeilBrown 	if (!nfs_is_exclusive_create(dir, flags) &&
1137912a108dSNeilBrown 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1138912a108dSNeilBrown 
11391fa1e384SNeilBrown 		if (nfs_lookup_verify_inode(inode, flags)) {
1140d51ac1a8SNeilBrown 			if (flags & LOOKUP_RCU)
1141d51ac1a8SNeilBrown 				return -ECHILD;
11421da177e4SLinus Torvalds 			goto out_zap_parent;
11431fa1e384SNeilBrown 		}
11441da177e4SLinus Torvalds 		goto out_valid;
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
1147912a108dSNeilBrown 	if (flags & LOOKUP_RCU)
1148912a108dSNeilBrown 		return -ECHILD;
1149912a108dSNeilBrown 
11501da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11511da177e4SLinus Torvalds 		goto out_bad;
11521da177e4SLinus Torvalds 
1153e1fb4d05STrond Myklebust 	error = -ENOMEM;
1154e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1155e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1156e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1157e1fb4d05STrond Myklebust 		goto out_error;
1158e1fb4d05STrond Myklebust 
115914c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
116014c43f76SDavid Quigley 	if (IS_ERR(label))
116114c43f76SDavid Quigley 		goto out_error;
116214c43f76SDavid Quigley 
11636e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
11641775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
11656e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
11661da177e4SLinus Torvalds 	if (error)
11671da177e4SLinus Torvalds 		goto out_bad;
1168e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11691da177e4SLinus Torvalds 		goto out_bad;
1170e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11711da177e4SLinus Torvalds 		goto out_bad;
11721da177e4SLinus Torvalds 
1173aa9c2669SDavid Quigley 	nfs_setsecurity(inode, fattr, label);
1174aa9c2669SDavid Quigley 
1175e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1176e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
117714c43f76SDavid Quigley 	nfs4_label_free(label);
117814c43f76SDavid Quigley 
117915860ab1STrond Myklebust out_set_verifier:
1180cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11811da177e4SLinus Torvalds  out_valid:
1182d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1183d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1184d69ee9b8STrond Myklebust  out_valid_noent:
1185d51ac1a8SNeilBrown 	if (flags & LOOKUP_RCU) {
118650d77739SNeilBrown 		if (parent != ACCESS_ONCE(dentry->d_parent))
1187d51ac1a8SNeilBrown 			return -ECHILD;
1188d51ac1a8SNeilBrown 	} else
11891da177e4SLinus Torvalds 		dput(parent);
11906de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11916de1472fSAl Viro 			__func__, dentry);
11921da177e4SLinus Torvalds 	return 1;
11931da177e4SLinus Torvalds out_zap_parent:
11941da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11951da177e4SLinus Torvalds  out_bad:
1196d51ac1a8SNeilBrown 	WARN_ON(flags & LOOKUP_RCU);
1197c44600c9SAl Viro 	nfs_free_fattr(fattr);
1198c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
119914c43f76SDavid Quigley 	nfs4_label_free(label);
1200a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
12011da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
12021da177e4SLinus Torvalds 		/* Purge readdir caches. */
12031da177e4SLinus Torvalds 		nfs_zap_caches(inode);
1204a3f432bfSJ. Bruce Fields 		/*
1205a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1206a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1207a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1208a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1209a3f432bfSJ. Bruce Fields 		 */
1210a3f432bfSJ. Bruce Fields 		if (IS_ROOT(dentry))
1211d9e80b7dSAl Viro 			goto out_valid;
12121da177e4SLinus Torvalds 	}
12131da177e4SLinus Torvalds 	dput(parent);
12146de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
12156de1472fSAl Viro 			__func__, dentry);
12161da177e4SLinus Torvalds 	return 0;
1217e1fb4d05STrond Myklebust out_error:
1218d51ac1a8SNeilBrown 	WARN_ON(flags & LOOKUP_RCU);
1219e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1220e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
122114c43f76SDavid Quigley 	nfs4_label_free(label);
1222e1fb4d05STrond Myklebust 	dput(parent);
12236de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
12246de1472fSAl Viro 			__func__, dentry, error);
1225e1fb4d05STrond Myklebust 	return error;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds /*
1229ecf3d1f1SJeff Layton  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1230ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1231ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1232ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1233ecf3d1f1SJeff Layton  *
1234ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1235ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1236ecf3d1f1SJeff Layton  */
1237ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1238ecf3d1f1SJeff Layton {
1239ecf3d1f1SJeff Layton 	int error;
1240ecf3d1f1SJeff Layton 	struct inode *inode = dentry->d_inode;
1241ecf3d1f1SJeff Layton 
1242ecf3d1f1SJeff Layton 	/*
1243ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1244ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1245ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1246ecf3d1f1SJeff Layton 	 */
1247ecf3d1f1SJeff Layton 	if (!inode) {
12486de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
12496de1472fSAl Viro 				__func__, dentry);
1250ecf3d1f1SJeff Layton 		return 1;
1251ecf3d1f1SJeff Layton 	}
1252ecf3d1f1SJeff Layton 
1253ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
12546de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12556de1472fSAl Viro 				__func__, dentry);
1256ecf3d1f1SJeff Layton 		return 0;
1257ecf3d1f1SJeff Layton 	}
1258ecf3d1f1SJeff Layton 
1259ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1260ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1261ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1262ecf3d1f1SJeff Layton 	return !error;
1263ecf3d1f1SJeff Layton }
1264ecf3d1f1SJeff Layton 
1265ecf3d1f1SJeff Layton /*
12661da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
12671da177e4SLinus Torvalds  */
1268fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
12691da177e4SLinus Torvalds {
12706de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
12716de1472fSAl Viro 		dentry, dentry->d_flags);
12721da177e4SLinus Torvalds 
127377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
127477f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
127577f11192STrond Myklebust 		return 1;
127677f11192STrond Myklebust 
12771da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12781da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12791da177e4SLinus Torvalds 		return 1;
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12821da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12831da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12841da177e4SLinus Torvalds 		return 1;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 	return 0;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
12901f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12911b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12921b83d707STrond Myklebust {
12931b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12941f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12951f018458STrond Myklebust 	if (inode->i_nlink == 1)
12961f018458STrond Myklebust 		clear_nlink(inode);
12971f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12981b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12991b83d707STrond Myklebust }
13001b83d707STrond Myklebust 
13011da177e4SLinus Torvalds /*
13021da177e4SLinus Torvalds  * Called when the dentry loses inode.
13031da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
13041da177e4SLinus Torvalds  */
13051da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
13061da177e4SLinus Torvalds {
130783672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
130883672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
130983672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
131083672d39SNeil Brown 
13111da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1312e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
13131f018458STrond Myklebust 		nfs_drop_nlink(inode);
13141da177e4SLinus Torvalds 	}
13151da177e4SLinus Torvalds 	iput(inode);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
1318b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1319b1942c5fSAl Viro {
1320b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1321b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1322b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1323b1942c5fSAl Viro 			WARN_ON(1);
1324b1942c5fSAl Viro 		else
1325b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1326b1942c5fSAl Viro 	}
1327b1942c5fSAl Viro }
1328b1942c5fSAl Viro 
1329f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
13301da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1331ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
13321da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13331da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
133436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1335b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13361da177e4SLinus Torvalds };
1337ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
13381da177e4SLinus Torvalds 
1339597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
13401da177e4SLinus Torvalds {
13411da177e4SLinus Torvalds 	struct dentry *res;
1342565277f6STrond Myklebust 	struct dentry *parent;
13431da177e4SLinus Torvalds 	struct inode *inode = NULL;
1344e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1345e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
13461775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
13471da177e4SLinus Torvalds 	int error;
13481da177e4SLinus Torvalds 
13496de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
135091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
13531da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
13541da177e4SLinus Torvalds 		goto out;
13551da177e4SLinus Torvalds 
1356fd684071STrond Myklebust 	/*
1357fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1358fd684071STrond Myklebust 	 * but don't hash the dentry.
1359fd684071STrond Myklebust 	 */
136000cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1361fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1362fd684071STrond Myklebust 		res = NULL;
1363fc0f684cSTrond Myklebust 		goto out;
1364fd684071STrond Myklebust 	}
13651da177e4SLinus Torvalds 
1366e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1367e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1368e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1369e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1370e1fb4d05STrond Myklebust 		goto out;
1371e1fb4d05STrond Myklebust 
137214c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
137314c43f76SDavid Quigley 	if (IS_ERR(label))
137414c43f76SDavid Quigley 		goto out;
137514c43f76SDavid Quigley 
1376565277f6STrond Myklebust 	parent = dentry->d_parent;
1377565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
13786e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1379565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
13801775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
13811da177e4SLinus Torvalds 	if (error == -ENOENT)
13821da177e4SLinus Torvalds 		goto no_entry;
13831da177e4SLinus Torvalds 	if (error < 0) {
13841da177e4SLinus Torvalds 		res = ERR_PTR(error);
1385565277f6STrond Myklebust 		goto out_unblock_sillyrename;
13861da177e4SLinus Torvalds 	}
13871775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1388bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
138903f28e3aSTrond Myklebust 	if (IS_ERR(res))
1390565277f6STrond Myklebust 		goto out_unblock_sillyrename;
139154ceac45SDavid Howells 
1392d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1393d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1394d69ee9b8STrond Myklebust 
13951da177e4SLinus Torvalds no_entry:
139654ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13979eaef27bSTrond Myklebust 	if (res != NULL) {
13989eaef27bSTrond Myklebust 		if (IS_ERR(res))
1399565277f6STrond Myklebust 			goto out_unblock_sillyrename;
14001da177e4SLinus Torvalds 		dentry = res;
14019eaef27bSTrond Myklebust 	}
14021da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1403565277f6STrond Myklebust out_unblock_sillyrename:
1404565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
14056e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
140614c43f76SDavid Quigley 	nfs4_label_free(label);
14071da177e4SLinus Torvalds out:
1408e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1409e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
14101da177e4SLinus Torvalds 	return res;
14111da177e4SLinus Torvalds }
1412ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
14131da177e4SLinus Torvalds 
141489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
14150b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
14161da177e4SLinus Torvalds 
1417f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
14180ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
14191da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
14201da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
142136d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1422b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14231da177e4SLinus Torvalds };
142489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
14251da177e4SLinus Torvalds 
14268a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
14278a5e929dSAl Viro {
14288a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
14298a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
14308a5e929dSAl Viro 		res |= FMODE_READ;
14318a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
14328a5e929dSAl Viro 		res |= FMODE_WRITE;
14338a5e929dSAl Viro 	return res;
14348a5e929dSAl Viro }
14358a5e929dSAl Viro 
143651141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1437cd9a1c0eSTrond Myklebust {
14385ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1439cd9a1c0eSTrond Myklebust }
1440cd9a1c0eSTrond Myklebust 
1441cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1442cd9a1c0eSTrond Myklebust {
1443f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1444cd9a1c0eSTrond Myklebust 	return 0;
1445cd9a1c0eSTrond Myklebust }
1446cd9a1c0eSTrond Myklebust 
1447d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
14480dd2b474SMiklos Szeredi 			   struct dentry *dentry,
144930d90494SAl Viro 			   struct file *file, unsigned open_flags,
145047237687SAl Viro 			   int *opened)
1451cd9a1c0eSTrond Myklebust {
14520dd2b474SMiklos Szeredi 	int err;
14530dd2b474SMiklos Szeredi 
145401c919abSMiklos Szeredi 	if ((open_flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
145501c919abSMiklos Szeredi 		*opened |= FILE_CREATED;
145601c919abSMiklos Szeredi 
145730d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
145830d90494SAl Viro 	if (err)
1459d9585277SAl Viro 		goto out;
146030d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
14610dd2b474SMiklos Szeredi 
1462cd9a1c0eSTrond Myklebust out:
1463d9585277SAl Viro 	return err;
1464cd9a1c0eSTrond Myklebust }
1465cd9a1c0eSTrond Myklebust 
146673a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
146730d90494SAl Viro 		    struct file *file, unsigned open_flags,
146847237687SAl Viro 		    umode_t mode, int *opened)
14691da177e4SLinus Torvalds {
1470cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
14710dd2b474SMiklos Szeredi 	struct dentry *res;
14720dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1473f46e0bd3STrond Myklebust 	struct inode *inode;
14741472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1475898f635cSTrond Myklebust 	int err;
14761da177e4SLinus Torvalds 
14770dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14780dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
14790dd2b474SMiklos Szeredi 
14801e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
14816de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
14821e7cb3dcSChuck Lever 
14839597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
14849597c13bSJeff Layton 	if (err)
14859597c13bSJeff Layton 		return err;
14869597c13bSJeff Layton 
14870dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14880dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14890dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14900dd2b474SMiklos Szeredi 			/*
14910dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14920dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14930dd2b474SMiklos Szeredi 			 * again
14940dd2b474SMiklos Szeredi 			 */
1495d9585277SAl Viro 			return -ENOENT;
14960dd2b474SMiklos Szeredi 		}
14971472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
14981da177e4SLinus Torvalds 		goto no_open;
14991da177e4SLinus Torvalds 	}
15001da177e4SLinus Torvalds 
15010dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1502d9585277SAl Viro 		return -ENAMETOOLONG;
15031da177e4SLinus Torvalds 
15040dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1505536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
15060dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
15070dd2b474SMiklos Szeredi 	}
1508536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1509536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1510536e43d1STrond Myklebust 		attr.ia_size = 0;
1511cd9a1c0eSTrond Myklebust 	}
1512cd9a1c0eSTrond Myklebust 
15130dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
15140dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
15150dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1516d9585277SAl Viro 		goto out;
15170dd2b474SMiklos Szeredi 
15186e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
1519f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
15205bc2afc2STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1521f46e0bd3STrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1522275bb307STrond Myklebust 	if (IS_ERR(inode)) {
15230dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
15246e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15252d9db750STrond Myklebust 		put_nfs_open_context(ctx);
15260dd2b474SMiklos Szeredi 		switch (err) {
15271da177e4SLinus Torvalds 		case -ENOENT:
1528275bb307STrond Myklebust 			d_drop(dentry);
1529f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
15300dd2b474SMiklos Szeredi 			break;
15311788ea6eSJeff Layton 		case -EISDIR:
15326f926b5bSTrond Myklebust 		case -ENOTDIR:
15336f926b5bSTrond Myklebust 			goto no_open;
15341da177e4SLinus Torvalds 		case -ELOOP:
15350dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
15361da177e4SLinus Torvalds 				goto no_open;
15370dd2b474SMiklos Szeredi 			break;
15381da177e4SLinus Torvalds 			/* case -EINVAL: */
15391da177e4SLinus Torvalds 		default:
15400dd2b474SMiklos Szeredi 			break;
15411da177e4SLinus Torvalds 		}
15421da177e4SLinus Torvalds 		goto out;
15431da177e4SLinus Torvalds 	}
15440dd2b474SMiklos Szeredi 
1545275bb307STrond Myklebust 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
15466e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15472d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1548d9585277SAl Viro out:
1549d9585277SAl Viro 	return err;
15500dd2b474SMiklos Szeredi 
15511da177e4SLinus Torvalds no_open:
15521472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
15530dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
15540dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1555d9585277SAl Viro 		goto out;
15560dd2b474SMiklos Szeredi 
1557e45198a6SAl Viro 	return finish_no_open(file, res);
15581da177e4SLinus Torvalds }
155989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
15601da177e4SLinus Torvalds 
15610b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
15621da177e4SLinus Torvalds {
1563657e94b6SNick Piggin 	struct inode *inode;
156450de348cSMiklos Szeredi 	int ret = 0;
15651da177e4SLinus Torvalds 
1566fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1567eda72afbSMiklos Szeredi 		goto no_open;
1568eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15695584c306STrond Myklebust 		goto no_open;
157049f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
157149f9a0faSTrond Myklebust 		goto no_open;
15722b484297STrond Myklebust 
1573eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
15742b484297STrond Myklebust 
15751da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15761da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15771da177e4SLinus Torvalds 	 */
1578216d5d06STrond Myklebust 	if (inode == NULL) {
157949317a7fSNeilBrown 		struct dentry *parent;
158049317a7fSNeilBrown 		struct inode *dir;
158149317a7fSNeilBrown 
1582912a108dSNeilBrown 		if (flags & LOOKUP_RCU) {
158350d77739SNeilBrown 			parent = ACCESS_ONCE(dentry->d_parent);
1584912a108dSNeilBrown 			dir = ACCESS_ONCE(parent->d_inode);
1585912a108dSNeilBrown 			if (!dir)
1586d51ac1a8SNeilBrown 				return -ECHILD;
1587912a108dSNeilBrown 		} else {
158849317a7fSNeilBrown 			parent = dget_parent(dentry);
158949317a7fSNeilBrown 			dir = parent->d_inode;
1590912a108dSNeilBrown 		}
1591fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1592216d5d06STrond Myklebust 			ret = 1;
1593912a108dSNeilBrown 		else if (flags & LOOKUP_RCU)
1594912a108dSNeilBrown 			ret = -ECHILD;
1595912a108dSNeilBrown 		if (!(flags & LOOKUP_RCU))
159649317a7fSNeilBrown 			dput(parent);
159750d77739SNeilBrown 		else if (parent != ACCESS_ONCE(dentry->d_parent))
1598912a108dSNeilBrown 			return -ECHILD;
15991da177e4SLinus Torvalds 		goto out;
1600216d5d06STrond Myklebust 	}
1601216d5d06STrond Myklebust 
16021da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
16031da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
160449317a7fSNeilBrown 		goto no_open;
16051da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1606fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
160749317a7fSNeilBrown 		goto no_open;
16081da177e4SLinus Torvalds 
16090ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1610898f635cSTrond Myklebust 	ret = 1;
16110ef97dcfSMiklos Szeredi 
16121da177e4SLinus Torvalds out:
16131da177e4SLinus Torvalds 	return ret;
1614535918f1STrond Myklebust 
16155584c306STrond Myklebust no_open:
16160b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1617c0204fd2STrond Myklebust }
1618c0204fd2STrond Myklebust 
16191da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds /*
16221da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
16231da177e4SLinus Torvalds  */
16241da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
16251775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
16261775fd3eSDavid Quigley 				struct nfs4_label *label)
16271da177e4SLinus Torvalds {
1628fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1629fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
16301da177e4SLinus Torvalds 	struct inode *inode;
16311da177e4SLinus Torvalds 	int error = -EACCES;
16321da177e4SLinus Torvalds 
1633fab728e1STrond Myklebust 	d_drop(dentry);
1634fab728e1STrond Myklebust 
16351da177e4SLinus Torvalds 	/* We may have been initialized further down */
16361da177e4SLinus Torvalds 	if (dentry->d_inode)
1637fab728e1STrond Myklebust 		goto out;
16381da177e4SLinus Torvalds 	if (fhandle->size == 0) {
16391775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
16401da177e4SLinus Torvalds 		if (error)
1641fab728e1STrond Myklebust 			goto out_error;
16421da177e4SLinus Torvalds 	}
16435724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16441da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
16451da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
16461775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
16471da177e4SLinus Torvalds 		if (error < 0)
1648fab728e1STrond Myklebust 			goto out_error;
16491da177e4SLinus Torvalds 	}
16501775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
165103f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
165203f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1653fab728e1STrond Myklebust 		goto out_error;
1654fab728e1STrond Myklebust 	d_add(dentry, inode);
1655fab728e1STrond Myklebust out:
1656fab728e1STrond Myklebust 	dput(parent);
16571da177e4SLinus Torvalds 	return 0;
1658fab728e1STrond Myklebust out_error:
1659fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1660fab728e1STrond Myklebust 	dput(parent);
1661fab728e1STrond Myklebust 	return error;
16621da177e4SLinus Torvalds }
1663ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds /*
16661da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
16671da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
16681da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
16691da177e4SLinus Torvalds  * reply path made it appear to have failed.
16701da177e4SLinus Torvalds  */
1671597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1672ebfc3b49SAl Viro 		umode_t mode, bool excl)
16731da177e4SLinus Torvalds {
16741da177e4SLinus Torvalds 	struct iattr attr;
1675ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
16761da177e4SLinus Torvalds 	int error;
16771da177e4SLinus Torvalds 
16781e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
16796de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	attr.ia_mode = mode;
16821da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16831da177e4SLinus Torvalds 
16848b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
16858867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16868b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
16871da177e4SLinus Torvalds 	if (error != 0)
16881da177e4SLinus Torvalds 		goto out_err;
16891da177e4SLinus Torvalds 	return 0;
16901da177e4SLinus Torvalds out_err:
16911da177e4SLinus Torvalds 	d_drop(dentry);
16921da177e4SLinus Torvalds 	return error;
16931da177e4SLinus Torvalds }
1694ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds /*
16971da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16981da177e4SLinus Torvalds  */
1699597d9289SBryan Schumaker int
17001a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
17011da177e4SLinus Torvalds {
17021da177e4SLinus Torvalds 	struct iattr attr;
17031da177e4SLinus Torvalds 	int status;
17041da177e4SLinus Torvalds 
17051e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
17066de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
17091da177e4SLinus Torvalds 		return -EINVAL;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	attr.ia_mode = mode;
17121da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17131da177e4SLinus Torvalds 
17141ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
17151da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
17161ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
17171da177e4SLinus Torvalds 	if (status != 0)
17181da177e4SLinus Torvalds 		goto out_err;
17191da177e4SLinus Torvalds 	return 0;
17201da177e4SLinus Torvalds out_err:
17211da177e4SLinus Torvalds 	d_drop(dentry);
17221da177e4SLinus Torvalds 	return status;
17231da177e4SLinus Torvalds }
1724ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds /*
17271da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17281da177e4SLinus Torvalds  */
1729597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17301da177e4SLinus Torvalds {
17311da177e4SLinus Torvalds 	struct iattr attr;
17321da177e4SLinus Torvalds 	int error;
17331da177e4SLinus Torvalds 
17341e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
17356de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17381da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
17391da177e4SLinus Torvalds 
17401ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
17411da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
17421ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
17431da177e4SLinus Torvalds 	if (error != 0)
17441da177e4SLinus Torvalds 		goto out_err;
17451da177e4SLinus Torvalds 	return 0;
17461da177e4SLinus Torvalds out_err:
17471da177e4SLinus Torvalds 	d_drop(dentry);
17481da177e4SLinus Torvalds 	return error;
17491da177e4SLinus Torvalds }
1750ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
17511da177e4SLinus Torvalds 
1752d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1753d45b9d8bSTrond Myklebust {
1754d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1755d45b9d8bSTrond Myklebust 		d_delete(dentry);
1756d45b9d8bSTrond Myklebust }
1757d45b9d8bSTrond Myklebust 
1758597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
17591da177e4SLinus Torvalds {
17601da177e4SLinus Torvalds 	int error;
17611da177e4SLinus Torvalds 
17621e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
17636de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17641da177e4SLinus Torvalds 
17651ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
1766ba6c0592STrond Myklebust 	if (dentry->d_inode) {
1767ba6c0592STrond Myklebust 		nfs_wait_on_sillyrename(dentry);
17681da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17691da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1770ba6c0592STrond Myklebust 		switch (error) {
1771ba6c0592STrond Myklebust 		case 0:
1772ce71ec36SDave Hansen 			clear_nlink(dentry->d_inode);
1773ba6c0592STrond Myklebust 			break;
1774ba6c0592STrond Myklebust 		case -ENOENT:
1775d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1776ba6c0592STrond Myklebust 		}
1777ba6c0592STrond Myklebust 	} else
1778ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17791ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	return error;
17821da177e4SLinus Torvalds }
1783ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds /*
17861da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17871da177e4SLinus Torvalds  * and after checking that the file has only one user.
17881da177e4SLinus Torvalds  *
17891da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17901da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17911da177e4SLinus Torvalds  */
17921da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17931da177e4SLinus Torvalds {
17941da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17951da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17961da177e4SLinus Torvalds 	int error = -EBUSY;
17971da177e4SLinus Torvalds 
17986de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
18011da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
18021da177e4SLinus Torvalds 		error = 0;
18031da177e4SLinus Torvalds 		goto out;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 
18061ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
18071da177e4SLinus Torvalds 	if (inode != NULL) {
180857ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
18091da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
18101da177e4SLinus Torvalds 		if (error == 0)
18111b83d707STrond Myklebust 			nfs_drop_nlink(inode);
18121da177e4SLinus Torvalds 	} else
18131da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1814d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1815d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
18161ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
18171da177e4SLinus Torvalds out:
18181da177e4SLinus Torvalds 	return error;
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
18221da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
18231da177e4SLinus Torvalds  *
18241da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
18251da177e4SLinus Torvalds  */
1826597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
18271da177e4SLinus Torvalds {
18281da177e4SLinus Torvalds 	int error;
18291da177e4SLinus Torvalds 	int need_rehash = 0;
18301da177e4SLinus Torvalds 
18311e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
18326de1472fSAl Viro 		dir->i_ino, dentry);
18331da177e4SLinus Torvalds 
18341ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
18351da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
183684d08fa8SAl Viro 	if (d_count(dentry) > 1) {
18371da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1838ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1839ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
18401da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
18411ca42382STrond Myklebust 		goto out;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
18441da177e4SLinus Torvalds 		__d_drop(dentry);
18451da177e4SLinus Torvalds 		need_rehash = 1;
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
18481da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1849d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
18501da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18511da177e4SLinus Torvalds 	} else if (need_rehash)
18521da177e4SLinus Torvalds 		d_rehash(dentry);
18531ca42382STrond Myklebust out:
18541ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
18551da177e4SLinus Torvalds 	return error;
18561da177e4SLinus Torvalds }
1857ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
18581da177e4SLinus Torvalds 
1859873101b3SChuck Lever /*
1860873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1861873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1862873101b3SChuck Lever  * using prepare_write/commit_write.
1863873101b3SChuck Lever  *
1864873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1865873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1866873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1867873101b3SChuck Lever  * symlink request has completed on the server.
1868873101b3SChuck Lever  *
1869873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1870873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1871873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1872873101b3SChuck Lever  * and move the raw page into its mapping.
1873873101b3SChuck Lever  */
1874597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18751da177e4SLinus Torvalds {
1876873101b3SChuck Lever 	struct page *page;
1877873101b3SChuck Lever 	char *kaddr;
18781da177e4SLinus Torvalds 	struct iattr attr;
1879873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
18801da177e4SLinus Torvalds 	int error;
18811da177e4SLinus Torvalds 
18821e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
18836de1472fSAl Viro 		dir->i_ino, dentry, symname);
18841da177e4SLinus Torvalds 
1885873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1886873101b3SChuck Lever 		return -ENAMETOOLONG;
18871da177e4SLinus Torvalds 
1888873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1889873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18901da177e4SLinus Torvalds 
189183d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
189276566991STrond Myklebust 	if (!page)
1893873101b3SChuck Lever 		return -ENOMEM;
1894873101b3SChuck Lever 
18952b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1896873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1897873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1898873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
18992b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1900873101b3SChuck Lever 
19011ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
190294a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
19031ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1904873101b3SChuck Lever 	if (error != 0) {
19051e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1906873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
19076de1472fSAl Viro 			dentry, symname, error);
19081da177e4SLinus Torvalds 		d_drop(dentry);
1909873101b3SChuck Lever 		__free_page(page);
19101da177e4SLinus Torvalds 		return error;
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 
1913873101b3SChuck Lever 	/*
1914873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1915873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1916873101b3SChuck Lever 	 */
1917a0b8cab3SMel Gorman 	if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
1918873101b3SChuck Lever 							GFP_KERNEL)) {
1919873101b3SChuck Lever 		SetPageUptodate(page);
1920873101b3SChuck Lever 		unlock_page(page);
1921a0b54addSRafael Aquini 		/*
1922a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
1923a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
1924a0b54addSRafael Aquini 		 */
1925a0b54addSRafael Aquini 		page_cache_release(page);
1926873101b3SChuck Lever 	} else
1927873101b3SChuck Lever 		__free_page(page);
1928873101b3SChuck Lever 
1929873101b3SChuck Lever 	return 0;
1930873101b3SChuck Lever }
1931ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1932873101b3SChuck Lever 
1933597d9289SBryan Schumaker int
19341da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
19351da177e4SLinus Torvalds {
19361da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
19371da177e4SLinus Torvalds 	int error;
19381da177e4SLinus Torvalds 
19396de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
19406de1472fSAl Viro 		old_dentry, dentry);
19411da177e4SLinus Torvalds 
19421fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
194357ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
19449a3936aaSTrond Myklebust 
19459697d234STrond Myklebust 	d_drop(dentry);
19461da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1947cf809556STrond Myklebust 	if (error == 0) {
19487de9c6eeSAl Viro 		ihold(inode);
19499697d234STrond Myklebust 		d_add(dentry, inode);
1950cf809556STrond Myklebust 	}
19511fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
19521da177e4SLinus Torvalds 	return error;
19531da177e4SLinus Torvalds }
1954ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds /*
19571da177e4SLinus Torvalds  * RENAME
19581da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
19591da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
19601da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
19611da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
19621da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
19631da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
19641da177e4SLinus Torvalds  *
19651da177e4SLinus Torvalds  * FIXED.
19661da177e4SLinus Torvalds  *
19671da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
19681da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
19691da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
19701da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
19711da177e4SLinus Torvalds  * using the inode layer
19721da177e4SLinus Torvalds  *
19731da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
19741da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
19751da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
19761da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
19771da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
19781da177e4SLinus Torvalds  * the rename.
19791da177e4SLinus Torvalds  */
1980597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
19811da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
19821da177e4SLinus Torvalds {
19831da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
19841da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
19851da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
198680a491fdSJeff Layton 	struct rpc_task *task;
19871da177e4SLinus Torvalds 	int error = -EBUSY;
19881da177e4SLinus Torvalds 
19896de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
19906de1472fSAl Viro 		 old_dentry, new_dentry,
199184d08fa8SAl Viro 		 d_count(new_dentry));
19921da177e4SLinus Torvalds 
199370ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
19941da177e4SLinus Torvalds 	/*
199528f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
199628f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
199728f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
199828f79a1aSMiklos Szeredi 	 * the new target.
19991da177e4SLinus Torvalds 	 */
200027226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
200127226104SMiklos Szeredi 		/*
200227226104SMiklos Szeredi 		 * To prevent any new references to the target during the
200327226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
200427226104SMiklos Szeredi 		 */
200527226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
200627226104SMiklos Szeredi 			d_drop(new_dentry);
200727226104SMiklos Szeredi 			rehash = new_dentry;
200827226104SMiklos Szeredi 		}
200927226104SMiklos Szeredi 
201084d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
20111da177e4SLinus Torvalds 			int err;
201227226104SMiklos Szeredi 
20131da177e4SLinus Torvalds 			/* copy the target dentry's name */
20141da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
20151da177e4SLinus Torvalds 					 &new_dentry->d_name);
20161da177e4SLinus Torvalds 			if (!dentry)
20171da177e4SLinus Torvalds 				goto out;
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
20201da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
202124e93025SMiklos Szeredi 			if (err)
20221da177e4SLinus Torvalds 				goto out;
202324e93025SMiklos Szeredi 
202424e93025SMiklos Szeredi 			new_dentry = dentry;
202556335936SOGAWA Hirofumi 			rehash = NULL;
202624e93025SMiklos Szeredi 			new_inode = NULL;
2027b1e4adf4STrond Myklebust 		}
202827226104SMiklos Szeredi 	}
20291da177e4SLinus Torvalds 
203057ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
2031b1e4adf4STrond Myklebust 	if (new_inode != NULL)
203257ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
20331da177e4SLinus Torvalds 
203480a491fdSJeff Layton 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
203580a491fdSJeff Layton 	if (IS_ERR(task)) {
203680a491fdSJeff Layton 		error = PTR_ERR(task);
203780a491fdSJeff Layton 		goto out;
203880a491fdSJeff Layton 	}
203980a491fdSJeff Layton 
204080a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
204180a491fdSJeff Layton 	if (error == 0)
204280a491fdSJeff Layton 		error = task->tk_status;
204380a491fdSJeff Layton 	rpc_put_task(task);
20445ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
20451da177e4SLinus Torvalds out:
20461da177e4SLinus Torvalds 	if (rehash)
20471da177e4SLinus Torvalds 		d_rehash(rehash);
204870ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
204970ded201STrond Myklebust 			new_dir, new_dentry, error);
20501da177e4SLinus Torvalds 	if (!error) {
2051b1e4adf4STrond Myklebust 		if (new_inode != NULL)
2052b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
20531da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
20548fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
20558fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
2056d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
2057d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	/* new dentry created? */
20601da177e4SLinus Torvalds 	if (dentry)
20611da177e4SLinus Torvalds 		dput(dentry);
20621da177e4SLinus Torvalds 	return error;
20631da177e4SLinus Torvalds }
2064ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
20651da177e4SLinus Torvalds 
2066cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2067cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2068cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2069cfcea3e8STrond Myklebust 
20703a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
20713a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
20723a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
20733a505845STrond Myklebust 
20741c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
20751c3c07e9STrond Myklebust {
20761c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
2077f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
20784e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2079cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
20804e857c58SPeter Zijlstra 	smp_mb__after_atomic();
20811c3c07e9STrond Myklebust }
20821c3c07e9STrond Myklebust 
20831a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
20841a81bb8aSTrond Myklebust {
20851a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
20861a81bb8aSTrond Myklebust 
20871a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
20881a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
20891a81bb8aSTrond Myklebust 		list_del(&cache->lru);
20901a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
20911a81bb8aSTrond Myklebust 	}
20921a81bb8aSTrond Myklebust }
20931a81bb8aSTrond Myklebust 
20943a505845STrond Myklebust static unsigned long
20953a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2096979df72eSTrond Myklebust {
2097979df72eSTrond Myklebust 	LIST_HEAD(head);
2098aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2099979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
21001ab6c499SDave Chinner 	long freed = 0;
2101979df72eSTrond Myklebust 
2102a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2103aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2104979df72eSTrond Myklebust 		struct inode *inode;
2105979df72eSTrond Myklebust 
2106979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2107979df72eSTrond Myklebust 			break;
21089c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2109979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2110979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2111979df72eSTrond Myklebust 			goto remove_lru_entry;
2112979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2113979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2114979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2115979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
21161ab6c499SDave Chinner 		freed++;
2117979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2118979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2119979df72eSTrond Myklebust 					&nfs_access_lru_list);
2120979df72eSTrond Myklebust 		else {
2121979df72eSTrond Myklebust remove_lru_entry:
2122979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
21234e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2124979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
21254e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2126979df72eSTrond Myklebust 		}
212759844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2128979df72eSTrond Myklebust 	}
2129979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
21301a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
21311ab6c499SDave Chinner 	return freed;
21321ab6c499SDave Chinner }
21331ab6c499SDave Chinner 
21341ab6c499SDave Chinner unsigned long
21353a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
21363a505845STrond Myklebust {
21373a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
21383a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
21393a505845STrond Myklebust 
21403a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
21413a505845STrond Myklebust 		return SHRINK_STOP;
21423a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
21433a505845STrond Myklebust }
21443a505845STrond Myklebust 
21453a505845STrond Myklebust 
21463a505845STrond Myklebust unsigned long
21471ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
21481ab6c499SDave Chinner {
214955f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2150979df72eSTrond Myklebust }
2151979df72eSTrond Myklebust 
21523a505845STrond Myklebust static void
21533a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
21543a505845STrond Myklebust {
21553a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
21563a505845STrond Myklebust 	unsigned long diff;
21573a505845STrond Myklebust 	unsigned int nr_to_scan;
21583a505845STrond Myklebust 
21593a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
21603a505845STrond Myklebust 		return;
21613a505845STrond Myklebust 	nr_to_scan = 100;
21623a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
21633a505845STrond Myklebust 	if (diff < nr_to_scan)
21643a505845STrond Myklebust 		nr_to_scan = diff;
21653a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
21663a505845STrond Myklebust }
21673a505845STrond Myklebust 
21681a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
21691c3c07e9STrond Myklebust {
21701c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21711a81bb8aSTrond Myklebust 	struct rb_node *n;
21721c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21731c3c07e9STrond Myklebust 
21741c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
21751c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
21761c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
21771c3c07e9STrond Myklebust 		rb_erase(n, root_node);
21781a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
21791c3c07e9STrond Myklebust 	}
21801c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
21811c3c07e9STrond Myklebust }
21821c3c07e9STrond Myklebust 
21831c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
21841c3c07e9STrond Myklebust {
21851a81bb8aSTrond Myklebust 	LIST_HEAD(head);
21861a81bb8aSTrond Myklebust 
21871a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
21881a81bb8aSTrond Myklebust 		return;
2189cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2190cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
21911a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2192cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2193cfcea3e8STrond Myklebust 
21941c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21951a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
21961a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21971a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
21981a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
21991c3c07e9STrond Myklebust }
22001c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
22011c3c07e9STrond Myklebust 
22021c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
22031c3c07e9STrond Myklebust {
22041c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
22051c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
22061c3c07e9STrond Myklebust 
22071c3c07e9STrond Myklebust 	while (n != NULL) {
22081c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
22091c3c07e9STrond Myklebust 
22101c3c07e9STrond Myklebust 		if (cred < entry->cred)
22111c3c07e9STrond Myklebust 			n = n->rb_left;
22121c3c07e9STrond Myklebust 		else if (cred > entry->cred)
22131c3c07e9STrond Myklebust 			n = n->rb_right;
22141c3c07e9STrond Myklebust 		else
22151c3c07e9STrond Myklebust 			return entry;
22161c3c07e9STrond Myklebust 	}
22171c3c07e9STrond Myklebust 	return NULL;
22181c3c07e9STrond Myklebust }
22191c3c07e9STrond Myklebust 
2220af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
22211da177e4SLinus Torvalds {
222255296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
22231c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
22241c3c07e9STrond Myklebust 	int err = -ENOENT;
22251da177e4SLinus Torvalds 
22261c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
22271c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
22281c3c07e9STrond Myklebust 		goto out_zap;
22291c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
22301c3c07e9STrond Myklebust 	if (cache == NULL)
22311c3c07e9STrond Myklebust 		goto out;
2232b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
223364672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
22341c3c07e9STrond Myklebust 		goto out_stale;
22351c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
22361c3c07e9STrond Myklebust 	res->cred = cache->cred;
22371c3c07e9STrond Myklebust 	res->mask = cache->mask;
2238cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
22391c3c07e9STrond Myklebust 	err = 0;
22401c3c07e9STrond Myklebust out:
22411c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
22421c3c07e9STrond Myklebust 	return err;
22431c3c07e9STrond Myklebust out_stale:
22441c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2245cfcea3e8STrond Myklebust 	list_del(&cache->lru);
22461c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
22471c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
22481da177e4SLinus Torvalds 	return -ENOENT;
22491c3c07e9STrond Myklebust out_zap:
22501a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
22511a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
22521c3c07e9STrond Myklebust 	return -ENOENT;
22531c3c07e9STrond Myklebust }
22541c3c07e9STrond Myklebust 
2255f682a398SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2256f682a398SNeilBrown {
2257f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2258f682a398SNeilBrown 	 * but do it without locking.
2259f682a398SNeilBrown 	 */
2260f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2261f682a398SNeilBrown 	struct nfs_access_entry *cache;
2262f682a398SNeilBrown 	int err = -ECHILD;
2263f682a398SNeilBrown 	struct list_head *lh;
2264f682a398SNeilBrown 
2265f682a398SNeilBrown 	rcu_read_lock();
2266f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2267f682a398SNeilBrown 		goto out;
2268f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2269f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2270f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2271f682a398SNeilBrown 	    cred != cache->cred)
2272f682a398SNeilBrown 		cache = NULL;
2273f682a398SNeilBrown 	if (cache == NULL)
2274f682a398SNeilBrown 		goto out;
2275f682a398SNeilBrown 	if (!nfs_have_delegated_attributes(inode) &&
2276f682a398SNeilBrown 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
2277f682a398SNeilBrown 		goto out;
2278f682a398SNeilBrown 	res->jiffies = cache->jiffies;
2279f682a398SNeilBrown 	res->cred = cache->cred;
2280f682a398SNeilBrown 	res->mask = cache->mask;
2281f682a398SNeilBrown 	err = 0;
2282f682a398SNeilBrown out:
2283f682a398SNeilBrown 	rcu_read_unlock();
2284f682a398SNeilBrown 	return err;
2285f682a398SNeilBrown }
2286f682a398SNeilBrown 
22871c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
22881c3c07e9STrond Myklebust {
2289cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2290cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
22911c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
22921c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
22931c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
22941c3c07e9STrond Myklebust 
22951c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
22961c3c07e9STrond Myklebust 	while (*p != NULL) {
22971c3c07e9STrond Myklebust 		parent = *p;
22981c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
22991c3c07e9STrond Myklebust 
23001c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
23011c3c07e9STrond Myklebust 			p = &parent->rb_left;
23021c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
23031c3c07e9STrond Myklebust 			p = &parent->rb_right;
23041c3c07e9STrond Myklebust 		else
23051c3c07e9STrond Myklebust 			goto found;
23061c3c07e9STrond Myklebust 	}
23071c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
23081c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2309cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
23101c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23111c3c07e9STrond Myklebust 	return;
23121c3c07e9STrond Myklebust found:
23131c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2314cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2315cfcea3e8STrond Myklebust 	list_del(&entry->lru);
23161c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23171c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
23206168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
23211da177e4SLinus Torvalds {
23221c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
23231c3c07e9STrond Myklebust 	if (cache == NULL)
23241c3c07e9STrond Myklebust 		return;
23251c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
23261da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
23271c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
23281da177e4SLinus Torvalds 	cache->mask = set->mask;
23291c3c07e9STrond Myklebust 
2330f682a398SNeilBrown 	/* The above field assignments must be visible
2331f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2332f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2333f682a398SNeilBrown 	 */
2334f682a398SNeilBrown 	smp_wmb();
23351c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2336cfcea3e8STrond Myklebust 
2337cfcea3e8STrond Myklebust 	/* Update accounting */
23384e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2339cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
23404e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2341cfcea3e8STrond Myklebust 
2342cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
23431a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2344cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
23451a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
23461a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
23471a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2348cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2349cfcea3e8STrond Myklebust 	}
23503a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
23511da177e4SLinus Torvalds }
23526168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
23536168f62cSWeston Andros Adamson 
23546168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
23556168f62cSWeston Andros Adamson {
23566168f62cSWeston Andros Adamson 	entry->mask = 0;
23576168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
23586168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
23596168f62cSWeston Andros Adamson 	if (access_result &
23606168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
23616168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
23626168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
23636168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
23646168f62cSWeston Andros Adamson }
23656168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
23681da177e4SLinus Torvalds {
23691da177e4SLinus Torvalds 	struct nfs_access_entry cache;
23701da177e4SLinus Torvalds 	int status;
23711da177e4SLinus Torvalds 
2372f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2373f4ce1299STrond Myklebust 
2374f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2375f682a398SNeilBrown 	if (status != 0)
23761da177e4SLinus Torvalds 		status = nfs_access_get_cached(inode, cred, &cache);
23771da177e4SLinus Torvalds 	if (status == 0)
2378f4ce1299STrond Myklebust 		goto out_cached;
23791da177e4SLinus Torvalds 
2380f3324a2aSNeilBrown 	status = -ECHILD;
2381f3324a2aSNeilBrown 	if (mask & MAY_NOT_BLOCK)
2382f3324a2aSNeilBrown 		goto out;
2383f3324a2aSNeilBrown 
23841da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
23851da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
23861da177e4SLinus Torvalds 	cache.cred = cred;
23871da177e4SLinus Torvalds 	cache.jiffies = jiffies;
23881da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2389a71ee337SSuresh Jayaraman 	if (status != 0) {
2390a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2391a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2392a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2393a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2394a71ee337SSuresh Jayaraman 		}
2395f4ce1299STrond Myklebust 		goto out;
2396a71ee337SSuresh Jayaraman 	}
23971da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2398f4ce1299STrond Myklebust out_cached:
2399f4ce1299STrond Myklebust 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2400f4ce1299STrond Myklebust 		status = -EACCES;
24011da177e4SLinus Torvalds out:
2402f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2403f4ce1299STrond Myklebust 	return status;
24041da177e4SLinus Torvalds }
24051da177e4SLinus Torvalds 
2406af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2407af22f94aSTrond Myklebust {
2408af22f94aSTrond Myklebust 	int mask = 0;
2409af22f94aSTrond Myklebust 
2410f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2411f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2412f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2413f8d9a897SWeston Andros Adamson 	} else {
24148a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2415af22f94aSTrond Myklebust 			mask |= MAY_READ;
24168a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2417af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2418f8d9a897SWeston Andros Adamson 	}
2419f8d9a897SWeston Andros Adamson 
2420af22f94aSTrond Myklebust 	return mask;
2421af22f94aSTrond Myklebust }
2422af22f94aSTrond Myklebust 
2423af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2424af22f94aSTrond Myklebust {
2425af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2426af22f94aSTrond Myklebust }
242789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2428af22f94aSTrond Myklebust 
242910556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
24301da177e4SLinus Torvalds {
24311da177e4SLinus Torvalds 	struct rpc_cred *cred;
24321da177e4SLinus Torvalds 	int res = 0;
24331da177e4SLinus Torvalds 
243491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
243591d5b470SChuck Lever 
2436e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
24371da177e4SLinus Torvalds 		goto out;
24381da177e4SLinus Torvalds 	/* Is this sys_access() ? */
24399cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
24401da177e4SLinus Torvalds 		goto force_lookup;
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
24431da177e4SLinus Torvalds 		case S_IFLNK:
24441da177e4SLinus Torvalds 			goto out;
24451da177e4SLinus Torvalds 		case S_IFREG:
24461da177e4SLinus Torvalds 			break;
24471da177e4SLinus Torvalds 		case S_IFDIR:
24481da177e4SLinus Torvalds 			/*
24491da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
24501da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
24511da177e4SLinus Torvalds 			 */
24521da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
24531da177e4SLinus Torvalds 				goto out;
24541da177e4SLinus Torvalds 	}
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds force_lookup:
24571da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
24581da177e4SLinus Torvalds 		goto out_notsup;
24591da177e4SLinus Torvalds 
2460f3324a2aSNeilBrown 	/* Always try fast lookups first */
2461f3324a2aSNeilBrown 	rcu_read_lock();
2462f3324a2aSNeilBrown 	cred = rpc_lookup_cred_nonblock();
2463f3324a2aSNeilBrown 	if (!IS_ERR(cred))
2464f3324a2aSNeilBrown 		res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2465f3324a2aSNeilBrown 	else
2466f3324a2aSNeilBrown 		res = PTR_ERR(cred);
2467f3324a2aSNeilBrown 	rcu_read_unlock();
2468f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2469f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
247098a8e323STrond Myklebust 		cred = rpc_lookup_cred();
24711da177e4SLinus Torvalds 		if (!IS_ERR(cred)) {
24721da177e4SLinus Torvalds 			res = nfs_do_access(inode, cred, mask);
24731da177e4SLinus Torvalds 			put_rpccred(cred);
24741da177e4SLinus Torvalds 		} else
24751da177e4SLinus Torvalds 			res = PTR_ERR(cred);
2476f3324a2aSNeilBrown 	}
24771da177e4SLinus Torvalds out:
2478f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2479f696a365SMiklos Szeredi 		res = -EACCES;
2480f696a365SMiklos Szeredi 
24811e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
24821e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
24831da177e4SLinus Torvalds 	return res;
24841da177e4SLinus Torvalds out_notsup:
2485d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2486d51ac1a8SNeilBrown 		return -ECHILD;
2487d51ac1a8SNeilBrown 
24881da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
24891da177e4SLinus Torvalds 	if (res == 0)
24902830ba7fSAl Viro 		res = generic_permission(inode, mask);
24911e7cb3dcSChuck Lever 	goto out;
24921da177e4SLinus Torvalds }
2493ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds /*
24961da177e4SLinus Torvalds  * Local variables:
24971da177e4SLinus Torvalds  *  version-control: t
24981da177e4SLinus Torvalds  *  kept-new-versions: 5
24991da177e4SLinus Torvalds  * End:
25001da177e4SLinus Torvalds  */
2501