xref: /openbmc/linux/fs/nfs/dir.c (revision 9ff89c25d8addeee8eea84fa828f1d2ad659cc54)
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 
72580f2367STrond Myklebust #define NFS_INIT_DTSIZE PAGE_SIZE
73580f2367STrond Myklebust 
74281f31b2STrond Myklebust static struct nfs_open_dir_context *
75281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir)
76480c2006SBryan Schumaker {
77311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
78480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
79281f31b2STrond Myklebust 
80281f31b2STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
81480c2006SBryan Schumaker 	if (ctx != NULL) {
82311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
83580f2367STrond Myklebust 		ctx->dtsize = NFS_INIT_DTSIZE;
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);
92d1e32ea3STrond 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 {
146d09e673fSTrond Myklebust 	u64 change_attr;
147d1bacf9eSBryan Schumaker 	u64 last_cookie;
148b1e21c97STrond Myklebust 	unsigned int size;
149b1e21c97STrond Myklebust 	unsigned char page_full : 1,
150762567b7STrond Myklebust 		      page_is_eof : 1,
151762567b7STrond Myklebust 		      cookies_are_ordered : 1;
1525601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
153d1bacf9eSBryan Schumaker };
154d1bacf9eSBryan Schumaker 
1556c981effSTrond Myklebust struct nfs_readdir_descriptor {
1561da177e4SLinus Torvalds 	struct file	*file;
1571da177e4SLinus Torvalds 	struct page	*page;
15823db8620SAl Viro 	struct dir_context *ctx;
1591f1d4aa4STrond Myklebust 	pgoff_t		page_index;
160580f2367STrond Myklebust 	pgoff_t		page_index_max;
1612e7a4641STrond Myklebust 	u64		dir_cookie;
1620aded708STrond Myklebust 	u64		last_cookie;
1632e7a4641STrond Myklebust 	u64		dup_cookie;
164f0dd2136STrond Myklebust 	loff_t		current_index;
16559e356a9STrond Myklebust 	loff_t		prev_index;
166d1bacf9eSBryan Schumaker 
167b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
168a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1691f4eab7eSNeil Brown 	unsigned long	timestamp;
1704704f0e2STrond Myklebust 	unsigned long	gencount;
1712e7a4641STrond Myklebust 	unsigned long	attr_gencount;
172d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
173580f2367STrond Myklebust 	unsigned int	buffer_fills;
174580f2367STrond Myklebust 	unsigned int	dtsize;
1752e7a4641STrond Myklebust 	signed char duped;
176a7a3b1e9SBenjamin Coddington 	bool plus;
177e1d2699bSTrond Myklebust 	bool eob;
178a7a3b1e9SBenjamin Coddington 	bool eof;
1796c981effSTrond Myklebust };
1801da177e4SLinus Torvalds 
181580f2367STrond Myklebust static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
182580f2367STrond Myklebust {
183580f2367STrond Myklebust 	struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
184580f2367STrond Myklebust 	unsigned int maxsize = server->dtsize;
185580f2367STrond Myklebust 
186580f2367STrond Myklebust 	if (sz > maxsize)
187580f2367STrond Myklebust 		sz = maxsize;
188580f2367STrond Myklebust 	if (sz < NFS_MIN_FILE_IO_SIZE)
189580f2367STrond Myklebust 		sz = NFS_MIN_FILE_IO_SIZE;
190580f2367STrond Myklebust 	desc->dtsize = sz;
191580f2367STrond Myklebust }
192580f2367STrond Myklebust 
193580f2367STrond Myklebust static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
194580f2367STrond Myklebust {
195580f2367STrond Myklebust 	nfs_set_dtsize(desc, desc->dtsize >> 1);
196580f2367STrond Myklebust }
197580f2367STrond Myklebust 
198580f2367STrond Myklebust static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
199580f2367STrond Myklebust {
200580f2367STrond Myklebust 	nfs_set_dtsize(desc, desc->dtsize << 1);
201580f2367STrond Myklebust }
202580f2367STrond Myklebust 
2031f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array)
2041f1d4aa4STrond Myklebust {
2051f1d4aa4STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
2061f1d4aa4STrond Myklebust }
2071f1d4aa4STrond Myklebust 
208d09e673fSTrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie,
209d09e673fSTrond Myklebust 					u64 change_attr)
2104b310319STrond Myklebust {
2114b310319STrond Myklebust 	struct nfs_cache_array *array;
2124b310319STrond Myklebust 
2134b310319STrond Myklebust 	array = kmap_atomic(page);
2141f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
215d09e673fSTrond Myklebust 	array->change_attr = change_attr;
2161f1d4aa4STrond Myklebust 	array->last_cookie = last_cookie;
217762567b7STrond Myklebust 	array->cookies_are_ordered = 1;
2184b310319STrond Myklebust 	kunmap_atomic(array);
2194b310319STrond Myklebust }
2204b310319STrond Myklebust 
221d1bacf9eSBryan Schumaker /*
222d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
223d1bacf9eSBryan Schumaker  */
224d1bacf9eSBryan Schumaker static
22511de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
226d1bacf9eSBryan Schumaker {
22711de3b11STrond Myklebust 	struct nfs_cache_array *array;
228d1bacf9eSBryan Schumaker 	int i;
2298cd51a0cSTrond Myklebust 
2302b86ce2dSCong Wang 	array = kmap_atomic(page);
231d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
232a52a8a6aSTrond Myklebust 		kfree(array->array[i].name);
2331f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
2342b86ce2dSCong Wang 	kunmap_atomic(array);
235d1bacf9eSBryan Schumaker }
236d1bacf9eSBryan Schumaker 
23735df59d3STrond Myklebust static struct page *
23835df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
23935df59d3STrond Myklebust {
24035df59d3STrond Myklebust 	struct page *page = alloc_page(gfp_flags);
24135df59d3STrond Myklebust 	if (page)
242d09e673fSTrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie, 0);
24335df59d3STrond Myklebust 	return page;
24435df59d3STrond Myklebust }
24535df59d3STrond Myklebust 
24635df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page)
24735df59d3STrond Myklebust {
24835df59d3STrond Myklebust 	if (page) {
24935df59d3STrond Myklebust 		nfs_readdir_clear_array(page);
25035df59d3STrond Myklebust 		put_page(page);
25135df59d3STrond Myklebust 	}
25235df59d3STrond Myklebust }
25335df59d3STrond Myklebust 
254b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
255b1e21c97STrond Myklebust {
256b1e21c97STrond Myklebust 	array->page_is_eof = 1;
257b1e21c97STrond Myklebust 	array->page_full = 1;
258b1e21c97STrond Myklebust }
259b1e21c97STrond Myklebust 
260b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
261b1e21c97STrond Myklebust {
262b1e21c97STrond Myklebust 	return array->page_full;
263b1e21c97STrond Myklebust }
264b1e21c97STrond Myklebust 
265d1bacf9eSBryan Schumaker /*
266d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
267d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
268d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
269d1bacf9eSBryan Schumaker  */
270a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
271d1bacf9eSBryan Schumaker {
272a52a8a6aSTrond Myklebust 	const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
273a52a8a6aSTrond Myklebust 
27404e4bd1cSCatalin Marinas 	/*
27504e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
27604e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
27704e4bd1cSCatalin Marinas 	 */
278a52a8a6aSTrond Myklebust 	if (ret != NULL)
279a52a8a6aSTrond Myklebust 		kmemleak_not_leak(ret);
280a52a8a6aSTrond Myklebust 	return ret;
281d1bacf9eSBryan Schumaker }
282d1bacf9eSBryan Schumaker 
2830b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void)
2840b2662b7STrond Myklebust {
2850b2662b7STrond Myklebust 	return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
2860b2662b7STrond Myklebust 	       sizeof(struct nfs_cache_array_entry);
2870b2662b7STrond Myklebust }
2880b2662b7STrond Myklebust 
289b1e21c97STrond Myklebust /*
290b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
291b1e21c97STrond Myklebust  */
292b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
293b1e21c97STrond Myklebust {
294b1e21c97STrond Myklebust 	if (array->page_full)
295b1e21c97STrond Myklebust 		return -ENOSPC;
2960b2662b7STrond Myklebust 	if (array->size == nfs_readdir_array_maxentries()) {
297b1e21c97STrond Myklebust 		array->page_full = 1;
298b1e21c97STrond Myklebust 		return -ENOSPC;
299b1e21c97STrond Myklebust 	}
300d1bacf9eSBryan Schumaker 	return 0;
301d1bacf9eSBryan Schumaker }
302d1bacf9eSBryan Schumaker 
303d1bacf9eSBryan Schumaker static
304d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
305d1bacf9eSBryan Schumaker {
306a52a8a6aSTrond Myklebust 	struct nfs_cache_array *array;
3074a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
308a52a8a6aSTrond Myklebust 	const char *name;
3094a201d6eSTrond Myklebust 	int ret;
3104a201d6eSTrond Myklebust 
311a52a8a6aSTrond Myklebust 	name = nfs_readdir_copy_name(entry->name, entry->len);
312a52a8a6aSTrond Myklebust 	if (!name)
313a52a8a6aSTrond Myklebust 		return -ENOMEM;
3143020093fSTrond Myklebust 
315a52a8a6aSTrond Myklebust 	array = kmap_atomic(page);
316b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
317a52a8a6aSTrond Myklebust 	if (ret) {
318a52a8a6aSTrond Myklebust 		kfree(name);
3193020093fSTrond Myklebust 		goto out;
320a52a8a6aSTrond Myklebust 	}
3213020093fSTrond Myklebust 
322b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
3234a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
3244a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
3250b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
326a52a8a6aSTrond Myklebust 	cache_entry->name_len = entry->len;
327a52a8a6aSTrond Myklebust 	cache_entry->name = name;
328d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
329762567b7STrond Myklebust 	if (array->last_cookie <= cache_entry->cookie)
330762567b7STrond Myklebust 		array->cookies_are_ordered = 0;
3318cd51a0cSTrond Myklebust 	array->size++;
33247c716cbSTrond Myklebust 	if (entry->eof != 0)
333b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
3344a201d6eSTrond Myklebust out:
335a52a8a6aSTrond Myklebust 	kunmap_atomic(array);
3364a201d6eSTrond Myklebust 	return ret;
337d1bacf9eSBryan Schumaker }
338d1bacf9eSBryan Schumaker 
339d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie,
340d09e673fSTrond Myklebust 				      u64 change_attr)
341d09e673fSTrond Myklebust {
342d09e673fSTrond Myklebust 	struct nfs_cache_array *array = kmap_atomic(page);
343d09e673fSTrond Myklebust 	int ret = true;
344d09e673fSTrond Myklebust 
345d09e673fSTrond Myklebust 	if (array->change_attr != change_attr)
346d09e673fSTrond Myklebust 		ret = false;
347d09e673fSTrond Myklebust 	if (array->size > 0 && array->array[0].cookie != last_cookie)
348d09e673fSTrond Myklebust 		ret = false;
349d09e673fSTrond Myklebust 	kunmap_atomic(array);
350d09e673fSTrond Myklebust 	return ret;
351d09e673fSTrond Myklebust }
352d09e673fSTrond Myklebust 
353d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
354d09e673fSTrond Myklebust {
355d09e673fSTrond Myklebust 	unlock_page(page);
356d09e673fSTrond Myklebust 	put_page(page);
357d09e673fSTrond Myklebust }
358d09e673fSTrond Myklebust 
3591f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
3601f1d4aa4STrond Myklebust 						pgoff_t index, u64 last_cookie)
3611f1d4aa4STrond Myklebust {
3621f1d4aa4STrond Myklebust 	struct page *page;
363d09e673fSTrond Myklebust 	u64 change_attr;
3641f1d4aa4STrond Myklebust 
3651f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
366d09e673fSTrond Myklebust 	if (!page)
367d09e673fSTrond Myklebust 		return NULL;
368d09e673fSTrond Myklebust 	change_attr = inode_peek_iversion_raw(mapping->host);
369d09e673fSTrond Myklebust 	if (PageUptodate(page)) {
370d09e673fSTrond Myklebust 		if (nfs_readdir_page_validate(page, last_cookie, change_attr))
371d09e673fSTrond Myklebust 			return page;
372d09e673fSTrond Myklebust 		nfs_readdir_clear_array(page);
3731f1d4aa4STrond Myklebust 	}
374d09e673fSTrond Myklebust 	nfs_readdir_page_init_array(page, last_cookie, change_attr);
375d09e673fSTrond Myklebust 	SetPageUptodate(page);
3761f1d4aa4STrond Myklebust 	return page;
3771f1d4aa4STrond Myklebust }
3781f1d4aa4STrond Myklebust 
3790b2662b7STrond Myklebust static loff_t nfs_readdir_page_offset(struct page *page)
3800b2662b7STrond Myklebust {
3810b2662b7STrond Myklebust 	return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries();
3820b2662b7STrond Myklebust }
3830b2662b7STrond Myklebust 
3841f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
3851f1d4aa4STrond Myklebust {
3861f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3871f1d4aa4STrond Myklebust 	u64 ret;
3881f1d4aa4STrond Myklebust 
3891f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3901f1d4aa4STrond Myklebust 	ret = array->last_cookie;
3911f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3921f1d4aa4STrond Myklebust 	return ret;
3931f1d4aa4STrond Myklebust }
3941f1d4aa4STrond Myklebust 
3951f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
3961f1d4aa4STrond Myklebust {
3971f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3981f1d4aa4STrond Myklebust 	bool ret;
3991f1d4aa4STrond Myklebust 
4001f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
4011f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
4021f1d4aa4STrond Myklebust 	kunmap_atomic(array);
4031f1d4aa4STrond Myklebust 	return ret;
4041f1d4aa4STrond Myklebust }
4051f1d4aa4STrond Myklebust 
406b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
407b1e21c97STrond Myklebust {
408b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
409b1e21c97STrond Myklebust 
410b1e21c97STrond Myklebust 	array = kmap_atomic(page);
411b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
412b1e21c97STrond Myklebust 	kunmap_atomic(array);
413b1e21c97STrond Myklebust }
414b1e21c97STrond Myklebust 
4153b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
4163b2a09f1STrond Myklebust 					      pgoff_t index, u64 cookie)
4173b2a09f1STrond Myklebust {
4183b2a09f1STrond Myklebust 	struct page *page;
4193b2a09f1STrond Myklebust 
4203b2a09f1STrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
4213b2a09f1STrond Myklebust 	if (page) {
4223b2a09f1STrond Myklebust 		if (nfs_readdir_page_last_cookie(page) == cookie)
4233b2a09f1STrond Myklebust 			return page;
4243b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
4253b2a09f1STrond Myklebust 	}
4263b2a09f1STrond Myklebust 	return NULL;
4273b2a09f1STrond Myklebust }
4283b2a09f1STrond Myklebust 
42959e356a9STrond Myklebust static inline
43059e356a9STrond Myklebust int is_32bit_api(void)
43159e356a9STrond Myklebust {
43259e356a9STrond Myklebust #ifdef CONFIG_COMPAT
43359e356a9STrond Myklebust 	return in_compat_syscall();
43459e356a9STrond Myklebust #else
43559e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
43659e356a9STrond Myklebust #endif
43759e356a9STrond Myklebust }
43859e356a9STrond Myklebust 
43959e356a9STrond Myklebust static
44059e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
44159e356a9STrond Myklebust {
44259e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
44359e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
44459e356a9STrond Myklebust 		return false;
44559e356a9STrond Myklebust 	return true;
44659e356a9STrond Myklebust }
44759e356a9STrond Myklebust 
448c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
449c8f0523bSTrond Myklebust 					struct nfs_readdir_descriptor *desc)
450c8f0523bSTrond Myklebust {
451c8f0523bSTrond Myklebust 	if (array->page_full) {
452c8f0523bSTrond Myklebust 		desc->last_cookie = array->last_cookie;
453c8f0523bSTrond Myklebust 		desc->current_index += array->size;
454c8f0523bSTrond Myklebust 		desc->cache_entry_index = 0;
455c8f0523bSTrond Myklebust 		desc->page_index++;
456c8f0523bSTrond Myklebust 	} else
457c8f0523bSTrond Myklebust 		desc->last_cookie = array->array[0].cookie;
458c8f0523bSTrond Myklebust }
459c8f0523bSTrond Myklebust 
4606c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
4616c981effSTrond Myklebust 				      struct nfs_readdir_descriptor *desc)
462d1bacf9eSBryan Schumaker {
46323db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
464d1bacf9eSBryan Schumaker 	unsigned int index;
465d1bacf9eSBryan Schumaker 
466d1bacf9eSBryan Schumaker 	if (diff < 0)
467d1bacf9eSBryan Schumaker 		goto out_eof;
468d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
469b1e21c97STrond Myklebust 		if (array->page_is_eof)
470d1bacf9eSBryan Schumaker 			goto out_eof;
471c8f0523bSTrond Myklebust 		nfs_readdir_seek_next_array(array, desc);
472d1bacf9eSBryan Schumaker 		return -EAGAIN;
473d1bacf9eSBryan Schumaker 	}
474d1bacf9eSBryan Schumaker 
475d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
4762e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
477d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
478d1bacf9eSBryan Schumaker 	return 0;
479d1bacf9eSBryan Schumaker out_eof:
4806089dd0dSThomas Meyer 	desc->eof = true;
481d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
482d1bacf9eSBryan Schumaker }
483d1bacf9eSBryan Schumaker 
484762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
485762567b7STrond Myklebust 					      u64 cookie)
486762567b7STrond Myklebust {
487762567b7STrond Myklebust 	if (!array->cookies_are_ordered)
488762567b7STrond Myklebust 		return true;
489762567b7STrond Myklebust 	/* Optimisation for monotonically increasing cookies */
490762567b7STrond Myklebust 	if (cookie >= array->last_cookie)
491762567b7STrond Myklebust 		return false;
492762567b7STrond Myklebust 	if (array->size && cookie < array->array[0].cookie)
493762567b7STrond Myklebust 		return false;
494762567b7STrond Myklebust 	return true;
495762567b7STrond Myklebust }
496762567b7STrond Myklebust 
4976c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
4986c981effSTrond Myklebust 					 struct nfs_readdir_descriptor *desc)
499d1bacf9eSBryan Schumaker {
500d1bacf9eSBryan Schumaker 	int i;
5018ef2ce3eSBryan Schumaker 	loff_t new_pos;
502d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
503d1bacf9eSBryan Schumaker 
504762567b7STrond Myklebust 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
505762567b7STrond Myklebust 		goto check_eof;
506762567b7STrond Myklebust 
507d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
5082e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
509496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
5100c030806STrond Myklebust 
5110b2662b7STrond Myklebust 			new_pos = nfs_readdir_page_offset(desc->page) + i;
512d09e673fSTrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount) {
5132e7a4641STrond Myklebust 				desc->duped = 0;
5142e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
51559e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
5162e7a4641STrond Myklebust 				if (desc->duped > 0
5172e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
5180c030806STrond Myklebust 					if (printk_ratelimit()) {
5196de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
5200c030806STrond Myklebust 								"Please contact your server vendor.  "
521a52a8a6aSTrond Myklebust 								"The file: %s has duplicate cookie %llu\n",
522a52a8a6aSTrond Myklebust 								desc->file, array->array[i].name, desc->dir_cookie);
5230c030806STrond Myklebust 					}
5240c030806STrond Myklebust 					status = -ELOOP;
5250c030806STrond Myklebust 					goto out;
5260c030806STrond Myklebust 				}
5272e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
5282e7a4641STrond Myklebust 				desc->duped = -1;
5298ef2ce3eSBryan Schumaker 			}
53059e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
5312e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
53259e356a9STrond Myklebust 			else
53323db8620SAl Viro 				desc->ctx->pos = new_pos;
53459e356a9STrond Myklebust 			desc->prev_index = new_pos;
5358cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
53647c716cbSTrond Myklebust 			return 0;
5378cd51a0cSTrond Myklebust 		}
5388cd51a0cSTrond Myklebust 	}
539762567b7STrond Myklebust check_eof:
540b1e21c97STrond Myklebust 	if (array->page_is_eof) {
541d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
5422e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
5436089dd0dSThomas Meyer 			desc->eof = true;
544c8f0523bSTrond Myklebust 	} else
545c8f0523bSTrond Myklebust 		nfs_readdir_seek_next_array(array, desc);
5460c030806STrond Myklebust out:
547d1bacf9eSBryan Schumaker 	return status;
548d1bacf9eSBryan Schumaker }
549d1bacf9eSBryan Schumaker 
5506c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
551d1bacf9eSBryan Schumaker {
552d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
55347c716cbSTrond Myklebust 	int status;
554d1bacf9eSBryan Schumaker 
555ed09222dSTrond Myklebust 	array = kmap_atomic(desc->page);
556d1bacf9eSBryan Schumaker 
5572e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
558d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
559d1bacf9eSBryan Schumaker 	else
560d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
561d1bacf9eSBryan Schumaker 
562ed09222dSTrond Myklebust 	kunmap_atomic(array);
563d1bacf9eSBryan Schumaker 	return status;
564d1bacf9eSBryan Schumaker }
565d1bacf9eSBryan Schumaker 
566d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
56793b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
568b593c09fSTrond Myklebust 				  __be32 *verf, u64 cookie,
569b593c09fSTrond Myklebust 				  struct page **pages, size_t bufsize,
570b593c09fSTrond Myklebust 				  __be32 *verf_res)
571d1bacf9eSBryan Schumaker {
57282e22a5eSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
57382e22a5eSTrond Myklebust 	struct nfs_readdir_arg arg = {
57482e22a5eSTrond Myklebust 		.dentry = file_dentry(desc->file),
57582e22a5eSTrond Myklebust 		.cred = desc->file->f_cred,
576b593c09fSTrond Myklebust 		.verf = verf,
57782e22a5eSTrond Myklebust 		.cookie = cookie,
57882e22a5eSTrond Myklebust 		.pages = pages,
57982e22a5eSTrond Myklebust 		.page_len = bufsize,
58082e22a5eSTrond Myklebust 		.plus = desc->plus,
58182e22a5eSTrond Myklebust 	};
58282e22a5eSTrond Myklebust 	struct nfs_readdir_res res = {
58382e22a5eSTrond Myklebust 		.verf = verf_res,
58482e22a5eSTrond Myklebust 	};
5854704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
5861da177e4SLinus Torvalds 	int		error;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds  again:
5891da177e4SLinus Torvalds 	timestamp = jiffies;
5904704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
591a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
59282e22a5eSTrond Myklebust 	error = NFS_PROTO(inode)->readdir(&arg, &res);
5931da177e4SLinus Torvalds 	if (error < 0) {
5941da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
5951da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
5961da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
5973a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
59882e22a5eSTrond Myklebust 			desc->plus = arg.plus = false;
5991da177e4SLinus Torvalds 			goto again;
6001da177e4SLinus Torvalds 		}
6011da177e4SLinus Torvalds 		goto error;
6021da177e4SLinus Torvalds 	}
6031f4eab7eSNeil Brown 	desc->timestamp = timestamp;
6044704f0e2STrond Myklebust 	desc->gencount = gencount;
605d1bacf9eSBryan Schumaker error:
606d1bacf9eSBryan Schumaker 	return error;
607d1bacf9eSBryan Schumaker }
608d1bacf9eSBryan Schumaker 
6096c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
610573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
611d1bacf9eSBryan Schumaker {
61259e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
613573c4e1eSChuck Lever 	int error;
614d1bacf9eSBryan Schumaker 
61559e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
616573c4e1eSChuck Lever 	if (error)
617573c4e1eSChuck Lever 		return error;
618d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
619d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
620d1bacf9eSBryan Schumaker 	return 0;
621d1bacf9eSBryan Schumaker }
622d1bacf9eSBryan Schumaker 
623fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
624fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
625fa923369STrond Myklebust  */
626d39ab9deSBryan Schumaker static
627d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
628d39ab9deSBryan Schumaker {
629d8fdb47fSTrond Myklebust 	struct inode *inode;
630fa923369STrond Myklebust 	struct nfs_inode *nfsi;
631fa923369STrond Myklebust 
6322b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
6332b0143b5SDavid Howells 		return 0;
634fa923369STrond Myklebust 
635d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
636d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
637d8fdb47fSTrond Myklebust 		return 0;
638d8fdb47fSTrond Myklebust 
639d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
6407dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
641d39ab9deSBryan Schumaker 		return 0;
6427dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
6437dc72d5fSTrond Myklebust 		return 0;
6447dc72d5fSTrond Myklebust 	return 1;
645d39ab9deSBryan Schumaker }
646d39ab9deSBryan Schumaker 
647d39ab9deSBryan Schumaker static
64823db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
649d69ee9b8STrond Myklebust {
650d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
651d69ee9b8STrond Myklebust 		return false;
652d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
653d69ee9b8STrond Myklebust 		return true;
65423db8620SAl Viro 	if (ctx->pos == 0)
655d69ee9b8STrond Myklebust 		return true;
656d69ee9b8STrond Myklebust 	return false;
657d69ee9b8STrond Myklebust }
658d69ee9b8STrond Myklebust 
659d69ee9b8STrond Myklebust /*
66063519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
66163519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
662d69ee9b8STrond Myklebust  * directory.
663d69ee9b8STrond Myklebust  */
664d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
665d69ee9b8STrond Myklebust {
66663519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
66763519fbcSTrond Myklebust 
66863519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
66963519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
67063519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
671d69ee9b8STrond Myklebust }
672d69ee9b8STrond Myklebust 
673311324adSTrond Myklebust /*
674311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
675311324adSTrond Myklebust  *
676311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
677311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
678311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
679311324adSTrond Myklebust  * cache flush.
680311324adSTrond Myklebust  */
681311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
682311324adSTrond Myklebust {
68363519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
68463519fbcSTrond Myklebust 
68563519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
68663519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
68763519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
688ff81dfb5STrond Myklebust 		set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags);
689311324adSTrond Myklebust 	}
690311324adSTrond Myklebust }
691311324adSTrond Myklebust 
692d69ee9b8STrond Myklebust static
693a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
694a1147b82STrond Myklebust 		unsigned long dir_verifier)
695d39ab9deSBryan Schumaker {
69626fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
6979ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
6984a201d6eSTrond Myklebust 	struct dentry *dentry;
6994a201d6eSTrond Myklebust 	struct dentry *alias;
700d39ab9deSBryan Schumaker 	struct inode *inode;
701aa9c2669SDavid Quigley 	int status;
702d39ab9deSBryan Schumaker 
703fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
704fa923369STrond Myklebust 		return;
7056c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
7066c441c25STrond Myklebust 		return;
70778d04af4STrond Myklebust 	if (filename.len == 0)
70878d04af4STrond Myklebust 		return;
70978d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
71078d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
71178d04af4STrond Myklebust 		return;
71278d04af4STrond Myklebust 	/* ...or '/' */
71378d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
71478d04af4STrond Myklebust 		return;
7154a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
7164a201d6eSTrond Myklebust 		if (filename.len == 1)
7174a201d6eSTrond Myklebust 			return;
7184a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
7194a201d6eSTrond Myklebust 			return;
7204a201d6eSTrond Myklebust 	}
7218387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
722d39ab9deSBryan Schumaker 
7234a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
7249ac3d3e8SAl Viro again:
7259ac3d3e8SAl Viro 	if (!dentry) {
7269ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
7279ac3d3e8SAl Viro 		if (IS_ERR(dentry))
7289ac3d3e8SAl Viro 			return;
7299ac3d3e8SAl Viro 	}
7309ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
7316c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
7326c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
7336c441c25STrond Myklebust 					&entry->fattr->fsid))
7346c441c25STrond Myklebust 			goto out;
735d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
7367dc72d5fSTrond Myklebust 			if (!entry->fh->size)
7377dc72d5fSTrond Myklebust 				goto out;
738a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
7392b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
740aa9c2669SDavid Quigley 			if (!status)
741dd225cb3SAnna Schumaker 				nfs_setsecurity(d_inode(dentry), entry->fattr);
742d39ab9deSBryan Schumaker 			goto out;
743d39ab9deSBryan Schumaker 		} else {
7445542aa2fSEric W. Biederman 			d_invalidate(dentry);
745d39ab9deSBryan Schumaker 			dput(dentry);
7469ac3d3e8SAl Viro 			dentry = NULL;
7479ac3d3e8SAl Viro 			goto again;
748d39ab9deSBryan Schumaker 		}
749d39ab9deSBryan Schumaker 	}
7507dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
7517dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
7527dc72d5fSTrond Myklebust 		goto out;
7537dc72d5fSTrond Myklebust 	}
754d39ab9deSBryan Schumaker 
755cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
75641d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
7579ac3d3e8SAl Viro 	d_lookup_done(dentry);
7589ac3d3e8SAl Viro 	if (alias) {
759d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
760d39ab9deSBryan Schumaker 			goto out;
7619ac3d3e8SAl Viro 		dput(dentry);
7629ac3d3e8SAl Viro 		dentry = alias;
7639ac3d3e8SAl Viro 	}
764a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
765d39ab9deSBryan Schumaker out:
766d39ab9deSBryan Schumaker 	dput(dentry);
767d39ab9deSBryan Schumaker }
768d39ab9deSBryan Schumaker 
769d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
7703b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
7713b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
7723b2a09f1STrond Myklebust 				   struct page **xdr_pages,
77335df59d3STrond Myklebust 				   unsigned int buflen,
77435df59d3STrond Myklebust 				   struct page **arrays,
77535df59d3STrond Myklebust 				   size_t narrays)
776d1bacf9eSBryan Schumaker {
7773b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
778babddc72SBryan Schumaker 	struct xdr_stream stream;
779f7da7a12SBenny Halevy 	struct xdr_buf buf;
78035df59d3STrond Myklebust 	struct page *scratch, *new, *page = *arrays;
7815c346854STrond Myklebust 	int status;
782babddc72SBryan Schumaker 
7836650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
7846650239aSTrond Myklebust 	if (scratch == NULL)
7856650239aSTrond Myklebust 		return -ENOMEM;
786babddc72SBryan Schumaker 
787f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
7880ae4c3e8SChuck Lever 	xdr_set_scratch_page(&stream, scratch);
78999424380SBryan Schumaker 
79099424380SBryan Schumaker 	do {
791b1db9a40SAnna Schumaker 		if (entry->fattr->label)
792b1db9a40SAnna Schumaker 			entry->fattr->label->len = NFS4_MAXLABELLEN;
793d33030e2SJeffrey Mitchell 
79499424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
795972bcdf2STrond Myklebust 		if (status != 0)
79699424380SBryan Schumaker 			break;
7975c346854STrond Myklebust 
798a7a3b1e9SBenjamin Coddington 		if (desc->plus)
799a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
800a1147b82STrond Myklebust 					desc->dir_verifier);
8018cd51a0cSTrond Myklebust 
802db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
8033b2a09f1STrond Myklebust 		if (status != -ENOSPC)
8043b2a09f1STrond Myklebust 			continue;
80599424380SBryan Schumaker 
80635df59d3STrond Myklebust 		if (page->mapping != mapping) {
80735df59d3STrond Myklebust 			if (!--narrays)
8083b2a09f1STrond Myklebust 				break;
80935df59d3STrond Myklebust 			new = nfs_readdir_page_array_alloc(entry->prev_cookie,
81035df59d3STrond Myklebust 							   GFP_KERNEL);
81135df59d3STrond Myklebust 			if (!new)
81235df59d3STrond Myklebust 				break;
81335df59d3STrond Myklebust 			arrays++;
81435df59d3STrond Myklebust 			*arrays = page = new;
815580f2367STrond Myklebust 			desc->page_index_max++;
81635df59d3STrond Myklebust 		} else {
81735df59d3STrond Myklebust 			new = nfs_readdir_page_get_next(mapping,
81835df59d3STrond Myklebust 							page->index + 1,
8193b2a09f1STrond Myklebust 							entry->prev_cookie);
8203b2a09f1STrond Myklebust 			if (!new)
8213b2a09f1STrond Myklebust 				break;
82235df59d3STrond Myklebust 			if (page != *arrays)
8233b2a09f1STrond Myklebust 				nfs_readdir_page_unlock_and_put(page);
8243b2a09f1STrond Myklebust 			page = new;
825580f2367STrond Myklebust 			desc->page_index_max = new->index;
82656e4ebf8SBryan Schumaker 		}
8273b2a09f1STrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
828972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
82956e4ebf8SBryan Schumaker 
830972bcdf2STrond Myklebust 	switch (status) {
831972bcdf2STrond Myklebust 	case -EBADCOOKIE:
832972bcdf2STrond Myklebust 		if (entry->eof) {
833b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
8348cd51a0cSTrond Myklebust 			status = 0;
83556e4ebf8SBryan Schumaker 		}
836972bcdf2STrond Myklebust 		break;
837972bcdf2STrond Myklebust 	case -ENOSPC:
838972bcdf2STrond Myklebust 	case -EAGAIN:
839972bcdf2STrond Myklebust 		status = 0;
840972bcdf2STrond Myklebust 		break;
841972bcdf2STrond Myklebust 	}
8426650239aSTrond Myklebust 
84335df59d3STrond Myklebust 	if (page != *arrays)
8443b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
8456650239aSTrond Myklebust 
8466650239aSTrond Myklebust 	put_page(scratch);
847d1bacf9eSBryan Schumaker 	return status;
84856e4ebf8SBryan Schumaker }
84956e4ebf8SBryan Schumaker 
8501a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
85156e4ebf8SBryan Schumaker {
8521a34c8c9STrond Myklebust 	while (npages--)
8531a34c8c9STrond Myklebust 		put_page(pages[npages]);
8541a34c8c9STrond Myklebust 	kfree(pages);
85556e4ebf8SBryan Schumaker }
85656e4ebf8SBryan Schumaker 
85756e4ebf8SBryan Schumaker /*
858bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
859bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
86056e4ebf8SBryan Schumaker  */
8611a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
86256e4ebf8SBryan Schumaker {
8631a34c8c9STrond Myklebust 	struct page **pages;
8641a34c8c9STrond Myklebust 	size_t i;
86556e4ebf8SBryan Schumaker 
8661a34c8c9STrond Myklebust 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
8671a34c8c9STrond Myklebust 	if (!pages)
8681a34c8c9STrond Myklebust 		return NULL;
86956e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
87056e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
87156e4ebf8SBryan Schumaker 		if (page == NULL)
87256e4ebf8SBryan Schumaker 			goto out_freepages;
87356e4ebf8SBryan Schumaker 		pages[i] = page;
87456e4ebf8SBryan Schumaker 	}
8751a34c8c9STrond Myklebust 	return pages;
87656e4ebf8SBryan Schumaker 
87756e4ebf8SBryan Schumaker out_freepages:
878c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
8791a34c8c9STrond Myklebust 	return NULL;
880d1bacf9eSBryan Schumaker }
881d1bacf9eSBryan Schumaker 
8826c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
88335df59d3STrond Myklebust 				    __be32 *verf_arg, __be32 *verf_res,
88435df59d3STrond Myklebust 				    struct page **arrays, size_t narrays)
885d1bacf9eSBryan Schumaker {
8861a34c8c9STrond Myklebust 	struct page **pages;
88735df59d3STrond Myklebust 	struct page *page = *arrays;
8886b75cf9eSTrond Myklebust 	struct nfs_entry *entry;
8891a34c8c9STrond Myklebust 	size_t array_size;
890b593c09fSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
891580f2367STrond Myklebust 	unsigned int dtsize = desc->dtsize;
892*9ff89c25STrond Myklebust 	unsigned int pglen;
8938cd51a0cSTrond Myklebust 	int status = -ENOMEM;
894d1bacf9eSBryan Schumaker 
8956b75cf9eSTrond Myklebust 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
8966b75cf9eSTrond Myklebust 	if (!entry)
8976b75cf9eSTrond Myklebust 		return -ENOMEM;
8986b75cf9eSTrond Myklebust 	entry->cookie = nfs_readdir_page_last_cookie(page);
8996b75cf9eSTrond Myklebust 	entry->fh = nfs_alloc_fhandle();
900b1db9a40SAnna Schumaker 	entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
9016b75cf9eSTrond Myklebust 	entry->server = NFS_SERVER(inode);
9026b75cf9eSTrond Myklebust 	if (entry->fh == NULL || entry->fattr == NULL)
903d1bacf9eSBryan Schumaker 		goto out;
904d1bacf9eSBryan Schumaker 
9051a34c8c9STrond Myklebust 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
9061a34c8c9STrond Myklebust 	pages = nfs_readdir_alloc_pages(array_size);
9071a34c8c9STrond Myklebust 	if (!pages)
908b1db9a40SAnna Schumaker 		goto out;
909d1bacf9eSBryan Schumaker 
910*9ff89c25STrond Myklebust 	status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
911*9ff89c25STrond Myklebust 					dtsize, verf_res);
912d1bacf9eSBryan Schumaker 	if (status < 0)
913*9ff89c25STrond Myklebust 		goto free_pages;
914972bcdf2STrond Myklebust 
915ac396128STrond Myklebust 	pglen = status;
916*9ff89c25STrond Myklebust 	if (pglen != 0)
91735df59d3STrond Myklebust 		status = nfs_readdir_page_filler(desc, entry, pages, pglen,
91835df59d3STrond Myklebust 						 arrays, narrays);
919*9ff89c25STrond Myklebust 	else
920*9ff89c25STrond Myklebust 		nfs_readdir_page_set_eof(page);
921580f2367STrond Myklebust 	desc->buffer_fills++;
922d1bacf9eSBryan Schumaker 
923*9ff89c25STrond Myklebust free_pages:
924c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
925d1bacf9eSBryan Schumaker out:
9266b75cf9eSTrond Myklebust 	nfs_free_fattr(entry->fattr);
9276b75cf9eSTrond Myklebust 	nfs_free_fhandle(entry->fh);
9286b75cf9eSTrond Myklebust 	kfree(entry);
929d1bacf9eSBryan Schumaker 	return status;
930d1bacf9eSBryan Schumaker }
931d1bacf9eSBryan Schumaker 
9321f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
9331da177e4SLinus Torvalds {
93409cbfeafSKirill A. Shutemov 	put_page(desc->page);
9351da177e4SLinus Torvalds 	desc->page = NULL;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381f1d4aa4STrond Myklebust static void
9391f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
9401da177e4SLinus Torvalds {
9411f1d4aa4STrond Myklebust 	unlock_page(desc->page);
9421f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
9431f1d4aa4STrond Myklebust }
9441f1d4aa4STrond Myklebust 
9451f1d4aa4STrond Myklebust static struct page *
9461f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
9471f1d4aa4STrond Myklebust {
9481f1d4aa4STrond Myklebust 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
9491f1d4aa4STrond Myklebust 					   desc->page_index,
9501f1d4aa4STrond Myklebust 					   desc->last_cookie);
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds /*
954d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
955114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
9561da177e4SLinus Torvalds  */
9576c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
958d1bacf9eSBryan Schumaker {
959227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
960227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
961b593c09fSTrond Myklebust 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
962d1bacf9eSBryan Schumaker 	int res;
963d1bacf9eSBryan Schumaker 
9641f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
9651f1d4aa4STrond Myklebust 	if (!desc->page)
9661f1d4aa4STrond Myklebust 		return -ENOMEM;
9671f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
968580f2367STrond Myklebust 		/* Grow the dtsize if we had to go back for more pages */
969580f2367STrond Myklebust 		if (desc->page_index == desc->page_index_max)
970580f2367STrond Myklebust 			nfs_grow_dtsize(desc);
971580f2367STrond Myklebust 		desc->page_index_max = desc->page_index;
97235df59d3STrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
97335df59d3STrond Myklebust 					       &desc->page, 1);
9749fff59edSTrond Myklebust 		if (res < 0) {
9759fff59edSTrond Myklebust 			nfs_readdir_page_unlock_and_put_cached(desc);
9769fff59edSTrond Myklebust 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
9779fff59edSTrond Myklebust 				invalidate_inode_pages2(desc->file->f_mapping);
9789fff59edSTrond Myklebust 				desc->page_index = 0;
9799fff59edSTrond Myklebust 				return -EAGAIN;
9809fff59edSTrond Myklebust 			}
9819fff59edSTrond Myklebust 			return res;
9829fff59edSTrond Myklebust 		}
983f892c41cSTrond Myklebust 		/*
984f892c41cSTrond Myklebust 		 * Set the cookie verifier if the page cache was empty
985f892c41cSTrond Myklebust 		 */
9866c34f05bSTrond Myklebust 		if (desc->last_cookie == 0 &&
9876c34f05bSTrond Myklebust 		    memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
988f892c41cSTrond Myklebust 			memcpy(nfsi->cookieverf, verf,
989f892c41cSTrond Myklebust 			       sizeof(nfsi->cookieverf));
9906c34f05bSTrond Myklebust 			invalidate_inode_pages2_range(desc->file->f_mapping,
9916c34f05bSTrond Myklebust 						      desc->page_index_max + 1,
9926c34f05bSTrond Myklebust 						      -1);
9936c34f05bSTrond Myklebust 		}
9941f1d4aa4STrond Myklebust 	}
995114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
996ff81dfb5STrond Myklebust 	if (res == 0)
997114de382STrond Myklebust 		return 0;
9981f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
999d1bacf9eSBryan Schumaker 	return res;
1000d1bacf9eSBryan Schumaker }
1001d1bacf9eSBryan Schumaker 
1002794092c5STrond Myklebust static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc)
1003794092c5STrond Myklebust {
1004794092c5STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
1005794092c5STrond Myklebust 	struct inode *dir = file_inode(desc->file);
1006794092c5STrond Myklebust 	unsigned int dtsize = NFS_SERVER(dir)->dtsize;
1007794092c5STrond Myklebust 	loff_t size = i_size_read(dir);
1008794092c5STrond Myklebust 
1009794092c5STrond Myklebust 	/*
1010794092c5STrond Myklebust 	 * Default to uncached readdir if the page cache is empty, and
1011794092c5STrond Myklebust 	 * we're looking for a non-zero cookie in a large directory.
1012794092c5STrond Myklebust 	 */
1013794092c5STrond Myklebust 	return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize;
1014794092c5STrond Myklebust }
1015794092c5STrond Myklebust 
1016d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
10176c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
10181da177e4SLinus Torvalds {
10198cd51a0cSTrond Myklebust 	int res;
1020d1bacf9eSBryan Schumaker 
1021794092c5STrond Myklebust 	if (nfs_readdir_dont_search_cache(desc))
1022794092c5STrond Myklebust 		return -EBADCOOKIE;
1023794092c5STrond Myklebust 
10249fff59edSTrond Myklebust 	do {
10250aded708STrond Myklebust 		if (desc->page_index == 0) {
10268cd51a0cSTrond Myklebust 			desc->current_index = 0;
102759e356a9STrond Myklebust 			desc->prev_index = 0;
10280aded708STrond Myklebust 			desc->last_cookie = 0;
10290aded708STrond Myklebust 		}
1030114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
103147c716cbSTrond Myklebust 	} while (res == -EAGAIN);
10321da177e4SLinus Torvalds 	return res;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds /*
10361da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
10371da177e4SLinus Torvalds  */
103813884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
103913884ff2STrond Myklebust 			   const __be32 *verf)
10401da177e4SLinus Torvalds {
10411da177e4SLinus Torvalds 	struct file	*file = desc->file;
1042dbeaf8c9STrond Myklebust 	struct nfs_cache_array *array;
1043c8f0523bSTrond Myklebust 	unsigned int i;
10448ef2ce3eSBryan Schumaker 
10450795bf83SFabian Frederick 	array = kmap(desc->page);
1046d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
1047ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
10481da177e4SLinus Torvalds 
1049ece0b423STrond Myklebust 		ent = &array->array[i];
1050a52a8a6aSTrond Myklebust 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
105123db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1052e1d2699bSTrond Myklebust 			desc->eob = true;
10531da177e4SLinus Torvalds 			break;
1054ece0b423STrond Myklebust 		}
105513884ff2STrond Myklebust 		memcpy(desc->verf, verf, sizeof(desc->verf));
1056c8f0523bSTrond Myklebust 		if (i == array->size - 1) {
10572e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
1058c8f0523bSTrond Myklebust 			nfs_readdir_seek_next_array(array, desc);
1059c8f0523bSTrond Myklebust 		} else {
1060c8f0523bSTrond Myklebust 			desc->dir_cookie = array->array[i + 1].cookie;
1061c8f0523bSTrond Myklebust 			desc->last_cookie = array->array[0].cookie;
1062c8f0523bSTrond Myklebust 		}
106359e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
10642e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
106559e356a9STrond Myklebust 		else
106659e356a9STrond Myklebust 			desc->ctx->pos++;
10672e7a4641STrond Myklebust 		if (desc->duped != 0)
10682e7a4641STrond Myklebust 			desc->duped = 1;
10698cd51a0cSTrond Myklebust 	}
1070b1e21c97STrond Myklebust 	if (array->page_is_eof)
1071e1d2699bSTrond Myklebust 		desc->eof = !desc->eob;
1072d1bacf9eSBryan Schumaker 
10730795bf83SFabian Frederick 	kunmap(desc->page);
1074dbeaf8c9STrond Myklebust 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1075dbeaf8c9STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds /*
10791da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
10801da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
10811da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
10821da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
10831da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
10841da177e4SLinus Torvalds  *
10851da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
10861da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
10871da177e4SLinus Torvalds  *	 we should already have a complete representation of the
10881da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
10891da177e4SLinus Torvalds  */
10906c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
10911da177e4SLinus Torvalds {
109235df59d3STrond Myklebust 	struct page	**arrays;
109335df59d3STrond Myklebust 	size_t		i, sz = 512;
1094b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
109535df59d3STrond Myklebust 	int		status = -ENOMEM;
10961da177e4SLinus Torvalds 
109735df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
10982e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10991da177e4SLinus Torvalds 
110035df59d3STrond Myklebust 	arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
110135df59d3STrond Myklebust 	if (!arrays)
11021da177e4SLinus Torvalds 		goto out;
110335df59d3STrond Myklebust 	arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
110435df59d3STrond Myklebust 	if (!arrays[0])
110535df59d3STrond Myklebust 		goto out;
11061da177e4SLinus Torvalds 
11077a8e1dc3STrond Myklebust 	desc->page_index = 0;
1108ce292d8fStrondmy@kernel.org 	desc->cache_entry_index = 0;
11092e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
11102e7a4641STrond Myklebust 	desc->duped = 0;
1111580f2367STrond Myklebust 	desc->page_index_max = 0;
11127a8e1dc3STrond Myklebust 
111335df59d3STrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1114d1bacf9eSBryan Schumaker 
1115e1d2699bSTrond Myklebust 	for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
111635df59d3STrond Myklebust 		desc->page = arrays[i];
111713884ff2STrond Myklebust 		nfs_do_filldir(desc, verf);
111835df59d3STrond Myklebust 	}
111935df59d3STrond Myklebust 	desc->page = NULL;
11201da177e4SLinus Torvalds 
1121580f2367STrond Myklebust 	/*
1122580f2367STrond Myklebust 	 * Grow the dtsize if we have to go back for more pages,
1123580f2367STrond Myklebust 	 * or shrink it if we're reading too many.
1124580f2367STrond Myklebust 	 */
1125580f2367STrond Myklebust 	if (!desc->eof) {
1126580f2367STrond Myklebust 		if (!desc->eob)
1127580f2367STrond Myklebust 			nfs_grow_dtsize(desc);
1128580f2367STrond Myklebust 		else if (desc->buffer_fills == 1 &&
1129580f2367STrond Myklebust 			 i < (desc->page_index_max >> 1))
1130580f2367STrond Myklebust 			nfs_shrink_dtsize(desc);
1131580f2367STrond Myklebust 	}
113235df59d3STrond Myklebust 
113335df59d3STrond Myklebust 	for (i = 0; i < sz && arrays[i]; i++)
113435df59d3STrond Myklebust 		nfs_readdir_page_array_free(arrays[i]);
11351da177e4SLinus Torvalds out:
1136580f2367STrond Myklebust 	desc->page_index_max = -1;
113735df59d3STrond Myklebust 	kfree(arrays);
113835df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
11391da177e4SLinus Torvalds 	return status;
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
114200a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
114300a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
114400a92642SOlivier Galibert    whole directory.
11451da177e4SLinus Torvalds  */
114623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
11471da177e4SLinus Torvalds {
1148be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
11492b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
115013884ff2STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
115123db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
11526b75cf9eSTrond Myklebust 	struct nfs_readdir_descriptor *desc;
1153ff81dfb5STrond Myklebust 	pgoff_t page_index;
11546b75cf9eSTrond Myklebust 	int res;
11551da177e4SLinus Torvalds 
11566de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
11576de1472fSAl Viro 			file, (long long)ctx->pos);
115891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
115991d5b470SChuck Lever 
11601da177e4SLinus Torvalds 	/*
116123db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
1162f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
116300a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
116400a92642SOlivier Galibert 	 * revalidate the cookie.
11651da177e4SLinus Torvalds 	 */
1166d09e673fSTrond Myklebust 	nfs_revalidate_mapping(inode, file->f_mapping);
11676b75cf9eSTrond Myklebust 
11686b75cf9eSTrond Myklebust 	res = -ENOMEM;
11696b75cf9eSTrond Myklebust 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
11706b75cf9eSTrond Myklebust 	if (!desc)
11716b75cf9eSTrond Myklebust 		goto out;
11726b75cf9eSTrond Myklebust 	desc->file = file;
11736b75cf9eSTrond Myklebust 	desc->ctx = ctx;
11746b75cf9eSTrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, ctx);
1175580f2367STrond Myklebust 	desc->page_index_max = -1;
1176fccca7fcSTrond Myklebust 
11772e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
11782e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
11792e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
11802e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
1181ff81dfb5STrond Myklebust 	page_index = dir_ctx->page_index;
1182728dd0abSTrond Myklebust 	desc->page_index = page_index;
1183728dd0abSTrond Myklebust 	desc->last_cookie = dir_ctx->last_cookie;
11842e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
1185e1d2699bSTrond Myklebust 	desc->eof = dir_ctx->eof;
1186580f2367STrond Myklebust 	nfs_set_dtsize(desc, dir_ctx->dtsize);
1187b593c09fSTrond Myklebust 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
11882e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1189565277f6STrond Myklebust 
1190e1d2699bSTrond Myklebust 	if (desc->eof) {
1191e1d2699bSTrond Myklebust 		res = 0;
1192e1d2699bSTrond Myklebust 		goto out_free;
1193e1d2699bSTrond Myklebust 	}
1194e1d2699bSTrond Myklebust 
1195ff81dfb5STrond Myklebust 	if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) &&
1196ff81dfb5STrond Myklebust 	    list_is_singular(&nfsi->open_files))
1197ff81dfb5STrond Myklebust 		invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1);
1198ff81dfb5STrond Myklebust 
119947c716cbSTrond Myklebust 	do {
12001da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
120100a92642SOlivier Galibert 
12021da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1203ece0b423STrond Myklebust 			res = 0;
12041da177e4SLinus Torvalds 			/* This means either end of directory */
12052e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
12061da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
120723db8620SAl Viro 				res = uncached_readdir(desc);
1208ece0b423STrond Myklebust 				if (res == 0)
12091da177e4SLinus Torvalds 					continue;
12109fff59edSTrond Myklebust 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
12119fff59edSTrond Myklebust 					res = 0;
12121da177e4SLinus Torvalds 			}
12131da177e4SLinus Torvalds 			break;
12141da177e4SLinus Torvalds 		}
12151da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
121613884ff2STrond Myklebust 			clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
12171da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1218baf57a09STrond Myklebust 			desc->page_index = 0;
1219a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1220a7a3b1e9SBenjamin Coddington 			desc->eof = false;
12211da177e4SLinus Torvalds 			continue;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 		if (res < 0)
12241da177e4SLinus Torvalds 			break;
12251da177e4SLinus Torvalds 
122613884ff2STrond Myklebust 		nfs_do_filldir(desc, nfsi->cookieverf);
12271f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
1228e1d2699bSTrond Myklebust 	} while (!desc->eob && !desc->eof);
12292e7a4641STrond Myklebust 
12302e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
12312e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
12322e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
1233728dd0abSTrond Myklebust 	dir_ctx->last_cookie = desc->last_cookie;
12342e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
12352e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
1236ff81dfb5STrond Myklebust 	dir_ctx->page_index = desc->page_index;
1237e1d2699bSTrond Myklebust 	dir_ctx->eof = desc->eof;
1238580f2367STrond Myklebust 	dir_ctx->dtsize = desc->dtsize;
1239b593c09fSTrond Myklebust 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
12402e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1241e1d2699bSTrond Myklebust out_free:
12426b75cf9eSTrond Myklebust 	kfree(desc);
12436b75cf9eSTrond Myklebust 
1244fccca7fcSTrond Myklebust out:
12456de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
12461da177e4SLinus Torvalds 	return res;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
1249965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1250f0dd2136STrond Myklebust {
1251480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1252b84e06c5SChuck Lever 
12536de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
12546de1472fSAl Viro 			filp, offset, whence);
1255b84e06c5SChuck Lever 
1256965c8e59SAndrew Morton 	switch (whence) {
1257f0dd2136STrond Myklebust 	default:
1258b2b1ff3dSTrond Myklebust 		return -EINVAL;
1259b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1260b2b1ff3dSTrond Myklebust 		if (offset < 0)
1261b2b1ff3dSTrond Myklebust 			return -EINVAL;
126283f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1263b2b1ff3dSTrond Myklebust 		break;
1264b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1265b2b1ff3dSTrond Myklebust 		if (offset == 0)
1266b2b1ff3dSTrond Myklebust 			return filp->f_pos;
126783f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1268b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1269b2b1ff3dSTrond Myklebust 		if (offset < 0) {
127083f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1271b2b1ff3dSTrond Myklebust 			return -EINVAL;
1272b2b1ff3dSTrond Myklebust 		}
1273f0dd2136STrond Myklebust 	}
1274f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1275f0dd2136STrond Myklebust 		filp->f_pos = offset;
1276728dd0abSTrond Myklebust 		if (!nfs_readdir_use_cookie(filp)) {
1277480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
1278728dd0abSTrond Myklebust 			dir_ctx->page_index = 0;
1279728dd0abSTrond Myklebust 		} else
1280728dd0abSTrond Myklebust 			dir_ctx->dir_cookie = offset;
1281b593c09fSTrond Myklebust 		if (offset == 0)
1282b593c09fSTrond Myklebust 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
12838ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1284e1d2699bSTrond Myklebust 		dir_ctx->eof = false;
1285f0dd2136STrond Myklebust 	}
128683f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1287f0dd2136STrond Myklebust 	return offset;
1288f0dd2136STrond Myklebust }
1289f0dd2136STrond Myklebust 
12901da177e4SLinus Torvalds /*
12911da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
12921da177e4SLinus Torvalds  * is a dummy operation.
12931da177e4SLinus Torvalds  */
129402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
129502c24a82SJosef Bacik 			 int datasync)
12961da177e4SLinus Torvalds {
12976de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
12981e7cb3dcSChuck Lever 
129911decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
13001da177e4SLinus Torvalds 	return 0;
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
1303bfc69a45STrond Myklebust /**
1304bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1305302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1306bfc69a45STrond Myklebust  *
1307bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1308bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1309bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1310bfc69a45STrond Myklebust  *
1311efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1312efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1313efeda80dSTrond Myklebust  *
1314bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1315bfc69a45STrond Myklebust  */
1316bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1317bfc69a45STrond Myklebust {
1318efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1319bfc69a45STrond Myklebust }
132089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1321bfc69a45STrond Myklebust 
1322efeda80dSTrond Myklebust /**
1323efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1324efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1325efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1326efeda80dSTrond Myklebust  *
1327efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1328efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1329efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1330efeda80dSTrond Myklebust  */
1331efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1332efeda80dSTrond Myklebust {
1333efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1334efeda80dSTrond Myklebust }
1335efeda80dSTrond Myklebust 
1336efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1337efeda80dSTrond Myklebust {
1338efeda80dSTrond Myklebust 	*verf |= 1UL;
1339efeda80dSTrond Myklebust }
1340efeda80dSTrond Myklebust 
1341efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1342efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1343efeda80dSTrond Myklebust {
1344efeda80dSTrond Myklebust 	*verf &= ~1UL;
1345efeda80dSTrond Myklebust }
1346efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1347efeda80dSTrond Myklebust 
1348efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1349efeda80dSTrond Myklebust {
1350efeda80dSTrond Myklebust 	return verf & 1;
1351efeda80dSTrond Myklebust }
1352efeda80dSTrond Myklebust 
1353efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1354efeda80dSTrond Myklebust {
1355efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1356efeda80dSTrond Myklebust }
1357efeda80dSTrond Myklebust 
1358efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1359efeda80dSTrond Myklebust {
1360efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1361cec08f45STrond Myklebust 	struct inode *dir = d_inode(dentry->d_parent);
1362efeda80dSTrond Myklebust 
1363cec08f45STrond Myklebust 	if (!nfs_verify_change_attribute(dir, verf))
1364cec08f45STrond Myklebust 		return;
1365efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1366efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1367efeda80dSTrond Myklebust 	dentry->d_time = verf;
1368efeda80dSTrond Myklebust }
1369efeda80dSTrond Myklebust 
1370efeda80dSTrond Myklebust /**
1371efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1372efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1373efeda80dSTrond Myklebust  * @verf: verifier to save
1374efeda80dSTrond Myklebust  *
1375efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1376efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1377efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1378efeda80dSTrond Myklebust  * look it up again after a directory change.
1379efeda80dSTrond Myklebust  */
1380efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1381efeda80dSTrond Myklebust {
1382efeda80dSTrond Myklebust 
1383efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1384efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1385efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1386efeda80dSTrond Myklebust }
1387efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1388efeda80dSTrond Myklebust 
1389efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1390efeda80dSTrond Myklebust /**
1391efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1392efeda80dSTrond Myklebust  * @inode: pointer to inode
1393efeda80dSTrond Myklebust  *
1394efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1395efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1396efeda80dSTrond Myklebust  * while holding a delegation.
1397efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1398efeda80dSTrond Myklebust  * returned or revoked.
1399efeda80dSTrond Myklebust  */
1400efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1401efeda80dSTrond Myklebust {
1402efeda80dSTrond Myklebust 	struct dentry *alias;
1403efeda80dSTrond Myklebust 
1404efeda80dSTrond Myklebust 	if (!inode)
1405efeda80dSTrond Myklebust 		return;
1406efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1407efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1408efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1409efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1410efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1411efeda80dSTrond Myklebust 	}
1412efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1413efeda80dSTrond Myklebust }
1414efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1415efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1416efeda80dSTrond Myklebust 
14178ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
14188ce37abdSTrond Myklebust {
14198ce37abdSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
14208ce37abdSTrond Myklebust 	    d_really_is_negative(dentry))
14218ce37abdSTrond Myklebust 		return dentry->d_time == inode_peek_iversion_raw(dir);
14228ce37abdSTrond Myklebust 	return nfs_verify_change_attribute(dir, dentry->d_time);
14238ce37abdSTrond Myklebust }
14248ce37abdSTrond Myklebust 
14251da177e4SLinus Torvalds /*
14261da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
14271da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
14281da177e4SLinus Torvalds  * and may need to be looked up again.
1429912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
14301da177e4SLinus Torvalds  */
1431912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1432912a108dSNeilBrown 			      int rcu_walk)
14331da177e4SLinus Torvalds {
14341da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
14351da177e4SLinus Torvalds 		return 1;
14364eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
14374eec952eSTrond Myklebust 		return 0;
14388ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
14396ecc5e8fSTrond Myklebust 		return 0;
1440f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
14411cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1442912a108dSNeilBrown 		if (rcu_walk)
1443f2c77f4eSTrond Myklebust 			return 0;
14441cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
14451cd9cb05STrond Myklebust 			return 0;
14461cd9cb05STrond Myklebust 	}
14478ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
1448f2c77f4eSTrond Myklebust 		return 0;
1449f2c77f4eSTrond Myklebust 	return 1;
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds /*
1453a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1454a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1455a12802caSTrond Myklebust  */
1456fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1457a12802caSTrond Myklebust {
1458a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1459a12802caSTrond Myklebust 		return 0;
1460fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1461a12802caSTrond Myklebust }
1462a12802caSTrond Myklebust 
1463a12802caSTrond Myklebust /*
14641d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
14651d6757fbSTrond Myklebust  *
14661d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
14671d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
14681d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
14691d6757fbSTrond Myklebust  *
14701d6757fbSTrond Myklebust  */
147165a0c149STrond Myklebust static
1472fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
147565a0c149STrond Myklebust 	int ret;
14761da177e4SLinus Torvalds 
147736d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
14784e99a1ffSTrond Myklebust 		return 0;
147947921921STrond Myklebust 
148047921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
148147921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
148247921921STrond Myklebust 		case S_IFREG:
148347921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
148447921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
148547921921STrond Myklebust 				goto out;
1486df561f66SGustavo A. R. Silva 			fallthrough;
148747921921STrond Myklebust 		case S_IFDIR:
148847921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
148947921921STrond Myklebust 				break;
149047921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
149147921921STrond Myklebust 			goto out_force;
149247921921STrond Myklebust 		}
149347921921STrond Myklebust 	}
149447921921STrond Myklebust 
14951da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1496fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
14971da177e4SLinus Torvalds 		goto out_force;
149865a0c149STrond Myklebust out:
149943245ecaSOlga Kornievskaia 	if (inode->i_nlink > 0 ||
150043245ecaSOlga Kornievskaia 	    (inode->i_nlink == 0 &&
150143245ecaSOlga Kornievskaia 	     test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
150243245ecaSOlga Kornievskaia 		return 0;
150343245ecaSOlga Kornievskaia 	else
150443245ecaSOlga Kornievskaia 		return -ESTALE;
15051da177e4SLinus Torvalds out_force:
15061fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
15071fa1e384SNeilBrown 		return -ECHILD;
150865a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
150965a0c149STrond Myklebust 	if (ret != 0)
151065a0c149STrond Myklebust 		return ret;
151165a0c149STrond Myklebust 	goto out;
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds 
151482e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode)
151582e7ca13STrond Myklebust {
151682e7ca13STrond Myklebust 	spin_lock(&inode->i_lock);
1517a6a361c4STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
151882e7ca13STrond Myklebust 	spin_unlock(&inode->i_lock);
151982e7ca13STrond Myklebust }
152082e7ca13STrond Myklebust 
15211da177e4SLinus Torvalds /*
15221da177e4SLinus Torvalds  * We judge how long we want to trust negative
15231da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
15241da177e4SLinus Torvalds  *
15251da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
15261da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1527912a108dSNeilBrown  *
1528912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1529912a108dSNeilBrown  * suggesting a reval is needed.
15309f6d44d4STrond Myklebust  *
15319f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
15329f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
15331da177e4SLinus Torvalds  */
15341da177e4SLinus Torvalds static inline
15351da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1536fa3c56bbSAl Viro 		       unsigned int flags)
15371da177e4SLinus Torvalds {
15389f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
15391da177e4SLinus Torvalds 		return 0;
15404eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
15414eec952eSTrond Myklebust 		return 1;
154298ca3ee6STrond Myklebust 	/* Case insensitive server? Revalidate negative dentries */
154398ca3ee6STrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
154498ca3ee6STrond Myklebust 		return 1;
1545912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds 
15485ceb9d7fSTrond Myklebust static int
15495ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
15505ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
15511da177e4SLinus Torvalds {
15525ceb9d7fSTrond Myklebust 	switch (error) {
15535ceb9d7fSTrond Myklebust 	case 1:
15542eef8a31STrond Myklebust 		break;
15555ceb9d7fSTrond Myklebust 	case 0:
1556a3f432bfSJ. Bruce Fields 		/*
1557a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1558a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1559a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1560a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1561a3f432bfSJ. Bruce Fields 		 */
156247397915STrond Myklebust 		if (inode && IS_ROOT(dentry))
15632eef8a31STrond Myklebust 			error = 1;
15642eef8a31STrond Myklebust 		break;
15655ceb9d7fSTrond Myklebust 	}
15662eef8a31STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1567e1fb4d05STrond Myklebust 	return error;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds 
15705ceb9d7fSTrond Myklebust static int
15715ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
15725ceb9d7fSTrond Myklebust 			       unsigned int flags)
15735ceb9d7fSTrond Myklebust {
15745ceb9d7fSTrond Myklebust 	int ret = 1;
15755ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
15765ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
15775ceb9d7fSTrond Myklebust 			return -ECHILD;
15785ceb9d7fSTrond Myklebust 		ret = 0;
15795ceb9d7fSTrond Myklebust 	}
15805ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
15815ceb9d7fSTrond Myklebust }
15825ceb9d7fSTrond Myklebust 
15835ceb9d7fSTrond Myklebust static int
15845ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
15855ceb9d7fSTrond Myklebust 				struct inode *inode)
15865ceb9d7fSTrond Myklebust {
15875ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15885ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
15895ceb9d7fSTrond Myklebust }
15905ceb9d7fSTrond Myklebust 
15915ceb9d7fSTrond Myklebust static int
15925ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
15935ceb9d7fSTrond Myklebust 			     struct inode *inode)
15945ceb9d7fSTrond Myklebust {
15955ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
15965ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
1597a1147b82STrond Myklebust 	unsigned long dir_verifier;
15985ceb9d7fSTrond Myklebust 	int ret;
15995ceb9d7fSTrond Myklebust 
16005ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
16015ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
16029558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
16039558a007SAnna Schumaker 	if (fhandle == NULL || fattr == NULL)
16045ceb9d7fSTrond Myklebust 		goto out;
16055ceb9d7fSTrond Myklebust 
1606a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
16079558a007SAnna Schumaker 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
16085ceb9d7fSTrond Myklebust 	if (ret < 0) {
1609f7b37b8bSTrond Myklebust 		switch (ret) {
1610f7b37b8bSTrond Myklebust 		case -ESTALE:
1611f7b37b8bSTrond Myklebust 		case -ENOENT:
16125ceb9d7fSTrond Myklebust 			ret = 0;
1613f7b37b8bSTrond Myklebust 			break;
1614f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1615f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1616f7b37b8bSTrond Myklebust 				ret = 1;
1617f7b37b8bSTrond Myklebust 		}
16185ceb9d7fSTrond Myklebust 		goto out;
16195ceb9d7fSTrond Myklebust 	}
16205ceb9d7fSTrond Myklebust 	ret = 0;
16215ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
16225ceb9d7fSTrond Myklebust 		goto out;
16235ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
16245ceb9d7fSTrond Myklebust 		goto out;
16255ceb9d7fSTrond Myklebust 
1626dd225cb3SAnna Schumaker 	nfs_setsecurity(inode, fattr);
1627a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
16285ceb9d7fSTrond Myklebust 
16295ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
16305ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
16315ceb9d7fSTrond Myklebust 	ret = 1;
16325ceb9d7fSTrond Myklebust out:
16335ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
16345ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
163582e7ca13STrond Myklebust 
163682e7ca13STrond Myklebust 	/*
163782e7ca13STrond Myklebust 	 * If the lookup failed despite the dentry change attribute being
163882e7ca13STrond Myklebust 	 * a match, then we should revalidate the directory cache.
163982e7ca13STrond Myklebust 	 */
16408ce37abdSTrond Myklebust 	if (!ret && nfs_dentry_verify_change(dir, dentry))
164182e7ca13STrond Myklebust 		nfs_mark_dir_for_revalidate(dir);
16425ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
16435ceb9d7fSTrond Myklebust }
16445ceb9d7fSTrond Myklebust 
16455ceb9d7fSTrond Myklebust /*
16465ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
16475ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
16485ceb9d7fSTrond Myklebust  * lookup.
16495ceb9d7fSTrond Myklebust  *
16505ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
16515ceb9d7fSTrond Myklebust  * we have an inode!
16525ceb9d7fSTrond Myklebust  *
16535ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
16545ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
16555ceb9d7fSTrond Myklebust  */
16565ceb9d7fSTrond Myklebust static int
16575ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
16585ceb9d7fSTrond Myklebust 			 unsigned int flags)
16595ceb9d7fSTrond Myklebust {
16605ceb9d7fSTrond Myklebust 	struct inode *inode;
16615ceb9d7fSTrond Myklebust 	int error;
16625ceb9d7fSTrond Myklebust 
16635ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
16645ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
16655ceb9d7fSTrond Myklebust 
16665ceb9d7fSTrond Myklebust 	if (!inode)
16675ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
16685ceb9d7fSTrond Myklebust 
16695ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
16705ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
16715ceb9d7fSTrond Myklebust 				__func__, dentry);
16725ceb9d7fSTrond Myklebust 		goto out_bad;
16735ceb9d7fSTrond Myklebust 	}
16745ceb9d7fSTrond Myklebust 
1675efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
16765ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
16775ceb9d7fSTrond Myklebust 
16785ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
16795ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
16805ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
16815ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
16825ceb9d7fSTrond Myklebust 		if (error) {
16835ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
168482e7ca13STrond Myklebust 				nfs_mark_dir_for_revalidate(dir);
16855ceb9d7fSTrond Myklebust 			goto out_bad;
16865ceb9d7fSTrond Myklebust 		}
16875ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
16885ceb9d7fSTrond Myklebust 		goto out_valid;
16895ceb9d7fSTrond Myklebust 	}
16905ceb9d7fSTrond Myklebust 
16915ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
16925ceb9d7fSTrond Myklebust 		return -ECHILD;
16935ceb9d7fSTrond Myklebust 
16945ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
16955ceb9d7fSTrond Myklebust 		goto out_bad;
16965ceb9d7fSTrond Myklebust 
16975ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
16982eef8a31STrond Myklebust 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
16995ceb9d7fSTrond Myklebust out_valid:
17005ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
17015ceb9d7fSTrond Myklebust out_bad:
17025ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
17035ceb9d7fSTrond Myklebust 		return -ECHILD;
17045ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
17055ceb9d7fSTrond Myklebust }
17065ceb9d7fSTrond Myklebust 
17075ceb9d7fSTrond Myklebust static int
1708c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1709c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
17105ceb9d7fSTrond Myklebust {
17115ceb9d7fSTrond Myklebust 	struct dentry *parent;
17125ceb9d7fSTrond Myklebust 	struct inode *dir;
17135ceb9d7fSTrond Myklebust 	int ret;
17145ceb9d7fSTrond Myklebust 
17155ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
17165ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
17175ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
17185ceb9d7fSTrond Myklebust 		if (!dir)
17195ceb9d7fSTrond Myklebust 			return -ECHILD;
1720c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
17215ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
17225ceb9d7fSTrond Myklebust 			return -ECHILD;
17235ceb9d7fSTrond Myklebust 	} else {
17245ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1725c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
17265ceb9d7fSTrond Myklebust 		dput(parent);
17275ceb9d7fSTrond Myklebust 	}
17285ceb9d7fSTrond Myklebust 	return ret;
17295ceb9d7fSTrond Myklebust }
17305ceb9d7fSTrond Myklebust 
1731c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1732c7944ebbSTrond Myklebust {
1733c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1734c7944ebbSTrond Myklebust }
1735c7944ebbSTrond Myklebust 
17361da177e4SLinus Torvalds /*
17372b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1738ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1739ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1740ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1741ecf3d1f1SJeff Layton  *
1742ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1743ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1744ecf3d1f1SJeff Layton  */
1745ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1746ecf3d1f1SJeff Layton {
17472b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
17489cdd1d3fSTrond Myklebust 	int error = 0;
1749ecf3d1f1SJeff Layton 
1750ecf3d1f1SJeff Layton 	/*
1751ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1752ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1753ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1754ecf3d1f1SJeff Layton 	 */
1755ecf3d1f1SJeff Layton 	if (!inode) {
17566de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
17576de1472fSAl Viro 				__func__, dentry);
1758ecf3d1f1SJeff Layton 		return 1;
1759ecf3d1f1SJeff Layton 	}
1760ecf3d1f1SJeff Layton 
1761ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
17626de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
17636de1472fSAl Viro 				__func__, dentry);
1764ecf3d1f1SJeff Layton 		return 0;
1765ecf3d1f1SJeff Layton 	}
1766ecf3d1f1SJeff Layton 
1767b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1768ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1769ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1770ecf3d1f1SJeff Layton 	return !error;
1771ecf3d1f1SJeff Layton }
1772ecf3d1f1SJeff Layton 
1773ecf3d1f1SJeff Layton /*
17741da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
17751da177e4SLinus Torvalds  */
1776fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
17771da177e4SLinus Torvalds {
17786de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
17796de1472fSAl Viro 		dentry, dentry->d_flags);
17801da177e4SLinus Torvalds 
178177f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
17822b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
178377f11192STrond Myklebust 		return 1;
178477f11192STrond Myklebust 
17851da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17861da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
17871da177e4SLinus Torvalds 		return 1;
17881da177e4SLinus Torvalds 	}
17891751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
17901da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
17911da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
17921da177e4SLinus Torvalds 		return 1;
17931da177e4SLinus Torvalds 	}
17941da177e4SLinus Torvalds 	return 0;
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
17981f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
17991b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
18001b83d707STrond Myklebust {
18011b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
18021f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
180359a707b0STrond Myklebust 	if (inode->i_nlink > 0)
180459a707b0STrond Myklebust 		drop_nlink(inode);
180559a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1806ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(
1807ac46b3d7STrond Myklebust 		inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
18081301e421STrond Myklebust 			       NFS_INO_INVALID_NLINK);
18091b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
18101b83d707STrond Myklebust }
18111b83d707STrond Myklebust 
18121da177e4SLinus Torvalds /*
18131da177e4SLinus Torvalds  * Called when the dentry loses inode.
18141da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
18151da177e4SLinus Torvalds  */
18161da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
18171da177e4SLinus Torvalds {
18181da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1819e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
18201f018458STrond Myklebust 		nfs_drop_nlink(inode);
18211da177e4SLinus Torvalds 	}
18221da177e4SLinus Torvalds 	iput(inode);
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
1825b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1826b1942c5fSAl Viro {
1827b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1828b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1829b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1830b1942c5fSAl Viro 			WARN_ON(1);
1831b1942c5fSAl Viro 		else
1832b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1833b1942c5fSAl Viro 	}
1834b1942c5fSAl Viro }
1835b1942c5fSAl Viro 
1836f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
18371da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1838ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
18391da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
18401da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
184136d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1842b1942c5fSAl Viro 	.d_release	= nfs_d_release,
18431da177e4SLinus Torvalds };
1844ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
18451da177e4SLinus Torvalds 
1846597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	struct dentry *res;
18491da177e4SLinus Torvalds 	struct inode *inode = NULL;
1850e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1851e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
1852a1147b82STrond Myklebust 	unsigned long dir_verifier;
18531da177e4SLinus Torvalds 	int error;
18541da177e4SLinus Torvalds 
18556de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
185691d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
18571da177e4SLinus Torvalds 
1858130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1859130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
18601da177e4SLinus Torvalds 
1861fd684071STrond Myklebust 	/*
1862fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1863fd684071STrond Myklebust 	 * but don't hash the dentry.
1864fd684071STrond Myklebust 	 */
18659f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1866130f9ab7SAl Viro 		return NULL;
18671da177e4SLinus Torvalds 
1868e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1869e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
18709558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1871e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1872e1fb4d05STrond Myklebust 		goto out;
1873e1fb4d05STrond Myklebust 
1874a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
18756e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
18769558a007SAnna Schumaker 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
18778ce37abdSTrond Myklebust 	if (error == -ENOENT) {
18788ce37abdSTrond Myklebust 		if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
18798ce37abdSTrond Myklebust 			dir_verifier = inode_peek_iversion_raw(dir);
18801da177e4SLinus Torvalds 		goto no_entry;
18818ce37abdSTrond Myklebust 	}
18821da177e4SLinus Torvalds 	if (error < 0) {
18831da177e4SLinus Torvalds 		res = ERR_PTR(error);
18849558a007SAnna Schumaker 		goto out;
18851da177e4SLinus Torvalds 	}
1886cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1887bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
188803f28e3aSTrond Myklebust 	if (IS_ERR(res))
18899558a007SAnna Schumaker 		goto out;
189054ceac45SDavid Howells 
189163519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
189263519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1893d69ee9b8STrond Myklebust 
18941da177e4SLinus Torvalds no_entry:
189541d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
18969eaef27bSTrond Myklebust 	if (res != NULL) {
18979eaef27bSTrond Myklebust 		if (IS_ERR(res))
18989558a007SAnna Schumaker 			goto out;
18991da177e4SLinus Torvalds 		dentry = res;
19009eaef27bSTrond Myklebust 	}
1901a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
19021da177e4SLinus Torvalds out:
19039558a007SAnna Schumaker 	trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1904e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1905e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
19061da177e4SLinus Torvalds 	return res;
19071da177e4SLinus Torvalds }
1908ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
19091da177e4SLinus Torvalds 
191000bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
191100bdadc7STrond Myklebust {
191200bdadc7STrond Myklebust 	/* Case insensitive server? Revalidate dentries */
191300bdadc7STrond Myklebust 	if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
191400bdadc7STrond Myklebust 		d_prune_aliases(inode);
191500bdadc7STrond Myklebust }
191600bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
191700bdadc7STrond Myklebust 
191889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
19190b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
19201da177e4SLinus Torvalds 
1921f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
19220ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1923b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
19241da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
19251da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
192636d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1927b1942c5fSAl Viro 	.d_release	= nfs_d_release,
19281da177e4SLinus Torvalds };
192989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
19301da177e4SLinus Torvalds 
19318a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
19328a5e929dSAl Viro {
19338a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
19348a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
19358a5e929dSAl Viro 		res |= FMODE_READ;
19368a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
19378a5e929dSAl Viro 		res |= FMODE_WRITE;
19388a5e929dSAl Viro 	return res;
19398a5e929dSAl Viro }
19408a5e929dSAl Viro 
1941532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1942cd9a1c0eSTrond Myklebust {
1943532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1944cd9a1c0eSTrond Myklebust }
1945cd9a1c0eSTrond Myklebust 
1946cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1947cd9a1c0eSTrond Myklebust {
1948f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1949cd9a1c0eSTrond Myklebust 	return 0;
1950cd9a1c0eSTrond Myklebust }
1951cd9a1c0eSTrond Myklebust 
1952d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
19530dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1954b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1955cd9a1c0eSTrond Myklebust {
19560dd2b474SMiklos Szeredi 	int err;
19570dd2b474SMiklos Szeredi 
1958be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
195930d90494SAl Viro 	if (err)
1960d9585277SAl Viro 		goto out;
1961eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
196230d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1963eaa2b82cSNeilBrown 	else
19649821421aSTrond Myklebust 		err = -EOPENSTALE;
1965cd9a1c0eSTrond Myklebust out:
1966d9585277SAl Viro 	return err;
1967cd9a1c0eSTrond Myklebust }
1968cd9a1c0eSTrond Myklebust 
196973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
197030d90494SAl Viro 		    struct file *file, unsigned open_flags,
197144907d79SAl Viro 		    umode_t mode)
19721da177e4SLinus Torvalds {
1973c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1974cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
19750dd2b474SMiklos Szeredi 	struct dentry *res;
19760dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1977f46e0bd3STrond Myklebust 	struct inode *inode;
19781472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
197968eaba4cSTrond Myklebust 	unsigned long dir_verifier;
1980c94c0953SAl Viro 	bool switched = false;
198173a09dd9SAl Viro 	int created = 0;
1982898f635cSTrond Myklebust 	int err;
19831da177e4SLinus Torvalds 
19840dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
19852b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
19860dd2b474SMiklos Szeredi 
19871e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
19886de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19891e7cb3dcSChuck Lever 
19909597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
19919597c13bSJeff Layton 	if (err)
19929597c13bSJeff Layton 		return err;
19939597c13bSJeff Layton 
19940dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
19950dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
199600699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
19970dd2b474SMiklos Szeredi 			/*
19980dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
19990dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
20000dd2b474SMiklos Szeredi 			 * again
20010dd2b474SMiklos Szeredi 			 */
2002d9585277SAl Viro 			return -ENOENT;
20030dd2b474SMiklos Szeredi 		}
20041472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
20051da177e4SLinus Torvalds 		goto no_open;
20061da177e4SLinus Torvalds 	}
20071da177e4SLinus Torvalds 
20080dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
2009d9585277SAl Viro 		return -ENAMETOOLONG;
20101da177e4SLinus Torvalds 
20110dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
2012dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
2013dff25ddbSAndreas Gruenbacher 
2014dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
2015dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
2016dff25ddbSAndreas Gruenbacher 
2017536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
2018dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
20190dd2b474SMiklos Szeredi 	}
2020536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
2021536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
2022536e43d1STrond Myklebust 		attr.ia_size = 0;
2023cd9a1c0eSTrond Myklebust 	}
2024cd9a1c0eSTrond Myklebust 
2025c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
2026c94c0953SAl Viro 		d_drop(dentry);
2027c94c0953SAl Viro 		switched = true;
2028c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
2029c94c0953SAl Viro 					  &dentry->d_name, &wq);
2030c94c0953SAl Viro 		if (IS_ERR(dentry))
2031c94c0953SAl Viro 			return PTR_ERR(dentry);
2032c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
2033c94c0953SAl Viro 			return finish_no_open(file, dentry);
2034c94c0953SAl Viro 	}
2035c94c0953SAl Viro 
2036532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
20370dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
20380dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
2039d9585277SAl Viro 		goto out;
20400dd2b474SMiklos Szeredi 
20416e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
204273a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
204373a09dd9SAl Viro 	if (created)
204473a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
2045275bb307STrond Myklebust 	if (IS_ERR(inode)) {
20460dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
20476e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
20482d9db750STrond Myklebust 		put_nfs_open_context(ctx);
2049d20cb71dSAl Viro 		d_drop(dentry);
20500dd2b474SMiklos Szeredi 		switch (err) {
20511da177e4SLinus Torvalds 		case -ENOENT:
2052774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
205368eaba4cSTrond Myklebust 			if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
205468eaba4cSTrond Myklebust 				dir_verifier = inode_peek_iversion_raw(dir);
205568eaba4cSTrond Myklebust 			else
205668eaba4cSTrond Myklebust 				dir_verifier = nfs_save_change_attribute(dir);
205768eaba4cSTrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
20580dd2b474SMiklos Szeredi 			break;
20591788ea6eSJeff Layton 		case -EISDIR:
20606f926b5bSTrond Myklebust 		case -ENOTDIR:
20616f926b5bSTrond Myklebust 			goto no_open;
20621da177e4SLinus Torvalds 		case -ELOOP:
20630dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
20641da177e4SLinus Torvalds 				goto no_open;
20650dd2b474SMiklos Szeredi 			break;
20661da177e4SLinus Torvalds 			/* case -EINVAL: */
20671da177e4SLinus Torvalds 		default:
20680dd2b474SMiklos Szeredi 			break;
20691da177e4SLinus Torvalds 		}
20701da177e4SLinus Torvalds 		goto out;
20711da177e4SLinus Torvalds 	}
20720dd2b474SMiklos Szeredi 
2073b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
20746e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
20752d9db750STrond Myklebust 	put_nfs_open_context(ctx);
2076d9585277SAl Viro out:
2077c94c0953SAl Viro 	if (unlikely(switched)) {
2078c94c0953SAl Viro 		d_lookup_done(dentry);
2079c94c0953SAl Viro 		dput(dentry);
2080c94c0953SAl Viro 	}
2081d9585277SAl Viro 	return err;
20820dd2b474SMiklos Szeredi 
20831da177e4SLinus Torvalds no_open:
20841472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
2085ac795161STrond Myklebust 	if (!res) {
2086ac795161STrond Myklebust 		inode = d_inode(dentry);
2087ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2088e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2089ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
20901751fc1dSTrond Myklebust 		else if (inode && S_ISREG(inode->i_mode))
20911751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2092ac795161STrond Myklebust 	} else if (!IS_ERR(res)) {
2093ac795161STrond Myklebust 		inode = d_inode(res);
2094ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2095e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2096ac795161STrond Myklebust 			dput(res);
2097ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
20981751fc1dSTrond Myklebust 		} else if (inode && S_ISREG(inode->i_mode)) {
20991751fc1dSTrond Myklebust 			dput(res);
21001751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2101ac795161STrond Myklebust 		}
2102ac795161STrond Myklebust 	}
2103c94c0953SAl Viro 	if (switched) {
2104c94c0953SAl Viro 		d_lookup_done(dentry);
2105c94c0953SAl Viro 		if (!res)
2106c94c0953SAl Viro 			res = dentry;
2107c94c0953SAl Viro 		else
2108c94c0953SAl Viro 			dput(dentry);
2109c94c0953SAl Viro 	}
21100dd2b474SMiklos Szeredi 	if (IS_ERR(res))
2111c94c0953SAl Viro 		return PTR_ERR(res);
2112e45198a6SAl Viro 	return finish_no_open(file, res);
21131da177e4SLinus Torvalds }
211489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
21151da177e4SLinus Torvalds 
2116c7944ebbSTrond Myklebust static int
2117c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2118c7944ebbSTrond Myklebust 			  unsigned int flags)
21191da177e4SLinus Torvalds {
2120657e94b6SNick Piggin 	struct inode *inode;
21211da177e4SLinus Torvalds 
2122fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2123c7944ebbSTrond Myklebust 		goto full_reval;
2124eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
2125c7944ebbSTrond Myklebust 		goto full_reval;
21262b484297STrond Myklebust 
21272b0143b5SDavid Howells 	inode = d_inode(dentry);
21282b484297STrond Myklebust 
21291da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
21301da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
21311da177e4SLinus Torvalds 	 */
2132c7944ebbSTrond Myklebust 	if (inode == NULL)
2133c7944ebbSTrond Myklebust 		goto full_reval;
213449317a7fSNeilBrown 
2135efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
2136c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2137216d5d06STrond Myklebust 
21381da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
21391da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
2140c7944ebbSTrond Myklebust 		goto full_reval;
2141c7944ebbSTrond Myklebust 
21421da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
2143c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2144c7944ebbSTrond Myklebust 		goto reval_dentry;
2145c7944ebbSTrond Myklebust 
2146c7944ebbSTrond Myklebust 	/* Check if the directory changed */
2147c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2148c7944ebbSTrond Myklebust 		goto reval_dentry;
21491da177e4SLinus Torvalds 
21500ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
2151c7944ebbSTrond Myklebust 	return 1;
2152c7944ebbSTrond Myklebust reval_dentry:
2153c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
2154c7944ebbSTrond Myklebust 		return -ECHILD;
215542f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
21560ef97dcfSMiklos Szeredi 
2157c7944ebbSTrond Myklebust full_reval:
2158c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
2159c7944ebbSTrond Myklebust }
2160535918f1STrond Myklebust 
2161c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2162c7944ebbSTrond Myklebust {
2163c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
2164c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
2165c0204fd2STrond Myklebust }
2166c0204fd2STrond Myklebust 
21671da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
21681da177e4SLinus Torvalds 
2169406cd915SBenjamin Coddington struct dentry *
2170406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2171cc6f3298SAnna Schumaker 				struct nfs_fattr *fattr)
21721da177e4SLinus Torvalds {
2173fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
21742b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
21751da177e4SLinus Torvalds 	struct inode *inode;
2176b0c6108eSAl Viro 	struct dentry *d;
2177406cd915SBenjamin Coddington 	int error;
21781da177e4SLinus Torvalds 
2179fab728e1STrond Myklebust 	d_drop(dentry);
2180fab728e1STrond Myklebust 
21811da177e4SLinus Torvalds 	if (fhandle->size == 0) {
21829558a007SAnna Schumaker 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
21831da177e4SLinus Torvalds 		if (error)
2184fab728e1STrond Myklebust 			goto out_error;
21851da177e4SLinus Torvalds 	}
21865724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
21871da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
21881da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2189a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
21902ef61e0eSAnna Schumaker 				fattr, NULL);
21911da177e4SLinus Torvalds 		if (error < 0)
2192fab728e1STrond Myklebust 			goto out_error;
21931da177e4SLinus Torvalds 	}
2194cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2195b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
2196fab728e1STrond Myklebust out:
2197fab728e1STrond Myklebust 	dput(parent);
2198406cd915SBenjamin Coddington 	return d;
2199fab728e1STrond Myklebust out_error:
2200406cd915SBenjamin Coddington 	d = ERR_PTR(error);
2201406cd915SBenjamin Coddington 	goto out;
2202406cd915SBenjamin Coddington }
2203406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2204406cd915SBenjamin Coddington 
2205406cd915SBenjamin Coddington /*
2206406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
2207406cd915SBenjamin Coddington  */
2208406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2209d91bfc46SAnna Schumaker 				struct nfs_fattr *fattr)
2210406cd915SBenjamin Coddington {
2211406cd915SBenjamin Coddington 	struct dentry *d;
2212406cd915SBenjamin Coddington 
2213cc6f3298SAnna Schumaker 	d = nfs_add_or_obtain(dentry, fhandle, fattr);
2214406cd915SBenjamin Coddington 	if (IS_ERR(d))
2215406cd915SBenjamin Coddington 		return PTR_ERR(d);
2216406cd915SBenjamin Coddington 
2217406cd915SBenjamin Coddington 	/* Callers don't care */
2218406cd915SBenjamin Coddington 	dput(d);
2219406cd915SBenjamin Coddington 	return 0;
22201da177e4SLinus Torvalds }
2221ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds /*
22241da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
22251da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
22261da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
22271da177e4SLinus Torvalds  * reply path made it appear to have failed.
22281da177e4SLinus Torvalds  */
2229549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2230549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, bool excl)
22311da177e4SLinus Torvalds {
22321da177e4SLinus Torvalds 	struct iattr attr;
2233ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
22341da177e4SLinus Torvalds 	int error;
22351da177e4SLinus Torvalds 
22361e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
22376de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	attr.ia_mode = mode;
22401da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
22411da177e4SLinus Torvalds 
22428b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
22438867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
22448b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
22451da177e4SLinus Torvalds 	if (error != 0)
22461da177e4SLinus Torvalds 		goto out_err;
22471da177e4SLinus Torvalds 	return 0;
22481da177e4SLinus Torvalds out_err:
22491da177e4SLinus Torvalds 	d_drop(dentry);
22501da177e4SLinus Torvalds 	return error;
22511da177e4SLinus Torvalds }
2252ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds /*
22551da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
22561da177e4SLinus Torvalds  */
2257597d9289SBryan Schumaker int
2258549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2259549c7297SChristian Brauner 	  struct dentry *dentry, umode_t mode, dev_t rdev)
22601da177e4SLinus Torvalds {
22611da177e4SLinus Torvalds 	struct iattr attr;
22621da177e4SLinus Torvalds 	int status;
22631da177e4SLinus Torvalds 
22641e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
22656de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	attr.ia_mode = mode;
22681da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
22691da177e4SLinus Torvalds 
22701ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
22711da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
22721ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
22731da177e4SLinus Torvalds 	if (status != 0)
22741da177e4SLinus Torvalds 		goto out_err;
22751da177e4SLinus Torvalds 	return 0;
22761da177e4SLinus Torvalds out_err:
22771da177e4SLinus Torvalds 	d_drop(dentry);
22781da177e4SLinus Torvalds 	return status;
22791da177e4SLinus Torvalds }
2280ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds /*
22831da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
22841da177e4SLinus Torvalds  */
2285549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2286549c7297SChristian Brauner 	      struct dentry *dentry, umode_t mode)
22871da177e4SLinus Torvalds {
22881da177e4SLinus Torvalds 	struct iattr attr;
22891da177e4SLinus Torvalds 	int error;
22901da177e4SLinus Torvalds 
22911e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
22926de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
22951da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
22961da177e4SLinus Torvalds 
22971ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
22981da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
22991ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
23001da177e4SLinus Torvalds 	if (error != 0)
23011da177e4SLinus Torvalds 		goto out_err;
23021da177e4SLinus Torvalds 	return 0;
23031da177e4SLinus Torvalds out_err:
23041da177e4SLinus Torvalds 	d_drop(dentry);
23051da177e4SLinus Torvalds 	return error;
23061da177e4SLinus Torvalds }
2307ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
23081da177e4SLinus Torvalds 
2309d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2310d45b9d8bSTrond Myklebust {
2311dc3f4198SAl Viro 	if (simple_positive(dentry))
2312d45b9d8bSTrond Myklebust 		d_delete(dentry);
2313d45b9d8bSTrond Myklebust }
2314d45b9d8bSTrond Myklebust 
23159019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir,
23169019fb39STrond Myklebust 					   struct dentry *dentry, int error)
23179019fb39STrond Myklebust {
23189019fb39STrond Myklebust 	switch (error) {
23199019fb39STrond Myklebust 	case -ENOENT:
23209019fb39STrond Myklebust 		d_delete(dentry);
232100bdadc7STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
232200bdadc7STrond Myklebust 		break;
23239019fb39STrond Myklebust 	case 0:
232400bdadc7STrond Myklebust 		nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
23259019fb39STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
23269019fb39STrond Myklebust 	}
23279019fb39STrond Myklebust }
23289019fb39STrond Myklebust 
2329597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
23301da177e4SLinus Torvalds {
23311da177e4SLinus Torvalds 	int error;
23321da177e4SLinus Torvalds 
23331e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
23346de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
23351da177e4SLinus Torvalds 
23361ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
23372b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2338884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
23391da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
23401da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2341ba6c0592STrond Myklebust 		switch (error) {
2342ba6c0592STrond Myklebust 		case 0:
23432b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2344ba6c0592STrond Myklebust 			break;
2345ba6c0592STrond Myklebust 		case -ENOENT:
2346d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2347ba6c0592STrond Myklebust 		}
2348884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2349ba6c0592STrond Myklebust 	} else
2350ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
23519019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
23521ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	return error;
23551da177e4SLinus Torvalds }
2356ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds /*
23591da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
23601da177e4SLinus Torvalds  * and after checking that the file has only one user.
23611da177e4SLinus Torvalds  *
23621da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
23631da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
23641da177e4SLinus Torvalds  */
23651da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
23661da177e4SLinus Torvalds {
23672b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
23682b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
23691da177e4SLinus Torvalds 	int error = -EBUSY;
23701da177e4SLinus Torvalds 
23716de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
23741da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
23751da177e4SLinus Torvalds 		error = 0;
23761da177e4SLinus Torvalds 		goto out;
23771da177e4SLinus Torvalds 	}
23781da177e4SLinus Torvalds 
23791ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
23801da177e4SLinus Torvalds 	if (inode != NULL) {
2381912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
23821da177e4SLinus Torvalds 		if (error == 0)
23831b83d707STrond Myklebust 			nfs_drop_nlink(inode);
23841da177e4SLinus Torvalds 	} else
2385912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2386d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2387d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
23881ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
23891da177e4SLinus Torvalds out:
23901da177e4SLinus Torvalds 	return error;
23911da177e4SLinus Torvalds }
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
23941da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
23951da177e4SLinus Torvalds  *
23961da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
23971da177e4SLinus Torvalds  */
2398597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
23991da177e4SLinus Torvalds {
24001da177e4SLinus Torvalds 	int error;
24011da177e4SLinus Torvalds 	int need_rehash = 0;
24021da177e4SLinus Torvalds 
24031e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
24046de1472fSAl Viro 		dir->i_ino, dentry);
24051da177e4SLinus Torvalds 
24061ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
24071da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
240843245ecaSOlga Kornievskaia 	if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
240943245ecaSOlga Kornievskaia 					     &NFS_I(d_inode(dentry))->flags)) {
24101da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2411ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
24122b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
24131da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
24141ca42382STrond Myklebust 		goto out;
24151da177e4SLinus Torvalds 	}
24161da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
24171da177e4SLinus Torvalds 		__d_drop(dentry);
24181da177e4SLinus Torvalds 		need_rehash = 1;
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
24211da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
24229019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
24239019fb39STrond Myklebust 	if (need_rehash)
24241da177e4SLinus Torvalds 		d_rehash(dentry);
24251ca42382STrond Myklebust out:
24261ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
24271da177e4SLinus Torvalds 	return error;
24281da177e4SLinus Torvalds }
2429ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
24301da177e4SLinus Torvalds 
2431873101b3SChuck Lever /*
2432873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2433873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2434873101b3SChuck Lever  * using prepare_write/commit_write.
2435873101b3SChuck Lever  *
2436873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2437873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2438873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2439873101b3SChuck Lever  * symlink request has completed on the server.
2440873101b3SChuck Lever  *
2441873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2442873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2443873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2444873101b3SChuck Lever  * and move the raw page into its mapping.
2445873101b3SChuck Lever  */
2446549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2447549c7297SChristian Brauner 		struct dentry *dentry, const char *symname)
24481da177e4SLinus Torvalds {
2449873101b3SChuck Lever 	struct page *page;
2450873101b3SChuck Lever 	char *kaddr;
24511da177e4SLinus Torvalds 	struct iattr attr;
2452873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
24531da177e4SLinus Torvalds 	int error;
24541da177e4SLinus Torvalds 
24551e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
24566de1472fSAl Viro 		dir->i_ino, dentry, symname);
24571da177e4SLinus Torvalds 
2458873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2459873101b3SChuck Lever 		return -ENAMETOOLONG;
24601da177e4SLinus Torvalds 
2461873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2462873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
24631da177e4SLinus Torvalds 
2464e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
246576566991STrond Myklebust 	if (!page)
2466873101b3SChuck Lever 		return -ENOMEM;
2467873101b3SChuck Lever 
2468e8ecde25SAl Viro 	kaddr = page_address(page);
2469873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2470873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2471873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2472873101b3SChuck Lever 
24731ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
247494a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
24751ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2476873101b3SChuck Lever 	if (error != 0) {
24771e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2478873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
24796de1472fSAl Viro 			dentry, symname, error);
24801da177e4SLinus Torvalds 		d_drop(dentry);
2481873101b3SChuck Lever 		__free_page(page);
24821da177e4SLinus Torvalds 		return error;
24831da177e4SLinus Torvalds 	}
24841da177e4SLinus Torvalds 
2485342a67f0STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2486342a67f0STrond Myklebust 
2487873101b3SChuck Lever 	/*
2488873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2489873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2490873101b3SChuck Lever 	 */
24912b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2492873101b3SChuck Lever 							GFP_KERNEL)) {
2493873101b3SChuck Lever 		SetPageUptodate(page);
2494873101b3SChuck Lever 		unlock_page(page);
2495a0b54addSRafael Aquini 		/*
2496a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2497a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2498a0b54addSRafael Aquini 		 */
249909cbfeafSKirill A. Shutemov 		put_page(page);
2500873101b3SChuck Lever 	} else
2501873101b3SChuck Lever 		__free_page(page);
2502873101b3SChuck Lever 
2503873101b3SChuck Lever 	return 0;
2504873101b3SChuck Lever }
2505ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2506873101b3SChuck Lever 
2507597d9289SBryan Schumaker int
25081da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
25091da177e4SLinus Torvalds {
25102b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
25111da177e4SLinus Torvalds 	int error;
25121da177e4SLinus Torvalds 
25136de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
25146de1472fSAl Viro 		old_dentry, dentry);
25151da177e4SLinus Torvalds 
25161fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
25179697d234STrond Myklebust 	d_drop(dentry);
251820497503STrond Myklebust 	if (S_ISREG(inode->i_mode))
251920497503STrond Myklebust 		nfs_sync_inode(inode);
25201da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2521cf809556STrond Myklebust 	if (error == 0) {
2522342a67f0STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
25237de9c6eeSAl Viro 		ihold(inode);
25249697d234STrond Myklebust 		d_add(dentry, inode);
2525cf809556STrond Myklebust 	}
25261fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
25271da177e4SLinus Torvalds 	return error;
25281da177e4SLinus Torvalds }
2529ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds /*
25321da177e4SLinus Torvalds  * RENAME
25331da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
25341da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
25351da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
25361da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
25371da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
25381da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
25391da177e4SLinus Torvalds  *
25401da177e4SLinus Torvalds  * FIXED.
25411da177e4SLinus Torvalds  *
25421da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
25431da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
25441da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
25451da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
25461da177e4SLinus Torvalds  * using the inode layer
25471da177e4SLinus Torvalds  *
25481da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
25491da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
25501da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
25511da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
25521da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
25531da177e4SLinus Torvalds  * the rename.
25541da177e4SLinus Torvalds  */
2555549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2556549c7297SChristian Brauner 	       struct dentry *old_dentry, struct inode *new_dir,
2557549c7297SChristian Brauner 	       struct dentry *new_dentry, unsigned int flags)
25581da177e4SLinus Torvalds {
25592b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
25602b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2561d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
256280a491fdSJeff Layton 	struct rpc_task *task;
25631da177e4SLinus Torvalds 	int error = -EBUSY;
25641da177e4SLinus Torvalds 
25651cd66c93SMiklos Szeredi 	if (flags)
25661cd66c93SMiklos Szeredi 		return -EINVAL;
25671cd66c93SMiklos Szeredi 
25686de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
25696de1472fSAl Viro 		 old_dentry, new_dentry,
257084d08fa8SAl Viro 		 d_count(new_dentry));
25711da177e4SLinus Torvalds 
257270ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
25731da177e4SLinus Torvalds 	/*
257428f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
257528f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
257628f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
257728f79a1aSMiklos Szeredi 	 * the new target.
25781da177e4SLinus Torvalds 	 */
257927226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
258027226104SMiklos Szeredi 		/*
258127226104SMiklos Szeredi 		 * To prevent any new references to the target during the
258227226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
258327226104SMiklos Szeredi 		 */
2584d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
258527226104SMiklos Szeredi 			d_drop(new_dentry);
2586d9f29500SBenjamin Coddington 			rehash = new_dentry;
2587d9f29500SBenjamin Coddington 		}
258827226104SMiklos Szeredi 
258984d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
25901da177e4SLinus Torvalds 			int err;
259127226104SMiklos Szeredi 
25921da177e4SLinus Torvalds 			/* copy the target dentry's name */
25931da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
25941da177e4SLinus Torvalds 					 &new_dentry->d_name);
25951da177e4SLinus Torvalds 			if (!dentry)
25961da177e4SLinus Torvalds 				goto out;
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
25991da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
260024e93025SMiklos Szeredi 			if (err)
26011da177e4SLinus Torvalds 				goto out;
260224e93025SMiklos Szeredi 
260324e93025SMiklos Szeredi 			new_dentry = dentry;
2604d9f29500SBenjamin Coddington 			rehash = NULL;
260524e93025SMiklos Szeredi 			new_inode = NULL;
2606b1e4adf4STrond Myklebust 		}
260727226104SMiklos Szeredi 	}
26081da177e4SLinus Torvalds 
26096ff9d99bSTrond Myklebust 	if (S_ISREG(old_inode->i_mode))
26106ff9d99bSTrond Myklebust 		nfs_sync_inode(old_inode);
2611d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
261280a491fdSJeff Layton 	if (IS_ERR(task)) {
261380a491fdSJeff Layton 		error = PTR_ERR(task);
261480a491fdSJeff Layton 		goto out;
261580a491fdSJeff Layton 	}
261680a491fdSJeff Layton 
261780a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2618818a8dbeSBenjamin Coddington 	if (error != 0) {
2619818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2620818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2621818a8dbeSBenjamin Coddington 		smp_wmb();
2622818a8dbeSBenjamin Coddington 	} else
262380a491fdSJeff Layton 		error = task->tk_status;
262480a491fdSJeff Layton 	rpc_put_task(task);
262559a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
262659a707b0STrond Myklebust 	if (error == 0) {
262759a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
262859a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2629ac46b3d7STrond Myklebust 		nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2630ac46b3d7STrond Myklebust 							 NFS_INO_INVALID_CTIME |
2631ac46b3d7STrond Myklebust 							 NFS_INO_REVAL_FORCED);
263259a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
263359a707b0STrond Myklebust 	}
26341da177e4SLinus Torvalds out:
2635d9f29500SBenjamin Coddington 	if (rehash)
2636d9f29500SBenjamin Coddington 		d_rehash(rehash);
263770ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
263870ded201STrond Myklebust 			new_dir, new_dentry, error);
2639d9f29500SBenjamin Coddington 	if (!error) {
2640d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2641d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2642d9f29500SBenjamin Coddington 		/*
2643d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2644d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2645d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2646d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2647d9f29500SBenjamin Coddington 		 */
2648d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2649d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2650d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2651d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2652d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2653d9f29500SBenjamin Coddington 
26541da177e4SLinus Torvalds 	/* new dentry created? */
26551da177e4SLinus Torvalds 	if (dentry)
26561da177e4SLinus Torvalds 		dput(dentry);
26571da177e4SLinus Torvalds 	return error;
26581da177e4SLinus Torvalds }
2659ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
26601da177e4SLinus Torvalds 
2661cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2662cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2663cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2664cfcea3e8STrond Myklebust 
2665a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
26663a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
26673a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
26683a505845STrond Myklebust 
26691c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
26701c3c07e9STrond Myklebust {
26716238aec8SNeilBrown 	put_group_info(entry->group_info);
2672f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
26734e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2674cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
26754e857c58SPeter Zijlstra 	smp_mb__after_atomic();
26761c3c07e9STrond Myklebust }
26771c3c07e9STrond Myklebust 
26781a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
26791a81bb8aSTrond Myklebust {
26801a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
26811a81bb8aSTrond Myklebust 
26821a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
26831a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
26841a81bb8aSTrond Myklebust 		list_del(&cache->lru);
26851a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
26861a81bb8aSTrond Myklebust 	}
26871a81bb8aSTrond Myklebust }
26881a81bb8aSTrond Myklebust 
26893a505845STrond Myklebust static unsigned long
26903a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2691979df72eSTrond Myklebust {
2692979df72eSTrond Myklebust 	LIST_HEAD(head);
2693aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2694979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
26951ab6c499SDave Chinner 	long freed = 0;
2696979df72eSTrond Myklebust 
2697a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2698aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2699979df72eSTrond Myklebust 		struct inode *inode;
2700979df72eSTrond Myklebust 
2701979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2702979df72eSTrond Myklebust 			break;
27039c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2704979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2705979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2706979df72eSTrond Myklebust 			goto remove_lru_entry;
2707979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2708979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2709979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2710979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
27111ab6c499SDave Chinner 		freed++;
2712979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2713979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2714979df72eSTrond Myklebust 					&nfs_access_lru_list);
2715979df72eSTrond Myklebust 		else {
2716979df72eSTrond Myklebust remove_lru_entry:
2717979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
27184e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2719979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
27204e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2721979df72eSTrond Myklebust 		}
272259844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2723979df72eSTrond Myklebust 	}
2724979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
27251a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
27261ab6c499SDave Chinner 	return freed;
27271ab6c499SDave Chinner }
27281ab6c499SDave Chinner 
27291ab6c499SDave Chinner unsigned long
27303a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
27313a505845STrond Myklebust {
27323a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
27333a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
27343a505845STrond Myklebust 
27353a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
27363a505845STrond Myklebust 		return SHRINK_STOP;
27373a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
27383a505845STrond Myklebust }
27393a505845STrond Myklebust 
27403a505845STrond Myklebust 
27413a505845STrond Myklebust unsigned long
27421ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
27431ab6c499SDave Chinner {
274455f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2745979df72eSTrond Myklebust }
2746979df72eSTrond Myklebust 
27473a505845STrond Myklebust static void
27483a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
27493a505845STrond Myklebust {
27503a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
27513a505845STrond Myklebust 	unsigned long diff;
27523a505845STrond Myklebust 	unsigned int nr_to_scan;
27533a505845STrond Myklebust 
27543a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
27553a505845STrond Myklebust 		return;
27563a505845STrond Myklebust 	nr_to_scan = 100;
27573a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
27583a505845STrond Myklebust 	if (diff < nr_to_scan)
27593a505845STrond Myklebust 		nr_to_scan = diff;
27603a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
27613a505845STrond Myklebust }
27623a505845STrond Myklebust 
27631a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
27641c3c07e9STrond Myklebust {
27651c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
27661a81bb8aSTrond Myklebust 	struct rb_node *n;
27671c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
27681c3c07e9STrond Myklebust 
27691c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
27701c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
27711c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
27721c3c07e9STrond Myklebust 		rb_erase(n, root_node);
27731a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
27741c3c07e9STrond Myklebust 	}
27751c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
27761c3c07e9STrond Myklebust }
27771c3c07e9STrond Myklebust 
27781c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
27791c3c07e9STrond Myklebust {
27801a81bb8aSTrond Myklebust 	LIST_HEAD(head);
27811a81bb8aSTrond Myklebust 
27821a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
27831a81bb8aSTrond Myklebust 		return;
2784cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2785cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
27861a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2787cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2788cfcea3e8STrond Myklebust 
27891c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
27901a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
27911a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
27921a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
27931a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
27941c3c07e9STrond Myklebust }
27951c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
27961c3c07e9STrond Myklebust 
27976238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
27986238aec8SNeilBrown {
27996238aec8SNeilBrown 	struct group_info *ga, *gb;
28006238aec8SNeilBrown 	int g;
28016238aec8SNeilBrown 
28026238aec8SNeilBrown 	if (uid_lt(a->fsuid, b->fsuid))
28036238aec8SNeilBrown 		return -1;
28046238aec8SNeilBrown 	if (uid_gt(a->fsuid, b->fsuid))
28056238aec8SNeilBrown 		return 1;
28066238aec8SNeilBrown 
28076238aec8SNeilBrown 	if (gid_lt(a->fsgid, b->fsgid))
28086238aec8SNeilBrown 		return -1;
28096238aec8SNeilBrown 	if (gid_gt(a->fsgid, b->fsgid))
28106238aec8SNeilBrown 		return 1;
28116238aec8SNeilBrown 
28126238aec8SNeilBrown 	ga = a->group_info;
28136238aec8SNeilBrown 	gb = b->group_info;
28146238aec8SNeilBrown 	if (ga == gb)
28156238aec8SNeilBrown 		return 0;
28166238aec8SNeilBrown 	if (ga == NULL)
28176238aec8SNeilBrown 		return -1;
28186238aec8SNeilBrown 	if (gb == NULL)
28196238aec8SNeilBrown 		return 1;
28206238aec8SNeilBrown 	if (ga->ngroups < gb->ngroups)
28216238aec8SNeilBrown 		return -1;
28226238aec8SNeilBrown 	if (ga->ngroups > gb->ngroups)
28236238aec8SNeilBrown 		return 1;
28246238aec8SNeilBrown 
28256238aec8SNeilBrown 	for (g = 0; g < ga->ngroups; g++) {
28266238aec8SNeilBrown 		if (gid_lt(ga->gid[g], gb->gid[g]))
28276238aec8SNeilBrown 			return -1;
28286238aec8SNeilBrown 		if (gid_gt(ga->gid[g], gb->gid[g]))
28296238aec8SNeilBrown 			return 1;
28306238aec8SNeilBrown 	}
28316238aec8SNeilBrown 	return 0;
28326238aec8SNeilBrown }
28336238aec8SNeilBrown 
2834b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
28351c3c07e9STrond Myklebust {
28361c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
28371c3c07e9STrond Myklebust 
28381c3c07e9STrond Myklebust 	while (n != NULL) {
2839b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2840b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
28416238aec8SNeilBrown 		int cmp = access_cmp(cred, entry);
28421c3c07e9STrond Myklebust 
2843b68572e0SNeilBrown 		if (cmp < 0)
28441c3c07e9STrond Myklebust 			n = n->rb_left;
2845b68572e0SNeilBrown 		else if (cmp > 0)
28461c3c07e9STrond Myklebust 			n = n->rb_right;
28471c3c07e9STrond Myklebust 		else
28481c3c07e9STrond Myklebust 			return entry;
28491c3c07e9STrond Myklebust 	}
28501c3c07e9STrond Myklebust 	return NULL;
28511c3c07e9STrond Myklebust }
28521c3c07e9STrond Myklebust 
2853b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
28541da177e4SLinus Torvalds {
285555296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
28561c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
285757b69181STrond Myklebust 	bool retry = true;
285857b69181STrond Myklebust 	int err;
28591da177e4SLinus Torvalds 
28601c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
286157b69181STrond Myklebust 	for(;;) {
28621c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
28631c3c07e9STrond Myklebust 			goto out_zap;
28641c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
286557b69181STrond Myklebust 		err = -ENOENT;
28661c3c07e9STrond Myklebust 		if (cache == NULL)
28671c3c07e9STrond Myklebust 			goto out;
286857b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
286921c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
287057b69181STrond Myklebust 			break;
28715c965db8STrond Myklebust 		if (!retry)
28725c965db8STrond Myklebust 			break;
287357b69181STrond Myklebust 		err = -ECHILD;
287457b69181STrond Myklebust 		if (!may_block)
287557b69181STrond Myklebust 			goto out;
287657b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
287757b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
287857b69181STrond Myklebust 		if (err)
287957b69181STrond Myklebust 			return err;
288057b69181STrond Myklebust 		spin_lock(&inode->i_lock);
288157b69181STrond Myklebust 		retry = false;
288257b69181STrond Myklebust 	}
2883b5e7b59cSNeilBrown 	*mask = cache->mask;
2884cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
28851c3c07e9STrond Myklebust 	err = 0;
28861c3c07e9STrond Myklebust out:
28871c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
28881c3c07e9STrond Myklebust 	return err;
28891c3c07e9STrond Myklebust out_zap:
28901a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
28911a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
28921c3c07e9STrond Myklebust 	return -ENOENT;
28931c3c07e9STrond Myklebust }
28941c3c07e9STrond Myklebust 
2895b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2896f682a398SNeilBrown {
2897f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2898f682a398SNeilBrown 	 * but do it without locking.
2899f682a398SNeilBrown 	 */
2900f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2901f682a398SNeilBrown 	struct nfs_access_entry *cache;
2902f682a398SNeilBrown 	int err = -ECHILD;
2903f682a398SNeilBrown 	struct list_head *lh;
2904f682a398SNeilBrown 
2905f682a398SNeilBrown 	rcu_read_lock();
2906f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2907f682a398SNeilBrown 		goto out;
29089f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2909f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2910f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
29116238aec8SNeilBrown 	    access_cmp(cred, cache) != 0)
2912f682a398SNeilBrown 		cache = NULL;
2913f682a398SNeilBrown 	if (cache == NULL)
2914f682a398SNeilBrown 		goto out;
291521c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2916f682a398SNeilBrown 		goto out;
2917b5e7b59cSNeilBrown 	*mask = cache->mask;
291821c3ba7eSTrond Myklebust 	err = 0;
2919f682a398SNeilBrown out:
2920f682a398SNeilBrown 	rcu_read_unlock();
2921f682a398SNeilBrown 	return err;
2922f682a398SNeilBrown }
2923f682a398SNeilBrown 
2924b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2925b5e7b59cSNeilBrown 			  u32 *mask, bool may_block)
2926d2ae4f8bSFrank van der Linden {
2927d2ae4f8bSFrank van der Linden 	int status;
2928d2ae4f8bSFrank van der Linden 
2929b5e7b59cSNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, mask);
2930d2ae4f8bSFrank van der Linden 	if (status != 0)
2931b5e7b59cSNeilBrown 		status = nfs_access_get_cached_locked(inode, cred, mask,
2932d2ae4f8bSFrank van der Linden 		    may_block);
2933d2ae4f8bSFrank van der Linden 
2934d2ae4f8bSFrank van der Linden 	return status;
2935d2ae4f8bSFrank van der Linden }
2936d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2937d2ae4f8bSFrank van der Linden 
293873fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode,
293973fbb3faSNeilBrown 				  struct nfs_access_entry *set,
294073fbb3faSNeilBrown 				  const struct cred *cred)
29411c3c07e9STrond Myklebust {
2942cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2943cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
29441c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
29451c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
29461c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2947b68572e0SNeilBrown 	int cmp;
29481c3c07e9STrond Myklebust 
29491c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
29501c3c07e9STrond Myklebust 	while (*p != NULL) {
29511c3c07e9STrond Myklebust 		parent = *p;
29521c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
29536238aec8SNeilBrown 		cmp = access_cmp(cred, entry);
29541c3c07e9STrond Myklebust 
2955b68572e0SNeilBrown 		if (cmp < 0)
29561c3c07e9STrond Myklebust 			p = &parent->rb_left;
2957b68572e0SNeilBrown 		else if (cmp > 0)
29581c3c07e9STrond Myklebust 			p = &parent->rb_right;
29591c3c07e9STrond Myklebust 		else
29601c3c07e9STrond Myklebust 			goto found;
29611c3c07e9STrond Myklebust 	}
29621c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
29631c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2964cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
29651c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
29661c3c07e9STrond Myklebust 	return;
29671c3c07e9STrond Myklebust found:
29681c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2969cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2970cfcea3e8STrond Myklebust 	list_del(&entry->lru);
29711c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
29721c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
29731da177e4SLinus Torvalds }
29741da177e4SLinus Torvalds 
297573fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
297673fbb3faSNeilBrown 			  const struct cred *cred)
29771da177e4SLinus Torvalds {
29781c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
29791c3c07e9STrond Myklebust 	if (cache == NULL)
29801c3c07e9STrond Myklebust 		return;
29811c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
29826238aec8SNeilBrown 	cache->fsuid = cred->fsuid;
29836238aec8SNeilBrown 	cache->fsgid = cred->fsgid;
29846238aec8SNeilBrown 	cache->group_info = get_group_info(cred->group_info);
29851da177e4SLinus Torvalds 	cache->mask = set->mask;
29861c3c07e9STrond Myklebust 
2987f682a398SNeilBrown 	/* The above field assignments must be visible
2988f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2989f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2990f682a398SNeilBrown 	 */
2991f682a398SNeilBrown 	smp_wmb();
299273fbb3faSNeilBrown 	nfs_access_add_rbtree(inode, cache, cred);
2993cfcea3e8STrond Myklebust 
2994cfcea3e8STrond Myklebust 	/* Update accounting */
29954e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2996cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
29974e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2998cfcea3e8STrond Myklebust 
2999cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
30001a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
3001cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
30021a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
30031a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
30041a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
3005cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
3006cfcea3e8STrond Myklebust 	}
30073a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
30081da177e4SLinus Torvalds }
30096168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
30106168f62cSWeston Andros Adamson 
30113c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
30123c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
30133c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
30143c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
30153c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
30163c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
3017ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
30183c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
30193c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
302015d4b73aSTrond Myklebust static int
3021ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
302215d4b73aSTrond Myklebust {
302315d4b73aSTrond Myklebust 	int mask = 0;
302415d4b73aSTrond Myklebust 
302515d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
302615d4b73aSTrond Myklebust 		mask |= MAY_READ;
3027ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
3028ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
302915d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
3030ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
303115d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
3032ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
3033ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
3034ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
3035ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
303615d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
3037ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
3038ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
303915d4b73aSTrond Myklebust 	return mask;
304015d4b73aSTrond Myklebust }
304115d4b73aSTrond Myklebust 
30426168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
30436168f62cSWeston Andros Adamson {
3044bd8b2441STrond Myklebust 	entry->mask = access_result;
30456168f62cSWeston Andros Adamson }
30466168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
30471da177e4SLinus Torvalds 
3048b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
30491da177e4SLinus Torvalds {
30501da177e4SLinus Torvalds 	struct nfs_access_entry cache;
305157b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3052e8194b7dSTrond Myklebust 	int cache_mask = -1;
30531da177e4SLinus Torvalds 	int status;
30541da177e4SLinus Torvalds 
3055f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
3056f4ce1299STrond Myklebust 
3057b5e7b59cSNeilBrown 	status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
30581da177e4SLinus Torvalds 	if (status == 0)
3059f4ce1299STrond Myklebust 		goto out_cached;
30601da177e4SLinus Torvalds 
3061f3324a2aSNeilBrown 	status = -ECHILD;
306257b69181STrond Myklebust 	if (!may_block)
3063f3324a2aSNeilBrown 		goto out;
3064f3324a2aSNeilBrown 
30651750d929SAnna Schumaker 	/*
30661750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
30671750d929SAnna Schumaker 	 */
306884631f84STrond Myklebust 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
306984631f84STrond Myklebust 		     nfs_access_xattr_mask(NFS_SERVER(inode));
30701750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
30711750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
30721750d929SAnna Schumaker 	else
30731750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
307473fbb3faSNeilBrown 	status = NFS_PROTO(inode)->access(inode, &cache, cred);
3075a71ee337SSuresh Jayaraman 	if (status != 0) {
3076a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
3077a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
307893ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
307993ce4af7STrond Myklebust 			else
308093ce4af7STrond Myklebust 				nfs_zap_caches(inode);
3081a71ee337SSuresh Jayaraman 		}
3082f4ce1299STrond Myklebust 		goto out;
3083a71ee337SSuresh Jayaraman 	}
308473fbb3faSNeilBrown 	nfs_access_add_cache(inode, &cache, cred);
3085f4ce1299STrond Myklebust out_cached:
3086ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3087bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3088f4ce1299STrond Myklebust 		status = -EACCES;
30891da177e4SLinus Torvalds out:
3090e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
3091f4ce1299STrond Myklebust 	return status;
30921da177e4SLinus Torvalds }
30931da177e4SLinus Torvalds 
3094af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
3095af22f94aSTrond Myklebust {
3096af22f94aSTrond Myklebust 	int mask = 0;
3097af22f94aSTrond Myklebust 
3098f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
3099f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
3100f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
3101f8d9a897SWeston Andros Adamson 	} else {
31028a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
3103af22f94aSTrond Myklebust 			mask |= MAY_READ;
31048a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
3105af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
3106f8d9a897SWeston Andros Adamson 	}
3107f8d9a897SWeston Andros Adamson 
3108af22f94aSTrond Myklebust 	return mask;
3109af22f94aSTrond Myklebust }
3110af22f94aSTrond Myklebust 
3111b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3112af22f94aSTrond Myklebust {
3113af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3114af22f94aSTrond Myklebust }
311589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
3116af22f94aSTrond Myklebust 
31175c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
31185c5fc09aSTrond Myklebust {
31195c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
312021c3ba7eSTrond Myklebust 	int ret = 0;
31215c5fc09aSTrond Myklebust 
31223825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
31233825827eSTrond Myklebust 		return 0;
3124720869ebSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
31255c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
312621c3ba7eSTrond Myklebust 			return -ECHILD;
312721c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
312821c3ba7eSTrond Myklebust 	}
31295c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
31305c5fc09aSTrond Myklebust 		ret = -EACCES;
31315c5fc09aSTrond Myklebust 	return ret;
31325c5fc09aSTrond Myklebust }
31335c5fc09aSTrond Myklebust 
3134549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns,
3135549c7297SChristian Brauner 		   struct inode *inode,
3136549c7297SChristian Brauner 		   int mask)
31371da177e4SLinus Torvalds {
3138b68572e0SNeilBrown 	const struct cred *cred = current_cred();
31391da177e4SLinus Torvalds 	int res = 0;
31401da177e4SLinus Torvalds 
314191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
314291d5b470SChuck Lever 
3143e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
31441da177e4SLinus Torvalds 		goto out;
31451da177e4SLinus Torvalds 	/* Is this sys_access() ? */
31469cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
31471da177e4SLinus Torvalds 		goto force_lookup;
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
31501da177e4SLinus Torvalds 		case S_IFLNK:
31511da177e4SLinus Torvalds 			goto out;
31521da177e4SLinus Torvalds 		case S_IFREG:
3153762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
3154762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3155762674f8STrond Myklebust 				return 0;
31561da177e4SLinus Torvalds 			break;
31571da177e4SLinus Torvalds 		case S_IFDIR:
31581da177e4SLinus Torvalds 			/*
31591da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
31601da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
31611da177e4SLinus Torvalds 			 */
31621da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
31631da177e4SLinus Torvalds 				goto out;
31641da177e4SLinus Torvalds 	}
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds force_lookup:
31671da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
31681da177e4SLinus Torvalds 		goto out_notsup;
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
31711da177e4SLinus Torvalds out:
31725c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
31735c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
3174f696a365SMiklos Szeredi 
31751e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
31761e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
31771da177e4SLinus Torvalds 	return res;
31781da177e4SLinus Torvalds out_notsup:
3179d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
3180d51ac1a8SNeilBrown 		return -ECHILD;
3181d51ac1a8SNeilBrown 
3182720869ebSTrond Myklebust 	res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3183720869ebSTrond Myklebust 						  NFS_INO_INVALID_OTHER);
31841da177e4SLinus Torvalds 	if (res == 0)
318547291baaSChristian Brauner 		res = generic_permission(&init_user_ns, inode, mask);
31861e7cb3dcSChuck Lever 	goto out;
31871da177e4SLinus Torvalds }
3188ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
3189