xref: /openbmc/linux/fs/nfs/dir.c (revision d1e32ea35502bcbf9241c54338882f18b6fa6452)
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 
7293b8959aSTrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir)
73480c2006SBryan Schumaker {
74311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
75480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
76d7867712STrond Myklebust 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
77480c2006SBryan Schumaker 	if (ctx != NULL) {
788ef2ce3eSBryan Schumaker 		ctx->duped = 0;
79311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
80480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
818ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
82ff81dfb5STrond Myklebust 		ctx->page_index = 0;
83e1d2699bSTrond Myklebust 		ctx->eof = false;
84311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
851c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
861c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
87ac46b3d7STrond Myklebust 			nfs_set_cache_invalid(dir,
88ac46b3d7STrond Myklebust 					      NFS_INO_INVALID_DATA |
89ac46b3d7STrond Myklebust 						      NFS_INO_REVAL_FORCED);
90311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
91ff81dfb5STrond Myklebust 		clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
92*d1e32ea3STrond Myklebust 		memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
93311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
94480c2006SBryan Schumaker 		return ctx;
95480c2006SBryan Schumaker 	}
960c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
970c030806STrond Myklebust }
98480c2006SBryan Schumaker 
99311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
100480c2006SBryan Schumaker {
101311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
102311324adSTrond Myklebust 	list_del(&ctx->list);
103311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
104480c2006SBryan Schumaker 	kfree(ctx);
105480c2006SBryan Schumaker }
106480c2006SBryan Schumaker 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * Open file
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds static int
1111da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1121da177e4SLinus Torvalds {
113480c2006SBryan Schumaker 	int res = 0;
114480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1151da177e4SLinus Torvalds 
1166de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
117cc0dd2d1SChuck Lever 
118cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1191e7cb3dcSChuck Lever 
12093b8959aSTrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode);
121480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
122480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
123480c2006SBryan Schumaker 		goto out;
124480c2006SBryan Schumaker 	}
125480c2006SBryan Schumaker 	filp->private_data = ctx;
126480c2006SBryan Schumaker out:
1271da177e4SLinus Torvalds 	return res;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
130480c2006SBryan Schumaker static int
131480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
132480c2006SBryan Schumaker {
133a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
134480c2006SBryan Schumaker 	return 0;
135480c2006SBryan Schumaker }
136480c2006SBryan Schumaker 
137d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
138d1bacf9eSBryan Schumaker 	u64 cookie;
139d1bacf9eSBryan Schumaker 	u64 ino;
140a52a8a6aSTrond Myklebust 	const char *name;
141a52a8a6aSTrond Myklebust 	unsigned int name_len;
1420b26a0bfSTrond Myklebust 	unsigned char d_type;
143d1bacf9eSBryan Schumaker };
144d1bacf9eSBryan Schumaker 
145d1bacf9eSBryan Schumaker struct nfs_cache_array {
146d1bacf9eSBryan Schumaker 	u64 last_cookie;
147b1e21c97STrond Myklebust 	unsigned int size;
148b1e21c97STrond Myklebust 	unsigned char page_full : 1,
149762567b7STrond Myklebust 		      page_is_eof : 1,
150762567b7STrond Myklebust 		      cookies_are_ordered : 1;
1515601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
152d1bacf9eSBryan Schumaker };
153d1bacf9eSBryan Schumaker 
1546c981effSTrond Myklebust struct nfs_readdir_descriptor {
1551da177e4SLinus Torvalds 	struct file	*file;
1561da177e4SLinus Torvalds 	struct page	*page;
15723db8620SAl Viro 	struct dir_context *ctx;
1581f1d4aa4STrond Myklebust 	pgoff_t		page_index;
1592e7a4641STrond Myklebust 	u64		dir_cookie;
1600aded708STrond Myklebust 	u64		last_cookie;
1612e7a4641STrond Myklebust 	u64		dup_cookie;
162f0dd2136STrond Myklebust 	loff_t		current_index;
16359e356a9STrond Myklebust 	loff_t		prev_index;
164d1bacf9eSBryan Schumaker 
165b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
166a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1671f4eab7eSNeil Brown 	unsigned long	timestamp;
1684704f0e2STrond Myklebust 	unsigned long	gencount;
1692e7a4641STrond Myklebust 	unsigned long	attr_gencount;
170d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
1712e7a4641STrond Myklebust 	signed char duped;
172a7a3b1e9SBenjamin Coddington 	bool plus;
173e1d2699bSTrond Myklebust 	bool eob;
174a7a3b1e9SBenjamin Coddington 	bool eof;
1756c981effSTrond Myklebust };
1761da177e4SLinus Torvalds 
1771f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array)
1781f1d4aa4STrond Myklebust {
1791f1d4aa4STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
1801f1d4aa4STrond Myklebust }
1811f1d4aa4STrond Myklebust 
1821f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie)
1834b310319STrond Myklebust {
1844b310319STrond Myklebust 	struct nfs_cache_array *array;
1854b310319STrond Myklebust 
1864b310319STrond Myklebust 	array = kmap_atomic(page);
1871f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
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)
21435df59d3STrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie);
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 
255b1e21c97STrond Myklebust /*
256b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
257b1e21c97STrond Myklebust  */
258b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
259b1e21c97STrond Myklebust {
260b1e21c97STrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
261b1e21c97STrond Myklebust 
262b1e21c97STrond Myklebust 	if (array->page_full)
263b1e21c97STrond Myklebust 		return -ENOSPC;
264b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size + 1];
265b1e21c97STrond Myklebust 	if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
266b1e21c97STrond Myklebust 		array->page_full = 1;
267b1e21c97STrond Myklebust 		return -ENOSPC;
268b1e21c97STrond Myklebust 	}
269d1bacf9eSBryan Schumaker 	return 0;
270d1bacf9eSBryan Schumaker }
271d1bacf9eSBryan Schumaker 
272d1bacf9eSBryan Schumaker static
273d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
274d1bacf9eSBryan Schumaker {
275a52a8a6aSTrond Myklebust 	struct nfs_cache_array *array;
2764a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
277a52a8a6aSTrond Myklebust 	const char *name;
2784a201d6eSTrond Myklebust 	int ret;
2794a201d6eSTrond Myklebust 
280a52a8a6aSTrond Myklebust 	name = nfs_readdir_copy_name(entry->name, entry->len);
281a52a8a6aSTrond Myklebust 	if (!name)
282a52a8a6aSTrond Myklebust 		return -ENOMEM;
2833020093fSTrond Myklebust 
284a52a8a6aSTrond Myklebust 	array = kmap_atomic(page);
285b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
286a52a8a6aSTrond Myklebust 	if (ret) {
287a52a8a6aSTrond Myklebust 		kfree(name);
2883020093fSTrond Myklebust 		goto out;
289a52a8a6aSTrond Myklebust 	}
2903020093fSTrond Myklebust 
291b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
2924a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2934a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2940b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
295a52a8a6aSTrond Myklebust 	cache_entry->name_len = entry->len;
296a52a8a6aSTrond Myklebust 	cache_entry->name = name;
297d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
298762567b7STrond Myklebust 	if (array->last_cookie <= cache_entry->cookie)
299762567b7STrond Myklebust 		array->cookies_are_ordered = 0;
3008cd51a0cSTrond Myklebust 	array->size++;
30147c716cbSTrond Myklebust 	if (entry->eof != 0)
302b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
3034a201d6eSTrond Myklebust out:
304a52a8a6aSTrond Myklebust 	kunmap_atomic(array);
3054a201d6eSTrond Myklebust 	return ret;
306d1bacf9eSBryan Schumaker }
307d1bacf9eSBryan Schumaker 
3081f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
3091f1d4aa4STrond Myklebust 						pgoff_t index, u64 last_cookie)
3101f1d4aa4STrond Myklebust {
3111f1d4aa4STrond Myklebust 	struct page *page;
3121f1d4aa4STrond Myklebust 
3131f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
3141f1d4aa4STrond Myklebust 	if (page && !PageUptodate(page)) {
3151f1d4aa4STrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie);
3161f1d4aa4STrond Myklebust 		if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
3171f1d4aa4STrond Myklebust 			nfs_zap_mapping(mapping->host, mapping);
3181f1d4aa4STrond Myklebust 		SetPageUptodate(page);
3191f1d4aa4STrond Myklebust 	}
3201f1d4aa4STrond Myklebust 
3211f1d4aa4STrond Myklebust 	return page;
3221f1d4aa4STrond Myklebust }
3231f1d4aa4STrond Myklebust 
3241f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
3251f1d4aa4STrond Myklebust {
3261f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3271f1d4aa4STrond Myklebust 	u64 ret;
3281f1d4aa4STrond Myklebust 
3291f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3301f1d4aa4STrond Myklebust 	ret = array->last_cookie;
3311f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3321f1d4aa4STrond Myklebust 	return ret;
3331f1d4aa4STrond Myklebust }
3341f1d4aa4STrond Myklebust 
3351f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
3361f1d4aa4STrond Myklebust {
3371f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3381f1d4aa4STrond Myklebust 	bool ret;
3391f1d4aa4STrond Myklebust 
3401f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3411f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
3421f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3431f1d4aa4STrond Myklebust 	return ret;
3441f1d4aa4STrond Myklebust }
3451f1d4aa4STrond Myklebust 
346b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
347b1e21c97STrond Myklebust {
348b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
349b1e21c97STrond Myklebust 
350b1e21c97STrond Myklebust 	array = kmap_atomic(page);
351b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
352b1e21c97STrond Myklebust 	kunmap_atomic(array);
353b1e21c97STrond Myklebust }
354b1e21c97STrond Myklebust 
3553b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
3563b2a09f1STrond Myklebust {
3573b2a09f1STrond Myklebust 	unlock_page(page);
3583b2a09f1STrond Myklebust 	put_page(page);
3593b2a09f1STrond Myklebust }
3603b2a09f1STrond Myklebust 
3613b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
3623b2a09f1STrond Myklebust 					      pgoff_t index, u64 cookie)
3633b2a09f1STrond Myklebust {
3643b2a09f1STrond Myklebust 	struct page *page;
3653b2a09f1STrond Myklebust 
3663b2a09f1STrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
3673b2a09f1STrond Myklebust 	if (page) {
3683b2a09f1STrond Myklebust 		if (nfs_readdir_page_last_cookie(page) == cookie)
3693b2a09f1STrond Myklebust 			return page;
3703b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
3713b2a09f1STrond Myklebust 	}
3723b2a09f1STrond Myklebust 	return NULL;
3733b2a09f1STrond Myklebust }
3743b2a09f1STrond Myklebust 
37559e356a9STrond Myklebust static inline
37659e356a9STrond Myklebust int is_32bit_api(void)
37759e356a9STrond Myklebust {
37859e356a9STrond Myklebust #ifdef CONFIG_COMPAT
37959e356a9STrond Myklebust 	return in_compat_syscall();
38059e356a9STrond Myklebust #else
38159e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
38259e356a9STrond Myklebust #endif
38359e356a9STrond Myklebust }
38459e356a9STrond Myklebust 
38559e356a9STrond Myklebust static
38659e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
38759e356a9STrond Myklebust {
38859e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
38959e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
39059e356a9STrond Myklebust 		return false;
39159e356a9STrond Myklebust 	return true;
39259e356a9STrond Myklebust }
39359e356a9STrond Myklebust 
3946c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
3956c981effSTrond Myklebust 				      struct nfs_readdir_descriptor *desc)
396d1bacf9eSBryan Schumaker {
39723db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
398d1bacf9eSBryan Schumaker 	unsigned int index;
399d1bacf9eSBryan Schumaker 
400d1bacf9eSBryan Schumaker 	if (diff < 0)
401d1bacf9eSBryan Schumaker 		goto out_eof;
402d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
403b1e21c97STrond Myklebust 		if (array->page_is_eof)
404d1bacf9eSBryan Schumaker 			goto out_eof;
405d1bacf9eSBryan Schumaker 		return -EAGAIN;
406d1bacf9eSBryan Schumaker 	}
407d1bacf9eSBryan Schumaker 
408d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
4092e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
410d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
411d1bacf9eSBryan Schumaker 	return 0;
412d1bacf9eSBryan Schumaker out_eof:
4136089dd0dSThomas Meyer 	desc->eof = true;
414d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
415d1bacf9eSBryan Schumaker }
416d1bacf9eSBryan Schumaker 
4174db72b40SJeff Layton static bool
4184db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
4194db72b40SJeff Layton {
4202929bc33STrond Myklebust 	if (nfsi->cache_validity & (NFS_INO_INVALID_CHANGE |
4212929bc33STrond Myklebust 				    NFS_INO_INVALID_DATA))
4224db72b40SJeff Layton 		return false;
4234db72b40SJeff Layton 	smp_rmb();
4244db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
4254db72b40SJeff Layton }
4264db72b40SJeff Layton 
427762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
428762567b7STrond Myklebust 					      u64 cookie)
429762567b7STrond Myklebust {
430762567b7STrond Myklebust 	if (!array->cookies_are_ordered)
431762567b7STrond Myklebust 		return true;
432762567b7STrond Myklebust 	/* Optimisation for monotonically increasing cookies */
433762567b7STrond Myklebust 	if (cookie >= array->last_cookie)
434762567b7STrond Myklebust 		return false;
435762567b7STrond Myklebust 	if (array->size && cookie < array->array[0].cookie)
436762567b7STrond Myklebust 		return false;
437762567b7STrond Myklebust 	return true;
438762567b7STrond Myklebust }
439762567b7STrond Myklebust 
4406c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
4416c981effSTrond Myklebust 					 struct nfs_readdir_descriptor *desc)
442d1bacf9eSBryan Schumaker {
443d1bacf9eSBryan Schumaker 	int i;
4448ef2ce3eSBryan Schumaker 	loff_t new_pos;
445d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
446d1bacf9eSBryan Schumaker 
447762567b7STrond Myklebust 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
448762567b7STrond Myklebust 		goto check_eof;
449762567b7STrond Myklebust 
450d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
4512e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
452496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
4530c030806STrond Myklebust 
4548ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
4552e7a4641STrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount ||
4564db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
4572e7a4641STrond Myklebust 				desc->duped = 0;
4582e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
45959e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
4602e7a4641STrond Myklebust 				if (desc->duped > 0
4612e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
4620c030806STrond Myklebust 					if (printk_ratelimit()) {
4636de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
4640c030806STrond Myklebust 								"Please contact your server vendor.  "
465a52a8a6aSTrond Myklebust 								"The file: %s has duplicate cookie %llu\n",
466a52a8a6aSTrond Myklebust 								desc->file, array->array[i].name, desc->dir_cookie);
4670c030806STrond Myklebust 					}
4680c030806STrond Myklebust 					status = -ELOOP;
4690c030806STrond Myklebust 					goto out;
4700c030806STrond Myklebust 				}
4712e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
4722e7a4641STrond Myklebust 				desc->duped = -1;
4738ef2ce3eSBryan Schumaker 			}
47459e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
4752e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
47659e356a9STrond Myklebust 			else
47723db8620SAl Viro 				desc->ctx->pos = new_pos;
47859e356a9STrond Myklebust 			desc->prev_index = new_pos;
4798cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
48047c716cbSTrond Myklebust 			return 0;
4818cd51a0cSTrond Myklebust 		}
4828cd51a0cSTrond Myklebust 	}
483762567b7STrond Myklebust check_eof:
484b1e21c97STrond Myklebust 	if (array->page_is_eof) {
485d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
4862e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
4876089dd0dSThomas Meyer 			desc->eof = true;
488d1bacf9eSBryan Schumaker 	}
4890c030806STrond Myklebust out:
490d1bacf9eSBryan Schumaker 	return status;
491d1bacf9eSBryan Schumaker }
492d1bacf9eSBryan Schumaker 
4936c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
494d1bacf9eSBryan Schumaker {
495d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
49647c716cbSTrond Myklebust 	int status;
497d1bacf9eSBryan Schumaker 
498ed09222dSTrond Myklebust 	array = kmap_atomic(desc->page);
499d1bacf9eSBryan Schumaker 
5002e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
501d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
502d1bacf9eSBryan Schumaker 	else
503d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
504d1bacf9eSBryan Schumaker 
50547c716cbSTrond Myklebust 	if (status == -EAGAIN) {
5060aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
507e47c085aSTrond Myklebust 		desc->current_index += array->size;
50847c716cbSTrond Myklebust 		desc->page_index++;
50947c716cbSTrond Myklebust 	}
510ed09222dSTrond Myklebust 	kunmap_atomic(array);
511d1bacf9eSBryan Schumaker 	return status;
512d1bacf9eSBryan Schumaker }
513d1bacf9eSBryan Schumaker 
514d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
51593b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
516b593c09fSTrond Myklebust 				  __be32 *verf, u64 cookie,
517b593c09fSTrond Myklebust 				  struct page **pages, size_t bufsize,
518b593c09fSTrond Myklebust 				  __be32 *verf_res)
519d1bacf9eSBryan Schumaker {
52082e22a5eSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
52182e22a5eSTrond Myklebust 	struct nfs_readdir_arg arg = {
52282e22a5eSTrond Myklebust 		.dentry = file_dentry(desc->file),
52382e22a5eSTrond Myklebust 		.cred = desc->file->f_cred,
524b593c09fSTrond Myklebust 		.verf = verf,
52582e22a5eSTrond Myklebust 		.cookie = cookie,
52682e22a5eSTrond Myklebust 		.pages = pages,
52782e22a5eSTrond Myklebust 		.page_len = bufsize,
52882e22a5eSTrond Myklebust 		.plus = desc->plus,
52982e22a5eSTrond Myklebust 	};
53082e22a5eSTrond Myklebust 	struct nfs_readdir_res res = {
53182e22a5eSTrond Myklebust 		.verf = verf_res,
53282e22a5eSTrond Myklebust 	};
5334704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
5341da177e4SLinus Torvalds 	int		error;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds  again:
5371da177e4SLinus Torvalds 	timestamp = jiffies;
5384704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
539a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
54082e22a5eSTrond Myklebust 	error = NFS_PROTO(inode)->readdir(&arg, &res);
5411da177e4SLinus Torvalds 	if (error < 0) {
5421da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
5431da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
5441da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
5453a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
54682e22a5eSTrond Myklebust 			desc->plus = arg.plus = false;
5471da177e4SLinus Torvalds 			goto again;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		goto error;
5501da177e4SLinus Torvalds 	}
5511f4eab7eSNeil Brown 	desc->timestamp = timestamp;
5524704f0e2STrond Myklebust 	desc->gencount = gencount;
553d1bacf9eSBryan Schumaker error:
554d1bacf9eSBryan Schumaker 	return error;
555d1bacf9eSBryan Schumaker }
556d1bacf9eSBryan Schumaker 
5576c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
558573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
559d1bacf9eSBryan Schumaker {
56059e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
561573c4e1eSChuck Lever 	int error;
562d1bacf9eSBryan Schumaker 
56359e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
564573c4e1eSChuck Lever 	if (error)
565573c4e1eSChuck Lever 		return error;
566d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
567d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
568d1bacf9eSBryan Schumaker 	return 0;
569d1bacf9eSBryan Schumaker }
570d1bacf9eSBryan Schumaker 
571fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
572fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
573fa923369STrond Myklebust  */
574d39ab9deSBryan Schumaker static
575d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
576d39ab9deSBryan Schumaker {
577d8fdb47fSTrond Myklebust 	struct inode *inode;
578fa923369STrond Myklebust 	struct nfs_inode *nfsi;
579fa923369STrond Myklebust 
5802b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
5812b0143b5SDavid Howells 		return 0;
582fa923369STrond Myklebust 
583d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
584d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
585d8fdb47fSTrond Myklebust 		return 0;
586d8fdb47fSTrond Myklebust 
587d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
5887dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
589d39ab9deSBryan Schumaker 		return 0;
5907dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
5917dc72d5fSTrond Myklebust 		return 0;
5927dc72d5fSTrond Myklebust 	return 1;
593d39ab9deSBryan Schumaker }
594d39ab9deSBryan Schumaker 
595d39ab9deSBryan Schumaker static
59623db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
597d69ee9b8STrond Myklebust {
598d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
599d69ee9b8STrond Myklebust 		return false;
600d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
601d69ee9b8STrond Myklebust 		return true;
60223db8620SAl Viro 	if (ctx->pos == 0)
603d69ee9b8STrond Myklebust 		return true;
604d69ee9b8STrond Myklebust 	return false;
605d69ee9b8STrond Myklebust }
606d69ee9b8STrond Myklebust 
607d69ee9b8STrond Myklebust /*
60863519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
60963519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
610d69ee9b8STrond Myklebust  * directory.
611d69ee9b8STrond Myklebust  */
612d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
613d69ee9b8STrond Myklebust {
61463519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
61563519fbcSTrond Myklebust 
61663519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
61763519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
61863519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
619d69ee9b8STrond Myklebust }
620d69ee9b8STrond Myklebust 
621311324adSTrond Myklebust /*
622311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
623311324adSTrond Myklebust  *
624311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
625311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
626311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
627311324adSTrond Myklebust  * cache flush.
628311324adSTrond Myklebust  */
629311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
630311324adSTrond Myklebust {
63163519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
63263519fbcSTrond Myklebust 
63363519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
63463519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
63563519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
636ff81dfb5STrond Myklebust 		set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
637311324adSTrond Myklebust 	}
638311324adSTrond Myklebust }
639311324adSTrond Myklebust 
640d69ee9b8STrond Myklebust static
641a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
642a1147b82STrond Myklebust 		unsigned long dir_verifier)
643d39ab9deSBryan Schumaker {
64426fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
6459ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
6464a201d6eSTrond Myklebust 	struct dentry *dentry;
6474a201d6eSTrond Myklebust 	struct dentry *alias;
648d39ab9deSBryan Schumaker 	struct inode *inode;
649aa9c2669SDavid Quigley 	int status;
650d39ab9deSBryan Schumaker 
651fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
652fa923369STrond Myklebust 		return;
6536c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
6546c441c25STrond Myklebust 		return;
65578d04af4STrond Myklebust 	if (filename.len == 0)
65678d04af4STrond Myklebust 		return;
65778d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
65878d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
65978d04af4STrond Myklebust 		return;
66078d04af4STrond Myklebust 	/* ...or '/' */
66178d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
66278d04af4STrond Myklebust 		return;
6634a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
6644a201d6eSTrond Myklebust 		if (filename.len == 1)
6654a201d6eSTrond Myklebust 			return;
6664a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
6674a201d6eSTrond Myklebust 			return;
6684a201d6eSTrond Myklebust 	}
6698387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
670d39ab9deSBryan Schumaker 
6714a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
6729ac3d3e8SAl Viro again:
6739ac3d3e8SAl Viro 	if (!dentry) {
6749ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
6759ac3d3e8SAl Viro 		if (IS_ERR(dentry))
6769ac3d3e8SAl Viro 			return;
6779ac3d3e8SAl Viro 	}
6789ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
6796c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
6806c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
6816c441c25STrond Myklebust 					&entry->fattr->fsid))
6826c441c25STrond Myklebust 			goto out;
683d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
6847dc72d5fSTrond Myklebust 			if (!entry->fh->size)
6857dc72d5fSTrond Myklebust 				goto out;
686a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
6872b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
688aa9c2669SDavid Quigley 			if (!status)
689dd225cb3SAnna Schumaker 				nfs_setsecurity(d_inode(dentry), entry->fattr);
690d39ab9deSBryan Schumaker 			goto out;
691d39ab9deSBryan Schumaker 		} else {
6925542aa2fSEric W. Biederman 			d_invalidate(dentry);
693d39ab9deSBryan Schumaker 			dput(dentry);
6949ac3d3e8SAl Viro 			dentry = NULL;
6959ac3d3e8SAl Viro 			goto again;
696d39ab9deSBryan Schumaker 		}
697d39ab9deSBryan Schumaker 	}
6987dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
6997dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
7007dc72d5fSTrond Myklebust 		goto out;
7017dc72d5fSTrond Myklebust 	}
702d39ab9deSBryan Schumaker 
703cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
70441d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
7059ac3d3e8SAl Viro 	d_lookup_done(dentry);
7069ac3d3e8SAl Viro 	if (alias) {
707d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
708d39ab9deSBryan Schumaker 			goto out;
7099ac3d3e8SAl Viro 		dput(dentry);
7109ac3d3e8SAl Viro 		dentry = alias;
7119ac3d3e8SAl Viro 	}
712a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
713d39ab9deSBryan Schumaker out:
714d39ab9deSBryan Schumaker 	dput(dentry);
715d39ab9deSBryan Schumaker }
716d39ab9deSBryan Schumaker 
717d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
7183b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
7193b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
7203b2a09f1STrond Myklebust 				   struct page **xdr_pages,
72135df59d3STrond Myklebust 				   unsigned int buflen,
72235df59d3STrond Myklebust 				   struct page **arrays,
72335df59d3STrond Myklebust 				   size_t narrays)
724d1bacf9eSBryan Schumaker {
7253b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
726babddc72SBryan Schumaker 	struct xdr_stream stream;
727f7da7a12SBenny Halevy 	struct xdr_buf buf;
72835df59d3STrond Myklebust 	struct page *scratch, *new, *page = *arrays;
7295c346854STrond Myklebust 	int status;
730babddc72SBryan Schumaker 
7316650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
7326650239aSTrond Myklebust 	if (scratch == NULL)
7336650239aSTrond Myklebust 		return -ENOMEM;
734babddc72SBryan Schumaker 
735f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
7360ae4c3e8SChuck Lever 	xdr_set_scratch_page(&stream, scratch);
73799424380SBryan Schumaker 
73899424380SBryan Schumaker 	do {
739b1db9a40SAnna Schumaker 		if (entry->fattr->label)
740b1db9a40SAnna Schumaker 			entry->fattr->label->len = NFS4_MAXLABELLEN;
741d33030e2SJeffrey Mitchell 
74299424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
743972bcdf2STrond Myklebust 		if (status != 0)
74499424380SBryan Schumaker 			break;
7455c346854STrond Myklebust 
746a7a3b1e9SBenjamin Coddington 		if (desc->plus)
747a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
748a1147b82STrond Myklebust 					desc->dir_verifier);
7498cd51a0cSTrond Myklebust 
750db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
7513b2a09f1STrond Myklebust 		if (status != -ENOSPC)
7523b2a09f1STrond Myklebust 			continue;
75399424380SBryan Schumaker 
75435df59d3STrond Myklebust 		if (page->mapping != mapping) {
75535df59d3STrond Myklebust 			if (!--narrays)
7563b2a09f1STrond Myklebust 				break;
75735df59d3STrond Myklebust 			new = nfs_readdir_page_array_alloc(entry->prev_cookie,
75835df59d3STrond Myklebust 							   GFP_KERNEL);
75935df59d3STrond Myklebust 			if (!new)
76035df59d3STrond Myklebust 				break;
76135df59d3STrond Myklebust 			arrays++;
76235df59d3STrond Myklebust 			*arrays = page = new;
76335df59d3STrond Myklebust 		} else {
76435df59d3STrond Myklebust 			new = nfs_readdir_page_get_next(mapping,
76535df59d3STrond Myklebust 							page->index + 1,
7663b2a09f1STrond Myklebust 							entry->prev_cookie);
7673b2a09f1STrond Myklebust 			if (!new)
7683b2a09f1STrond Myklebust 				break;
76935df59d3STrond Myklebust 			if (page != *arrays)
7703b2a09f1STrond Myklebust 				nfs_readdir_page_unlock_and_put(page);
7713b2a09f1STrond Myklebust 			page = new;
77256e4ebf8SBryan Schumaker 		}
7733b2a09f1STrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
774972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
77556e4ebf8SBryan Schumaker 
776972bcdf2STrond Myklebust 	switch (status) {
777972bcdf2STrond Myklebust 	case -EBADCOOKIE:
778972bcdf2STrond Myklebust 		if (entry->eof) {
779b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
7808cd51a0cSTrond Myklebust 			status = 0;
78156e4ebf8SBryan Schumaker 		}
782972bcdf2STrond Myklebust 		break;
783972bcdf2STrond Myklebust 	case -ENOSPC:
784972bcdf2STrond Myklebust 	case -EAGAIN:
785972bcdf2STrond Myklebust 		status = 0;
786972bcdf2STrond Myklebust 		break;
787972bcdf2STrond Myklebust 	}
7886650239aSTrond Myklebust 
78935df59d3STrond Myklebust 	if (page != *arrays)
7903b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
7916650239aSTrond Myklebust 
7926650239aSTrond Myklebust 	put_page(scratch);
793d1bacf9eSBryan Schumaker 	return status;
79456e4ebf8SBryan Schumaker }
79556e4ebf8SBryan Schumaker 
7961a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
79756e4ebf8SBryan Schumaker {
7981a34c8c9STrond Myklebust 	while (npages--)
7991a34c8c9STrond Myklebust 		put_page(pages[npages]);
8001a34c8c9STrond Myklebust 	kfree(pages);
80156e4ebf8SBryan Schumaker }
80256e4ebf8SBryan Schumaker 
80356e4ebf8SBryan Schumaker /*
804bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
805bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
80656e4ebf8SBryan Schumaker  */
8071a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
80856e4ebf8SBryan Schumaker {
8091a34c8c9STrond Myklebust 	struct page **pages;
8101a34c8c9STrond Myklebust 	size_t i;
81156e4ebf8SBryan Schumaker 
8121a34c8c9STrond Myklebust 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
8131a34c8c9STrond Myklebust 	if (!pages)
8141a34c8c9STrond Myklebust 		return NULL;
81556e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
81656e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
81756e4ebf8SBryan Schumaker 		if (page == NULL)
81856e4ebf8SBryan Schumaker 			goto out_freepages;
81956e4ebf8SBryan Schumaker 		pages[i] = page;
82056e4ebf8SBryan Schumaker 	}
8211a34c8c9STrond Myklebust 	return pages;
82256e4ebf8SBryan Schumaker 
82356e4ebf8SBryan Schumaker out_freepages:
824c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
8251a34c8c9STrond Myklebust 	return NULL;
826d1bacf9eSBryan Schumaker }
827d1bacf9eSBryan Schumaker 
8286c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
82935df59d3STrond Myklebust 				    __be32 *verf_arg, __be32 *verf_res,
83035df59d3STrond Myklebust 				    struct page **arrays, size_t narrays)
831d1bacf9eSBryan Schumaker {
8321a34c8c9STrond Myklebust 	struct page **pages;
83335df59d3STrond Myklebust 	struct page *page = *arrays;
8346b75cf9eSTrond Myklebust 	struct nfs_entry *entry;
8351a34c8c9STrond Myklebust 	size_t array_size;
836b593c09fSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
8371a34c8c9STrond Myklebust 	size_t dtsize = NFS_SERVER(inode)->dtsize;
8388cd51a0cSTrond Myklebust 	int status = -ENOMEM;
839d1bacf9eSBryan Schumaker 
8406b75cf9eSTrond Myklebust 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
8416b75cf9eSTrond Myklebust 	if (!entry)
8426b75cf9eSTrond Myklebust 		return -ENOMEM;
8436b75cf9eSTrond Myklebust 	entry->cookie = nfs_readdir_page_last_cookie(page);
8446b75cf9eSTrond Myklebust 	entry->fh = nfs_alloc_fhandle();
845b1db9a40SAnna Schumaker 	entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
8466b75cf9eSTrond Myklebust 	entry->server = NFS_SERVER(inode);
8476b75cf9eSTrond Myklebust 	if (entry->fh == NULL || entry->fattr == NULL)
848d1bacf9eSBryan Schumaker 		goto out;
849d1bacf9eSBryan Schumaker 
8501a34c8c9STrond Myklebust 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
8511a34c8c9STrond Myklebust 	pages = nfs_readdir_alloc_pages(array_size);
8521a34c8c9STrond Myklebust 	if (!pages)
853b1db9a40SAnna Schumaker 		goto out;
854d1bacf9eSBryan Schumaker 
855d1bacf9eSBryan Schumaker 	do {
856ac396128STrond Myklebust 		unsigned int pglen;
857b593c09fSTrond Myklebust 		status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie,
858b593c09fSTrond Myklebust 						pages, dtsize,
859b593c09fSTrond Myklebust 						verf_res);
860d1bacf9eSBryan Schumaker 		if (status < 0)
861d1bacf9eSBryan Schumaker 			break;
862972bcdf2STrond Myklebust 
863ac396128STrond Myklebust 		pglen = status;
864972bcdf2STrond Myklebust 		if (pglen == 0) {
865972bcdf2STrond Myklebust 			nfs_readdir_page_set_eof(page);
8668cd51a0cSTrond Myklebust 			break;
8678cd51a0cSTrond Myklebust 		}
868972bcdf2STrond Myklebust 
869ee3707aeSNagendra S Tomar 		verf_arg = verf_res;
870ee3707aeSNagendra S Tomar 
87135df59d3STrond Myklebust 		status = nfs_readdir_page_filler(desc, entry, pages, pglen,
87235df59d3STrond Myklebust 						 arrays, narrays);
873d9c4e39cStrondmy@kernel.org 	} while (!status && nfs_readdir_page_needs_filling(page) &&
874d9c4e39cStrondmy@kernel.org 		page_mapping(page));
875d1bacf9eSBryan Schumaker 
876c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
877d1bacf9eSBryan Schumaker out:
8786b75cf9eSTrond Myklebust 	nfs_free_fattr(entry->fattr);
8796b75cf9eSTrond Myklebust 	nfs_free_fhandle(entry->fh);
8806b75cf9eSTrond Myklebust 	kfree(entry);
881d1bacf9eSBryan Schumaker 	return status;
882d1bacf9eSBryan Schumaker }
883d1bacf9eSBryan Schumaker 
8841f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
8851da177e4SLinus Torvalds {
88609cbfeafSKirill A. Shutemov 	put_page(desc->page);
8871da177e4SLinus Torvalds 	desc->page = NULL;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901f1d4aa4STrond Myklebust static void
8911f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
8921da177e4SLinus Torvalds {
8931f1d4aa4STrond Myklebust 	unlock_page(desc->page);
8941f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
8951f1d4aa4STrond Myklebust }
8961f1d4aa4STrond Myklebust 
8971f1d4aa4STrond Myklebust static struct page *
8981f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
8991f1d4aa4STrond Myklebust {
9001f1d4aa4STrond Myklebust 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
9011f1d4aa4STrond Myklebust 					   desc->page_index,
9021f1d4aa4STrond Myklebust 					   desc->last_cookie);
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds /*
906d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
907114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
9081da177e4SLinus Torvalds  */
9096c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
910d1bacf9eSBryan Schumaker {
911227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
912227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
913b593c09fSTrond Myklebust 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
914d1bacf9eSBryan Schumaker 	int res;
915d1bacf9eSBryan Schumaker 
9161f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
9171f1d4aa4STrond Myklebust 	if (!desc->page)
9181f1d4aa4STrond Myklebust 		return -ENOMEM;
9191f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
92035df59d3STrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
92135df59d3STrond Myklebust 					       &desc->page, 1);
9229fff59edSTrond Myklebust 		if (res < 0) {
9239fff59edSTrond Myklebust 			nfs_readdir_page_unlock_and_put_cached(desc);
9249fff59edSTrond Myklebust 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
9259fff59edSTrond Myklebust 				invalidate_inode_pages2(desc->file->f_mapping);
9269fff59edSTrond Myklebust 				desc->page_index = 0;
9279fff59edSTrond Myklebust 				return -EAGAIN;
9289fff59edSTrond Myklebust 			}
9299fff59edSTrond Myklebust 			return res;
9309fff59edSTrond Myklebust 		}
931f892c41cSTrond Myklebust 		/*
932f892c41cSTrond Myklebust 		 * Set the cookie verifier if the page cache was empty
933f892c41cSTrond Myklebust 		 */
934f892c41cSTrond Myklebust 		if (desc->page_index == 0)
935f892c41cSTrond Myklebust 			memcpy(nfsi->cookieverf, verf,
936f892c41cSTrond Myklebust 			       sizeof(nfsi->cookieverf));
9371f1d4aa4STrond Myklebust 	}
938114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
939ff81dfb5STrond Myklebust 	if (res == 0)
940114de382STrond Myklebust 		return 0;
9411f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
942d1bacf9eSBryan Schumaker 	return res;
943d1bacf9eSBryan Schumaker }
944d1bacf9eSBryan Schumaker 
945794092c5STrond Myklebust static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc)
946794092c5STrond Myklebust {
947794092c5STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
948794092c5STrond Myklebust 	struct inode *dir = file_inode(desc->file);
949794092c5STrond Myklebust 	unsigned int dtsize = NFS_SERVER(dir)->dtsize;
950794092c5STrond Myklebust 	loff_t size = i_size_read(dir);
951794092c5STrond Myklebust 
952794092c5STrond Myklebust 	/*
953794092c5STrond Myklebust 	 * Default to uncached readdir if the page cache is empty, and
954794092c5STrond Myklebust 	 * we're looking for a non-zero cookie in a large directory.
955794092c5STrond Myklebust 	 */
956794092c5STrond Myklebust 	return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize;
957794092c5STrond Myklebust }
958794092c5STrond Myklebust 
959d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
9606c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
9611da177e4SLinus Torvalds {
9628cd51a0cSTrond Myklebust 	int res;
963d1bacf9eSBryan Schumaker 
964794092c5STrond Myklebust 	if (nfs_readdir_dont_search_cache(desc))
965794092c5STrond Myklebust 		return -EBADCOOKIE;
966794092c5STrond Myklebust 
9679fff59edSTrond Myklebust 	do {
9680aded708STrond Myklebust 		if (desc->page_index == 0) {
9698cd51a0cSTrond Myklebust 			desc->current_index = 0;
97059e356a9STrond Myklebust 			desc->prev_index = 0;
9710aded708STrond Myklebust 			desc->last_cookie = 0;
9720aded708STrond Myklebust 		}
973114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
97447c716cbSTrond Myklebust 	} while (res == -EAGAIN);
9751da177e4SLinus Torvalds 	return res;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds /*
9791da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
9801da177e4SLinus Torvalds  */
98113884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
98213884ff2STrond Myklebust 			   const __be32 *verf)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	struct file	*file = desc->file;
985dbeaf8c9STrond Myklebust 	struct nfs_cache_array *array;
986dbeaf8c9STrond Myklebust 	unsigned int i = 0;
9878ef2ce3eSBryan Schumaker 
9880795bf83SFabian Frederick 	array = kmap(desc->page);
989d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
990ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
9911da177e4SLinus Torvalds 
992ece0b423STrond Myklebust 		ent = &array->array[i];
993a52a8a6aSTrond Myklebust 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
99423db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
995e1d2699bSTrond Myklebust 			desc->eob = true;
9961da177e4SLinus Torvalds 			break;
997ece0b423STrond Myklebust 		}
99813884ff2STrond Myklebust 		memcpy(desc->verf, verf, sizeof(desc->verf));
999d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
10002e7a4641STrond Myklebust 			desc->dir_cookie = array->array[i+1].cookie;
1001d1bacf9eSBryan Schumaker 		else
10022e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
100359e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
10042e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
100559e356a9STrond Myklebust 		else
100659e356a9STrond Myklebust 			desc->ctx->pos++;
10072e7a4641STrond Myklebust 		if (desc->duped != 0)
10082e7a4641STrond Myklebust 			desc->duped = 1;
10098cd51a0cSTrond Myklebust 	}
1010b1e21c97STrond Myklebust 	if (array->page_is_eof)
1011e1d2699bSTrond Myklebust 		desc->eof = !desc->eob;
1012d1bacf9eSBryan Schumaker 
10130795bf83SFabian Frederick 	kunmap(desc->page);
1014dbeaf8c9STrond Myklebust 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1015dbeaf8c9STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds /*
10191da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
10201da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
10211da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
10221da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
10231da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
10241da177e4SLinus Torvalds  *
10251da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
10261da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
10271da177e4SLinus Torvalds  *	 we should already have a complete representation of the
10281da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
10291da177e4SLinus Torvalds  */
10306c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
10311da177e4SLinus Torvalds {
103235df59d3STrond Myklebust 	struct page	**arrays;
103335df59d3STrond Myklebust 	size_t		i, sz = 512;
1034b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
103535df59d3STrond Myklebust 	int		status = -ENOMEM;
10361da177e4SLinus Torvalds 
103735df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
10382e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10391da177e4SLinus Torvalds 
104035df59d3STrond Myklebust 	arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
104135df59d3STrond Myklebust 	if (!arrays)
10421da177e4SLinus Torvalds 		goto out;
104335df59d3STrond Myklebust 	arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
104435df59d3STrond Myklebust 	if (!arrays[0])
104535df59d3STrond Myklebust 		goto out;
10461da177e4SLinus Torvalds 
10477a8e1dc3STrond Myklebust 	desc->page_index = 0;
1048ce292d8fStrondmy@kernel.org 	desc->cache_entry_index = 0;
10492e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
10502e7a4641STrond Myklebust 	desc->duped = 0;
10517a8e1dc3STrond Myklebust 
105235df59d3STrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1053d1bacf9eSBryan Schumaker 
1054e1d2699bSTrond Myklebust 	for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
105535df59d3STrond Myklebust 		desc->page = arrays[i];
105613884ff2STrond Myklebust 		nfs_do_filldir(desc, verf);
105735df59d3STrond Myklebust 	}
105835df59d3STrond Myklebust 	desc->page = NULL;
10591da177e4SLinus Torvalds 
106035df59d3STrond Myklebust 
106135df59d3STrond Myklebust 	for (i = 0; i < sz && arrays[i]; i++)
106235df59d3STrond Myklebust 		nfs_readdir_page_array_free(arrays[i]);
10631da177e4SLinus Torvalds out:
106435df59d3STrond Myklebust 	kfree(arrays);
106535df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
10661da177e4SLinus Torvalds 	return status;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
106900a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
107000a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
107100a92642SOlivier Galibert    whole directory.
10721da177e4SLinus Torvalds  */
107323db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
10741da177e4SLinus Torvalds {
1075be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
10762b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
107713884ff2STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
107823db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
10796b75cf9eSTrond Myklebust 	struct nfs_readdir_descriptor *desc;
1080ff81dfb5STrond Myklebust 	pgoff_t page_index;
10816b75cf9eSTrond Myklebust 	int res;
10821da177e4SLinus Torvalds 
10836de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
10846de1472fSAl Viro 			file, (long long)ctx->pos);
108591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
108691d5b470SChuck Lever 
10871da177e4SLinus Torvalds 	/*
108823db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
1089f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
109000a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
109100a92642SOlivier Galibert 	 * revalidate the cookie.
10921da177e4SLinus Torvalds 	 */
10936b75cf9eSTrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) {
109423db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
1095fccca7fcSTrond Myklebust 		if (res < 0)
1096fccca7fcSTrond Myklebust 			goto out;
10976b75cf9eSTrond Myklebust 	}
10986b75cf9eSTrond Myklebust 
10996b75cf9eSTrond Myklebust 	res = -ENOMEM;
11006b75cf9eSTrond Myklebust 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
11016b75cf9eSTrond Myklebust 	if (!desc)
11026b75cf9eSTrond Myklebust 		goto out;
11036b75cf9eSTrond Myklebust 	desc->file = file;
11046b75cf9eSTrond Myklebust 	desc->ctx = ctx;
11056b75cf9eSTrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, ctx);
1106fccca7fcSTrond Myklebust 
11072e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
11082e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
11092e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
11102e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
1111ff81dfb5STrond Myklebust 	page_index = dir_ctx->page_index;
11122e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
1113e1d2699bSTrond Myklebust 	desc->eof = dir_ctx->eof;
1114b593c09fSTrond Myklebust 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
11152e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1116565277f6STrond Myklebust 
1117e1d2699bSTrond Myklebust 	if (desc->eof) {
1118e1d2699bSTrond Myklebust 		res = 0;
1119e1d2699bSTrond Myklebust 		goto out_free;
1120e1d2699bSTrond Myklebust 	}
1121e1d2699bSTrond Myklebust 
1122ff81dfb5STrond Myklebust 	if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) &&
1123ff81dfb5STrond Myklebust 	    list_is_singular(&nfsi->open_files))
1124ff81dfb5STrond Myklebust 		invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1);
1125ff81dfb5STrond Myklebust 
112647c716cbSTrond Myklebust 	do {
11271da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
112800a92642SOlivier Galibert 
11291da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1130ece0b423STrond Myklebust 			res = 0;
11311da177e4SLinus Torvalds 			/* This means either end of directory */
11322e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
11331da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
113423db8620SAl Viro 				res = uncached_readdir(desc);
1135ece0b423STrond Myklebust 				if (res == 0)
11361da177e4SLinus Torvalds 					continue;
11379fff59edSTrond Myklebust 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
11389fff59edSTrond Myklebust 					res = 0;
11391da177e4SLinus Torvalds 			}
11401da177e4SLinus Torvalds 			break;
11411da177e4SLinus Torvalds 		}
11421da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
114313884ff2STrond Myklebust 			clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
11441da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1145baf57a09STrond Myklebust 			desc->page_index = 0;
1146a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1147a7a3b1e9SBenjamin Coddington 			desc->eof = false;
11481da177e4SLinus Torvalds 			continue;
11491da177e4SLinus Torvalds 		}
11501da177e4SLinus Torvalds 		if (res < 0)
11511da177e4SLinus Torvalds 			break;
11521da177e4SLinus Torvalds 
115313884ff2STrond Myklebust 		nfs_do_filldir(desc, nfsi->cookieverf);
11541f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
1155e1d2699bSTrond Myklebust 	} while (!desc->eob && !desc->eof);
11562e7a4641STrond Myklebust 
11572e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
11582e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
11592e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
11602e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
11612e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
1162ff81dfb5STrond Myklebust 	dir_ctx->page_index = desc->page_index;
1163e1d2699bSTrond Myklebust 	dir_ctx->eof = desc->eof;
1164b593c09fSTrond Myklebust 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
11652e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1166e1d2699bSTrond Myklebust out_free:
11676b75cf9eSTrond Myklebust 	kfree(desc);
11686b75cf9eSTrond Myklebust 
1169fccca7fcSTrond Myklebust out:
11706de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
11711da177e4SLinus Torvalds 	return res;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
1174965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1175f0dd2136STrond Myklebust {
1176480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1177b84e06c5SChuck Lever 
11786de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
11796de1472fSAl Viro 			filp, offset, whence);
1180b84e06c5SChuck Lever 
1181965c8e59SAndrew Morton 	switch (whence) {
1182f0dd2136STrond Myklebust 	default:
1183b2b1ff3dSTrond Myklebust 		return -EINVAL;
1184b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1185b2b1ff3dSTrond Myklebust 		if (offset < 0)
1186b2b1ff3dSTrond Myklebust 			return -EINVAL;
118783f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1188b2b1ff3dSTrond Myklebust 		break;
1189b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1190b2b1ff3dSTrond Myklebust 		if (offset == 0)
1191b2b1ff3dSTrond Myklebust 			return filp->f_pos;
119283f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1193b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1194b2b1ff3dSTrond Myklebust 		if (offset < 0) {
119583f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1196b2b1ff3dSTrond Myklebust 			return -EINVAL;
1197b2b1ff3dSTrond Myklebust 		}
1198f0dd2136STrond Myklebust 	}
1199f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1200f0dd2136STrond Myklebust 		filp->f_pos = offset;
120159e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(filp))
120259e356a9STrond Myklebust 			dir_ctx->dir_cookie = offset;
120359e356a9STrond Myklebust 		else
1204480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
1205b593c09fSTrond Myklebust 		if (offset == 0)
1206b593c09fSTrond Myklebust 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
12078ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1208e1d2699bSTrond Myklebust 		dir_ctx->eof = false;
1209f0dd2136STrond Myklebust 	}
121083f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1211f0dd2136STrond Myklebust 	return offset;
1212f0dd2136STrond Myklebust }
1213f0dd2136STrond Myklebust 
12141da177e4SLinus Torvalds /*
12151da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
12161da177e4SLinus Torvalds  * is a dummy operation.
12171da177e4SLinus Torvalds  */
121802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
121902c24a82SJosef Bacik 			 int datasync)
12201da177e4SLinus Torvalds {
12216de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
12221e7cb3dcSChuck Lever 
122311decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
12241da177e4SLinus Torvalds 	return 0;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
1227bfc69a45STrond Myklebust /**
1228bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1229302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1230bfc69a45STrond Myklebust  *
1231bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1232bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1233bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1234bfc69a45STrond Myklebust  *
1235efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1236efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1237efeda80dSTrond Myklebust  *
1238bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1239bfc69a45STrond Myklebust  */
1240bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1241bfc69a45STrond Myklebust {
1242efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1243bfc69a45STrond Myklebust }
124489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1245bfc69a45STrond Myklebust 
1246efeda80dSTrond Myklebust /**
1247efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1248efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1249efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1250efeda80dSTrond Myklebust  *
1251efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1252efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1253efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1254efeda80dSTrond Myklebust  */
1255efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1256efeda80dSTrond Myklebust {
1257efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1258efeda80dSTrond Myklebust }
1259efeda80dSTrond Myklebust 
1260efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1261efeda80dSTrond Myklebust {
1262efeda80dSTrond Myklebust 	*verf |= 1UL;
1263efeda80dSTrond Myklebust }
1264efeda80dSTrond Myklebust 
1265efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1266efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1267efeda80dSTrond Myklebust {
1268efeda80dSTrond Myklebust 	*verf &= ~1UL;
1269efeda80dSTrond Myklebust }
1270efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1271efeda80dSTrond Myklebust 
1272efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1273efeda80dSTrond Myklebust {
1274efeda80dSTrond Myklebust 	return verf & 1;
1275efeda80dSTrond Myklebust }
1276efeda80dSTrond Myklebust 
1277efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1278efeda80dSTrond Myklebust {
1279efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1280efeda80dSTrond Myklebust }
1281efeda80dSTrond Myklebust 
1282efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1283efeda80dSTrond Myklebust {
1284efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1285cec08f45STrond Myklebust 	struct inode *dir = d_inode(dentry->d_parent);
1286efeda80dSTrond Myklebust 
1287cec08f45STrond Myklebust 	if (!nfs_verify_change_attribute(dir, verf))
1288cec08f45STrond Myklebust 		return;
1289efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1290efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1291efeda80dSTrond Myklebust 	dentry->d_time = verf;
1292efeda80dSTrond Myklebust }
1293efeda80dSTrond Myklebust 
1294efeda80dSTrond Myklebust /**
1295efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1296efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1297efeda80dSTrond Myklebust  * @verf: verifier to save
1298efeda80dSTrond Myklebust  *
1299efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1300efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1301efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1302efeda80dSTrond Myklebust  * look it up again after a directory change.
1303efeda80dSTrond Myklebust  */
1304efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1305efeda80dSTrond Myklebust {
1306efeda80dSTrond Myklebust 
1307efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1308efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1309efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1310efeda80dSTrond Myklebust }
1311efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1312efeda80dSTrond Myklebust 
1313efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1314efeda80dSTrond Myklebust /**
1315efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1316efeda80dSTrond Myklebust  * @inode: pointer to inode
1317efeda80dSTrond Myklebust  *
1318efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1319efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1320efeda80dSTrond Myklebust  * while holding a delegation.
1321efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1322efeda80dSTrond Myklebust  * returned or revoked.
1323efeda80dSTrond Myklebust  */
1324efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1325efeda80dSTrond Myklebust {
1326efeda80dSTrond Myklebust 	struct dentry *alias;
1327efeda80dSTrond Myklebust 
1328efeda80dSTrond Myklebust 	if (!inode)
1329efeda80dSTrond Myklebust 		return;
1330efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1331efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1332efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1333efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1334efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1335efeda80dSTrond Myklebust 	}
1336efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1337efeda80dSTrond Myklebust }
1338efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1339efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1340efeda80dSTrond Myklebust 
13418ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
13428ce37abdSTrond Myklebust {
13438ce37abdSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
13448ce37abdSTrond Myklebust 	    d_really_is_negative(dentry))
13458ce37abdSTrond Myklebust 		return dentry->d_time == inode_peek_iversion_raw(dir);
13468ce37abdSTrond Myklebust 	return nfs_verify_change_attribute(dir, dentry->d_time);
13478ce37abdSTrond Myklebust }
13488ce37abdSTrond Myklebust 
13491da177e4SLinus Torvalds /*
13501da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
13511da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
13521da177e4SLinus Torvalds  * and may need to be looked up again.
1353912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
13541da177e4SLinus Torvalds  */
1355912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1356912a108dSNeilBrown 			      int rcu_walk)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
13591da177e4SLinus Torvalds 		return 1;
13604eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
13614eec952eSTrond Myklebust 		return 0;
13628ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
13636ecc5e8fSTrond Myklebust 		return 0;
1364f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
13651cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1366912a108dSNeilBrown 		if (rcu_walk)
1367f2c77f4eSTrond Myklebust 			return 0;
13681cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
13691cd9cb05STrond Myklebust 			return 0;
13701cd9cb05STrond Myklebust 	}
13718ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
1372f2c77f4eSTrond Myklebust 		return 0;
1373f2c77f4eSTrond Myklebust 	return 1;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds /*
1377a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1378a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1379a12802caSTrond Myklebust  */
1380fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1381a12802caSTrond Myklebust {
1382a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1383a12802caSTrond Myklebust 		return 0;
1384fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1385a12802caSTrond Myklebust }
1386a12802caSTrond Myklebust 
1387a12802caSTrond Myklebust /*
13881d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
13891d6757fbSTrond Myklebust  *
13901d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
13911d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
13921d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
13931d6757fbSTrond Myklebust  *
13941d6757fbSTrond Myklebust  */
139565a0c149STrond Myklebust static
1396fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
13971da177e4SLinus Torvalds {
13981da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
139965a0c149STrond Myklebust 	int ret;
14001da177e4SLinus Torvalds 
140136d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
14024e99a1ffSTrond Myklebust 		return 0;
140347921921STrond Myklebust 
140447921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
140547921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
140647921921STrond Myklebust 		case S_IFREG:
140747921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
140847921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
140947921921STrond Myklebust 				goto out;
1410df561f66SGustavo A. R. Silva 			fallthrough;
141147921921STrond Myklebust 		case S_IFDIR:
141247921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
141347921921STrond Myklebust 				break;
141447921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
141547921921STrond Myklebust 			goto out_force;
141647921921STrond Myklebust 		}
141747921921STrond Myklebust 	}
141847921921STrond Myklebust 
14191da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1420fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
14211da177e4SLinus Torvalds 		goto out_force;
142265a0c149STrond Myklebust out:
142343245ecaSOlga Kornievskaia 	if (inode->i_nlink > 0 ||
142443245ecaSOlga Kornievskaia 	    (inode->i_nlink == 0 &&
142543245ecaSOlga Kornievskaia 	     test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
142643245ecaSOlga Kornievskaia 		return 0;
142743245ecaSOlga Kornievskaia 	else
142843245ecaSOlga Kornievskaia 		return -ESTALE;
14291da177e4SLinus Torvalds out_force:
14301fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
14311fa1e384SNeilBrown 		return -ECHILD;
143265a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
143365a0c149STrond Myklebust 	if (ret != 0)
143465a0c149STrond Myklebust 		return ret;
143565a0c149STrond Myklebust 	goto out;
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
143882e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode)
143982e7ca13STrond Myklebust {
144082e7ca13STrond Myklebust 	spin_lock(&inode->i_lock);
1441a6a361c4STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
144282e7ca13STrond Myklebust 	spin_unlock(&inode->i_lock);
144382e7ca13STrond Myklebust }
144482e7ca13STrond Myklebust 
14451da177e4SLinus Torvalds /*
14461da177e4SLinus Torvalds  * We judge how long we want to trust negative
14471da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
14481da177e4SLinus Torvalds  *
14491da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
14501da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1451912a108dSNeilBrown  *
1452912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1453912a108dSNeilBrown  * suggesting a reval is needed.
14549f6d44d4STrond Myklebust  *
14559f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
14569f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
14571da177e4SLinus Torvalds  */
14581da177e4SLinus Torvalds static inline
14591da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1460fa3c56bbSAl Viro 		       unsigned int flags)
14611da177e4SLinus Torvalds {
14629f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
14631da177e4SLinus Torvalds 		return 0;
14644eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
14654eec952eSTrond Myklebust 		return 1;
146698ca3ee6STrond Myklebust 	/* Case insensitive server? Revalidate negative dentries */
146798ca3ee6STrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
146898ca3ee6STrond Myklebust 		return 1;
1469912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
14725ceb9d7fSTrond Myklebust static int
14735ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
14745ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
14751da177e4SLinus Torvalds {
14765ceb9d7fSTrond Myklebust 	switch (error) {
14775ceb9d7fSTrond Myklebust 	case 1:
14782eef8a31STrond Myklebust 		break;
14795ceb9d7fSTrond Myklebust 	case 0:
1480a3f432bfSJ. Bruce Fields 		/*
1481a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1482a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1483a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1484a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1485a3f432bfSJ. Bruce Fields 		 */
148647397915STrond Myklebust 		if (inode && IS_ROOT(dentry))
14872eef8a31STrond Myklebust 			error = 1;
14882eef8a31STrond Myklebust 		break;
14895ceb9d7fSTrond Myklebust 	}
14902eef8a31STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1491e1fb4d05STrond Myklebust 	return error;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
14945ceb9d7fSTrond Myklebust static int
14955ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
14965ceb9d7fSTrond Myklebust 			       unsigned int flags)
14975ceb9d7fSTrond Myklebust {
14985ceb9d7fSTrond Myklebust 	int ret = 1;
14995ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
15005ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
15015ceb9d7fSTrond Myklebust 			return -ECHILD;
15025ceb9d7fSTrond Myklebust 		ret = 0;
15035ceb9d7fSTrond Myklebust 	}
15045ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
15055ceb9d7fSTrond Myklebust }
15065ceb9d7fSTrond Myklebust 
15075ceb9d7fSTrond Myklebust static int
15085ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
15095ceb9d7fSTrond Myklebust 				struct inode *inode)
15105ceb9d7fSTrond Myklebust {
15115ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15125ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
15135ceb9d7fSTrond Myklebust }
15145ceb9d7fSTrond Myklebust 
15155ceb9d7fSTrond Myklebust static int
15165ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
15175ceb9d7fSTrond Myklebust 			     struct inode *inode)
15185ceb9d7fSTrond Myklebust {
15195ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
15205ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
1521a1147b82STrond Myklebust 	unsigned long dir_verifier;
15225ceb9d7fSTrond Myklebust 	int ret;
15235ceb9d7fSTrond Myklebust 
15245ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
15255ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
15269558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
15279558a007SAnna Schumaker 	if (fhandle == NULL || fattr == NULL)
15285ceb9d7fSTrond Myklebust 		goto out;
15295ceb9d7fSTrond Myklebust 
1530a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
15319558a007SAnna Schumaker 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
15325ceb9d7fSTrond Myklebust 	if (ret < 0) {
1533f7b37b8bSTrond Myklebust 		switch (ret) {
1534f7b37b8bSTrond Myklebust 		case -ESTALE:
1535f7b37b8bSTrond Myklebust 		case -ENOENT:
15365ceb9d7fSTrond Myklebust 			ret = 0;
1537f7b37b8bSTrond Myklebust 			break;
1538f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1539f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1540f7b37b8bSTrond Myklebust 				ret = 1;
1541f7b37b8bSTrond Myklebust 		}
15425ceb9d7fSTrond Myklebust 		goto out;
15435ceb9d7fSTrond Myklebust 	}
15445ceb9d7fSTrond Myklebust 	ret = 0;
15455ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
15465ceb9d7fSTrond Myklebust 		goto out;
15475ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
15485ceb9d7fSTrond Myklebust 		goto out;
15495ceb9d7fSTrond Myklebust 
1550dd225cb3SAnna Schumaker 	nfs_setsecurity(inode, fattr);
1551a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
15525ceb9d7fSTrond Myklebust 
15535ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
15545ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
15555ceb9d7fSTrond Myklebust 	ret = 1;
15565ceb9d7fSTrond Myklebust out:
15575ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
15585ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
155982e7ca13STrond Myklebust 
156082e7ca13STrond Myklebust 	/*
156182e7ca13STrond Myklebust 	 * If the lookup failed despite the dentry change attribute being
156282e7ca13STrond Myklebust 	 * a match, then we should revalidate the directory cache.
156382e7ca13STrond Myklebust 	 */
15648ce37abdSTrond Myklebust 	if (!ret && nfs_dentry_verify_change(dir, dentry))
156582e7ca13STrond Myklebust 		nfs_mark_dir_for_revalidate(dir);
15665ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
15675ceb9d7fSTrond Myklebust }
15685ceb9d7fSTrond Myklebust 
15695ceb9d7fSTrond Myklebust /*
15705ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
15715ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
15725ceb9d7fSTrond Myklebust  * lookup.
15735ceb9d7fSTrond Myklebust  *
15745ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
15755ceb9d7fSTrond Myklebust  * we have an inode!
15765ceb9d7fSTrond Myklebust  *
15775ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
15785ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
15795ceb9d7fSTrond Myklebust  */
15805ceb9d7fSTrond Myklebust static int
15815ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
15825ceb9d7fSTrond Myklebust 			 unsigned int flags)
15835ceb9d7fSTrond Myklebust {
15845ceb9d7fSTrond Myklebust 	struct inode *inode;
15855ceb9d7fSTrond Myklebust 	int error;
15865ceb9d7fSTrond Myklebust 
15875ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
15885ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
15895ceb9d7fSTrond Myklebust 
15905ceb9d7fSTrond Myklebust 	if (!inode)
15915ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
15925ceb9d7fSTrond Myklebust 
15935ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
15945ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
15955ceb9d7fSTrond Myklebust 				__func__, dentry);
15965ceb9d7fSTrond Myklebust 		goto out_bad;
15975ceb9d7fSTrond Myklebust 	}
15985ceb9d7fSTrond Myklebust 
1599efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
16005ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
16015ceb9d7fSTrond Myklebust 
16025ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
16035ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
16045ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
16055ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
16065ceb9d7fSTrond Myklebust 		if (error) {
16075ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
160882e7ca13STrond Myklebust 				nfs_mark_dir_for_revalidate(dir);
16095ceb9d7fSTrond Myklebust 			goto out_bad;
16105ceb9d7fSTrond Myklebust 		}
16115ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
16125ceb9d7fSTrond Myklebust 		goto out_valid;
16135ceb9d7fSTrond Myklebust 	}
16145ceb9d7fSTrond Myklebust 
16155ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
16165ceb9d7fSTrond Myklebust 		return -ECHILD;
16175ceb9d7fSTrond Myklebust 
16185ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
16195ceb9d7fSTrond Myklebust 		goto out_bad;
16205ceb9d7fSTrond Myklebust 
16215ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
16222eef8a31STrond Myklebust 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
16235ceb9d7fSTrond Myklebust out_valid:
16245ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
16255ceb9d7fSTrond Myklebust out_bad:
16265ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
16275ceb9d7fSTrond Myklebust 		return -ECHILD;
16285ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
16295ceb9d7fSTrond Myklebust }
16305ceb9d7fSTrond Myklebust 
16315ceb9d7fSTrond Myklebust static int
1632c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1633c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
16345ceb9d7fSTrond Myklebust {
16355ceb9d7fSTrond Myklebust 	struct dentry *parent;
16365ceb9d7fSTrond Myklebust 	struct inode *dir;
16375ceb9d7fSTrond Myklebust 	int ret;
16385ceb9d7fSTrond Myklebust 
16395ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
16405ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
16415ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
16425ceb9d7fSTrond Myklebust 		if (!dir)
16435ceb9d7fSTrond Myklebust 			return -ECHILD;
1644c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
16455ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
16465ceb9d7fSTrond Myklebust 			return -ECHILD;
16475ceb9d7fSTrond Myklebust 	} else {
16485ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1649c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
16505ceb9d7fSTrond Myklebust 		dput(parent);
16515ceb9d7fSTrond Myklebust 	}
16525ceb9d7fSTrond Myklebust 	return ret;
16535ceb9d7fSTrond Myklebust }
16545ceb9d7fSTrond Myklebust 
1655c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1656c7944ebbSTrond Myklebust {
1657c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1658c7944ebbSTrond Myklebust }
1659c7944ebbSTrond Myklebust 
16601da177e4SLinus Torvalds /*
16612b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1662ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1663ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1664ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1665ecf3d1f1SJeff Layton  *
1666ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1667ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1668ecf3d1f1SJeff Layton  */
1669ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1670ecf3d1f1SJeff Layton {
16712b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
16729cdd1d3fSTrond Myklebust 	int error = 0;
1673ecf3d1f1SJeff Layton 
1674ecf3d1f1SJeff Layton 	/*
1675ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1676ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1677ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1678ecf3d1f1SJeff Layton 	 */
1679ecf3d1f1SJeff Layton 	if (!inode) {
16806de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
16816de1472fSAl Viro 				__func__, dentry);
1682ecf3d1f1SJeff Layton 		return 1;
1683ecf3d1f1SJeff Layton 	}
1684ecf3d1f1SJeff Layton 
1685ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
16866de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
16876de1472fSAl Viro 				__func__, dentry);
1688ecf3d1f1SJeff Layton 		return 0;
1689ecf3d1f1SJeff Layton 	}
1690ecf3d1f1SJeff Layton 
1691b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1692ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1693ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1694ecf3d1f1SJeff Layton 	return !error;
1695ecf3d1f1SJeff Layton }
1696ecf3d1f1SJeff Layton 
1697ecf3d1f1SJeff Layton /*
16981da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
16991da177e4SLinus Torvalds  */
1700fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
17011da177e4SLinus Torvalds {
17026de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
17036de1472fSAl Viro 		dentry, dentry->d_flags);
17041da177e4SLinus Torvalds 
170577f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
17062b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
170777f11192STrond Myklebust 		return 1;
170877f11192STrond Myklebust 
17091da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17101da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
17111da177e4SLinus Torvalds 		return 1;
17121da177e4SLinus Torvalds 	}
17131751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
17141da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
17151da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
17161da177e4SLinus Torvalds 		return 1;
17171da177e4SLinus Torvalds 	}
17181da177e4SLinus Torvalds 	return 0;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
17231b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
17241b83d707STrond Myklebust {
17251b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
17261f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
172759a707b0STrond Myklebust 	if (inode->i_nlink > 0)
172859a707b0STrond Myklebust 		drop_nlink(inode);
172959a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1730ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(
1731ac46b3d7STrond Myklebust 		inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
17321301e421STrond Myklebust 			       NFS_INO_INVALID_NLINK);
17331b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
17341b83d707STrond Myklebust }
17351b83d707STrond Myklebust 
17361da177e4SLinus Torvalds /*
17371da177e4SLinus Torvalds  * Called when the dentry loses inode.
17381da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
17391da177e4SLinus Torvalds  */
17401da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1743e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
17441f018458STrond Myklebust 		nfs_drop_nlink(inode);
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 	iput(inode);
17471da177e4SLinus Torvalds }
17481da177e4SLinus Torvalds 
1749b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1750b1942c5fSAl Viro {
1751b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1752b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1753b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1754b1942c5fSAl Viro 			WARN_ON(1);
1755b1942c5fSAl Viro 		else
1756b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1757b1942c5fSAl Viro 	}
1758b1942c5fSAl Viro }
1759b1942c5fSAl Viro 
1760f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
17611da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1762ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
17631da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
17641da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
176536d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1766b1942c5fSAl Viro 	.d_release	= nfs_d_release,
17671da177e4SLinus Torvalds };
1768ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
17691da177e4SLinus Torvalds 
1770597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
17711da177e4SLinus Torvalds {
17721da177e4SLinus Torvalds 	struct dentry *res;
17731da177e4SLinus Torvalds 	struct inode *inode = NULL;
1774e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1775e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
1776a1147b82STrond Myklebust 	unsigned long dir_verifier;
17771da177e4SLinus Torvalds 	int error;
17781da177e4SLinus Torvalds 
17796de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
178091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
17811da177e4SLinus Torvalds 
1782130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1783130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
17841da177e4SLinus Torvalds 
1785fd684071STrond Myklebust 	/*
1786fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1787fd684071STrond Myklebust 	 * but don't hash the dentry.
1788fd684071STrond Myklebust 	 */
17899f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1790130f9ab7SAl Viro 		return NULL;
17911da177e4SLinus Torvalds 
1792e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1793e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
17949558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1795e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1796e1fb4d05STrond Myklebust 		goto out;
1797e1fb4d05STrond Myklebust 
1798a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
17996e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
18009558a007SAnna Schumaker 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
18018ce37abdSTrond Myklebust 	if (error == -ENOENT) {
18028ce37abdSTrond Myklebust 		if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
18038ce37abdSTrond Myklebust 			dir_verifier = inode_peek_iversion_raw(dir);
18041da177e4SLinus Torvalds 		goto no_entry;
18058ce37abdSTrond Myklebust 	}
18061da177e4SLinus Torvalds 	if (error < 0) {
18071da177e4SLinus Torvalds 		res = ERR_PTR(error);
18089558a007SAnna Schumaker 		goto out;
18091da177e4SLinus Torvalds 	}
1810cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1811bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
181203f28e3aSTrond Myklebust 	if (IS_ERR(res))
18139558a007SAnna Schumaker 		goto out;
181454ceac45SDavid Howells 
181563519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
181663519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1817d69ee9b8STrond Myklebust 
18181da177e4SLinus Torvalds no_entry:
181941d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
18209eaef27bSTrond Myklebust 	if (res != NULL) {
18219eaef27bSTrond Myklebust 		if (IS_ERR(res))
18229558a007SAnna Schumaker 			goto out;
18231da177e4SLinus Torvalds 		dentry = res;
18249eaef27bSTrond Myklebust 	}
1825a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
18261da177e4SLinus Torvalds out:
18279558a007SAnna Schumaker 	trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1828e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1829e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
18301da177e4SLinus Torvalds 	return res;
18311da177e4SLinus Torvalds }
1832ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
18331da177e4SLinus Torvalds 
183400bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
183500bdadc7STrond Myklebust {
183600bdadc7STrond Myklebust 	/* Case insensitive server? Revalidate dentries */
183700bdadc7STrond Myklebust 	if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
183800bdadc7STrond Myklebust 		d_prune_aliases(inode);
183900bdadc7STrond Myklebust }
184000bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
184100bdadc7STrond Myklebust 
184289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
18430b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
18441da177e4SLinus Torvalds 
1845f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
18460ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1847b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
18481da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
18491da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
185036d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1851b1942c5fSAl Viro 	.d_release	= nfs_d_release,
18521da177e4SLinus Torvalds };
185389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
18541da177e4SLinus Torvalds 
18558a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
18568a5e929dSAl Viro {
18578a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
18588a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
18598a5e929dSAl Viro 		res |= FMODE_READ;
18608a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
18618a5e929dSAl Viro 		res |= FMODE_WRITE;
18628a5e929dSAl Viro 	return res;
18638a5e929dSAl Viro }
18648a5e929dSAl Viro 
1865532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1866cd9a1c0eSTrond Myklebust {
1867532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1868cd9a1c0eSTrond Myklebust }
1869cd9a1c0eSTrond Myklebust 
1870cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1871cd9a1c0eSTrond Myklebust {
1872f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1873cd9a1c0eSTrond Myklebust 	return 0;
1874cd9a1c0eSTrond Myklebust }
1875cd9a1c0eSTrond Myklebust 
1876d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
18770dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1878b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1879cd9a1c0eSTrond Myklebust {
18800dd2b474SMiklos Szeredi 	int err;
18810dd2b474SMiklos Szeredi 
1882be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
188330d90494SAl Viro 	if (err)
1884d9585277SAl Viro 		goto out;
1885eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
188630d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1887eaa2b82cSNeilBrown 	else
18889821421aSTrond Myklebust 		err = -EOPENSTALE;
1889cd9a1c0eSTrond Myklebust out:
1890d9585277SAl Viro 	return err;
1891cd9a1c0eSTrond Myklebust }
1892cd9a1c0eSTrond Myklebust 
189373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
189430d90494SAl Viro 		    struct file *file, unsigned open_flags,
189544907d79SAl Viro 		    umode_t mode)
18961da177e4SLinus Torvalds {
1897c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1898cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
18990dd2b474SMiklos Szeredi 	struct dentry *res;
19000dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1901f46e0bd3STrond Myklebust 	struct inode *inode;
19021472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
190368eaba4cSTrond Myklebust 	unsigned long dir_verifier;
1904c94c0953SAl Viro 	bool switched = false;
190573a09dd9SAl Viro 	int created = 0;
1906898f635cSTrond Myklebust 	int err;
19071da177e4SLinus Torvalds 
19080dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
19092b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
19100dd2b474SMiklos Szeredi 
19111e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
19126de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19131e7cb3dcSChuck Lever 
19149597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
19159597c13bSJeff Layton 	if (err)
19169597c13bSJeff Layton 		return err;
19179597c13bSJeff Layton 
19180dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
19190dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
192000699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
19210dd2b474SMiklos Szeredi 			/*
19220dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
19230dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
19240dd2b474SMiklos Szeredi 			 * again
19250dd2b474SMiklos Szeredi 			 */
1926d9585277SAl Viro 			return -ENOENT;
19270dd2b474SMiklos Szeredi 		}
19281472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
19291da177e4SLinus Torvalds 		goto no_open;
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 
19320dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1933d9585277SAl Viro 		return -ENAMETOOLONG;
19341da177e4SLinus Torvalds 
19350dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1936dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1937dff25ddbSAndreas Gruenbacher 
1938dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1939dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1940dff25ddbSAndreas Gruenbacher 
1941536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1942dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
19430dd2b474SMiklos Szeredi 	}
1944536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1945536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1946536e43d1STrond Myklebust 		attr.ia_size = 0;
1947cd9a1c0eSTrond Myklebust 	}
1948cd9a1c0eSTrond Myklebust 
1949c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1950c94c0953SAl Viro 		d_drop(dentry);
1951c94c0953SAl Viro 		switched = true;
1952c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1953c94c0953SAl Viro 					  &dentry->d_name, &wq);
1954c94c0953SAl Viro 		if (IS_ERR(dentry))
1955c94c0953SAl Viro 			return PTR_ERR(dentry);
1956c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1957c94c0953SAl Viro 			return finish_no_open(file, dentry);
1958c94c0953SAl Viro 	}
1959c94c0953SAl Viro 
1960532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
19610dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
19620dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1963d9585277SAl Viro 		goto out;
19640dd2b474SMiklos Szeredi 
19656e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
196673a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
196773a09dd9SAl Viro 	if (created)
196873a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1969275bb307STrond Myklebust 	if (IS_ERR(inode)) {
19700dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
19716e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
19722d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1973d20cb71dSAl Viro 		d_drop(dentry);
19740dd2b474SMiklos Szeredi 		switch (err) {
19751da177e4SLinus Torvalds 		case -ENOENT:
1976774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
197768eaba4cSTrond Myklebust 			if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
197868eaba4cSTrond Myklebust 				dir_verifier = inode_peek_iversion_raw(dir);
197968eaba4cSTrond Myklebust 			else
198068eaba4cSTrond Myklebust 				dir_verifier = nfs_save_change_attribute(dir);
198168eaba4cSTrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
19820dd2b474SMiklos Szeredi 			break;
19831788ea6eSJeff Layton 		case -EISDIR:
19846f926b5bSTrond Myklebust 		case -ENOTDIR:
19856f926b5bSTrond Myklebust 			goto no_open;
19861da177e4SLinus Torvalds 		case -ELOOP:
19870dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
19881da177e4SLinus Torvalds 				goto no_open;
19890dd2b474SMiklos Szeredi 			break;
19901da177e4SLinus Torvalds 			/* case -EINVAL: */
19911da177e4SLinus Torvalds 		default:
19920dd2b474SMiklos Szeredi 			break;
19931da177e4SLinus Torvalds 		}
19941da177e4SLinus Torvalds 		goto out;
19951da177e4SLinus Torvalds 	}
19960dd2b474SMiklos Szeredi 
1997b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
19986e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
19992d9db750STrond Myklebust 	put_nfs_open_context(ctx);
2000d9585277SAl Viro out:
2001c94c0953SAl Viro 	if (unlikely(switched)) {
2002c94c0953SAl Viro 		d_lookup_done(dentry);
2003c94c0953SAl Viro 		dput(dentry);
2004c94c0953SAl Viro 	}
2005d9585277SAl Viro 	return err;
20060dd2b474SMiklos Szeredi 
20071da177e4SLinus Torvalds no_open:
20081472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
2009ac795161STrond Myklebust 	if (!res) {
2010ac795161STrond Myklebust 		inode = d_inode(dentry);
2011ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2012e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2013ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
20141751fc1dSTrond Myklebust 		else if (inode && S_ISREG(inode->i_mode))
20151751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2016ac795161STrond Myklebust 	} else if (!IS_ERR(res)) {
2017ac795161STrond Myklebust 		inode = d_inode(res);
2018ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2019e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2020ac795161STrond Myklebust 			dput(res);
2021ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
20221751fc1dSTrond Myklebust 		} else if (inode && S_ISREG(inode->i_mode)) {
20231751fc1dSTrond Myklebust 			dput(res);
20241751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2025ac795161STrond Myklebust 		}
2026ac795161STrond Myklebust 	}
2027c94c0953SAl Viro 	if (switched) {
2028c94c0953SAl Viro 		d_lookup_done(dentry);
2029c94c0953SAl Viro 		if (!res)
2030c94c0953SAl Viro 			res = dentry;
2031c94c0953SAl Viro 		else
2032c94c0953SAl Viro 			dput(dentry);
2033c94c0953SAl Viro 	}
20340dd2b474SMiklos Szeredi 	if (IS_ERR(res))
2035c94c0953SAl Viro 		return PTR_ERR(res);
2036e45198a6SAl Viro 	return finish_no_open(file, res);
20371da177e4SLinus Torvalds }
203889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
20391da177e4SLinus Torvalds 
2040c7944ebbSTrond Myklebust static int
2041c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2042c7944ebbSTrond Myklebust 			  unsigned int flags)
20431da177e4SLinus Torvalds {
2044657e94b6SNick Piggin 	struct inode *inode;
20451da177e4SLinus Torvalds 
2046fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2047c7944ebbSTrond Myklebust 		goto full_reval;
2048eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
2049c7944ebbSTrond Myklebust 		goto full_reval;
20502b484297STrond Myklebust 
20512b0143b5SDavid Howells 	inode = d_inode(dentry);
20522b484297STrond Myklebust 
20531da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
20541da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
20551da177e4SLinus Torvalds 	 */
2056c7944ebbSTrond Myklebust 	if (inode == NULL)
2057c7944ebbSTrond Myklebust 		goto full_reval;
205849317a7fSNeilBrown 
2059efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
2060c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2061216d5d06STrond Myklebust 
20621da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
20631da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
2064c7944ebbSTrond Myklebust 		goto full_reval;
2065c7944ebbSTrond Myklebust 
20661da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
2067c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2068c7944ebbSTrond Myklebust 		goto reval_dentry;
2069c7944ebbSTrond Myklebust 
2070c7944ebbSTrond Myklebust 	/* Check if the directory changed */
2071c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2072c7944ebbSTrond Myklebust 		goto reval_dentry;
20731da177e4SLinus Torvalds 
20740ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
2075c7944ebbSTrond Myklebust 	return 1;
2076c7944ebbSTrond Myklebust reval_dentry:
2077c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
2078c7944ebbSTrond Myklebust 		return -ECHILD;
207942f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
20800ef97dcfSMiklos Szeredi 
2081c7944ebbSTrond Myklebust full_reval:
2082c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
2083c7944ebbSTrond Myklebust }
2084535918f1STrond Myklebust 
2085c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2086c7944ebbSTrond Myklebust {
2087c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
2088c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
2089c0204fd2STrond Myklebust }
2090c0204fd2STrond Myklebust 
20911da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
20921da177e4SLinus Torvalds 
2093406cd915SBenjamin Coddington struct dentry *
2094406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2095cc6f3298SAnna Schumaker 				struct nfs_fattr *fattr)
20961da177e4SLinus Torvalds {
2097fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
20982b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
20991da177e4SLinus Torvalds 	struct inode *inode;
2100b0c6108eSAl Viro 	struct dentry *d;
2101406cd915SBenjamin Coddington 	int error;
21021da177e4SLinus Torvalds 
2103fab728e1STrond Myklebust 	d_drop(dentry);
2104fab728e1STrond Myklebust 
21051da177e4SLinus Torvalds 	if (fhandle->size == 0) {
21069558a007SAnna Schumaker 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
21071da177e4SLinus Torvalds 		if (error)
2108fab728e1STrond Myklebust 			goto out_error;
21091da177e4SLinus Torvalds 	}
21105724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
21111da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
21121da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2113a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
21142ef61e0eSAnna Schumaker 				fattr, NULL);
21151da177e4SLinus Torvalds 		if (error < 0)
2116fab728e1STrond Myklebust 			goto out_error;
21171da177e4SLinus Torvalds 	}
2118cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2119b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
2120fab728e1STrond Myklebust out:
2121fab728e1STrond Myklebust 	dput(parent);
2122406cd915SBenjamin Coddington 	return d;
2123fab728e1STrond Myklebust out_error:
2124406cd915SBenjamin Coddington 	d = ERR_PTR(error);
2125406cd915SBenjamin Coddington 	goto out;
2126406cd915SBenjamin Coddington }
2127406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2128406cd915SBenjamin Coddington 
2129406cd915SBenjamin Coddington /*
2130406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
2131406cd915SBenjamin Coddington  */
2132406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2133d91bfc46SAnna Schumaker 				struct nfs_fattr *fattr)
2134406cd915SBenjamin Coddington {
2135406cd915SBenjamin Coddington 	struct dentry *d;
2136406cd915SBenjamin Coddington 
2137cc6f3298SAnna Schumaker 	d = nfs_add_or_obtain(dentry, fhandle, fattr);
2138406cd915SBenjamin Coddington 	if (IS_ERR(d))
2139406cd915SBenjamin Coddington 		return PTR_ERR(d);
2140406cd915SBenjamin Coddington 
2141406cd915SBenjamin Coddington 	/* Callers don't care */
2142406cd915SBenjamin Coddington 	dput(d);
2143406cd915SBenjamin Coddington 	return 0;
21441da177e4SLinus Torvalds }
2145ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds /*
21481da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
21491da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
21501da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
21511da177e4SLinus Torvalds  * reply path made it appear to have failed.
21521da177e4SLinus Torvalds  */
2153549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2154549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, bool excl)
21551da177e4SLinus Torvalds {
21561da177e4SLinus Torvalds 	struct iattr attr;
2157ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
21581da177e4SLinus Torvalds 	int error;
21591da177e4SLinus Torvalds 
21601e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
21616de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	attr.ia_mode = mode;
21641da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
21651da177e4SLinus Torvalds 
21668b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
21678867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
21688b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
21691da177e4SLinus Torvalds 	if (error != 0)
21701da177e4SLinus Torvalds 		goto out_err;
21711da177e4SLinus Torvalds 	return 0;
21721da177e4SLinus Torvalds out_err:
21731da177e4SLinus Torvalds 	d_drop(dentry);
21741da177e4SLinus Torvalds 	return error;
21751da177e4SLinus Torvalds }
2176ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds /*
21791da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
21801da177e4SLinus Torvalds  */
2181597d9289SBryan Schumaker int
2182549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2183549c7297SChristian Brauner 	  struct dentry *dentry, umode_t mode, dev_t rdev)
21841da177e4SLinus Torvalds {
21851da177e4SLinus Torvalds 	struct iattr attr;
21861da177e4SLinus Torvalds 	int status;
21871da177e4SLinus Torvalds 
21881e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
21896de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	attr.ia_mode = mode;
21921da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
21931da177e4SLinus Torvalds 
21941ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
21951da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
21961ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
21971da177e4SLinus Torvalds 	if (status != 0)
21981da177e4SLinus Torvalds 		goto out_err;
21991da177e4SLinus Torvalds 	return 0;
22001da177e4SLinus Torvalds out_err:
22011da177e4SLinus Torvalds 	d_drop(dentry);
22021da177e4SLinus Torvalds 	return status;
22031da177e4SLinus Torvalds }
2204ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds /*
22071da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
22081da177e4SLinus Torvalds  */
2209549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2210549c7297SChristian Brauner 	      struct dentry *dentry, umode_t mode)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds 	struct iattr attr;
22131da177e4SLinus Torvalds 	int error;
22141da177e4SLinus Torvalds 
22151e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
22166de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
22191da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
22201da177e4SLinus Torvalds 
22211ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
22221da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
22231ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
22241da177e4SLinus Torvalds 	if (error != 0)
22251da177e4SLinus Torvalds 		goto out_err;
22261da177e4SLinus Torvalds 	return 0;
22271da177e4SLinus Torvalds out_err:
22281da177e4SLinus Torvalds 	d_drop(dentry);
22291da177e4SLinus Torvalds 	return error;
22301da177e4SLinus Torvalds }
2231ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
22321da177e4SLinus Torvalds 
2233d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2234d45b9d8bSTrond Myklebust {
2235dc3f4198SAl Viro 	if (simple_positive(dentry))
2236d45b9d8bSTrond Myklebust 		d_delete(dentry);
2237d45b9d8bSTrond Myklebust }
2238d45b9d8bSTrond Myklebust 
22399019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir,
22409019fb39STrond Myklebust 					   struct dentry *dentry, int error)
22419019fb39STrond Myklebust {
22429019fb39STrond Myklebust 	switch (error) {
22439019fb39STrond Myklebust 	case -ENOENT:
22449019fb39STrond Myklebust 		d_delete(dentry);
224500bdadc7STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
224600bdadc7STrond Myklebust 		break;
22479019fb39STrond Myklebust 	case 0:
224800bdadc7STrond Myklebust 		nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
22499019fb39STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
22509019fb39STrond Myklebust 	}
22519019fb39STrond Myklebust }
22529019fb39STrond Myklebust 
2253597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
22541da177e4SLinus Torvalds {
22551da177e4SLinus Torvalds 	int error;
22561da177e4SLinus Torvalds 
22571e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
22586de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22591da177e4SLinus Torvalds 
22601ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
22612b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2262884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
22631da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
22641da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2265ba6c0592STrond Myklebust 		switch (error) {
2266ba6c0592STrond Myklebust 		case 0:
22672b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2268ba6c0592STrond Myklebust 			break;
2269ba6c0592STrond Myklebust 		case -ENOENT:
2270d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2271ba6c0592STrond Myklebust 		}
2272884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2273ba6c0592STrond Myklebust 	} else
2274ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
22759019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
22761ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 	return error;
22791da177e4SLinus Torvalds }
2280ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds /*
22831da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
22841da177e4SLinus Torvalds  * and after checking that the file has only one user.
22851da177e4SLinus Torvalds  *
22861da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
22871da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
22881da177e4SLinus Torvalds  */
22891da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
22901da177e4SLinus Torvalds {
22912b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
22922b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
22931da177e4SLinus Torvalds 	int error = -EBUSY;
22941da177e4SLinus Torvalds 
22956de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
22981da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
22991da177e4SLinus Torvalds 		error = 0;
23001da177e4SLinus Torvalds 		goto out;
23011da177e4SLinus Torvalds 	}
23021da177e4SLinus Torvalds 
23031ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
23041da177e4SLinus Torvalds 	if (inode != NULL) {
2305912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
23061da177e4SLinus Torvalds 		if (error == 0)
23071b83d707STrond Myklebust 			nfs_drop_nlink(inode);
23081da177e4SLinus Torvalds 	} else
2309912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2310d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2311d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
23121ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
23131da177e4SLinus Torvalds out:
23141da177e4SLinus Torvalds 	return error;
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
23181da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
23191da177e4SLinus Torvalds  *
23201da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
23211da177e4SLinus Torvalds  */
2322597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
23231da177e4SLinus Torvalds {
23241da177e4SLinus Torvalds 	int error;
23251da177e4SLinus Torvalds 	int need_rehash = 0;
23261da177e4SLinus Torvalds 
23271e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
23286de1472fSAl Viro 		dir->i_ino, dentry);
23291da177e4SLinus Torvalds 
23301ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
23311da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
233243245ecaSOlga Kornievskaia 	if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
233343245ecaSOlga Kornievskaia 					     &NFS_I(d_inode(dentry))->flags)) {
23341da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2335ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
23362b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
23371da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
23381ca42382STrond Myklebust 		goto out;
23391da177e4SLinus Torvalds 	}
23401da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
23411da177e4SLinus Torvalds 		__d_drop(dentry);
23421da177e4SLinus Torvalds 		need_rehash = 1;
23431da177e4SLinus Torvalds 	}
23441da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
23451da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
23469019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
23479019fb39STrond Myklebust 	if (need_rehash)
23481da177e4SLinus Torvalds 		d_rehash(dentry);
23491ca42382STrond Myklebust out:
23501ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
23511da177e4SLinus Torvalds 	return error;
23521da177e4SLinus Torvalds }
2353ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
23541da177e4SLinus Torvalds 
2355873101b3SChuck Lever /*
2356873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2357873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2358873101b3SChuck Lever  * using prepare_write/commit_write.
2359873101b3SChuck Lever  *
2360873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2361873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2362873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2363873101b3SChuck Lever  * symlink request has completed on the server.
2364873101b3SChuck Lever  *
2365873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2366873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2367873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2368873101b3SChuck Lever  * and move the raw page into its mapping.
2369873101b3SChuck Lever  */
2370549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2371549c7297SChristian Brauner 		struct dentry *dentry, const char *symname)
23721da177e4SLinus Torvalds {
2373873101b3SChuck Lever 	struct page *page;
2374873101b3SChuck Lever 	char *kaddr;
23751da177e4SLinus Torvalds 	struct iattr attr;
2376873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
23771da177e4SLinus Torvalds 	int error;
23781da177e4SLinus Torvalds 
23791e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
23806de1472fSAl Viro 		dir->i_ino, dentry, symname);
23811da177e4SLinus Torvalds 
2382873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2383873101b3SChuck Lever 		return -ENAMETOOLONG;
23841da177e4SLinus Torvalds 
2385873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2386873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
23871da177e4SLinus Torvalds 
2388e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
238976566991STrond Myklebust 	if (!page)
2390873101b3SChuck Lever 		return -ENOMEM;
2391873101b3SChuck Lever 
2392e8ecde25SAl Viro 	kaddr = page_address(page);
2393873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2394873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2395873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2396873101b3SChuck Lever 
23971ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
239894a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
23991ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2400873101b3SChuck Lever 	if (error != 0) {
24011e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2402873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
24036de1472fSAl Viro 			dentry, symname, error);
24041da177e4SLinus Torvalds 		d_drop(dentry);
2405873101b3SChuck Lever 		__free_page(page);
24061da177e4SLinus Torvalds 		return error;
24071da177e4SLinus Torvalds 	}
24081da177e4SLinus Torvalds 
2409342a67f0STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2410342a67f0STrond Myklebust 
2411873101b3SChuck Lever 	/*
2412873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2413873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2414873101b3SChuck Lever 	 */
24152b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2416873101b3SChuck Lever 							GFP_KERNEL)) {
2417873101b3SChuck Lever 		SetPageUptodate(page);
2418873101b3SChuck Lever 		unlock_page(page);
2419a0b54addSRafael Aquini 		/*
2420a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2421a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2422a0b54addSRafael Aquini 		 */
242309cbfeafSKirill A. Shutemov 		put_page(page);
2424873101b3SChuck Lever 	} else
2425873101b3SChuck Lever 		__free_page(page);
2426873101b3SChuck Lever 
2427873101b3SChuck Lever 	return 0;
2428873101b3SChuck Lever }
2429ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2430873101b3SChuck Lever 
2431597d9289SBryan Schumaker int
24321da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
24331da177e4SLinus Torvalds {
24342b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
24351da177e4SLinus Torvalds 	int error;
24361da177e4SLinus Torvalds 
24376de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
24386de1472fSAl Viro 		old_dentry, dentry);
24391da177e4SLinus Torvalds 
24401fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
24419697d234STrond Myklebust 	d_drop(dentry);
244220497503STrond Myklebust 	if (S_ISREG(inode->i_mode))
244320497503STrond Myklebust 		nfs_sync_inode(inode);
24441da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2445cf809556STrond Myklebust 	if (error == 0) {
2446342a67f0STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
24477de9c6eeSAl Viro 		ihold(inode);
24489697d234STrond Myklebust 		d_add(dentry, inode);
2449cf809556STrond Myklebust 	}
24501fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
24511da177e4SLinus Torvalds 	return error;
24521da177e4SLinus Torvalds }
2453ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds /*
24561da177e4SLinus Torvalds  * RENAME
24571da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
24581da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
24591da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
24601da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
24611da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
24621da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
24631da177e4SLinus Torvalds  *
24641da177e4SLinus Torvalds  * FIXED.
24651da177e4SLinus Torvalds  *
24661da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
24671da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
24681da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
24691da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
24701da177e4SLinus Torvalds  * using the inode layer
24711da177e4SLinus Torvalds  *
24721da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
24731da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
24741da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
24751da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
24761da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
24771da177e4SLinus Torvalds  * the rename.
24781da177e4SLinus Torvalds  */
2479549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2480549c7297SChristian Brauner 	       struct dentry *old_dentry, struct inode *new_dir,
2481549c7297SChristian Brauner 	       struct dentry *new_dentry, unsigned int flags)
24821da177e4SLinus Torvalds {
24832b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
24842b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2485d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
248680a491fdSJeff Layton 	struct rpc_task *task;
24871da177e4SLinus Torvalds 	int error = -EBUSY;
24881da177e4SLinus Torvalds 
24891cd66c93SMiklos Szeredi 	if (flags)
24901cd66c93SMiklos Szeredi 		return -EINVAL;
24911cd66c93SMiklos Szeredi 
24926de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
24936de1472fSAl Viro 		 old_dentry, new_dentry,
249484d08fa8SAl Viro 		 d_count(new_dentry));
24951da177e4SLinus Torvalds 
249670ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
24971da177e4SLinus Torvalds 	/*
249828f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
249928f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
250028f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
250128f79a1aSMiklos Szeredi 	 * the new target.
25021da177e4SLinus Torvalds 	 */
250327226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
250427226104SMiklos Szeredi 		/*
250527226104SMiklos Szeredi 		 * To prevent any new references to the target during the
250627226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
250727226104SMiklos Szeredi 		 */
2508d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
250927226104SMiklos Szeredi 			d_drop(new_dentry);
2510d9f29500SBenjamin Coddington 			rehash = new_dentry;
2511d9f29500SBenjamin Coddington 		}
251227226104SMiklos Szeredi 
251384d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
25141da177e4SLinus Torvalds 			int err;
251527226104SMiklos Szeredi 
25161da177e4SLinus Torvalds 			/* copy the target dentry's name */
25171da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
25181da177e4SLinus Torvalds 					 &new_dentry->d_name);
25191da177e4SLinus Torvalds 			if (!dentry)
25201da177e4SLinus Torvalds 				goto out;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
25231da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
252424e93025SMiklos Szeredi 			if (err)
25251da177e4SLinus Torvalds 				goto out;
252624e93025SMiklos Szeredi 
252724e93025SMiklos Szeredi 			new_dentry = dentry;
2528d9f29500SBenjamin Coddington 			rehash = NULL;
252924e93025SMiklos Szeredi 			new_inode = NULL;
2530b1e4adf4STrond Myklebust 		}
253127226104SMiklos Szeredi 	}
25321da177e4SLinus Torvalds 
25336ff9d99bSTrond Myklebust 	if (S_ISREG(old_inode->i_mode))
25346ff9d99bSTrond Myklebust 		nfs_sync_inode(old_inode);
2535d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
253680a491fdSJeff Layton 	if (IS_ERR(task)) {
253780a491fdSJeff Layton 		error = PTR_ERR(task);
253880a491fdSJeff Layton 		goto out;
253980a491fdSJeff Layton 	}
254080a491fdSJeff Layton 
254180a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2542818a8dbeSBenjamin Coddington 	if (error != 0) {
2543818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2544818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2545818a8dbeSBenjamin Coddington 		smp_wmb();
2546818a8dbeSBenjamin Coddington 	} else
254780a491fdSJeff Layton 		error = task->tk_status;
254880a491fdSJeff Layton 	rpc_put_task(task);
254959a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
255059a707b0STrond Myklebust 	if (error == 0) {
255159a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
255259a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2553ac46b3d7STrond Myklebust 		nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2554ac46b3d7STrond Myklebust 							 NFS_INO_INVALID_CTIME |
2555ac46b3d7STrond Myklebust 							 NFS_INO_REVAL_FORCED);
255659a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
255759a707b0STrond Myklebust 	}
25581da177e4SLinus Torvalds out:
2559d9f29500SBenjamin Coddington 	if (rehash)
2560d9f29500SBenjamin Coddington 		d_rehash(rehash);
256170ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
256270ded201STrond Myklebust 			new_dir, new_dentry, error);
2563d9f29500SBenjamin Coddington 	if (!error) {
2564d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2565d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2566d9f29500SBenjamin Coddington 		/*
2567d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2568d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2569d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2570d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2571d9f29500SBenjamin Coddington 		 */
2572d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2573d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2574d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2575d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2576d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2577d9f29500SBenjamin Coddington 
25781da177e4SLinus Torvalds 	/* new dentry created? */
25791da177e4SLinus Torvalds 	if (dentry)
25801da177e4SLinus Torvalds 		dput(dentry);
25811da177e4SLinus Torvalds 	return error;
25821da177e4SLinus Torvalds }
2583ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
25841da177e4SLinus Torvalds 
2585cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2586cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2587cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2588cfcea3e8STrond Myklebust 
2589a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
25903a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
25913a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
25923a505845STrond Myklebust 
25931c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
25941c3c07e9STrond Myklebust {
25956238aec8SNeilBrown 	put_group_info(entry->group_info);
2596f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
25974e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2598cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
25994e857c58SPeter Zijlstra 	smp_mb__after_atomic();
26001c3c07e9STrond Myklebust }
26011c3c07e9STrond Myklebust 
26021a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
26031a81bb8aSTrond Myklebust {
26041a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
26051a81bb8aSTrond Myklebust 
26061a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
26071a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
26081a81bb8aSTrond Myklebust 		list_del(&cache->lru);
26091a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
26101a81bb8aSTrond Myklebust 	}
26111a81bb8aSTrond Myklebust }
26121a81bb8aSTrond Myklebust 
26133a505845STrond Myklebust static unsigned long
26143a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2615979df72eSTrond Myklebust {
2616979df72eSTrond Myklebust 	LIST_HEAD(head);
2617aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2618979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
26191ab6c499SDave Chinner 	long freed = 0;
2620979df72eSTrond Myklebust 
2621a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2622aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2623979df72eSTrond Myklebust 		struct inode *inode;
2624979df72eSTrond Myklebust 
2625979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2626979df72eSTrond Myklebust 			break;
26279c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2628979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2629979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2630979df72eSTrond Myklebust 			goto remove_lru_entry;
2631979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2632979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2633979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2634979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
26351ab6c499SDave Chinner 		freed++;
2636979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2637979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2638979df72eSTrond Myklebust 					&nfs_access_lru_list);
2639979df72eSTrond Myklebust 		else {
2640979df72eSTrond Myklebust remove_lru_entry:
2641979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
26424e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2643979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
26444e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2645979df72eSTrond Myklebust 		}
264659844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2647979df72eSTrond Myklebust 	}
2648979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
26491a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
26501ab6c499SDave Chinner 	return freed;
26511ab6c499SDave Chinner }
26521ab6c499SDave Chinner 
26531ab6c499SDave Chinner unsigned long
26543a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
26553a505845STrond Myklebust {
26563a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
26573a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
26583a505845STrond Myklebust 
26593a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
26603a505845STrond Myklebust 		return SHRINK_STOP;
26613a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
26623a505845STrond Myklebust }
26633a505845STrond Myklebust 
26643a505845STrond Myklebust 
26653a505845STrond Myklebust unsigned long
26661ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
26671ab6c499SDave Chinner {
266855f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2669979df72eSTrond Myklebust }
2670979df72eSTrond Myklebust 
26713a505845STrond Myklebust static void
26723a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
26733a505845STrond Myklebust {
26743a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
26753a505845STrond Myklebust 	unsigned long diff;
26763a505845STrond Myklebust 	unsigned int nr_to_scan;
26773a505845STrond Myklebust 
26783a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
26793a505845STrond Myklebust 		return;
26803a505845STrond Myklebust 	nr_to_scan = 100;
26813a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
26823a505845STrond Myklebust 	if (diff < nr_to_scan)
26833a505845STrond Myklebust 		nr_to_scan = diff;
26843a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
26853a505845STrond Myklebust }
26863a505845STrond Myklebust 
26871a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
26881c3c07e9STrond Myklebust {
26891c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
26901a81bb8aSTrond Myklebust 	struct rb_node *n;
26911c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
26921c3c07e9STrond Myklebust 
26931c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
26941c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
26951c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
26961c3c07e9STrond Myklebust 		rb_erase(n, root_node);
26971a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
26981c3c07e9STrond Myklebust 	}
26991c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
27001c3c07e9STrond Myklebust }
27011c3c07e9STrond Myklebust 
27021c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
27031c3c07e9STrond Myklebust {
27041a81bb8aSTrond Myklebust 	LIST_HEAD(head);
27051a81bb8aSTrond Myklebust 
27061a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
27071a81bb8aSTrond Myklebust 		return;
2708cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2709cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
27101a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2711cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2712cfcea3e8STrond Myklebust 
27131c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
27141a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
27151a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
27161a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
27171a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
27181c3c07e9STrond Myklebust }
27191c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
27201c3c07e9STrond Myklebust 
27216238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
27226238aec8SNeilBrown {
27236238aec8SNeilBrown 	struct group_info *ga, *gb;
27246238aec8SNeilBrown 	int g;
27256238aec8SNeilBrown 
27266238aec8SNeilBrown 	if (uid_lt(a->fsuid, b->fsuid))
27276238aec8SNeilBrown 		return -1;
27286238aec8SNeilBrown 	if (uid_gt(a->fsuid, b->fsuid))
27296238aec8SNeilBrown 		return 1;
27306238aec8SNeilBrown 
27316238aec8SNeilBrown 	if (gid_lt(a->fsgid, b->fsgid))
27326238aec8SNeilBrown 		return -1;
27336238aec8SNeilBrown 	if (gid_gt(a->fsgid, b->fsgid))
27346238aec8SNeilBrown 		return 1;
27356238aec8SNeilBrown 
27366238aec8SNeilBrown 	ga = a->group_info;
27376238aec8SNeilBrown 	gb = b->group_info;
27386238aec8SNeilBrown 	if (ga == gb)
27396238aec8SNeilBrown 		return 0;
27406238aec8SNeilBrown 	if (ga == NULL)
27416238aec8SNeilBrown 		return -1;
27426238aec8SNeilBrown 	if (gb == NULL)
27436238aec8SNeilBrown 		return 1;
27446238aec8SNeilBrown 	if (ga->ngroups < gb->ngroups)
27456238aec8SNeilBrown 		return -1;
27466238aec8SNeilBrown 	if (ga->ngroups > gb->ngroups)
27476238aec8SNeilBrown 		return 1;
27486238aec8SNeilBrown 
27496238aec8SNeilBrown 	for (g = 0; g < ga->ngroups; g++) {
27506238aec8SNeilBrown 		if (gid_lt(ga->gid[g], gb->gid[g]))
27516238aec8SNeilBrown 			return -1;
27526238aec8SNeilBrown 		if (gid_gt(ga->gid[g], gb->gid[g]))
27536238aec8SNeilBrown 			return 1;
27546238aec8SNeilBrown 	}
27556238aec8SNeilBrown 	return 0;
27566238aec8SNeilBrown }
27576238aec8SNeilBrown 
2758b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
27591c3c07e9STrond Myklebust {
27601c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
27611c3c07e9STrond Myklebust 
27621c3c07e9STrond Myklebust 	while (n != NULL) {
2763b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2764b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
27656238aec8SNeilBrown 		int cmp = access_cmp(cred, entry);
27661c3c07e9STrond Myklebust 
2767b68572e0SNeilBrown 		if (cmp < 0)
27681c3c07e9STrond Myklebust 			n = n->rb_left;
2769b68572e0SNeilBrown 		else if (cmp > 0)
27701c3c07e9STrond Myklebust 			n = n->rb_right;
27711c3c07e9STrond Myklebust 		else
27721c3c07e9STrond Myklebust 			return entry;
27731c3c07e9STrond Myklebust 	}
27741c3c07e9STrond Myklebust 	return NULL;
27751c3c07e9STrond Myklebust }
27761c3c07e9STrond Myklebust 
2777b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
27781da177e4SLinus Torvalds {
277955296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
27801c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
278157b69181STrond Myklebust 	bool retry = true;
278257b69181STrond Myklebust 	int err;
27831da177e4SLinus Torvalds 
27841c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
278557b69181STrond Myklebust 	for(;;) {
27861c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
27871c3c07e9STrond Myklebust 			goto out_zap;
27881c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
278957b69181STrond Myklebust 		err = -ENOENT;
27901c3c07e9STrond Myklebust 		if (cache == NULL)
27911c3c07e9STrond Myklebust 			goto out;
279257b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
279321c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
279457b69181STrond Myklebust 			break;
27955c965db8STrond Myklebust 		if (!retry)
27965c965db8STrond Myklebust 			break;
279757b69181STrond Myklebust 		err = -ECHILD;
279857b69181STrond Myklebust 		if (!may_block)
279957b69181STrond Myklebust 			goto out;
280057b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
280157b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
280257b69181STrond Myklebust 		if (err)
280357b69181STrond Myklebust 			return err;
280457b69181STrond Myklebust 		spin_lock(&inode->i_lock);
280557b69181STrond Myklebust 		retry = false;
280657b69181STrond Myklebust 	}
2807b5e7b59cSNeilBrown 	*mask = cache->mask;
2808cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
28091c3c07e9STrond Myklebust 	err = 0;
28101c3c07e9STrond Myklebust out:
28111c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
28121c3c07e9STrond Myklebust 	return err;
28131c3c07e9STrond Myklebust out_zap:
28141a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
28151a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
28161c3c07e9STrond Myklebust 	return -ENOENT;
28171c3c07e9STrond Myklebust }
28181c3c07e9STrond Myklebust 
2819b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2820f682a398SNeilBrown {
2821f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2822f682a398SNeilBrown 	 * but do it without locking.
2823f682a398SNeilBrown 	 */
2824f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2825f682a398SNeilBrown 	struct nfs_access_entry *cache;
2826f682a398SNeilBrown 	int err = -ECHILD;
2827f682a398SNeilBrown 	struct list_head *lh;
2828f682a398SNeilBrown 
2829f682a398SNeilBrown 	rcu_read_lock();
2830f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2831f682a398SNeilBrown 		goto out;
28329f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2833f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2834f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
28356238aec8SNeilBrown 	    access_cmp(cred, cache) != 0)
2836f682a398SNeilBrown 		cache = NULL;
2837f682a398SNeilBrown 	if (cache == NULL)
2838f682a398SNeilBrown 		goto out;
283921c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2840f682a398SNeilBrown 		goto out;
2841b5e7b59cSNeilBrown 	*mask = cache->mask;
284221c3ba7eSTrond Myklebust 	err = 0;
2843f682a398SNeilBrown out:
2844f682a398SNeilBrown 	rcu_read_unlock();
2845f682a398SNeilBrown 	return err;
2846f682a398SNeilBrown }
2847f682a398SNeilBrown 
2848b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2849b5e7b59cSNeilBrown 			  u32 *mask, bool may_block)
2850d2ae4f8bSFrank van der Linden {
2851d2ae4f8bSFrank van der Linden 	int status;
2852d2ae4f8bSFrank van der Linden 
2853b5e7b59cSNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, mask);
2854d2ae4f8bSFrank van der Linden 	if (status != 0)
2855b5e7b59cSNeilBrown 		status = nfs_access_get_cached_locked(inode, cred, mask,
2856d2ae4f8bSFrank van der Linden 		    may_block);
2857d2ae4f8bSFrank van der Linden 
2858d2ae4f8bSFrank van der Linden 	return status;
2859d2ae4f8bSFrank van der Linden }
2860d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2861d2ae4f8bSFrank van der Linden 
286273fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode,
286373fbb3faSNeilBrown 				  struct nfs_access_entry *set,
286473fbb3faSNeilBrown 				  const struct cred *cred)
28651c3c07e9STrond Myklebust {
2866cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2867cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
28681c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
28691c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
28701c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2871b68572e0SNeilBrown 	int cmp;
28721c3c07e9STrond Myklebust 
28731c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
28741c3c07e9STrond Myklebust 	while (*p != NULL) {
28751c3c07e9STrond Myklebust 		parent = *p;
28761c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
28776238aec8SNeilBrown 		cmp = access_cmp(cred, entry);
28781c3c07e9STrond Myklebust 
2879b68572e0SNeilBrown 		if (cmp < 0)
28801c3c07e9STrond Myklebust 			p = &parent->rb_left;
2881b68572e0SNeilBrown 		else if (cmp > 0)
28821c3c07e9STrond Myklebust 			p = &parent->rb_right;
28831c3c07e9STrond Myklebust 		else
28841c3c07e9STrond Myklebust 			goto found;
28851c3c07e9STrond Myklebust 	}
28861c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
28871c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2888cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
28891c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
28901c3c07e9STrond Myklebust 	return;
28911c3c07e9STrond Myklebust found:
28921c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2893cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2894cfcea3e8STrond Myklebust 	list_del(&entry->lru);
28951c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
28961c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
28971da177e4SLinus Torvalds }
28981da177e4SLinus Torvalds 
289973fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
290073fbb3faSNeilBrown 			  const struct cred *cred)
29011da177e4SLinus Torvalds {
29021c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
29031c3c07e9STrond Myklebust 	if (cache == NULL)
29041c3c07e9STrond Myklebust 		return;
29051c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
29066238aec8SNeilBrown 	cache->fsuid = cred->fsuid;
29076238aec8SNeilBrown 	cache->fsgid = cred->fsgid;
29086238aec8SNeilBrown 	cache->group_info = get_group_info(cred->group_info);
29091da177e4SLinus Torvalds 	cache->mask = set->mask;
29101c3c07e9STrond Myklebust 
2911f682a398SNeilBrown 	/* The above field assignments must be visible
2912f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2913f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2914f682a398SNeilBrown 	 */
2915f682a398SNeilBrown 	smp_wmb();
291673fbb3faSNeilBrown 	nfs_access_add_rbtree(inode, cache, cred);
2917cfcea3e8STrond Myklebust 
2918cfcea3e8STrond Myklebust 	/* Update accounting */
29194e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2920cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
29214e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2922cfcea3e8STrond Myklebust 
2923cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
29241a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2925cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
29261a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
29271a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
29281a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2929cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2930cfcea3e8STrond Myklebust 	}
29313a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
29321da177e4SLinus Torvalds }
29336168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
29346168f62cSWeston Andros Adamson 
29353c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
29363c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
29373c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
29383c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
29393c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
29403c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2941ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
29423c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
29433c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
294415d4b73aSTrond Myklebust static int
2945ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
294615d4b73aSTrond Myklebust {
294715d4b73aSTrond Myklebust 	int mask = 0;
294815d4b73aSTrond Myklebust 
294915d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
295015d4b73aSTrond Myklebust 		mask |= MAY_READ;
2951ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2952ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
295315d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2954ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
295515d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2956ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2957ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2958ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2959ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
296015d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2961ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2962ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
296315d4b73aSTrond Myklebust 	return mask;
296415d4b73aSTrond Myklebust }
296515d4b73aSTrond Myklebust 
29666168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
29676168f62cSWeston Andros Adamson {
2968bd8b2441STrond Myklebust 	entry->mask = access_result;
29696168f62cSWeston Andros Adamson }
29706168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
29711da177e4SLinus Torvalds 
2972b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
29731da177e4SLinus Torvalds {
29741da177e4SLinus Torvalds 	struct nfs_access_entry cache;
297557b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2976e8194b7dSTrond Myklebust 	int cache_mask = -1;
29771da177e4SLinus Torvalds 	int status;
29781da177e4SLinus Torvalds 
2979f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2980f4ce1299STrond Myklebust 
2981b5e7b59cSNeilBrown 	status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
29821da177e4SLinus Torvalds 	if (status == 0)
2983f4ce1299STrond Myklebust 		goto out_cached;
29841da177e4SLinus Torvalds 
2985f3324a2aSNeilBrown 	status = -ECHILD;
298657b69181STrond Myklebust 	if (!may_block)
2987f3324a2aSNeilBrown 		goto out;
2988f3324a2aSNeilBrown 
29891750d929SAnna Schumaker 	/*
29901750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
29911750d929SAnna Schumaker 	 */
299284631f84STrond Myklebust 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
299384631f84STrond Myklebust 		     nfs_access_xattr_mask(NFS_SERVER(inode));
29941750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
29951750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
29961750d929SAnna Schumaker 	else
29971750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
299873fbb3faSNeilBrown 	status = NFS_PROTO(inode)->access(inode, &cache, cred);
2999a71ee337SSuresh Jayaraman 	if (status != 0) {
3000a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
3001a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
300293ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
300393ce4af7STrond Myklebust 			else
300493ce4af7STrond Myklebust 				nfs_zap_caches(inode);
3005a71ee337SSuresh Jayaraman 		}
3006f4ce1299STrond Myklebust 		goto out;
3007a71ee337SSuresh Jayaraman 	}
300873fbb3faSNeilBrown 	nfs_access_add_cache(inode, &cache, cred);
3009f4ce1299STrond Myklebust out_cached:
3010ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3011bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3012f4ce1299STrond Myklebust 		status = -EACCES;
30131da177e4SLinus Torvalds out:
3014e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
3015f4ce1299STrond Myklebust 	return status;
30161da177e4SLinus Torvalds }
30171da177e4SLinus Torvalds 
3018af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
3019af22f94aSTrond Myklebust {
3020af22f94aSTrond Myklebust 	int mask = 0;
3021af22f94aSTrond Myklebust 
3022f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
3023f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
3024f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
3025f8d9a897SWeston Andros Adamson 	} else {
30268a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
3027af22f94aSTrond Myklebust 			mask |= MAY_READ;
30288a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
3029af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
3030f8d9a897SWeston Andros Adamson 	}
3031f8d9a897SWeston Andros Adamson 
3032af22f94aSTrond Myklebust 	return mask;
3033af22f94aSTrond Myklebust }
3034af22f94aSTrond Myklebust 
3035b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3036af22f94aSTrond Myklebust {
3037af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3038af22f94aSTrond Myklebust }
303989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
3040af22f94aSTrond Myklebust 
30415c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
30425c5fc09aSTrond Myklebust {
30435c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
304421c3ba7eSTrond Myklebust 	int ret = 0;
30455c5fc09aSTrond Myklebust 
30463825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
30473825827eSTrond Myklebust 		return 0;
3048720869ebSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
30495c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
305021c3ba7eSTrond Myklebust 			return -ECHILD;
305121c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
305221c3ba7eSTrond Myklebust 	}
30535c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
30545c5fc09aSTrond Myklebust 		ret = -EACCES;
30555c5fc09aSTrond Myklebust 	return ret;
30565c5fc09aSTrond Myklebust }
30575c5fc09aSTrond Myklebust 
3058549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns,
3059549c7297SChristian Brauner 		   struct inode *inode,
3060549c7297SChristian Brauner 		   int mask)
30611da177e4SLinus Torvalds {
3062b68572e0SNeilBrown 	const struct cred *cred = current_cred();
30631da177e4SLinus Torvalds 	int res = 0;
30641da177e4SLinus Torvalds 
306591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
306691d5b470SChuck Lever 
3067e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
30681da177e4SLinus Torvalds 		goto out;
30691da177e4SLinus Torvalds 	/* Is this sys_access() ? */
30709cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
30711da177e4SLinus Torvalds 		goto force_lookup;
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
30741da177e4SLinus Torvalds 		case S_IFLNK:
30751da177e4SLinus Torvalds 			goto out;
30761da177e4SLinus Torvalds 		case S_IFREG:
3077762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
3078762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3079762674f8STrond Myklebust 				return 0;
30801da177e4SLinus Torvalds 			break;
30811da177e4SLinus Torvalds 		case S_IFDIR:
30821da177e4SLinus Torvalds 			/*
30831da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
30841da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
30851da177e4SLinus Torvalds 			 */
30861da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
30871da177e4SLinus Torvalds 				goto out;
30881da177e4SLinus Torvalds 	}
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds force_lookup:
30911da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
30921da177e4SLinus Torvalds 		goto out_notsup;
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
30951da177e4SLinus Torvalds out:
30965c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
30975c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
3098f696a365SMiklos Szeredi 
30991e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
31001e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
31011da177e4SLinus Torvalds 	return res;
31021da177e4SLinus Torvalds out_notsup:
3103d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
3104d51ac1a8SNeilBrown 		return -ECHILD;
3105d51ac1a8SNeilBrown 
3106720869ebSTrond Myklebust 	res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3107720869ebSTrond Myklebust 						  NFS_INO_INVALID_OTHER);
31081da177e4SLinus Torvalds 	if (res == 0)
310947291baaSChristian Brauner 		res = generic_permission(&init_user_ns, inode, mask);
31101e7cb3dcSChuck Lever 	goto out;
31111da177e4SLinus Torvalds }
3112ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
3113