xref: /openbmc/linux/fs/nfs/dir.c (revision a46126ccc77e764429d63bf958d117f607f4b6c6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
301da177e4SLinus Torvalds #include <linux/nfs_fs.h>
311da177e4SLinus Torvalds #include <linux/nfs_mount.h>
321da177e4SLinus Torvalds #include <linux/pagemap.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
36a0b8cab3SMel Gorman #include <linux/swap.h>
37e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "delegation.h"
4291d5b470SChuck Lever #include "iostat.h"
434c30d56eSAdrian Bunk #include "internal.h"
44cd9a1c0eSTrond Myklebust #include "fscache.h"
451da177e4SLinus Torvalds 
46f4ce1299STrond Myklebust #include "nfstrace.h"
47f4ce1299STrond Myklebust 
481da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
51480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5223db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
54f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5511de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
561da177e4SLinus Torvalds 
574b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
58f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
591da177e4SLinus Torvalds 	.read		= generic_read_dir,
60b044f645SBenjamin Coddington 	.iterate	= nfs_readdir,
611da177e4SLinus Torvalds 	.open		= nfs_opendir,
62480c2006SBryan Schumaker 	.release	= nfs_closedir,
631da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
6611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6711de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
68d1bacf9eSBryan Schumaker };
69d1bacf9eSBryan Schumaker 
70684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
71480c2006SBryan Schumaker {
72311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
73480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
74480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
75480c2006SBryan Schumaker 	if (ctx != NULL) {
768ef2ce3eSBryan Schumaker 		ctx->duped = 0;
77311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
78480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
798ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
80684f39b4SNeilBrown 		ctx->cred = get_cred(cred);
81311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
82311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
83311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
84480c2006SBryan Schumaker 		return ctx;
85480c2006SBryan Schumaker 	}
860c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
870c030806STrond Myklebust }
88480c2006SBryan Schumaker 
89311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
90480c2006SBryan Schumaker {
91311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
92311324adSTrond Myklebust 	list_del(&ctx->list);
93311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
94684f39b4SNeilBrown 	put_cred(ctx->cred);
95480c2006SBryan Schumaker 	kfree(ctx);
96480c2006SBryan Schumaker }
97480c2006SBryan Schumaker 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Open file
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds static int
1021da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1031da177e4SLinus Torvalds {
104480c2006SBryan Schumaker 	int res = 0;
105480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1061da177e4SLinus Torvalds 
1076de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
108cc0dd2d1SChuck Lever 
109cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1101e7cb3dcSChuck Lever 
111684f39b4SNeilBrown 	ctx = alloc_nfs_open_dir_context(inode, current_cred());
112480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
113480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
114480c2006SBryan Schumaker 		goto out;
115480c2006SBryan Schumaker 	}
116480c2006SBryan Schumaker 	filp->private_data = ctx;
117480c2006SBryan Schumaker out:
1181da177e4SLinus Torvalds 	return res;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
121480c2006SBryan Schumaker static int
122480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
123480c2006SBryan Schumaker {
124a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
125480c2006SBryan Schumaker 	return 0;
126480c2006SBryan Schumaker }
127480c2006SBryan Schumaker 
128d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
129d1bacf9eSBryan Schumaker 	u64 cookie;
130d1bacf9eSBryan Schumaker 	u64 ino;
131d1bacf9eSBryan Schumaker 	struct qstr string;
1320b26a0bfSTrond Myklebust 	unsigned char d_type;
133d1bacf9eSBryan Schumaker };
134d1bacf9eSBryan Schumaker 
135d1bacf9eSBryan Schumaker struct nfs_cache_array {
13688b8e133SChuck Lever 	int size;
137d1bacf9eSBryan Schumaker 	int eof_index;
138d1bacf9eSBryan Schumaker 	u64 last_cookie;
139d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
140d1bacf9eSBryan Schumaker };
141d1bacf9eSBryan Schumaker 
142be4c2d47Sluanshi struct readdirvec {
143be4c2d47Sluanshi 	unsigned long nr;
144be4c2d47Sluanshi 	unsigned long index;
145be4c2d47Sluanshi 	struct page *pages[NFS_MAX_READDIR_RAPAGES];
146be4c2d47Sluanshi };
147be4c2d47Sluanshi 
148a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
1491da177e4SLinus Torvalds typedef struct {
1501da177e4SLinus Torvalds 	struct file	*file;
1511da177e4SLinus Torvalds 	struct page	*page;
15223db8620SAl Viro 	struct dir_context *ctx;
1531da177e4SLinus Torvalds 	unsigned long	page_index;
154be4c2d47Sluanshi 	struct readdirvec pvec;
155f0dd2136STrond Myklebust 	u64		*dir_cookie;
1560aded708STrond Myklebust 	u64		last_cookie;
157f0dd2136STrond Myklebust 	loff_t		current_index;
1581da177e4SLinus Torvalds 	decode_dirent_t	decode;
159d1bacf9eSBryan Schumaker 
1601f4eab7eSNeil Brown 	unsigned long	timestamp;
1614704f0e2STrond Myklebust 	unsigned long	gencount;
162d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
163a7a3b1e9SBenjamin Coddington 	bool plus;
164a7a3b1e9SBenjamin Coddington 	bool eof;
1651da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1661da177e4SLinus Torvalds 
167d1bacf9eSBryan Schumaker /*
168d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
169d1bacf9eSBryan Schumaker  */
170d1bacf9eSBryan Schumaker static
17111de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
172d1bacf9eSBryan Schumaker {
17311de3b11STrond Myklebust 	struct nfs_cache_array *array;
174d1bacf9eSBryan Schumaker 	int i;
1758cd51a0cSTrond Myklebust 
1762b86ce2dSCong Wang 	array = kmap_atomic(page);
177d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
178d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1792b86ce2dSCong Wang 	kunmap_atomic(array);
180d1bacf9eSBryan Schumaker }
181d1bacf9eSBryan Schumaker 
182d1bacf9eSBryan Schumaker /*
183d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
184d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
185d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
186d1bacf9eSBryan Schumaker  */
187d1bacf9eSBryan Schumaker static
1884a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
189d1bacf9eSBryan Schumaker {
190d1bacf9eSBryan Schumaker 	string->len = len;
191d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
1924a201d6eSTrond Myklebust 	if (string->name == NULL)
1934a201d6eSTrond Myklebust 		return -ENOMEM;
19404e4bd1cSCatalin Marinas 	/*
19504e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
19604e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
19704e4bd1cSCatalin Marinas 	 */
19804e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
1998387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2004a201d6eSTrond Myklebust 	return 0;
201d1bacf9eSBryan Schumaker }
202d1bacf9eSBryan Schumaker 
203d1bacf9eSBryan Schumaker static
204d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
205d1bacf9eSBryan Schumaker {
2060795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2074a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2084a201d6eSTrond Myklebust 	int ret;
2094a201d6eSTrond Myklebust 
2104a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2113020093fSTrond Myklebust 
2123020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2133020093fSTrond Myklebust 	ret = -ENOSPC;
2143020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2153020093fSTrond Myklebust 		goto out;
2163020093fSTrond Myklebust 
2174a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2184a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2190b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2204a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2214a201d6eSTrond Myklebust 	if (ret)
2224a201d6eSTrond Myklebust 		goto out;
223d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2248cd51a0cSTrond Myklebust 	array->size++;
22547c716cbSTrond Myklebust 	if (entry->eof != 0)
226d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2274a201d6eSTrond Myklebust out:
2280795bf83SFabian Frederick 	kunmap(page);
2294a201d6eSTrond Myklebust 	return ret;
230d1bacf9eSBryan Schumaker }
231d1bacf9eSBryan Schumaker 
232d1bacf9eSBryan Schumaker static
233d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
234d1bacf9eSBryan Schumaker {
23523db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
236d1bacf9eSBryan Schumaker 	unsigned int index;
237d1bacf9eSBryan Schumaker 
238d1bacf9eSBryan Schumaker 	if (diff < 0)
239d1bacf9eSBryan Schumaker 		goto out_eof;
240d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2418cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
242d1bacf9eSBryan Schumaker 			goto out_eof;
243d1bacf9eSBryan Schumaker 		return -EAGAIN;
244d1bacf9eSBryan Schumaker 	}
245d1bacf9eSBryan Schumaker 
246d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
247d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
248d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
249d1bacf9eSBryan Schumaker 	return 0;
250d1bacf9eSBryan Schumaker out_eof:
2516089dd0dSThomas Meyer 	desc->eof = true;
252d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
253d1bacf9eSBryan Schumaker }
254d1bacf9eSBryan Schumaker 
2554db72b40SJeff Layton static bool
2564db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2574db72b40SJeff Layton {
2584db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2594db72b40SJeff Layton 		return false;
2604db72b40SJeff Layton 	smp_rmb();
2614db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2624db72b40SJeff Layton }
2634db72b40SJeff Layton 
264d1bacf9eSBryan Schumaker static
265d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
266d1bacf9eSBryan Schumaker {
267d1bacf9eSBryan Schumaker 	int i;
2688ef2ce3eSBryan Schumaker 	loff_t new_pos;
269d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
270d1bacf9eSBryan Schumaker 
271d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2728cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
273496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2740c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2750c030806STrond Myklebust 
2768ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2774db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
2784db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
2790c030806STrond Myklebust 				ctx->duped = 0;
2800c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
28123db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2820c030806STrond Myklebust 				if (ctx->duped > 0
2830c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2840c030806STrond Myklebust 					if (printk_ratelimit()) {
2856de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
2860c030806STrond Myklebust 								"Please contact your server vendor.  "
2879581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
2889581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
2899581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
2900c030806STrond Myklebust 					}
2910c030806STrond Myklebust 					status = -ELOOP;
2920c030806STrond Myklebust 					goto out;
2930c030806STrond Myklebust 				}
2948ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
2950c030806STrond Myklebust 				ctx->duped = -1;
2968ef2ce3eSBryan Schumaker 			}
29723db8620SAl Viro 			desc->ctx->pos = new_pos;
2988cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
29947c716cbSTrond Myklebust 			return 0;
3008cd51a0cSTrond Myklebust 		}
3018cd51a0cSTrond Myklebust 	}
30247c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
303d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
30418fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
3056089dd0dSThomas Meyer 			desc->eof = true;
306d1bacf9eSBryan Schumaker 	}
3070c030806STrond Myklebust out:
308d1bacf9eSBryan Schumaker 	return status;
309d1bacf9eSBryan Schumaker }
310d1bacf9eSBryan Schumaker 
311d1bacf9eSBryan Schumaker static
312d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
313d1bacf9eSBryan Schumaker {
314d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
31547c716cbSTrond Myklebust 	int status;
316d1bacf9eSBryan Schumaker 
3170795bf83SFabian Frederick 	array = kmap(desc->page);
318d1bacf9eSBryan Schumaker 
319d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
320d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
321d1bacf9eSBryan Schumaker 	else
322d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
323d1bacf9eSBryan Schumaker 
32447c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3250aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
326e47c085aSTrond Myklebust 		desc->current_index += array->size;
32747c716cbSTrond Myklebust 		desc->page_index++;
32847c716cbSTrond Myklebust 	}
3290795bf83SFabian Frederick 	kunmap(desc->page);
330d1bacf9eSBryan Schumaker 	return status;
331d1bacf9eSBryan Schumaker }
332d1bacf9eSBryan Schumaker 
333d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
334d1bacf9eSBryan Schumaker static
33556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
336d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
337d1bacf9eSBryan Schumaker {
338480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
339684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
3404704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3411da177e4SLinus Torvalds 	int		error;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds  again:
3441da177e4SLinus Torvalds 	timestamp = jiffies;
3454704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
346be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3471da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3481da177e4SLinus Torvalds 	if (error < 0) {
3491da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3501da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3511da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3523a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
353a7a3b1e9SBenjamin Coddington 			desc->plus = false;
3541da177e4SLinus Torvalds 			goto again;
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		goto error;
3571da177e4SLinus Torvalds 	}
3581f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3594704f0e2STrond Myklebust 	desc->gencount = gencount;
360d1bacf9eSBryan Schumaker error:
361d1bacf9eSBryan Schumaker 	return error;
362d1bacf9eSBryan Schumaker }
363d1bacf9eSBryan Schumaker 
364573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
365573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
366d1bacf9eSBryan Schumaker {
367573c4e1eSChuck Lever 	int error;
368d1bacf9eSBryan Schumaker 
369573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
370573c4e1eSChuck Lever 	if (error)
371573c4e1eSChuck Lever 		return error;
372d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
373d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
374d1bacf9eSBryan Schumaker 	return 0;
375d1bacf9eSBryan Schumaker }
376d1bacf9eSBryan Schumaker 
377fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
378fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
379fa923369STrond Myklebust  */
380d39ab9deSBryan Schumaker static
381d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
382d39ab9deSBryan Schumaker {
383d8fdb47fSTrond Myklebust 	struct inode *inode;
384fa923369STrond Myklebust 	struct nfs_inode *nfsi;
385fa923369STrond Myklebust 
3862b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
3872b0143b5SDavid Howells 		return 0;
388fa923369STrond Myklebust 
389d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
390d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
391d8fdb47fSTrond Myklebust 		return 0;
392d8fdb47fSTrond Myklebust 
393d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
3947dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
395d39ab9deSBryan Schumaker 		return 0;
3967dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
3977dc72d5fSTrond Myklebust 		return 0;
3987dc72d5fSTrond Myklebust 	return 1;
399d39ab9deSBryan Schumaker }
400d39ab9deSBryan Schumaker 
401d39ab9deSBryan Schumaker static
40223db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
403d69ee9b8STrond Myklebust {
404d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
405d69ee9b8STrond Myklebust 		return false;
406d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
407d69ee9b8STrond Myklebust 		return true;
40823db8620SAl Viro 	if (ctx->pos == 0)
409d69ee9b8STrond Myklebust 		return true;
410d69ee9b8STrond Myklebust 	return false;
411d69ee9b8STrond Myklebust }
412d69ee9b8STrond Myklebust 
413d69ee9b8STrond Myklebust /*
41463519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
41563519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
416d69ee9b8STrond Myklebust  * directory.
417d69ee9b8STrond Myklebust  */
418d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
419d69ee9b8STrond Myklebust {
42063519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
42163519fbcSTrond Myklebust 
42263519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
42363519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
42463519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
425d69ee9b8STrond Myklebust }
426d69ee9b8STrond Myklebust 
427311324adSTrond Myklebust /*
428311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
429311324adSTrond Myklebust  *
430311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
431311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
432311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
433311324adSTrond Myklebust  * cache flush.
434311324adSTrond Myklebust  */
435311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
436311324adSTrond Myklebust {
43763519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
43863519fbcSTrond Myklebust 
43963519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
44063519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
44163519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
44279f687a3STrond Myklebust 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
443311324adSTrond Myklebust 	}
444311324adSTrond Myklebust }
445311324adSTrond Myklebust 
446d69ee9b8STrond Myklebust static
447d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
448d39ab9deSBryan Schumaker {
44926fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4509ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4514a201d6eSTrond Myklebust 	struct dentry *dentry;
4524a201d6eSTrond Myklebust 	struct dentry *alias;
4532b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
454d39ab9deSBryan Schumaker 	struct inode *inode;
455aa9c2669SDavid Quigley 	int status;
456d39ab9deSBryan Schumaker 
457fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
458fa923369STrond Myklebust 		return;
4596c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
4606c441c25STrond Myklebust 		return;
46178d04af4STrond Myklebust 	if (filename.len == 0)
46278d04af4STrond Myklebust 		return;
46378d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
46478d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
46578d04af4STrond Myklebust 		return;
46678d04af4STrond Myklebust 	/* ...or '/' */
46778d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
46878d04af4STrond Myklebust 		return;
4694a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4704a201d6eSTrond Myklebust 		if (filename.len == 1)
4714a201d6eSTrond Myklebust 			return;
4724a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4734a201d6eSTrond Myklebust 			return;
4744a201d6eSTrond Myklebust 	}
4758387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
476d39ab9deSBryan Schumaker 
4774a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
4789ac3d3e8SAl Viro again:
4799ac3d3e8SAl Viro 	if (!dentry) {
4809ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
4819ac3d3e8SAl Viro 		if (IS_ERR(dentry))
4829ac3d3e8SAl Viro 			return;
4839ac3d3e8SAl Viro 	}
4849ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
4856c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
4866c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
4876c441c25STrond Myklebust 					&entry->fattr->fsid))
4886c441c25STrond Myklebust 			goto out;
489d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
4907dc72d5fSTrond Myklebust 			if (!entry->fh->size)
4917dc72d5fSTrond Myklebust 				goto out;
492cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
4932b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
494aa9c2669SDavid Quigley 			if (!status)
4952b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
496d39ab9deSBryan Schumaker 			goto out;
497d39ab9deSBryan Schumaker 		} else {
4985542aa2fSEric W. Biederman 			d_invalidate(dentry);
499d39ab9deSBryan Schumaker 			dput(dentry);
5009ac3d3e8SAl Viro 			dentry = NULL;
5019ac3d3e8SAl Viro 			goto again;
502d39ab9deSBryan Schumaker 		}
503d39ab9deSBryan Schumaker 	}
5047dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5057dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5067dc72d5fSTrond Myklebust 		goto out;
5077dc72d5fSTrond Myklebust 	}
508d39ab9deSBryan Schumaker 
5091775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
51041d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5119ac3d3e8SAl Viro 	d_lookup_done(dentry);
5129ac3d3e8SAl Viro 	if (alias) {
513d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
514d39ab9deSBryan Schumaker 			goto out;
5159ac3d3e8SAl Viro 		dput(dentry);
5169ac3d3e8SAl Viro 		dentry = alias;
5179ac3d3e8SAl Viro 	}
518d39ab9deSBryan Schumaker 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
519d39ab9deSBryan Schumaker out:
520d39ab9deSBryan Schumaker 	dput(dentry);
521d39ab9deSBryan Schumaker }
522d39ab9deSBryan Schumaker 
523d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
524d1bacf9eSBryan Schumaker static
5258cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5266650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
527d1bacf9eSBryan Schumaker {
528babddc72SBryan Schumaker 	struct xdr_stream stream;
529f7da7a12SBenny Halevy 	struct xdr_buf buf;
5306650239aSTrond Myklebust 	struct page *scratch;
53199424380SBryan Schumaker 	struct nfs_cache_array *array;
5325c346854STrond Myklebust 	unsigned int count = 0;
5335c346854STrond Myklebust 	int status;
534be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
535be4c2d47Sluanshi 
536be4c2d47Sluanshi 	desc->pvec.index = desc->page_index;
537be4c2d47Sluanshi 	desc->pvec.nr = 0;
538babddc72SBryan Schumaker 
5396650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5406650239aSTrond Myklebust 	if (scratch == NULL)
5416650239aSTrond Myklebust 		return -ENOMEM;
542babddc72SBryan Schumaker 
543ce85cfbeSBenjamin Coddington 	if (buflen == 0)
544ce85cfbeSBenjamin Coddington 		goto out_nopages;
545ce85cfbeSBenjamin Coddington 
546f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5476650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
54899424380SBryan Schumaker 
54999424380SBryan Schumaker 	do {
55099424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5518cd51a0cSTrond Myklebust 		if (status != 0) {
5528cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5538cd51a0cSTrond Myklebust 				status = 0;
55499424380SBryan Schumaker 			break;
5558cd51a0cSTrond Myklebust 		}
55699424380SBryan Schumaker 
5575c346854STrond Myklebust 		count++;
5585c346854STrond Myklebust 
559a7a3b1e9SBenjamin Coddington 		if (desc->plus)
560be62a1a8SMiklos Szeredi 			nfs_prime_dcache(file_dentry(desc->file), entry);
5618cd51a0cSTrond Myklebust 
562be4c2d47Sluanshi 		status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]);
563be4c2d47Sluanshi 		if (status == -ENOSPC) {
564be4c2d47Sluanshi 			desc->pvec.nr++;
565be4c2d47Sluanshi 			if (desc->pvec.nr == max_rapages)
566be4c2d47Sluanshi 				break;
567be4c2d47Sluanshi 			status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]);
568be4c2d47Sluanshi 		}
5698cd51a0cSTrond Myklebust 		if (status != 0)
5708cd51a0cSTrond Myklebust 			break;
57199424380SBryan Schumaker 	} while (!entry->eof);
57299424380SBryan Schumaker 
573be4c2d47Sluanshi 	/*
574be4c2d47Sluanshi 	 * page and desc->pvec.pages[0] are valid, don't need to check
575be4c2d47Sluanshi 	 * whether or not to be NULL.
576be4c2d47Sluanshi 	 */
577be4c2d47Sluanshi 	copy_highpage(page, desc->pvec.pages[0]);
578be4c2d47Sluanshi 
579ce85cfbeSBenjamin Coddington out_nopages:
58047c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
581be4c2d47Sluanshi 		array = kmap_atomic(desc->pvec.pages[desc->pvec.nr]);
5828cd51a0cSTrond Myklebust 		array->eof_index = array->size;
58399424380SBryan Schumaker 		status = 0;
584be4c2d47Sluanshi 		kunmap_atomic(array);
58556e4ebf8SBryan Schumaker 	}
5866650239aSTrond Myklebust 
5876650239aSTrond Myklebust 	put_page(scratch);
588be4c2d47Sluanshi 
589be4c2d47Sluanshi 	/*
590be4c2d47Sluanshi 	 * desc->pvec.nr > 0 means at least one page was completely filled,
591be4c2d47Sluanshi 	 * we should return -ENOSPC. Otherwise function
592be4c2d47Sluanshi 	 * nfs_readdir_xdr_to_array will enter infinite loop.
593be4c2d47Sluanshi 	 */
594be4c2d47Sluanshi 	if (desc->pvec.nr > 0)
595be4c2d47Sluanshi 		return -ENOSPC;
5968cd51a0cSTrond Myklebust 	return status;
5978cd51a0cSTrond Myklebust }
59856e4ebf8SBryan Schumaker 
59956e4ebf8SBryan Schumaker static
600c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
60156e4ebf8SBryan Schumaker {
60256e4ebf8SBryan Schumaker 	unsigned int i;
60356e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
60456e4ebf8SBryan Schumaker 		put_page(pages[i]);
60556e4ebf8SBryan Schumaker }
60656e4ebf8SBryan Schumaker 
60756e4ebf8SBryan Schumaker /*
608bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
609bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
61056e4ebf8SBryan Schumaker  */
61156e4ebf8SBryan Schumaker static
612c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
61356e4ebf8SBryan Schumaker {
61456e4ebf8SBryan Schumaker 	unsigned int i;
61556e4ebf8SBryan Schumaker 
61656e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
61756e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
61856e4ebf8SBryan Schumaker 		if (page == NULL)
61956e4ebf8SBryan Schumaker 			goto out_freepages;
62056e4ebf8SBryan Schumaker 		pages[i] = page;
62156e4ebf8SBryan Schumaker 	}
6226650239aSTrond Myklebust 	return 0;
62356e4ebf8SBryan Schumaker 
62456e4ebf8SBryan Schumaker out_freepages:
625c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6266650239aSTrond Myklebust 	return -ENOMEM;
627d1bacf9eSBryan Schumaker }
628d1bacf9eSBryan Schumaker 
629be4c2d47Sluanshi /*
630be4c2d47Sluanshi  * nfs_readdir_rapages_init initialize rapages by nfs_cache_array structure.
631be4c2d47Sluanshi  */
632be4c2d47Sluanshi static
633be4c2d47Sluanshi void nfs_readdir_rapages_init(nfs_readdir_descriptor_t *desc)
634be4c2d47Sluanshi {
635be4c2d47Sluanshi 	struct nfs_cache_array *array;
636be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
637be4c2d47Sluanshi 	int index;
638be4c2d47Sluanshi 
639be4c2d47Sluanshi 	for (index = 0; index < max_rapages; index++) {
640be4c2d47Sluanshi 		array = kmap_atomic(desc->pvec.pages[index]);
641be4c2d47Sluanshi 		memset(array, 0, sizeof(struct nfs_cache_array));
642be4c2d47Sluanshi 		array->eof_index = -1;
643be4c2d47Sluanshi 		kunmap_atomic(array);
644be4c2d47Sluanshi 	}
645be4c2d47Sluanshi }
646be4c2d47Sluanshi 
647d1bacf9eSBryan Schumaker static
648d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
649d1bacf9eSBryan Schumaker {
65056e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
651d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
652d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
653d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6548cd51a0cSTrond Myklebust 	int status = -ENOMEM;
65556e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
656d1bacf9eSBryan Schumaker 
657be4c2d47Sluanshi 	/*
658be4c2d47Sluanshi 	 * This means we hit readdir rdpages miss, the preallocated rdpages
659be4c2d47Sluanshi 	 * are useless, the preallocate rdpages should be reinitialized.
660be4c2d47Sluanshi 	 */
661be4c2d47Sluanshi 	nfs_readdir_rapages_init(desc);
662be4c2d47Sluanshi 
663d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6640aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
665d1bacf9eSBryan Schumaker 	entry.eof = 0;
666d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
667d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
668573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
669d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
670d1bacf9eSBryan Schumaker 		goto out;
671d1bacf9eSBryan Schumaker 
67214c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
67314c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
67414c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
67514c43f76SDavid Quigley 		goto out;
67614c43f76SDavid Quigley 	}
67714c43f76SDavid Quigley 
6780795bf83SFabian Frederick 	array = kmap(page);
679d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
680d1bacf9eSBryan Schumaker 	array->eof_index = -1;
681d1bacf9eSBryan Schumaker 
682c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
6836650239aSTrond Myklebust 	if (status < 0)
684d1bacf9eSBryan Schumaker 		goto out_release_array;
685d1bacf9eSBryan Schumaker 	do {
686ac396128STrond Myklebust 		unsigned int pglen;
68756e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
688babddc72SBryan Schumaker 
689d1bacf9eSBryan Schumaker 		if (status < 0)
690d1bacf9eSBryan Schumaker 			break;
691ac396128STrond Myklebust 		pglen = status;
6926650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6938cd51a0cSTrond Myklebust 		if (status < 0) {
6948cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6958cd51a0cSTrond Myklebust 				status = 0;
6968cd51a0cSTrond Myklebust 			break;
6978cd51a0cSTrond Myklebust 		}
6988cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
699d1bacf9eSBryan Schumaker 
700c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
701d1bacf9eSBryan Schumaker out_release_array:
7020795bf83SFabian Frederick 	kunmap(page);
70314c43f76SDavid Quigley 	nfs4_label_free(entry.label);
704d1bacf9eSBryan Schumaker out:
705d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
706d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
707d1bacf9eSBryan Schumaker 	return status;
708d1bacf9eSBryan Schumaker }
709d1bacf9eSBryan Schumaker 
710d1bacf9eSBryan Schumaker /*
711d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
712d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
713d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
714d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
7151da177e4SLinus Torvalds  */
716d1bacf9eSBryan Schumaker static
717*a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page)
718d1bacf9eSBryan Schumaker {
719*a46126ccSChristoph Hellwig 	nfs_readdir_descriptor_t *desc = data;
720496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
7218cd51a0cSTrond Myklebust 	int ret;
722d1bacf9eSBryan Schumaker 
723be4c2d47Sluanshi 	/*
724be4c2d47Sluanshi 	 * If desc->page_index in range desc->pvec.index and
725be4c2d47Sluanshi 	 * desc->pvec.index + desc->pvec.nr, we get readdir cache hit.
726be4c2d47Sluanshi 	 */
727be4c2d47Sluanshi 	if (desc->page_index >= desc->pvec.index &&
728be4c2d47Sluanshi 		desc->page_index < (desc->pvec.index + desc->pvec.nr)) {
729be4c2d47Sluanshi 		/*
730be4c2d47Sluanshi 		 * page and desc->pvec.pages[x] are valid, don't need to check
731be4c2d47Sluanshi 		 * whether or not to be NULL.
732be4c2d47Sluanshi 		 */
733be4c2d47Sluanshi 		copy_highpage(page, desc->pvec.pages[desc->page_index - desc->pvec.index]);
734be4c2d47Sluanshi 		ret = 0;
735be4c2d47Sluanshi 	} else {
7368cd51a0cSTrond Myklebust 		ret = nfs_readdir_xdr_to_array(desc, page, inode);
7378cd51a0cSTrond Myklebust 		if (ret < 0)
738d1bacf9eSBryan Schumaker 			goto error;
739be4c2d47Sluanshi 	}
740be4c2d47Sluanshi 
741d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
742d1bacf9eSBryan Schumaker 
7432aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
744cd9ae2b6STrond Myklebust 		/* Should never happen */
745cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
746cd9ae2b6STrond Myklebust 	}
7471da177e4SLinus Torvalds 	unlock_page(page);
7481da177e4SLinus Torvalds 	return 0;
7491da177e4SLinus Torvalds  error:
7501da177e4SLinus Torvalds 	unlock_page(page);
7518cd51a0cSTrond Myklebust 	return ret;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
754d1bacf9eSBryan Schumaker static
755d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7561da177e4SLinus Torvalds {
757b044f645SBenjamin Coddington 	if (!desc->page->mapping)
75811de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
75909cbfeafSKirill A. Shutemov 	put_page(desc->page);
7601da177e4SLinus Torvalds 	desc->page = NULL;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
763d1bacf9eSBryan Schumaker static
764d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7651da177e4SLinus Torvalds {
766*a46126ccSChristoph Hellwig 	return read_cache_page(desc->file->f_mapping, desc->page_index,
767*a46126ccSChristoph Hellwig 			nfs_readdir_filler, desc);
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds /*
771d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7721da177e4SLinus Torvalds  */
773d1bacf9eSBryan Schumaker static
774d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
775d1bacf9eSBryan Schumaker {
776d1bacf9eSBryan Schumaker 	int res;
777d1bacf9eSBryan Schumaker 
778d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
779d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
780d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
781d1bacf9eSBryan Schumaker 
782d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
78347c716cbSTrond Myklebust 	if (res != 0)
784d1bacf9eSBryan Schumaker 		cache_page_release(desc);
785d1bacf9eSBryan Schumaker 	return res;
786d1bacf9eSBryan Schumaker }
787d1bacf9eSBryan Schumaker 
788d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7891da177e4SLinus Torvalds static inline
7901da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7911da177e4SLinus Torvalds {
7928cd51a0cSTrond Myklebust 	int res;
793d1bacf9eSBryan Schumaker 
7940aded708STrond Myklebust 	if (desc->page_index == 0) {
7958cd51a0cSTrond Myklebust 		desc->current_index = 0;
7960aded708STrond Myklebust 		desc->last_cookie = 0;
7970aded708STrond Myklebust 	}
79847c716cbSTrond Myklebust 	do {
799d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
80047c716cbSTrond Myklebust 	} while (res == -EAGAIN);
8011da177e4SLinus Torvalds 	return res;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds /*
8051da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
8061da177e4SLinus Torvalds  */
8071da177e4SLinus Torvalds static
80823db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
8091da177e4SLinus Torvalds {
8101da177e4SLinus Torvalds 	struct file	*file = desc->file;
811d1bacf9eSBryan Schumaker 	int i = 0;
812d1bacf9eSBryan Schumaker 	int res = 0;
813d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8148ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
8158ef2ce3eSBryan Schumaker 
8160795bf83SFabian Frederick 	array = kmap(desc->page);
817d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
818ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8191da177e4SLinus Torvalds 
820ece0b423STrond Myklebust 		ent = &array->array[i];
82123db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
82223db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
8236089dd0dSThomas Meyer 			desc->eof = true;
8241da177e4SLinus Torvalds 			break;
825ece0b423STrond Myklebust 		}
82623db8620SAl Viro 		desc->ctx->pos++;
827d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
828d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
829d1bacf9eSBryan Schumaker 		else
830d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
8310c030806STrond Myklebust 		if (ctx->duped != 0)
8320c030806STrond Myklebust 			ctx->duped = 1;
8338cd51a0cSTrond Myklebust 	}
83447c716cbSTrond Myklebust 	if (array->eof_index >= 0)
8356089dd0dSThomas Meyer 		desc->eof = true;
836d1bacf9eSBryan Schumaker 
8370795bf83SFabian Frederick 	kunmap(desc->page);
838d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8391e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8401e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
8411da177e4SLinus Torvalds 	return res;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds /*
8451da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8461da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8471da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8481da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8491da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8501da177e4SLinus Torvalds  *
8511da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8521da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8531da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8541da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8551da177e4SLinus Torvalds  */
8561da177e4SLinus Torvalds static inline
85723db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	struct page	*page = NULL;
8601da177e4SLinus Torvalds 	int		status;
861496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8620c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8631da177e4SLinus Torvalds 
8641e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8651e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8681da177e4SLinus Torvalds 	if (!page) {
8691da177e4SLinus Torvalds 		status = -ENOMEM;
8701da177e4SLinus Torvalds 		goto out;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 
8737a8e1dc3STrond Myklebust 	desc->page_index = 0;
8740aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8757a8e1dc3STrond Myklebust 	desc->page = page;
8760c030806STrond Myklebust 	ctx->duped = 0;
8777a8e1dc3STrond Myklebust 
87885f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
87985f8607eSTrond Myklebust 	if (status < 0)
880d1bacf9eSBryan Schumaker 		goto out_release;
881d1bacf9eSBryan Schumaker 
88223db8620SAl Viro 	status = nfs_do_filldir(desc);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds  out:
8851e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8863110ff80SHarvey Harrison 			__func__, status);
8871da177e4SLinus Torvalds 	return status;
8881da177e4SLinus Torvalds  out_release:
889d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8901da177e4SLinus Torvalds 	goto out;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
89300a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
89400a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
89500a92642SOlivier Galibert    whole directory.
8961da177e4SLinus Torvalds  */
89723db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8981da177e4SLinus Torvalds {
899be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
9002b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
9011da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
9021da177e4SLinus Torvalds 			*desc = &my_desc;
90323db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
90407b5ce8eSScott Mayhew 	int res = 0;
905be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
9061da177e4SLinus Torvalds 
9076de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9086de1472fSAl Viro 			file, (long long)ctx->pos);
90991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
91091d5b470SChuck Lever 
9111da177e4SLinus Torvalds 	/*
91223db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
913f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
91400a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
91500a92642SOlivier Galibert 	 * revalidate the cookie.
9161da177e4SLinus Torvalds 	 */
9171da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
9181da177e4SLinus Torvalds 
91923db8620SAl Viro 	desc->file = file;
92023db8620SAl Viro 	desc->ctx = ctx;
921480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
9221da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
923a7a3b1e9SBenjamin Coddington 	desc->plus = nfs_use_readdirplus(inode, ctx);
9241da177e4SLinus Torvalds 
925be4c2d47Sluanshi 	res = nfs_readdir_alloc_pages(desc->pvec.pages, max_rapages);
926be4c2d47Sluanshi 	if (res < 0)
927be4c2d47Sluanshi 		return -ENOMEM;
928be4c2d47Sluanshi 
929be4c2d47Sluanshi 	nfs_readdir_rapages_init(desc);
930be4c2d47Sluanshi 
93179f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
93223db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
933fccca7fcSTrond Myklebust 	if (res < 0)
934fccca7fcSTrond Myklebust 		goto out;
935fccca7fcSTrond Myklebust 
93647c716cbSTrond Myklebust 	do {
9371da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
93800a92642SOlivier Galibert 
9391da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
940ece0b423STrond Myklebust 			res = 0;
9411da177e4SLinus Torvalds 			/* This means either end of directory */
9426089dd0dSThomas Meyer 			if (*desc->dir_cookie && !desc->eof) {
9431da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
94423db8620SAl Viro 				res = uncached_readdir(desc);
945ece0b423STrond Myklebust 				if (res == 0)
9461da177e4SLinus Torvalds 					continue;
9471da177e4SLinus Torvalds 			}
9481da177e4SLinus Torvalds 			break;
9491da177e4SLinus Torvalds 		}
9501da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9513a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9521da177e4SLinus Torvalds 			nfs_zap_caches(inode);
953baf57a09STrond Myklebust 			desc->page_index = 0;
954a7a3b1e9SBenjamin Coddington 			desc->plus = false;
955a7a3b1e9SBenjamin Coddington 			desc->eof = false;
9561da177e4SLinus Torvalds 			continue;
9571da177e4SLinus Torvalds 		}
9581da177e4SLinus Torvalds 		if (res < 0)
9591da177e4SLinus Torvalds 			break;
9601da177e4SLinus Torvalds 
96123db8620SAl Viro 		res = nfs_do_filldir(desc);
962ece0b423STrond Myklebust 		if (res < 0)
9631da177e4SLinus Torvalds 			break;
96447c716cbSTrond Myklebust 	} while (!desc->eof);
965fccca7fcSTrond Myklebust out:
966be4c2d47Sluanshi 	nfs_readdir_free_pages(desc->pvec.pages, max_rapages);
9671e7cb3dcSChuck Lever 	if (res > 0)
9681e7cb3dcSChuck Lever 		res = 0;
9696de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9701da177e4SLinus Torvalds 	return res;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
973965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
974f0dd2136STrond Myklebust {
975b044f645SBenjamin Coddington 	struct inode *inode = file_inode(filp);
976480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
977b84e06c5SChuck Lever 
9786de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9796de1472fSAl Viro 			filp, offset, whence);
980b84e06c5SChuck Lever 
981965c8e59SAndrew Morton 	switch (whence) {
982f0dd2136STrond Myklebust 	default:
983b2b1ff3dSTrond Myklebust 		return -EINVAL;
984b2b1ff3dSTrond Myklebust 	case SEEK_SET:
985b2b1ff3dSTrond Myklebust 		if (offset < 0)
986b2b1ff3dSTrond Myklebust 			return -EINVAL;
987b2b1ff3dSTrond Myklebust 		inode_lock(inode);
988b2b1ff3dSTrond Myklebust 		break;
989b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
990b2b1ff3dSTrond Myklebust 		if (offset == 0)
991b2b1ff3dSTrond Myklebust 			return filp->f_pos;
992b2b1ff3dSTrond Myklebust 		inode_lock(inode);
993b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
994b2b1ff3dSTrond Myklebust 		if (offset < 0) {
995b2b1ff3dSTrond Myklebust 			inode_unlock(inode);
996b2b1ff3dSTrond Myklebust 			return -EINVAL;
997b2b1ff3dSTrond Myklebust 		}
998f0dd2136STrond Myklebust 	}
999f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1000f0dd2136STrond Myklebust 		filp->f_pos = offset;
1001480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
10028ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1003f0dd2136STrond Myklebust 	}
1004b044f645SBenjamin Coddington 	inode_unlock(inode);
1005f0dd2136STrond Myklebust 	return offset;
1006f0dd2136STrond Myklebust }
1007f0dd2136STrond Myklebust 
10081da177e4SLinus Torvalds /*
10091da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
10101da177e4SLinus Torvalds  * is a dummy operation.
10111da177e4SLinus Torvalds  */
101202c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
101302c24a82SJosef Bacik 			 int datasync)
10141da177e4SLinus Torvalds {
10156de1472fSAl Viro 	struct inode *inode = file_inode(filp);
10167ea80859SChristoph Hellwig 
10176de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10181e7cb3dcSChuck Lever 
10195955102cSAl Viro 	inode_lock(inode);
10206de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
10215955102cSAl Viro 	inode_unlock(inode);
10221da177e4SLinus Torvalds 	return 0;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
1025bfc69a45STrond Myklebust /**
1026bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1027302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1028bfc69a45STrond Myklebust  *
1029bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1030bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1031bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1032bfc69a45STrond Myklebust  *
1033bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1034bfc69a45STrond Myklebust  */
1035bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1036bfc69a45STrond Myklebust {
1037011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
1038bfc69a45STrond Myklebust }
103989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1040bfc69a45STrond Myklebust 
10411da177e4SLinus Torvalds /*
10421da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
10431da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
10441da177e4SLinus Torvalds  * and may need to be looked up again.
1045912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
10461da177e4SLinus Torvalds  */
1047912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1048912a108dSNeilBrown 			      int rcu_walk)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
10511da177e4SLinus Torvalds 		return 1;
10524eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
10534eec952eSTrond Myklebust 		return 0;
1054f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10556ecc5e8fSTrond Myklebust 		return 0;
1056f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
10571cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1058912a108dSNeilBrown 		if (rcu_walk)
1059f2c77f4eSTrond Myklebust 			return 0;
10601cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
10611cd9cb05STrond Myklebust 			return 0;
10621cd9cb05STrond Myklebust 	}
1063f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1064f2c77f4eSTrond Myklebust 		return 0;
1065f2c77f4eSTrond Myklebust 	return 1;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds /*
1069a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1070a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1071a12802caSTrond Myklebust  */
1072fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1073a12802caSTrond Myklebust {
1074a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1075a12802caSTrond Myklebust 		return 0;
1076fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1077a12802caSTrond Myklebust }
1078a12802caSTrond Myklebust 
1079a12802caSTrond Myklebust /*
10801d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10811d6757fbSTrond Myklebust  *
10821d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10831d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10841d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10851d6757fbSTrond Myklebust  *
10861d6757fbSTrond Myklebust  */
108765a0c149STrond Myklebust static
1088fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
109165a0c149STrond Myklebust 	int ret;
10921da177e4SLinus Torvalds 
109336d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10944e99a1ffSTrond Myklebust 		return 0;
109547921921STrond Myklebust 
109647921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
109747921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
109847921921STrond Myklebust 		case S_IFREG:
109947921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
110047921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
110147921921STrond Myklebust 				goto out;
110247921921STrond Myklebust 			/* Fallthrough */
110347921921STrond Myklebust 		case S_IFDIR:
110447921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
110547921921STrond Myklebust 				break;
110647921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
110747921921STrond Myklebust 			goto out_force;
110847921921STrond Myklebust 		}
110947921921STrond Myklebust 	}
111047921921STrond Myklebust 
11111da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1112fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
11131da177e4SLinus Torvalds 		goto out_force;
111465a0c149STrond Myklebust out:
1115a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
11161da177e4SLinus Torvalds out_force:
11171fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
11181fa1e384SNeilBrown 		return -ECHILD;
111965a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
112065a0c149STrond Myklebust 	if (ret != 0)
112165a0c149STrond Myklebust 		return ret;
112265a0c149STrond Myklebust 	goto out;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds /*
11261da177e4SLinus Torvalds  * We judge how long we want to trust negative
11271da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
11281da177e4SLinus Torvalds  *
11291da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
11301da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1131912a108dSNeilBrown  *
1132912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1133912a108dSNeilBrown  * suggesting a reval is needed.
11349f6d44d4STrond Myklebust  *
11359f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
11369f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
11371da177e4SLinus Torvalds  */
11381da177e4SLinus Torvalds static inline
11391da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1140fa3c56bbSAl Viro 		       unsigned int flags)
11411da177e4SLinus Torvalds {
11429f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
11431da177e4SLinus Torvalds 		return 0;
11444eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
11454eec952eSTrond Myklebust 		return 1;
1146912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
11495ceb9d7fSTrond Myklebust static int
11505ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
11515ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
11521da177e4SLinus Torvalds {
11535ceb9d7fSTrond Myklebust 	switch (error) {
11545ceb9d7fSTrond Myklebust 	case 1:
11556de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11566de1472fSAl Viro 			__func__, dentry);
11571da177e4SLinus Torvalds 		return 1;
11585ceb9d7fSTrond Myklebust 	case 0:
1159a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
11601da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
11611da177e4SLinus Torvalds 			/* Purge readdir caches. */
11621da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1163a3f432bfSJ. Bruce Fields 			/*
1164a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1165a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1166a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1167a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1168a3f432bfSJ. Bruce Fields 			 */
1169a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
11705ceb9d7fSTrond Myklebust 				return 1;
11711da177e4SLinus Torvalds 		}
11726de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11736de1472fSAl Viro 				__func__, dentry);
11741da177e4SLinus Torvalds 		return 0;
11755ceb9d7fSTrond Myklebust 	}
11766de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
11776de1472fSAl Viro 				__func__, dentry, error);
1178e1fb4d05STrond Myklebust 	return error;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
11815ceb9d7fSTrond Myklebust static int
11825ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
11835ceb9d7fSTrond Myklebust 			       unsigned int flags)
11845ceb9d7fSTrond Myklebust {
11855ceb9d7fSTrond Myklebust 	int ret = 1;
11865ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
11875ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
11885ceb9d7fSTrond Myklebust 			return -ECHILD;
11895ceb9d7fSTrond Myklebust 		ret = 0;
11905ceb9d7fSTrond Myklebust 	}
11915ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
11925ceb9d7fSTrond Myklebust }
11935ceb9d7fSTrond Myklebust 
11945ceb9d7fSTrond Myklebust static int
11955ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
11965ceb9d7fSTrond Myklebust 				struct inode *inode)
11975ceb9d7fSTrond Myklebust {
11985ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11995ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
12005ceb9d7fSTrond Myklebust }
12015ceb9d7fSTrond Myklebust 
12025ceb9d7fSTrond Myklebust static int
12035ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
12045ceb9d7fSTrond Myklebust 			     struct inode *inode)
12055ceb9d7fSTrond Myklebust {
12065ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
12075ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
12085ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
12095ceb9d7fSTrond Myklebust 	int ret;
12105ceb9d7fSTrond Myklebust 
12115ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
12125ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
12135ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
12145ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
12155ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
12165ceb9d7fSTrond Myklebust 		goto out;
12175ceb9d7fSTrond Myklebust 
12185ceb9d7fSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
12195ceb9d7fSTrond Myklebust 	if (ret < 0) {
12205ceb9d7fSTrond Myklebust 		if (ret == -ESTALE || ret == -ENOENT)
12215ceb9d7fSTrond Myklebust 			ret = 0;
12225ceb9d7fSTrond Myklebust 		goto out;
12235ceb9d7fSTrond Myklebust 	}
12245ceb9d7fSTrond Myklebust 	ret = 0;
12255ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
12265ceb9d7fSTrond Myklebust 		goto out;
12275ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
12285ceb9d7fSTrond Myklebust 		goto out;
12295ceb9d7fSTrond Myklebust 
12305ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
12315ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12325ceb9d7fSTrond Myklebust 
12335ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
12345ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
12355ceb9d7fSTrond Myklebust 	ret = 1;
12365ceb9d7fSTrond Myklebust out:
12375ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
12385ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
12395ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
12405ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
12415ceb9d7fSTrond Myklebust }
12425ceb9d7fSTrond Myklebust 
12435ceb9d7fSTrond Myklebust /*
12445ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
12455ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
12465ceb9d7fSTrond Myklebust  * lookup.
12475ceb9d7fSTrond Myklebust  *
12485ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
12495ceb9d7fSTrond Myklebust  * we have an inode!
12505ceb9d7fSTrond Myklebust  *
12515ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
12525ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
12535ceb9d7fSTrond Myklebust  */
12545ceb9d7fSTrond Myklebust static int
12555ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
12565ceb9d7fSTrond Myklebust 			 unsigned int flags)
12575ceb9d7fSTrond Myklebust {
12585ceb9d7fSTrond Myklebust 	struct inode *inode;
12595ceb9d7fSTrond Myklebust 	int error;
12605ceb9d7fSTrond Myklebust 
12615ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
12625ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
12635ceb9d7fSTrond Myklebust 
12645ceb9d7fSTrond Myklebust 	if (!inode)
12655ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
12665ceb9d7fSTrond Myklebust 
12675ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
12685ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12695ceb9d7fSTrond Myklebust 				__func__, dentry);
12705ceb9d7fSTrond Myklebust 		goto out_bad;
12715ceb9d7fSTrond Myklebust 	}
12725ceb9d7fSTrond Myklebust 
12735ceb9d7fSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
12745ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
12755ceb9d7fSTrond Myklebust 
12765ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
12775ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
12785ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
12795ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
12805ceb9d7fSTrond Myklebust 		if (error) {
12815ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
12825ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
12835ceb9d7fSTrond Myklebust 			goto out_bad;
12845ceb9d7fSTrond Myklebust 		}
12855ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
12865ceb9d7fSTrond Myklebust 		goto out_valid;
12875ceb9d7fSTrond Myklebust 	}
12885ceb9d7fSTrond Myklebust 
12895ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
12905ceb9d7fSTrond Myklebust 		return -ECHILD;
12915ceb9d7fSTrond Myklebust 
12925ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
12935ceb9d7fSTrond Myklebust 		goto out_bad;
12945ceb9d7fSTrond Myklebust 
12955ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
12965ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
12975ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
12985ceb9d7fSTrond Myklebust 	return error;
12995ceb9d7fSTrond Myklebust out_valid:
13005ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
13015ceb9d7fSTrond Myklebust out_bad:
13025ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
13035ceb9d7fSTrond Myklebust 		return -ECHILD;
13045ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
13055ceb9d7fSTrond Myklebust }
13065ceb9d7fSTrond Myklebust 
13075ceb9d7fSTrond Myklebust static int
1308c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1309c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
13105ceb9d7fSTrond Myklebust {
13115ceb9d7fSTrond Myklebust 	struct dentry *parent;
13125ceb9d7fSTrond Myklebust 	struct inode *dir;
13135ceb9d7fSTrond Myklebust 	int ret;
13145ceb9d7fSTrond Myklebust 
13155ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
13165ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
13175ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
13185ceb9d7fSTrond Myklebust 		if (!dir)
13195ceb9d7fSTrond Myklebust 			return -ECHILD;
1320c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
13215ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
13225ceb9d7fSTrond Myklebust 			return -ECHILD;
13235ceb9d7fSTrond Myklebust 	} else {
13245ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1325c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
13265ceb9d7fSTrond Myklebust 		dput(parent);
13275ceb9d7fSTrond Myklebust 	}
13285ceb9d7fSTrond Myklebust 	return ret;
13295ceb9d7fSTrond Myklebust }
13305ceb9d7fSTrond Myklebust 
1331c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1332c7944ebbSTrond Myklebust {
1333c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1334c7944ebbSTrond Myklebust }
1335c7944ebbSTrond Myklebust 
13361da177e4SLinus Torvalds /*
13372b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1338ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1339ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1340ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1341ecf3d1f1SJeff Layton  *
1342ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1343ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1344ecf3d1f1SJeff Layton  */
1345ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1346ecf3d1f1SJeff Layton {
13472b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
13489cdd1d3fSTrond Myklebust 	int error = 0;
1349ecf3d1f1SJeff Layton 
1350ecf3d1f1SJeff Layton 	/*
1351ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1352ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1353ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1354ecf3d1f1SJeff Layton 	 */
1355ecf3d1f1SJeff Layton 	if (!inode) {
13566de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
13576de1472fSAl Viro 				__func__, dentry);
1358ecf3d1f1SJeff Layton 		return 1;
1359ecf3d1f1SJeff Layton 	}
1360ecf3d1f1SJeff Layton 
1361ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
13626de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
13636de1472fSAl Viro 				__func__, dentry);
1364ecf3d1f1SJeff Layton 		return 0;
1365ecf3d1f1SJeff Layton 	}
1366ecf3d1f1SJeff Layton 
1367b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1368ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1369ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1370ecf3d1f1SJeff Layton 	return !error;
1371ecf3d1f1SJeff Layton }
1372ecf3d1f1SJeff Layton 
1373ecf3d1f1SJeff Layton /*
13741da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
13751da177e4SLinus Torvalds  */
1376fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
13771da177e4SLinus Torvalds {
13786de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
13796de1472fSAl Viro 		dentry, dentry->d_flags);
13801da177e4SLinus Torvalds 
138177f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
13822b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
138377f11192STrond Myklebust 		return 1;
138477f11192STrond Myklebust 
13851da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
13861da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
13871da177e4SLinus Torvalds 		return 1;
13881da177e4SLinus Torvalds 	}
13891751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
13901da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
13911da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
13921da177e4SLinus Torvalds 		return 1;
13931da177e4SLinus Torvalds 	}
13941da177e4SLinus Torvalds 	return 0;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13981f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
13991b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
14001b83d707STrond Myklebust {
14011b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
14021f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
140359a707b0STrond Myklebust 	if (inode->i_nlink > 0)
140459a707b0STrond Myklebust 		drop_nlink(inode);
140559a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
140616e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
140716e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
140859a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
140959a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
14101b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
14111b83d707STrond Myklebust }
14121b83d707STrond Myklebust 
14131da177e4SLinus Torvalds /*
14141da177e4SLinus Torvalds  * Called when the dentry loses inode.
14151da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
14161da177e4SLinus Torvalds  */
14171da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
14181da177e4SLinus Torvalds {
141983672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
142083672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
142183672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
142283672d39SNeil Brown 
14231da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1424e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
14251f018458STrond Myklebust 		nfs_drop_nlink(inode);
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 	iput(inode);
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
1430b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1431b1942c5fSAl Viro {
1432b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1433b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1434b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1435b1942c5fSAl Viro 			WARN_ON(1);
1436b1942c5fSAl Viro 		else
1437b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1438b1942c5fSAl Viro 	}
1439b1942c5fSAl Viro }
1440b1942c5fSAl Viro 
1441f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
14421da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1443ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
14441da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
14451da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
144636d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1447b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14481da177e4SLinus Torvalds };
1449ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
14501da177e4SLinus Torvalds 
1451597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	struct dentry *res;
14541da177e4SLinus Torvalds 	struct inode *inode = NULL;
1455e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1456e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
14571775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
14581da177e4SLinus Torvalds 	int error;
14591da177e4SLinus Torvalds 
14606de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
146191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
14621da177e4SLinus Torvalds 
1463130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1464130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
14651da177e4SLinus Torvalds 
1466fd684071STrond Myklebust 	/*
1467fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1468fd684071STrond Myklebust 	 * but don't hash the dentry.
1469fd684071STrond Myklebust 	 */
14709f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1471130f9ab7SAl Viro 		return NULL;
14721da177e4SLinus Torvalds 
1473e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1474e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1475e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1476e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1477e1fb4d05STrond Myklebust 		goto out;
1478e1fb4d05STrond Myklebust 
147914c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
148014c43f76SDavid Quigley 	if (IS_ERR(label))
148114c43f76SDavid Quigley 		goto out;
148214c43f76SDavid Quigley 
14836e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
14841775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
14851da177e4SLinus Torvalds 	if (error == -ENOENT)
14861da177e4SLinus Torvalds 		goto no_entry;
14871da177e4SLinus Torvalds 	if (error < 0) {
14881da177e4SLinus Torvalds 		res = ERR_PTR(error);
1489bf130914SAl Viro 		goto out_label;
14901da177e4SLinus Torvalds 	}
14911775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1492bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
149303f28e3aSTrond Myklebust 	if (IS_ERR(res))
1494bf130914SAl Viro 		goto out_label;
149554ceac45SDavid Howells 
149663519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
149763519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1498d69ee9b8STrond Myklebust 
14991da177e4SLinus Torvalds no_entry:
150041d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
15019eaef27bSTrond Myklebust 	if (res != NULL) {
15029eaef27bSTrond Myklebust 		if (IS_ERR(res))
1503bf130914SAl Viro 			goto out_label;
15041da177e4SLinus Torvalds 		dentry = res;
15059eaef27bSTrond Myklebust 	}
15061da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1507bf130914SAl Viro out_label:
15086e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
150914c43f76SDavid Quigley 	nfs4_label_free(label);
15101da177e4SLinus Torvalds out:
1511e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1512e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
15131da177e4SLinus Torvalds 	return res;
15141da177e4SLinus Torvalds }
1515ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
15161da177e4SLinus Torvalds 
151789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
15180b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
15191da177e4SLinus Torvalds 
1520f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
15210ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1522b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
15231da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
15241da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
152536d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1526b1942c5fSAl Viro 	.d_release	= nfs_d_release,
15271da177e4SLinus Torvalds };
152889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
15291da177e4SLinus Torvalds 
15308a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
15318a5e929dSAl Viro {
15328a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
15338a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
15348a5e929dSAl Viro 		res |= FMODE_READ;
15358a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
15368a5e929dSAl Viro 		res |= FMODE_WRITE;
15378a5e929dSAl Viro 	return res;
15388a5e929dSAl Viro }
15398a5e929dSAl Viro 
1540532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1541cd9a1c0eSTrond Myklebust {
1542532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1543cd9a1c0eSTrond Myklebust }
1544cd9a1c0eSTrond Myklebust 
1545cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1546cd9a1c0eSTrond Myklebust {
1547f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1548cd9a1c0eSTrond Myklebust 	return 0;
1549cd9a1c0eSTrond Myklebust }
1550cd9a1c0eSTrond Myklebust 
1551d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
15520dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1553b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1554cd9a1c0eSTrond Myklebust {
15550dd2b474SMiklos Szeredi 	int err;
15560dd2b474SMiklos Szeredi 
1557be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
155830d90494SAl Viro 	if (err)
1559d9585277SAl Viro 		goto out;
1560eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
156130d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1562eaa2b82cSNeilBrown 	else
1563eaa2b82cSNeilBrown 		err = -ESTALE;
1564cd9a1c0eSTrond Myklebust out:
1565d9585277SAl Viro 	return err;
1566cd9a1c0eSTrond Myklebust }
1567cd9a1c0eSTrond Myklebust 
156873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
156930d90494SAl Viro 		    struct file *file, unsigned open_flags,
157044907d79SAl Viro 		    umode_t mode)
15711da177e4SLinus Torvalds {
1572c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1573cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
15740dd2b474SMiklos Szeredi 	struct dentry *res;
15750dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1576f46e0bd3STrond Myklebust 	struct inode *inode;
15771472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1578c94c0953SAl Viro 	bool switched = false;
157973a09dd9SAl Viro 	int created = 0;
1580898f635cSTrond Myklebust 	int err;
15811da177e4SLinus Torvalds 
15820dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
15832b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
15840dd2b474SMiklos Szeredi 
15851e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
15866de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
15871e7cb3dcSChuck Lever 
15889597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
15899597c13bSJeff Layton 	if (err)
15909597c13bSJeff Layton 		return err;
15919597c13bSJeff Layton 
15920dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
15930dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
159400699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
15950dd2b474SMiklos Szeredi 			/*
15960dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
15970dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
15980dd2b474SMiklos Szeredi 			 * again
15990dd2b474SMiklos Szeredi 			 */
1600d9585277SAl Viro 			return -ENOENT;
16010dd2b474SMiklos Szeredi 		}
16021472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
16031da177e4SLinus Torvalds 		goto no_open;
16041da177e4SLinus Torvalds 	}
16051da177e4SLinus Torvalds 
16060dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1607d9585277SAl Viro 		return -ENAMETOOLONG;
16081da177e4SLinus Torvalds 
16090dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1610dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1611dff25ddbSAndreas Gruenbacher 
1612dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1613dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1614dff25ddbSAndreas Gruenbacher 
1615536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1616dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
16170dd2b474SMiklos Szeredi 	}
1618536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1619536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1620536e43d1STrond Myklebust 		attr.ia_size = 0;
1621cd9a1c0eSTrond Myklebust 	}
1622cd9a1c0eSTrond Myklebust 
1623c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1624c94c0953SAl Viro 		d_drop(dentry);
1625c94c0953SAl Viro 		switched = true;
1626c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1627c94c0953SAl Viro 					  &dentry->d_name, &wq);
1628c94c0953SAl Viro 		if (IS_ERR(dentry))
1629c94c0953SAl Viro 			return PTR_ERR(dentry);
1630c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1631c94c0953SAl Viro 			return finish_no_open(file, dentry);
1632c94c0953SAl Viro 	}
1633c94c0953SAl Viro 
1634532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
16350dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
16360dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1637d9585277SAl Viro 		goto out;
16380dd2b474SMiklos Szeredi 
16396e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
164073a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
164173a09dd9SAl Viro 	if (created)
164273a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1643275bb307STrond Myklebust 	if (IS_ERR(inode)) {
16440dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
16456e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16462d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1647d20cb71dSAl Viro 		d_drop(dentry);
16480dd2b474SMiklos Szeredi 		switch (err) {
16491da177e4SLinus Torvalds 		case -ENOENT:
1650774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1651809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16520dd2b474SMiklos Szeredi 			break;
16531788ea6eSJeff Layton 		case -EISDIR:
16546f926b5bSTrond Myklebust 		case -ENOTDIR:
16556f926b5bSTrond Myklebust 			goto no_open;
16561da177e4SLinus Torvalds 		case -ELOOP:
16570dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
16581da177e4SLinus Torvalds 				goto no_open;
16590dd2b474SMiklos Szeredi 			break;
16601da177e4SLinus Torvalds 			/* case -EINVAL: */
16611da177e4SLinus Torvalds 		default:
16620dd2b474SMiklos Szeredi 			break;
16631da177e4SLinus Torvalds 		}
16641da177e4SLinus Torvalds 		goto out;
16651da177e4SLinus Torvalds 	}
16660dd2b474SMiklos Szeredi 
1667b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
16686e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16692d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1670d9585277SAl Viro out:
1671c94c0953SAl Viro 	if (unlikely(switched)) {
1672c94c0953SAl Viro 		d_lookup_done(dentry);
1673c94c0953SAl Viro 		dput(dentry);
1674c94c0953SAl Viro 	}
1675d9585277SAl Viro 	return err;
16760dd2b474SMiklos Szeredi 
16771da177e4SLinus Torvalds no_open:
16781472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1679c94c0953SAl Viro 	if (switched) {
1680c94c0953SAl Viro 		d_lookup_done(dentry);
1681c94c0953SAl Viro 		if (!res)
1682c94c0953SAl Viro 			res = dentry;
1683c94c0953SAl Viro 		else
1684c94c0953SAl Viro 			dput(dentry);
1685c94c0953SAl Viro 	}
16860dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1687c94c0953SAl Viro 		return PTR_ERR(res);
1688e45198a6SAl Viro 	return finish_no_open(file, res);
16891da177e4SLinus Torvalds }
169089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
16911da177e4SLinus Torvalds 
1692c7944ebbSTrond Myklebust static int
1693c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1694c7944ebbSTrond Myklebust 			  unsigned int flags)
16951da177e4SLinus Torvalds {
1696657e94b6SNick Piggin 	struct inode *inode;
16971da177e4SLinus Torvalds 
1698fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1699c7944ebbSTrond Myklebust 		goto full_reval;
1700eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1701c7944ebbSTrond Myklebust 		goto full_reval;
17022b484297STrond Myklebust 
17032b0143b5SDavid Howells 	inode = d_inode(dentry);
17042b484297STrond Myklebust 
17051da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
17061da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
17071da177e4SLinus Torvalds 	 */
1708c7944ebbSTrond Myklebust 	if (inode == NULL)
1709c7944ebbSTrond Myklebust 		goto full_reval;
171049317a7fSNeilBrown 
1711c7944ebbSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1712c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1713216d5d06STrond Myklebust 
17141da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
17151da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1716c7944ebbSTrond Myklebust 		goto full_reval;
1717c7944ebbSTrond Myklebust 
17181da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1719c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1720c7944ebbSTrond Myklebust 		goto reval_dentry;
1721c7944ebbSTrond Myklebust 
1722c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1723c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1724c7944ebbSTrond Myklebust 		goto reval_dentry;
17251da177e4SLinus Torvalds 
17260ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1727c7944ebbSTrond Myklebust 	return 1;
1728c7944ebbSTrond Myklebust reval_dentry:
1729c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1730c7944ebbSTrond Myklebust 		return -ECHILD;
173142f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
17320ef97dcfSMiklos Szeredi 
1733c7944ebbSTrond Myklebust full_reval:
1734c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1735c7944ebbSTrond Myklebust }
1736535918f1STrond Myklebust 
1737c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1738c7944ebbSTrond Myklebust {
1739c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1740c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1741c0204fd2STrond Myklebust }
1742c0204fd2STrond Myklebust 
17431da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds /*
17461da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
17471da177e4SLinus Torvalds  */
17481da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
17491775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
17501775fd3eSDavid Quigley 				struct nfs4_label *label)
17511da177e4SLinus Torvalds {
1752fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
17532b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
17541da177e4SLinus Torvalds 	struct inode *inode;
1755b0c6108eSAl Viro 	struct dentry *d;
17561da177e4SLinus Torvalds 	int error = -EACCES;
17571da177e4SLinus Torvalds 
1758fab728e1STrond Myklebust 	d_drop(dentry);
1759fab728e1STrond Myklebust 
17601da177e4SLinus Torvalds 	/* We may have been initialized further down */
17612b0143b5SDavid Howells 	if (d_really_is_positive(dentry))
1762fab728e1STrond Myklebust 		goto out;
17631da177e4SLinus Torvalds 	if (fhandle->size == 0) {
17641775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
17651da177e4SLinus Torvalds 		if (error)
1766fab728e1STrond Myklebust 			goto out_error;
17671da177e4SLinus Torvalds 	}
17685724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17691da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
17701da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1771a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1772a841b54dSTrond Myklebust 				fattr, NULL, NULL);
17731da177e4SLinus Torvalds 		if (error < 0)
1774fab728e1STrond Myklebust 			goto out_error;
17751da177e4SLinus Torvalds 	}
17761775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1777b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1778b0c6108eSAl Viro 	if (IS_ERR(d)) {
1779b0c6108eSAl Viro 		error = PTR_ERR(d);
1780fab728e1STrond Myklebust 		goto out_error;
1781b0c6108eSAl Viro 	}
1782b0c6108eSAl Viro 	dput(d);
1783fab728e1STrond Myklebust out:
1784fab728e1STrond Myklebust 	dput(parent);
17851da177e4SLinus Torvalds 	return 0;
1786fab728e1STrond Myklebust out_error:
1787fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1788fab728e1STrond Myklebust 	dput(parent);
1789fab728e1STrond Myklebust 	return error;
17901da177e4SLinus Torvalds }
1791ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds /*
17941da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
17951da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
17961da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
17971da177e4SLinus Torvalds  * reply path made it appear to have failed.
17981da177e4SLinus Torvalds  */
1799597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1800ebfc3b49SAl Viro 		umode_t mode, bool excl)
18011da177e4SLinus Torvalds {
18021da177e4SLinus Torvalds 	struct iattr attr;
1803ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
18041da177e4SLinus Torvalds 	int error;
18051da177e4SLinus Torvalds 
18061e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
18076de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	attr.ia_mode = mode;
18101da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18111da177e4SLinus Torvalds 
18128b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
18138867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
18148b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
18151da177e4SLinus Torvalds 	if (error != 0)
18161da177e4SLinus Torvalds 		goto out_err;
18171da177e4SLinus Torvalds 	return 0;
18181da177e4SLinus Torvalds out_err:
18191da177e4SLinus Torvalds 	d_drop(dentry);
18201da177e4SLinus Torvalds 	return error;
18211da177e4SLinus Torvalds }
1822ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds /*
18251da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
18261da177e4SLinus Torvalds  */
1827597d9289SBryan Schumaker int
18281a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
18291da177e4SLinus Torvalds {
18301da177e4SLinus Torvalds 	struct iattr attr;
18311da177e4SLinus Torvalds 	int status;
18321da177e4SLinus Torvalds 
18331e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
18346de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	attr.ia_mode = mode;
18371da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18381da177e4SLinus Torvalds 
18391ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
18401da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
18411ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
18421da177e4SLinus Torvalds 	if (status != 0)
18431da177e4SLinus Torvalds 		goto out_err;
18441da177e4SLinus Torvalds 	return 0;
18451da177e4SLinus Torvalds out_err:
18461da177e4SLinus Torvalds 	d_drop(dentry);
18471da177e4SLinus Torvalds 	return status;
18481da177e4SLinus Torvalds }
1849ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds /*
18521da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
18531da177e4SLinus Torvalds  */
1854597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18551da177e4SLinus Torvalds {
18561da177e4SLinus Torvalds 	struct iattr attr;
18571da177e4SLinus Torvalds 	int error;
18581da177e4SLinus Torvalds 
18591e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
18606de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18631da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
18641da177e4SLinus Torvalds 
18651ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
18661da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
18671ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
18681da177e4SLinus Torvalds 	if (error != 0)
18691da177e4SLinus Torvalds 		goto out_err;
18701da177e4SLinus Torvalds 	return 0;
18711da177e4SLinus Torvalds out_err:
18721da177e4SLinus Torvalds 	d_drop(dentry);
18731da177e4SLinus Torvalds 	return error;
18741da177e4SLinus Torvalds }
1875ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
18761da177e4SLinus Torvalds 
1877d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1878d45b9d8bSTrond Myklebust {
1879dc3f4198SAl Viro 	if (simple_positive(dentry))
1880d45b9d8bSTrond Myklebust 		d_delete(dentry);
1881d45b9d8bSTrond Myklebust }
1882d45b9d8bSTrond Myklebust 
1883597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
18841da177e4SLinus Torvalds {
18851da177e4SLinus Torvalds 	int error;
18861da177e4SLinus Torvalds 
18871e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
18886de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18891da177e4SLinus Torvalds 
18901ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
18912b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1892884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
18931da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18941da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1895ba6c0592STrond Myklebust 		switch (error) {
1896ba6c0592STrond Myklebust 		case 0:
18972b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1898ba6c0592STrond Myklebust 			break;
1899ba6c0592STrond Myklebust 		case -ENOENT:
1900d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1901ba6c0592STrond Myklebust 		}
1902884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1903ba6c0592STrond Myklebust 	} else
1904ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
19051ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	return error;
19081da177e4SLinus Torvalds }
1909ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds /*
19121da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
19131da177e4SLinus Torvalds  * and after checking that the file has only one user.
19141da177e4SLinus Torvalds  *
19151da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
19161da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
19171da177e4SLinus Torvalds  */
19181da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
19191da177e4SLinus Torvalds {
19202b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
19212b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
19221da177e4SLinus Torvalds 	int error = -EBUSY;
19231da177e4SLinus Torvalds 
19246de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
19271da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
19281da177e4SLinus Torvalds 		error = 0;
19291da177e4SLinus Torvalds 		goto out;
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 
19321ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
19331da177e4SLinus Torvalds 	if (inode != NULL) {
1934912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
19351da177e4SLinus Torvalds 		if (error == 0)
19361b83d707STrond Myklebust 			nfs_drop_nlink(inode);
19371da177e4SLinus Torvalds 	} else
1938912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
1939d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1940d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
19411ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
19421da177e4SLinus Torvalds out:
19431da177e4SLinus Torvalds 	return error;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
19471da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
19481da177e4SLinus Torvalds  *
19491da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
19501da177e4SLinus Torvalds  */
1951597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
19521da177e4SLinus Torvalds {
19531da177e4SLinus Torvalds 	int error;
19541da177e4SLinus Torvalds 	int need_rehash = 0;
19551da177e4SLinus Torvalds 
19561e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
19576de1472fSAl Viro 		dir->i_ino, dentry);
19581da177e4SLinus Torvalds 
19591ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
19601da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
196184d08fa8SAl Viro 	if (d_count(dentry) > 1) {
19621da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1963ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
19642b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
19651da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
19661ca42382STrond Myklebust 		goto out;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
19691da177e4SLinus Torvalds 		__d_drop(dentry);
19701da177e4SLinus Torvalds 		need_rehash = 1;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
19731da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1974d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
19751da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
19761da177e4SLinus Torvalds 	} else if (need_rehash)
19771da177e4SLinus Torvalds 		d_rehash(dentry);
19781ca42382STrond Myklebust out:
19791ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
19801da177e4SLinus Torvalds 	return error;
19811da177e4SLinus Torvalds }
1982ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
19831da177e4SLinus Torvalds 
1984873101b3SChuck Lever /*
1985873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1986873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1987873101b3SChuck Lever  * using prepare_write/commit_write.
1988873101b3SChuck Lever  *
1989873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1990873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1991873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1992873101b3SChuck Lever  * symlink request has completed on the server.
1993873101b3SChuck Lever  *
1994873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1995873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1996873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1997873101b3SChuck Lever  * and move the raw page into its mapping.
1998873101b3SChuck Lever  */
1999597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20001da177e4SLinus Torvalds {
2001873101b3SChuck Lever 	struct page *page;
2002873101b3SChuck Lever 	char *kaddr;
20031da177e4SLinus Torvalds 	struct iattr attr;
2004873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
20051da177e4SLinus Torvalds 	int error;
20061da177e4SLinus Torvalds 
20071e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
20086de1472fSAl Viro 		dir->i_ino, dentry, symname);
20091da177e4SLinus Torvalds 
2010873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2011873101b3SChuck Lever 		return -ENAMETOOLONG;
20121da177e4SLinus Torvalds 
2013873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2014873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
20151da177e4SLinus Torvalds 
2016e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
201776566991STrond Myklebust 	if (!page)
2018873101b3SChuck Lever 		return -ENOMEM;
2019873101b3SChuck Lever 
2020e8ecde25SAl Viro 	kaddr = page_address(page);
2021873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2022873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2023873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2024873101b3SChuck Lever 
20251ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
202694a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
20271ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2028873101b3SChuck Lever 	if (error != 0) {
20291e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2030873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
20316de1472fSAl Viro 			dentry, symname, error);
20321da177e4SLinus Torvalds 		d_drop(dentry);
2033873101b3SChuck Lever 		__free_page(page);
20341da177e4SLinus Torvalds 		return error;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 
2037873101b3SChuck Lever 	/*
2038873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2039873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2040873101b3SChuck Lever 	 */
20412b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2042873101b3SChuck Lever 							GFP_KERNEL)) {
2043873101b3SChuck Lever 		SetPageUptodate(page);
2044873101b3SChuck Lever 		unlock_page(page);
2045a0b54addSRafael Aquini 		/*
2046a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2047a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2048a0b54addSRafael Aquini 		 */
204909cbfeafSKirill A. Shutemov 		put_page(page);
2050873101b3SChuck Lever 	} else
2051873101b3SChuck Lever 		__free_page(page);
2052873101b3SChuck Lever 
2053873101b3SChuck Lever 	return 0;
2054873101b3SChuck Lever }
2055ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2056873101b3SChuck Lever 
2057597d9289SBryan Schumaker int
20581da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
20591da177e4SLinus Torvalds {
20602b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
20611da177e4SLinus Torvalds 	int error;
20621da177e4SLinus Torvalds 
20636de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
20646de1472fSAl Viro 		old_dentry, dentry);
20651da177e4SLinus Torvalds 
20661fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
20679697d234STrond Myklebust 	d_drop(dentry);
20681da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2069cf809556STrond Myklebust 	if (error == 0) {
20707de9c6eeSAl Viro 		ihold(inode);
20719697d234STrond Myklebust 		d_add(dentry, inode);
2072cf809556STrond Myklebust 	}
20731fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
20741da177e4SLinus Torvalds 	return error;
20751da177e4SLinus Torvalds }
2076ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds /*
20791da177e4SLinus Torvalds  * RENAME
20801da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
20811da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
20821da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
20831da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
20841da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
20851da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
20861da177e4SLinus Torvalds  *
20871da177e4SLinus Torvalds  * FIXED.
20881da177e4SLinus Torvalds  *
20891da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
20901da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
20911da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
20921da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
20931da177e4SLinus Torvalds  * using the inode layer
20941da177e4SLinus Torvalds  *
20951da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
20961da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
20971da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
20981da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
20991da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
21001da177e4SLinus Torvalds  * the rename.
21011da177e4SLinus Torvalds  */
2102597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
21031cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
21041cd66c93SMiklos Szeredi 	       unsigned int flags)
21051da177e4SLinus Torvalds {
21062b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
21072b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2108d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
210980a491fdSJeff Layton 	struct rpc_task *task;
21101da177e4SLinus Torvalds 	int error = -EBUSY;
21111da177e4SLinus Torvalds 
21121cd66c93SMiklos Szeredi 	if (flags)
21131cd66c93SMiklos Szeredi 		return -EINVAL;
21141cd66c93SMiklos Szeredi 
21156de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
21166de1472fSAl Viro 		 old_dentry, new_dentry,
211784d08fa8SAl Viro 		 d_count(new_dentry));
21181da177e4SLinus Torvalds 
211970ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
21201da177e4SLinus Torvalds 	/*
212128f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
212228f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
212328f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
212428f79a1aSMiklos Szeredi 	 * the new target.
21251da177e4SLinus Torvalds 	 */
212627226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
212727226104SMiklos Szeredi 		/*
212827226104SMiklos Szeredi 		 * To prevent any new references to the target during the
212927226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
213027226104SMiklos Szeredi 		 */
2131d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
213227226104SMiklos Szeredi 			d_drop(new_dentry);
2133d9f29500SBenjamin Coddington 			rehash = new_dentry;
2134d9f29500SBenjamin Coddington 		}
213527226104SMiklos Szeredi 
213684d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
21371da177e4SLinus Torvalds 			int err;
213827226104SMiklos Szeredi 
21391da177e4SLinus Torvalds 			/* copy the target dentry's name */
21401da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
21411da177e4SLinus Torvalds 					 &new_dentry->d_name);
21421da177e4SLinus Torvalds 			if (!dentry)
21431da177e4SLinus Torvalds 				goto out;
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
21461da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
214724e93025SMiklos Szeredi 			if (err)
21481da177e4SLinus Torvalds 				goto out;
214924e93025SMiklos Szeredi 
215024e93025SMiklos Szeredi 			new_dentry = dentry;
2151d9f29500SBenjamin Coddington 			rehash = NULL;
215224e93025SMiklos Szeredi 			new_inode = NULL;
2153b1e4adf4STrond Myklebust 		}
215427226104SMiklos Szeredi 	}
21551da177e4SLinus Torvalds 
2156d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
215780a491fdSJeff Layton 	if (IS_ERR(task)) {
215880a491fdSJeff Layton 		error = PTR_ERR(task);
215980a491fdSJeff Layton 		goto out;
216080a491fdSJeff Layton 	}
216180a491fdSJeff Layton 
216280a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2163818a8dbeSBenjamin Coddington 	if (error != 0) {
2164818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2165818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2166818a8dbeSBenjamin Coddington 		smp_wmb();
2167818a8dbeSBenjamin Coddington 	} else
216880a491fdSJeff Layton 		error = task->tk_status;
216980a491fdSJeff Layton 	rpc_put_task(task);
217059a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
217159a707b0STrond Myklebust 	if (error == 0) {
217259a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
217359a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
217459a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
217559a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
217659a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
217759a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
217859a707b0STrond Myklebust 	}
21791da177e4SLinus Torvalds out:
2180d9f29500SBenjamin Coddington 	if (rehash)
2181d9f29500SBenjamin Coddington 		d_rehash(rehash);
218270ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
218370ded201STrond Myklebust 			new_dir, new_dentry, error);
2184d9f29500SBenjamin Coddington 	if (!error) {
2185d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2186d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2187d9f29500SBenjamin Coddington 		/*
2188d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2189d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2190d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2191d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2192d9f29500SBenjamin Coddington 		 */
2193d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2194d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2195d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2196d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2197d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2198d9f29500SBenjamin Coddington 
21991da177e4SLinus Torvalds 	/* new dentry created? */
22001da177e4SLinus Torvalds 	if (dentry)
22011da177e4SLinus Torvalds 		dput(dentry);
22021da177e4SLinus Torvalds 	return error;
22031da177e4SLinus Torvalds }
2204ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
22051da177e4SLinus Torvalds 
2206cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2207cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2208cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2209cfcea3e8STrond Myklebust 
22103a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
22113a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
22123a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
22133a505845STrond Myklebust 
22141c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
22151c3c07e9STrond Myklebust {
2216b68572e0SNeilBrown 	put_cred(entry->cred);
2217f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
22184e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2219cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
22204e857c58SPeter Zijlstra 	smp_mb__after_atomic();
22211c3c07e9STrond Myklebust }
22221c3c07e9STrond Myklebust 
22231a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
22241a81bb8aSTrond Myklebust {
22251a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
22261a81bb8aSTrond Myklebust 
22271a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
22281a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
22291a81bb8aSTrond Myklebust 		list_del(&cache->lru);
22301a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
22311a81bb8aSTrond Myklebust 	}
22321a81bb8aSTrond Myklebust }
22331a81bb8aSTrond Myklebust 
22343a505845STrond Myklebust static unsigned long
22353a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2236979df72eSTrond Myklebust {
2237979df72eSTrond Myklebust 	LIST_HEAD(head);
2238aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2239979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
22401ab6c499SDave Chinner 	long freed = 0;
2241979df72eSTrond Myklebust 
2242a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2243aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2244979df72eSTrond Myklebust 		struct inode *inode;
2245979df72eSTrond Myklebust 
2246979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2247979df72eSTrond Myklebust 			break;
22489c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2249979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2250979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2251979df72eSTrond Myklebust 			goto remove_lru_entry;
2252979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2253979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2254979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2255979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
22561ab6c499SDave Chinner 		freed++;
2257979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2258979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2259979df72eSTrond Myklebust 					&nfs_access_lru_list);
2260979df72eSTrond Myklebust 		else {
2261979df72eSTrond Myklebust remove_lru_entry:
2262979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
22634e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2264979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
22654e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2266979df72eSTrond Myklebust 		}
226759844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2268979df72eSTrond Myklebust 	}
2269979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22701a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22711ab6c499SDave Chinner 	return freed;
22721ab6c499SDave Chinner }
22731ab6c499SDave Chinner 
22741ab6c499SDave Chinner unsigned long
22753a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
22763a505845STrond Myklebust {
22773a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
22783a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
22793a505845STrond Myklebust 
22803a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
22813a505845STrond Myklebust 		return SHRINK_STOP;
22823a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
22833a505845STrond Myklebust }
22843a505845STrond Myklebust 
22853a505845STrond Myklebust 
22863a505845STrond Myklebust unsigned long
22871ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
22881ab6c499SDave Chinner {
228955f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2290979df72eSTrond Myklebust }
2291979df72eSTrond Myklebust 
22923a505845STrond Myklebust static void
22933a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
22943a505845STrond Myklebust {
22953a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
22963a505845STrond Myklebust 	unsigned long diff;
22973a505845STrond Myklebust 	unsigned int nr_to_scan;
22983a505845STrond Myklebust 
22993a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
23003a505845STrond Myklebust 		return;
23013a505845STrond Myklebust 	nr_to_scan = 100;
23023a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
23033a505845STrond Myklebust 	if (diff < nr_to_scan)
23043a505845STrond Myklebust 		nr_to_scan = diff;
23053a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
23063a505845STrond Myklebust }
23073a505845STrond Myklebust 
23081a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
23091c3c07e9STrond Myklebust {
23101c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
23111a81bb8aSTrond Myklebust 	struct rb_node *n;
23121c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
23131c3c07e9STrond Myklebust 
23141c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
23151c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
23161c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
23171c3c07e9STrond Myklebust 		rb_erase(n, root_node);
23181a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
23191c3c07e9STrond Myklebust 	}
23201c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
23211c3c07e9STrond Myklebust }
23221c3c07e9STrond Myklebust 
23231c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
23241c3c07e9STrond Myklebust {
23251a81bb8aSTrond Myklebust 	LIST_HEAD(head);
23261a81bb8aSTrond Myklebust 
23271a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
23281a81bb8aSTrond Myklebust 		return;
2329cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2330cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
23311a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2332cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2333cfcea3e8STrond Myklebust 
23341c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
23351a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
23361a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
23371a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
23381a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
23391c3c07e9STrond Myklebust }
23401c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
23411c3c07e9STrond Myklebust 
2342b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
23431c3c07e9STrond Myklebust {
23441c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
23451c3c07e9STrond Myklebust 
23461c3c07e9STrond Myklebust 	while (n != NULL) {
2347b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2348b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2349b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
23501c3c07e9STrond Myklebust 
2351b68572e0SNeilBrown 		if (cmp < 0)
23521c3c07e9STrond Myklebust 			n = n->rb_left;
2353b68572e0SNeilBrown 		else if (cmp > 0)
23541c3c07e9STrond Myklebust 			n = n->rb_right;
23551c3c07e9STrond Myklebust 		else
23561c3c07e9STrond Myklebust 			return entry;
23571c3c07e9STrond Myklebust 	}
23581c3c07e9STrond Myklebust 	return NULL;
23591c3c07e9STrond Myklebust }
23601c3c07e9STrond Myklebust 
2361b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
23621da177e4SLinus Torvalds {
236355296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
23641c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
236557b69181STrond Myklebust 	bool retry = true;
236657b69181STrond Myklebust 	int err;
23671da177e4SLinus Torvalds 
23681c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
236957b69181STrond Myklebust 	for(;;) {
23701c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
23711c3c07e9STrond Myklebust 			goto out_zap;
23721c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
237357b69181STrond Myklebust 		err = -ENOENT;
23741c3c07e9STrond Myklebust 		if (cache == NULL)
23751c3c07e9STrond Myklebust 			goto out;
237657b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
237721c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
237857b69181STrond Myklebust 			break;
237957b69181STrond Myklebust 		err = -ECHILD;
238057b69181STrond Myklebust 		if (!may_block)
238157b69181STrond Myklebust 			goto out;
238257b69181STrond Myklebust 		if (!retry)
238357b69181STrond Myklebust 			goto out_zap;
238457b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
238557b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
238657b69181STrond Myklebust 		if (err)
238757b69181STrond Myklebust 			return err;
238857b69181STrond Myklebust 		spin_lock(&inode->i_lock);
238957b69181STrond Myklebust 		retry = false;
239057b69181STrond Myklebust 	}
23911c3c07e9STrond Myklebust 	res->cred = cache->cred;
23921c3c07e9STrond Myklebust 	res->mask = cache->mask;
2393cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
23941c3c07e9STrond Myklebust 	err = 0;
23951c3c07e9STrond Myklebust out:
23961c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23971c3c07e9STrond Myklebust 	return err;
23981c3c07e9STrond Myklebust out_zap:
23991a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
24001a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
24011c3c07e9STrond Myklebust 	return -ENOENT;
24021c3c07e9STrond Myklebust }
24031c3c07e9STrond Myklebust 
2404b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2405f682a398SNeilBrown {
2406f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2407f682a398SNeilBrown 	 * but do it without locking.
2408f682a398SNeilBrown 	 */
2409f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2410f682a398SNeilBrown 	struct nfs_access_entry *cache;
2411f682a398SNeilBrown 	int err = -ECHILD;
2412f682a398SNeilBrown 	struct list_head *lh;
2413f682a398SNeilBrown 
2414f682a398SNeilBrown 	rcu_read_lock();
2415f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2416f682a398SNeilBrown 		goto out;
2417f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2418f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2419f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2420f682a398SNeilBrown 	    cred != cache->cred)
2421f682a398SNeilBrown 		cache = NULL;
2422f682a398SNeilBrown 	if (cache == NULL)
2423f682a398SNeilBrown 		goto out;
242421c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2425f682a398SNeilBrown 		goto out;
2426f682a398SNeilBrown 	res->cred = cache->cred;
2427f682a398SNeilBrown 	res->mask = cache->mask;
242821c3ba7eSTrond Myklebust 	err = 0;
2429f682a398SNeilBrown out:
2430f682a398SNeilBrown 	rcu_read_unlock();
2431f682a398SNeilBrown 	return err;
2432f682a398SNeilBrown }
2433f682a398SNeilBrown 
24341c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
24351c3c07e9STrond Myklebust {
2436cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2437cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
24381c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
24391c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
24401c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2441b68572e0SNeilBrown 	int cmp;
24421c3c07e9STrond Myklebust 
24431c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
24441c3c07e9STrond Myklebust 	while (*p != NULL) {
24451c3c07e9STrond Myklebust 		parent = *p;
24461c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2447b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
24481c3c07e9STrond Myklebust 
2449b68572e0SNeilBrown 		if (cmp < 0)
24501c3c07e9STrond Myklebust 			p = &parent->rb_left;
2451b68572e0SNeilBrown 		else if (cmp > 0)
24521c3c07e9STrond Myklebust 			p = &parent->rb_right;
24531c3c07e9STrond Myklebust 		else
24541c3c07e9STrond Myklebust 			goto found;
24551c3c07e9STrond Myklebust 	}
24561c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
24571c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2458cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
24591c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24601c3c07e9STrond Myklebust 	return;
24611c3c07e9STrond Myklebust found:
24621c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2463cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2464cfcea3e8STrond Myklebust 	list_del(&entry->lru);
24651c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24661c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
24671da177e4SLinus Torvalds }
24681da177e4SLinus Torvalds 
24696168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
24701da177e4SLinus Torvalds {
24711c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
24721c3c07e9STrond Myklebust 	if (cache == NULL)
24731c3c07e9STrond Myklebust 		return;
24741c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2475b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
24761da177e4SLinus Torvalds 	cache->mask = set->mask;
24771c3c07e9STrond Myklebust 
2478f682a398SNeilBrown 	/* The above field assignments must be visible
2479f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2480f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2481f682a398SNeilBrown 	 */
2482f682a398SNeilBrown 	smp_wmb();
24831c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2484cfcea3e8STrond Myklebust 
2485cfcea3e8STrond Myklebust 	/* Update accounting */
24864e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2487cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
24884e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2489cfcea3e8STrond Myklebust 
2490cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
24911a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2492cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
24931a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
24941a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
24951a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2496cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2497cfcea3e8STrond Myklebust 	}
24983a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
24991da177e4SLinus Torvalds }
25006168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
25016168f62cSWeston Andros Adamson 
25023c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
25033c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
25043c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
25053c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
25063c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
25073c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2508ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
25093c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
25103c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
251115d4b73aSTrond Myklebust static int
2512ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
251315d4b73aSTrond Myklebust {
251415d4b73aSTrond Myklebust 	int mask = 0;
251515d4b73aSTrond Myklebust 
251615d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
251715d4b73aSTrond Myklebust 		mask |= MAY_READ;
2518ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2519ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
252015d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2521ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
252215d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2523ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2524ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2525ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2526ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
252715d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2528ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2529ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
253015d4b73aSTrond Myklebust 	return mask;
253115d4b73aSTrond Myklebust }
253215d4b73aSTrond Myklebust 
25336168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
25346168f62cSWeston Andros Adamson {
2535bd8b2441STrond Myklebust 	entry->mask = access_result;
25366168f62cSWeston Andros Adamson }
25376168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
25381da177e4SLinus Torvalds 
2539b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
25401da177e4SLinus Torvalds {
25411da177e4SLinus Torvalds 	struct nfs_access_entry cache;
254257b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2543bd8b2441STrond Myklebust 	int cache_mask;
25441da177e4SLinus Torvalds 	int status;
25451da177e4SLinus Torvalds 
2546f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2547f4ce1299STrond Myklebust 
2548f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2549f682a398SNeilBrown 	if (status != 0)
255057b69181STrond Myklebust 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
25511da177e4SLinus Torvalds 	if (status == 0)
2552f4ce1299STrond Myklebust 		goto out_cached;
25531da177e4SLinus Torvalds 
2554f3324a2aSNeilBrown 	status = -ECHILD;
255557b69181STrond Myklebust 	if (!may_block)
2556f3324a2aSNeilBrown 		goto out;
2557f3324a2aSNeilBrown 
25581750d929SAnna Schumaker 	/*
25591750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
25601750d929SAnna Schumaker 	 */
25611750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
25621750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
25631750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
25641750d929SAnna Schumaker 	else
25651750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
25661da177e4SLinus Torvalds 	cache.cred = cred;
25671da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2568a71ee337SSuresh Jayaraman 	if (status != 0) {
2569a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2570a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2571a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2572a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2573a71ee337SSuresh Jayaraman 		}
2574f4ce1299STrond Myklebust 		goto out;
2575a71ee337SSuresh Jayaraman 	}
25761da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2577f4ce1299STrond Myklebust out_cached:
2578ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2579bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2580f4ce1299STrond Myklebust 		status = -EACCES;
25811da177e4SLinus Torvalds out:
2582f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2583f4ce1299STrond Myklebust 	return status;
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds 
2586af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2587af22f94aSTrond Myklebust {
2588af22f94aSTrond Myklebust 	int mask = 0;
2589af22f94aSTrond Myklebust 
2590f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2591f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2592f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2593f8d9a897SWeston Andros Adamson 	} else {
25948a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2595af22f94aSTrond Myklebust 			mask |= MAY_READ;
25968a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2597af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2598f8d9a897SWeston Andros Adamson 	}
2599f8d9a897SWeston Andros Adamson 
2600af22f94aSTrond Myklebust 	return mask;
2601af22f94aSTrond Myklebust }
2602af22f94aSTrond Myklebust 
2603b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2604af22f94aSTrond Myklebust {
2605af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2606af22f94aSTrond Myklebust }
260789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2608af22f94aSTrond Myklebust 
26095c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
26105c5fc09aSTrond Myklebust {
26115c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
261221c3ba7eSTrond Myklebust 	int ret = 0;
26135c5fc09aSTrond Myklebust 
26143825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
26153825827eSTrond Myklebust 		return 0;
2616cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
26175c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
261821c3ba7eSTrond Myklebust 			return -ECHILD;
261921c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
262021c3ba7eSTrond Myklebust 	}
26215c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
26225c5fc09aSTrond Myklebust 		ret = -EACCES;
26235c5fc09aSTrond Myklebust 	return ret;
26245c5fc09aSTrond Myklebust }
26255c5fc09aSTrond Myklebust 
262610556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
26271da177e4SLinus Torvalds {
2628b68572e0SNeilBrown 	const struct cred *cred = current_cred();
26291da177e4SLinus Torvalds 	int res = 0;
26301da177e4SLinus Torvalds 
263191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
263291d5b470SChuck Lever 
2633e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
26341da177e4SLinus Torvalds 		goto out;
26351da177e4SLinus Torvalds 	/* Is this sys_access() ? */
26369cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
26371da177e4SLinus Torvalds 		goto force_lookup;
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
26401da177e4SLinus Torvalds 		case S_IFLNK:
26411da177e4SLinus Torvalds 			goto out;
26421da177e4SLinus Torvalds 		case S_IFREG:
2643762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2644762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2645762674f8STrond Myklebust 				return 0;
26461da177e4SLinus Torvalds 			break;
26471da177e4SLinus Torvalds 		case S_IFDIR:
26481da177e4SLinus Torvalds 			/*
26491da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
26501da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
26511da177e4SLinus Torvalds 			 */
26521da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
26531da177e4SLinus Torvalds 				goto out;
26541da177e4SLinus Torvalds 	}
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds force_lookup:
26571da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
26581da177e4SLinus Torvalds 		goto out_notsup;
26591da177e4SLinus Torvalds 
2660f3324a2aSNeilBrown 	/* Always try fast lookups first */
2661f3324a2aSNeilBrown 	rcu_read_lock();
2662f3324a2aSNeilBrown 	res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2663f3324a2aSNeilBrown 	rcu_read_unlock();
2664f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2665f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
26661da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
2667f3324a2aSNeilBrown 	}
26681da177e4SLinus Torvalds out:
26695c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
26705c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2671f696a365SMiklos Szeredi 
26721e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
26731e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
26741da177e4SLinus Torvalds 	return res;
26751da177e4SLinus Torvalds out_notsup:
2676d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2677d51ac1a8SNeilBrown 		return -ECHILD;
2678d51ac1a8SNeilBrown 
26791da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
26801da177e4SLinus Torvalds 	if (res == 0)
26812830ba7fSAl Viro 		res = generic_permission(inode, mask);
26821e7cb3dcSChuck Lever 	goto out;
26831da177e4SLinus Torvalds }
2684ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds /*
26871da177e4SLinus Torvalds  * Local variables:
26881da177e4SLinus Torvalds  *  version-control: t
26891da177e4SLinus Torvalds  *  kept-new-versions: 5
26901da177e4SLinus Torvalds  * End:
26911da177e4SLinus Torvalds  */
2692