xref: /openbmc/linux/fs/nfs/dir.c (revision 972bcdf233096d36b2f3e02f34a80d0f073b6b05)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  nfs directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
101da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
111da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
121da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
131da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
141da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
151da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
161da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
171da177e4SLinus Torvalds  *              layer (iput() and friends).
181da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21ddda8e0aSBryan Schumaker #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
311da177e4SLinus Torvalds #include <linux/nfs_fs.h>
321da177e4SLinus Torvalds #include <linux/nfs_mount.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
34873101b3SChuck Lever #include <linux/pagevec.h>
351da177e4SLinus Torvalds #include <linux/namei.h>
3654ceac45SDavid Howells #include <linux/mount.h>
37a0b8cab3SMel Gorman #include <linux/swap.h>
38e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "delegation.h"
4391d5b470SChuck Lever #include "iostat.h"
444c30d56eSAdrian Bunk #include "internal.h"
45cd9a1c0eSTrond Myklebust #include "fscache.h"
461da177e4SLinus Torvalds 
47f4ce1299STrond Myklebust #include "nfstrace.h"
48f4ce1299STrond Myklebust 
491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
571da177e4SLinus Torvalds 
584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
59f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
601da177e4SLinus Torvalds 	.read		= generic_read_dir,
6193a6ab7bSTrond Myklebust 	.iterate_shared	= nfs_readdir,
621da177e4SLinus Torvalds 	.open		= nfs_opendir,
63480c2006SBryan Schumaker 	.release	= nfs_closedir,
641da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
69d1bacf9eSBryan Schumaker };
70d1bacf9eSBryan Schumaker 
71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
72480c2006SBryan Schumaker {
73311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
74480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
75480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76480c2006SBryan Schumaker 	if (ctx != NULL) {
778ef2ce3eSBryan Schumaker 		ctx->duped = 0;
78311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
79480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
808ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
81684f39b4SNeilBrown 		ctx->cred = get_cred(cred);
82311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
831c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
841c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
851c341b77STrond Myklebust 			nfsi->cache_validity |= NFS_INO_INVALID_DATA |
861c341b77STrond Myklebust 				NFS_INO_REVAL_FORCED;
87311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
88311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
89480c2006SBryan Schumaker 		return ctx;
90480c2006SBryan Schumaker 	}
910c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
920c030806STrond Myklebust }
93480c2006SBryan Schumaker 
94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
95480c2006SBryan Schumaker {
96311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
97311324adSTrond Myklebust 	list_del(&ctx->list);
98311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
99684f39b4SNeilBrown 	put_cred(ctx->cred);
100480c2006SBryan Schumaker 	kfree(ctx);
101480c2006SBryan Schumaker }
102480c2006SBryan Schumaker 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * Open file
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static int
1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1081da177e4SLinus Torvalds {
109480c2006SBryan Schumaker 	int res = 0;
110480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1111da177e4SLinus Torvalds 
1126de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
113cc0dd2d1SChuck Lever 
114cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1151e7cb3dcSChuck Lever 
116684f39b4SNeilBrown 	ctx = alloc_nfs_open_dir_context(inode, current_cred());
117480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
118480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
119480c2006SBryan Schumaker 		goto out;
120480c2006SBryan Schumaker 	}
121480c2006SBryan Schumaker 	filp->private_data = ctx;
122480c2006SBryan Schumaker out:
1231da177e4SLinus Torvalds 	return res;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126480c2006SBryan Schumaker static int
127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
128480c2006SBryan Schumaker {
129a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
130480c2006SBryan Schumaker 	return 0;
131480c2006SBryan Schumaker }
132480c2006SBryan Schumaker 
133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
134d1bacf9eSBryan Schumaker 	u64 cookie;
135d1bacf9eSBryan Schumaker 	u64 ino;
136d1bacf9eSBryan Schumaker 	struct qstr string;
1370b26a0bfSTrond Myklebust 	unsigned char d_type;
138d1bacf9eSBryan Schumaker };
139d1bacf9eSBryan Schumaker 
140d1bacf9eSBryan Schumaker struct nfs_cache_array {
141d1bacf9eSBryan Schumaker 	u64 last_cookie;
142b1e21c97STrond Myklebust 	unsigned int size;
143b1e21c97STrond Myklebust 	unsigned char page_full : 1,
144b1e21c97STrond Myklebust 		      page_is_eof : 1;
1455601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
146d1bacf9eSBryan Schumaker };
147d1bacf9eSBryan Schumaker 
1482e7a4641STrond Myklebust typedef struct nfs_readdir_descriptor {
1491da177e4SLinus Torvalds 	struct file	*file;
1501da177e4SLinus Torvalds 	struct page	*page;
15123db8620SAl Viro 	struct dir_context *ctx;
1521da177e4SLinus Torvalds 	unsigned long	page_index;
1532e7a4641STrond Myklebust 	u64		dir_cookie;
1540aded708STrond Myklebust 	u64		last_cookie;
1552e7a4641STrond Myklebust 	u64		dup_cookie;
156f0dd2136STrond Myklebust 	loff_t		current_index;
15759e356a9STrond Myklebust 	loff_t		prev_index;
158d1bacf9eSBryan Schumaker 
159a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1601f4eab7eSNeil Brown 	unsigned long	timestamp;
1614704f0e2STrond Myklebust 	unsigned long	gencount;
1622e7a4641STrond Myklebust 	unsigned long	attr_gencount;
163d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
1642e7a4641STrond Myklebust 	signed char duped;
165a7a3b1e9SBenjamin Coddington 	bool plus;
166a7a3b1e9SBenjamin Coddington 	bool eof;
1671da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1681da177e4SLinus Torvalds 
1694b310319STrond Myklebust static
1704b310319STrond Myklebust void nfs_readdir_init_array(struct page *page)
1714b310319STrond Myklebust {
1724b310319STrond Myklebust 	struct nfs_cache_array *array;
1734b310319STrond Myklebust 
1744b310319STrond Myklebust 	array = kmap_atomic(page);
1754b310319STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
1764b310319STrond Myklebust 	kunmap_atomic(array);
1774b310319STrond Myklebust }
1784b310319STrond Myklebust 
179d1bacf9eSBryan Schumaker /*
180d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
181d1bacf9eSBryan Schumaker  */
182d1bacf9eSBryan Schumaker static
18311de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
184d1bacf9eSBryan Schumaker {
18511de3b11STrond Myklebust 	struct nfs_cache_array *array;
186d1bacf9eSBryan Schumaker 	int i;
1878cd51a0cSTrond Myklebust 
1882b86ce2dSCong Wang 	array = kmap_atomic(page);
189d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
190d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1914b310319STrond Myklebust 	array->size = 0;
1922b86ce2dSCong Wang 	kunmap_atomic(array);
193d1bacf9eSBryan Schumaker }
194d1bacf9eSBryan Schumaker 
195b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
196b1e21c97STrond Myklebust {
197b1e21c97STrond Myklebust 	array->page_is_eof = 1;
198b1e21c97STrond Myklebust 	array->page_full = 1;
199b1e21c97STrond Myklebust }
200b1e21c97STrond Myklebust 
201b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
202b1e21c97STrond Myklebust {
203b1e21c97STrond Myklebust 	return array->page_full;
204b1e21c97STrond Myklebust }
205b1e21c97STrond Myklebust 
206d1bacf9eSBryan Schumaker /*
207d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
208d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
209d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
210d1bacf9eSBryan Schumaker  */
211d1bacf9eSBryan Schumaker static
2124a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
213d1bacf9eSBryan Schumaker {
214d1bacf9eSBryan Schumaker 	string->len = len;
2153803d672STrond Myklebust 	string->name = kmemdup_nul(name, len, GFP_KERNEL);
2164a201d6eSTrond Myklebust 	if (string->name == NULL)
2174a201d6eSTrond Myklebust 		return -ENOMEM;
21804e4bd1cSCatalin Marinas 	/*
21904e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
22004e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
22104e4bd1cSCatalin Marinas 	 */
22204e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2238387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2244a201d6eSTrond Myklebust 	return 0;
225d1bacf9eSBryan Schumaker }
226d1bacf9eSBryan Schumaker 
227b1e21c97STrond Myklebust /*
228b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
229b1e21c97STrond Myklebust  */
230b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
231b1e21c97STrond Myklebust {
232b1e21c97STrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
233b1e21c97STrond Myklebust 
234b1e21c97STrond Myklebust 	if (array->page_full)
235b1e21c97STrond Myklebust 		return -ENOSPC;
236b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size + 1];
237b1e21c97STrond Myklebust 	if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
238b1e21c97STrond Myklebust 		array->page_full = 1;
239b1e21c97STrond Myklebust 		return -ENOSPC;
240b1e21c97STrond Myklebust 	}
241b1e21c97STrond Myklebust 	return 0;
242b1e21c97STrond Myklebust }
243b1e21c97STrond Myklebust 
244d1bacf9eSBryan Schumaker static
245d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
246d1bacf9eSBryan Schumaker {
2470795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2484a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2494a201d6eSTrond Myklebust 	int ret;
2504a201d6eSTrond Myklebust 
251b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
252b1e21c97STrond Myklebust 	if (ret)
2533020093fSTrond Myklebust 		goto out;
2543020093fSTrond Myklebust 
255b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
2564a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2574a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2580b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2594a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2604a201d6eSTrond Myklebust 	if (ret)
2614a201d6eSTrond Myklebust 		goto out;
262d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2638cd51a0cSTrond Myklebust 	array->size++;
26447c716cbSTrond Myklebust 	if (entry->eof != 0)
265b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
2664a201d6eSTrond Myklebust out:
2670795bf83SFabian Frederick 	kunmap(page);
2684a201d6eSTrond Myklebust 	return ret;
269d1bacf9eSBryan Schumaker }
270d1bacf9eSBryan Schumaker 
271b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
272b1e21c97STrond Myklebust {
273b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
274b1e21c97STrond Myklebust 
275b1e21c97STrond Myklebust 	array = kmap_atomic(page);
276b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
277b1e21c97STrond Myklebust 	kunmap_atomic(array);
278b1e21c97STrond Myklebust }
279b1e21c97STrond Myklebust 
28059e356a9STrond Myklebust static inline
28159e356a9STrond Myklebust int is_32bit_api(void)
28259e356a9STrond Myklebust {
28359e356a9STrond Myklebust #ifdef CONFIG_COMPAT
28459e356a9STrond Myklebust 	return in_compat_syscall();
28559e356a9STrond Myklebust #else
28659e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
28759e356a9STrond Myklebust #endif
28859e356a9STrond Myklebust }
28959e356a9STrond Myklebust 
29059e356a9STrond Myklebust static
29159e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
29259e356a9STrond Myklebust {
29359e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
29459e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
29559e356a9STrond Myklebust 		return false;
29659e356a9STrond Myklebust 	return true;
29759e356a9STrond Myklebust }
29859e356a9STrond Myklebust 
299d1bacf9eSBryan Schumaker static
300d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
301d1bacf9eSBryan Schumaker {
30223db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
303d1bacf9eSBryan Schumaker 	unsigned int index;
304d1bacf9eSBryan Schumaker 
305d1bacf9eSBryan Schumaker 	if (diff < 0)
306d1bacf9eSBryan Schumaker 		goto out_eof;
307d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
308b1e21c97STrond Myklebust 		if (array->page_is_eof)
309d1bacf9eSBryan Schumaker 			goto out_eof;
310d1bacf9eSBryan Schumaker 		return -EAGAIN;
311d1bacf9eSBryan Schumaker 	}
312d1bacf9eSBryan Schumaker 
313d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
3142e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
315d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
316d1bacf9eSBryan Schumaker 	return 0;
317d1bacf9eSBryan Schumaker out_eof:
3186089dd0dSThomas Meyer 	desc->eof = true;
319d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
320d1bacf9eSBryan Schumaker }
321d1bacf9eSBryan Schumaker 
3224db72b40SJeff Layton static bool
3234db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
3244db72b40SJeff Layton {
3254db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
3264db72b40SJeff Layton 		return false;
3274db72b40SJeff Layton 	smp_rmb();
3284db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
3294db72b40SJeff Layton }
3304db72b40SJeff Layton 
331d1bacf9eSBryan Schumaker static
332d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
333d1bacf9eSBryan Schumaker {
334d1bacf9eSBryan Schumaker 	int i;
3358ef2ce3eSBryan Schumaker 	loff_t new_pos;
336d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
337d1bacf9eSBryan Schumaker 
338d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3392e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
340496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
3410c030806STrond Myklebust 
3428ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3432e7a4641STrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount ||
3444db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
3452e7a4641STrond Myklebust 				desc->duped = 0;
3462e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
34759e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
3482e7a4641STrond Myklebust 				if (desc->duped > 0
3492e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
3500c030806STrond Myklebust 					if (printk_ratelimit()) {
3516de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
3520c030806STrond Myklebust 								"Please contact your server vendor.  "
3539581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
3549581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
3552e7a4641STrond Myklebust 								array->array[i].string.name, desc->dir_cookie);
3560c030806STrond Myklebust 					}
3570c030806STrond Myklebust 					status = -ELOOP;
3580c030806STrond Myklebust 					goto out;
3590c030806STrond Myklebust 				}
3602e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
3612e7a4641STrond Myklebust 				desc->duped = -1;
3628ef2ce3eSBryan Schumaker 			}
36359e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
3642e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
36559e356a9STrond Myklebust 			else
36623db8620SAl Viro 				desc->ctx->pos = new_pos;
36759e356a9STrond Myklebust 			desc->prev_index = new_pos;
3688cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
36947c716cbSTrond Myklebust 			return 0;
3708cd51a0cSTrond Myklebust 		}
3718cd51a0cSTrond Myklebust 	}
372b1e21c97STrond Myklebust 	if (array->page_is_eof) {
373d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
3742e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
3756089dd0dSThomas Meyer 			desc->eof = true;
376d1bacf9eSBryan Schumaker 	}
3770c030806STrond Myklebust out:
378d1bacf9eSBryan Schumaker 	return status;
379d1bacf9eSBryan Schumaker }
380d1bacf9eSBryan Schumaker 
381d1bacf9eSBryan Schumaker static
382d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
383d1bacf9eSBryan Schumaker {
384d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
38547c716cbSTrond Myklebust 	int status;
386d1bacf9eSBryan Schumaker 
3870795bf83SFabian Frederick 	array = kmap(desc->page);
388d1bacf9eSBryan Schumaker 
3892e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
390d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
391d1bacf9eSBryan Schumaker 	else
392d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
393d1bacf9eSBryan Schumaker 
39447c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3950aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
396e47c085aSTrond Myklebust 		desc->current_index += array->size;
39747c716cbSTrond Myklebust 		desc->page_index++;
39847c716cbSTrond Myklebust 	}
3990795bf83SFabian Frederick 	kunmap(desc->page);
400d1bacf9eSBryan Schumaker 	return status;
401d1bacf9eSBryan Schumaker }
402d1bacf9eSBryan Schumaker 
403d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
404d1bacf9eSBryan Schumaker static
40556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
406d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
407d1bacf9eSBryan Schumaker {
408480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
409684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
4104704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
4111da177e4SLinus Torvalds 	int		error;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds  again:
4141da177e4SLinus Torvalds 	timestamp = jiffies;
4154704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
416a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
417be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
4181da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
4191da177e4SLinus Torvalds 	if (error < 0) {
4201da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
4211da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
4221da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
4233a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
424a7a3b1e9SBenjamin Coddington 			desc->plus = false;
4251da177e4SLinus Torvalds 			goto again;
4261da177e4SLinus Torvalds 		}
4271da177e4SLinus Torvalds 		goto error;
4281da177e4SLinus Torvalds 	}
4291f4eab7eSNeil Brown 	desc->timestamp = timestamp;
4304704f0e2STrond Myklebust 	desc->gencount = gencount;
431d1bacf9eSBryan Schumaker error:
432d1bacf9eSBryan Schumaker 	return error;
433d1bacf9eSBryan Schumaker }
434d1bacf9eSBryan Schumaker 
435573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
436573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
437d1bacf9eSBryan Schumaker {
43859e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
439573c4e1eSChuck Lever 	int error;
440d1bacf9eSBryan Schumaker 
44159e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
442573c4e1eSChuck Lever 	if (error)
443573c4e1eSChuck Lever 		return error;
444d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
445d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
446d1bacf9eSBryan Schumaker 	return 0;
447d1bacf9eSBryan Schumaker }
448d1bacf9eSBryan Schumaker 
449fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
450fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
451fa923369STrond Myklebust  */
452d39ab9deSBryan Schumaker static
453d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
454d39ab9deSBryan Schumaker {
455d8fdb47fSTrond Myklebust 	struct inode *inode;
456fa923369STrond Myklebust 	struct nfs_inode *nfsi;
457fa923369STrond Myklebust 
4582b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
4592b0143b5SDavid Howells 		return 0;
460fa923369STrond Myklebust 
461d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
462d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
463d8fdb47fSTrond Myklebust 		return 0;
464d8fdb47fSTrond Myklebust 
465d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
4667dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
467d39ab9deSBryan Schumaker 		return 0;
4687dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
4697dc72d5fSTrond Myklebust 		return 0;
4707dc72d5fSTrond Myklebust 	return 1;
471d39ab9deSBryan Schumaker }
472d39ab9deSBryan Schumaker 
473d39ab9deSBryan Schumaker static
47423db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
475d69ee9b8STrond Myklebust {
476d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
477d69ee9b8STrond Myklebust 		return false;
478d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
479d69ee9b8STrond Myklebust 		return true;
48023db8620SAl Viro 	if (ctx->pos == 0)
481d69ee9b8STrond Myklebust 		return true;
482d69ee9b8STrond Myklebust 	return false;
483d69ee9b8STrond Myklebust }
484d69ee9b8STrond Myklebust 
485d69ee9b8STrond Myklebust /*
48663519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
48763519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
488d69ee9b8STrond Myklebust  * directory.
489d69ee9b8STrond Myklebust  */
490d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
491d69ee9b8STrond Myklebust {
49263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
49363519fbcSTrond Myklebust 
49463519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
49563519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
49663519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
497d69ee9b8STrond Myklebust }
498d69ee9b8STrond Myklebust 
499311324adSTrond Myklebust /*
500311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
501311324adSTrond Myklebust  *
502311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
503311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
504311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
505311324adSTrond Myklebust  * cache flush.
506311324adSTrond Myklebust  */
507311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
508311324adSTrond Myklebust {
50963519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
51063519fbcSTrond Myklebust 
51163519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
51263519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
51363519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
514227823d2SDai Ngo 		invalidate_mapping_pages(dir->i_mapping,
515227823d2SDai Ngo 			nfsi->page_index + 1, -1);
516311324adSTrond Myklebust 	}
517311324adSTrond Myklebust }
518311324adSTrond Myklebust 
519d69ee9b8STrond Myklebust static
520a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
521a1147b82STrond Myklebust 		unsigned long dir_verifier)
522d39ab9deSBryan Schumaker {
52326fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
5249ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
5254a201d6eSTrond Myklebust 	struct dentry *dentry;
5264a201d6eSTrond Myklebust 	struct dentry *alias;
527d39ab9deSBryan Schumaker 	struct inode *inode;
528aa9c2669SDavid Quigley 	int status;
529d39ab9deSBryan Schumaker 
530fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
531fa923369STrond Myklebust 		return;
5326c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
5336c441c25STrond Myklebust 		return;
53478d04af4STrond Myklebust 	if (filename.len == 0)
53578d04af4STrond Myklebust 		return;
53678d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
53778d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
53878d04af4STrond Myklebust 		return;
53978d04af4STrond Myklebust 	/* ...or '/' */
54078d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
54178d04af4STrond Myklebust 		return;
5424a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
5434a201d6eSTrond Myklebust 		if (filename.len == 1)
5444a201d6eSTrond Myklebust 			return;
5454a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
5464a201d6eSTrond Myklebust 			return;
5474a201d6eSTrond Myklebust 	}
5488387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
549d39ab9deSBryan Schumaker 
5504a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
5519ac3d3e8SAl Viro again:
5529ac3d3e8SAl Viro 	if (!dentry) {
5539ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
5549ac3d3e8SAl Viro 		if (IS_ERR(dentry))
5559ac3d3e8SAl Viro 			return;
5569ac3d3e8SAl Viro 	}
5579ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
5586c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
5596c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
5606c441c25STrond Myklebust 					&entry->fattr->fsid))
5616c441c25STrond Myklebust 			goto out;
562d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
5637dc72d5fSTrond Myklebust 			if (!entry->fh->size)
5647dc72d5fSTrond Myklebust 				goto out;
565a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
5662b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
567aa9c2669SDavid Quigley 			if (!status)
5682b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
569d39ab9deSBryan Schumaker 			goto out;
570d39ab9deSBryan Schumaker 		} else {
5715542aa2fSEric W. Biederman 			d_invalidate(dentry);
572d39ab9deSBryan Schumaker 			dput(dentry);
5739ac3d3e8SAl Viro 			dentry = NULL;
5749ac3d3e8SAl Viro 			goto again;
575d39ab9deSBryan Schumaker 		}
576d39ab9deSBryan Schumaker 	}
5777dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5787dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5797dc72d5fSTrond Myklebust 		goto out;
5807dc72d5fSTrond Myklebust 	}
581d39ab9deSBryan Schumaker 
5821775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
58341d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5849ac3d3e8SAl Viro 	d_lookup_done(dentry);
5859ac3d3e8SAl Viro 	if (alias) {
586d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
587d39ab9deSBryan Schumaker 			goto out;
5889ac3d3e8SAl Viro 		dput(dentry);
5899ac3d3e8SAl Viro 		dentry = alias;
5909ac3d3e8SAl Viro 	}
591a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
592d39ab9deSBryan Schumaker out:
593d39ab9deSBryan Schumaker 	dput(dentry);
594d39ab9deSBryan Schumaker }
595d39ab9deSBryan Schumaker 
596d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
597d1bacf9eSBryan Schumaker static
5988cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5996650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
600d1bacf9eSBryan Schumaker {
601babddc72SBryan Schumaker 	struct xdr_stream stream;
602f7da7a12SBenny Halevy 	struct xdr_buf buf;
6036650239aSTrond Myklebust 	struct page *scratch;
6045c346854STrond Myklebust 	int status;
605babddc72SBryan Schumaker 
6066650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
6076650239aSTrond Myklebust 	if (scratch == NULL)
6086650239aSTrond Myklebust 		return -ENOMEM;
609babddc72SBryan Schumaker 
610f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
6116650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
61299424380SBryan Schumaker 
61399424380SBryan Schumaker 	do {
614d33030e2SJeffrey Mitchell 		if (entry->label)
615d33030e2SJeffrey Mitchell 			entry->label->len = NFS4_MAXLABELLEN;
616d33030e2SJeffrey Mitchell 
61799424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
618*972bcdf2STrond Myklebust 		if (status != 0)
61999424380SBryan Schumaker 			break;
6205c346854STrond Myklebust 
621a7a3b1e9SBenjamin Coddington 		if (desc->plus)
622a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
623a1147b82STrond Myklebust 					desc->dir_verifier);
6248cd51a0cSTrond Myklebust 
625db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
626*972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
62799424380SBryan Schumaker 
628*972bcdf2STrond Myklebust 	switch (status) {
629*972bcdf2STrond Myklebust 	case -EBADCOOKIE:
630*972bcdf2STrond Myklebust 		if (entry->eof) {
631b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
63299424380SBryan Schumaker 			status = 0;
63356e4ebf8SBryan Schumaker 		}
634*972bcdf2STrond Myklebust 		break;
635*972bcdf2STrond Myklebust 	case -ENOSPC:
636*972bcdf2STrond Myklebust 	case -EAGAIN:
637*972bcdf2STrond Myklebust 		status = 0;
638*972bcdf2STrond Myklebust 		break;
639*972bcdf2STrond Myklebust 	}
6406650239aSTrond Myklebust 
6416650239aSTrond Myklebust 	put_page(scratch);
6428cd51a0cSTrond Myklebust 	return status;
6438cd51a0cSTrond Myklebust }
64456e4ebf8SBryan Schumaker 
64556e4ebf8SBryan Schumaker static
646c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
64756e4ebf8SBryan Schumaker {
64856e4ebf8SBryan Schumaker 	unsigned int i;
64956e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
65056e4ebf8SBryan Schumaker 		put_page(pages[i]);
65156e4ebf8SBryan Schumaker }
65256e4ebf8SBryan Schumaker 
65356e4ebf8SBryan Schumaker /*
654bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
655bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
65656e4ebf8SBryan Schumaker  */
65756e4ebf8SBryan Schumaker static
658c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
65956e4ebf8SBryan Schumaker {
66056e4ebf8SBryan Schumaker 	unsigned int i;
66156e4ebf8SBryan Schumaker 
66256e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
66356e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
66456e4ebf8SBryan Schumaker 		if (page == NULL)
66556e4ebf8SBryan Schumaker 			goto out_freepages;
66656e4ebf8SBryan Schumaker 		pages[i] = page;
66756e4ebf8SBryan Schumaker 	}
6686650239aSTrond Myklebust 	return 0;
66956e4ebf8SBryan Schumaker 
67056e4ebf8SBryan Schumaker out_freepages:
671c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6726650239aSTrond Myklebust 	return -ENOMEM;
673d1bacf9eSBryan Schumaker }
674d1bacf9eSBryan Schumaker 
675d1bacf9eSBryan Schumaker static
676d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
677d1bacf9eSBryan Schumaker {
67856e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
679d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
680d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
681d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6828cd51a0cSTrond Myklebust 	int status = -ENOMEM;
68356e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
684d1bacf9eSBryan Schumaker 
6854b310319STrond Myklebust 	nfs_readdir_init_array(page);
6864b310319STrond Myklebust 
687d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6880aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
689d1bacf9eSBryan Schumaker 	entry.eof = 0;
690d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
691d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
692573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
693d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
694d1bacf9eSBryan Schumaker 		goto out;
695d1bacf9eSBryan Schumaker 
69614c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
69714c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
69814c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
69914c43f76SDavid Quigley 		goto out;
70014c43f76SDavid Quigley 	}
70114c43f76SDavid Quigley 
7020795bf83SFabian Frederick 	array = kmap(page);
703d1bacf9eSBryan Schumaker 
704c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
7056650239aSTrond Myklebust 	if (status < 0)
706d1bacf9eSBryan Schumaker 		goto out_release_array;
707d1bacf9eSBryan Schumaker 	do {
708ac396128STrond Myklebust 		unsigned int pglen;
70956e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
710babddc72SBryan Schumaker 
711d1bacf9eSBryan Schumaker 		if (status < 0)
712d1bacf9eSBryan Schumaker 			break;
713*972bcdf2STrond Myklebust 
714ac396128STrond Myklebust 		pglen = status;
715*972bcdf2STrond Myklebust 		if (pglen == 0) {
716*972bcdf2STrond Myklebust 			nfs_readdir_page_set_eof(page);
7178cd51a0cSTrond Myklebust 			break;
7188cd51a0cSTrond Myklebust 		}
719*972bcdf2STrond Myklebust 
720*972bcdf2STrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
721*972bcdf2STrond Myklebust 	} while (!status && !nfs_readdir_array_is_full(array));
722d1bacf9eSBryan Schumaker 
723c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
724d1bacf9eSBryan Schumaker out_release_array:
7250795bf83SFabian Frederick 	kunmap(page);
72614c43f76SDavid Quigley 	nfs4_label_free(entry.label);
727d1bacf9eSBryan Schumaker out:
728d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
729d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
730d1bacf9eSBryan Schumaker 	return status;
731d1bacf9eSBryan Schumaker }
732d1bacf9eSBryan Schumaker 
733d1bacf9eSBryan Schumaker /*
734d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
735d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
736d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
737d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
7381da177e4SLinus Torvalds  */
739d1bacf9eSBryan Schumaker static
740a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page)
741d1bacf9eSBryan Schumaker {
742a46126ccSChristoph Hellwig 	nfs_readdir_descriptor_t *desc = data;
743496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
7448cd51a0cSTrond Myklebust 	int ret;
745d1bacf9eSBryan Schumaker 
7468cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
7478cd51a0cSTrond Myklebust 	if (ret < 0)
748d1bacf9eSBryan Schumaker 		goto error;
749d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
750d1bacf9eSBryan Schumaker 
7512aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
752cd9ae2b6STrond Myklebust 		/* Should never happen */
753cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
754cd9ae2b6STrond Myklebust 	}
7551da177e4SLinus Torvalds 	unlock_page(page);
7561da177e4SLinus Torvalds 	return 0;
7571da177e4SLinus Torvalds  error:
7584b310319STrond Myklebust 	nfs_readdir_clear_array(page);
7591da177e4SLinus Torvalds 	unlock_page(page);
7608cd51a0cSTrond Myklebust 	return ret;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
763d1bacf9eSBryan Schumaker static
764d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7651da177e4SLinus Torvalds {
76609cbfeafSKirill A. Shutemov 	put_page(desc->page);
7671da177e4SLinus Torvalds 	desc->page = NULL;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
770d1bacf9eSBryan Schumaker static
771d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7721da177e4SLinus Torvalds {
773a46126ccSChristoph Hellwig 	return read_cache_page(desc->file->f_mapping, desc->page_index,
774a46126ccSChristoph Hellwig 			nfs_readdir_filler, desc);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds /*
778d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
779114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
7801da177e4SLinus Torvalds  */
781d1bacf9eSBryan Schumaker static
782114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
783d1bacf9eSBryan Schumaker {
784227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
785227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
786d1bacf9eSBryan Schumaker 	int res;
787d1bacf9eSBryan Schumaker 
788d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
789d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
790d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
791114de382STrond Myklebust 	res = lock_page_killable(desc->page);
79247c716cbSTrond Myklebust 	if (res != 0)
793114de382STrond Myklebust 		goto error;
794114de382STrond Myklebust 	res = -EAGAIN;
795114de382STrond Myklebust 	if (desc->page->mapping != NULL) {
796114de382STrond Myklebust 		res = nfs_readdir_search_array(desc);
797227823d2SDai Ngo 		if (res == 0) {
798227823d2SDai Ngo 			nfsi->page_index = desc->page_index;
799114de382STrond Myklebust 			return 0;
800114de382STrond Myklebust 		}
801227823d2SDai Ngo 	}
802114de382STrond Myklebust 	unlock_page(desc->page);
803114de382STrond Myklebust error:
804d1bacf9eSBryan Schumaker 	cache_page_release(desc);
805d1bacf9eSBryan Schumaker 	return res;
806d1bacf9eSBryan Schumaker }
807d1bacf9eSBryan Schumaker 
808d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
8091da177e4SLinus Torvalds static inline
8101da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
8111da177e4SLinus Torvalds {
8128cd51a0cSTrond Myklebust 	int res;
813d1bacf9eSBryan Schumaker 
8140aded708STrond Myklebust 	if (desc->page_index == 0) {
8158cd51a0cSTrond Myklebust 		desc->current_index = 0;
81659e356a9STrond Myklebust 		desc->prev_index = 0;
8170aded708STrond Myklebust 		desc->last_cookie = 0;
8180aded708STrond Myklebust 	}
81947c716cbSTrond Myklebust 	do {
820114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
82147c716cbSTrond Myklebust 	} while (res == -EAGAIN);
8221da177e4SLinus Torvalds 	return res;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /*
8261da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds static
82923db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
8301da177e4SLinus Torvalds {
8311da177e4SLinus Torvalds 	struct file	*file = desc->file;
832d1bacf9eSBryan Schumaker 	int i = 0;
833d1bacf9eSBryan Schumaker 	int res = 0;
834d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8358ef2ce3eSBryan Schumaker 
8360795bf83SFabian Frederick 	array = kmap(desc->page);
837d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
838ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8391da177e4SLinus Torvalds 
840ece0b423STrond Myklebust 		ent = &array->array[i];
84123db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
84223db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
8436089dd0dSThomas Meyer 			desc->eof = true;
8441da177e4SLinus Torvalds 			break;
845ece0b423STrond Myklebust 		}
846d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
8472e7a4641STrond Myklebust 			desc->dir_cookie = array->array[i+1].cookie;
848d1bacf9eSBryan Schumaker 		else
8492e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
85059e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
8512e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
85259e356a9STrond Myklebust 		else
85359e356a9STrond Myklebust 			desc->ctx->pos++;
8542e7a4641STrond Myklebust 		if (desc->duped != 0)
8552e7a4641STrond Myklebust 			desc->duped = 1;
8568cd51a0cSTrond Myklebust 	}
857b1e21c97STrond Myklebust 	if (array->page_is_eof)
8586089dd0dSThomas Meyer 		desc->eof = true;
859d1bacf9eSBryan Schumaker 
8600795bf83SFabian Frederick 	kunmap(desc->page);
8611e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8622e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie, res);
8631da177e4SLinus Torvalds 	return res;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds /*
8671da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8681da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8691da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8701da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8711da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8721da177e4SLinus Torvalds  *
8731da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8741da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8751da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8761da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8771da177e4SLinus Torvalds  */
8781da177e4SLinus Torvalds static inline
87923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	struct page	*page = NULL;
8821da177e4SLinus Torvalds 	int		status;
883496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8841da177e4SLinus Torvalds 
8851e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8862e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8891da177e4SLinus Torvalds 	if (!page) {
8901da177e4SLinus Torvalds 		status = -ENOMEM;
8911da177e4SLinus Torvalds 		goto out;
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8947a8e1dc3STrond Myklebust 	desc->page_index = 0;
8952e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
8967a8e1dc3STrond Myklebust 	desc->page = page;
8972e7a4641STrond Myklebust 	desc->duped = 0;
8987a8e1dc3STrond Myklebust 
89985f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
90085f8607eSTrond Myklebust 	if (status < 0)
901d1bacf9eSBryan Schumaker 		goto out_release;
902d1bacf9eSBryan Schumaker 
90323db8620SAl Viro 	status = nfs_do_filldir(desc);
9041da177e4SLinus Torvalds 
905114de382STrond Myklebust  out_release:
906114de382STrond Myklebust 	nfs_readdir_clear_array(desc->page);
907114de382STrond Myklebust 	cache_page_release(desc);
9081da177e4SLinus Torvalds  out:
9091e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
9103110ff80SHarvey Harrison 			__func__, status);
9111da177e4SLinus Torvalds 	return status;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
91400a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
91500a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
91600a92642SOlivier Galibert    whole directory.
9171da177e4SLinus Torvalds  */
91823db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
9191da177e4SLinus Torvalds {
920be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
9212b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
92223db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
92359e356a9STrond Myklebust 	nfs_readdir_descriptor_t my_desc = {
92459e356a9STrond Myklebust 		.file = file,
92559e356a9STrond Myklebust 		.ctx = ctx,
92659e356a9STrond Myklebust 		.plus = nfs_use_readdirplus(inode, ctx),
92759e356a9STrond Myklebust 	},
92859e356a9STrond Myklebust 			*desc = &my_desc;
92907b5ce8eSScott Mayhew 	int res = 0;
9301da177e4SLinus Torvalds 
9316de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9326de1472fSAl Viro 			file, (long long)ctx->pos);
93391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
93491d5b470SChuck Lever 
9351da177e4SLinus Torvalds 	/*
93623db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
937f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
93800a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
93900a92642SOlivier Galibert 	 * revalidate the cookie.
9401da177e4SLinus Torvalds 	 */
94179f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
94223db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
943fccca7fcSTrond Myklebust 	if (res < 0)
944fccca7fcSTrond Myklebust 		goto out;
945fccca7fcSTrond Myklebust 
9462e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
9472e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
9482e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
9492e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
9502e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
9512e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
9522e7a4641STrond Myklebust 
95347c716cbSTrond Myklebust 	do {
9541da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
95500a92642SOlivier Galibert 
9561da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
957ece0b423STrond Myklebust 			res = 0;
9581da177e4SLinus Torvalds 			/* This means either end of directory */
9592e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
9601da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
96123db8620SAl Viro 				res = uncached_readdir(desc);
962ece0b423STrond Myklebust 				if (res == 0)
9631da177e4SLinus Torvalds 					continue;
9641da177e4SLinus Torvalds 			}
9651da177e4SLinus Torvalds 			break;
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9683a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9691da177e4SLinus Torvalds 			nfs_zap_caches(inode);
970baf57a09STrond Myklebust 			desc->page_index = 0;
971a7a3b1e9SBenjamin Coddington 			desc->plus = false;
972a7a3b1e9SBenjamin Coddington 			desc->eof = false;
9731da177e4SLinus Torvalds 			continue;
9741da177e4SLinus Torvalds 		}
9751da177e4SLinus Torvalds 		if (res < 0)
9761da177e4SLinus Torvalds 			break;
9771da177e4SLinus Torvalds 
97823db8620SAl Viro 		res = nfs_do_filldir(desc);
979114de382STrond Myklebust 		unlock_page(desc->page);
980114de382STrond Myklebust 		cache_page_release(desc);
981ece0b423STrond Myklebust 		if (res < 0)
9821da177e4SLinus Torvalds 			break;
98347c716cbSTrond Myklebust 	} while (!desc->eof);
9842e7a4641STrond Myklebust 
9852e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
9862e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
9872e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
9882e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
9892e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
9902e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
9912e7a4641STrond Myklebust 
992fccca7fcSTrond Myklebust out:
9931e7cb3dcSChuck Lever 	if (res > 0)
9941e7cb3dcSChuck Lever 		res = 0;
9956de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9961da177e4SLinus Torvalds 	return res;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
999965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1000f0dd2136STrond Myklebust {
1001480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1002b84e06c5SChuck Lever 
10036de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
10046de1472fSAl Viro 			filp, offset, whence);
1005b84e06c5SChuck Lever 
1006965c8e59SAndrew Morton 	switch (whence) {
1007f0dd2136STrond Myklebust 	default:
1008b2b1ff3dSTrond Myklebust 		return -EINVAL;
1009b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1010b2b1ff3dSTrond Myklebust 		if (offset < 0)
1011b2b1ff3dSTrond Myklebust 			return -EINVAL;
101283f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1013b2b1ff3dSTrond Myklebust 		break;
1014b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1015b2b1ff3dSTrond Myklebust 		if (offset == 0)
1016b2b1ff3dSTrond Myklebust 			return filp->f_pos;
101783f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1018b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1019b2b1ff3dSTrond Myklebust 		if (offset < 0) {
102083f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1021b2b1ff3dSTrond Myklebust 			return -EINVAL;
1022b2b1ff3dSTrond Myklebust 		}
1023f0dd2136STrond Myklebust 	}
1024f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1025f0dd2136STrond Myklebust 		filp->f_pos = offset;
102659e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(filp))
102759e356a9STrond Myklebust 			dir_ctx->dir_cookie = offset;
102859e356a9STrond Myklebust 		else
1029480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
10308ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1031f0dd2136STrond Myklebust 	}
103283f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1033f0dd2136STrond Myklebust 	return offset;
1034f0dd2136STrond Myklebust }
1035f0dd2136STrond Myklebust 
10361da177e4SLinus Torvalds /*
10371da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
10381da177e4SLinus Torvalds  * is a dummy operation.
10391da177e4SLinus Torvalds  */
104002c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
104102c24a82SJosef Bacik 			 int datasync)
10421da177e4SLinus Torvalds {
10436de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10441e7cb3dcSChuck Lever 
104511decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
10461da177e4SLinus Torvalds 	return 0;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
1049bfc69a45STrond Myklebust /**
1050bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1051302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1052bfc69a45STrond Myklebust  *
1053bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1054bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1055bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1056bfc69a45STrond Myklebust  *
1057efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1058efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1059efeda80dSTrond Myklebust  *
1060bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1061bfc69a45STrond Myklebust  */
1062bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1063bfc69a45STrond Myklebust {
1064efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1065bfc69a45STrond Myklebust }
106689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1067bfc69a45STrond Myklebust 
1068efeda80dSTrond Myklebust /**
1069efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1070efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1071efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1072efeda80dSTrond Myklebust  *
1073efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1074efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1075efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1076efeda80dSTrond Myklebust  */
1077efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1078efeda80dSTrond Myklebust {
1079efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1080efeda80dSTrond Myklebust }
1081efeda80dSTrond Myklebust 
1082efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1083efeda80dSTrond Myklebust {
1084efeda80dSTrond Myklebust 	*verf |= 1UL;
1085efeda80dSTrond Myklebust }
1086efeda80dSTrond Myklebust 
1087efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1088efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1089efeda80dSTrond Myklebust {
1090efeda80dSTrond Myklebust 	*verf &= ~1UL;
1091efeda80dSTrond Myklebust }
1092efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1093efeda80dSTrond Myklebust 
1094efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1095efeda80dSTrond Myklebust {
1096efeda80dSTrond Myklebust 	return verf & 1;
1097efeda80dSTrond Myklebust }
1098efeda80dSTrond Myklebust 
1099efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1100efeda80dSTrond Myklebust {
1101efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1102efeda80dSTrond Myklebust }
1103efeda80dSTrond Myklebust 
1104efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1105efeda80dSTrond Myklebust {
1106efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1107efeda80dSTrond Myklebust 
1108efeda80dSTrond Myklebust 	if (!nfs_verifier_is_delegated(dentry) &&
1109efeda80dSTrond Myklebust 	    !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1110efeda80dSTrond Myklebust 		goto out;
1111efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1112efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1113efeda80dSTrond Myklebust out:
1114efeda80dSTrond Myklebust 	dentry->d_time = verf;
1115efeda80dSTrond Myklebust }
1116efeda80dSTrond Myklebust 
1117efeda80dSTrond Myklebust /**
1118efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1119efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1120efeda80dSTrond Myklebust  * @verf: verifier to save
1121efeda80dSTrond Myklebust  *
1122efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1123efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1124efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1125efeda80dSTrond Myklebust  * look it up again after a directory change.
1126efeda80dSTrond Myklebust  */
1127efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1128efeda80dSTrond Myklebust {
1129efeda80dSTrond Myklebust 
1130efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1131efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1132efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1133efeda80dSTrond Myklebust }
1134efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1135efeda80dSTrond Myklebust 
1136efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1137efeda80dSTrond Myklebust /**
1138efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1139efeda80dSTrond Myklebust  * @inode: pointer to inode
1140efeda80dSTrond Myklebust  *
1141efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1142efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1143efeda80dSTrond Myklebust  * while holding a delegation.
1144efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1145efeda80dSTrond Myklebust  * returned or revoked.
1146efeda80dSTrond Myklebust  */
1147efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1148efeda80dSTrond Myklebust {
1149efeda80dSTrond Myklebust 	struct dentry *alias;
1150efeda80dSTrond Myklebust 
1151efeda80dSTrond Myklebust 	if (!inode)
1152efeda80dSTrond Myklebust 		return;
1153efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1154efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1155efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1156efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1157efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1158efeda80dSTrond Myklebust 	}
1159efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1160efeda80dSTrond Myklebust }
1161efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1162efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1163efeda80dSTrond Myklebust 
11641da177e4SLinus Torvalds /*
11651da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
11661da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
11671da177e4SLinus Torvalds  * and may need to be looked up again.
1168912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
11691da177e4SLinus Torvalds  */
1170912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1171912a108dSNeilBrown 			      int rcu_walk)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
11741da177e4SLinus Torvalds 		return 1;
11754eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
11764eec952eSTrond Myklebust 		return 0;
1177f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
11786ecc5e8fSTrond Myklebust 		return 0;
1179f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
11801cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1181912a108dSNeilBrown 		if (rcu_walk)
1182f2c77f4eSTrond Myklebust 			return 0;
11831cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
11841cd9cb05STrond Myklebust 			return 0;
11851cd9cb05STrond Myklebust 	}
1186f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1187f2c77f4eSTrond Myklebust 		return 0;
1188f2c77f4eSTrond Myklebust 	return 1;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds /*
1192a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1193a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1194a12802caSTrond Myklebust  */
1195fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1196a12802caSTrond Myklebust {
1197a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1198a12802caSTrond Myklebust 		return 0;
1199fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1200a12802caSTrond Myklebust }
1201a12802caSTrond Myklebust 
1202a12802caSTrond Myklebust /*
12031d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
12041d6757fbSTrond Myklebust  *
12051d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
12061d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
12071d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
12081d6757fbSTrond Myklebust  *
12091d6757fbSTrond Myklebust  */
121065a0c149STrond Myklebust static
1211fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
121465a0c149STrond Myklebust 	int ret;
12151da177e4SLinus Torvalds 
121636d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
12174e99a1ffSTrond Myklebust 		return 0;
121847921921STrond Myklebust 
121947921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
122047921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
122147921921STrond Myklebust 		case S_IFREG:
122247921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
122347921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
122447921921STrond Myklebust 				goto out;
1225df561f66SGustavo A. R. Silva 			fallthrough;
122647921921STrond Myklebust 		case S_IFDIR:
122747921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
122847921921STrond Myklebust 				break;
122947921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
123047921921STrond Myklebust 			goto out_force;
123147921921STrond Myklebust 		}
123247921921STrond Myklebust 	}
123347921921STrond Myklebust 
12341da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1235fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
12361da177e4SLinus Torvalds 		goto out_force;
123765a0c149STrond Myklebust out:
1238a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
12391da177e4SLinus Torvalds out_force:
12401fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
12411fa1e384SNeilBrown 		return -ECHILD;
124265a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
124365a0c149STrond Myklebust 	if (ret != 0)
124465a0c149STrond Myklebust 		return ret;
124565a0c149STrond Myklebust 	goto out;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds /*
12491da177e4SLinus Torvalds  * We judge how long we want to trust negative
12501da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
12511da177e4SLinus Torvalds  *
12521da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
12531da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1254912a108dSNeilBrown  *
1255912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1256912a108dSNeilBrown  * suggesting a reval is needed.
12579f6d44d4STrond Myklebust  *
12589f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
12599f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
12601da177e4SLinus Torvalds  */
12611da177e4SLinus Torvalds static inline
12621da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1263fa3c56bbSAl Viro 		       unsigned int flags)
12641da177e4SLinus Torvalds {
12659f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
12661da177e4SLinus Torvalds 		return 0;
12674eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
12684eec952eSTrond Myklebust 		return 1;
1269912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds 
12725ceb9d7fSTrond Myklebust static int
12735ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
12745ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
12751da177e4SLinus Torvalds {
12765ceb9d7fSTrond Myklebust 	switch (error) {
12775ceb9d7fSTrond Myklebust 	case 1:
12786de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
12796de1472fSAl Viro 			__func__, dentry);
12801da177e4SLinus Torvalds 		return 1;
12815ceb9d7fSTrond Myklebust 	case 0:
1282a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
12831da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
12841da177e4SLinus Torvalds 			/* Purge readdir caches. */
12851da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1286a3f432bfSJ. Bruce Fields 			/*
1287a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1288a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1289a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1290a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1291a3f432bfSJ. Bruce Fields 			 */
1292a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
12935ceb9d7fSTrond Myklebust 				return 1;
12941da177e4SLinus Torvalds 		}
12956de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
12966de1472fSAl Viro 				__func__, dentry);
12971da177e4SLinus Torvalds 		return 0;
12985ceb9d7fSTrond Myklebust 	}
12996de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
13006de1472fSAl Viro 				__func__, dentry, error);
1301e1fb4d05STrond Myklebust 	return error;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13045ceb9d7fSTrond Myklebust static int
13055ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
13065ceb9d7fSTrond Myklebust 			       unsigned int flags)
13075ceb9d7fSTrond Myklebust {
13085ceb9d7fSTrond Myklebust 	int ret = 1;
13095ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
13105ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
13115ceb9d7fSTrond Myklebust 			return -ECHILD;
13125ceb9d7fSTrond Myklebust 		ret = 0;
13135ceb9d7fSTrond Myklebust 	}
13145ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
13155ceb9d7fSTrond Myklebust }
13165ceb9d7fSTrond Myklebust 
13175ceb9d7fSTrond Myklebust static int
13185ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
13195ceb9d7fSTrond Myklebust 				struct inode *inode)
13205ceb9d7fSTrond Myklebust {
13215ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
13225ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
13235ceb9d7fSTrond Myklebust }
13245ceb9d7fSTrond Myklebust 
13255ceb9d7fSTrond Myklebust static int
13265ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
13275ceb9d7fSTrond Myklebust 			     struct inode *inode)
13285ceb9d7fSTrond Myklebust {
13295ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
13305ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
13315ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
1332a1147b82STrond Myklebust 	unsigned long dir_verifier;
13335ceb9d7fSTrond Myklebust 	int ret;
13345ceb9d7fSTrond Myklebust 
13355ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
13365ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
13375ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
13385ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
13395ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
13405ceb9d7fSTrond Myklebust 		goto out;
13415ceb9d7fSTrond Myklebust 
1342a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
1343f7b37b8bSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
13445ceb9d7fSTrond Myklebust 	if (ret < 0) {
1345f7b37b8bSTrond Myklebust 		switch (ret) {
1346f7b37b8bSTrond Myklebust 		case -ESTALE:
1347f7b37b8bSTrond Myklebust 		case -ENOENT:
13485ceb9d7fSTrond Myklebust 			ret = 0;
1349f7b37b8bSTrond Myklebust 			break;
1350f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1351f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1352f7b37b8bSTrond Myklebust 				ret = 1;
1353f7b37b8bSTrond Myklebust 		}
13545ceb9d7fSTrond Myklebust 		goto out;
13555ceb9d7fSTrond Myklebust 	}
13565ceb9d7fSTrond Myklebust 	ret = 0;
13575ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
13585ceb9d7fSTrond Myklebust 		goto out;
13595ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
13605ceb9d7fSTrond Myklebust 		goto out;
13615ceb9d7fSTrond Myklebust 
13625ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
1363a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
13645ceb9d7fSTrond Myklebust 
13655ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
13665ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
13675ceb9d7fSTrond Myklebust 	ret = 1;
13685ceb9d7fSTrond Myklebust out:
13695ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
13705ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
13715ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
13725ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
13735ceb9d7fSTrond Myklebust }
13745ceb9d7fSTrond Myklebust 
13755ceb9d7fSTrond Myklebust /*
13765ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
13775ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
13785ceb9d7fSTrond Myklebust  * lookup.
13795ceb9d7fSTrond Myklebust  *
13805ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
13815ceb9d7fSTrond Myklebust  * we have an inode!
13825ceb9d7fSTrond Myklebust  *
13835ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
13845ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
13855ceb9d7fSTrond Myklebust  */
13865ceb9d7fSTrond Myklebust static int
13875ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
13885ceb9d7fSTrond Myklebust 			 unsigned int flags)
13895ceb9d7fSTrond Myklebust {
13905ceb9d7fSTrond Myklebust 	struct inode *inode;
13915ceb9d7fSTrond Myklebust 	int error;
13925ceb9d7fSTrond Myklebust 
13935ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
13945ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
13955ceb9d7fSTrond Myklebust 
13965ceb9d7fSTrond Myklebust 	if (!inode)
13975ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
13985ceb9d7fSTrond Myklebust 
13995ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
14005ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
14015ceb9d7fSTrond Myklebust 				__func__, dentry);
14025ceb9d7fSTrond Myklebust 		goto out_bad;
14035ceb9d7fSTrond Myklebust 	}
14045ceb9d7fSTrond Myklebust 
1405efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
14065ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
14075ceb9d7fSTrond Myklebust 
14085ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
14095ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
14105ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
14115ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
14125ceb9d7fSTrond Myklebust 		if (error) {
14135ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
14145ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
14155ceb9d7fSTrond Myklebust 			goto out_bad;
14165ceb9d7fSTrond Myklebust 		}
14175ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
14185ceb9d7fSTrond Myklebust 		goto out_valid;
14195ceb9d7fSTrond Myklebust 	}
14205ceb9d7fSTrond Myklebust 
14215ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
14225ceb9d7fSTrond Myklebust 		return -ECHILD;
14235ceb9d7fSTrond Myklebust 
14245ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
14255ceb9d7fSTrond Myklebust 		goto out_bad;
14265ceb9d7fSTrond Myklebust 
14275ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
14285ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
14295ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
14305ceb9d7fSTrond Myklebust 	return error;
14315ceb9d7fSTrond Myklebust out_valid:
14325ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
14335ceb9d7fSTrond Myklebust out_bad:
14345ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
14355ceb9d7fSTrond Myklebust 		return -ECHILD;
14365ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
14375ceb9d7fSTrond Myklebust }
14385ceb9d7fSTrond Myklebust 
14395ceb9d7fSTrond Myklebust static int
1440c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1441c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
14425ceb9d7fSTrond Myklebust {
14435ceb9d7fSTrond Myklebust 	struct dentry *parent;
14445ceb9d7fSTrond Myklebust 	struct inode *dir;
14455ceb9d7fSTrond Myklebust 	int ret;
14465ceb9d7fSTrond Myklebust 
14475ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
14485ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
14495ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
14505ceb9d7fSTrond Myklebust 		if (!dir)
14515ceb9d7fSTrond Myklebust 			return -ECHILD;
1452c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
14535ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
14545ceb9d7fSTrond Myklebust 			return -ECHILD;
14555ceb9d7fSTrond Myklebust 	} else {
14565ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1457c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
14585ceb9d7fSTrond Myklebust 		dput(parent);
14595ceb9d7fSTrond Myklebust 	}
14605ceb9d7fSTrond Myklebust 	return ret;
14615ceb9d7fSTrond Myklebust }
14625ceb9d7fSTrond Myklebust 
1463c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1464c7944ebbSTrond Myklebust {
1465c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1466c7944ebbSTrond Myklebust }
1467c7944ebbSTrond Myklebust 
14681da177e4SLinus Torvalds /*
14692b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1470ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1471ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1472ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1473ecf3d1f1SJeff Layton  *
1474ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1475ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1476ecf3d1f1SJeff Layton  */
1477ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1478ecf3d1f1SJeff Layton {
14792b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
14809cdd1d3fSTrond Myklebust 	int error = 0;
1481ecf3d1f1SJeff Layton 
1482ecf3d1f1SJeff Layton 	/*
1483ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1484ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1485ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1486ecf3d1f1SJeff Layton 	 */
1487ecf3d1f1SJeff Layton 	if (!inode) {
14886de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
14896de1472fSAl Viro 				__func__, dentry);
1490ecf3d1f1SJeff Layton 		return 1;
1491ecf3d1f1SJeff Layton 	}
1492ecf3d1f1SJeff Layton 
1493ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
14946de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
14956de1472fSAl Viro 				__func__, dentry);
1496ecf3d1f1SJeff Layton 		return 0;
1497ecf3d1f1SJeff Layton 	}
1498ecf3d1f1SJeff Layton 
1499b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1500ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1501ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1502ecf3d1f1SJeff Layton 	return !error;
1503ecf3d1f1SJeff Layton }
1504ecf3d1f1SJeff Layton 
1505ecf3d1f1SJeff Layton /*
15061da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
15071da177e4SLinus Torvalds  */
1508fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
15091da177e4SLinus Torvalds {
15106de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
15116de1472fSAl Viro 		dentry, dentry->d_flags);
15121da177e4SLinus Torvalds 
151377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
15142b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
151577f11192STrond Myklebust 		return 1;
151677f11192STrond Myklebust 
15171da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
15181da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
15191da177e4SLinus Torvalds 		return 1;
15201da177e4SLinus Torvalds 	}
15211751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
15221da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
15231da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
15241da177e4SLinus Torvalds 		return 1;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 	return 0;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
15311b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
15321b83d707STrond Myklebust {
15331b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
15341f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
153559a707b0STrond Myklebust 	if (inode->i_nlink > 0)
153659a707b0STrond Myklebust 		drop_nlink(inode);
153759a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
153816e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
153916e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
154059a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
154159a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
15421b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
15431b83d707STrond Myklebust }
15441b83d707STrond Myklebust 
15451da177e4SLinus Torvalds /*
15461da177e4SLinus Torvalds  * Called when the dentry loses inode.
15471da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
15481da177e4SLinus Torvalds  */
15491da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
15501da177e4SLinus Torvalds {
155183672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
155283672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
155383672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
155483672d39SNeil Brown 
15551da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1556e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
15571f018458STrond Myklebust 		nfs_drop_nlink(inode);
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 	iput(inode);
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
1562b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1563b1942c5fSAl Viro {
1564b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1565b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1566b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1567b1942c5fSAl Viro 			WARN_ON(1);
1568b1942c5fSAl Viro 		else
1569b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1570b1942c5fSAl Viro 	}
1571b1942c5fSAl Viro }
1572b1942c5fSAl Viro 
1573f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
15741da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1575ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
15761da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
15771da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
157836d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1579b1942c5fSAl Viro 	.d_release	= nfs_d_release,
15801da177e4SLinus Torvalds };
1581ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
15821da177e4SLinus Torvalds 
1583597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
15841da177e4SLinus Torvalds {
15851da177e4SLinus Torvalds 	struct dentry *res;
15861da177e4SLinus Torvalds 	struct inode *inode = NULL;
1587e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1588e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
15891775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
1590a1147b82STrond Myklebust 	unsigned long dir_verifier;
15911da177e4SLinus Torvalds 	int error;
15921da177e4SLinus Torvalds 
15936de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
159491d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
15951da177e4SLinus Torvalds 
1596130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1597130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
15981da177e4SLinus Torvalds 
1599fd684071STrond Myklebust 	/*
1600fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1601fd684071STrond Myklebust 	 * but don't hash the dentry.
1602fd684071STrond Myklebust 	 */
16039f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1604130f9ab7SAl Viro 		return NULL;
16051da177e4SLinus Torvalds 
1606e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1607e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1608e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1609e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1610e1fb4d05STrond Myklebust 		goto out;
1611e1fb4d05STrond Myklebust 
161214c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
161314c43f76SDavid Quigley 	if (IS_ERR(label))
161414c43f76SDavid Quigley 		goto out;
161514c43f76SDavid Quigley 
1616a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
16176e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1618f7b37b8bSTrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
16191da177e4SLinus Torvalds 	if (error == -ENOENT)
16201da177e4SLinus Torvalds 		goto no_entry;
16211da177e4SLinus Torvalds 	if (error < 0) {
16221da177e4SLinus Torvalds 		res = ERR_PTR(error);
1623bf130914SAl Viro 		goto out_label;
16241da177e4SLinus Torvalds 	}
16251775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1626bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
162703f28e3aSTrond Myklebust 	if (IS_ERR(res))
1628bf130914SAl Viro 		goto out_label;
162954ceac45SDavid Howells 
163063519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
163163519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1632d69ee9b8STrond Myklebust 
16331da177e4SLinus Torvalds no_entry:
163441d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
16359eaef27bSTrond Myklebust 	if (res != NULL) {
16369eaef27bSTrond Myklebust 		if (IS_ERR(res))
1637bf130914SAl Viro 			goto out_label;
16381da177e4SLinus Torvalds 		dentry = res;
16399eaef27bSTrond Myklebust 	}
1640a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
1641bf130914SAl Viro out_label:
16426e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
164314c43f76SDavid Quigley 	nfs4_label_free(label);
16441da177e4SLinus Torvalds out:
1645e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1646e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
16471da177e4SLinus Torvalds 	return res;
16481da177e4SLinus Torvalds }
1649ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
16501da177e4SLinus Torvalds 
165189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
16520b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
16531da177e4SLinus Torvalds 
1654f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
16550ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1656b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
16571da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
16581da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
165936d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1660b1942c5fSAl Viro 	.d_release	= nfs_d_release,
16611da177e4SLinus Torvalds };
166289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
16631da177e4SLinus Torvalds 
16648a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
16658a5e929dSAl Viro {
16668a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
16678a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
16688a5e929dSAl Viro 		res |= FMODE_READ;
16698a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
16708a5e929dSAl Viro 		res |= FMODE_WRITE;
16718a5e929dSAl Viro 	return res;
16728a5e929dSAl Viro }
16738a5e929dSAl Viro 
1674532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1675cd9a1c0eSTrond Myklebust {
1676532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1677cd9a1c0eSTrond Myklebust }
1678cd9a1c0eSTrond Myklebust 
1679cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1680cd9a1c0eSTrond Myklebust {
1681f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1682cd9a1c0eSTrond Myklebust 	return 0;
1683cd9a1c0eSTrond Myklebust }
1684cd9a1c0eSTrond Myklebust 
1685d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
16860dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1687b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1688cd9a1c0eSTrond Myklebust {
16890dd2b474SMiklos Szeredi 	int err;
16900dd2b474SMiklos Szeredi 
1691be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
169230d90494SAl Viro 	if (err)
1693d9585277SAl Viro 		goto out;
1694eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
169530d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1696eaa2b82cSNeilBrown 	else
16979821421aSTrond Myklebust 		err = -EOPENSTALE;
1698cd9a1c0eSTrond Myklebust out:
1699d9585277SAl Viro 	return err;
1700cd9a1c0eSTrond Myklebust }
1701cd9a1c0eSTrond Myklebust 
170273a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
170330d90494SAl Viro 		    struct file *file, unsigned open_flags,
170444907d79SAl Viro 		    umode_t mode)
17051da177e4SLinus Torvalds {
1706c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1707cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
17080dd2b474SMiklos Szeredi 	struct dentry *res;
17090dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1710f46e0bd3STrond Myklebust 	struct inode *inode;
17111472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1712c94c0953SAl Viro 	bool switched = false;
171373a09dd9SAl Viro 	int created = 0;
1714898f635cSTrond Myklebust 	int err;
17151da177e4SLinus Torvalds 
17160dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
17172b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
17180dd2b474SMiklos Szeredi 
17191e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
17206de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17211e7cb3dcSChuck Lever 
17229597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
17239597c13bSJeff Layton 	if (err)
17249597c13bSJeff Layton 		return err;
17259597c13bSJeff Layton 
17260dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
17270dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
172800699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
17290dd2b474SMiklos Szeredi 			/*
17300dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
17310dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
17320dd2b474SMiklos Szeredi 			 * again
17330dd2b474SMiklos Szeredi 			 */
1734d9585277SAl Viro 			return -ENOENT;
17350dd2b474SMiklos Szeredi 		}
17361472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
17371da177e4SLinus Torvalds 		goto no_open;
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds 
17400dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1741d9585277SAl Viro 		return -ENAMETOOLONG;
17421da177e4SLinus Torvalds 
17430dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1744dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1745dff25ddbSAndreas Gruenbacher 
1746dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1747dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1748dff25ddbSAndreas Gruenbacher 
1749536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1750dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
17510dd2b474SMiklos Szeredi 	}
1752536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1753536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1754536e43d1STrond Myklebust 		attr.ia_size = 0;
1755cd9a1c0eSTrond Myklebust 	}
1756cd9a1c0eSTrond Myklebust 
1757c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1758c94c0953SAl Viro 		d_drop(dentry);
1759c94c0953SAl Viro 		switched = true;
1760c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1761c94c0953SAl Viro 					  &dentry->d_name, &wq);
1762c94c0953SAl Viro 		if (IS_ERR(dentry))
1763c94c0953SAl Viro 			return PTR_ERR(dentry);
1764c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1765c94c0953SAl Viro 			return finish_no_open(file, dentry);
1766c94c0953SAl Viro 	}
1767c94c0953SAl Viro 
1768532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
17690dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
17700dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1771d9585277SAl Viro 		goto out;
17720dd2b474SMiklos Szeredi 
17736e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
177473a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
177573a09dd9SAl Viro 	if (created)
177673a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1777275bb307STrond Myklebust 	if (IS_ERR(inode)) {
17780dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
17796e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
17802d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1781d20cb71dSAl Viro 		d_drop(dentry);
17820dd2b474SMiklos Szeredi 		switch (err) {
17831da177e4SLinus Torvalds 		case -ENOENT:
1784774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1785809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17860dd2b474SMiklos Szeredi 			break;
17871788ea6eSJeff Layton 		case -EISDIR:
17886f926b5bSTrond Myklebust 		case -ENOTDIR:
17896f926b5bSTrond Myklebust 			goto no_open;
17901da177e4SLinus Torvalds 		case -ELOOP:
17910dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
17921da177e4SLinus Torvalds 				goto no_open;
17930dd2b474SMiklos Szeredi 			break;
17941da177e4SLinus Torvalds 			/* case -EINVAL: */
17951da177e4SLinus Torvalds 		default:
17960dd2b474SMiklos Szeredi 			break;
17971da177e4SLinus Torvalds 		}
17981da177e4SLinus Torvalds 		goto out;
17991da177e4SLinus Torvalds 	}
18000dd2b474SMiklos Szeredi 
1801b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
18026e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
18032d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1804d9585277SAl Viro out:
1805c94c0953SAl Viro 	if (unlikely(switched)) {
1806c94c0953SAl Viro 		d_lookup_done(dentry);
1807c94c0953SAl Viro 		dput(dentry);
1808c94c0953SAl Viro 	}
1809d9585277SAl Viro 	return err;
18100dd2b474SMiklos Szeredi 
18111da177e4SLinus Torvalds no_open:
18121472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1813c94c0953SAl Viro 	if (switched) {
1814c94c0953SAl Viro 		d_lookup_done(dentry);
1815c94c0953SAl Viro 		if (!res)
1816c94c0953SAl Viro 			res = dentry;
1817c94c0953SAl Viro 		else
1818c94c0953SAl Viro 			dput(dentry);
1819c94c0953SAl Viro 	}
18200dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1821c94c0953SAl Viro 		return PTR_ERR(res);
1822e45198a6SAl Viro 	return finish_no_open(file, res);
18231da177e4SLinus Torvalds }
182489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
18251da177e4SLinus Torvalds 
1826c7944ebbSTrond Myklebust static int
1827c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1828c7944ebbSTrond Myklebust 			  unsigned int flags)
18291da177e4SLinus Torvalds {
1830657e94b6SNick Piggin 	struct inode *inode;
18311da177e4SLinus Torvalds 
1832fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1833c7944ebbSTrond Myklebust 		goto full_reval;
1834eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1835c7944ebbSTrond Myklebust 		goto full_reval;
18362b484297STrond Myklebust 
18372b0143b5SDavid Howells 	inode = d_inode(dentry);
18382b484297STrond Myklebust 
18391da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
18401da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
18411da177e4SLinus Torvalds 	 */
1842c7944ebbSTrond Myklebust 	if (inode == NULL)
1843c7944ebbSTrond Myklebust 		goto full_reval;
184449317a7fSNeilBrown 
1845efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
1846c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1847216d5d06STrond Myklebust 
18481da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
18491da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1850c7944ebbSTrond Myklebust 		goto full_reval;
1851c7944ebbSTrond Myklebust 
18521da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1853c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1854c7944ebbSTrond Myklebust 		goto reval_dentry;
1855c7944ebbSTrond Myklebust 
1856c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1857c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1858c7944ebbSTrond Myklebust 		goto reval_dentry;
18591da177e4SLinus Torvalds 
18600ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1861c7944ebbSTrond Myklebust 	return 1;
1862c7944ebbSTrond Myklebust reval_dentry:
1863c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1864c7944ebbSTrond Myklebust 		return -ECHILD;
186542f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
18660ef97dcfSMiklos Szeredi 
1867c7944ebbSTrond Myklebust full_reval:
1868c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1869c7944ebbSTrond Myklebust }
1870535918f1STrond Myklebust 
1871c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1872c7944ebbSTrond Myklebust {
1873c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1874c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1875c0204fd2STrond Myklebust }
1876c0204fd2STrond Myklebust 
18771da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
18781da177e4SLinus Torvalds 
1879406cd915SBenjamin Coddington struct dentry *
1880406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
18811775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
18821775fd3eSDavid Quigley 				struct nfs4_label *label)
18831da177e4SLinus Torvalds {
1884fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
18852b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
18861da177e4SLinus Torvalds 	struct inode *inode;
1887b0c6108eSAl Viro 	struct dentry *d;
1888406cd915SBenjamin Coddington 	int error;
18891da177e4SLinus Torvalds 
1890fab728e1STrond Myklebust 	d_drop(dentry);
1891fab728e1STrond Myklebust 
18921da177e4SLinus Torvalds 	if (fhandle->size == 0) {
1893f7b37b8bSTrond Myklebust 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
18941da177e4SLinus Torvalds 		if (error)
1895fab728e1STrond Myklebust 			goto out_error;
18961da177e4SLinus Torvalds 	}
18975724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18981da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
18991da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1900a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1901a841b54dSTrond Myklebust 				fattr, NULL, NULL);
19021da177e4SLinus Torvalds 		if (error < 0)
1903fab728e1STrond Myklebust 			goto out_error;
19041da177e4SLinus Torvalds 	}
19051775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1906b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1907fab728e1STrond Myklebust out:
1908fab728e1STrond Myklebust 	dput(parent);
1909406cd915SBenjamin Coddington 	return d;
1910fab728e1STrond Myklebust out_error:
1911fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1912406cd915SBenjamin Coddington 	d = ERR_PTR(error);
1913406cd915SBenjamin Coddington 	goto out;
1914406cd915SBenjamin Coddington }
1915406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1916406cd915SBenjamin Coddington 
1917406cd915SBenjamin Coddington /*
1918406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
1919406cd915SBenjamin Coddington  */
1920406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1921406cd915SBenjamin Coddington 				struct nfs_fattr *fattr,
1922406cd915SBenjamin Coddington 				struct nfs4_label *label)
1923406cd915SBenjamin Coddington {
1924406cd915SBenjamin Coddington 	struct dentry *d;
1925406cd915SBenjamin Coddington 
1926406cd915SBenjamin Coddington 	d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1927406cd915SBenjamin Coddington 	if (IS_ERR(d))
1928406cd915SBenjamin Coddington 		return PTR_ERR(d);
1929406cd915SBenjamin Coddington 
1930406cd915SBenjamin Coddington 	/* Callers don't care */
1931406cd915SBenjamin Coddington 	dput(d);
1932406cd915SBenjamin Coddington 	return 0;
19331da177e4SLinus Torvalds }
1934ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds /*
19371da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
19381da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
19391da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
19401da177e4SLinus Torvalds  * reply path made it appear to have failed.
19411da177e4SLinus Torvalds  */
1942597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1943ebfc3b49SAl Viro 		umode_t mode, bool excl)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds 	struct iattr attr;
1946ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
19471da177e4SLinus Torvalds 	int error;
19481da177e4SLinus Torvalds 
19491e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
19506de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	attr.ia_mode = mode;
19531da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
19541da177e4SLinus Torvalds 
19558b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
19568867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
19578b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
19581da177e4SLinus Torvalds 	if (error != 0)
19591da177e4SLinus Torvalds 		goto out_err;
19601da177e4SLinus Torvalds 	return 0;
19611da177e4SLinus Torvalds out_err:
19621da177e4SLinus Torvalds 	d_drop(dentry);
19631da177e4SLinus Torvalds 	return error;
19641da177e4SLinus Torvalds }
1965ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds /*
19681da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
19691da177e4SLinus Torvalds  */
1970597d9289SBryan Schumaker int
19711a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
19721da177e4SLinus Torvalds {
19731da177e4SLinus Torvalds 	struct iattr attr;
19741da177e4SLinus Torvalds 	int status;
19751da177e4SLinus Torvalds 
19761e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
19776de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	attr.ia_mode = mode;
19801da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
19811da177e4SLinus Torvalds 
19821ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
19831da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
19841ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
19851da177e4SLinus Torvalds 	if (status != 0)
19861da177e4SLinus Torvalds 		goto out_err;
19871da177e4SLinus Torvalds 	return 0;
19881da177e4SLinus Torvalds out_err:
19891da177e4SLinus Torvalds 	d_drop(dentry);
19901da177e4SLinus Torvalds 	return status;
19911da177e4SLinus Torvalds }
1992ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds /*
19951da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
19961da177e4SLinus Torvalds  */
1997597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
19981da177e4SLinus Torvalds {
19991da177e4SLinus Torvalds 	struct iattr attr;
20001da177e4SLinus Torvalds 	int error;
20011da177e4SLinus Torvalds 
20021e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
20036de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20061da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
20071da177e4SLinus Torvalds 
20081ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
20091da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
20101ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
20111da177e4SLinus Torvalds 	if (error != 0)
20121da177e4SLinus Torvalds 		goto out_err;
20131da177e4SLinus Torvalds 	return 0;
20141da177e4SLinus Torvalds out_err:
20151da177e4SLinus Torvalds 	d_drop(dentry);
20161da177e4SLinus Torvalds 	return error;
20171da177e4SLinus Torvalds }
2018ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
20191da177e4SLinus Torvalds 
2020d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2021d45b9d8bSTrond Myklebust {
2022dc3f4198SAl Viro 	if (simple_positive(dentry))
2023d45b9d8bSTrond Myklebust 		d_delete(dentry);
2024d45b9d8bSTrond Myklebust }
2025d45b9d8bSTrond Myklebust 
2026597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
20271da177e4SLinus Torvalds {
20281da177e4SLinus Torvalds 	int error;
20291da177e4SLinus Torvalds 
20301e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
20316de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20321da177e4SLinus Torvalds 
20331ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
20342b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2035884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
20361da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
20371da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2038ba6c0592STrond Myklebust 		switch (error) {
2039ba6c0592STrond Myklebust 		case 0:
20402b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2041ba6c0592STrond Myklebust 			break;
2042ba6c0592STrond Myklebust 		case -ENOENT:
2043d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2044ba6c0592STrond Myklebust 		}
2045884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2046ba6c0592STrond Myklebust 	} else
2047ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
20481ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	return error;
20511da177e4SLinus Torvalds }
2052ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds /*
20551da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
20561da177e4SLinus Torvalds  * and after checking that the file has only one user.
20571da177e4SLinus Torvalds  *
20581da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
20591da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
20601da177e4SLinus Torvalds  */
20611da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
20621da177e4SLinus Torvalds {
20632b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
20642b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
20651da177e4SLinus Torvalds 	int error = -EBUSY;
20661da177e4SLinus Torvalds 
20676de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
20701da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
20711da177e4SLinus Torvalds 		error = 0;
20721da177e4SLinus Torvalds 		goto out;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 
20751ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
20761da177e4SLinus Torvalds 	if (inode != NULL) {
2077912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
20781da177e4SLinus Torvalds 		if (error == 0)
20791b83d707STrond Myklebust 			nfs_drop_nlink(inode);
20801da177e4SLinus Torvalds 	} else
2081912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2082d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2083d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
20841ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
20851da177e4SLinus Torvalds out:
20861da177e4SLinus Torvalds 	return error;
20871da177e4SLinus Torvalds }
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
20901da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
20911da177e4SLinus Torvalds  *
20921da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
20931da177e4SLinus Torvalds  */
2094597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
20951da177e4SLinus Torvalds {
20961da177e4SLinus Torvalds 	int error;
20971da177e4SLinus Torvalds 	int need_rehash = 0;
20981da177e4SLinus Torvalds 
20991e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
21006de1472fSAl Viro 		dir->i_ino, dentry);
21011da177e4SLinus Torvalds 
21021ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
21031da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
210484d08fa8SAl Viro 	if (d_count(dentry) > 1) {
21051da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2106ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
21072b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
21081da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
21091ca42382STrond Myklebust 		goto out;
21101da177e4SLinus Torvalds 	}
21111da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
21121da177e4SLinus Torvalds 		__d_drop(dentry);
21131da177e4SLinus Torvalds 		need_rehash = 1;
21141da177e4SLinus Torvalds 	}
21151da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21161da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
2117d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
21181da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
21191da177e4SLinus Torvalds 	} else if (need_rehash)
21201da177e4SLinus Torvalds 		d_rehash(dentry);
21211ca42382STrond Myklebust out:
21221ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
21231da177e4SLinus Torvalds 	return error;
21241da177e4SLinus Torvalds }
2125ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
21261da177e4SLinus Torvalds 
2127873101b3SChuck Lever /*
2128873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2129873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2130873101b3SChuck Lever  * using prepare_write/commit_write.
2131873101b3SChuck Lever  *
2132873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2133873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2134873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2135873101b3SChuck Lever  * symlink request has completed on the server.
2136873101b3SChuck Lever  *
2137873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2138873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2139873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2140873101b3SChuck Lever  * and move the raw page into its mapping.
2141873101b3SChuck Lever  */
2142597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
21431da177e4SLinus Torvalds {
2144873101b3SChuck Lever 	struct page *page;
2145873101b3SChuck Lever 	char *kaddr;
21461da177e4SLinus Torvalds 	struct iattr attr;
2147873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
21481da177e4SLinus Torvalds 	int error;
21491da177e4SLinus Torvalds 
21501e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
21516de1472fSAl Viro 		dir->i_ino, dentry, symname);
21521da177e4SLinus Torvalds 
2153873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2154873101b3SChuck Lever 		return -ENAMETOOLONG;
21551da177e4SLinus Torvalds 
2156873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2157873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
21581da177e4SLinus Torvalds 
2159e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
216076566991STrond Myklebust 	if (!page)
2161873101b3SChuck Lever 		return -ENOMEM;
2162873101b3SChuck Lever 
2163e8ecde25SAl Viro 	kaddr = page_address(page);
2164873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2165873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2166873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2167873101b3SChuck Lever 
21681ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
216994a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
21701ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2171873101b3SChuck Lever 	if (error != 0) {
21721e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2173873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
21746de1472fSAl Viro 			dentry, symname, error);
21751da177e4SLinus Torvalds 		d_drop(dentry);
2176873101b3SChuck Lever 		__free_page(page);
21771da177e4SLinus Torvalds 		return error;
21781da177e4SLinus Torvalds 	}
21791da177e4SLinus Torvalds 
2180873101b3SChuck Lever 	/*
2181873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2182873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2183873101b3SChuck Lever 	 */
21842b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2185873101b3SChuck Lever 							GFP_KERNEL)) {
2186873101b3SChuck Lever 		SetPageUptodate(page);
2187873101b3SChuck Lever 		unlock_page(page);
2188a0b54addSRafael Aquini 		/*
2189a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2190a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2191a0b54addSRafael Aquini 		 */
219209cbfeafSKirill A. Shutemov 		put_page(page);
2193873101b3SChuck Lever 	} else
2194873101b3SChuck Lever 		__free_page(page);
2195873101b3SChuck Lever 
2196873101b3SChuck Lever 	return 0;
2197873101b3SChuck Lever }
2198ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2199873101b3SChuck Lever 
2200597d9289SBryan Schumaker int
22011da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
22021da177e4SLinus Torvalds {
22032b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
22041da177e4SLinus Torvalds 	int error;
22051da177e4SLinus Torvalds 
22066de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
22076de1472fSAl Viro 		old_dentry, dentry);
22081da177e4SLinus Torvalds 
22091fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
22109697d234STrond Myklebust 	d_drop(dentry);
22111da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2212cf809556STrond Myklebust 	if (error == 0) {
22137de9c6eeSAl Viro 		ihold(inode);
22149697d234STrond Myklebust 		d_add(dentry, inode);
2215cf809556STrond Myklebust 	}
22161fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
22171da177e4SLinus Torvalds 	return error;
22181da177e4SLinus Torvalds }
2219ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds /*
22221da177e4SLinus Torvalds  * RENAME
22231da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
22241da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
22251da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
22261da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
22271da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
22281da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
22291da177e4SLinus Torvalds  *
22301da177e4SLinus Torvalds  * FIXED.
22311da177e4SLinus Torvalds  *
22321da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
22331da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
22341da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
22351da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
22361da177e4SLinus Torvalds  * using the inode layer
22371da177e4SLinus Torvalds  *
22381da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
22391da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
22401da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
22411da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
22421da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
22431da177e4SLinus Torvalds  * the rename.
22441da177e4SLinus Torvalds  */
2245597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
22461cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
22471cd66c93SMiklos Szeredi 	       unsigned int flags)
22481da177e4SLinus Torvalds {
22492b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
22502b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2251d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
225280a491fdSJeff Layton 	struct rpc_task *task;
22531da177e4SLinus Torvalds 	int error = -EBUSY;
22541da177e4SLinus Torvalds 
22551cd66c93SMiklos Szeredi 	if (flags)
22561cd66c93SMiklos Szeredi 		return -EINVAL;
22571cd66c93SMiklos Szeredi 
22586de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
22596de1472fSAl Viro 		 old_dentry, new_dentry,
226084d08fa8SAl Viro 		 d_count(new_dentry));
22611da177e4SLinus Torvalds 
226270ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
22631da177e4SLinus Torvalds 	/*
226428f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
226528f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
226628f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
226728f79a1aSMiklos Szeredi 	 * the new target.
22681da177e4SLinus Torvalds 	 */
226927226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
227027226104SMiklos Szeredi 		/*
227127226104SMiklos Szeredi 		 * To prevent any new references to the target during the
227227226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
227327226104SMiklos Szeredi 		 */
2274d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
227527226104SMiklos Szeredi 			d_drop(new_dentry);
2276d9f29500SBenjamin Coddington 			rehash = new_dentry;
2277d9f29500SBenjamin Coddington 		}
227827226104SMiklos Szeredi 
227984d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
22801da177e4SLinus Torvalds 			int err;
228127226104SMiklos Szeredi 
22821da177e4SLinus Torvalds 			/* copy the target dentry's name */
22831da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
22841da177e4SLinus Torvalds 					 &new_dentry->d_name);
22851da177e4SLinus Torvalds 			if (!dentry)
22861da177e4SLinus Torvalds 				goto out;
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
22891da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
229024e93025SMiklos Szeredi 			if (err)
22911da177e4SLinus Torvalds 				goto out;
229224e93025SMiklos Szeredi 
229324e93025SMiklos Szeredi 			new_dentry = dentry;
2294d9f29500SBenjamin Coddington 			rehash = NULL;
229524e93025SMiklos Szeredi 			new_inode = NULL;
2296b1e4adf4STrond Myklebust 		}
229727226104SMiklos Szeredi 	}
22981da177e4SLinus Torvalds 
2299d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
230080a491fdSJeff Layton 	if (IS_ERR(task)) {
230180a491fdSJeff Layton 		error = PTR_ERR(task);
230280a491fdSJeff Layton 		goto out;
230380a491fdSJeff Layton 	}
230480a491fdSJeff Layton 
230580a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2306818a8dbeSBenjamin Coddington 	if (error != 0) {
2307818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2308818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2309818a8dbeSBenjamin Coddington 		smp_wmb();
2310818a8dbeSBenjamin Coddington 	} else
231180a491fdSJeff Layton 		error = task->tk_status;
231280a491fdSJeff Layton 	rpc_put_task(task);
231359a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
231459a707b0STrond Myklebust 	if (error == 0) {
231559a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
231659a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
231759a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
231859a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
231959a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
232059a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
232159a707b0STrond Myklebust 	}
23221da177e4SLinus Torvalds out:
2323d9f29500SBenjamin Coddington 	if (rehash)
2324d9f29500SBenjamin Coddington 		d_rehash(rehash);
232570ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
232670ded201STrond Myklebust 			new_dir, new_dentry, error);
2327d9f29500SBenjamin Coddington 	if (!error) {
2328d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2329d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2330d9f29500SBenjamin Coddington 		/*
2331d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2332d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2333d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2334d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2335d9f29500SBenjamin Coddington 		 */
2336d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2337d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2338d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2339d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2340d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2341d9f29500SBenjamin Coddington 
23421da177e4SLinus Torvalds 	/* new dentry created? */
23431da177e4SLinus Torvalds 	if (dentry)
23441da177e4SLinus Torvalds 		dput(dentry);
23451da177e4SLinus Torvalds 	return error;
23461da177e4SLinus Torvalds }
2347ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
23481da177e4SLinus Torvalds 
2349cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2350cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2351cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2352cfcea3e8STrond Myklebust 
2353a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
23543a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
23553a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
23563a505845STrond Myklebust 
23571c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
23581c3c07e9STrond Myklebust {
2359b68572e0SNeilBrown 	put_cred(entry->cred);
2360f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
23614e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2362cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
23634e857c58SPeter Zijlstra 	smp_mb__after_atomic();
23641c3c07e9STrond Myklebust }
23651c3c07e9STrond Myklebust 
23661a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
23671a81bb8aSTrond Myklebust {
23681a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
23691a81bb8aSTrond Myklebust 
23701a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
23711a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
23721a81bb8aSTrond Myklebust 		list_del(&cache->lru);
23731a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
23741a81bb8aSTrond Myklebust 	}
23751a81bb8aSTrond Myklebust }
23761a81bb8aSTrond Myklebust 
23773a505845STrond Myklebust static unsigned long
23783a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2379979df72eSTrond Myklebust {
2380979df72eSTrond Myklebust 	LIST_HEAD(head);
2381aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2382979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
23831ab6c499SDave Chinner 	long freed = 0;
2384979df72eSTrond Myklebust 
2385a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2386aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2387979df72eSTrond Myklebust 		struct inode *inode;
2388979df72eSTrond Myklebust 
2389979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2390979df72eSTrond Myklebust 			break;
23919c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2392979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2393979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2394979df72eSTrond Myklebust 			goto remove_lru_entry;
2395979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2396979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2397979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2398979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
23991ab6c499SDave Chinner 		freed++;
2400979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2401979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2402979df72eSTrond Myklebust 					&nfs_access_lru_list);
2403979df72eSTrond Myklebust 		else {
2404979df72eSTrond Myklebust remove_lru_entry:
2405979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
24064e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2407979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
24084e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2409979df72eSTrond Myklebust 		}
241059844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2411979df72eSTrond Myklebust 	}
2412979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
24131a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
24141ab6c499SDave Chinner 	return freed;
24151ab6c499SDave Chinner }
24161ab6c499SDave Chinner 
24171ab6c499SDave Chinner unsigned long
24183a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
24193a505845STrond Myklebust {
24203a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
24213a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
24223a505845STrond Myklebust 
24233a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
24243a505845STrond Myklebust 		return SHRINK_STOP;
24253a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
24263a505845STrond Myklebust }
24273a505845STrond Myklebust 
24283a505845STrond Myklebust 
24293a505845STrond Myklebust unsigned long
24301ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
24311ab6c499SDave Chinner {
243255f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2433979df72eSTrond Myklebust }
2434979df72eSTrond Myklebust 
24353a505845STrond Myklebust static void
24363a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
24373a505845STrond Myklebust {
24383a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
24393a505845STrond Myklebust 	unsigned long diff;
24403a505845STrond Myklebust 	unsigned int nr_to_scan;
24413a505845STrond Myklebust 
24423a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
24433a505845STrond Myklebust 		return;
24443a505845STrond Myklebust 	nr_to_scan = 100;
24453a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
24463a505845STrond Myklebust 	if (diff < nr_to_scan)
24473a505845STrond Myklebust 		nr_to_scan = diff;
24483a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
24493a505845STrond Myklebust }
24503a505845STrond Myklebust 
24511a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
24521c3c07e9STrond Myklebust {
24531c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
24541a81bb8aSTrond Myklebust 	struct rb_node *n;
24551c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
24561c3c07e9STrond Myklebust 
24571c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
24581c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
24591c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
24601c3c07e9STrond Myklebust 		rb_erase(n, root_node);
24611a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
24621c3c07e9STrond Myklebust 	}
24631c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
24641c3c07e9STrond Myklebust }
24651c3c07e9STrond Myklebust 
24661c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
24671c3c07e9STrond Myklebust {
24681a81bb8aSTrond Myklebust 	LIST_HEAD(head);
24691a81bb8aSTrond Myklebust 
24701a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
24711a81bb8aSTrond Myklebust 		return;
2472cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2473cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
24741a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2475cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2476cfcea3e8STrond Myklebust 
24771c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
24781a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
24791a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
24801a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
24811a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
24821c3c07e9STrond Myklebust }
24831c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
24841c3c07e9STrond Myklebust 
2485b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
24861c3c07e9STrond Myklebust {
24871c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
24881c3c07e9STrond Myklebust 
24891c3c07e9STrond Myklebust 	while (n != NULL) {
2490b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2491b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2492b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
24931c3c07e9STrond Myklebust 
2494b68572e0SNeilBrown 		if (cmp < 0)
24951c3c07e9STrond Myklebust 			n = n->rb_left;
2496b68572e0SNeilBrown 		else if (cmp > 0)
24971c3c07e9STrond Myklebust 			n = n->rb_right;
24981c3c07e9STrond Myklebust 		else
24991c3c07e9STrond Myklebust 			return entry;
25001c3c07e9STrond Myklebust 	}
25011c3c07e9STrond Myklebust 	return NULL;
25021c3c07e9STrond Myklebust }
25031c3c07e9STrond Myklebust 
2504d2ae4f8bSFrank van der Linden static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
25051da177e4SLinus Torvalds {
250655296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
25071c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
250857b69181STrond Myklebust 	bool retry = true;
250957b69181STrond Myklebust 	int err;
25101da177e4SLinus Torvalds 
25111c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
251257b69181STrond Myklebust 	for(;;) {
25131c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
25141c3c07e9STrond Myklebust 			goto out_zap;
25151c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
251657b69181STrond Myklebust 		err = -ENOENT;
25171c3c07e9STrond Myklebust 		if (cache == NULL)
25181c3c07e9STrond Myklebust 			goto out;
251957b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
252021c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
252157b69181STrond Myklebust 			break;
25225c965db8STrond Myklebust 		if (!retry)
25235c965db8STrond Myklebust 			break;
252457b69181STrond Myklebust 		err = -ECHILD;
252557b69181STrond Myklebust 		if (!may_block)
252657b69181STrond Myklebust 			goto out;
252757b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
252857b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
252957b69181STrond Myklebust 		if (err)
253057b69181STrond Myklebust 			return err;
253157b69181STrond Myklebust 		spin_lock(&inode->i_lock);
253257b69181STrond Myklebust 		retry = false;
253357b69181STrond Myklebust 	}
25341c3c07e9STrond Myklebust 	res->cred = cache->cred;
25351c3c07e9STrond Myklebust 	res->mask = cache->mask;
2536cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
25371c3c07e9STrond Myklebust 	err = 0;
25381c3c07e9STrond Myklebust out:
25391c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
25401c3c07e9STrond Myklebust 	return err;
25411c3c07e9STrond Myklebust out_zap:
25421a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
25431a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
25441c3c07e9STrond Myklebust 	return -ENOENT;
25451c3c07e9STrond Myklebust }
25461c3c07e9STrond Myklebust 
2547b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2548f682a398SNeilBrown {
2549f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2550f682a398SNeilBrown 	 * but do it without locking.
2551f682a398SNeilBrown 	 */
2552f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2553f682a398SNeilBrown 	struct nfs_access_entry *cache;
2554f682a398SNeilBrown 	int err = -ECHILD;
2555f682a398SNeilBrown 	struct list_head *lh;
2556f682a398SNeilBrown 
2557f682a398SNeilBrown 	rcu_read_lock();
2558f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2559f682a398SNeilBrown 		goto out;
25609f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2561f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2562f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
25639a206de2STrond Myklebust 	    cred_fscmp(cred, cache->cred) != 0)
2564f682a398SNeilBrown 		cache = NULL;
2565f682a398SNeilBrown 	if (cache == NULL)
2566f682a398SNeilBrown 		goto out;
256721c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2568f682a398SNeilBrown 		goto out;
2569f682a398SNeilBrown 	res->cred = cache->cred;
2570f682a398SNeilBrown 	res->mask = cache->mask;
257121c3ba7eSTrond Myklebust 	err = 0;
2572f682a398SNeilBrown out:
2573f682a398SNeilBrown 	rcu_read_unlock();
2574f682a398SNeilBrown 	return err;
2575f682a398SNeilBrown }
2576f682a398SNeilBrown 
2577d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2578d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block)
2579d2ae4f8bSFrank van der Linden {
2580d2ae4f8bSFrank van der Linden 	int status;
2581d2ae4f8bSFrank van der Linden 
2582d2ae4f8bSFrank van der Linden 	status = nfs_access_get_cached_rcu(inode, cred, res);
2583d2ae4f8bSFrank van der Linden 	if (status != 0)
2584d2ae4f8bSFrank van der Linden 		status = nfs_access_get_cached_locked(inode, cred, res,
2585d2ae4f8bSFrank van der Linden 		    may_block);
2586d2ae4f8bSFrank van der Linden 
2587d2ae4f8bSFrank van der Linden 	return status;
2588d2ae4f8bSFrank van der Linden }
2589d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2590d2ae4f8bSFrank van der Linden 
25911c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
25921c3c07e9STrond Myklebust {
2593cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2594cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
25951c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
25961c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
25971c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2598b68572e0SNeilBrown 	int cmp;
25991c3c07e9STrond Myklebust 
26001c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
26011c3c07e9STrond Myklebust 	while (*p != NULL) {
26021c3c07e9STrond Myklebust 		parent = *p;
26031c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2604b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
26051c3c07e9STrond Myklebust 
2606b68572e0SNeilBrown 		if (cmp < 0)
26071c3c07e9STrond Myklebust 			p = &parent->rb_left;
2608b68572e0SNeilBrown 		else if (cmp > 0)
26091c3c07e9STrond Myklebust 			p = &parent->rb_right;
26101c3c07e9STrond Myklebust 		else
26111c3c07e9STrond Myklebust 			goto found;
26121c3c07e9STrond Myklebust 	}
26131c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
26141c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2615cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
26161c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
26171c3c07e9STrond Myklebust 	return;
26181c3c07e9STrond Myklebust found:
26191c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2620cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2621cfcea3e8STrond Myklebust 	list_del(&entry->lru);
26221c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
26231c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds 
26266168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
26271da177e4SLinus Torvalds {
26281c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
26291c3c07e9STrond Myklebust 	if (cache == NULL)
26301c3c07e9STrond Myklebust 		return;
26311c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2632b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
26331da177e4SLinus Torvalds 	cache->mask = set->mask;
26341c3c07e9STrond Myklebust 
2635f682a398SNeilBrown 	/* The above field assignments must be visible
2636f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2637f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2638f682a398SNeilBrown 	 */
2639f682a398SNeilBrown 	smp_wmb();
26401c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2641cfcea3e8STrond Myklebust 
2642cfcea3e8STrond Myklebust 	/* Update accounting */
26434e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2644cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
26454e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2646cfcea3e8STrond Myklebust 
2647cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
26481a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2649cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
26501a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
26511a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
26521a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2653cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2654cfcea3e8STrond Myklebust 	}
26553a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
26561da177e4SLinus Torvalds }
26576168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
26586168f62cSWeston Andros Adamson 
26593c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
26603c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
26613c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
26623c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
26633c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
26643c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2665ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
26663c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
26673c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
266815d4b73aSTrond Myklebust static int
2669ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
267015d4b73aSTrond Myklebust {
267115d4b73aSTrond Myklebust 	int mask = 0;
267215d4b73aSTrond Myklebust 
267315d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
267415d4b73aSTrond Myklebust 		mask |= MAY_READ;
2675ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2676ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
267715d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2678ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
267915d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2680ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2681ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2682ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2683ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
268415d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2685ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2686ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
268715d4b73aSTrond Myklebust 	return mask;
268815d4b73aSTrond Myklebust }
268915d4b73aSTrond Myklebust 
26906168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
26916168f62cSWeston Andros Adamson {
2692bd8b2441STrond Myklebust 	entry->mask = access_result;
26936168f62cSWeston Andros Adamson }
26946168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
26951da177e4SLinus Torvalds 
2696b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
26971da177e4SLinus Torvalds {
26981da177e4SLinus Torvalds 	struct nfs_access_entry cache;
269957b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2700e8194b7dSTrond Myklebust 	int cache_mask = -1;
27011da177e4SLinus Torvalds 	int status;
27021da177e4SLinus Torvalds 
2703f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2704f4ce1299STrond Myklebust 
270557b69181STrond Myklebust 	status = nfs_access_get_cached(inode, cred, &cache, may_block);
27061da177e4SLinus Torvalds 	if (status == 0)
2707f4ce1299STrond Myklebust 		goto out_cached;
27081da177e4SLinus Torvalds 
2709f3324a2aSNeilBrown 	status = -ECHILD;
271057b69181STrond Myklebust 	if (!may_block)
2711f3324a2aSNeilBrown 		goto out;
2712f3324a2aSNeilBrown 
27131750d929SAnna Schumaker 	/*
27141750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
27151750d929SAnna Schumaker 	 */
27161750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
271772832a24SFrank van der Linden 	if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
271872832a24SFrank van der Linden 		cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
271972832a24SFrank van der Linden 		    NFS_ACCESS_XALIST;
272072832a24SFrank van der Linden 	}
27211750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
27221750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
27231750d929SAnna Schumaker 	else
27241750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
27251da177e4SLinus Torvalds 	cache.cred = cred;
27261da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2727a71ee337SSuresh Jayaraman 	if (status != 0) {
2728a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2729a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
273093ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
273193ce4af7STrond Myklebust 			else
273293ce4af7STrond Myklebust 				nfs_zap_caches(inode);
2733a71ee337SSuresh Jayaraman 		}
2734f4ce1299STrond Myklebust 		goto out;
2735a71ee337SSuresh Jayaraman 	}
27361da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2737f4ce1299STrond Myklebust out_cached:
2738ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2739bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2740f4ce1299STrond Myklebust 		status = -EACCES;
27411da177e4SLinus Torvalds out:
2742e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
2743f4ce1299STrond Myklebust 	return status;
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
2746af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2747af22f94aSTrond Myklebust {
2748af22f94aSTrond Myklebust 	int mask = 0;
2749af22f94aSTrond Myklebust 
2750f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2751f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2752f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2753f8d9a897SWeston Andros Adamson 	} else {
27548a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2755af22f94aSTrond Myklebust 			mask |= MAY_READ;
27568a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2757af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2758f8d9a897SWeston Andros Adamson 	}
2759f8d9a897SWeston Andros Adamson 
2760af22f94aSTrond Myklebust 	return mask;
2761af22f94aSTrond Myklebust }
2762af22f94aSTrond Myklebust 
2763b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2764af22f94aSTrond Myklebust {
2765af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2766af22f94aSTrond Myklebust }
276789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2768af22f94aSTrond Myklebust 
27695c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
27705c5fc09aSTrond Myklebust {
27715c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
277221c3ba7eSTrond Myklebust 	int ret = 0;
27735c5fc09aSTrond Myklebust 
27743825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
27753825827eSTrond Myklebust 		return 0;
2776cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
27775c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
277821c3ba7eSTrond Myklebust 			return -ECHILD;
277921c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
278021c3ba7eSTrond Myklebust 	}
27815c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
27825c5fc09aSTrond Myklebust 		ret = -EACCES;
27835c5fc09aSTrond Myklebust 	return ret;
27845c5fc09aSTrond Myklebust }
27855c5fc09aSTrond Myklebust 
278610556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
27871da177e4SLinus Torvalds {
2788b68572e0SNeilBrown 	const struct cred *cred = current_cred();
27891da177e4SLinus Torvalds 	int res = 0;
27901da177e4SLinus Torvalds 
279191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
279291d5b470SChuck Lever 
2793e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
27941da177e4SLinus Torvalds 		goto out;
27951da177e4SLinus Torvalds 	/* Is this sys_access() ? */
27969cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
27971da177e4SLinus Torvalds 		goto force_lookup;
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
28001da177e4SLinus Torvalds 		case S_IFLNK:
28011da177e4SLinus Torvalds 			goto out;
28021da177e4SLinus Torvalds 		case S_IFREG:
2803762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2804762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2805762674f8STrond Myklebust 				return 0;
28061da177e4SLinus Torvalds 			break;
28071da177e4SLinus Torvalds 		case S_IFDIR:
28081da177e4SLinus Torvalds 			/*
28091da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
28101da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
28111da177e4SLinus Torvalds 			 */
28121da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
28131da177e4SLinus Torvalds 				goto out;
28141da177e4SLinus Torvalds 	}
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds force_lookup:
28171da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
28181da177e4SLinus Torvalds 		goto out_notsup;
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
28211da177e4SLinus Torvalds out:
28225c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
28235c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2824f696a365SMiklos Szeredi 
28251e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
28261e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
28271da177e4SLinus Torvalds 	return res;
28281da177e4SLinus Torvalds out_notsup:
2829d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2830d51ac1a8SNeilBrown 		return -ECHILD;
2831d51ac1a8SNeilBrown 
28321da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
28331da177e4SLinus Torvalds 	if (res == 0)
28342830ba7fSAl Viro 		res = generic_permission(inode, mask);
28351e7cb3dcSChuck Lever 	goto out;
28361da177e4SLinus Torvalds }
2837ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds /*
28401da177e4SLinus Torvalds  * Local variables:
28411da177e4SLinus Torvalds  *  version-control: t
28421da177e4SLinus Torvalds  *  kept-new-versions: 5
28431da177e4SLinus Torvalds  * End:
28441da177e4SLinus Torvalds  */
2845