xref: /openbmc/linux/fs/nfs/dir.c (revision 0ae4c3e8)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  nfs directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
101da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
111da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
121da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
131da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
141da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
151da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
161da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
171da177e4SLinus Torvalds  *              layer (iput() and friends).
181da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21ddda8e0aSBryan Schumaker #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
311da177e4SLinus Torvalds #include <linux/nfs_fs.h>
321da177e4SLinus Torvalds #include <linux/nfs_mount.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
34873101b3SChuck Lever #include <linux/pagevec.h>
351da177e4SLinus Torvalds #include <linux/namei.h>
3654ceac45SDavid Howells #include <linux/mount.h>
37a0b8cab3SMel Gorman #include <linux/swap.h>
38e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "delegation.h"
4391d5b470SChuck Lever #include "iostat.h"
444c30d56eSAdrian Bunk #include "internal.h"
45cd9a1c0eSTrond Myklebust #include "fscache.h"
461da177e4SLinus Torvalds 
47f4ce1299STrond Myklebust #include "nfstrace.h"
48f4ce1299STrond Myklebust 
491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
571da177e4SLinus Torvalds 
584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
59f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
601da177e4SLinus Torvalds 	.read		= generic_read_dir,
6193a6ab7bSTrond Myklebust 	.iterate_shared	= nfs_readdir,
621da177e4SLinus Torvalds 	.open		= nfs_opendir,
63480c2006SBryan Schumaker 	.release	= nfs_closedir,
641da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
69d1bacf9eSBryan Schumaker };
70d1bacf9eSBryan Schumaker 
71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
72480c2006SBryan Schumaker {
73311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
74480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
75480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76480c2006SBryan Schumaker 	if (ctx != NULL) {
778ef2ce3eSBryan Schumaker 		ctx->duped = 0;
78311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
79480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
808ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
81684f39b4SNeilBrown 		ctx->cred = get_cred(cred);
82311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
831c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
841c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
851c341b77STrond Myklebust 			nfsi->cache_validity |= NFS_INO_INVALID_DATA |
861c341b77STrond Myklebust 				NFS_INO_REVAL_FORCED;
87311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
88311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
89480c2006SBryan Schumaker 		return ctx;
90480c2006SBryan Schumaker 	}
910c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
920c030806STrond Myklebust }
93480c2006SBryan Schumaker 
94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
95480c2006SBryan Schumaker {
96311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
97311324adSTrond Myklebust 	list_del(&ctx->list);
98311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
99684f39b4SNeilBrown 	put_cred(ctx->cred);
100480c2006SBryan Schumaker 	kfree(ctx);
101480c2006SBryan Schumaker }
102480c2006SBryan Schumaker 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * Open file
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static int
1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1081da177e4SLinus Torvalds {
109480c2006SBryan Schumaker 	int res = 0;
110480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1111da177e4SLinus Torvalds 
1126de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
113cc0dd2d1SChuck Lever 
114cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1151e7cb3dcSChuck Lever 
116684f39b4SNeilBrown 	ctx = alloc_nfs_open_dir_context(inode, current_cred());
117480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
118480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
119480c2006SBryan Schumaker 		goto out;
120480c2006SBryan Schumaker 	}
121480c2006SBryan Schumaker 	filp->private_data = ctx;
122480c2006SBryan Schumaker out:
1231da177e4SLinus Torvalds 	return res;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126480c2006SBryan Schumaker static int
127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
128480c2006SBryan Schumaker {
129a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
130480c2006SBryan Schumaker 	return 0;
131480c2006SBryan Schumaker }
132480c2006SBryan Schumaker 
133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
134d1bacf9eSBryan Schumaker 	u64 cookie;
135d1bacf9eSBryan Schumaker 	u64 ino;
136d1bacf9eSBryan Schumaker 	struct qstr string;
1370b26a0bfSTrond Myklebust 	unsigned char d_type;
138d1bacf9eSBryan Schumaker };
139d1bacf9eSBryan Schumaker 
140d1bacf9eSBryan Schumaker struct nfs_cache_array {
14188b8e133SChuck Lever 	int size;
142d1bacf9eSBryan Schumaker 	int eof_index;
143d1bacf9eSBryan Schumaker 	u64 last_cookie;
1445601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
145d1bacf9eSBryan Schumaker };
146d1bacf9eSBryan Schumaker 
1471da177e4SLinus Torvalds typedef struct {
1481da177e4SLinus Torvalds 	struct file	*file;
1491da177e4SLinus Torvalds 	struct page	*page;
15023db8620SAl Viro 	struct dir_context *ctx;
1511da177e4SLinus Torvalds 	unsigned long	page_index;
152f0dd2136STrond Myklebust 	u64		*dir_cookie;
1530aded708STrond Myklebust 	u64		last_cookie;
154f0dd2136STrond Myklebust 	loff_t		current_index;
15559e356a9STrond Myklebust 	loff_t		prev_index;
156d1bacf9eSBryan Schumaker 
157a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1581f4eab7eSNeil Brown 	unsigned long	timestamp;
1594704f0e2STrond Myklebust 	unsigned long	gencount;
160d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
161a7a3b1e9SBenjamin Coddington 	bool plus;
162a7a3b1e9SBenjamin Coddington 	bool eof;
1631da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1641da177e4SLinus Torvalds 
1654b310319STrond Myklebust static
1664b310319STrond Myklebust void nfs_readdir_init_array(struct page *page)
1674b310319STrond Myklebust {
1684b310319STrond Myklebust 	struct nfs_cache_array *array;
1694b310319STrond Myklebust 
1704b310319STrond Myklebust 	array = kmap_atomic(page);
1714b310319STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
1724b310319STrond Myklebust 	array->eof_index = -1;
1734b310319STrond Myklebust 	kunmap_atomic(array);
1744b310319STrond Myklebust }
1754b310319STrond Myklebust 
176d1bacf9eSBryan Schumaker /*
177d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
178d1bacf9eSBryan Schumaker  */
179d1bacf9eSBryan Schumaker static
18011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
181d1bacf9eSBryan Schumaker {
18211de3b11STrond Myklebust 	struct nfs_cache_array *array;
183d1bacf9eSBryan Schumaker 	int i;
1848cd51a0cSTrond Myklebust 
1852b86ce2dSCong Wang 	array = kmap_atomic(page);
186d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
187d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1884b310319STrond Myklebust 	array->size = 0;
1892b86ce2dSCong Wang 	kunmap_atomic(array);
190d1bacf9eSBryan Schumaker }
191d1bacf9eSBryan Schumaker 
192d1bacf9eSBryan Schumaker /*
193d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
194d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
195d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
196d1bacf9eSBryan Schumaker  */
197d1bacf9eSBryan Schumaker static
1984a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
199d1bacf9eSBryan Schumaker {
200d1bacf9eSBryan Schumaker 	string->len = len;
2013803d672STrond Myklebust 	string->name = kmemdup_nul(name, len, GFP_KERNEL);
2024a201d6eSTrond Myklebust 	if (string->name == NULL)
2034a201d6eSTrond Myklebust 		return -ENOMEM;
20404e4bd1cSCatalin Marinas 	/*
20504e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
20604e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
20704e4bd1cSCatalin Marinas 	 */
20804e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2098387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2104a201d6eSTrond Myklebust 	return 0;
211d1bacf9eSBryan Schumaker }
212d1bacf9eSBryan Schumaker 
213d1bacf9eSBryan Schumaker static
214d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
215d1bacf9eSBryan Schumaker {
2160795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2174a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2184a201d6eSTrond Myklebust 	int ret;
2194a201d6eSTrond Myklebust 
2204a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2213020093fSTrond Myklebust 
2223020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2233020093fSTrond Myklebust 	ret = -ENOSPC;
2243020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2253020093fSTrond Myklebust 		goto out;
2263020093fSTrond Myklebust 
2274a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2284a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2290b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2304a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2314a201d6eSTrond Myklebust 	if (ret)
2324a201d6eSTrond Myklebust 		goto out;
233d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2348cd51a0cSTrond Myklebust 	array->size++;
23547c716cbSTrond Myklebust 	if (entry->eof != 0)
236d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2374a201d6eSTrond Myklebust out:
2380795bf83SFabian Frederick 	kunmap(page);
2394a201d6eSTrond Myklebust 	return ret;
240d1bacf9eSBryan Schumaker }
241d1bacf9eSBryan Schumaker 
24259e356a9STrond Myklebust static inline
24359e356a9STrond Myklebust int is_32bit_api(void)
24459e356a9STrond Myklebust {
24559e356a9STrond Myklebust #ifdef CONFIG_COMPAT
24659e356a9STrond Myklebust 	return in_compat_syscall();
24759e356a9STrond Myklebust #else
24859e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
24959e356a9STrond Myklebust #endif
25059e356a9STrond Myklebust }
25159e356a9STrond Myklebust 
25259e356a9STrond Myklebust static
25359e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
25459e356a9STrond Myklebust {
25559e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
25659e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
25759e356a9STrond Myklebust 		return false;
25859e356a9STrond Myklebust 	return true;
25959e356a9STrond Myklebust }
26059e356a9STrond Myklebust 
261d1bacf9eSBryan Schumaker static
262d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
263d1bacf9eSBryan Schumaker {
26423db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
265d1bacf9eSBryan Schumaker 	unsigned int index;
266d1bacf9eSBryan Schumaker 
267d1bacf9eSBryan Schumaker 	if (diff < 0)
268d1bacf9eSBryan Schumaker 		goto out_eof;
269d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2708cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
271d1bacf9eSBryan Schumaker 			goto out_eof;
272d1bacf9eSBryan Schumaker 		return -EAGAIN;
273d1bacf9eSBryan Schumaker 	}
274d1bacf9eSBryan Schumaker 
275d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
276d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
277d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
278d1bacf9eSBryan Schumaker 	return 0;
279d1bacf9eSBryan Schumaker out_eof:
2806089dd0dSThomas Meyer 	desc->eof = true;
281d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
282d1bacf9eSBryan Schumaker }
283d1bacf9eSBryan Schumaker 
2844db72b40SJeff Layton static bool
2854db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2864db72b40SJeff Layton {
2874db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2884db72b40SJeff Layton 		return false;
2894db72b40SJeff Layton 	smp_rmb();
2904db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2914db72b40SJeff Layton }
2924db72b40SJeff Layton 
293d1bacf9eSBryan Schumaker static
294d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
295d1bacf9eSBryan Schumaker {
296d1bacf9eSBryan Schumaker 	int i;
2978ef2ce3eSBryan Schumaker 	loff_t new_pos;
298d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
299d1bacf9eSBryan Schumaker 
300d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3018cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
302496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
3030c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
3040c030806STrond Myklebust 
3058ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3064db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
3074db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
3080c030806STrond Myklebust 				ctx->duped = 0;
3090c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
31059e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
3110c030806STrond Myklebust 				if (ctx->duped > 0
3120c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
3130c030806STrond Myklebust 					if (printk_ratelimit()) {
3146de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
3150c030806STrond Myklebust 								"Please contact your server vendor.  "
3169581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
3179581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
3189581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
3190c030806STrond Myklebust 					}
3200c030806STrond Myklebust 					status = -ELOOP;
3210c030806STrond Myklebust 					goto out;
3220c030806STrond Myklebust 				}
3238ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3240c030806STrond Myklebust 				ctx->duped = -1;
3258ef2ce3eSBryan Schumaker 			}
32659e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
32759e356a9STrond Myklebust 				desc->ctx->pos = *desc->dir_cookie;
32859e356a9STrond Myklebust 			else
32923db8620SAl Viro 				desc->ctx->pos = new_pos;
33059e356a9STrond Myklebust 			desc->prev_index = new_pos;
3318cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
33247c716cbSTrond Myklebust 			return 0;
3338cd51a0cSTrond Myklebust 		}
3348cd51a0cSTrond Myklebust 	}
33547c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
336d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
33718fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
3386089dd0dSThomas Meyer 			desc->eof = true;
339d1bacf9eSBryan Schumaker 	}
3400c030806STrond Myklebust out:
341d1bacf9eSBryan Schumaker 	return status;
342d1bacf9eSBryan Schumaker }
343d1bacf9eSBryan Schumaker 
344d1bacf9eSBryan Schumaker static
345d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
346d1bacf9eSBryan Schumaker {
347d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
34847c716cbSTrond Myklebust 	int status;
349d1bacf9eSBryan Schumaker 
3500795bf83SFabian Frederick 	array = kmap(desc->page);
351d1bacf9eSBryan Schumaker 
352d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
353d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
354d1bacf9eSBryan Schumaker 	else
355d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
356d1bacf9eSBryan Schumaker 
35747c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3580aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
359e47c085aSTrond Myklebust 		desc->current_index += array->size;
36047c716cbSTrond Myklebust 		desc->page_index++;
36147c716cbSTrond Myklebust 	}
3620795bf83SFabian Frederick 	kunmap(desc->page);
363d1bacf9eSBryan Schumaker 	return status;
364d1bacf9eSBryan Schumaker }
365d1bacf9eSBryan Schumaker 
366d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
367d1bacf9eSBryan Schumaker static
36856e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
369d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
370d1bacf9eSBryan Schumaker {
371480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
372684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
3734704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3741da177e4SLinus Torvalds 	int		error;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds  again:
3771da177e4SLinus Torvalds 	timestamp = jiffies;
3784704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
379a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
380be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3811da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3821da177e4SLinus Torvalds 	if (error < 0) {
3831da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3841da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3851da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3863a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
387a7a3b1e9SBenjamin Coddington 			desc->plus = false;
3881da177e4SLinus Torvalds 			goto again;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 		goto error;
3911da177e4SLinus Torvalds 	}
3921f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3934704f0e2STrond Myklebust 	desc->gencount = gencount;
394d1bacf9eSBryan Schumaker error:
395d1bacf9eSBryan Schumaker 	return error;
396d1bacf9eSBryan Schumaker }
397d1bacf9eSBryan Schumaker 
398573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
399573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
400d1bacf9eSBryan Schumaker {
40159e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
402573c4e1eSChuck Lever 	int error;
403d1bacf9eSBryan Schumaker 
40459e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
405573c4e1eSChuck Lever 	if (error)
406573c4e1eSChuck Lever 		return error;
407d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
408d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
409d1bacf9eSBryan Schumaker 	return 0;
410d1bacf9eSBryan Schumaker }
411d1bacf9eSBryan Schumaker 
412fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
413fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
414fa923369STrond Myklebust  */
415d39ab9deSBryan Schumaker static
416d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
417d39ab9deSBryan Schumaker {
418d8fdb47fSTrond Myklebust 	struct inode *inode;
419fa923369STrond Myklebust 	struct nfs_inode *nfsi;
420fa923369STrond Myklebust 
4212b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
4222b0143b5SDavid Howells 		return 0;
423fa923369STrond Myklebust 
424d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
425d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
426d8fdb47fSTrond Myklebust 		return 0;
427d8fdb47fSTrond Myklebust 
428d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
4297dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
430d39ab9deSBryan Schumaker 		return 0;
4317dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
4327dc72d5fSTrond Myklebust 		return 0;
4337dc72d5fSTrond Myklebust 	return 1;
434d39ab9deSBryan Schumaker }
435d39ab9deSBryan Schumaker 
436d39ab9deSBryan Schumaker static
43723db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
438d69ee9b8STrond Myklebust {
439d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
440d69ee9b8STrond Myklebust 		return false;
441d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
442d69ee9b8STrond Myklebust 		return true;
44323db8620SAl Viro 	if (ctx->pos == 0)
444d69ee9b8STrond Myklebust 		return true;
445d69ee9b8STrond Myklebust 	return false;
446d69ee9b8STrond Myklebust }
447d69ee9b8STrond Myklebust 
448d69ee9b8STrond Myklebust /*
44963519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
45063519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
451d69ee9b8STrond Myklebust  * directory.
452d69ee9b8STrond Myklebust  */
453d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
454d69ee9b8STrond Myklebust {
45563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
45663519fbcSTrond Myklebust 
45763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
45863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
45963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
460d69ee9b8STrond Myklebust }
461d69ee9b8STrond Myklebust 
462311324adSTrond Myklebust /*
463311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
464311324adSTrond Myklebust  *
465311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
466311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
467311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
468311324adSTrond Myklebust  * cache flush.
469311324adSTrond Myklebust  */
470311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
471311324adSTrond Myklebust {
47263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
47363519fbcSTrond Myklebust 
47463519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
47563519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
47663519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
477227823d2SDai Ngo 		invalidate_mapping_pages(dir->i_mapping,
478227823d2SDai Ngo 			nfsi->page_index + 1, -1);
479311324adSTrond Myklebust 	}
480311324adSTrond Myklebust }
481311324adSTrond Myklebust 
482d69ee9b8STrond Myklebust static
483a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
484a1147b82STrond Myklebust 		unsigned long dir_verifier)
485d39ab9deSBryan Schumaker {
48626fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4879ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4884a201d6eSTrond Myklebust 	struct dentry *dentry;
4894a201d6eSTrond Myklebust 	struct dentry *alias;
490d39ab9deSBryan Schumaker 	struct inode *inode;
491aa9c2669SDavid Quigley 	int status;
492d39ab9deSBryan Schumaker 
493fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
494fa923369STrond Myklebust 		return;
4956c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
4966c441c25STrond Myklebust 		return;
49778d04af4STrond Myklebust 	if (filename.len == 0)
49878d04af4STrond Myklebust 		return;
49978d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
50078d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
50178d04af4STrond Myklebust 		return;
50278d04af4STrond Myklebust 	/* ...or '/' */
50378d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
50478d04af4STrond Myklebust 		return;
5054a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
5064a201d6eSTrond Myklebust 		if (filename.len == 1)
5074a201d6eSTrond Myklebust 			return;
5084a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
5094a201d6eSTrond Myklebust 			return;
5104a201d6eSTrond Myklebust 	}
5118387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
512d39ab9deSBryan Schumaker 
5134a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
5149ac3d3e8SAl Viro again:
5159ac3d3e8SAl Viro 	if (!dentry) {
5169ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
5179ac3d3e8SAl Viro 		if (IS_ERR(dentry))
5189ac3d3e8SAl Viro 			return;
5199ac3d3e8SAl Viro 	}
5209ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
5216c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
5226c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
5236c441c25STrond Myklebust 					&entry->fattr->fsid))
5246c441c25STrond Myklebust 			goto out;
525d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
5267dc72d5fSTrond Myklebust 			if (!entry->fh->size)
5277dc72d5fSTrond Myklebust 				goto out;
528a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
5292b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
530aa9c2669SDavid Quigley 			if (!status)
5312b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
532d39ab9deSBryan Schumaker 			goto out;
533d39ab9deSBryan Schumaker 		} else {
5345542aa2fSEric W. Biederman 			d_invalidate(dentry);
535d39ab9deSBryan Schumaker 			dput(dentry);
5369ac3d3e8SAl Viro 			dentry = NULL;
5379ac3d3e8SAl Viro 			goto again;
538d39ab9deSBryan Schumaker 		}
539d39ab9deSBryan Schumaker 	}
5407dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5417dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5427dc72d5fSTrond Myklebust 		goto out;
5437dc72d5fSTrond Myklebust 	}
544d39ab9deSBryan Schumaker 
5451775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
54641d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5479ac3d3e8SAl Viro 	d_lookup_done(dentry);
5489ac3d3e8SAl Viro 	if (alias) {
549d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
550d39ab9deSBryan Schumaker 			goto out;
5519ac3d3e8SAl Viro 		dput(dentry);
5529ac3d3e8SAl Viro 		dentry = alias;
5539ac3d3e8SAl Viro 	}
554a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
555d39ab9deSBryan Schumaker out:
556d39ab9deSBryan Schumaker 	dput(dentry);
557d39ab9deSBryan Schumaker }
558d39ab9deSBryan Schumaker 
559d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
560d1bacf9eSBryan Schumaker static
5618cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5626650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
563d1bacf9eSBryan Schumaker {
564babddc72SBryan Schumaker 	struct xdr_stream stream;
565f7da7a12SBenny Halevy 	struct xdr_buf buf;
5666650239aSTrond Myklebust 	struct page *scratch;
56799424380SBryan Schumaker 	struct nfs_cache_array *array;
5685c346854STrond Myklebust 	unsigned int count = 0;
5695c346854STrond Myklebust 	int status;
570babddc72SBryan Schumaker 
5716650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5726650239aSTrond Myklebust 	if (scratch == NULL)
5736650239aSTrond Myklebust 		return -ENOMEM;
574babddc72SBryan Schumaker 
575ce85cfbeSBenjamin Coddington 	if (buflen == 0)
576ce85cfbeSBenjamin Coddington 		goto out_nopages;
577ce85cfbeSBenjamin Coddington 
578f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
579*0ae4c3e8SChuck Lever 	xdr_set_scratch_page(&stream, scratch);
58099424380SBryan Schumaker 
58199424380SBryan Schumaker 	do {
582d33030e2SJeffrey Mitchell 		if (entry->label)
583d33030e2SJeffrey Mitchell 			entry->label->len = NFS4_MAXLABELLEN;
584d33030e2SJeffrey Mitchell 
58599424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5868cd51a0cSTrond Myklebust 		if (status != 0) {
5878cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5888cd51a0cSTrond Myklebust 				status = 0;
58999424380SBryan Schumaker 			break;
5908cd51a0cSTrond Myklebust 		}
59199424380SBryan Schumaker 
5925c346854STrond Myklebust 		count++;
5935c346854STrond Myklebust 
594a7a3b1e9SBenjamin Coddington 		if (desc->plus)
595a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
596a1147b82STrond Myklebust 					desc->dir_verifier);
5978cd51a0cSTrond Myklebust 
598db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
5998cd51a0cSTrond Myklebust 		if (status != 0)
6008cd51a0cSTrond Myklebust 			break;
60199424380SBryan Schumaker 	} while (!entry->eof);
60299424380SBryan Schumaker 
603ce85cfbeSBenjamin Coddington out_nopages:
60447c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
605db531db9SMax Kellermann 		array = kmap(page);
6068cd51a0cSTrond Myklebust 		array->eof_index = array->size;
60799424380SBryan Schumaker 		status = 0;
608db531db9SMax Kellermann 		kunmap(page);
60956e4ebf8SBryan Schumaker 	}
6106650239aSTrond Myklebust 
6116650239aSTrond Myklebust 	put_page(scratch);
6128cd51a0cSTrond Myklebust 	return status;
6138cd51a0cSTrond Myklebust }
61456e4ebf8SBryan Schumaker 
61556e4ebf8SBryan Schumaker static
616c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
61756e4ebf8SBryan Schumaker {
61856e4ebf8SBryan Schumaker 	unsigned int i;
61956e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
62056e4ebf8SBryan Schumaker 		put_page(pages[i]);
62156e4ebf8SBryan Schumaker }
62256e4ebf8SBryan Schumaker 
62356e4ebf8SBryan Schumaker /*
624bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
625bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
62656e4ebf8SBryan Schumaker  */
62756e4ebf8SBryan Schumaker static
628c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
62956e4ebf8SBryan Schumaker {
63056e4ebf8SBryan Schumaker 	unsigned int i;
63156e4ebf8SBryan Schumaker 
63256e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
63356e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
63456e4ebf8SBryan Schumaker 		if (page == NULL)
63556e4ebf8SBryan Schumaker 			goto out_freepages;
63656e4ebf8SBryan Schumaker 		pages[i] = page;
63756e4ebf8SBryan Schumaker 	}
6386650239aSTrond Myklebust 	return 0;
63956e4ebf8SBryan Schumaker 
64056e4ebf8SBryan Schumaker out_freepages:
641c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6426650239aSTrond Myklebust 	return -ENOMEM;
643d1bacf9eSBryan Schumaker }
644d1bacf9eSBryan Schumaker 
645d1bacf9eSBryan Schumaker static
646d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
647d1bacf9eSBryan Schumaker {
64856e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
649d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
650d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
651d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6528cd51a0cSTrond Myklebust 	int status = -ENOMEM;
65356e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
654d1bacf9eSBryan Schumaker 
6554b310319STrond Myklebust 	nfs_readdir_init_array(page);
6564b310319STrond Myklebust 
657d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6580aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
659d1bacf9eSBryan Schumaker 	entry.eof = 0;
660d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
661d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
662573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
663d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
664d1bacf9eSBryan Schumaker 		goto out;
665d1bacf9eSBryan Schumaker 
66614c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
66714c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
66814c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
66914c43f76SDavid Quigley 		goto out;
67014c43f76SDavid Quigley 	}
67114c43f76SDavid Quigley 
6720795bf83SFabian Frederick 	array = kmap(page);
673d1bacf9eSBryan Schumaker 
674c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
6756650239aSTrond Myklebust 	if (status < 0)
676d1bacf9eSBryan Schumaker 		goto out_release_array;
677d1bacf9eSBryan Schumaker 	do {
678ac396128STrond Myklebust 		unsigned int pglen;
67956e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
680babddc72SBryan Schumaker 
681d1bacf9eSBryan Schumaker 		if (status < 0)
682d1bacf9eSBryan Schumaker 			break;
683ac396128STrond Myklebust 		pglen = status;
6846650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6858cd51a0cSTrond Myklebust 		if (status < 0) {
6868cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6878cd51a0cSTrond Myklebust 				status = 0;
6888cd51a0cSTrond Myklebust 			break;
6898cd51a0cSTrond Myklebust 		}
6908cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
691d1bacf9eSBryan Schumaker 
692c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
693d1bacf9eSBryan Schumaker out_release_array:
6940795bf83SFabian Frederick 	kunmap(page);
69514c43f76SDavid Quigley 	nfs4_label_free(entry.label);
696d1bacf9eSBryan Schumaker out:
697d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
698d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
699d1bacf9eSBryan Schumaker 	return status;
700d1bacf9eSBryan Schumaker }
701d1bacf9eSBryan Schumaker 
702d1bacf9eSBryan Schumaker /*
703d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
704d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
705d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
706d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
7071da177e4SLinus Torvalds  */
708d1bacf9eSBryan Schumaker static
709a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page)
710d1bacf9eSBryan Schumaker {
711a46126ccSChristoph Hellwig 	nfs_readdir_descriptor_t *desc = data;
712496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
7138cd51a0cSTrond Myklebust 	int ret;
714d1bacf9eSBryan Schumaker 
7158cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
7168cd51a0cSTrond Myklebust 	if (ret < 0)
717d1bacf9eSBryan Schumaker 		goto error;
718d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
719d1bacf9eSBryan Schumaker 
7202aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
721cd9ae2b6STrond Myklebust 		/* Should never happen */
722cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
723cd9ae2b6STrond Myklebust 	}
7241da177e4SLinus Torvalds 	unlock_page(page);
7251da177e4SLinus Torvalds 	return 0;
7261da177e4SLinus Torvalds  error:
7274b310319STrond Myklebust 	nfs_readdir_clear_array(page);
7281da177e4SLinus Torvalds 	unlock_page(page);
7298cd51a0cSTrond Myklebust 	return ret;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
732d1bacf9eSBryan Schumaker static
733d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7341da177e4SLinus Torvalds {
73509cbfeafSKirill A. Shutemov 	put_page(desc->page);
7361da177e4SLinus Torvalds 	desc->page = NULL;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
739d1bacf9eSBryan Schumaker static
740d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7411da177e4SLinus Torvalds {
742a46126ccSChristoph Hellwig 	return read_cache_page(desc->file->f_mapping, desc->page_index,
743a46126ccSChristoph Hellwig 			nfs_readdir_filler, desc);
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /*
747d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
748114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
7491da177e4SLinus Torvalds  */
750d1bacf9eSBryan Schumaker static
751114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
752d1bacf9eSBryan Schumaker {
753227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
754227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
755d1bacf9eSBryan Schumaker 	int res;
756d1bacf9eSBryan Schumaker 
757d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
758d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
759d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
760114de382STrond Myklebust 	res = lock_page_killable(desc->page);
76147c716cbSTrond Myklebust 	if (res != 0)
762114de382STrond Myklebust 		goto error;
763114de382STrond Myklebust 	res = -EAGAIN;
764114de382STrond Myklebust 	if (desc->page->mapping != NULL) {
765114de382STrond Myklebust 		res = nfs_readdir_search_array(desc);
766227823d2SDai Ngo 		if (res == 0) {
767227823d2SDai Ngo 			nfsi->page_index = desc->page_index;
768114de382STrond Myklebust 			return 0;
769114de382STrond Myklebust 		}
770227823d2SDai Ngo 	}
771114de382STrond Myklebust 	unlock_page(desc->page);
772114de382STrond Myklebust error:
773d1bacf9eSBryan Schumaker 	cache_page_release(desc);
774d1bacf9eSBryan Schumaker 	return res;
775d1bacf9eSBryan Schumaker }
776d1bacf9eSBryan Schumaker 
777d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7781da177e4SLinus Torvalds static inline
7791da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7801da177e4SLinus Torvalds {
7818cd51a0cSTrond Myklebust 	int res;
782d1bacf9eSBryan Schumaker 
7830aded708STrond Myklebust 	if (desc->page_index == 0) {
7848cd51a0cSTrond Myklebust 		desc->current_index = 0;
78559e356a9STrond Myklebust 		desc->prev_index = 0;
7860aded708STrond Myklebust 		desc->last_cookie = 0;
7870aded708STrond Myklebust 	}
78847c716cbSTrond Myklebust 	do {
789114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
79047c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7911da177e4SLinus Torvalds 	return res;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7961da177e4SLinus Torvalds  */
7971da177e4SLinus Torvalds static
79823db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7991da177e4SLinus Torvalds {
8001da177e4SLinus Torvalds 	struct file	*file = desc->file;
801d1bacf9eSBryan Schumaker 	int i = 0;
802d1bacf9eSBryan Schumaker 	int res = 0;
803d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8048ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
8058ef2ce3eSBryan Schumaker 
8060795bf83SFabian Frederick 	array = kmap(desc->page);
807d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
808ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8091da177e4SLinus Torvalds 
810ece0b423STrond Myklebust 		ent = &array->array[i];
81123db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
81223db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
8136089dd0dSThomas Meyer 			desc->eof = true;
8141da177e4SLinus Torvalds 			break;
815ece0b423STrond Myklebust 		}
816d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
817d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
818d1bacf9eSBryan Schumaker 		else
819d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
82059e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
82159e356a9STrond Myklebust 			desc->ctx->pos = *desc->dir_cookie;
82259e356a9STrond Myklebust 		else
82359e356a9STrond Myklebust 			desc->ctx->pos++;
8240c030806STrond Myklebust 		if (ctx->duped != 0)
8250c030806STrond Myklebust 			ctx->duped = 1;
8268cd51a0cSTrond Myklebust 	}
82747c716cbSTrond Myklebust 	if (array->eof_index >= 0)
8286089dd0dSThomas Meyer 		desc->eof = true;
829d1bacf9eSBryan Schumaker 
8300795bf83SFabian Frederick 	kunmap(desc->page);
8311e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8321e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
8331da177e4SLinus Torvalds 	return res;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds /*
8371da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8381da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8391da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8401da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8411da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8421da177e4SLinus Torvalds  *
8431da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8441da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8451da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8461da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8471da177e4SLinus Torvalds  */
8481da177e4SLinus Torvalds static inline
84923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	struct page	*page = NULL;
8521da177e4SLinus Torvalds 	int		status;
853496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8540c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8551da177e4SLinus Torvalds 
8561e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8571e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8601da177e4SLinus Torvalds 	if (!page) {
8611da177e4SLinus Torvalds 		status = -ENOMEM;
8621da177e4SLinus Torvalds 		goto out;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8657a8e1dc3STrond Myklebust 	desc->page_index = 0;
8660aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8677a8e1dc3STrond Myklebust 	desc->page = page;
8680c030806STrond Myklebust 	ctx->duped = 0;
8697a8e1dc3STrond Myklebust 
87085f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
87185f8607eSTrond Myklebust 	if (status < 0)
872d1bacf9eSBryan Schumaker 		goto out_release;
873d1bacf9eSBryan Schumaker 
87423db8620SAl Viro 	status = nfs_do_filldir(desc);
8751da177e4SLinus Torvalds 
876114de382STrond Myklebust  out_release:
877114de382STrond Myklebust 	nfs_readdir_clear_array(desc->page);
878114de382STrond Myklebust 	cache_page_release(desc);
8791da177e4SLinus Torvalds  out:
8801e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8813110ff80SHarvey Harrison 			__func__, status);
8821da177e4SLinus Torvalds 	return status;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
88500a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
88600a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
88700a92642SOlivier Galibert    whole directory.
8881da177e4SLinus Torvalds  */
88923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8901da177e4SLinus Torvalds {
891be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
8922b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
89323db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
89459e356a9STrond Myklebust 	nfs_readdir_descriptor_t my_desc = {
89559e356a9STrond Myklebust 		.file = file,
89659e356a9STrond Myklebust 		.ctx = ctx,
89759e356a9STrond Myklebust 		.dir_cookie = &dir_ctx->dir_cookie,
89859e356a9STrond Myklebust 		.plus = nfs_use_readdirplus(inode, ctx),
89959e356a9STrond Myklebust 	},
90059e356a9STrond Myklebust 			*desc = &my_desc;
90107b5ce8eSScott Mayhew 	int res = 0;
9021da177e4SLinus Torvalds 
9036de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9046de1472fSAl Viro 			file, (long long)ctx->pos);
90591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
90691d5b470SChuck Lever 
9071da177e4SLinus Torvalds 	/*
90823db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
909f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
91000a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
91100a92642SOlivier Galibert 	 * revalidate the cookie.
9121da177e4SLinus Torvalds 	 */
91379f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
91423db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
915fccca7fcSTrond Myklebust 	if (res < 0)
916fccca7fcSTrond Myklebust 		goto out;
917fccca7fcSTrond Myklebust 
91847c716cbSTrond Myklebust 	do {
9191da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
92000a92642SOlivier Galibert 
9211da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
922ece0b423STrond Myklebust 			res = 0;
9231da177e4SLinus Torvalds 			/* This means either end of directory */
9246089dd0dSThomas Meyer 			if (*desc->dir_cookie && !desc->eof) {
9251da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
92623db8620SAl Viro 				res = uncached_readdir(desc);
927ece0b423STrond Myklebust 				if (res == 0)
9281da177e4SLinus Torvalds 					continue;
9291da177e4SLinus Torvalds 			}
9301da177e4SLinus Torvalds 			break;
9311da177e4SLinus Torvalds 		}
9321da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9333a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9341da177e4SLinus Torvalds 			nfs_zap_caches(inode);
935baf57a09STrond Myklebust 			desc->page_index = 0;
936a7a3b1e9SBenjamin Coddington 			desc->plus = false;
937a7a3b1e9SBenjamin Coddington 			desc->eof = false;
9381da177e4SLinus Torvalds 			continue;
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 		if (res < 0)
9411da177e4SLinus Torvalds 			break;
9421da177e4SLinus Torvalds 
94323db8620SAl Viro 		res = nfs_do_filldir(desc);
944114de382STrond Myklebust 		unlock_page(desc->page);
945114de382STrond Myklebust 		cache_page_release(desc);
946ece0b423STrond Myklebust 		if (res < 0)
9471da177e4SLinus Torvalds 			break;
94847c716cbSTrond Myklebust 	} while (!desc->eof);
949fccca7fcSTrond Myklebust out:
9501e7cb3dcSChuck Lever 	if (res > 0)
9511e7cb3dcSChuck Lever 		res = 0;
9526de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9531da177e4SLinus Torvalds 	return res;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
956965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
957f0dd2136STrond Myklebust {
958480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
959b84e06c5SChuck Lever 
9606de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9616de1472fSAl Viro 			filp, offset, whence);
962b84e06c5SChuck Lever 
963965c8e59SAndrew Morton 	switch (whence) {
964f0dd2136STrond Myklebust 	default:
965b2b1ff3dSTrond Myklebust 		return -EINVAL;
966b2b1ff3dSTrond Myklebust 	case SEEK_SET:
967b2b1ff3dSTrond Myklebust 		if (offset < 0)
968b2b1ff3dSTrond Myklebust 			return -EINVAL;
96983f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
970b2b1ff3dSTrond Myklebust 		break;
971b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
972b2b1ff3dSTrond Myklebust 		if (offset == 0)
973b2b1ff3dSTrond Myklebust 			return filp->f_pos;
97483f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
975b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
976b2b1ff3dSTrond Myklebust 		if (offset < 0) {
97783f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
978b2b1ff3dSTrond Myklebust 			return -EINVAL;
979b2b1ff3dSTrond Myklebust 		}
980f0dd2136STrond Myklebust 	}
981f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
982f0dd2136STrond Myklebust 		filp->f_pos = offset;
98359e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(filp))
98459e356a9STrond Myklebust 			dir_ctx->dir_cookie = offset;
98559e356a9STrond Myklebust 		else
986480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
9878ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
988f0dd2136STrond Myklebust 	}
98983f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
990f0dd2136STrond Myklebust 	return offset;
991f0dd2136STrond Myklebust }
992f0dd2136STrond Myklebust 
9931da177e4SLinus Torvalds /*
9941da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9951da177e4SLinus Torvalds  * is a dummy operation.
9961da177e4SLinus Torvalds  */
99702c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
99802c24a82SJosef Bacik 			 int datasync)
9991da177e4SLinus Torvalds {
10006de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10011e7cb3dcSChuck Lever 
100211decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
10031da177e4SLinus Torvalds 	return 0;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
1006bfc69a45STrond Myklebust /**
1007bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1008302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1009bfc69a45STrond Myklebust  *
1010bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1011bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1012bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1013bfc69a45STrond Myklebust  *
1014efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1015efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1016efeda80dSTrond Myklebust  *
1017bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1018bfc69a45STrond Myklebust  */
1019bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1020bfc69a45STrond Myklebust {
1021efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1022bfc69a45STrond Myklebust }
102389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1024bfc69a45STrond Myklebust 
1025efeda80dSTrond Myklebust /**
1026efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1027efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1028efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1029efeda80dSTrond Myklebust  *
1030efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1031efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1032efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1033efeda80dSTrond Myklebust  */
1034efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1035efeda80dSTrond Myklebust {
1036efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1037efeda80dSTrond Myklebust }
1038efeda80dSTrond Myklebust 
1039efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1040efeda80dSTrond Myklebust {
1041efeda80dSTrond Myklebust 	*verf |= 1UL;
1042efeda80dSTrond Myklebust }
1043efeda80dSTrond Myklebust 
1044efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1045efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1046efeda80dSTrond Myklebust {
1047efeda80dSTrond Myklebust 	*verf &= ~1UL;
1048efeda80dSTrond Myklebust }
1049efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1050efeda80dSTrond Myklebust 
1051efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1052efeda80dSTrond Myklebust {
1053efeda80dSTrond Myklebust 	return verf & 1;
1054efeda80dSTrond Myklebust }
1055efeda80dSTrond Myklebust 
1056efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1057efeda80dSTrond Myklebust {
1058efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1059efeda80dSTrond Myklebust }
1060efeda80dSTrond Myklebust 
1061efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1062efeda80dSTrond Myklebust {
1063efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1064efeda80dSTrond Myklebust 
1065efeda80dSTrond Myklebust 	if (!nfs_verifier_is_delegated(dentry) &&
1066efeda80dSTrond Myklebust 	    !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1067efeda80dSTrond Myklebust 		goto out;
1068efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1069efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1070efeda80dSTrond Myklebust out:
1071efeda80dSTrond Myklebust 	dentry->d_time = verf;
1072efeda80dSTrond Myklebust }
1073efeda80dSTrond Myklebust 
1074efeda80dSTrond Myklebust /**
1075efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1076efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1077efeda80dSTrond Myklebust  * @verf: verifier to save
1078efeda80dSTrond Myklebust  *
1079efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1080efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1081efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1082efeda80dSTrond Myklebust  * look it up again after a directory change.
1083efeda80dSTrond Myklebust  */
1084efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1085efeda80dSTrond Myklebust {
1086efeda80dSTrond Myklebust 
1087efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1088efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1089efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1090efeda80dSTrond Myklebust }
1091efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1092efeda80dSTrond Myklebust 
1093efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1094efeda80dSTrond Myklebust /**
1095efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1096efeda80dSTrond Myklebust  * @inode: pointer to inode
1097efeda80dSTrond Myklebust  *
1098efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1099efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1100efeda80dSTrond Myklebust  * while holding a delegation.
1101efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1102efeda80dSTrond Myklebust  * returned or revoked.
1103efeda80dSTrond Myklebust  */
1104efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1105efeda80dSTrond Myklebust {
1106efeda80dSTrond Myklebust 	struct dentry *alias;
1107efeda80dSTrond Myklebust 
1108efeda80dSTrond Myklebust 	if (!inode)
1109efeda80dSTrond Myklebust 		return;
1110efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1111efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1112efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1113efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1114efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1115efeda80dSTrond Myklebust 	}
1116efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1117efeda80dSTrond Myklebust }
1118efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1119efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1120efeda80dSTrond Myklebust 
11211da177e4SLinus Torvalds /*
11221da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
11231da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
11241da177e4SLinus Torvalds  * and may need to be looked up again.
1125912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
11261da177e4SLinus Torvalds  */
1127912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1128912a108dSNeilBrown 			      int rcu_walk)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
11311da177e4SLinus Torvalds 		return 1;
11324eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
11334eec952eSTrond Myklebust 		return 0;
1134f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
11356ecc5e8fSTrond Myklebust 		return 0;
1136f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
11371cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1138912a108dSNeilBrown 		if (rcu_walk)
1139f2c77f4eSTrond Myklebust 			return 0;
11401cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
11411cd9cb05STrond Myklebust 			return 0;
11421cd9cb05STrond Myklebust 	}
1143f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1144f2c77f4eSTrond Myklebust 		return 0;
1145f2c77f4eSTrond Myklebust 	return 1;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /*
1149a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1150a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1151a12802caSTrond Myklebust  */
1152fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1153a12802caSTrond Myklebust {
1154a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1155a12802caSTrond Myklebust 		return 0;
1156fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1157a12802caSTrond Myklebust }
1158a12802caSTrond Myklebust 
1159a12802caSTrond Myklebust /*
11601d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
11611d6757fbSTrond Myklebust  *
11621d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
11631d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
11641d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
11651d6757fbSTrond Myklebust  *
11661d6757fbSTrond Myklebust  */
116765a0c149STrond Myklebust static
1168fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
117165a0c149STrond Myklebust 	int ret;
11721da177e4SLinus Torvalds 
117336d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
11744e99a1ffSTrond Myklebust 		return 0;
117547921921STrond Myklebust 
117647921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
117747921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
117847921921STrond Myklebust 		case S_IFREG:
117947921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
118047921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
118147921921STrond Myklebust 				goto out;
1182df561f66SGustavo A. R. Silva 			fallthrough;
118347921921STrond Myklebust 		case S_IFDIR:
118447921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
118547921921STrond Myklebust 				break;
118647921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
118747921921STrond Myklebust 			goto out_force;
118847921921STrond Myklebust 		}
118947921921STrond Myklebust 	}
119047921921STrond Myklebust 
11911da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1192fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
11931da177e4SLinus Torvalds 		goto out_force;
119465a0c149STrond Myklebust out:
1195a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
11961da177e4SLinus Torvalds out_force:
11971fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
11981fa1e384SNeilBrown 		return -ECHILD;
119965a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
120065a0c149STrond Myklebust 	if (ret != 0)
120165a0c149STrond Myklebust 		return ret;
120265a0c149STrond Myklebust 	goto out;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds /*
12061da177e4SLinus Torvalds  * We judge how long we want to trust negative
12071da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
12081da177e4SLinus Torvalds  *
12091da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
12101da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1211912a108dSNeilBrown  *
1212912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1213912a108dSNeilBrown  * suggesting a reval is needed.
12149f6d44d4STrond Myklebust  *
12159f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
12169f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
12171da177e4SLinus Torvalds  */
12181da177e4SLinus Torvalds static inline
12191da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1220fa3c56bbSAl Viro 		       unsigned int flags)
12211da177e4SLinus Torvalds {
12229f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
12231da177e4SLinus Torvalds 		return 0;
12244eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
12254eec952eSTrond Myklebust 		return 1;
1226912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12295ceb9d7fSTrond Myklebust static int
12305ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
12315ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
12321da177e4SLinus Torvalds {
12335ceb9d7fSTrond Myklebust 	switch (error) {
12345ceb9d7fSTrond Myklebust 	case 1:
12356de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
12366de1472fSAl Viro 			__func__, dentry);
12371da177e4SLinus Torvalds 		return 1;
12385ceb9d7fSTrond Myklebust 	case 0:
1239a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
12401da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
12411da177e4SLinus Torvalds 			/* Purge readdir caches. */
12421da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1243a3f432bfSJ. Bruce Fields 			/*
1244a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1245a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1246a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1247a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1248a3f432bfSJ. Bruce Fields 			 */
1249a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
12505ceb9d7fSTrond Myklebust 				return 1;
12511da177e4SLinus Torvalds 		}
12526de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
12536de1472fSAl Viro 				__func__, dentry);
12541da177e4SLinus Torvalds 		return 0;
12555ceb9d7fSTrond Myklebust 	}
12566de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
12576de1472fSAl Viro 				__func__, dentry, error);
1258e1fb4d05STrond Myklebust 	return error;
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12615ceb9d7fSTrond Myklebust static int
12625ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
12635ceb9d7fSTrond Myklebust 			       unsigned int flags)
12645ceb9d7fSTrond Myklebust {
12655ceb9d7fSTrond Myklebust 	int ret = 1;
12665ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
12675ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
12685ceb9d7fSTrond Myklebust 			return -ECHILD;
12695ceb9d7fSTrond Myklebust 		ret = 0;
12705ceb9d7fSTrond Myklebust 	}
12715ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
12725ceb9d7fSTrond Myklebust }
12735ceb9d7fSTrond Myklebust 
12745ceb9d7fSTrond Myklebust static int
12755ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
12765ceb9d7fSTrond Myklebust 				struct inode *inode)
12775ceb9d7fSTrond Myklebust {
12785ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12795ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
12805ceb9d7fSTrond Myklebust }
12815ceb9d7fSTrond Myklebust 
12825ceb9d7fSTrond Myklebust static int
12835ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
12845ceb9d7fSTrond Myklebust 			     struct inode *inode)
12855ceb9d7fSTrond Myklebust {
12865ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
12875ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
12885ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
1289a1147b82STrond Myklebust 	unsigned long dir_verifier;
12905ceb9d7fSTrond Myklebust 	int ret;
12915ceb9d7fSTrond Myklebust 
12925ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
12935ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
12945ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
12955ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
12965ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
12975ceb9d7fSTrond Myklebust 		goto out;
12985ceb9d7fSTrond Myklebust 
1299a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
1300f7b37b8bSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
13015ceb9d7fSTrond Myklebust 	if (ret < 0) {
1302f7b37b8bSTrond Myklebust 		switch (ret) {
1303f7b37b8bSTrond Myklebust 		case -ESTALE:
1304f7b37b8bSTrond Myklebust 		case -ENOENT:
13055ceb9d7fSTrond Myklebust 			ret = 0;
1306f7b37b8bSTrond Myklebust 			break;
1307f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1308f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1309f7b37b8bSTrond Myklebust 				ret = 1;
1310f7b37b8bSTrond Myklebust 		}
13115ceb9d7fSTrond Myklebust 		goto out;
13125ceb9d7fSTrond Myklebust 	}
13135ceb9d7fSTrond Myklebust 	ret = 0;
13145ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
13155ceb9d7fSTrond Myklebust 		goto out;
13165ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
13175ceb9d7fSTrond Myklebust 		goto out;
13185ceb9d7fSTrond Myklebust 
13195ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
1320a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
13215ceb9d7fSTrond Myklebust 
13225ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
13235ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
13245ceb9d7fSTrond Myklebust 	ret = 1;
13255ceb9d7fSTrond Myklebust out:
13265ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
13275ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
13285ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
13295ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
13305ceb9d7fSTrond Myklebust }
13315ceb9d7fSTrond Myklebust 
13325ceb9d7fSTrond Myklebust /*
13335ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
13345ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
13355ceb9d7fSTrond Myklebust  * lookup.
13365ceb9d7fSTrond Myklebust  *
13375ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
13385ceb9d7fSTrond Myklebust  * we have an inode!
13395ceb9d7fSTrond Myklebust  *
13405ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
13415ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
13425ceb9d7fSTrond Myklebust  */
13435ceb9d7fSTrond Myklebust static int
13445ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
13455ceb9d7fSTrond Myklebust 			 unsigned int flags)
13465ceb9d7fSTrond Myklebust {
13475ceb9d7fSTrond Myklebust 	struct inode *inode;
13485ceb9d7fSTrond Myklebust 	int error;
13495ceb9d7fSTrond Myklebust 
13505ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
13515ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
13525ceb9d7fSTrond Myklebust 
13535ceb9d7fSTrond Myklebust 	if (!inode)
13545ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
13555ceb9d7fSTrond Myklebust 
13565ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
13575ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
13585ceb9d7fSTrond Myklebust 				__func__, dentry);
13595ceb9d7fSTrond Myklebust 		goto out_bad;
13605ceb9d7fSTrond Myklebust 	}
13615ceb9d7fSTrond Myklebust 
1362efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
13635ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
13645ceb9d7fSTrond Myklebust 
13655ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
13665ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
13675ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
13685ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
13695ceb9d7fSTrond Myklebust 		if (error) {
13705ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
13715ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
13725ceb9d7fSTrond Myklebust 			goto out_bad;
13735ceb9d7fSTrond Myklebust 		}
13745ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
13755ceb9d7fSTrond Myklebust 		goto out_valid;
13765ceb9d7fSTrond Myklebust 	}
13775ceb9d7fSTrond Myklebust 
13785ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
13795ceb9d7fSTrond Myklebust 		return -ECHILD;
13805ceb9d7fSTrond Myklebust 
13815ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
13825ceb9d7fSTrond Myklebust 		goto out_bad;
13835ceb9d7fSTrond Myklebust 
13845ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
13855ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
13865ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
13875ceb9d7fSTrond Myklebust 	return error;
13885ceb9d7fSTrond Myklebust out_valid:
13895ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
13905ceb9d7fSTrond Myklebust out_bad:
13915ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
13925ceb9d7fSTrond Myklebust 		return -ECHILD;
13935ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
13945ceb9d7fSTrond Myklebust }
13955ceb9d7fSTrond Myklebust 
13965ceb9d7fSTrond Myklebust static int
1397c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1398c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
13995ceb9d7fSTrond Myklebust {
14005ceb9d7fSTrond Myklebust 	struct dentry *parent;
14015ceb9d7fSTrond Myklebust 	struct inode *dir;
14025ceb9d7fSTrond Myklebust 	int ret;
14035ceb9d7fSTrond Myklebust 
14045ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
14055ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
14065ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
14075ceb9d7fSTrond Myklebust 		if (!dir)
14085ceb9d7fSTrond Myklebust 			return -ECHILD;
1409c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
14105ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
14115ceb9d7fSTrond Myklebust 			return -ECHILD;
14125ceb9d7fSTrond Myklebust 	} else {
14135ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1414c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
14155ceb9d7fSTrond Myklebust 		dput(parent);
14165ceb9d7fSTrond Myklebust 	}
14175ceb9d7fSTrond Myklebust 	return ret;
14185ceb9d7fSTrond Myklebust }
14195ceb9d7fSTrond Myklebust 
1420c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1421c7944ebbSTrond Myklebust {
1422c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1423c7944ebbSTrond Myklebust }
1424c7944ebbSTrond Myklebust 
14251da177e4SLinus Torvalds /*
14262b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1427ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1428ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1429ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1430ecf3d1f1SJeff Layton  *
1431ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1432ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1433ecf3d1f1SJeff Layton  */
1434ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1435ecf3d1f1SJeff Layton {
14362b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
14379cdd1d3fSTrond Myklebust 	int error = 0;
1438ecf3d1f1SJeff Layton 
1439ecf3d1f1SJeff Layton 	/*
1440ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1441ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1442ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1443ecf3d1f1SJeff Layton 	 */
1444ecf3d1f1SJeff Layton 	if (!inode) {
14456de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
14466de1472fSAl Viro 				__func__, dentry);
1447ecf3d1f1SJeff Layton 		return 1;
1448ecf3d1f1SJeff Layton 	}
1449ecf3d1f1SJeff Layton 
1450ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
14516de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
14526de1472fSAl Viro 				__func__, dentry);
1453ecf3d1f1SJeff Layton 		return 0;
1454ecf3d1f1SJeff Layton 	}
1455ecf3d1f1SJeff Layton 
1456b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1457ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1458ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1459ecf3d1f1SJeff Layton 	return !error;
1460ecf3d1f1SJeff Layton }
1461ecf3d1f1SJeff Layton 
1462ecf3d1f1SJeff Layton /*
14631da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
14641da177e4SLinus Torvalds  */
1465fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
14661da177e4SLinus Torvalds {
14676de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
14686de1472fSAl Viro 		dentry, dentry->d_flags);
14691da177e4SLinus Torvalds 
147077f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
14712b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
147277f11192STrond Myklebust 		return 1;
147377f11192STrond Myklebust 
14741da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
14751da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
14761da177e4SLinus Torvalds 		return 1;
14771da177e4SLinus Torvalds 	}
14781751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
14791da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
14801da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
14811da177e4SLinus Torvalds 		return 1;
14821da177e4SLinus Torvalds 	}
14831da177e4SLinus Torvalds 	return 0;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds }
14861da177e4SLinus Torvalds 
14871f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
14881b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
14891b83d707STrond Myklebust {
14901b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
14911f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
149259a707b0STrond Myklebust 	if (inode->i_nlink > 0)
149359a707b0STrond Myklebust 		drop_nlink(inode);
149459a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
149516e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
149616e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
149759a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
149859a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
14991b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
15001b83d707STrond Myklebust }
15011b83d707STrond Myklebust 
15021da177e4SLinus Torvalds /*
15031da177e4SLinus Torvalds  * Called when the dentry loses inode.
15041da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
15051da177e4SLinus Torvalds  */
15061da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
15071da177e4SLinus Torvalds {
150883672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
150983672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
151083672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
151183672d39SNeil Brown 
15121da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1513e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
15141f018458STrond Myklebust 		nfs_drop_nlink(inode);
15151da177e4SLinus Torvalds 	}
15161da177e4SLinus Torvalds 	iput(inode);
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
1519b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1520b1942c5fSAl Viro {
1521b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1522b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1523b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1524b1942c5fSAl Viro 			WARN_ON(1);
1525b1942c5fSAl Viro 		else
1526b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1527b1942c5fSAl Viro 	}
1528b1942c5fSAl Viro }
1529b1942c5fSAl Viro 
1530f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
15311da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1532ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
15331da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
15341da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
153536d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1536b1942c5fSAl Viro 	.d_release	= nfs_d_release,
15371da177e4SLinus Torvalds };
1538ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
15391da177e4SLinus Torvalds 
1540597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	struct dentry *res;
15431da177e4SLinus Torvalds 	struct inode *inode = NULL;
1544e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1545e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
15461775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
1547a1147b82STrond Myklebust 	unsigned long dir_verifier;
15481da177e4SLinus Torvalds 	int error;
15491da177e4SLinus Torvalds 
15506de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
155191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
15521da177e4SLinus Torvalds 
1553130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1554130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
15551da177e4SLinus Torvalds 
1556fd684071STrond Myklebust 	/*
1557fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1558fd684071STrond Myklebust 	 * but don't hash the dentry.
1559fd684071STrond Myklebust 	 */
15609f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1561130f9ab7SAl Viro 		return NULL;
15621da177e4SLinus Torvalds 
1563e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1564e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1565e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1566e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1567e1fb4d05STrond Myklebust 		goto out;
1568e1fb4d05STrond Myklebust 
156914c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
157014c43f76SDavid Quigley 	if (IS_ERR(label))
157114c43f76SDavid Quigley 		goto out;
157214c43f76SDavid Quigley 
1573a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
15746e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1575f7b37b8bSTrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
15761da177e4SLinus Torvalds 	if (error == -ENOENT)
15771da177e4SLinus Torvalds 		goto no_entry;
15781da177e4SLinus Torvalds 	if (error < 0) {
15791da177e4SLinus Torvalds 		res = ERR_PTR(error);
1580bf130914SAl Viro 		goto out_label;
15811da177e4SLinus Torvalds 	}
15821775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1583bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
158403f28e3aSTrond Myklebust 	if (IS_ERR(res))
1585bf130914SAl Viro 		goto out_label;
158654ceac45SDavid Howells 
158763519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
158863519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1589d69ee9b8STrond Myklebust 
15901da177e4SLinus Torvalds no_entry:
159141d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
15929eaef27bSTrond Myklebust 	if (res != NULL) {
15939eaef27bSTrond Myklebust 		if (IS_ERR(res))
1594bf130914SAl Viro 			goto out_label;
15951da177e4SLinus Torvalds 		dentry = res;
15969eaef27bSTrond Myklebust 	}
1597a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
1598bf130914SAl Viro out_label:
15996e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
160014c43f76SDavid Quigley 	nfs4_label_free(label);
16011da177e4SLinus Torvalds out:
1602e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1603e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
16041da177e4SLinus Torvalds 	return res;
16051da177e4SLinus Torvalds }
1606ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
16071da177e4SLinus Torvalds 
160889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
16090b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
16101da177e4SLinus Torvalds 
1611f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
16120ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1613b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
16141da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
16151da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
161636d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1617b1942c5fSAl Viro 	.d_release	= nfs_d_release,
16181da177e4SLinus Torvalds };
161989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
16201da177e4SLinus Torvalds 
16218a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
16228a5e929dSAl Viro {
16238a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
16248a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
16258a5e929dSAl Viro 		res |= FMODE_READ;
16268a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
16278a5e929dSAl Viro 		res |= FMODE_WRITE;
16288a5e929dSAl Viro 	return res;
16298a5e929dSAl Viro }
16308a5e929dSAl Viro 
1631532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1632cd9a1c0eSTrond Myklebust {
1633532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1634cd9a1c0eSTrond Myklebust }
1635cd9a1c0eSTrond Myklebust 
1636cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1637cd9a1c0eSTrond Myklebust {
1638f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1639cd9a1c0eSTrond Myklebust 	return 0;
1640cd9a1c0eSTrond Myklebust }
1641cd9a1c0eSTrond Myklebust 
1642d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
16430dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1644b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1645cd9a1c0eSTrond Myklebust {
16460dd2b474SMiklos Szeredi 	int err;
16470dd2b474SMiklos Szeredi 
1648be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
164930d90494SAl Viro 	if (err)
1650d9585277SAl Viro 		goto out;
1651eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
165230d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1653eaa2b82cSNeilBrown 	else
16549821421aSTrond Myklebust 		err = -EOPENSTALE;
1655cd9a1c0eSTrond Myklebust out:
1656d9585277SAl Viro 	return err;
1657cd9a1c0eSTrond Myklebust }
1658cd9a1c0eSTrond Myklebust 
165973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
166030d90494SAl Viro 		    struct file *file, unsigned open_flags,
166144907d79SAl Viro 		    umode_t mode)
16621da177e4SLinus Torvalds {
1663c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1664cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
16650dd2b474SMiklos Szeredi 	struct dentry *res;
16660dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1667f46e0bd3STrond Myklebust 	struct inode *inode;
16681472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1669c94c0953SAl Viro 	bool switched = false;
167073a09dd9SAl Viro 	int created = 0;
1671898f635cSTrond Myklebust 	int err;
16721da177e4SLinus Torvalds 
16730dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
16742b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
16750dd2b474SMiklos Szeredi 
16761e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
16776de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16781e7cb3dcSChuck Lever 
16799597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
16809597c13bSJeff Layton 	if (err)
16819597c13bSJeff Layton 		return err;
16829597c13bSJeff Layton 
16830dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
16840dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
168500699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
16860dd2b474SMiklos Szeredi 			/*
16870dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
16880dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
16890dd2b474SMiklos Szeredi 			 * again
16900dd2b474SMiklos Szeredi 			 */
1691d9585277SAl Viro 			return -ENOENT;
16920dd2b474SMiklos Szeredi 		}
16931472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
16941da177e4SLinus Torvalds 		goto no_open;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 
16970dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1698d9585277SAl Viro 		return -ENAMETOOLONG;
16991da177e4SLinus Torvalds 
17000dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1701dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1702dff25ddbSAndreas Gruenbacher 
1703dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1704dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1705dff25ddbSAndreas Gruenbacher 
1706536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1707dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
17080dd2b474SMiklos Szeredi 	}
1709536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1710536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1711536e43d1STrond Myklebust 		attr.ia_size = 0;
1712cd9a1c0eSTrond Myklebust 	}
1713cd9a1c0eSTrond Myklebust 
1714c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1715c94c0953SAl Viro 		d_drop(dentry);
1716c94c0953SAl Viro 		switched = true;
1717c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1718c94c0953SAl Viro 					  &dentry->d_name, &wq);
1719c94c0953SAl Viro 		if (IS_ERR(dentry))
1720c94c0953SAl Viro 			return PTR_ERR(dentry);
1721c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1722c94c0953SAl Viro 			return finish_no_open(file, dentry);
1723c94c0953SAl Viro 	}
1724c94c0953SAl Viro 
1725532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
17260dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
17270dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1728d9585277SAl Viro 		goto out;
17290dd2b474SMiklos Szeredi 
17306e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
173173a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
173273a09dd9SAl Viro 	if (created)
173373a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1734275bb307STrond Myklebust 	if (IS_ERR(inode)) {
17350dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
17366e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
17372d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1738d20cb71dSAl Viro 		d_drop(dentry);
17390dd2b474SMiklos Szeredi 		switch (err) {
17401da177e4SLinus Torvalds 		case -ENOENT:
1741774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1742809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17430dd2b474SMiklos Szeredi 			break;
17441788ea6eSJeff Layton 		case -EISDIR:
17456f926b5bSTrond Myklebust 		case -ENOTDIR:
17466f926b5bSTrond Myklebust 			goto no_open;
17471da177e4SLinus Torvalds 		case -ELOOP:
17480dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
17491da177e4SLinus Torvalds 				goto no_open;
17500dd2b474SMiklos Szeredi 			break;
17511da177e4SLinus Torvalds 			/* case -EINVAL: */
17521da177e4SLinus Torvalds 		default:
17530dd2b474SMiklos Szeredi 			break;
17541da177e4SLinus Torvalds 		}
17551da177e4SLinus Torvalds 		goto out;
17561da177e4SLinus Torvalds 	}
17570dd2b474SMiklos Szeredi 
1758b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
17596e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
17602d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1761d9585277SAl Viro out:
1762c94c0953SAl Viro 	if (unlikely(switched)) {
1763c94c0953SAl Viro 		d_lookup_done(dentry);
1764c94c0953SAl Viro 		dput(dentry);
1765c94c0953SAl Viro 	}
1766d9585277SAl Viro 	return err;
17670dd2b474SMiklos Szeredi 
17681da177e4SLinus Torvalds no_open:
17691472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1770c94c0953SAl Viro 	if (switched) {
1771c94c0953SAl Viro 		d_lookup_done(dentry);
1772c94c0953SAl Viro 		if (!res)
1773c94c0953SAl Viro 			res = dentry;
1774c94c0953SAl Viro 		else
1775c94c0953SAl Viro 			dput(dentry);
1776c94c0953SAl Viro 	}
17770dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1778c94c0953SAl Viro 		return PTR_ERR(res);
1779e45198a6SAl Viro 	return finish_no_open(file, res);
17801da177e4SLinus Torvalds }
178189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
17821da177e4SLinus Torvalds 
1783c7944ebbSTrond Myklebust static int
1784c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1785c7944ebbSTrond Myklebust 			  unsigned int flags)
17861da177e4SLinus Torvalds {
1787657e94b6SNick Piggin 	struct inode *inode;
17881da177e4SLinus Torvalds 
1789fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1790c7944ebbSTrond Myklebust 		goto full_reval;
1791eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1792c7944ebbSTrond Myklebust 		goto full_reval;
17932b484297STrond Myklebust 
17942b0143b5SDavid Howells 	inode = d_inode(dentry);
17952b484297STrond Myklebust 
17961da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
17971da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
17981da177e4SLinus Torvalds 	 */
1799c7944ebbSTrond Myklebust 	if (inode == NULL)
1800c7944ebbSTrond Myklebust 		goto full_reval;
180149317a7fSNeilBrown 
1802efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
1803c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1804216d5d06STrond Myklebust 
18051da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
18061da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1807c7944ebbSTrond Myklebust 		goto full_reval;
1808c7944ebbSTrond Myklebust 
18091da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1810c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1811c7944ebbSTrond Myklebust 		goto reval_dentry;
1812c7944ebbSTrond Myklebust 
1813c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1814c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1815c7944ebbSTrond Myklebust 		goto reval_dentry;
18161da177e4SLinus Torvalds 
18170ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1818c7944ebbSTrond Myklebust 	return 1;
1819c7944ebbSTrond Myklebust reval_dentry:
1820c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1821c7944ebbSTrond Myklebust 		return -ECHILD;
182242f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
18230ef97dcfSMiklos Szeredi 
1824c7944ebbSTrond Myklebust full_reval:
1825c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1826c7944ebbSTrond Myklebust }
1827535918f1STrond Myklebust 
1828c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1829c7944ebbSTrond Myklebust {
1830c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1831c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1832c0204fd2STrond Myklebust }
1833c0204fd2STrond Myklebust 
18341da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
18351da177e4SLinus Torvalds 
1836406cd915SBenjamin Coddington struct dentry *
1837406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
18381775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
18391775fd3eSDavid Quigley 				struct nfs4_label *label)
18401da177e4SLinus Torvalds {
1841fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
18422b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
18431da177e4SLinus Torvalds 	struct inode *inode;
1844b0c6108eSAl Viro 	struct dentry *d;
1845406cd915SBenjamin Coddington 	int error;
18461da177e4SLinus Torvalds 
1847fab728e1STrond Myklebust 	d_drop(dentry);
1848fab728e1STrond Myklebust 
18491da177e4SLinus Torvalds 	if (fhandle->size == 0) {
1850f7b37b8bSTrond Myklebust 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
18511da177e4SLinus Torvalds 		if (error)
1852fab728e1STrond Myklebust 			goto out_error;
18531da177e4SLinus Torvalds 	}
18545724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18551da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
18561da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1857a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1858a841b54dSTrond Myklebust 				fattr, NULL, NULL);
18591da177e4SLinus Torvalds 		if (error < 0)
1860fab728e1STrond Myklebust 			goto out_error;
18611da177e4SLinus Torvalds 	}
18621775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1863b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1864fab728e1STrond Myklebust out:
1865fab728e1STrond Myklebust 	dput(parent);
1866406cd915SBenjamin Coddington 	return d;
1867fab728e1STrond Myklebust out_error:
1868fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1869406cd915SBenjamin Coddington 	d = ERR_PTR(error);
1870406cd915SBenjamin Coddington 	goto out;
1871406cd915SBenjamin Coddington }
1872406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1873406cd915SBenjamin Coddington 
1874406cd915SBenjamin Coddington /*
1875406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
1876406cd915SBenjamin Coddington  */
1877406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1878406cd915SBenjamin Coddington 				struct nfs_fattr *fattr,
1879406cd915SBenjamin Coddington 				struct nfs4_label *label)
1880406cd915SBenjamin Coddington {
1881406cd915SBenjamin Coddington 	struct dentry *d;
1882406cd915SBenjamin Coddington 
1883406cd915SBenjamin Coddington 	d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1884406cd915SBenjamin Coddington 	if (IS_ERR(d))
1885406cd915SBenjamin Coddington 		return PTR_ERR(d);
1886406cd915SBenjamin Coddington 
1887406cd915SBenjamin Coddington 	/* Callers don't care */
1888406cd915SBenjamin Coddington 	dput(d);
1889406cd915SBenjamin Coddington 	return 0;
18901da177e4SLinus Torvalds }
1891ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds /*
18941da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
18951da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
18961da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
18971da177e4SLinus Torvalds  * reply path made it appear to have failed.
18981da177e4SLinus Torvalds  */
1899597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1900ebfc3b49SAl Viro 		umode_t mode, bool excl)
19011da177e4SLinus Torvalds {
19021da177e4SLinus Torvalds 	struct iattr attr;
1903ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
19041da177e4SLinus Torvalds 	int error;
19051da177e4SLinus Torvalds 
19061e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
19076de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	attr.ia_mode = mode;
19101da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
19111da177e4SLinus Torvalds 
19128b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
19138867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
19148b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
19151da177e4SLinus Torvalds 	if (error != 0)
19161da177e4SLinus Torvalds 		goto out_err;
19171da177e4SLinus Torvalds 	return 0;
19181da177e4SLinus Torvalds out_err:
19191da177e4SLinus Torvalds 	d_drop(dentry);
19201da177e4SLinus Torvalds 	return error;
19211da177e4SLinus Torvalds }
1922ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds /*
19251da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
19261da177e4SLinus Torvalds  */
1927597d9289SBryan Schumaker int
19281a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	struct iattr attr;
19311da177e4SLinus Torvalds 	int status;
19321da177e4SLinus Torvalds 
19331e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
19346de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	attr.ia_mode = mode;
19371da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
19381da177e4SLinus Torvalds 
19391ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
19401da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
19411ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
19421da177e4SLinus Torvalds 	if (status != 0)
19431da177e4SLinus Torvalds 		goto out_err;
19441da177e4SLinus Torvalds 	return 0;
19451da177e4SLinus Torvalds out_err:
19461da177e4SLinus Torvalds 	d_drop(dentry);
19471da177e4SLinus Torvalds 	return status;
19481da177e4SLinus Torvalds }
1949ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds /*
19521da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
19531da177e4SLinus Torvalds  */
1954597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
19551da177e4SLinus Torvalds {
19561da177e4SLinus Torvalds 	struct iattr attr;
19571da177e4SLinus Torvalds 	int error;
19581da177e4SLinus Torvalds 
19591e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
19606de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
19631da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
19641da177e4SLinus Torvalds 
19651ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
19661da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
19671ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
19681da177e4SLinus Torvalds 	if (error != 0)
19691da177e4SLinus Torvalds 		goto out_err;
19701da177e4SLinus Torvalds 	return 0;
19711da177e4SLinus Torvalds out_err:
19721da177e4SLinus Torvalds 	d_drop(dentry);
19731da177e4SLinus Torvalds 	return error;
19741da177e4SLinus Torvalds }
1975ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
19761da177e4SLinus Torvalds 
1977d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1978d45b9d8bSTrond Myklebust {
1979dc3f4198SAl Viro 	if (simple_positive(dentry))
1980d45b9d8bSTrond Myklebust 		d_delete(dentry);
1981d45b9d8bSTrond Myklebust }
1982d45b9d8bSTrond Myklebust 
1983597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	int error;
19861da177e4SLinus Torvalds 
19871e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
19886de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
19891da177e4SLinus Torvalds 
19901ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
19912b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1992884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
19931da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
19941da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1995ba6c0592STrond Myklebust 		switch (error) {
1996ba6c0592STrond Myklebust 		case 0:
19972b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1998ba6c0592STrond Myklebust 			break;
1999ba6c0592STrond Myklebust 		case -ENOENT:
2000d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2001ba6c0592STrond Myklebust 		}
2002884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2003ba6c0592STrond Myklebust 	} else
2004ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
20051ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	return error;
20081da177e4SLinus Torvalds }
2009ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds /*
20121da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
20131da177e4SLinus Torvalds  * and after checking that the file has only one user.
20141da177e4SLinus Torvalds  *
20151da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
20161da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
20171da177e4SLinus Torvalds  */
20181da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
20191da177e4SLinus Torvalds {
20202b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
20212b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
20221da177e4SLinus Torvalds 	int error = -EBUSY;
20231da177e4SLinus Torvalds 
20246de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
20271da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
20281da177e4SLinus Torvalds 		error = 0;
20291da177e4SLinus Torvalds 		goto out;
20301da177e4SLinus Torvalds 	}
20311da177e4SLinus Torvalds 
20321ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
20331da177e4SLinus Torvalds 	if (inode != NULL) {
2034912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
20351da177e4SLinus Torvalds 		if (error == 0)
20361b83d707STrond Myklebust 			nfs_drop_nlink(inode);
20371da177e4SLinus Torvalds 	} else
2038912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2039d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2040d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
20411ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
20421da177e4SLinus Torvalds out:
20431da177e4SLinus Torvalds 	return error;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
20471da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
20481da177e4SLinus Torvalds  *
20491da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
20501da177e4SLinus Torvalds  */
2051597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	int error;
20541da177e4SLinus Torvalds 	int need_rehash = 0;
20551da177e4SLinus Torvalds 
20561e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
20576de1472fSAl Viro 		dir->i_ino, dentry);
20581da177e4SLinus Torvalds 
20591ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
20601da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
206184d08fa8SAl Viro 	if (d_count(dentry) > 1) {
20621da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2063ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
20642b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
20651da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
20661ca42382STrond Myklebust 		goto out;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
20691da177e4SLinus Torvalds 		__d_drop(dentry);
20701da177e4SLinus Torvalds 		need_rehash = 1;
20711da177e4SLinus Torvalds 	}
20721da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
20731da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
2074d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
20751da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
20761da177e4SLinus Torvalds 	} else if (need_rehash)
20771da177e4SLinus Torvalds 		d_rehash(dentry);
20781ca42382STrond Myklebust out:
20791ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
20801da177e4SLinus Torvalds 	return error;
20811da177e4SLinus Torvalds }
2082ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
20831da177e4SLinus Torvalds 
2084873101b3SChuck Lever /*
2085873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2086873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2087873101b3SChuck Lever  * using prepare_write/commit_write.
2088873101b3SChuck Lever  *
2089873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2090873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2091873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2092873101b3SChuck Lever  * symlink request has completed on the server.
2093873101b3SChuck Lever  *
2094873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2095873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2096873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2097873101b3SChuck Lever  * and move the raw page into its mapping.
2098873101b3SChuck Lever  */
2099597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
21001da177e4SLinus Torvalds {
2101873101b3SChuck Lever 	struct page *page;
2102873101b3SChuck Lever 	char *kaddr;
21031da177e4SLinus Torvalds 	struct iattr attr;
2104873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
21051da177e4SLinus Torvalds 	int error;
21061da177e4SLinus Torvalds 
21071e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
21086de1472fSAl Viro 		dir->i_ino, dentry, symname);
21091da177e4SLinus Torvalds 
2110873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2111873101b3SChuck Lever 		return -ENAMETOOLONG;
21121da177e4SLinus Torvalds 
2113873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2114873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
21151da177e4SLinus Torvalds 
2116e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
211776566991STrond Myklebust 	if (!page)
2118873101b3SChuck Lever 		return -ENOMEM;
2119873101b3SChuck Lever 
2120e8ecde25SAl Viro 	kaddr = page_address(page);
2121873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2122873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2123873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2124873101b3SChuck Lever 
21251ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
212694a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
21271ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2128873101b3SChuck Lever 	if (error != 0) {
21291e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2130873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
21316de1472fSAl Viro 			dentry, symname, error);
21321da177e4SLinus Torvalds 		d_drop(dentry);
2133873101b3SChuck Lever 		__free_page(page);
21341da177e4SLinus Torvalds 		return error;
21351da177e4SLinus Torvalds 	}
21361da177e4SLinus Torvalds 
2137873101b3SChuck Lever 	/*
2138873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2139873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2140873101b3SChuck Lever 	 */
21412b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2142873101b3SChuck Lever 							GFP_KERNEL)) {
2143873101b3SChuck Lever 		SetPageUptodate(page);
2144873101b3SChuck Lever 		unlock_page(page);
2145a0b54addSRafael Aquini 		/*
2146a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2147a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2148a0b54addSRafael Aquini 		 */
214909cbfeafSKirill A. Shutemov 		put_page(page);
2150873101b3SChuck Lever 	} else
2151873101b3SChuck Lever 		__free_page(page);
2152873101b3SChuck Lever 
2153873101b3SChuck Lever 	return 0;
2154873101b3SChuck Lever }
2155ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2156873101b3SChuck Lever 
2157597d9289SBryan Schumaker int
21581da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
21591da177e4SLinus Torvalds {
21602b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
21611da177e4SLinus Torvalds 	int error;
21621da177e4SLinus Torvalds 
21636de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
21646de1472fSAl Viro 		old_dentry, dentry);
21651da177e4SLinus Torvalds 
21661fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
21679697d234STrond Myklebust 	d_drop(dentry);
21681da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2169cf809556STrond Myklebust 	if (error == 0) {
21707de9c6eeSAl Viro 		ihold(inode);
21719697d234STrond Myklebust 		d_add(dentry, inode);
2172cf809556STrond Myklebust 	}
21731fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
21741da177e4SLinus Torvalds 	return error;
21751da177e4SLinus Torvalds }
2176ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds /*
21791da177e4SLinus Torvalds  * RENAME
21801da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
21811da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
21821da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
21831da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
21841da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
21851da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
21861da177e4SLinus Torvalds  *
21871da177e4SLinus Torvalds  * FIXED.
21881da177e4SLinus Torvalds  *
21891da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
21901da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
21911da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
21921da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
21931da177e4SLinus Torvalds  * using the inode layer
21941da177e4SLinus Torvalds  *
21951da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
21961da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
21971da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
21981da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
21991da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
22001da177e4SLinus Torvalds  * the rename.
22011da177e4SLinus Torvalds  */
2202597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
22031cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
22041cd66c93SMiklos Szeredi 	       unsigned int flags)
22051da177e4SLinus Torvalds {
22062b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
22072b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2208d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
220980a491fdSJeff Layton 	struct rpc_task *task;
22101da177e4SLinus Torvalds 	int error = -EBUSY;
22111da177e4SLinus Torvalds 
22121cd66c93SMiklos Szeredi 	if (flags)
22131cd66c93SMiklos Szeredi 		return -EINVAL;
22141cd66c93SMiklos Szeredi 
22156de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
22166de1472fSAl Viro 		 old_dentry, new_dentry,
221784d08fa8SAl Viro 		 d_count(new_dentry));
22181da177e4SLinus Torvalds 
221970ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
22201da177e4SLinus Torvalds 	/*
222128f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
222228f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
222328f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
222428f79a1aSMiklos Szeredi 	 * the new target.
22251da177e4SLinus Torvalds 	 */
222627226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
222727226104SMiklos Szeredi 		/*
222827226104SMiklos Szeredi 		 * To prevent any new references to the target during the
222927226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
223027226104SMiklos Szeredi 		 */
2231d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
223227226104SMiklos Szeredi 			d_drop(new_dentry);
2233d9f29500SBenjamin Coddington 			rehash = new_dentry;
2234d9f29500SBenjamin Coddington 		}
223527226104SMiklos Szeredi 
223684d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
22371da177e4SLinus Torvalds 			int err;
223827226104SMiklos Szeredi 
22391da177e4SLinus Torvalds 			/* copy the target dentry's name */
22401da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
22411da177e4SLinus Torvalds 					 &new_dentry->d_name);
22421da177e4SLinus Torvalds 			if (!dentry)
22431da177e4SLinus Torvalds 				goto out;
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
22461da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
224724e93025SMiklos Szeredi 			if (err)
22481da177e4SLinus Torvalds 				goto out;
224924e93025SMiklos Szeredi 
225024e93025SMiklos Szeredi 			new_dentry = dentry;
2251d9f29500SBenjamin Coddington 			rehash = NULL;
225224e93025SMiklos Szeredi 			new_inode = NULL;
2253b1e4adf4STrond Myklebust 		}
225427226104SMiklos Szeredi 	}
22551da177e4SLinus Torvalds 
2256d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
225780a491fdSJeff Layton 	if (IS_ERR(task)) {
225880a491fdSJeff Layton 		error = PTR_ERR(task);
225980a491fdSJeff Layton 		goto out;
226080a491fdSJeff Layton 	}
226180a491fdSJeff Layton 
226280a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2263818a8dbeSBenjamin Coddington 	if (error != 0) {
2264818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2265818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2266818a8dbeSBenjamin Coddington 		smp_wmb();
2267818a8dbeSBenjamin Coddington 	} else
226880a491fdSJeff Layton 		error = task->tk_status;
226980a491fdSJeff Layton 	rpc_put_task(task);
227059a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
227159a707b0STrond Myklebust 	if (error == 0) {
227259a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
227359a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
227459a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
227559a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
227659a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
227759a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
227859a707b0STrond Myklebust 	}
22791da177e4SLinus Torvalds out:
2280d9f29500SBenjamin Coddington 	if (rehash)
2281d9f29500SBenjamin Coddington 		d_rehash(rehash);
228270ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
228370ded201STrond Myklebust 			new_dir, new_dentry, error);
2284d9f29500SBenjamin Coddington 	if (!error) {
2285d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2286d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2287d9f29500SBenjamin Coddington 		/*
2288d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2289d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2290d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2291d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2292d9f29500SBenjamin Coddington 		 */
2293d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2294d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2295d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2296d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2297d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2298d9f29500SBenjamin Coddington 
22991da177e4SLinus Torvalds 	/* new dentry created? */
23001da177e4SLinus Torvalds 	if (dentry)
23011da177e4SLinus Torvalds 		dput(dentry);
23021da177e4SLinus Torvalds 	return error;
23031da177e4SLinus Torvalds }
2304ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
23051da177e4SLinus Torvalds 
2306cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2307cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2308cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2309cfcea3e8STrond Myklebust 
2310a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
23113a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
23123a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
23133a505845STrond Myklebust 
23141c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
23151c3c07e9STrond Myklebust {
2316b68572e0SNeilBrown 	put_cred(entry->cred);
2317f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
23184e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2319cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
23204e857c58SPeter Zijlstra 	smp_mb__after_atomic();
23211c3c07e9STrond Myklebust }
23221c3c07e9STrond Myklebust 
23231a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
23241a81bb8aSTrond Myklebust {
23251a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
23261a81bb8aSTrond Myklebust 
23271a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
23281a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
23291a81bb8aSTrond Myklebust 		list_del(&cache->lru);
23301a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
23311a81bb8aSTrond Myklebust 	}
23321a81bb8aSTrond Myklebust }
23331a81bb8aSTrond Myklebust 
23343a505845STrond Myklebust static unsigned long
23353a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2336979df72eSTrond Myklebust {
2337979df72eSTrond Myklebust 	LIST_HEAD(head);
2338aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2339979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
23401ab6c499SDave Chinner 	long freed = 0;
2341979df72eSTrond Myklebust 
2342a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2343aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2344979df72eSTrond Myklebust 		struct inode *inode;
2345979df72eSTrond Myklebust 
2346979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2347979df72eSTrond Myklebust 			break;
23489c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2349979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2350979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2351979df72eSTrond Myklebust 			goto remove_lru_entry;
2352979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2353979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2354979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2355979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
23561ab6c499SDave Chinner 		freed++;
2357979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2358979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2359979df72eSTrond Myklebust 					&nfs_access_lru_list);
2360979df72eSTrond Myklebust 		else {
2361979df72eSTrond Myklebust remove_lru_entry:
2362979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
23634e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2364979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
23654e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2366979df72eSTrond Myklebust 		}
236759844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2368979df72eSTrond Myklebust 	}
2369979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
23701a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
23711ab6c499SDave Chinner 	return freed;
23721ab6c499SDave Chinner }
23731ab6c499SDave Chinner 
23741ab6c499SDave Chinner unsigned long
23753a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
23763a505845STrond Myklebust {
23773a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
23783a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
23793a505845STrond Myklebust 
23803a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
23813a505845STrond Myklebust 		return SHRINK_STOP;
23823a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
23833a505845STrond Myklebust }
23843a505845STrond Myklebust 
23853a505845STrond Myklebust 
23863a505845STrond Myklebust unsigned long
23871ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
23881ab6c499SDave Chinner {
238955f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2390979df72eSTrond Myklebust }
2391979df72eSTrond Myklebust 
23923a505845STrond Myklebust static void
23933a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
23943a505845STrond Myklebust {
23953a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
23963a505845STrond Myklebust 	unsigned long diff;
23973a505845STrond Myklebust 	unsigned int nr_to_scan;
23983a505845STrond Myklebust 
23993a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
24003a505845STrond Myklebust 		return;
24013a505845STrond Myklebust 	nr_to_scan = 100;
24023a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
24033a505845STrond Myklebust 	if (diff < nr_to_scan)
24043a505845STrond Myklebust 		nr_to_scan = diff;
24053a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
24063a505845STrond Myklebust }
24073a505845STrond Myklebust 
24081a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
24091c3c07e9STrond Myklebust {
24101c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
24111a81bb8aSTrond Myklebust 	struct rb_node *n;
24121c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
24131c3c07e9STrond Myklebust 
24141c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
24151c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
24161c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
24171c3c07e9STrond Myklebust 		rb_erase(n, root_node);
24181a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
24191c3c07e9STrond Myklebust 	}
24201c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
24211c3c07e9STrond Myklebust }
24221c3c07e9STrond Myklebust 
24231c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
24241c3c07e9STrond Myklebust {
24251a81bb8aSTrond Myklebust 	LIST_HEAD(head);
24261a81bb8aSTrond Myklebust 
24271a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
24281a81bb8aSTrond Myklebust 		return;
2429cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2430cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
24311a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2432cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2433cfcea3e8STrond Myklebust 
24341c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
24351a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
24361a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
24371a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
24381a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
24391c3c07e9STrond Myklebust }
24401c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
24411c3c07e9STrond Myklebust 
2442b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
24431c3c07e9STrond Myklebust {
24441c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
24451c3c07e9STrond Myklebust 
24461c3c07e9STrond Myklebust 	while (n != NULL) {
2447b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2448b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2449b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
24501c3c07e9STrond Myklebust 
2451b68572e0SNeilBrown 		if (cmp < 0)
24521c3c07e9STrond Myklebust 			n = n->rb_left;
2453b68572e0SNeilBrown 		else if (cmp > 0)
24541c3c07e9STrond Myklebust 			n = n->rb_right;
24551c3c07e9STrond Myklebust 		else
24561c3c07e9STrond Myklebust 			return entry;
24571c3c07e9STrond Myklebust 	}
24581c3c07e9STrond Myklebust 	return NULL;
24591c3c07e9STrond Myklebust }
24601c3c07e9STrond Myklebust 
2461d2ae4f8bSFrank van der Linden static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
24621da177e4SLinus Torvalds {
246355296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
24641c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
246557b69181STrond Myklebust 	bool retry = true;
246657b69181STrond Myklebust 	int err;
24671da177e4SLinus Torvalds 
24681c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
246957b69181STrond Myklebust 	for(;;) {
24701c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
24711c3c07e9STrond Myklebust 			goto out_zap;
24721c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
247357b69181STrond Myklebust 		err = -ENOENT;
24741c3c07e9STrond Myklebust 		if (cache == NULL)
24751c3c07e9STrond Myklebust 			goto out;
247657b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
247721c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
247857b69181STrond Myklebust 			break;
24795c965db8STrond Myklebust 		if (!retry)
24805c965db8STrond Myklebust 			break;
248157b69181STrond Myklebust 		err = -ECHILD;
248257b69181STrond Myklebust 		if (!may_block)
248357b69181STrond Myklebust 			goto out;
248457b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
248557b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
248657b69181STrond Myklebust 		if (err)
248757b69181STrond Myklebust 			return err;
248857b69181STrond Myklebust 		spin_lock(&inode->i_lock);
248957b69181STrond Myklebust 		retry = false;
249057b69181STrond Myklebust 	}
24911c3c07e9STrond Myklebust 	res->cred = cache->cred;
24921c3c07e9STrond Myklebust 	res->mask = cache->mask;
2493cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
24941c3c07e9STrond Myklebust 	err = 0;
24951c3c07e9STrond Myklebust out:
24961c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24971c3c07e9STrond Myklebust 	return err;
24981c3c07e9STrond Myklebust out_zap:
24991a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
25001a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
25011c3c07e9STrond Myklebust 	return -ENOENT;
25021c3c07e9STrond Myklebust }
25031c3c07e9STrond Myklebust 
2504b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2505f682a398SNeilBrown {
2506f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2507f682a398SNeilBrown 	 * but do it without locking.
2508f682a398SNeilBrown 	 */
2509f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2510f682a398SNeilBrown 	struct nfs_access_entry *cache;
2511f682a398SNeilBrown 	int err = -ECHILD;
2512f682a398SNeilBrown 	struct list_head *lh;
2513f682a398SNeilBrown 
2514f682a398SNeilBrown 	rcu_read_lock();
2515f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2516f682a398SNeilBrown 		goto out;
25179f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2518f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2519f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
25209a206de2STrond Myklebust 	    cred_fscmp(cred, cache->cred) != 0)
2521f682a398SNeilBrown 		cache = NULL;
2522f682a398SNeilBrown 	if (cache == NULL)
2523f682a398SNeilBrown 		goto out;
252421c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2525f682a398SNeilBrown 		goto out;
2526f682a398SNeilBrown 	res->cred = cache->cred;
2527f682a398SNeilBrown 	res->mask = cache->mask;
252821c3ba7eSTrond Myklebust 	err = 0;
2529f682a398SNeilBrown out:
2530f682a398SNeilBrown 	rcu_read_unlock();
2531f682a398SNeilBrown 	return err;
2532f682a398SNeilBrown }
2533f682a398SNeilBrown 
2534d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2535d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block)
2536d2ae4f8bSFrank van der Linden {
2537d2ae4f8bSFrank van der Linden 	int status;
2538d2ae4f8bSFrank van der Linden 
2539d2ae4f8bSFrank van der Linden 	status = nfs_access_get_cached_rcu(inode, cred, res);
2540d2ae4f8bSFrank van der Linden 	if (status != 0)
2541d2ae4f8bSFrank van der Linden 		status = nfs_access_get_cached_locked(inode, cred, res,
2542d2ae4f8bSFrank van der Linden 		    may_block);
2543d2ae4f8bSFrank van der Linden 
2544d2ae4f8bSFrank van der Linden 	return status;
2545d2ae4f8bSFrank van der Linden }
2546d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2547d2ae4f8bSFrank van der Linden 
25481c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
25491c3c07e9STrond Myklebust {
2550cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2551cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
25521c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
25531c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
25541c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2555b68572e0SNeilBrown 	int cmp;
25561c3c07e9STrond Myklebust 
25571c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
25581c3c07e9STrond Myklebust 	while (*p != NULL) {
25591c3c07e9STrond Myklebust 		parent = *p;
25601c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2561b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
25621c3c07e9STrond Myklebust 
2563b68572e0SNeilBrown 		if (cmp < 0)
25641c3c07e9STrond Myklebust 			p = &parent->rb_left;
2565b68572e0SNeilBrown 		else if (cmp > 0)
25661c3c07e9STrond Myklebust 			p = &parent->rb_right;
25671c3c07e9STrond Myklebust 		else
25681c3c07e9STrond Myklebust 			goto found;
25691c3c07e9STrond Myklebust 	}
25701c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
25711c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2572cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
25731c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
25741c3c07e9STrond Myklebust 	return;
25751c3c07e9STrond Myklebust found:
25761c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2577cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2578cfcea3e8STrond Myklebust 	list_del(&entry->lru);
25791c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
25801c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25836168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
25841da177e4SLinus Torvalds {
25851c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
25861c3c07e9STrond Myklebust 	if (cache == NULL)
25871c3c07e9STrond Myklebust 		return;
25881c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2589b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
25901da177e4SLinus Torvalds 	cache->mask = set->mask;
25911c3c07e9STrond Myklebust 
2592f682a398SNeilBrown 	/* The above field assignments must be visible
2593f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2594f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2595f682a398SNeilBrown 	 */
2596f682a398SNeilBrown 	smp_wmb();
25971c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2598cfcea3e8STrond Myklebust 
2599cfcea3e8STrond Myklebust 	/* Update accounting */
26004e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2601cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
26024e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2603cfcea3e8STrond Myklebust 
2604cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
26051a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2606cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
26071a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
26081a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
26091a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2610cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2611cfcea3e8STrond Myklebust 	}
26123a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
26131da177e4SLinus Torvalds }
26146168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
26156168f62cSWeston Andros Adamson 
26163c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
26173c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
26183c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
26193c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
26203c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
26213c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2622ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
26233c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
26243c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
262515d4b73aSTrond Myklebust static int
2626ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
262715d4b73aSTrond Myklebust {
262815d4b73aSTrond Myklebust 	int mask = 0;
262915d4b73aSTrond Myklebust 
263015d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
263115d4b73aSTrond Myklebust 		mask |= MAY_READ;
2632ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2633ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
263415d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2635ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
263615d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2637ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2638ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2639ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2640ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
264115d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2642ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2643ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
264415d4b73aSTrond Myklebust 	return mask;
264515d4b73aSTrond Myklebust }
264615d4b73aSTrond Myklebust 
26476168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
26486168f62cSWeston Andros Adamson {
2649bd8b2441STrond Myklebust 	entry->mask = access_result;
26506168f62cSWeston Andros Adamson }
26516168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
26521da177e4SLinus Torvalds 
2653b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
26541da177e4SLinus Torvalds {
26551da177e4SLinus Torvalds 	struct nfs_access_entry cache;
265657b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2657e8194b7dSTrond Myklebust 	int cache_mask = -1;
26581da177e4SLinus Torvalds 	int status;
26591da177e4SLinus Torvalds 
2660f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2661f4ce1299STrond Myklebust 
266257b69181STrond Myklebust 	status = nfs_access_get_cached(inode, cred, &cache, may_block);
26631da177e4SLinus Torvalds 	if (status == 0)
2664f4ce1299STrond Myklebust 		goto out_cached;
26651da177e4SLinus Torvalds 
2666f3324a2aSNeilBrown 	status = -ECHILD;
266757b69181STrond Myklebust 	if (!may_block)
2668f3324a2aSNeilBrown 		goto out;
2669f3324a2aSNeilBrown 
26701750d929SAnna Schumaker 	/*
26711750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
26721750d929SAnna Schumaker 	 */
26731750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
267472832a24SFrank van der Linden 	if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
267572832a24SFrank van der Linden 		cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
267672832a24SFrank van der Linden 		    NFS_ACCESS_XALIST;
267772832a24SFrank van der Linden 	}
26781750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
26791750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
26801750d929SAnna Schumaker 	else
26811750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
26821da177e4SLinus Torvalds 	cache.cred = cred;
26831da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2684a71ee337SSuresh Jayaraman 	if (status != 0) {
2685a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2686a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
268793ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
268893ce4af7STrond Myklebust 			else
268993ce4af7STrond Myklebust 				nfs_zap_caches(inode);
2690a71ee337SSuresh Jayaraman 		}
2691f4ce1299STrond Myklebust 		goto out;
2692a71ee337SSuresh Jayaraman 	}
26931da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2694f4ce1299STrond Myklebust out_cached:
2695ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2696bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2697f4ce1299STrond Myklebust 		status = -EACCES;
26981da177e4SLinus Torvalds out:
2699e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
2700f4ce1299STrond Myklebust 	return status;
27011da177e4SLinus Torvalds }
27021da177e4SLinus Torvalds 
2703af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2704af22f94aSTrond Myklebust {
2705af22f94aSTrond Myklebust 	int mask = 0;
2706af22f94aSTrond Myklebust 
2707f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2708f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2709f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2710f8d9a897SWeston Andros Adamson 	} else {
27118a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2712af22f94aSTrond Myklebust 			mask |= MAY_READ;
27138a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2714af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2715f8d9a897SWeston Andros Adamson 	}
2716f8d9a897SWeston Andros Adamson 
2717af22f94aSTrond Myklebust 	return mask;
2718af22f94aSTrond Myklebust }
2719af22f94aSTrond Myklebust 
2720b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2721af22f94aSTrond Myklebust {
2722af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2723af22f94aSTrond Myklebust }
272489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2725af22f94aSTrond Myklebust 
27265c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
27275c5fc09aSTrond Myklebust {
27285c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
272921c3ba7eSTrond Myklebust 	int ret = 0;
27305c5fc09aSTrond Myklebust 
27313825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
27323825827eSTrond Myklebust 		return 0;
2733cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
27345c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
273521c3ba7eSTrond Myklebust 			return -ECHILD;
273621c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
273721c3ba7eSTrond Myklebust 	}
27385c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
27395c5fc09aSTrond Myklebust 		ret = -EACCES;
27405c5fc09aSTrond Myklebust 	return ret;
27415c5fc09aSTrond Myklebust }
27425c5fc09aSTrond Myklebust 
274310556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
27441da177e4SLinus Torvalds {
2745b68572e0SNeilBrown 	const struct cred *cred = current_cred();
27461da177e4SLinus Torvalds 	int res = 0;
27471da177e4SLinus Torvalds 
274891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
274991d5b470SChuck Lever 
2750e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
27511da177e4SLinus Torvalds 		goto out;
27521da177e4SLinus Torvalds 	/* Is this sys_access() ? */
27539cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
27541da177e4SLinus Torvalds 		goto force_lookup;
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
27571da177e4SLinus Torvalds 		case S_IFLNK:
27581da177e4SLinus Torvalds 			goto out;
27591da177e4SLinus Torvalds 		case S_IFREG:
2760762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2761762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2762762674f8STrond Myklebust 				return 0;
27631da177e4SLinus Torvalds 			break;
27641da177e4SLinus Torvalds 		case S_IFDIR:
27651da177e4SLinus Torvalds 			/*
27661da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
27671da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
27681da177e4SLinus Torvalds 			 */
27691da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
27701da177e4SLinus Torvalds 				goto out;
27711da177e4SLinus Torvalds 	}
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds force_lookup:
27741da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
27751da177e4SLinus Torvalds 		goto out_notsup;
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
27781da177e4SLinus Torvalds out:
27795c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
27805c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2781f696a365SMiklos Szeredi 
27821e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
27831e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
27841da177e4SLinus Torvalds 	return res;
27851da177e4SLinus Torvalds out_notsup:
2786d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2787d51ac1a8SNeilBrown 		return -ECHILD;
2788d51ac1a8SNeilBrown 
27891da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
27901da177e4SLinus Torvalds 	if (res == 0)
27912830ba7fSAl Viro 		res = generic_permission(inode, mask);
27921e7cb3dcSChuck Lever 	goto out;
27931da177e4SLinus Torvalds }
2794ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds /*
27971da177e4SLinus Torvalds  * Local variables:
27981da177e4SLinus Torvalds  *  version-control: t
27991da177e4SLinus Torvalds  *  kept-new-versions: 5
28001da177e4SLinus Torvalds  * End:
28011da177e4SLinus Torvalds  */
2802