xref: /openbmc/linux/fs/nfs/dir.c (revision 14c43f767818c42f91ec7ffa586ee975845f68c8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
301da177e4SLinus Torvalds #include <linux/nfs_fs.h>
311da177e4SLinus Torvalds #include <linux/nfs_mount.h>
321da177e4SLinus Torvalds #include <linux/pagemap.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
36e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3704e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3864c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include "delegation.h"
4191d5b470SChuck Lever #include "iostat.h"
424c30d56eSAdrian Bunk #include "internal.h"
43cd9a1c0eSTrond Myklebust #include "fscache.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
48480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
491da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
5002c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
51f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5211de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
531da177e4SLinus Torvalds 
544b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
55f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
561da177e4SLinus Torvalds 	.read		= generic_read_dir,
571da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
581da177e4SLinus Torvalds 	.open		= nfs_opendir,
59480c2006SBryan Schumaker 	.release	= nfs_closedir,
601da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
6311de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6411de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
65d1bacf9eSBryan Schumaker };
66d1bacf9eSBryan Schumaker 
670c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
68480c2006SBryan Schumaker {
69480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
70480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
71480c2006SBryan Schumaker 	if (ctx != NULL) {
728ef2ce3eSBryan Schumaker 		ctx->duped = 0;
730c030806STrond Myklebust 		ctx->attr_gencount = NFS_I(dir)->attr_gencount;
74480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
758ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
76480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
77480c2006SBryan Schumaker 		return ctx;
78480c2006SBryan Schumaker 	}
790c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
800c030806STrond Myklebust }
81480c2006SBryan Schumaker 
82480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
83480c2006SBryan Schumaker {
84480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
85480c2006SBryan Schumaker 	kfree(ctx);
86480c2006SBryan Schumaker }
87480c2006SBryan Schumaker 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * Open file
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds static int
921da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
931da177e4SLinus Torvalds {
94480c2006SBryan Schumaker 	int res = 0;
95480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
96480c2006SBryan Schumaker 	struct rpc_cred *cred;
971da177e4SLinus Torvalds 
986da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
99cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
100cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
101cc0dd2d1SChuck Lever 
102cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1031e7cb3dcSChuck Lever 
104480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
105480c2006SBryan Schumaker 	if (IS_ERR(cred))
106480c2006SBryan Schumaker 		return PTR_ERR(cred);
1070c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
108480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
109480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
110480c2006SBryan Schumaker 		goto out;
111480c2006SBryan Schumaker 	}
112480c2006SBryan Schumaker 	filp->private_data = ctx;
113f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
114f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
115f5a73672SNeil Brown 		 * have been called, so we need to refresh the
116f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
117f5a73672SNeil Brown 		 */
118f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
119f5a73672SNeil Brown 	}
120480c2006SBryan Schumaker out:
121480c2006SBryan Schumaker 	put_rpccred(cred);
1221da177e4SLinus Torvalds 	return res;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
125480c2006SBryan Schumaker static int
126480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
127480c2006SBryan Schumaker {
128480c2006SBryan Schumaker 	put_nfs_open_dir_context(filp->private_data);
129480c2006SBryan Schumaker 	return 0;
130480c2006SBryan Schumaker }
131480c2006SBryan Schumaker 
132d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
133d1bacf9eSBryan Schumaker 	u64 cookie;
134d1bacf9eSBryan Schumaker 	u64 ino;
135d1bacf9eSBryan Schumaker 	struct qstr string;
1360b26a0bfSTrond Myklebust 	unsigned char d_type;
137d1bacf9eSBryan Schumaker };
138d1bacf9eSBryan Schumaker 
139d1bacf9eSBryan Schumaker struct nfs_cache_array {
14088b8e133SChuck Lever 	int size;
141d1bacf9eSBryan Schumaker 	int eof_index;
142d1bacf9eSBryan Schumaker 	u64 last_cookie;
143d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
144d1bacf9eSBryan Schumaker };
145d1bacf9eSBryan Schumaker 
146573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1471da177e4SLinus Torvalds typedef struct {
1481da177e4SLinus Torvalds 	struct file	*file;
1491da177e4SLinus Torvalds 	struct page	*page;
1501da177e4SLinus Torvalds 	unsigned long	page_index;
151f0dd2136STrond Myklebust 	u64		*dir_cookie;
1520aded708STrond Myklebust 	u64		last_cookie;
153f0dd2136STrond Myklebust 	loff_t		current_index;
1541da177e4SLinus Torvalds 	decode_dirent_t	decode;
155d1bacf9eSBryan Schumaker 
1561f4eab7eSNeil Brown 	unsigned long	timestamp;
1574704f0e2STrond Myklebust 	unsigned long	gencount;
158d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
159d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
160d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1611da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1621da177e4SLinus Torvalds 
163d1bacf9eSBryan Schumaker /*
164d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds static
167d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1681da177e4SLinus Torvalds {
1698cd51a0cSTrond Myklebust 	void *ptr;
170d1bacf9eSBryan Schumaker 	if (page == NULL)
171d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1728cd51a0cSTrond Myklebust 	ptr = kmap(page);
1738cd51a0cSTrond Myklebust 	if (ptr == NULL)
1748cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1758cd51a0cSTrond Myklebust 	return ptr;
176d1bacf9eSBryan Schumaker }
177d1bacf9eSBryan Schumaker 
178d1bacf9eSBryan Schumaker static
179d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
180d1bacf9eSBryan Schumaker {
181d1bacf9eSBryan Schumaker 	kunmap(page);
182d1bacf9eSBryan Schumaker }
183d1bacf9eSBryan Schumaker 
184d1bacf9eSBryan Schumaker /*
185d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
186d1bacf9eSBryan Schumaker  */
187d1bacf9eSBryan Schumaker static
18811de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
189d1bacf9eSBryan Schumaker {
19011de3b11STrond Myklebust 	struct nfs_cache_array *array;
191d1bacf9eSBryan Schumaker 	int i;
1928cd51a0cSTrond Myklebust 
1932b86ce2dSCong Wang 	array = kmap_atomic(page);
194d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
195d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1962b86ce2dSCong Wang 	kunmap_atomic(array);
197d1bacf9eSBryan Schumaker }
198d1bacf9eSBryan Schumaker 
199d1bacf9eSBryan Schumaker /*
200d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
201d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
202d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
203d1bacf9eSBryan Schumaker  */
204d1bacf9eSBryan Schumaker static
2054a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
206d1bacf9eSBryan Schumaker {
207d1bacf9eSBryan Schumaker 	string->len = len;
208d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2094a201d6eSTrond Myklebust 	if (string->name == NULL)
2104a201d6eSTrond Myklebust 		return -ENOMEM;
21104e4bd1cSCatalin Marinas 	/*
21204e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
21304e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
21404e4bd1cSCatalin Marinas 	 */
21504e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2164a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2174a201d6eSTrond Myklebust 	return 0;
218d1bacf9eSBryan Schumaker }
219d1bacf9eSBryan Schumaker 
220d1bacf9eSBryan Schumaker static
221d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
222d1bacf9eSBryan Schumaker {
223d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2244a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2254a201d6eSTrond Myklebust 	int ret;
2264a201d6eSTrond Myklebust 
227d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
228d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
229d1bacf9eSBryan Schumaker 
2304a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2313020093fSTrond Myklebust 
2323020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2333020093fSTrond Myklebust 	ret = -ENOSPC;
2343020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2353020093fSTrond Myklebust 		goto out;
2363020093fSTrond Myklebust 
2374a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2384a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2390b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2404a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2414a201d6eSTrond Myklebust 	if (ret)
2424a201d6eSTrond Myklebust 		goto out;
243d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2448cd51a0cSTrond Myklebust 	array->size++;
24547c716cbSTrond Myklebust 	if (entry->eof != 0)
246d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2474a201d6eSTrond Myklebust out:
248d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2494a201d6eSTrond Myklebust 	return ret;
250d1bacf9eSBryan Schumaker }
251d1bacf9eSBryan Schumaker 
252d1bacf9eSBryan Schumaker static
253d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
254d1bacf9eSBryan Schumaker {
255d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
256d1bacf9eSBryan Schumaker 	unsigned int index;
257d1bacf9eSBryan Schumaker 
258d1bacf9eSBryan Schumaker 	if (diff < 0)
259d1bacf9eSBryan Schumaker 		goto out_eof;
260d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2618cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
262d1bacf9eSBryan Schumaker 			goto out_eof;
263d1bacf9eSBryan Schumaker 		return -EAGAIN;
264d1bacf9eSBryan Schumaker 	}
265d1bacf9eSBryan Schumaker 
266d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
267d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
268d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
269d1bacf9eSBryan Schumaker 	return 0;
270d1bacf9eSBryan Schumaker out_eof:
271d1bacf9eSBryan Schumaker 	desc->eof = 1;
272d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
273d1bacf9eSBryan Schumaker }
274d1bacf9eSBryan Schumaker 
275d1bacf9eSBryan Schumaker static
276d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
277d1bacf9eSBryan Schumaker {
278d1bacf9eSBryan Schumaker 	int i;
2798ef2ce3eSBryan Schumaker 	loff_t new_pos;
280d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
281d1bacf9eSBryan Schumaker 
282d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2838cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
284496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2850c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2860c030806STrond Myklebust 
2878ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2880c030806STrond Myklebust 			if (ctx->attr_gencount != nfsi->attr_gencount
2890c030806STrond Myklebust 			    || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
2900c030806STrond Myklebust 				ctx->duped = 0;
2910c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
2920c030806STrond Myklebust 			} else if (new_pos < desc->file->f_pos) {
2930c030806STrond Myklebust 				if (ctx->duped > 0
2940c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2950c030806STrond Myklebust 					if (printk_ratelimit()) {
2960c030806STrond Myklebust 						pr_notice("NFS: directory %s/%s contains a readdir loop."
2970c030806STrond Myklebust 								"Please contact your server vendor.  "
298374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
2990c030806STrond Myklebust 								desc->file->f_dentry->d_parent->d_name.name,
3000c030806STrond Myklebust 								desc->file->f_dentry->d_name.name,
301374e4e3eSBryan Schumaker 								array->array[i].string.name,
3020c030806STrond Myklebust 								*desc->dir_cookie);
3030c030806STrond Myklebust 					}
3040c030806STrond Myklebust 					status = -ELOOP;
3050c030806STrond Myklebust 					goto out;
3060c030806STrond Myklebust 				}
3078ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3080c030806STrond Myklebust 				ctx->duped = -1;
3098ef2ce3eSBryan Schumaker 			}
3108ef2ce3eSBryan Schumaker 			desc->file->f_pos = new_pos;
3118cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
31247c716cbSTrond Myklebust 			return 0;
3138cd51a0cSTrond Myklebust 		}
3148cd51a0cSTrond Myklebust 	}
31547c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
316d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
31718fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
31818fb5fe4STrond Myklebust 			desc->eof = 1;
319d1bacf9eSBryan Schumaker 	}
3200c030806STrond Myklebust out:
321d1bacf9eSBryan Schumaker 	return status;
322d1bacf9eSBryan Schumaker }
323d1bacf9eSBryan Schumaker 
324d1bacf9eSBryan Schumaker static
325d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
326d1bacf9eSBryan Schumaker {
327d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
32847c716cbSTrond Myklebust 	int status;
329d1bacf9eSBryan Schumaker 
330d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
331d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
332d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
333d1bacf9eSBryan Schumaker 		goto out;
334d1bacf9eSBryan Schumaker 	}
335d1bacf9eSBryan Schumaker 
336d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
337d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
338d1bacf9eSBryan Schumaker 	else
339d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
340d1bacf9eSBryan Schumaker 
34147c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3420aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
343e47c085aSTrond Myklebust 		desc->current_index += array->size;
34447c716cbSTrond Myklebust 		desc->page_index++;
34547c716cbSTrond Myklebust 	}
346d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
347d1bacf9eSBryan Schumaker out:
348d1bacf9eSBryan Schumaker 	return status;
349d1bacf9eSBryan Schumaker }
350d1bacf9eSBryan Schumaker 
351d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
352d1bacf9eSBryan Schumaker static
35356e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
354d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
355d1bacf9eSBryan Schumaker {
356480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
357480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3584704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3591da177e4SLinus Torvalds 	int		error;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds  again:
3621da177e4SLinus Torvalds 	timestamp = jiffies;
3634704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
36456e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3651da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3661da177e4SLinus Torvalds 	if (error < 0) {
3671da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3681da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3691da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3703a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3711da177e4SLinus Torvalds 			desc->plus = 0;
3721da177e4SLinus Torvalds 			goto again;
3731da177e4SLinus Torvalds 		}
3741da177e4SLinus Torvalds 		goto error;
3751da177e4SLinus Torvalds 	}
3761f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3774704f0e2STrond Myklebust 	desc->gencount = gencount;
378d1bacf9eSBryan Schumaker error:
379d1bacf9eSBryan Schumaker 	return error;
380d1bacf9eSBryan Schumaker }
381d1bacf9eSBryan Schumaker 
382573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
383573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
384d1bacf9eSBryan Schumaker {
385573c4e1eSChuck Lever 	int error;
386d1bacf9eSBryan Schumaker 
387573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
388573c4e1eSChuck Lever 	if (error)
389573c4e1eSChuck Lever 		return error;
390d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
391d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
392d1bacf9eSBryan Schumaker 	return 0;
393d1bacf9eSBryan Schumaker }
394d1bacf9eSBryan Schumaker 
395d39ab9deSBryan Schumaker static
396d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
397d39ab9deSBryan Schumaker {
398d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
399d39ab9deSBryan Schumaker 		goto different;
40037a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
401d39ab9deSBryan Schumaker 		goto different;
402d39ab9deSBryan Schumaker 	return 1;
403d39ab9deSBryan Schumaker different:
404d39ab9deSBryan Schumaker 	return 0;
405d39ab9deSBryan Schumaker }
406d39ab9deSBryan Schumaker 
407d39ab9deSBryan Schumaker static
408d69ee9b8STrond Myklebust bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
409d69ee9b8STrond Myklebust {
410d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
411d69ee9b8STrond Myklebust 		return false;
412d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
413d69ee9b8STrond Myklebust 		return true;
414d69ee9b8STrond Myklebust 	if (filp->f_pos == 0)
415d69ee9b8STrond Myklebust 		return true;
416d69ee9b8STrond Myklebust 	return false;
417d69ee9b8STrond Myklebust }
418d69ee9b8STrond Myklebust 
419d69ee9b8STrond Myklebust /*
420d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
421d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
422d69ee9b8STrond Myklebust  * directory.
423d69ee9b8STrond Myklebust  */
424d69ee9b8STrond Myklebust static
425d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
426d69ee9b8STrond Myklebust {
427d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
428d69ee9b8STrond Myklebust }
429d69ee9b8STrond Myklebust 
430d69ee9b8STrond Myklebust static
431d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
432d39ab9deSBryan Schumaker {
43326fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4344a201d6eSTrond Myklebust 	struct dentry *dentry;
4354a201d6eSTrond Myklebust 	struct dentry *alias;
436d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
437d39ab9deSBryan Schumaker 	struct inode *inode;
438d39ab9deSBryan Schumaker 
4394a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4404a201d6eSTrond Myklebust 		if (filename.len == 1)
4414a201d6eSTrond Myklebust 			return;
4424a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4434a201d6eSTrond Myklebust 			return;
4444a201d6eSTrond Myklebust 	}
4454a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
446d39ab9deSBryan Schumaker 
4474a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
448d39ab9deSBryan Schumaker 	if (dentry != NULL) {
449d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
450d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
451d39ab9deSBryan Schumaker 			goto out;
452d39ab9deSBryan Schumaker 		} else {
453696199f8SAl Viro 			if (d_invalidate(dentry) != 0)
454696199f8SAl Viro 				goto out;
455d39ab9deSBryan Schumaker 			dput(dentry);
456d39ab9deSBryan Schumaker 		}
457d39ab9deSBryan Schumaker 	}
458d39ab9deSBryan Schumaker 
459d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4604a201d6eSTrond Myklebust 	if (dentry == NULL)
4614a201d6eSTrond Myklebust 		return;
4624a201d6eSTrond Myklebust 
4631775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
464d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
465d39ab9deSBryan Schumaker 		goto out;
466d39ab9deSBryan Schumaker 
467d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
468d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
469d39ab9deSBryan Schumaker 		goto out;
470d39ab9deSBryan Schumaker 	else if (alias) {
471d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
472d39ab9deSBryan Schumaker 		dput(alias);
473d39ab9deSBryan Schumaker 	} else
474d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
475d39ab9deSBryan Schumaker 
476d39ab9deSBryan Schumaker out:
477d39ab9deSBryan Schumaker 	dput(dentry);
478d39ab9deSBryan Schumaker }
479d39ab9deSBryan Schumaker 
480d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
481d1bacf9eSBryan Schumaker static
4828cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
4836650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
484d1bacf9eSBryan Schumaker {
485babddc72SBryan Schumaker 	struct xdr_stream stream;
486f7da7a12SBenny Halevy 	struct xdr_buf buf;
4876650239aSTrond Myklebust 	struct page *scratch;
48899424380SBryan Schumaker 	struct nfs_cache_array *array;
4895c346854STrond Myklebust 	unsigned int count = 0;
4905c346854STrond Myklebust 	int status;
491babddc72SBryan Schumaker 
4926650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
4936650239aSTrond Myklebust 	if (scratch == NULL)
4946650239aSTrond Myklebust 		return -ENOMEM;
495babddc72SBryan Schumaker 
496f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
4976650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
49899424380SBryan Schumaker 
49999424380SBryan Schumaker 	do {
50099424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5018cd51a0cSTrond Myklebust 		if (status != 0) {
5028cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5038cd51a0cSTrond Myklebust 				status = 0;
50499424380SBryan Schumaker 			break;
5058cd51a0cSTrond Myklebust 		}
50699424380SBryan Schumaker 
5075c346854STrond Myklebust 		count++;
5085c346854STrond Myklebust 
50947c716cbSTrond Myklebust 		if (desc->plus != 0)
510d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5118cd51a0cSTrond Myklebust 
5128cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5138cd51a0cSTrond Myklebust 		if (status != 0)
5148cd51a0cSTrond Myklebust 			break;
51599424380SBryan Schumaker 	} while (!entry->eof);
51699424380SBryan Schumaker 
51747c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
51899424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5198cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5208cd51a0cSTrond Myklebust 			array->eof_index = array->size;
52199424380SBryan Schumaker 			status = 0;
52299424380SBryan Schumaker 			nfs_readdir_release_array(page);
5235c346854STrond Myklebust 		} else
5245c346854STrond Myklebust 			status = PTR_ERR(array);
52556e4ebf8SBryan Schumaker 	}
5266650239aSTrond Myklebust 
5276650239aSTrond Myklebust 	put_page(scratch);
5288cd51a0cSTrond Myklebust 	return status;
5298cd51a0cSTrond Myklebust }
53056e4ebf8SBryan Schumaker 
53156e4ebf8SBryan Schumaker static
53256e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
53356e4ebf8SBryan Schumaker {
53456e4ebf8SBryan Schumaker 	unsigned int i;
53556e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
53656e4ebf8SBryan Schumaker 		put_page(pages[i]);
53756e4ebf8SBryan Schumaker }
53856e4ebf8SBryan Schumaker 
53956e4ebf8SBryan Schumaker static
54056e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
54156e4ebf8SBryan Schumaker 		unsigned int npages)
54256e4ebf8SBryan Schumaker {
54356e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
54456e4ebf8SBryan Schumaker }
54556e4ebf8SBryan Schumaker 
54656e4ebf8SBryan Schumaker /*
54756e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
54856e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
54956e4ebf8SBryan Schumaker  */
55056e4ebf8SBryan Schumaker static
5516650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
55256e4ebf8SBryan Schumaker {
55356e4ebf8SBryan Schumaker 	unsigned int i;
55456e4ebf8SBryan Schumaker 
55556e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
55656e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
55756e4ebf8SBryan Schumaker 		if (page == NULL)
55856e4ebf8SBryan Schumaker 			goto out_freepages;
55956e4ebf8SBryan Schumaker 		pages[i] = page;
56056e4ebf8SBryan Schumaker 	}
5616650239aSTrond Myklebust 	return 0;
56256e4ebf8SBryan Schumaker 
56356e4ebf8SBryan Schumaker out_freepages:
56456e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
5656650239aSTrond Myklebust 	return -ENOMEM;
566d1bacf9eSBryan Schumaker }
567d1bacf9eSBryan Schumaker 
568d1bacf9eSBryan Schumaker static
569d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
570d1bacf9eSBryan Schumaker {
57156e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
57256e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
573d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
574d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
575d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5768cd51a0cSTrond Myklebust 	int status = -ENOMEM;
57756e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
578d1bacf9eSBryan Schumaker 
579d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
5800aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
581d1bacf9eSBryan Schumaker 	entry.eof = 0;
582d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
583d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
584573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
585d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
586d1bacf9eSBryan Schumaker 		goto out;
587d1bacf9eSBryan Schumaker 
588*14c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
589*14c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
590*14c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
591*14c43f76SDavid Quigley 		goto out;
592*14c43f76SDavid Quigley 	}
593*14c43f76SDavid Quigley 
594d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
5958cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
5968cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
597*14c43f76SDavid Quigley 		goto out_label_free;
5988cd51a0cSTrond Myklebust 	}
599d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
600d1bacf9eSBryan Schumaker 	array->eof_index = -1;
601d1bacf9eSBryan Schumaker 
6026650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6036650239aSTrond Myklebust 	if (status < 0)
604d1bacf9eSBryan Schumaker 		goto out_release_array;
605d1bacf9eSBryan Schumaker 	do {
606ac396128STrond Myklebust 		unsigned int pglen;
60756e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
608babddc72SBryan Schumaker 
609d1bacf9eSBryan Schumaker 		if (status < 0)
610d1bacf9eSBryan Schumaker 			break;
611ac396128STrond Myklebust 		pglen = status;
6126650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6138cd51a0cSTrond Myklebust 		if (status < 0) {
6148cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6158cd51a0cSTrond Myklebust 				status = 0;
6168cd51a0cSTrond Myklebust 			break;
6178cd51a0cSTrond Myklebust 		}
6188cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
619d1bacf9eSBryan Schumaker 
62056e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
621d1bacf9eSBryan Schumaker out_release_array:
622d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
623*14c43f76SDavid Quigley out_label_free:
624*14c43f76SDavid Quigley 	nfs4_label_free(entry.label);
625d1bacf9eSBryan Schumaker out:
626d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
627d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
628d1bacf9eSBryan Schumaker 	return status;
629d1bacf9eSBryan Schumaker }
630d1bacf9eSBryan Schumaker 
631d1bacf9eSBryan Schumaker /*
632d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
633d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
634d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
635d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6361da177e4SLinus Torvalds  */
637d1bacf9eSBryan Schumaker static
638d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
639d1bacf9eSBryan Schumaker {
640496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6418cd51a0cSTrond Myklebust 	int ret;
642d1bacf9eSBryan Schumaker 
6438cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6448cd51a0cSTrond Myklebust 	if (ret < 0)
645d1bacf9eSBryan Schumaker 		goto error;
646d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
647d1bacf9eSBryan Schumaker 
6482aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
649cd9ae2b6STrond Myklebust 		/* Should never happen */
650cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
651cd9ae2b6STrond Myklebust 	}
6521da177e4SLinus Torvalds 	unlock_page(page);
6531da177e4SLinus Torvalds 	return 0;
6541da177e4SLinus Torvalds  error:
6551da177e4SLinus Torvalds 	unlock_page(page);
6568cd51a0cSTrond Myklebust 	return ret;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
659d1bacf9eSBryan Schumaker static
660d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6611da177e4SLinus Torvalds {
66211de3b11STrond Myklebust 	if (!desc->page->mapping)
66311de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6641da177e4SLinus Torvalds 	page_cache_release(desc->page);
6651da177e4SLinus Torvalds 	desc->page = NULL;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
668d1bacf9eSBryan Schumaker static
669d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6701da177e4SLinus Torvalds {
671496ad9aaSAl Viro 	return read_cache_page(file_inode(desc->file)->i_mapping,
672d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds /*
676d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6771da177e4SLinus Torvalds  */
678d1bacf9eSBryan Schumaker static
679d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
680d1bacf9eSBryan Schumaker {
681d1bacf9eSBryan Schumaker 	int res;
682d1bacf9eSBryan Schumaker 
683d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
684d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
685d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
686d1bacf9eSBryan Schumaker 
687d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
68847c716cbSTrond Myklebust 	if (res != 0)
689d1bacf9eSBryan Schumaker 		cache_page_release(desc);
690d1bacf9eSBryan Schumaker 	return res;
691d1bacf9eSBryan Schumaker }
692d1bacf9eSBryan Schumaker 
693d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
6941da177e4SLinus Torvalds static inline
6951da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
6961da177e4SLinus Torvalds {
6978cd51a0cSTrond Myklebust 	int res;
698d1bacf9eSBryan Schumaker 
6990aded708STrond Myklebust 	if (desc->page_index == 0) {
7008cd51a0cSTrond Myklebust 		desc->current_index = 0;
7010aded708STrond Myklebust 		desc->last_cookie = 0;
7020aded708STrond Myklebust 	}
70347c716cbSTrond Myklebust 	do {
704d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
70547c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7061da177e4SLinus Torvalds 	return res;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /*
7101da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7111da177e4SLinus Torvalds  */
7121da177e4SLinus Torvalds static
7131da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
7141da177e4SLinus Torvalds 		   filldir_t filldir)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	struct file	*file = desc->file;
717d1bacf9eSBryan Schumaker 	int i = 0;
718d1bacf9eSBryan Schumaker 	int res = 0;
719d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7208ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7218ef2ce3eSBryan Schumaker 
722d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
723e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
724e7c58e97STrond Myklebust 		res = PTR_ERR(array);
725e7c58e97STrond Myklebust 		goto out;
726e7c58e97STrond Myklebust 	}
7271da177e4SLinus Torvalds 
728d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
729ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7301da177e4SLinus Torvalds 
731ece0b423STrond Myklebust 		ent = &array->array[i];
732ece0b423STrond Myklebust 		if (filldir(dirent, ent->string.name, ent->string.len,
7330b26a0bfSTrond Myklebust 		    file->f_pos, nfs_compat_user_ino64(ent->ino),
7340b26a0bfSTrond Myklebust 		    ent->d_type) < 0) {
735ece0b423STrond Myklebust 			desc->eof = 1;
7361da177e4SLinus Torvalds 			break;
737ece0b423STrond Myklebust 		}
73800a92642SOlivier Galibert 		file->f_pos++;
739d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
740d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
741d1bacf9eSBryan Schumaker 		else
742d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7430c030806STrond Myklebust 		if (ctx->duped != 0)
7440c030806STrond Myklebust 			ctx->duped = 1;
7458cd51a0cSTrond Myklebust 	}
74647c716cbSTrond Myklebust 	if (array->eof_index >= 0)
747d1bacf9eSBryan Schumaker 		desc->eof = 1;
748d1bacf9eSBryan Schumaker 
749d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
750e7c58e97STrond Myklebust out:
751d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7521e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7531e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7541da177e4SLinus Torvalds 	return res;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds /*
7581da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7591da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7601da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7611da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7621da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7631da177e4SLinus Torvalds  *
7641da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7651da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7661da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7671da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7681da177e4SLinus Torvalds  */
7691da177e4SLinus Torvalds static inline
7701da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
7711da177e4SLinus Torvalds 		     filldir_t filldir)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	struct page	*page = NULL;
7741da177e4SLinus Torvalds 	int		status;
775496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
7760c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
7771da177e4SLinus Torvalds 
7781e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7791e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7821da177e4SLinus Torvalds 	if (!page) {
7831da177e4SLinus Torvalds 		status = -ENOMEM;
7841da177e4SLinus Torvalds 		goto out;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
7877a8e1dc3STrond Myklebust 	desc->page_index = 0;
7880aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
7897a8e1dc3STrond Myklebust 	desc->page = page;
7900c030806STrond Myklebust 	ctx->duped = 0;
7917a8e1dc3STrond Myklebust 
79285f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
79385f8607eSTrond Myklebust 	if (status < 0)
794d1bacf9eSBryan Schumaker 		goto out_release;
795d1bacf9eSBryan Schumaker 
7961da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds  out:
7991e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8003110ff80SHarvey Harrison 			__func__, status);
8011da177e4SLinus Torvalds 	return status;
8021da177e4SLinus Torvalds  out_release:
803d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8041da177e4SLinus Torvalds 	goto out;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
80700a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
80800a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
80900a92642SOlivier Galibert    whole directory.
8101da177e4SLinus Torvalds  */
8111da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
8121da177e4SLinus Torvalds {
81301cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
8141da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8151da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8161da177e4SLinus Torvalds 			*desc = &my_desc;
817480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
81847c716cbSTrond Myklebust 	int res;
8191da177e4SLinus Torvalds 
8206da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8211e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8221e7cb3dcSChuck Lever 			(long long)filp->f_pos);
82391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
82491d5b470SChuck Lever 
8251da177e4SLinus Torvalds 	/*
82600a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
827f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
82800a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
82900a92642SOlivier Galibert 	 * revalidate the cookie.
8301da177e4SLinus Torvalds 	 */
8311da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	desc->file = filp;
834480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8351da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
836d69ee9b8STrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
8371da177e4SLinus Torvalds 
838565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
8391cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
840fccca7fcSTrond Myklebust 	if (res < 0)
841fccca7fcSTrond Myklebust 		goto out;
842fccca7fcSTrond Myklebust 
84347c716cbSTrond Myklebust 	do {
8441da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
84500a92642SOlivier Galibert 
8461da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
847ece0b423STrond Myklebust 			res = 0;
8481da177e4SLinus Torvalds 			/* This means either end of directory */
849d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8501da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
8511da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
852ece0b423STrond Myklebust 				if (res == 0)
8531da177e4SLinus Torvalds 					continue;
8541da177e4SLinus Torvalds 			}
8551da177e4SLinus Torvalds 			break;
8561da177e4SLinus Torvalds 		}
8571da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8583a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8591da177e4SLinus Torvalds 			nfs_zap_caches(inode);
860baf57a09STrond Myklebust 			desc->page_index = 0;
8611da177e4SLinus Torvalds 			desc->plus = 0;
862d1bacf9eSBryan Schumaker 			desc->eof = 0;
8631da177e4SLinus Torvalds 			continue;
8641da177e4SLinus Torvalds 		}
8651da177e4SLinus Torvalds 		if (res < 0)
8661da177e4SLinus Torvalds 			break;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
869ece0b423STrond Myklebust 		if (res < 0)
8701da177e4SLinus Torvalds 			break;
87147c716cbSTrond Myklebust 	} while (!desc->eof);
872fccca7fcSTrond Myklebust out:
873565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8741e7cb3dcSChuck Lever 	if (res > 0)
8751e7cb3dcSChuck Lever 		res = 0;
876aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8771e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8781e7cb3dcSChuck Lever 			res);
8791da177e4SLinus Torvalds 	return res;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
882965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
883f0dd2136STrond Myklebust {
884b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
885b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
886480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
887b84e06c5SChuck Lever 
8886da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
889b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
890b84e06c5SChuck Lever 			dentry->d_name.name,
891965c8e59SAndrew Morton 			offset, whence);
892b84e06c5SChuck Lever 
893b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
894965c8e59SAndrew Morton 	switch (whence) {
895f0dd2136STrond Myklebust 		case 1:
896f0dd2136STrond Myklebust 			offset += filp->f_pos;
897f0dd2136STrond Myklebust 		case 0:
898f0dd2136STrond Myklebust 			if (offset >= 0)
899f0dd2136STrond Myklebust 				break;
900f0dd2136STrond Myklebust 		default:
901f0dd2136STrond Myklebust 			offset = -EINVAL;
902f0dd2136STrond Myklebust 			goto out;
903f0dd2136STrond Myklebust 	}
904f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
905f0dd2136STrond Myklebust 		filp->f_pos = offset;
906480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9078ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
908f0dd2136STrond Myklebust 	}
909f0dd2136STrond Myklebust out:
910b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
911f0dd2136STrond Myklebust 	return offset;
912f0dd2136STrond Myklebust }
913f0dd2136STrond Myklebust 
9141da177e4SLinus Torvalds /*
9151da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9161da177e4SLinus Torvalds  * is a dummy operation.
9171da177e4SLinus Torvalds  */
91802c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
91902c24a82SJosef Bacik 			 int datasync)
9201da177e4SLinus Torvalds {
9217ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
92202c24a82SJosef Bacik 	struct inode *inode = dentry->d_inode;
9237ea80859SChristoph Hellwig 
9246da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9251e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9261e7cb3dcSChuck Lever 			datasync);
9271e7cb3dcSChuck Lever 
92802c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
92954917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
93002c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9311da177e4SLinus Torvalds 	return 0;
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
934bfc69a45STrond Myklebust /**
935bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
936bfc69a45STrond Myklebust  * @dir - pointer to directory inode
937bfc69a45STrond Myklebust  *
938bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
939bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
940bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
941bfc69a45STrond Myklebust  *
942bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
943bfc69a45STrond Myklebust  */
944bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
945bfc69a45STrond Myklebust {
946011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
947bfc69a45STrond Myklebust }
94889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
949bfc69a45STrond Myklebust 
9501da177e4SLinus Torvalds /*
9511da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9521da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9531da177e4SLinus Torvalds  * and may need to be looked up again.
9541da177e4SLinus Torvalds  */
955c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9581da177e4SLinus Torvalds 		return 1;
9594eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9604eec952eSTrond Myklebust 		return 0;
961f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9626ecc5e8fSTrond Myklebust 		return 0;
963f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
964f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
965f2c77f4eSTrond Myklebust 		return 0;
966f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
967f2c77f4eSTrond Myklebust 		return 0;
968f2c77f4eSTrond Myklebust 	return 1;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds /*
972a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
973a12802caSTrond Myklebust  * an O_EXCL create using this path component.
974a12802caSTrond Myklebust  */
975fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
976a12802caSTrond Myklebust {
977a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
978a12802caSTrond Myklebust 		return 0;
979fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
980a12802caSTrond Myklebust }
981a12802caSTrond Myklebust 
982a12802caSTrond Myklebust /*
9831d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9841d6757fbSTrond Myklebust  *
9851d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9861d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9871d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9881d6757fbSTrond Myklebust  *
9891d6757fbSTrond Myklebust  */
99065a0c149STrond Myklebust static
991fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
99465a0c149STrond Myklebust 	int ret;
9951da177e4SLinus Torvalds 
99636d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
9974e99a1ffSTrond Myklebust 		return 0;
9981da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
999fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10001da177e4SLinus Torvalds 		goto out_force;
10011da177e4SLinus Torvalds 	/* This is an open(2) */
1002fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1003fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10041da177e4SLinus Torvalds 		goto out_force;
100565a0c149STrond Myklebust out:
100665a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10071da177e4SLinus Torvalds out_force:
100865a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
100965a0c149STrond Myklebust 	if (ret != 0)
101065a0c149STrond Myklebust 		return ret;
101165a0c149STrond Myklebust 	goto out;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds /*
10151da177e4SLinus Torvalds  * We judge how long we want to trust negative
10161da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10171da177e4SLinus Torvalds  *
10181da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10191da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10201da177e4SLinus Torvalds  */
10211da177e4SLinus Torvalds static inline
10221da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1023fa3c56bbSAl Viro 		       unsigned int flags)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1026fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10271da177e4SLinus Torvalds 		return 0;
10284eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10294eec952eSTrond Myklebust 		return 1;
10301da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /*
10341da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10351da177e4SLinus Torvalds  * and we should check whether we can really trust that
10361da177e4SLinus Torvalds  * lookup.
10371da177e4SLinus Torvalds  *
10381da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10391da177e4SLinus Torvalds  * we have an inode!
10401da177e4SLinus Torvalds  *
10411da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10421da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10431da177e4SLinus Torvalds  */
10440b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	struct inode *dir;
10471da177e4SLinus Torvalds 	struct inode *inode;
10481da177e4SLinus Torvalds 	struct dentry *parent;
1049e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1050e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10511775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
10521da177e4SLinus Torvalds 	int error;
10531da177e4SLinus Torvalds 
1054fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
105534286d66SNick Piggin 		return -ECHILD;
105634286d66SNick Piggin 
10571da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10581da177e4SLinus Torvalds 	dir = parent->d_inode;
105991d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10601da177e4SLinus Torvalds 	inode = dentry->d_inode;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	if (!inode) {
1063fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
10641da177e4SLinus Torvalds 			goto out_bad;
1065d69ee9b8STrond Myklebust 		goto out_valid_noent;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10691e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10703110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10711e7cb3dcSChuck Lever 				dentry->d_name.name);
10721da177e4SLinus Torvalds 		goto out_bad;
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 
1075011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
107615860ab1STrond Myklebust 		goto out_set_verifier;
107715860ab1STrond Myklebust 
10781da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1079fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1080fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
10811da177e4SLinus Torvalds 			goto out_zap_parent;
10821da177e4SLinus Torvalds 		goto out_valid;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10861da177e4SLinus Torvalds 		goto out_bad;
10871da177e4SLinus Torvalds 
1088e1fb4d05STrond Myklebust 	error = -ENOMEM;
1089e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1090e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1091e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1092e1fb4d05STrond Myklebust 		goto out_error;
1093e1fb4d05STrond Myklebust 
1094*14c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
1095*14c43f76SDavid Quigley 	if (IS_ERR(label))
1096*14c43f76SDavid Quigley 		goto out_error;
1097*14c43f76SDavid Quigley 
10981775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
10991da177e4SLinus Torvalds 	if (error)
11001da177e4SLinus Torvalds 		goto out_bad;
1101e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11021da177e4SLinus Torvalds 		goto out_bad;
1103e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11041da177e4SLinus Torvalds 		goto out_bad;
11051da177e4SLinus Torvalds 
1106e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1107e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1108*14c43f76SDavid Quigley 	nfs4_label_free(label);
1109*14c43f76SDavid Quigley 
111015860ab1STrond Myklebust out_set_verifier:
1111cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11121da177e4SLinus Torvalds  out_valid:
1113d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1114d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1115d69ee9b8STrond Myklebust  out_valid_noent:
11161da177e4SLinus Torvalds 	dput(parent);
11171e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
11183110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11191e7cb3dcSChuck Lever 			dentry->d_name.name);
11201da177e4SLinus Torvalds 	return 1;
11211da177e4SLinus Torvalds out_zap_parent:
11221da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11231da177e4SLinus Torvalds  out_bad:
1124c44600c9SAl Viro 	nfs_free_fattr(fattr);
1125c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
1126*14c43f76SDavid Quigley 	nfs4_label_free(label);
1127a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11281da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11291da177e4SLinus Torvalds 		/* Purge readdir caches. */
11301da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11311da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11321da177e4SLinus Torvalds 		if (have_submounts(dentry))
11331da177e4SLinus Torvalds 			goto out_valid;
1134d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1135d9e80b7dSAl Viro 			goto out_valid;
11361da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 	d_drop(dentry);
11391da177e4SLinus Torvalds 	dput(parent);
11401e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11413110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11421e7cb3dcSChuck Lever 			dentry->d_name.name);
11431da177e4SLinus Torvalds 	return 0;
1144e1fb4d05STrond Myklebust out_error:
1145e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1146e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1147*14c43f76SDavid Quigley 	nfs4_label_free(label);
1148e1fb4d05STrond Myklebust 	dput(parent);
1149e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1150e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1151e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1152e1fb4d05STrond Myklebust 	return error;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds /*
1156ecf3d1f1SJeff Layton  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1157ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1158ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1159ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1160ecf3d1f1SJeff Layton  *
1161ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1162ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1163ecf3d1f1SJeff Layton  */
1164ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1165ecf3d1f1SJeff Layton {
1166ecf3d1f1SJeff Layton 	int error;
1167ecf3d1f1SJeff Layton 	struct inode *inode = dentry->d_inode;
1168ecf3d1f1SJeff Layton 
1169ecf3d1f1SJeff Layton 	/*
1170ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1171ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1172ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1173ecf3d1f1SJeff Layton 	 */
1174ecf3d1f1SJeff Layton 	if (!inode) {
1175ecf3d1f1SJeff Layton 		dfprintk(LOOKUPCACHE, "%s: %s/%s has negative inode\n",
1176ecf3d1f1SJeff Layton 				__func__, dentry->d_parent->d_name.name,
1177ecf3d1f1SJeff Layton 				dentry->d_name.name);
1178ecf3d1f1SJeff Layton 		return 1;
1179ecf3d1f1SJeff Layton 	}
1180ecf3d1f1SJeff Layton 
1181ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
1182ecf3d1f1SJeff Layton 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
1183ecf3d1f1SJeff Layton 				__func__, dentry->d_parent->d_name.name,
1184ecf3d1f1SJeff Layton 				dentry->d_name.name);
1185ecf3d1f1SJeff Layton 		return 0;
1186ecf3d1f1SJeff Layton 	}
1187ecf3d1f1SJeff Layton 
1188ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1189ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1190ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1191ecf3d1f1SJeff Layton 	return !error;
1192ecf3d1f1SJeff Layton }
1193ecf3d1f1SJeff Layton 
1194ecf3d1f1SJeff Layton /*
11951da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11961da177e4SLinus Torvalds  */
1197fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
11981da177e4SLinus Torvalds {
11991da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
12001da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
12011da177e4SLinus Torvalds 		dentry->d_flags);
12021da177e4SLinus Torvalds 
120377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
120477f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
120577f11192STrond Myklebust 		return 1;
120677f11192STrond Myklebust 
12071da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12081da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12091da177e4SLinus Torvalds 		return 1;
12101da177e4SLinus Torvalds 	}
12111da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12121da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12131da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12141da177e4SLinus Torvalds 		return 1;
12151da177e4SLinus Torvalds 	}
12161da177e4SLinus Torvalds 	return 0;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
12201f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12211b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12221b83d707STrond Myklebust {
12231b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12241f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12251f018458STrond Myklebust 	if (inode->i_nlink == 1)
12261f018458STrond Myklebust 		clear_nlink(inode);
12271f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12281b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12291b83d707STrond Myklebust }
12301b83d707STrond Myklebust 
12311da177e4SLinus Torvalds /*
12321da177e4SLinus Torvalds  * Called when the dentry loses inode.
12331da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12341da177e4SLinus Torvalds  */
12351da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12361da177e4SLinus Torvalds {
123783672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
123883672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
123983672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
124083672d39SNeil Brown 
12411da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1242e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12431f018458STrond Myklebust 		nfs_drop_nlink(inode);
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 	iput(inode);
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
1248b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1249b1942c5fSAl Viro {
1250b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1251b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1252b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1253b1942c5fSAl Viro 			WARN_ON(1);
1254b1942c5fSAl Viro 		else
1255b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1256b1942c5fSAl Viro 	}
1257b1942c5fSAl Viro }
1258b1942c5fSAl Viro 
1259f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12601da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1261ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
12621da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12631da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
126436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1265b1942c5fSAl Viro 	.d_release	= nfs_d_release,
12661da177e4SLinus Torvalds };
1267ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
12681da177e4SLinus Torvalds 
1269597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
12701da177e4SLinus Torvalds {
12711da177e4SLinus Torvalds 	struct dentry *res;
1272565277f6STrond Myklebust 	struct dentry *parent;
12731da177e4SLinus Torvalds 	struct inode *inode = NULL;
1274e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1275e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12761775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
12771da177e4SLinus Torvalds 	int error;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
12801da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
128191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12841da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12851da177e4SLinus Torvalds 		goto out;
12861da177e4SLinus Torvalds 
1287fd684071STrond Myklebust 	/*
1288fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1289fd684071STrond Myklebust 	 * but don't hash the dentry.
1290fd684071STrond Myklebust 	 */
129100cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1292fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1293fd684071STrond Myklebust 		res = NULL;
1294fc0f684cSTrond Myklebust 		goto out;
1295fd684071STrond Myklebust 	}
12961da177e4SLinus Torvalds 
1297e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1298e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1299e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1300e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1301e1fb4d05STrond Myklebust 		goto out;
1302e1fb4d05STrond Myklebust 
1303*14c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
1304*14c43f76SDavid Quigley 	if (IS_ERR(label))
1305*14c43f76SDavid Quigley 		goto out;
1306*14c43f76SDavid Quigley 
1307565277f6STrond Myklebust 	parent = dentry->d_parent;
1308565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1309565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
13101775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
13111da177e4SLinus Torvalds 	if (error == -ENOENT)
13121da177e4SLinus Torvalds 		goto no_entry;
13131da177e4SLinus Torvalds 	if (error < 0) {
13141da177e4SLinus Torvalds 		res = ERR_PTR(error);
1315565277f6STrond Myklebust 		goto out_unblock_sillyrename;
13161da177e4SLinus Torvalds 	}
13171775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1318bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
131903f28e3aSTrond Myklebust 	if (IS_ERR(res))
1320565277f6STrond Myklebust 		goto out_unblock_sillyrename;
132154ceac45SDavid Howells 
1322d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1323d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1324d69ee9b8STrond Myklebust 
13251da177e4SLinus Torvalds no_entry:
132654ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13279eaef27bSTrond Myklebust 	if (res != NULL) {
13289eaef27bSTrond Myklebust 		if (IS_ERR(res))
1329565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13301da177e4SLinus Torvalds 		dentry = res;
13319eaef27bSTrond Myklebust 	}
13321da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1333565277f6STrond Myklebust out_unblock_sillyrename:
1334565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
1335*14c43f76SDavid Quigley 	nfs4_label_free(label);
13361da177e4SLinus Torvalds out:
1337e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1338e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13391da177e4SLinus Torvalds 	return res;
13401da177e4SLinus Torvalds }
1341ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
13421da177e4SLinus Torvalds 
134389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
13440b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13451da177e4SLinus Torvalds 
1346f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13470ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13481da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13491da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
135036d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1351b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13521da177e4SLinus Torvalds };
135389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
13541da177e4SLinus Torvalds 
13558a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13568a5e929dSAl Viro {
13578a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13588a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
13598a5e929dSAl Viro 		res |= FMODE_READ;
13608a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
13618a5e929dSAl Viro 		res |= FMODE_WRITE;
13628a5e929dSAl Viro 	return res;
13638a5e929dSAl Viro }
13648a5e929dSAl Viro 
136551141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1366cd9a1c0eSTrond Myklebust {
13675ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1368cd9a1c0eSTrond Myklebust }
1369cd9a1c0eSTrond Myklebust 
1370cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1371cd9a1c0eSTrond Myklebust {
1372cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1373cd9a1c0eSTrond Myklebust 	return 0;
1374cd9a1c0eSTrond Myklebust }
1375cd9a1c0eSTrond Myklebust 
1376d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
13770dd2b474SMiklos Szeredi 			   struct dentry *dentry,
137830d90494SAl Viro 			   struct file *file, unsigned open_flags,
137947237687SAl Viro 			   int *opened)
1380cd9a1c0eSTrond Myklebust {
13810dd2b474SMiklos Szeredi 	int err;
13820dd2b474SMiklos Szeredi 
13830dd2b474SMiklos Szeredi 	if (ctx->dentry != dentry) {
13840dd2b474SMiklos Szeredi 		dput(ctx->dentry);
13850dd2b474SMiklos Szeredi 		ctx->dentry = dget(dentry);
13860dd2b474SMiklos Szeredi 	}
1387cd9a1c0eSTrond Myklebust 
1388cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1389cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
13900dd2b474SMiklos Szeredi 		err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1391d9585277SAl Viro 		if (err < 0)
1392cd9a1c0eSTrond Myklebust 			goto out;
1393cd9a1c0eSTrond Myklebust 	}
13940dd2b474SMiklos Szeredi 
139530d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
139630d90494SAl Viro 	if (err)
1397d9585277SAl Viro 		goto out;
139830d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
13990dd2b474SMiklos Szeredi 
1400cd9a1c0eSTrond Myklebust out:
1401cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1402d9585277SAl Viro 	return err;
1403cd9a1c0eSTrond Myklebust }
1404cd9a1c0eSTrond Myklebust 
140573a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
140630d90494SAl Viro 		    struct file *file, unsigned open_flags,
140747237687SAl Viro 		    umode_t mode, int *opened)
14081da177e4SLinus Torvalds {
1409cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
14100dd2b474SMiklos Szeredi 	struct dentry *res;
14110dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1412f46e0bd3STrond Myklebust 	struct inode *inode;
1413898f635cSTrond Myklebust 	int err;
14141da177e4SLinus Torvalds 
14150dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14160dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
14170dd2b474SMiklos Szeredi 
14180dd2b474SMiklos Szeredi 	dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
14191e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
14201e7cb3dcSChuck Lever 
14210dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14220dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14230dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14240dd2b474SMiklos Szeredi 			/*
14250dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14260dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14270dd2b474SMiklos Szeredi 			 * again
14280dd2b474SMiklos Szeredi 			 */
1429d9585277SAl Viro 			return -ENOENT;
14300dd2b474SMiklos Szeredi 		}
14311da177e4SLinus Torvalds 		goto no_open;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 
14340dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1435d9585277SAl Viro 		return -ENAMETOOLONG;
14361da177e4SLinus Torvalds 
14370dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1438536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14390dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14400dd2b474SMiklos Szeredi 	}
1441536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1442536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1443536e43d1STrond Myklebust 		attr.ia_size = 0;
1444cd9a1c0eSTrond Myklebust 	}
1445cd9a1c0eSTrond Myklebust 
14460dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14470dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14480dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1449d9585277SAl Viro 		goto out;
14500dd2b474SMiklos Szeredi 
1451f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14522b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
14530dd2b474SMiklos Szeredi 	d_drop(dentry);
1454f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1455f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1456cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
14570dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14580dd2b474SMiklos Szeredi 		switch (err) {
14591da177e4SLinus Torvalds 		case -ENOENT:
1460f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
14610dd2b474SMiklos Szeredi 			break;
14621788ea6eSJeff Layton 		case -EISDIR:
14636f926b5bSTrond Myklebust 		case -ENOTDIR:
14646f926b5bSTrond Myklebust 			goto no_open;
14651da177e4SLinus Torvalds 		case -ELOOP:
14660dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
14671da177e4SLinus Torvalds 				goto no_open;
14680dd2b474SMiklos Szeredi 			break;
14691da177e4SLinus Torvalds 			/* case -EINVAL: */
14701da177e4SLinus Torvalds 		default:
14710dd2b474SMiklos Szeredi 			break;
14721da177e4SLinus Torvalds 		}
14731da177e4SLinus Torvalds 		goto out;
14741da177e4SLinus Torvalds 	}
1475f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1476898f635cSTrond Myklebust 	if (res != NULL)
14770dd2b474SMiklos Szeredi 		dentry = res;
14780dd2b474SMiklos Szeredi 
14790dd2b474SMiklos Szeredi 	nfs_unblock_sillyrename(dentry->d_parent);
1480f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14810dd2b474SMiklos Szeredi 
148230d90494SAl Viro 	err = nfs_finish_open(ctx, dentry, file, open_flags, opened);
14830dd2b474SMiklos Szeredi 
14840dd2b474SMiklos Szeredi 	dput(res);
1485d9585277SAl Viro out:
1486d9585277SAl Viro 	return err;
14870dd2b474SMiklos Szeredi 
14881da177e4SLinus Torvalds no_open:
148900cd8dd3SAl Viro 	res = nfs_lookup(dir, dentry, 0);
14900dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
14910dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1492d9585277SAl Viro 		goto out;
14930dd2b474SMiklos Szeredi 
1494e45198a6SAl Viro 	return finish_no_open(file, res);
14951da177e4SLinus Torvalds }
149689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
14971da177e4SLinus Torvalds 
14980b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1501657e94b6SNick Piggin 	struct inode *inode;
15021da177e4SLinus Torvalds 	struct inode *dir;
150350de348cSMiklos Szeredi 	int ret = 0;
15041da177e4SLinus Torvalds 
1505fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1506657e94b6SNick Piggin 		return -ECHILD;
1507657e94b6SNick Piggin 
1508fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1509eda72afbSMiklos Szeredi 		goto no_open;
1510eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15115584c306STrond Myklebust 		goto no_open;
151249f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
151349f9a0faSTrond Myklebust 		goto no_open;
15142b484297STrond Myklebust 
1515eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
15161da177e4SLinus Torvalds 	parent = dget_parent(dentry);
15171da177e4SLinus Torvalds 	dir = parent->d_inode;
15182b484297STrond Myklebust 
15191da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15201da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15211da177e4SLinus Torvalds 	 */
1522216d5d06STrond Myklebust 	if (inode == NULL) {
1523fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1524216d5d06STrond Myklebust 			ret = 1;
15251da177e4SLinus Torvalds 		goto out;
1526216d5d06STrond Myklebust 	}
1527216d5d06STrond Myklebust 
15281da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
15291da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15305584c306STrond Myklebust 		goto no_open_dput;
15311da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1532fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
15335584c306STrond Myklebust 		goto no_open_dput;
15341da177e4SLinus Torvalds 
15350ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1536898f635cSTrond Myklebust 	ret = 1;
15370ef97dcfSMiklos Szeredi 
15381da177e4SLinus Torvalds out:
15391da177e4SLinus Torvalds 	dput(parent);
15401da177e4SLinus Torvalds 	return ret;
1541535918f1STrond Myklebust 
15425584c306STrond Myklebust no_open_dput:
15431da177e4SLinus Torvalds 	dput(parent);
15445584c306STrond Myklebust no_open:
15450b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1546c0204fd2STrond Myklebust }
1547c0204fd2STrond Myklebust 
15481da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds /*
15511da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15521da177e4SLinus Torvalds  */
15531da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15541775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
15551775fd3eSDavid Quigley 				struct nfs4_label *label)
15561da177e4SLinus Torvalds {
1557fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1558fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15591da177e4SLinus Torvalds 	struct inode *inode;
15601da177e4SLinus Torvalds 	int error = -EACCES;
15611da177e4SLinus Torvalds 
1562fab728e1STrond Myklebust 	d_drop(dentry);
1563fab728e1STrond Myklebust 
15641da177e4SLinus Torvalds 	/* We may have been initialized further down */
15651da177e4SLinus Torvalds 	if (dentry->d_inode)
1566fab728e1STrond Myklebust 		goto out;
15671da177e4SLinus Torvalds 	if (fhandle->size == 0) {
15681775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
15691da177e4SLinus Torvalds 		if (error)
1570fab728e1STrond Myklebust 			goto out_error;
15711da177e4SLinus Torvalds 	}
15725724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15731da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15741da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15751775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
15761da177e4SLinus Torvalds 		if (error < 0)
1577fab728e1STrond Myklebust 			goto out_error;
15781da177e4SLinus Torvalds 	}
15791775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
158003f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
158103f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1582fab728e1STrond Myklebust 		goto out_error;
1583fab728e1STrond Myklebust 	d_add(dentry, inode);
1584fab728e1STrond Myklebust out:
1585fab728e1STrond Myklebust 	dput(parent);
15861da177e4SLinus Torvalds 	return 0;
1587fab728e1STrond Myklebust out_error:
1588fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1589fab728e1STrond Myklebust 	dput(parent);
1590fab728e1STrond Myklebust 	return error;
15911da177e4SLinus Torvalds }
1592ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds /*
15951da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15961da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15971da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15981da177e4SLinus Torvalds  * reply path made it appear to have failed.
15991da177e4SLinus Torvalds  */
1600597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1601ebfc3b49SAl Viro 		umode_t mode, bool excl)
16021da177e4SLinus Torvalds {
16031da177e4SLinus Torvalds 	struct iattr attr;
1604ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
16051da177e4SLinus Torvalds 	int error;
16061da177e4SLinus Torvalds 
16071e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
16081e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	attr.ia_mode = mode;
16111da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16121da177e4SLinus Torvalds 
16138867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16141da177e4SLinus Torvalds 	if (error != 0)
16151da177e4SLinus Torvalds 		goto out_err;
16161da177e4SLinus Torvalds 	return 0;
16171da177e4SLinus Torvalds out_err:
16181da177e4SLinus Torvalds 	d_drop(dentry);
16191da177e4SLinus Torvalds 	return error;
16201da177e4SLinus Torvalds }
1621ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds /*
16241da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16251da177e4SLinus Torvalds  */
1626597d9289SBryan Schumaker int
16271a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
16281da177e4SLinus Torvalds {
16291da177e4SLinus Torvalds 	struct iattr attr;
16301da177e4SLinus Torvalds 	int status;
16311da177e4SLinus Torvalds 
16321e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
16331e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16361da177e4SLinus Torvalds 		return -EINVAL;
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	attr.ia_mode = mode;
16391da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16421da177e4SLinus Torvalds 	if (status != 0)
16431da177e4SLinus Torvalds 		goto out_err;
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds out_err:
16461da177e4SLinus Torvalds 	d_drop(dentry);
16471da177e4SLinus Torvalds 	return status;
16481da177e4SLinus Torvalds }
1649ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds /*
16521da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16531da177e4SLinus Torvalds  */
1654597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16551da177e4SLinus Torvalds {
16561da177e4SLinus Torvalds 	struct iattr attr;
16571da177e4SLinus Torvalds 	int error;
16581da177e4SLinus Torvalds 
16591e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16601e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16631da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16661da177e4SLinus Torvalds 	if (error != 0)
16671da177e4SLinus Torvalds 		goto out_err;
16681da177e4SLinus Torvalds 	return 0;
16691da177e4SLinus Torvalds out_err:
16701da177e4SLinus Torvalds 	d_drop(dentry);
16711da177e4SLinus Torvalds 	return error;
16721da177e4SLinus Torvalds }
1673ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
16741da177e4SLinus Torvalds 
1675d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1676d45b9d8bSTrond Myklebust {
1677d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1678d45b9d8bSTrond Myklebust 		d_delete(dentry);
1679d45b9d8bSTrond Myklebust }
1680d45b9d8bSTrond Myklebust 
1681597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16821da177e4SLinus Torvalds {
16831da177e4SLinus Torvalds 	int error;
16841da177e4SLinus Torvalds 
16851e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16861e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16891da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
16901da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1691ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1692d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1693d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	return error;
16961da177e4SLinus Torvalds }
1697ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds /*
17001da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17011da177e4SLinus Torvalds  * and after checking that the file has only one user.
17021da177e4SLinus Torvalds  *
17031da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17041da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17051da177e4SLinus Torvalds  */
17061da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17071da177e4SLinus Torvalds {
17081da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17091da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17101da177e4SLinus Torvalds 	int error = -EBUSY;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
17131da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
17161da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17171da177e4SLinus Torvalds 		error = 0;
17181da177e4SLinus Torvalds 		goto out;
17191da177e4SLinus Torvalds 	}
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	if (inode != NULL) {
172257ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
17231da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17241da177e4SLinus Torvalds 		if (error == 0)
17251b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17261da177e4SLinus Torvalds 	} else
17271da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1728d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1729d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17301da177e4SLinus Torvalds out:
17311da177e4SLinus Torvalds 	return error;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17351da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17361da177e4SLinus Torvalds  *
17371da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17381da177e4SLinus Torvalds  */
1739597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
17401da177e4SLinus Torvalds {
17411da177e4SLinus Torvalds 	int error;
17421da177e4SLinus Torvalds 	int need_rehash = 0;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17451da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
1748b7ab39f6SNick Piggin 	if (dentry->d_count > 1) {
17491da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1750ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1751ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17521da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17531da177e4SLinus Torvalds 		return error;
17541da177e4SLinus Torvalds 	}
17551da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17561da177e4SLinus Torvalds 		__d_drop(dentry);
17571da177e4SLinus Torvalds 		need_rehash = 1;
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17601da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1761d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17621da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17631da177e4SLinus Torvalds 	} else if (need_rehash)
17641da177e4SLinus Torvalds 		d_rehash(dentry);
17651da177e4SLinus Torvalds 	return error;
17661da177e4SLinus Torvalds }
1767ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
17681da177e4SLinus Torvalds 
1769873101b3SChuck Lever /*
1770873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1771873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1772873101b3SChuck Lever  * using prepare_write/commit_write.
1773873101b3SChuck Lever  *
1774873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1775873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1776873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1777873101b3SChuck Lever  * symlink request has completed on the server.
1778873101b3SChuck Lever  *
1779873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1780873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1781873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1782873101b3SChuck Lever  * and move the raw page into its mapping.
1783873101b3SChuck Lever  */
1784597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17851da177e4SLinus Torvalds {
1786873101b3SChuck Lever 	struct pagevec lru_pvec;
1787873101b3SChuck Lever 	struct page *page;
1788873101b3SChuck Lever 	char *kaddr;
17891da177e4SLinus Torvalds 	struct iattr attr;
1790873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17911da177e4SLinus Torvalds 	int error;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
17941da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
17951da177e4SLinus Torvalds 
1796873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1797873101b3SChuck Lever 		return -ENAMETOOLONG;
17981da177e4SLinus Torvalds 
1799873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1800873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18011da177e4SLinus Torvalds 
180283d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
180376566991STrond Myklebust 	if (!page)
1804873101b3SChuck Lever 		return -ENOMEM;
1805873101b3SChuck Lever 
18062b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1807873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1808873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1809873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
18102b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1811873101b3SChuck Lever 
181294a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1813873101b3SChuck Lever 	if (error != 0) {
1814873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1815873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1816873101b3SChuck Lever 			dentry->d_name.name, symname, error);
18171da177e4SLinus Torvalds 		d_drop(dentry);
1818873101b3SChuck Lever 		__free_page(page);
18191da177e4SLinus Torvalds 		return error;
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 
1822873101b3SChuck Lever 	/*
1823873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1824873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1825873101b3SChuck Lever 	 */
1826873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1827873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1828873101b3SChuck Lever 							GFP_KERNEL)) {
182939cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
18304f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1831873101b3SChuck Lever 		SetPageUptodate(page);
1832873101b3SChuck Lever 		unlock_page(page);
1833873101b3SChuck Lever 	} else
1834873101b3SChuck Lever 		__free_page(page);
1835873101b3SChuck Lever 
1836873101b3SChuck Lever 	return 0;
1837873101b3SChuck Lever }
1838ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1839873101b3SChuck Lever 
1840597d9289SBryan Schumaker int
18411da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18421da177e4SLinus Torvalds {
18431da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18441da177e4SLinus Torvalds 	int error;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18471da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18481da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18491da177e4SLinus Torvalds 
185057ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
18519a3936aaSTrond Myklebust 
18529697d234STrond Myklebust 	d_drop(dentry);
18531da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1854cf809556STrond Myklebust 	if (error == 0) {
18557de9c6eeSAl Viro 		ihold(inode);
18569697d234STrond Myklebust 		d_add(dentry, inode);
1857cf809556STrond Myklebust 	}
18581da177e4SLinus Torvalds 	return error;
18591da177e4SLinus Torvalds }
1860ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds /*
18631da177e4SLinus Torvalds  * RENAME
18641da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18651da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18661da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18671da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18681da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18691da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18701da177e4SLinus Torvalds  *
18711da177e4SLinus Torvalds  * FIXED.
18721da177e4SLinus Torvalds  *
18731da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18741da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18751da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18761da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18771da177e4SLinus Torvalds  * using the inode layer
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18801da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18811da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18821da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18831da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18841da177e4SLinus Torvalds  * the rename.
18851da177e4SLinus Torvalds  */
1886597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18871da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18881da177e4SLinus Torvalds {
18891da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18901da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18911da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
18921da177e4SLinus Torvalds 	int error = -EBUSY;
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
18951da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18961da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1897b7ab39f6SNick Piggin 		 new_dentry->d_count);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	/*
190028f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
190128f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
190228f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
190328f79a1aSMiklos Szeredi 	 * the new target.
19041da177e4SLinus Torvalds 	 */
190527226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
190627226104SMiklos Szeredi 		/*
190727226104SMiklos Szeredi 		 * To prevent any new references to the target during the
190827226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
190927226104SMiklos Szeredi 		 */
191027226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
191127226104SMiklos Szeredi 			d_drop(new_dentry);
191227226104SMiklos Szeredi 			rehash = new_dentry;
191327226104SMiklos Szeredi 		}
191427226104SMiklos Szeredi 
1915b7ab39f6SNick Piggin 		if (new_dentry->d_count > 2) {
19161da177e4SLinus Torvalds 			int err;
191727226104SMiklos Szeredi 
19181da177e4SLinus Torvalds 			/* copy the target dentry's name */
19191da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19201da177e4SLinus Torvalds 					 &new_dentry->d_name);
19211da177e4SLinus Torvalds 			if (!dentry)
19221da177e4SLinus Torvalds 				goto out;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19251da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
192624e93025SMiklos Szeredi 			if (err)
19271da177e4SLinus Torvalds 				goto out;
192824e93025SMiklos Szeredi 
192924e93025SMiklos Szeredi 			new_dentry = dentry;
193056335936SOGAWA Hirofumi 			rehash = NULL;
193124e93025SMiklos Szeredi 			new_inode = NULL;
1932b1e4adf4STrond Myklebust 		}
193327226104SMiklos Szeredi 	}
19341da177e4SLinus Torvalds 
193557ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1936b1e4adf4STrond Myklebust 	if (new_inode != NULL)
193757ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19401da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19415ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19421da177e4SLinus Torvalds out:
19431da177e4SLinus Torvalds 	if (rehash)
19441da177e4SLinus Torvalds 		d_rehash(rehash);
19451da177e4SLinus Torvalds 	if (!error) {
1946b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1947b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19481da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19498fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19508fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1951d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1952d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	/* new dentry created? */
19551da177e4SLinus Torvalds 	if (dentry)
19561da177e4SLinus Torvalds 		dput(dentry);
19571da177e4SLinus Torvalds 	return error;
19581da177e4SLinus Torvalds }
1959ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
19601da177e4SLinus Torvalds 
1961cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1962cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1963cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1964cfcea3e8STrond Myklebust 
19651c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19661c3c07e9STrond Myklebust {
19671c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19681c3c07e9STrond Myklebust 	kfree(entry);
1969cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1970cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1971cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19721c3c07e9STrond Myklebust }
19731c3c07e9STrond Myklebust 
19741a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19751a81bb8aSTrond Myklebust {
19761a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19771a81bb8aSTrond Myklebust 
19781a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19791a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19801a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19811a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19821a81bb8aSTrond Myklebust 	}
19831a81bb8aSTrond Myklebust }
19841a81bb8aSTrond Myklebust 
19851495f230SYing Han int nfs_access_cache_shrinker(struct shrinker *shrink,
19861495f230SYing Han 			      struct shrink_control *sc)
1987979df72eSTrond Myklebust {
1988979df72eSTrond Myklebust 	LIST_HEAD(head);
1989aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1990979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
19911495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
19921495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
1993979df72eSTrond Myklebust 
199461d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
199561d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
19969c7e7e23STrond Myklebust 
1997a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1998aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1999979df72eSTrond Myklebust 		struct inode *inode;
2000979df72eSTrond Myklebust 
2001979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2002979df72eSTrond Myklebust 			break;
20039c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2004979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2005979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2006979df72eSTrond Myklebust 			goto remove_lru_entry;
2007979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2008979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2009979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2010979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
2011979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2012979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2013979df72eSTrond Myklebust 					&nfs_access_lru_list);
2014979df72eSTrond Myklebust 		else {
2015979df72eSTrond Myklebust remove_lru_entry:
2016979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
20179c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
2018979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
20199c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
2020979df72eSTrond Myklebust 		}
202159844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2022979df72eSTrond Myklebust 	}
2023979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20241a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
2025979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2026979df72eSTrond Myklebust }
2027979df72eSTrond Myklebust 
20281a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20291c3c07e9STrond Myklebust {
20301c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20311a81bb8aSTrond Myklebust 	struct rb_node *n;
20321c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20331c3c07e9STrond Myklebust 
20341c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20351c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20361c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20371c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20381a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20391c3c07e9STrond Myklebust 	}
20401c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20411c3c07e9STrond Myklebust }
20421c3c07e9STrond Myklebust 
20431c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20441c3c07e9STrond Myklebust {
20451a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20461a81bb8aSTrond Myklebust 
20471a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20481a81bb8aSTrond Myklebust 		return;
2049cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2050cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20511a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2052cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2053cfcea3e8STrond Myklebust 
20541c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20551a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20561a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20571a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20581a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20591c3c07e9STrond Myklebust }
20601c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
20611c3c07e9STrond Myklebust 
20621c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20631c3c07e9STrond Myklebust {
20641c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20651c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20661c3c07e9STrond Myklebust 
20671c3c07e9STrond Myklebust 	while (n != NULL) {
20681c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20691c3c07e9STrond Myklebust 
20701c3c07e9STrond Myklebust 		if (cred < entry->cred)
20711c3c07e9STrond Myklebust 			n = n->rb_left;
20721c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20731c3c07e9STrond Myklebust 			n = n->rb_right;
20741c3c07e9STrond Myklebust 		else
20751c3c07e9STrond Myklebust 			return entry;
20761c3c07e9STrond Myklebust 	}
20771c3c07e9STrond Myklebust 	return NULL;
20781c3c07e9STrond Myklebust }
20791c3c07e9STrond Myklebust 
2080af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20811da177e4SLinus Torvalds {
208255296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
20831c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
20841c3c07e9STrond Myklebust 	int err = -ENOENT;
20851da177e4SLinus Torvalds 
20861c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20871c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
20881c3c07e9STrond Myklebust 		goto out_zap;
20891c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20901c3c07e9STrond Myklebust 	if (cache == NULL)
20911c3c07e9STrond Myklebust 		goto out;
2092b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
209364672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
20941c3c07e9STrond Myklebust 		goto out_stale;
20951c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
20961c3c07e9STrond Myklebust 	res->cred = cache->cred;
20971c3c07e9STrond Myklebust 	res->mask = cache->mask;
2098cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
20991c3c07e9STrond Myklebust 	err = 0;
21001c3c07e9STrond Myklebust out:
21011c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21021c3c07e9STrond Myklebust 	return err;
21031c3c07e9STrond Myklebust out_stale:
21041c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2105cfcea3e8STrond Myklebust 	list_del(&cache->lru);
21061c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21071c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
21081da177e4SLinus Torvalds 	return -ENOENT;
21091c3c07e9STrond Myklebust out_zap:
21101a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21111a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
21121c3c07e9STrond Myklebust 	return -ENOENT;
21131c3c07e9STrond Myklebust }
21141c3c07e9STrond Myklebust 
21151c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
21161c3c07e9STrond Myklebust {
2117cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2118cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21191c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
21201c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
21211c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21221c3c07e9STrond Myklebust 
21231c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21241c3c07e9STrond Myklebust 	while (*p != NULL) {
21251c3c07e9STrond Myklebust 		parent = *p;
21261c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21271c3c07e9STrond Myklebust 
21281c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21291c3c07e9STrond Myklebust 			p = &parent->rb_left;
21301c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21311c3c07e9STrond Myklebust 			p = &parent->rb_right;
21321c3c07e9STrond Myklebust 		else
21331c3c07e9STrond Myklebust 			goto found;
21341c3c07e9STrond Myklebust 	}
21351c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21361c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2137cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21381c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21391c3c07e9STrond Myklebust 	return;
21401c3c07e9STrond Myklebust found:
21411c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2142cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2143cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21441c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21451c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
21486168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21491da177e4SLinus Torvalds {
21501c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21511c3c07e9STrond Myklebust 	if (cache == NULL)
21521c3c07e9STrond Myklebust 		return;
21531c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21541da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21551c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21561da177e4SLinus Torvalds 	cache->mask = set->mask;
21571c3c07e9STrond Myklebust 
21581c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2159cfcea3e8STrond Myklebust 
2160cfcea3e8STrond Myklebust 	/* Update accounting */
2161cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2162cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2163cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2164cfcea3e8STrond Myklebust 
2165cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21661a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2167cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21681a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21691a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21701a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2171cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2172cfcea3e8STrond Myklebust 	}
21731da177e4SLinus Torvalds }
21746168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
21756168f62cSWeston Andros Adamson 
21766168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
21776168f62cSWeston Andros Adamson {
21786168f62cSWeston Andros Adamson 	entry->mask = 0;
21796168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
21806168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
21816168f62cSWeston Andros Adamson 	if (access_result &
21826168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
21836168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
21846168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
21856168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
21866168f62cSWeston Andros Adamson }
21876168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21901da177e4SLinus Torvalds {
21911da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21921da177e4SLinus Torvalds 	int status;
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21951da177e4SLinus Torvalds 	if (status == 0)
21961da177e4SLinus Torvalds 		goto out;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
21991da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
22001da177e4SLinus Torvalds 	cache.cred = cred;
22011da177e4SLinus Torvalds 	cache.jiffies = jiffies;
22021da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2203a71ee337SSuresh Jayaraman 	if (status != 0) {
2204a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2205a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2206a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2207a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2208a71ee337SSuresh Jayaraman 		}
22091da177e4SLinus Torvalds 		return status;
2210a71ee337SSuresh Jayaraman 	}
22111da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
22121da177e4SLinus Torvalds out:
2213e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22141da177e4SLinus Torvalds 		return 0;
22151da177e4SLinus Torvalds 	return -EACCES;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
2218af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2219af22f94aSTrond Myklebust {
2220af22f94aSTrond Myklebust 	int mask = 0;
2221af22f94aSTrond Myklebust 
2222f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2223f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2224f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2225f8d9a897SWeston Andros Adamson 	} else {
22268a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2227af22f94aSTrond Myklebust 			mask |= MAY_READ;
22288a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2229af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2230f8d9a897SWeston Andros Adamson 	}
2231f8d9a897SWeston Andros Adamson 
2232af22f94aSTrond Myklebust 	return mask;
2233af22f94aSTrond Myklebust }
2234af22f94aSTrond Myklebust 
2235af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2236af22f94aSTrond Myklebust {
2237af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2238af22f94aSTrond Myklebust }
223989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2240af22f94aSTrond Myklebust 
224110556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds 	struct rpc_cred *cred;
22441da177e4SLinus Torvalds 	int res = 0;
22451da177e4SLinus Torvalds 
224610556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2247b74c79e9SNick Piggin 		return -ECHILD;
2248b74c79e9SNick Piggin 
224991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
225091d5b470SChuck Lever 
2251e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22521da177e4SLinus Torvalds 		goto out;
22531da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22549cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22551da177e4SLinus Torvalds 		goto force_lookup;
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22581da177e4SLinus Torvalds 		case S_IFLNK:
22591da177e4SLinus Torvalds 			goto out;
22601da177e4SLinus Torvalds 		case S_IFREG:
22611da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
22621da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
22637ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
22647ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
22651da177e4SLinus Torvalds 				goto out;
22661da177e4SLinus Torvalds 			break;
22671da177e4SLinus Torvalds 		case S_IFDIR:
22681da177e4SLinus Torvalds 			/*
22691da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22701da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22711da177e4SLinus Torvalds 			 */
22721da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22731da177e4SLinus Torvalds 				goto out;
22741da177e4SLinus Torvalds 	}
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds force_lookup:
22771da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22781da177e4SLinus Torvalds 		goto out_notsup;
22791da177e4SLinus Torvalds 
228098a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22811da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22821da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
22831da177e4SLinus Torvalds 		put_rpccred(cred);
22841da177e4SLinus Torvalds 	} else
22851da177e4SLinus Torvalds 		res = PTR_ERR(cred);
22861da177e4SLinus Torvalds out:
2287f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2288f696a365SMiklos Szeredi 		res = -EACCES;
2289f696a365SMiklos Szeredi 
22901e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
22911e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22921da177e4SLinus Torvalds 	return res;
22931da177e4SLinus Torvalds out_notsup:
22941da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22951da177e4SLinus Torvalds 	if (res == 0)
22962830ba7fSAl Viro 		res = generic_permission(inode, mask);
22971e7cb3dcSChuck Lever 	goto out;
22981da177e4SLinus Torvalds }
2299ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds /*
23021da177e4SLinus Torvalds  * Local variables:
23031da177e4SLinus Torvalds  *  version-control: t
23041da177e4SLinus Torvalds  *  kept-new-versions: 5
23051da177e4SLinus Torvalds  * End:
23061da177e4SLinus Torvalds  */
2307