xref: /openbmc/linux/fs/nfs/dir.c (revision 73a79706d7f197a428a43fbf335bbe75cdbc221f)
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 
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/stat.h>
231da177e4SLinus Torvalds #include <linux/fcntl.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
291da177e4SLinus Torvalds #include <linux/nfs_fs.h>
301da177e4SLinus Torvalds #include <linux/nfs_mount.h>
311da177e4SLinus Torvalds #include <linux/pagemap.h>
32873101b3SChuck Lever #include <linux/pagevec.h>
331da177e4SLinus Torvalds #include <linux/namei.h>
3454ceac45SDavid Howells #include <linux/mount.h>
35e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3604e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3764c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include "delegation.h"
4091d5b470SChuck Lever #include "iostat.h"
414c30d56eSAdrian Bunk #include "internal.h"
42cd9a1c0eSTrond Myklebust #include "fscache.h"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
47480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
481da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
4902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
50f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5111de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
521da177e4SLinus Torvalds 
534b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
54f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
551da177e4SLinus Torvalds 	.read		= generic_read_dir,
561da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
571da177e4SLinus Torvalds 	.open		= nfs_opendir,
58480c2006SBryan Schumaker 	.release	= nfs_closedir,
591da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
6211de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6311de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
64d1bacf9eSBryan Schumaker };
65d1bacf9eSBryan Schumaker 
660c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
67480c2006SBryan Schumaker {
68480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
69480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
70480c2006SBryan Schumaker 	if (ctx != NULL) {
718ef2ce3eSBryan Schumaker 		ctx->duped = 0;
720c030806STrond Myklebust 		ctx->attr_gencount = NFS_I(dir)->attr_gencount;
73480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
748ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
75480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
76480c2006SBryan Schumaker 		return ctx;
77480c2006SBryan Schumaker 	}
780c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
790c030806STrond Myklebust }
80480c2006SBryan Schumaker 
81480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
82480c2006SBryan Schumaker {
83480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
84480c2006SBryan Schumaker 	kfree(ctx);
85480c2006SBryan Schumaker }
86480c2006SBryan Schumaker 
871da177e4SLinus Torvalds /*
881da177e4SLinus Torvalds  * Open file
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds static int
911da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
921da177e4SLinus Torvalds {
93480c2006SBryan Schumaker 	int res = 0;
94480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
95480c2006SBryan Schumaker 	struct rpc_cred *cred;
961da177e4SLinus Torvalds 
976da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
98cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
99cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
100cc0dd2d1SChuck Lever 
101cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1021e7cb3dcSChuck Lever 
103480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
104480c2006SBryan Schumaker 	if (IS_ERR(cred))
105480c2006SBryan Schumaker 		return PTR_ERR(cred);
1060c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
107480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
108480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
109480c2006SBryan Schumaker 		goto out;
110480c2006SBryan Schumaker 	}
111480c2006SBryan Schumaker 	filp->private_data = ctx;
112f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
113f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
114f5a73672SNeil Brown 		 * have been called, so we need to refresh the
115f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
116f5a73672SNeil Brown 		 */
117f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
118f5a73672SNeil Brown 	}
119480c2006SBryan Schumaker out:
120480c2006SBryan Schumaker 	put_rpccred(cred);
1211da177e4SLinus Torvalds 	return res;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124480c2006SBryan Schumaker static int
125480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
126480c2006SBryan Schumaker {
127480c2006SBryan Schumaker 	put_nfs_open_dir_context(filp->private_data);
128480c2006SBryan Schumaker 	return 0;
129480c2006SBryan Schumaker }
130480c2006SBryan Schumaker 
131d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
132d1bacf9eSBryan Schumaker 	u64 cookie;
133d1bacf9eSBryan Schumaker 	u64 ino;
134d1bacf9eSBryan Schumaker 	struct qstr string;
1350b26a0bfSTrond Myklebust 	unsigned char d_type;
136d1bacf9eSBryan Schumaker };
137d1bacf9eSBryan Schumaker 
138d1bacf9eSBryan Schumaker struct nfs_cache_array {
13988b8e133SChuck Lever 	int size;
140d1bacf9eSBryan Schumaker 	int eof_index;
141d1bacf9eSBryan Schumaker 	u64 last_cookie;
142d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
143d1bacf9eSBryan Schumaker };
144d1bacf9eSBryan Schumaker 
145573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1461da177e4SLinus Torvalds typedef struct {
1471da177e4SLinus Torvalds 	struct file	*file;
1481da177e4SLinus Torvalds 	struct page	*page;
1491da177e4SLinus Torvalds 	unsigned long	page_index;
150f0dd2136STrond Myklebust 	u64		*dir_cookie;
1510aded708STrond Myklebust 	u64		last_cookie;
152f0dd2136STrond Myklebust 	loff_t		current_index;
1531da177e4SLinus Torvalds 	decode_dirent_t	decode;
154d1bacf9eSBryan Schumaker 
1551f4eab7eSNeil Brown 	unsigned long	timestamp;
1564704f0e2STrond Myklebust 	unsigned long	gencount;
157d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
158d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
159d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1601da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1611da177e4SLinus Torvalds 
162d1bacf9eSBryan Schumaker /*
163d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1641da177e4SLinus Torvalds  */
1651da177e4SLinus Torvalds static
166d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1671da177e4SLinus Torvalds {
1688cd51a0cSTrond Myklebust 	void *ptr;
169d1bacf9eSBryan Schumaker 	if (page == NULL)
170d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1718cd51a0cSTrond Myklebust 	ptr = kmap(page);
1728cd51a0cSTrond Myklebust 	if (ptr == NULL)
1738cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1748cd51a0cSTrond Myklebust 	return ptr;
175d1bacf9eSBryan Schumaker }
176d1bacf9eSBryan Schumaker 
177d1bacf9eSBryan Schumaker static
178d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
179d1bacf9eSBryan Schumaker {
180d1bacf9eSBryan Schumaker 	kunmap(page);
181d1bacf9eSBryan Schumaker }
182d1bacf9eSBryan Schumaker 
183d1bacf9eSBryan Schumaker /*
184d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
185d1bacf9eSBryan Schumaker  */
186d1bacf9eSBryan Schumaker static
18711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
188d1bacf9eSBryan Schumaker {
18911de3b11STrond Myklebust 	struct nfs_cache_array *array;
190d1bacf9eSBryan Schumaker 	int i;
1918cd51a0cSTrond Myklebust 
1922b86ce2dSCong Wang 	array = kmap_atomic(page);
193d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
194d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1952b86ce2dSCong Wang 	kunmap_atomic(array);
196d1bacf9eSBryan Schumaker }
197d1bacf9eSBryan Schumaker 
198d1bacf9eSBryan Schumaker /*
199d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
200d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
201d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
202d1bacf9eSBryan Schumaker  */
203d1bacf9eSBryan Schumaker static
2044a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
205d1bacf9eSBryan Schumaker {
206d1bacf9eSBryan Schumaker 	string->len = len;
207d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2084a201d6eSTrond Myklebust 	if (string->name == NULL)
2094a201d6eSTrond Myklebust 		return -ENOMEM;
21004e4bd1cSCatalin Marinas 	/*
21104e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
21204e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
21304e4bd1cSCatalin Marinas 	 */
21404e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2154a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2164a201d6eSTrond Myklebust 	return 0;
217d1bacf9eSBryan Schumaker }
218d1bacf9eSBryan Schumaker 
219d1bacf9eSBryan Schumaker static
220d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
221d1bacf9eSBryan Schumaker {
222d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2234a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2244a201d6eSTrond Myklebust 	int ret;
2254a201d6eSTrond Myklebust 
226d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
227d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
228d1bacf9eSBryan Schumaker 
2294a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2303020093fSTrond Myklebust 
2313020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2323020093fSTrond Myklebust 	ret = -ENOSPC;
2333020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2343020093fSTrond Myklebust 		goto out;
2353020093fSTrond Myklebust 
2364a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2374a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2380b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2394a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2404a201d6eSTrond Myklebust 	if (ret)
2414a201d6eSTrond Myklebust 		goto out;
242d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2438cd51a0cSTrond Myklebust 	array->size++;
24447c716cbSTrond Myklebust 	if (entry->eof != 0)
245d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2464a201d6eSTrond Myklebust out:
247d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2484a201d6eSTrond Myklebust 	return ret;
249d1bacf9eSBryan Schumaker }
250d1bacf9eSBryan Schumaker 
251d1bacf9eSBryan Schumaker static
252d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
253d1bacf9eSBryan Schumaker {
254d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
255d1bacf9eSBryan Schumaker 	unsigned int index;
256d1bacf9eSBryan Schumaker 
257d1bacf9eSBryan Schumaker 	if (diff < 0)
258d1bacf9eSBryan Schumaker 		goto out_eof;
259d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2608cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
261d1bacf9eSBryan Schumaker 			goto out_eof;
262d1bacf9eSBryan Schumaker 		return -EAGAIN;
263d1bacf9eSBryan Schumaker 	}
264d1bacf9eSBryan Schumaker 
265d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
266d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
267d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
268d1bacf9eSBryan Schumaker 	return 0;
269d1bacf9eSBryan Schumaker out_eof:
270d1bacf9eSBryan Schumaker 	desc->eof = 1;
271d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
272d1bacf9eSBryan Schumaker }
273d1bacf9eSBryan Schumaker 
274d1bacf9eSBryan Schumaker static
275d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
276d1bacf9eSBryan Schumaker {
277d1bacf9eSBryan Schumaker 	int i;
2788ef2ce3eSBryan Schumaker 	loff_t new_pos;
279d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
280d1bacf9eSBryan Schumaker 
281d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2828cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
2830c030806STrond Myklebust 			struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode);
2840c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2850c030806STrond Myklebust 
2868ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2870c030806STrond Myklebust 			if (ctx->attr_gencount != nfsi->attr_gencount
2880c030806STrond Myklebust 			    || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
2890c030806STrond Myklebust 				ctx->duped = 0;
2900c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
2910c030806STrond Myklebust 			} else if (new_pos < desc->file->f_pos) {
2920c030806STrond Myklebust 				if (ctx->duped > 0
2930c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2940c030806STrond Myklebust 					if (printk_ratelimit()) {
2950c030806STrond Myklebust 						pr_notice("NFS: directory %s/%s contains a readdir loop."
2960c030806STrond Myklebust 								"Please contact your server vendor.  "
297374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
2980c030806STrond Myklebust 								desc->file->f_dentry->d_parent->d_name.name,
2990c030806STrond Myklebust 								desc->file->f_dentry->d_name.name,
300374e4e3eSBryan Schumaker 								array->array[i].string.name,
3010c030806STrond Myklebust 								*desc->dir_cookie);
3020c030806STrond Myklebust 					}
3030c030806STrond Myklebust 					status = -ELOOP;
3040c030806STrond Myklebust 					goto out;
3050c030806STrond Myklebust 				}
3068ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3070c030806STrond Myklebust 				ctx->duped = -1;
3088ef2ce3eSBryan Schumaker 			}
3098ef2ce3eSBryan Schumaker 			desc->file->f_pos = new_pos;
3108cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
31147c716cbSTrond Myklebust 			return 0;
3128cd51a0cSTrond Myklebust 		}
3138cd51a0cSTrond Myklebust 	}
31447c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
315d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
31618fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
31718fb5fe4STrond Myklebust 			desc->eof = 1;
318d1bacf9eSBryan Schumaker 	}
3190c030806STrond Myklebust out:
320d1bacf9eSBryan Schumaker 	return status;
321d1bacf9eSBryan Schumaker }
322d1bacf9eSBryan Schumaker 
323d1bacf9eSBryan Schumaker static
324d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
325d1bacf9eSBryan Schumaker {
326d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
32747c716cbSTrond Myklebust 	int status;
328d1bacf9eSBryan Schumaker 
329d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
330d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
331d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
332d1bacf9eSBryan Schumaker 		goto out;
333d1bacf9eSBryan Schumaker 	}
334d1bacf9eSBryan Schumaker 
335d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
336d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
337d1bacf9eSBryan Schumaker 	else
338d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
339d1bacf9eSBryan Schumaker 
34047c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3410aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
342e47c085aSTrond Myklebust 		desc->current_index += array->size;
34347c716cbSTrond Myklebust 		desc->page_index++;
34447c716cbSTrond Myklebust 	}
345d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
346d1bacf9eSBryan Schumaker out:
347d1bacf9eSBryan Schumaker 	return status;
348d1bacf9eSBryan Schumaker }
349d1bacf9eSBryan Schumaker 
350d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
351d1bacf9eSBryan Schumaker static
35256e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
353d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
354d1bacf9eSBryan Schumaker {
355480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
356480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3574704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3581da177e4SLinus Torvalds 	int		error;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds  again:
3611da177e4SLinus Torvalds 	timestamp = jiffies;
3624704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
36356e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3641da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3651da177e4SLinus Torvalds 	if (error < 0) {
3661da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3671da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3681da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3693a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3701da177e4SLinus Torvalds 			desc->plus = 0;
3711da177e4SLinus Torvalds 			goto again;
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 		goto error;
3741da177e4SLinus Torvalds 	}
3751f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3764704f0e2STrond Myklebust 	desc->gencount = gencount;
377d1bacf9eSBryan Schumaker error:
378d1bacf9eSBryan Schumaker 	return error;
379d1bacf9eSBryan Schumaker }
380d1bacf9eSBryan Schumaker 
381573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
382573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
383d1bacf9eSBryan Schumaker {
384573c4e1eSChuck Lever 	int error;
385d1bacf9eSBryan Schumaker 
386573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
387573c4e1eSChuck Lever 	if (error)
388573c4e1eSChuck Lever 		return error;
389d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
390d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
391d1bacf9eSBryan Schumaker 	return 0;
392d1bacf9eSBryan Schumaker }
393d1bacf9eSBryan Schumaker 
394d39ab9deSBryan Schumaker static
395d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
396d39ab9deSBryan Schumaker {
397d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
398d39ab9deSBryan Schumaker 		goto different;
39937a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
400d39ab9deSBryan Schumaker 		goto different;
401d39ab9deSBryan Schumaker 	return 1;
402d39ab9deSBryan Schumaker different:
403d39ab9deSBryan Schumaker 	return 0;
404d39ab9deSBryan Schumaker }
405d39ab9deSBryan Schumaker 
406d39ab9deSBryan Schumaker static
407d69ee9b8STrond Myklebust bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
408d69ee9b8STrond Myklebust {
409d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
410d69ee9b8STrond Myklebust 		return false;
411d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
412d69ee9b8STrond Myklebust 		return true;
413d69ee9b8STrond Myklebust 	if (filp->f_pos == 0)
414d69ee9b8STrond Myklebust 		return true;
415d69ee9b8STrond Myklebust 	return false;
416d69ee9b8STrond Myklebust }
417d69ee9b8STrond Myklebust 
418d69ee9b8STrond Myklebust /*
419d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
420d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
421d69ee9b8STrond Myklebust  * directory.
422d69ee9b8STrond Myklebust  */
423d69ee9b8STrond Myklebust static
424d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
425d69ee9b8STrond Myklebust {
426d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
427d69ee9b8STrond Myklebust }
428d69ee9b8STrond Myklebust 
429d69ee9b8STrond Myklebust static
430d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
431d39ab9deSBryan Schumaker {
43226fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4334a201d6eSTrond Myklebust 	struct dentry *dentry;
4344a201d6eSTrond Myklebust 	struct dentry *alias;
435d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
436d39ab9deSBryan Schumaker 	struct inode *inode;
437d39ab9deSBryan Schumaker 
4384a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4394a201d6eSTrond Myklebust 		if (filename.len == 1)
4404a201d6eSTrond Myklebust 			return;
4414a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4424a201d6eSTrond Myklebust 			return;
4434a201d6eSTrond Myklebust 	}
4444a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
445d39ab9deSBryan Schumaker 
4464a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
447d39ab9deSBryan Schumaker 	if (dentry != NULL) {
448d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
449d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
450d39ab9deSBryan Schumaker 			goto out;
451d39ab9deSBryan Schumaker 		} else {
452d39ab9deSBryan Schumaker 			d_drop(dentry);
453d39ab9deSBryan Schumaker 			dput(dentry);
454d39ab9deSBryan Schumaker 		}
455d39ab9deSBryan Schumaker 	}
456d39ab9deSBryan Schumaker 
457d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4584a201d6eSTrond Myklebust 	if (dentry == NULL)
4594a201d6eSTrond Myklebust 		return;
4604a201d6eSTrond Myklebust 
461d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
462d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
463d39ab9deSBryan Schumaker 		goto out;
464d39ab9deSBryan Schumaker 
465d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
466d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
467d39ab9deSBryan Schumaker 		goto out;
468d39ab9deSBryan Schumaker 	else if (alias) {
469d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
470d39ab9deSBryan Schumaker 		dput(alias);
471d39ab9deSBryan Schumaker 	} else
472d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
473d39ab9deSBryan Schumaker 
474d39ab9deSBryan Schumaker out:
475d39ab9deSBryan Schumaker 	dput(dentry);
476d39ab9deSBryan Schumaker }
477d39ab9deSBryan Schumaker 
478d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
479d1bacf9eSBryan Schumaker static
4808cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
4816650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
482d1bacf9eSBryan Schumaker {
483babddc72SBryan Schumaker 	struct xdr_stream stream;
484f7da7a12SBenny Halevy 	struct xdr_buf buf;
4856650239aSTrond Myklebust 	struct page *scratch;
48699424380SBryan Schumaker 	struct nfs_cache_array *array;
4875c346854STrond Myklebust 	unsigned int count = 0;
4885c346854STrond Myklebust 	int status;
489babddc72SBryan Schumaker 
4906650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
4916650239aSTrond Myklebust 	if (scratch == NULL)
4926650239aSTrond Myklebust 		return -ENOMEM;
493babddc72SBryan Schumaker 
494f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
4956650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
49699424380SBryan Schumaker 
49799424380SBryan Schumaker 	do {
49899424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
4998cd51a0cSTrond Myklebust 		if (status != 0) {
5008cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5018cd51a0cSTrond Myklebust 				status = 0;
50299424380SBryan Schumaker 			break;
5038cd51a0cSTrond Myklebust 		}
50499424380SBryan Schumaker 
5055c346854STrond Myklebust 		count++;
5065c346854STrond Myklebust 
50747c716cbSTrond Myklebust 		if (desc->plus != 0)
508d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5098cd51a0cSTrond Myklebust 
5108cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5118cd51a0cSTrond Myklebust 		if (status != 0)
5128cd51a0cSTrond Myklebust 			break;
51399424380SBryan Schumaker 	} while (!entry->eof);
51499424380SBryan Schumaker 
51547c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
51699424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5178cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5188cd51a0cSTrond Myklebust 			array->eof_index = array->size;
51999424380SBryan Schumaker 			status = 0;
52099424380SBryan Schumaker 			nfs_readdir_release_array(page);
5215c346854STrond Myklebust 		} else
5225c346854STrond Myklebust 			status = PTR_ERR(array);
52356e4ebf8SBryan Schumaker 	}
5246650239aSTrond Myklebust 
5256650239aSTrond Myklebust 	put_page(scratch);
5268cd51a0cSTrond Myklebust 	return status;
5278cd51a0cSTrond Myklebust }
52856e4ebf8SBryan Schumaker 
52956e4ebf8SBryan Schumaker static
53056e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
53156e4ebf8SBryan Schumaker {
53256e4ebf8SBryan Schumaker 	unsigned int i;
53356e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
53456e4ebf8SBryan Schumaker 		put_page(pages[i]);
53556e4ebf8SBryan Schumaker }
53656e4ebf8SBryan Schumaker 
53756e4ebf8SBryan Schumaker static
53856e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
53956e4ebf8SBryan Schumaker 		unsigned int npages)
54056e4ebf8SBryan Schumaker {
54156e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
54256e4ebf8SBryan Schumaker }
54356e4ebf8SBryan Schumaker 
54456e4ebf8SBryan Schumaker /*
54556e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
54656e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
54756e4ebf8SBryan Schumaker  */
54856e4ebf8SBryan Schumaker static
5496650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
55056e4ebf8SBryan Schumaker {
55156e4ebf8SBryan Schumaker 	unsigned int i;
55256e4ebf8SBryan Schumaker 
55356e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
55456e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
55556e4ebf8SBryan Schumaker 		if (page == NULL)
55656e4ebf8SBryan Schumaker 			goto out_freepages;
55756e4ebf8SBryan Schumaker 		pages[i] = page;
55856e4ebf8SBryan Schumaker 	}
5596650239aSTrond Myklebust 	return 0;
56056e4ebf8SBryan Schumaker 
56156e4ebf8SBryan Schumaker out_freepages:
56256e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
5636650239aSTrond Myklebust 	return -ENOMEM;
564d1bacf9eSBryan Schumaker }
565d1bacf9eSBryan Schumaker 
566d1bacf9eSBryan Schumaker static
567d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
568d1bacf9eSBryan Schumaker {
56956e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
57056e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
571d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
572d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
573d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5748cd51a0cSTrond Myklebust 	int status = -ENOMEM;
57556e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
576d1bacf9eSBryan Schumaker 
577d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
5780aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
579d1bacf9eSBryan Schumaker 	entry.eof = 0;
580d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
581d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
582573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
583d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
584d1bacf9eSBryan Schumaker 		goto out;
585d1bacf9eSBryan Schumaker 
586d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
5878cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
5888cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
5898cd51a0cSTrond Myklebust 		goto out;
5908cd51a0cSTrond Myklebust 	}
591d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
592d1bacf9eSBryan Schumaker 	array->eof_index = -1;
593d1bacf9eSBryan Schumaker 
5946650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
5956650239aSTrond Myklebust 	if (status < 0)
596d1bacf9eSBryan Schumaker 		goto out_release_array;
597d1bacf9eSBryan Schumaker 	do {
598ac396128STrond Myklebust 		unsigned int pglen;
59956e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
600babddc72SBryan Schumaker 
601d1bacf9eSBryan Schumaker 		if (status < 0)
602d1bacf9eSBryan Schumaker 			break;
603ac396128STrond Myklebust 		pglen = status;
6046650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6058cd51a0cSTrond Myklebust 		if (status < 0) {
6068cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6078cd51a0cSTrond Myklebust 				status = 0;
6088cd51a0cSTrond Myklebust 			break;
6098cd51a0cSTrond Myklebust 		}
6108cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
611d1bacf9eSBryan Schumaker 
61256e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
613d1bacf9eSBryan Schumaker out_release_array:
614d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
615d1bacf9eSBryan Schumaker out:
616d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
617d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
618d1bacf9eSBryan Schumaker 	return status;
619d1bacf9eSBryan Schumaker }
620d1bacf9eSBryan Schumaker 
621d1bacf9eSBryan Schumaker /*
622d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
623d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
624d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
625d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6261da177e4SLinus Torvalds  */
627d1bacf9eSBryan Schumaker static
628d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
629d1bacf9eSBryan Schumaker {
630d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
6318cd51a0cSTrond Myklebust 	int ret;
632d1bacf9eSBryan Schumaker 
6338cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6348cd51a0cSTrond Myklebust 	if (ret < 0)
635d1bacf9eSBryan Schumaker 		goto error;
636d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
637d1bacf9eSBryan Schumaker 
6382aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
639cd9ae2b6STrond Myklebust 		/* Should never happen */
640cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
641cd9ae2b6STrond Myklebust 	}
6421da177e4SLinus Torvalds 	unlock_page(page);
6431da177e4SLinus Torvalds 	return 0;
6441da177e4SLinus Torvalds  error:
6451da177e4SLinus Torvalds 	unlock_page(page);
6468cd51a0cSTrond Myklebust 	return ret;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
649d1bacf9eSBryan Schumaker static
650d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6511da177e4SLinus Torvalds {
65211de3b11STrond Myklebust 	if (!desc->page->mapping)
65311de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6541da177e4SLinus Torvalds 	page_cache_release(desc->page);
6551da177e4SLinus Torvalds 	desc->page = NULL;
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
658d1bacf9eSBryan Schumaker static
659d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6601da177e4SLinus Torvalds {
6618cd51a0cSTrond Myklebust 	return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
662d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds /*
666d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6671da177e4SLinus Torvalds  */
668d1bacf9eSBryan Schumaker static
669d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
670d1bacf9eSBryan Schumaker {
671d1bacf9eSBryan Schumaker 	int res;
672d1bacf9eSBryan Schumaker 
673d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
674d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
675d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
676d1bacf9eSBryan Schumaker 
677d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
67847c716cbSTrond Myklebust 	if (res != 0)
679d1bacf9eSBryan Schumaker 		cache_page_release(desc);
680d1bacf9eSBryan Schumaker 	return res;
681d1bacf9eSBryan Schumaker }
682d1bacf9eSBryan Schumaker 
683d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
6841da177e4SLinus Torvalds static inline
6851da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
6861da177e4SLinus Torvalds {
6878cd51a0cSTrond Myklebust 	int res;
688d1bacf9eSBryan Schumaker 
6890aded708STrond Myklebust 	if (desc->page_index == 0) {
6908cd51a0cSTrond Myklebust 		desc->current_index = 0;
6910aded708STrond Myklebust 		desc->last_cookie = 0;
6920aded708STrond Myklebust 	}
69347c716cbSTrond Myklebust 	do {
694d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
69547c716cbSTrond Myklebust 	} while (res == -EAGAIN);
6961da177e4SLinus Torvalds 	return res;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds /*
7001da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7011da177e4SLinus Torvalds  */
7021da177e4SLinus Torvalds static
7031da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
7041da177e4SLinus Torvalds 		   filldir_t filldir)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	struct file	*file = desc->file;
707d1bacf9eSBryan Schumaker 	int i = 0;
708d1bacf9eSBryan Schumaker 	int res = 0;
709d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7108ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7118ef2ce3eSBryan Schumaker 
712d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
713e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
714e7c58e97STrond Myklebust 		res = PTR_ERR(array);
715e7c58e97STrond Myklebust 		goto out;
716e7c58e97STrond Myklebust 	}
7171da177e4SLinus Torvalds 
718d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
719ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7201da177e4SLinus Torvalds 
721ece0b423STrond Myklebust 		ent = &array->array[i];
722ece0b423STrond Myklebust 		if (filldir(dirent, ent->string.name, ent->string.len,
7230b26a0bfSTrond Myklebust 		    file->f_pos, nfs_compat_user_ino64(ent->ino),
7240b26a0bfSTrond Myklebust 		    ent->d_type) < 0) {
725ece0b423STrond Myklebust 			desc->eof = 1;
7261da177e4SLinus Torvalds 			break;
727ece0b423STrond Myklebust 		}
72800a92642SOlivier Galibert 		file->f_pos++;
729d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
730d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
731d1bacf9eSBryan Schumaker 		else
732d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7330c030806STrond Myklebust 		if (ctx->duped != 0)
7340c030806STrond Myklebust 			ctx->duped = 1;
7358cd51a0cSTrond Myklebust 	}
73647c716cbSTrond Myklebust 	if (array->eof_index >= 0)
737d1bacf9eSBryan Schumaker 		desc->eof = 1;
738d1bacf9eSBryan Schumaker 
739d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
740e7c58e97STrond Myklebust out:
741d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7421e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7431e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7441da177e4SLinus Torvalds 	return res;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /*
7481da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7491da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7501da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7511da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7521da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7531da177e4SLinus Torvalds  *
7541da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7551da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7561da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7571da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7581da177e4SLinus Torvalds  */
7591da177e4SLinus Torvalds static inline
7601da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
7611da177e4SLinus Torvalds 		     filldir_t filldir)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct page	*page = NULL;
7641da177e4SLinus Torvalds 	int		status;
765d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
7660c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
7671da177e4SLinus Torvalds 
7681e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7691e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7721da177e4SLinus Torvalds 	if (!page) {
7731da177e4SLinus Torvalds 		status = -ENOMEM;
7741da177e4SLinus Torvalds 		goto out;
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 
7777a8e1dc3STrond Myklebust 	desc->page_index = 0;
7780aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
7797a8e1dc3STrond Myklebust 	desc->page = page;
7800c030806STrond Myklebust 	ctx->duped = 0;
7817a8e1dc3STrond Myklebust 
78285f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
78385f8607eSTrond Myklebust 	if (status < 0)
784d1bacf9eSBryan Schumaker 		goto out_release;
785d1bacf9eSBryan Schumaker 
7861da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds  out:
7891e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
7903110ff80SHarvey Harrison 			__func__, status);
7911da177e4SLinus Torvalds 	return status;
7921da177e4SLinus Torvalds  out_release:
793d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7941da177e4SLinus Torvalds 	goto out;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
79700a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
79800a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
79900a92642SOlivier Galibert    whole directory.
8001da177e4SLinus Torvalds  */
8011da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
8021da177e4SLinus Torvalds {
80301cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
8041da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8051da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8061da177e4SLinus Torvalds 			*desc = &my_desc;
807480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
80847c716cbSTrond Myklebust 	int res;
8091da177e4SLinus Torvalds 
8106da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8111e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8121e7cb3dcSChuck Lever 			(long long)filp->f_pos);
81391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
81491d5b470SChuck Lever 
8151da177e4SLinus Torvalds 	/*
81600a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
817f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
81800a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
81900a92642SOlivier Galibert 	 * revalidate the cookie.
8201da177e4SLinus Torvalds 	 */
8211da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	desc->file = filp;
824480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8251da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
826d69ee9b8STrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
8271da177e4SLinus Torvalds 
828565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
8291cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
830fccca7fcSTrond Myklebust 	if (res < 0)
831fccca7fcSTrond Myklebust 		goto out;
832fccca7fcSTrond Myklebust 
83347c716cbSTrond Myklebust 	do {
8341da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
83500a92642SOlivier Galibert 
8361da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
837ece0b423STrond Myklebust 			res = 0;
8381da177e4SLinus Torvalds 			/* This means either end of directory */
839d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8401da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
8411da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
842ece0b423STrond Myklebust 				if (res == 0)
8431da177e4SLinus Torvalds 					continue;
8441da177e4SLinus Torvalds 			}
8451da177e4SLinus Torvalds 			break;
8461da177e4SLinus Torvalds 		}
8471da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8483a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8491da177e4SLinus Torvalds 			nfs_zap_caches(inode);
850baf57a09STrond Myklebust 			desc->page_index = 0;
8511da177e4SLinus Torvalds 			desc->plus = 0;
852d1bacf9eSBryan Schumaker 			desc->eof = 0;
8531da177e4SLinus Torvalds 			continue;
8541da177e4SLinus Torvalds 		}
8551da177e4SLinus Torvalds 		if (res < 0)
8561da177e4SLinus Torvalds 			break;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
859ece0b423STrond Myklebust 		if (res < 0)
8601da177e4SLinus Torvalds 			break;
86147c716cbSTrond Myklebust 	} while (!desc->eof);
862fccca7fcSTrond Myklebust out:
863565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8641e7cb3dcSChuck Lever 	if (res > 0)
8651e7cb3dcSChuck Lever 		res = 0;
866aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8671e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8681e7cb3dcSChuck Lever 			res);
8691da177e4SLinus Torvalds 	return res;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
87210afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
873f0dd2136STrond Myklebust {
874b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
875b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
876480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
877b84e06c5SChuck Lever 
8786da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
879b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
880b84e06c5SChuck Lever 			dentry->d_name.name,
881b84e06c5SChuck Lever 			offset, origin);
882b84e06c5SChuck Lever 
883b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
884f0dd2136STrond Myklebust 	switch (origin) {
885f0dd2136STrond Myklebust 		case 1:
886f0dd2136STrond Myklebust 			offset += filp->f_pos;
887f0dd2136STrond Myklebust 		case 0:
888f0dd2136STrond Myklebust 			if (offset >= 0)
889f0dd2136STrond Myklebust 				break;
890f0dd2136STrond Myklebust 		default:
891f0dd2136STrond Myklebust 			offset = -EINVAL;
892f0dd2136STrond Myklebust 			goto out;
893f0dd2136STrond Myklebust 	}
894f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
895f0dd2136STrond Myklebust 		filp->f_pos = offset;
896480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
8978ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
898f0dd2136STrond Myklebust 	}
899f0dd2136STrond Myklebust out:
900b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
901f0dd2136STrond Myklebust 	return offset;
902f0dd2136STrond Myklebust }
903f0dd2136STrond Myklebust 
9041da177e4SLinus Torvalds /*
9051da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9061da177e4SLinus Torvalds  * is a dummy operation.
9071da177e4SLinus Torvalds  */
90802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
90902c24a82SJosef Bacik 			 int datasync)
9101da177e4SLinus Torvalds {
9117ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
91202c24a82SJosef Bacik 	struct inode *inode = dentry->d_inode;
9137ea80859SChristoph Hellwig 
9146da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9151e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9161e7cb3dcSChuck Lever 			datasync);
9171e7cb3dcSChuck Lever 
91802c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
91954917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
92002c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924bfc69a45STrond Myklebust /**
925bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
926bfc69a45STrond Myklebust  * @dir - pointer to directory inode
927bfc69a45STrond Myklebust  *
928bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
929bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
930bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
931bfc69a45STrond Myklebust  *
932bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
933bfc69a45STrond Myklebust  */
934bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
935bfc69a45STrond Myklebust {
936011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
937bfc69a45STrond Myklebust }
938bfc69a45STrond Myklebust 
9391da177e4SLinus Torvalds /*
9401da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9411da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9421da177e4SLinus Torvalds  * and may need to be looked up again.
9431da177e4SLinus Torvalds  */
944c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9471da177e4SLinus Torvalds 		return 1;
9484eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9494eec952eSTrond Myklebust 		return 0;
950f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9516ecc5e8fSTrond Myklebust 		return 0;
952f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
953f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
954f2c77f4eSTrond Myklebust 		return 0;
955f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
956f2c77f4eSTrond Myklebust 		return 0;
957f2c77f4eSTrond Myklebust 	return 1;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds /*
961a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
962a12802caSTrond Myklebust  * an O_EXCL create using this path component.
963a12802caSTrond Myklebust  */
964fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
965a12802caSTrond Myklebust {
966a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
967a12802caSTrond Myklebust 		return 0;
968fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
969a12802caSTrond Myklebust }
970a12802caSTrond Myklebust 
971a12802caSTrond Myklebust /*
9721d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9731d6757fbSTrond Myklebust  *
9741d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9751d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9761d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9771d6757fbSTrond Myklebust  *
9781d6757fbSTrond Myklebust  */
9791da177e4SLinus Torvalds static inline
980fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
9831da177e4SLinus Torvalds 
98436d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
9854e99a1ffSTrond Myklebust 		return 0;
9861da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
987fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
9881da177e4SLinus Torvalds 		goto out_force;
9891da177e4SLinus Torvalds 	/* This is an open(2) */
990fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
991fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
9921da177e4SLinus Torvalds 		goto out_force;
9934f48af45STrond Myklebust 	return 0;
9941da177e4SLinus Torvalds out_force:
9951da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds /*
9991da177e4SLinus Torvalds  * We judge how long we want to trust negative
10001da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10011da177e4SLinus Torvalds  *
10021da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10031da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10041da177e4SLinus Torvalds  */
10051da177e4SLinus Torvalds static inline
10061da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1007fa3c56bbSAl Viro 		       unsigned int flags)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1010fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10111da177e4SLinus Torvalds 		return 0;
10124eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10134eec952eSTrond Myklebust 		return 1;
10141da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds /*
10181da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10191da177e4SLinus Torvalds  * and we should check whether we can really trust that
10201da177e4SLinus Torvalds  * lookup.
10211da177e4SLinus Torvalds  *
10221da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10231da177e4SLinus Torvalds  * we have an inode!
10241da177e4SLinus Torvalds  *
10251da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10261da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10271da177e4SLinus Torvalds  */
10280b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	struct inode *dir;
10311da177e4SLinus Torvalds 	struct inode *inode;
10321da177e4SLinus Torvalds 	struct dentry *parent;
1033e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1034e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10351da177e4SLinus Torvalds 	int error;
10361da177e4SLinus Torvalds 
1037fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
103834286d66SNick Piggin 		return -ECHILD;
103934286d66SNick Piggin 
10401da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10411da177e4SLinus Torvalds 	dir = parent->d_inode;
104291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10431da177e4SLinus Torvalds 	inode = dentry->d_inode;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	if (!inode) {
1046fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
10471da177e4SLinus Torvalds 			goto out_bad;
1048d69ee9b8STrond Myklebust 		goto out_valid_noent;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10521e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10533110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10541e7cb3dcSChuck Lever 				dentry->d_name.name);
10551da177e4SLinus Torvalds 		goto out_bad;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 
1058011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
105915860ab1STrond Myklebust 		goto out_set_verifier;
106015860ab1STrond Myklebust 
10611da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1062fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1063fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
10641da177e4SLinus Torvalds 			goto out_zap_parent;
10651da177e4SLinus Torvalds 		goto out_valid;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10691da177e4SLinus Torvalds 		goto out_bad;
10701da177e4SLinus Torvalds 
1071e1fb4d05STrond Myklebust 	error = -ENOMEM;
1072e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1073e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1074e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1075e1fb4d05STrond Myklebust 		goto out_error;
1076e1fb4d05STrond Myklebust 
107780a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
10781da177e4SLinus Torvalds 	if (error)
10791da177e4SLinus Torvalds 		goto out_bad;
1080e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
10811da177e4SLinus Torvalds 		goto out_bad;
1082e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
10831da177e4SLinus Torvalds 		goto out_bad;
10841da177e4SLinus Torvalds 
1085e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1086e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
108715860ab1STrond Myklebust out_set_verifier:
1088cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10891da177e4SLinus Torvalds  out_valid:
1090d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1091d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1092d69ee9b8STrond Myklebust  out_valid_noent:
10931da177e4SLinus Torvalds 	dput(parent);
10941e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
10953110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
10961e7cb3dcSChuck Lever 			dentry->d_name.name);
10971da177e4SLinus Torvalds 	return 1;
10981da177e4SLinus Torvalds out_zap_parent:
10991da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11001da177e4SLinus Torvalds  out_bad:
1101a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11021da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11031da177e4SLinus Torvalds 		/* Purge readdir caches. */
11041da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11051da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11061da177e4SLinus Torvalds 		if (have_submounts(dentry))
11071da177e4SLinus Torvalds 			goto out_valid;
1108d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1109d9e80b7dSAl Viro 			goto out_valid;
11101da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 	d_drop(dentry);
1113e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1114e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
11151da177e4SLinus Torvalds 	dput(parent);
11161e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11173110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11181e7cb3dcSChuck Lever 			dentry->d_name.name);
11191da177e4SLinus Torvalds 	return 0;
1120e1fb4d05STrond Myklebust out_error:
1121e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1122e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1123e1fb4d05STrond Myklebust 	dput(parent);
1124e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1125e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1126e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1127e1fb4d05STrond Myklebust 	return error;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds /*
11311da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11321da177e4SLinus Torvalds  */
1133fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
11341da177e4SLinus Torvalds {
11351da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
11361da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
11371da177e4SLinus Torvalds 		dentry->d_flags);
11381da177e4SLinus Torvalds 
113977f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
114077f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
114177f11192STrond Myklebust 		return 1;
114277f11192STrond Myklebust 
11431da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11441da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
11451da177e4SLinus Torvalds 		return 1;
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
11481da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
11491da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
11501da177e4SLinus Torvalds 		return 1;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	return 0;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
11571b83d707STrond Myklebust {
11581b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
11591b83d707STrond Myklebust 	if (inode->i_nlink > 0)
11601b83d707STrond Myklebust 		drop_nlink(inode);
11611b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
11621b83d707STrond Myklebust }
11631b83d707STrond Myklebust 
11641da177e4SLinus Torvalds /*
11651da177e4SLinus Torvalds  * Called when the dentry loses inode.
11661da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
11671da177e4SLinus Torvalds  */
11681da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
11691da177e4SLinus Torvalds {
117083672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
117183672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
117283672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
117383672d39SNeil Brown 
11741da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11759a53c3a7SDave Hansen 		drop_nlink(inode);
1176e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds 	iput(inode);
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
1181b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1182b1942c5fSAl Viro {
1183b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1184b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1185b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1186b1942c5fSAl Viro 			WARN_ON(1);
1187b1942c5fSAl Viro 		else
1188b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1189b1942c5fSAl Viro 	}
1190b1942c5fSAl Viro }
1191b1942c5fSAl Viro 
1192f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
11931da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
11941da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
11951da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
119636d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1197b1942c5fSAl Viro 	.d_release	= nfs_d_release,
11981da177e4SLinus Torvalds };
11991da177e4SLinus Torvalds 
1200597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
12011da177e4SLinus Torvalds {
12021da177e4SLinus Torvalds 	struct dentry *res;
1203565277f6STrond Myklebust 	struct dentry *parent;
12041da177e4SLinus Torvalds 	struct inode *inode = NULL;
1205e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1206e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12071da177e4SLinus Torvalds 	int error;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
12101da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
121191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12141da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12151da177e4SLinus Torvalds 		goto out;
12161da177e4SLinus Torvalds 
1217fd684071STrond Myklebust 	/*
1218fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1219fd684071STrond Myklebust 	 * but don't hash the dentry.
1220fd684071STrond Myklebust 	 */
122100cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1222fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1223fd684071STrond Myklebust 		res = NULL;
1224fc0f684cSTrond Myklebust 		goto out;
1225fd684071STrond Myklebust 	}
12261da177e4SLinus Torvalds 
1227e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1228e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1229e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1230e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1231e1fb4d05STrond Myklebust 		goto out;
1232e1fb4d05STrond Myklebust 
1233565277f6STrond Myklebust 	parent = dentry->d_parent;
1234565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1235565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
123680a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
12371da177e4SLinus Torvalds 	if (error == -ENOENT)
12381da177e4SLinus Torvalds 		goto no_entry;
12391da177e4SLinus Torvalds 	if (error < 0) {
12401da177e4SLinus Torvalds 		res = ERR_PTR(error);
1241565277f6STrond Myklebust 		goto out_unblock_sillyrename;
12421da177e4SLinus Torvalds 	}
1243e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1244bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
124503f28e3aSTrond Myklebust 	if (IS_ERR(res))
1246565277f6STrond Myklebust 		goto out_unblock_sillyrename;
124754ceac45SDavid Howells 
1248d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1249d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1250d69ee9b8STrond Myklebust 
12511da177e4SLinus Torvalds no_entry:
125254ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
12539eaef27bSTrond Myklebust 	if (res != NULL) {
12549eaef27bSTrond Myklebust 		if (IS_ERR(res))
1255565277f6STrond Myklebust 			goto out_unblock_sillyrename;
12561da177e4SLinus Torvalds 		dentry = res;
12579eaef27bSTrond Myklebust 	}
12581da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1259565277f6STrond Myklebust out_unblock_sillyrename:
1260565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
12611da177e4SLinus Torvalds out:
1262e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1263e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
12641da177e4SLinus Torvalds 	return res;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
12680b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
12691da177e4SLinus Torvalds 
1270f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
12710ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_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 };
12771da177e4SLinus Torvalds 
12788a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
12798a5e929dSAl Viro {
12808a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
12818a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
12828a5e929dSAl Viro 		res |= FMODE_READ;
12838a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
12848a5e929dSAl Viro 		res |= FMODE_WRITE;
12858a5e929dSAl Viro 	return res;
12868a5e929dSAl Viro }
12878a5e929dSAl Viro 
128851141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1289cd9a1c0eSTrond Myklebust {
12905ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1291cd9a1c0eSTrond Myklebust }
1292cd9a1c0eSTrond Myklebust 
1293cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1294cd9a1c0eSTrond Myklebust {
1295cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1296cd9a1c0eSTrond Myklebust 	return 0;
1297cd9a1c0eSTrond Myklebust }
1298cd9a1c0eSTrond Myklebust 
1299d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13000dd2b474SMiklos Szeredi 			   struct dentry *dentry,
130130d90494SAl Viro 			   struct file *file, unsigned open_flags,
130247237687SAl Viro 			   int *opened)
1303cd9a1c0eSTrond Myklebust {
13040dd2b474SMiklos Szeredi 	int err;
13050dd2b474SMiklos Szeredi 
13060dd2b474SMiklos Szeredi 	if (ctx->dentry != dentry) {
13070dd2b474SMiklos Szeredi 		dput(ctx->dentry);
13080dd2b474SMiklos Szeredi 		ctx->dentry = dget(dentry);
13090dd2b474SMiklos Szeredi 	}
1310cd9a1c0eSTrond Myklebust 
1311cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1312cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
13130dd2b474SMiklos Szeredi 		err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1314d9585277SAl Viro 		if (err < 0)
1315cd9a1c0eSTrond Myklebust 			goto out;
1316cd9a1c0eSTrond Myklebust 	}
13170dd2b474SMiklos Szeredi 
131830d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
131930d90494SAl Viro 	if (err)
1320d9585277SAl Viro 		goto out;
132130d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
13220dd2b474SMiklos Szeredi 
1323cd9a1c0eSTrond Myklebust out:
1324cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1325d9585277SAl Viro 	return err;
1326cd9a1c0eSTrond Myklebust }
1327cd9a1c0eSTrond Myklebust 
1328*73a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
132930d90494SAl Viro 		    struct file *file, unsigned open_flags,
133047237687SAl Viro 		    umode_t mode, int *opened)
13311da177e4SLinus Torvalds {
1332cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
13330dd2b474SMiklos Szeredi 	struct dentry *res;
13340dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1335f46e0bd3STrond Myklebust 	struct inode *inode;
1336898f635cSTrond Myklebust 	int err;
13371da177e4SLinus Torvalds 
13380dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
13390dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
13400dd2b474SMiklos Szeredi 
13410dd2b474SMiklos Szeredi 	dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
13421e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13431e7cb3dcSChuck Lever 
13440dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
13450dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
13460dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
13470dd2b474SMiklos Szeredi 			/*
13480dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
13490dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
13500dd2b474SMiklos Szeredi 			 * again
13510dd2b474SMiklos Szeredi 			 */
1352d9585277SAl Viro 			return -ENOENT;
13530dd2b474SMiklos Szeredi 		}
13541da177e4SLinus Torvalds 		goto no_open;
13551da177e4SLinus Torvalds 	}
13561da177e4SLinus Torvalds 
13570dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1358d9585277SAl Viro 		return -ENAMETOOLONG;
13591da177e4SLinus Torvalds 
13600dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1361536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
13620dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
13630dd2b474SMiklos Szeredi 	}
1364536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1365536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1366536e43d1STrond Myklebust 		attr.ia_size = 0;
1367cd9a1c0eSTrond Myklebust 	}
1368cd9a1c0eSTrond Myklebust 
13690dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
13700dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
13710dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1372d9585277SAl Viro 		goto out;
13730dd2b474SMiklos Szeredi 
1374f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
13752b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
13760dd2b474SMiklos Szeredi 	d_drop(dentry);
1377f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1378f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1379cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
13800dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
13810dd2b474SMiklos Szeredi 		switch (err) {
13821da177e4SLinus Torvalds 		case -ENOENT:
1383f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
13840dd2b474SMiklos Szeredi 			break;
13851788ea6eSJeff Layton 		case -EISDIR:
13866f926b5bSTrond Myklebust 		case -ENOTDIR:
13876f926b5bSTrond Myklebust 			goto no_open;
13881da177e4SLinus Torvalds 		case -ELOOP:
13890dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
13901da177e4SLinus Torvalds 				goto no_open;
13910dd2b474SMiklos Szeredi 			break;
13921da177e4SLinus Torvalds 			/* case -EINVAL: */
13931da177e4SLinus Torvalds 		default:
13940dd2b474SMiklos Szeredi 			break;
13951da177e4SLinus Torvalds 		}
13961da177e4SLinus Torvalds 		goto out;
13971da177e4SLinus Torvalds 	}
1398f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1399898f635cSTrond Myklebust 	if (res != NULL)
14000dd2b474SMiklos Szeredi 		dentry = res;
14010dd2b474SMiklos Szeredi 
14020dd2b474SMiklos Szeredi 	nfs_unblock_sillyrename(dentry->d_parent);
1403f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14040dd2b474SMiklos Szeredi 
140530d90494SAl Viro 	err = nfs_finish_open(ctx, dentry, file, open_flags, opened);
14060dd2b474SMiklos Szeredi 
14070dd2b474SMiklos Szeredi 	dput(res);
1408d9585277SAl Viro out:
1409d9585277SAl Viro 	return err;
14100dd2b474SMiklos Szeredi 
14111da177e4SLinus Torvalds no_open:
141200cd8dd3SAl Viro 	res = nfs_lookup(dir, dentry, 0);
14130dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
14140dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1415d9585277SAl Viro 		goto out;
14160dd2b474SMiklos Szeredi 
1417e45198a6SAl Viro 	return finish_no_open(file, res);
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
14200b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1423657e94b6SNick Piggin 	struct inode *inode;
14241da177e4SLinus Torvalds 	struct inode *dir;
142550de348cSMiklos Szeredi 	int ret = 0;
14261da177e4SLinus Torvalds 
1427fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1428657e94b6SNick Piggin 		return -ECHILD;
1429657e94b6SNick Piggin 
1430fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1431eda72afbSMiklos Szeredi 		goto no_open;
1432eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
14335584c306STrond Myklebust 		goto no_open;
14342b484297STrond Myklebust 
1435eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
14361da177e4SLinus Torvalds 	parent = dget_parent(dentry);
14371da177e4SLinus Torvalds 	dir = parent->d_inode;
14382b484297STrond Myklebust 
14391da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
14401da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
14411da177e4SLinus Torvalds 	 */
1442216d5d06STrond Myklebust 	if (inode == NULL) {
1443fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1444216d5d06STrond Myklebust 			ret = 1;
14451da177e4SLinus Torvalds 		goto out;
1446216d5d06STrond Myklebust 	}
1447216d5d06STrond Myklebust 
14481da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
14491da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
14505584c306STrond Myklebust 		goto no_open_dput;
14511da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1452fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
14535584c306STrond Myklebust 		goto no_open_dput;
14541da177e4SLinus Torvalds 
14550ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1456898f635cSTrond Myklebust 	ret = 1;
14570ef97dcfSMiklos Szeredi 
14581da177e4SLinus Torvalds out:
14591da177e4SLinus Torvalds 	dput(parent);
14601da177e4SLinus Torvalds 	return ret;
1461535918f1STrond Myklebust 
14625584c306STrond Myklebust no_open_dput:
14631da177e4SLinus Torvalds 	dput(parent);
14645584c306STrond Myklebust no_open:
14650b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1466c0204fd2STrond Myklebust }
1467c0204fd2STrond Myklebust 
14681da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds /*
14711da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
14721da177e4SLinus Torvalds  */
14731da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
14741da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
14751da177e4SLinus Torvalds {
1476fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1477fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
14781da177e4SLinus Torvalds 	struct inode *inode;
14791da177e4SLinus Torvalds 	int error = -EACCES;
14801da177e4SLinus Torvalds 
1481fab728e1STrond Myklebust 	d_drop(dentry);
1482fab728e1STrond Myklebust 
14831da177e4SLinus Torvalds 	/* We may have been initialized further down */
14841da177e4SLinus Torvalds 	if (dentry->d_inode)
1485fab728e1STrond Myklebust 		goto out;
14861da177e4SLinus Torvalds 	if (fhandle->size == 0) {
148780a16b21SBryan Schumaker 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
14881da177e4SLinus Torvalds 		if (error)
1489fab728e1STrond Myklebust 			goto out_error;
14901da177e4SLinus Torvalds 	}
14915724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14921da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
14931da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
14948fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
14951da177e4SLinus Torvalds 		if (error < 0)
1496fab728e1STrond Myklebust 			goto out_error;
14971da177e4SLinus Torvalds 	}
14981da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
149903f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
150003f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1501fab728e1STrond Myklebust 		goto out_error;
1502fab728e1STrond Myklebust 	d_add(dentry, inode);
1503fab728e1STrond Myklebust out:
1504fab728e1STrond Myklebust 	dput(parent);
15051da177e4SLinus Torvalds 	return 0;
1506fab728e1STrond Myklebust out_error:
1507fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1508fab728e1STrond Myklebust 	dput(parent);
1509fab728e1STrond Myklebust 	return error;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds /*
15131da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15141da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15151da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15161da177e4SLinus Torvalds  * reply path made it appear to have failed.
15171da177e4SLinus Torvalds  */
1518597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1519ebfc3b49SAl Viro 		umode_t mode, bool excl)
15201da177e4SLinus Torvalds {
15211da177e4SLinus Torvalds 	struct iattr attr;
1522ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
15231da177e4SLinus Torvalds 	int error;
15241da177e4SLinus Torvalds 
15251e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
15261e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	attr.ia_mode = mode;
15291da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15301da177e4SLinus Torvalds 
15318867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
15321da177e4SLinus Torvalds 	if (error != 0)
15331da177e4SLinus Torvalds 		goto out_err;
15341da177e4SLinus Torvalds 	return 0;
15351da177e4SLinus Torvalds out_err:
15361da177e4SLinus Torvalds 	d_drop(dentry);
15371da177e4SLinus Torvalds 	return error;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds /*
15411da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15421da177e4SLinus Torvalds  */
1543597d9289SBryan Schumaker int
15441a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	struct iattr attr;
15471da177e4SLinus Torvalds 	int status;
15481da177e4SLinus Torvalds 
15491e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
15501e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
15531da177e4SLinus Torvalds 		return -EINVAL;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	attr.ia_mode = mode;
15561da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
15591da177e4SLinus Torvalds 	if (status != 0)
15601da177e4SLinus Torvalds 		goto out_err;
15611da177e4SLinus Torvalds 	return 0;
15621da177e4SLinus Torvalds out_err:
15631da177e4SLinus Torvalds 	d_drop(dentry);
15641da177e4SLinus Torvalds 	return status;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds /*
15681da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15691da177e4SLinus Torvalds  */
1570597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	struct iattr attr;
15731da177e4SLinus Torvalds 	int error;
15741da177e4SLinus Torvalds 
15751e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
15761e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15791da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
15821da177e4SLinus Torvalds 	if (error != 0)
15831da177e4SLinus Torvalds 		goto out_err;
15841da177e4SLinus Torvalds 	return 0;
15851da177e4SLinus Torvalds out_err:
15861da177e4SLinus Torvalds 	d_drop(dentry);
15871da177e4SLinus Torvalds 	return error;
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds 
1590d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1591d45b9d8bSTrond Myklebust {
1592d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1593d45b9d8bSTrond Myklebust 		d_delete(dentry);
1594d45b9d8bSTrond Myklebust }
1595d45b9d8bSTrond Myklebust 
1596597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	int error;
15991da177e4SLinus Torvalds 
16001e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16011e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16041da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
16051da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1606ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1607d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1608d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	return error;
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds /*
16141da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
16151da177e4SLinus Torvalds  * and after checking that the file has only one user.
16161da177e4SLinus Torvalds  *
16171da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
16181da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
16191da177e4SLinus Torvalds  */
16201da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
16211da177e4SLinus Torvalds {
16221da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
16231da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16241da177e4SLinus Torvalds 	int error = -EBUSY;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
16271da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
16301da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16311da177e4SLinus Torvalds 		error = 0;
16321da177e4SLinus Torvalds 		goto out;
16331da177e4SLinus Torvalds 	}
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	if (inode != NULL) {
163657ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
16371da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
16381da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
16391da177e4SLinus Torvalds 		if (error == 0)
16401b83d707STrond Myklebust 			nfs_drop_nlink(inode);
16415ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
16421da177e4SLinus Torvalds 	} else
16431da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1644d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1645d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16461da177e4SLinus Torvalds out:
16471da177e4SLinus Torvalds 	return error;
16481da177e4SLinus Torvalds }
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
16511da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
16521da177e4SLinus Torvalds  *
16531da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
16541da177e4SLinus Torvalds  */
1655597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
16561da177e4SLinus Torvalds {
16571da177e4SLinus Torvalds 	int error;
16581da177e4SLinus Torvalds 	int need_rehash = 0;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
16611da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
1664b7ab39f6SNick Piggin 	if (dentry->d_count > 1) {
16651da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1666ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1667ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
16681da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
16691da177e4SLinus Torvalds 		return error;
16701da177e4SLinus Torvalds 	}
16711da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
16721da177e4SLinus Torvalds 		__d_drop(dentry);
16731da177e4SLinus Torvalds 		need_rehash = 1;
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
16761da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1677d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
16781da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16791da177e4SLinus Torvalds 	} else if (need_rehash)
16801da177e4SLinus Torvalds 		d_rehash(dentry);
16811da177e4SLinus Torvalds 	return error;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
1684873101b3SChuck Lever /*
1685873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1686873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1687873101b3SChuck Lever  * using prepare_write/commit_write.
1688873101b3SChuck Lever  *
1689873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1690873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1691873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1692873101b3SChuck Lever  * symlink request has completed on the server.
1693873101b3SChuck Lever  *
1694873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1695873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1696873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1697873101b3SChuck Lever  * and move the raw page into its mapping.
1698873101b3SChuck Lever  */
1699597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17001da177e4SLinus Torvalds {
1701873101b3SChuck Lever 	struct pagevec lru_pvec;
1702873101b3SChuck Lever 	struct page *page;
1703873101b3SChuck Lever 	char *kaddr;
17041da177e4SLinus Torvalds 	struct iattr attr;
1705873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17061da177e4SLinus Torvalds 	int error;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
17091da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
17101da177e4SLinus Torvalds 
1711873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1712873101b3SChuck Lever 		return -ENAMETOOLONG;
17131da177e4SLinus Torvalds 
1714873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1715873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
17161da177e4SLinus Torvalds 
171783d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
171876566991STrond Myklebust 	if (!page)
1719873101b3SChuck Lever 		return -ENOMEM;
1720873101b3SChuck Lever 
17212b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1722873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1723873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1724873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
17252b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1726873101b3SChuck Lever 
172794a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1728873101b3SChuck Lever 	if (error != 0) {
1729873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1730873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1731873101b3SChuck Lever 			dentry->d_name.name, symname, error);
17321da177e4SLinus Torvalds 		d_drop(dentry);
1733873101b3SChuck Lever 		__free_page(page);
17341da177e4SLinus Torvalds 		return error;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 
1737873101b3SChuck Lever 	/*
1738873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1739873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1740873101b3SChuck Lever 	 */
1741873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1742873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1743873101b3SChuck Lever 							GFP_KERNEL)) {
174439cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
17454f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1746873101b3SChuck Lever 		SetPageUptodate(page);
1747873101b3SChuck Lever 		unlock_page(page);
1748873101b3SChuck Lever 	} else
1749873101b3SChuck Lever 		__free_page(page);
1750873101b3SChuck Lever 
1751873101b3SChuck Lever 	return 0;
1752873101b3SChuck Lever }
1753873101b3SChuck Lever 
1754597d9289SBryan Schumaker int
17551da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17581da177e4SLinus Torvalds 	int error;
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
17611da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
17621da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17631da177e4SLinus Torvalds 
176457ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
17659a3936aaSTrond Myklebust 
17669697d234STrond Myklebust 	d_drop(dentry);
17671da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1768cf809556STrond Myklebust 	if (error == 0) {
17697de9c6eeSAl Viro 		ihold(inode);
17709697d234STrond Myklebust 		d_add(dentry, inode);
1771cf809556STrond Myklebust 	}
17721da177e4SLinus Torvalds 	return error;
17731da177e4SLinus Torvalds }
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds /*
17761da177e4SLinus Torvalds  * RENAME
17771da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
17781da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
17791da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
17801da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
17811da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
17821da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
17831da177e4SLinus Torvalds  *
17841da177e4SLinus Torvalds  * FIXED.
17851da177e4SLinus Torvalds  *
17861da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
17871da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
17881da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
17891da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
17901da177e4SLinus Torvalds  * using the inode layer
17911da177e4SLinus Torvalds  *
17921da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
17931da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
17941da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
17951da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
17961da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
17971da177e4SLinus Torvalds  * the rename.
17981da177e4SLinus Torvalds  */
1799597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18001da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18011da177e4SLinus Torvalds {
18021da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18031da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18041da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
18051da177e4SLinus Torvalds 	int error = -EBUSY;
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
18081da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18091da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1810b7ab39f6SNick Piggin 		 new_dentry->d_count);
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	/*
181328f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
181428f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
181528f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
181628f79a1aSMiklos Szeredi 	 * the new target.
18171da177e4SLinus Torvalds 	 */
181827226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
181927226104SMiklos Szeredi 		/*
182027226104SMiklos Szeredi 		 * To prevent any new references to the target during the
182127226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
182227226104SMiklos Szeredi 		 */
182327226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
182427226104SMiklos Szeredi 			d_drop(new_dentry);
182527226104SMiklos Szeredi 			rehash = new_dentry;
182627226104SMiklos Szeredi 		}
182727226104SMiklos Szeredi 
1828b7ab39f6SNick Piggin 		if (new_dentry->d_count > 2) {
18291da177e4SLinus Torvalds 			int err;
183027226104SMiklos Szeredi 
18311da177e4SLinus Torvalds 			/* copy the target dentry's name */
18321da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
18331da177e4SLinus Torvalds 					 &new_dentry->d_name);
18341da177e4SLinus Torvalds 			if (!dentry)
18351da177e4SLinus Torvalds 				goto out;
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
18381da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
183924e93025SMiklos Szeredi 			if (err)
18401da177e4SLinus Torvalds 				goto out;
184124e93025SMiklos Szeredi 
184224e93025SMiklos Szeredi 			new_dentry = dentry;
184356335936SOGAWA Hirofumi 			rehash = NULL;
184424e93025SMiklos Szeredi 			new_inode = NULL;
1845b1e4adf4STrond Myklebust 		}
184627226104SMiklos Szeredi 	}
18471da177e4SLinus Torvalds 
184857ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1849b1e4adf4STrond Myklebust 	if (new_inode != NULL)
185057ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
18531da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
18545ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
18551da177e4SLinus Torvalds out:
18561da177e4SLinus Torvalds 	if (rehash)
18571da177e4SLinus Torvalds 		d_rehash(rehash);
18581da177e4SLinus Torvalds 	if (!error) {
1859b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1860b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
18611da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
18628fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
18638fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1864d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1865d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	/* new dentry created? */
18681da177e4SLinus Torvalds 	if (dentry)
18691da177e4SLinus Torvalds 		dput(dentry);
18701da177e4SLinus Torvalds 	return error;
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
1873cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1874cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1875cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1876cfcea3e8STrond Myklebust 
18771c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
18781c3c07e9STrond Myklebust {
18791c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
18801c3c07e9STrond Myklebust 	kfree(entry);
1881cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1882cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1883cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
18841c3c07e9STrond Myklebust }
18851c3c07e9STrond Myklebust 
18861a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
18871a81bb8aSTrond Myklebust {
18881a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
18891a81bb8aSTrond Myklebust 
18901a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
18911a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
18921a81bb8aSTrond Myklebust 		list_del(&cache->lru);
18931a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
18941a81bb8aSTrond Myklebust 	}
18951a81bb8aSTrond Myklebust }
18961a81bb8aSTrond Myklebust 
18971495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink,
18981495f230SYing Han 			      struct shrink_control *sc)
1899979df72eSTrond Myklebust {
1900979df72eSTrond Myklebust 	LIST_HEAD(head);
1901aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1902979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
19031495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
19041495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
1905979df72eSTrond Myklebust 
190661d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
190761d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
19089c7e7e23STrond Myklebust 
1909a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1910aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1911979df72eSTrond Myklebust 		struct inode *inode;
1912979df72eSTrond Myklebust 
1913979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1914979df72eSTrond Myklebust 			break;
19159c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
1916979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1917979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1918979df72eSTrond Myklebust 			goto remove_lru_entry;
1919979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1920979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1921979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1922979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1923979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1924979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1925979df72eSTrond Myklebust 					&nfs_access_lru_list);
1926979df72eSTrond Myklebust 		else {
1927979df72eSTrond Myklebust remove_lru_entry:
1928979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
19299c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
1930979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
19319c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
1932979df72eSTrond Myklebust 		}
193359844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
1934979df72eSTrond Myklebust 	}
1935979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19361a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
1937979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1938979df72eSTrond Myklebust }
1939979df72eSTrond Myklebust 
19401a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
19411c3c07e9STrond Myklebust {
19421c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
19431a81bb8aSTrond Myklebust 	struct rb_node *n;
19441c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19451c3c07e9STrond Myklebust 
19461c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
19471c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
19481c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19491c3c07e9STrond Myklebust 		rb_erase(n, root_node);
19501a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
19511c3c07e9STrond Myklebust 	}
19521c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
19531c3c07e9STrond Myklebust }
19541c3c07e9STrond Myklebust 
19551c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
19561c3c07e9STrond Myklebust {
19571a81bb8aSTrond Myklebust 	LIST_HEAD(head);
19581a81bb8aSTrond Myklebust 
19591a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
19601a81bb8aSTrond Myklebust 		return;
1961cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
1962cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
19631a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
1964cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1965cfcea3e8STrond Myklebust 
19661c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
19671a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
19681a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
19691a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19701a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
19711c3c07e9STrond Myklebust }
19721c3c07e9STrond Myklebust 
19731c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
19741c3c07e9STrond Myklebust {
19751c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
19761c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19771c3c07e9STrond Myklebust 
19781c3c07e9STrond Myklebust 	while (n != NULL) {
19791c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19801c3c07e9STrond Myklebust 
19811c3c07e9STrond Myklebust 		if (cred < entry->cred)
19821c3c07e9STrond Myklebust 			n = n->rb_left;
19831c3c07e9STrond Myklebust 		else if (cred > entry->cred)
19841c3c07e9STrond Myklebust 			n = n->rb_right;
19851c3c07e9STrond Myklebust 		else
19861c3c07e9STrond Myklebust 			return entry;
19871c3c07e9STrond Myklebust 	}
19881c3c07e9STrond Myklebust 	return NULL;
19891c3c07e9STrond Myklebust }
19901c3c07e9STrond Myklebust 
1991af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
19921da177e4SLinus Torvalds {
199355296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
19941c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
19951c3c07e9STrond Myklebust 	int err = -ENOENT;
19961da177e4SLinus Torvalds 
19971c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
19981c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
19991c3c07e9STrond Myklebust 		goto out_zap;
20001c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20011c3c07e9STrond Myklebust 	if (cache == NULL)
20021c3c07e9STrond Myklebust 		goto out;
2003b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
200464672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
20051c3c07e9STrond Myklebust 		goto out_stale;
20061c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
20071c3c07e9STrond Myklebust 	res->cred = cache->cred;
20081c3c07e9STrond Myklebust 	res->mask = cache->mask;
2009cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
20101c3c07e9STrond Myklebust 	err = 0;
20111c3c07e9STrond Myklebust out:
20121c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20131c3c07e9STrond Myklebust 	return err;
20141c3c07e9STrond Myklebust out_stale:
20151c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2016cfcea3e8STrond Myklebust 	list_del(&cache->lru);
20171c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20181c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
20191da177e4SLinus Torvalds 	return -ENOENT;
20201c3c07e9STrond Myklebust out_zap:
20211a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20221a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
20231c3c07e9STrond Myklebust 	return -ENOENT;
20241c3c07e9STrond Myklebust }
20251c3c07e9STrond Myklebust 
20261c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
20271c3c07e9STrond Myklebust {
2028cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2029cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20301c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
20311c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
20321c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20331c3c07e9STrond Myklebust 
20341c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20351c3c07e9STrond Myklebust 	while (*p != NULL) {
20361c3c07e9STrond Myklebust 		parent = *p;
20371c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
20381c3c07e9STrond Myklebust 
20391c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
20401c3c07e9STrond Myklebust 			p = &parent->rb_left;
20411c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
20421c3c07e9STrond Myklebust 			p = &parent->rb_right;
20431c3c07e9STrond Myklebust 		else
20441c3c07e9STrond Myklebust 			goto found;
20451c3c07e9STrond Myklebust 	}
20461c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
20471c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2048cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
20491c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20501c3c07e9STrond Myklebust 	return;
20511c3c07e9STrond Myklebust found:
20521c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2053cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2054cfcea3e8STrond Myklebust 	list_del(&entry->lru);
20551c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20561c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
20571da177e4SLinus Torvalds }
20581da177e4SLinus Torvalds 
2059af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
20601da177e4SLinus Torvalds {
20611c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
20621c3c07e9STrond Myklebust 	if (cache == NULL)
20631c3c07e9STrond Myklebust 		return;
20641c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
20651da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
20661c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
20671da177e4SLinus Torvalds 	cache->mask = set->mask;
20681c3c07e9STrond Myklebust 
20691c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2070cfcea3e8STrond Myklebust 
2071cfcea3e8STrond Myklebust 	/* Update accounting */
2072cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2073cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2074cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2075cfcea3e8STrond Myklebust 
2076cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
20771a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2078cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
20791a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
20801a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
20811a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2082cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2083cfcea3e8STrond Myklebust 	}
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
20871da177e4SLinus Torvalds {
20881da177e4SLinus Torvalds 	struct nfs_access_entry cache;
20891da177e4SLinus Torvalds 	int status;
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
20921da177e4SLinus Torvalds 	if (status == 0)
20931da177e4SLinus Torvalds 		goto out;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
20961da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
20971da177e4SLinus Torvalds 	cache.cred = cred;
20981da177e4SLinus Torvalds 	cache.jiffies = jiffies;
20991da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2100a71ee337SSuresh Jayaraman 	if (status != 0) {
2101a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2102a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2103a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2104a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2105a71ee337SSuresh Jayaraman 		}
21061da177e4SLinus Torvalds 		return status;
2107a71ee337SSuresh Jayaraman 	}
21081da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
21091da177e4SLinus Torvalds out:
2110e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21111da177e4SLinus Torvalds 		return 0;
21121da177e4SLinus Torvalds 	return -EACCES;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2116af22f94aSTrond Myklebust {
2117af22f94aSTrond Myklebust 	int mask = 0;
2118af22f94aSTrond Myklebust 
21198a5e929dSAl Viro 	if ((openflags & O_ACCMODE) != O_WRONLY)
2120af22f94aSTrond Myklebust 		mask |= MAY_READ;
21218a5e929dSAl Viro 	if ((openflags & O_ACCMODE) != O_RDONLY)
2122af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
21238a5e929dSAl Viro 	if (openflags & __FMODE_EXEC)
2124af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2125af22f94aSTrond Myklebust 	return mask;
2126af22f94aSTrond Myklebust }
2127af22f94aSTrond Myklebust 
2128af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2129af22f94aSTrond Myklebust {
2130af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2131af22f94aSTrond Myklebust }
2132af22f94aSTrond Myklebust 
213310556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	struct rpc_cred *cred;
21361da177e4SLinus Torvalds 	int res = 0;
21371da177e4SLinus Torvalds 
213810556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2139b74c79e9SNick Piggin 		return -ECHILD;
2140b74c79e9SNick Piggin 
214191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
214291d5b470SChuck Lever 
2143e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21441da177e4SLinus Torvalds 		goto out;
21451da177e4SLinus Torvalds 	/* Is this sys_access() ? */
21469cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
21471da177e4SLinus Torvalds 		goto force_lookup;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
21501da177e4SLinus Torvalds 		case S_IFLNK:
21511da177e4SLinus Torvalds 			goto out;
21521da177e4SLinus Torvalds 		case S_IFREG:
21531da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
21541da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
21557ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
21567ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
21571da177e4SLinus Torvalds 				goto out;
21581da177e4SLinus Torvalds 			break;
21591da177e4SLinus Torvalds 		case S_IFDIR:
21601da177e4SLinus Torvalds 			/*
21611da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
21621da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
21631da177e4SLinus Torvalds 			 */
21641da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
21651da177e4SLinus Torvalds 				goto out;
21661da177e4SLinus Torvalds 	}
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds force_lookup:
21691da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
21701da177e4SLinus Torvalds 		goto out_notsup;
21711da177e4SLinus Torvalds 
217298a8e323STrond Myklebust 	cred = rpc_lookup_cred();
21731da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
21741da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
21751da177e4SLinus Torvalds 		put_rpccred(cred);
21761da177e4SLinus Torvalds 	} else
21771da177e4SLinus Torvalds 		res = PTR_ERR(cred);
21781da177e4SLinus Torvalds out:
2179f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2180f696a365SMiklos Szeredi 		res = -EACCES;
2181f696a365SMiklos Szeredi 
21821e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
21831e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
21841da177e4SLinus Torvalds 	return res;
21851da177e4SLinus Torvalds out_notsup:
21861da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
21871da177e4SLinus Torvalds 	if (res == 0)
21882830ba7fSAl Viro 		res = generic_permission(inode, mask);
21891e7cb3dcSChuck Lever 	goto out;
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds /*
21931da177e4SLinus Torvalds  * Local variables:
21941da177e4SLinus Torvalds  *  version-control: t
21951da177e4SLinus Torvalds  *  kept-new-versions: 5
21961da177e4SLinus Torvalds  * End:
21971da177e4SLinus Torvalds  */
2198