xref: /openbmc/linux/fs/nfs/dir.c (revision 1e8968c5b0582392d5f132422f581e3ebc24e627)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
301da177e4SLinus Torvalds #include <linux/nfs_fs.h>
311da177e4SLinus Torvalds #include <linux/nfs_mount.h>
321da177e4SLinus Torvalds #include <linux/pagemap.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
36a0b8cab3SMel Gorman #include <linux/swap.h>
37e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "delegation.h"
4291d5b470SChuck Lever #include "iostat.h"
434c30d56eSAdrian Bunk #include "internal.h"
44cd9a1c0eSTrond Myklebust #include "fscache.h"
451da177e4SLinus Torvalds 
46f4ce1299STrond Myklebust #include "nfstrace.h"
47f4ce1299STrond Myklebust 
481da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
51480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5223db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
54f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5511de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
561da177e4SLinus Torvalds 
574b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
58f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
591da177e4SLinus Torvalds 	.read		= generic_read_dir,
6023db8620SAl Viro 	.iterate	= nfs_readdir,
611da177e4SLinus Torvalds 	.open		= nfs_opendir,
62480c2006SBryan Schumaker 	.release	= nfs_closedir,
631da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
6611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6711de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
68d1bacf9eSBryan Schumaker };
69d1bacf9eSBryan Schumaker 
700c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
71480c2006SBryan Schumaker {
72480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
73480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
74480c2006SBryan Schumaker 	if (ctx != NULL) {
758ef2ce3eSBryan Schumaker 		ctx->duped = 0;
760c030806STrond Myklebust 		ctx->attr_gencount = NFS_I(dir)->attr_gencount;
77480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
788ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
79480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
80480c2006SBryan Schumaker 		return ctx;
81480c2006SBryan Schumaker 	}
820c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
830c030806STrond Myklebust }
84480c2006SBryan Schumaker 
85480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
86480c2006SBryan Schumaker {
87480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
88480c2006SBryan Schumaker 	kfree(ctx);
89480c2006SBryan Schumaker }
90480c2006SBryan Schumaker 
911da177e4SLinus Torvalds /*
921da177e4SLinus Torvalds  * Open file
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds static int
951da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
961da177e4SLinus Torvalds {
97480c2006SBryan Schumaker 	int res = 0;
98480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
99480c2006SBryan Schumaker 	struct rpc_cred *cred;
1001da177e4SLinus Torvalds 
1016de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
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()) {
2986de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
2990c030806STrond Myklebust 								"Please contact your server vendor.  "
300374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
3016de1472fSAl Viro 								desc->file,
302374e4e3eSBryan Schumaker 								array->array[i].string.name,
3030c030806STrond Myklebust 								*desc->dir_cookie);
3040c030806STrond Myklebust 					}
3050c030806STrond Myklebust 					status = -ELOOP;
3060c030806STrond Myklebust 					goto out;
3070c030806STrond Myklebust 				}
3088ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3090c030806STrond Myklebust 				ctx->duped = -1;
3108ef2ce3eSBryan Schumaker 			}
31123db8620SAl Viro 			desc->ctx->pos = new_pos;
3128cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
31347c716cbSTrond Myklebust 			return 0;
3148cd51a0cSTrond Myklebust 		}
3158cd51a0cSTrond Myklebust 	}
31647c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
317d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
31818fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
31918fb5fe4STrond Myklebust 			desc->eof = 1;
320d1bacf9eSBryan Schumaker 	}
3210c030806STrond Myklebust out:
322d1bacf9eSBryan Schumaker 	return status;
323d1bacf9eSBryan Schumaker }
324d1bacf9eSBryan Schumaker 
325d1bacf9eSBryan Schumaker static
326d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
327d1bacf9eSBryan Schumaker {
328d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
32947c716cbSTrond Myklebust 	int status;
330d1bacf9eSBryan Schumaker 
331d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
332d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
333d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
334d1bacf9eSBryan Schumaker 		goto out;
335d1bacf9eSBryan Schumaker 	}
336d1bacf9eSBryan Schumaker 
337d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
338d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
339d1bacf9eSBryan Schumaker 	else
340d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
341d1bacf9eSBryan Schumaker 
34247c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3430aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
344e47c085aSTrond Myklebust 		desc->current_index += array->size;
34547c716cbSTrond Myklebust 		desc->page_index++;
34647c716cbSTrond Myklebust 	}
347d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
348d1bacf9eSBryan Schumaker out:
349d1bacf9eSBryan Schumaker 	return status;
350d1bacf9eSBryan Schumaker }
351d1bacf9eSBryan Schumaker 
352d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
353d1bacf9eSBryan Schumaker static
35456e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
355d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
356d1bacf9eSBryan Schumaker {
357480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
358480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3594704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3601da177e4SLinus Torvalds 	int		error;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds  again:
3631da177e4SLinus Torvalds 	timestamp = jiffies;
3644704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
36556e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3661da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3671da177e4SLinus Torvalds 	if (error < 0) {
3681da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3691da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3701da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3713a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3721da177e4SLinus Torvalds 			desc->plus = 0;
3731da177e4SLinus Torvalds 			goto again;
3741da177e4SLinus Torvalds 		}
3751da177e4SLinus Torvalds 		goto error;
3761da177e4SLinus Torvalds 	}
3771f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3784704f0e2STrond Myklebust 	desc->gencount = gencount;
379d1bacf9eSBryan Schumaker error:
380d1bacf9eSBryan Schumaker 	return error;
381d1bacf9eSBryan Schumaker }
382d1bacf9eSBryan Schumaker 
383573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
384573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
385d1bacf9eSBryan Schumaker {
386573c4e1eSChuck Lever 	int error;
387d1bacf9eSBryan Schumaker 
388573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
389573c4e1eSChuck Lever 	if (error)
390573c4e1eSChuck Lever 		return error;
391d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
392d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
393d1bacf9eSBryan Schumaker 	return 0;
394d1bacf9eSBryan Schumaker }
395d1bacf9eSBryan Schumaker 
396d39ab9deSBryan Schumaker static
397d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
398d39ab9deSBryan Schumaker {
399d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
400d39ab9deSBryan Schumaker 		goto different;
40137a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
402d39ab9deSBryan Schumaker 		goto different;
403d39ab9deSBryan Schumaker 	return 1;
404d39ab9deSBryan Schumaker different:
405d39ab9deSBryan Schumaker 	return 0;
406d39ab9deSBryan Schumaker }
407d39ab9deSBryan Schumaker 
408d39ab9deSBryan Schumaker static
40923db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
410d69ee9b8STrond Myklebust {
411d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
412d69ee9b8STrond Myklebust 		return false;
413d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
414d69ee9b8STrond Myklebust 		return true;
41523db8620SAl Viro 	if (ctx->pos == 0)
416d69ee9b8STrond Myklebust 		return true;
417d69ee9b8STrond Myklebust 	return false;
418d69ee9b8STrond Myklebust }
419d69ee9b8STrond Myklebust 
420d69ee9b8STrond Myklebust /*
421d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
422d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
423d69ee9b8STrond Myklebust  * directory.
424d69ee9b8STrond Myklebust  */
425d69ee9b8STrond Myklebust static
426d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
427d69ee9b8STrond Myklebust {
428d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
429d69ee9b8STrond Myklebust }
430d69ee9b8STrond Myklebust 
431d69ee9b8STrond Myklebust static
432d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
433d39ab9deSBryan Schumaker {
43426fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4354a201d6eSTrond Myklebust 	struct dentry *dentry;
4364a201d6eSTrond Myklebust 	struct dentry *alias;
437d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
438d39ab9deSBryan Schumaker 	struct inode *inode;
439aa9c2669SDavid Quigley 	int status;
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)) {
452cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
453aa9c2669SDavid Quigley 			status = nfs_refresh_inode(dentry->d_inode, entry->fattr);
454aa9c2669SDavid Quigley 			if (!status)
455aa9c2669SDavid Quigley 				nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label);
456d39ab9deSBryan Schumaker 			goto out;
457d39ab9deSBryan Schumaker 		} else {
458696199f8SAl Viro 			if (d_invalidate(dentry) != 0)
459696199f8SAl Viro 				goto out;
460d39ab9deSBryan Schumaker 			dput(dentry);
461d39ab9deSBryan Schumaker 		}
462d39ab9deSBryan Schumaker 	}
463d39ab9deSBryan Schumaker 
464d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4654a201d6eSTrond Myklebust 	if (dentry == NULL)
4664a201d6eSTrond Myklebust 		return;
4674a201d6eSTrond Myklebust 
4681775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
469d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
470d39ab9deSBryan Schumaker 		goto out;
471d39ab9deSBryan Schumaker 
472d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
473d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
474d39ab9deSBryan Schumaker 		goto out;
475d39ab9deSBryan Schumaker 	else if (alias) {
476d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
477d39ab9deSBryan Schumaker 		dput(alias);
478d39ab9deSBryan Schumaker 	} else
479d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
480d39ab9deSBryan Schumaker 
481d39ab9deSBryan Schumaker out:
482d39ab9deSBryan Schumaker 	dput(dentry);
483d39ab9deSBryan Schumaker }
484d39ab9deSBryan Schumaker 
485d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
486d1bacf9eSBryan Schumaker static
4878cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
4886650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
489d1bacf9eSBryan Schumaker {
490babddc72SBryan Schumaker 	struct xdr_stream stream;
491f7da7a12SBenny Halevy 	struct xdr_buf buf;
4926650239aSTrond Myklebust 	struct page *scratch;
49399424380SBryan Schumaker 	struct nfs_cache_array *array;
4945c346854STrond Myklebust 	unsigned int count = 0;
4955c346854STrond Myklebust 	int status;
496babddc72SBryan Schumaker 
4976650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
4986650239aSTrond Myklebust 	if (scratch == NULL)
4996650239aSTrond Myklebust 		return -ENOMEM;
500babddc72SBryan Schumaker 
501f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5026650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
50399424380SBryan Schumaker 
50499424380SBryan Schumaker 	do {
50599424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5068cd51a0cSTrond Myklebust 		if (status != 0) {
5078cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5088cd51a0cSTrond Myklebust 				status = 0;
50999424380SBryan Schumaker 			break;
5108cd51a0cSTrond Myklebust 		}
51199424380SBryan Schumaker 
5125c346854STrond Myklebust 		count++;
5135c346854STrond Myklebust 
51447c716cbSTrond Myklebust 		if (desc->plus != 0)
515d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5168cd51a0cSTrond Myklebust 
5178cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5188cd51a0cSTrond Myklebust 		if (status != 0)
5198cd51a0cSTrond Myklebust 			break;
52099424380SBryan Schumaker 	} while (!entry->eof);
52199424380SBryan Schumaker 
52247c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
52399424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5248cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5258cd51a0cSTrond Myklebust 			array->eof_index = array->size;
52699424380SBryan Schumaker 			status = 0;
52799424380SBryan Schumaker 			nfs_readdir_release_array(page);
5285c346854STrond Myklebust 		} else
5295c346854STrond Myklebust 			status = PTR_ERR(array);
53056e4ebf8SBryan Schumaker 	}
5316650239aSTrond Myklebust 
5326650239aSTrond Myklebust 	put_page(scratch);
5338cd51a0cSTrond Myklebust 	return status;
5348cd51a0cSTrond Myklebust }
53556e4ebf8SBryan Schumaker 
53656e4ebf8SBryan Schumaker static
53756e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
53856e4ebf8SBryan Schumaker {
53956e4ebf8SBryan Schumaker 	unsigned int i;
54056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
54156e4ebf8SBryan Schumaker 		put_page(pages[i]);
54256e4ebf8SBryan Schumaker }
54356e4ebf8SBryan Schumaker 
54456e4ebf8SBryan Schumaker static
54556e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
54656e4ebf8SBryan Schumaker 		unsigned int npages)
54756e4ebf8SBryan Schumaker {
54856e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
54956e4ebf8SBryan Schumaker }
55056e4ebf8SBryan Schumaker 
55156e4ebf8SBryan Schumaker /*
55256e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
55356e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
55456e4ebf8SBryan Schumaker  */
55556e4ebf8SBryan Schumaker static
5566650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
55756e4ebf8SBryan Schumaker {
55856e4ebf8SBryan Schumaker 	unsigned int i;
55956e4ebf8SBryan Schumaker 
56056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
56156e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
56256e4ebf8SBryan Schumaker 		if (page == NULL)
56356e4ebf8SBryan Schumaker 			goto out_freepages;
56456e4ebf8SBryan Schumaker 		pages[i] = page;
56556e4ebf8SBryan Schumaker 	}
5666650239aSTrond Myklebust 	return 0;
56756e4ebf8SBryan Schumaker 
56856e4ebf8SBryan Schumaker out_freepages:
56956e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
5706650239aSTrond Myklebust 	return -ENOMEM;
571d1bacf9eSBryan Schumaker }
572d1bacf9eSBryan Schumaker 
573d1bacf9eSBryan Schumaker static
574d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
575d1bacf9eSBryan Schumaker {
57656e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
57756e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
578d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
579d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
580d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5818cd51a0cSTrond Myklebust 	int status = -ENOMEM;
58256e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
583d1bacf9eSBryan Schumaker 
584d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
5850aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
586d1bacf9eSBryan Schumaker 	entry.eof = 0;
587d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
588d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
589573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
590d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
591d1bacf9eSBryan Schumaker 		goto out;
592d1bacf9eSBryan Schumaker 
59314c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
59414c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
59514c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
59614c43f76SDavid Quigley 		goto out;
59714c43f76SDavid Quigley 	}
59814c43f76SDavid Quigley 
599d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6008cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6018cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
60214c43f76SDavid Quigley 		goto out_label_free;
6038cd51a0cSTrond Myklebust 	}
604d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
605d1bacf9eSBryan Schumaker 	array->eof_index = -1;
606d1bacf9eSBryan Schumaker 
6076650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6086650239aSTrond Myklebust 	if (status < 0)
609d1bacf9eSBryan Schumaker 		goto out_release_array;
610d1bacf9eSBryan Schumaker 	do {
611ac396128STrond Myklebust 		unsigned int pglen;
61256e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
613babddc72SBryan Schumaker 
614d1bacf9eSBryan Schumaker 		if (status < 0)
615d1bacf9eSBryan Schumaker 			break;
616ac396128STrond Myklebust 		pglen = status;
6176650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6188cd51a0cSTrond Myklebust 		if (status < 0) {
6198cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6208cd51a0cSTrond Myklebust 				status = 0;
6218cd51a0cSTrond Myklebust 			break;
6228cd51a0cSTrond Myklebust 		}
6238cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
624d1bacf9eSBryan Schumaker 
62556e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
626d1bacf9eSBryan Schumaker out_release_array:
627d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
62814c43f76SDavid Quigley out_label_free:
62914c43f76SDavid Quigley 	nfs4_label_free(entry.label);
630d1bacf9eSBryan Schumaker out:
631d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
632d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
633d1bacf9eSBryan Schumaker 	return status;
634d1bacf9eSBryan Schumaker }
635d1bacf9eSBryan Schumaker 
636d1bacf9eSBryan Schumaker /*
637d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
638d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
639d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
640d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6411da177e4SLinus Torvalds  */
642d1bacf9eSBryan Schumaker static
643d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
644d1bacf9eSBryan Schumaker {
645496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6468cd51a0cSTrond Myklebust 	int ret;
647d1bacf9eSBryan Schumaker 
6488cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6498cd51a0cSTrond Myklebust 	if (ret < 0)
650d1bacf9eSBryan Schumaker 		goto error;
651d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
652d1bacf9eSBryan Schumaker 
6532aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
654cd9ae2b6STrond Myklebust 		/* Should never happen */
655cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
656cd9ae2b6STrond Myklebust 	}
6571da177e4SLinus Torvalds 	unlock_page(page);
6581da177e4SLinus Torvalds 	return 0;
6591da177e4SLinus Torvalds  error:
6601da177e4SLinus Torvalds 	unlock_page(page);
6618cd51a0cSTrond Myklebust 	return ret;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
664d1bacf9eSBryan Schumaker static
665d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6661da177e4SLinus Torvalds {
66711de3b11STrond Myklebust 	if (!desc->page->mapping)
66811de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6691da177e4SLinus Torvalds 	page_cache_release(desc->page);
6701da177e4SLinus Torvalds 	desc->page = NULL;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
673d1bacf9eSBryan Schumaker static
674d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6751da177e4SLinus Torvalds {
676496ad9aaSAl Viro 	return read_cache_page(file_inode(desc->file)->i_mapping,
677d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /*
681d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6821da177e4SLinus Torvalds  */
683d1bacf9eSBryan Schumaker static
684d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
685d1bacf9eSBryan Schumaker {
686d1bacf9eSBryan Schumaker 	int res;
687d1bacf9eSBryan Schumaker 
688d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
689d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
690d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
691d1bacf9eSBryan Schumaker 
692d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
69347c716cbSTrond Myklebust 	if (res != 0)
694d1bacf9eSBryan Schumaker 		cache_page_release(desc);
695d1bacf9eSBryan Schumaker 	return res;
696d1bacf9eSBryan Schumaker }
697d1bacf9eSBryan Schumaker 
698d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
6991da177e4SLinus Torvalds static inline
7001da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7011da177e4SLinus Torvalds {
7028cd51a0cSTrond Myklebust 	int res;
703d1bacf9eSBryan Schumaker 
7040aded708STrond Myklebust 	if (desc->page_index == 0) {
7058cd51a0cSTrond Myklebust 		desc->current_index = 0;
7060aded708STrond Myklebust 		desc->last_cookie = 0;
7070aded708STrond Myklebust 	}
70847c716cbSTrond Myklebust 	do {
709d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
71047c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7111da177e4SLinus Torvalds 	return res;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds /*
7151da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7161da177e4SLinus Torvalds  */
7171da177e4SLinus Torvalds static
71823db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct file	*file = desc->file;
721d1bacf9eSBryan Schumaker 	int i = 0;
722d1bacf9eSBryan Schumaker 	int res = 0;
723d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7248ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7258ef2ce3eSBryan Schumaker 
726d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
727e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
728e7c58e97STrond Myklebust 		res = PTR_ERR(array);
729e7c58e97STrond Myklebust 		goto out;
730e7c58e97STrond Myklebust 	}
7311da177e4SLinus Torvalds 
732d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
733ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7341da177e4SLinus Torvalds 
735ece0b423STrond Myklebust 		ent = &array->array[i];
73623db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
73723db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
738ece0b423STrond Myklebust 			desc->eof = 1;
7391da177e4SLinus Torvalds 			break;
740ece0b423STrond Myklebust 		}
74123db8620SAl Viro 		desc->ctx->pos++;
742d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
743d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
744d1bacf9eSBryan Schumaker 		else
745d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7460c030806STrond Myklebust 		if (ctx->duped != 0)
7470c030806STrond Myklebust 			ctx->duped = 1;
7488cd51a0cSTrond Myklebust 	}
74947c716cbSTrond Myklebust 	if (array->eof_index >= 0)
750d1bacf9eSBryan Schumaker 		desc->eof = 1;
751d1bacf9eSBryan Schumaker 
752d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
753e7c58e97STrond Myklebust out:
754d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7551e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7561e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7571da177e4SLinus Torvalds 	return res;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds /*
7611da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7621da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7631da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7641da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7651da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7661da177e4SLinus Torvalds  *
7671da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7681da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7691da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7701da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7711da177e4SLinus Torvalds  */
7721da177e4SLinus Torvalds static inline
77323db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	struct page	*page = NULL;
7761da177e4SLinus Torvalds 	int		status;
777496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
7780c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
7791da177e4SLinus Torvalds 
7801e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7811e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7841da177e4SLinus Torvalds 	if (!page) {
7851da177e4SLinus Torvalds 		status = -ENOMEM;
7861da177e4SLinus Torvalds 		goto out;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 
7897a8e1dc3STrond Myklebust 	desc->page_index = 0;
7900aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
7917a8e1dc3STrond Myklebust 	desc->page = page;
7920c030806STrond Myklebust 	ctx->duped = 0;
7937a8e1dc3STrond Myklebust 
79485f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
79585f8607eSTrond Myklebust 	if (status < 0)
796d1bacf9eSBryan Schumaker 		goto out_release;
797d1bacf9eSBryan Schumaker 
79823db8620SAl Viro 	status = nfs_do_filldir(desc);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds  out:
8011e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8023110ff80SHarvey Harrison 			__func__, status);
8031da177e4SLinus Torvalds 	return status;
8041da177e4SLinus Torvalds  out_release:
805d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8061da177e4SLinus Torvalds 	goto out;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
80900a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
81000a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
81100a92642SOlivier Galibert    whole directory.
8121da177e4SLinus Torvalds  */
81323db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8141da177e4SLinus Torvalds {
81523db8620SAl Viro 	struct dentry	*dentry = file->f_path.dentry;
8161da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8171da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8181da177e4SLinus Torvalds 			*desc = &my_desc;
81923db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
82007b5ce8eSScott Mayhew 	int res = 0;
8211da177e4SLinus Torvalds 
8226de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
8236de1472fSAl Viro 			file, (long long)ctx->pos);
82491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
82591d5b470SChuck Lever 
8261da177e4SLinus Torvalds 	/*
82723db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
828f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
82900a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
83000a92642SOlivier Galibert 	 * revalidate the cookie.
8311da177e4SLinus Torvalds 	 */
8321da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8331da177e4SLinus Torvalds 
83423db8620SAl Viro 	desc->file = file;
83523db8620SAl Viro 	desc->ctx = ctx;
836480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8371da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
83823db8620SAl Viro 	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
8391da177e4SLinus Torvalds 
840565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
84107b5ce8eSScott Mayhew 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
84223db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
843fccca7fcSTrond Myklebust 	if (res < 0)
844fccca7fcSTrond Myklebust 		goto out;
845fccca7fcSTrond Myklebust 
84647c716cbSTrond Myklebust 	do {
8471da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
84800a92642SOlivier Galibert 
8491da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
850ece0b423STrond Myklebust 			res = 0;
8511da177e4SLinus Torvalds 			/* This means either end of directory */
852d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8531da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
85423db8620SAl Viro 				res = uncached_readdir(desc);
855ece0b423STrond Myklebust 				if (res == 0)
8561da177e4SLinus Torvalds 					continue;
8571da177e4SLinus Torvalds 			}
8581da177e4SLinus Torvalds 			break;
8591da177e4SLinus Torvalds 		}
8601da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8613a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8621da177e4SLinus Torvalds 			nfs_zap_caches(inode);
863baf57a09STrond Myklebust 			desc->page_index = 0;
8641da177e4SLinus Torvalds 			desc->plus = 0;
865d1bacf9eSBryan Schumaker 			desc->eof = 0;
8661da177e4SLinus Torvalds 			continue;
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 		if (res < 0)
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 
87123db8620SAl Viro 		res = nfs_do_filldir(desc);
872ece0b423STrond Myklebust 		if (res < 0)
8731da177e4SLinus Torvalds 			break;
87447c716cbSTrond Myklebust 	} while (!desc->eof);
875fccca7fcSTrond Myklebust out:
876565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8771e7cb3dcSChuck Lever 	if (res > 0)
8781e7cb3dcSChuck Lever 		res = 0;
8796de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
8801da177e4SLinus Torvalds 	return res;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
883965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
884f0dd2136STrond Myklebust {
8856de1472fSAl Viro 	struct inode *inode = file_inode(filp);
886480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
887b84e06c5SChuck Lever 
8886de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
8896de1472fSAl Viro 			filp, offset, whence);
890b84e06c5SChuck Lever 
891b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
892965c8e59SAndrew Morton 	switch (whence) {
893f0dd2136STrond Myklebust 		case 1:
894f0dd2136STrond Myklebust 			offset += filp->f_pos;
895f0dd2136STrond Myklebust 		case 0:
896f0dd2136STrond Myklebust 			if (offset >= 0)
897f0dd2136STrond Myklebust 				break;
898f0dd2136STrond Myklebust 		default:
899f0dd2136STrond Myklebust 			offset = -EINVAL;
900f0dd2136STrond Myklebust 			goto out;
901f0dd2136STrond Myklebust 	}
902f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
903f0dd2136STrond Myklebust 		filp->f_pos = offset;
904480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9058ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
906f0dd2136STrond Myklebust 	}
907f0dd2136STrond Myklebust out:
908b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
909f0dd2136STrond Myklebust 	return offset;
910f0dd2136STrond Myklebust }
911f0dd2136STrond Myklebust 
9121da177e4SLinus Torvalds /*
9131da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9141da177e4SLinus Torvalds  * is a dummy operation.
9151da177e4SLinus Torvalds  */
91602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
91702c24a82SJosef Bacik 			 int datasync)
9181da177e4SLinus Torvalds {
9196de1472fSAl Viro 	struct inode *inode = file_inode(filp);
9207ea80859SChristoph Hellwig 
9216de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
9221e7cb3dcSChuck Lever 
92302c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
9246de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
92502c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9261da177e4SLinus Torvalds 	return 0;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
929bfc69a45STrond Myklebust /**
930bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
931bfc69a45STrond Myklebust  * @dir - pointer to directory inode
932bfc69a45STrond Myklebust  *
933bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
934bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
935bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
936bfc69a45STrond Myklebust  *
937bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
938bfc69a45STrond Myklebust  */
939bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
940bfc69a45STrond Myklebust {
941011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
942bfc69a45STrond Myklebust }
94389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
944bfc69a45STrond Myklebust 
9451da177e4SLinus Torvalds /*
9461da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9471da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9481da177e4SLinus Torvalds  * and may need to be looked up again.
9491da177e4SLinus Torvalds  */
950c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9531da177e4SLinus Torvalds 		return 1;
9544eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9554eec952eSTrond Myklebust 		return 0;
956f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9576ecc5e8fSTrond Myklebust 		return 0;
958f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
959f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
960f2c77f4eSTrond Myklebust 		return 0;
961f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
962f2c77f4eSTrond Myklebust 		return 0;
963f2c77f4eSTrond Myklebust 	return 1;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds /*
967a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
968a12802caSTrond Myklebust  * an O_EXCL create using this path component.
969a12802caSTrond Myklebust  */
970fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
971a12802caSTrond Myklebust {
972a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
973a12802caSTrond Myklebust 		return 0;
974fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
975a12802caSTrond Myklebust }
976a12802caSTrond Myklebust 
977a12802caSTrond Myklebust /*
9781d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9791d6757fbSTrond Myklebust  *
9801d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9811d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9821d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9831d6757fbSTrond Myklebust  *
9841d6757fbSTrond Myklebust  */
98565a0c149STrond Myklebust static
986fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
98965a0c149STrond Myklebust 	int ret;
9901da177e4SLinus Torvalds 
99136d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
9924e99a1ffSTrond Myklebust 		return 0;
9931da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
994fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
9951da177e4SLinus Torvalds 		goto out_force;
9961da177e4SLinus Torvalds 	/* This is an open(2) */
997fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
998fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
9991da177e4SLinus Torvalds 		goto out_force;
100065a0c149STrond Myklebust out:
100165a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10021da177e4SLinus Torvalds out_force:
100365a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
100465a0c149STrond Myklebust 	if (ret != 0)
100565a0c149STrond Myklebust 		return ret;
100665a0c149STrond Myklebust 	goto out;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds /*
10101da177e4SLinus Torvalds  * We judge how long we want to trust negative
10111da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10121da177e4SLinus Torvalds  *
10131da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10141da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10151da177e4SLinus Torvalds  */
10161da177e4SLinus Torvalds static inline
10171da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1018fa3c56bbSAl Viro 		       unsigned int flags)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1021fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10221da177e4SLinus Torvalds 		return 0;
10234eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10244eec952eSTrond Myklebust 		return 1;
10251da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds /*
10291da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10301da177e4SLinus Torvalds  * and we should check whether we can really trust that
10311da177e4SLinus Torvalds  * lookup.
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10341da177e4SLinus Torvalds  * we have an inode!
10351da177e4SLinus Torvalds  *
10361da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10371da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10381da177e4SLinus Torvalds  */
10390b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10401da177e4SLinus Torvalds {
10411da177e4SLinus Torvalds 	struct inode *dir;
10421da177e4SLinus Torvalds 	struct inode *inode;
10431da177e4SLinus Torvalds 	struct dentry *parent;
1044e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1045e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10461775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
10471da177e4SLinus Torvalds 	int error;
10481da177e4SLinus Torvalds 
1049fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
105034286d66SNick Piggin 		return -ECHILD;
105134286d66SNick Piggin 
10521da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10531da177e4SLinus Torvalds 	dir = parent->d_inode;
105491d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10551da177e4SLinus Torvalds 	inode = dentry->d_inode;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	if (!inode) {
1058fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
10591da177e4SLinus Torvalds 			goto out_bad;
1060d69ee9b8STrond Myklebust 		goto out_valid_noent;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10646de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
10656de1472fSAl Viro 				__func__, dentry);
10661da177e4SLinus Torvalds 		goto out_bad;
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 
1069011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
107015860ab1STrond Myklebust 		goto out_set_verifier;
107115860ab1STrond Myklebust 
10721da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1073fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1074fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
10751da177e4SLinus Torvalds 			goto out_zap_parent;
10761da177e4SLinus Torvalds 		goto out_valid;
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10801da177e4SLinus Torvalds 		goto out_bad;
10811da177e4SLinus Torvalds 
1082e1fb4d05STrond Myklebust 	error = -ENOMEM;
1083e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1084e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1085e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1086e1fb4d05STrond Myklebust 		goto out_error;
1087e1fb4d05STrond Myklebust 
108814c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
108914c43f76SDavid Quigley 	if (IS_ERR(label))
109014c43f76SDavid Quigley 		goto out_error;
109114c43f76SDavid Quigley 
10926e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
10931775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
10946e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
10951da177e4SLinus Torvalds 	if (error)
10961da177e4SLinus Torvalds 		goto out_bad;
1097e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
10981da177e4SLinus Torvalds 		goto out_bad;
1099e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11001da177e4SLinus Torvalds 		goto out_bad;
11011da177e4SLinus Torvalds 
1102aa9c2669SDavid Quigley 	nfs_setsecurity(inode, fattr, label);
1103aa9c2669SDavid Quigley 
1104e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1105e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
110614c43f76SDavid Quigley 	nfs4_label_free(label);
110714c43f76SDavid Quigley 
110815860ab1STrond Myklebust out_set_verifier:
1109cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11101da177e4SLinus Torvalds  out_valid:
1111d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1112d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1113d69ee9b8STrond Myklebust  out_valid_noent:
11141da177e4SLinus Torvalds 	dput(parent);
11156de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11166de1472fSAl Viro 			__func__, dentry);
11171da177e4SLinus Torvalds 	return 1;
11181da177e4SLinus Torvalds out_zap_parent:
11191da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11201da177e4SLinus Torvalds  out_bad:
1121c44600c9SAl Viro 	nfs_free_fattr(fattr);
1122c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
112314c43f76SDavid Quigley 	nfs4_label_free(label);
1124a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11251da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11261da177e4SLinus Torvalds 		/* Purge readdir caches. */
11271da177e4SLinus Torvalds 		nfs_zap_caches(inode);
1128a3f432bfSJ. Bruce Fields 		/*
1129a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1130a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1131a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1132a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1133a3f432bfSJ. Bruce Fields 		 */
1134a3f432bfSJ. Bruce Fields 		if (IS_ROOT(dentry))
1135d9e80b7dSAl Viro 			goto out_valid;
11361da177e4SLinus Torvalds 	}
113713caa9fbSMiklos Szeredi 	/* If we have submounts, don't unhash ! */
113813caa9fbSMiklos Szeredi 	if (check_submounts_and_drop(dentry) != 0)
113913caa9fbSMiklos Szeredi 		goto out_valid;
114013caa9fbSMiklos Szeredi 
11411da177e4SLinus Torvalds 	dput(parent);
11426de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11436de1472fSAl Viro 			__func__, dentry);
11441da177e4SLinus Torvalds 	return 0;
1145e1fb4d05STrond Myklebust out_error:
1146e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1147e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
114814c43f76SDavid Quigley 	nfs4_label_free(label);
1149e1fb4d05STrond Myklebust 	dput(parent);
11506de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
11516de1472fSAl Viro 			__func__, dentry, error);
1152e1fb4d05STrond Myklebust 	return error;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds /*
1156ecf3d1f1SJeff Layton  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1157ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1158ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1159ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1160ecf3d1f1SJeff Layton  *
1161ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1162ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1163ecf3d1f1SJeff Layton  */
1164ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1165ecf3d1f1SJeff Layton {
1166ecf3d1f1SJeff Layton 	int error;
1167ecf3d1f1SJeff Layton 	struct inode *inode = dentry->d_inode;
1168ecf3d1f1SJeff Layton 
1169ecf3d1f1SJeff Layton 	/*
1170ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1171ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1172ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1173ecf3d1f1SJeff Layton 	 */
1174ecf3d1f1SJeff Layton 	if (!inode) {
11756de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
11766de1472fSAl Viro 				__func__, dentry);
1177ecf3d1f1SJeff Layton 		return 1;
1178ecf3d1f1SJeff Layton 	}
1179ecf3d1f1SJeff Layton 
1180ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
11816de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
11826de1472fSAl Viro 				__func__, dentry);
1183ecf3d1f1SJeff Layton 		return 0;
1184ecf3d1f1SJeff Layton 	}
1185ecf3d1f1SJeff Layton 
1186ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1187ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1188ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1189ecf3d1f1SJeff Layton 	return !error;
1190ecf3d1f1SJeff Layton }
1191ecf3d1f1SJeff Layton 
1192ecf3d1f1SJeff Layton /*
11931da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11941da177e4SLinus Torvalds  */
1195fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
11961da177e4SLinus Torvalds {
11976de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
11986de1472fSAl Viro 		dentry, dentry->d_flags);
11991da177e4SLinus Torvalds 
120077f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
120177f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
120277f11192STrond Myklebust 		return 1;
120377f11192STrond Myklebust 
12041da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12051da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12061da177e4SLinus Torvalds 		return 1;
12071da177e4SLinus Torvalds 	}
12081da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12091da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12101da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12111da177e4SLinus Torvalds 		return 1;
12121da177e4SLinus Torvalds 	}
12131da177e4SLinus Torvalds 	return 0;
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12181b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12191b83d707STrond Myklebust {
12201b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12211f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12221f018458STrond Myklebust 	if (inode->i_nlink == 1)
12231f018458STrond Myklebust 		clear_nlink(inode);
12241f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12251b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12261b83d707STrond Myklebust }
12271b83d707STrond Myklebust 
12281da177e4SLinus Torvalds /*
12291da177e4SLinus Torvalds  * Called when the dentry loses inode.
12301da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12311da177e4SLinus Torvalds  */
12321da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12331da177e4SLinus Torvalds {
123483672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
123583672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
123683672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
123783672d39SNeil Brown 
12381da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1239e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12401f018458STrond Myklebust 		nfs_drop_nlink(inode);
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 	iput(inode);
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
1245b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1246b1942c5fSAl Viro {
1247b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1248b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1249b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1250b1942c5fSAl Viro 			WARN_ON(1);
1251b1942c5fSAl Viro 		else
1252b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1253b1942c5fSAl Viro 	}
1254b1942c5fSAl Viro }
1255b1942c5fSAl Viro 
1256f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12571da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1258ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
12591da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12601da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
126136d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1262b1942c5fSAl Viro 	.d_release	= nfs_d_release,
12631da177e4SLinus Torvalds };
1264ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
12651da177e4SLinus Torvalds 
1266597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
12671da177e4SLinus Torvalds {
12681da177e4SLinus Torvalds 	struct dentry *res;
1269565277f6STrond Myklebust 	struct dentry *parent;
12701da177e4SLinus Torvalds 	struct inode *inode = NULL;
1271e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1272e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12731775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
12741da177e4SLinus Torvalds 	int error;
12751da177e4SLinus Torvalds 
12766de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
127791d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12801da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12811da177e4SLinus Torvalds 		goto out;
12821da177e4SLinus Torvalds 
1283fd684071STrond Myklebust 	/*
1284fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1285fd684071STrond Myklebust 	 * but don't hash the dentry.
1286fd684071STrond Myklebust 	 */
128700cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1288fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1289fd684071STrond Myklebust 		res = NULL;
1290fc0f684cSTrond Myklebust 		goto out;
1291fd684071STrond Myklebust 	}
12921da177e4SLinus Torvalds 
1293e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1294e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1295e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1296e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1297e1fb4d05STrond Myklebust 		goto out;
1298e1fb4d05STrond Myklebust 
129914c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
130014c43f76SDavid Quigley 	if (IS_ERR(label))
130114c43f76SDavid Quigley 		goto out;
130214c43f76SDavid Quigley 
1303565277f6STrond Myklebust 	parent = dentry->d_parent;
1304565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
13056e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1306565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
13071775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
13081da177e4SLinus Torvalds 	if (error == -ENOENT)
13091da177e4SLinus Torvalds 		goto no_entry;
13101da177e4SLinus Torvalds 	if (error < 0) {
13111da177e4SLinus Torvalds 		res = ERR_PTR(error);
1312565277f6STrond Myklebust 		goto out_unblock_sillyrename;
13131da177e4SLinus Torvalds 	}
13141775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1315bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
131603f28e3aSTrond Myklebust 	if (IS_ERR(res))
1317565277f6STrond Myklebust 		goto out_unblock_sillyrename;
131854ceac45SDavid Howells 
1319d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1320d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1321d69ee9b8STrond Myklebust 
13221da177e4SLinus Torvalds no_entry:
132354ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13249eaef27bSTrond Myklebust 	if (res != NULL) {
13259eaef27bSTrond Myklebust 		if (IS_ERR(res))
1326565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13271da177e4SLinus Torvalds 		dentry = res;
13289eaef27bSTrond Myklebust 	}
13291da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1330565277f6STrond Myklebust out_unblock_sillyrename:
1331565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
13326e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
133314c43f76SDavid Quigley 	nfs4_label_free(label);
13341da177e4SLinus Torvalds out:
1335e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1336e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13371da177e4SLinus Torvalds 	return res;
13381da177e4SLinus Torvalds }
1339ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
13401da177e4SLinus Torvalds 
134189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
13420b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13431da177e4SLinus Torvalds 
1344f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13450ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13461da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13471da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
134836d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1349b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13501da177e4SLinus Torvalds };
135189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
13521da177e4SLinus Torvalds 
13538a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13548a5e929dSAl Viro {
13558a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13568a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
13578a5e929dSAl Viro 		res |= FMODE_READ;
13588a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
13598a5e929dSAl Viro 		res |= FMODE_WRITE;
13608a5e929dSAl Viro 	return res;
13618a5e929dSAl Viro }
13628a5e929dSAl Viro 
136351141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1364cd9a1c0eSTrond Myklebust {
13655ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1366cd9a1c0eSTrond Myklebust }
1367cd9a1c0eSTrond Myklebust 
1368cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1369cd9a1c0eSTrond Myklebust {
1370f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1371cd9a1c0eSTrond Myklebust 	return 0;
1372cd9a1c0eSTrond Myklebust }
1373cd9a1c0eSTrond Myklebust 
1374d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13750dd2b474SMiklos Szeredi 			   struct dentry *dentry,
137630d90494SAl Viro 			   struct file *file, unsigned open_flags,
137747237687SAl Viro 			   int *opened)
1378cd9a1c0eSTrond Myklebust {
13790dd2b474SMiklos Szeredi 	int err;
13800dd2b474SMiklos Szeredi 
138101c919abSMiklos Szeredi 	if ((open_flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
138201c919abSMiklos Szeredi 		*opened |= FILE_CREATED;
138301c919abSMiklos Szeredi 
138430d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
138530d90494SAl Viro 	if (err)
1386d9585277SAl Viro 		goto out;
138730d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
13880dd2b474SMiklos Szeredi 
1389cd9a1c0eSTrond Myklebust out:
1390d9585277SAl Viro 	return err;
1391cd9a1c0eSTrond Myklebust }
1392cd9a1c0eSTrond Myklebust 
139373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
139430d90494SAl Viro 		    struct file *file, unsigned open_flags,
139547237687SAl Viro 		    umode_t mode, int *opened)
13961da177e4SLinus Torvalds {
1397cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
13980dd2b474SMiklos Szeredi 	struct dentry *res;
13990dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1400f46e0bd3STrond Myklebust 	struct inode *inode;
14011472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1402898f635cSTrond Myklebust 	int err;
14031da177e4SLinus Torvalds 
14040dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14050dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
14060dd2b474SMiklos Szeredi 
1407*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
14086de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
14091e7cb3dcSChuck Lever 
14109597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
14119597c13bSJeff Layton 	if (err)
14129597c13bSJeff Layton 		return err;
14139597c13bSJeff Layton 
14140dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14150dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14160dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14170dd2b474SMiklos Szeredi 			/*
14180dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14190dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14200dd2b474SMiklos Szeredi 			 * again
14210dd2b474SMiklos Szeredi 			 */
1422d9585277SAl Viro 			return -ENOENT;
14230dd2b474SMiklos Szeredi 		}
14241472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
14251da177e4SLinus Torvalds 		goto no_open;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
14280dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1429d9585277SAl Viro 		return -ENAMETOOLONG;
14301da177e4SLinus Torvalds 
14310dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1432536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14330dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14340dd2b474SMiklos Szeredi 	}
1435536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1436536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1437536e43d1STrond Myklebust 		attr.ia_size = 0;
1438cd9a1c0eSTrond Myklebust 	}
1439cd9a1c0eSTrond Myklebust 
14400dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14410dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14420dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1443d9585277SAl Viro 		goto out;
14440dd2b474SMiklos Szeredi 
14456e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
1446f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14475bc2afc2STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1448f46e0bd3STrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1449275bb307STrond Myklebust 	if (IS_ERR(inode)) {
14500dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14516e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
14522d9db750STrond Myklebust 		put_nfs_open_context(ctx);
14530dd2b474SMiklos Szeredi 		switch (err) {
14541da177e4SLinus Torvalds 		case -ENOENT:
1455275bb307STrond Myklebust 			d_drop(dentry);
1456f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
14570dd2b474SMiklos Szeredi 			break;
14581788ea6eSJeff Layton 		case -EISDIR:
14596f926b5bSTrond Myklebust 		case -ENOTDIR:
14606f926b5bSTrond Myklebust 			goto no_open;
14611da177e4SLinus Torvalds 		case -ELOOP:
14620dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
14631da177e4SLinus Torvalds 				goto no_open;
14640dd2b474SMiklos Szeredi 			break;
14651da177e4SLinus Torvalds 			/* case -EINVAL: */
14661da177e4SLinus Torvalds 		default:
14670dd2b474SMiklos Szeredi 			break;
14681da177e4SLinus Torvalds 		}
14691da177e4SLinus Torvalds 		goto out;
14701da177e4SLinus Torvalds 	}
14710dd2b474SMiklos Szeredi 
1472275bb307STrond Myklebust 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
14736e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
14742d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1475d9585277SAl Viro out:
1476d9585277SAl Viro 	return err;
14770dd2b474SMiklos Szeredi 
14781da177e4SLinus Torvalds no_open:
14791472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
14800dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
14810dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1482d9585277SAl Viro 		goto out;
14830dd2b474SMiklos Szeredi 
1484e45198a6SAl Viro 	return finish_no_open(file, res);
14851da177e4SLinus Torvalds }
148689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
14871da177e4SLinus Torvalds 
14880b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1491657e94b6SNick Piggin 	struct inode *inode;
14921da177e4SLinus Torvalds 	struct inode *dir;
149350de348cSMiklos Szeredi 	int ret = 0;
14941da177e4SLinus Torvalds 
1495fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1496657e94b6SNick Piggin 		return -ECHILD;
1497657e94b6SNick Piggin 
1498fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1499eda72afbSMiklos Szeredi 		goto no_open;
1500eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15015584c306STrond Myklebust 		goto no_open;
150249f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
150349f9a0faSTrond Myklebust 		goto no_open;
15042b484297STrond Myklebust 
1505eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
15061da177e4SLinus Torvalds 	parent = dget_parent(dentry);
15071da177e4SLinus Torvalds 	dir = parent->d_inode;
15082b484297STrond Myklebust 
15091da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15101da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15111da177e4SLinus Torvalds 	 */
1512216d5d06STrond Myklebust 	if (inode == NULL) {
1513fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1514216d5d06STrond Myklebust 			ret = 1;
15151da177e4SLinus Torvalds 		goto out;
1516216d5d06STrond Myklebust 	}
1517216d5d06STrond Myklebust 
15181da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
15191da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15205584c306STrond Myklebust 		goto no_open_dput;
15211da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1522fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
15235584c306STrond Myklebust 		goto no_open_dput;
15241da177e4SLinus Torvalds 
15250ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1526898f635cSTrond Myklebust 	ret = 1;
15270ef97dcfSMiklos Szeredi 
15281da177e4SLinus Torvalds out:
15291da177e4SLinus Torvalds 	dput(parent);
15301da177e4SLinus Torvalds 	return ret;
1531535918f1STrond Myklebust 
15325584c306STrond Myklebust no_open_dput:
15331da177e4SLinus Torvalds 	dput(parent);
15345584c306STrond Myklebust no_open:
15350b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1536c0204fd2STrond Myklebust }
1537c0204fd2STrond Myklebust 
15381da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds /*
15411da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15421da177e4SLinus Torvalds  */
15431da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15441775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
15451775fd3eSDavid Quigley 				struct nfs4_label *label)
15461da177e4SLinus Torvalds {
1547fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1548fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15491da177e4SLinus Torvalds 	struct inode *inode;
15501da177e4SLinus Torvalds 	int error = -EACCES;
15511da177e4SLinus Torvalds 
1552fab728e1STrond Myklebust 	d_drop(dentry);
1553fab728e1STrond Myklebust 
15541da177e4SLinus Torvalds 	/* We may have been initialized further down */
15551da177e4SLinus Torvalds 	if (dentry->d_inode)
1556fab728e1STrond Myklebust 		goto out;
15571da177e4SLinus Torvalds 	if (fhandle->size == 0) {
15581775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
15591da177e4SLinus Torvalds 		if (error)
1560fab728e1STrond Myklebust 			goto out_error;
15611da177e4SLinus Torvalds 	}
15625724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15631da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15641da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15651775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
15661da177e4SLinus Torvalds 		if (error < 0)
1567fab728e1STrond Myklebust 			goto out_error;
15681da177e4SLinus Torvalds 	}
15691775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
157003f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
157103f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1572fab728e1STrond Myklebust 		goto out_error;
1573fab728e1STrond Myklebust 	d_add(dentry, inode);
1574fab728e1STrond Myklebust out:
1575fab728e1STrond Myklebust 	dput(parent);
15761da177e4SLinus Torvalds 	return 0;
1577fab728e1STrond Myklebust out_error:
1578fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1579fab728e1STrond Myklebust 	dput(parent);
1580fab728e1STrond Myklebust 	return error;
15811da177e4SLinus Torvalds }
1582ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds /*
15851da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15861da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15871da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15881da177e4SLinus Torvalds  * reply path made it appear to have failed.
15891da177e4SLinus Torvalds  */
1590597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1591ebfc3b49SAl Viro 		umode_t mode, bool excl)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	struct iattr attr;
1594ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
15951da177e4SLinus Torvalds 	int error;
15961da177e4SLinus Torvalds 
1597*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
15986de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	attr.ia_mode = mode;
16011da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16021da177e4SLinus Torvalds 
16038b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
16048867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16058b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
16061da177e4SLinus Torvalds 	if (error != 0)
16071da177e4SLinus Torvalds 		goto out_err;
16081da177e4SLinus Torvalds 	return 0;
16091da177e4SLinus Torvalds out_err:
16101da177e4SLinus Torvalds 	d_drop(dentry);
16111da177e4SLinus Torvalds 	return error;
16121da177e4SLinus Torvalds }
1613ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds /*
16161da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16171da177e4SLinus Torvalds  */
1618597d9289SBryan Schumaker int
16191a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
16201da177e4SLinus Torvalds {
16211da177e4SLinus Torvalds 	struct iattr attr;
16221da177e4SLinus Torvalds 	int status;
16231da177e4SLinus Torvalds 
1624*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
16256de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16281da177e4SLinus Torvalds 		return -EINVAL;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	attr.ia_mode = mode;
16311da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16321da177e4SLinus Torvalds 
16331ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
16341da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16351ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
16361da177e4SLinus Torvalds 	if (status != 0)
16371da177e4SLinus Torvalds 		goto out_err;
16381da177e4SLinus Torvalds 	return 0;
16391da177e4SLinus Torvalds out_err:
16401da177e4SLinus Torvalds 	d_drop(dentry);
16411da177e4SLinus Torvalds 	return status;
16421da177e4SLinus Torvalds }
1643ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds /*
16461da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16471da177e4SLinus Torvalds  */
1648597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16491da177e4SLinus Torvalds {
16501da177e4SLinus Torvalds 	struct iattr attr;
16511da177e4SLinus Torvalds 	int error;
16521da177e4SLinus Torvalds 
1653*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
16546de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16571da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16581da177e4SLinus Torvalds 
16591ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
16601da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16611ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
16621da177e4SLinus Torvalds 	if (error != 0)
16631da177e4SLinus Torvalds 		goto out_err;
16641da177e4SLinus Torvalds 	return 0;
16651da177e4SLinus Torvalds out_err:
16661da177e4SLinus Torvalds 	d_drop(dentry);
16671da177e4SLinus Torvalds 	return error;
16681da177e4SLinus Torvalds }
1669ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
16701da177e4SLinus Torvalds 
1671d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1672d45b9d8bSTrond Myklebust {
1673d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1674d45b9d8bSTrond Myklebust 		d_delete(dentry);
1675d45b9d8bSTrond Myklebust }
1676d45b9d8bSTrond Myklebust 
1677597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16781da177e4SLinus Torvalds {
16791da177e4SLinus Torvalds 	int error;
16801da177e4SLinus Torvalds 
1681*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
16826de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16831da177e4SLinus Torvalds 
16841ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
1685ba6c0592STrond Myklebust 	if (dentry->d_inode) {
1686ba6c0592STrond Myklebust 		nfs_wait_on_sillyrename(dentry);
16871da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16881da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1689ba6c0592STrond Myklebust 		switch (error) {
1690ba6c0592STrond Myklebust 		case 0:
1691ce71ec36SDave Hansen 			clear_nlink(dentry->d_inode);
1692ba6c0592STrond Myklebust 			break;
1693ba6c0592STrond Myklebust 		case -ENOENT:
1694d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1695ba6c0592STrond Myklebust 		}
1696ba6c0592STrond Myklebust 	} else
1697ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16981ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	return error;
17011da177e4SLinus Torvalds }
1702ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds /*
17051da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17061da177e4SLinus Torvalds  * and after checking that the file has only one user.
17071da177e4SLinus Torvalds  *
17081da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17091da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17101da177e4SLinus Torvalds  */
17111da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17121da177e4SLinus Torvalds {
17131da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17141da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17151da177e4SLinus Torvalds 	int error = -EBUSY;
17161da177e4SLinus Torvalds 
17176de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
17201da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17211da177e4SLinus Torvalds 		error = 0;
17221da177e4SLinus Torvalds 		goto out;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 
17251ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
17261da177e4SLinus Torvalds 	if (inode != NULL) {
172757ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
17281da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17291da177e4SLinus Torvalds 		if (error == 0)
17301b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17311da177e4SLinus Torvalds 	} else
17321da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1733d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1734d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17351ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
17361da177e4SLinus Torvalds out:
17371da177e4SLinus Torvalds 	return error;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17411da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17421da177e4SLinus Torvalds  *
17431da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17441da177e4SLinus Torvalds  */
1745597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
17461da177e4SLinus Torvalds {
17471da177e4SLinus Torvalds 	int error;
17481da177e4SLinus Torvalds 	int need_rehash = 0;
17491da177e4SLinus Torvalds 
1750*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
17516de1472fSAl Viro 		dir->i_ino, dentry);
17521da177e4SLinus Torvalds 
17531ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
17541da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
175584d08fa8SAl Viro 	if (d_count(dentry) > 1) {
17561da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1757ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1758ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17591da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17601ca42382STrond Myklebust 		goto out;
17611da177e4SLinus Torvalds 	}
17621da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17631da177e4SLinus Torvalds 		__d_drop(dentry);
17641da177e4SLinus Torvalds 		need_rehash = 1;
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17671da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1768d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17691da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17701da177e4SLinus Torvalds 	} else if (need_rehash)
17711da177e4SLinus Torvalds 		d_rehash(dentry);
17721ca42382STrond Myklebust out:
17731ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
17741da177e4SLinus Torvalds 	return error;
17751da177e4SLinus Torvalds }
1776ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
17771da177e4SLinus Torvalds 
1778873101b3SChuck Lever /*
1779873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1780873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1781873101b3SChuck Lever  * using prepare_write/commit_write.
1782873101b3SChuck Lever  *
1783873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1784873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1785873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1786873101b3SChuck Lever  * symlink request has completed on the server.
1787873101b3SChuck Lever  *
1788873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1789873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1790873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1791873101b3SChuck Lever  * and move the raw page into its mapping.
1792873101b3SChuck Lever  */
1793597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17941da177e4SLinus Torvalds {
1795873101b3SChuck Lever 	struct page *page;
1796873101b3SChuck Lever 	char *kaddr;
17971da177e4SLinus Torvalds 	struct iattr attr;
1798873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17991da177e4SLinus Torvalds 	int error;
18001da177e4SLinus Torvalds 
1801*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
18026de1472fSAl Viro 		dir->i_ino, dentry, symname);
18031da177e4SLinus Torvalds 
1804873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1805873101b3SChuck Lever 		return -ENAMETOOLONG;
18061da177e4SLinus Torvalds 
1807873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1808873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18091da177e4SLinus Torvalds 
181083d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
181176566991STrond Myklebust 	if (!page)
1812873101b3SChuck Lever 		return -ENOMEM;
1813873101b3SChuck Lever 
18142b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1815873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1816873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1817873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
18182b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1819873101b3SChuck Lever 
18201ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
182194a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
18221ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1823873101b3SChuck Lever 	if (error != 0) {
1824*1e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1825873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
18266de1472fSAl Viro 			dentry, symname, error);
18271da177e4SLinus Torvalds 		d_drop(dentry);
1828873101b3SChuck Lever 		__free_page(page);
18291da177e4SLinus Torvalds 		return error;
18301da177e4SLinus Torvalds 	}
18311da177e4SLinus Torvalds 
1832873101b3SChuck Lever 	/*
1833873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1834873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1835873101b3SChuck Lever 	 */
1836a0b8cab3SMel Gorman 	if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
1837873101b3SChuck Lever 							GFP_KERNEL)) {
1838873101b3SChuck Lever 		SetPageUptodate(page);
1839873101b3SChuck Lever 		unlock_page(page);
1840873101b3SChuck Lever 	} else
1841873101b3SChuck Lever 		__free_page(page);
1842873101b3SChuck Lever 
1843873101b3SChuck Lever 	return 0;
1844873101b3SChuck Lever }
1845ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1846873101b3SChuck Lever 
1847597d9289SBryan Schumaker int
18481da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18491da177e4SLinus Torvalds {
18501da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18511da177e4SLinus Torvalds 	int error;
18521da177e4SLinus Torvalds 
18536de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
18546de1472fSAl Viro 		old_dentry, dentry);
18551da177e4SLinus Torvalds 
18561fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
185757ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
18589a3936aaSTrond Myklebust 
18599697d234STrond Myklebust 	d_drop(dentry);
18601da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1861cf809556STrond Myklebust 	if (error == 0) {
18627de9c6eeSAl Viro 		ihold(inode);
18639697d234STrond Myklebust 		d_add(dentry, inode);
1864cf809556STrond Myklebust 	}
18651fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
18661da177e4SLinus Torvalds 	return error;
18671da177e4SLinus Torvalds }
1868ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds /*
18711da177e4SLinus Torvalds  * RENAME
18721da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18731da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18741da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18751da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18761da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18771da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  * FIXED.
18801da177e4SLinus Torvalds  *
18811da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18821da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18831da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18841da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18851da177e4SLinus Torvalds  * using the inode layer
18861da177e4SLinus Torvalds  *
18871da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18881da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18891da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18901da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18911da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18921da177e4SLinus Torvalds  * the rename.
18931da177e4SLinus Torvalds  */
1894597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18951da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18981da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18991da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
19001da177e4SLinus Torvalds 	int error = -EBUSY;
19011da177e4SLinus Torvalds 
19026de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
19036de1472fSAl Viro 		 old_dentry, new_dentry,
190484d08fa8SAl Viro 		 d_count(new_dentry));
19051da177e4SLinus Torvalds 
190670ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
19071da177e4SLinus Torvalds 	/*
190828f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
190928f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
191028f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
191128f79a1aSMiklos Szeredi 	 * the new target.
19121da177e4SLinus Torvalds 	 */
191327226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
191427226104SMiklos Szeredi 		/*
191527226104SMiklos Szeredi 		 * To prevent any new references to the target during the
191627226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
191727226104SMiklos Szeredi 		 */
191827226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
191927226104SMiklos Szeredi 			d_drop(new_dentry);
192027226104SMiklos Szeredi 			rehash = new_dentry;
192127226104SMiklos Szeredi 		}
192227226104SMiklos Szeredi 
192384d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
19241da177e4SLinus Torvalds 			int err;
192527226104SMiklos Szeredi 
19261da177e4SLinus Torvalds 			/* copy the target dentry's name */
19271da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19281da177e4SLinus Torvalds 					 &new_dentry->d_name);
19291da177e4SLinus Torvalds 			if (!dentry)
19301da177e4SLinus Torvalds 				goto out;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19331da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
193424e93025SMiklos Szeredi 			if (err)
19351da177e4SLinus Torvalds 				goto out;
193624e93025SMiklos Szeredi 
193724e93025SMiklos Szeredi 			new_dentry = dentry;
193856335936SOGAWA Hirofumi 			rehash = NULL;
193924e93025SMiklos Szeredi 			new_inode = NULL;
1940b1e4adf4STrond Myklebust 		}
194127226104SMiklos Szeredi 	}
19421da177e4SLinus Torvalds 
194357ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1944b1e4adf4STrond Myklebust 	if (new_inode != NULL)
194557ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19481da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19495ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19501da177e4SLinus Torvalds out:
19511da177e4SLinus Torvalds 	if (rehash)
19521da177e4SLinus Torvalds 		d_rehash(rehash);
195370ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
195470ded201STrond Myklebust 			new_dir, new_dentry, error);
19551da177e4SLinus Torvalds 	if (!error) {
1956b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1957b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19581da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19598fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19608fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1961d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1962d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	/* new dentry created? */
19651da177e4SLinus Torvalds 	if (dentry)
19661da177e4SLinus Torvalds 		dput(dentry);
19671da177e4SLinus Torvalds 	return error;
19681da177e4SLinus Torvalds }
1969ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
19701da177e4SLinus Torvalds 
1971cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1972cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1973cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1974cfcea3e8STrond Myklebust 
19751c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19761c3c07e9STrond Myklebust {
19771c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19781c3c07e9STrond Myklebust 	kfree(entry);
1979cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1980cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1981cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19821c3c07e9STrond Myklebust }
19831c3c07e9STrond Myklebust 
19841a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19851a81bb8aSTrond Myklebust {
19861a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19871a81bb8aSTrond Myklebust 
19881a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19891a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19901a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19911a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19921a81bb8aSTrond Myklebust 	}
19931a81bb8aSTrond Myklebust }
19941a81bb8aSTrond Myklebust 
19951ab6c499SDave Chinner unsigned long
19961ab6c499SDave Chinner nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
1997979df72eSTrond Myklebust {
1998979df72eSTrond Myklebust 	LIST_HEAD(head);
1999aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2000979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
20011495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
20021495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
20031ab6c499SDave Chinner 	long freed = 0;
2004979df72eSTrond Myklebust 
200561d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
20061ab6c499SDave Chinner 		return SHRINK_STOP;
20079c7e7e23STrond Myklebust 
2008a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2009aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2010979df72eSTrond Myklebust 		struct inode *inode;
2011979df72eSTrond Myklebust 
2012979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2013979df72eSTrond Myklebust 			break;
20149c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2015979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2016979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2017979df72eSTrond Myklebust 			goto remove_lru_entry;
2018979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2019979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2020979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2021979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
20221ab6c499SDave Chinner 		freed++;
2023979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2024979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2025979df72eSTrond Myklebust 					&nfs_access_lru_list);
2026979df72eSTrond Myklebust 		else {
2027979df72eSTrond Myklebust remove_lru_entry:
2028979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
20299c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
2030979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
20319c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
2032979df72eSTrond Myklebust 		}
203359844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2034979df72eSTrond Myklebust 	}
2035979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20361a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20371ab6c499SDave Chinner 	return freed;
20381ab6c499SDave Chinner }
20391ab6c499SDave Chinner 
20401ab6c499SDave Chinner unsigned long
20411ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
20421ab6c499SDave Chinner {
204355f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2044979df72eSTrond Myklebust }
2045979df72eSTrond Myklebust 
20461a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20471c3c07e9STrond Myklebust {
20481c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20491a81bb8aSTrond Myklebust 	struct rb_node *n;
20501c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20511c3c07e9STrond Myklebust 
20521c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20531c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20541c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20551c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20561a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20571c3c07e9STrond Myklebust 	}
20581c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20591c3c07e9STrond Myklebust }
20601c3c07e9STrond Myklebust 
20611c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20621c3c07e9STrond Myklebust {
20631a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20641a81bb8aSTrond Myklebust 
20651a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20661a81bb8aSTrond Myklebust 		return;
2067cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2068cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20691a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2070cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2071cfcea3e8STrond Myklebust 
20721c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20731a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20741a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20751a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20761a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20771c3c07e9STrond Myklebust }
20781c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
20791c3c07e9STrond Myklebust 
20801c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20811c3c07e9STrond Myklebust {
20821c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20831c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20841c3c07e9STrond Myklebust 
20851c3c07e9STrond Myklebust 	while (n != NULL) {
20861c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20871c3c07e9STrond Myklebust 
20881c3c07e9STrond Myklebust 		if (cred < entry->cred)
20891c3c07e9STrond Myklebust 			n = n->rb_left;
20901c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20911c3c07e9STrond Myklebust 			n = n->rb_right;
20921c3c07e9STrond Myklebust 		else
20931c3c07e9STrond Myklebust 			return entry;
20941c3c07e9STrond Myklebust 	}
20951c3c07e9STrond Myklebust 	return NULL;
20961c3c07e9STrond Myklebust }
20971c3c07e9STrond Myklebust 
2098af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20991da177e4SLinus Torvalds {
210055296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
21011c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
21021c3c07e9STrond Myklebust 	int err = -ENOENT;
21031da177e4SLinus Torvalds 
21041c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21051c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
21061c3c07e9STrond Myklebust 		goto out_zap;
21071c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
21081c3c07e9STrond Myklebust 	if (cache == NULL)
21091c3c07e9STrond Myklebust 		goto out;
2110b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
211164672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
21121c3c07e9STrond Myklebust 		goto out_stale;
21131c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
21141c3c07e9STrond Myklebust 	res->cred = cache->cred;
21151c3c07e9STrond Myklebust 	res->mask = cache->mask;
2116cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
21171c3c07e9STrond Myklebust 	err = 0;
21181c3c07e9STrond Myklebust out:
21191c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21201c3c07e9STrond Myklebust 	return err;
21211c3c07e9STrond Myklebust out_stale:
21221c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2123cfcea3e8STrond Myklebust 	list_del(&cache->lru);
21241c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21251c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
21261da177e4SLinus Torvalds 	return -ENOENT;
21271c3c07e9STrond Myklebust out_zap:
21281a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21291a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
21301c3c07e9STrond Myklebust 	return -ENOENT;
21311c3c07e9STrond Myklebust }
21321c3c07e9STrond Myklebust 
21331c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
21341c3c07e9STrond Myklebust {
2135cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2136cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21371c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
21381c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
21391c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21401c3c07e9STrond Myklebust 
21411c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21421c3c07e9STrond Myklebust 	while (*p != NULL) {
21431c3c07e9STrond Myklebust 		parent = *p;
21441c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21451c3c07e9STrond Myklebust 
21461c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21471c3c07e9STrond Myklebust 			p = &parent->rb_left;
21481c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21491c3c07e9STrond Myklebust 			p = &parent->rb_right;
21501c3c07e9STrond Myklebust 		else
21511c3c07e9STrond Myklebust 			goto found;
21521c3c07e9STrond Myklebust 	}
21531c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21541c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2155cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21561c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21571c3c07e9STrond Myklebust 	return;
21581c3c07e9STrond Myklebust found:
21591c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2160cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2161cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21621c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21631c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
21666168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21671da177e4SLinus Torvalds {
21681c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21691c3c07e9STrond Myklebust 	if (cache == NULL)
21701c3c07e9STrond Myklebust 		return;
21711c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21721da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21731c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21741da177e4SLinus Torvalds 	cache->mask = set->mask;
21751c3c07e9STrond Myklebust 
21761c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2177cfcea3e8STrond Myklebust 
2178cfcea3e8STrond Myklebust 	/* Update accounting */
2179cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2180cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2181cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2182cfcea3e8STrond Myklebust 
2183cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21841a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2185cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21861a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21871a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21881a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2189cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2190cfcea3e8STrond Myklebust 	}
21911da177e4SLinus Torvalds }
21926168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
21936168f62cSWeston Andros Adamson 
21946168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
21956168f62cSWeston Andros Adamson {
21966168f62cSWeston Andros Adamson 	entry->mask = 0;
21976168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
21986168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
21996168f62cSWeston Andros Adamson 	if (access_result &
22006168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
22016168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
22026168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
22036168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
22046168f62cSWeston Andros Adamson }
22056168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
22081da177e4SLinus Torvalds {
22091da177e4SLinus Torvalds 	struct nfs_access_entry cache;
22101da177e4SLinus Torvalds 	int status;
22111da177e4SLinus Torvalds 
2212f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2213f4ce1299STrond Myklebust 
22141da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
22151da177e4SLinus Torvalds 	if (status == 0)
2216f4ce1299STrond Myklebust 		goto out_cached;
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
22191da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
22201da177e4SLinus Torvalds 	cache.cred = cred;
22211da177e4SLinus Torvalds 	cache.jiffies = jiffies;
22221da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2223a71ee337SSuresh Jayaraman 	if (status != 0) {
2224a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2225a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2226a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2227a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2228a71ee337SSuresh Jayaraman 		}
2229f4ce1299STrond Myklebust 		goto out;
2230a71ee337SSuresh Jayaraman 	}
22311da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2232f4ce1299STrond Myklebust out_cached:
2233f4ce1299STrond Myklebust 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2234f4ce1299STrond Myklebust 		status = -EACCES;
22351da177e4SLinus Torvalds out:
2236f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2237f4ce1299STrond Myklebust 	return status;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
2240af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2241af22f94aSTrond Myklebust {
2242af22f94aSTrond Myklebust 	int mask = 0;
2243af22f94aSTrond Myklebust 
2244f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2245f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2246f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2247f8d9a897SWeston Andros Adamson 	} else {
22488a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2249af22f94aSTrond Myklebust 			mask |= MAY_READ;
22508a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2251af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2252f8d9a897SWeston Andros Adamson 	}
2253f8d9a897SWeston Andros Adamson 
2254af22f94aSTrond Myklebust 	return mask;
2255af22f94aSTrond Myklebust }
2256af22f94aSTrond Myklebust 
2257af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2258af22f94aSTrond Myklebust {
2259af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2260af22f94aSTrond Myklebust }
226189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2262af22f94aSTrond Myklebust 
226310556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
22641da177e4SLinus Torvalds {
22651da177e4SLinus Torvalds 	struct rpc_cred *cred;
22661da177e4SLinus Torvalds 	int res = 0;
22671da177e4SLinus Torvalds 
226810556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2269b74c79e9SNick Piggin 		return -ECHILD;
2270b74c79e9SNick Piggin 
227191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
227291d5b470SChuck Lever 
2273e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22741da177e4SLinus Torvalds 		goto out;
22751da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22769cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22771da177e4SLinus Torvalds 		goto force_lookup;
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22801da177e4SLinus Torvalds 		case S_IFLNK:
22811da177e4SLinus Torvalds 			goto out;
22821da177e4SLinus Torvalds 		case S_IFREG:
22831da177e4SLinus Torvalds 			break;
22841da177e4SLinus Torvalds 		case S_IFDIR:
22851da177e4SLinus Torvalds 			/*
22861da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22871da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22881da177e4SLinus Torvalds 			 */
22891da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22901da177e4SLinus Torvalds 				goto out;
22911da177e4SLinus Torvalds 	}
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds force_lookup:
22941da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22951da177e4SLinus Torvalds 		goto out_notsup;
22961da177e4SLinus Torvalds 
229798a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22981da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22991da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
23001da177e4SLinus Torvalds 		put_rpccred(cred);
23011da177e4SLinus Torvalds 	} else
23021da177e4SLinus Torvalds 		res = PTR_ERR(cred);
23031da177e4SLinus Torvalds out:
2304f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2305f696a365SMiklos Szeredi 		res = -EACCES;
2306f696a365SMiklos Szeredi 
2307*1e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
23081e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
23091da177e4SLinus Torvalds 	return res;
23101da177e4SLinus Torvalds out_notsup:
23111da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
23121da177e4SLinus Torvalds 	if (res == 0)
23132830ba7fSAl Viro 		res = generic_permission(inode, mask);
23141e7cb3dcSChuck Lever 	goto out;
23151da177e4SLinus Torvalds }
2316ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds /*
23191da177e4SLinus Torvalds  * Local variables:
23201da177e4SLinus Torvalds  *  version-control: t
23211da177e4SLinus Torvalds  *  kept-new-versions: 5
23221da177e4SLinus Torvalds  * End:
23231da177e4SLinus Torvalds  */
2324