xref: /openbmc/linux/fs/nfs/dir.c (revision 1ab6c499)
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 
1016da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
102cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
103cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
104cc0dd2d1SChuck Lever 
105cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1061e7cb3dcSChuck Lever 
107480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
108480c2006SBryan Schumaker 	if (IS_ERR(cred))
109480c2006SBryan Schumaker 		return PTR_ERR(cred);
1100c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
111480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
112480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
113480c2006SBryan Schumaker 		goto out;
114480c2006SBryan Schumaker 	}
115480c2006SBryan Schumaker 	filp->private_data = ctx;
116f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
117f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
118f5a73672SNeil Brown 		 * have been called, so we need to refresh the
119f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
120f5a73672SNeil Brown 		 */
121f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
122f5a73672SNeil Brown 	}
123480c2006SBryan Schumaker out:
124480c2006SBryan Schumaker 	put_rpccred(cred);
1251da177e4SLinus Torvalds 	return res;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128480c2006SBryan Schumaker static int
129480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
130480c2006SBryan Schumaker {
131480c2006SBryan Schumaker 	put_nfs_open_dir_context(filp->private_data);
132480c2006SBryan Schumaker 	return 0;
133480c2006SBryan Schumaker }
134480c2006SBryan Schumaker 
135d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
136d1bacf9eSBryan Schumaker 	u64 cookie;
137d1bacf9eSBryan Schumaker 	u64 ino;
138d1bacf9eSBryan Schumaker 	struct qstr string;
1390b26a0bfSTrond Myklebust 	unsigned char d_type;
140d1bacf9eSBryan Schumaker };
141d1bacf9eSBryan Schumaker 
142d1bacf9eSBryan Schumaker struct nfs_cache_array {
14388b8e133SChuck Lever 	int size;
144d1bacf9eSBryan Schumaker 	int eof_index;
145d1bacf9eSBryan Schumaker 	u64 last_cookie;
146d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
147d1bacf9eSBryan Schumaker };
148d1bacf9eSBryan Schumaker 
149573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1501da177e4SLinus Torvalds typedef struct {
1511da177e4SLinus Torvalds 	struct file	*file;
1521da177e4SLinus Torvalds 	struct page	*page;
15323db8620SAl Viro 	struct dir_context *ctx;
1541da177e4SLinus Torvalds 	unsigned long	page_index;
155f0dd2136STrond Myklebust 	u64		*dir_cookie;
1560aded708STrond Myklebust 	u64		last_cookie;
157f0dd2136STrond Myklebust 	loff_t		current_index;
1581da177e4SLinus Torvalds 	decode_dirent_t	decode;
159d1bacf9eSBryan Schumaker 
1601f4eab7eSNeil Brown 	unsigned long	timestamp;
1614704f0e2STrond Myklebust 	unsigned long	gencount;
162d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
163d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
164d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1651da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1661da177e4SLinus Torvalds 
167d1bacf9eSBryan Schumaker /*
168d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds static
171d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1721da177e4SLinus Torvalds {
1738cd51a0cSTrond Myklebust 	void *ptr;
174d1bacf9eSBryan Schumaker 	if (page == NULL)
175d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1768cd51a0cSTrond Myklebust 	ptr = kmap(page);
1778cd51a0cSTrond Myklebust 	if (ptr == NULL)
1788cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1798cd51a0cSTrond Myklebust 	return ptr;
180d1bacf9eSBryan Schumaker }
181d1bacf9eSBryan Schumaker 
182d1bacf9eSBryan Schumaker static
183d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
184d1bacf9eSBryan Schumaker {
185d1bacf9eSBryan Schumaker 	kunmap(page);
186d1bacf9eSBryan Schumaker }
187d1bacf9eSBryan Schumaker 
188d1bacf9eSBryan Schumaker /*
189d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
190d1bacf9eSBryan Schumaker  */
191d1bacf9eSBryan Schumaker static
19211de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
193d1bacf9eSBryan Schumaker {
19411de3b11STrond Myklebust 	struct nfs_cache_array *array;
195d1bacf9eSBryan Schumaker 	int i;
1968cd51a0cSTrond Myklebust 
1972b86ce2dSCong Wang 	array = kmap_atomic(page);
198d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
199d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
2002b86ce2dSCong Wang 	kunmap_atomic(array);
201d1bacf9eSBryan Schumaker }
202d1bacf9eSBryan Schumaker 
203d1bacf9eSBryan Schumaker /*
204d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
205d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
206d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
207d1bacf9eSBryan Schumaker  */
208d1bacf9eSBryan Schumaker static
2094a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
210d1bacf9eSBryan Schumaker {
211d1bacf9eSBryan Schumaker 	string->len = len;
212d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2134a201d6eSTrond Myklebust 	if (string->name == NULL)
2144a201d6eSTrond Myklebust 		return -ENOMEM;
21504e4bd1cSCatalin Marinas 	/*
21604e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
21704e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
21804e4bd1cSCatalin Marinas 	 */
21904e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2204a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2214a201d6eSTrond Myklebust 	return 0;
222d1bacf9eSBryan Schumaker }
223d1bacf9eSBryan Schumaker 
224d1bacf9eSBryan Schumaker static
225d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
226d1bacf9eSBryan Schumaker {
227d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2284a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2294a201d6eSTrond Myklebust 	int ret;
2304a201d6eSTrond Myklebust 
231d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
232d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
233d1bacf9eSBryan Schumaker 
2344a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2353020093fSTrond Myklebust 
2363020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2373020093fSTrond Myklebust 	ret = -ENOSPC;
2383020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2393020093fSTrond Myklebust 		goto out;
2403020093fSTrond Myklebust 
2414a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2424a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2430b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2444a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2454a201d6eSTrond Myklebust 	if (ret)
2464a201d6eSTrond Myklebust 		goto out;
247d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2488cd51a0cSTrond Myklebust 	array->size++;
24947c716cbSTrond Myklebust 	if (entry->eof != 0)
250d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2514a201d6eSTrond Myklebust out:
252d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2534a201d6eSTrond Myklebust 	return ret;
254d1bacf9eSBryan Schumaker }
255d1bacf9eSBryan Schumaker 
256d1bacf9eSBryan Schumaker static
257d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
258d1bacf9eSBryan Schumaker {
25923db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
260d1bacf9eSBryan Schumaker 	unsigned int index;
261d1bacf9eSBryan Schumaker 
262d1bacf9eSBryan Schumaker 	if (diff < 0)
263d1bacf9eSBryan Schumaker 		goto out_eof;
264d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2658cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
266d1bacf9eSBryan Schumaker 			goto out_eof;
267d1bacf9eSBryan Schumaker 		return -EAGAIN;
268d1bacf9eSBryan Schumaker 	}
269d1bacf9eSBryan Schumaker 
270d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
271d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
272d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
273d1bacf9eSBryan Schumaker 	return 0;
274d1bacf9eSBryan Schumaker out_eof:
275d1bacf9eSBryan Schumaker 	desc->eof = 1;
276d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
277d1bacf9eSBryan Schumaker }
278d1bacf9eSBryan Schumaker 
279d1bacf9eSBryan Schumaker static
280d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
281d1bacf9eSBryan Schumaker {
282d1bacf9eSBryan Schumaker 	int i;
2838ef2ce3eSBryan Schumaker 	loff_t new_pos;
284d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
285d1bacf9eSBryan Schumaker 
286d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2878cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
288496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2890c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2900c030806STrond Myklebust 
2918ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2920c030806STrond Myklebust 			if (ctx->attr_gencount != nfsi->attr_gencount
2930c030806STrond Myklebust 			    || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
2940c030806STrond Myklebust 				ctx->duped = 0;
2950c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
29623db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2970c030806STrond Myklebust 				if (ctx->duped > 0
2980c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2990c030806STrond Myklebust 					if (printk_ratelimit()) {
3000c030806STrond Myklebust 						pr_notice("NFS: directory %s/%s contains a readdir loop."
3010c030806STrond Myklebust 								"Please contact your server vendor.  "
302374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
3030c030806STrond Myklebust 								desc->file->f_dentry->d_parent->d_name.name,
3040c030806STrond Myklebust 								desc->file->f_dentry->d_name.name,
305374e4e3eSBryan Schumaker 								array->array[i].string.name,
3060c030806STrond Myklebust 								*desc->dir_cookie);
3070c030806STrond Myklebust 					}
3080c030806STrond Myklebust 					status = -ELOOP;
3090c030806STrond Myklebust 					goto out;
3100c030806STrond Myklebust 				}
3118ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3120c030806STrond Myklebust 				ctx->duped = -1;
3138ef2ce3eSBryan Schumaker 			}
31423db8620SAl Viro 			desc->ctx->pos = new_pos;
3158cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
31647c716cbSTrond Myklebust 			return 0;
3178cd51a0cSTrond Myklebust 		}
3188cd51a0cSTrond Myklebust 	}
31947c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
320d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
32118fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
32218fb5fe4STrond Myklebust 			desc->eof = 1;
323d1bacf9eSBryan Schumaker 	}
3240c030806STrond Myklebust out:
325d1bacf9eSBryan Schumaker 	return status;
326d1bacf9eSBryan Schumaker }
327d1bacf9eSBryan Schumaker 
328d1bacf9eSBryan Schumaker static
329d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
330d1bacf9eSBryan Schumaker {
331d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
33247c716cbSTrond Myklebust 	int status;
333d1bacf9eSBryan Schumaker 
334d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
335d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
336d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
337d1bacf9eSBryan Schumaker 		goto out;
338d1bacf9eSBryan Schumaker 	}
339d1bacf9eSBryan Schumaker 
340d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
341d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
342d1bacf9eSBryan Schumaker 	else
343d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
344d1bacf9eSBryan Schumaker 
34547c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3460aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
347e47c085aSTrond Myklebust 		desc->current_index += array->size;
34847c716cbSTrond Myklebust 		desc->page_index++;
34947c716cbSTrond Myklebust 	}
350d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
351d1bacf9eSBryan Schumaker out:
352d1bacf9eSBryan Schumaker 	return status;
353d1bacf9eSBryan Schumaker }
354d1bacf9eSBryan Schumaker 
355d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
356d1bacf9eSBryan Schumaker static
35756e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
358d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
359d1bacf9eSBryan Schumaker {
360480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
361480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3624704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3631da177e4SLinus Torvalds 	int		error;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds  again:
3661da177e4SLinus Torvalds 	timestamp = jiffies;
3674704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
36856e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3691da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3701da177e4SLinus Torvalds 	if (error < 0) {
3711da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3721da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3731da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3743a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3751da177e4SLinus Torvalds 			desc->plus = 0;
3761da177e4SLinus Torvalds 			goto again;
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 		goto error;
3791da177e4SLinus Torvalds 	}
3801f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3814704f0e2STrond Myklebust 	desc->gencount = gencount;
382d1bacf9eSBryan Schumaker error:
383d1bacf9eSBryan Schumaker 	return error;
384d1bacf9eSBryan Schumaker }
385d1bacf9eSBryan Schumaker 
386573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
387573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
388d1bacf9eSBryan Schumaker {
389573c4e1eSChuck Lever 	int error;
390d1bacf9eSBryan Schumaker 
391573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
392573c4e1eSChuck Lever 	if (error)
393573c4e1eSChuck Lever 		return error;
394d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
395d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
396d1bacf9eSBryan Schumaker 	return 0;
397d1bacf9eSBryan Schumaker }
398d1bacf9eSBryan Schumaker 
399d39ab9deSBryan Schumaker static
400d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
401d39ab9deSBryan Schumaker {
402d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
403d39ab9deSBryan Schumaker 		goto different;
40437a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
405d39ab9deSBryan Schumaker 		goto different;
406d39ab9deSBryan Schumaker 	return 1;
407d39ab9deSBryan Schumaker different:
408d39ab9deSBryan Schumaker 	return 0;
409d39ab9deSBryan Schumaker }
410d39ab9deSBryan Schumaker 
411d39ab9deSBryan Schumaker static
41223db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
413d69ee9b8STrond Myklebust {
414d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
415d69ee9b8STrond Myklebust 		return false;
416d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
417d69ee9b8STrond Myklebust 		return true;
41823db8620SAl Viro 	if (ctx->pos == 0)
419d69ee9b8STrond Myklebust 		return true;
420d69ee9b8STrond Myklebust 	return false;
421d69ee9b8STrond Myklebust }
422d69ee9b8STrond Myklebust 
423d69ee9b8STrond Myklebust /*
424d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
425d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
426d69ee9b8STrond Myklebust  * directory.
427d69ee9b8STrond Myklebust  */
428d69ee9b8STrond Myklebust static
429d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
430d69ee9b8STrond Myklebust {
431d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
432d69ee9b8STrond Myklebust }
433d69ee9b8STrond Myklebust 
434d69ee9b8STrond Myklebust static
435d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
436d39ab9deSBryan Schumaker {
43726fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4384a201d6eSTrond Myklebust 	struct dentry *dentry;
4394a201d6eSTrond Myklebust 	struct dentry *alias;
440d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
441d39ab9deSBryan Schumaker 	struct inode *inode;
442aa9c2669SDavid Quigley 	int status;
443d39ab9deSBryan Schumaker 
4444a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4454a201d6eSTrond Myklebust 		if (filename.len == 1)
4464a201d6eSTrond Myklebust 			return;
4474a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4484a201d6eSTrond Myklebust 			return;
4494a201d6eSTrond Myklebust 	}
4504a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
451d39ab9deSBryan Schumaker 
4524a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
453d39ab9deSBryan Schumaker 	if (dentry != NULL) {
454d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
455cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
456aa9c2669SDavid Quigley 			status = nfs_refresh_inode(dentry->d_inode, entry->fattr);
457aa9c2669SDavid Quigley 			if (!status)
458aa9c2669SDavid Quigley 				nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label);
459d39ab9deSBryan Schumaker 			goto out;
460d39ab9deSBryan Schumaker 		} else {
461696199f8SAl Viro 			if (d_invalidate(dentry) != 0)
462696199f8SAl Viro 				goto out;
463d39ab9deSBryan Schumaker 			dput(dentry);
464d39ab9deSBryan Schumaker 		}
465d39ab9deSBryan Schumaker 	}
466d39ab9deSBryan Schumaker 
467d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4684a201d6eSTrond Myklebust 	if (dentry == NULL)
4694a201d6eSTrond Myklebust 		return;
4704a201d6eSTrond Myklebust 
4711775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
472d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
473d39ab9deSBryan Schumaker 		goto out;
474d39ab9deSBryan Schumaker 
475d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
476d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
477d39ab9deSBryan Schumaker 		goto out;
478d39ab9deSBryan Schumaker 	else if (alias) {
479d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
480d39ab9deSBryan Schumaker 		dput(alias);
481d39ab9deSBryan Schumaker 	} else
482d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
483d39ab9deSBryan Schumaker 
484d39ab9deSBryan Schumaker out:
485d39ab9deSBryan Schumaker 	dput(dentry);
486d39ab9deSBryan Schumaker }
487d39ab9deSBryan Schumaker 
488d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
489d1bacf9eSBryan Schumaker static
4908cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
4916650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
492d1bacf9eSBryan Schumaker {
493babddc72SBryan Schumaker 	struct xdr_stream stream;
494f7da7a12SBenny Halevy 	struct xdr_buf buf;
4956650239aSTrond Myklebust 	struct page *scratch;
49699424380SBryan Schumaker 	struct nfs_cache_array *array;
4975c346854STrond Myklebust 	unsigned int count = 0;
4985c346854STrond Myklebust 	int status;
499babddc72SBryan Schumaker 
5006650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5016650239aSTrond Myklebust 	if (scratch == NULL)
5026650239aSTrond Myklebust 		return -ENOMEM;
503babddc72SBryan Schumaker 
504f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5056650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
50699424380SBryan Schumaker 
50799424380SBryan Schumaker 	do {
50899424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5098cd51a0cSTrond Myklebust 		if (status != 0) {
5108cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5118cd51a0cSTrond Myklebust 				status = 0;
51299424380SBryan Schumaker 			break;
5138cd51a0cSTrond Myklebust 		}
51499424380SBryan Schumaker 
5155c346854STrond Myklebust 		count++;
5165c346854STrond Myklebust 
51747c716cbSTrond Myklebust 		if (desc->plus != 0)
518d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5198cd51a0cSTrond Myklebust 
5208cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5218cd51a0cSTrond Myklebust 		if (status != 0)
5228cd51a0cSTrond Myklebust 			break;
52399424380SBryan Schumaker 	} while (!entry->eof);
52499424380SBryan Schumaker 
52547c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
52699424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5278cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5288cd51a0cSTrond Myklebust 			array->eof_index = array->size;
52999424380SBryan Schumaker 			status = 0;
53099424380SBryan Schumaker 			nfs_readdir_release_array(page);
5315c346854STrond Myklebust 		} else
5325c346854STrond Myklebust 			status = PTR_ERR(array);
53356e4ebf8SBryan Schumaker 	}
5346650239aSTrond Myklebust 
5356650239aSTrond Myklebust 	put_page(scratch);
5368cd51a0cSTrond Myklebust 	return status;
5378cd51a0cSTrond Myklebust }
53856e4ebf8SBryan Schumaker 
53956e4ebf8SBryan Schumaker static
54056e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
54156e4ebf8SBryan Schumaker {
54256e4ebf8SBryan Schumaker 	unsigned int i;
54356e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
54456e4ebf8SBryan Schumaker 		put_page(pages[i]);
54556e4ebf8SBryan Schumaker }
54656e4ebf8SBryan Schumaker 
54756e4ebf8SBryan Schumaker static
54856e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
54956e4ebf8SBryan Schumaker 		unsigned int npages)
55056e4ebf8SBryan Schumaker {
55156e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
55256e4ebf8SBryan Schumaker }
55356e4ebf8SBryan Schumaker 
55456e4ebf8SBryan Schumaker /*
55556e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
55656e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
55756e4ebf8SBryan Schumaker  */
55856e4ebf8SBryan Schumaker static
5596650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
56056e4ebf8SBryan Schumaker {
56156e4ebf8SBryan Schumaker 	unsigned int i;
56256e4ebf8SBryan Schumaker 
56356e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
56456e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
56556e4ebf8SBryan Schumaker 		if (page == NULL)
56656e4ebf8SBryan Schumaker 			goto out_freepages;
56756e4ebf8SBryan Schumaker 		pages[i] = page;
56856e4ebf8SBryan Schumaker 	}
5696650239aSTrond Myklebust 	return 0;
57056e4ebf8SBryan Schumaker 
57156e4ebf8SBryan Schumaker out_freepages:
57256e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
5736650239aSTrond Myklebust 	return -ENOMEM;
574d1bacf9eSBryan Schumaker }
575d1bacf9eSBryan Schumaker 
576d1bacf9eSBryan Schumaker static
577d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
578d1bacf9eSBryan Schumaker {
57956e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
58056e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
581d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
582d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
583d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5848cd51a0cSTrond Myklebust 	int status = -ENOMEM;
58556e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
586d1bacf9eSBryan Schumaker 
587d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
5880aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
589d1bacf9eSBryan Schumaker 	entry.eof = 0;
590d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
591d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
592573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
593d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
594d1bacf9eSBryan Schumaker 		goto out;
595d1bacf9eSBryan Schumaker 
59614c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
59714c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
59814c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
59914c43f76SDavid Quigley 		goto out;
60014c43f76SDavid Quigley 	}
60114c43f76SDavid Quigley 
602d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6038cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6048cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
60514c43f76SDavid Quigley 		goto out_label_free;
6068cd51a0cSTrond Myklebust 	}
607d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
608d1bacf9eSBryan Schumaker 	array->eof_index = -1;
609d1bacf9eSBryan Schumaker 
6106650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6116650239aSTrond Myklebust 	if (status < 0)
612d1bacf9eSBryan Schumaker 		goto out_release_array;
613d1bacf9eSBryan Schumaker 	do {
614ac396128STrond Myklebust 		unsigned int pglen;
61556e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
616babddc72SBryan Schumaker 
617d1bacf9eSBryan Schumaker 		if (status < 0)
618d1bacf9eSBryan Schumaker 			break;
619ac396128STrond Myklebust 		pglen = status;
6206650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6218cd51a0cSTrond Myklebust 		if (status < 0) {
6228cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6238cd51a0cSTrond Myklebust 				status = 0;
6248cd51a0cSTrond Myklebust 			break;
6258cd51a0cSTrond Myklebust 		}
6268cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
627d1bacf9eSBryan Schumaker 
62856e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
629d1bacf9eSBryan Schumaker out_release_array:
630d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
63114c43f76SDavid Quigley out_label_free:
63214c43f76SDavid Quigley 	nfs4_label_free(entry.label);
633d1bacf9eSBryan Schumaker out:
634d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
635d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
636d1bacf9eSBryan Schumaker 	return status;
637d1bacf9eSBryan Schumaker }
638d1bacf9eSBryan Schumaker 
639d1bacf9eSBryan Schumaker /*
640d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
641d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
642d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
643d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6441da177e4SLinus Torvalds  */
645d1bacf9eSBryan Schumaker static
646d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
647d1bacf9eSBryan Schumaker {
648496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6498cd51a0cSTrond Myklebust 	int ret;
650d1bacf9eSBryan Schumaker 
6518cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6528cd51a0cSTrond Myklebust 	if (ret < 0)
653d1bacf9eSBryan Schumaker 		goto error;
654d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
655d1bacf9eSBryan Schumaker 
6562aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
657cd9ae2b6STrond Myklebust 		/* Should never happen */
658cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
659cd9ae2b6STrond Myklebust 	}
6601da177e4SLinus Torvalds 	unlock_page(page);
6611da177e4SLinus Torvalds 	return 0;
6621da177e4SLinus Torvalds  error:
6631da177e4SLinus Torvalds 	unlock_page(page);
6648cd51a0cSTrond Myklebust 	return ret;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
667d1bacf9eSBryan Schumaker static
668d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6691da177e4SLinus Torvalds {
67011de3b11STrond Myklebust 	if (!desc->page->mapping)
67111de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6721da177e4SLinus Torvalds 	page_cache_release(desc->page);
6731da177e4SLinus Torvalds 	desc->page = NULL;
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
676d1bacf9eSBryan Schumaker static
677d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6781da177e4SLinus Torvalds {
679496ad9aaSAl Viro 	return read_cache_page(file_inode(desc->file)->i_mapping,
680d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /*
684d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6851da177e4SLinus Torvalds  */
686d1bacf9eSBryan Schumaker static
687d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
688d1bacf9eSBryan Schumaker {
689d1bacf9eSBryan Schumaker 	int res;
690d1bacf9eSBryan Schumaker 
691d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
692d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
693d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
694d1bacf9eSBryan Schumaker 
695d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
69647c716cbSTrond Myklebust 	if (res != 0)
697d1bacf9eSBryan Schumaker 		cache_page_release(desc);
698d1bacf9eSBryan Schumaker 	return res;
699d1bacf9eSBryan Schumaker }
700d1bacf9eSBryan Schumaker 
701d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7021da177e4SLinus Torvalds static inline
7031da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7041da177e4SLinus Torvalds {
7058cd51a0cSTrond Myklebust 	int res;
706d1bacf9eSBryan Schumaker 
7070aded708STrond Myklebust 	if (desc->page_index == 0) {
7088cd51a0cSTrond Myklebust 		desc->current_index = 0;
7090aded708STrond Myklebust 		desc->last_cookie = 0;
7100aded708STrond Myklebust 	}
71147c716cbSTrond Myklebust 	do {
712d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
71347c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7141da177e4SLinus Torvalds 	return res;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /*
7181da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7191da177e4SLinus Torvalds  */
7201da177e4SLinus Torvalds static
72123db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	struct file	*file = desc->file;
724d1bacf9eSBryan Schumaker 	int i = 0;
725d1bacf9eSBryan Schumaker 	int res = 0;
726d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7278ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7288ef2ce3eSBryan Schumaker 
729d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
730e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
731e7c58e97STrond Myklebust 		res = PTR_ERR(array);
732e7c58e97STrond Myklebust 		goto out;
733e7c58e97STrond Myklebust 	}
7341da177e4SLinus Torvalds 
735d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
736ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7371da177e4SLinus Torvalds 
738ece0b423STrond Myklebust 		ent = &array->array[i];
73923db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
74023db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
741ece0b423STrond Myklebust 			desc->eof = 1;
7421da177e4SLinus Torvalds 			break;
743ece0b423STrond Myklebust 		}
74423db8620SAl Viro 		desc->ctx->pos++;
745d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
746d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
747d1bacf9eSBryan Schumaker 		else
748d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7490c030806STrond Myklebust 		if (ctx->duped != 0)
7500c030806STrond Myklebust 			ctx->duped = 1;
7518cd51a0cSTrond Myklebust 	}
75247c716cbSTrond Myklebust 	if (array->eof_index >= 0)
753d1bacf9eSBryan Schumaker 		desc->eof = 1;
754d1bacf9eSBryan Schumaker 
755d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
756e7c58e97STrond Myklebust out:
757d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7581e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7591e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7601da177e4SLinus Torvalds 	return res;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7651da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7661da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7671da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7681da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7691da177e4SLinus Torvalds  *
7701da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7711da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7721da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7731da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7741da177e4SLinus Torvalds  */
7751da177e4SLinus Torvalds static inline
77623db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	struct page	*page = NULL;
7791da177e4SLinus Torvalds 	int		status;
780496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
7810c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
7821da177e4SLinus Torvalds 
7831e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7841e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7871da177e4SLinus Torvalds 	if (!page) {
7881da177e4SLinus Torvalds 		status = -ENOMEM;
7891da177e4SLinus Torvalds 		goto out;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 
7927a8e1dc3STrond Myklebust 	desc->page_index = 0;
7930aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
7947a8e1dc3STrond Myklebust 	desc->page = page;
7950c030806STrond Myklebust 	ctx->duped = 0;
7967a8e1dc3STrond Myklebust 
79785f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
79885f8607eSTrond Myklebust 	if (status < 0)
799d1bacf9eSBryan Schumaker 		goto out_release;
800d1bacf9eSBryan Schumaker 
80123db8620SAl Viro 	status = nfs_do_filldir(desc);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds  out:
8041e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8053110ff80SHarvey Harrison 			__func__, status);
8061da177e4SLinus Torvalds 	return status;
8071da177e4SLinus Torvalds  out_release:
808d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8091da177e4SLinus Torvalds 	goto out;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
81200a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
81300a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
81400a92642SOlivier Galibert    whole directory.
8151da177e4SLinus Torvalds  */
81623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8171da177e4SLinus Torvalds {
81823db8620SAl Viro 	struct dentry	*dentry = file->f_path.dentry;
8191da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8201da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8211da177e4SLinus Torvalds 			*desc = &my_desc;
82223db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
82307b5ce8eSScott Mayhew 	int res = 0;
8241da177e4SLinus Torvalds 
8256da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8261e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
82723db8620SAl Viro 			(long long)ctx->pos);
82891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
82991d5b470SChuck Lever 
8301da177e4SLinus Torvalds 	/*
83123db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
832f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
83300a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
83400a92642SOlivier Galibert 	 * revalidate the cookie.
8351da177e4SLinus Torvalds 	 */
8361da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8371da177e4SLinus Torvalds 
83823db8620SAl Viro 	desc->file = file;
83923db8620SAl Viro 	desc->ctx = ctx;
840480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8411da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
84223db8620SAl Viro 	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
8431da177e4SLinus Torvalds 
844565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
84507b5ce8eSScott Mayhew 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
84623db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
847fccca7fcSTrond Myklebust 	if (res < 0)
848fccca7fcSTrond Myklebust 		goto out;
849fccca7fcSTrond Myklebust 
85047c716cbSTrond Myklebust 	do {
8511da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
85200a92642SOlivier Galibert 
8531da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
854ece0b423STrond Myklebust 			res = 0;
8551da177e4SLinus Torvalds 			/* This means either end of directory */
856d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8571da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
85823db8620SAl Viro 				res = uncached_readdir(desc);
859ece0b423STrond Myklebust 				if (res == 0)
8601da177e4SLinus Torvalds 					continue;
8611da177e4SLinus Torvalds 			}
8621da177e4SLinus Torvalds 			break;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8653a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8661da177e4SLinus Torvalds 			nfs_zap_caches(inode);
867baf57a09STrond Myklebust 			desc->page_index = 0;
8681da177e4SLinus Torvalds 			desc->plus = 0;
869d1bacf9eSBryan Schumaker 			desc->eof = 0;
8701da177e4SLinus Torvalds 			continue;
8711da177e4SLinus Torvalds 		}
8721da177e4SLinus Torvalds 		if (res < 0)
8731da177e4SLinus Torvalds 			break;
8741da177e4SLinus Torvalds 
87523db8620SAl Viro 		res = nfs_do_filldir(desc);
876ece0b423STrond Myklebust 		if (res < 0)
8771da177e4SLinus Torvalds 			break;
87847c716cbSTrond Myklebust 	} while (!desc->eof);
879fccca7fcSTrond Myklebust out:
880565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8811e7cb3dcSChuck Lever 	if (res > 0)
8821e7cb3dcSChuck Lever 		res = 0;
883aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8841e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8851e7cb3dcSChuck Lever 			res);
8861da177e4SLinus Torvalds 	return res;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
889965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
890f0dd2136STrond Myklebust {
891b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
892b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
893480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
894b84e06c5SChuck Lever 
8956da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
896b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
897b84e06c5SChuck Lever 			dentry->d_name.name,
898965c8e59SAndrew Morton 			offset, whence);
899b84e06c5SChuck Lever 
900b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
901965c8e59SAndrew Morton 	switch (whence) {
902f0dd2136STrond Myklebust 		case 1:
903f0dd2136STrond Myklebust 			offset += filp->f_pos;
904f0dd2136STrond Myklebust 		case 0:
905f0dd2136STrond Myklebust 			if (offset >= 0)
906f0dd2136STrond Myklebust 				break;
907f0dd2136STrond Myklebust 		default:
908f0dd2136STrond Myklebust 			offset = -EINVAL;
909f0dd2136STrond Myklebust 			goto out;
910f0dd2136STrond Myklebust 	}
911f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
912f0dd2136STrond Myklebust 		filp->f_pos = offset;
913480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9148ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
915f0dd2136STrond Myklebust 	}
916f0dd2136STrond Myklebust out:
917b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
918f0dd2136STrond Myklebust 	return offset;
919f0dd2136STrond Myklebust }
920f0dd2136STrond Myklebust 
9211da177e4SLinus Torvalds /*
9221da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9231da177e4SLinus Torvalds  * is a dummy operation.
9241da177e4SLinus Torvalds  */
92502c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
92602c24a82SJosef Bacik 			 int datasync)
9271da177e4SLinus Torvalds {
9287ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
92902c24a82SJosef Bacik 	struct inode *inode = dentry->d_inode;
9307ea80859SChristoph Hellwig 
9316da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9321e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9331e7cb3dcSChuck Lever 			datasync);
9341e7cb3dcSChuck Lever 
93502c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
93654917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
93702c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9381da177e4SLinus Torvalds 	return 0;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
941bfc69a45STrond Myklebust /**
942bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
943bfc69a45STrond Myklebust  * @dir - pointer to directory inode
944bfc69a45STrond Myklebust  *
945bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
946bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
947bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
948bfc69a45STrond Myklebust  *
949bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
950bfc69a45STrond Myklebust  */
951bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
952bfc69a45STrond Myklebust {
953011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
954bfc69a45STrond Myklebust }
95589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
956bfc69a45STrond Myklebust 
9571da177e4SLinus Torvalds /*
9581da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9591da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9601da177e4SLinus Torvalds  * and may need to be looked up again.
9611da177e4SLinus Torvalds  */
962c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9651da177e4SLinus Torvalds 		return 1;
9664eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9674eec952eSTrond Myklebust 		return 0;
968f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9696ecc5e8fSTrond Myklebust 		return 0;
970f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
971f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
972f2c77f4eSTrond Myklebust 		return 0;
973f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
974f2c77f4eSTrond Myklebust 		return 0;
975f2c77f4eSTrond Myklebust 	return 1;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds /*
979a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
980a12802caSTrond Myklebust  * an O_EXCL create using this path component.
981a12802caSTrond Myklebust  */
982fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
983a12802caSTrond Myklebust {
984a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
985a12802caSTrond Myklebust 		return 0;
986fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
987a12802caSTrond Myklebust }
988a12802caSTrond Myklebust 
989a12802caSTrond Myklebust /*
9901d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9911d6757fbSTrond Myklebust  *
9921d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9931d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9941d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9951d6757fbSTrond Myklebust  *
9961d6757fbSTrond Myklebust  */
99765a0c149STrond Myklebust static
998fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
100165a0c149STrond Myklebust 	int ret;
10021da177e4SLinus Torvalds 
100336d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10044e99a1ffSTrond Myklebust 		return 0;
10051da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1006fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10071da177e4SLinus Torvalds 		goto out_force;
10081da177e4SLinus Torvalds 	/* This is an open(2) */
1009fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1010fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10111da177e4SLinus Torvalds 		goto out_force;
101265a0c149STrond Myklebust out:
101365a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10141da177e4SLinus Torvalds out_force:
101565a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
101665a0c149STrond Myklebust 	if (ret != 0)
101765a0c149STrond Myklebust 		return ret;
101865a0c149STrond Myklebust 	goto out;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds /*
10221da177e4SLinus Torvalds  * We judge how long we want to trust negative
10231da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10241da177e4SLinus Torvalds  *
10251da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10261da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10271da177e4SLinus Torvalds  */
10281da177e4SLinus Torvalds static inline
10291da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1030fa3c56bbSAl Viro 		       unsigned int flags)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1033fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10341da177e4SLinus Torvalds 		return 0;
10354eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10364eec952eSTrond Myklebust 		return 1;
10371da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds /*
10411da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10421da177e4SLinus Torvalds  * and we should check whether we can really trust that
10431da177e4SLinus Torvalds  * lookup.
10441da177e4SLinus Torvalds  *
10451da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10461da177e4SLinus Torvalds  * we have an inode!
10471da177e4SLinus Torvalds  *
10481da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10491da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10501da177e4SLinus Torvalds  */
10510b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	struct inode *dir;
10541da177e4SLinus Torvalds 	struct inode *inode;
10551da177e4SLinus Torvalds 	struct dentry *parent;
1056e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1057e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10581775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
10591da177e4SLinus Torvalds 	int error;
10601da177e4SLinus Torvalds 
1061fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
106234286d66SNick Piggin 		return -ECHILD;
106334286d66SNick Piggin 
10641da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10651da177e4SLinus Torvalds 	dir = parent->d_inode;
106691d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10671da177e4SLinus Torvalds 	inode = dentry->d_inode;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if (!inode) {
1070fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
10711da177e4SLinus Torvalds 			goto out_bad;
1072d69ee9b8STrond Myklebust 		goto out_valid_noent;
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10761e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10773110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10781e7cb3dcSChuck Lever 				dentry->d_name.name);
10791da177e4SLinus Torvalds 		goto out_bad;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
1082011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
108315860ab1STrond Myklebust 		goto out_set_verifier;
108415860ab1STrond Myklebust 
10851da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1086fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1087fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
10881da177e4SLinus Torvalds 			goto out_zap_parent;
10891da177e4SLinus Torvalds 		goto out_valid;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10931da177e4SLinus Torvalds 		goto out_bad;
10941da177e4SLinus Torvalds 
1095e1fb4d05STrond Myklebust 	error = -ENOMEM;
1096e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1097e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1098e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1099e1fb4d05STrond Myklebust 		goto out_error;
1100e1fb4d05STrond Myklebust 
110114c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
110214c43f76SDavid Quigley 	if (IS_ERR(label))
110314c43f76SDavid Quigley 		goto out_error;
110414c43f76SDavid Quigley 
11056e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
11061775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
11076e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
11081da177e4SLinus Torvalds 	if (error)
11091da177e4SLinus Torvalds 		goto out_bad;
1110e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11111da177e4SLinus Torvalds 		goto out_bad;
1112e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11131da177e4SLinus Torvalds 		goto out_bad;
11141da177e4SLinus Torvalds 
1115aa9c2669SDavid Quigley 	nfs_setsecurity(inode, fattr, label);
1116aa9c2669SDavid Quigley 
1117e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1118e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
111914c43f76SDavid Quigley 	nfs4_label_free(label);
112014c43f76SDavid Quigley 
112115860ab1STrond Myklebust out_set_verifier:
1122cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11231da177e4SLinus Torvalds  out_valid:
1124d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1125d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1126d69ee9b8STrond Myklebust  out_valid_noent:
11271da177e4SLinus Torvalds 	dput(parent);
11281e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
11293110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11301e7cb3dcSChuck Lever 			dentry->d_name.name);
11311da177e4SLinus Torvalds 	return 1;
11321da177e4SLinus Torvalds out_zap_parent:
11331da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11341da177e4SLinus Torvalds  out_bad:
1135c44600c9SAl Viro 	nfs_free_fattr(fattr);
1136c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
113714c43f76SDavid Quigley 	nfs4_label_free(label);
1138a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11391da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11401da177e4SLinus Torvalds 		/* Purge readdir caches. */
11411da177e4SLinus Torvalds 		nfs_zap_caches(inode);
1142d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1143d9e80b7dSAl Viro 			goto out_valid;
11441da177e4SLinus Torvalds 	}
114513caa9fbSMiklos Szeredi 	/* If we have submounts, don't unhash ! */
114613caa9fbSMiklos Szeredi 	if (check_submounts_and_drop(dentry) != 0)
114713caa9fbSMiklos Szeredi 		goto out_valid;
114813caa9fbSMiklos Szeredi 
11491da177e4SLinus Torvalds 	dput(parent);
11501e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11513110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11521e7cb3dcSChuck Lever 			dentry->d_name.name);
11531da177e4SLinus Torvalds 	return 0;
1154e1fb4d05STrond Myklebust out_error:
1155e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1156e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
115714c43f76SDavid Quigley 	nfs4_label_free(label);
1158e1fb4d05STrond Myklebust 	dput(parent);
1159e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1160e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1161e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1162e1fb4d05STrond Myklebust 	return error;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds /*
1166ecf3d1f1SJeff Layton  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1167ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1168ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1169ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1170ecf3d1f1SJeff Layton  *
1171ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1172ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1173ecf3d1f1SJeff Layton  */
1174ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1175ecf3d1f1SJeff Layton {
1176ecf3d1f1SJeff Layton 	int error;
1177ecf3d1f1SJeff Layton 	struct inode *inode = dentry->d_inode;
1178ecf3d1f1SJeff Layton 
1179ecf3d1f1SJeff Layton 	/*
1180ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1181ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1182ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1183ecf3d1f1SJeff Layton 	 */
1184ecf3d1f1SJeff Layton 	if (!inode) {
1185ecf3d1f1SJeff Layton 		dfprintk(LOOKUPCACHE, "%s: %s/%s has negative inode\n",
1186ecf3d1f1SJeff Layton 				__func__, dentry->d_parent->d_name.name,
1187ecf3d1f1SJeff Layton 				dentry->d_name.name);
1188ecf3d1f1SJeff Layton 		return 1;
1189ecf3d1f1SJeff Layton 	}
1190ecf3d1f1SJeff Layton 
1191ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
1192ecf3d1f1SJeff Layton 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
1193ecf3d1f1SJeff Layton 				__func__, dentry->d_parent->d_name.name,
1194ecf3d1f1SJeff Layton 				dentry->d_name.name);
1195ecf3d1f1SJeff Layton 		return 0;
1196ecf3d1f1SJeff Layton 	}
1197ecf3d1f1SJeff Layton 
1198ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1199ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1200ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1201ecf3d1f1SJeff Layton 	return !error;
1202ecf3d1f1SJeff Layton }
1203ecf3d1f1SJeff Layton 
1204ecf3d1f1SJeff Layton /*
12051da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
12061da177e4SLinus Torvalds  */
1207fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
12101da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
12111da177e4SLinus Torvalds 		dentry->d_flags);
12121da177e4SLinus Torvalds 
121377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
121477f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
121577f11192STrond Myklebust 		return 1;
121677f11192STrond Myklebust 
12171da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12181da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12191da177e4SLinus Torvalds 		return 1;
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12221da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12231da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12241da177e4SLinus Torvalds 		return 1;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 	return 0;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12311b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12321b83d707STrond Myklebust {
12331b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12341f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12351f018458STrond Myklebust 	if (inode->i_nlink == 1)
12361f018458STrond Myklebust 		clear_nlink(inode);
12371f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12381b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12391b83d707STrond Myklebust }
12401b83d707STrond Myklebust 
12411da177e4SLinus Torvalds /*
12421da177e4SLinus Torvalds  * Called when the dentry loses inode.
12431da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12441da177e4SLinus Torvalds  */
12451da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12461da177e4SLinus Torvalds {
124783672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
124883672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
124983672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
125083672d39SNeil Brown 
12511da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1252e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12531f018458STrond Myklebust 		nfs_drop_nlink(inode);
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 	iput(inode);
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
1258b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1259b1942c5fSAl Viro {
1260b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1261b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1262b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1263b1942c5fSAl Viro 			WARN_ON(1);
1264b1942c5fSAl Viro 		else
1265b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1266b1942c5fSAl Viro 	}
1267b1942c5fSAl Viro }
1268b1942c5fSAl Viro 
1269f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12701da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1271ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
12721da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12731da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
127436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1275b1942c5fSAl Viro 	.d_release	= nfs_d_release,
12761da177e4SLinus Torvalds };
1277ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
12781da177e4SLinus Torvalds 
1279597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	struct dentry *res;
1282565277f6STrond Myklebust 	struct dentry *parent;
12831da177e4SLinus Torvalds 	struct inode *inode = NULL;
1284e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1285e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12861775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
12871da177e4SLinus Torvalds 	int error;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
12901da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
129191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12941da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12951da177e4SLinus Torvalds 		goto out;
12961da177e4SLinus Torvalds 
1297fd684071STrond Myklebust 	/*
1298fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1299fd684071STrond Myklebust 	 * but don't hash the dentry.
1300fd684071STrond Myklebust 	 */
130100cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1302fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1303fd684071STrond Myklebust 		res = NULL;
1304fc0f684cSTrond Myklebust 		goto out;
1305fd684071STrond Myklebust 	}
13061da177e4SLinus Torvalds 
1307e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1308e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1309e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1310e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1311e1fb4d05STrond Myklebust 		goto out;
1312e1fb4d05STrond Myklebust 
131314c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
131414c43f76SDavid Quigley 	if (IS_ERR(label))
131514c43f76SDavid Quigley 		goto out;
131614c43f76SDavid Quigley 
1317565277f6STrond Myklebust 	parent = dentry->d_parent;
1318565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
13196e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1320565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
13211775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
13221da177e4SLinus Torvalds 	if (error == -ENOENT)
13231da177e4SLinus Torvalds 		goto no_entry;
13241da177e4SLinus Torvalds 	if (error < 0) {
13251da177e4SLinus Torvalds 		res = ERR_PTR(error);
1326565277f6STrond Myklebust 		goto out_unblock_sillyrename;
13271da177e4SLinus Torvalds 	}
13281775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1329bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
133003f28e3aSTrond Myklebust 	if (IS_ERR(res))
1331565277f6STrond Myklebust 		goto out_unblock_sillyrename;
133254ceac45SDavid Howells 
1333d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1334d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1335d69ee9b8STrond Myklebust 
13361da177e4SLinus Torvalds no_entry:
133754ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13389eaef27bSTrond Myklebust 	if (res != NULL) {
13399eaef27bSTrond Myklebust 		if (IS_ERR(res))
1340565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13411da177e4SLinus Torvalds 		dentry = res;
13429eaef27bSTrond Myklebust 	}
13431da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1344565277f6STrond Myklebust out_unblock_sillyrename:
1345565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
13466e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
134714c43f76SDavid Quigley 	nfs4_label_free(label);
13481da177e4SLinus Torvalds out:
1349e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1350e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13511da177e4SLinus Torvalds 	return res;
13521da177e4SLinus Torvalds }
1353ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
13541da177e4SLinus Torvalds 
135589d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
13560b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13571da177e4SLinus Torvalds 
1358f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13590ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13601da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13611da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
136236d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1363b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13641da177e4SLinus Torvalds };
136589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
13661da177e4SLinus Torvalds 
13678a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13688a5e929dSAl Viro {
13698a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13708a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
13718a5e929dSAl Viro 		res |= FMODE_READ;
13728a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
13738a5e929dSAl Viro 		res |= FMODE_WRITE;
13748a5e929dSAl Viro 	return res;
13758a5e929dSAl Viro }
13768a5e929dSAl Viro 
137751141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1378cd9a1c0eSTrond Myklebust {
13795ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1380cd9a1c0eSTrond Myklebust }
1381cd9a1c0eSTrond Myklebust 
1382cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1383cd9a1c0eSTrond Myklebust {
1384cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1385cd9a1c0eSTrond Myklebust 	return 0;
1386cd9a1c0eSTrond Myklebust }
1387cd9a1c0eSTrond Myklebust 
1388d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13890dd2b474SMiklos Szeredi 			   struct dentry *dentry,
139030d90494SAl Viro 			   struct file *file, unsigned open_flags,
139147237687SAl Viro 			   int *opened)
1392cd9a1c0eSTrond Myklebust {
13930dd2b474SMiklos Szeredi 	int err;
13940dd2b474SMiklos Szeredi 
139530d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
139630d90494SAl Viro 	if (err)
1397d9585277SAl Viro 		goto out;
139830d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
13990dd2b474SMiklos Szeredi 
1400cd9a1c0eSTrond Myklebust out:
1401d9585277SAl Viro 	return err;
1402cd9a1c0eSTrond Myklebust }
1403cd9a1c0eSTrond Myklebust 
140473a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
140530d90494SAl Viro 		    struct file *file, unsigned open_flags,
140647237687SAl Viro 		    umode_t mode, int *opened)
14071da177e4SLinus Torvalds {
1408cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
14090dd2b474SMiklos Szeredi 	struct dentry *res;
14100dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1411f46e0bd3STrond Myklebust 	struct inode *inode;
14121472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1413898f635cSTrond Myklebust 	int err;
14141da177e4SLinus Torvalds 
14150dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14160dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
14170dd2b474SMiklos Szeredi 
14180dd2b474SMiklos Szeredi 	dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
14191e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
14201e7cb3dcSChuck Lever 
14219597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
14229597c13bSJeff Layton 	if (err)
14239597c13bSJeff Layton 		return err;
14249597c13bSJeff Layton 
14250dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14260dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14270dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14280dd2b474SMiklos Szeredi 			/*
14290dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14300dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14310dd2b474SMiklos Szeredi 			 * again
14320dd2b474SMiklos Szeredi 			 */
1433d9585277SAl Viro 			return -ENOENT;
14340dd2b474SMiklos Szeredi 		}
14351472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
14361da177e4SLinus Torvalds 		goto no_open;
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds 
14390dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1440d9585277SAl Viro 		return -ENAMETOOLONG;
14411da177e4SLinus Torvalds 
14420dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1443536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14440dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14450dd2b474SMiklos Szeredi 	}
1446536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1447536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1448536e43d1STrond Myklebust 		attr.ia_size = 0;
1449cd9a1c0eSTrond Myklebust 	}
1450cd9a1c0eSTrond Myklebust 
14510dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14520dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14530dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1454d9585277SAl Viro 		goto out;
14550dd2b474SMiklos Szeredi 
14566e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
1457f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14582b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1459f46e0bd3STrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1460275bb307STrond Myklebust 	if (IS_ERR(inode)) {
14610dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14626e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
14632d9db750STrond Myklebust 		put_nfs_open_context(ctx);
14640dd2b474SMiklos Szeredi 		switch (err) {
14651da177e4SLinus Torvalds 		case -ENOENT:
1466275bb307STrond Myklebust 			d_drop(dentry);
1467f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
14680dd2b474SMiklos Szeredi 			break;
14691788ea6eSJeff Layton 		case -EISDIR:
14706f926b5bSTrond Myklebust 		case -ENOTDIR:
14716f926b5bSTrond Myklebust 			goto no_open;
14721da177e4SLinus Torvalds 		case -ELOOP:
14730dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
14741da177e4SLinus Torvalds 				goto no_open;
14750dd2b474SMiklos Szeredi 			break;
14761da177e4SLinus Torvalds 			/* case -EINVAL: */
14771da177e4SLinus Torvalds 		default:
14780dd2b474SMiklos Szeredi 			break;
14791da177e4SLinus Torvalds 		}
14801da177e4SLinus Torvalds 		goto out;
14811da177e4SLinus Torvalds 	}
14820dd2b474SMiklos Szeredi 
1483275bb307STrond Myklebust 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
14846e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
14852d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1486d9585277SAl Viro out:
1487d9585277SAl Viro 	return err;
14880dd2b474SMiklos Szeredi 
14891da177e4SLinus Torvalds no_open:
14901472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
14910dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
14920dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1493d9585277SAl Viro 		goto out;
14940dd2b474SMiklos Szeredi 
1495e45198a6SAl Viro 	return finish_no_open(file, res);
14961da177e4SLinus Torvalds }
149789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
14981da177e4SLinus Torvalds 
14990b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
15001da177e4SLinus Torvalds {
15011da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1502657e94b6SNick Piggin 	struct inode *inode;
15031da177e4SLinus Torvalds 	struct inode *dir;
150450de348cSMiklos Szeredi 	int ret = 0;
15051da177e4SLinus Torvalds 
1506fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1507657e94b6SNick Piggin 		return -ECHILD;
1508657e94b6SNick Piggin 
1509fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1510eda72afbSMiklos Szeredi 		goto no_open;
1511eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15125584c306STrond Myklebust 		goto no_open;
151349f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
151449f9a0faSTrond Myklebust 		goto no_open;
15152b484297STrond Myklebust 
1516eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
15171da177e4SLinus Torvalds 	parent = dget_parent(dentry);
15181da177e4SLinus Torvalds 	dir = parent->d_inode;
15192b484297STrond Myklebust 
15201da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15211da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15221da177e4SLinus Torvalds 	 */
1523216d5d06STrond Myklebust 	if (inode == NULL) {
1524fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1525216d5d06STrond Myklebust 			ret = 1;
15261da177e4SLinus Torvalds 		goto out;
1527216d5d06STrond Myklebust 	}
1528216d5d06STrond Myklebust 
15291da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
15301da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15315584c306STrond Myklebust 		goto no_open_dput;
15321da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1533fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
15345584c306STrond Myklebust 		goto no_open_dput;
15351da177e4SLinus Torvalds 
15360ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1537898f635cSTrond Myklebust 	ret = 1;
15380ef97dcfSMiklos Szeredi 
15391da177e4SLinus Torvalds out:
15401da177e4SLinus Torvalds 	dput(parent);
15411da177e4SLinus Torvalds 	return ret;
1542535918f1STrond Myklebust 
15435584c306STrond Myklebust no_open_dput:
15441da177e4SLinus Torvalds 	dput(parent);
15455584c306STrond Myklebust no_open:
15460b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1547c0204fd2STrond Myklebust }
1548c0204fd2STrond Myklebust 
15491da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds /*
15521da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15531da177e4SLinus Torvalds  */
15541da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15551775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
15561775fd3eSDavid Quigley 				struct nfs4_label *label)
15571da177e4SLinus Torvalds {
1558fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1559fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15601da177e4SLinus Torvalds 	struct inode *inode;
15611da177e4SLinus Torvalds 	int error = -EACCES;
15621da177e4SLinus Torvalds 
1563fab728e1STrond Myklebust 	d_drop(dentry);
1564fab728e1STrond Myklebust 
15651da177e4SLinus Torvalds 	/* We may have been initialized further down */
15661da177e4SLinus Torvalds 	if (dentry->d_inode)
1567fab728e1STrond Myklebust 		goto out;
15681da177e4SLinus Torvalds 	if (fhandle->size == 0) {
15691775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
15701da177e4SLinus Torvalds 		if (error)
1571fab728e1STrond Myklebust 			goto out_error;
15721da177e4SLinus Torvalds 	}
15735724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15741da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15751da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15761775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
15771da177e4SLinus Torvalds 		if (error < 0)
1578fab728e1STrond Myklebust 			goto out_error;
15791da177e4SLinus Torvalds 	}
15801775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
158103f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
158203f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1583fab728e1STrond Myklebust 		goto out_error;
1584fab728e1STrond Myklebust 	d_add(dentry, inode);
1585fab728e1STrond Myklebust out:
1586fab728e1STrond Myklebust 	dput(parent);
15871da177e4SLinus Torvalds 	return 0;
1588fab728e1STrond Myklebust out_error:
1589fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1590fab728e1STrond Myklebust 	dput(parent);
1591fab728e1STrond Myklebust 	return error;
15921da177e4SLinus Torvalds }
1593ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds /*
15961da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15971da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15981da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15991da177e4SLinus Torvalds  * reply path made it appear to have failed.
16001da177e4SLinus Torvalds  */
1601597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1602ebfc3b49SAl Viro 		umode_t mode, bool excl)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds 	struct iattr attr;
1605ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
16061da177e4SLinus Torvalds 	int error;
16071da177e4SLinus Torvalds 
16081e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
16091e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	attr.ia_mode = mode;
16121da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16131da177e4SLinus Torvalds 
16148b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
16158867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16168b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
16171da177e4SLinus Torvalds 	if (error != 0)
16181da177e4SLinus Torvalds 		goto out_err;
16191da177e4SLinus Torvalds 	return 0;
16201da177e4SLinus Torvalds out_err:
16211da177e4SLinus Torvalds 	d_drop(dentry);
16221da177e4SLinus Torvalds 	return error;
16231da177e4SLinus Torvalds }
1624ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds /*
16271da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16281da177e4SLinus Torvalds  */
1629597d9289SBryan Schumaker int
16301a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	struct iattr attr;
16331da177e4SLinus Torvalds 	int status;
16341da177e4SLinus Torvalds 
16351e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
16361e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16391da177e4SLinus Torvalds 		return -EINVAL;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	attr.ia_mode = mode;
16421da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16431da177e4SLinus Torvalds 
16441ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
16451da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16461ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
16471da177e4SLinus Torvalds 	if (status != 0)
16481da177e4SLinus Torvalds 		goto out_err;
16491da177e4SLinus Torvalds 	return 0;
16501da177e4SLinus Torvalds out_err:
16511da177e4SLinus Torvalds 	d_drop(dentry);
16521da177e4SLinus Torvalds 	return status;
16531da177e4SLinus Torvalds }
1654ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds /*
16571da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16581da177e4SLinus Torvalds  */
1659597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	struct iattr attr;
16621da177e4SLinus Torvalds 	int error;
16631da177e4SLinus Torvalds 
16641e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16651e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16681da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16691da177e4SLinus Torvalds 
16701ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
16711da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16721ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
16731da177e4SLinus Torvalds 	if (error != 0)
16741da177e4SLinus Torvalds 		goto out_err;
16751da177e4SLinus Torvalds 	return 0;
16761da177e4SLinus Torvalds out_err:
16771da177e4SLinus Torvalds 	d_drop(dentry);
16781da177e4SLinus Torvalds 	return error;
16791da177e4SLinus Torvalds }
1680ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
16811da177e4SLinus Torvalds 
1682d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1683d45b9d8bSTrond Myklebust {
1684d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1685d45b9d8bSTrond Myklebust 		d_delete(dentry);
1686d45b9d8bSTrond Myklebust }
1687d45b9d8bSTrond Myklebust 
1688597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16891da177e4SLinus Torvalds {
16901da177e4SLinus Torvalds 	int error;
16911da177e4SLinus Torvalds 
16921e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16931e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16941da177e4SLinus Torvalds 
16951ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
1696ba6c0592STrond Myklebust 	if (dentry->d_inode) {
1697ba6c0592STrond Myklebust 		nfs_wait_on_sillyrename(dentry);
16981da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16991da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1700ba6c0592STrond Myklebust 		switch (error) {
1701ba6c0592STrond Myklebust 		case 0:
1702ce71ec36SDave Hansen 			clear_nlink(dentry->d_inode);
1703ba6c0592STrond Myklebust 			break;
1704ba6c0592STrond Myklebust 		case -ENOENT:
1705d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1706ba6c0592STrond Myklebust 		}
1707ba6c0592STrond Myklebust 	} else
1708ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17091ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	return error;
17121da177e4SLinus Torvalds }
1713ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds /*
17161da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17171da177e4SLinus Torvalds  * and after checking that the file has only one user.
17181da177e4SLinus Torvalds  *
17191da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17201da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17211da177e4SLinus Torvalds  */
17221da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17251da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17261da177e4SLinus Torvalds 	int error = -EBUSY;
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
17291da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
17321da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17331da177e4SLinus Torvalds 		error = 0;
17341da177e4SLinus Torvalds 		goto out;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 
17371ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
17381da177e4SLinus Torvalds 	if (inode != NULL) {
173957ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
17401da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17411da177e4SLinus Torvalds 		if (error == 0)
17421b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17431da177e4SLinus Torvalds 	} else
17441da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1745d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1746d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17471ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
17481da177e4SLinus Torvalds out:
17491da177e4SLinus Torvalds 	return error;
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17531da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17541da177e4SLinus Torvalds  *
17551da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17561da177e4SLinus Torvalds  */
1757597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
17581da177e4SLinus Torvalds {
17591da177e4SLinus Torvalds 	int error;
17601da177e4SLinus Torvalds 	int need_rehash = 0;
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17631da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17641da177e4SLinus Torvalds 
17651ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
17661da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
176784d08fa8SAl Viro 	if (d_count(dentry) > 1) {
17681da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1769ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1770ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17711da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17721ca42382STrond Myklebust 		goto out;
17731da177e4SLinus Torvalds 	}
17741da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17751da177e4SLinus Torvalds 		__d_drop(dentry);
17761da177e4SLinus Torvalds 		need_rehash = 1;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17791da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1780d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17811da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17821da177e4SLinus Torvalds 	} else if (need_rehash)
17831da177e4SLinus Torvalds 		d_rehash(dentry);
17841ca42382STrond Myklebust out:
17851ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
17861da177e4SLinus Torvalds 	return error;
17871da177e4SLinus Torvalds }
1788ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
17891da177e4SLinus Torvalds 
1790873101b3SChuck Lever /*
1791873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1792873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1793873101b3SChuck Lever  * using prepare_write/commit_write.
1794873101b3SChuck Lever  *
1795873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1796873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1797873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1798873101b3SChuck Lever  * symlink request has completed on the server.
1799873101b3SChuck Lever  *
1800873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1801873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1802873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1803873101b3SChuck Lever  * and move the raw page into its mapping.
1804873101b3SChuck Lever  */
1805597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18061da177e4SLinus Torvalds {
1807873101b3SChuck Lever 	struct page *page;
1808873101b3SChuck Lever 	char *kaddr;
18091da177e4SLinus Torvalds 	struct iattr attr;
1810873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
18111da177e4SLinus Torvalds 	int error;
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
18141da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
18151da177e4SLinus Torvalds 
1816873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1817873101b3SChuck Lever 		return -ENAMETOOLONG;
18181da177e4SLinus Torvalds 
1819873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1820873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18211da177e4SLinus Torvalds 
182283d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
182376566991STrond Myklebust 	if (!page)
1824873101b3SChuck Lever 		return -ENOMEM;
1825873101b3SChuck Lever 
18262b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1827873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1828873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1829873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
18302b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1831873101b3SChuck Lever 
18321ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
183394a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
18341ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1835873101b3SChuck Lever 	if (error != 0) {
1836873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1837873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1838873101b3SChuck Lever 			dentry->d_name.name, symname, error);
18391da177e4SLinus Torvalds 		d_drop(dentry);
1840873101b3SChuck Lever 		__free_page(page);
18411da177e4SLinus Torvalds 		return error;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 
1844873101b3SChuck Lever 	/*
1845873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1846873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1847873101b3SChuck Lever 	 */
1848a0b8cab3SMel Gorman 	if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
1849873101b3SChuck Lever 							GFP_KERNEL)) {
1850873101b3SChuck Lever 		SetPageUptodate(page);
1851873101b3SChuck Lever 		unlock_page(page);
1852873101b3SChuck Lever 	} else
1853873101b3SChuck Lever 		__free_page(page);
1854873101b3SChuck Lever 
1855873101b3SChuck Lever 	return 0;
1856873101b3SChuck Lever }
1857ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1858873101b3SChuck Lever 
1859597d9289SBryan Schumaker int
18601da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18611da177e4SLinus Torvalds {
18621da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18631da177e4SLinus Torvalds 	int error;
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18661da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18671da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18681da177e4SLinus Torvalds 
18691fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
187057ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
18719a3936aaSTrond Myklebust 
18729697d234STrond Myklebust 	d_drop(dentry);
18731da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1874cf809556STrond Myklebust 	if (error == 0) {
18757de9c6eeSAl Viro 		ihold(inode);
18769697d234STrond Myklebust 		d_add(dentry, inode);
1877cf809556STrond Myklebust 	}
18781fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
18791da177e4SLinus Torvalds 	return error;
18801da177e4SLinus Torvalds }
1881ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds /*
18841da177e4SLinus Torvalds  * RENAME
18851da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18861da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18871da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18881da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18891da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18901da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18911da177e4SLinus Torvalds  *
18921da177e4SLinus Torvalds  * FIXED.
18931da177e4SLinus Torvalds  *
18941da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18951da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18961da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18971da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18981da177e4SLinus Torvalds  * using the inode layer
18991da177e4SLinus Torvalds  *
19001da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
19011da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
19021da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
19031da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
19041da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
19051da177e4SLinus Torvalds  * the rename.
19061da177e4SLinus Torvalds  */
1907597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
19081da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
19091da177e4SLinus Torvalds {
19101da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
19111da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
19121da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
19131da177e4SLinus Torvalds 	int error = -EBUSY;
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
19161da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
19171da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
191884d08fa8SAl Viro 		 d_count(new_dentry));
19191da177e4SLinus Torvalds 
192070ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
19211da177e4SLinus Torvalds 	/*
192228f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
192328f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
192428f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
192528f79a1aSMiklos Szeredi 	 * the new target.
19261da177e4SLinus Torvalds 	 */
192727226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
192827226104SMiklos Szeredi 		/*
192927226104SMiklos Szeredi 		 * To prevent any new references to the target during the
193027226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
193127226104SMiklos Szeredi 		 */
193227226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
193327226104SMiklos Szeredi 			d_drop(new_dentry);
193427226104SMiklos Szeredi 			rehash = new_dentry;
193527226104SMiklos Szeredi 		}
193627226104SMiklos Szeredi 
193784d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
19381da177e4SLinus Torvalds 			int err;
193927226104SMiklos Szeredi 
19401da177e4SLinus Torvalds 			/* copy the target dentry's name */
19411da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19421da177e4SLinus Torvalds 					 &new_dentry->d_name);
19431da177e4SLinus Torvalds 			if (!dentry)
19441da177e4SLinus Torvalds 				goto out;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19471da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
194824e93025SMiklos Szeredi 			if (err)
19491da177e4SLinus Torvalds 				goto out;
195024e93025SMiklos Szeredi 
195124e93025SMiklos Szeredi 			new_dentry = dentry;
195256335936SOGAWA Hirofumi 			rehash = NULL;
195324e93025SMiklos Szeredi 			new_inode = NULL;
1954b1e4adf4STrond Myklebust 		}
195527226104SMiklos Szeredi 	}
19561da177e4SLinus Torvalds 
195757ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1958b1e4adf4STrond Myklebust 	if (new_inode != NULL)
195957ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19621da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19635ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19641da177e4SLinus Torvalds out:
19651da177e4SLinus Torvalds 	if (rehash)
19661da177e4SLinus Torvalds 		d_rehash(rehash);
196770ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
196870ded201STrond Myklebust 			new_dir, new_dentry, error);
19691da177e4SLinus Torvalds 	if (!error) {
1970b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1971b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19721da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19738fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19748fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1975d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1976d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	/* new dentry created? */
19791da177e4SLinus Torvalds 	if (dentry)
19801da177e4SLinus Torvalds 		dput(dentry);
19811da177e4SLinus Torvalds 	return error;
19821da177e4SLinus Torvalds }
1983ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
19841da177e4SLinus Torvalds 
1985cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1986cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1987cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1988cfcea3e8STrond Myklebust 
19891c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19901c3c07e9STrond Myklebust {
19911c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19921c3c07e9STrond Myklebust 	kfree(entry);
1993cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1994cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1995cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19961c3c07e9STrond Myklebust }
19971c3c07e9STrond Myklebust 
19981a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19991a81bb8aSTrond Myklebust {
20001a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
20011a81bb8aSTrond Myklebust 
20021a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
20031a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
20041a81bb8aSTrond Myklebust 		list_del(&cache->lru);
20051a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
20061a81bb8aSTrond Myklebust 	}
20071a81bb8aSTrond Myklebust }
20081a81bb8aSTrond Myklebust 
20091ab6c499SDave Chinner unsigned long
20101ab6c499SDave Chinner nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2011979df72eSTrond Myklebust {
2012979df72eSTrond Myklebust 	LIST_HEAD(head);
2013aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2014979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
20151495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
20161495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
20171ab6c499SDave Chinner 	long freed = 0;
2018979df72eSTrond Myklebust 
201961d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
20201ab6c499SDave Chinner 		return SHRINK_STOP;
20219c7e7e23STrond Myklebust 
2022a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2023aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2024979df72eSTrond Myklebust 		struct inode *inode;
2025979df72eSTrond Myklebust 
2026979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2027979df72eSTrond Myklebust 			break;
20289c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2029979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2030979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2031979df72eSTrond Myklebust 			goto remove_lru_entry;
2032979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2033979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2034979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2035979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
20361ab6c499SDave Chinner 		freed++;
2037979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2038979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2039979df72eSTrond Myklebust 					&nfs_access_lru_list);
2040979df72eSTrond Myklebust 		else {
2041979df72eSTrond Myklebust remove_lru_entry:
2042979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
20439c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
2044979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
20459c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
2046979df72eSTrond Myklebust 		}
204759844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2048979df72eSTrond Myklebust 	}
2049979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20501a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20511ab6c499SDave Chinner 	return freed;
20521ab6c499SDave Chinner }
20531ab6c499SDave Chinner 
20541ab6c499SDave Chinner unsigned long
20551ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
20561ab6c499SDave Chinner {
205755f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2058979df72eSTrond Myklebust }
2059979df72eSTrond Myklebust 
20601a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20611c3c07e9STrond Myklebust {
20621c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20631a81bb8aSTrond Myklebust 	struct rb_node *n;
20641c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20651c3c07e9STrond Myklebust 
20661c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20671c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20681c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20691c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20701a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20711c3c07e9STrond Myklebust 	}
20721c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20731c3c07e9STrond Myklebust }
20741c3c07e9STrond Myklebust 
20751c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20761c3c07e9STrond Myklebust {
20771a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20781a81bb8aSTrond Myklebust 
20791a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20801a81bb8aSTrond Myklebust 		return;
2081cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2082cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20831a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2084cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2085cfcea3e8STrond Myklebust 
20861c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20871a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20881a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20891a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20901a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20911c3c07e9STrond Myklebust }
20921c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
20931c3c07e9STrond Myklebust 
20941c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20951c3c07e9STrond Myklebust {
20961c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20971c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20981c3c07e9STrond Myklebust 
20991c3c07e9STrond Myklebust 	while (n != NULL) {
21001c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
21011c3c07e9STrond Myklebust 
21021c3c07e9STrond Myklebust 		if (cred < entry->cred)
21031c3c07e9STrond Myklebust 			n = n->rb_left;
21041c3c07e9STrond Myklebust 		else if (cred > entry->cred)
21051c3c07e9STrond Myklebust 			n = n->rb_right;
21061c3c07e9STrond Myklebust 		else
21071c3c07e9STrond Myklebust 			return entry;
21081c3c07e9STrond Myklebust 	}
21091c3c07e9STrond Myklebust 	return NULL;
21101c3c07e9STrond Myklebust }
21111c3c07e9STrond Myklebust 
2112af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
21131da177e4SLinus Torvalds {
211455296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
21151c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
21161c3c07e9STrond Myklebust 	int err = -ENOENT;
21171da177e4SLinus Torvalds 
21181c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21191c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
21201c3c07e9STrond Myklebust 		goto out_zap;
21211c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
21221c3c07e9STrond Myklebust 	if (cache == NULL)
21231c3c07e9STrond Myklebust 		goto out;
2124b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
212564672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
21261c3c07e9STrond Myklebust 		goto out_stale;
21271c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
21281c3c07e9STrond Myklebust 	res->cred = cache->cred;
21291c3c07e9STrond Myklebust 	res->mask = cache->mask;
2130cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
21311c3c07e9STrond Myklebust 	err = 0;
21321c3c07e9STrond Myklebust out:
21331c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21341c3c07e9STrond Myklebust 	return err;
21351c3c07e9STrond Myklebust out_stale:
21361c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2137cfcea3e8STrond Myklebust 	list_del(&cache->lru);
21381c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21391c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
21401da177e4SLinus Torvalds 	return -ENOENT;
21411c3c07e9STrond Myklebust out_zap:
21421a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21431a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
21441c3c07e9STrond Myklebust 	return -ENOENT;
21451c3c07e9STrond Myklebust }
21461c3c07e9STrond Myklebust 
21471c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
21481c3c07e9STrond Myklebust {
2149cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2150cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21511c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
21521c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
21531c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21541c3c07e9STrond Myklebust 
21551c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21561c3c07e9STrond Myklebust 	while (*p != NULL) {
21571c3c07e9STrond Myklebust 		parent = *p;
21581c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21591c3c07e9STrond Myklebust 
21601c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21611c3c07e9STrond Myklebust 			p = &parent->rb_left;
21621c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21631c3c07e9STrond Myklebust 			p = &parent->rb_right;
21641c3c07e9STrond Myklebust 		else
21651c3c07e9STrond Myklebust 			goto found;
21661c3c07e9STrond Myklebust 	}
21671c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21681c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2169cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21701c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21711c3c07e9STrond Myklebust 	return;
21721c3c07e9STrond Myklebust found:
21731c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2174cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2175cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21761c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21771c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21806168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21811da177e4SLinus Torvalds {
21821c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21831c3c07e9STrond Myklebust 	if (cache == NULL)
21841c3c07e9STrond Myklebust 		return;
21851c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21861da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21871c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21881da177e4SLinus Torvalds 	cache->mask = set->mask;
21891c3c07e9STrond Myklebust 
21901c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2191cfcea3e8STrond Myklebust 
2192cfcea3e8STrond Myklebust 	/* Update accounting */
2193cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2194cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2195cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2196cfcea3e8STrond Myklebust 
2197cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21981a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2199cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
22001a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
22011a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
22021a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2203cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2204cfcea3e8STrond Myklebust 	}
22051da177e4SLinus Torvalds }
22066168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
22076168f62cSWeston Andros Adamson 
22086168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
22096168f62cSWeston Andros Adamson {
22106168f62cSWeston Andros Adamson 	entry->mask = 0;
22116168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
22126168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
22136168f62cSWeston Andros Adamson 	if (access_result &
22146168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
22156168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
22166168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
22176168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
22186168f62cSWeston Andros Adamson }
22196168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	struct nfs_access_entry cache;
22241da177e4SLinus Torvalds 	int status;
22251da177e4SLinus Torvalds 
2226f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2227f4ce1299STrond Myklebust 
22281da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
22291da177e4SLinus Torvalds 	if (status == 0)
2230f4ce1299STrond Myklebust 		goto out_cached;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
22331da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
22341da177e4SLinus Torvalds 	cache.cred = cred;
22351da177e4SLinus Torvalds 	cache.jiffies = jiffies;
22361da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2237a71ee337SSuresh Jayaraman 	if (status != 0) {
2238a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2239a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2240a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2241a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2242a71ee337SSuresh Jayaraman 		}
2243f4ce1299STrond Myklebust 		goto out;
2244a71ee337SSuresh Jayaraman 	}
22451da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2246f4ce1299STrond Myklebust out_cached:
2247f4ce1299STrond Myklebust 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2248f4ce1299STrond Myklebust 		status = -EACCES;
22491da177e4SLinus Torvalds out:
2250f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2251f4ce1299STrond Myklebust 	return status;
22521da177e4SLinus Torvalds }
22531da177e4SLinus Torvalds 
2254af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2255af22f94aSTrond Myklebust {
2256af22f94aSTrond Myklebust 	int mask = 0;
2257af22f94aSTrond Myklebust 
2258f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2259f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2260f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2261f8d9a897SWeston Andros Adamson 	} else {
22628a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2263af22f94aSTrond Myklebust 			mask |= MAY_READ;
22648a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2265af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2266f8d9a897SWeston Andros Adamson 	}
2267f8d9a897SWeston Andros Adamson 
2268af22f94aSTrond Myklebust 	return mask;
2269af22f94aSTrond Myklebust }
2270af22f94aSTrond Myklebust 
2271af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2272af22f94aSTrond Myklebust {
2273af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2274af22f94aSTrond Myklebust }
227589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2276af22f94aSTrond Myklebust 
227710556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
22781da177e4SLinus Torvalds {
22791da177e4SLinus Torvalds 	struct rpc_cred *cred;
22801da177e4SLinus Torvalds 	int res = 0;
22811da177e4SLinus Torvalds 
228210556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2283b74c79e9SNick Piggin 		return -ECHILD;
2284b74c79e9SNick Piggin 
228591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
228691d5b470SChuck Lever 
2287e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22881da177e4SLinus Torvalds 		goto out;
22891da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22909cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22911da177e4SLinus Torvalds 		goto force_lookup;
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22941da177e4SLinus Torvalds 		case S_IFLNK:
22951da177e4SLinus Torvalds 			goto out;
22961da177e4SLinus Torvalds 		case S_IFREG:
22971da177e4SLinus Torvalds 			break;
22981da177e4SLinus Torvalds 		case S_IFDIR:
22991da177e4SLinus Torvalds 			/*
23001da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
23011da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
23021da177e4SLinus Torvalds 			 */
23031da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
23041da177e4SLinus Torvalds 				goto out;
23051da177e4SLinus Torvalds 	}
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds force_lookup:
23081da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
23091da177e4SLinus Torvalds 		goto out_notsup;
23101da177e4SLinus Torvalds 
231198a8e323STrond Myklebust 	cred = rpc_lookup_cred();
23121da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
23131da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
23141da177e4SLinus Torvalds 		put_rpccred(cred);
23151da177e4SLinus Torvalds 	} else
23161da177e4SLinus Torvalds 		res = PTR_ERR(cred);
23171da177e4SLinus Torvalds out:
2318f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2319f696a365SMiklos Szeredi 		res = -EACCES;
2320f696a365SMiklos Szeredi 
23211e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
23221e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
23231da177e4SLinus Torvalds 	return res;
23241da177e4SLinus Torvalds out_notsup:
23251da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
23261da177e4SLinus Torvalds 	if (res == 0)
23272830ba7fSAl Viro 		res = generic_permission(inode, mask);
23281e7cb3dcSChuck Lever 	goto out;
23291da177e4SLinus Torvalds }
2330ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds /*
23331da177e4SLinus Torvalds  * Local variables:
23341da177e4SLinus Torvalds  *  version-control: t
23351da177e4SLinus Torvalds  *  kept-new-versions: 5
23361da177e4SLinus Torvalds  * End:
23371da177e4SLinus Torvalds  */
2338