xref: /openbmc/linux/fs/nfs/dir.c (revision 597d92891b8859b4b4949fd08e25e60fc80ddaaf)
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 
66b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
6792e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
68b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
69b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
70b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
71b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
72b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
73b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
74b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
75b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
76b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
77b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
78b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
79b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
80b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
81b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
82b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
83b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
84b7fa0554SAndreas Gruenbacher };
85b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
86b7fa0554SAndreas Gruenbacher 
871da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
881da177e4SLinus Torvalds 
89d9585277SAl Viro static int nfs_atomic_open(struct inode *, struct dentry *,
9030d90494SAl Viro 			   struct file *, unsigned, umode_t,
9147237687SAl Viro 			   int *);
9292e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
938867fe58SMiklos Szeredi 	.create		= nfs_create,
940dd2b474SMiklos Szeredi 	.lookup		= nfs_lookup,
950dd2b474SMiklos Szeredi 	.atomic_open	= nfs_atomic_open,
961da177e4SLinus Torvalds 	.link		= nfs_link,
971da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
981da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
991da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1001da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1011da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1021da177e4SLinus Torvalds 	.rename		= nfs_rename,
1031da177e4SLinus Torvalds 	.permission	= nfs_permission,
1041da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1051da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
10664c2ce8bSAneesh Kumar K.V 	.getxattr	= generic_getxattr,
10764c2ce8bSAneesh Kumar K.V 	.setxattr	= generic_setxattr,
10864c2ce8bSAneesh Kumar K.V 	.listxattr	= generic_listxattr,
10964c2ce8bSAneesh Kumar K.V 	.removexattr	= generic_removexattr,
1101da177e4SLinus Torvalds };
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1131da177e4SLinus Torvalds 
1140c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
115480c2006SBryan Schumaker {
116480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
117480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
118480c2006SBryan Schumaker 	if (ctx != NULL) {
1198ef2ce3eSBryan Schumaker 		ctx->duped = 0;
1200c030806STrond Myklebust 		ctx->attr_gencount = NFS_I(dir)->attr_gencount;
121480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
1228ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
123480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
124480c2006SBryan Schumaker 		return ctx;
125480c2006SBryan Schumaker 	}
1260c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
1270c030806STrond Myklebust }
128480c2006SBryan Schumaker 
129480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
130480c2006SBryan Schumaker {
131480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
132480c2006SBryan Schumaker 	kfree(ctx);
133480c2006SBryan Schumaker }
134480c2006SBryan Schumaker 
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds  * Open file
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds static int
1391da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1401da177e4SLinus Torvalds {
141480c2006SBryan Schumaker 	int res = 0;
142480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
143480c2006SBryan Schumaker 	struct rpc_cred *cred;
1441da177e4SLinus Torvalds 
1456da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
146cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
147cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
148cc0dd2d1SChuck Lever 
149cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1501e7cb3dcSChuck Lever 
151480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
152480c2006SBryan Schumaker 	if (IS_ERR(cred))
153480c2006SBryan Schumaker 		return PTR_ERR(cred);
1540c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
155480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
156480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
157480c2006SBryan Schumaker 		goto out;
158480c2006SBryan Schumaker 	}
159480c2006SBryan Schumaker 	filp->private_data = ctx;
160f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
161f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
162f5a73672SNeil Brown 		 * have been called, so we need to refresh the
163f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
164f5a73672SNeil Brown 		 */
165f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
166f5a73672SNeil Brown 	}
167480c2006SBryan Schumaker out:
168480c2006SBryan Schumaker 	put_rpccred(cred);
1691da177e4SLinus Torvalds 	return res;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
172480c2006SBryan Schumaker static int
173480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
174480c2006SBryan Schumaker {
175480c2006SBryan Schumaker 	put_nfs_open_dir_context(filp->private_data);
176480c2006SBryan Schumaker 	return 0;
177480c2006SBryan Schumaker }
178480c2006SBryan Schumaker 
179d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
180d1bacf9eSBryan Schumaker 	u64 cookie;
181d1bacf9eSBryan Schumaker 	u64 ino;
182d1bacf9eSBryan Schumaker 	struct qstr string;
1830b26a0bfSTrond Myklebust 	unsigned char d_type;
184d1bacf9eSBryan Schumaker };
185d1bacf9eSBryan Schumaker 
186d1bacf9eSBryan Schumaker struct nfs_cache_array {
18788b8e133SChuck Lever 	int size;
188d1bacf9eSBryan Schumaker 	int eof_index;
189d1bacf9eSBryan Schumaker 	u64 last_cookie;
190d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
191d1bacf9eSBryan Schumaker };
192d1bacf9eSBryan Schumaker 
193573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1941da177e4SLinus Torvalds typedef struct {
1951da177e4SLinus Torvalds 	struct file	*file;
1961da177e4SLinus Torvalds 	struct page	*page;
1971da177e4SLinus Torvalds 	unsigned long	page_index;
198f0dd2136STrond Myklebust 	u64		*dir_cookie;
1990aded708STrond Myklebust 	u64		last_cookie;
200f0dd2136STrond Myklebust 	loff_t		current_index;
2011da177e4SLinus Torvalds 	decode_dirent_t	decode;
202d1bacf9eSBryan Schumaker 
2031f4eab7eSNeil Brown 	unsigned long	timestamp;
2044704f0e2STrond Myklebust 	unsigned long	gencount;
205d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
206d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
207d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
2081da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
2091da177e4SLinus Torvalds 
210d1bacf9eSBryan Schumaker /*
211d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
2121da177e4SLinus Torvalds  */
2131da177e4SLinus Torvalds static
214d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
2151da177e4SLinus Torvalds {
2168cd51a0cSTrond Myklebust 	void *ptr;
217d1bacf9eSBryan Schumaker 	if (page == NULL)
218d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
2198cd51a0cSTrond Myklebust 	ptr = kmap(page);
2208cd51a0cSTrond Myklebust 	if (ptr == NULL)
2218cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
2228cd51a0cSTrond Myklebust 	return ptr;
223d1bacf9eSBryan Schumaker }
224d1bacf9eSBryan Schumaker 
225d1bacf9eSBryan Schumaker static
226d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
227d1bacf9eSBryan Schumaker {
228d1bacf9eSBryan Schumaker 	kunmap(page);
229d1bacf9eSBryan Schumaker }
230d1bacf9eSBryan Schumaker 
231d1bacf9eSBryan Schumaker /*
232d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
233d1bacf9eSBryan Schumaker  */
234d1bacf9eSBryan Schumaker static
23511de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
236d1bacf9eSBryan Schumaker {
23711de3b11STrond Myklebust 	struct nfs_cache_array *array;
238d1bacf9eSBryan Schumaker 	int i;
2398cd51a0cSTrond Myklebust 
2402b86ce2dSCong Wang 	array = kmap_atomic(page);
241d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
242d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
2432b86ce2dSCong Wang 	kunmap_atomic(array);
244d1bacf9eSBryan Schumaker }
245d1bacf9eSBryan Schumaker 
246d1bacf9eSBryan Schumaker /*
247d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
248d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
249d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
250d1bacf9eSBryan Schumaker  */
251d1bacf9eSBryan Schumaker static
2524a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
253d1bacf9eSBryan Schumaker {
254d1bacf9eSBryan Schumaker 	string->len = len;
255d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2564a201d6eSTrond Myklebust 	if (string->name == NULL)
2574a201d6eSTrond Myklebust 		return -ENOMEM;
25804e4bd1cSCatalin Marinas 	/*
25904e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
26004e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
26104e4bd1cSCatalin Marinas 	 */
26204e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2634a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2644a201d6eSTrond Myklebust 	return 0;
265d1bacf9eSBryan Schumaker }
266d1bacf9eSBryan Schumaker 
267d1bacf9eSBryan Schumaker static
268d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
269d1bacf9eSBryan Schumaker {
270d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2714a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2724a201d6eSTrond Myklebust 	int ret;
2734a201d6eSTrond Myklebust 
274d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
275d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
276d1bacf9eSBryan Schumaker 
2774a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2783020093fSTrond Myklebust 
2793020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2803020093fSTrond Myklebust 	ret = -ENOSPC;
2813020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2823020093fSTrond Myklebust 		goto out;
2833020093fSTrond Myklebust 
2844a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2854a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2860b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2874a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2884a201d6eSTrond Myklebust 	if (ret)
2894a201d6eSTrond Myklebust 		goto out;
290d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2918cd51a0cSTrond Myklebust 	array->size++;
29247c716cbSTrond Myklebust 	if (entry->eof != 0)
293d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2944a201d6eSTrond Myklebust out:
295d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2964a201d6eSTrond Myklebust 	return ret;
297d1bacf9eSBryan Schumaker }
298d1bacf9eSBryan Schumaker 
299d1bacf9eSBryan Schumaker static
300d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
301d1bacf9eSBryan Schumaker {
302d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
303d1bacf9eSBryan Schumaker 	unsigned int index;
304d1bacf9eSBryan Schumaker 
305d1bacf9eSBryan Schumaker 	if (diff < 0)
306d1bacf9eSBryan Schumaker 		goto out_eof;
307d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
3088cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
309d1bacf9eSBryan Schumaker 			goto out_eof;
310d1bacf9eSBryan Schumaker 		return -EAGAIN;
311d1bacf9eSBryan Schumaker 	}
312d1bacf9eSBryan Schumaker 
313d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
314d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
315d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
316d1bacf9eSBryan Schumaker 	return 0;
317d1bacf9eSBryan Schumaker out_eof:
318d1bacf9eSBryan Schumaker 	desc->eof = 1;
319d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
320d1bacf9eSBryan Schumaker }
321d1bacf9eSBryan Schumaker 
322d1bacf9eSBryan Schumaker static
323d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
324d1bacf9eSBryan Schumaker {
325d1bacf9eSBryan Schumaker 	int i;
3268ef2ce3eSBryan Schumaker 	loff_t new_pos;
327d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
328d1bacf9eSBryan Schumaker 
329d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3308cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
3310c030806STrond Myklebust 			struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode);
3320c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
3330c030806STrond Myklebust 
3348ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3350c030806STrond Myklebust 			if (ctx->attr_gencount != nfsi->attr_gencount
3360c030806STrond Myklebust 			    || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
3370c030806STrond Myklebust 				ctx->duped = 0;
3380c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
3390c030806STrond Myklebust 			} else if (new_pos < desc->file->f_pos) {
3400c030806STrond Myklebust 				if (ctx->duped > 0
3410c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
3420c030806STrond Myklebust 					if (printk_ratelimit()) {
3430c030806STrond Myklebust 						pr_notice("NFS: directory %s/%s contains a readdir loop."
3440c030806STrond Myklebust 								"Please contact your server vendor.  "
345374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
3460c030806STrond Myklebust 								desc->file->f_dentry->d_parent->d_name.name,
3470c030806STrond Myklebust 								desc->file->f_dentry->d_name.name,
348374e4e3eSBryan Schumaker 								array->array[i].string.name,
3490c030806STrond Myklebust 								*desc->dir_cookie);
3500c030806STrond Myklebust 					}
3510c030806STrond Myklebust 					status = -ELOOP;
3520c030806STrond Myklebust 					goto out;
3530c030806STrond Myklebust 				}
3548ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3550c030806STrond Myklebust 				ctx->duped = -1;
3568ef2ce3eSBryan Schumaker 			}
3578ef2ce3eSBryan Schumaker 			desc->file->f_pos = new_pos;
3588cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
35947c716cbSTrond Myklebust 			return 0;
3608cd51a0cSTrond Myklebust 		}
3618cd51a0cSTrond Myklebust 	}
36247c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
363d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
36418fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
36518fb5fe4STrond Myklebust 			desc->eof = 1;
366d1bacf9eSBryan Schumaker 	}
3670c030806STrond Myklebust out:
368d1bacf9eSBryan Schumaker 	return status;
369d1bacf9eSBryan Schumaker }
370d1bacf9eSBryan Schumaker 
371d1bacf9eSBryan Schumaker static
372d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
373d1bacf9eSBryan Schumaker {
374d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
37547c716cbSTrond Myklebust 	int status;
376d1bacf9eSBryan Schumaker 
377d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
378d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
379d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
380d1bacf9eSBryan Schumaker 		goto out;
381d1bacf9eSBryan Schumaker 	}
382d1bacf9eSBryan Schumaker 
383d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
384d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
385d1bacf9eSBryan Schumaker 	else
386d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
387d1bacf9eSBryan Schumaker 
38847c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3890aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
390e47c085aSTrond Myklebust 		desc->current_index += array->size;
39147c716cbSTrond Myklebust 		desc->page_index++;
39247c716cbSTrond Myklebust 	}
393d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
394d1bacf9eSBryan Schumaker out:
395d1bacf9eSBryan Schumaker 	return status;
396d1bacf9eSBryan Schumaker }
397d1bacf9eSBryan Schumaker 
398d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
399d1bacf9eSBryan Schumaker static
40056e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
401d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
402d1bacf9eSBryan Schumaker {
403480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
404480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
4054704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
4061da177e4SLinus Torvalds 	int		error;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds  again:
4091da177e4SLinus Torvalds 	timestamp = jiffies;
4104704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
41156e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
4121da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
4131da177e4SLinus Torvalds 	if (error < 0) {
4141da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
4151da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
4161da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
4173a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
4181da177e4SLinus Torvalds 			desc->plus = 0;
4191da177e4SLinus Torvalds 			goto again;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 		goto error;
4221da177e4SLinus Torvalds 	}
4231f4eab7eSNeil Brown 	desc->timestamp = timestamp;
4244704f0e2STrond Myklebust 	desc->gencount = gencount;
425d1bacf9eSBryan Schumaker error:
426d1bacf9eSBryan Schumaker 	return error;
427d1bacf9eSBryan Schumaker }
428d1bacf9eSBryan Schumaker 
429573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
430573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
431d1bacf9eSBryan Schumaker {
432573c4e1eSChuck Lever 	int error;
433d1bacf9eSBryan Schumaker 
434573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
435573c4e1eSChuck Lever 	if (error)
436573c4e1eSChuck Lever 		return error;
437d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
438d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
439d1bacf9eSBryan Schumaker 	return 0;
440d1bacf9eSBryan Schumaker }
441d1bacf9eSBryan Schumaker 
442d39ab9deSBryan Schumaker static
443d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
444d39ab9deSBryan Schumaker {
445d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
446d39ab9deSBryan Schumaker 		goto different;
44737a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
448d39ab9deSBryan Schumaker 		goto different;
449d39ab9deSBryan Schumaker 	return 1;
450d39ab9deSBryan Schumaker different:
451d39ab9deSBryan Schumaker 	return 0;
452d39ab9deSBryan Schumaker }
453d39ab9deSBryan Schumaker 
454d39ab9deSBryan Schumaker static
455d69ee9b8STrond Myklebust bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
456d69ee9b8STrond Myklebust {
457d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
458d69ee9b8STrond Myklebust 		return false;
459d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
460d69ee9b8STrond Myklebust 		return true;
461d69ee9b8STrond Myklebust 	if (filp->f_pos == 0)
462d69ee9b8STrond Myklebust 		return true;
463d69ee9b8STrond Myklebust 	return false;
464d69ee9b8STrond Myklebust }
465d69ee9b8STrond Myklebust 
466d69ee9b8STrond Myklebust /*
467d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
468d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
469d69ee9b8STrond Myklebust  * directory.
470d69ee9b8STrond Myklebust  */
471d69ee9b8STrond Myklebust static
472d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
473d69ee9b8STrond Myklebust {
474d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
475d69ee9b8STrond Myklebust }
476d69ee9b8STrond Myklebust 
477d69ee9b8STrond Myklebust static
478d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
479d39ab9deSBryan Schumaker {
48026fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4814a201d6eSTrond Myklebust 	struct dentry *dentry;
4824a201d6eSTrond Myklebust 	struct dentry *alias;
483d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
484d39ab9deSBryan Schumaker 	struct inode *inode;
485d39ab9deSBryan Schumaker 
4864a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4874a201d6eSTrond Myklebust 		if (filename.len == 1)
4884a201d6eSTrond Myklebust 			return;
4894a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4904a201d6eSTrond Myklebust 			return;
4914a201d6eSTrond Myklebust 	}
4924a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
493d39ab9deSBryan Schumaker 
4944a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
495d39ab9deSBryan Schumaker 	if (dentry != NULL) {
496d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
497d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
498d39ab9deSBryan Schumaker 			goto out;
499d39ab9deSBryan Schumaker 		} else {
500d39ab9deSBryan Schumaker 			d_drop(dentry);
501d39ab9deSBryan Schumaker 			dput(dentry);
502d39ab9deSBryan Schumaker 		}
503d39ab9deSBryan Schumaker 	}
504d39ab9deSBryan Schumaker 
505d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
5064a201d6eSTrond Myklebust 	if (dentry == NULL)
5074a201d6eSTrond Myklebust 		return;
5084a201d6eSTrond Myklebust 
509d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
510d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
511d39ab9deSBryan Schumaker 		goto out;
512d39ab9deSBryan Schumaker 
513d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
514d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
515d39ab9deSBryan Schumaker 		goto out;
516d39ab9deSBryan Schumaker 	else if (alias) {
517d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
518d39ab9deSBryan Schumaker 		dput(alias);
519d39ab9deSBryan Schumaker 	} else
520d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
521d39ab9deSBryan Schumaker 
522d39ab9deSBryan Schumaker out:
523d39ab9deSBryan Schumaker 	dput(dentry);
524d39ab9deSBryan Schumaker }
525d39ab9deSBryan Schumaker 
526d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
527d1bacf9eSBryan Schumaker static
5288cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5296650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
530d1bacf9eSBryan Schumaker {
531babddc72SBryan Schumaker 	struct xdr_stream stream;
532f7da7a12SBenny Halevy 	struct xdr_buf buf;
5336650239aSTrond Myklebust 	struct page *scratch;
53499424380SBryan Schumaker 	struct nfs_cache_array *array;
5355c346854STrond Myklebust 	unsigned int count = 0;
5365c346854STrond Myklebust 	int status;
537babddc72SBryan Schumaker 
5386650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5396650239aSTrond Myklebust 	if (scratch == NULL)
5406650239aSTrond Myklebust 		return -ENOMEM;
541babddc72SBryan Schumaker 
542f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5436650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
54499424380SBryan Schumaker 
54599424380SBryan Schumaker 	do {
54699424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5478cd51a0cSTrond Myklebust 		if (status != 0) {
5488cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5498cd51a0cSTrond Myklebust 				status = 0;
55099424380SBryan Schumaker 			break;
5518cd51a0cSTrond Myklebust 		}
55299424380SBryan Schumaker 
5535c346854STrond Myklebust 		count++;
5545c346854STrond Myklebust 
55547c716cbSTrond Myklebust 		if (desc->plus != 0)
556d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5578cd51a0cSTrond Myklebust 
5588cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5598cd51a0cSTrond Myklebust 		if (status != 0)
5608cd51a0cSTrond Myklebust 			break;
56199424380SBryan Schumaker 	} while (!entry->eof);
56299424380SBryan Schumaker 
56347c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
56499424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5658cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5668cd51a0cSTrond Myklebust 			array->eof_index = array->size;
56799424380SBryan Schumaker 			status = 0;
56899424380SBryan Schumaker 			nfs_readdir_release_array(page);
5695c346854STrond Myklebust 		} else
5705c346854STrond Myklebust 			status = PTR_ERR(array);
57156e4ebf8SBryan Schumaker 	}
5726650239aSTrond Myklebust 
5736650239aSTrond Myklebust 	put_page(scratch);
5748cd51a0cSTrond Myklebust 	return status;
5758cd51a0cSTrond Myklebust }
57656e4ebf8SBryan Schumaker 
57756e4ebf8SBryan Schumaker static
57856e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
57956e4ebf8SBryan Schumaker {
58056e4ebf8SBryan Schumaker 	unsigned int i;
58156e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
58256e4ebf8SBryan Schumaker 		put_page(pages[i]);
58356e4ebf8SBryan Schumaker }
58456e4ebf8SBryan Schumaker 
58556e4ebf8SBryan Schumaker static
58656e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
58756e4ebf8SBryan Schumaker 		unsigned int npages)
58856e4ebf8SBryan Schumaker {
58956e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
59056e4ebf8SBryan Schumaker }
59156e4ebf8SBryan Schumaker 
59256e4ebf8SBryan Schumaker /*
59356e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
59456e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
59556e4ebf8SBryan Schumaker  */
59656e4ebf8SBryan Schumaker static
5976650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
59856e4ebf8SBryan Schumaker {
59956e4ebf8SBryan Schumaker 	unsigned int i;
60056e4ebf8SBryan Schumaker 
60156e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
60256e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
60356e4ebf8SBryan Schumaker 		if (page == NULL)
60456e4ebf8SBryan Schumaker 			goto out_freepages;
60556e4ebf8SBryan Schumaker 		pages[i] = page;
60656e4ebf8SBryan Schumaker 	}
6076650239aSTrond Myklebust 	return 0;
60856e4ebf8SBryan Schumaker 
60956e4ebf8SBryan Schumaker out_freepages:
61056e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
6116650239aSTrond Myklebust 	return -ENOMEM;
612d1bacf9eSBryan Schumaker }
613d1bacf9eSBryan Schumaker 
614d1bacf9eSBryan Schumaker static
615d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
616d1bacf9eSBryan Schumaker {
61756e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
61856e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
619d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
620d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
621d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6228cd51a0cSTrond Myklebust 	int status = -ENOMEM;
62356e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
624d1bacf9eSBryan Schumaker 
625d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6260aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
627d1bacf9eSBryan Schumaker 	entry.eof = 0;
628d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
629d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
630573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
631d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
632d1bacf9eSBryan Schumaker 		goto out;
633d1bacf9eSBryan Schumaker 
634d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6358cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6368cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
6378cd51a0cSTrond Myklebust 		goto out;
6388cd51a0cSTrond Myklebust 	}
639d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
640d1bacf9eSBryan Schumaker 	array->eof_index = -1;
641d1bacf9eSBryan Schumaker 
6426650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6436650239aSTrond Myklebust 	if (status < 0)
644d1bacf9eSBryan Schumaker 		goto out_release_array;
645d1bacf9eSBryan Schumaker 	do {
646ac396128STrond Myklebust 		unsigned int pglen;
64756e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
648babddc72SBryan Schumaker 
649d1bacf9eSBryan Schumaker 		if (status < 0)
650d1bacf9eSBryan Schumaker 			break;
651ac396128STrond Myklebust 		pglen = status;
6526650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6538cd51a0cSTrond Myklebust 		if (status < 0) {
6548cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6558cd51a0cSTrond Myklebust 				status = 0;
6568cd51a0cSTrond Myklebust 			break;
6578cd51a0cSTrond Myklebust 		}
6588cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
659d1bacf9eSBryan Schumaker 
66056e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
661d1bacf9eSBryan Schumaker out_release_array:
662d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
663d1bacf9eSBryan Schumaker out:
664d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
665d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
666d1bacf9eSBryan Schumaker 	return status;
667d1bacf9eSBryan Schumaker }
668d1bacf9eSBryan Schumaker 
669d1bacf9eSBryan Schumaker /*
670d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
671d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
672d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
673d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6741da177e4SLinus Torvalds  */
675d1bacf9eSBryan Schumaker static
676d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
677d1bacf9eSBryan Schumaker {
678d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
6798cd51a0cSTrond Myklebust 	int ret;
680d1bacf9eSBryan Schumaker 
6818cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6828cd51a0cSTrond Myklebust 	if (ret < 0)
683d1bacf9eSBryan Schumaker 		goto error;
684d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
685d1bacf9eSBryan Schumaker 
6862aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
687cd9ae2b6STrond Myklebust 		/* Should never happen */
688cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
689cd9ae2b6STrond Myklebust 	}
6901da177e4SLinus Torvalds 	unlock_page(page);
6911da177e4SLinus Torvalds 	return 0;
6921da177e4SLinus Torvalds  error:
6931da177e4SLinus Torvalds 	unlock_page(page);
6948cd51a0cSTrond Myklebust 	return ret;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
697d1bacf9eSBryan Schumaker static
698d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6991da177e4SLinus Torvalds {
70011de3b11STrond Myklebust 	if (!desc->page->mapping)
70111de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
7021da177e4SLinus Torvalds 	page_cache_release(desc->page);
7031da177e4SLinus Torvalds 	desc->page = NULL;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
706d1bacf9eSBryan Schumaker static
707d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7081da177e4SLinus Torvalds {
7098cd51a0cSTrond Myklebust 	return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
710d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds /*
714d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7151da177e4SLinus Torvalds  */
716d1bacf9eSBryan Schumaker static
717d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
718d1bacf9eSBryan Schumaker {
719d1bacf9eSBryan Schumaker 	int res;
720d1bacf9eSBryan Schumaker 
721d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
722d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
723d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
724d1bacf9eSBryan Schumaker 
725d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
72647c716cbSTrond Myklebust 	if (res != 0)
727d1bacf9eSBryan Schumaker 		cache_page_release(desc);
728d1bacf9eSBryan Schumaker 	return res;
729d1bacf9eSBryan Schumaker }
730d1bacf9eSBryan Schumaker 
731d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7321da177e4SLinus Torvalds static inline
7331da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7341da177e4SLinus Torvalds {
7358cd51a0cSTrond Myklebust 	int res;
736d1bacf9eSBryan Schumaker 
7370aded708STrond Myklebust 	if (desc->page_index == 0) {
7388cd51a0cSTrond Myklebust 		desc->current_index = 0;
7390aded708STrond Myklebust 		desc->last_cookie = 0;
7400aded708STrond Myklebust 	}
74147c716cbSTrond Myklebust 	do {
742d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
74347c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7441da177e4SLinus Torvalds 	return res;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /*
7481da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7491da177e4SLinus Torvalds  */
7501da177e4SLinus Torvalds static
7511da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
7521da177e4SLinus Torvalds 		   filldir_t filldir)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	struct file	*file = desc->file;
755d1bacf9eSBryan Schumaker 	int i = 0;
756d1bacf9eSBryan Schumaker 	int res = 0;
757d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7588ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7598ef2ce3eSBryan Schumaker 
760d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
761e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
762e7c58e97STrond Myklebust 		res = PTR_ERR(array);
763e7c58e97STrond Myklebust 		goto out;
764e7c58e97STrond Myklebust 	}
7651da177e4SLinus Torvalds 
766d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
767ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7681da177e4SLinus Torvalds 
769ece0b423STrond Myklebust 		ent = &array->array[i];
770ece0b423STrond Myklebust 		if (filldir(dirent, ent->string.name, ent->string.len,
7710b26a0bfSTrond Myklebust 		    file->f_pos, nfs_compat_user_ino64(ent->ino),
7720b26a0bfSTrond Myklebust 		    ent->d_type) < 0) {
773ece0b423STrond Myklebust 			desc->eof = 1;
7741da177e4SLinus Torvalds 			break;
775ece0b423STrond Myklebust 		}
77600a92642SOlivier Galibert 		file->f_pos++;
777d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
778d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
779d1bacf9eSBryan Schumaker 		else
780d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7810c030806STrond Myklebust 		if (ctx->duped != 0)
7820c030806STrond Myklebust 			ctx->duped = 1;
7838cd51a0cSTrond Myklebust 	}
78447c716cbSTrond Myklebust 	if (array->eof_index >= 0)
785d1bacf9eSBryan Schumaker 		desc->eof = 1;
786d1bacf9eSBryan Schumaker 
787d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
788e7c58e97STrond Myklebust out:
789d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7901e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7911e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7921da177e4SLinus Torvalds 	return res;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds /*
7961da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7971da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7981da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7991da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8001da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8011da177e4SLinus Torvalds  *
8021da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8031da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8041da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8051da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8061da177e4SLinus Torvalds  */
8071da177e4SLinus Torvalds static inline
8081da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
8091da177e4SLinus Torvalds 		     filldir_t filldir)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	struct page	*page = NULL;
8121da177e4SLinus Torvalds 	int		status;
813d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
8140c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8151da177e4SLinus Torvalds 
8161e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8171e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8201da177e4SLinus Torvalds 	if (!page) {
8211da177e4SLinus Torvalds 		status = -ENOMEM;
8221da177e4SLinus Torvalds 		goto out;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 
8257a8e1dc3STrond Myklebust 	desc->page_index = 0;
8260aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8277a8e1dc3STrond Myklebust 	desc->page = page;
8280c030806STrond Myklebust 	ctx->duped = 0;
8297a8e1dc3STrond Myklebust 
83085f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
83185f8607eSTrond Myklebust 	if (status < 0)
832d1bacf9eSBryan Schumaker 		goto out_release;
833d1bacf9eSBryan Schumaker 
8341da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds  out:
8371e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8383110ff80SHarvey Harrison 			__func__, status);
8391da177e4SLinus Torvalds 	return status;
8401da177e4SLinus Torvalds  out_release:
841d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8421da177e4SLinus Torvalds 	goto out;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
84500a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
84600a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
84700a92642SOlivier Galibert    whole directory.
8481da177e4SLinus Torvalds  */
8491da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
8501da177e4SLinus Torvalds {
85101cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
8521da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8531da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8541da177e4SLinus Torvalds 			*desc = &my_desc;
855480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
85647c716cbSTrond Myklebust 	int res;
8571da177e4SLinus Torvalds 
8586da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8591e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8601e7cb3dcSChuck Lever 			(long long)filp->f_pos);
86191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
86291d5b470SChuck Lever 
8631da177e4SLinus Torvalds 	/*
86400a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
865f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
86600a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
86700a92642SOlivier Galibert 	 * revalidate the cookie.
8681da177e4SLinus Torvalds 	 */
8691da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	desc->file = filp;
872480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8731da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
874d69ee9b8STrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
8751da177e4SLinus Torvalds 
876565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
8771cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
878fccca7fcSTrond Myklebust 	if (res < 0)
879fccca7fcSTrond Myklebust 		goto out;
880fccca7fcSTrond Myklebust 
88147c716cbSTrond Myklebust 	do {
8821da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
88300a92642SOlivier Galibert 
8841da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
885ece0b423STrond Myklebust 			res = 0;
8861da177e4SLinus Torvalds 			/* This means either end of directory */
887d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8881da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
8891da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
890ece0b423STrond Myklebust 				if (res == 0)
8911da177e4SLinus Torvalds 					continue;
8921da177e4SLinus Torvalds 			}
8931da177e4SLinus Torvalds 			break;
8941da177e4SLinus Torvalds 		}
8951da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8963a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8971da177e4SLinus Torvalds 			nfs_zap_caches(inode);
898baf57a09STrond Myklebust 			desc->page_index = 0;
8991da177e4SLinus Torvalds 			desc->plus = 0;
900d1bacf9eSBryan Schumaker 			desc->eof = 0;
9011da177e4SLinus Torvalds 			continue;
9021da177e4SLinus Torvalds 		}
9031da177e4SLinus Torvalds 		if (res < 0)
9041da177e4SLinus Torvalds 			break;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
907ece0b423STrond Myklebust 		if (res < 0)
9081da177e4SLinus Torvalds 			break;
90947c716cbSTrond Myklebust 	} while (!desc->eof);
910fccca7fcSTrond Myklebust out:
911565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
9121e7cb3dcSChuck Lever 	if (res > 0)
9131e7cb3dcSChuck Lever 		res = 0;
914aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
9151e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9161e7cb3dcSChuck Lever 			res);
9171da177e4SLinus Torvalds 	return res;
9181da177e4SLinus Torvalds }
9191da177e4SLinus Torvalds 
92010afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
921f0dd2136STrond Myklebust {
922b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
923b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
924480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
925b84e06c5SChuck Lever 
9266da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
927b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
928b84e06c5SChuck Lever 			dentry->d_name.name,
929b84e06c5SChuck Lever 			offset, origin);
930b84e06c5SChuck Lever 
931b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
932f0dd2136STrond Myklebust 	switch (origin) {
933f0dd2136STrond Myklebust 		case 1:
934f0dd2136STrond Myklebust 			offset += filp->f_pos;
935f0dd2136STrond Myklebust 		case 0:
936f0dd2136STrond Myklebust 			if (offset >= 0)
937f0dd2136STrond Myklebust 				break;
938f0dd2136STrond Myklebust 		default:
939f0dd2136STrond Myklebust 			offset = -EINVAL;
940f0dd2136STrond Myklebust 			goto out;
941f0dd2136STrond Myklebust 	}
942f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
943f0dd2136STrond Myklebust 		filp->f_pos = offset;
944480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9458ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
946f0dd2136STrond Myklebust 	}
947f0dd2136STrond Myklebust out:
948b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
949f0dd2136STrond Myklebust 	return offset;
950f0dd2136STrond Myklebust }
951f0dd2136STrond Myklebust 
9521da177e4SLinus Torvalds /*
9531da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9541da177e4SLinus Torvalds  * is a dummy operation.
9551da177e4SLinus Torvalds  */
95602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
95702c24a82SJosef Bacik 			 int datasync)
9581da177e4SLinus Torvalds {
9597ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
96002c24a82SJosef Bacik 	struct inode *inode = dentry->d_inode;
9617ea80859SChristoph Hellwig 
9626da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9631e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9641e7cb3dcSChuck Lever 			datasync);
9651e7cb3dcSChuck Lever 
96602c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
96754917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
96802c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9691da177e4SLinus Torvalds 	return 0;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
972bfc69a45STrond Myklebust /**
973bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
974bfc69a45STrond Myklebust  * @dir - pointer to directory inode
975bfc69a45STrond Myklebust  *
976bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
977bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
978bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
979bfc69a45STrond Myklebust  *
980bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
981bfc69a45STrond Myklebust  */
982bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
983bfc69a45STrond Myklebust {
984011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
985bfc69a45STrond Myklebust }
986bfc69a45STrond Myklebust 
9871da177e4SLinus Torvalds /*
9881da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9891da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9901da177e4SLinus Torvalds  * and may need to be looked up again.
9911da177e4SLinus Torvalds  */
992c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9951da177e4SLinus Torvalds 		return 1;
9964eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9974eec952eSTrond Myklebust 		return 0;
998f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9996ecc5e8fSTrond Myklebust 		return 0;
1000f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1001f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1002f2c77f4eSTrond Myklebust 		return 0;
1003f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1004f2c77f4eSTrond Myklebust 		return 0;
1005f2c77f4eSTrond Myklebust 	return 1;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds /*
1009a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1010a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1011a12802caSTrond Myklebust  */
1012fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1013a12802caSTrond Myklebust {
1014a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1015a12802caSTrond Myklebust 		return 0;
1016fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1017a12802caSTrond Myklebust }
1018a12802caSTrond Myklebust 
1019a12802caSTrond Myklebust /*
10201d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10211d6757fbSTrond Myklebust  *
10221d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10231d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10241d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10251d6757fbSTrond Myklebust  *
10261d6757fbSTrond Myklebust  */
10271da177e4SLinus Torvalds static inline
1028fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
10311da177e4SLinus Torvalds 
103236d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10334e99a1ffSTrond Myklebust 		return 0;
10341da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1035fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10361da177e4SLinus Torvalds 		goto out_force;
10371da177e4SLinus Torvalds 	/* This is an open(2) */
1038fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1039fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10401da177e4SLinus Torvalds 		goto out_force;
10414f48af45STrond Myklebust 	return 0;
10421da177e4SLinus Torvalds out_force:
10431da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds /*
10471da177e4SLinus Torvalds  * We judge how long we want to trust negative
10481da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10491da177e4SLinus Torvalds  *
10501da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10511da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds static inline
10541da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1055fa3c56bbSAl Viro 		       unsigned int flags)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1058fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10591da177e4SLinus Torvalds 		return 0;
10604eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10614eec952eSTrond Myklebust 		return 1;
10621da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds /*
10661da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10671da177e4SLinus Torvalds  * and we should check whether we can really trust that
10681da177e4SLinus Torvalds  * lookup.
10691da177e4SLinus Torvalds  *
10701da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10711da177e4SLinus Torvalds  * we have an inode!
10721da177e4SLinus Torvalds  *
10731da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10741da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10751da177e4SLinus Torvalds  */
10760b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10771da177e4SLinus Torvalds {
10781da177e4SLinus Torvalds 	struct inode *dir;
10791da177e4SLinus Torvalds 	struct inode *inode;
10801da177e4SLinus Torvalds 	struct dentry *parent;
1081e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1082e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10831da177e4SLinus Torvalds 	int error;
10841da177e4SLinus Torvalds 
1085fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
108634286d66SNick Piggin 		return -ECHILD;
108734286d66SNick Piggin 
10881da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10891da177e4SLinus Torvalds 	dir = parent->d_inode;
109091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10911da177e4SLinus Torvalds 	inode = dentry->d_inode;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	if (!inode) {
1094fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
10951da177e4SLinus Torvalds 			goto out_bad;
1096d69ee9b8STrond Myklebust 		goto out_valid_noent;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
11001e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
11013110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
11021e7cb3dcSChuck Lever 				dentry->d_name.name);
11031da177e4SLinus Torvalds 		goto out_bad;
11041da177e4SLinus Torvalds 	}
11051da177e4SLinus Torvalds 
1106011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
110715860ab1STrond Myklebust 		goto out_set_verifier;
110815860ab1STrond Myklebust 
11091da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1110fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1111fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
11121da177e4SLinus Torvalds 			goto out_zap_parent;
11131da177e4SLinus Torvalds 		goto out_valid;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11171da177e4SLinus Torvalds 		goto out_bad;
11181da177e4SLinus Torvalds 
1119e1fb4d05STrond Myklebust 	error = -ENOMEM;
1120e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1121e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1122e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1123e1fb4d05STrond Myklebust 		goto out_error;
1124e1fb4d05STrond Myklebust 
112580a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
11261da177e4SLinus Torvalds 	if (error)
11271da177e4SLinus Torvalds 		goto out_bad;
1128e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11291da177e4SLinus Torvalds 		goto out_bad;
1130e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11311da177e4SLinus Torvalds 		goto out_bad;
11321da177e4SLinus Torvalds 
1133e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1134e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
113515860ab1STrond Myklebust out_set_verifier:
1136cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11371da177e4SLinus Torvalds  out_valid:
1138d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1139d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1140d69ee9b8STrond Myklebust  out_valid_noent:
11411da177e4SLinus Torvalds 	dput(parent);
11421e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
11433110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11441e7cb3dcSChuck Lever 			dentry->d_name.name);
11451da177e4SLinus Torvalds 	return 1;
11461da177e4SLinus Torvalds out_zap_parent:
11471da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11481da177e4SLinus Torvalds  out_bad:
1149a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11501da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11511da177e4SLinus Torvalds 		/* Purge readdir caches. */
11521da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11531da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11541da177e4SLinus Torvalds 		if (have_submounts(dentry))
11551da177e4SLinus Torvalds 			goto out_valid;
1156d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1157d9e80b7dSAl Viro 			goto out_valid;
11581da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
11591da177e4SLinus Torvalds 	}
11601da177e4SLinus Torvalds 	d_drop(dentry);
1161e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1162e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
11631da177e4SLinus Torvalds 	dput(parent);
11641e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11653110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11661e7cb3dcSChuck Lever 			dentry->d_name.name);
11671da177e4SLinus Torvalds 	return 0;
1168e1fb4d05STrond Myklebust out_error:
1169e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1170e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1171e1fb4d05STrond Myklebust 	dput(parent);
1172e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1173e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1174e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1175e1fb4d05STrond Myklebust 	return error;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds /*
11791da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11801da177e4SLinus Torvalds  */
1181fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
11841da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
11851da177e4SLinus Torvalds 		dentry->d_flags);
11861da177e4SLinus Torvalds 
118777f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
118877f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
118977f11192STrond Myklebust 		return 1;
119077f11192STrond Myklebust 
11911da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11921da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
11931da177e4SLinus Torvalds 		return 1;
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
11961da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
11971da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
11981da177e4SLinus Torvalds 		return 1;
11991da177e4SLinus Torvalds 	}
12001da177e4SLinus Torvalds 	return 0;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12051b83d707STrond Myklebust {
12061b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12071b83d707STrond Myklebust 	if (inode->i_nlink > 0)
12081b83d707STrond Myklebust 		drop_nlink(inode);
12091b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12101b83d707STrond Myklebust }
12111b83d707STrond Myklebust 
12121da177e4SLinus Torvalds /*
12131da177e4SLinus Torvalds  * Called when the dentry loses inode.
12141da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12151da177e4SLinus Torvalds  */
12161da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12171da177e4SLinus Torvalds {
121883672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
121983672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
122083672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
122183672d39SNeil Brown 
12221da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12239a53c3a7SDave Hansen 		drop_nlink(inode);
1224e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 	iput(inode);
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
1229b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1230b1942c5fSAl Viro {
1231b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1232b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1233b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1234b1942c5fSAl Viro 			WARN_ON(1);
1235b1942c5fSAl Viro 		else
1236b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1237b1942c5fSAl Viro 	}
1238b1942c5fSAl Viro }
1239b1942c5fSAl Viro 
1240f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12411da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
12421da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12431da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
124436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1245b1942c5fSAl Viro 	.d_release	= nfs_d_release,
12461da177e4SLinus Torvalds };
12471da177e4SLinus Torvalds 
1248*597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
12491da177e4SLinus Torvalds {
12501da177e4SLinus Torvalds 	struct dentry *res;
1251565277f6STrond Myklebust 	struct dentry *parent;
12521da177e4SLinus Torvalds 	struct inode *inode = NULL;
1253e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1254e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12551da177e4SLinus Torvalds 	int error;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
12581da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
125991d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12621da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12631da177e4SLinus Torvalds 		goto out;
12641da177e4SLinus Torvalds 
1265fd684071STrond Myklebust 	/*
1266fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1267fd684071STrond Myklebust 	 * but don't hash the dentry.
1268fd684071STrond Myklebust 	 */
126900cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1270fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1271fd684071STrond Myklebust 		res = NULL;
1272fc0f684cSTrond Myklebust 		goto out;
1273fd684071STrond Myklebust 	}
12741da177e4SLinus Torvalds 
1275e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1276e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1277e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1278e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1279e1fb4d05STrond Myklebust 		goto out;
1280e1fb4d05STrond Myklebust 
1281565277f6STrond Myklebust 	parent = dentry->d_parent;
1282565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1283565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
128480a16b21SBryan Schumaker 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
12851da177e4SLinus Torvalds 	if (error == -ENOENT)
12861da177e4SLinus Torvalds 		goto no_entry;
12871da177e4SLinus Torvalds 	if (error < 0) {
12881da177e4SLinus Torvalds 		res = ERR_PTR(error);
1289565277f6STrond Myklebust 		goto out_unblock_sillyrename;
12901da177e4SLinus Torvalds 	}
1291e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1292bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
129303f28e3aSTrond Myklebust 	if (IS_ERR(res))
1294565277f6STrond Myklebust 		goto out_unblock_sillyrename;
129554ceac45SDavid Howells 
1296d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1297d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1298d69ee9b8STrond Myklebust 
12991da177e4SLinus Torvalds no_entry:
130054ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13019eaef27bSTrond Myklebust 	if (res != NULL) {
13029eaef27bSTrond Myklebust 		if (IS_ERR(res))
1303565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13041da177e4SLinus Torvalds 		dentry = res;
13059eaef27bSTrond Myklebust 	}
13061da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1307565277f6STrond Myklebust out_unblock_sillyrename:
1308565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
13091da177e4SLinus Torvalds out:
1310e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1311e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13121da177e4SLinus Torvalds 	return res;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
13160b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13171da177e4SLinus Torvalds 
1318f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13190ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13201da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13211da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
132236d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1323b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13241da177e4SLinus Torvalds };
13251da177e4SLinus Torvalds 
13268a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13278a5e929dSAl Viro {
13288a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13298a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
13308a5e929dSAl Viro 		res |= FMODE_READ;
13318a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
13328a5e929dSAl Viro 		res |= FMODE_WRITE;
13338a5e929dSAl Viro 	return res;
13348a5e929dSAl Viro }
13358a5e929dSAl Viro 
133651141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1337cd9a1c0eSTrond Myklebust {
13385ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1339cd9a1c0eSTrond Myklebust }
1340cd9a1c0eSTrond Myklebust 
1341cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1342cd9a1c0eSTrond Myklebust {
1343cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1344cd9a1c0eSTrond Myklebust 	return 0;
1345cd9a1c0eSTrond Myklebust }
1346cd9a1c0eSTrond Myklebust 
1347d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13480dd2b474SMiklos Szeredi 			   struct dentry *dentry,
134930d90494SAl Viro 			   struct file *file, unsigned open_flags,
135047237687SAl Viro 			   int *opened)
1351cd9a1c0eSTrond Myklebust {
13520dd2b474SMiklos Szeredi 	int err;
13530dd2b474SMiklos Szeredi 
13540dd2b474SMiklos Szeredi 	if (ctx->dentry != dentry) {
13550dd2b474SMiklos Szeredi 		dput(ctx->dentry);
13560dd2b474SMiklos Szeredi 		ctx->dentry = dget(dentry);
13570dd2b474SMiklos Szeredi 	}
1358cd9a1c0eSTrond Myklebust 
1359cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1360cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
13610dd2b474SMiklos Szeredi 		err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1362d9585277SAl Viro 		if (err < 0)
1363cd9a1c0eSTrond Myklebust 			goto out;
1364cd9a1c0eSTrond Myklebust 	}
13650dd2b474SMiklos Szeredi 
136630d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
136730d90494SAl Viro 	if (err)
1368d9585277SAl Viro 		goto out;
136930d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
13700dd2b474SMiklos Szeredi 
1371cd9a1c0eSTrond Myklebust out:
1372cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1373d9585277SAl Viro 	return err;
1374cd9a1c0eSTrond Myklebust }
1375cd9a1c0eSTrond Myklebust 
1376d9585277SAl Viro static int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
137730d90494SAl Viro 			    struct file *file, unsigned open_flags,
137847237687SAl Viro 			    umode_t mode, int *opened)
13791da177e4SLinus Torvalds {
1380cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
13810dd2b474SMiklos Szeredi 	struct dentry *res;
13820dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1383f46e0bd3STrond Myklebust 	struct inode *inode;
1384898f635cSTrond Myklebust 	int err;
13851da177e4SLinus Torvalds 
13860dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
13870dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
13880dd2b474SMiklos Szeredi 
13890dd2b474SMiklos Szeredi 	dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
13901e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13911e7cb3dcSChuck Lever 
13920dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
13930dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
13940dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
13950dd2b474SMiklos Szeredi 			/*
13960dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
13970dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
13980dd2b474SMiklos Szeredi 			 * again
13990dd2b474SMiklos Szeredi 			 */
1400d9585277SAl Viro 			return -ENOENT;
14010dd2b474SMiklos Szeredi 		}
14021da177e4SLinus Torvalds 		goto no_open;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14050dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1406d9585277SAl Viro 		return -ENAMETOOLONG;
14071da177e4SLinus Torvalds 
14080dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1409536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14100dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14110dd2b474SMiklos Szeredi 	}
1412536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1413536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1414536e43d1STrond Myklebust 		attr.ia_size = 0;
1415cd9a1c0eSTrond Myklebust 	}
1416cd9a1c0eSTrond Myklebust 
14170dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14180dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14190dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1420d9585277SAl Viro 		goto out;
14210dd2b474SMiklos Szeredi 
1422f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14232b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
14240dd2b474SMiklos Szeredi 	d_drop(dentry);
1425f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1426f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1427cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
14280dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14290dd2b474SMiklos Szeredi 		switch (err) {
14301da177e4SLinus Torvalds 		case -ENOENT:
1431f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
14320dd2b474SMiklos Szeredi 			break;
14331788ea6eSJeff Layton 		case -EISDIR:
14346f926b5bSTrond Myklebust 		case -ENOTDIR:
14356f926b5bSTrond Myklebust 			goto no_open;
14361da177e4SLinus Torvalds 		case -ELOOP:
14370dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
14381da177e4SLinus Torvalds 				goto no_open;
14390dd2b474SMiklos Szeredi 			break;
14401da177e4SLinus Torvalds 			/* case -EINVAL: */
14411da177e4SLinus Torvalds 		default:
14420dd2b474SMiklos Szeredi 			break;
14431da177e4SLinus Torvalds 		}
14441da177e4SLinus Torvalds 		goto out;
14451da177e4SLinus Torvalds 	}
1446f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1447898f635cSTrond Myklebust 	if (res != NULL)
14480dd2b474SMiklos Szeredi 		dentry = res;
14490dd2b474SMiklos Szeredi 
14500dd2b474SMiklos Szeredi 	nfs_unblock_sillyrename(dentry->d_parent);
1451f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14520dd2b474SMiklos Szeredi 
145330d90494SAl Viro 	err = nfs_finish_open(ctx, dentry, file, open_flags, opened);
14540dd2b474SMiklos Szeredi 
14550dd2b474SMiklos Szeredi 	dput(res);
1456d9585277SAl Viro out:
1457d9585277SAl Viro 	return err;
14580dd2b474SMiklos Szeredi 
14591da177e4SLinus Torvalds no_open:
146000cd8dd3SAl Viro 	res = nfs_lookup(dir, dentry, 0);
14610dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
14620dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1463d9585277SAl Viro 		goto out;
14640dd2b474SMiklos Szeredi 
1465e45198a6SAl Viro 	return finish_no_open(file, res);
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14680b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
14691da177e4SLinus Torvalds {
14701da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1471657e94b6SNick Piggin 	struct inode *inode;
14721da177e4SLinus Torvalds 	struct inode *dir;
147350de348cSMiklos Szeredi 	int ret = 0;
14741da177e4SLinus Torvalds 
1475fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1476657e94b6SNick Piggin 		return -ECHILD;
1477657e94b6SNick Piggin 
1478fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1479eda72afbSMiklos Szeredi 		goto no_open;
1480eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
14815584c306STrond Myklebust 		goto no_open;
14822b484297STrond Myklebust 
1483eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
14841da177e4SLinus Torvalds 	parent = dget_parent(dentry);
14851da177e4SLinus Torvalds 	dir = parent->d_inode;
14862b484297STrond Myklebust 
14871da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
14881da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
14891da177e4SLinus Torvalds 	 */
1490216d5d06STrond Myklebust 	if (inode == NULL) {
1491fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1492216d5d06STrond Myklebust 			ret = 1;
14931da177e4SLinus Torvalds 		goto out;
1494216d5d06STrond Myklebust 	}
1495216d5d06STrond Myklebust 
14961da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
14971da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
14985584c306STrond Myklebust 		goto no_open_dput;
14991da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1500fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
15015584c306STrond Myklebust 		goto no_open_dput;
15021da177e4SLinus Torvalds 
15030ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1504898f635cSTrond Myklebust 	ret = 1;
15050ef97dcfSMiklos Szeredi 
15061da177e4SLinus Torvalds out:
15071da177e4SLinus Torvalds 	dput(parent);
15081da177e4SLinus Torvalds 	return ret;
1509535918f1STrond Myklebust 
15105584c306STrond Myklebust no_open_dput:
15111da177e4SLinus Torvalds 	dput(parent);
15125584c306STrond Myklebust no_open:
15130b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1514c0204fd2STrond Myklebust }
1515c0204fd2STrond Myklebust 
15161da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds /*
15191da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15201da177e4SLinus Torvalds  */
15211da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15221da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
15231da177e4SLinus Torvalds {
1524fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1525fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15261da177e4SLinus Torvalds 	struct inode *inode;
15271da177e4SLinus Torvalds 	int error = -EACCES;
15281da177e4SLinus Torvalds 
1529fab728e1STrond Myklebust 	d_drop(dentry);
1530fab728e1STrond Myklebust 
15311da177e4SLinus Torvalds 	/* We may have been initialized further down */
15321da177e4SLinus Torvalds 	if (dentry->d_inode)
1533fab728e1STrond Myklebust 		goto out;
15341da177e4SLinus Torvalds 	if (fhandle->size == 0) {
153580a16b21SBryan Schumaker 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
15361da177e4SLinus Torvalds 		if (error)
1537fab728e1STrond Myklebust 			goto out_error;
15381da177e4SLinus Torvalds 	}
15395724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15401da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15411da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15428fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
15431da177e4SLinus Torvalds 		if (error < 0)
1544fab728e1STrond Myklebust 			goto out_error;
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
154703f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
154803f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1549fab728e1STrond Myklebust 		goto out_error;
1550fab728e1STrond Myklebust 	d_add(dentry, inode);
1551fab728e1STrond Myklebust out:
1552fab728e1STrond Myklebust 	dput(parent);
15531da177e4SLinus Torvalds 	return 0;
1554fab728e1STrond Myklebust out_error:
1555fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1556fab728e1STrond Myklebust 	dput(parent);
1557fab728e1STrond Myklebust 	return error;
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds /*
15611da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15621da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15631da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15641da177e4SLinus Torvalds  * reply path made it appear to have failed.
15651da177e4SLinus Torvalds  */
1566*597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1567ebfc3b49SAl Viro 		umode_t mode, bool excl)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	struct iattr attr;
1570ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
15711da177e4SLinus Torvalds 	int error;
15721da177e4SLinus Torvalds 
15731e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
15741e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	attr.ia_mode = mode;
15771da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15781da177e4SLinus Torvalds 
15798867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
15801da177e4SLinus Torvalds 	if (error != 0)
15811da177e4SLinus Torvalds 		goto out_err;
15821da177e4SLinus Torvalds 	return 0;
15831da177e4SLinus Torvalds out_err:
15841da177e4SLinus Torvalds 	d_drop(dentry);
15851da177e4SLinus Torvalds 	return error;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds /*
15891da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15901da177e4SLinus Torvalds  */
1591*597d9289SBryan Schumaker int
15921a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
15931da177e4SLinus Torvalds {
15941da177e4SLinus Torvalds 	struct iattr attr;
15951da177e4SLinus Torvalds 	int status;
15961da177e4SLinus Torvalds 
15971e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
15981e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16011da177e4SLinus Torvalds 		return -EINVAL;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	attr.ia_mode = mode;
16041da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16071da177e4SLinus Torvalds 	if (status != 0)
16081da177e4SLinus Torvalds 		goto out_err;
16091da177e4SLinus Torvalds 	return 0;
16101da177e4SLinus Torvalds out_err:
16111da177e4SLinus Torvalds 	d_drop(dentry);
16121da177e4SLinus Torvalds 	return status;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds /*
16161da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16171da177e4SLinus Torvalds  */
1618*597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	struct iattr attr;
16211da177e4SLinus Torvalds 	int error;
16221da177e4SLinus Torvalds 
16231e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16241e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16271da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16301da177e4SLinus Torvalds 	if (error != 0)
16311da177e4SLinus Torvalds 		goto out_err;
16321da177e4SLinus Torvalds 	return 0;
16331da177e4SLinus Torvalds out_err:
16341da177e4SLinus Torvalds 	d_drop(dentry);
16351da177e4SLinus Torvalds 	return error;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
1638d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1639d45b9d8bSTrond Myklebust {
1640d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1641d45b9d8bSTrond Myklebust 		d_delete(dentry);
1642d45b9d8bSTrond Myklebust }
1643d45b9d8bSTrond Myklebust 
1644*597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	int error;
16471da177e4SLinus Torvalds 
16481e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16491e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16521da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
16531da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1654ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1655d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1656d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	return error;
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds /*
16621da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
16631da177e4SLinus Torvalds  * and after checking that the file has only one user.
16641da177e4SLinus Torvalds  *
16651da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
16661da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
16671da177e4SLinus Torvalds  */
16681da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
16691da177e4SLinus Torvalds {
16701da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
16711da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16721da177e4SLinus Torvalds 	int error = -EBUSY;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
16751da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
16781da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16791da177e4SLinus Torvalds 		error = 0;
16801da177e4SLinus Torvalds 		goto out;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	if (inode != NULL) {
168457ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
16851da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
16861da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
16871da177e4SLinus Torvalds 		if (error == 0)
16881b83d707STrond Myklebust 			nfs_drop_nlink(inode);
16895ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
16901da177e4SLinus Torvalds 	} else
16911da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1692d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1693d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16941da177e4SLinus Torvalds out:
16951da177e4SLinus Torvalds 	return error;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
16991da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17001da177e4SLinus Torvalds  *
17011da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17021da177e4SLinus Torvalds  */
1703*597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
17041da177e4SLinus Torvalds {
17051da177e4SLinus Torvalds 	int error;
17061da177e4SLinus Torvalds 	int need_rehash = 0;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17091da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
1712b7ab39f6SNick Piggin 	if (dentry->d_count > 1) {
17131da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1714ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1715ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17161da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17171da177e4SLinus Torvalds 		return error;
17181da177e4SLinus Torvalds 	}
17191da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17201da177e4SLinus Torvalds 		__d_drop(dentry);
17211da177e4SLinus Torvalds 		need_rehash = 1;
17221da177e4SLinus Torvalds 	}
17231da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17241da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1725d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17261da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17271da177e4SLinus Torvalds 	} else if (need_rehash)
17281da177e4SLinus Torvalds 		d_rehash(dentry);
17291da177e4SLinus Torvalds 	return error;
17301da177e4SLinus Torvalds }
17311da177e4SLinus Torvalds 
1732873101b3SChuck Lever /*
1733873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1734873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1735873101b3SChuck Lever  * using prepare_write/commit_write.
1736873101b3SChuck Lever  *
1737873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1738873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1739873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1740873101b3SChuck Lever  * symlink request has completed on the server.
1741873101b3SChuck Lever  *
1742873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1743873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1744873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1745873101b3SChuck Lever  * and move the raw page into its mapping.
1746873101b3SChuck Lever  */
1747*597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17481da177e4SLinus Torvalds {
1749873101b3SChuck Lever 	struct pagevec lru_pvec;
1750873101b3SChuck Lever 	struct page *page;
1751873101b3SChuck Lever 	char *kaddr;
17521da177e4SLinus Torvalds 	struct iattr attr;
1753873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17541da177e4SLinus Torvalds 	int error;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
17571da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
17581da177e4SLinus Torvalds 
1759873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1760873101b3SChuck Lever 		return -ENAMETOOLONG;
17611da177e4SLinus Torvalds 
1762873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1763873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
17641da177e4SLinus Torvalds 
176583d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
176676566991STrond Myklebust 	if (!page)
1767873101b3SChuck Lever 		return -ENOMEM;
1768873101b3SChuck Lever 
17692b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1770873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1771873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1772873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
17732b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1774873101b3SChuck Lever 
177594a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1776873101b3SChuck Lever 	if (error != 0) {
1777873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1778873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1779873101b3SChuck Lever 			dentry->d_name.name, symname, error);
17801da177e4SLinus Torvalds 		d_drop(dentry);
1781873101b3SChuck Lever 		__free_page(page);
17821da177e4SLinus Torvalds 		return error;
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds 
1785873101b3SChuck Lever 	/*
1786873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1787873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1788873101b3SChuck Lever 	 */
1789873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1790873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1791873101b3SChuck Lever 							GFP_KERNEL)) {
179239cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
17934f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1794873101b3SChuck Lever 		SetPageUptodate(page);
1795873101b3SChuck Lever 		unlock_page(page);
1796873101b3SChuck Lever 	} else
1797873101b3SChuck Lever 		__free_page(page);
1798873101b3SChuck Lever 
1799873101b3SChuck Lever 	return 0;
1800873101b3SChuck Lever }
1801873101b3SChuck Lever 
1802*597d9289SBryan Schumaker int
18031da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18041da177e4SLinus Torvalds {
18051da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18061da177e4SLinus Torvalds 	int error;
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18091da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18101da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18111da177e4SLinus Torvalds 
181257ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
18139a3936aaSTrond Myklebust 
18149697d234STrond Myklebust 	d_drop(dentry);
18151da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1816cf809556STrond Myklebust 	if (error == 0) {
18177de9c6eeSAl Viro 		ihold(inode);
18189697d234STrond Myklebust 		d_add(dentry, inode);
1819cf809556STrond Myklebust 	}
18201da177e4SLinus Torvalds 	return error;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds /*
18241da177e4SLinus Torvalds  * RENAME
18251da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18261da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18271da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18281da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18291da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18301da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18311da177e4SLinus Torvalds  *
18321da177e4SLinus Torvalds  * FIXED.
18331da177e4SLinus Torvalds  *
18341da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18351da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18361da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18371da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18381da177e4SLinus Torvalds  * using the inode layer
18391da177e4SLinus Torvalds  *
18401da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18411da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18421da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18431da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18441da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18451da177e4SLinus Torvalds  * the rename.
18461da177e4SLinus Torvalds  */
1847*597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18481da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18491da177e4SLinus Torvalds {
18501da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18511da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18521da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
18531da177e4SLinus Torvalds 	int error = -EBUSY;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
18561da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18571da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1858b7ab39f6SNick Piggin 		 new_dentry->d_count);
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	/*
186128f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
186228f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
186328f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
186428f79a1aSMiklos Szeredi 	 * the new target.
18651da177e4SLinus Torvalds 	 */
186627226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
186727226104SMiklos Szeredi 		/*
186827226104SMiklos Szeredi 		 * To prevent any new references to the target during the
186927226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
187027226104SMiklos Szeredi 		 */
187127226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
187227226104SMiklos Szeredi 			d_drop(new_dentry);
187327226104SMiklos Szeredi 			rehash = new_dentry;
187427226104SMiklos Szeredi 		}
187527226104SMiklos Szeredi 
1876b7ab39f6SNick Piggin 		if (new_dentry->d_count > 2) {
18771da177e4SLinus Torvalds 			int err;
187827226104SMiklos Szeredi 
18791da177e4SLinus Torvalds 			/* copy the target dentry's name */
18801da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
18811da177e4SLinus Torvalds 					 &new_dentry->d_name);
18821da177e4SLinus Torvalds 			if (!dentry)
18831da177e4SLinus Torvalds 				goto out;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
18861da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
188724e93025SMiklos Szeredi 			if (err)
18881da177e4SLinus Torvalds 				goto out;
188924e93025SMiklos Szeredi 
189024e93025SMiklos Szeredi 			new_dentry = dentry;
189156335936SOGAWA Hirofumi 			rehash = NULL;
189224e93025SMiklos Szeredi 			new_inode = NULL;
1893b1e4adf4STrond Myklebust 		}
189427226104SMiklos Szeredi 	}
18951da177e4SLinus Torvalds 
189657ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1897b1e4adf4STrond Myklebust 	if (new_inode != NULL)
189857ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19011da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19025ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19031da177e4SLinus Torvalds out:
19041da177e4SLinus Torvalds 	if (rehash)
19051da177e4SLinus Torvalds 		d_rehash(rehash);
19061da177e4SLinus Torvalds 	if (!error) {
1907b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1908b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19091da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19108fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19118fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1912d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1913d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	/* new dentry created? */
19161da177e4SLinus Torvalds 	if (dentry)
19171da177e4SLinus Torvalds 		dput(dentry);
19181da177e4SLinus Torvalds 	return error;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
1921cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1922cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1923cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1924cfcea3e8STrond Myklebust 
19251c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19261c3c07e9STrond Myklebust {
19271c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19281c3c07e9STrond Myklebust 	kfree(entry);
1929cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1930cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1931cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19321c3c07e9STrond Myklebust }
19331c3c07e9STrond Myklebust 
19341a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19351a81bb8aSTrond Myklebust {
19361a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19371a81bb8aSTrond Myklebust 
19381a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19391a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19401a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19411a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19421a81bb8aSTrond Myklebust 	}
19431a81bb8aSTrond Myklebust }
19441a81bb8aSTrond Myklebust 
19451495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink,
19461495f230SYing Han 			      struct shrink_control *sc)
1947979df72eSTrond Myklebust {
1948979df72eSTrond Myklebust 	LIST_HEAD(head);
1949aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1950979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
19511495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
19521495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
1953979df72eSTrond Myklebust 
195461d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
195561d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
19569c7e7e23STrond Myklebust 
1957a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1958aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1959979df72eSTrond Myklebust 		struct inode *inode;
1960979df72eSTrond Myklebust 
1961979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1962979df72eSTrond Myklebust 			break;
19639c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
1964979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1965979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1966979df72eSTrond Myklebust 			goto remove_lru_entry;
1967979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1968979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1969979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1970979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1971979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1972979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1973979df72eSTrond Myklebust 					&nfs_access_lru_list);
1974979df72eSTrond Myklebust 		else {
1975979df72eSTrond Myklebust remove_lru_entry:
1976979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
19779c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
1978979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
19799c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
1980979df72eSTrond Myklebust 		}
198159844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
1982979df72eSTrond Myklebust 	}
1983979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19841a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
1985979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1986979df72eSTrond Myklebust }
1987979df72eSTrond Myklebust 
19881a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
19891c3c07e9STrond Myklebust {
19901c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
19911a81bb8aSTrond Myklebust 	struct rb_node *n;
19921c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19931c3c07e9STrond Myklebust 
19941c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
19951c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
19961c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19971c3c07e9STrond Myklebust 		rb_erase(n, root_node);
19981a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
19991c3c07e9STrond Myklebust 	}
20001c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20011c3c07e9STrond Myklebust }
20021c3c07e9STrond Myklebust 
20031c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20041c3c07e9STrond Myklebust {
20051a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20061a81bb8aSTrond Myklebust 
20071a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20081a81bb8aSTrond Myklebust 		return;
2009cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2010cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20111a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2012cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2013cfcea3e8STrond Myklebust 
20141c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20151a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20161a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20171a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20181a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20191c3c07e9STrond Myklebust }
20201c3c07e9STrond Myklebust 
20211c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20221c3c07e9STrond Myklebust {
20231c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20241c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20251c3c07e9STrond Myklebust 
20261c3c07e9STrond Myklebust 	while (n != NULL) {
20271c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20281c3c07e9STrond Myklebust 
20291c3c07e9STrond Myklebust 		if (cred < entry->cred)
20301c3c07e9STrond Myklebust 			n = n->rb_left;
20311c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20321c3c07e9STrond Myklebust 			n = n->rb_right;
20331c3c07e9STrond Myklebust 		else
20341c3c07e9STrond Myklebust 			return entry;
20351c3c07e9STrond Myklebust 	}
20361c3c07e9STrond Myklebust 	return NULL;
20371c3c07e9STrond Myklebust }
20381c3c07e9STrond Myklebust 
2039af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20401da177e4SLinus Torvalds {
204155296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
20421c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
20431c3c07e9STrond Myklebust 	int err = -ENOENT;
20441da177e4SLinus Torvalds 
20451c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20461c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
20471c3c07e9STrond Myklebust 		goto out_zap;
20481c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20491c3c07e9STrond Myklebust 	if (cache == NULL)
20501c3c07e9STrond Myklebust 		goto out;
2051b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
205264672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
20531c3c07e9STrond Myklebust 		goto out_stale;
20541c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
20551c3c07e9STrond Myklebust 	res->cred = cache->cred;
20561c3c07e9STrond Myklebust 	res->mask = cache->mask;
2057cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
20581c3c07e9STrond Myklebust 	err = 0;
20591c3c07e9STrond Myklebust out:
20601c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20611c3c07e9STrond Myklebust 	return err;
20621c3c07e9STrond Myklebust out_stale:
20631c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2064cfcea3e8STrond Myklebust 	list_del(&cache->lru);
20651c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20661c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
20671da177e4SLinus Torvalds 	return -ENOENT;
20681c3c07e9STrond Myklebust out_zap:
20691a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20701a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
20711c3c07e9STrond Myklebust 	return -ENOENT;
20721c3c07e9STrond Myklebust }
20731c3c07e9STrond Myklebust 
20741c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
20751c3c07e9STrond Myklebust {
2076cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2077cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20781c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
20791c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
20801c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20811c3c07e9STrond Myklebust 
20821c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20831c3c07e9STrond Myklebust 	while (*p != NULL) {
20841c3c07e9STrond Myklebust 		parent = *p;
20851c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
20861c3c07e9STrond Myklebust 
20871c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
20881c3c07e9STrond Myklebust 			p = &parent->rb_left;
20891c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
20901c3c07e9STrond Myklebust 			p = &parent->rb_right;
20911c3c07e9STrond Myklebust 		else
20921c3c07e9STrond Myklebust 			goto found;
20931c3c07e9STrond Myklebust 	}
20941c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
20951c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2096cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
20971c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20981c3c07e9STrond Myklebust 	return;
20991c3c07e9STrond Myklebust found:
21001c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2101cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2102cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21031c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21041c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
2107af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21081da177e4SLinus Torvalds {
21091c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21101c3c07e9STrond Myklebust 	if (cache == NULL)
21111c3c07e9STrond Myklebust 		return;
21121c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21131da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21141c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21151da177e4SLinus Torvalds 	cache->mask = set->mask;
21161c3c07e9STrond Myklebust 
21171c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2118cfcea3e8STrond Myklebust 
2119cfcea3e8STrond Myklebust 	/* Update accounting */
2120cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2121cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2122cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2123cfcea3e8STrond Myklebust 
2124cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21251a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2126cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21271a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21281a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21291a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2130cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2131cfcea3e8STrond Myklebust 	}
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21351da177e4SLinus Torvalds {
21361da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21371da177e4SLinus Torvalds 	int status;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21401da177e4SLinus Torvalds 	if (status == 0)
21411da177e4SLinus Torvalds 		goto out;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
21441da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
21451da177e4SLinus Torvalds 	cache.cred = cred;
21461da177e4SLinus Torvalds 	cache.jiffies = jiffies;
21471da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2148a71ee337SSuresh Jayaraman 	if (status != 0) {
2149a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2150a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2151a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2152a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2153a71ee337SSuresh Jayaraman 		}
21541da177e4SLinus Torvalds 		return status;
2155a71ee337SSuresh Jayaraman 	}
21561da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
21571da177e4SLinus Torvalds out:
2158e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21591da177e4SLinus Torvalds 		return 0;
21601da177e4SLinus Torvalds 	return -EACCES;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
2163af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2164af22f94aSTrond Myklebust {
2165af22f94aSTrond Myklebust 	int mask = 0;
2166af22f94aSTrond Myklebust 
21678a5e929dSAl Viro 	if ((openflags & O_ACCMODE) != O_WRONLY)
2168af22f94aSTrond Myklebust 		mask |= MAY_READ;
21698a5e929dSAl Viro 	if ((openflags & O_ACCMODE) != O_RDONLY)
2170af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
21718a5e929dSAl Viro 	if (openflags & __FMODE_EXEC)
2172af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2173af22f94aSTrond Myklebust 	return mask;
2174af22f94aSTrond Myklebust }
2175af22f94aSTrond Myklebust 
2176af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2177af22f94aSTrond Myklebust {
2178af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2179af22f94aSTrond Myklebust }
2180af22f94aSTrond Myklebust 
218110556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
21821da177e4SLinus Torvalds {
21831da177e4SLinus Torvalds 	struct rpc_cred *cred;
21841da177e4SLinus Torvalds 	int res = 0;
21851da177e4SLinus Torvalds 
218610556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2187b74c79e9SNick Piggin 		return -ECHILD;
2188b74c79e9SNick Piggin 
218991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
219091d5b470SChuck Lever 
2191e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21921da177e4SLinus Torvalds 		goto out;
21931da177e4SLinus Torvalds 	/* Is this sys_access() ? */
21949cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
21951da177e4SLinus Torvalds 		goto force_lookup;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
21981da177e4SLinus Torvalds 		case S_IFLNK:
21991da177e4SLinus Torvalds 			goto out;
22001da177e4SLinus Torvalds 		case S_IFREG:
22011da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
22021da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
22037ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
22047ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
22051da177e4SLinus Torvalds 				goto out;
22061da177e4SLinus Torvalds 			break;
22071da177e4SLinus Torvalds 		case S_IFDIR:
22081da177e4SLinus Torvalds 			/*
22091da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22101da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22111da177e4SLinus Torvalds 			 */
22121da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22131da177e4SLinus Torvalds 				goto out;
22141da177e4SLinus Torvalds 	}
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds force_lookup:
22171da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22181da177e4SLinus Torvalds 		goto out_notsup;
22191da177e4SLinus Torvalds 
222098a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22211da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22221da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
22231da177e4SLinus Torvalds 		put_rpccred(cred);
22241da177e4SLinus Torvalds 	} else
22251da177e4SLinus Torvalds 		res = PTR_ERR(cred);
22261da177e4SLinus Torvalds out:
2227f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2228f696a365SMiklos Szeredi 		res = -EACCES;
2229f696a365SMiklos Szeredi 
22301e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
22311e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22321da177e4SLinus Torvalds 	return res;
22331da177e4SLinus Torvalds out_notsup:
22341da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22351da177e4SLinus Torvalds 	if (res == 0)
22362830ba7fSAl Viro 		res = generic_permission(inode, mask);
22371e7cb3dcSChuck Lever 	goto out;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds /*
22411da177e4SLinus Torvalds  * Local variables:
22421da177e4SLinus Torvalds  *  version-control: t
22431da177e4SLinus Torvalds  *  kept-new-versions: 5
22441da177e4SLinus Torvalds  * End:
22451da177e4SLinus Torvalds  */
2246