xref: /openbmc/linux/fs/nfs/dir.c (revision 457c8996)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  nfs directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
101da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
111da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
121da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
131da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
141da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
151da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
161da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
171da177e4SLinus Torvalds  *              layer (iput() and friends).
181da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21ddda8e0aSBryan Schumaker #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
311da177e4SLinus Torvalds #include <linux/nfs_fs.h>
321da177e4SLinus Torvalds #include <linux/nfs_mount.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
34873101b3SChuck Lever #include <linux/pagevec.h>
351da177e4SLinus Torvalds #include <linux/namei.h>
3654ceac45SDavid Howells #include <linux/mount.h>
37a0b8cab3SMel Gorman #include <linux/swap.h>
38e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "delegation.h"
4391d5b470SChuck Lever #include "iostat.h"
444c30d56eSAdrian Bunk #include "internal.h"
45cd9a1c0eSTrond Myklebust #include "fscache.h"
461da177e4SLinus Torvalds 
47f4ce1299STrond Myklebust #include "nfstrace.h"
48f4ce1299STrond Myklebust 
491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
571da177e4SLinus Torvalds 
584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
59f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
601da177e4SLinus Torvalds 	.read		= generic_read_dir,
61b044f645SBenjamin Coddington 	.iterate	= nfs_readdir,
621da177e4SLinus Torvalds 	.open		= nfs_opendir,
63480c2006SBryan Schumaker 	.release	= nfs_closedir,
641da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
69d1bacf9eSBryan Schumaker };
70d1bacf9eSBryan Schumaker 
71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
72480c2006SBryan Schumaker {
73311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
74480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
75480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76480c2006SBryan Schumaker 	if (ctx != NULL) {
778ef2ce3eSBryan Schumaker 		ctx->duped = 0;
78311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
79480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
808ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
81684f39b4SNeilBrown 		ctx->cred = get_cred(cred);
82311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
83311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
84311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
85480c2006SBryan Schumaker 		return ctx;
86480c2006SBryan Schumaker 	}
870c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
880c030806STrond Myklebust }
89480c2006SBryan Schumaker 
90311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
91480c2006SBryan Schumaker {
92311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
93311324adSTrond Myklebust 	list_del(&ctx->list);
94311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
95684f39b4SNeilBrown 	put_cred(ctx->cred);
96480c2006SBryan Schumaker 	kfree(ctx);
97480c2006SBryan Schumaker }
98480c2006SBryan Schumaker 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * Open file
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds static int
1031da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1041da177e4SLinus Torvalds {
105480c2006SBryan Schumaker 	int res = 0;
106480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1071da177e4SLinus Torvalds 
1086de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
109cc0dd2d1SChuck Lever 
110cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1111e7cb3dcSChuck Lever 
112684f39b4SNeilBrown 	ctx = alloc_nfs_open_dir_context(inode, current_cred());
113480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
114480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
115480c2006SBryan Schumaker 		goto out;
116480c2006SBryan Schumaker 	}
117480c2006SBryan Schumaker 	filp->private_data = ctx;
118480c2006SBryan Schumaker out:
1191da177e4SLinus Torvalds 	return res;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
122480c2006SBryan Schumaker static int
123480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
124480c2006SBryan Schumaker {
125a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
126480c2006SBryan Schumaker 	return 0;
127480c2006SBryan Schumaker }
128480c2006SBryan Schumaker 
129d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
130d1bacf9eSBryan Schumaker 	u64 cookie;
131d1bacf9eSBryan Schumaker 	u64 ino;
132d1bacf9eSBryan Schumaker 	struct qstr string;
1330b26a0bfSTrond Myklebust 	unsigned char d_type;
134d1bacf9eSBryan Schumaker };
135d1bacf9eSBryan Schumaker 
136d1bacf9eSBryan Schumaker struct nfs_cache_array {
13788b8e133SChuck Lever 	int size;
138d1bacf9eSBryan Schumaker 	int eof_index;
139d1bacf9eSBryan Schumaker 	u64 last_cookie;
140d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
141d1bacf9eSBryan Schumaker };
142d1bacf9eSBryan Schumaker 
143be4c2d47Sluanshi struct readdirvec {
144be4c2d47Sluanshi 	unsigned long nr;
145be4c2d47Sluanshi 	unsigned long index;
146be4c2d47Sluanshi 	struct page *pages[NFS_MAX_READDIR_RAPAGES];
147be4c2d47Sluanshi };
148be4c2d47Sluanshi 
149a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
1501da177e4SLinus Torvalds typedef struct {
1511da177e4SLinus Torvalds 	struct file	*file;
1521da177e4SLinus Torvalds 	struct page	*page;
15323db8620SAl Viro 	struct dir_context *ctx;
1541da177e4SLinus Torvalds 	unsigned long	page_index;
155be4c2d47Sluanshi 	struct readdirvec pvec;
156f0dd2136STrond Myklebust 	u64		*dir_cookie;
1570aded708STrond Myklebust 	u64		last_cookie;
158f0dd2136STrond Myklebust 	loff_t		current_index;
1591da177e4SLinus Torvalds 	decode_dirent_t	decode;
160d1bacf9eSBryan Schumaker 
1611f4eab7eSNeil Brown 	unsigned long	timestamp;
1624704f0e2STrond Myklebust 	unsigned long	gencount;
163d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
164a7a3b1e9SBenjamin Coddington 	bool plus;
165a7a3b1e9SBenjamin Coddington 	bool eof;
1661da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1671da177e4SLinus Torvalds 
168d1bacf9eSBryan Schumaker /*
169d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
170d1bacf9eSBryan Schumaker  */
171d1bacf9eSBryan Schumaker static
17211de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
173d1bacf9eSBryan Schumaker {
17411de3b11STrond Myklebust 	struct nfs_cache_array *array;
175d1bacf9eSBryan Schumaker 	int i;
1768cd51a0cSTrond Myklebust 
1772b86ce2dSCong Wang 	array = kmap_atomic(page);
178d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
179d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1802b86ce2dSCong Wang 	kunmap_atomic(array);
181d1bacf9eSBryan Schumaker }
182d1bacf9eSBryan Schumaker 
183d1bacf9eSBryan Schumaker /*
184d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
185d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
186d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
187d1bacf9eSBryan Schumaker  */
188d1bacf9eSBryan Schumaker static
1894a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
190d1bacf9eSBryan Schumaker {
191d1bacf9eSBryan Schumaker 	string->len = len;
192d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
1934a201d6eSTrond Myklebust 	if (string->name == NULL)
1944a201d6eSTrond Myklebust 		return -ENOMEM;
19504e4bd1cSCatalin Marinas 	/*
19604e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
19704e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
19804e4bd1cSCatalin Marinas 	 */
19904e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2008387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2014a201d6eSTrond Myklebust 	return 0;
202d1bacf9eSBryan Schumaker }
203d1bacf9eSBryan Schumaker 
204d1bacf9eSBryan Schumaker static
205d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
206d1bacf9eSBryan Schumaker {
2070795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2084a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2094a201d6eSTrond Myklebust 	int ret;
2104a201d6eSTrond Myklebust 
2114a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2123020093fSTrond Myklebust 
2133020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2143020093fSTrond Myklebust 	ret = -ENOSPC;
2153020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2163020093fSTrond Myklebust 		goto out;
2173020093fSTrond Myklebust 
2184a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2194a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2200b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2214a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2224a201d6eSTrond Myklebust 	if (ret)
2234a201d6eSTrond Myklebust 		goto out;
224d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2258cd51a0cSTrond Myklebust 	array->size++;
22647c716cbSTrond Myklebust 	if (entry->eof != 0)
227d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2284a201d6eSTrond Myklebust out:
2290795bf83SFabian Frederick 	kunmap(page);
2304a201d6eSTrond Myklebust 	return ret;
231d1bacf9eSBryan Schumaker }
232d1bacf9eSBryan Schumaker 
233d1bacf9eSBryan Schumaker static
234d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
235d1bacf9eSBryan Schumaker {
23623db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
237d1bacf9eSBryan Schumaker 	unsigned int index;
238d1bacf9eSBryan Schumaker 
239d1bacf9eSBryan Schumaker 	if (diff < 0)
240d1bacf9eSBryan Schumaker 		goto out_eof;
241d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2428cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
243d1bacf9eSBryan Schumaker 			goto out_eof;
244d1bacf9eSBryan Schumaker 		return -EAGAIN;
245d1bacf9eSBryan Schumaker 	}
246d1bacf9eSBryan Schumaker 
247d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
248d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
249d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
250d1bacf9eSBryan Schumaker 	return 0;
251d1bacf9eSBryan Schumaker out_eof:
2526089dd0dSThomas Meyer 	desc->eof = true;
253d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
254d1bacf9eSBryan Schumaker }
255d1bacf9eSBryan Schumaker 
2564db72b40SJeff Layton static bool
2574db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2584db72b40SJeff Layton {
2594db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2604db72b40SJeff Layton 		return false;
2614db72b40SJeff Layton 	smp_rmb();
2624db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2634db72b40SJeff Layton }
2644db72b40SJeff Layton 
265d1bacf9eSBryan Schumaker static
266d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
267d1bacf9eSBryan Schumaker {
268d1bacf9eSBryan Schumaker 	int i;
2698ef2ce3eSBryan Schumaker 	loff_t new_pos;
270d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
271d1bacf9eSBryan Schumaker 
272d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2738cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
274496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2750c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2760c030806STrond Myklebust 
2778ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2784db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
2794db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
2800c030806STrond Myklebust 				ctx->duped = 0;
2810c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
28223db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2830c030806STrond Myklebust 				if (ctx->duped > 0
2840c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2850c030806STrond Myklebust 					if (printk_ratelimit()) {
2866de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
2870c030806STrond Myklebust 								"Please contact your server vendor.  "
2889581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
2899581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
2909581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
2910c030806STrond Myklebust 					}
2920c030806STrond Myklebust 					status = -ELOOP;
2930c030806STrond Myklebust 					goto out;
2940c030806STrond Myklebust 				}
2958ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
2960c030806STrond Myklebust 				ctx->duped = -1;
2978ef2ce3eSBryan Schumaker 			}
29823db8620SAl Viro 			desc->ctx->pos = new_pos;
2998cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
30047c716cbSTrond Myklebust 			return 0;
3018cd51a0cSTrond Myklebust 		}
3028cd51a0cSTrond Myklebust 	}
30347c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
304d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
30518fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
3066089dd0dSThomas Meyer 			desc->eof = true;
307d1bacf9eSBryan Schumaker 	}
3080c030806STrond Myklebust out:
309d1bacf9eSBryan Schumaker 	return status;
310d1bacf9eSBryan Schumaker }
311d1bacf9eSBryan Schumaker 
312d1bacf9eSBryan Schumaker static
313d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
314d1bacf9eSBryan Schumaker {
315d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
31647c716cbSTrond Myklebust 	int status;
317d1bacf9eSBryan Schumaker 
3180795bf83SFabian Frederick 	array = kmap(desc->page);
319d1bacf9eSBryan Schumaker 
320d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
321d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
322d1bacf9eSBryan Schumaker 	else
323d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
324d1bacf9eSBryan Schumaker 
32547c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3260aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
327e47c085aSTrond Myklebust 		desc->current_index += array->size;
32847c716cbSTrond Myklebust 		desc->page_index++;
32947c716cbSTrond Myklebust 	}
3300795bf83SFabian Frederick 	kunmap(desc->page);
331d1bacf9eSBryan Schumaker 	return status;
332d1bacf9eSBryan Schumaker }
333d1bacf9eSBryan Schumaker 
334d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
335d1bacf9eSBryan Schumaker static
33656e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
337d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
338d1bacf9eSBryan Schumaker {
339480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
340684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
3414704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3421da177e4SLinus Torvalds 	int		error;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds  again:
3451da177e4SLinus Torvalds 	timestamp = jiffies;
3464704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
347be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3481da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3491da177e4SLinus Torvalds 	if (error < 0) {
3501da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3511da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3521da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3533a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
354a7a3b1e9SBenjamin Coddington 			desc->plus = false;
3551da177e4SLinus Torvalds 			goto again;
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 		goto error;
3581da177e4SLinus Torvalds 	}
3591f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3604704f0e2STrond Myklebust 	desc->gencount = gencount;
361d1bacf9eSBryan Schumaker error:
362d1bacf9eSBryan Schumaker 	return error;
363d1bacf9eSBryan Schumaker }
364d1bacf9eSBryan Schumaker 
365573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
366573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
367d1bacf9eSBryan Schumaker {
368573c4e1eSChuck Lever 	int error;
369d1bacf9eSBryan Schumaker 
370573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
371573c4e1eSChuck Lever 	if (error)
372573c4e1eSChuck Lever 		return error;
373d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
374d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
375d1bacf9eSBryan Schumaker 	return 0;
376d1bacf9eSBryan Schumaker }
377d1bacf9eSBryan Schumaker 
378fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
379fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
380fa923369STrond Myklebust  */
381d39ab9deSBryan Schumaker static
382d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
383d39ab9deSBryan Schumaker {
384d8fdb47fSTrond Myklebust 	struct inode *inode;
385fa923369STrond Myklebust 	struct nfs_inode *nfsi;
386fa923369STrond Myklebust 
3872b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
3882b0143b5SDavid Howells 		return 0;
389fa923369STrond Myklebust 
390d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
391d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
392d8fdb47fSTrond Myklebust 		return 0;
393d8fdb47fSTrond Myklebust 
394d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
3957dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
396d39ab9deSBryan Schumaker 		return 0;
3977dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
3987dc72d5fSTrond Myklebust 		return 0;
3997dc72d5fSTrond Myklebust 	return 1;
400d39ab9deSBryan Schumaker }
401d39ab9deSBryan Schumaker 
402d39ab9deSBryan Schumaker static
40323db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
404d69ee9b8STrond Myklebust {
405d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
406d69ee9b8STrond Myklebust 		return false;
407d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
408d69ee9b8STrond Myklebust 		return true;
40923db8620SAl Viro 	if (ctx->pos == 0)
410d69ee9b8STrond Myklebust 		return true;
411d69ee9b8STrond Myklebust 	return false;
412d69ee9b8STrond Myklebust }
413d69ee9b8STrond Myklebust 
414d69ee9b8STrond Myklebust /*
41563519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
41663519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
417d69ee9b8STrond Myklebust  * directory.
418d69ee9b8STrond Myklebust  */
419d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
420d69ee9b8STrond Myklebust {
42163519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
42263519fbcSTrond Myklebust 
42363519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
42463519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
42563519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
426d69ee9b8STrond Myklebust }
427d69ee9b8STrond Myklebust 
428311324adSTrond Myklebust /*
429311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
430311324adSTrond Myklebust  *
431311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
432311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
433311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
434311324adSTrond Myklebust  * cache flush.
435311324adSTrond Myklebust  */
436311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
437311324adSTrond Myklebust {
43863519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
43963519fbcSTrond Myklebust 
44063519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
44163519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
44263519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
44379f687a3STrond Myklebust 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
444311324adSTrond Myklebust 	}
445311324adSTrond Myklebust }
446311324adSTrond Myklebust 
447d69ee9b8STrond Myklebust static
448d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
449d39ab9deSBryan Schumaker {
45026fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4519ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4524a201d6eSTrond Myklebust 	struct dentry *dentry;
4534a201d6eSTrond Myklebust 	struct dentry *alias;
4542b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
455d39ab9deSBryan Schumaker 	struct inode *inode;
456aa9c2669SDavid Quigley 	int status;
457d39ab9deSBryan Schumaker 
458fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
459fa923369STrond Myklebust 		return;
4606c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
4616c441c25STrond Myklebust 		return;
46278d04af4STrond Myklebust 	if (filename.len == 0)
46378d04af4STrond Myklebust 		return;
46478d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
46578d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
46678d04af4STrond Myklebust 		return;
46778d04af4STrond Myklebust 	/* ...or '/' */
46878d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
46978d04af4STrond Myklebust 		return;
4704a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4714a201d6eSTrond Myklebust 		if (filename.len == 1)
4724a201d6eSTrond Myklebust 			return;
4734a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4744a201d6eSTrond Myklebust 			return;
4754a201d6eSTrond Myklebust 	}
4768387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
477d39ab9deSBryan Schumaker 
4784a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
4799ac3d3e8SAl Viro again:
4809ac3d3e8SAl Viro 	if (!dentry) {
4819ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
4829ac3d3e8SAl Viro 		if (IS_ERR(dentry))
4839ac3d3e8SAl Viro 			return;
4849ac3d3e8SAl Viro 	}
4859ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
4866c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
4876c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
4886c441c25STrond Myklebust 					&entry->fattr->fsid))
4896c441c25STrond Myklebust 			goto out;
490d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
4917dc72d5fSTrond Myklebust 			if (!entry->fh->size)
4927dc72d5fSTrond Myklebust 				goto out;
493cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
4942b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
495aa9c2669SDavid Quigley 			if (!status)
4962b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
497d39ab9deSBryan Schumaker 			goto out;
498d39ab9deSBryan Schumaker 		} else {
4995542aa2fSEric W. Biederman 			d_invalidate(dentry);
500d39ab9deSBryan Schumaker 			dput(dentry);
5019ac3d3e8SAl Viro 			dentry = NULL;
5029ac3d3e8SAl Viro 			goto again;
503d39ab9deSBryan Schumaker 		}
504d39ab9deSBryan Schumaker 	}
5057dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5067dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5077dc72d5fSTrond Myklebust 		goto out;
5087dc72d5fSTrond Myklebust 	}
509d39ab9deSBryan Schumaker 
5101775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
51141d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5129ac3d3e8SAl Viro 	d_lookup_done(dentry);
5139ac3d3e8SAl Viro 	if (alias) {
514d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
515d39ab9deSBryan Schumaker 			goto out;
5169ac3d3e8SAl Viro 		dput(dentry);
5179ac3d3e8SAl Viro 		dentry = alias;
5189ac3d3e8SAl Viro 	}
519d39ab9deSBryan Schumaker 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
520d39ab9deSBryan Schumaker out:
521d39ab9deSBryan Schumaker 	dput(dentry);
522d39ab9deSBryan Schumaker }
523d39ab9deSBryan Schumaker 
524d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
525d1bacf9eSBryan Schumaker static
5268cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5276650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
528d1bacf9eSBryan Schumaker {
529babddc72SBryan Schumaker 	struct xdr_stream stream;
530f7da7a12SBenny Halevy 	struct xdr_buf buf;
5316650239aSTrond Myklebust 	struct page *scratch;
53299424380SBryan Schumaker 	struct nfs_cache_array *array;
5335c346854STrond Myklebust 	unsigned int count = 0;
5345c346854STrond Myklebust 	int status;
535be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
536be4c2d47Sluanshi 
537be4c2d47Sluanshi 	desc->pvec.index = desc->page_index;
538be4c2d47Sluanshi 	desc->pvec.nr = 0;
539babddc72SBryan Schumaker 
5406650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5416650239aSTrond Myklebust 	if (scratch == NULL)
5426650239aSTrond Myklebust 		return -ENOMEM;
543babddc72SBryan Schumaker 
544ce85cfbeSBenjamin Coddington 	if (buflen == 0)
545ce85cfbeSBenjamin Coddington 		goto out_nopages;
546ce85cfbeSBenjamin Coddington 
547f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5486650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
54999424380SBryan Schumaker 
55099424380SBryan Schumaker 	do {
55199424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5528cd51a0cSTrond Myklebust 		if (status != 0) {
5538cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5548cd51a0cSTrond Myklebust 				status = 0;
55599424380SBryan Schumaker 			break;
5568cd51a0cSTrond Myklebust 		}
55799424380SBryan Schumaker 
5585c346854STrond Myklebust 		count++;
5595c346854STrond Myklebust 
560a7a3b1e9SBenjamin Coddington 		if (desc->plus)
561be62a1a8SMiklos Szeredi 			nfs_prime_dcache(file_dentry(desc->file), entry);
5628cd51a0cSTrond Myklebust 
563be4c2d47Sluanshi 		status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]);
564be4c2d47Sluanshi 		if (status == -ENOSPC) {
565be4c2d47Sluanshi 			desc->pvec.nr++;
566be4c2d47Sluanshi 			if (desc->pvec.nr == max_rapages)
567be4c2d47Sluanshi 				break;
568be4c2d47Sluanshi 			status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]);
569be4c2d47Sluanshi 		}
5708cd51a0cSTrond Myklebust 		if (status != 0)
5718cd51a0cSTrond Myklebust 			break;
57299424380SBryan Schumaker 	} while (!entry->eof);
57399424380SBryan Schumaker 
574be4c2d47Sluanshi 	/*
575be4c2d47Sluanshi 	 * page and desc->pvec.pages[0] are valid, don't need to check
576be4c2d47Sluanshi 	 * whether or not to be NULL.
577be4c2d47Sluanshi 	 */
578be4c2d47Sluanshi 	copy_highpage(page, desc->pvec.pages[0]);
579be4c2d47Sluanshi 
580ce85cfbeSBenjamin Coddington out_nopages:
58147c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
582be4c2d47Sluanshi 		array = kmap_atomic(desc->pvec.pages[desc->pvec.nr]);
5838cd51a0cSTrond Myklebust 		array->eof_index = array->size;
58499424380SBryan Schumaker 		status = 0;
585be4c2d47Sluanshi 		kunmap_atomic(array);
58656e4ebf8SBryan Schumaker 	}
5876650239aSTrond Myklebust 
5886650239aSTrond Myklebust 	put_page(scratch);
589be4c2d47Sluanshi 
590be4c2d47Sluanshi 	/*
591be4c2d47Sluanshi 	 * desc->pvec.nr > 0 means at least one page was completely filled,
592be4c2d47Sluanshi 	 * we should return -ENOSPC. Otherwise function
593be4c2d47Sluanshi 	 * nfs_readdir_xdr_to_array will enter infinite loop.
594be4c2d47Sluanshi 	 */
595be4c2d47Sluanshi 	if (desc->pvec.nr > 0)
596be4c2d47Sluanshi 		return -ENOSPC;
5978cd51a0cSTrond Myklebust 	return status;
5988cd51a0cSTrond Myklebust }
59956e4ebf8SBryan Schumaker 
60056e4ebf8SBryan Schumaker static
601c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
60256e4ebf8SBryan Schumaker {
60356e4ebf8SBryan Schumaker 	unsigned int i;
60456e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
60556e4ebf8SBryan Schumaker 		put_page(pages[i]);
60656e4ebf8SBryan Schumaker }
60756e4ebf8SBryan Schumaker 
60856e4ebf8SBryan Schumaker /*
609bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
610bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
61156e4ebf8SBryan Schumaker  */
61256e4ebf8SBryan Schumaker static
613c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
61456e4ebf8SBryan Schumaker {
61556e4ebf8SBryan Schumaker 	unsigned int i;
61656e4ebf8SBryan Schumaker 
61756e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
61856e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
61956e4ebf8SBryan Schumaker 		if (page == NULL)
62056e4ebf8SBryan Schumaker 			goto out_freepages;
62156e4ebf8SBryan Schumaker 		pages[i] = page;
62256e4ebf8SBryan Schumaker 	}
6236650239aSTrond Myklebust 	return 0;
62456e4ebf8SBryan Schumaker 
62556e4ebf8SBryan Schumaker out_freepages:
626c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6276650239aSTrond Myklebust 	return -ENOMEM;
628d1bacf9eSBryan Schumaker }
629d1bacf9eSBryan Schumaker 
630be4c2d47Sluanshi /*
631be4c2d47Sluanshi  * nfs_readdir_rapages_init initialize rapages by nfs_cache_array structure.
632be4c2d47Sluanshi  */
633be4c2d47Sluanshi static
634be4c2d47Sluanshi void nfs_readdir_rapages_init(nfs_readdir_descriptor_t *desc)
635be4c2d47Sluanshi {
636be4c2d47Sluanshi 	struct nfs_cache_array *array;
637be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
638be4c2d47Sluanshi 	int index;
639be4c2d47Sluanshi 
640be4c2d47Sluanshi 	for (index = 0; index < max_rapages; index++) {
641be4c2d47Sluanshi 		array = kmap_atomic(desc->pvec.pages[index]);
642be4c2d47Sluanshi 		memset(array, 0, sizeof(struct nfs_cache_array));
643be4c2d47Sluanshi 		array->eof_index = -1;
644be4c2d47Sluanshi 		kunmap_atomic(array);
645be4c2d47Sluanshi 	}
646be4c2d47Sluanshi }
647be4c2d47Sluanshi 
648d1bacf9eSBryan Schumaker static
649d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
650d1bacf9eSBryan Schumaker {
65156e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
652d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
653d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
654d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6558cd51a0cSTrond Myklebust 	int status = -ENOMEM;
65656e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
657d1bacf9eSBryan Schumaker 
658be4c2d47Sluanshi 	/*
659be4c2d47Sluanshi 	 * This means we hit readdir rdpages miss, the preallocated rdpages
660be4c2d47Sluanshi 	 * are useless, the preallocate rdpages should be reinitialized.
661be4c2d47Sluanshi 	 */
662be4c2d47Sluanshi 	nfs_readdir_rapages_init(desc);
663be4c2d47Sluanshi 
664d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6650aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
666d1bacf9eSBryan Schumaker 	entry.eof = 0;
667d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
668d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
669573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
670d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
671d1bacf9eSBryan Schumaker 		goto out;
672d1bacf9eSBryan Schumaker 
67314c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
67414c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
67514c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
67614c43f76SDavid Quigley 		goto out;
67714c43f76SDavid Quigley 	}
67814c43f76SDavid Quigley 
6790795bf83SFabian Frederick 	array = kmap(page);
680d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
681d1bacf9eSBryan Schumaker 	array->eof_index = -1;
682d1bacf9eSBryan Schumaker 
683c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
6846650239aSTrond Myklebust 	if (status < 0)
685d1bacf9eSBryan Schumaker 		goto out_release_array;
686d1bacf9eSBryan Schumaker 	do {
687ac396128STrond Myklebust 		unsigned int pglen;
68856e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
689babddc72SBryan Schumaker 
690d1bacf9eSBryan Schumaker 		if (status < 0)
691d1bacf9eSBryan Schumaker 			break;
692ac396128STrond Myklebust 		pglen = status;
6936650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6948cd51a0cSTrond Myklebust 		if (status < 0) {
6958cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6968cd51a0cSTrond Myklebust 				status = 0;
6978cd51a0cSTrond Myklebust 			break;
6988cd51a0cSTrond Myklebust 		}
6998cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
700d1bacf9eSBryan Schumaker 
701c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
702d1bacf9eSBryan Schumaker out_release_array:
7030795bf83SFabian Frederick 	kunmap(page);
70414c43f76SDavid Quigley 	nfs4_label_free(entry.label);
705d1bacf9eSBryan Schumaker out:
706d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
707d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
708d1bacf9eSBryan Schumaker 	return status;
709d1bacf9eSBryan Schumaker }
710d1bacf9eSBryan Schumaker 
711d1bacf9eSBryan Schumaker /*
712d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
713d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
714d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
715d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
7161da177e4SLinus Torvalds  */
717d1bacf9eSBryan Schumaker static
718a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page)
719d1bacf9eSBryan Schumaker {
720a46126ccSChristoph Hellwig 	nfs_readdir_descriptor_t *desc = data;
721496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
7228cd51a0cSTrond Myklebust 	int ret;
723d1bacf9eSBryan Schumaker 
724be4c2d47Sluanshi 	/*
725be4c2d47Sluanshi 	 * If desc->page_index in range desc->pvec.index and
726be4c2d47Sluanshi 	 * desc->pvec.index + desc->pvec.nr, we get readdir cache hit.
727be4c2d47Sluanshi 	 */
728be4c2d47Sluanshi 	if (desc->page_index >= desc->pvec.index &&
729be4c2d47Sluanshi 		desc->page_index < (desc->pvec.index + desc->pvec.nr)) {
730be4c2d47Sluanshi 		/*
731be4c2d47Sluanshi 		 * page and desc->pvec.pages[x] are valid, don't need to check
732be4c2d47Sluanshi 		 * whether or not to be NULL.
733be4c2d47Sluanshi 		 */
734be4c2d47Sluanshi 		copy_highpage(page, desc->pvec.pages[desc->page_index - desc->pvec.index]);
735be4c2d47Sluanshi 		ret = 0;
736be4c2d47Sluanshi 	} else {
7378cd51a0cSTrond Myklebust 		ret = nfs_readdir_xdr_to_array(desc, page, inode);
7388cd51a0cSTrond Myklebust 		if (ret < 0)
739d1bacf9eSBryan Schumaker 			goto error;
740be4c2d47Sluanshi 	}
741be4c2d47Sluanshi 
742d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
743d1bacf9eSBryan Schumaker 
7442aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
745cd9ae2b6STrond Myklebust 		/* Should never happen */
746cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
747cd9ae2b6STrond Myklebust 	}
7481da177e4SLinus Torvalds 	unlock_page(page);
7491da177e4SLinus Torvalds 	return 0;
7501da177e4SLinus Torvalds  error:
7511da177e4SLinus Torvalds 	unlock_page(page);
7528cd51a0cSTrond Myklebust 	return ret;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
755d1bacf9eSBryan Schumaker static
756d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7571da177e4SLinus Torvalds {
758b044f645SBenjamin Coddington 	if (!desc->page->mapping)
75911de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
76009cbfeafSKirill A. Shutemov 	put_page(desc->page);
7611da177e4SLinus Torvalds 	desc->page = NULL;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
764d1bacf9eSBryan Schumaker static
765d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7661da177e4SLinus Torvalds {
767a46126ccSChristoph Hellwig 	return read_cache_page(desc->file->f_mapping, desc->page_index,
768a46126ccSChristoph Hellwig 			nfs_readdir_filler, desc);
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds /*
772d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7731da177e4SLinus Torvalds  */
774d1bacf9eSBryan Schumaker static
775d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
776d1bacf9eSBryan Schumaker {
777d1bacf9eSBryan Schumaker 	int res;
778d1bacf9eSBryan Schumaker 
779d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
780d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
781d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
782d1bacf9eSBryan Schumaker 
783d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
78447c716cbSTrond Myklebust 	if (res != 0)
785d1bacf9eSBryan Schumaker 		cache_page_release(desc);
786d1bacf9eSBryan Schumaker 	return res;
787d1bacf9eSBryan Schumaker }
788d1bacf9eSBryan Schumaker 
789d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7901da177e4SLinus Torvalds static inline
7911da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7921da177e4SLinus Torvalds {
7938cd51a0cSTrond Myklebust 	int res;
794d1bacf9eSBryan Schumaker 
7950aded708STrond Myklebust 	if (desc->page_index == 0) {
7968cd51a0cSTrond Myklebust 		desc->current_index = 0;
7970aded708STrond Myklebust 		desc->last_cookie = 0;
7980aded708STrond Myklebust 	}
79947c716cbSTrond Myklebust 	do {
800d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
80147c716cbSTrond Myklebust 	} while (res == -EAGAIN);
8021da177e4SLinus Torvalds 	return res;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds /*
8061da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
8071da177e4SLinus Torvalds  */
8081da177e4SLinus Torvalds static
80923db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	struct file	*file = desc->file;
812d1bacf9eSBryan Schumaker 	int i = 0;
813d1bacf9eSBryan Schumaker 	int res = 0;
814d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8158ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
8168ef2ce3eSBryan Schumaker 
8170795bf83SFabian Frederick 	array = kmap(desc->page);
818d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
819ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8201da177e4SLinus Torvalds 
821ece0b423STrond Myklebust 		ent = &array->array[i];
82223db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
82323db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
8246089dd0dSThomas Meyer 			desc->eof = true;
8251da177e4SLinus Torvalds 			break;
826ece0b423STrond Myklebust 		}
82723db8620SAl Viro 		desc->ctx->pos++;
828d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
829d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
830d1bacf9eSBryan Schumaker 		else
831d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
8320c030806STrond Myklebust 		if (ctx->duped != 0)
8330c030806STrond Myklebust 			ctx->duped = 1;
8348cd51a0cSTrond Myklebust 	}
83547c716cbSTrond Myklebust 	if (array->eof_index >= 0)
8366089dd0dSThomas Meyer 		desc->eof = true;
837d1bacf9eSBryan Schumaker 
8380795bf83SFabian Frederick 	kunmap(desc->page);
839d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8401e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8411e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
8421da177e4SLinus Torvalds 	return res;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds /*
8461da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8471da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8481da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8491da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8501da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8511da177e4SLinus Torvalds  *
8521da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8531da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8541da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8551da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8561da177e4SLinus Torvalds  */
8571da177e4SLinus Torvalds static inline
85823db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	struct page	*page = NULL;
8611da177e4SLinus Torvalds 	int		status;
862496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8630c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8641da177e4SLinus Torvalds 
8651e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8661e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8691da177e4SLinus Torvalds 	if (!page) {
8701da177e4SLinus Torvalds 		status = -ENOMEM;
8711da177e4SLinus Torvalds 		goto out;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 
8747a8e1dc3STrond Myklebust 	desc->page_index = 0;
8750aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8767a8e1dc3STrond Myklebust 	desc->page = page;
8770c030806STrond Myklebust 	ctx->duped = 0;
8787a8e1dc3STrond Myklebust 
87985f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
88085f8607eSTrond Myklebust 	if (status < 0)
881d1bacf9eSBryan Schumaker 		goto out_release;
882d1bacf9eSBryan Schumaker 
88323db8620SAl Viro 	status = nfs_do_filldir(desc);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds  out:
8861e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8873110ff80SHarvey Harrison 			__func__, status);
8881da177e4SLinus Torvalds 	return status;
8891da177e4SLinus Torvalds  out_release:
890d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8911da177e4SLinus Torvalds 	goto out;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
89400a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
89500a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
89600a92642SOlivier Galibert    whole directory.
8971da177e4SLinus Torvalds  */
89823db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8991da177e4SLinus Torvalds {
900be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
9012b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
9021da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
9031da177e4SLinus Torvalds 			*desc = &my_desc;
90423db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
90507b5ce8eSScott Mayhew 	int res = 0;
906be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
9071da177e4SLinus Torvalds 
9086de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9096de1472fSAl Viro 			file, (long long)ctx->pos);
91091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
91191d5b470SChuck Lever 
9121da177e4SLinus Torvalds 	/*
91323db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
914f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
91500a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
91600a92642SOlivier Galibert 	 * revalidate the cookie.
9171da177e4SLinus Torvalds 	 */
9181da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
9191da177e4SLinus Torvalds 
92023db8620SAl Viro 	desc->file = file;
92123db8620SAl Viro 	desc->ctx = ctx;
922480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
9231da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
924a7a3b1e9SBenjamin Coddington 	desc->plus = nfs_use_readdirplus(inode, ctx);
9251da177e4SLinus Torvalds 
926be4c2d47Sluanshi 	res = nfs_readdir_alloc_pages(desc->pvec.pages, max_rapages);
927be4c2d47Sluanshi 	if (res < 0)
928be4c2d47Sluanshi 		return -ENOMEM;
929be4c2d47Sluanshi 
930be4c2d47Sluanshi 	nfs_readdir_rapages_init(desc);
931be4c2d47Sluanshi 
93279f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
93323db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
934fccca7fcSTrond Myklebust 	if (res < 0)
935fccca7fcSTrond Myklebust 		goto out;
936fccca7fcSTrond Myklebust 
93747c716cbSTrond Myklebust 	do {
9381da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
93900a92642SOlivier Galibert 
9401da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
941ece0b423STrond Myklebust 			res = 0;
9421da177e4SLinus Torvalds 			/* This means either end of directory */
9436089dd0dSThomas Meyer 			if (*desc->dir_cookie && !desc->eof) {
9441da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
94523db8620SAl Viro 				res = uncached_readdir(desc);
946ece0b423STrond Myklebust 				if (res == 0)
9471da177e4SLinus Torvalds 					continue;
9481da177e4SLinus Torvalds 			}
9491da177e4SLinus Torvalds 			break;
9501da177e4SLinus Torvalds 		}
9511da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9523a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9531da177e4SLinus Torvalds 			nfs_zap_caches(inode);
954baf57a09STrond Myklebust 			desc->page_index = 0;
955a7a3b1e9SBenjamin Coddington 			desc->plus = false;
956a7a3b1e9SBenjamin Coddington 			desc->eof = false;
9571da177e4SLinus Torvalds 			continue;
9581da177e4SLinus Torvalds 		}
9591da177e4SLinus Torvalds 		if (res < 0)
9601da177e4SLinus Torvalds 			break;
9611da177e4SLinus Torvalds 
96223db8620SAl Viro 		res = nfs_do_filldir(desc);
963ece0b423STrond Myklebust 		if (res < 0)
9641da177e4SLinus Torvalds 			break;
96547c716cbSTrond Myklebust 	} while (!desc->eof);
966fccca7fcSTrond Myklebust out:
967be4c2d47Sluanshi 	nfs_readdir_free_pages(desc->pvec.pages, max_rapages);
9681e7cb3dcSChuck Lever 	if (res > 0)
9691e7cb3dcSChuck Lever 		res = 0;
9706de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9711da177e4SLinus Torvalds 	return res;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
974965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
975f0dd2136STrond Myklebust {
976b044f645SBenjamin Coddington 	struct inode *inode = file_inode(filp);
977480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
978b84e06c5SChuck Lever 
9796de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9806de1472fSAl Viro 			filp, offset, whence);
981b84e06c5SChuck Lever 
982965c8e59SAndrew Morton 	switch (whence) {
983f0dd2136STrond Myklebust 	default:
984b2b1ff3dSTrond Myklebust 		return -EINVAL;
985b2b1ff3dSTrond Myklebust 	case SEEK_SET:
986b2b1ff3dSTrond Myklebust 		if (offset < 0)
987b2b1ff3dSTrond Myklebust 			return -EINVAL;
988b2b1ff3dSTrond Myklebust 		inode_lock(inode);
989b2b1ff3dSTrond Myklebust 		break;
990b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
991b2b1ff3dSTrond Myklebust 		if (offset == 0)
992b2b1ff3dSTrond Myklebust 			return filp->f_pos;
993b2b1ff3dSTrond Myklebust 		inode_lock(inode);
994b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
995b2b1ff3dSTrond Myklebust 		if (offset < 0) {
996b2b1ff3dSTrond Myklebust 			inode_unlock(inode);
997b2b1ff3dSTrond Myklebust 			return -EINVAL;
998b2b1ff3dSTrond Myklebust 		}
999f0dd2136STrond Myklebust 	}
1000f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1001f0dd2136STrond Myklebust 		filp->f_pos = offset;
1002480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
10038ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1004f0dd2136STrond Myklebust 	}
1005b044f645SBenjamin Coddington 	inode_unlock(inode);
1006f0dd2136STrond Myklebust 	return offset;
1007f0dd2136STrond Myklebust }
1008f0dd2136STrond Myklebust 
10091da177e4SLinus Torvalds /*
10101da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
10111da177e4SLinus Torvalds  * is a dummy operation.
10121da177e4SLinus Torvalds  */
101302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
101402c24a82SJosef Bacik 			 int datasync)
10151da177e4SLinus Torvalds {
10166de1472fSAl Viro 	struct inode *inode = file_inode(filp);
10177ea80859SChristoph Hellwig 
10186de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10191e7cb3dcSChuck Lever 
10205955102cSAl Viro 	inode_lock(inode);
10216de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
10225955102cSAl Viro 	inode_unlock(inode);
10231da177e4SLinus Torvalds 	return 0;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
1026bfc69a45STrond Myklebust /**
1027bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1028302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1029bfc69a45STrond Myklebust  *
1030bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1031bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1032bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1033bfc69a45STrond Myklebust  *
1034bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1035bfc69a45STrond Myklebust  */
1036bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1037bfc69a45STrond Myklebust {
1038011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
1039bfc69a45STrond Myklebust }
104089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1041bfc69a45STrond Myklebust 
10421da177e4SLinus Torvalds /*
10431da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
10441da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
10451da177e4SLinus Torvalds  * and may need to be looked up again.
1046912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
10471da177e4SLinus Torvalds  */
1048912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1049912a108dSNeilBrown 			      int rcu_walk)
10501da177e4SLinus Torvalds {
10511da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
10521da177e4SLinus Torvalds 		return 1;
10534eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
10544eec952eSTrond Myklebust 		return 0;
1055f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10566ecc5e8fSTrond Myklebust 		return 0;
1057f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
10581cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1059912a108dSNeilBrown 		if (rcu_walk)
1060f2c77f4eSTrond Myklebust 			return 0;
10611cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
10621cd9cb05STrond Myklebust 			return 0;
10631cd9cb05STrond Myklebust 	}
1064f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1065f2c77f4eSTrond Myklebust 		return 0;
1066f2c77f4eSTrond Myklebust 	return 1;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds /*
1070a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1071a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1072a12802caSTrond Myklebust  */
1073fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1074a12802caSTrond Myklebust {
1075a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1076a12802caSTrond Myklebust 		return 0;
1077fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1078a12802caSTrond Myklebust }
1079a12802caSTrond Myklebust 
1080a12802caSTrond Myklebust /*
10811d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10821d6757fbSTrond Myklebust  *
10831d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10841d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10851d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10861d6757fbSTrond Myklebust  *
10871d6757fbSTrond Myklebust  */
108865a0c149STrond Myklebust static
1089fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10901da177e4SLinus Torvalds {
10911da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
109265a0c149STrond Myklebust 	int ret;
10931da177e4SLinus Torvalds 
109436d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10954e99a1ffSTrond Myklebust 		return 0;
109647921921STrond Myklebust 
109747921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
109847921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
109947921921STrond Myklebust 		case S_IFREG:
110047921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
110147921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
110247921921STrond Myklebust 				goto out;
110347921921STrond Myklebust 			/* Fallthrough */
110447921921STrond Myklebust 		case S_IFDIR:
110547921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
110647921921STrond Myklebust 				break;
110747921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
110847921921STrond Myklebust 			goto out_force;
110947921921STrond Myklebust 		}
111047921921STrond Myklebust 	}
111147921921STrond Myklebust 
11121da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1113fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
11141da177e4SLinus Torvalds 		goto out_force;
111565a0c149STrond Myklebust out:
1116a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
11171da177e4SLinus Torvalds out_force:
11181fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
11191fa1e384SNeilBrown 		return -ECHILD;
112065a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
112165a0c149STrond Myklebust 	if (ret != 0)
112265a0c149STrond Myklebust 		return ret;
112365a0c149STrond Myklebust 	goto out;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds /*
11271da177e4SLinus Torvalds  * We judge how long we want to trust negative
11281da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
11291da177e4SLinus Torvalds  *
11301da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
11311da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1132912a108dSNeilBrown  *
1133912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1134912a108dSNeilBrown  * suggesting a reval is needed.
11359f6d44d4STrond Myklebust  *
11369f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
11379f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
11381da177e4SLinus Torvalds  */
11391da177e4SLinus Torvalds static inline
11401da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1141fa3c56bbSAl Viro 		       unsigned int flags)
11421da177e4SLinus Torvalds {
11439f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
11441da177e4SLinus Torvalds 		return 0;
11454eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
11464eec952eSTrond Myklebust 		return 1;
1147912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11505ceb9d7fSTrond Myklebust static int
11515ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
11525ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
11531da177e4SLinus Torvalds {
11545ceb9d7fSTrond Myklebust 	switch (error) {
11555ceb9d7fSTrond Myklebust 	case 1:
11566de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11576de1472fSAl Viro 			__func__, dentry);
11581da177e4SLinus Torvalds 		return 1;
11595ceb9d7fSTrond Myklebust 	case 0:
1160a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
11611da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
11621da177e4SLinus Torvalds 			/* Purge readdir caches. */
11631da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1164a3f432bfSJ. Bruce Fields 			/*
1165a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1166a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1167a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1168a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1169a3f432bfSJ. Bruce Fields 			 */
1170a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
11715ceb9d7fSTrond Myklebust 				return 1;
11721da177e4SLinus Torvalds 		}
11736de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11746de1472fSAl Viro 				__func__, dentry);
11751da177e4SLinus Torvalds 		return 0;
11765ceb9d7fSTrond Myklebust 	}
11776de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
11786de1472fSAl Viro 				__func__, dentry, error);
1179e1fb4d05STrond Myklebust 	return error;
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds 
11825ceb9d7fSTrond Myklebust static int
11835ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
11845ceb9d7fSTrond Myklebust 			       unsigned int flags)
11855ceb9d7fSTrond Myklebust {
11865ceb9d7fSTrond Myklebust 	int ret = 1;
11875ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
11885ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
11895ceb9d7fSTrond Myklebust 			return -ECHILD;
11905ceb9d7fSTrond Myklebust 		ret = 0;
11915ceb9d7fSTrond Myklebust 	}
11925ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
11935ceb9d7fSTrond Myklebust }
11945ceb9d7fSTrond Myklebust 
11955ceb9d7fSTrond Myklebust static int
11965ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
11975ceb9d7fSTrond Myklebust 				struct inode *inode)
11985ceb9d7fSTrond Myklebust {
11995ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12005ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
12015ceb9d7fSTrond Myklebust }
12025ceb9d7fSTrond Myklebust 
12035ceb9d7fSTrond Myklebust static int
12045ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
12055ceb9d7fSTrond Myklebust 			     struct inode *inode)
12065ceb9d7fSTrond Myklebust {
12075ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
12085ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
12095ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
12105ceb9d7fSTrond Myklebust 	int ret;
12115ceb9d7fSTrond Myklebust 
12125ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
12135ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
12145ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
12155ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
12165ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
12175ceb9d7fSTrond Myklebust 		goto out;
12185ceb9d7fSTrond Myklebust 
12195ceb9d7fSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
12205ceb9d7fSTrond Myklebust 	if (ret < 0) {
12215ceb9d7fSTrond Myklebust 		if (ret == -ESTALE || ret == -ENOENT)
12225ceb9d7fSTrond Myklebust 			ret = 0;
12235ceb9d7fSTrond Myklebust 		goto out;
12245ceb9d7fSTrond Myklebust 	}
12255ceb9d7fSTrond Myklebust 	ret = 0;
12265ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
12275ceb9d7fSTrond Myklebust 		goto out;
12285ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
12295ceb9d7fSTrond Myklebust 		goto out;
12305ceb9d7fSTrond Myklebust 
12315ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
12325ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12335ceb9d7fSTrond Myklebust 
12345ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
12355ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
12365ceb9d7fSTrond Myklebust 	ret = 1;
12375ceb9d7fSTrond Myklebust out:
12385ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
12395ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
12405ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
12415ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
12425ceb9d7fSTrond Myklebust }
12435ceb9d7fSTrond Myklebust 
12445ceb9d7fSTrond Myklebust /*
12455ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
12465ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
12475ceb9d7fSTrond Myklebust  * lookup.
12485ceb9d7fSTrond Myklebust  *
12495ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
12505ceb9d7fSTrond Myklebust  * we have an inode!
12515ceb9d7fSTrond Myklebust  *
12525ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
12535ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
12545ceb9d7fSTrond Myklebust  */
12555ceb9d7fSTrond Myklebust static int
12565ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
12575ceb9d7fSTrond Myklebust 			 unsigned int flags)
12585ceb9d7fSTrond Myklebust {
12595ceb9d7fSTrond Myklebust 	struct inode *inode;
12605ceb9d7fSTrond Myklebust 	int error;
12615ceb9d7fSTrond Myklebust 
12625ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
12635ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
12645ceb9d7fSTrond Myklebust 
12655ceb9d7fSTrond Myklebust 	if (!inode)
12665ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
12675ceb9d7fSTrond Myklebust 
12685ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
12695ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12705ceb9d7fSTrond Myklebust 				__func__, dentry);
12715ceb9d7fSTrond Myklebust 		goto out_bad;
12725ceb9d7fSTrond Myklebust 	}
12735ceb9d7fSTrond Myklebust 
12745ceb9d7fSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
12755ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
12765ceb9d7fSTrond Myklebust 
12775ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
12785ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
12795ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
12805ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
12815ceb9d7fSTrond Myklebust 		if (error) {
12825ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
12835ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
12845ceb9d7fSTrond Myklebust 			goto out_bad;
12855ceb9d7fSTrond Myklebust 		}
12865ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
12875ceb9d7fSTrond Myklebust 		goto out_valid;
12885ceb9d7fSTrond Myklebust 	}
12895ceb9d7fSTrond Myklebust 
12905ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
12915ceb9d7fSTrond Myklebust 		return -ECHILD;
12925ceb9d7fSTrond Myklebust 
12935ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
12945ceb9d7fSTrond Myklebust 		goto out_bad;
12955ceb9d7fSTrond Myklebust 
12965ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
12975ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
12985ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
12995ceb9d7fSTrond Myklebust 	return error;
13005ceb9d7fSTrond Myklebust out_valid:
13015ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
13025ceb9d7fSTrond Myklebust out_bad:
13035ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
13045ceb9d7fSTrond Myklebust 		return -ECHILD;
13055ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
13065ceb9d7fSTrond Myklebust }
13075ceb9d7fSTrond Myklebust 
13085ceb9d7fSTrond Myklebust static int
1309c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1310c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
13115ceb9d7fSTrond Myklebust {
13125ceb9d7fSTrond Myklebust 	struct dentry *parent;
13135ceb9d7fSTrond Myklebust 	struct inode *dir;
13145ceb9d7fSTrond Myklebust 	int ret;
13155ceb9d7fSTrond Myklebust 
13165ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
13175ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
13185ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
13195ceb9d7fSTrond Myklebust 		if (!dir)
13205ceb9d7fSTrond Myklebust 			return -ECHILD;
1321c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
13225ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
13235ceb9d7fSTrond Myklebust 			return -ECHILD;
13245ceb9d7fSTrond Myklebust 	} else {
13255ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1326c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
13275ceb9d7fSTrond Myklebust 		dput(parent);
13285ceb9d7fSTrond Myklebust 	}
13295ceb9d7fSTrond Myklebust 	return ret;
13305ceb9d7fSTrond Myklebust }
13315ceb9d7fSTrond Myklebust 
1332c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1333c7944ebbSTrond Myklebust {
1334c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1335c7944ebbSTrond Myklebust }
1336c7944ebbSTrond Myklebust 
13371da177e4SLinus Torvalds /*
13382b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1339ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1340ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1341ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1342ecf3d1f1SJeff Layton  *
1343ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1344ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1345ecf3d1f1SJeff Layton  */
1346ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1347ecf3d1f1SJeff Layton {
13482b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
13499cdd1d3fSTrond Myklebust 	int error = 0;
1350ecf3d1f1SJeff Layton 
1351ecf3d1f1SJeff Layton 	/*
1352ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1353ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1354ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1355ecf3d1f1SJeff Layton 	 */
1356ecf3d1f1SJeff Layton 	if (!inode) {
13576de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
13586de1472fSAl Viro 				__func__, dentry);
1359ecf3d1f1SJeff Layton 		return 1;
1360ecf3d1f1SJeff Layton 	}
1361ecf3d1f1SJeff Layton 
1362ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
13636de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
13646de1472fSAl Viro 				__func__, dentry);
1365ecf3d1f1SJeff Layton 		return 0;
1366ecf3d1f1SJeff Layton 	}
1367ecf3d1f1SJeff Layton 
1368b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1369ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1370ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1371ecf3d1f1SJeff Layton 	return !error;
1372ecf3d1f1SJeff Layton }
1373ecf3d1f1SJeff Layton 
1374ecf3d1f1SJeff Layton /*
13751da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
13761da177e4SLinus Torvalds  */
1377fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
13781da177e4SLinus Torvalds {
13796de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
13806de1472fSAl Viro 		dentry, dentry->d_flags);
13811da177e4SLinus Torvalds 
138277f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
13832b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
138477f11192STrond Myklebust 		return 1;
138577f11192STrond Myklebust 
13861da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
13871da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
13881da177e4SLinus Torvalds 		return 1;
13891da177e4SLinus Torvalds 	}
13901751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
13911da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
13921da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
13931da177e4SLinus Torvalds 		return 1;
13941da177e4SLinus Torvalds 	}
13951da177e4SLinus Torvalds 	return 0;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
13991f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
14001b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
14011b83d707STrond Myklebust {
14021b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
14031f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
140459a707b0STrond Myklebust 	if (inode->i_nlink > 0)
140559a707b0STrond Myklebust 		drop_nlink(inode);
140659a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
140716e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
140816e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
140959a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
141059a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
14111b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
14121b83d707STrond Myklebust }
14131b83d707STrond Myklebust 
14141da177e4SLinus Torvalds /*
14151da177e4SLinus Torvalds  * Called when the dentry loses inode.
14161da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
14171da177e4SLinus Torvalds  */
14181da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
14191da177e4SLinus Torvalds {
142083672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
142183672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
142283672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
142383672d39SNeil Brown 
14241da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1425e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
14261f018458STrond Myklebust 		nfs_drop_nlink(inode);
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 	iput(inode);
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
1431b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1432b1942c5fSAl Viro {
1433b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1434b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1435b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1436b1942c5fSAl Viro 			WARN_ON(1);
1437b1942c5fSAl Viro 		else
1438b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1439b1942c5fSAl Viro 	}
1440b1942c5fSAl Viro }
1441b1942c5fSAl Viro 
1442f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
14431da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1444ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
14451da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
14461da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
144736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1448b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14491da177e4SLinus Torvalds };
1450ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
14511da177e4SLinus Torvalds 
1452597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
14531da177e4SLinus Torvalds {
14541da177e4SLinus Torvalds 	struct dentry *res;
14551da177e4SLinus Torvalds 	struct inode *inode = NULL;
1456e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1457e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
14581775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
14591da177e4SLinus Torvalds 	int error;
14601da177e4SLinus Torvalds 
14616de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
146291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
14631da177e4SLinus Torvalds 
1464130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1465130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
14661da177e4SLinus Torvalds 
1467fd684071STrond Myklebust 	/*
1468fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1469fd684071STrond Myklebust 	 * but don't hash the dentry.
1470fd684071STrond Myklebust 	 */
14719f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1472130f9ab7SAl Viro 		return NULL;
14731da177e4SLinus Torvalds 
1474e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1475e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1476e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1477e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1478e1fb4d05STrond Myklebust 		goto out;
1479e1fb4d05STrond Myklebust 
148014c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
148114c43f76SDavid Quigley 	if (IS_ERR(label))
148214c43f76SDavid Quigley 		goto out;
148314c43f76SDavid Quigley 
14846e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
14851775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
14861da177e4SLinus Torvalds 	if (error == -ENOENT)
14871da177e4SLinus Torvalds 		goto no_entry;
14881da177e4SLinus Torvalds 	if (error < 0) {
14891da177e4SLinus Torvalds 		res = ERR_PTR(error);
1490bf130914SAl Viro 		goto out_label;
14911da177e4SLinus Torvalds 	}
14921775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1493bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
149403f28e3aSTrond Myklebust 	if (IS_ERR(res))
1495bf130914SAl Viro 		goto out_label;
149654ceac45SDavid Howells 
149763519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
149863519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1499d69ee9b8STrond Myklebust 
15001da177e4SLinus Torvalds no_entry:
150141d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
15029eaef27bSTrond Myklebust 	if (res != NULL) {
15039eaef27bSTrond Myklebust 		if (IS_ERR(res))
1504bf130914SAl Viro 			goto out_label;
15051da177e4SLinus Torvalds 		dentry = res;
15069eaef27bSTrond Myklebust 	}
15071da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1508bf130914SAl Viro out_label:
15096e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
151014c43f76SDavid Quigley 	nfs4_label_free(label);
15111da177e4SLinus Torvalds out:
1512e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1513e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
15141da177e4SLinus Torvalds 	return res;
15151da177e4SLinus Torvalds }
1516ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
15171da177e4SLinus Torvalds 
151889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
15190b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
15201da177e4SLinus Torvalds 
1521f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
15220ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1523b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
15241da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
15251da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
152636d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1527b1942c5fSAl Viro 	.d_release	= nfs_d_release,
15281da177e4SLinus Torvalds };
152989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
15301da177e4SLinus Torvalds 
15318a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
15328a5e929dSAl Viro {
15338a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
15348a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
15358a5e929dSAl Viro 		res |= FMODE_READ;
15368a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
15378a5e929dSAl Viro 		res |= FMODE_WRITE;
15388a5e929dSAl Viro 	return res;
15398a5e929dSAl Viro }
15408a5e929dSAl Viro 
1541532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1542cd9a1c0eSTrond Myklebust {
1543532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1544cd9a1c0eSTrond Myklebust }
1545cd9a1c0eSTrond Myklebust 
1546cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1547cd9a1c0eSTrond Myklebust {
1548f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1549cd9a1c0eSTrond Myklebust 	return 0;
1550cd9a1c0eSTrond Myklebust }
1551cd9a1c0eSTrond Myklebust 
1552d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
15530dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1554b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1555cd9a1c0eSTrond Myklebust {
15560dd2b474SMiklos Szeredi 	int err;
15570dd2b474SMiklos Szeredi 
1558be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
155930d90494SAl Viro 	if (err)
1560d9585277SAl Viro 		goto out;
1561eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
156230d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1563eaa2b82cSNeilBrown 	else
1564eaa2b82cSNeilBrown 		err = -ESTALE;
1565cd9a1c0eSTrond Myklebust out:
1566d9585277SAl Viro 	return err;
1567cd9a1c0eSTrond Myklebust }
1568cd9a1c0eSTrond Myklebust 
156973a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
157030d90494SAl Viro 		    struct file *file, unsigned open_flags,
157144907d79SAl Viro 		    umode_t mode)
15721da177e4SLinus Torvalds {
1573c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1574cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
15750dd2b474SMiklos Szeredi 	struct dentry *res;
15760dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1577f46e0bd3STrond Myklebust 	struct inode *inode;
15781472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1579c94c0953SAl Viro 	bool switched = false;
158073a09dd9SAl Viro 	int created = 0;
1581898f635cSTrond Myklebust 	int err;
15821da177e4SLinus Torvalds 
15830dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
15842b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
15850dd2b474SMiklos Szeredi 
15861e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
15876de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
15881e7cb3dcSChuck Lever 
15899597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
15909597c13bSJeff Layton 	if (err)
15919597c13bSJeff Layton 		return err;
15929597c13bSJeff Layton 
15930dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
15940dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
159500699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
15960dd2b474SMiklos Szeredi 			/*
15970dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
15980dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
15990dd2b474SMiklos Szeredi 			 * again
16000dd2b474SMiklos Szeredi 			 */
1601d9585277SAl Viro 			return -ENOENT;
16020dd2b474SMiklos Szeredi 		}
16031472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
16041da177e4SLinus Torvalds 		goto no_open;
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 
16070dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1608d9585277SAl Viro 		return -ENAMETOOLONG;
16091da177e4SLinus Torvalds 
16100dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1611dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1612dff25ddbSAndreas Gruenbacher 
1613dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1614dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1615dff25ddbSAndreas Gruenbacher 
1616536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1617dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
16180dd2b474SMiklos Szeredi 	}
1619536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1620536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1621536e43d1STrond Myklebust 		attr.ia_size = 0;
1622cd9a1c0eSTrond Myklebust 	}
1623cd9a1c0eSTrond Myklebust 
1624c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1625c94c0953SAl Viro 		d_drop(dentry);
1626c94c0953SAl Viro 		switched = true;
1627c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1628c94c0953SAl Viro 					  &dentry->d_name, &wq);
1629c94c0953SAl Viro 		if (IS_ERR(dentry))
1630c94c0953SAl Viro 			return PTR_ERR(dentry);
1631c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1632c94c0953SAl Viro 			return finish_no_open(file, dentry);
1633c94c0953SAl Viro 	}
1634c94c0953SAl Viro 
1635532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
16360dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
16370dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1638d9585277SAl Viro 		goto out;
16390dd2b474SMiklos Szeredi 
16406e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
164173a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
164273a09dd9SAl Viro 	if (created)
164373a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1644275bb307STrond Myklebust 	if (IS_ERR(inode)) {
16450dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
16466e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16472d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1648d20cb71dSAl Viro 		d_drop(dentry);
16490dd2b474SMiklos Szeredi 		switch (err) {
16501da177e4SLinus Torvalds 		case -ENOENT:
1651774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1652809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16530dd2b474SMiklos Szeredi 			break;
16541788ea6eSJeff Layton 		case -EISDIR:
16556f926b5bSTrond Myklebust 		case -ENOTDIR:
16566f926b5bSTrond Myklebust 			goto no_open;
16571da177e4SLinus Torvalds 		case -ELOOP:
16580dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
16591da177e4SLinus Torvalds 				goto no_open;
16600dd2b474SMiklos Szeredi 			break;
16611da177e4SLinus Torvalds 			/* case -EINVAL: */
16621da177e4SLinus Torvalds 		default:
16630dd2b474SMiklos Szeredi 			break;
16641da177e4SLinus Torvalds 		}
16651da177e4SLinus Torvalds 		goto out;
16661da177e4SLinus Torvalds 	}
16670dd2b474SMiklos Szeredi 
1668b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
16696e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16702d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1671d9585277SAl Viro out:
1672c94c0953SAl Viro 	if (unlikely(switched)) {
1673c94c0953SAl Viro 		d_lookup_done(dentry);
1674c94c0953SAl Viro 		dput(dentry);
1675c94c0953SAl Viro 	}
1676d9585277SAl Viro 	return err;
16770dd2b474SMiklos Szeredi 
16781da177e4SLinus Torvalds no_open:
16791472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1680c94c0953SAl Viro 	if (switched) {
1681c94c0953SAl Viro 		d_lookup_done(dentry);
1682c94c0953SAl Viro 		if (!res)
1683c94c0953SAl Viro 			res = dentry;
1684c94c0953SAl Viro 		else
1685c94c0953SAl Viro 			dput(dentry);
1686c94c0953SAl Viro 	}
16870dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1688c94c0953SAl Viro 		return PTR_ERR(res);
1689e45198a6SAl Viro 	return finish_no_open(file, res);
16901da177e4SLinus Torvalds }
169189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
16921da177e4SLinus Torvalds 
1693c7944ebbSTrond Myklebust static int
1694c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1695c7944ebbSTrond Myklebust 			  unsigned int flags)
16961da177e4SLinus Torvalds {
1697657e94b6SNick Piggin 	struct inode *inode;
16981da177e4SLinus Torvalds 
1699fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1700c7944ebbSTrond Myklebust 		goto full_reval;
1701eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1702c7944ebbSTrond Myklebust 		goto full_reval;
17032b484297STrond Myklebust 
17042b0143b5SDavid Howells 	inode = d_inode(dentry);
17052b484297STrond Myklebust 
17061da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
17071da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
17081da177e4SLinus Torvalds 	 */
1709c7944ebbSTrond Myklebust 	if (inode == NULL)
1710c7944ebbSTrond Myklebust 		goto full_reval;
171149317a7fSNeilBrown 
1712c7944ebbSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1713c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1714216d5d06STrond Myklebust 
17151da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
17161da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1717c7944ebbSTrond Myklebust 		goto full_reval;
1718c7944ebbSTrond Myklebust 
17191da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1720c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1721c7944ebbSTrond Myklebust 		goto reval_dentry;
1722c7944ebbSTrond Myklebust 
1723c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1724c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1725c7944ebbSTrond Myklebust 		goto reval_dentry;
17261da177e4SLinus Torvalds 
17270ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1728c7944ebbSTrond Myklebust 	return 1;
1729c7944ebbSTrond Myklebust reval_dentry:
1730c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1731c7944ebbSTrond Myklebust 		return -ECHILD;
173242f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
17330ef97dcfSMiklos Szeredi 
1734c7944ebbSTrond Myklebust full_reval:
1735c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1736c7944ebbSTrond Myklebust }
1737535918f1STrond Myklebust 
1738c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1739c7944ebbSTrond Myklebust {
1740c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1741c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1742c0204fd2STrond Myklebust }
1743c0204fd2STrond Myklebust 
17441da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds /*
17471da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
17481da177e4SLinus Torvalds  */
17491da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
17501775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
17511775fd3eSDavid Quigley 				struct nfs4_label *label)
17521da177e4SLinus Torvalds {
1753fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
17542b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
17551da177e4SLinus Torvalds 	struct inode *inode;
1756b0c6108eSAl Viro 	struct dentry *d;
17571da177e4SLinus Torvalds 	int error = -EACCES;
17581da177e4SLinus Torvalds 
1759fab728e1STrond Myklebust 	d_drop(dentry);
1760fab728e1STrond Myklebust 
17611da177e4SLinus Torvalds 	/* We may have been initialized further down */
17622b0143b5SDavid Howells 	if (d_really_is_positive(dentry))
1763fab728e1STrond Myklebust 		goto out;
17641da177e4SLinus Torvalds 	if (fhandle->size == 0) {
17651775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
17661da177e4SLinus Torvalds 		if (error)
1767fab728e1STrond Myklebust 			goto out_error;
17681da177e4SLinus Torvalds 	}
17695724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17701da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
17711da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1772a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1773a841b54dSTrond Myklebust 				fattr, NULL, NULL);
17741da177e4SLinus Torvalds 		if (error < 0)
1775fab728e1STrond Myklebust 			goto out_error;
17761da177e4SLinus Torvalds 	}
17771775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1778b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1779b0c6108eSAl Viro 	if (IS_ERR(d)) {
1780b0c6108eSAl Viro 		error = PTR_ERR(d);
1781fab728e1STrond Myklebust 		goto out_error;
1782b0c6108eSAl Viro 	}
1783b0c6108eSAl Viro 	dput(d);
1784fab728e1STrond Myklebust out:
1785fab728e1STrond Myklebust 	dput(parent);
17861da177e4SLinus Torvalds 	return 0;
1787fab728e1STrond Myklebust out_error:
1788fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1789fab728e1STrond Myklebust 	dput(parent);
1790fab728e1STrond Myklebust 	return error;
17911da177e4SLinus Torvalds }
1792ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds /*
17951da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
17961da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
17971da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
17981da177e4SLinus Torvalds  * reply path made it appear to have failed.
17991da177e4SLinus Torvalds  */
1800597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1801ebfc3b49SAl Viro 		umode_t mode, bool excl)
18021da177e4SLinus Torvalds {
18031da177e4SLinus Torvalds 	struct iattr attr;
1804ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
18051da177e4SLinus Torvalds 	int error;
18061da177e4SLinus Torvalds 
18071e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
18086de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	attr.ia_mode = mode;
18111da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18121da177e4SLinus Torvalds 
18138b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
18148867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
18158b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
18161da177e4SLinus Torvalds 	if (error != 0)
18171da177e4SLinus Torvalds 		goto out_err;
18181da177e4SLinus Torvalds 	return 0;
18191da177e4SLinus Torvalds out_err:
18201da177e4SLinus Torvalds 	d_drop(dentry);
18211da177e4SLinus Torvalds 	return error;
18221da177e4SLinus Torvalds }
1823ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds /*
18261da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
18271da177e4SLinus Torvalds  */
1828597d9289SBryan Schumaker int
18291a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
18301da177e4SLinus Torvalds {
18311da177e4SLinus Torvalds 	struct iattr attr;
18321da177e4SLinus Torvalds 	int status;
18331da177e4SLinus Torvalds 
18341e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
18356de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	attr.ia_mode = mode;
18381da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18391da177e4SLinus Torvalds 
18401ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
18411da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
18421ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
18431da177e4SLinus Torvalds 	if (status != 0)
18441da177e4SLinus Torvalds 		goto out_err;
18451da177e4SLinus Torvalds 	return 0;
18461da177e4SLinus Torvalds out_err:
18471da177e4SLinus Torvalds 	d_drop(dentry);
18481da177e4SLinus Torvalds 	return status;
18491da177e4SLinus Torvalds }
1850ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds /*
18531da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
18541da177e4SLinus Torvalds  */
1855597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds 	struct iattr attr;
18581da177e4SLinus Torvalds 	int error;
18591da177e4SLinus Torvalds 
18601e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
18616de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18641da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
18651da177e4SLinus Torvalds 
18661ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
18671da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
18681ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
18691da177e4SLinus Torvalds 	if (error != 0)
18701da177e4SLinus Torvalds 		goto out_err;
18711da177e4SLinus Torvalds 	return 0;
18721da177e4SLinus Torvalds out_err:
18731da177e4SLinus Torvalds 	d_drop(dentry);
18741da177e4SLinus Torvalds 	return error;
18751da177e4SLinus Torvalds }
1876ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
18771da177e4SLinus Torvalds 
1878d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1879d45b9d8bSTrond Myklebust {
1880dc3f4198SAl Viro 	if (simple_positive(dentry))
1881d45b9d8bSTrond Myklebust 		d_delete(dentry);
1882d45b9d8bSTrond Myklebust }
1883d45b9d8bSTrond Myklebust 
1884597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
18851da177e4SLinus Torvalds {
18861da177e4SLinus Torvalds 	int error;
18871da177e4SLinus Torvalds 
18881e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
18896de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18901da177e4SLinus Torvalds 
18911ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
18922b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1893884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
18941da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18951da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1896ba6c0592STrond Myklebust 		switch (error) {
1897ba6c0592STrond Myklebust 		case 0:
18982b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1899ba6c0592STrond Myklebust 			break;
1900ba6c0592STrond Myklebust 		case -ENOENT:
1901d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1902ba6c0592STrond Myklebust 		}
1903884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1904ba6c0592STrond Myklebust 	} else
1905ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
19061ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	return error;
19091da177e4SLinus Torvalds }
1910ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds /*
19131da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
19141da177e4SLinus Torvalds  * and after checking that the file has only one user.
19151da177e4SLinus Torvalds  *
19161da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
19171da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
19181da177e4SLinus Torvalds  */
19191da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
19201da177e4SLinus Torvalds {
19212b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
19222b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
19231da177e4SLinus Torvalds 	int error = -EBUSY;
19241da177e4SLinus Torvalds 
19256de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
19281da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
19291da177e4SLinus Torvalds 		error = 0;
19301da177e4SLinus Torvalds 		goto out;
19311da177e4SLinus Torvalds 	}
19321da177e4SLinus Torvalds 
19331ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
19341da177e4SLinus Torvalds 	if (inode != NULL) {
1935912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
19361da177e4SLinus Torvalds 		if (error == 0)
19371b83d707STrond Myklebust 			nfs_drop_nlink(inode);
19381da177e4SLinus Torvalds 	} else
1939912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
1940d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1941d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
19421ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
19431da177e4SLinus Torvalds out:
19441da177e4SLinus Torvalds 	return error;
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
19481da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
19491da177e4SLinus Torvalds  *
19501da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
19511da177e4SLinus Torvalds  */
1952597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
19531da177e4SLinus Torvalds {
19541da177e4SLinus Torvalds 	int error;
19551da177e4SLinus Torvalds 	int need_rehash = 0;
19561da177e4SLinus Torvalds 
19571e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
19586de1472fSAl Viro 		dir->i_ino, dentry);
19591da177e4SLinus Torvalds 
19601ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
19611da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
196284d08fa8SAl Viro 	if (d_count(dentry) > 1) {
19631da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1964ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
19652b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
19661da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
19671ca42382STrond Myklebust 		goto out;
19681da177e4SLinus Torvalds 	}
19691da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
19701da177e4SLinus Torvalds 		__d_drop(dentry);
19711da177e4SLinus Torvalds 		need_rehash = 1;
19721da177e4SLinus Torvalds 	}
19731da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
19741da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1975d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
19761da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
19771da177e4SLinus Torvalds 	} else if (need_rehash)
19781da177e4SLinus Torvalds 		d_rehash(dentry);
19791ca42382STrond Myklebust out:
19801ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
19811da177e4SLinus Torvalds 	return error;
19821da177e4SLinus Torvalds }
1983ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
19841da177e4SLinus Torvalds 
1985873101b3SChuck Lever /*
1986873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1987873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1988873101b3SChuck Lever  * using prepare_write/commit_write.
1989873101b3SChuck Lever  *
1990873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1991873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1992873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1993873101b3SChuck Lever  * symlink request has completed on the server.
1994873101b3SChuck Lever  *
1995873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1996873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1997873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1998873101b3SChuck Lever  * and move the raw page into its mapping.
1999873101b3SChuck Lever  */
2000597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20011da177e4SLinus Torvalds {
2002873101b3SChuck Lever 	struct page *page;
2003873101b3SChuck Lever 	char *kaddr;
20041da177e4SLinus Torvalds 	struct iattr attr;
2005873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
20061da177e4SLinus Torvalds 	int error;
20071da177e4SLinus Torvalds 
20081e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
20096de1472fSAl Viro 		dir->i_ino, dentry, symname);
20101da177e4SLinus Torvalds 
2011873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2012873101b3SChuck Lever 		return -ENAMETOOLONG;
20131da177e4SLinus Torvalds 
2014873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2015873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
20161da177e4SLinus Torvalds 
2017e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
201876566991STrond Myklebust 	if (!page)
2019873101b3SChuck Lever 		return -ENOMEM;
2020873101b3SChuck Lever 
2021e8ecde25SAl Viro 	kaddr = page_address(page);
2022873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2023873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2024873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2025873101b3SChuck Lever 
20261ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
202794a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
20281ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2029873101b3SChuck Lever 	if (error != 0) {
20301e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2031873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
20326de1472fSAl Viro 			dentry, symname, error);
20331da177e4SLinus Torvalds 		d_drop(dentry);
2034873101b3SChuck Lever 		__free_page(page);
20351da177e4SLinus Torvalds 		return error;
20361da177e4SLinus Torvalds 	}
20371da177e4SLinus Torvalds 
2038873101b3SChuck Lever 	/*
2039873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2040873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2041873101b3SChuck Lever 	 */
20422b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2043873101b3SChuck Lever 							GFP_KERNEL)) {
2044873101b3SChuck Lever 		SetPageUptodate(page);
2045873101b3SChuck Lever 		unlock_page(page);
2046a0b54addSRafael Aquini 		/*
2047a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2048a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2049a0b54addSRafael Aquini 		 */
205009cbfeafSKirill A. Shutemov 		put_page(page);
2051873101b3SChuck Lever 	} else
2052873101b3SChuck Lever 		__free_page(page);
2053873101b3SChuck Lever 
2054873101b3SChuck Lever 	return 0;
2055873101b3SChuck Lever }
2056ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2057873101b3SChuck Lever 
2058597d9289SBryan Schumaker int
20591da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
20601da177e4SLinus Torvalds {
20612b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
20621da177e4SLinus Torvalds 	int error;
20631da177e4SLinus Torvalds 
20646de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
20656de1472fSAl Viro 		old_dentry, dentry);
20661da177e4SLinus Torvalds 
20671fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
20689697d234STrond Myklebust 	d_drop(dentry);
20691da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2070cf809556STrond Myklebust 	if (error == 0) {
20717de9c6eeSAl Viro 		ihold(inode);
20729697d234STrond Myklebust 		d_add(dentry, inode);
2073cf809556STrond Myklebust 	}
20741fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
20751da177e4SLinus Torvalds 	return error;
20761da177e4SLinus Torvalds }
2077ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds /*
20801da177e4SLinus Torvalds  * RENAME
20811da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
20821da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
20831da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
20841da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
20851da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
20861da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
20871da177e4SLinus Torvalds  *
20881da177e4SLinus Torvalds  * FIXED.
20891da177e4SLinus Torvalds  *
20901da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
20911da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
20921da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
20931da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
20941da177e4SLinus Torvalds  * using the inode layer
20951da177e4SLinus Torvalds  *
20961da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
20971da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
20981da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
20991da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
21001da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
21011da177e4SLinus Torvalds  * the rename.
21021da177e4SLinus Torvalds  */
2103597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
21041cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
21051cd66c93SMiklos Szeredi 	       unsigned int flags)
21061da177e4SLinus Torvalds {
21072b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
21082b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2109d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
211080a491fdSJeff Layton 	struct rpc_task *task;
21111da177e4SLinus Torvalds 	int error = -EBUSY;
21121da177e4SLinus Torvalds 
21131cd66c93SMiklos Szeredi 	if (flags)
21141cd66c93SMiklos Szeredi 		return -EINVAL;
21151cd66c93SMiklos Szeredi 
21166de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
21176de1472fSAl Viro 		 old_dentry, new_dentry,
211884d08fa8SAl Viro 		 d_count(new_dentry));
21191da177e4SLinus Torvalds 
212070ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
21211da177e4SLinus Torvalds 	/*
212228f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
212328f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
212428f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
212528f79a1aSMiklos Szeredi 	 * the new target.
21261da177e4SLinus Torvalds 	 */
212727226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
212827226104SMiklos Szeredi 		/*
212927226104SMiklos Szeredi 		 * To prevent any new references to the target during the
213027226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
213127226104SMiklos Szeredi 		 */
2132d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
213327226104SMiklos Szeredi 			d_drop(new_dentry);
2134d9f29500SBenjamin Coddington 			rehash = new_dentry;
2135d9f29500SBenjamin Coddington 		}
213627226104SMiklos Szeredi 
213784d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
21381da177e4SLinus Torvalds 			int err;
213927226104SMiklos Szeredi 
21401da177e4SLinus Torvalds 			/* copy the target dentry's name */
21411da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
21421da177e4SLinus Torvalds 					 &new_dentry->d_name);
21431da177e4SLinus Torvalds 			if (!dentry)
21441da177e4SLinus Torvalds 				goto out;
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
21471da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
214824e93025SMiklos Szeredi 			if (err)
21491da177e4SLinus Torvalds 				goto out;
215024e93025SMiklos Szeredi 
215124e93025SMiklos Szeredi 			new_dentry = dentry;
2152d9f29500SBenjamin Coddington 			rehash = NULL;
215324e93025SMiklos Szeredi 			new_inode = NULL;
2154b1e4adf4STrond Myklebust 		}
215527226104SMiklos Szeredi 	}
21561da177e4SLinus Torvalds 
2157d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
215880a491fdSJeff Layton 	if (IS_ERR(task)) {
215980a491fdSJeff Layton 		error = PTR_ERR(task);
216080a491fdSJeff Layton 		goto out;
216180a491fdSJeff Layton 	}
216280a491fdSJeff Layton 
216380a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2164818a8dbeSBenjamin Coddington 	if (error != 0) {
2165818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2166818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2167818a8dbeSBenjamin Coddington 		smp_wmb();
2168818a8dbeSBenjamin Coddington 	} else
216980a491fdSJeff Layton 		error = task->tk_status;
217080a491fdSJeff Layton 	rpc_put_task(task);
217159a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
217259a707b0STrond Myklebust 	if (error == 0) {
217359a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
217459a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
217559a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
217659a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
217759a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
217859a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
217959a707b0STrond Myklebust 	}
21801da177e4SLinus Torvalds out:
2181d9f29500SBenjamin Coddington 	if (rehash)
2182d9f29500SBenjamin Coddington 		d_rehash(rehash);
218370ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
218470ded201STrond Myklebust 			new_dir, new_dentry, error);
2185d9f29500SBenjamin Coddington 	if (!error) {
2186d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2187d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2188d9f29500SBenjamin Coddington 		/*
2189d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2190d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2191d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2192d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2193d9f29500SBenjamin Coddington 		 */
2194d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2195d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2196d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2197d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2198d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2199d9f29500SBenjamin Coddington 
22001da177e4SLinus Torvalds 	/* new dentry created? */
22011da177e4SLinus Torvalds 	if (dentry)
22021da177e4SLinus Torvalds 		dput(dentry);
22031da177e4SLinus Torvalds 	return error;
22041da177e4SLinus Torvalds }
2205ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
22061da177e4SLinus Torvalds 
2207cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2208cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2209cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2210cfcea3e8STrond Myklebust 
22113a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
22123a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
22133a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
22143a505845STrond Myklebust 
22151c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
22161c3c07e9STrond Myklebust {
2217b68572e0SNeilBrown 	put_cred(entry->cred);
2218f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
22194e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2220cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
22214e857c58SPeter Zijlstra 	smp_mb__after_atomic();
22221c3c07e9STrond Myklebust }
22231c3c07e9STrond Myklebust 
22241a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
22251a81bb8aSTrond Myklebust {
22261a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
22271a81bb8aSTrond Myklebust 
22281a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
22291a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
22301a81bb8aSTrond Myklebust 		list_del(&cache->lru);
22311a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
22321a81bb8aSTrond Myklebust 	}
22331a81bb8aSTrond Myklebust }
22341a81bb8aSTrond Myklebust 
22353a505845STrond Myklebust static unsigned long
22363a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2237979df72eSTrond Myklebust {
2238979df72eSTrond Myklebust 	LIST_HEAD(head);
2239aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2240979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
22411ab6c499SDave Chinner 	long freed = 0;
2242979df72eSTrond Myklebust 
2243a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2244aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2245979df72eSTrond Myklebust 		struct inode *inode;
2246979df72eSTrond Myklebust 
2247979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2248979df72eSTrond Myklebust 			break;
22499c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2250979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2251979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2252979df72eSTrond Myklebust 			goto remove_lru_entry;
2253979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2254979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2255979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2256979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
22571ab6c499SDave Chinner 		freed++;
2258979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2259979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2260979df72eSTrond Myklebust 					&nfs_access_lru_list);
2261979df72eSTrond Myklebust 		else {
2262979df72eSTrond Myklebust remove_lru_entry:
2263979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
22644e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2265979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
22664e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2267979df72eSTrond Myklebust 		}
226859844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2269979df72eSTrond Myklebust 	}
2270979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22711a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22721ab6c499SDave Chinner 	return freed;
22731ab6c499SDave Chinner }
22741ab6c499SDave Chinner 
22751ab6c499SDave Chinner unsigned long
22763a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
22773a505845STrond Myklebust {
22783a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
22793a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
22803a505845STrond Myklebust 
22813a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
22823a505845STrond Myklebust 		return SHRINK_STOP;
22833a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
22843a505845STrond Myklebust }
22853a505845STrond Myklebust 
22863a505845STrond Myklebust 
22873a505845STrond Myklebust unsigned long
22881ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
22891ab6c499SDave Chinner {
229055f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2291979df72eSTrond Myklebust }
2292979df72eSTrond Myklebust 
22933a505845STrond Myklebust static void
22943a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
22953a505845STrond Myklebust {
22963a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
22973a505845STrond Myklebust 	unsigned long diff;
22983a505845STrond Myklebust 	unsigned int nr_to_scan;
22993a505845STrond Myklebust 
23003a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
23013a505845STrond Myklebust 		return;
23023a505845STrond Myklebust 	nr_to_scan = 100;
23033a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
23043a505845STrond Myklebust 	if (diff < nr_to_scan)
23053a505845STrond Myklebust 		nr_to_scan = diff;
23063a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
23073a505845STrond Myklebust }
23083a505845STrond Myklebust 
23091a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
23101c3c07e9STrond Myklebust {
23111c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
23121a81bb8aSTrond Myklebust 	struct rb_node *n;
23131c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
23141c3c07e9STrond Myklebust 
23151c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
23161c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
23171c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
23181c3c07e9STrond Myklebust 		rb_erase(n, root_node);
23191a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
23201c3c07e9STrond Myklebust 	}
23211c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
23221c3c07e9STrond Myklebust }
23231c3c07e9STrond Myklebust 
23241c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
23251c3c07e9STrond Myklebust {
23261a81bb8aSTrond Myklebust 	LIST_HEAD(head);
23271a81bb8aSTrond Myklebust 
23281a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
23291a81bb8aSTrond Myklebust 		return;
2330cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2331cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
23321a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2333cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2334cfcea3e8STrond Myklebust 
23351c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
23361a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
23371a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
23381a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
23391a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
23401c3c07e9STrond Myklebust }
23411c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
23421c3c07e9STrond Myklebust 
2343b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
23441c3c07e9STrond Myklebust {
23451c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
23461c3c07e9STrond Myklebust 
23471c3c07e9STrond Myklebust 	while (n != NULL) {
2348b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2349b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2350b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
23511c3c07e9STrond Myklebust 
2352b68572e0SNeilBrown 		if (cmp < 0)
23531c3c07e9STrond Myklebust 			n = n->rb_left;
2354b68572e0SNeilBrown 		else if (cmp > 0)
23551c3c07e9STrond Myklebust 			n = n->rb_right;
23561c3c07e9STrond Myklebust 		else
23571c3c07e9STrond Myklebust 			return entry;
23581c3c07e9STrond Myklebust 	}
23591c3c07e9STrond Myklebust 	return NULL;
23601c3c07e9STrond Myklebust }
23611c3c07e9STrond Myklebust 
2362b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
23631da177e4SLinus Torvalds {
236455296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
23651c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
236657b69181STrond Myklebust 	bool retry = true;
236757b69181STrond Myklebust 	int err;
23681da177e4SLinus Torvalds 
23691c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
237057b69181STrond Myklebust 	for(;;) {
23711c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
23721c3c07e9STrond Myklebust 			goto out_zap;
23731c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
237457b69181STrond Myklebust 		err = -ENOENT;
23751c3c07e9STrond Myklebust 		if (cache == NULL)
23761c3c07e9STrond Myklebust 			goto out;
237757b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
237821c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
237957b69181STrond Myklebust 			break;
238057b69181STrond Myklebust 		err = -ECHILD;
238157b69181STrond Myklebust 		if (!may_block)
238257b69181STrond Myklebust 			goto out;
238357b69181STrond Myklebust 		if (!retry)
238457b69181STrond Myklebust 			goto out_zap;
238557b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
238657b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
238757b69181STrond Myklebust 		if (err)
238857b69181STrond Myklebust 			return err;
238957b69181STrond Myklebust 		spin_lock(&inode->i_lock);
239057b69181STrond Myklebust 		retry = false;
239157b69181STrond Myklebust 	}
23921c3c07e9STrond Myklebust 	res->cred = cache->cred;
23931c3c07e9STrond Myklebust 	res->mask = cache->mask;
2394cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
23951c3c07e9STrond Myklebust 	err = 0;
23961c3c07e9STrond Myklebust out:
23971c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23981c3c07e9STrond Myklebust 	return err;
23991c3c07e9STrond Myklebust out_zap:
24001a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
24011a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
24021c3c07e9STrond Myklebust 	return -ENOENT;
24031c3c07e9STrond Myklebust }
24041c3c07e9STrond Myklebust 
2405b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2406f682a398SNeilBrown {
2407f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2408f682a398SNeilBrown 	 * but do it without locking.
2409f682a398SNeilBrown 	 */
2410f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2411f682a398SNeilBrown 	struct nfs_access_entry *cache;
2412f682a398SNeilBrown 	int err = -ECHILD;
2413f682a398SNeilBrown 	struct list_head *lh;
2414f682a398SNeilBrown 
2415f682a398SNeilBrown 	rcu_read_lock();
2416f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2417f682a398SNeilBrown 		goto out;
2418f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2419f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2420f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2421f682a398SNeilBrown 	    cred != cache->cred)
2422f682a398SNeilBrown 		cache = NULL;
2423f682a398SNeilBrown 	if (cache == NULL)
2424f682a398SNeilBrown 		goto out;
242521c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2426f682a398SNeilBrown 		goto out;
2427f682a398SNeilBrown 	res->cred = cache->cred;
2428f682a398SNeilBrown 	res->mask = cache->mask;
242921c3ba7eSTrond Myklebust 	err = 0;
2430f682a398SNeilBrown out:
2431f682a398SNeilBrown 	rcu_read_unlock();
2432f682a398SNeilBrown 	return err;
2433f682a398SNeilBrown }
2434f682a398SNeilBrown 
24351c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
24361c3c07e9STrond Myklebust {
2437cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2438cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
24391c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
24401c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
24411c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2442b68572e0SNeilBrown 	int cmp;
24431c3c07e9STrond Myklebust 
24441c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
24451c3c07e9STrond Myklebust 	while (*p != NULL) {
24461c3c07e9STrond Myklebust 		parent = *p;
24471c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2448b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
24491c3c07e9STrond Myklebust 
2450b68572e0SNeilBrown 		if (cmp < 0)
24511c3c07e9STrond Myklebust 			p = &parent->rb_left;
2452b68572e0SNeilBrown 		else if (cmp > 0)
24531c3c07e9STrond Myklebust 			p = &parent->rb_right;
24541c3c07e9STrond Myklebust 		else
24551c3c07e9STrond Myklebust 			goto found;
24561c3c07e9STrond Myklebust 	}
24571c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
24581c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2459cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
24601c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24611c3c07e9STrond Myklebust 	return;
24621c3c07e9STrond Myklebust found:
24631c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2464cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2465cfcea3e8STrond Myklebust 	list_del(&entry->lru);
24661c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24671c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds 
24706168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
24711da177e4SLinus Torvalds {
24721c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
24731c3c07e9STrond Myklebust 	if (cache == NULL)
24741c3c07e9STrond Myklebust 		return;
24751c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2476b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
24771da177e4SLinus Torvalds 	cache->mask = set->mask;
24781c3c07e9STrond Myklebust 
2479f682a398SNeilBrown 	/* The above field assignments must be visible
2480f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2481f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2482f682a398SNeilBrown 	 */
2483f682a398SNeilBrown 	smp_wmb();
24841c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2485cfcea3e8STrond Myklebust 
2486cfcea3e8STrond Myklebust 	/* Update accounting */
24874e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2488cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
24894e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2490cfcea3e8STrond Myklebust 
2491cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
24921a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2493cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
24941a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
24951a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
24961a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2497cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2498cfcea3e8STrond Myklebust 	}
24993a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
25001da177e4SLinus Torvalds }
25016168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
25026168f62cSWeston Andros Adamson 
25033c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
25043c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
25053c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
25063c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
25073c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
25083c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2509ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
25103c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
25113c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
251215d4b73aSTrond Myklebust static int
2513ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
251415d4b73aSTrond Myklebust {
251515d4b73aSTrond Myklebust 	int mask = 0;
251615d4b73aSTrond Myklebust 
251715d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
251815d4b73aSTrond Myklebust 		mask |= MAY_READ;
2519ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2520ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
252115d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2522ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
252315d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2524ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2525ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2526ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2527ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
252815d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2529ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2530ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
253115d4b73aSTrond Myklebust 	return mask;
253215d4b73aSTrond Myklebust }
253315d4b73aSTrond Myklebust 
25346168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
25356168f62cSWeston Andros Adamson {
2536bd8b2441STrond Myklebust 	entry->mask = access_result;
25376168f62cSWeston Andros Adamson }
25386168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
25391da177e4SLinus Torvalds 
2540b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
25411da177e4SLinus Torvalds {
25421da177e4SLinus Torvalds 	struct nfs_access_entry cache;
254357b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2544bd8b2441STrond Myklebust 	int cache_mask;
25451da177e4SLinus Torvalds 	int status;
25461da177e4SLinus Torvalds 
2547f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2548f4ce1299STrond Myklebust 
2549f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2550f682a398SNeilBrown 	if (status != 0)
255157b69181STrond Myklebust 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
25521da177e4SLinus Torvalds 	if (status == 0)
2553f4ce1299STrond Myklebust 		goto out_cached;
25541da177e4SLinus Torvalds 
2555f3324a2aSNeilBrown 	status = -ECHILD;
255657b69181STrond Myklebust 	if (!may_block)
2557f3324a2aSNeilBrown 		goto out;
2558f3324a2aSNeilBrown 
25591750d929SAnna Schumaker 	/*
25601750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
25611750d929SAnna Schumaker 	 */
25621750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
25631750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
25641750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
25651750d929SAnna Schumaker 	else
25661750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
25671da177e4SLinus Torvalds 	cache.cred = cred;
25681da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2569a71ee337SSuresh Jayaraman 	if (status != 0) {
2570a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2571a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2572a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2573a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2574a71ee337SSuresh Jayaraman 		}
2575f4ce1299STrond Myklebust 		goto out;
2576a71ee337SSuresh Jayaraman 	}
25771da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2578f4ce1299STrond Myklebust out_cached:
2579ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2580bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2581f4ce1299STrond Myklebust 		status = -EACCES;
25821da177e4SLinus Torvalds out:
2583f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2584f4ce1299STrond Myklebust 	return status;
25851da177e4SLinus Torvalds }
25861da177e4SLinus Torvalds 
2587af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2588af22f94aSTrond Myklebust {
2589af22f94aSTrond Myklebust 	int mask = 0;
2590af22f94aSTrond Myklebust 
2591f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2592f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2593f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2594f8d9a897SWeston Andros Adamson 	} else {
25958a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2596af22f94aSTrond Myklebust 			mask |= MAY_READ;
25978a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2598af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2599f8d9a897SWeston Andros Adamson 	}
2600f8d9a897SWeston Andros Adamson 
2601af22f94aSTrond Myklebust 	return mask;
2602af22f94aSTrond Myklebust }
2603af22f94aSTrond Myklebust 
2604b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2605af22f94aSTrond Myklebust {
2606af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2607af22f94aSTrond Myklebust }
260889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2609af22f94aSTrond Myklebust 
26105c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
26115c5fc09aSTrond Myklebust {
26125c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
261321c3ba7eSTrond Myklebust 	int ret = 0;
26145c5fc09aSTrond Myklebust 
26153825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
26163825827eSTrond Myklebust 		return 0;
2617cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
26185c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
261921c3ba7eSTrond Myklebust 			return -ECHILD;
262021c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
262121c3ba7eSTrond Myklebust 	}
26225c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
26235c5fc09aSTrond Myklebust 		ret = -EACCES;
26245c5fc09aSTrond Myklebust 	return ret;
26255c5fc09aSTrond Myklebust }
26265c5fc09aSTrond Myklebust 
262710556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
26281da177e4SLinus Torvalds {
2629b68572e0SNeilBrown 	const struct cred *cred = current_cred();
26301da177e4SLinus Torvalds 	int res = 0;
26311da177e4SLinus Torvalds 
263291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
263391d5b470SChuck Lever 
2634e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
26351da177e4SLinus Torvalds 		goto out;
26361da177e4SLinus Torvalds 	/* Is this sys_access() ? */
26379cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
26381da177e4SLinus Torvalds 		goto force_lookup;
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
26411da177e4SLinus Torvalds 		case S_IFLNK:
26421da177e4SLinus Torvalds 			goto out;
26431da177e4SLinus Torvalds 		case S_IFREG:
2644762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2645762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2646762674f8STrond Myklebust 				return 0;
26471da177e4SLinus Torvalds 			break;
26481da177e4SLinus Torvalds 		case S_IFDIR:
26491da177e4SLinus Torvalds 			/*
26501da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
26511da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
26521da177e4SLinus Torvalds 			 */
26531da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
26541da177e4SLinus Torvalds 				goto out;
26551da177e4SLinus Torvalds 	}
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds force_lookup:
26581da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
26591da177e4SLinus Torvalds 		goto out_notsup;
26601da177e4SLinus Torvalds 
2661f3324a2aSNeilBrown 	/* Always try fast lookups first */
2662f3324a2aSNeilBrown 	rcu_read_lock();
2663f3324a2aSNeilBrown 	res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2664f3324a2aSNeilBrown 	rcu_read_unlock();
2665f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2666f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
26671da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
2668f3324a2aSNeilBrown 	}
26691da177e4SLinus Torvalds out:
26705c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
26715c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2672f696a365SMiklos Szeredi 
26731e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
26741e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
26751da177e4SLinus Torvalds 	return res;
26761da177e4SLinus Torvalds out_notsup:
2677d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2678d51ac1a8SNeilBrown 		return -ECHILD;
2679d51ac1a8SNeilBrown 
26801da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
26811da177e4SLinus Torvalds 	if (res == 0)
26822830ba7fSAl Viro 		res = generic_permission(inode, mask);
26831e7cb3dcSChuck Lever 	goto out;
26841da177e4SLinus Torvalds }
2685ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds /*
26881da177e4SLinus Torvalds  * Local variables:
26891da177e4SLinus Torvalds  *  version-control: t
26901da177e4SLinus Torvalds  *  kept-new-versions: 5
26911da177e4SLinus Torvalds  * End:
26921da177e4SLinus Torvalds  */
2693