xref: /openbmc/linux/fs/nfs/dir.c (revision c8f0523b)
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 
21b6459415SJakub Kicinski #include <linux/compat.h>
22ddda8e0aSBryan Schumaker #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/time.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/stat.h>
261da177e4SLinus Torvalds #include <linux/fcntl.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
321da177e4SLinus Torvalds #include <linux/nfs_fs.h>
331da177e4SLinus Torvalds #include <linux/nfs_mount.h>
341da177e4SLinus Torvalds #include <linux/pagemap.h>
35873101b3SChuck Lever #include <linux/pagevec.h>
361da177e4SLinus Torvalds #include <linux/namei.h>
3754ceac45SDavid Howells #include <linux/mount.h>
38a0b8cab3SMel Gorman #include <linux/swap.h>
39e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
4004e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4164c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include "delegation.h"
4491d5b470SChuck Lever #include "iostat.h"
454c30d56eSAdrian Bunk #include "internal.h"
46cd9a1c0eSTrond Myklebust #include "fscache.h"
471da177e4SLinus Torvalds 
48f4ce1299STrond Myklebust #include "nfstrace.h"
49f4ce1299STrond Myklebust 
501da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
53480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5423db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5502c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
56f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5711de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
581da177e4SLinus Torvalds 
594b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
60f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
611da177e4SLinus Torvalds 	.read		= generic_read_dir,
6293a6ab7bSTrond Myklebust 	.iterate_shared	= nfs_readdir,
631da177e4SLinus Torvalds 	.open		= nfs_opendir,
64480c2006SBryan Schumaker 	.release	= nfs_closedir,
651da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
6811de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6911de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
70d1bacf9eSBryan Schumaker };
71d1bacf9eSBryan Schumaker 
72281f31b2STrond Myklebust static struct nfs_open_dir_context *
73281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir)
74480c2006SBryan Schumaker {
75311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
76480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
77281f31b2STrond Myklebust 
78281f31b2STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
79480c2006SBryan Schumaker 	if (ctx != NULL) {
80311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
81311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
821c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
831c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
84ac46b3d7STrond Myklebust 			nfs_set_cache_invalid(dir,
85ac46b3d7STrond Myklebust 					      NFS_INO_INVALID_DATA |
86ac46b3d7STrond Myklebust 						      NFS_INO_REVAL_FORCED);
87311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
88ff81dfb5STrond Myklebust 		clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
89d1e32ea3STrond Myklebust 		memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
90311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
91480c2006SBryan Schumaker 		return ctx;
92480c2006SBryan Schumaker 	}
930c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
940c030806STrond Myklebust }
95480c2006SBryan Schumaker 
96311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
97480c2006SBryan Schumaker {
98311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
99311324adSTrond Myklebust 	list_del(&ctx->list);
100311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
101480c2006SBryan Schumaker 	kfree(ctx);
102480c2006SBryan Schumaker }
103480c2006SBryan Schumaker 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Open file
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds static int
1081da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1091da177e4SLinus Torvalds {
110480c2006SBryan Schumaker 	int res = 0;
111480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1121da177e4SLinus Torvalds 
1136de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
114cc0dd2d1SChuck Lever 
115cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1161e7cb3dcSChuck Lever 
11793b8959aSTrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode);
118480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
119480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
120480c2006SBryan Schumaker 		goto out;
121480c2006SBryan Schumaker 	}
122480c2006SBryan Schumaker 	filp->private_data = ctx;
123480c2006SBryan Schumaker out:
1241da177e4SLinus Torvalds 	return res;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
127480c2006SBryan Schumaker static int
128480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
129480c2006SBryan Schumaker {
130a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
131480c2006SBryan Schumaker 	return 0;
132480c2006SBryan Schumaker }
133480c2006SBryan Schumaker 
134d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
135d1bacf9eSBryan Schumaker 	u64 cookie;
136d1bacf9eSBryan Schumaker 	u64 ino;
137a52a8a6aSTrond Myklebust 	const char *name;
138a52a8a6aSTrond Myklebust 	unsigned int name_len;
1390b26a0bfSTrond Myklebust 	unsigned char d_type;
140d1bacf9eSBryan Schumaker };
141d1bacf9eSBryan Schumaker 
142d1bacf9eSBryan Schumaker struct nfs_cache_array {
143d09e673fSTrond Myklebust 	u64 change_attr;
144d1bacf9eSBryan Schumaker 	u64 last_cookie;
145b1e21c97STrond Myklebust 	unsigned int size;
146b1e21c97STrond Myklebust 	unsigned char page_full : 1,
147762567b7STrond Myklebust 		      page_is_eof : 1,
148762567b7STrond Myklebust 		      cookies_are_ordered : 1;
1495601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
150d1bacf9eSBryan Schumaker };
151d1bacf9eSBryan Schumaker 
1526c981effSTrond Myklebust struct nfs_readdir_descriptor {
1531da177e4SLinus Torvalds 	struct file	*file;
1541da177e4SLinus Torvalds 	struct page	*page;
15523db8620SAl Viro 	struct dir_context *ctx;
1561f1d4aa4STrond Myklebust 	pgoff_t		page_index;
1572e7a4641STrond Myklebust 	u64		dir_cookie;
1580aded708STrond Myklebust 	u64		last_cookie;
1592e7a4641STrond Myklebust 	u64		dup_cookie;
160f0dd2136STrond Myklebust 	loff_t		current_index;
16159e356a9STrond Myklebust 	loff_t		prev_index;
162d1bacf9eSBryan Schumaker 
163b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
164a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1651f4eab7eSNeil Brown 	unsigned long	timestamp;
1664704f0e2STrond Myklebust 	unsigned long	gencount;
1672e7a4641STrond Myklebust 	unsigned long	attr_gencount;
168d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
1692e7a4641STrond Myklebust 	signed char duped;
170a7a3b1e9SBenjamin Coddington 	bool plus;
171e1d2699bSTrond Myklebust 	bool eob;
172a7a3b1e9SBenjamin Coddington 	bool eof;
1736c981effSTrond Myklebust };
1741da177e4SLinus Torvalds 
1751f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array)
1761f1d4aa4STrond Myklebust {
1771f1d4aa4STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
1781f1d4aa4STrond Myklebust }
1791f1d4aa4STrond Myklebust 
180d09e673fSTrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie,
181d09e673fSTrond Myklebust 					u64 change_attr)
1824b310319STrond Myklebust {
1834b310319STrond Myklebust 	struct nfs_cache_array *array;
1844b310319STrond Myklebust 
1854b310319STrond Myklebust 	array = kmap_atomic(page);
1861f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
187d09e673fSTrond Myklebust 	array->change_attr = change_attr;
1881f1d4aa4STrond Myklebust 	array->last_cookie = last_cookie;
189762567b7STrond Myklebust 	array->cookies_are_ordered = 1;
1904b310319STrond Myklebust 	kunmap_atomic(array);
1914b310319STrond Myklebust }
1924b310319STrond Myklebust 
193d1bacf9eSBryan Schumaker /*
194d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
195d1bacf9eSBryan Schumaker  */
196d1bacf9eSBryan Schumaker static
19711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
198d1bacf9eSBryan Schumaker {
19911de3b11STrond Myklebust 	struct nfs_cache_array *array;
200d1bacf9eSBryan Schumaker 	int i;
2018cd51a0cSTrond Myklebust 
2022b86ce2dSCong Wang 	array = kmap_atomic(page);
203d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
204a52a8a6aSTrond Myklebust 		kfree(array->array[i].name);
2051f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
2062b86ce2dSCong Wang 	kunmap_atomic(array);
207d1bacf9eSBryan Schumaker }
208d1bacf9eSBryan Schumaker 
20935df59d3STrond Myklebust static struct page *
21035df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
21135df59d3STrond Myklebust {
21235df59d3STrond Myklebust 	struct page *page = alloc_page(gfp_flags);
21335df59d3STrond Myklebust 	if (page)
214d09e673fSTrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie, 0);
21535df59d3STrond Myklebust 	return page;
21635df59d3STrond Myklebust }
21735df59d3STrond Myklebust 
21835df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page)
21935df59d3STrond Myklebust {
22035df59d3STrond Myklebust 	if (page) {
22135df59d3STrond Myklebust 		nfs_readdir_clear_array(page);
22235df59d3STrond Myklebust 		put_page(page);
22335df59d3STrond Myklebust 	}
22435df59d3STrond Myklebust }
22535df59d3STrond Myklebust 
226b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
227b1e21c97STrond Myklebust {
228b1e21c97STrond Myklebust 	array->page_is_eof = 1;
229b1e21c97STrond Myklebust 	array->page_full = 1;
230b1e21c97STrond Myklebust }
231b1e21c97STrond Myklebust 
232b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
233b1e21c97STrond Myklebust {
234b1e21c97STrond Myklebust 	return array->page_full;
235b1e21c97STrond Myklebust }
236b1e21c97STrond Myklebust 
237d1bacf9eSBryan Schumaker /*
238d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
239d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
240d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
241d1bacf9eSBryan Schumaker  */
242a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
243d1bacf9eSBryan Schumaker {
244a52a8a6aSTrond Myklebust 	const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
245a52a8a6aSTrond Myklebust 
24604e4bd1cSCatalin Marinas 	/*
24704e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
24804e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
24904e4bd1cSCatalin Marinas 	 */
250a52a8a6aSTrond Myklebust 	if (ret != NULL)
251a52a8a6aSTrond Myklebust 		kmemleak_not_leak(ret);
252a52a8a6aSTrond Myklebust 	return ret;
253d1bacf9eSBryan Schumaker }
254d1bacf9eSBryan Schumaker 
2550b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void)
2560b2662b7STrond Myklebust {
2570b2662b7STrond Myklebust 	return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
2580b2662b7STrond Myklebust 	       sizeof(struct nfs_cache_array_entry);
2590b2662b7STrond Myklebust }
2600b2662b7STrond Myklebust 
261b1e21c97STrond Myklebust /*
262b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
263b1e21c97STrond Myklebust  */
264b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
265b1e21c97STrond Myklebust {
266b1e21c97STrond Myklebust 	if (array->page_full)
267b1e21c97STrond Myklebust 		return -ENOSPC;
2680b2662b7STrond Myklebust 	if (array->size == nfs_readdir_array_maxentries()) {
269b1e21c97STrond Myklebust 		array->page_full = 1;
270b1e21c97STrond Myklebust 		return -ENOSPC;
271b1e21c97STrond Myklebust 	}
272d1bacf9eSBryan Schumaker 	return 0;
273d1bacf9eSBryan Schumaker }
274d1bacf9eSBryan Schumaker 
275d1bacf9eSBryan Schumaker static
276d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
277d1bacf9eSBryan Schumaker {
278a52a8a6aSTrond Myklebust 	struct nfs_cache_array *array;
2794a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
280a52a8a6aSTrond Myklebust 	const char *name;
2814a201d6eSTrond Myklebust 	int ret;
2824a201d6eSTrond Myklebust 
283a52a8a6aSTrond Myklebust 	name = nfs_readdir_copy_name(entry->name, entry->len);
284a52a8a6aSTrond Myklebust 	if (!name)
285a52a8a6aSTrond Myklebust 		return -ENOMEM;
2863020093fSTrond Myklebust 
287a52a8a6aSTrond Myklebust 	array = kmap_atomic(page);
288b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
289a52a8a6aSTrond Myklebust 	if (ret) {
290a52a8a6aSTrond Myklebust 		kfree(name);
2913020093fSTrond Myklebust 		goto out;
292a52a8a6aSTrond Myklebust 	}
2933020093fSTrond Myklebust 
294b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
2954a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2964a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2970b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
298a52a8a6aSTrond Myklebust 	cache_entry->name_len = entry->len;
299a52a8a6aSTrond Myklebust 	cache_entry->name = name;
300d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
301762567b7STrond Myklebust 	if (array->last_cookie <= cache_entry->cookie)
302762567b7STrond Myklebust 		array->cookies_are_ordered = 0;
3038cd51a0cSTrond Myklebust 	array->size++;
30447c716cbSTrond Myklebust 	if (entry->eof != 0)
305b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
3064a201d6eSTrond Myklebust out:
307a52a8a6aSTrond Myklebust 	kunmap_atomic(array);
3084a201d6eSTrond Myklebust 	return ret;
309d1bacf9eSBryan Schumaker }
310d1bacf9eSBryan Schumaker 
311d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie,
312d09e673fSTrond Myklebust 				      u64 change_attr)
313d09e673fSTrond Myklebust {
314d09e673fSTrond Myklebust 	struct nfs_cache_array *array = kmap_atomic(page);
315d09e673fSTrond Myklebust 	int ret = true;
316d09e673fSTrond Myklebust 
317d09e673fSTrond Myklebust 	if (array->change_attr != change_attr)
318d09e673fSTrond Myklebust 		ret = false;
319d09e673fSTrond Myklebust 	if (array->size > 0 && array->array[0].cookie != last_cookie)
320d09e673fSTrond Myklebust 		ret = false;
321d09e673fSTrond Myklebust 	kunmap_atomic(array);
322d09e673fSTrond Myklebust 	return ret;
323d09e673fSTrond Myklebust }
324d09e673fSTrond Myklebust 
325d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
326d09e673fSTrond Myklebust {
327d09e673fSTrond Myklebust 	unlock_page(page);
328d09e673fSTrond Myklebust 	put_page(page);
329d09e673fSTrond Myklebust }
330d09e673fSTrond Myklebust 
3311f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
3321f1d4aa4STrond Myklebust 						pgoff_t index, u64 last_cookie)
3331f1d4aa4STrond Myklebust {
3341f1d4aa4STrond Myklebust 	struct page *page;
335d09e673fSTrond Myklebust 	u64 change_attr;
3361f1d4aa4STrond Myklebust 
3371f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
338d09e673fSTrond Myklebust 	if (!page)
339d09e673fSTrond Myklebust 		return NULL;
340d09e673fSTrond Myklebust 	change_attr = inode_peek_iversion_raw(mapping->host);
341d09e673fSTrond Myklebust 	if (PageUptodate(page)) {
342d09e673fSTrond Myklebust 		if (nfs_readdir_page_validate(page, last_cookie, change_attr))
343d09e673fSTrond Myklebust 			return page;
344d09e673fSTrond Myklebust 		nfs_readdir_clear_array(page);
3451f1d4aa4STrond Myklebust 	}
346d09e673fSTrond Myklebust 	nfs_readdir_page_init_array(page, last_cookie, change_attr);
347d09e673fSTrond Myklebust 	SetPageUptodate(page);
3481f1d4aa4STrond Myklebust 	return page;
3491f1d4aa4STrond Myklebust }
3501f1d4aa4STrond Myklebust 
3510b2662b7STrond Myklebust static loff_t nfs_readdir_page_offset(struct page *page)
3520b2662b7STrond Myklebust {
3530b2662b7STrond Myklebust 	return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries();
3540b2662b7STrond Myklebust }
3550b2662b7STrond Myklebust 
3561f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
3571f1d4aa4STrond Myklebust {
3581f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3591f1d4aa4STrond Myklebust 	u64 ret;
3601f1d4aa4STrond Myklebust 
3611f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3621f1d4aa4STrond Myklebust 	ret = array->last_cookie;
3631f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3641f1d4aa4STrond Myklebust 	return ret;
3651f1d4aa4STrond Myklebust }
3661f1d4aa4STrond Myklebust 
3671f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
3681f1d4aa4STrond Myklebust {
3691f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3701f1d4aa4STrond Myklebust 	bool ret;
3711f1d4aa4STrond Myklebust 
3721f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3731f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
3741f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3751f1d4aa4STrond Myklebust 	return ret;
3761f1d4aa4STrond Myklebust }
3771f1d4aa4STrond Myklebust 
378b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
379b1e21c97STrond Myklebust {
380b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
381b1e21c97STrond Myklebust 
382b1e21c97STrond Myklebust 	array = kmap_atomic(page);
383b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
384b1e21c97STrond Myklebust 	kunmap_atomic(array);
385b1e21c97STrond Myklebust }
386b1e21c97STrond Myklebust 
3873b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
3883b2a09f1STrond Myklebust 					      pgoff_t index, u64 cookie)
3893b2a09f1STrond Myklebust {
3903b2a09f1STrond Myklebust 	struct page *page;
3913b2a09f1STrond Myklebust 
3923b2a09f1STrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
3933b2a09f1STrond Myklebust 	if (page) {
3943b2a09f1STrond Myklebust 		if (nfs_readdir_page_last_cookie(page) == cookie)
3953b2a09f1STrond Myklebust 			return page;
3963b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
3973b2a09f1STrond Myklebust 	}
3983b2a09f1STrond Myklebust 	return NULL;
3993b2a09f1STrond Myklebust }
4003b2a09f1STrond Myklebust 
40159e356a9STrond Myklebust static inline
40259e356a9STrond Myklebust int is_32bit_api(void)
40359e356a9STrond Myklebust {
40459e356a9STrond Myklebust #ifdef CONFIG_COMPAT
40559e356a9STrond Myklebust 	return in_compat_syscall();
40659e356a9STrond Myklebust #else
40759e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
40859e356a9STrond Myklebust #endif
40959e356a9STrond Myklebust }
41059e356a9STrond Myklebust 
41159e356a9STrond Myklebust static
41259e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
41359e356a9STrond Myklebust {
41459e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
41559e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
41659e356a9STrond Myklebust 		return false;
41759e356a9STrond Myklebust 	return true;
41859e356a9STrond Myklebust }
41959e356a9STrond Myklebust 
420*c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
421*c8f0523bSTrond Myklebust 					struct nfs_readdir_descriptor *desc)
422*c8f0523bSTrond Myklebust {
423*c8f0523bSTrond Myklebust 	if (array->page_full) {
424*c8f0523bSTrond Myklebust 		desc->last_cookie = array->last_cookie;
425*c8f0523bSTrond Myklebust 		desc->current_index += array->size;
426*c8f0523bSTrond Myklebust 		desc->cache_entry_index = 0;
427*c8f0523bSTrond Myklebust 		desc->page_index++;
428*c8f0523bSTrond Myklebust 	} else
429*c8f0523bSTrond Myklebust 		desc->last_cookie = array->array[0].cookie;
430*c8f0523bSTrond Myklebust }
431*c8f0523bSTrond Myklebust 
4326c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
4336c981effSTrond Myklebust 				      struct nfs_readdir_descriptor *desc)
434d1bacf9eSBryan Schumaker {
43523db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
436d1bacf9eSBryan Schumaker 	unsigned int index;
437d1bacf9eSBryan Schumaker 
438d1bacf9eSBryan Schumaker 	if (diff < 0)
439d1bacf9eSBryan Schumaker 		goto out_eof;
440d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
441b1e21c97STrond Myklebust 		if (array->page_is_eof)
442d1bacf9eSBryan Schumaker 			goto out_eof;
443*c8f0523bSTrond Myklebust 		nfs_readdir_seek_next_array(array, desc);
444d1bacf9eSBryan Schumaker 		return -EAGAIN;
445d1bacf9eSBryan Schumaker 	}
446d1bacf9eSBryan Schumaker 
447d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
4482e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
449d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
450d1bacf9eSBryan Schumaker 	return 0;
451d1bacf9eSBryan Schumaker out_eof:
4526089dd0dSThomas Meyer 	desc->eof = true;
453d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
454d1bacf9eSBryan Schumaker }
455d1bacf9eSBryan Schumaker 
456762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
457762567b7STrond Myklebust 					      u64 cookie)
458762567b7STrond Myklebust {
459762567b7STrond Myklebust 	if (!array->cookies_are_ordered)
460762567b7STrond Myklebust 		return true;
461762567b7STrond Myklebust 	/* Optimisation for monotonically increasing cookies */
462762567b7STrond Myklebust 	if (cookie >= array->last_cookie)
463762567b7STrond Myklebust 		return false;
464762567b7STrond Myklebust 	if (array->size && cookie < array->array[0].cookie)
465762567b7STrond Myklebust 		return false;
466762567b7STrond Myklebust 	return true;
467762567b7STrond Myklebust }
468762567b7STrond Myklebust 
4696c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
4706c981effSTrond Myklebust 					 struct nfs_readdir_descriptor *desc)
471d1bacf9eSBryan Schumaker {
472d1bacf9eSBryan Schumaker 	int i;
4738ef2ce3eSBryan Schumaker 	loff_t new_pos;
474d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
475d1bacf9eSBryan Schumaker 
476762567b7STrond Myklebust 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
477762567b7STrond Myklebust 		goto check_eof;
478762567b7STrond Myklebust 
479d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
4802e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
481496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
4820c030806STrond Myklebust 
4830b2662b7STrond Myklebust 			new_pos = nfs_readdir_page_offset(desc->page) + i;
484d09e673fSTrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount) {
4852e7a4641STrond Myklebust 				desc->duped = 0;
4862e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
48759e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
4882e7a4641STrond Myklebust 				if (desc->duped > 0
4892e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
4900c030806STrond Myklebust 					if (printk_ratelimit()) {
4916de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
4920c030806STrond Myklebust 								"Please contact your server vendor.  "
493a52a8a6aSTrond Myklebust 								"The file: %s has duplicate cookie %llu\n",
494a52a8a6aSTrond Myklebust 								desc->file, array->array[i].name, desc->dir_cookie);
4950c030806STrond Myklebust 					}
4960c030806STrond Myklebust 					status = -ELOOP;
4970c030806STrond Myklebust 					goto out;
4980c030806STrond Myklebust 				}
4992e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
5002e7a4641STrond Myklebust 				desc->duped = -1;
5018ef2ce3eSBryan Schumaker 			}
50259e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
5032e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
50459e356a9STrond Myklebust 			else
50523db8620SAl Viro 				desc->ctx->pos = new_pos;
50659e356a9STrond Myklebust 			desc->prev_index = new_pos;
5078cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
50847c716cbSTrond Myklebust 			return 0;
5098cd51a0cSTrond Myklebust 		}
5108cd51a0cSTrond Myklebust 	}
511762567b7STrond Myklebust check_eof:
512b1e21c97STrond Myklebust 	if (array->page_is_eof) {
513d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
5142e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
5156089dd0dSThomas Meyer 			desc->eof = true;
516*c8f0523bSTrond Myklebust 	} else
517*c8f0523bSTrond Myklebust 		nfs_readdir_seek_next_array(array, desc);
5180c030806STrond Myklebust out:
519d1bacf9eSBryan Schumaker 	return status;
520d1bacf9eSBryan Schumaker }
521d1bacf9eSBryan Schumaker 
5226c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
523d1bacf9eSBryan Schumaker {
524d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
52547c716cbSTrond Myklebust 	int status;
526d1bacf9eSBryan Schumaker 
527ed09222dSTrond Myklebust 	array = kmap_atomic(desc->page);
528d1bacf9eSBryan Schumaker 
5292e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
530d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
531d1bacf9eSBryan Schumaker 	else
532d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
533d1bacf9eSBryan Schumaker 
534ed09222dSTrond Myklebust 	kunmap_atomic(array);
535d1bacf9eSBryan Schumaker 	return status;
536d1bacf9eSBryan Schumaker }
537d1bacf9eSBryan Schumaker 
538d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
53993b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
540b593c09fSTrond Myklebust 				  __be32 *verf, u64 cookie,
541b593c09fSTrond Myklebust 				  struct page **pages, size_t bufsize,
542b593c09fSTrond Myklebust 				  __be32 *verf_res)
543d1bacf9eSBryan Schumaker {
54482e22a5eSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
54582e22a5eSTrond Myklebust 	struct nfs_readdir_arg arg = {
54682e22a5eSTrond Myklebust 		.dentry = file_dentry(desc->file),
54782e22a5eSTrond Myklebust 		.cred = desc->file->f_cred,
548b593c09fSTrond Myklebust 		.verf = verf,
54982e22a5eSTrond Myklebust 		.cookie = cookie,
55082e22a5eSTrond Myklebust 		.pages = pages,
55182e22a5eSTrond Myklebust 		.page_len = bufsize,
55282e22a5eSTrond Myklebust 		.plus = desc->plus,
55382e22a5eSTrond Myklebust 	};
55482e22a5eSTrond Myklebust 	struct nfs_readdir_res res = {
55582e22a5eSTrond Myklebust 		.verf = verf_res,
55682e22a5eSTrond Myklebust 	};
5574704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
5581da177e4SLinus Torvalds 	int		error;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds  again:
5611da177e4SLinus Torvalds 	timestamp = jiffies;
5624704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
563a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
56482e22a5eSTrond Myklebust 	error = NFS_PROTO(inode)->readdir(&arg, &res);
5651da177e4SLinus Torvalds 	if (error < 0) {
5661da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
5671da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
5681da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
5693a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
57082e22a5eSTrond Myklebust 			desc->plus = arg.plus = false;
5711da177e4SLinus Torvalds 			goto again;
5721da177e4SLinus Torvalds 		}
5731da177e4SLinus Torvalds 		goto error;
5741da177e4SLinus Torvalds 	}
5751f4eab7eSNeil Brown 	desc->timestamp = timestamp;
5764704f0e2STrond Myklebust 	desc->gencount = gencount;
577d1bacf9eSBryan Schumaker error:
578d1bacf9eSBryan Schumaker 	return error;
579d1bacf9eSBryan Schumaker }
580d1bacf9eSBryan Schumaker 
5816c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
582573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
583d1bacf9eSBryan Schumaker {
58459e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
585573c4e1eSChuck Lever 	int error;
586d1bacf9eSBryan Schumaker 
58759e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
588573c4e1eSChuck Lever 	if (error)
589573c4e1eSChuck Lever 		return error;
590d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
591d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
592d1bacf9eSBryan Schumaker 	return 0;
593d1bacf9eSBryan Schumaker }
594d1bacf9eSBryan Schumaker 
595fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
596fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
597fa923369STrond Myklebust  */
598d39ab9deSBryan Schumaker static
599d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
600d39ab9deSBryan Schumaker {
601d8fdb47fSTrond Myklebust 	struct inode *inode;
602fa923369STrond Myklebust 	struct nfs_inode *nfsi;
603fa923369STrond Myklebust 
6042b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
6052b0143b5SDavid Howells 		return 0;
606fa923369STrond Myklebust 
607d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
608d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
609d8fdb47fSTrond Myklebust 		return 0;
610d8fdb47fSTrond Myklebust 
611d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
6127dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
613d39ab9deSBryan Schumaker 		return 0;
6147dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
6157dc72d5fSTrond Myklebust 		return 0;
6167dc72d5fSTrond Myklebust 	return 1;
617d39ab9deSBryan Schumaker }
618d39ab9deSBryan Schumaker 
619d39ab9deSBryan Schumaker static
62023db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
621d69ee9b8STrond Myklebust {
622d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
623d69ee9b8STrond Myklebust 		return false;
624d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
625d69ee9b8STrond Myklebust 		return true;
62623db8620SAl Viro 	if (ctx->pos == 0)
627d69ee9b8STrond Myklebust 		return true;
628d69ee9b8STrond Myklebust 	return false;
629d69ee9b8STrond Myklebust }
630d69ee9b8STrond Myklebust 
631d69ee9b8STrond Myklebust /*
63263519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
63363519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
634d69ee9b8STrond Myklebust  * directory.
635d69ee9b8STrond Myklebust  */
636d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
637d69ee9b8STrond Myklebust {
63863519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
63963519fbcSTrond Myklebust 
64063519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
64163519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
64263519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
643d69ee9b8STrond Myklebust }
644d69ee9b8STrond Myklebust 
645311324adSTrond Myklebust /*
646311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
647311324adSTrond Myklebust  *
648311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
649311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
650311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
651311324adSTrond Myklebust  * cache flush.
652311324adSTrond Myklebust  */
653311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
654311324adSTrond Myklebust {
65563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
65663519fbcSTrond Myklebust 
65763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
65863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
65963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
660ff81dfb5STrond Myklebust 		set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
661311324adSTrond Myklebust 	}
662311324adSTrond Myklebust }
663311324adSTrond Myklebust 
664d69ee9b8STrond Myklebust static
665a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
666a1147b82STrond Myklebust 		unsigned long dir_verifier)
667d39ab9deSBryan Schumaker {
66826fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
6699ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
6704a201d6eSTrond Myklebust 	struct dentry *dentry;
6714a201d6eSTrond Myklebust 	struct dentry *alias;
672d39ab9deSBryan Schumaker 	struct inode *inode;
673aa9c2669SDavid Quigley 	int status;
674d39ab9deSBryan Schumaker 
675fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
676fa923369STrond Myklebust 		return;
6776c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
6786c441c25STrond Myklebust 		return;
67978d04af4STrond Myklebust 	if (filename.len == 0)
68078d04af4STrond Myklebust 		return;
68178d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
68278d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
68378d04af4STrond Myklebust 		return;
68478d04af4STrond Myklebust 	/* ...or '/' */
68578d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
68678d04af4STrond Myklebust 		return;
6874a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
6884a201d6eSTrond Myklebust 		if (filename.len == 1)
6894a201d6eSTrond Myklebust 			return;
6904a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
6914a201d6eSTrond Myklebust 			return;
6924a201d6eSTrond Myklebust 	}
6938387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
694d39ab9deSBryan Schumaker 
6954a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
6969ac3d3e8SAl Viro again:
6979ac3d3e8SAl Viro 	if (!dentry) {
6989ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
6999ac3d3e8SAl Viro 		if (IS_ERR(dentry))
7009ac3d3e8SAl Viro 			return;
7019ac3d3e8SAl Viro 	}
7029ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
7036c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
7046c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
7056c441c25STrond Myklebust 					&entry->fattr->fsid))
7066c441c25STrond Myklebust 			goto out;
707d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
7087dc72d5fSTrond Myklebust 			if (!entry->fh->size)
7097dc72d5fSTrond Myklebust 				goto out;
710a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
7112b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
712aa9c2669SDavid Quigley 			if (!status)
713dd225cb3SAnna Schumaker 				nfs_setsecurity(d_inode(dentry), entry->fattr);
714d39ab9deSBryan Schumaker 			goto out;
715d39ab9deSBryan Schumaker 		} else {
7165542aa2fSEric W. Biederman 			d_invalidate(dentry);
717d39ab9deSBryan Schumaker 			dput(dentry);
7189ac3d3e8SAl Viro 			dentry = NULL;
7199ac3d3e8SAl Viro 			goto again;
720d39ab9deSBryan Schumaker 		}
721d39ab9deSBryan Schumaker 	}
7227dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
7237dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
7247dc72d5fSTrond Myklebust 		goto out;
7257dc72d5fSTrond Myklebust 	}
726d39ab9deSBryan Schumaker 
727cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
72841d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
7299ac3d3e8SAl Viro 	d_lookup_done(dentry);
7309ac3d3e8SAl Viro 	if (alias) {
731d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
732d39ab9deSBryan Schumaker 			goto out;
7339ac3d3e8SAl Viro 		dput(dentry);
7349ac3d3e8SAl Viro 		dentry = alias;
7359ac3d3e8SAl Viro 	}
736a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
737d39ab9deSBryan Schumaker out:
738d39ab9deSBryan Schumaker 	dput(dentry);
739d39ab9deSBryan Schumaker }
740d39ab9deSBryan Schumaker 
741d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
7423b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
7433b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
7443b2a09f1STrond Myklebust 				   struct page **xdr_pages,
74535df59d3STrond Myklebust 				   unsigned int buflen,
74635df59d3STrond Myklebust 				   struct page **arrays,
74735df59d3STrond Myklebust 				   size_t narrays)
748d1bacf9eSBryan Schumaker {
7493b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
750babddc72SBryan Schumaker 	struct xdr_stream stream;
751f7da7a12SBenny Halevy 	struct xdr_buf buf;
75235df59d3STrond Myklebust 	struct page *scratch, *new, *page = *arrays;
7535c346854STrond Myklebust 	int status;
754babddc72SBryan Schumaker 
7556650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
7566650239aSTrond Myklebust 	if (scratch == NULL)
7576650239aSTrond Myklebust 		return -ENOMEM;
758babddc72SBryan Schumaker 
759f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
7600ae4c3e8SChuck Lever 	xdr_set_scratch_page(&stream, scratch);
76199424380SBryan Schumaker 
76299424380SBryan Schumaker 	do {
763b1db9a40SAnna Schumaker 		if (entry->fattr->label)
764b1db9a40SAnna Schumaker 			entry->fattr->label->len = NFS4_MAXLABELLEN;
765d33030e2SJeffrey Mitchell 
76699424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
767972bcdf2STrond Myklebust 		if (status != 0)
76899424380SBryan Schumaker 			break;
7695c346854STrond Myklebust 
770a7a3b1e9SBenjamin Coddington 		if (desc->plus)
771a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
772a1147b82STrond Myklebust 					desc->dir_verifier);
7738cd51a0cSTrond Myklebust 
774db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
7753b2a09f1STrond Myklebust 		if (status != -ENOSPC)
7763b2a09f1STrond Myklebust 			continue;
77799424380SBryan Schumaker 
77835df59d3STrond Myklebust 		if (page->mapping != mapping) {
77935df59d3STrond Myklebust 			if (!--narrays)
7803b2a09f1STrond Myklebust 				break;
78135df59d3STrond Myklebust 			new = nfs_readdir_page_array_alloc(entry->prev_cookie,
78235df59d3STrond Myklebust 							   GFP_KERNEL);
78335df59d3STrond Myklebust 			if (!new)
78435df59d3STrond Myklebust 				break;
78535df59d3STrond Myklebust 			arrays++;
78635df59d3STrond Myklebust 			*arrays = page = new;
78735df59d3STrond Myklebust 		} else {
78835df59d3STrond Myklebust 			new = nfs_readdir_page_get_next(mapping,
78935df59d3STrond Myklebust 							page->index + 1,
7903b2a09f1STrond Myklebust 							entry->prev_cookie);
7913b2a09f1STrond Myklebust 			if (!new)
7923b2a09f1STrond Myklebust 				break;
79335df59d3STrond Myklebust 			if (page != *arrays)
7943b2a09f1STrond Myklebust 				nfs_readdir_page_unlock_and_put(page);
7953b2a09f1STrond Myklebust 			page = new;
79656e4ebf8SBryan Schumaker 		}
7973b2a09f1STrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
798972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
79956e4ebf8SBryan Schumaker 
800972bcdf2STrond Myklebust 	switch (status) {
801972bcdf2STrond Myklebust 	case -EBADCOOKIE:
802972bcdf2STrond Myklebust 		if (entry->eof) {
803b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
8048cd51a0cSTrond Myklebust 			status = 0;
80556e4ebf8SBryan Schumaker 		}
806972bcdf2STrond Myklebust 		break;
807972bcdf2STrond Myklebust 	case -ENOSPC:
808972bcdf2STrond Myklebust 	case -EAGAIN:
809972bcdf2STrond Myklebust 		status = 0;
810972bcdf2STrond Myklebust 		break;
811972bcdf2STrond Myklebust 	}
8126650239aSTrond Myklebust 
81335df59d3STrond Myklebust 	if (page != *arrays)
8143b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
8156650239aSTrond Myklebust 
8166650239aSTrond Myklebust 	put_page(scratch);
817d1bacf9eSBryan Schumaker 	return status;
81856e4ebf8SBryan Schumaker }
81956e4ebf8SBryan Schumaker 
8201a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
82156e4ebf8SBryan Schumaker {
8221a34c8c9STrond Myklebust 	while (npages--)
8231a34c8c9STrond Myklebust 		put_page(pages[npages]);
8241a34c8c9STrond Myklebust 	kfree(pages);
82556e4ebf8SBryan Schumaker }
82656e4ebf8SBryan Schumaker 
82756e4ebf8SBryan Schumaker /*
828bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
829bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
83056e4ebf8SBryan Schumaker  */
8311a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
83256e4ebf8SBryan Schumaker {
8331a34c8c9STrond Myklebust 	struct page **pages;
8341a34c8c9STrond Myklebust 	size_t i;
83556e4ebf8SBryan Schumaker 
8361a34c8c9STrond Myklebust 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
8371a34c8c9STrond Myklebust 	if (!pages)
8381a34c8c9STrond Myklebust 		return NULL;
83956e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
84056e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
84156e4ebf8SBryan Schumaker 		if (page == NULL)
84256e4ebf8SBryan Schumaker 			goto out_freepages;
84356e4ebf8SBryan Schumaker 		pages[i] = page;
84456e4ebf8SBryan Schumaker 	}
8451a34c8c9STrond Myklebust 	return pages;
84656e4ebf8SBryan Schumaker 
84756e4ebf8SBryan Schumaker out_freepages:
848c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
8491a34c8c9STrond Myklebust 	return NULL;
850d1bacf9eSBryan Schumaker }
851d1bacf9eSBryan Schumaker 
8526c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
85335df59d3STrond Myklebust 				    __be32 *verf_arg, __be32 *verf_res,
85435df59d3STrond Myklebust 				    struct page **arrays, size_t narrays)
855d1bacf9eSBryan Schumaker {
8561a34c8c9STrond Myklebust 	struct page **pages;
85735df59d3STrond Myklebust 	struct page *page = *arrays;
8586b75cf9eSTrond Myklebust 	struct nfs_entry *entry;
8591a34c8c9STrond Myklebust 	size_t array_size;
860b593c09fSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
8611a34c8c9STrond Myklebust 	size_t dtsize = NFS_SERVER(inode)->dtsize;
8628cd51a0cSTrond Myklebust 	int status = -ENOMEM;
863d1bacf9eSBryan Schumaker 
8646b75cf9eSTrond Myklebust 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
8656b75cf9eSTrond Myklebust 	if (!entry)
8666b75cf9eSTrond Myklebust 		return -ENOMEM;
8676b75cf9eSTrond Myklebust 	entry->cookie = nfs_readdir_page_last_cookie(page);
8686b75cf9eSTrond Myklebust 	entry->fh = nfs_alloc_fhandle();
869b1db9a40SAnna Schumaker 	entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
8706b75cf9eSTrond Myklebust 	entry->server = NFS_SERVER(inode);
8716b75cf9eSTrond Myklebust 	if (entry->fh == NULL || entry->fattr == NULL)
872d1bacf9eSBryan Schumaker 		goto out;
873d1bacf9eSBryan Schumaker 
8741a34c8c9STrond Myklebust 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
8751a34c8c9STrond Myklebust 	pages = nfs_readdir_alloc_pages(array_size);
8761a34c8c9STrond Myklebust 	if (!pages)
877b1db9a40SAnna Schumaker 		goto out;
878d1bacf9eSBryan Schumaker 
879d1bacf9eSBryan Schumaker 	do {
880ac396128STrond Myklebust 		unsigned int pglen;
881b593c09fSTrond Myklebust 		status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie,
882b593c09fSTrond Myklebust 						pages, dtsize,
883b593c09fSTrond Myklebust 						verf_res);
884d1bacf9eSBryan Schumaker 		if (status < 0)
885d1bacf9eSBryan Schumaker 			break;
886972bcdf2STrond Myklebust 
887ac396128STrond Myklebust 		pglen = status;
888972bcdf2STrond Myklebust 		if (pglen == 0) {
889972bcdf2STrond Myklebust 			nfs_readdir_page_set_eof(page);
8908cd51a0cSTrond Myklebust 			break;
8918cd51a0cSTrond Myklebust 		}
892972bcdf2STrond Myklebust 
893ee3707aeSNagendra S Tomar 		verf_arg = verf_res;
894ee3707aeSNagendra S Tomar 
89535df59d3STrond Myklebust 		status = nfs_readdir_page_filler(desc, entry, pages, pglen,
89635df59d3STrond Myklebust 						 arrays, narrays);
897d9c4e39cStrondmy@kernel.org 	} while (!status && nfs_readdir_page_needs_filling(page) &&
898d9c4e39cStrondmy@kernel.org 		page_mapping(page));
899d1bacf9eSBryan Schumaker 
900c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
901d1bacf9eSBryan Schumaker out:
9026b75cf9eSTrond Myklebust 	nfs_free_fattr(entry->fattr);
9036b75cf9eSTrond Myklebust 	nfs_free_fhandle(entry->fh);
9046b75cf9eSTrond Myklebust 	kfree(entry);
905d1bacf9eSBryan Schumaker 	return status;
906d1bacf9eSBryan Schumaker }
907d1bacf9eSBryan Schumaker 
9081f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
9091da177e4SLinus Torvalds {
91009cbfeafSKirill A. Shutemov 	put_page(desc->page);
9111da177e4SLinus Torvalds 	desc->page = NULL;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141f1d4aa4STrond Myklebust static void
9151f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
9161da177e4SLinus Torvalds {
9171f1d4aa4STrond Myklebust 	unlock_page(desc->page);
9181f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
9191f1d4aa4STrond Myklebust }
9201f1d4aa4STrond Myklebust 
9211f1d4aa4STrond Myklebust static struct page *
9221f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
9231f1d4aa4STrond Myklebust {
9241f1d4aa4STrond Myklebust 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
9251f1d4aa4STrond Myklebust 					   desc->page_index,
9261f1d4aa4STrond Myklebust 					   desc->last_cookie);
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds /*
930d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
931114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
9321da177e4SLinus Torvalds  */
9336c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
934d1bacf9eSBryan Schumaker {
935227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
936227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
937b593c09fSTrond Myklebust 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
938d1bacf9eSBryan Schumaker 	int res;
939d1bacf9eSBryan Schumaker 
9401f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
9411f1d4aa4STrond Myklebust 	if (!desc->page)
9421f1d4aa4STrond Myklebust 		return -ENOMEM;
9431f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
94435df59d3STrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
94535df59d3STrond Myklebust 					       &desc->page, 1);
9469fff59edSTrond Myklebust 		if (res < 0) {
9479fff59edSTrond Myklebust 			nfs_readdir_page_unlock_and_put_cached(desc);
9489fff59edSTrond Myklebust 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
9499fff59edSTrond Myklebust 				invalidate_inode_pages2(desc->file->f_mapping);
9509fff59edSTrond Myklebust 				desc->page_index = 0;
9519fff59edSTrond Myklebust 				return -EAGAIN;
9529fff59edSTrond Myklebust 			}
9539fff59edSTrond Myklebust 			return res;
9549fff59edSTrond Myklebust 		}
955f892c41cSTrond Myklebust 		/*
956f892c41cSTrond Myklebust 		 * Set the cookie verifier if the page cache was empty
957f892c41cSTrond Myklebust 		 */
958f892c41cSTrond Myklebust 		if (desc->page_index == 0)
959f892c41cSTrond Myklebust 			memcpy(nfsi->cookieverf, verf,
960f892c41cSTrond Myklebust 			       sizeof(nfsi->cookieverf));
9611f1d4aa4STrond Myklebust 	}
962114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
963ff81dfb5STrond Myklebust 	if (res == 0)
964114de382STrond Myklebust 		return 0;
9651f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
966d1bacf9eSBryan Schumaker 	return res;
967d1bacf9eSBryan Schumaker }
968d1bacf9eSBryan Schumaker 
969794092c5STrond Myklebust static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc)
970794092c5STrond Myklebust {
971794092c5STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
972794092c5STrond Myklebust 	struct inode *dir = file_inode(desc->file);
973794092c5STrond Myklebust 	unsigned int dtsize = NFS_SERVER(dir)->dtsize;
974794092c5STrond Myklebust 	loff_t size = i_size_read(dir);
975794092c5STrond Myklebust 
976794092c5STrond Myklebust 	/*
977794092c5STrond Myklebust 	 * Default to uncached readdir if the page cache is empty, and
978794092c5STrond Myklebust 	 * we're looking for a non-zero cookie in a large directory.
979794092c5STrond Myklebust 	 */
980794092c5STrond Myklebust 	return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize;
981794092c5STrond Myklebust }
982794092c5STrond Myklebust 
983d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
9846c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
9851da177e4SLinus Torvalds {
9868cd51a0cSTrond Myklebust 	int res;
987d1bacf9eSBryan Schumaker 
988794092c5STrond Myklebust 	if (nfs_readdir_dont_search_cache(desc))
989794092c5STrond Myklebust 		return -EBADCOOKIE;
990794092c5STrond Myklebust 
9919fff59edSTrond Myklebust 	do {
9920aded708STrond Myklebust 		if (desc->page_index == 0) {
9938cd51a0cSTrond Myklebust 			desc->current_index = 0;
99459e356a9STrond Myklebust 			desc->prev_index = 0;
9950aded708STrond Myklebust 			desc->last_cookie = 0;
9960aded708STrond Myklebust 		}
997114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
99847c716cbSTrond Myklebust 	} while (res == -EAGAIN);
9991da177e4SLinus Torvalds 	return res;
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds /*
10031da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
10041da177e4SLinus Torvalds  */
100513884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
100613884ff2STrond Myklebust 			   const __be32 *verf)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	struct file	*file = desc->file;
1009dbeaf8c9STrond Myklebust 	struct nfs_cache_array *array;
1010*c8f0523bSTrond Myklebust 	unsigned int i;
10118ef2ce3eSBryan Schumaker 
10120795bf83SFabian Frederick 	array = kmap(desc->page);
1013d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
1014ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
10151da177e4SLinus Torvalds 
1016ece0b423STrond Myklebust 		ent = &array->array[i];
1017a52a8a6aSTrond Myklebust 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
101823db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1019e1d2699bSTrond Myklebust 			desc->eob = true;
10201da177e4SLinus Torvalds 			break;
1021ece0b423STrond Myklebust 		}
102213884ff2STrond Myklebust 		memcpy(desc->verf, verf, sizeof(desc->verf));
1023*c8f0523bSTrond Myklebust 		if (i == array->size - 1) {
10242e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
1025*c8f0523bSTrond Myklebust 			nfs_readdir_seek_next_array(array, desc);
1026*c8f0523bSTrond Myklebust 		} else {
1027*c8f0523bSTrond Myklebust 			desc->dir_cookie = array->array[i + 1].cookie;
1028*c8f0523bSTrond Myklebust 			desc->last_cookie = array->array[0].cookie;
1029*c8f0523bSTrond Myklebust 		}
103059e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
10312e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
103259e356a9STrond Myklebust 		else
103359e356a9STrond Myklebust 			desc->ctx->pos++;
10342e7a4641STrond Myklebust 		if (desc->duped != 0)
10352e7a4641STrond Myklebust 			desc->duped = 1;
10368cd51a0cSTrond Myklebust 	}
1037b1e21c97STrond Myklebust 	if (array->page_is_eof)
1038e1d2699bSTrond Myklebust 		desc->eof = !desc->eob;
1039d1bacf9eSBryan Schumaker 
10400795bf83SFabian Frederick 	kunmap(desc->page);
1041dbeaf8c9STrond Myklebust 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1042dbeaf8c9STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds /*
10461da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
10471da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
10481da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
10491da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
10501da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
10511da177e4SLinus Torvalds  *
10521da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
10531da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
10541da177e4SLinus Torvalds  *	 we should already have a complete representation of the
10551da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
10561da177e4SLinus Torvalds  */
10576c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
10581da177e4SLinus Torvalds {
105935df59d3STrond Myklebust 	struct page	**arrays;
106035df59d3STrond Myklebust 	size_t		i, sz = 512;
1061b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
106235df59d3STrond Myklebust 	int		status = -ENOMEM;
10631da177e4SLinus Torvalds 
106435df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
10652e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10661da177e4SLinus Torvalds 
106735df59d3STrond Myklebust 	arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
106835df59d3STrond Myklebust 	if (!arrays)
10691da177e4SLinus Torvalds 		goto out;
107035df59d3STrond Myklebust 	arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
107135df59d3STrond Myklebust 	if (!arrays[0])
107235df59d3STrond Myklebust 		goto out;
10731da177e4SLinus Torvalds 
10747a8e1dc3STrond Myklebust 	desc->page_index = 0;
1075ce292d8fStrondmy@kernel.org 	desc->cache_entry_index = 0;
10762e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
10772e7a4641STrond Myklebust 	desc->duped = 0;
10787a8e1dc3STrond Myklebust 
107935df59d3STrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1080d1bacf9eSBryan Schumaker 
1081e1d2699bSTrond Myklebust 	for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
108235df59d3STrond Myklebust 		desc->page = arrays[i];
108313884ff2STrond Myklebust 		nfs_do_filldir(desc, verf);
108435df59d3STrond Myklebust 	}
108535df59d3STrond Myklebust 	desc->page = NULL;
10861da177e4SLinus Torvalds 
108735df59d3STrond Myklebust 
108835df59d3STrond Myklebust 	for (i = 0; i < sz && arrays[i]; i++)
108935df59d3STrond Myklebust 		nfs_readdir_page_array_free(arrays[i]);
10901da177e4SLinus Torvalds out:
109135df59d3STrond Myklebust 	kfree(arrays);
109235df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
10931da177e4SLinus Torvalds 	return status;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
109600a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
109700a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
109800a92642SOlivier Galibert    whole directory.
10991da177e4SLinus Torvalds  */
110023db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
11011da177e4SLinus Torvalds {
1102be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
11032b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
110413884ff2STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
110523db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
11066b75cf9eSTrond Myklebust 	struct nfs_readdir_descriptor *desc;
1107ff81dfb5STrond Myklebust 	pgoff_t page_index;
11086b75cf9eSTrond Myklebust 	int res;
11091da177e4SLinus Torvalds 
11106de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
11116de1472fSAl Viro 			file, (long long)ctx->pos);
111291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
111391d5b470SChuck Lever 
11141da177e4SLinus Torvalds 	/*
111523db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
1116f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
111700a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
111800a92642SOlivier Galibert 	 * revalidate the cookie.
11191da177e4SLinus Torvalds 	 */
1120d09e673fSTrond Myklebust 	nfs_revalidate_mapping(inode, file->f_mapping);
11216b75cf9eSTrond Myklebust 
11226b75cf9eSTrond Myklebust 	res = -ENOMEM;
11236b75cf9eSTrond Myklebust 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
11246b75cf9eSTrond Myklebust 	if (!desc)
11256b75cf9eSTrond Myklebust 		goto out;
11266b75cf9eSTrond Myklebust 	desc->file = file;
11276b75cf9eSTrond Myklebust 	desc->ctx = ctx;
11286b75cf9eSTrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, ctx);
1129fccca7fcSTrond Myklebust 
11302e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
11312e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
11322e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
11332e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
1134ff81dfb5STrond Myklebust 	page_index = dir_ctx->page_index;
1135728dd0abSTrond Myklebust 	desc->page_index = page_index;
1136728dd0abSTrond Myklebust 	desc->last_cookie = dir_ctx->last_cookie;
11372e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
1138e1d2699bSTrond Myklebust 	desc->eof = dir_ctx->eof;
1139b593c09fSTrond Myklebust 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
11402e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1141565277f6STrond Myklebust 
1142e1d2699bSTrond Myklebust 	if (desc->eof) {
1143e1d2699bSTrond Myklebust 		res = 0;
1144e1d2699bSTrond Myklebust 		goto out_free;
1145e1d2699bSTrond Myklebust 	}
1146e1d2699bSTrond Myklebust 
1147ff81dfb5STrond Myklebust 	if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) &&
1148ff81dfb5STrond Myklebust 	    list_is_singular(&nfsi->open_files))
1149ff81dfb5STrond Myklebust 		invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1);
1150ff81dfb5STrond Myklebust 
115147c716cbSTrond Myklebust 	do {
11521da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
115300a92642SOlivier Galibert 
11541da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1155ece0b423STrond Myklebust 			res = 0;
11561da177e4SLinus Torvalds 			/* This means either end of directory */
11572e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
11581da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
115923db8620SAl Viro 				res = uncached_readdir(desc);
1160ece0b423STrond Myklebust 				if (res == 0)
11611da177e4SLinus Torvalds 					continue;
11629fff59edSTrond Myklebust 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
11639fff59edSTrond Myklebust 					res = 0;
11641da177e4SLinus Torvalds 			}
11651da177e4SLinus Torvalds 			break;
11661da177e4SLinus Torvalds 		}
11671da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
116813884ff2STrond Myklebust 			clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
11691da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1170baf57a09STrond Myklebust 			desc->page_index = 0;
1171a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1172a7a3b1e9SBenjamin Coddington 			desc->eof = false;
11731da177e4SLinus Torvalds 			continue;
11741da177e4SLinus Torvalds 		}
11751da177e4SLinus Torvalds 		if (res < 0)
11761da177e4SLinus Torvalds 			break;
11771da177e4SLinus Torvalds 
117813884ff2STrond Myklebust 		nfs_do_filldir(desc, nfsi->cookieverf);
11791f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
1180e1d2699bSTrond Myklebust 	} while (!desc->eob && !desc->eof);
11812e7a4641STrond Myklebust 
11822e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
11832e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
11842e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
1185728dd0abSTrond Myklebust 	dir_ctx->last_cookie = desc->last_cookie;
11862e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
11872e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
1188ff81dfb5STrond Myklebust 	dir_ctx->page_index = desc->page_index;
1189e1d2699bSTrond Myklebust 	dir_ctx->eof = desc->eof;
1190b593c09fSTrond Myklebust 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
11912e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1192e1d2699bSTrond Myklebust out_free:
11936b75cf9eSTrond Myklebust 	kfree(desc);
11946b75cf9eSTrond Myklebust 
1195fccca7fcSTrond Myklebust out:
11966de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
11971da177e4SLinus Torvalds 	return res;
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
1200965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1201f0dd2136STrond Myklebust {
1202480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1203b84e06c5SChuck Lever 
12046de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
12056de1472fSAl Viro 			filp, offset, whence);
1206b84e06c5SChuck Lever 
1207965c8e59SAndrew Morton 	switch (whence) {
1208f0dd2136STrond Myklebust 	default:
1209b2b1ff3dSTrond Myklebust 		return -EINVAL;
1210b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1211b2b1ff3dSTrond Myklebust 		if (offset < 0)
1212b2b1ff3dSTrond Myklebust 			return -EINVAL;
121383f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1214b2b1ff3dSTrond Myklebust 		break;
1215b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1216b2b1ff3dSTrond Myklebust 		if (offset == 0)
1217b2b1ff3dSTrond Myklebust 			return filp->f_pos;
121883f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1219b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1220b2b1ff3dSTrond Myklebust 		if (offset < 0) {
122183f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1222b2b1ff3dSTrond Myklebust 			return -EINVAL;
1223b2b1ff3dSTrond Myklebust 		}
1224f0dd2136STrond Myklebust 	}
1225f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1226f0dd2136STrond Myklebust 		filp->f_pos = offset;
1227728dd0abSTrond Myklebust 		if (!nfs_readdir_use_cookie(filp)) {
1228480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
1229728dd0abSTrond Myklebust 			dir_ctx->page_index = 0;
1230728dd0abSTrond Myklebust 		} else
1231728dd0abSTrond Myklebust 			dir_ctx->dir_cookie = offset;
1232b593c09fSTrond Myklebust 		if (offset == 0)
1233b593c09fSTrond Myklebust 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
12348ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1235e1d2699bSTrond Myklebust 		dir_ctx->eof = false;
1236f0dd2136STrond Myklebust 	}
123783f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1238f0dd2136STrond Myklebust 	return offset;
1239f0dd2136STrond Myklebust }
1240f0dd2136STrond Myklebust 
12411da177e4SLinus Torvalds /*
12421da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
12431da177e4SLinus Torvalds  * is a dummy operation.
12441da177e4SLinus Torvalds  */
124502c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
124602c24a82SJosef Bacik 			 int datasync)
12471da177e4SLinus Torvalds {
12486de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
12491e7cb3dcSChuck Lever 
125011decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
12511da177e4SLinus Torvalds 	return 0;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
1254bfc69a45STrond Myklebust /**
1255bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1256302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1257bfc69a45STrond Myklebust  *
1258bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1259bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1260bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1261bfc69a45STrond Myklebust  *
1262efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1263efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1264efeda80dSTrond Myklebust  *
1265bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1266bfc69a45STrond Myklebust  */
1267bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1268bfc69a45STrond Myklebust {
1269efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1270bfc69a45STrond Myklebust }
127189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1272bfc69a45STrond Myklebust 
1273efeda80dSTrond Myklebust /**
1274efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1275efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1276efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1277efeda80dSTrond Myklebust  *
1278efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1279efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1280efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1281efeda80dSTrond Myklebust  */
1282efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1283efeda80dSTrond Myklebust {
1284efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1285efeda80dSTrond Myklebust }
1286efeda80dSTrond Myklebust 
1287efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1288efeda80dSTrond Myklebust {
1289efeda80dSTrond Myklebust 	*verf |= 1UL;
1290efeda80dSTrond Myklebust }
1291efeda80dSTrond Myklebust 
1292efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1293efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1294efeda80dSTrond Myklebust {
1295efeda80dSTrond Myklebust 	*verf &= ~1UL;
1296efeda80dSTrond Myklebust }
1297efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1298efeda80dSTrond Myklebust 
1299efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1300efeda80dSTrond Myklebust {
1301efeda80dSTrond Myklebust 	return verf & 1;
1302efeda80dSTrond Myklebust }
1303efeda80dSTrond Myklebust 
1304efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1305efeda80dSTrond Myklebust {
1306efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1307efeda80dSTrond Myklebust }
1308efeda80dSTrond Myklebust 
1309efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1310efeda80dSTrond Myklebust {
1311efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1312cec08f45STrond Myklebust 	struct inode *dir = d_inode(dentry->d_parent);
1313efeda80dSTrond Myklebust 
1314cec08f45STrond Myklebust 	if (!nfs_verify_change_attribute(dir, verf))
1315cec08f45STrond Myklebust 		return;
1316efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1317efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1318efeda80dSTrond Myklebust 	dentry->d_time = verf;
1319efeda80dSTrond Myklebust }
1320efeda80dSTrond Myklebust 
1321efeda80dSTrond Myklebust /**
1322efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1323efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1324efeda80dSTrond Myklebust  * @verf: verifier to save
1325efeda80dSTrond Myklebust  *
1326efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1327efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1328efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1329efeda80dSTrond Myklebust  * look it up again after a directory change.
1330efeda80dSTrond Myklebust  */
1331efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1332efeda80dSTrond Myklebust {
1333efeda80dSTrond Myklebust 
1334efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1335efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1336efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1337efeda80dSTrond Myklebust }
1338efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1339efeda80dSTrond Myklebust 
1340efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1341efeda80dSTrond Myklebust /**
1342efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1343efeda80dSTrond Myklebust  * @inode: pointer to inode
1344efeda80dSTrond Myklebust  *
1345efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1346efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1347efeda80dSTrond Myklebust  * while holding a delegation.
1348efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1349efeda80dSTrond Myklebust  * returned or revoked.
1350efeda80dSTrond Myklebust  */
1351efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1352efeda80dSTrond Myklebust {
1353efeda80dSTrond Myklebust 	struct dentry *alias;
1354efeda80dSTrond Myklebust 
1355efeda80dSTrond Myklebust 	if (!inode)
1356efeda80dSTrond Myklebust 		return;
1357efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1358efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1359efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1360efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1361efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1362efeda80dSTrond Myklebust 	}
1363efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1364efeda80dSTrond Myklebust }
1365efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1366efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1367efeda80dSTrond Myklebust 
13688ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
13698ce37abdSTrond Myklebust {
13708ce37abdSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
13718ce37abdSTrond Myklebust 	    d_really_is_negative(dentry))
13728ce37abdSTrond Myklebust 		return dentry->d_time == inode_peek_iversion_raw(dir);
13738ce37abdSTrond Myklebust 	return nfs_verify_change_attribute(dir, dentry->d_time);
13748ce37abdSTrond Myklebust }
13758ce37abdSTrond Myklebust 
13761da177e4SLinus Torvalds /*
13771da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
13781da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
13791da177e4SLinus Torvalds  * and may need to be looked up again.
1380912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
13811da177e4SLinus Torvalds  */
1382912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1383912a108dSNeilBrown 			      int rcu_walk)
13841da177e4SLinus Torvalds {
13851da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
13861da177e4SLinus Torvalds 		return 1;
13874eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
13884eec952eSTrond Myklebust 		return 0;
13898ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
13906ecc5e8fSTrond Myklebust 		return 0;
1391f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
13921cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1393912a108dSNeilBrown 		if (rcu_walk)
1394f2c77f4eSTrond Myklebust 			return 0;
13951cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
13961cd9cb05STrond Myklebust 			return 0;
13971cd9cb05STrond Myklebust 	}
13988ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
1399f2c77f4eSTrond Myklebust 		return 0;
1400f2c77f4eSTrond Myklebust 	return 1;
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds /*
1404a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1405a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1406a12802caSTrond Myklebust  */
1407fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1408a12802caSTrond Myklebust {
1409a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1410a12802caSTrond Myklebust 		return 0;
1411fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1412a12802caSTrond Myklebust }
1413a12802caSTrond Myklebust 
1414a12802caSTrond Myklebust /*
14151d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
14161d6757fbSTrond Myklebust  *
14171d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
14181d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
14191d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
14201d6757fbSTrond Myklebust  *
14211d6757fbSTrond Myklebust  */
142265a0c149STrond Myklebust static
1423fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
142665a0c149STrond Myklebust 	int ret;
14271da177e4SLinus Torvalds 
142836d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
14294e99a1ffSTrond Myklebust 		return 0;
143047921921STrond Myklebust 
143147921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
143247921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
143347921921STrond Myklebust 		case S_IFREG:
143447921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
143547921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
143647921921STrond Myklebust 				goto out;
1437df561f66SGustavo A. R. Silva 			fallthrough;
143847921921STrond Myklebust 		case S_IFDIR:
143947921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
144047921921STrond Myklebust 				break;
144147921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
144247921921STrond Myklebust 			goto out_force;
144347921921STrond Myklebust 		}
144447921921STrond Myklebust 	}
144547921921STrond Myklebust 
14461da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1447fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
14481da177e4SLinus Torvalds 		goto out_force;
144965a0c149STrond Myklebust out:
145043245ecaSOlga Kornievskaia 	if (inode->i_nlink > 0 ||
145143245ecaSOlga Kornievskaia 	    (inode->i_nlink == 0 &&
145243245ecaSOlga Kornievskaia 	     test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
145343245ecaSOlga Kornievskaia 		return 0;
145443245ecaSOlga Kornievskaia 	else
145543245ecaSOlga Kornievskaia 		return -ESTALE;
14561da177e4SLinus Torvalds out_force:
14571fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
14581fa1e384SNeilBrown 		return -ECHILD;
145965a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
146065a0c149STrond Myklebust 	if (ret != 0)
146165a0c149STrond Myklebust 		return ret;
146265a0c149STrond Myklebust 	goto out;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
146582e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode)
146682e7ca13STrond Myklebust {
146782e7ca13STrond Myklebust 	spin_lock(&inode->i_lock);
1468a6a361c4STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
146982e7ca13STrond Myklebust 	spin_unlock(&inode->i_lock);
147082e7ca13STrond Myklebust }
147182e7ca13STrond Myklebust 
14721da177e4SLinus Torvalds /*
14731da177e4SLinus Torvalds  * We judge how long we want to trust negative
14741da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
14751da177e4SLinus Torvalds  *
14761da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
14771da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1478912a108dSNeilBrown  *
1479912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1480912a108dSNeilBrown  * suggesting a reval is needed.
14819f6d44d4STrond Myklebust  *
14829f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
14839f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
14841da177e4SLinus Torvalds  */
14851da177e4SLinus Torvalds static inline
14861da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1487fa3c56bbSAl Viro 		       unsigned int flags)
14881da177e4SLinus Torvalds {
14899f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
14901da177e4SLinus Torvalds 		return 0;
14914eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
14924eec952eSTrond Myklebust 		return 1;
149398ca3ee6STrond Myklebust 	/* Case insensitive server? Revalidate negative dentries */
149498ca3ee6STrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
149598ca3ee6STrond Myklebust 		return 1;
1496912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
14995ceb9d7fSTrond Myklebust static int
15005ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
15015ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
15021da177e4SLinus Torvalds {
15035ceb9d7fSTrond Myklebust 	switch (error) {
15045ceb9d7fSTrond Myklebust 	case 1:
15052eef8a31STrond Myklebust 		break;
15065ceb9d7fSTrond Myklebust 	case 0:
1507a3f432bfSJ. Bruce Fields 		/*
1508a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1509a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1510a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1511a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1512a3f432bfSJ. Bruce Fields 		 */
151347397915STrond Myklebust 		if (inode && IS_ROOT(dentry))
15142eef8a31STrond Myklebust 			error = 1;
15152eef8a31STrond Myklebust 		break;
15165ceb9d7fSTrond Myklebust 	}
15172eef8a31STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1518e1fb4d05STrond Myklebust 	return error;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
15215ceb9d7fSTrond Myklebust static int
15225ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
15235ceb9d7fSTrond Myklebust 			       unsigned int flags)
15245ceb9d7fSTrond Myklebust {
15255ceb9d7fSTrond Myklebust 	int ret = 1;
15265ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
15275ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
15285ceb9d7fSTrond Myklebust 			return -ECHILD;
15295ceb9d7fSTrond Myklebust 		ret = 0;
15305ceb9d7fSTrond Myklebust 	}
15315ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
15325ceb9d7fSTrond Myklebust }
15335ceb9d7fSTrond Myklebust 
15345ceb9d7fSTrond Myklebust static int
15355ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
15365ceb9d7fSTrond Myklebust 				struct inode *inode)
15375ceb9d7fSTrond Myklebust {
15385ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15395ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
15405ceb9d7fSTrond Myklebust }
15415ceb9d7fSTrond Myklebust 
15425ceb9d7fSTrond Myklebust static int
15435ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
15445ceb9d7fSTrond Myklebust 			     struct inode *inode)
15455ceb9d7fSTrond Myklebust {
15465ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
15475ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
1548a1147b82STrond Myklebust 	unsigned long dir_verifier;
15495ceb9d7fSTrond Myklebust 	int ret;
15505ceb9d7fSTrond Myklebust 
15515ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
15525ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
15539558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
15549558a007SAnna Schumaker 	if (fhandle == NULL || fattr == NULL)
15555ceb9d7fSTrond Myklebust 		goto out;
15565ceb9d7fSTrond Myklebust 
1557a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
15589558a007SAnna Schumaker 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
15595ceb9d7fSTrond Myklebust 	if (ret < 0) {
1560f7b37b8bSTrond Myklebust 		switch (ret) {
1561f7b37b8bSTrond Myklebust 		case -ESTALE:
1562f7b37b8bSTrond Myklebust 		case -ENOENT:
15635ceb9d7fSTrond Myklebust 			ret = 0;
1564f7b37b8bSTrond Myklebust 			break;
1565f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1566f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1567f7b37b8bSTrond Myklebust 				ret = 1;
1568f7b37b8bSTrond Myklebust 		}
15695ceb9d7fSTrond Myklebust 		goto out;
15705ceb9d7fSTrond Myklebust 	}
15715ceb9d7fSTrond Myklebust 	ret = 0;
15725ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
15735ceb9d7fSTrond Myklebust 		goto out;
15745ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
15755ceb9d7fSTrond Myklebust 		goto out;
15765ceb9d7fSTrond Myklebust 
1577dd225cb3SAnna Schumaker 	nfs_setsecurity(inode, fattr);
1578a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
15795ceb9d7fSTrond Myklebust 
15805ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
15815ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
15825ceb9d7fSTrond Myklebust 	ret = 1;
15835ceb9d7fSTrond Myklebust out:
15845ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
15855ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
158682e7ca13STrond Myklebust 
158782e7ca13STrond Myklebust 	/*
158882e7ca13STrond Myklebust 	 * If the lookup failed despite the dentry change attribute being
158982e7ca13STrond Myklebust 	 * a match, then we should revalidate the directory cache.
159082e7ca13STrond Myklebust 	 */
15918ce37abdSTrond Myklebust 	if (!ret && nfs_dentry_verify_change(dir, dentry))
159282e7ca13STrond Myklebust 		nfs_mark_dir_for_revalidate(dir);
15935ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
15945ceb9d7fSTrond Myklebust }
15955ceb9d7fSTrond Myklebust 
15965ceb9d7fSTrond Myklebust /*
15975ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
15985ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
15995ceb9d7fSTrond Myklebust  * lookup.
16005ceb9d7fSTrond Myklebust  *
16015ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
16025ceb9d7fSTrond Myklebust  * we have an inode!
16035ceb9d7fSTrond Myklebust  *
16045ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
16055ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
16065ceb9d7fSTrond Myklebust  */
16075ceb9d7fSTrond Myklebust static int
16085ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
16095ceb9d7fSTrond Myklebust 			 unsigned int flags)
16105ceb9d7fSTrond Myklebust {
16115ceb9d7fSTrond Myklebust 	struct inode *inode;
16125ceb9d7fSTrond Myklebust 	int error;
16135ceb9d7fSTrond Myklebust 
16145ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
16155ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
16165ceb9d7fSTrond Myklebust 
16175ceb9d7fSTrond Myklebust 	if (!inode)
16185ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
16195ceb9d7fSTrond Myklebust 
16205ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
16215ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
16225ceb9d7fSTrond Myklebust 				__func__, dentry);
16235ceb9d7fSTrond Myklebust 		goto out_bad;
16245ceb9d7fSTrond Myklebust 	}
16255ceb9d7fSTrond Myklebust 
1626efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
16275ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
16285ceb9d7fSTrond Myklebust 
16295ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
16305ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
16315ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
16325ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
16335ceb9d7fSTrond Myklebust 		if (error) {
16345ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
163582e7ca13STrond Myklebust 				nfs_mark_dir_for_revalidate(dir);
16365ceb9d7fSTrond Myklebust 			goto out_bad;
16375ceb9d7fSTrond Myklebust 		}
16385ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
16395ceb9d7fSTrond Myklebust 		goto out_valid;
16405ceb9d7fSTrond Myklebust 	}
16415ceb9d7fSTrond Myklebust 
16425ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
16435ceb9d7fSTrond Myklebust 		return -ECHILD;
16445ceb9d7fSTrond Myklebust 
16455ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
16465ceb9d7fSTrond Myklebust 		goto out_bad;
16475ceb9d7fSTrond Myklebust 
16485ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
16492eef8a31STrond Myklebust 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
16505ceb9d7fSTrond Myklebust out_valid:
16515ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
16525ceb9d7fSTrond Myklebust out_bad:
16535ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
16545ceb9d7fSTrond Myklebust 		return -ECHILD;
16555ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
16565ceb9d7fSTrond Myklebust }
16575ceb9d7fSTrond Myklebust 
16585ceb9d7fSTrond Myklebust static int
1659c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1660c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
16615ceb9d7fSTrond Myklebust {
16625ceb9d7fSTrond Myklebust 	struct dentry *parent;
16635ceb9d7fSTrond Myklebust 	struct inode *dir;
16645ceb9d7fSTrond Myklebust 	int ret;
16655ceb9d7fSTrond Myklebust 
16665ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
16675ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
16685ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
16695ceb9d7fSTrond Myklebust 		if (!dir)
16705ceb9d7fSTrond Myklebust 			return -ECHILD;
1671c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
16725ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
16735ceb9d7fSTrond Myklebust 			return -ECHILD;
16745ceb9d7fSTrond Myklebust 	} else {
16755ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1676c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
16775ceb9d7fSTrond Myklebust 		dput(parent);
16785ceb9d7fSTrond Myklebust 	}
16795ceb9d7fSTrond Myklebust 	return ret;
16805ceb9d7fSTrond Myklebust }
16815ceb9d7fSTrond Myklebust 
1682c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1683c7944ebbSTrond Myklebust {
1684c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1685c7944ebbSTrond Myklebust }
1686c7944ebbSTrond Myklebust 
16871da177e4SLinus Torvalds /*
16882b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1689ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1690ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1691ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1692ecf3d1f1SJeff Layton  *
1693ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1694ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1695ecf3d1f1SJeff Layton  */
1696ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1697ecf3d1f1SJeff Layton {
16982b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
16999cdd1d3fSTrond Myklebust 	int error = 0;
1700ecf3d1f1SJeff Layton 
1701ecf3d1f1SJeff Layton 	/*
1702ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1703ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1704ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1705ecf3d1f1SJeff Layton 	 */
1706ecf3d1f1SJeff Layton 	if (!inode) {
17076de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
17086de1472fSAl Viro 				__func__, dentry);
1709ecf3d1f1SJeff Layton 		return 1;
1710ecf3d1f1SJeff Layton 	}
1711ecf3d1f1SJeff Layton 
1712ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
17136de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
17146de1472fSAl Viro 				__func__, dentry);
1715ecf3d1f1SJeff Layton 		return 0;
1716ecf3d1f1SJeff Layton 	}
1717ecf3d1f1SJeff Layton 
1718b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1719ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1720ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1721ecf3d1f1SJeff Layton 	return !error;
1722ecf3d1f1SJeff Layton }
1723ecf3d1f1SJeff Layton 
1724ecf3d1f1SJeff Layton /*
17251da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
17261da177e4SLinus Torvalds  */
1727fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
17281da177e4SLinus Torvalds {
17296de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
17306de1472fSAl Viro 		dentry, dentry->d_flags);
17311da177e4SLinus Torvalds 
173277f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
17332b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
173477f11192STrond Myklebust 		return 1;
173577f11192STrond Myklebust 
17361da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17371da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
17381da177e4SLinus Torvalds 		return 1;
17391da177e4SLinus Torvalds 	}
17401751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
17411da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
17421da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
17431da177e4SLinus Torvalds 		return 1;
17441da177e4SLinus Torvalds 	}
17451da177e4SLinus Torvalds 	return 0;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds }
17481da177e4SLinus Torvalds 
17491f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
17501b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
17511b83d707STrond Myklebust {
17521b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
17531f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
175459a707b0STrond Myklebust 	if (inode->i_nlink > 0)
175559a707b0STrond Myklebust 		drop_nlink(inode);
175659a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1757ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(
1758ac46b3d7STrond Myklebust 		inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
17591301e421STrond Myklebust 			       NFS_INO_INVALID_NLINK);
17601b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
17611b83d707STrond Myklebust }
17621b83d707STrond Myklebust 
17631da177e4SLinus Torvalds /*
17641da177e4SLinus Torvalds  * Called when the dentry loses inode.
17651da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
17661da177e4SLinus Torvalds  */
17671da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1770e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
17711f018458STrond Myklebust 		nfs_drop_nlink(inode);
17721da177e4SLinus Torvalds 	}
17731da177e4SLinus Torvalds 	iput(inode);
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
1776b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1777b1942c5fSAl Viro {
1778b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1779b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1780b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1781b1942c5fSAl Viro 			WARN_ON(1);
1782b1942c5fSAl Viro 		else
1783b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1784b1942c5fSAl Viro 	}
1785b1942c5fSAl Viro }
1786b1942c5fSAl Viro 
1787f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
17881da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1789ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
17901da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
17911da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
179236d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1793b1942c5fSAl Viro 	.d_release	= nfs_d_release,
17941da177e4SLinus Torvalds };
1795ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
17961da177e4SLinus Torvalds 
1797597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
17981da177e4SLinus Torvalds {
17991da177e4SLinus Torvalds 	struct dentry *res;
18001da177e4SLinus Torvalds 	struct inode *inode = NULL;
1801e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1802e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
1803a1147b82STrond Myklebust 	unsigned long dir_verifier;
18041da177e4SLinus Torvalds 	int error;
18051da177e4SLinus Torvalds 
18066de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
180791d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
18081da177e4SLinus Torvalds 
1809130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1810130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
18111da177e4SLinus Torvalds 
1812fd684071STrond Myklebust 	/*
1813fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1814fd684071STrond Myklebust 	 * but don't hash the dentry.
1815fd684071STrond Myklebust 	 */
18169f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1817130f9ab7SAl Viro 		return NULL;
18181da177e4SLinus Torvalds 
1819e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1820e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
18219558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1822e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1823e1fb4d05STrond Myklebust 		goto out;
1824e1fb4d05STrond Myklebust 
1825a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
18266e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
18279558a007SAnna Schumaker 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
18288ce37abdSTrond Myklebust 	if (error == -ENOENT) {
18298ce37abdSTrond Myklebust 		if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
18308ce37abdSTrond Myklebust 			dir_verifier = inode_peek_iversion_raw(dir);
18311da177e4SLinus Torvalds 		goto no_entry;
18328ce37abdSTrond Myklebust 	}
18331da177e4SLinus Torvalds 	if (error < 0) {
18341da177e4SLinus Torvalds 		res = ERR_PTR(error);
18359558a007SAnna Schumaker 		goto out;
18361da177e4SLinus Torvalds 	}
1837cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1838bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
183903f28e3aSTrond Myklebust 	if (IS_ERR(res))
18409558a007SAnna Schumaker 		goto out;
184154ceac45SDavid Howells 
184263519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
184363519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1844d69ee9b8STrond Myklebust 
18451da177e4SLinus Torvalds no_entry:
184641d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
18479eaef27bSTrond Myklebust 	if (res != NULL) {
18489eaef27bSTrond Myklebust 		if (IS_ERR(res))
18499558a007SAnna Schumaker 			goto out;
18501da177e4SLinus Torvalds 		dentry = res;
18519eaef27bSTrond Myklebust 	}
1852a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
18531da177e4SLinus Torvalds out:
18549558a007SAnna Schumaker 	trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1855e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1856e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
18571da177e4SLinus Torvalds 	return res;
18581da177e4SLinus Torvalds }
1859ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
18601da177e4SLinus Torvalds 
186100bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
186200bdadc7STrond Myklebust {
186300bdadc7STrond Myklebust 	/* Case insensitive server? Revalidate dentries */
186400bdadc7STrond Myklebust 	if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
186500bdadc7STrond Myklebust 		d_prune_aliases(inode);
186600bdadc7STrond Myklebust }
186700bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
186800bdadc7STrond Myklebust 
186989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
18700b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
18711da177e4SLinus Torvalds 
1872f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
18730ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1874b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
18751da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
18761da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
187736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1878b1942c5fSAl Viro 	.d_release	= nfs_d_release,
18791da177e4SLinus Torvalds };
188089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
18811da177e4SLinus Torvalds 
18828a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
18838a5e929dSAl Viro {
18848a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
18858a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
18868a5e929dSAl Viro 		res |= FMODE_READ;
18878a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
18888a5e929dSAl Viro 		res |= FMODE_WRITE;
18898a5e929dSAl Viro 	return res;
18908a5e929dSAl Viro }
18918a5e929dSAl Viro 
1892532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1893cd9a1c0eSTrond Myklebust {
1894532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1895cd9a1c0eSTrond Myklebust }
1896cd9a1c0eSTrond Myklebust 
1897cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1898cd9a1c0eSTrond Myklebust {
1899f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1900cd9a1c0eSTrond Myklebust 	return 0;
1901cd9a1c0eSTrond Myklebust }
1902cd9a1c0eSTrond Myklebust 
1903d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
19040dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1905b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1906cd9a1c0eSTrond Myklebust {
19070dd2b474SMiklos Szeredi 	int err;
19080dd2b474SMiklos Szeredi 
1909be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
191030d90494SAl Viro 	if (err)
1911d9585277SAl Viro 		goto out;
1912eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
191330d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1914eaa2b82cSNeilBrown 	else
19159821421aSTrond Myklebust 		err = -EOPENSTALE;
1916cd9a1c0eSTrond Myklebust out:
1917d9585277SAl Viro 	return err;
1918cd9a1c0eSTrond Myklebust }
1919cd9a1c0eSTrond Myklebust 
192073a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
192130d90494SAl Viro 		    struct file *file, unsigned open_flags,
192244907d79SAl Viro 		    umode_t mode)
19231da177e4SLinus Torvalds {
1924c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1925cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
19260dd2b474SMiklos Szeredi 	struct dentry *res;
19270dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1928f46e0bd3STrond Myklebust 	struct inode *inode;
19291472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
193068eaba4cSTrond Myklebust 	unsigned long dir_verifier;
1931c94c0953SAl Viro 	bool switched = false;
193273a09dd9SAl Viro 	int created = 0;
1933898f635cSTrond Myklebust 	int err;
19341da177e4SLinus Torvalds 
19350dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
19362b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
19370dd2b474SMiklos Szeredi 
19381e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
19396de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19401e7cb3dcSChuck Lever 
19419597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
19429597c13bSJeff Layton 	if (err)
19439597c13bSJeff Layton 		return err;
19449597c13bSJeff Layton 
19450dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
19460dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
194700699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
19480dd2b474SMiklos Szeredi 			/*
19490dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
19500dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
19510dd2b474SMiklos Szeredi 			 * again
19520dd2b474SMiklos Szeredi 			 */
1953d9585277SAl Viro 			return -ENOENT;
19540dd2b474SMiklos Szeredi 		}
19551472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
19561da177e4SLinus Torvalds 		goto no_open;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 
19590dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1960d9585277SAl Viro 		return -ENAMETOOLONG;
19611da177e4SLinus Torvalds 
19620dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1963dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1964dff25ddbSAndreas Gruenbacher 
1965dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1966dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1967dff25ddbSAndreas Gruenbacher 
1968536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1969dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
19700dd2b474SMiklos Szeredi 	}
1971536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1972536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1973536e43d1STrond Myklebust 		attr.ia_size = 0;
1974cd9a1c0eSTrond Myklebust 	}
1975cd9a1c0eSTrond Myklebust 
1976c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1977c94c0953SAl Viro 		d_drop(dentry);
1978c94c0953SAl Viro 		switched = true;
1979c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1980c94c0953SAl Viro 					  &dentry->d_name, &wq);
1981c94c0953SAl Viro 		if (IS_ERR(dentry))
1982c94c0953SAl Viro 			return PTR_ERR(dentry);
1983c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1984c94c0953SAl Viro 			return finish_no_open(file, dentry);
1985c94c0953SAl Viro 	}
1986c94c0953SAl Viro 
1987532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
19880dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
19890dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1990d9585277SAl Viro 		goto out;
19910dd2b474SMiklos Szeredi 
19926e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
199373a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
199473a09dd9SAl Viro 	if (created)
199573a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1996275bb307STrond Myklebust 	if (IS_ERR(inode)) {
19970dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
19986e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
19992d9db750STrond Myklebust 		put_nfs_open_context(ctx);
2000d20cb71dSAl Viro 		d_drop(dentry);
20010dd2b474SMiklos Szeredi 		switch (err) {
20021da177e4SLinus Torvalds 		case -ENOENT:
2003774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
200468eaba4cSTrond Myklebust 			if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
200568eaba4cSTrond Myklebust 				dir_verifier = inode_peek_iversion_raw(dir);
200668eaba4cSTrond Myklebust 			else
200768eaba4cSTrond Myklebust 				dir_verifier = nfs_save_change_attribute(dir);
200868eaba4cSTrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
20090dd2b474SMiklos Szeredi 			break;
20101788ea6eSJeff Layton 		case -EISDIR:
20116f926b5bSTrond Myklebust 		case -ENOTDIR:
20126f926b5bSTrond Myklebust 			goto no_open;
20131da177e4SLinus Torvalds 		case -ELOOP:
20140dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
20151da177e4SLinus Torvalds 				goto no_open;
20160dd2b474SMiklos Szeredi 			break;
20171da177e4SLinus Torvalds 			/* case -EINVAL: */
20181da177e4SLinus Torvalds 		default:
20190dd2b474SMiklos Szeredi 			break;
20201da177e4SLinus Torvalds 		}
20211da177e4SLinus Torvalds 		goto out;
20221da177e4SLinus Torvalds 	}
20230dd2b474SMiklos Szeredi 
2024b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
20256e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
20262d9db750STrond Myklebust 	put_nfs_open_context(ctx);
2027d9585277SAl Viro out:
2028c94c0953SAl Viro 	if (unlikely(switched)) {
2029c94c0953SAl Viro 		d_lookup_done(dentry);
2030c94c0953SAl Viro 		dput(dentry);
2031c94c0953SAl Viro 	}
2032d9585277SAl Viro 	return err;
20330dd2b474SMiklos Szeredi 
20341da177e4SLinus Torvalds no_open:
20351472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
2036ac795161STrond Myklebust 	if (!res) {
2037ac795161STrond Myklebust 		inode = d_inode(dentry);
2038ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2039e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2040ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
20411751fc1dSTrond Myklebust 		else if (inode && S_ISREG(inode->i_mode))
20421751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2043ac795161STrond Myklebust 	} else if (!IS_ERR(res)) {
2044ac795161STrond Myklebust 		inode = d_inode(res);
2045ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2046e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2047ac795161STrond Myklebust 			dput(res);
2048ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
20491751fc1dSTrond Myklebust 		} else if (inode && S_ISREG(inode->i_mode)) {
20501751fc1dSTrond Myklebust 			dput(res);
20511751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2052ac795161STrond Myklebust 		}
2053ac795161STrond Myklebust 	}
2054c94c0953SAl Viro 	if (switched) {
2055c94c0953SAl Viro 		d_lookup_done(dentry);
2056c94c0953SAl Viro 		if (!res)
2057c94c0953SAl Viro 			res = dentry;
2058c94c0953SAl Viro 		else
2059c94c0953SAl Viro 			dput(dentry);
2060c94c0953SAl Viro 	}
20610dd2b474SMiklos Szeredi 	if (IS_ERR(res))
2062c94c0953SAl Viro 		return PTR_ERR(res);
2063e45198a6SAl Viro 	return finish_no_open(file, res);
20641da177e4SLinus Torvalds }
206589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
20661da177e4SLinus Torvalds 
2067c7944ebbSTrond Myklebust static int
2068c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2069c7944ebbSTrond Myklebust 			  unsigned int flags)
20701da177e4SLinus Torvalds {
2071657e94b6SNick Piggin 	struct inode *inode;
20721da177e4SLinus Torvalds 
2073fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2074c7944ebbSTrond Myklebust 		goto full_reval;
2075eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
2076c7944ebbSTrond Myklebust 		goto full_reval;
20772b484297STrond Myklebust 
20782b0143b5SDavid Howells 	inode = d_inode(dentry);
20792b484297STrond Myklebust 
20801da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
20811da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
20821da177e4SLinus Torvalds 	 */
2083c7944ebbSTrond Myklebust 	if (inode == NULL)
2084c7944ebbSTrond Myklebust 		goto full_reval;
208549317a7fSNeilBrown 
2086efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
2087c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2088216d5d06STrond Myklebust 
20891da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
20901da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
2091c7944ebbSTrond Myklebust 		goto full_reval;
2092c7944ebbSTrond Myklebust 
20931da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
2094c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2095c7944ebbSTrond Myklebust 		goto reval_dentry;
2096c7944ebbSTrond Myklebust 
2097c7944ebbSTrond Myklebust 	/* Check if the directory changed */
2098c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2099c7944ebbSTrond Myklebust 		goto reval_dentry;
21001da177e4SLinus Torvalds 
21010ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
2102c7944ebbSTrond Myklebust 	return 1;
2103c7944ebbSTrond Myklebust reval_dentry:
2104c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
2105c7944ebbSTrond Myklebust 		return -ECHILD;
210642f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
21070ef97dcfSMiklos Szeredi 
2108c7944ebbSTrond Myklebust full_reval:
2109c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
2110c7944ebbSTrond Myklebust }
2111535918f1STrond Myklebust 
2112c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2113c7944ebbSTrond Myklebust {
2114c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
2115c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
2116c0204fd2STrond Myklebust }
2117c0204fd2STrond Myklebust 
21181da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
21191da177e4SLinus Torvalds 
2120406cd915SBenjamin Coddington struct dentry *
2121406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2122cc6f3298SAnna Schumaker 				struct nfs_fattr *fattr)
21231da177e4SLinus Torvalds {
2124fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
21252b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
21261da177e4SLinus Torvalds 	struct inode *inode;
2127b0c6108eSAl Viro 	struct dentry *d;
2128406cd915SBenjamin Coddington 	int error;
21291da177e4SLinus Torvalds 
2130fab728e1STrond Myklebust 	d_drop(dentry);
2131fab728e1STrond Myklebust 
21321da177e4SLinus Torvalds 	if (fhandle->size == 0) {
21339558a007SAnna Schumaker 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
21341da177e4SLinus Torvalds 		if (error)
2135fab728e1STrond Myklebust 			goto out_error;
21361da177e4SLinus Torvalds 	}
21375724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
21381da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
21391da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2140a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
21412ef61e0eSAnna Schumaker 				fattr, NULL);
21421da177e4SLinus Torvalds 		if (error < 0)
2143fab728e1STrond Myklebust 			goto out_error;
21441da177e4SLinus Torvalds 	}
2145cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2146b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
2147fab728e1STrond Myklebust out:
2148fab728e1STrond Myklebust 	dput(parent);
2149406cd915SBenjamin Coddington 	return d;
2150fab728e1STrond Myklebust out_error:
2151406cd915SBenjamin Coddington 	d = ERR_PTR(error);
2152406cd915SBenjamin Coddington 	goto out;
2153406cd915SBenjamin Coddington }
2154406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2155406cd915SBenjamin Coddington 
2156406cd915SBenjamin Coddington /*
2157406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
2158406cd915SBenjamin Coddington  */
2159406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2160d91bfc46SAnna Schumaker 				struct nfs_fattr *fattr)
2161406cd915SBenjamin Coddington {
2162406cd915SBenjamin Coddington 	struct dentry *d;
2163406cd915SBenjamin Coddington 
2164cc6f3298SAnna Schumaker 	d = nfs_add_or_obtain(dentry, fhandle, fattr);
2165406cd915SBenjamin Coddington 	if (IS_ERR(d))
2166406cd915SBenjamin Coddington 		return PTR_ERR(d);
2167406cd915SBenjamin Coddington 
2168406cd915SBenjamin Coddington 	/* Callers don't care */
2169406cd915SBenjamin Coddington 	dput(d);
2170406cd915SBenjamin Coddington 	return 0;
21711da177e4SLinus Torvalds }
2172ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds /*
21751da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
21761da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
21771da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
21781da177e4SLinus Torvalds  * reply path made it appear to have failed.
21791da177e4SLinus Torvalds  */
2180549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2181549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, bool excl)
21821da177e4SLinus Torvalds {
21831da177e4SLinus Torvalds 	struct iattr attr;
2184ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
21851da177e4SLinus Torvalds 	int error;
21861da177e4SLinus Torvalds 
21871e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
21886de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	attr.ia_mode = mode;
21911da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
21921da177e4SLinus Torvalds 
21938b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
21948867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
21958b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
21961da177e4SLinus Torvalds 	if (error != 0)
21971da177e4SLinus Torvalds 		goto out_err;
21981da177e4SLinus Torvalds 	return 0;
21991da177e4SLinus Torvalds out_err:
22001da177e4SLinus Torvalds 	d_drop(dentry);
22011da177e4SLinus Torvalds 	return error;
22021da177e4SLinus Torvalds }
2203ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds /*
22061da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
22071da177e4SLinus Torvalds  */
2208597d9289SBryan Schumaker int
2209549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2210549c7297SChristian Brauner 	  struct dentry *dentry, umode_t mode, dev_t rdev)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds 	struct iattr attr;
22131da177e4SLinus Torvalds 	int status;
22141da177e4SLinus Torvalds 
22151e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
22166de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	attr.ia_mode = mode;
22191da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
22201da177e4SLinus Torvalds 
22211ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
22221da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
22231ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
22241da177e4SLinus Torvalds 	if (status != 0)
22251da177e4SLinus Torvalds 		goto out_err;
22261da177e4SLinus Torvalds 	return 0;
22271da177e4SLinus Torvalds out_err:
22281da177e4SLinus Torvalds 	d_drop(dentry);
22291da177e4SLinus Torvalds 	return status;
22301da177e4SLinus Torvalds }
2231ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds /*
22341da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
22351da177e4SLinus Torvalds  */
2236549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2237549c7297SChristian Brauner 	      struct dentry *dentry, umode_t mode)
22381da177e4SLinus Torvalds {
22391da177e4SLinus Torvalds 	struct iattr attr;
22401da177e4SLinus Torvalds 	int error;
22411da177e4SLinus Torvalds 
22421e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
22436de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
22461da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
22471da177e4SLinus Torvalds 
22481ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
22491da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
22501ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
22511da177e4SLinus Torvalds 	if (error != 0)
22521da177e4SLinus Torvalds 		goto out_err;
22531da177e4SLinus Torvalds 	return 0;
22541da177e4SLinus Torvalds out_err:
22551da177e4SLinus Torvalds 	d_drop(dentry);
22561da177e4SLinus Torvalds 	return error;
22571da177e4SLinus Torvalds }
2258ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
22591da177e4SLinus Torvalds 
2260d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2261d45b9d8bSTrond Myklebust {
2262dc3f4198SAl Viro 	if (simple_positive(dentry))
2263d45b9d8bSTrond Myklebust 		d_delete(dentry);
2264d45b9d8bSTrond Myklebust }
2265d45b9d8bSTrond Myklebust 
22669019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir,
22679019fb39STrond Myklebust 					   struct dentry *dentry, int error)
22689019fb39STrond Myklebust {
22699019fb39STrond Myklebust 	switch (error) {
22709019fb39STrond Myklebust 	case -ENOENT:
22719019fb39STrond Myklebust 		d_delete(dentry);
227200bdadc7STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
227300bdadc7STrond Myklebust 		break;
22749019fb39STrond Myklebust 	case 0:
227500bdadc7STrond Myklebust 		nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
22769019fb39STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
22779019fb39STrond Myklebust 	}
22789019fb39STrond Myklebust }
22799019fb39STrond Myklebust 
2280597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
22811da177e4SLinus Torvalds {
22821da177e4SLinus Torvalds 	int error;
22831da177e4SLinus Torvalds 
22841e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
22856de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22861da177e4SLinus Torvalds 
22871ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
22882b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2289884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
22901da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
22911da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2292ba6c0592STrond Myklebust 		switch (error) {
2293ba6c0592STrond Myklebust 		case 0:
22942b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2295ba6c0592STrond Myklebust 			break;
2296ba6c0592STrond Myklebust 		case -ENOENT:
2297d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2298ba6c0592STrond Myklebust 		}
2299884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2300ba6c0592STrond Myklebust 	} else
2301ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
23029019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
23031ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	return error;
23061da177e4SLinus Torvalds }
2307ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds /*
23101da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
23111da177e4SLinus Torvalds  * and after checking that the file has only one user.
23121da177e4SLinus Torvalds  *
23131da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
23141da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
23151da177e4SLinus Torvalds  */
23161da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
23171da177e4SLinus Torvalds {
23182b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
23192b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
23201da177e4SLinus Torvalds 	int error = -EBUSY;
23211da177e4SLinus Torvalds 
23226de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
23251da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
23261da177e4SLinus Torvalds 		error = 0;
23271da177e4SLinus Torvalds 		goto out;
23281da177e4SLinus Torvalds 	}
23291da177e4SLinus Torvalds 
23301ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
23311da177e4SLinus Torvalds 	if (inode != NULL) {
2332912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
23331da177e4SLinus Torvalds 		if (error == 0)
23341b83d707STrond Myklebust 			nfs_drop_nlink(inode);
23351da177e4SLinus Torvalds 	} else
2336912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2337d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2338d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
23391ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
23401da177e4SLinus Torvalds out:
23411da177e4SLinus Torvalds 	return error;
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
23451da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
23461da177e4SLinus Torvalds  *
23471da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
23481da177e4SLinus Torvalds  */
2349597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
23501da177e4SLinus Torvalds {
23511da177e4SLinus Torvalds 	int error;
23521da177e4SLinus Torvalds 	int need_rehash = 0;
23531da177e4SLinus Torvalds 
23541e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
23556de1472fSAl Viro 		dir->i_ino, dentry);
23561da177e4SLinus Torvalds 
23571ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
23581da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
235943245ecaSOlga Kornievskaia 	if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
236043245ecaSOlga Kornievskaia 					     &NFS_I(d_inode(dentry))->flags)) {
23611da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2362ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
23632b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
23641da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
23651ca42382STrond Myklebust 		goto out;
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
23681da177e4SLinus Torvalds 		__d_drop(dentry);
23691da177e4SLinus Torvalds 		need_rehash = 1;
23701da177e4SLinus Torvalds 	}
23711da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
23721da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
23739019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
23749019fb39STrond Myklebust 	if (need_rehash)
23751da177e4SLinus Torvalds 		d_rehash(dentry);
23761ca42382STrond Myklebust out:
23771ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
23781da177e4SLinus Torvalds 	return error;
23791da177e4SLinus Torvalds }
2380ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
23811da177e4SLinus Torvalds 
2382873101b3SChuck Lever /*
2383873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2384873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2385873101b3SChuck Lever  * using prepare_write/commit_write.
2386873101b3SChuck Lever  *
2387873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2388873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2389873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2390873101b3SChuck Lever  * symlink request has completed on the server.
2391873101b3SChuck Lever  *
2392873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2393873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2394873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2395873101b3SChuck Lever  * and move the raw page into its mapping.
2396873101b3SChuck Lever  */
2397549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2398549c7297SChristian Brauner 		struct dentry *dentry, const char *symname)
23991da177e4SLinus Torvalds {
2400873101b3SChuck Lever 	struct page *page;
2401873101b3SChuck Lever 	char *kaddr;
24021da177e4SLinus Torvalds 	struct iattr attr;
2403873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
24041da177e4SLinus Torvalds 	int error;
24051da177e4SLinus Torvalds 
24061e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
24076de1472fSAl Viro 		dir->i_ino, dentry, symname);
24081da177e4SLinus Torvalds 
2409873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2410873101b3SChuck Lever 		return -ENAMETOOLONG;
24111da177e4SLinus Torvalds 
2412873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2413873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
24141da177e4SLinus Torvalds 
2415e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
241676566991STrond Myklebust 	if (!page)
2417873101b3SChuck Lever 		return -ENOMEM;
2418873101b3SChuck Lever 
2419e8ecde25SAl Viro 	kaddr = page_address(page);
2420873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2421873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2422873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2423873101b3SChuck Lever 
24241ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
242594a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
24261ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2427873101b3SChuck Lever 	if (error != 0) {
24281e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2429873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
24306de1472fSAl Viro 			dentry, symname, error);
24311da177e4SLinus Torvalds 		d_drop(dentry);
2432873101b3SChuck Lever 		__free_page(page);
24331da177e4SLinus Torvalds 		return error;
24341da177e4SLinus Torvalds 	}
24351da177e4SLinus Torvalds 
2436342a67f0STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2437342a67f0STrond Myklebust 
2438873101b3SChuck Lever 	/*
2439873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2440873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2441873101b3SChuck Lever 	 */
24422b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2443873101b3SChuck Lever 							GFP_KERNEL)) {
2444873101b3SChuck Lever 		SetPageUptodate(page);
2445873101b3SChuck Lever 		unlock_page(page);
2446a0b54addSRafael Aquini 		/*
2447a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2448a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2449a0b54addSRafael Aquini 		 */
245009cbfeafSKirill A. Shutemov 		put_page(page);
2451873101b3SChuck Lever 	} else
2452873101b3SChuck Lever 		__free_page(page);
2453873101b3SChuck Lever 
2454873101b3SChuck Lever 	return 0;
2455873101b3SChuck Lever }
2456ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2457873101b3SChuck Lever 
2458597d9289SBryan Schumaker int
24591da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
24601da177e4SLinus Torvalds {
24612b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
24621da177e4SLinus Torvalds 	int error;
24631da177e4SLinus Torvalds 
24646de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
24656de1472fSAl Viro 		old_dentry, dentry);
24661da177e4SLinus Torvalds 
24671fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
24689697d234STrond Myklebust 	d_drop(dentry);
246920497503STrond Myklebust 	if (S_ISREG(inode->i_mode))
247020497503STrond Myklebust 		nfs_sync_inode(inode);
24711da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2472cf809556STrond Myklebust 	if (error == 0) {
2473342a67f0STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
24747de9c6eeSAl Viro 		ihold(inode);
24759697d234STrond Myklebust 		d_add(dentry, inode);
2476cf809556STrond Myklebust 	}
24771fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
24781da177e4SLinus Torvalds 	return error;
24791da177e4SLinus Torvalds }
2480ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds /*
24831da177e4SLinus Torvalds  * RENAME
24841da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
24851da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
24861da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
24871da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
24881da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
24891da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
24901da177e4SLinus Torvalds  *
24911da177e4SLinus Torvalds  * FIXED.
24921da177e4SLinus Torvalds  *
24931da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
24941da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
24951da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
24961da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
24971da177e4SLinus Torvalds  * using the inode layer
24981da177e4SLinus Torvalds  *
24991da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
25001da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
25011da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
25021da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
25031da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
25041da177e4SLinus Torvalds  * the rename.
25051da177e4SLinus Torvalds  */
2506549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2507549c7297SChristian Brauner 	       struct dentry *old_dentry, struct inode *new_dir,
2508549c7297SChristian Brauner 	       struct dentry *new_dentry, unsigned int flags)
25091da177e4SLinus Torvalds {
25102b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
25112b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2512d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
251380a491fdSJeff Layton 	struct rpc_task *task;
25141da177e4SLinus Torvalds 	int error = -EBUSY;
25151da177e4SLinus Torvalds 
25161cd66c93SMiklos Szeredi 	if (flags)
25171cd66c93SMiklos Szeredi 		return -EINVAL;
25181cd66c93SMiklos Szeredi 
25196de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
25206de1472fSAl Viro 		 old_dentry, new_dentry,
252184d08fa8SAl Viro 		 d_count(new_dentry));
25221da177e4SLinus Torvalds 
252370ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
25241da177e4SLinus Torvalds 	/*
252528f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
252628f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
252728f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
252828f79a1aSMiklos Szeredi 	 * the new target.
25291da177e4SLinus Torvalds 	 */
253027226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
253127226104SMiklos Szeredi 		/*
253227226104SMiklos Szeredi 		 * To prevent any new references to the target during the
253327226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
253427226104SMiklos Szeredi 		 */
2535d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
253627226104SMiklos Szeredi 			d_drop(new_dentry);
2537d9f29500SBenjamin Coddington 			rehash = new_dentry;
2538d9f29500SBenjamin Coddington 		}
253927226104SMiklos Szeredi 
254084d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
25411da177e4SLinus Torvalds 			int err;
254227226104SMiklos Szeredi 
25431da177e4SLinus Torvalds 			/* copy the target dentry's name */
25441da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
25451da177e4SLinus Torvalds 					 &new_dentry->d_name);
25461da177e4SLinus Torvalds 			if (!dentry)
25471da177e4SLinus Torvalds 				goto out;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
25501da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
255124e93025SMiklos Szeredi 			if (err)
25521da177e4SLinus Torvalds 				goto out;
255324e93025SMiklos Szeredi 
255424e93025SMiklos Szeredi 			new_dentry = dentry;
2555d9f29500SBenjamin Coddington 			rehash = NULL;
255624e93025SMiklos Szeredi 			new_inode = NULL;
2557b1e4adf4STrond Myklebust 		}
255827226104SMiklos Szeredi 	}
25591da177e4SLinus Torvalds 
25606ff9d99bSTrond Myklebust 	if (S_ISREG(old_inode->i_mode))
25616ff9d99bSTrond Myklebust 		nfs_sync_inode(old_inode);
2562d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
256380a491fdSJeff Layton 	if (IS_ERR(task)) {
256480a491fdSJeff Layton 		error = PTR_ERR(task);
256580a491fdSJeff Layton 		goto out;
256680a491fdSJeff Layton 	}
256780a491fdSJeff Layton 
256880a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2569818a8dbeSBenjamin Coddington 	if (error != 0) {
2570818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2571818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2572818a8dbeSBenjamin Coddington 		smp_wmb();
2573818a8dbeSBenjamin Coddington 	} else
257480a491fdSJeff Layton 		error = task->tk_status;
257580a491fdSJeff Layton 	rpc_put_task(task);
257659a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
257759a707b0STrond Myklebust 	if (error == 0) {
257859a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
257959a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2580ac46b3d7STrond Myklebust 		nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2581ac46b3d7STrond Myklebust 							 NFS_INO_INVALID_CTIME |
2582ac46b3d7STrond Myklebust 							 NFS_INO_REVAL_FORCED);
258359a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
258459a707b0STrond Myklebust 	}
25851da177e4SLinus Torvalds out:
2586d9f29500SBenjamin Coddington 	if (rehash)
2587d9f29500SBenjamin Coddington 		d_rehash(rehash);
258870ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
258970ded201STrond Myklebust 			new_dir, new_dentry, error);
2590d9f29500SBenjamin Coddington 	if (!error) {
2591d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2592d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2593d9f29500SBenjamin Coddington 		/*
2594d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2595d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2596d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2597d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2598d9f29500SBenjamin Coddington 		 */
2599d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2600d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2601d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2602d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2603d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2604d9f29500SBenjamin Coddington 
26051da177e4SLinus Torvalds 	/* new dentry created? */
26061da177e4SLinus Torvalds 	if (dentry)
26071da177e4SLinus Torvalds 		dput(dentry);
26081da177e4SLinus Torvalds 	return error;
26091da177e4SLinus Torvalds }
2610ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
26111da177e4SLinus Torvalds 
2612cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2613cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2614cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2615cfcea3e8STrond Myklebust 
2616a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
26173a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
26183a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
26193a505845STrond Myklebust 
26201c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
26211c3c07e9STrond Myklebust {
26226238aec8SNeilBrown 	put_group_info(entry->group_info);
2623f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
26244e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2625cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
26264e857c58SPeter Zijlstra 	smp_mb__after_atomic();
26271c3c07e9STrond Myklebust }
26281c3c07e9STrond Myklebust 
26291a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
26301a81bb8aSTrond Myklebust {
26311a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
26321a81bb8aSTrond Myklebust 
26331a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
26341a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
26351a81bb8aSTrond Myklebust 		list_del(&cache->lru);
26361a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
26371a81bb8aSTrond Myklebust 	}
26381a81bb8aSTrond Myklebust }
26391a81bb8aSTrond Myklebust 
26403a505845STrond Myklebust static unsigned long
26413a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2642979df72eSTrond Myklebust {
2643979df72eSTrond Myklebust 	LIST_HEAD(head);
2644aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2645979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
26461ab6c499SDave Chinner 	long freed = 0;
2647979df72eSTrond Myklebust 
2648a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2649aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2650979df72eSTrond Myklebust 		struct inode *inode;
2651979df72eSTrond Myklebust 
2652979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2653979df72eSTrond Myklebust 			break;
26549c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2655979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2656979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2657979df72eSTrond Myklebust 			goto remove_lru_entry;
2658979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2659979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2660979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2661979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
26621ab6c499SDave Chinner 		freed++;
2663979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2664979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2665979df72eSTrond Myklebust 					&nfs_access_lru_list);
2666979df72eSTrond Myklebust 		else {
2667979df72eSTrond Myklebust remove_lru_entry:
2668979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
26694e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2670979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
26714e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2672979df72eSTrond Myklebust 		}
267359844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2674979df72eSTrond Myklebust 	}
2675979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
26761a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
26771ab6c499SDave Chinner 	return freed;
26781ab6c499SDave Chinner }
26791ab6c499SDave Chinner 
26801ab6c499SDave Chinner unsigned long
26813a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
26823a505845STrond Myklebust {
26833a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
26843a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
26853a505845STrond Myklebust 
26863a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
26873a505845STrond Myklebust 		return SHRINK_STOP;
26883a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
26893a505845STrond Myklebust }
26903a505845STrond Myklebust 
26913a505845STrond Myklebust 
26923a505845STrond Myklebust unsigned long
26931ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
26941ab6c499SDave Chinner {
269555f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2696979df72eSTrond Myklebust }
2697979df72eSTrond Myklebust 
26983a505845STrond Myklebust static void
26993a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
27003a505845STrond Myklebust {
27013a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
27023a505845STrond Myklebust 	unsigned long diff;
27033a505845STrond Myklebust 	unsigned int nr_to_scan;
27043a505845STrond Myklebust 
27053a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
27063a505845STrond Myklebust 		return;
27073a505845STrond Myklebust 	nr_to_scan = 100;
27083a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
27093a505845STrond Myklebust 	if (diff < nr_to_scan)
27103a505845STrond Myklebust 		nr_to_scan = diff;
27113a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
27123a505845STrond Myklebust }
27133a505845STrond Myklebust 
27141a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
27151c3c07e9STrond Myklebust {
27161c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
27171a81bb8aSTrond Myklebust 	struct rb_node *n;
27181c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
27191c3c07e9STrond Myklebust 
27201c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
27211c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
27221c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
27231c3c07e9STrond Myklebust 		rb_erase(n, root_node);
27241a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
27251c3c07e9STrond Myklebust 	}
27261c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
27271c3c07e9STrond Myklebust }
27281c3c07e9STrond Myklebust 
27291c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
27301c3c07e9STrond Myklebust {
27311a81bb8aSTrond Myklebust 	LIST_HEAD(head);
27321a81bb8aSTrond Myklebust 
27331a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
27341a81bb8aSTrond Myklebust 		return;
2735cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2736cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
27371a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2738cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2739cfcea3e8STrond Myklebust 
27401c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
27411a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
27421a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
27431a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
27441a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
27451c3c07e9STrond Myklebust }
27461c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
27471c3c07e9STrond Myklebust 
27486238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
27496238aec8SNeilBrown {
27506238aec8SNeilBrown 	struct group_info *ga, *gb;
27516238aec8SNeilBrown 	int g;
27526238aec8SNeilBrown 
27536238aec8SNeilBrown 	if (uid_lt(a->fsuid, b->fsuid))
27546238aec8SNeilBrown 		return -1;
27556238aec8SNeilBrown 	if (uid_gt(a->fsuid, b->fsuid))
27566238aec8SNeilBrown 		return 1;
27576238aec8SNeilBrown 
27586238aec8SNeilBrown 	if (gid_lt(a->fsgid, b->fsgid))
27596238aec8SNeilBrown 		return -1;
27606238aec8SNeilBrown 	if (gid_gt(a->fsgid, b->fsgid))
27616238aec8SNeilBrown 		return 1;
27626238aec8SNeilBrown 
27636238aec8SNeilBrown 	ga = a->group_info;
27646238aec8SNeilBrown 	gb = b->group_info;
27656238aec8SNeilBrown 	if (ga == gb)
27666238aec8SNeilBrown 		return 0;
27676238aec8SNeilBrown 	if (ga == NULL)
27686238aec8SNeilBrown 		return -1;
27696238aec8SNeilBrown 	if (gb == NULL)
27706238aec8SNeilBrown 		return 1;
27716238aec8SNeilBrown 	if (ga->ngroups < gb->ngroups)
27726238aec8SNeilBrown 		return -1;
27736238aec8SNeilBrown 	if (ga->ngroups > gb->ngroups)
27746238aec8SNeilBrown 		return 1;
27756238aec8SNeilBrown 
27766238aec8SNeilBrown 	for (g = 0; g < ga->ngroups; g++) {
27776238aec8SNeilBrown 		if (gid_lt(ga->gid[g], gb->gid[g]))
27786238aec8SNeilBrown 			return -1;
27796238aec8SNeilBrown 		if (gid_gt(ga->gid[g], gb->gid[g]))
27806238aec8SNeilBrown 			return 1;
27816238aec8SNeilBrown 	}
27826238aec8SNeilBrown 	return 0;
27836238aec8SNeilBrown }
27846238aec8SNeilBrown 
2785b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
27861c3c07e9STrond Myklebust {
27871c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
27881c3c07e9STrond Myklebust 
27891c3c07e9STrond Myklebust 	while (n != NULL) {
2790b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2791b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
27926238aec8SNeilBrown 		int cmp = access_cmp(cred, entry);
27931c3c07e9STrond Myklebust 
2794b68572e0SNeilBrown 		if (cmp < 0)
27951c3c07e9STrond Myklebust 			n = n->rb_left;
2796b68572e0SNeilBrown 		else if (cmp > 0)
27971c3c07e9STrond Myklebust 			n = n->rb_right;
27981c3c07e9STrond Myklebust 		else
27991c3c07e9STrond Myklebust 			return entry;
28001c3c07e9STrond Myklebust 	}
28011c3c07e9STrond Myklebust 	return NULL;
28021c3c07e9STrond Myklebust }
28031c3c07e9STrond Myklebust 
2804b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
28051da177e4SLinus Torvalds {
280655296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
28071c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
280857b69181STrond Myklebust 	bool retry = true;
280957b69181STrond Myklebust 	int err;
28101da177e4SLinus Torvalds 
28111c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
281257b69181STrond Myklebust 	for(;;) {
28131c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
28141c3c07e9STrond Myklebust 			goto out_zap;
28151c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
281657b69181STrond Myklebust 		err = -ENOENT;
28171c3c07e9STrond Myklebust 		if (cache == NULL)
28181c3c07e9STrond Myklebust 			goto out;
281957b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
282021c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
282157b69181STrond Myklebust 			break;
28225c965db8STrond Myklebust 		if (!retry)
28235c965db8STrond Myklebust 			break;
282457b69181STrond Myklebust 		err = -ECHILD;
282557b69181STrond Myklebust 		if (!may_block)
282657b69181STrond Myklebust 			goto out;
282757b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
282857b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
282957b69181STrond Myklebust 		if (err)
283057b69181STrond Myklebust 			return err;
283157b69181STrond Myklebust 		spin_lock(&inode->i_lock);
283257b69181STrond Myklebust 		retry = false;
283357b69181STrond Myklebust 	}
2834b5e7b59cSNeilBrown 	*mask = cache->mask;
2835cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
28361c3c07e9STrond Myklebust 	err = 0;
28371c3c07e9STrond Myklebust out:
28381c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
28391c3c07e9STrond Myklebust 	return err;
28401c3c07e9STrond Myklebust out_zap:
28411a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
28421a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
28431c3c07e9STrond Myklebust 	return -ENOENT;
28441c3c07e9STrond Myklebust }
28451c3c07e9STrond Myklebust 
2846b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2847f682a398SNeilBrown {
2848f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2849f682a398SNeilBrown 	 * but do it without locking.
2850f682a398SNeilBrown 	 */
2851f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2852f682a398SNeilBrown 	struct nfs_access_entry *cache;
2853f682a398SNeilBrown 	int err = -ECHILD;
2854f682a398SNeilBrown 	struct list_head *lh;
2855f682a398SNeilBrown 
2856f682a398SNeilBrown 	rcu_read_lock();
2857f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2858f682a398SNeilBrown 		goto out;
28599f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2860f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2861f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
28626238aec8SNeilBrown 	    access_cmp(cred, cache) != 0)
2863f682a398SNeilBrown 		cache = NULL;
2864f682a398SNeilBrown 	if (cache == NULL)
2865f682a398SNeilBrown 		goto out;
286621c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2867f682a398SNeilBrown 		goto out;
2868b5e7b59cSNeilBrown 	*mask = cache->mask;
286921c3ba7eSTrond Myklebust 	err = 0;
2870f682a398SNeilBrown out:
2871f682a398SNeilBrown 	rcu_read_unlock();
2872f682a398SNeilBrown 	return err;
2873f682a398SNeilBrown }
2874f682a398SNeilBrown 
2875b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2876b5e7b59cSNeilBrown 			  u32 *mask, bool may_block)
2877d2ae4f8bSFrank van der Linden {
2878d2ae4f8bSFrank van der Linden 	int status;
2879d2ae4f8bSFrank van der Linden 
2880b5e7b59cSNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, mask);
2881d2ae4f8bSFrank van der Linden 	if (status != 0)
2882b5e7b59cSNeilBrown 		status = nfs_access_get_cached_locked(inode, cred, mask,
2883d2ae4f8bSFrank van der Linden 		    may_block);
2884d2ae4f8bSFrank van der Linden 
2885d2ae4f8bSFrank van der Linden 	return status;
2886d2ae4f8bSFrank van der Linden }
2887d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2888d2ae4f8bSFrank van der Linden 
288973fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode,
289073fbb3faSNeilBrown 				  struct nfs_access_entry *set,
289173fbb3faSNeilBrown 				  const struct cred *cred)
28921c3c07e9STrond Myklebust {
2893cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2894cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
28951c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
28961c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
28971c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2898b68572e0SNeilBrown 	int cmp;
28991c3c07e9STrond Myklebust 
29001c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
29011c3c07e9STrond Myklebust 	while (*p != NULL) {
29021c3c07e9STrond Myklebust 		parent = *p;
29031c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
29046238aec8SNeilBrown 		cmp = access_cmp(cred, entry);
29051c3c07e9STrond Myklebust 
2906b68572e0SNeilBrown 		if (cmp < 0)
29071c3c07e9STrond Myklebust 			p = &parent->rb_left;
2908b68572e0SNeilBrown 		else if (cmp > 0)
29091c3c07e9STrond Myklebust 			p = &parent->rb_right;
29101c3c07e9STrond Myklebust 		else
29111c3c07e9STrond Myklebust 			goto found;
29121c3c07e9STrond Myklebust 	}
29131c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
29141c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2915cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
29161c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
29171c3c07e9STrond Myklebust 	return;
29181c3c07e9STrond Myklebust found:
29191c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2920cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2921cfcea3e8STrond Myklebust 	list_del(&entry->lru);
29221c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
29231c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
29241da177e4SLinus Torvalds }
29251da177e4SLinus Torvalds 
292673fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
292773fbb3faSNeilBrown 			  const struct cred *cred)
29281da177e4SLinus Torvalds {
29291c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
29301c3c07e9STrond Myklebust 	if (cache == NULL)
29311c3c07e9STrond Myklebust 		return;
29321c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
29336238aec8SNeilBrown 	cache->fsuid = cred->fsuid;
29346238aec8SNeilBrown 	cache->fsgid = cred->fsgid;
29356238aec8SNeilBrown 	cache->group_info = get_group_info(cred->group_info);
29361da177e4SLinus Torvalds 	cache->mask = set->mask;
29371c3c07e9STrond Myklebust 
2938f682a398SNeilBrown 	/* The above field assignments must be visible
2939f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2940f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2941f682a398SNeilBrown 	 */
2942f682a398SNeilBrown 	smp_wmb();
294373fbb3faSNeilBrown 	nfs_access_add_rbtree(inode, cache, cred);
2944cfcea3e8STrond Myklebust 
2945cfcea3e8STrond Myklebust 	/* Update accounting */
29464e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2947cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
29484e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2949cfcea3e8STrond Myklebust 
2950cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
29511a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2952cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
29531a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
29541a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
29551a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2956cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2957cfcea3e8STrond Myklebust 	}
29583a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
29591da177e4SLinus Torvalds }
29606168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
29616168f62cSWeston Andros Adamson 
29623c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
29633c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
29643c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
29653c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
29663c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
29673c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2968ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
29693c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
29703c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
297115d4b73aSTrond Myklebust static int
2972ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
297315d4b73aSTrond Myklebust {
297415d4b73aSTrond Myklebust 	int mask = 0;
297515d4b73aSTrond Myklebust 
297615d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
297715d4b73aSTrond Myklebust 		mask |= MAY_READ;
2978ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2979ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
298015d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2981ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
298215d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2983ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2984ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2985ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2986ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
298715d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2988ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2989ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
299015d4b73aSTrond Myklebust 	return mask;
299115d4b73aSTrond Myklebust }
299215d4b73aSTrond Myklebust 
29936168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
29946168f62cSWeston Andros Adamson {
2995bd8b2441STrond Myklebust 	entry->mask = access_result;
29966168f62cSWeston Andros Adamson }
29976168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
29981da177e4SLinus Torvalds 
2999b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
30001da177e4SLinus Torvalds {
30011da177e4SLinus Torvalds 	struct nfs_access_entry cache;
300257b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3003e8194b7dSTrond Myklebust 	int cache_mask = -1;
30041da177e4SLinus Torvalds 	int status;
30051da177e4SLinus Torvalds 
3006f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
3007f4ce1299STrond Myklebust 
3008b5e7b59cSNeilBrown 	status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
30091da177e4SLinus Torvalds 	if (status == 0)
3010f4ce1299STrond Myklebust 		goto out_cached;
30111da177e4SLinus Torvalds 
3012f3324a2aSNeilBrown 	status = -ECHILD;
301357b69181STrond Myklebust 	if (!may_block)
3014f3324a2aSNeilBrown 		goto out;
3015f3324a2aSNeilBrown 
30161750d929SAnna Schumaker 	/*
30171750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
30181750d929SAnna Schumaker 	 */
301984631f84STrond Myklebust 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
302084631f84STrond Myklebust 		     nfs_access_xattr_mask(NFS_SERVER(inode));
30211750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
30221750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
30231750d929SAnna Schumaker 	else
30241750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
302573fbb3faSNeilBrown 	status = NFS_PROTO(inode)->access(inode, &cache, cred);
3026a71ee337SSuresh Jayaraman 	if (status != 0) {
3027a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
3028a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
302993ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
303093ce4af7STrond Myklebust 			else
303193ce4af7STrond Myklebust 				nfs_zap_caches(inode);
3032a71ee337SSuresh Jayaraman 		}
3033f4ce1299STrond Myklebust 		goto out;
3034a71ee337SSuresh Jayaraman 	}
303573fbb3faSNeilBrown 	nfs_access_add_cache(inode, &cache, cred);
3036f4ce1299STrond Myklebust out_cached:
3037ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3038bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3039f4ce1299STrond Myklebust 		status = -EACCES;
30401da177e4SLinus Torvalds out:
3041e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
3042f4ce1299STrond Myklebust 	return status;
30431da177e4SLinus Torvalds }
30441da177e4SLinus Torvalds 
3045af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
3046af22f94aSTrond Myklebust {
3047af22f94aSTrond Myklebust 	int mask = 0;
3048af22f94aSTrond Myklebust 
3049f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
3050f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
3051f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
3052f8d9a897SWeston Andros Adamson 	} else {
30538a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
3054af22f94aSTrond Myklebust 			mask |= MAY_READ;
30558a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
3056af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
3057f8d9a897SWeston Andros Adamson 	}
3058f8d9a897SWeston Andros Adamson 
3059af22f94aSTrond Myklebust 	return mask;
3060af22f94aSTrond Myklebust }
3061af22f94aSTrond Myklebust 
3062b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3063af22f94aSTrond Myklebust {
3064af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3065af22f94aSTrond Myklebust }
306689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
3067af22f94aSTrond Myklebust 
30685c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
30695c5fc09aSTrond Myklebust {
30705c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
307121c3ba7eSTrond Myklebust 	int ret = 0;
30725c5fc09aSTrond Myklebust 
30733825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
30743825827eSTrond Myklebust 		return 0;
3075720869ebSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
30765c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
307721c3ba7eSTrond Myklebust 			return -ECHILD;
307821c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
307921c3ba7eSTrond Myklebust 	}
30805c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
30815c5fc09aSTrond Myklebust 		ret = -EACCES;
30825c5fc09aSTrond Myklebust 	return ret;
30835c5fc09aSTrond Myklebust }
30845c5fc09aSTrond Myklebust 
3085549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns,
3086549c7297SChristian Brauner 		   struct inode *inode,
3087549c7297SChristian Brauner 		   int mask)
30881da177e4SLinus Torvalds {
3089b68572e0SNeilBrown 	const struct cred *cred = current_cred();
30901da177e4SLinus Torvalds 	int res = 0;
30911da177e4SLinus Torvalds 
309291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
309391d5b470SChuck Lever 
3094e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
30951da177e4SLinus Torvalds 		goto out;
30961da177e4SLinus Torvalds 	/* Is this sys_access() ? */
30979cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
30981da177e4SLinus Torvalds 		goto force_lookup;
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
31011da177e4SLinus Torvalds 		case S_IFLNK:
31021da177e4SLinus Torvalds 			goto out;
31031da177e4SLinus Torvalds 		case S_IFREG:
3104762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
3105762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3106762674f8STrond Myklebust 				return 0;
31071da177e4SLinus Torvalds 			break;
31081da177e4SLinus Torvalds 		case S_IFDIR:
31091da177e4SLinus Torvalds 			/*
31101da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
31111da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
31121da177e4SLinus Torvalds 			 */
31131da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
31141da177e4SLinus Torvalds 				goto out;
31151da177e4SLinus Torvalds 	}
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds force_lookup:
31181da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
31191da177e4SLinus Torvalds 		goto out_notsup;
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
31221da177e4SLinus Torvalds out:
31235c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
31245c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
3125f696a365SMiklos Szeredi 
31261e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
31271e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
31281da177e4SLinus Torvalds 	return res;
31291da177e4SLinus Torvalds out_notsup:
3130d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
3131d51ac1a8SNeilBrown 		return -ECHILD;
3132d51ac1a8SNeilBrown 
3133720869ebSTrond Myklebust 	res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3134720869ebSTrond Myklebust 						  NFS_INO_INVALID_OTHER);
31351da177e4SLinus Torvalds 	if (res == 0)
313647291baaSChristian Brauner 		res = generic_permission(&init_user_ns, inode, mask);
31371e7cb3dcSChuck Lever 	goto out;
31381da177e4SLinus Torvalds }
3139ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
3140