xref: /openbmc/linux/fs/nfs/dir.c (revision 84d08fa8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
301da177e4SLinus Torvalds #include <linux/nfs_fs.h>
311da177e4SLinus Torvalds #include <linux/nfs_mount.h>
321da177e4SLinus Torvalds #include <linux/pagemap.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
36a0b8cab3SMel Gorman #include <linux/swap.h>
37e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "delegation.h"
4291d5b470SChuck Lever #include "iostat.h"
434c30d56eSAdrian Bunk #include "internal.h"
44cd9a1c0eSTrond Myklebust #include "fscache.h"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
49480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5023db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5102c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
52f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5311de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
541da177e4SLinus Torvalds 
554b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
56f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
571da177e4SLinus Torvalds 	.read		= generic_read_dir,
5823db8620SAl Viro 	.iterate	= nfs_readdir,
591da177e4SLinus Torvalds 	.open		= nfs_opendir,
60480c2006SBryan Schumaker 	.release	= nfs_closedir,
611da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
6411de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6511de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
66d1bacf9eSBryan Schumaker };
67d1bacf9eSBryan Schumaker 
680c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
69480c2006SBryan Schumaker {
70480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
71480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
72480c2006SBryan Schumaker 	if (ctx != NULL) {
738ef2ce3eSBryan Schumaker 		ctx->duped = 0;
740c030806STrond Myklebust 		ctx->attr_gencount = NFS_I(dir)->attr_gencount;
75480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
768ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
77480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
78480c2006SBryan Schumaker 		return ctx;
79480c2006SBryan Schumaker 	}
800c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
810c030806STrond Myklebust }
82480c2006SBryan Schumaker 
83480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
84480c2006SBryan Schumaker {
85480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
86480c2006SBryan Schumaker 	kfree(ctx);
87480c2006SBryan Schumaker }
88480c2006SBryan Schumaker 
891da177e4SLinus Torvalds /*
901da177e4SLinus Torvalds  * Open file
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds static int
931da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
941da177e4SLinus Torvalds {
95480c2006SBryan Schumaker 	int res = 0;
96480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
97480c2006SBryan Schumaker 	struct rpc_cred *cred;
981da177e4SLinus Torvalds 
996da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
100cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
101cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
102cc0dd2d1SChuck Lever 
103cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1041e7cb3dcSChuck Lever 
105480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
106480c2006SBryan Schumaker 	if (IS_ERR(cred))
107480c2006SBryan Schumaker 		return PTR_ERR(cred);
1080c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
109480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
110480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
111480c2006SBryan Schumaker 		goto out;
112480c2006SBryan Schumaker 	}
113480c2006SBryan Schumaker 	filp->private_data = ctx;
114f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
115f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
116f5a73672SNeil Brown 		 * have been called, so we need to refresh the
117f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
118f5a73672SNeil Brown 		 */
119f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
120f5a73672SNeil Brown 	}
121480c2006SBryan Schumaker out:
122480c2006SBryan Schumaker 	put_rpccred(cred);
1231da177e4SLinus Torvalds 	return res;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126480c2006SBryan Schumaker static int
127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
128480c2006SBryan Schumaker {
129480c2006SBryan Schumaker 	put_nfs_open_dir_context(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 
147573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
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;
161d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
162d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1631da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1641da177e4SLinus Torvalds 
165d1bacf9eSBryan Schumaker /*
166d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1671da177e4SLinus Torvalds  */
1681da177e4SLinus Torvalds static
169d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1701da177e4SLinus Torvalds {
1718cd51a0cSTrond Myklebust 	void *ptr;
172d1bacf9eSBryan Schumaker 	if (page == NULL)
173d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1748cd51a0cSTrond Myklebust 	ptr = kmap(page);
1758cd51a0cSTrond Myklebust 	if (ptr == NULL)
1768cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1778cd51a0cSTrond Myklebust 	return ptr;
178d1bacf9eSBryan Schumaker }
179d1bacf9eSBryan Schumaker 
180d1bacf9eSBryan Schumaker static
181d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
182d1bacf9eSBryan Schumaker {
183d1bacf9eSBryan Schumaker 	kunmap(page);
184d1bacf9eSBryan Schumaker }
185d1bacf9eSBryan Schumaker 
186d1bacf9eSBryan Schumaker /*
187d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
188d1bacf9eSBryan Schumaker  */
189d1bacf9eSBryan Schumaker static
19011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
191d1bacf9eSBryan Schumaker {
19211de3b11STrond Myklebust 	struct nfs_cache_array *array;
193d1bacf9eSBryan Schumaker 	int i;
1948cd51a0cSTrond Myklebust 
1952b86ce2dSCong Wang 	array = kmap_atomic(page);
196d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
197d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1982b86ce2dSCong Wang 	kunmap_atomic(array);
199d1bacf9eSBryan Schumaker }
200d1bacf9eSBryan Schumaker 
201d1bacf9eSBryan Schumaker /*
202d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
203d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
204d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
205d1bacf9eSBryan Schumaker  */
206d1bacf9eSBryan Schumaker static
2074a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
208d1bacf9eSBryan Schumaker {
209d1bacf9eSBryan Schumaker 	string->len = len;
210d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2114a201d6eSTrond Myklebust 	if (string->name == NULL)
2124a201d6eSTrond Myklebust 		return -ENOMEM;
21304e4bd1cSCatalin Marinas 	/*
21404e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
21504e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
21604e4bd1cSCatalin Marinas 	 */
21704e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2184a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2194a201d6eSTrond Myklebust 	return 0;
220d1bacf9eSBryan Schumaker }
221d1bacf9eSBryan Schumaker 
222d1bacf9eSBryan Schumaker static
223d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
224d1bacf9eSBryan Schumaker {
225d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2264a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2274a201d6eSTrond Myklebust 	int ret;
2284a201d6eSTrond Myklebust 
229d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
230d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
231d1bacf9eSBryan Schumaker 
2324a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2333020093fSTrond Myklebust 
2343020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2353020093fSTrond Myklebust 	ret = -ENOSPC;
2363020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2373020093fSTrond Myklebust 		goto out;
2383020093fSTrond Myklebust 
2394a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2404a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2410b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2424a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2434a201d6eSTrond Myklebust 	if (ret)
2444a201d6eSTrond Myklebust 		goto out;
245d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2468cd51a0cSTrond Myklebust 	array->size++;
24747c716cbSTrond Myklebust 	if (entry->eof != 0)
248d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2494a201d6eSTrond Myklebust out:
250d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2514a201d6eSTrond Myklebust 	return ret;
252d1bacf9eSBryan Schumaker }
253d1bacf9eSBryan Schumaker 
254d1bacf9eSBryan Schumaker static
255d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
256d1bacf9eSBryan Schumaker {
25723db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
258d1bacf9eSBryan Schumaker 	unsigned int index;
259d1bacf9eSBryan Schumaker 
260d1bacf9eSBryan Schumaker 	if (diff < 0)
261d1bacf9eSBryan Schumaker 		goto out_eof;
262d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2638cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
264d1bacf9eSBryan Schumaker 			goto out_eof;
265d1bacf9eSBryan Schumaker 		return -EAGAIN;
266d1bacf9eSBryan Schumaker 	}
267d1bacf9eSBryan Schumaker 
268d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
269d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
270d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
271d1bacf9eSBryan Schumaker 	return 0;
272d1bacf9eSBryan Schumaker out_eof:
273d1bacf9eSBryan Schumaker 	desc->eof = 1;
274d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
275d1bacf9eSBryan Schumaker }
276d1bacf9eSBryan Schumaker 
277d1bacf9eSBryan Schumaker static
278d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
279d1bacf9eSBryan Schumaker {
280d1bacf9eSBryan Schumaker 	int i;
2818ef2ce3eSBryan Schumaker 	loff_t new_pos;
282d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
283d1bacf9eSBryan Schumaker 
284d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2858cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
286496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2870c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2880c030806STrond Myklebust 
2898ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2900c030806STrond Myklebust 			if (ctx->attr_gencount != nfsi->attr_gencount
2910c030806STrond Myklebust 			    || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
2920c030806STrond Myklebust 				ctx->duped = 0;
2930c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
29423db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2950c030806STrond Myklebust 				if (ctx->duped > 0
2960c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2970c030806STrond Myklebust 					if (printk_ratelimit()) {
2980c030806STrond Myklebust 						pr_notice("NFS: directory %s/%s contains a readdir loop."
2990c030806STrond Myklebust 								"Please contact your server vendor.  "
300374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
3010c030806STrond Myklebust 								desc->file->f_dentry->d_parent->d_name.name,
3020c030806STrond Myklebust 								desc->file->f_dentry->d_name.name,
303374e4e3eSBryan Schumaker 								array->array[i].string.name,
3040c030806STrond Myklebust 								*desc->dir_cookie);
3050c030806STrond Myklebust 					}
3060c030806STrond Myklebust 					status = -ELOOP;
3070c030806STrond Myklebust 					goto out;
3080c030806STrond Myklebust 				}
3098ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3100c030806STrond Myklebust 				ctx->duped = -1;
3118ef2ce3eSBryan Schumaker 			}
31223db8620SAl Viro 			desc->ctx->pos = new_pos;
3138cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
31447c716cbSTrond Myklebust 			return 0;
3158cd51a0cSTrond Myklebust 		}
3168cd51a0cSTrond Myklebust 	}
31747c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
318d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
31918fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
32018fb5fe4STrond Myklebust 			desc->eof = 1;
321d1bacf9eSBryan Schumaker 	}
3220c030806STrond Myklebust out:
323d1bacf9eSBryan Schumaker 	return status;
324d1bacf9eSBryan Schumaker }
325d1bacf9eSBryan Schumaker 
326d1bacf9eSBryan Schumaker static
327d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
328d1bacf9eSBryan Schumaker {
329d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
33047c716cbSTrond Myklebust 	int status;
331d1bacf9eSBryan Schumaker 
332d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
333d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
334d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
335d1bacf9eSBryan Schumaker 		goto out;
336d1bacf9eSBryan Schumaker 	}
337d1bacf9eSBryan Schumaker 
338d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
339d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
340d1bacf9eSBryan Schumaker 	else
341d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
342d1bacf9eSBryan Schumaker 
34347c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3440aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
345e47c085aSTrond Myklebust 		desc->current_index += array->size;
34647c716cbSTrond Myklebust 		desc->page_index++;
34747c716cbSTrond Myklebust 	}
348d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
349d1bacf9eSBryan Schumaker out:
350d1bacf9eSBryan Schumaker 	return status;
351d1bacf9eSBryan Schumaker }
352d1bacf9eSBryan Schumaker 
353d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
354d1bacf9eSBryan Schumaker static
35556e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
356d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
357d1bacf9eSBryan Schumaker {
358480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
359480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3604704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3611da177e4SLinus Torvalds 	int		error;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds  again:
3641da177e4SLinus Torvalds 	timestamp = jiffies;
3654704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
36656e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3671da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3681da177e4SLinus Torvalds 	if (error < 0) {
3691da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3701da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3711da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3723a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3731da177e4SLinus Torvalds 			desc->plus = 0;
3741da177e4SLinus Torvalds 			goto again;
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 		goto error;
3771da177e4SLinus Torvalds 	}
3781f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3794704f0e2STrond Myklebust 	desc->gencount = gencount;
380d1bacf9eSBryan Schumaker error:
381d1bacf9eSBryan Schumaker 	return error;
382d1bacf9eSBryan Schumaker }
383d1bacf9eSBryan Schumaker 
384573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
385573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
386d1bacf9eSBryan Schumaker {
387573c4e1eSChuck Lever 	int error;
388d1bacf9eSBryan Schumaker 
389573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
390573c4e1eSChuck Lever 	if (error)
391573c4e1eSChuck Lever 		return error;
392d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
393d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
394d1bacf9eSBryan Schumaker 	return 0;
395d1bacf9eSBryan Schumaker }
396d1bacf9eSBryan Schumaker 
397d39ab9deSBryan Schumaker static
398d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
399d39ab9deSBryan Schumaker {
400d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
401d39ab9deSBryan Schumaker 		goto different;
40237a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
403d39ab9deSBryan Schumaker 		goto different;
404d39ab9deSBryan Schumaker 	return 1;
405d39ab9deSBryan Schumaker different:
406d39ab9deSBryan Schumaker 	return 0;
407d39ab9deSBryan Schumaker }
408d39ab9deSBryan Schumaker 
409d39ab9deSBryan Schumaker static
41023db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
411d69ee9b8STrond Myklebust {
412d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
413d69ee9b8STrond Myklebust 		return false;
414d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
415d69ee9b8STrond Myklebust 		return true;
41623db8620SAl Viro 	if (ctx->pos == 0)
417d69ee9b8STrond Myklebust 		return true;
418d69ee9b8STrond Myklebust 	return false;
419d69ee9b8STrond Myklebust }
420d69ee9b8STrond Myklebust 
421d69ee9b8STrond Myklebust /*
422d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
423d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
424d69ee9b8STrond Myklebust  * directory.
425d69ee9b8STrond Myklebust  */
426d69ee9b8STrond Myklebust static
427d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
428d69ee9b8STrond Myklebust {
429d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
430d69ee9b8STrond Myklebust }
431d69ee9b8STrond Myklebust 
432d69ee9b8STrond Myklebust static
433d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
434d39ab9deSBryan Schumaker {
43526fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4364a201d6eSTrond Myklebust 	struct dentry *dentry;
4374a201d6eSTrond Myklebust 	struct dentry *alias;
438d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
439d39ab9deSBryan Schumaker 	struct inode *inode;
440d39ab9deSBryan Schumaker 
4414a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4424a201d6eSTrond Myklebust 		if (filename.len == 1)
4434a201d6eSTrond Myklebust 			return;
4444a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4454a201d6eSTrond Myklebust 			return;
4464a201d6eSTrond Myklebust 	}
4474a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
448d39ab9deSBryan Schumaker 
4494a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
450d39ab9deSBryan Schumaker 	if (dentry != NULL) {
451d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
452d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
453d39ab9deSBryan Schumaker 			goto out;
454d39ab9deSBryan Schumaker 		} else {
455696199f8SAl Viro 			if (d_invalidate(dentry) != 0)
456696199f8SAl Viro 				goto out;
457d39ab9deSBryan Schumaker 			dput(dentry);
458d39ab9deSBryan Schumaker 		}
459d39ab9deSBryan Schumaker 	}
460d39ab9deSBryan Schumaker 
461d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4624a201d6eSTrond Myklebust 	if (dentry == NULL)
4634a201d6eSTrond Myklebust 		return;
4644a201d6eSTrond Myklebust 
465d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
466d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
467d39ab9deSBryan Schumaker 		goto out;
468d39ab9deSBryan Schumaker 
469d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
470d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
471d39ab9deSBryan Schumaker 		goto out;
472d39ab9deSBryan Schumaker 	else if (alias) {
473d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
474d39ab9deSBryan Schumaker 		dput(alias);
475d39ab9deSBryan Schumaker 	} else
476d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
477d39ab9deSBryan Schumaker 
478d39ab9deSBryan Schumaker out:
479d39ab9deSBryan Schumaker 	dput(dentry);
480d39ab9deSBryan Schumaker }
481d39ab9deSBryan Schumaker 
482d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
483d1bacf9eSBryan Schumaker static
4848cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
4856650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
486d1bacf9eSBryan Schumaker {
487babddc72SBryan Schumaker 	struct xdr_stream stream;
488f7da7a12SBenny Halevy 	struct xdr_buf buf;
4896650239aSTrond Myklebust 	struct page *scratch;
49099424380SBryan Schumaker 	struct nfs_cache_array *array;
4915c346854STrond Myklebust 	unsigned int count = 0;
4925c346854STrond Myklebust 	int status;
493babddc72SBryan Schumaker 
4946650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
4956650239aSTrond Myklebust 	if (scratch == NULL)
4966650239aSTrond Myklebust 		return -ENOMEM;
497babddc72SBryan Schumaker 
498f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
4996650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
50099424380SBryan Schumaker 
50199424380SBryan Schumaker 	do {
50299424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5038cd51a0cSTrond Myklebust 		if (status != 0) {
5048cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5058cd51a0cSTrond Myklebust 				status = 0;
50699424380SBryan Schumaker 			break;
5078cd51a0cSTrond Myklebust 		}
50899424380SBryan Schumaker 
5095c346854STrond Myklebust 		count++;
5105c346854STrond Myklebust 
51147c716cbSTrond Myklebust 		if (desc->plus != 0)
512d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5138cd51a0cSTrond Myklebust 
5148cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5158cd51a0cSTrond Myklebust 		if (status != 0)
5168cd51a0cSTrond Myklebust 			break;
51799424380SBryan Schumaker 	} while (!entry->eof);
51899424380SBryan Schumaker 
51947c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
52099424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5218cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5228cd51a0cSTrond Myklebust 			array->eof_index = array->size;
52399424380SBryan Schumaker 			status = 0;
52499424380SBryan Schumaker 			nfs_readdir_release_array(page);
5255c346854STrond Myklebust 		} else
5265c346854STrond Myklebust 			status = PTR_ERR(array);
52756e4ebf8SBryan Schumaker 	}
5286650239aSTrond Myklebust 
5296650239aSTrond Myklebust 	put_page(scratch);
5308cd51a0cSTrond Myklebust 	return status;
5318cd51a0cSTrond Myklebust }
53256e4ebf8SBryan Schumaker 
53356e4ebf8SBryan Schumaker static
53456e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
53556e4ebf8SBryan Schumaker {
53656e4ebf8SBryan Schumaker 	unsigned int i;
53756e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
53856e4ebf8SBryan Schumaker 		put_page(pages[i]);
53956e4ebf8SBryan Schumaker }
54056e4ebf8SBryan Schumaker 
54156e4ebf8SBryan Schumaker static
54256e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
54356e4ebf8SBryan Schumaker 		unsigned int npages)
54456e4ebf8SBryan Schumaker {
54556e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
54656e4ebf8SBryan Schumaker }
54756e4ebf8SBryan Schumaker 
54856e4ebf8SBryan Schumaker /*
54956e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
55056e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
55156e4ebf8SBryan Schumaker  */
55256e4ebf8SBryan Schumaker static
5536650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
55456e4ebf8SBryan Schumaker {
55556e4ebf8SBryan Schumaker 	unsigned int i;
55656e4ebf8SBryan Schumaker 
55756e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
55856e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
55956e4ebf8SBryan Schumaker 		if (page == NULL)
56056e4ebf8SBryan Schumaker 			goto out_freepages;
56156e4ebf8SBryan Schumaker 		pages[i] = page;
56256e4ebf8SBryan Schumaker 	}
5636650239aSTrond Myklebust 	return 0;
56456e4ebf8SBryan Schumaker 
56556e4ebf8SBryan Schumaker out_freepages:
56656e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
5676650239aSTrond Myklebust 	return -ENOMEM;
568d1bacf9eSBryan Schumaker }
569d1bacf9eSBryan Schumaker 
570d1bacf9eSBryan Schumaker static
571d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
572d1bacf9eSBryan Schumaker {
57356e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
57456e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
575d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
576d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
577d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5788cd51a0cSTrond Myklebust 	int status = -ENOMEM;
57956e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
580d1bacf9eSBryan Schumaker 
581d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
5820aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
583d1bacf9eSBryan Schumaker 	entry.eof = 0;
584d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
585d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
586573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
587d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
588d1bacf9eSBryan Schumaker 		goto out;
589d1bacf9eSBryan Schumaker 
590d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
5918cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
5928cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
5938cd51a0cSTrond Myklebust 		goto out;
5948cd51a0cSTrond Myklebust 	}
595d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
596d1bacf9eSBryan Schumaker 	array->eof_index = -1;
597d1bacf9eSBryan Schumaker 
5986650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
5996650239aSTrond Myklebust 	if (status < 0)
600d1bacf9eSBryan Schumaker 		goto out_release_array;
601d1bacf9eSBryan Schumaker 	do {
602ac396128STrond Myklebust 		unsigned int pglen;
60356e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
604babddc72SBryan Schumaker 
605d1bacf9eSBryan Schumaker 		if (status < 0)
606d1bacf9eSBryan Schumaker 			break;
607ac396128STrond Myklebust 		pglen = status;
6086650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6098cd51a0cSTrond Myklebust 		if (status < 0) {
6108cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6118cd51a0cSTrond Myklebust 				status = 0;
6128cd51a0cSTrond Myklebust 			break;
6138cd51a0cSTrond Myklebust 		}
6148cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
615d1bacf9eSBryan Schumaker 
61656e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
617d1bacf9eSBryan Schumaker out_release_array:
618d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
619d1bacf9eSBryan Schumaker out:
620d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
621d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
622d1bacf9eSBryan Schumaker 	return status;
623d1bacf9eSBryan Schumaker }
624d1bacf9eSBryan Schumaker 
625d1bacf9eSBryan Schumaker /*
626d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
627d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
628d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
629d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6301da177e4SLinus Torvalds  */
631d1bacf9eSBryan Schumaker static
632d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
633d1bacf9eSBryan Schumaker {
634496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6358cd51a0cSTrond Myklebust 	int ret;
636d1bacf9eSBryan Schumaker 
6378cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6388cd51a0cSTrond Myklebust 	if (ret < 0)
639d1bacf9eSBryan Schumaker 		goto error;
640d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
641d1bacf9eSBryan Schumaker 
6422aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
643cd9ae2b6STrond Myklebust 		/* Should never happen */
644cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
645cd9ae2b6STrond Myklebust 	}
6461da177e4SLinus Torvalds 	unlock_page(page);
6471da177e4SLinus Torvalds 	return 0;
6481da177e4SLinus Torvalds  error:
6491da177e4SLinus Torvalds 	unlock_page(page);
6508cd51a0cSTrond Myklebust 	return ret;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
653d1bacf9eSBryan Schumaker static
654d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6551da177e4SLinus Torvalds {
65611de3b11STrond Myklebust 	if (!desc->page->mapping)
65711de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6581da177e4SLinus Torvalds 	page_cache_release(desc->page);
6591da177e4SLinus Torvalds 	desc->page = NULL;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
662d1bacf9eSBryan Schumaker static
663d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6641da177e4SLinus Torvalds {
665496ad9aaSAl Viro 	return read_cache_page(file_inode(desc->file)->i_mapping,
666d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /*
670d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6711da177e4SLinus Torvalds  */
672d1bacf9eSBryan Schumaker static
673d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
674d1bacf9eSBryan Schumaker {
675d1bacf9eSBryan Schumaker 	int res;
676d1bacf9eSBryan Schumaker 
677d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
678d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
679d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
680d1bacf9eSBryan Schumaker 
681d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
68247c716cbSTrond Myklebust 	if (res != 0)
683d1bacf9eSBryan Schumaker 		cache_page_release(desc);
684d1bacf9eSBryan Schumaker 	return res;
685d1bacf9eSBryan Schumaker }
686d1bacf9eSBryan Schumaker 
687d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
6881da177e4SLinus Torvalds static inline
6891da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
6901da177e4SLinus Torvalds {
6918cd51a0cSTrond Myklebust 	int res;
692d1bacf9eSBryan Schumaker 
6930aded708STrond Myklebust 	if (desc->page_index == 0) {
6948cd51a0cSTrond Myklebust 		desc->current_index = 0;
6950aded708STrond Myklebust 		desc->last_cookie = 0;
6960aded708STrond Myklebust 	}
69747c716cbSTrond Myklebust 	do {
698d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
69947c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7001da177e4SLinus Torvalds 	return res;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /*
7041da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7051da177e4SLinus Torvalds  */
7061da177e4SLinus Torvalds static
70723db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	struct file	*file = desc->file;
710d1bacf9eSBryan Schumaker 	int i = 0;
711d1bacf9eSBryan Schumaker 	int res = 0;
712d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7138ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7148ef2ce3eSBryan Schumaker 
715d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
716e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
717e7c58e97STrond Myklebust 		res = PTR_ERR(array);
718e7c58e97STrond Myklebust 		goto out;
719e7c58e97STrond Myklebust 	}
7201da177e4SLinus Torvalds 
721d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
722ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7231da177e4SLinus Torvalds 
724ece0b423STrond Myklebust 		ent = &array->array[i];
72523db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
72623db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
727ece0b423STrond Myklebust 			desc->eof = 1;
7281da177e4SLinus Torvalds 			break;
729ece0b423STrond Myklebust 		}
73023db8620SAl Viro 		desc->ctx->pos++;
731d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
732d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
733d1bacf9eSBryan Schumaker 		else
734d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7350c030806STrond Myklebust 		if (ctx->duped != 0)
7360c030806STrond Myklebust 			ctx->duped = 1;
7378cd51a0cSTrond Myklebust 	}
73847c716cbSTrond Myklebust 	if (array->eof_index >= 0)
739d1bacf9eSBryan Schumaker 		desc->eof = 1;
740d1bacf9eSBryan Schumaker 
741d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
742e7c58e97STrond Myklebust out:
743d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7441e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7451e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7461da177e4SLinus Torvalds 	return res;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds /*
7501da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7511da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7521da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7531da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7541da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7551da177e4SLinus Torvalds  *
7561da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7571da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7581da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7591da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds static inline
76223db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	struct page	*page = NULL;
7651da177e4SLinus Torvalds 	int		status;
766496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
7670c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
7681da177e4SLinus Torvalds 
7691e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7701e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7731da177e4SLinus Torvalds 	if (!page) {
7741da177e4SLinus Torvalds 		status = -ENOMEM;
7751da177e4SLinus Torvalds 		goto out;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
7787a8e1dc3STrond Myklebust 	desc->page_index = 0;
7790aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
7807a8e1dc3STrond Myklebust 	desc->page = page;
7810c030806STrond Myklebust 	ctx->duped = 0;
7827a8e1dc3STrond Myklebust 
78385f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
78485f8607eSTrond Myklebust 	if (status < 0)
785d1bacf9eSBryan Schumaker 		goto out_release;
786d1bacf9eSBryan Schumaker 
78723db8620SAl Viro 	status = nfs_do_filldir(desc);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds  out:
7901e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
7913110ff80SHarvey Harrison 			__func__, status);
7921da177e4SLinus Torvalds 	return status;
7931da177e4SLinus Torvalds  out_release:
794d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7951da177e4SLinus Torvalds 	goto out;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
79800a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
79900a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
80000a92642SOlivier Galibert    whole directory.
8011da177e4SLinus Torvalds  */
80223db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8031da177e4SLinus Torvalds {
80423db8620SAl Viro 	struct dentry	*dentry = file->f_path.dentry;
8051da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8061da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8071da177e4SLinus Torvalds 			*desc = &my_desc;
80823db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
80947c716cbSTrond Myklebust 	int res;
8101da177e4SLinus Torvalds 
8116da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8121e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
81323db8620SAl Viro 			(long long)ctx->pos);
81491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
81591d5b470SChuck Lever 
8161da177e4SLinus Torvalds 	/*
81723db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
818f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
81900a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
82000a92642SOlivier Galibert 	 * revalidate the cookie.
8211da177e4SLinus Torvalds 	 */
8221da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8231da177e4SLinus Torvalds 
82423db8620SAl Viro 	desc->file = file;
82523db8620SAl Viro 	desc->ctx = ctx;
826480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8271da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
82823db8620SAl Viro 	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
8291da177e4SLinus Torvalds 
830565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
83123db8620SAl Viro 	res = nfs_revalidate_mapping(inode, file->f_mapping);
832fccca7fcSTrond Myklebust 	if (res < 0)
833fccca7fcSTrond Myklebust 		goto out;
834fccca7fcSTrond Myklebust 
83547c716cbSTrond Myklebust 	do {
8361da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
83700a92642SOlivier Galibert 
8381da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
839ece0b423STrond Myklebust 			res = 0;
8401da177e4SLinus Torvalds 			/* This means either end of directory */
841d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8421da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
84323db8620SAl Viro 				res = uncached_readdir(desc);
844ece0b423STrond Myklebust 				if (res == 0)
8451da177e4SLinus Torvalds 					continue;
8461da177e4SLinus Torvalds 			}
8471da177e4SLinus Torvalds 			break;
8481da177e4SLinus Torvalds 		}
8491da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8503a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8511da177e4SLinus Torvalds 			nfs_zap_caches(inode);
852baf57a09STrond Myklebust 			desc->page_index = 0;
8531da177e4SLinus Torvalds 			desc->plus = 0;
854d1bacf9eSBryan Schumaker 			desc->eof = 0;
8551da177e4SLinus Torvalds 			continue;
8561da177e4SLinus Torvalds 		}
8571da177e4SLinus Torvalds 		if (res < 0)
8581da177e4SLinus Torvalds 			break;
8591da177e4SLinus Torvalds 
86023db8620SAl Viro 		res = nfs_do_filldir(desc);
861ece0b423STrond Myklebust 		if (res < 0)
8621da177e4SLinus Torvalds 			break;
86347c716cbSTrond Myklebust 	} while (!desc->eof);
864fccca7fcSTrond Myklebust out:
865565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8661e7cb3dcSChuck Lever 	if (res > 0)
8671e7cb3dcSChuck Lever 		res = 0;
868aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8691e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8701e7cb3dcSChuck Lever 			res);
8711da177e4SLinus Torvalds 	return res;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
874965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
875f0dd2136STrond Myklebust {
876b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
877b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
878480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
879b84e06c5SChuck Lever 
8806da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
881b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
882b84e06c5SChuck Lever 			dentry->d_name.name,
883965c8e59SAndrew Morton 			offset, whence);
884b84e06c5SChuck Lever 
885b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
886965c8e59SAndrew Morton 	switch (whence) {
887f0dd2136STrond Myklebust 		case 1:
888f0dd2136STrond Myklebust 			offset += filp->f_pos;
889f0dd2136STrond Myklebust 		case 0:
890f0dd2136STrond Myklebust 			if (offset >= 0)
891f0dd2136STrond Myklebust 				break;
892f0dd2136STrond Myklebust 		default:
893f0dd2136STrond Myklebust 			offset = -EINVAL;
894f0dd2136STrond Myklebust 			goto out;
895f0dd2136STrond Myklebust 	}
896f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
897f0dd2136STrond Myklebust 		filp->f_pos = offset;
898480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
8998ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
900f0dd2136STrond Myklebust 	}
901f0dd2136STrond Myklebust out:
902b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
903f0dd2136STrond Myklebust 	return offset;
904f0dd2136STrond Myklebust }
905f0dd2136STrond Myklebust 
9061da177e4SLinus Torvalds /*
9071da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9081da177e4SLinus Torvalds  * is a dummy operation.
9091da177e4SLinus Torvalds  */
91002c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
91102c24a82SJosef Bacik 			 int datasync)
9121da177e4SLinus Torvalds {
9137ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
91402c24a82SJosef Bacik 	struct inode *inode = dentry->d_inode;
9157ea80859SChristoph Hellwig 
9166da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9171e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9181e7cb3dcSChuck Lever 			datasync);
9191e7cb3dcSChuck Lever 
92002c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
92154917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
92202c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9231da177e4SLinus Torvalds 	return 0;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
926bfc69a45STrond Myklebust /**
927bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
928bfc69a45STrond Myklebust  * @dir - pointer to directory inode
929bfc69a45STrond Myklebust  *
930bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
931bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
932bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
933bfc69a45STrond Myklebust  *
934bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
935bfc69a45STrond Myklebust  */
936bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
937bfc69a45STrond Myklebust {
938011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
939bfc69a45STrond Myklebust }
94089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
941bfc69a45STrond Myklebust 
9421da177e4SLinus Torvalds /*
9431da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9441da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9451da177e4SLinus Torvalds  * and may need to be looked up again.
9461da177e4SLinus Torvalds  */
947c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9501da177e4SLinus Torvalds 		return 1;
9514eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9524eec952eSTrond Myklebust 		return 0;
953f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9546ecc5e8fSTrond Myklebust 		return 0;
955f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
956f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
957f2c77f4eSTrond Myklebust 		return 0;
958f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
959f2c77f4eSTrond Myklebust 		return 0;
960f2c77f4eSTrond Myklebust 	return 1;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds /*
964a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
965a12802caSTrond Myklebust  * an O_EXCL create using this path component.
966a12802caSTrond Myklebust  */
967fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
968a12802caSTrond Myklebust {
969a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
970a12802caSTrond Myklebust 		return 0;
971fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
972a12802caSTrond Myklebust }
973a12802caSTrond Myklebust 
974a12802caSTrond Myklebust /*
9751d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9761d6757fbSTrond Myklebust  *
9771d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9781d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9791d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9801d6757fbSTrond Myklebust  *
9811d6757fbSTrond Myklebust  */
98265a0c149STrond Myklebust static
983fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
98665a0c149STrond Myklebust 	int ret;
9871da177e4SLinus Torvalds 
98836d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
9894e99a1ffSTrond Myklebust 		return 0;
9901da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
991fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
9921da177e4SLinus Torvalds 		goto out_force;
9931da177e4SLinus Torvalds 	/* This is an open(2) */
994fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
995fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
9961da177e4SLinus Torvalds 		goto out_force;
99765a0c149STrond Myklebust out:
99865a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
9991da177e4SLinus Torvalds out_force:
100065a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
100165a0c149STrond Myklebust 	if (ret != 0)
100265a0c149STrond Myklebust 		return ret;
100365a0c149STrond Myklebust 	goto out;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds /*
10071da177e4SLinus Torvalds  * We judge how long we want to trust negative
10081da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10091da177e4SLinus Torvalds  *
10101da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10111da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10121da177e4SLinus Torvalds  */
10131da177e4SLinus Torvalds static inline
10141da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1015fa3c56bbSAl Viro 		       unsigned int flags)
10161da177e4SLinus Torvalds {
10171da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1018fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10191da177e4SLinus Torvalds 		return 0;
10204eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10214eec952eSTrond Myklebust 		return 1;
10221da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds /*
10261da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10271da177e4SLinus Torvalds  * and we should check whether we can really trust that
10281da177e4SLinus Torvalds  * lookup.
10291da177e4SLinus Torvalds  *
10301da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10311da177e4SLinus Torvalds  * we have an inode!
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10341da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10351da177e4SLinus Torvalds  */
10360b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	struct inode *dir;
10391da177e4SLinus Torvalds 	struct inode *inode;
10401da177e4SLinus Torvalds 	struct dentry *parent;
1041e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1042e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10431da177e4SLinus Torvalds 	int error;
10441da177e4SLinus Torvalds 
1045fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
104634286d66SNick Piggin 		return -ECHILD;
104734286d66SNick Piggin 
10481da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10491da177e4SLinus Torvalds 	dir = parent->d_inode;
105091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10511da177e4SLinus Torvalds 	inode = dentry->d_inode;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	if (!inode) {
1054fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
10551da177e4SLinus Torvalds 			goto out_bad;
1056d69ee9b8STrond Myklebust 		goto out_valid_noent;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10601e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10613110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10621e7cb3dcSChuck Lever 				dentry->d_name.name);
10631da177e4SLinus Torvalds 		goto out_bad;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds 
1066011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
106715860ab1STrond Myklebust 		goto out_set_verifier;
106815860ab1STrond Myklebust 
10691da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1070fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1071fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
10721da177e4SLinus Torvalds 			goto out_zap_parent;
10731da177e4SLinus Torvalds 		goto out_valid;
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10771da177e4SLinus Torvalds 		goto out_bad;
10781da177e4SLinus Torvalds 
1079e1fb4d05STrond Myklebust 	error = -ENOMEM;
1080e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1081e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1082e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1083e1fb4d05STrond Myklebust 		goto out_error;
1084e1fb4d05STrond Myklebust 
108580a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
10861da177e4SLinus Torvalds 	if (error)
10871da177e4SLinus Torvalds 		goto out_bad;
1088e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
10891da177e4SLinus Torvalds 		goto out_bad;
1090e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
10911da177e4SLinus Torvalds 		goto out_bad;
10921da177e4SLinus Torvalds 
1093e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1094e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
109515860ab1STrond Myklebust out_set_verifier:
1096cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10971da177e4SLinus Torvalds  out_valid:
1098d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1099d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1100d69ee9b8STrond Myklebust  out_valid_noent:
11011da177e4SLinus Torvalds 	dput(parent);
11021e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
11033110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11041e7cb3dcSChuck Lever 			dentry->d_name.name);
11051da177e4SLinus Torvalds 	return 1;
11061da177e4SLinus Torvalds out_zap_parent:
11071da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11081da177e4SLinus Torvalds  out_bad:
1109c44600c9SAl Viro 	nfs_free_fattr(fattr);
1110c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
1111a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11121da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11131da177e4SLinus Torvalds 		/* Purge readdir caches. */
11141da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11151da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11161da177e4SLinus Torvalds 		if (have_submounts(dentry))
11171da177e4SLinus Torvalds 			goto out_valid;
1118d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1119d9e80b7dSAl Viro 			goto out_valid;
11201da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 	d_drop(dentry);
11231da177e4SLinus Torvalds 	dput(parent);
11241e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11253110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11261e7cb3dcSChuck Lever 			dentry->d_name.name);
11271da177e4SLinus Torvalds 	return 0;
1128e1fb4d05STrond Myklebust out_error:
1129e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1130e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1131e1fb4d05STrond Myklebust 	dput(parent);
1132e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1133e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1134e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1135e1fb4d05STrond Myklebust 	return error;
11361da177e4SLinus Torvalds }
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds /*
1139ecf3d1f1SJeff Layton  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1140ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1141ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1142ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1143ecf3d1f1SJeff Layton  *
1144ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1145ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1146ecf3d1f1SJeff Layton  */
1147ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1148ecf3d1f1SJeff Layton {
1149ecf3d1f1SJeff Layton 	int error;
1150ecf3d1f1SJeff Layton 	struct inode *inode = dentry->d_inode;
1151ecf3d1f1SJeff Layton 
1152ecf3d1f1SJeff Layton 	/*
1153ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1154ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1155ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1156ecf3d1f1SJeff Layton 	 */
1157ecf3d1f1SJeff Layton 	if (!inode) {
1158ecf3d1f1SJeff Layton 		dfprintk(LOOKUPCACHE, "%s: %s/%s has negative inode\n",
1159ecf3d1f1SJeff Layton 				__func__, dentry->d_parent->d_name.name,
1160ecf3d1f1SJeff Layton 				dentry->d_name.name);
1161ecf3d1f1SJeff Layton 		return 1;
1162ecf3d1f1SJeff Layton 	}
1163ecf3d1f1SJeff Layton 
1164ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
1165ecf3d1f1SJeff Layton 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
1166ecf3d1f1SJeff Layton 				__func__, dentry->d_parent->d_name.name,
1167ecf3d1f1SJeff Layton 				dentry->d_name.name);
1168ecf3d1f1SJeff Layton 		return 0;
1169ecf3d1f1SJeff Layton 	}
1170ecf3d1f1SJeff Layton 
1171ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1172ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1173ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1174ecf3d1f1SJeff Layton 	return !error;
1175ecf3d1f1SJeff Layton }
1176ecf3d1f1SJeff Layton 
1177ecf3d1f1SJeff Layton /*
11781da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11791da177e4SLinus Torvalds  */
1180fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
11831da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
11841da177e4SLinus Torvalds 		dentry->d_flags);
11851da177e4SLinus Torvalds 
118677f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
118777f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
118877f11192STrond Myklebust 		return 1;
118977f11192STrond Myklebust 
11901da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11911da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
11921da177e4SLinus Torvalds 		return 1;
11931da177e4SLinus Torvalds 	}
11941da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
11951da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
11961da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
11971da177e4SLinus Torvalds 		return 1;
11981da177e4SLinus Torvalds 	}
11991da177e4SLinus Torvalds 	return 0;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12041b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12051b83d707STrond Myklebust {
12061b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12071f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12081f018458STrond Myklebust 	if (inode->i_nlink == 1)
12091f018458STrond Myklebust 		clear_nlink(inode);
12101f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12111b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12121b83d707STrond Myklebust }
12131b83d707STrond Myklebust 
12141da177e4SLinus Torvalds /*
12151da177e4SLinus Torvalds  * Called when the dentry loses inode.
12161da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12171da177e4SLinus Torvalds  */
12181da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12191da177e4SLinus Torvalds {
122083672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
122183672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
122283672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
122383672d39SNeil Brown 
12241da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1225e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12261f018458STrond Myklebust 		nfs_drop_nlink(inode);
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 	iput(inode);
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1232b1942c5fSAl Viro {
1233b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1234b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1235b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1236b1942c5fSAl Viro 			WARN_ON(1);
1237b1942c5fSAl Viro 		else
1238b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1239b1942c5fSAl Viro 	}
1240b1942c5fSAl Viro }
1241b1942c5fSAl Viro 
1242f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12431da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1244ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
12451da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12461da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
124736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1248b1942c5fSAl Viro 	.d_release	= nfs_d_release,
12491da177e4SLinus Torvalds };
1250ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
12511da177e4SLinus Torvalds 
1252597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	struct dentry *res;
1255565277f6STrond Myklebust 	struct dentry *parent;
12561da177e4SLinus Torvalds 	struct inode *inode = NULL;
1257e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1258e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12591da177e4SLinus Torvalds 	int error;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
12621da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
126391d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12661da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12671da177e4SLinus Torvalds 		goto out;
12681da177e4SLinus Torvalds 
1269fd684071STrond Myklebust 	/*
1270fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1271fd684071STrond Myklebust 	 * but don't hash the dentry.
1272fd684071STrond Myklebust 	 */
127300cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1274fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1275fd684071STrond Myklebust 		res = NULL;
1276fc0f684cSTrond Myklebust 		goto out;
1277fd684071STrond Myklebust 	}
12781da177e4SLinus Torvalds 
1279e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1280e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1281e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1282e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1283e1fb4d05STrond Myklebust 		goto out;
1284e1fb4d05STrond Myklebust 
1285565277f6STrond Myklebust 	parent = dentry->d_parent;
1286565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1287565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
128880a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
12891da177e4SLinus Torvalds 	if (error == -ENOENT)
12901da177e4SLinus Torvalds 		goto no_entry;
12911da177e4SLinus Torvalds 	if (error < 0) {
12921da177e4SLinus Torvalds 		res = ERR_PTR(error);
1293565277f6STrond Myklebust 		goto out_unblock_sillyrename;
12941da177e4SLinus Torvalds 	}
1295e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1296bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
129703f28e3aSTrond Myklebust 	if (IS_ERR(res))
1298565277f6STrond Myklebust 		goto out_unblock_sillyrename;
129954ceac45SDavid Howells 
1300d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1301d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1302d69ee9b8STrond Myklebust 
13031da177e4SLinus Torvalds no_entry:
130454ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13059eaef27bSTrond Myklebust 	if (res != NULL) {
13069eaef27bSTrond Myklebust 		if (IS_ERR(res))
1307565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13081da177e4SLinus Torvalds 		dentry = res;
13099eaef27bSTrond Myklebust 	}
13101da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1311565277f6STrond Myklebust out_unblock_sillyrename:
1312565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
13131da177e4SLinus Torvalds out:
1314e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1315e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13161da177e4SLinus Torvalds 	return res;
13171da177e4SLinus Torvalds }
1318ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
13191da177e4SLinus Torvalds 
132089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
13210b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13221da177e4SLinus Torvalds 
1323f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13240ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13251da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13261da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
132736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1328b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13291da177e4SLinus Torvalds };
133089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
13311da177e4SLinus Torvalds 
13328a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13338a5e929dSAl Viro {
13348a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13358a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
13368a5e929dSAl Viro 		res |= FMODE_READ;
13378a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
13388a5e929dSAl Viro 		res |= FMODE_WRITE;
13398a5e929dSAl Viro 	return res;
13408a5e929dSAl Viro }
13418a5e929dSAl Viro 
134251141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1343cd9a1c0eSTrond Myklebust {
13445ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1345cd9a1c0eSTrond Myklebust }
1346cd9a1c0eSTrond Myklebust 
1347cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1348cd9a1c0eSTrond Myklebust {
1349cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1350cd9a1c0eSTrond Myklebust 	return 0;
1351cd9a1c0eSTrond Myklebust }
1352cd9a1c0eSTrond Myklebust 
1353d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13540dd2b474SMiklos Szeredi 			   struct dentry *dentry,
135530d90494SAl Viro 			   struct file *file, unsigned open_flags,
135647237687SAl Viro 			   int *opened)
1357cd9a1c0eSTrond Myklebust {
13580dd2b474SMiklos Szeredi 	int err;
13590dd2b474SMiklos Szeredi 
13600dd2b474SMiklos Szeredi 	if (ctx->dentry != dentry) {
13610dd2b474SMiklos Szeredi 		dput(ctx->dentry);
13620dd2b474SMiklos Szeredi 		ctx->dentry = dget(dentry);
13630dd2b474SMiklos Szeredi 	}
1364cd9a1c0eSTrond Myklebust 
1365cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1366cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
13670dd2b474SMiklos Szeredi 		err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1368d9585277SAl Viro 		if (err < 0)
1369cd9a1c0eSTrond Myklebust 			goto out;
1370cd9a1c0eSTrond Myklebust 	}
13710dd2b474SMiklos Szeredi 
137230d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
137330d90494SAl Viro 	if (err)
1374d9585277SAl Viro 		goto out;
137530d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
13760dd2b474SMiklos Szeredi 
1377cd9a1c0eSTrond Myklebust out:
1378cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1379d9585277SAl Viro 	return err;
1380cd9a1c0eSTrond Myklebust }
1381cd9a1c0eSTrond Myklebust 
138273a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
138330d90494SAl Viro 		    struct file *file, unsigned open_flags,
138447237687SAl Viro 		    umode_t mode, int *opened)
13851da177e4SLinus Torvalds {
1386cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
13870dd2b474SMiklos Szeredi 	struct dentry *res;
13880dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1389f46e0bd3STrond Myklebust 	struct inode *inode;
1390898f635cSTrond Myklebust 	int err;
13911da177e4SLinus Torvalds 
13920dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
13930dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
13940dd2b474SMiklos Szeredi 
13950dd2b474SMiklos Szeredi 	dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
13961e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13971e7cb3dcSChuck Lever 
13980dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
13990dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14000dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14010dd2b474SMiklos Szeredi 			/*
14020dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14030dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14040dd2b474SMiklos Szeredi 			 * again
14050dd2b474SMiklos Szeredi 			 */
1406d9585277SAl Viro 			return -ENOENT;
14070dd2b474SMiklos Szeredi 		}
14081da177e4SLinus Torvalds 		goto no_open;
14091da177e4SLinus Torvalds 	}
14101da177e4SLinus Torvalds 
14110dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1412d9585277SAl Viro 		return -ENAMETOOLONG;
14131da177e4SLinus Torvalds 
14140dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1415536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14160dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14170dd2b474SMiklos Szeredi 	}
1418536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1419536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1420536e43d1STrond Myklebust 		attr.ia_size = 0;
1421cd9a1c0eSTrond Myklebust 	}
1422cd9a1c0eSTrond Myklebust 
14230dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14240dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14250dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1426d9585277SAl Viro 		goto out;
14270dd2b474SMiklos Szeredi 
1428f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14292b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
14300dd2b474SMiklos Szeredi 	d_drop(dentry);
1431f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1432f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1433cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
14340dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14350dd2b474SMiklos Szeredi 		switch (err) {
14361da177e4SLinus Torvalds 		case -ENOENT:
1437f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
14380dd2b474SMiklos Szeredi 			break;
14391788ea6eSJeff Layton 		case -EISDIR:
14406f926b5bSTrond Myklebust 		case -ENOTDIR:
14416f926b5bSTrond Myklebust 			goto no_open;
14421da177e4SLinus Torvalds 		case -ELOOP:
14430dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
14441da177e4SLinus Torvalds 				goto no_open;
14450dd2b474SMiklos Szeredi 			break;
14461da177e4SLinus Torvalds 			/* case -EINVAL: */
14471da177e4SLinus Torvalds 		default:
14480dd2b474SMiklos Szeredi 			break;
14491da177e4SLinus Torvalds 		}
14501da177e4SLinus Torvalds 		goto out;
14511da177e4SLinus Torvalds 	}
1452f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1453898f635cSTrond Myklebust 	if (res != NULL)
14540dd2b474SMiklos Szeredi 		dentry = res;
14550dd2b474SMiklos Szeredi 
14560dd2b474SMiklos Szeredi 	nfs_unblock_sillyrename(dentry->d_parent);
1457f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14580dd2b474SMiklos Szeredi 
145930d90494SAl Viro 	err = nfs_finish_open(ctx, dentry, file, open_flags, opened);
14600dd2b474SMiklos Szeredi 
14610dd2b474SMiklos Szeredi 	dput(res);
1462d9585277SAl Viro out:
1463d9585277SAl Viro 	return err;
14640dd2b474SMiklos Szeredi 
14651da177e4SLinus Torvalds no_open:
146600cd8dd3SAl Viro 	res = nfs_lookup(dir, dentry, 0);
14670dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
14680dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1469d9585277SAl Viro 		goto out;
14700dd2b474SMiklos Szeredi 
1471e45198a6SAl Viro 	return finish_no_open(file, res);
14721da177e4SLinus Torvalds }
147389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
14741da177e4SLinus Torvalds 
14750b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1478657e94b6SNick Piggin 	struct inode *inode;
14791da177e4SLinus Torvalds 	struct inode *dir;
148050de348cSMiklos Szeredi 	int ret = 0;
14811da177e4SLinus Torvalds 
1482fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1483657e94b6SNick Piggin 		return -ECHILD;
1484657e94b6SNick Piggin 
1485fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1486eda72afbSMiklos Szeredi 		goto no_open;
1487eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
14885584c306STrond Myklebust 		goto no_open;
148949f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
149049f9a0faSTrond Myklebust 		goto no_open;
14912b484297STrond Myklebust 
1492eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
14931da177e4SLinus Torvalds 	parent = dget_parent(dentry);
14941da177e4SLinus Torvalds 	dir = parent->d_inode;
14952b484297STrond Myklebust 
14961da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
14971da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
14981da177e4SLinus Torvalds 	 */
1499216d5d06STrond Myklebust 	if (inode == NULL) {
1500fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1501216d5d06STrond Myklebust 			ret = 1;
15021da177e4SLinus Torvalds 		goto out;
1503216d5d06STrond Myklebust 	}
1504216d5d06STrond Myklebust 
15051da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
15061da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15075584c306STrond Myklebust 		goto no_open_dput;
15081da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1509fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
15105584c306STrond Myklebust 		goto no_open_dput;
15111da177e4SLinus Torvalds 
15120ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1513898f635cSTrond Myklebust 	ret = 1;
15140ef97dcfSMiklos Szeredi 
15151da177e4SLinus Torvalds out:
15161da177e4SLinus Torvalds 	dput(parent);
15171da177e4SLinus Torvalds 	return ret;
1518535918f1STrond Myklebust 
15195584c306STrond Myklebust no_open_dput:
15201da177e4SLinus Torvalds 	dput(parent);
15215584c306STrond Myklebust no_open:
15220b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1523c0204fd2STrond Myklebust }
1524c0204fd2STrond Myklebust 
15251da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds /*
15281da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15291da177e4SLinus Torvalds  */
15301da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15311da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
15321da177e4SLinus Torvalds {
1533fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1534fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15351da177e4SLinus Torvalds 	struct inode *inode;
15361da177e4SLinus Torvalds 	int error = -EACCES;
15371da177e4SLinus Torvalds 
1538fab728e1STrond Myklebust 	d_drop(dentry);
1539fab728e1STrond Myklebust 
15401da177e4SLinus Torvalds 	/* We may have been initialized further down */
15411da177e4SLinus Torvalds 	if (dentry->d_inode)
1542fab728e1STrond Myklebust 		goto out;
15431da177e4SLinus Torvalds 	if (fhandle->size == 0) {
154480a16b21SBryan Schumaker 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
15451da177e4SLinus Torvalds 		if (error)
1546fab728e1STrond Myklebust 			goto out_error;
15471da177e4SLinus Torvalds 	}
15485724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15491da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15501da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15518fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
15521da177e4SLinus Torvalds 		if (error < 0)
1553fab728e1STrond Myklebust 			goto out_error;
15541da177e4SLinus Torvalds 	}
15551da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
155603f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
155703f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1558fab728e1STrond Myklebust 		goto out_error;
1559fab728e1STrond Myklebust 	d_add(dentry, inode);
1560fab728e1STrond Myklebust out:
1561fab728e1STrond Myklebust 	dput(parent);
15621da177e4SLinus Torvalds 	return 0;
1563fab728e1STrond Myklebust out_error:
1564fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1565fab728e1STrond Myklebust 	dput(parent);
1566fab728e1STrond Myklebust 	return error;
15671da177e4SLinus Torvalds }
1568ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds /*
15711da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15721da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15731da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15741da177e4SLinus Torvalds  * reply path made it appear to have failed.
15751da177e4SLinus Torvalds  */
1576597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1577ebfc3b49SAl Viro 		umode_t mode, bool excl)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	struct iattr attr;
1580ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
15811da177e4SLinus Torvalds 	int error;
15821da177e4SLinus Torvalds 
15831e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
15841e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	attr.ia_mode = mode;
15871da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15881da177e4SLinus Torvalds 
15898867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
15901da177e4SLinus Torvalds 	if (error != 0)
15911da177e4SLinus Torvalds 		goto out_err;
15921da177e4SLinus Torvalds 	return 0;
15931da177e4SLinus Torvalds out_err:
15941da177e4SLinus Torvalds 	d_drop(dentry);
15951da177e4SLinus Torvalds 	return error;
15961da177e4SLinus Torvalds }
1597ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds /*
16001da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16011da177e4SLinus Torvalds  */
1602597d9289SBryan Schumaker int
16031a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
16041da177e4SLinus Torvalds {
16051da177e4SLinus Torvalds 	struct iattr attr;
16061da177e4SLinus Torvalds 	int status;
16071da177e4SLinus Torvalds 
16081e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
16091e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16121da177e4SLinus Torvalds 		return -EINVAL;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	attr.ia_mode = mode;
16151da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16181da177e4SLinus Torvalds 	if (status != 0)
16191da177e4SLinus Torvalds 		goto out_err;
16201da177e4SLinus Torvalds 	return 0;
16211da177e4SLinus Torvalds out_err:
16221da177e4SLinus Torvalds 	d_drop(dentry);
16231da177e4SLinus Torvalds 	return status;
16241da177e4SLinus Torvalds }
1625ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds /*
16281da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16291da177e4SLinus Torvalds  */
1630597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	struct iattr attr;
16331da177e4SLinus Torvalds 	int error;
16341da177e4SLinus Torvalds 
16351e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16361e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16391da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16421da177e4SLinus Torvalds 	if (error != 0)
16431da177e4SLinus Torvalds 		goto out_err;
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds out_err:
16461da177e4SLinus Torvalds 	d_drop(dentry);
16471da177e4SLinus Torvalds 	return error;
16481da177e4SLinus Torvalds }
1649ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
16501da177e4SLinus Torvalds 
1651d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1652d45b9d8bSTrond Myklebust {
1653d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1654d45b9d8bSTrond Myklebust 		d_delete(dentry);
1655d45b9d8bSTrond Myklebust }
1656d45b9d8bSTrond Myklebust 
1657597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16581da177e4SLinus Torvalds {
16591da177e4SLinus Torvalds 	int error;
16601da177e4SLinus Torvalds 
16611e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16621e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16651da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
16661da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1667ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1668d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1669d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	return error;
16721da177e4SLinus Torvalds }
1673ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds /*
16761da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
16771da177e4SLinus Torvalds  * and after checking that the file has only one user.
16781da177e4SLinus Torvalds  *
16791da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
16801da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
16811da177e4SLinus Torvalds  */
16821da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
16851da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16861da177e4SLinus Torvalds 	int error = -EBUSY;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
16891da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
16921da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16931da177e4SLinus Torvalds 		error = 0;
16941da177e4SLinus Torvalds 		goto out;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	if (inode != NULL) {
169857ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
16991da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17001da177e4SLinus Torvalds 		if (error == 0)
17011b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17021da177e4SLinus Torvalds 	} else
17031da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1704d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1705d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17061da177e4SLinus Torvalds out:
17071da177e4SLinus Torvalds 	return error;
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17111da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17121da177e4SLinus Torvalds  *
17131da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17141da177e4SLinus Torvalds  */
1715597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	int error;
17181da177e4SLinus Torvalds 	int need_rehash = 0;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17211da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
1724*84d08fa8SAl Viro 	if (d_count(dentry) > 1) {
17251da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1726ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1727ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17281da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17291da177e4SLinus Torvalds 		return error;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17321da177e4SLinus Torvalds 		__d_drop(dentry);
17331da177e4SLinus Torvalds 		need_rehash = 1;
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17361da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1737d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17381da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17391da177e4SLinus Torvalds 	} else if (need_rehash)
17401da177e4SLinus Torvalds 		d_rehash(dentry);
17411da177e4SLinus Torvalds 	return error;
17421da177e4SLinus Torvalds }
1743ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
17441da177e4SLinus Torvalds 
1745873101b3SChuck Lever /*
1746873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1747873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1748873101b3SChuck Lever  * using prepare_write/commit_write.
1749873101b3SChuck Lever  *
1750873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1751873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1752873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1753873101b3SChuck Lever  * symlink request has completed on the server.
1754873101b3SChuck Lever  *
1755873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1756873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1757873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1758873101b3SChuck Lever  * and move the raw page into its mapping.
1759873101b3SChuck Lever  */
1760597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17611da177e4SLinus Torvalds {
1762873101b3SChuck Lever 	struct page *page;
1763873101b3SChuck Lever 	char *kaddr;
17641da177e4SLinus Torvalds 	struct iattr attr;
1765873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17661da177e4SLinus Torvalds 	int error;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
17691da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
17701da177e4SLinus Torvalds 
1771873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1772873101b3SChuck Lever 		return -ENAMETOOLONG;
17731da177e4SLinus Torvalds 
1774873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1775873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
17761da177e4SLinus Torvalds 
177783d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
177876566991STrond Myklebust 	if (!page)
1779873101b3SChuck Lever 		return -ENOMEM;
1780873101b3SChuck Lever 
17812b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1782873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1783873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1784873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
17852b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1786873101b3SChuck Lever 
178794a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1788873101b3SChuck Lever 	if (error != 0) {
1789873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1790873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1791873101b3SChuck Lever 			dentry->d_name.name, symname, error);
17921da177e4SLinus Torvalds 		d_drop(dentry);
1793873101b3SChuck Lever 		__free_page(page);
17941da177e4SLinus Torvalds 		return error;
17951da177e4SLinus Torvalds 	}
17961da177e4SLinus Torvalds 
1797873101b3SChuck Lever 	/*
1798873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1799873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1800873101b3SChuck Lever 	 */
1801a0b8cab3SMel Gorman 	if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
1802873101b3SChuck Lever 							GFP_KERNEL)) {
1803873101b3SChuck Lever 		SetPageUptodate(page);
1804873101b3SChuck Lever 		unlock_page(page);
1805873101b3SChuck Lever 	} else
1806873101b3SChuck Lever 		__free_page(page);
1807873101b3SChuck Lever 
1808873101b3SChuck Lever 	return 0;
1809873101b3SChuck Lever }
1810ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1811873101b3SChuck Lever 
1812597d9289SBryan Schumaker int
18131da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18141da177e4SLinus Torvalds {
18151da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18161da177e4SLinus Torvalds 	int error;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18191da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18201da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18211da177e4SLinus Torvalds 
182257ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
18239a3936aaSTrond Myklebust 
18249697d234STrond Myklebust 	d_drop(dentry);
18251da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1826cf809556STrond Myklebust 	if (error == 0) {
18277de9c6eeSAl Viro 		ihold(inode);
18289697d234STrond Myklebust 		d_add(dentry, inode);
1829cf809556STrond Myklebust 	}
18301da177e4SLinus Torvalds 	return error;
18311da177e4SLinus Torvalds }
1832ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds /*
18351da177e4SLinus Torvalds  * RENAME
18361da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18371da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18381da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18391da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18401da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18411da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18421da177e4SLinus Torvalds  *
18431da177e4SLinus Torvalds  * FIXED.
18441da177e4SLinus Torvalds  *
18451da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18461da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18471da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18481da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18491da177e4SLinus Torvalds  * using the inode layer
18501da177e4SLinus Torvalds  *
18511da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18521da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18531da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18541da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18551da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18561da177e4SLinus Torvalds  * the rename.
18571da177e4SLinus Torvalds  */
1858597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18591da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18621da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18631da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
18641da177e4SLinus Torvalds 	int error = -EBUSY;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
18671da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18681da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1869*84d08fa8SAl Viro 		 d_count(new_dentry));
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	/*
187228f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
187328f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
187428f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
187528f79a1aSMiklos Szeredi 	 * the new target.
18761da177e4SLinus Torvalds 	 */
187727226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
187827226104SMiklos Szeredi 		/*
187927226104SMiklos Szeredi 		 * To prevent any new references to the target during the
188027226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
188127226104SMiklos Szeredi 		 */
188227226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
188327226104SMiklos Szeredi 			d_drop(new_dentry);
188427226104SMiklos Szeredi 			rehash = new_dentry;
188527226104SMiklos Szeredi 		}
188627226104SMiklos Szeredi 
1887*84d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
18881da177e4SLinus Torvalds 			int err;
188927226104SMiklos Szeredi 
18901da177e4SLinus Torvalds 			/* copy the target dentry's name */
18911da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
18921da177e4SLinus Torvalds 					 &new_dentry->d_name);
18931da177e4SLinus Torvalds 			if (!dentry)
18941da177e4SLinus Torvalds 				goto out;
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
18971da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
189824e93025SMiklos Szeredi 			if (err)
18991da177e4SLinus Torvalds 				goto out;
190024e93025SMiklos Szeredi 
190124e93025SMiklos Szeredi 			new_dentry = dentry;
190256335936SOGAWA Hirofumi 			rehash = NULL;
190324e93025SMiklos Szeredi 			new_inode = NULL;
1904b1e4adf4STrond Myklebust 		}
190527226104SMiklos Szeredi 	}
19061da177e4SLinus Torvalds 
190757ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1908b1e4adf4STrond Myklebust 	if (new_inode != NULL)
190957ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19121da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19135ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19141da177e4SLinus Torvalds out:
19151da177e4SLinus Torvalds 	if (rehash)
19161da177e4SLinus Torvalds 		d_rehash(rehash);
19171da177e4SLinus Torvalds 	if (!error) {
1918b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1919b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19201da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19218fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19228fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1923d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1924d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	/* new dentry created? */
19271da177e4SLinus Torvalds 	if (dentry)
19281da177e4SLinus Torvalds 		dput(dentry);
19291da177e4SLinus Torvalds 	return error;
19301da177e4SLinus Torvalds }
1931ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
19321da177e4SLinus Torvalds 
1933cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1934cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1935cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1936cfcea3e8STrond Myklebust 
19371c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19381c3c07e9STrond Myklebust {
19391c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19401c3c07e9STrond Myklebust 	kfree(entry);
1941cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1942cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1943cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19441c3c07e9STrond Myklebust }
19451c3c07e9STrond Myklebust 
19461a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19471a81bb8aSTrond Myklebust {
19481a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19491a81bb8aSTrond Myklebust 
19501a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19511a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19521a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19531a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19541a81bb8aSTrond Myklebust 	}
19551a81bb8aSTrond Myklebust }
19561a81bb8aSTrond Myklebust 
19571495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink,
19581495f230SYing Han 			      struct shrink_control *sc)
1959979df72eSTrond Myklebust {
1960979df72eSTrond Myklebust 	LIST_HEAD(head);
1961aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1962979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
19631495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
19641495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
1965979df72eSTrond Myklebust 
196661d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
196761d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
19689c7e7e23STrond Myklebust 
1969a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1970aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1971979df72eSTrond Myklebust 		struct inode *inode;
1972979df72eSTrond Myklebust 
1973979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1974979df72eSTrond Myklebust 			break;
19759c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
1976979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1977979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1978979df72eSTrond Myklebust 			goto remove_lru_entry;
1979979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1980979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1981979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1982979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1983979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1984979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1985979df72eSTrond Myklebust 					&nfs_access_lru_list);
1986979df72eSTrond Myklebust 		else {
1987979df72eSTrond Myklebust remove_lru_entry:
1988979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
19899c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
1990979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
19919c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
1992979df72eSTrond Myklebust 		}
199359844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
1994979df72eSTrond Myklebust 	}
1995979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19961a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
1997979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1998979df72eSTrond Myklebust }
1999979df72eSTrond Myklebust 
20001a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20011c3c07e9STrond Myklebust {
20021c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20031a81bb8aSTrond Myklebust 	struct rb_node *n;
20041c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20051c3c07e9STrond Myklebust 
20061c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20071c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20081c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20091c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20101a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20111c3c07e9STrond Myklebust 	}
20121c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20131c3c07e9STrond Myklebust }
20141c3c07e9STrond Myklebust 
20151c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20161c3c07e9STrond Myklebust {
20171a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20181a81bb8aSTrond Myklebust 
20191a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20201a81bb8aSTrond Myklebust 		return;
2021cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2022cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20231a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2024cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2025cfcea3e8STrond Myklebust 
20261c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20271a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20281a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20291a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20301a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20311c3c07e9STrond Myklebust }
20321c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
20331c3c07e9STrond Myklebust 
20341c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20351c3c07e9STrond Myklebust {
20361c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20371c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20381c3c07e9STrond Myklebust 
20391c3c07e9STrond Myklebust 	while (n != NULL) {
20401c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20411c3c07e9STrond Myklebust 
20421c3c07e9STrond Myklebust 		if (cred < entry->cred)
20431c3c07e9STrond Myklebust 			n = n->rb_left;
20441c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20451c3c07e9STrond Myklebust 			n = n->rb_right;
20461c3c07e9STrond Myklebust 		else
20471c3c07e9STrond Myklebust 			return entry;
20481c3c07e9STrond Myklebust 	}
20491c3c07e9STrond Myklebust 	return NULL;
20501c3c07e9STrond Myklebust }
20511c3c07e9STrond Myklebust 
2052af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20531da177e4SLinus Torvalds {
205455296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
20551c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
20561c3c07e9STrond Myklebust 	int err = -ENOENT;
20571da177e4SLinus Torvalds 
20581c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20591c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
20601c3c07e9STrond Myklebust 		goto out_zap;
20611c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20621c3c07e9STrond Myklebust 	if (cache == NULL)
20631c3c07e9STrond Myklebust 		goto out;
2064b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
206564672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
20661c3c07e9STrond Myklebust 		goto out_stale;
20671c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
20681c3c07e9STrond Myklebust 	res->cred = cache->cred;
20691c3c07e9STrond Myklebust 	res->mask = cache->mask;
2070cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
20711c3c07e9STrond Myklebust 	err = 0;
20721c3c07e9STrond Myklebust out:
20731c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20741c3c07e9STrond Myklebust 	return err;
20751c3c07e9STrond Myklebust out_stale:
20761c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2077cfcea3e8STrond Myklebust 	list_del(&cache->lru);
20781c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20791c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
20801da177e4SLinus Torvalds 	return -ENOENT;
20811c3c07e9STrond Myklebust out_zap:
20821a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20831a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
20841c3c07e9STrond Myklebust 	return -ENOENT;
20851c3c07e9STrond Myklebust }
20861c3c07e9STrond Myklebust 
20871c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
20881c3c07e9STrond Myklebust {
2089cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2090cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20911c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
20921c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
20931c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20941c3c07e9STrond Myklebust 
20951c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20961c3c07e9STrond Myklebust 	while (*p != NULL) {
20971c3c07e9STrond Myklebust 		parent = *p;
20981c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
20991c3c07e9STrond Myklebust 
21001c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21011c3c07e9STrond Myklebust 			p = &parent->rb_left;
21021c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21031c3c07e9STrond Myklebust 			p = &parent->rb_right;
21041c3c07e9STrond Myklebust 		else
21051c3c07e9STrond Myklebust 			goto found;
21061c3c07e9STrond Myklebust 	}
21071c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21081c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2109cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21101c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21111c3c07e9STrond Myklebust 	return;
21121c3c07e9STrond Myklebust found:
21131c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2114cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2115cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21161c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21171c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21181da177e4SLinus Torvalds }
21191da177e4SLinus Torvalds 
21206168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21211da177e4SLinus Torvalds {
21221c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21231c3c07e9STrond Myklebust 	if (cache == NULL)
21241c3c07e9STrond Myklebust 		return;
21251c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21261da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21271c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21281da177e4SLinus Torvalds 	cache->mask = set->mask;
21291c3c07e9STrond Myklebust 
21301c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2131cfcea3e8STrond Myklebust 
2132cfcea3e8STrond Myklebust 	/* Update accounting */
2133cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2134cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2135cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2136cfcea3e8STrond Myklebust 
2137cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21381a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2139cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21401a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21411a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21421a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2143cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2144cfcea3e8STrond Myklebust 	}
21451da177e4SLinus Torvalds }
21466168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
21476168f62cSWeston Andros Adamson 
21486168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
21496168f62cSWeston Andros Adamson {
21506168f62cSWeston Andros Adamson 	entry->mask = 0;
21516168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
21526168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
21536168f62cSWeston Andros Adamson 	if (access_result &
21546168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
21556168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
21566168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
21576168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
21586168f62cSWeston Andros Adamson }
21596168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21641da177e4SLinus Torvalds 	int status;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21671da177e4SLinus Torvalds 	if (status == 0)
21681da177e4SLinus Torvalds 		goto out;
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
21711da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
21721da177e4SLinus Torvalds 	cache.cred = cred;
21731da177e4SLinus Torvalds 	cache.jiffies = jiffies;
21741da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2175a71ee337SSuresh Jayaraman 	if (status != 0) {
2176a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2177a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2178a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2179a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2180a71ee337SSuresh Jayaraman 		}
21811da177e4SLinus Torvalds 		return status;
2182a71ee337SSuresh Jayaraman 	}
21831da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
21841da177e4SLinus Torvalds out:
2185e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21861da177e4SLinus Torvalds 		return 0;
21871da177e4SLinus Torvalds 	return -EACCES;
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
2190af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2191af22f94aSTrond Myklebust {
2192af22f94aSTrond Myklebust 	int mask = 0;
2193af22f94aSTrond Myklebust 
2194f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2195f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2196f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2197f8d9a897SWeston Andros Adamson 	} else {
21988a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2199af22f94aSTrond Myklebust 			mask |= MAY_READ;
22008a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2201af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2202f8d9a897SWeston Andros Adamson 	}
2203f8d9a897SWeston Andros Adamson 
2204af22f94aSTrond Myklebust 	return mask;
2205af22f94aSTrond Myklebust }
2206af22f94aSTrond Myklebust 
2207af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2208af22f94aSTrond Myklebust {
2209af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2210af22f94aSTrond Myklebust }
221189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2212af22f94aSTrond Myklebust 
221310556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
22141da177e4SLinus Torvalds {
22151da177e4SLinus Torvalds 	struct rpc_cred *cred;
22161da177e4SLinus Torvalds 	int res = 0;
22171da177e4SLinus Torvalds 
221810556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2219b74c79e9SNick Piggin 		return -ECHILD;
2220b74c79e9SNick Piggin 
222191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
222291d5b470SChuck Lever 
2223e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22241da177e4SLinus Torvalds 		goto out;
22251da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22269cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22271da177e4SLinus Torvalds 		goto force_lookup;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22301da177e4SLinus Torvalds 		case S_IFLNK:
22311da177e4SLinus Torvalds 			goto out;
22321da177e4SLinus Torvalds 		case S_IFREG:
22331da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
22341da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
22357ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
22367ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
22371da177e4SLinus Torvalds 				goto out;
22381da177e4SLinus Torvalds 			break;
22391da177e4SLinus Torvalds 		case S_IFDIR:
22401da177e4SLinus Torvalds 			/*
22411da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22421da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22431da177e4SLinus Torvalds 			 */
22441da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22451da177e4SLinus Torvalds 				goto out;
22461da177e4SLinus Torvalds 	}
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds force_lookup:
22491da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22501da177e4SLinus Torvalds 		goto out_notsup;
22511da177e4SLinus Torvalds 
225298a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22531da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22541da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
22551da177e4SLinus Torvalds 		put_rpccred(cred);
22561da177e4SLinus Torvalds 	} else
22571da177e4SLinus Torvalds 		res = PTR_ERR(cred);
22581da177e4SLinus Torvalds out:
2259f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2260f696a365SMiklos Szeredi 		res = -EACCES;
2261f696a365SMiklos Szeredi 
22621e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
22631e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22641da177e4SLinus Torvalds 	return res;
22651da177e4SLinus Torvalds out_notsup:
22661da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22671da177e4SLinus Torvalds 	if (res == 0)
22682830ba7fSAl Viro 		res = generic_permission(inode, mask);
22691e7cb3dcSChuck Lever 	goto out;
22701da177e4SLinus Torvalds }
2271ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds /*
22741da177e4SLinus Torvalds  * Local variables:
22751da177e4SLinus Torvalds  *  version-control: t
22761da177e4SLinus Torvalds  *  kept-new-versions: 5
22771da177e4SLinus Torvalds  * End:
22781da177e4SLinus Torvalds  */
2279