xref: /openbmc/linux/fs/nfs/dir.c (revision 9a206de2ea878f4502e86b81c0d7eb9b651bde82)
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);
831c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
841c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
851c341b77STrond Myklebust 			nfsi->cache_validity |= NFS_INO_INVALID_DATA |
861c341b77STrond Myklebust 				NFS_INO_REVAL_FORCED;
87311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
88311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
89480c2006SBryan Schumaker 		return ctx;
90480c2006SBryan Schumaker 	}
910c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
920c030806STrond Myklebust }
93480c2006SBryan Schumaker 
94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
95480c2006SBryan Schumaker {
96311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
97311324adSTrond Myklebust 	list_del(&ctx->list);
98311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
99684f39b4SNeilBrown 	put_cred(ctx->cred);
100480c2006SBryan Schumaker 	kfree(ctx);
101480c2006SBryan Schumaker }
102480c2006SBryan Schumaker 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * Open file
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static int
1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1081da177e4SLinus Torvalds {
109480c2006SBryan Schumaker 	int res = 0;
110480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1111da177e4SLinus Torvalds 
1126de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
113cc0dd2d1SChuck Lever 
114cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1151e7cb3dcSChuck Lever 
116684f39b4SNeilBrown 	ctx = alloc_nfs_open_dir_context(inode, current_cred());
117480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
118480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
119480c2006SBryan Schumaker 		goto out;
120480c2006SBryan Schumaker 	}
121480c2006SBryan Schumaker 	filp->private_data = ctx;
122480c2006SBryan Schumaker out:
1231da177e4SLinus Torvalds 	return res;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126480c2006SBryan Schumaker static int
127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
128480c2006SBryan Schumaker {
129a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
130480c2006SBryan Schumaker 	return 0;
131480c2006SBryan Schumaker }
132480c2006SBryan Schumaker 
133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
134d1bacf9eSBryan Schumaker 	u64 cookie;
135d1bacf9eSBryan Schumaker 	u64 ino;
136d1bacf9eSBryan Schumaker 	struct qstr string;
1370b26a0bfSTrond Myklebust 	unsigned char d_type;
138d1bacf9eSBryan Schumaker };
139d1bacf9eSBryan Schumaker 
140d1bacf9eSBryan Schumaker struct nfs_cache_array {
14188b8e133SChuck Lever 	int size;
142d1bacf9eSBryan Schumaker 	int eof_index;
143d1bacf9eSBryan Schumaker 	u64 last_cookie;
144d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
145d1bacf9eSBryan Schumaker };
146d1bacf9eSBryan Schumaker 
147a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
1481da177e4SLinus Torvalds typedef struct {
1491da177e4SLinus Torvalds 	struct file	*file;
1501da177e4SLinus Torvalds 	struct page	*page;
15123db8620SAl Viro 	struct dir_context *ctx;
1521da177e4SLinus Torvalds 	unsigned long	page_index;
153f0dd2136STrond Myklebust 	u64		*dir_cookie;
1540aded708STrond Myklebust 	u64		last_cookie;
155f0dd2136STrond Myklebust 	loff_t		current_index;
1561da177e4SLinus Torvalds 	decode_dirent_t	decode;
157d1bacf9eSBryan Schumaker 
1581f4eab7eSNeil Brown 	unsigned long	timestamp;
1594704f0e2STrond Myklebust 	unsigned long	gencount;
160d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
161a7a3b1e9SBenjamin Coddington 	bool plus;
162a7a3b1e9SBenjamin Coddington 	bool eof;
1631da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1641da177e4SLinus Torvalds 
165d1bacf9eSBryan Schumaker /*
166d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
167d1bacf9eSBryan Schumaker  */
168d1bacf9eSBryan Schumaker static
16911de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
170d1bacf9eSBryan Schumaker {
17111de3b11STrond Myklebust 	struct nfs_cache_array *array;
172d1bacf9eSBryan Schumaker 	int i;
1738cd51a0cSTrond Myklebust 
1742b86ce2dSCong Wang 	array = kmap_atomic(page);
175d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
176d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1772b86ce2dSCong Wang 	kunmap_atomic(array);
178d1bacf9eSBryan Schumaker }
179d1bacf9eSBryan Schumaker 
180d1bacf9eSBryan Schumaker /*
181d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
182d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
183d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
184d1bacf9eSBryan Schumaker  */
185d1bacf9eSBryan Schumaker static
1864a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
187d1bacf9eSBryan Schumaker {
188d1bacf9eSBryan Schumaker 	string->len = len;
189d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
1904a201d6eSTrond Myklebust 	if (string->name == NULL)
1914a201d6eSTrond Myklebust 		return -ENOMEM;
19204e4bd1cSCatalin Marinas 	/*
19304e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
19404e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
19504e4bd1cSCatalin Marinas 	 */
19604e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
1978387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
1984a201d6eSTrond Myklebust 	return 0;
199d1bacf9eSBryan Schumaker }
200d1bacf9eSBryan Schumaker 
201d1bacf9eSBryan Schumaker static
202d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
203d1bacf9eSBryan Schumaker {
2040795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2054a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2064a201d6eSTrond Myklebust 	int ret;
2074a201d6eSTrond Myklebust 
2084a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2093020093fSTrond Myklebust 
2103020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2113020093fSTrond Myklebust 	ret = -ENOSPC;
2123020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2133020093fSTrond Myklebust 		goto out;
2143020093fSTrond Myklebust 
2154a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2164a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2170b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2184a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2194a201d6eSTrond Myklebust 	if (ret)
2204a201d6eSTrond Myklebust 		goto out;
221d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2228cd51a0cSTrond Myklebust 	array->size++;
22347c716cbSTrond Myklebust 	if (entry->eof != 0)
224d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2254a201d6eSTrond Myklebust out:
2260795bf83SFabian Frederick 	kunmap(page);
2274a201d6eSTrond Myklebust 	return ret;
228d1bacf9eSBryan Schumaker }
229d1bacf9eSBryan Schumaker 
230d1bacf9eSBryan Schumaker static
231d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
232d1bacf9eSBryan Schumaker {
23323db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
234d1bacf9eSBryan Schumaker 	unsigned int index;
235d1bacf9eSBryan Schumaker 
236d1bacf9eSBryan Schumaker 	if (diff < 0)
237d1bacf9eSBryan Schumaker 		goto out_eof;
238d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2398cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
240d1bacf9eSBryan Schumaker 			goto out_eof;
241d1bacf9eSBryan Schumaker 		return -EAGAIN;
242d1bacf9eSBryan Schumaker 	}
243d1bacf9eSBryan Schumaker 
244d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
245d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
246d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
247d1bacf9eSBryan Schumaker 	return 0;
248d1bacf9eSBryan Schumaker out_eof:
2496089dd0dSThomas Meyer 	desc->eof = true;
250d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
251d1bacf9eSBryan Schumaker }
252d1bacf9eSBryan Schumaker 
2534db72b40SJeff Layton static bool
2544db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2554db72b40SJeff Layton {
2564db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2574db72b40SJeff Layton 		return false;
2584db72b40SJeff Layton 	smp_rmb();
2594db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2604db72b40SJeff Layton }
2614db72b40SJeff Layton 
262d1bacf9eSBryan Schumaker static
263d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
264d1bacf9eSBryan Schumaker {
265d1bacf9eSBryan Schumaker 	int i;
2668ef2ce3eSBryan Schumaker 	loff_t new_pos;
267d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
268d1bacf9eSBryan Schumaker 
269d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2708cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
271496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2720c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2730c030806STrond Myklebust 
2748ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2754db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
2764db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
2770c030806STrond Myklebust 				ctx->duped = 0;
2780c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
27923db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2800c030806STrond Myklebust 				if (ctx->duped > 0
2810c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2820c030806STrond Myklebust 					if (printk_ratelimit()) {
2836de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
2840c030806STrond Myklebust 								"Please contact your server vendor.  "
2859581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
2869581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
2879581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
2880c030806STrond Myklebust 					}
2890c030806STrond Myklebust 					status = -ELOOP;
2900c030806STrond Myklebust 					goto out;
2910c030806STrond Myklebust 				}
2928ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
2930c030806STrond Myklebust 				ctx->duped = -1;
2948ef2ce3eSBryan Schumaker 			}
29523db8620SAl Viro 			desc->ctx->pos = new_pos;
2968cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
29747c716cbSTrond Myklebust 			return 0;
2988cd51a0cSTrond Myklebust 		}
2998cd51a0cSTrond Myklebust 	}
30047c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
301d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
30218fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
3036089dd0dSThomas Meyer 			desc->eof = true;
304d1bacf9eSBryan Schumaker 	}
3050c030806STrond Myklebust out:
306d1bacf9eSBryan Schumaker 	return status;
307d1bacf9eSBryan Schumaker }
308d1bacf9eSBryan Schumaker 
309d1bacf9eSBryan Schumaker static
310d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
311d1bacf9eSBryan Schumaker {
312d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
31347c716cbSTrond Myklebust 	int status;
314d1bacf9eSBryan Schumaker 
3150795bf83SFabian Frederick 	array = kmap(desc->page);
316d1bacf9eSBryan Schumaker 
317d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
318d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
319d1bacf9eSBryan Schumaker 	else
320d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
321d1bacf9eSBryan Schumaker 
32247c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3230aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
324e47c085aSTrond Myklebust 		desc->current_index += array->size;
32547c716cbSTrond Myklebust 		desc->page_index++;
32647c716cbSTrond Myklebust 	}
3270795bf83SFabian Frederick 	kunmap(desc->page);
328d1bacf9eSBryan Schumaker 	return status;
329d1bacf9eSBryan Schumaker }
330d1bacf9eSBryan Schumaker 
331d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
332d1bacf9eSBryan Schumaker static
33356e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
334d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
335d1bacf9eSBryan Schumaker {
336480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
337684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
3384704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3391da177e4SLinus Torvalds 	int		error;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds  again:
3421da177e4SLinus Torvalds 	timestamp = jiffies;
3434704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
344be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3451da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3461da177e4SLinus Torvalds 	if (error < 0) {
3471da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3481da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3491da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3503a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
351a7a3b1e9SBenjamin Coddington 			desc->plus = false;
3521da177e4SLinus Torvalds 			goto again;
3531da177e4SLinus Torvalds 		}
3541da177e4SLinus Torvalds 		goto error;
3551da177e4SLinus Torvalds 	}
3561f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3574704f0e2STrond Myklebust 	desc->gencount = gencount;
358d1bacf9eSBryan Schumaker error:
359d1bacf9eSBryan Schumaker 	return error;
360d1bacf9eSBryan Schumaker }
361d1bacf9eSBryan Schumaker 
362573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
363573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
364d1bacf9eSBryan Schumaker {
365573c4e1eSChuck Lever 	int error;
366d1bacf9eSBryan Schumaker 
367573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
368573c4e1eSChuck Lever 	if (error)
369573c4e1eSChuck Lever 		return error;
370d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
371d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
372d1bacf9eSBryan Schumaker 	return 0;
373d1bacf9eSBryan Schumaker }
374d1bacf9eSBryan Schumaker 
375fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
376fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
377fa923369STrond Myklebust  */
378d39ab9deSBryan Schumaker static
379d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
380d39ab9deSBryan Schumaker {
381d8fdb47fSTrond Myklebust 	struct inode *inode;
382fa923369STrond Myklebust 	struct nfs_inode *nfsi;
383fa923369STrond Myklebust 
3842b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
3852b0143b5SDavid Howells 		return 0;
386fa923369STrond Myklebust 
387d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
388d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
389d8fdb47fSTrond Myklebust 		return 0;
390d8fdb47fSTrond Myklebust 
391d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
3927dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
393d39ab9deSBryan Schumaker 		return 0;
3947dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
3957dc72d5fSTrond Myklebust 		return 0;
3967dc72d5fSTrond Myklebust 	return 1;
397d39ab9deSBryan Schumaker }
398d39ab9deSBryan Schumaker 
399d39ab9deSBryan Schumaker static
40023db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
401d69ee9b8STrond Myklebust {
402d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
403d69ee9b8STrond Myklebust 		return false;
404d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
405d69ee9b8STrond Myklebust 		return true;
40623db8620SAl Viro 	if (ctx->pos == 0)
407d69ee9b8STrond Myklebust 		return true;
408d69ee9b8STrond Myklebust 	return false;
409d69ee9b8STrond Myklebust }
410d69ee9b8STrond Myklebust 
411d69ee9b8STrond Myklebust /*
41263519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
41363519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
414d69ee9b8STrond Myklebust  * directory.
415d69ee9b8STrond Myklebust  */
416d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
417d69ee9b8STrond Myklebust {
41863519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
41963519fbcSTrond Myklebust 
42063519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
42163519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
42263519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
423d69ee9b8STrond Myklebust }
424d69ee9b8STrond Myklebust 
425311324adSTrond Myklebust /*
426311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
427311324adSTrond Myklebust  *
428311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
429311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
430311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
431311324adSTrond Myklebust  * cache flush.
432311324adSTrond Myklebust  */
433311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
434311324adSTrond Myklebust {
43563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
43663519fbcSTrond Myklebust 
43763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
43863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
43963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
44079f687a3STrond Myklebust 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
441311324adSTrond Myklebust 	}
442311324adSTrond Myklebust }
443311324adSTrond Myklebust 
444d69ee9b8STrond Myklebust static
445d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
446d39ab9deSBryan Schumaker {
44726fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4489ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4494a201d6eSTrond Myklebust 	struct dentry *dentry;
4504a201d6eSTrond Myklebust 	struct dentry *alias;
4512b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
452d39ab9deSBryan Schumaker 	struct inode *inode;
453aa9c2669SDavid Quigley 	int status;
454d39ab9deSBryan Schumaker 
455fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
456fa923369STrond Myklebust 		return;
4576c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
4586c441c25STrond Myklebust 		return;
45978d04af4STrond Myklebust 	if (filename.len == 0)
46078d04af4STrond Myklebust 		return;
46178d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
46278d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
46378d04af4STrond Myklebust 		return;
46478d04af4STrond Myklebust 	/* ...or '/' */
46578d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
46678d04af4STrond Myklebust 		return;
4674a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4684a201d6eSTrond Myklebust 		if (filename.len == 1)
4694a201d6eSTrond Myklebust 			return;
4704a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4714a201d6eSTrond Myklebust 			return;
4724a201d6eSTrond Myklebust 	}
4738387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
474d39ab9deSBryan Schumaker 
4754a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
4769ac3d3e8SAl Viro again:
4779ac3d3e8SAl Viro 	if (!dentry) {
4789ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
4799ac3d3e8SAl Viro 		if (IS_ERR(dentry))
4809ac3d3e8SAl Viro 			return;
4819ac3d3e8SAl Viro 	}
4829ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
4836c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
4846c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
4856c441c25STrond Myklebust 					&entry->fattr->fsid))
4866c441c25STrond Myklebust 			goto out;
487d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
4887dc72d5fSTrond Myklebust 			if (!entry->fh->size)
4897dc72d5fSTrond Myklebust 				goto out;
490cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
4912b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
492aa9c2669SDavid Quigley 			if (!status)
4932b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
494d39ab9deSBryan Schumaker 			goto out;
495d39ab9deSBryan Schumaker 		} else {
4965542aa2fSEric W. Biederman 			d_invalidate(dentry);
497d39ab9deSBryan Schumaker 			dput(dentry);
4989ac3d3e8SAl Viro 			dentry = NULL;
4999ac3d3e8SAl Viro 			goto again;
500d39ab9deSBryan Schumaker 		}
501d39ab9deSBryan Schumaker 	}
5027dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5037dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5047dc72d5fSTrond Myklebust 		goto out;
5057dc72d5fSTrond Myklebust 	}
506d39ab9deSBryan Schumaker 
5071775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
50841d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5099ac3d3e8SAl Viro 	d_lookup_done(dentry);
5109ac3d3e8SAl Viro 	if (alias) {
511d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
512d39ab9deSBryan Schumaker 			goto out;
5139ac3d3e8SAl Viro 		dput(dentry);
5149ac3d3e8SAl Viro 		dentry = alias;
5159ac3d3e8SAl Viro 	}
516d39ab9deSBryan Schumaker 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
517d39ab9deSBryan Schumaker out:
518d39ab9deSBryan Schumaker 	dput(dentry);
519d39ab9deSBryan Schumaker }
520d39ab9deSBryan Schumaker 
521d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
522d1bacf9eSBryan Schumaker static
5238cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5246650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
525d1bacf9eSBryan Schumaker {
526babddc72SBryan Schumaker 	struct xdr_stream stream;
527f7da7a12SBenny Halevy 	struct xdr_buf buf;
5286650239aSTrond Myklebust 	struct page *scratch;
52999424380SBryan Schumaker 	struct nfs_cache_array *array;
5305c346854STrond Myklebust 	unsigned int count = 0;
5315c346854STrond Myklebust 	int status;
532babddc72SBryan Schumaker 
5336650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5346650239aSTrond Myklebust 	if (scratch == NULL)
5356650239aSTrond Myklebust 		return -ENOMEM;
536babddc72SBryan Schumaker 
537ce85cfbeSBenjamin Coddington 	if (buflen == 0)
538ce85cfbeSBenjamin Coddington 		goto out_nopages;
539ce85cfbeSBenjamin Coddington 
540f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5416650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
54299424380SBryan Schumaker 
54399424380SBryan Schumaker 	do {
54499424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5458cd51a0cSTrond Myklebust 		if (status != 0) {
5468cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5478cd51a0cSTrond Myklebust 				status = 0;
54899424380SBryan Schumaker 			break;
5498cd51a0cSTrond Myklebust 		}
55099424380SBryan Schumaker 
5515c346854STrond Myklebust 		count++;
5525c346854STrond Myklebust 
553a7a3b1e9SBenjamin Coddington 		if (desc->plus)
554be62a1a8SMiklos Szeredi 			nfs_prime_dcache(file_dentry(desc->file), entry);
5558cd51a0cSTrond Myklebust 
556db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
5578cd51a0cSTrond Myklebust 		if (status != 0)
5588cd51a0cSTrond Myklebust 			break;
55999424380SBryan Schumaker 	} while (!entry->eof);
56099424380SBryan Schumaker 
561ce85cfbeSBenjamin Coddington out_nopages:
56247c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
563db531db9SMax Kellermann 		array = kmap(page);
5648cd51a0cSTrond Myklebust 		array->eof_index = array->size;
56599424380SBryan Schumaker 		status = 0;
566db531db9SMax Kellermann 		kunmap(page);
56756e4ebf8SBryan Schumaker 	}
5686650239aSTrond Myklebust 
5696650239aSTrond Myklebust 	put_page(scratch);
5708cd51a0cSTrond Myklebust 	return status;
5718cd51a0cSTrond Myklebust }
57256e4ebf8SBryan Schumaker 
57356e4ebf8SBryan Schumaker static
574c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
57556e4ebf8SBryan Schumaker {
57656e4ebf8SBryan Schumaker 	unsigned int i;
57756e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
57856e4ebf8SBryan Schumaker 		put_page(pages[i]);
57956e4ebf8SBryan Schumaker }
58056e4ebf8SBryan Schumaker 
58156e4ebf8SBryan Schumaker /*
582bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
583bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
58456e4ebf8SBryan Schumaker  */
58556e4ebf8SBryan Schumaker static
586c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
58756e4ebf8SBryan Schumaker {
58856e4ebf8SBryan Schumaker 	unsigned int i;
58956e4ebf8SBryan Schumaker 
59056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
59156e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
59256e4ebf8SBryan Schumaker 		if (page == NULL)
59356e4ebf8SBryan Schumaker 			goto out_freepages;
59456e4ebf8SBryan Schumaker 		pages[i] = page;
59556e4ebf8SBryan Schumaker 	}
5966650239aSTrond Myklebust 	return 0;
59756e4ebf8SBryan Schumaker 
59856e4ebf8SBryan Schumaker out_freepages:
599c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6006650239aSTrond Myklebust 	return -ENOMEM;
601d1bacf9eSBryan Schumaker }
602d1bacf9eSBryan Schumaker 
603d1bacf9eSBryan Schumaker static
604d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
605d1bacf9eSBryan Schumaker {
60656e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
607d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
608d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
609d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6108cd51a0cSTrond Myklebust 	int status = -ENOMEM;
61156e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
612d1bacf9eSBryan Schumaker 
613d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6140aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
615d1bacf9eSBryan Schumaker 	entry.eof = 0;
616d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
617d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
618573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
619d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
620d1bacf9eSBryan Schumaker 		goto out;
621d1bacf9eSBryan Schumaker 
62214c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
62314c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
62414c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
62514c43f76SDavid Quigley 		goto out;
62614c43f76SDavid Quigley 	}
62714c43f76SDavid Quigley 
6280795bf83SFabian Frederick 	array = kmap(page);
629d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
630d1bacf9eSBryan Schumaker 	array->eof_index = -1;
631d1bacf9eSBryan Schumaker 
632c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
6336650239aSTrond Myklebust 	if (status < 0)
634d1bacf9eSBryan Schumaker 		goto out_release_array;
635d1bacf9eSBryan Schumaker 	do {
636ac396128STrond Myklebust 		unsigned int pglen;
63756e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
638babddc72SBryan Schumaker 
639d1bacf9eSBryan Schumaker 		if (status < 0)
640d1bacf9eSBryan Schumaker 			break;
641ac396128STrond Myklebust 		pglen = status;
6426650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6438cd51a0cSTrond Myklebust 		if (status < 0) {
6448cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6458cd51a0cSTrond Myklebust 				status = 0;
6468cd51a0cSTrond Myklebust 			break;
6478cd51a0cSTrond Myklebust 		}
6488cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
649d1bacf9eSBryan Schumaker 
650c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
651d1bacf9eSBryan Schumaker out_release_array:
6520795bf83SFabian Frederick 	kunmap(page);
65314c43f76SDavid Quigley 	nfs4_label_free(entry.label);
654d1bacf9eSBryan Schumaker out:
655d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
656d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
657d1bacf9eSBryan Schumaker 	return status;
658d1bacf9eSBryan Schumaker }
659d1bacf9eSBryan Schumaker 
660d1bacf9eSBryan Schumaker /*
661d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
662d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
663d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
664d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6651da177e4SLinus Torvalds  */
666d1bacf9eSBryan Schumaker static
667a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page)
668d1bacf9eSBryan Schumaker {
669a46126ccSChristoph Hellwig 	nfs_readdir_descriptor_t *desc = data;
670496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6718cd51a0cSTrond Myklebust 	int ret;
672d1bacf9eSBryan Schumaker 
6738cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6748cd51a0cSTrond Myklebust 	if (ret < 0)
675d1bacf9eSBryan Schumaker 		goto error;
676d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
677d1bacf9eSBryan Schumaker 
6782aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
679cd9ae2b6STrond Myklebust 		/* Should never happen */
680cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
681cd9ae2b6STrond Myklebust 	}
6821da177e4SLinus Torvalds 	unlock_page(page);
6831da177e4SLinus Torvalds 	return 0;
6841da177e4SLinus Torvalds  error:
6851da177e4SLinus Torvalds 	unlock_page(page);
6868cd51a0cSTrond Myklebust 	return ret;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
689d1bacf9eSBryan Schumaker static
690d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6911da177e4SLinus Torvalds {
692b044f645SBenjamin Coddington 	if (!desc->page->mapping)
69311de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
69409cbfeafSKirill A. Shutemov 	put_page(desc->page);
6951da177e4SLinus Torvalds 	desc->page = NULL;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698d1bacf9eSBryan Schumaker static
699d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7001da177e4SLinus Torvalds {
701a46126ccSChristoph Hellwig 	return read_cache_page(desc->file->f_mapping, desc->page_index,
702a46126ccSChristoph Hellwig 			nfs_readdir_filler, desc);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds /*
706d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7071da177e4SLinus Torvalds  */
708d1bacf9eSBryan Schumaker static
709d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
710d1bacf9eSBryan Schumaker {
711d1bacf9eSBryan Schumaker 	int res;
712d1bacf9eSBryan Schumaker 
713d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
714d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
715d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
716d1bacf9eSBryan Schumaker 
717d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
71847c716cbSTrond Myklebust 	if (res != 0)
719d1bacf9eSBryan Schumaker 		cache_page_release(desc);
720d1bacf9eSBryan Schumaker 	return res;
721d1bacf9eSBryan Schumaker }
722d1bacf9eSBryan Schumaker 
723d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7241da177e4SLinus Torvalds static inline
7251da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7261da177e4SLinus Torvalds {
7278cd51a0cSTrond Myklebust 	int res;
728d1bacf9eSBryan Schumaker 
7290aded708STrond Myklebust 	if (desc->page_index == 0) {
7308cd51a0cSTrond Myklebust 		desc->current_index = 0;
7310aded708STrond Myklebust 		desc->last_cookie = 0;
7320aded708STrond Myklebust 	}
73347c716cbSTrond Myklebust 	do {
734d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
73547c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7361da177e4SLinus Torvalds 	return res;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds /*
7401da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7411da177e4SLinus Torvalds  */
7421da177e4SLinus Torvalds static
74323db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	struct file	*file = desc->file;
746d1bacf9eSBryan Schumaker 	int i = 0;
747d1bacf9eSBryan Schumaker 	int res = 0;
748d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7498ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7508ef2ce3eSBryan Schumaker 
7510795bf83SFabian Frederick 	array = kmap(desc->page);
752d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
753ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7541da177e4SLinus Torvalds 
755ece0b423STrond Myklebust 		ent = &array->array[i];
75623db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
75723db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
7586089dd0dSThomas Meyer 			desc->eof = true;
7591da177e4SLinus Torvalds 			break;
760ece0b423STrond Myklebust 		}
76123db8620SAl Viro 		desc->ctx->pos++;
762d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
763d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
764d1bacf9eSBryan Schumaker 		else
765d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7660c030806STrond Myklebust 		if (ctx->duped != 0)
7670c030806STrond Myklebust 			ctx->duped = 1;
7688cd51a0cSTrond Myklebust 	}
76947c716cbSTrond Myklebust 	if (array->eof_index >= 0)
7706089dd0dSThomas Meyer 		desc->eof = true;
771d1bacf9eSBryan Schumaker 
7720795bf83SFabian Frederick 	kunmap(desc->page);
773d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7741e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7751e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7761da177e4SLinus Torvalds 	return res;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds /*
7801da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7811da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7821da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7831da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7841da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7851da177e4SLinus Torvalds  *
7861da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7871da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7881da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7891da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7901da177e4SLinus Torvalds  */
7911da177e4SLinus Torvalds static inline
79223db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	struct page	*page = NULL;
7951da177e4SLinus Torvalds 	int		status;
796496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
7970c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
7981da177e4SLinus Torvalds 
7991e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8001e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8031da177e4SLinus Torvalds 	if (!page) {
8041da177e4SLinus Torvalds 		status = -ENOMEM;
8051da177e4SLinus Torvalds 		goto out;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
8087a8e1dc3STrond Myklebust 	desc->page_index = 0;
8090aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8107a8e1dc3STrond Myklebust 	desc->page = page;
8110c030806STrond Myklebust 	ctx->duped = 0;
8127a8e1dc3STrond Myklebust 
81385f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
81485f8607eSTrond Myklebust 	if (status < 0)
815d1bacf9eSBryan Schumaker 		goto out_release;
816d1bacf9eSBryan Schumaker 
81723db8620SAl Viro 	status = nfs_do_filldir(desc);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds  out:
8201e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8213110ff80SHarvey Harrison 			__func__, status);
8221da177e4SLinus Torvalds 	return status;
8231da177e4SLinus Torvalds  out_release:
824d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8251da177e4SLinus Torvalds 	goto out;
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
82800a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
82900a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
83000a92642SOlivier Galibert    whole directory.
8311da177e4SLinus Torvalds  */
83223db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8331da177e4SLinus Torvalds {
834be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
8352b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
8361da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8371da177e4SLinus Torvalds 			*desc = &my_desc;
83823db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
83907b5ce8eSScott Mayhew 	int res = 0;
8401da177e4SLinus Torvalds 
8416de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
8426de1472fSAl Viro 			file, (long long)ctx->pos);
84391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
84491d5b470SChuck Lever 
8451da177e4SLinus Torvalds 	/*
84623db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
847f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
84800a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
84900a92642SOlivier Galibert 	 * revalidate the cookie.
8501da177e4SLinus Torvalds 	 */
8511da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8521da177e4SLinus Torvalds 
85323db8620SAl Viro 	desc->file = file;
85423db8620SAl Viro 	desc->ctx = ctx;
855480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8561da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
857a7a3b1e9SBenjamin Coddington 	desc->plus = nfs_use_readdirplus(inode, ctx);
8581da177e4SLinus Torvalds 
85979f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
86023db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
861fccca7fcSTrond Myklebust 	if (res < 0)
862fccca7fcSTrond Myklebust 		goto out;
863fccca7fcSTrond Myklebust 
86447c716cbSTrond Myklebust 	do {
8651da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
86600a92642SOlivier Galibert 
8671da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
868ece0b423STrond Myklebust 			res = 0;
8691da177e4SLinus Torvalds 			/* This means either end of directory */
8706089dd0dSThomas Meyer 			if (*desc->dir_cookie && !desc->eof) {
8711da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
87223db8620SAl Viro 				res = uncached_readdir(desc);
873ece0b423STrond Myklebust 				if (res == 0)
8741da177e4SLinus Torvalds 					continue;
8751da177e4SLinus Torvalds 			}
8761da177e4SLinus Torvalds 			break;
8771da177e4SLinus Torvalds 		}
8781da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8793a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8801da177e4SLinus Torvalds 			nfs_zap_caches(inode);
881baf57a09STrond Myklebust 			desc->page_index = 0;
882a7a3b1e9SBenjamin Coddington 			desc->plus = false;
883a7a3b1e9SBenjamin Coddington 			desc->eof = false;
8841da177e4SLinus Torvalds 			continue;
8851da177e4SLinus Torvalds 		}
8861da177e4SLinus Torvalds 		if (res < 0)
8871da177e4SLinus Torvalds 			break;
8881da177e4SLinus Torvalds 
88923db8620SAl Viro 		res = nfs_do_filldir(desc);
890ece0b423STrond Myklebust 		if (res < 0)
8911da177e4SLinus Torvalds 			break;
89247c716cbSTrond Myklebust 	} while (!desc->eof);
893fccca7fcSTrond Myklebust out:
8941e7cb3dcSChuck Lever 	if (res > 0)
8951e7cb3dcSChuck Lever 		res = 0;
8966de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
8971da177e4SLinus Torvalds 	return res;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
900965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
901f0dd2136STrond Myklebust {
902b044f645SBenjamin Coddington 	struct inode *inode = file_inode(filp);
903480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
904b84e06c5SChuck Lever 
9056de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9066de1472fSAl Viro 			filp, offset, whence);
907b84e06c5SChuck Lever 
908965c8e59SAndrew Morton 	switch (whence) {
909f0dd2136STrond Myklebust 	default:
910b2b1ff3dSTrond Myklebust 		return -EINVAL;
911b2b1ff3dSTrond Myklebust 	case SEEK_SET:
912b2b1ff3dSTrond Myklebust 		if (offset < 0)
913b2b1ff3dSTrond Myklebust 			return -EINVAL;
914b2b1ff3dSTrond Myklebust 		inode_lock(inode);
915b2b1ff3dSTrond Myklebust 		break;
916b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
917b2b1ff3dSTrond Myklebust 		if (offset == 0)
918b2b1ff3dSTrond Myklebust 			return filp->f_pos;
919b2b1ff3dSTrond Myklebust 		inode_lock(inode);
920b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
921b2b1ff3dSTrond Myklebust 		if (offset < 0) {
922b2b1ff3dSTrond Myklebust 			inode_unlock(inode);
923b2b1ff3dSTrond Myklebust 			return -EINVAL;
924b2b1ff3dSTrond Myklebust 		}
925f0dd2136STrond Myklebust 	}
926f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
927f0dd2136STrond Myklebust 		filp->f_pos = offset;
928480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9298ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
930f0dd2136STrond Myklebust 	}
931b044f645SBenjamin Coddington 	inode_unlock(inode);
932f0dd2136STrond Myklebust 	return offset;
933f0dd2136STrond Myklebust }
934f0dd2136STrond Myklebust 
9351da177e4SLinus Torvalds /*
9361da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9371da177e4SLinus Torvalds  * is a dummy operation.
9381da177e4SLinus Torvalds  */
93902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
94002c24a82SJosef Bacik 			 int datasync)
9411da177e4SLinus Torvalds {
9426de1472fSAl Viro 	struct inode *inode = file_inode(filp);
9437ea80859SChristoph Hellwig 
9446de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
9451e7cb3dcSChuck Lever 
9465955102cSAl Viro 	inode_lock(inode);
9476de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
9485955102cSAl Viro 	inode_unlock(inode);
9491da177e4SLinus Torvalds 	return 0;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
952bfc69a45STrond Myklebust /**
953bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
954302fad7bSTrond Myklebust  * @dir: pointer to directory inode
955bfc69a45STrond Myklebust  *
956bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
957bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
958bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
959bfc69a45STrond Myklebust  *
960bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
961bfc69a45STrond Myklebust  */
962bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
963bfc69a45STrond Myklebust {
964011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
965bfc69a45STrond Myklebust }
96689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
967bfc69a45STrond Myklebust 
9681da177e4SLinus Torvalds /*
9691da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9701da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9711da177e4SLinus Torvalds  * and may need to be looked up again.
972912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
9731da177e4SLinus Torvalds  */
974912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
975912a108dSNeilBrown 			      int rcu_walk)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9781da177e4SLinus Torvalds 		return 1;
9794eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9804eec952eSTrond Myklebust 		return 0;
981f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9826ecc5e8fSTrond Myklebust 		return 0;
983f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
9841cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
985912a108dSNeilBrown 		if (rcu_walk)
986f2c77f4eSTrond Myklebust 			return 0;
9871cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
9881cd9cb05STrond Myklebust 			return 0;
9891cd9cb05STrond Myklebust 	}
990f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
991f2c77f4eSTrond Myklebust 		return 0;
992f2c77f4eSTrond Myklebust 	return 1;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
996a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
997a12802caSTrond Myklebust  * an O_EXCL create using this path component.
998a12802caSTrond Myklebust  */
999fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1000a12802caSTrond Myklebust {
1001a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1002a12802caSTrond Myklebust 		return 0;
1003fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1004a12802caSTrond Myklebust }
1005a12802caSTrond Myklebust 
1006a12802caSTrond Myklebust /*
10071d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10081d6757fbSTrond Myklebust  *
10091d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10101d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10111d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10121d6757fbSTrond Myklebust  *
10131d6757fbSTrond Myklebust  */
101465a0c149STrond Myklebust static
1015fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10161da177e4SLinus Torvalds {
10171da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
101865a0c149STrond Myklebust 	int ret;
10191da177e4SLinus Torvalds 
102036d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10214e99a1ffSTrond Myklebust 		return 0;
102247921921STrond Myklebust 
102347921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
102447921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
102547921921STrond Myklebust 		case S_IFREG:
102647921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
102747921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
102847921921STrond Myklebust 				goto out;
102947921921STrond Myklebust 			/* Fallthrough */
103047921921STrond Myklebust 		case S_IFDIR:
103147921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
103247921921STrond Myklebust 				break;
103347921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
103447921921STrond Myklebust 			goto out_force;
103547921921STrond Myklebust 		}
103647921921STrond Myklebust 	}
103747921921STrond Myklebust 
10381da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1039fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10401da177e4SLinus Torvalds 		goto out_force;
104165a0c149STrond Myklebust out:
1042a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
10431da177e4SLinus Torvalds out_force:
10441fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
10451fa1e384SNeilBrown 		return -ECHILD;
104665a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
104765a0c149STrond Myklebust 	if (ret != 0)
104865a0c149STrond Myklebust 		return ret;
104965a0c149STrond Myklebust 	goto out;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
10531da177e4SLinus Torvalds  * We judge how long we want to trust negative
10541da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10551da177e4SLinus Torvalds  *
10561da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10571da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1058912a108dSNeilBrown  *
1059912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1060912a108dSNeilBrown  * suggesting a reval is needed.
10619f6d44d4STrond Myklebust  *
10629f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
10639f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
10641da177e4SLinus Torvalds  */
10651da177e4SLinus Torvalds static inline
10661da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1067fa3c56bbSAl Viro 		       unsigned int flags)
10681da177e4SLinus Torvalds {
10699f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
10701da177e4SLinus Torvalds 		return 0;
10714eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10724eec952eSTrond Myklebust 		return 1;
1073912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10765ceb9d7fSTrond Myklebust static int
10775ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
10785ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
10791da177e4SLinus Torvalds {
10805ceb9d7fSTrond Myklebust 	switch (error) {
10815ceb9d7fSTrond Myklebust 	case 1:
10826de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
10836de1472fSAl Viro 			__func__, dentry);
10841da177e4SLinus Torvalds 		return 1;
10855ceb9d7fSTrond Myklebust 	case 0:
1086a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
10871da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
10881da177e4SLinus Torvalds 			/* Purge readdir caches. */
10891da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1090a3f432bfSJ. Bruce Fields 			/*
1091a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1092a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1093a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1094a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1095a3f432bfSJ. Bruce Fields 			 */
1096a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
10975ceb9d7fSTrond Myklebust 				return 1;
10981da177e4SLinus Torvalds 		}
10996de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11006de1472fSAl Viro 				__func__, dentry);
11011da177e4SLinus Torvalds 		return 0;
11025ceb9d7fSTrond Myklebust 	}
11036de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
11046de1472fSAl Viro 				__func__, dentry, error);
1105e1fb4d05STrond Myklebust 	return error;
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
11085ceb9d7fSTrond Myklebust static int
11095ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
11105ceb9d7fSTrond Myklebust 			       unsigned int flags)
11115ceb9d7fSTrond Myklebust {
11125ceb9d7fSTrond Myklebust 	int ret = 1;
11135ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
11145ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
11155ceb9d7fSTrond Myklebust 			return -ECHILD;
11165ceb9d7fSTrond Myklebust 		ret = 0;
11175ceb9d7fSTrond Myklebust 	}
11185ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
11195ceb9d7fSTrond Myklebust }
11205ceb9d7fSTrond Myklebust 
11215ceb9d7fSTrond Myklebust static int
11225ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
11235ceb9d7fSTrond Myklebust 				struct inode *inode)
11245ceb9d7fSTrond Myklebust {
11255ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11265ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
11275ceb9d7fSTrond Myklebust }
11285ceb9d7fSTrond Myklebust 
11295ceb9d7fSTrond Myklebust static int
11305ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
11315ceb9d7fSTrond Myklebust 			     struct inode *inode)
11325ceb9d7fSTrond Myklebust {
11335ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
11345ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
11355ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
11365ceb9d7fSTrond Myklebust 	int ret;
11375ceb9d7fSTrond Myklebust 
11385ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
11395ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
11405ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
11415ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
11425ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
11435ceb9d7fSTrond Myklebust 		goto out;
11445ceb9d7fSTrond Myklebust 
1145f7b37b8bSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
11465ceb9d7fSTrond Myklebust 	if (ret < 0) {
1147f7b37b8bSTrond Myklebust 		switch (ret) {
1148f7b37b8bSTrond Myklebust 		case -ESTALE:
1149f7b37b8bSTrond Myklebust 		case -ENOENT:
11505ceb9d7fSTrond Myklebust 			ret = 0;
1151f7b37b8bSTrond Myklebust 			break;
1152f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1153f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1154f7b37b8bSTrond Myklebust 				ret = 1;
1155f7b37b8bSTrond Myklebust 		}
11565ceb9d7fSTrond Myklebust 		goto out;
11575ceb9d7fSTrond Myklebust 	}
11585ceb9d7fSTrond Myklebust 	ret = 0;
11595ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11605ceb9d7fSTrond Myklebust 		goto out;
11615ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
11625ceb9d7fSTrond Myklebust 		goto out;
11635ceb9d7fSTrond Myklebust 
11645ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
11655ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11665ceb9d7fSTrond Myklebust 
11675ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
11685ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
11695ceb9d7fSTrond Myklebust 	ret = 1;
11705ceb9d7fSTrond Myklebust out:
11715ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
11725ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
11735ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
11745ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
11755ceb9d7fSTrond Myklebust }
11765ceb9d7fSTrond Myklebust 
11775ceb9d7fSTrond Myklebust /*
11785ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
11795ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
11805ceb9d7fSTrond Myklebust  * lookup.
11815ceb9d7fSTrond Myklebust  *
11825ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
11835ceb9d7fSTrond Myklebust  * we have an inode!
11845ceb9d7fSTrond Myklebust  *
11855ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
11865ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
11875ceb9d7fSTrond Myklebust  */
11885ceb9d7fSTrond Myklebust static int
11895ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
11905ceb9d7fSTrond Myklebust 			 unsigned int flags)
11915ceb9d7fSTrond Myklebust {
11925ceb9d7fSTrond Myklebust 	struct inode *inode;
11935ceb9d7fSTrond Myklebust 	int error;
11945ceb9d7fSTrond Myklebust 
11955ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
11965ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
11975ceb9d7fSTrond Myklebust 
11985ceb9d7fSTrond Myklebust 	if (!inode)
11995ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
12005ceb9d7fSTrond Myklebust 
12015ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
12025ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12035ceb9d7fSTrond Myklebust 				__func__, dentry);
12045ceb9d7fSTrond Myklebust 		goto out_bad;
12055ceb9d7fSTrond Myklebust 	}
12065ceb9d7fSTrond Myklebust 
12075ceb9d7fSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
12085ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
12095ceb9d7fSTrond Myklebust 
12105ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
12115ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
12125ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
12135ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
12145ceb9d7fSTrond Myklebust 		if (error) {
12155ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
12165ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
12175ceb9d7fSTrond Myklebust 			goto out_bad;
12185ceb9d7fSTrond Myklebust 		}
12195ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
12205ceb9d7fSTrond Myklebust 		goto out_valid;
12215ceb9d7fSTrond Myklebust 	}
12225ceb9d7fSTrond Myklebust 
12235ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
12245ceb9d7fSTrond Myklebust 		return -ECHILD;
12255ceb9d7fSTrond Myklebust 
12265ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
12275ceb9d7fSTrond Myklebust 		goto out_bad;
12285ceb9d7fSTrond Myklebust 
12295ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
12305ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
12315ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
12325ceb9d7fSTrond Myklebust 	return error;
12335ceb9d7fSTrond Myklebust out_valid:
12345ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
12355ceb9d7fSTrond Myklebust out_bad:
12365ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
12375ceb9d7fSTrond Myklebust 		return -ECHILD;
12385ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
12395ceb9d7fSTrond Myklebust }
12405ceb9d7fSTrond Myklebust 
12415ceb9d7fSTrond Myklebust static int
1242c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1243c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
12445ceb9d7fSTrond Myklebust {
12455ceb9d7fSTrond Myklebust 	struct dentry *parent;
12465ceb9d7fSTrond Myklebust 	struct inode *dir;
12475ceb9d7fSTrond Myklebust 	int ret;
12485ceb9d7fSTrond Myklebust 
12495ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
12505ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
12515ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
12525ceb9d7fSTrond Myklebust 		if (!dir)
12535ceb9d7fSTrond Myklebust 			return -ECHILD;
1254c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
12555ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
12565ceb9d7fSTrond Myklebust 			return -ECHILD;
12575ceb9d7fSTrond Myklebust 	} else {
12585ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1259c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
12605ceb9d7fSTrond Myklebust 		dput(parent);
12615ceb9d7fSTrond Myklebust 	}
12625ceb9d7fSTrond Myklebust 	return ret;
12635ceb9d7fSTrond Myklebust }
12645ceb9d7fSTrond Myklebust 
1265c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1266c7944ebbSTrond Myklebust {
1267c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1268c7944ebbSTrond Myklebust }
1269c7944ebbSTrond Myklebust 
12701da177e4SLinus Torvalds /*
12712b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1272ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1273ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1274ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1275ecf3d1f1SJeff Layton  *
1276ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1277ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1278ecf3d1f1SJeff Layton  */
1279ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1280ecf3d1f1SJeff Layton {
12812b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
12829cdd1d3fSTrond Myklebust 	int error = 0;
1283ecf3d1f1SJeff Layton 
1284ecf3d1f1SJeff Layton 	/*
1285ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1286ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1287ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1288ecf3d1f1SJeff Layton 	 */
1289ecf3d1f1SJeff Layton 	if (!inode) {
12906de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
12916de1472fSAl Viro 				__func__, dentry);
1292ecf3d1f1SJeff Layton 		return 1;
1293ecf3d1f1SJeff Layton 	}
1294ecf3d1f1SJeff Layton 
1295ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
12966de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12976de1472fSAl Viro 				__func__, dentry);
1298ecf3d1f1SJeff Layton 		return 0;
1299ecf3d1f1SJeff Layton 	}
1300ecf3d1f1SJeff Layton 
1301b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1302ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1303ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1304ecf3d1f1SJeff Layton 	return !error;
1305ecf3d1f1SJeff Layton }
1306ecf3d1f1SJeff Layton 
1307ecf3d1f1SJeff Layton /*
13081da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
13091da177e4SLinus Torvalds  */
1310fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
13111da177e4SLinus Torvalds {
13126de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
13136de1472fSAl Viro 		dentry, dentry->d_flags);
13141da177e4SLinus Torvalds 
131577f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
13162b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
131777f11192STrond Myklebust 		return 1;
131877f11192STrond Myklebust 
13191da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
13201da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
13211da177e4SLinus Torvalds 		return 1;
13221da177e4SLinus Torvalds 	}
13231751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
13241da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
13251da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
13261da177e4SLinus Torvalds 		return 1;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 	return 0;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
13321f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
13331b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
13341b83d707STrond Myklebust {
13351b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
13361f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
133759a707b0STrond Myklebust 	if (inode->i_nlink > 0)
133859a707b0STrond Myklebust 		drop_nlink(inode);
133959a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
134016e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
134116e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
134259a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
134359a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
13441b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
13451b83d707STrond Myklebust }
13461b83d707STrond Myklebust 
13471da177e4SLinus Torvalds /*
13481da177e4SLinus Torvalds  * Called when the dentry loses inode.
13491da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
13501da177e4SLinus Torvalds  */
13511da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
13521da177e4SLinus Torvalds {
135383672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
135483672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
135583672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
135683672d39SNeil Brown 
13571da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1358e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
13591f018458STrond Myklebust 		nfs_drop_nlink(inode);
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 	iput(inode);
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
1364b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1365b1942c5fSAl Viro {
1366b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1367b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1368b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1369b1942c5fSAl Viro 			WARN_ON(1);
1370b1942c5fSAl Viro 		else
1371b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1372b1942c5fSAl Viro 	}
1373b1942c5fSAl Viro }
1374b1942c5fSAl Viro 
1375f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
13761da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1377ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
13781da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13791da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
138036d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1381b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13821da177e4SLinus Torvalds };
1383ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
13841da177e4SLinus Torvalds 
1385597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds 	struct dentry *res;
13881da177e4SLinus Torvalds 	struct inode *inode = NULL;
1389e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1390e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
13911775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
13921da177e4SLinus Torvalds 	int error;
13931da177e4SLinus Torvalds 
13946de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
139591d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
13961da177e4SLinus Torvalds 
1397130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1398130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
13991da177e4SLinus Torvalds 
1400fd684071STrond Myklebust 	/*
1401fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1402fd684071STrond Myklebust 	 * but don't hash the dentry.
1403fd684071STrond Myklebust 	 */
14049f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1405130f9ab7SAl Viro 		return NULL;
14061da177e4SLinus Torvalds 
1407e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1408e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1409e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1410e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1411e1fb4d05STrond Myklebust 		goto out;
1412e1fb4d05STrond Myklebust 
141314c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
141414c43f76SDavid Quigley 	if (IS_ERR(label))
141514c43f76SDavid Quigley 		goto out;
141614c43f76SDavid Quigley 
14176e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1418f7b37b8bSTrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
14191da177e4SLinus Torvalds 	if (error == -ENOENT)
14201da177e4SLinus Torvalds 		goto no_entry;
14211da177e4SLinus Torvalds 	if (error < 0) {
14221da177e4SLinus Torvalds 		res = ERR_PTR(error);
1423bf130914SAl Viro 		goto out_label;
14241da177e4SLinus Torvalds 	}
14251775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1426bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
142703f28e3aSTrond Myklebust 	if (IS_ERR(res))
1428bf130914SAl Viro 		goto out_label;
142954ceac45SDavid Howells 
143063519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
143163519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1432d69ee9b8STrond Myklebust 
14331da177e4SLinus Torvalds no_entry:
143441d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
14359eaef27bSTrond Myklebust 	if (res != NULL) {
14369eaef27bSTrond Myklebust 		if (IS_ERR(res))
1437bf130914SAl Viro 			goto out_label;
14381da177e4SLinus Torvalds 		dentry = res;
14399eaef27bSTrond Myklebust 	}
14401da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1441bf130914SAl Viro out_label:
14426e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
144314c43f76SDavid Quigley 	nfs4_label_free(label);
14441da177e4SLinus Torvalds out:
1445e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1446e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
14471da177e4SLinus Torvalds 	return res;
14481da177e4SLinus Torvalds }
1449ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
14501da177e4SLinus Torvalds 
145189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
14520b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
14531da177e4SLinus Torvalds 
1454f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
14550ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1456b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
14571da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
14581da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
145936d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1460b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14611da177e4SLinus Torvalds };
146289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
14631da177e4SLinus Torvalds 
14648a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
14658a5e929dSAl Viro {
14668a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
14678a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
14688a5e929dSAl Viro 		res |= FMODE_READ;
14698a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
14708a5e929dSAl Viro 		res |= FMODE_WRITE;
14718a5e929dSAl Viro 	return res;
14728a5e929dSAl Viro }
14738a5e929dSAl Viro 
1474532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1475cd9a1c0eSTrond Myklebust {
1476532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1477cd9a1c0eSTrond Myklebust }
1478cd9a1c0eSTrond Myklebust 
1479cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1480cd9a1c0eSTrond Myklebust {
1481f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1482cd9a1c0eSTrond Myklebust 	return 0;
1483cd9a1c0eSTrond Myklebust }
1484cd9a1c0eSTrond Myklebust 
1485d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
14860dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1487b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1488cd9a1c0eSTrond Myklebust {
14890dd2b474SMiklos Szeredi 	int err;
14900dd2b474SMiklos Szeredi 
1491be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
149230d90494SAl Viro 	if (err)
1493d9585277SAl Viro 		goto out;
1494eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
149530d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1496eaa2b82cSNeilBrown 	else
14979821421aSTrond Myklebust 		err = -EOPENSTALE;
1498cd9a1c0eSTrond Myklebust out:
1499d9585277SAl Viro 	return err;
1500cd9a1c0eSTrond Myklebust }
1501cd9a1c0eSTrond Myklebust 
150273a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
150330d90494SAl Viro 		    struct file *file, unsigned open_flags,
150444907d79SAl Viro 		    umode_t mode)
15051da177e4SLinus Torvalds {
1506c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1507cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
15080dd2b474SMiklos Szeredi 	struct dentry *res;
15090dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1510f46e0bd3STrond Myklebust 	struct inode *inode;
15111472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1512c94c0953SAl Viro 	bool switched = false;
151373a09dd9SAl Viro 	int created = 0;
1514898f635cSTrond Myklebust 	int err;
15151da177e4SLinus Torvalds 
15160dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
15172b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
15180dd2b474SMiklos Szeredi 
15191e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
15206de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
15211e7cb3dcSChuck Lever 
15229597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
15239597c13bSJeff Layton 	if (err)
15249597c13bSJeff Layton 		return err;
15259597c13bSJeff Layton 
15260dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
15270dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
152800699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
15290dd2b474SMiklos Szeredi 			/*
15300dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
15310dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
15320dd2b474SMiklos Szeredi 			 * again
15330dd2b474SMiklos Szeredi 			 */
1534d9585277SAl Viro 			return -ENOENT;
15350dd2b474SMiklos Szeredi 		}
15361472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
15371da177e4SLinus Torvalds 		goto no_open;
15381da177e4SLinus Torvalds 	}
15391da177e4SLinus Torvalds 
15400dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1541d9585277SAl Viro 		return -ENAMETOOLONG;
15421da177e4SLinus Torvalds 
15430dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1544dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1545dff25ddbSAndreas Gruenbacher 
1546dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1547dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1548dff25ddbSAndreas Gruenbacher 
1549536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1550dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
15510dd2b474SMiklos Szeredi 	}
1552536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1553536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1554536e43d1STrond Myklebust 		attr.ia_size = 0;
1555cd9a1c0eSTrond Myklebust 	}
1556cd9a1c0eSTrond Myklebust 
1557c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1558c94c0953SAl Viro 		d_drop(dentry);
1559c94c0953SAl Viro 		switched = true;
1560c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1561c94c0953SAl Viro 					  &dentry->d_name, &wq);
1562c94c0953SAl Viro 		if (IS_ERR(dentry))
1563c94c0953SAl Viro 			return PTR_ERR(dentry);
1564c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1565c94c0953SAl Viro 			return finish_no_open(file, dentry);
1566c94c0953SAl Viro 	}
1567c94c0953SAl Viro 
1568532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
15690dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
15700dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1571d9585277SAl Viro 		goto out;
15720dd2b474SMiklos Szeredi 
15736e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
157473a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
157573a09dd9SAl Viro 	if (created)
157673a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1577275bb307STrond Myklebust 	if (IS_ERR(inode)) {
15780dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
15796e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15802d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1581d20cb71dSAl Viro 		d_drop(dentry);
15820dd2b474SMiklos Szeredi 		switch (err) {
15831da177e4SLinus Torvalds 		case -ENOENT:
1584774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1585809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15860dd2b474SMiklos Szeredi 			break;
15871788ea6eSJeff Layton 		case -EISDIR:
15886f926b5bSTrond Myklebust 		case -ENOTDIR:
15896f926b5bSTrond Myklebust 			goto no_open;
15901da177e4SLinus Torvalds 		case -ELOOP:
15910dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
15921da177e4SLinus Torvalds 				goto no_open;
15930dd2b474SMiklos Szeredi 			break;
15941da177e4SLinus Torvalds 			/* case -EINVAL: */
15951da177e4SLinus Torvalds 		default:
15960dd2b474SMiklos Szeredi 			break;
15971da177e4SLinus Torvalds 		}
15981da177e4SLinus Torvalds 		goto out;
15991da177e4SLinus Torvalds 	}
16000dd2b474SMiklos Szeredi 
1601b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
16026e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16032d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1604d9585277SAl Viro out:
1605c94c0953SAl Viro 	if (unlikely(switched)) {
1606c94c0953SAl Viro 		d_lookup_done(dentry);
1607c94c0953SAl Viro 		dput(dentry);
1608c94c0953SAl Viro 	}
1609d9585277SAl Viro 	return err;
16100dd2b474SMiklos Szeredi 
16111da177e4SLinus Torvalds no_open:
16121472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1613c94c0953SAl Viro 	if (switched) {
1614c94c0953SAl Viro 		d_lookup_done(dentry);
1615c94c0953SAl Viro 		if (!res)
1616c94c0953SAl Viro 			res = dentry;
1617c94c0953SAl Viro 		else
1618c94c0953SAl Viro 			dput(dentry);
1619c94c0953SAl Viro 	}
16200dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1621c94c0953SAl Viro 		return PTR_ERR(res);
1622e45198a6SAl Viro 	return finish_no_open(file, res);
16231da177e4SLinus Torvalds }
162489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
16251da177e4SLinus Torvalds 
1626c7944ebbSTrond Myklebust static int
1627c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1628c7944ebbSTrond Myklebust 			  unsigned int flags)
16291da177e4SLinus Torvalds {
1630657e94b6SNick Piggin 	struct inode *inode;
16311da177e4SLinus Torvalds 
1632fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1633c7944ebbSTrond Myklebust 		goto full_reval;
1634eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1635c7944ebbSTrond Myklebust 		goto full_reval;
16362b484297STrond Myklebust 
16372b0143b5SDavid Howells 	inode = d_inode(dentry);
16382b484297STrond Myklebust 
16391da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
16401da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
16411da177e4SLinus Torvalds 	 */
1642c7944ebbSTrond Myklebust 	if (inode == NULL)
1643c7944ebbSTrond Myklebust 		goto full_reval;
164449317a7fSNeilBrown 
1645c7944ebbSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1646c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1647216d5d06STrond Myklebust 
16481da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
16491da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1650c7944ebbSTrond Myklebust 		goto full_reval;
1651c7944ebbSTrond Myklebust 
16521da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1653c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1654c7944ebbSTrond Myklebust 		goto reval_dentry;
1655c7944ebbSTrond Myklebust 
1656c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1657c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1658c7944ebbSTrond Myklebust 		goto reval_dentry;
16591da177e4SLinus Torvalds 
16600ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1661c7944ebbSTrond Myklebust 	return 1;
1662c7944ebbSTrond Myklebust reval_dentry:
1663c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1664c7944ebbSTrond Myklebust 		return -ECHILD;
166542f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
16660ef97dcfSMiklos Szeredi 
1667c7944ebbSTrond Myklebust full_reval:
1668c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1669c7944ebbSTrond Myklebust }
1670535918f1STrond Myklebust 
1671c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1672c7944ebbSTrond Myklebust {
1673c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1674c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1675c0204fd2STrond Myklebust }
1676c0204fd2STrond Myklebust 
16771da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
16781da177e4SLinus Torvalds 
1679406cd915SBenjamin Coddington struct dentry *
1680406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
16811775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
16821775fd3eSDavid Quigley 				struct nfs4_label *label)
16831da177e4SLinus Torvalds {
1684fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
16852b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
16861da177e4SLinus Torvalds 	struct inode *inode;
1687b0c6108eSAl Viro 	struct dentry *d;
1688406cd915SBenjamin Coddington 	int error;
16891da177e4SLinus Torvalds 
1690fab728e1STrond Myklebust 	d_drop(dentry);
1691fab728e1STrond Myklebust 
16921da177e4SLinus Torvalds 	if (fhandle->size == 0) {
1693f7b37b8bSTrond Myklebust 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
16941da177e4SLinus Torvalds 		if (error)
1695fab728e1STrond Myklebust 			goto out_error;
16961da177e4SLinus Torvalds 	}
16975724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16981da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
16991da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1700a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1701a841b54dSTrond Myklebust 				fattr, NULL, NULL);
17021da177e4SLinus Torvalds 		if (error < 0)
1703fab728e1STrond Myklebust 			goto out_error;
17041da177e4SLinus Torvalds 	}
17051775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1706b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1707fab728e1STrond Myklebust out:
1708fab728e1STrond Myklebust 	dput(parent);
1709406cd915SBenjamin Coddington 	return d;
1710fab728e1STrond Myklebust out_error:
1711fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1712406cd915SBenjamin Coddington 	d = ERR_PTR(error);
1713406cd915SBenjamin Coddington 	goto out;
1714406cd915SBenjamin Coddington }
1715406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1716406cd915SBenjamin Coddington 
1717406cd915SBenjamin Coddington /*
1718406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
1719406cd915SBenjamin Coddington  */
1720406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1721406cd915SBenjamin Coddington 				struct nfs_fattr *fattr,
1722406cd915SBenjamin Coddington 				struct nfs4_label *label)
1723406cd915SBenjamin Coddington {
1724406cd915SBenjamin Coddington 	struct dentry *d;
1725406cd915SBenjamin Coddington 
1726406cd915SBenjamin Coddington 	d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1727406cd915SBenjamin Coddington 	if (IS_ERR(d))
1728406cd915SBenjamin Coddington 		return PTR_ERR(d);
1729406cd915SBenjamin Coddington 
1730406cd915SBenjamin Coddington 	/* Callers don't care */
1731406cd915SBenjamin Coddington 	dput(d);
1732406cd915SBenjamin Coddington 	return 0;
17331da177e4SLinus Torvalds }
1734ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds /*
17371da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
17381da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
17391da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
17401da177e4SLinus Torvalds  * reply path made it appear to have failed.
17411da177e4SLinus Torvalds  */
1742597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1743ebfc3b49SAl Viro 		umode_t mode, bool excl)
17441da177e4SLinus Torvalds {
17451da177e4SLinus Torvalds 	struct iattr attr;
1746ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
17471da177e4SLinus Torvalds 	int error;
17481da177e4SLinus Torvalds 
17491e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
17506de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	attr.ia_mode = mode;
17531da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17541da177e4SLinus Torvalds 
17558b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
17568867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
17578b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
17581da177e4SLinus Torvalds 	if (error != 0)
17591da177e4SLinus Torvalds 		goto out_err;
17601da177e4SLinus Torvalds 	return 0;
17611da177e4SLinus Torvalds out_err:
17621da177e4SLinus Torvalds 	d_drop(dentry);
17631da177e4SLinus Torvalds 	return error;
17641da177e4SLinus Torvalds }
1765ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds /*
17681da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17691da177e4SLinus Torvalds  */
1770597d9289SBryan Schumaker int
17711a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
17721da177e4SLinus Torvalds {
17731da177e4SLinus Torvalds 	struct iattr attr;
17741da177e4SLinus Torvalds 	int status;
17751da177e4SLinus Torvalds 
17761e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
17776de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	attr.ia_mode = mode;
17801da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17811da177e4SLinus Torvalds 
17821ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
17831da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
17841ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
17851da177e4SLinus Torvalds 	if (status != 0)
17861da177e4SLinus Torvalds 		goto out_err;
17871da177e4SLinus Torvalds 	return 0;
17881da177e4SLinus Torvalds out_err:
17891da177e4SLinus Torvalds 	d_drop(dentry);
17901da177e4SLinus Torvalds 	return status;
17911da177e4SLinus Torvalds }
1792ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds /*
17951da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17961da177e4SLinus Torvalds  */
1797597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17981da177e4SLinus Torvalds {
17991da177e4SLinus Torvalds 	struct iattr attr;
18001da177e4SLinus Torvalds 	int error;
18011da177e4SLinus Torvalds 
18021e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
18036de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18061da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
18071da177e4SLinus Torvalds 
18081ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
18091da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
18101ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
18111da177e4SLinus Torvalds 	if (error != 0)
18121da177e4SLinus Torvalds 		goto out_err;
18131da177e4SLinus Torvalds 	return 0;
18141da177e4SLinus Torvalds out_err:
18151da177e4SLinus Torvalds 	d_drop(dentry);
18161da177e4SLinus Torvalds 	return error;
18171da177e4SLinus Torvalds }
1818ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
18191da177e4SLinus Torvalds 
1820d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1821d45b9d8bSTrond Myklebust {
1822dc3f4198SAl Viro 	if (simple_positive(dentry))
1823d45b9d8bSTrond Myklebust 		d_delete(dentry);
1824d45b9d8bSTrond Myklebust }
1825d45b9d8bSTrond Myklebust 
1826597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
18271da177e4SLinus Torvalds {
18281da177e4SLinus Torvalds 	int error;
18291da177e4SLinus Torvalds 
18301e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
18316de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18321da177e4SLinus Torvalds 
18331ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
18342b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1835884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
18361da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18371da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1838ba6c0592STrond Myklebust 		switch (error) {
1839ba6c0592STrond Myklebust 		case 0:
18402b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1841ba6c0592STrond Myklebust 			break;
1842ba6c0592STrond Myklebust 		case -ENOENT:
1843d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1844ba6c0592STrond Myklebust 		}
1845884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1846ba6c0592STrond Myklebust 	} else
1847ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18481ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	return error;
18511da177e4SLinus Torvalds }
1852ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*
18551da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
18561da177e4SLinus Torvalds  * and after checking that the file has only one user.
18571da177e4SLinus Torvalds  *
18581da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
18591da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
18601da177e4SLinus Torvalds  */
18611da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
18621da177e4SLinus Torvalds {
18632b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
18642b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
18651da177e4SLinus Torvalds 	int error = -EBUSY;
18661da177e4SLinus Torvalds 
18676de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
18701da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
18711da177e4SLinus Torvalds 		error = 0;
18721da177e4SLinus Torvalds 		goto out;
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds 
18751ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
18761da177e4SLinus Torvalds 	if (inode != NULL) {
1877912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
18781da177e4SLinus Torvalds 		if (error == 0)
18791b83d707STrond Myklebust 			nfs_drop_nlink(inode);
18801da177e4SLinus Torvalds 	} else
1881912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
1882d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1883d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
18841ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
18851da177e4SLinus Torvalds out:
18861da177e4SLinus Torvalds 	return error;
18871da177e4SLinus Torvalds }
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
18901da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
18911da177e4SLinus Torvalds  *
18921da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
18931da177e4SLinus Torvalds  */
1894597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	int error;
18971da177e4SLinus Torvalds 	int need_rehash = 0;
18981da177e4SLinus Torvalds 
18991e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
19006de1472fSAl Viro 		dir->i_ino, dentry);
19011da177e4SLinus Torvalds 
19021ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
19031da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
190484d08fa8SAl Viro 	if (d_count(dentry) > 1) {
19051da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1906ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
19072b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
19081da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
19091ca42382STrond Myklebust 		goto out;
19101da177e4SLinus Torvalds 	}
19111da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
19121da177e4SLinus Torvalds 		__d_drop(dentry);
19131da177e4SLinus Torvalds 		need_rehash = 1;
19141da177e4SLinus Torvalds 	}
19151da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
19161da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1917d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
19181da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
19191da177e4SLinus Torvalds 	} else if (need_rehash)
19201da177e4SLinus Torvalds 		d_rehash(dentry);
19211ca42382STrond Myklebust out:
19221ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
19231da177e4SLinus Torvalds 	return error;
19241da177e4SLinus Torvalds }
1925ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
19261da177e4SLinus Torvalds 
1927873101b3SChuck Lever /*
1928873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1929873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1930873101b3SChuck Lever  * using prepare_write/commit_write.
1931873101b3SChuck Lever  *
1932873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1933873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1934873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1935873101b3SChuck Lever  * symlink request has completed on the server.
1936873101b3SChuck Lever  *
1937873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1938873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1939873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1940873101b3SChuck Lever  * and move the raw page into its mapping.
1941873101b3SChuck Lever  */
1942597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19431da177e4SLinus Torvalds {
1944873101b3SChuck Lever 	struct page *page;
1945873101b3SChuck Lever 	char *kaddr;
19461da177e4SLinus Torvalds 	struct iattr attr;
1947873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
19481da177e4SLinus Torvalds 	int error;
19491da177e4SLinus Torvalds 
19501e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
19516de1472fSAl Viro 		dir->i_ino, dentry, symname);
19521da177e4SLinus Torvalds 
1953873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1954873101b3SChuck Lever 		return -ENAMETOOLONG;
19551da177e4SLinus Torvalds 
1956873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1957873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
19581da177e4SLinus Torvalds 
1959e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
196076566991STrond Myklebust 	if (!page)
1961873101b3SChuck Lever 		return -ENOMEM;
1962873101b3SChuck Lever 
1963e8ecde25SAl Viro 	kaddr = page_address(page);
1964873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1965873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1966873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1967873101b3SChuck Lever 
19681ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
196994a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
19701ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1971873101b3SChuck Lever 	if (error != 0) {
19721e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1973873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
19746de1472fSAl Viro 			dentry, symname, error);
19751da177e4SLinus Torvalds 		d_drop(dentry);
1976873101b3SChuck Lever 		__free_page(page);
19771da177e4SLinus Torvalds 		return error;
19781da177e4SLinus Torvalds 	}
19791da177e4SLinus Torvalds 
1980873101b3SChuck Lever 	/*
1981873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1982873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1983873101b3SChuck Lever 	 */
19842b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
1985873101b3SChuck Lever 							GFP_KERNEL)) {
1986873101b3SChuck Lever 		SetPageUptodate(page);
1987873101b3SChuck Lever 		unlock_page(page);
1988a0b54addSRafael Aquini 		/*
1989a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
1990a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
1991a0b54addSRafael Aquini 		 */
199209cbfeafSKirill A. Shutemov 		put_page(page);
1993873101b3SChuck Lever 	} else
1994873101b3SChuck Lever 		__free_page(page);
1995873101b3SChuck Lever 
1996873101b3SChuck Lever 	return 0;
1997873101b3SChuck Lever }
1998ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1999873101b3SChuck Lever 
2000597d9289SBryan Schumaker int
20011da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
20021da177e4SLinus Torvalds {
20032b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
20041da177e4SLinus Torvalds 	int error;
20051da177e4SLinus Torvalds 
20066de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
20076de1472fSAl Viro 		old_dentry, dentry);
20081da177e4SLinus Torvalds 
20091fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
20109697d234STrond Myklebust 	d_drop(dentry);
20111da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2012cf809556STrond Myklebust 	if (error == 0) {
20137de9c6eeSAl Viro 		ihold(inode);
20149697d234STrond Myklebust 		d_add(dentry, inode);
2015cf809556STrond Myklebust 	}
20161fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
20171da177e4SLinus Torvalds 	return error;
20181da177e4SLinus Torvalds }
2019ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds /*
20221da177e4SLinus Torvalds  * RENAME
20231da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
20241da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
20251da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
20261da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
20271da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
20281da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
20291da177e4SLinus Torvalds  *
20301da177e4SLinus Torvalds  * FIXED.
20311da177e4SLinus Torvalds  *
20321da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
20331da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
20341da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
20351da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
20361da177e4SLinus Torvalds  * using the inode layer
20371da177e4SLinus Torvalds  *
20381da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
20391da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
20401da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
20411da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
20421da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
20431da177e4SLinus Torvalds  * the rename.
20441da177e4SLinus Torvalds  */
2045597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
20461cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
20471cd66c93SMiklos Szeredi 	       unsigned int flags)
20481da177e4SLinus Torvalds {
20492b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
20502b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2051d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
205280a491fdSJeff Layton 	struct rpc_task *task;
20531da177e4SLinus Torvalds 	int error = -EBUSY;
20541da177e4SLinus Torvalds 
20551cd66c93SMiklos Szeredi 	if (flags)
20561cd66c93SMiklos Szeredi 		return -EINVAL;
20571cd66c93SMiklos Szeredi 
20586de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
20596de1472fSAl Viro 		 old_dentry, new_dentry,
206084d08fa8SAl Viro 		 d_count(new_dentry));
20611da177e4SLinus Torvalds 
206270ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
20631da177e4SLinus Torvalds 	/*
206428f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
206528f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
206628f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
206728f79a1aSMiklos Szeredi 	 * the new target.
20681da177e4SLinus Torvalds 	 */
206927226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
207027226104SMiklos Szeredi 		/*
207127226104SMiklos Szeredi 		 * To prevent any new references to the target during the
207227226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
207327226104SMiklos Szeredi 		 */
2074d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
207527226104SMiklos Szeredi 			d_drop(new_dentry);
2076d9f29500SBenjamin Coddington 			rehash = new_dentry;
2077d9f29500SBenjamin Coddington 		}
207827226104SMiklos Szeredi 
207984d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
20801da177e4SLinus Torvalds 			int err;
208127226104SMiklos Szeredi 
20821da177e4SLinus Torvalds 			/* copy the target dentry's name */
20831da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
20841da177e4SLinus Torvalds 					 &new_dentry->d_name);
20851da177e4SLinus Torvalds 			if (!dentry)
20861da177e4SLinus Torvalds 				goto out;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
20891da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
209024e93025SMiklos Szeredi 			if (err)
20911da177e4SLinus Torvalds 				goto out;
209224e93025SMiklos Szeredi 
209324e93025SMiklos Szeredi 			new_dentry = dentry;
2094d9f29500SBenjamin Coddington 			rehash = NULL;
209524e93025SMiklos Szeredi 			new_inode = NULL;
2096b1e4adf4STrond Myklebust 		}
209727226104SMiklos Szeredi 	}
20981da177e4SLinus Torvalds 
2099d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
210080a491fdSJeff Layton 	if (IS_ERR(task)) {
210180a491fdSJeff Layton 		error = PTR_ERR(task);
210280a491fdSJeff Layton 		goto out;
210380a491fdSJeff Layton 	}
210480a491fdSJeff Layton 
210580a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2106818a8dbeSBenjamin Coddington 	if (error != 0) {
2107818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2108818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2109818a8dbeSBenjamin Coddington 		smp_wmb();
2110818a8dbeSBenjamin Coddington 	} else
211180a491fdSJeff Layton 		error = task->tk_status;
211280a491fdSJeff Layton 	rpc_put_task(task);
211359a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
211459a707b0STrond Myklebust 	if (error == 0) {
211559a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
211659a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
211759a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
211859a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
211959a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
212059a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
212159a707b0STrond Myklebust 	}
21221da177e4SLinus Torvalds out:
2123d9f29500SBenjamin Coddington 	if (rehash)
2124d9f29500SBenjamin Coddington 		d_rehash(rehash);
212570ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
212670ded201STrond Myklebust 			new_dir, new_dentry, error);
2127d9f29500SBenjamin Coddington 	if (!error) {
2128d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2129d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2130d9f29500SBenjamin Coddington 		/*
2131d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2132d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2133d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2134d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2135d9f29500SBenjamin Coddington 		 */
2136d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2137d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2138d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2139d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2140d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2141d9f29500SBenjamin Coddington 
21421da177e4SLinus Torvalds 	/* new dentry created? */
21431da177e4SLinus Torvalds 	if (dentry)
21441da177e4SLinus Torvalds 		dput(dentry);
21451da177e4SLinus Torvalds 	return error;
21461da177e4SLinus Torvalds }
2147ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
21481da177e4SLinus Torvalds 
2149cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2150cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2151cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2152cfcea3e8STrond Myklebust 
21533a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
21543a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
21553a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
21563a505845STrond Myklebust 
21571c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
21581c3c07e9STrond Myklebust {
2159b68572e0SNeilBrown 	put_cred(entry->cred);
2160f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
21614e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2162cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
21634e857c58SPeter Zijlstra 	smp_mb__after_atomic();
21641c3c07e9STrond Myklebust }
21651c3c07e9STrond Myklebust 
21661a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
21671a81bb8aSTrond Myklebust {
21681a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
21691a81bb8aSTrond Myklebust 
21701a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
21711a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
21721a81bb8aSTrond Myklebust 		list_del(&cache->lru);
21731a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
21741a81bb8aSTrond Myklebust 	}
21751a81bb8aSTrond Myklebust }
21761a81bb8aSTrond Myklebust 
21773a505845STrond Myklebust static unsigned long
21783a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2179979df72eSTrond Myklebust {
2180979df72eSTrond Myklebust 	LIST_HEAD(head);
2181aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2182979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
21831ab6c499SDave Chinner 	long freed = 0;
2184979df72eSTrond Myklebust 
2185a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2186aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2187979df72eSTrond Myklebust 		struct inode *inode;
2188979df72eSTrond Myklebust 
2189979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2190979df72eSTrond Myklebust 			break;
21919c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2192979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2193979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2194979df72eSTrond Myklebust 			goto remove_lru_entry;
2195979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2196979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2197979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2198979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
21991ab6c499SDave Chinner 		freed++;
2200979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2201979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2202979df72eSTrond Myklebust 					&nfs_access_lru_list);
2203979df72eSTrond Myklebust 		else {
2204979df72eSTrond Myklebust remove_lru_entry:
2205979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
22064e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2207979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
22084e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2209979df72eSTrond Myklebust 		}
221059844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2211979df72eSTrond Myklebust 	}
2212979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22131a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22141ab6c499SDave Chinner 	return freed;
22151ab6c499SDave Chinner }
22161ab6c499SDave Chinner 
22171ab6c499SDave Chinner unsigned long
22183a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
22193a505845STrond Myklebust {
22203a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
22213a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
22223a505845STrond Myklebust 
22233a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
22243a505845STrond Myklebust 		return SHRINK_STOP;
22253a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
22263a505845STrond Myklebust }
22273a505845STrond Myklebust 
22283a505845STrond Myklebust 
22293a505845STrond Myklebust unsigned long
22301ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
22311ab6c499SDave Chinner {
223255f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2233979df72eSTrond Myklebust }
2234979df72eSTrond Myklebust 
22353a505845STrond Myklebust static void
22363a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
22373a505845STrond Myklebust {
22383a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
22393a505845STrond Myklebust 	unsigned long diff;
22403a505845STrond Myklebust 	unsigned int nr_to_scan;
22413a505845STrond Myklebust 
22423a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
22433a505845STrond Myklebust 		return;
22443a505845STrond Myklebust 	nr_to_scan = 100;
22453a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
22463a505845STrond Myklebust 	if (diff < nr_to_scan)
22473a505845STrond Myklebust 		nr_to_scan = diff;
22483a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
22493a505845STrond Myklebust }
22503a505845STrond Myklebust 
22511a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
22521c3c07e9STrond Myklebust {
22531c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
22541a81bb8aSTrond Myklebust 	struct rb_node *n;
22551c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
22561c3c07e9STrond Myklebust 
22571c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
22581c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
22591c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
22601c3c07e9STrond Myklebust 		rb_erase(n, root_node);
22611a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
22621c3c07e9STrond Myklebust 	}
22631c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
22641c3c07e9STrond Myklebust }
22651c3c07e9STrond Myklebust 
22661c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
22671c3c07e9STrond Myklebust {
22681a81bb8aSTrond Myklebust 	LIST_HEAD(head);
22691a81bb8aSTrond Myklebust 
22701a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
22711a81bb8aSTrond Myklebust 		return;
2272cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2273cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
22741a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2275cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2276cfcea3e8STrond Myklebust 
22771c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
22781a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
22791a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
22801a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22811a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22821c3c07e9STrond Myklebust }
22831c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
22841c3c07e9STrond Myklebust 
2285b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
22861c3c07e9STrond Myklebust {
22871c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
22881c3c07e9STrond Myklebust 
22891c3c07e9STrond Myklebust 	while (n != NULL) {
2290b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2291b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2292b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
22931c3c07e9STrond Myklebust 
2294b68572e0SNeilBrown 		if (cmp < 0)
22951c3c07e9STrond Myklebust 			n = n->rb_left;
2296b68572e0SNeilBrown 		else if (cmp > 0)
22971c3c07e9STrond Myklebust 			n = n->rb_right;
22981c3c07e9STrond Myklebust 		else
22991c3c07e9STrond Myklebust 			return entry;
23001c3c07e9STrond Myklebust 	}
23011c3c07e9STrond Myklebust 	return NULL;
23021c3c07e9STrond Myklebust }
23031c3c07e9STrond Myklebust 
2304b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
23051da177e4SLinus Torvalds {
230655296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
23071c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
230857b69181STrond Myklebust 	bool retry = true;
230957b69181STrond Myklebust 	int err;
23101da177e4SLinus Torvalds 
23111c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
231257b69181STrond Myklebust 	for(;;) {
23131c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
23141c3c07e9STrond Myklebust 			goto out_zap;
23151c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
231657b69181STrond Myklebust 		err = -ENOENT;
23171c3c07e9STrond Myklebust 		if (cache == NULL)
23181c3c07e9STrond Myklebust 			goto out;
231957b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
232021c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
232157b69181STrond Myklebust 			break;
23225c965db8STrond Myklebust 		if (!retry)
23235c965db8STrond Myklebust 			break;
232457b69181STrond Myklebust 		err = -ECHILD;
232557b69181STrond Myklebust 		if (!may_block)
232657b69181STrond Myklebust 			goto out;
232757b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
232857b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
232957b69181STrond Myklebust 		if (err)
233057b69181STrond Myklebust 			return err;
233157b69181STrond Myklebust 		spin_lock(&inode->i_lock);
233257b69181STrond Myklebust 		retry = false;
233357b69181STrond Myklebust 	}
23341c3c07e9STrond Myklebust 	res->cred = cache->cred;
23351c3c07e9STrond Myklebust 	res->mask = cache->mask;
2336cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
23371c3c07e9STrond Myklebust 	err = 0;
23381c3c07e9STrond Myklebust out:
23391c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23401c3c07e9STrond Myklebust 	return err;
23411c3c07e9STrond Myklebust out_zap:
23421a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
23431a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
23441c3c07e9STrond Myklebust 	return -ENOENT;
23451c3c07e9STrond Myklebust }
23461c3c07e9STrond Myklebust 
2347b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2348f682a398SNeilBrown {
2349f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2350f682a398SNeilBrown 	 * but do it without locking.
2351f682a398SNeilBrown 	 */
2352f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2353f682a398SNeilBrown 	struct nfs_access_entry *cache;
2354f682a398SNeilBrown 	int err = -ECHILD;
2355f682a398SNeilBrown 	struct list_head *lh;
2356f682a398SNeilBrown 
2357f682a398SNeilBrown 	rcu_read_lock();
2358f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2359f682a398SNeilBrown 		goto out;
2360f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2361f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2362f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2363*9a206de2STrond Myklebust 	    cred_fscmp(cred, cache->cred) != 0)
2364f682a398SNeilBrown 		cache = NULL;
2365f682a398SNeilBrown 	if (cache == NULL)
2366f682a398SNeilBrown 		goto out;
236721c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2368f682a398SNeilBrown 		goto out;
2369f682a398SNeilBrown 	res->cred = cache->cred;
2370f682a398SNeilBrown 	res->mask = cache->mask;
237121c3ba7eSTrond Myklebust 	err = 0;
2372f682a398SNeilBrown out:
2373f682a398SNeilBrown 	rcu_read_unlock();
2374f682a398SNeilBrown 	return err;
2375f682a398SNeilBrown }
2376f682a398SNeilBrown 
23771c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
23781c3c07e9STrond Myklebust {
2379cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2380cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
23811c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
23821c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
23831c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2384b68572e0SNeilBrown 	int cmp;
23851c3c07e9STrond Myklebust 
23861c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
23871c3c07e9STrond Myklebust 	while (*p != NULL) {
23881c3c07e9STrond Myklebust 		parent = *p;
23891c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2390b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
23911c3c07e9STrond Myklebust 
2392b68572e0SNeilBrown 		if (cmp < 0)
23931c3c07e9STrond Myklebust 			p = &parent->rb_left;
2394b68572e0SNeilBrown 		else if (cmp > 0)
23951c3c07e9STrond Myklebust 			p = &parent->rb_right;
23961c3c07e9STrond Myklebust 		else
23971c3c07e9STrond Myklebust 			goto found;
23981c3c07e9STrond Myklebust 	}
23991c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
24001c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2401cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
24021c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24031c3c07e9STrond Myklebust 	return;
24041c3c07e9STrond Myklebust found:
24051c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2406cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2407cfcea3e8STrond Myklebust 	list_del(&entry->lru);
24081c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24091c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
24126168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
24131da177e4SLinus Torvalds {
24141c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
24151c3c07e9STrond Myklebust 	if (cache == NULL)
24161c3c07e9STrond Myklebust 		return;
24171c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2418b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
24191da177e4SLinus Torvalds 	cache->mask = set->mask;
24201c3c07e9STrond Myklebust 
2421f682a398SNeilBrown 	/* The above field assignments must be visible
2422f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2423f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2424f682a398SNeilBrown 	 */
2425f682a398SNeilBrown 	smp_wmb();
24261c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2427cfcea3e8STrond Myklebust 
2428cfcea3e8STrond Myklebust 	/* Update accounting */
24294e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2430cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
24314e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2432cfcea3e8STrond Myklebust 
2433cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
24341a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2435cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
24361a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
24371a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
24381a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2439cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2440cfcea3e8STrond Myklebust 	}
24413a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
24421da177e4SLinus Torvalds }
24436168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
24446168f62cSWeston Andros Adamson 
24453c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
24463c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
24473c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
24483c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
24493c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
24503c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2451ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
24523c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
24533c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
245415d4b73aSTrond Myklebust static int
2455ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
245615d4b73aSTrond Myklebust {
245715d4b73aSTrond Myklebust 	int mask = 0;
245815d4b73aSTrond Myklebust 
245915d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
246015d4b73aSTrond Myklebust 		mask |= MAY_READ;
2461ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2462ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
246315d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2464ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
246515d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2466ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2467ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2468ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2469ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
247015d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2471ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2472ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
247315d4b73aSTrond Myklebust 	return mask;
247415d4b73aSTrond Myklebust }
247515d4b73aSTrond Myklebust 
24766168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
24776168f62cSWeston Andros Adamson {
2478bd8b2441STrond Myklebust 	entry->mask = access_result;
24796168f62cSWeston Andros Adamson }
24806168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
24811da177e4SLinus Torvalds 
2482b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
24831da177e4SLinus Torvalds {
24841da177e4SLinus Torvalds 	struct nfs_access_entry cache;
248557b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2486e8194b7dSTrond Myklebust 	int cache_mask = -1;
24871da177e4SLinus Torvalds 	int status;
24881da177e4SLinus Torvalds 
2489f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2490f4ce1299STrond Myklebust 
2491f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2492f682a398SNeilBrown 	if (status != 0)
249357b69181STrond Myklebust 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
24941da177e4SLinus Torvalds 	if (status == 0)
2495f4ce1299STrond Myklebust 		goto out_cached;
24961da177e4SLinus Torvalds 
2497f3324a2aSNeilBrown 	status = -ECHILD;
249857b69181STrond Myklebust 	if (!may_block)
2499f3324a2aSNeilBrown 		goto out;
2500f3324a2aSNeilBrown 
25011750d929SAnna Schumaker 	/*
25021750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
25031750d929SAnna Schumaker 	 */
25041750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
25051750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
25061750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
25071750d929SAnna Schumaker 	else
25081750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
25091da177e4SLinus Torvalds 	cache.cred = cred;
25101da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2511a71ee337SSuresh Jayaraman 	if (status != 0) {
2512a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2513a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2514a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2515a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2516a71ee337SSuresh Jayaraman 		}
2517f4ce1299STrond Myklebust 		goto out;
2518a71ee337SSuresh Jayaraman 	}
25191da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2520f4ce1299STrond Myklebust out_cached:
2521ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2522bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2523f4ce1299STrond Myklebust 		status = -EACCES;
25241da177e4SLinus Torvalds out:
2525e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
2526f4ce1299STrond Myklebust 	return status;
25271da177e4SLinus Torvalds }
25281da177e4SLinus Torvalds 
2529af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2530af22f94aSTrond Myklebust {
2531af22f94aSTrond Myklebust 	int mask = 0;
2532af22f94aSTrond Myklebust 
2533f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2534f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2535f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2536f8d9a897SWeston Andros Adamson 	} else {
25378a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2538af22f94aSTrond Myklebust 			mask |= MAY_READ;
25398a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2540af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2541f8d9a897SWeston Andros Adamson 	}
2542f8d9a897SWeston Andros Adamson 
2543af22f94aSTrond Myklebust 	return mask;
2544af22f94aSTrond Myklebust }
2545af22f94aSTrond Myklebust 
2546b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2547af22f94aSTrond Myklebust {
2548af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2549af22f94aSTrond Myklebust }
255089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2551af22f94aSTrond Myklebust 
25525c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
25535c5fc09aSTrond Myklebust {
25545c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
255521c3ba7eSTrond Myklebust 	int ret = 0;
25565c5fc09aSTrond Myklebust 
25573825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
25583825827eSTrond Myklebust 		return 0;
2559cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
25605c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
256121c3ba7eSTrond Myklebust 			return -ECHILD;
256221c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
256321c3ba7eSTrond Myklebust 	}
25645c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
25655c5fc09aSTrond Myklebust 		ret = -EACCES;
25665c5fc09aSTrond Myklebust 	return ret;
25675c5fc09aSTrond Myklebust }
25685c5fc09aSTrond Myklebust 
256910556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
25701da177e4SLinus Torvalds {
2571b68572e0SNeilBrown 	const struct cred *cred = current_cred();
25721da177e4SLinus Torvalds 	int res = 0;
25731da177e4SLinus Torvalds 
257491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
257591d5b470SChuck Lever 
2576e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
25771da177e4SLinus Torvalds 		goto out;
25781da177e4SLinus Torvalds 	/* Is this sys_access() ? */
25799cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
25801da177e4SLinus Torvalds 		goto force_lookup;
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
25831da177e4SLinus Torvalds 		case S_IFLNK:
25841da177e4SLinus Torvalds 			goto out;
25851da177e4SLinus Torvalds 		case S_IFREG:
2586762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2587762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2588762674f8STrond Myklebust 				return 0;
25891da177e4SLinus Torvalds 			break;
25901da177e4SLinus Torvalds 		case S_IFDIR:
25911da177e4SLinus Torvalds 			/*
25921da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
25931da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
25941da177e4SLinus Torvalds 			 */
25951da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
25961da177e4SLinus Torvalds 				goto out;
25971da177e4SLinus Torvalds 	}
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds force_lookup:
26001da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
26011da177e4SLinus Torvalds 		goto out_notsup;
26021da177e4SLinus Torvalds 
2603f3324a2aSNeilBrown 	/* Always try fast lookups first */
2604f3324a2aSNeilBrown 	rcu_read_lock();
2605f3324a2aSNeilBrown 	res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2606f3324a2aSNeilBrown 	rcu_read_unlock();
2607f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2608f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
26091da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
2610f3324a2aSNeilBrown 	}
26111da177e4SLinus Torvalds out:
26125c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
26135c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2614f696a365SMiklos Szeredi 
26151e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
26161e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
26171da177e4SLinus Torvalds 	return res;
26181da177e4SLinus Torvalds out_notsup:
2619d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2620d51ac1a8SNeilBrown 		return -ECHILD;
2621d51ac1a8SNeilBrown 
26221da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
26231da177e4SLinus Torvalds 	if (res == 0)
26242830ba7fSAl Viro 		res = generic_permission(inode, mask);
26251e7cb3dcSChuck Lever 	goto out;
26261da177e4SLinus Torvalds }
2627ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds /*
26301da177e4SLinus Torvalds  * Local variables:
26311da177e4SLinus Torvalds  *  version-control: t
26321da177e4SLinus Torvalds  *  kept-new-versions: 5
26331da177e4SLinus Torvalds  * End:
26341da177e4SLinus Torvalds  */
2635