xref: /openbmc/linux/fs/nfs/dir.c (revision 311324ad)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
301da177e4SLinus Torvalds #include <linux/nfs_fs.h>
311da177e4SLinus Torvalds #include <linux/nfs_mount.h>
321da177e4SLinus Torvalds #include <linux/pagemap.h>
33873101b3SChuck Lever #include <linux/pagevec.h>
341da177e4SLinus Torvalds #include <linux/namei.h>
3554ceac45SDavid Howells #include <linux/mount.h>
36a0b8cab3SMel Gorman #include <linux/swap.h>
37e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3804e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3964c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "delegation.h"
4291d5b470SChuck Lever #include "iostat.h"
434c30d56eSAdrian Bunk #include "internal.h"
44cd9a1c0eSTrond Myklebust #include "fscache.h"
451da177e4SLinus Torvalds 
46f4ce1299STrond Myklebust #include "nfstrace.h"
47f4ce1299STrond Myklebust 
481da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
51480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5223db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5302c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
54f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5511de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
561da177e4SLinus Torvalds 
574b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
58f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
591da177e4SLinus Torvalds 	.read		= generic_read_dir,
6023db8620SAl Viro 	.iterate	= nfs_readdir,
611da177e4SLinus Torvalds 	.open		= nfs_opendir,
62480c2006SBryan Schumaker 	.release	= nfs_closedir,
631da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
6611de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6711de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
68d1bacf9eSBryan Schumaker };
69d1bacf9eSBryan Schumaker 
700c030806STrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
71480c2006SBryan Schumaker {
72311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
73480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
74480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
75480c2006SBryan Schumaker 	if (ctx != NULL) {
768ef2ce3eSBryan Schumaker 		ctx->duped = 0;
77311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
78480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
798ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
80480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
81311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
82311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
83311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
84480c2006SBryan Schumaker 		return ctx;
85480c2006SBryan Schumaker 	}
860c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
870c030806STrond Myklebust }
88480c2006SBryan Schumaker 
89311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
90480c2006SBryan Schumaker {
91311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
92311324adSTrond Myklebust 	list_del(&ctx->list);
93311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
94480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
95480c2006SBryan Schumaker 	kfree(ctx);
96480c2006SBryan Schumaker }
97480c2006SBryan Schumaker 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Open file
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds static int
1021da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1031da177e4SLinus Torvalds {
104480c2006SBryan Schumaker 	int res = 0;
105480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
106480c2006SBryan Schumaker 	struct rpc_cred *cred;
1071da177e4SLinus Torvalds 
1086de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
109cc0dd2d1SChuck Lever 
110cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1111e7cb3dcSChuck Lever 
112480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
113480c2006SBryan Schumaker 	if (IS_ERR(cred))
114480c2006SBryan Schumaker 		return PTR_ERR(cred);
1150c030806STrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode, cred);
116480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
117480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
118480c2006SBryan Schumaker 		goto out;
119480c2006SBryan Schumaker 	}
120480c2006SBryan Schumaker 	filp->private_data = ctx;
121f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
122f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
123f5a73672SNeil Brown 		 * have been called, so we need to refresh the
124f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
125f5a73672SNeil Brown 		 */
126f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
127f5a73672SNeil Brown 	}
128480c2006SBryan Schumaker out:
129480c2006SBryan Schumaker 	put_rpccred(cred);
1301da177e4SLinus Torvalds 	return res;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133480c2006SBryan Schumaker static int
134480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
135480c2006SBryan Schumaker {
136311324adSTrond Myklebust 	put_nfs_open_dir_context(filp->f_path.dentry->d_inode, filp->private_data);
137480c2006SBryan Schumaker 	return 0;
138480c2006SBryan Schumaker }
139480c2006SBryan Schumaker 
140d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
141d1bacf9eSBryan Schumaker 	u64 cookie;
142d1bacf9eSBryan Schumaker 	u64 ino;
143d1bacf9eSBryan Schumaker 	struct qstr string;
1440b26a0bfSTrond Myklebust 	unsigned char d_type;
145d1bacf9eSBryan Schumaker };
146d1bacf9eSBryan Schumaker 
147d1bacf9eSBryan Schumaker struct nfs_cache_array {
14888b8e133SChuck Lever 	int size;
149d1bacf9eSBryan Schumaker 	int eof_index;
150d1bacf9eSBryan Schumaker 	u64 last_cookie;
151d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
152d1bacf9eSBryan Schumaker };
153d1bacf9eSBryan Schumaker 
154573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1551da177e4SLinus Torvalds typedef struct {
1561da177e4SLinus Torvalds 	struct file	*file;
1571da177e4SLinus Torvalds 	struct page	*page;
15823db8620SAl Viro 	struct dir_context *ctx;
1591da177e4SLinus Torvalds 	unsigned long	page_index;
160f0dd2136STrond Myklebust 	u64		*dir_cookie;
1610aded708STrond Myklebust 	u64		last_cookie;
162f0dd2136STrond Myklebust 	loff_t		current_index;
1631da177e4SLinus Torvalds 	decode_dirent_t	decode;
164d1bacf9eSBryan Schumaker 
1651f4eab7eSNeil Brown 	unsigned long	timestamp;
1664704f0e2STrond Myklebust 	unsigned long	gencount;
167d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
168d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
169d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1701da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1711da177e4SLinus Torvalds 
172d1bacf9eSBryan Schumaker /*
173d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds static
176d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1771da177e4SLinus Torvalds {
1788cd51a0cSTrond Myklebust 	void *ptr;
179d1bacf9eSBryan Schumaker 	if (page == NULL)
180d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
1818cd51a0cSTrond Myklebust 	ptr = kmap(page);
1828cd51a0cSTrond Myklebust 	if (ptr == NULL)
1838cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1848cd51a0cSTrond Myklebust 	return ptr;
185d1bacf9eSBryan Schumaker }
186d1bacf9eSBryan Schumaker 
187d1bacf9eSBryan Schumaker static
188d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
189d1bacf9eSBryan Schumaker {
190d1bacf9eSBryan Schumaker 	kunmap(page);
191d1bacf9eSBryan Schumaker }
192d1bacf9eSBryan Schumaker 
193d1bacf9eSBryan Schumaker /*
194d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
195d1bacf9eSBryan Schumaker  */
196d1bacf9eSBryan Schumaker static
19711de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
198d1bacf9eSBryan Schumaker {
19911de3b11STrond Myklebust 	struct nfs_cache_array *array;
200d1bacf9eSBryan Schumaker 	int i;
2018cd51a0cSTrond Myklebust 
2022b86ce2dSCong Wang 	array = kmap_atomic(page);
203d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
204d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
2052b86ce2dSCong Wang 	kunmap_atomic(array);
206d1bacf9eSBryan Schumaker }
207d1bacf9eSBryan Schumaker 
208d1bacf9eSBryan Schumaker /*
209d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
210d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
211d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
212d1bacf9eSBryan Schumaker  */
213d1bacf9eSBryan Schumaker static
2144a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
215d1bacf9eSBryan Schumaker {
216d1bacf9eSBryan Schumaker 	string->len = len;
217d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2184a201d6eSTrond Myklebust 	if (string->name == NULL)
2194a201d6eSTrond Myklebust 		return -ENOMEM;
22004e4bd1cSCatalin Marinas 	/*
22104e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
22204e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
22304e4bd1cSCatalin Marinas 	 */
22404e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2254a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2264a201d6eSTrond Myklebust 	return 0;
227d1bacf9eSBryan Schumaker }
228d1bacf9eSBryan Schumaker 
229d1bacf9eSBryan Schumaker static
230d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
231d1bacf9eSBryan Schumaker {
232d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2334a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2344a201d6eSTrond Myklebust 	int ret;
2354a201d6eSTrond Myklebust 
236d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
237d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
238d1bacf9eSBryan Schumaker 
2394a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2403020093fSTrond Myklebust 
2413020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2423020093fSTrond Myklebust 	ret = -ENOSPC;
2433020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2443020093fSTrond Myklebust 		goto out;
2453020093fSTrond Myklebust 
2464a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2474a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2480b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2494a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2504a201d6eSTrond Myklebust 	if (ret)
2514a201d6eSTrond Myklebust 		goto out;
252d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2538cd51a0cSTrond Myklebust 	array->size++;
25447c716cbSTrond Myklebust 	if (entry->eof != 0)
255d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2564a201d6eSTrond Myklebust out:
257d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2584a201d6eSTrond Myklebust 	return ret;
259d1bacf9eSBryan Schumaker }
260d1bacf9eSBryan Schumaker 
261d1bacf9eSBryan Schumaker static
262d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
263d1bacf9eSBryan Schumaker {
26423db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
265d1bacf9eSBryan Schumaker 	unsigned int index;
266d1bacf9eSBryan Schumaker 
267d1bacf9eSBryan Schumaker 	if (diff < 0)
268d1bacf9eSBryan Schumaker 		goto out_eof;
269d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2708cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
271d1bacf9eSBryan Schumaker 			goto out_eof;
272d1bacf9eSBryan Schumaker 		return -EAGAIN;
273d1bacf9eSBryan Schumaker 	}
274d1bacf9eSBryan Schumaker 
275d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
276d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
277d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
278d1bacf9eSBryan Schumaker 	return 0;
279d1bacf9eSBryan Schumaker out_eof:
280d1bacf9eSBryan Schumaker 	desc->eof = 1;
281d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
282d1bacf9eSBryan Schumaker }
283d1bacf9eSBryan Schumaker 
2844db72b40SJeff Layton static bool
2854db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2864db72b40SJeff Layton {
2874db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2884db72b40SJeff Layton 		return false;
2894db72b40SJeff Layton 	smp_rmb();
2904db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2914db72b40SJeff Layton }
2924db72b40SJeff Layton 
293d1bacf9eSBryan Schumaker static
294d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
295d1bacf9eSBryan Schumaker {
296d1bacf9eSBryan Schumaker 	int i;
2978ef2ce3eSBryan Schumaker 	loff_t new_pos;
298d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
299d1bacf9eSBryan Schumaker 
300d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3018cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
302496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
3030c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
3040c030806STrond Myklebust 
3058ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
3064db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
3074db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
3080c030806STrond Myklebust 				ctx->duped = 0;
3090c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
31023db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
3110c030806STrond Myklebust 				if (ctx->duped > 0
3120c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
3130c030806STrond Myklebust 					if (printk_ratelimit()) {
3146de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
3150c030806STrond Myklebust 								"Please contact your server vendor.  "
316374e4e3eSBryan Schumaker 								"The file: %s has duplicate cookie %llu\n",
3176de1472fSAl Viro 								desc->file,
318374e4e3eSBryan Schumaker 								array->array[i].string.name,
3190c030806STrond Myklebust 								*desc->dir_cookie);
3200c030806STrond Myklebust 					}
3210c030806STrond Myklebust 					status = -ELOOP;
3220c030806STrond Myklebust 					goto out;
3230c030806STrond Myklebust 				}
3248ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3250c030806STrond Myklebust 				ctx->duped = -1;
3268ef2ce3eSBryan Schumaker 			}
32723db8620SAl Viro 			desc->ctx->pos = new_pos;
3288cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
32947c716cbSTrond Myklebust 			return 0;
3308cd51a0cSTrond Myklebust 		}
3318cd51a0cSTrond Myklebust 	}
33247c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
333d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
33418fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
33518fb5fe4STrond Myklebust 			desc->eof = 1;
336d1bacf9eSBryan Schumaker 	}
3370c030806STrond Myklebust out:
338d1bacf9eSBryan Schumaker 	return status;
339d1bacf9eSBryan Schumaker }
340d1bacf9eSBryan Schumaker 
341d1bacf9eSBryan Schumaker static
342d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
343d1bacf9eSBryan Schumaker {
344d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
34547c716cbSTrond Myklebust 	int status;
346d1bacf9eSBryan Schumaker 
347d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
348d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
349d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
350d1bacf9eSBryan Schumaker 		goto out;
351d1bacf9eSBryan Schumaker 	}
352d1bacf9eSBryan Schumaker 
353d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
354d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
355d1bacf9eSBryan Schumaker 	else
356d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
357d1bacf9eSBryan Schumaker 
35847c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3590aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
360e47c085aSTrond Myklebust 		desc->current_index += array->size;
36147c716cbSTrond Myklebust 		desc->page_index++;
36247c716cbSTrond Myklebust 	}
363d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
364d1bacf9eSBryan Schumaker out:
365d1bacf9eSBryan Schumaker 	return status;
366d1bacf9eSBryan Schumaker }
367d1bacf9eSBryan Schumaker 
368d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
369d1bacf9eSBryan Schumaker static
37056e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
371d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
372d1bacf9eSBryan Schumaker {
373480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
374480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3754704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3761da177e4SLinus Torvalds 	int		error;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds  again:
3791da177e4SLinus Torvalds 	timestamp = jiffies;
3804704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
38156e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3821da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3831da177e4SLinus Torvalds 	if (error < 0) {
3841da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3851da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3861da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3873a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3881da177e4SLinus Torvalds 			desc->plus = 0;
3891da177e4SLinus Torvalds 			goto again;
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 		goto error;
3921da177e4SLinus Torvalds 	}
3931f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3944704f0e2STrond Myklebust 	desc->gencount = gencount;
395d1bacf9eSBryan Schumaker error:
396d1bacf9eSBryan Schumaker 	return error;
397d1bacf9eSBryan Schumaker }
398d1bacf9eSBryan Schumaker 
399573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
400573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
401d1bacf9eSBryan Schumaker {
402573c4e1eSChuck Lever 	int error;
403d1bacf9eSBryan Schumaker 
404573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
405573c4e1eSChuck Lever 	if (error)
406573c4e1eSChuck Lever 		return error;
407d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
408d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
409d1bacf9eSBryan Schumaker 	return 0;
410d1bacf9eSBryan Schumaker }
411d1bacf9eSBryan Schumaker 
412d39ab9deSBryan Schumaker static
413d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
414d39ab9deSBryan Schumaker {
415d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
416d39ab9deSBryan Schumaker 		goto different;
41737a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
418d39ab9deSBryan Schumaker 		goto different;
419d39ab9deSBryan Schumaker 	return 1;
420d39ab9deSBryan Schumaker different:
421d39ab9deSBryan Schumaker 	return 0;
422d39ab9deSBryan Schumaker }
423d39ab9deSBryan Schumaker 
424d39ab9deSBryan Schumaker static
42523db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
426d69ee9b8STrond Myklebust {
427d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
428d69ee9b8STrond Myklebust 		return false;
429d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
430d69ee9b8STrond Myklebust 		return true;
43123db8620SAl Viro 	if (ctx->pos == 0)
432d69ee9b8STrond Myklebust 		return true;
433d69ee9b8STrond Myklebust 	return false;
434d69ee9b8STrond Myklebust }
435d69ee9b8STrond Myklebust 
436d69ee9b8STrond Myklebust /*
437d69ee9b8STrond Myklebust  * This function is called by the lookup code to request the use of
438d69ee9b8STrond Myklebust  * readdirplus to accelerate any future lookups in the same
439d69ee9b8STrond Myklebust  * directory.
440d69ee9b8STrond Myklebust  */
441d69ee9b8STrond Myklebust static
442d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
443d69ee9b8STrond Myklebust {
444d69ee9b8STrond Myklebust 	set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
445d69ee9b8STrond Myklebust }
446d69ee9b8STrond Myklebust 
447311324adSTrond Myklebust /*
448311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
449311324adSTrond Myklebust  *
450311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
451311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
452311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
453311324adSTrond Myklebust  * cache flush.
454311324adSTrond Myklebust  */
455311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
456311324adSTrond Myklebust {
457311324adSTrond Myklebust 	if (!list_empty(&NFS_I(dir)->open_files)) {
458311324adSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
459311324adSTrond Myklebust 		nfs_zap_mapping(dir, dir->i_mapping);
460311324adSTrond Myklebust 	}
461311324adSTrond Myklebust }
462311324adSTrond Myklebust 
463d69ee9b8STrond Myklebust static
464d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
465d39ab9deSBryan Schumaker {
46626fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4674a201d6eSTrond Myklebust 	struct dentry *dentry;
4684a201d6eSTrond Myklebust 	struct dentry *alias;
469d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
470d39ab9deSBryan Schumaker 	struct inode *inode;
471aa9c2669SDavid Quigley 	int status;
472d39ab9deSBryan Schumaker 
4734a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4744a201d6eSTrond Myklebust 		if (filename.len == 1)
4754a201d6eSTrond Myklebust 			return;
4764a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4774a201d6eSTrond Myklebust 			return;
4784a201d6eSTrond Myklebust 	}
4794a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
480d39ab9deSBryan Schumaker 
4814a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
482d39ab9deSBryan Schumaker 	if (dentry != NULL) {
483d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
484cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
485aa9c2669SDavid Quigley 			status = nfs_refresh_inode(dentry->d_inode, entry->fattr);
486aa9c2669SDavid Quigley 			if (!status)
487aa9c2669SDavid Quigley 				nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label);
488d39ab9deSBryan Schumaker 			goto out;
489d39ab9deSBryan Schumaker 		} else {
490696199f8SAl Viro 			if (d_invalidate(dentry) != 0)
491696199f8SAl Viro 				goto out;
492d39ab9deSBryan Schumaker 			dput(dentry);
493d39ab9deSBryan Schumaker 		}
494d39ab9deSBryan Schumaker 	}
495d39ab9deSBryan Schumaker 
496d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4974a201d6eSTrond Myklebust 	if (dentry == NULL)
4984a201d6eSTrond Myklebust 		return;
4994a201d6eSTrond Myklebust 
5001775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
501d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
502d39ab9deSBryan Schumaker 		goto out;
503d39ab9deSBryan Schumaker 
504d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
505d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
506d39ab9deSBryan Schumaker 		goto out;
507d39ab9deSBryan Schumaker 	else if (alias) {
508d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
509d39ab9deSBryan Schumaker 		dput(alias);
510d39ab9deSBryan Schumaker 	} else
511d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
512d39ab9deSBryan Schumaker 
513d39ab9deSBryan Schumaker out:
514d39ab9deSBryan Schumaker 	dput(dentry);
515d39ab9deSBryan Schumaker }
516d39ab9deSBryan Schumaker 
517d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
518d1bacf9eSBryan Schumaker static
5198cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5206650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
521d1bacf9eSBryan Schumaker {
522babddc72SBryan Schumaker 	struct xdr_stream stream;
523f7da7a12SBenny Halevy 	struct xdr_buf buf;
5246650239aSTrond Myklebust 	struct page *scratch;
52599424380SBryan Schumaker 	struct nfs_cache_array *array;
5265c346854STrond Myklebust 	unsigned int count = 0;
5275c346854STrond Myklebust 	int status;
528babddc72SBryan Schumaker 
5296650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5306650239aSTrond Myklebust 	if (scratch == NULL)
5316650239aSTrond Myklebust 		return -ENOMEM;
532babddc72SBryan Schumaker 
533f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5346650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
53599424380SBryan Schumaker 
53699424380SBryan Schumaker 	do {
53799424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5388cd51a0cSTrond Myklebust 		if (status != 0) {
5398cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5408cd51a0cSTrond Myklebust 				status = 0;
54199424380SBryan Schumaker 			break;
5428cd51a0cSTrond Myklebust 		}
54399424380SBryan Schumaker 
5445c346854STrond Myklebust 		count++;
5455c346854STrond Myklebust 
54647c716cbSTrond Myklebust 		if (desc->plus != 0)
547d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5488cd51a0cSTrond Myklebust 
5498cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5508cd51a0cSTrond Myklebust 		if (status != 0)
5518cd51a0cSTrond Myklebust 			break;
55299424380SBryan Schumaker 	} while (!entry->eof);
55399424380SBryan Schumaker 
55447c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
55599424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5568cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5578cd51a0cSTrond Myklebust 			array->eof_index = array->size;
55899424380SBryan Schumaker 			status = 0;
55999424380SBryan Schumaker 			nfs_readdir_release_array(page);
5605c346854STrond Myklebust 		} else
5615c346854STrond Myklebust 			status = PTR_ERR(array);
56256e4ebf8SBryan Schumaker 	}
5636650239aSTrond Myklebust 
5646650239aSTrond Myklebust 	put_page(scratch);
5658cd51a0cSTrond Myklebust 	return status;
5668cd51a0cSTrond Myklebust }
56756e4ebf8SBryan Schumaker 
56856e4ebf8SBryan Schumaker static
56956e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
57056e4ebf8SBryan Schumaker {
57156e4ebf8SBryan Schumaker 	unsigned int i;
57256e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
57356e4ebf8SBryan Schumaker 		put_page(pages[i]);
57456e4ebf8SBryan Schumaker }
57556e4ebf8SBryan Schumaker 
57656e4ebf8SBryan Schumaker static
57756e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
57856e4ebf8SBryan Schumaker 		unsigned int npages)
57956e4ebf8SBryan Schumaker {
58056e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
58156e4ebf8SBryan Schumaker }
58256e4ebf8SBryan Schumaker 
58356e4ebf8SBryan Schumaker /*
58456e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
58556e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
58656e4ebf8SBryan Schumaker  */
58756e4ebf8SBryan Schumaker static
5886650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
58956e4ebf8SBryan Schumaker {
59056e4ebf8SBryan Schumaker 	unsigned int i;
59156e4ebf8SBryan Schumaker 
59256e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
59356e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
59456e4ebf8SBryan Schumaker 		if (page == NULL)
59556e4ebf8SBryan Schumaker 			goto out_freepages;
59656e4ebf8SBryan Schumaker 		pages[i] = page;
59756e4ebf8SBryan Schumaker 	}
5986650239aSTrond Myklebust 	return 0;
59956e4ebf8SBryan Schumaker 
60056e4ebf8SBryan Schumaker out_freepages:
60156e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
6026650239aSTrond Myklebust 	return -ENOMEM;
603d1bacf9eSBryan Schumaker }
604d1bacf9eSBryan Schumaker 
605d1bacf9eSBryan Schumaker static
606d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
607d1bacf9eSBryan Schumaker {
60856e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
60956e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
610d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
611d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
612d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6138cd51a0cSTrond Myklebust 	int status = -ENOMEM;
61456e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
615d1bacf9eSBryan Schumaker 
616d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6170aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
618d1bacf9eSBryan Schumaker 	entry.eof = 0;
619d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
620d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
621573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
622d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
623d1bacf9eSBryan Schumaker 		goto out;
624d1bacf9eSBryan Schumaker 
62514c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
62614c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
62714c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
62814c43f76SDavid Quigley 		goto out;
62914c43f76SDavid Quigley 	}
63014c43f76SDavid Quigley 
631d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6328cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6338cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
63414c43f76SDavid Quigley 		goto out_label_free;
6358cd51a0cSTrond Myklebust 	}
636d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
637d1bacf9eSBryan Schumaker 	array->eof_index = -1;
638d1bacf9eSBryan Schumaker 
6396650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6406650239aSTrond Myklebust 	if (status < 0)
641d1bacf9eSBryan Schumaker 		goto out_release_array;
642d1bacf9eSBryan Schumaker 	do {
643ac396128STrond Myklebust 		unsigned int pglen;
64456e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
645babddc72SBryan Schumaker 
646d1bacf9eSBryan Schumaker 		if (status < 0)
647d1bacf9eSBryan Schumaker 			break;
648ac396128STrond Myklebust 		pglen = status;
6496650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6508cd51a0cSTrond Myklebust 		if (status < 0) {
6518cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6528cd51a0cSTrond Myklebust 				status = 0;
6538cd51a0cSTrond Myklebust 			break;
6548cd51a0cSTrond Myklebust 		}
6558cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
656d1bacf9eSBryan Schumaker 
65756e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
658d1bacf9eSBryan Schumaker out_release_array:
659d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
66014c43f76SDavid Quigley out_label_free:
66114c43f76SDavid Quigley 	nfs4_label_free(entry.label);
662d1bacf9eSBryan Schumaker out:
663d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
664d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
665d1bacf9eSBryan Schumaker 	return status;
666d1bacf9eSBryan Schumaker }
667d1bacf9eSBryan Schumaker 
668d1bacf9eSBryan Schumaker /*
669d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
670d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
671d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
672d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6731da177e4SLinus Torvalds  */
674d1bacf9eSBryan Schumaker static
675d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
676d1bacf9eSBryan Schumaker {
677496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6788cd51a0cSTrond Myklebust 	int ret;
679d1bacf9eSBryan Schumaker 
6808cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6818cd51a0cSTrond Myklebust 	if (ret < 0)
682d1bacf9eSBryan Schumaker 		goto error;
683d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
684d1bacf9eSBryan Schumaker 
6852aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
686cd9ae2b6STrond Myklebust 		/* Should never happen */
687cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
688cd9ae2b6STrond Myklebust 	}
6891da177e4SLinus Torvalds 	unlock_page(page);
6901da177e4SLinus Torvalds 	return 0;
6911da177e4SLinus Torvalds  error:
6921da177e4SLinus Torvalds 	unlock_page(page);
6938cd51a0cSTrond Myklebust 	return ret;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696d1bacf9eSBryan Schumaker static
697d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6981da177e4SLinus Torvalds {
69911de3b11STrond Myklebust 	if (!desc->page->mapping)
70011de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
7011da177e4SLinus Torvalds 	page_cache_release(desc->page);
7021da177e4SLinus Torvalds 	desc->page = NULL;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705d1bacf9eSBryan Schumaker static
706d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7071da177e4SLinus Torvalds {
708496ad9aaSAl Viro 	return read_cache_page(file_inode(desc->file)->i_mapping,
709d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /*
713d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7141da177e4SLinus Torvalds  */
715d1bacf9eSBryan Schumaker static
716d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
717d1bacf9eSBryan Schumaker {
718d1bacf9eSBryan Schumaker 	int res;
719d1bacf9eSBryan Schumaker 
720d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
721d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
722d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
723d1bacf9eSBryan Schumaker 
724d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
72547c716cbSTrond Myklebust 	if (res != 0)
726d1bacf9eSBryan Schumaker 		cache_page_release(desc);
727d1bacf9eSBryan Schumaker 	return res;
728d1bacf9eSBryan Schumaker }
729d1bacf9eSBryan Schumaker 
730d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7311da177e4SLinus Torvalds static inline
7321da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7331da177e4SLinus Torvalds {
7348cd51a0cSTrond Myklebust 	int res;
735d1bacf9eSBryan Schumaker 
7360aded708STrond Myklebust 	if (desc->page_index == 0) {
7378cd51a0cSTrond Myklebust 		desc->current_index = 0;
7380aded708STrond Myklebust 		desc->last_cookie = 0;
7390aded708STrond Myklebust 	}
74047c716cbSTrond Myklebust 	do {
741d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
74247c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7431da177e4SLinus Torvalds 	return res;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /*
7471da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static
75023db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	struct file	*file = desc->file;
753d1bacf9eSBryan Schumaker 	int i = 0;
754d1bacf9eSBryan Schumaker 	int res = 0;
755d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7568ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7578ef2ce3eSBryan Schumaker 
758d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
759e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
760e7c58e97STrond Myklebust 		res = PTR_ERR(array);
761e7c58e97STrond Myklebust 		goto out;
762e7c58e97STrond Myklebust 	}
7631da177e4SLinus Torvalds 
764d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
765ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7661da177e4SLinus Torvalds 
767ece0b423STrond Myklebust 		ent = &array->array[i];
76823db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
76923db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
770ece0b423STrond Myklebust 			desc->eof = 1;
7711da177e4SLinus Torvalds 			break;
772ece0b423STrond Myklebust 		}
77323db8620SAl Viro 		desc->ctx->pos++;
774d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
775d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
776d1bacf9eSBryan Schumaker 		else
777d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7780c030806STrond Myklebust 		if (ctx->duped != 0)
7790c030806STrond Myklebust 			ctx->duped = 1;
7808cd51a0cSTrond Myklebust 	}
78147c716cbSTrond Myklebust 	if (array->eof_index >= 0)
782d1bacf9eSBryan Schumaker 		desc->eof = 1;
783d1bacf9eSBryan Schumaker 
784d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
785e7c58e97STrond Myklebust out:
786d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7871e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7881e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7891da177e4SLinus Torvalds 	return res;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds /*
7931da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7941da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7951da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7961da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7971da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7981da177e4SLinus Torvalds  *
7991da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8001da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8011da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8021da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds static inline
80523db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	struct page	*page = NULL;
8081da177e4SLinus Torvalds 	int		status;
809496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8100c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8111da177e4SLinus Torvalds 
8121e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8131e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8161da177e4SLinus Torvalds 	if (!page) {
8171da177e4SLinus Torvalds 		status = -ENOMEM;
8181da177e4SLinus Torvalds 		goto out;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8217a8e1dc3STrond Myklebust 	desc->page_index = 0;
8220aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8237a8e1dc3STrond Myklebust 	desc->page = page;
8240c030806STrond Myklebust 	ctx->duped = 0;
8257a8e1dc3STrond Myklebust 
82685f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
82785f8607eSTrond Myklebust 	if (status < 0)
828d1bacf9eSBryan Schumaker 		goto out_release;
829d1bacf9eSBryan Schumaker 
83023db8620SAl Viro 	status = nfs_do_filldir(desc);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds  out:
8331e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8343110ff80SHarvey Harrison 			__func__, status);
8351da177e4SLinus Torvalds 	return status;
8361da177e4SLinus Torvalds  out_release:
837d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8381da177e4SLinus Torvalds 	goto out;
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
841311324adSTrond Myklebust static bool nfs_dir_mapping_need_revalidate(struct inode *dir)
842311324adSTrond Myklebust {
843311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
844311324adSTrond Myklebust 
845311324adSTrond Myklebust 	if (nfs_attribute_cache_expired(dir))
846311324adSTrond Myklebust 		return true;
847311324adSTrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
848311324adSTrond Myklebust 		return true;
849311324adSTrond Myklebust 	return false;
850311324adSTrond Myklebust }
851311324adSTrond Myklebust 
85200a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
85300a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
85400a92642SOlivier Galibert    whole directory.
8551da177e4SLinus Torvalds  */
85623db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8571da177e4SLinus Torvalds {
85823db8620SAl Viro 	struct dentry	*dentry = file->f_path.dentry;
8591da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8601da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8611da177e4SLinus Torvalds 			*desc = &my_desc;
86223db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
86307b5ce8eSScott Mayhew 	int res = 0;
8641da177e4SLinus Torvalds 
8656de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
8666de1472fSAl Viro 			file, (long long)ctx->pos);
86791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
86891d5b470SChuck Lever 
8691da177e4SLinus Torvalds 	/*
87023db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
871f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
87200a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
87300a92642SOlivier Galibert 	 * revalidate the cookie.
8741da177e4SLinus Torvalds 	 */
8751da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8761da177e4SLinus Torvalds 
87723db8620SAl Viro 	desc->file = file;
87823db8620SAl Viro 	desc->ctx = ctx;
879480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8801da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
88123db8620SAl Viro 	desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
8821da177e4SLinus Torvalds 
883565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
884311324adSTrond Myklebust 	if (ctx->pos == 0 || nfs_dir_mapping_need_revalidate(inode))
88523db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
886fccca7fcSTrond Myklebust 	if (res < 0)
887fccca7fcSTrond Myklebust 		goto out;
888fccca7fcSTrond Myklebust 
88947c716cbSTrond Myklebust 	do {
8901da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
89100a92642SOlivier Galibert 
8921da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
893ece0b423STrond Myklebust 			res = 0;
8941da177e4SLinus Torvalds 			/* This means either end of directory */
895d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8961da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
89723db8620SAl Viro 				res = uncached_readdir(desc);
898ece0b423STrond Myklebust 				if (res == 0)
8991da177e4SLinus Torvalds 					continue;
9001da177e4SLinus Torvalds 			}
9011da177e4SLinus Torvalds 			break;
9021da177e4SLinus Torvalds 		}
9031da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9043a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9051da177e4SLinus Torvalds 			nfs_zap_caches(inode);
906baf57a09STrond Myklebust 			desc->page_index = 0;
9071da177e4SLinus Torvalds 			desc->plus = 0;
908d1bacf9eSBryan Schumaker 			desc->eof = 0;
9091da177e4SLinus Torvalds 			continue;
9101da177e4SLinus Torvalds 		}
9111da177e4SLinus Torvalds 		if (res < 0)
9121da177e4SLinus Torvalds 			break;
9131da177e4SLinus Torvalds 
91423db8620SAl Viro 		res = nfs_do_filldir(desc);
915ece0b423STrond Myklebust 		if (res < 0)
9161da177e4SLinus Torvalds 			break;
91747c716cbSTrond Myklebust 	} while (!desc->eof);
918fccca7fcSTrond Myklebust out:
919565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
9201e7cb3dcSChuck Lever 	if (res > 0)
9211e7cb3dcSChuck Lever 		res = 0;
9226de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9231da177e4SLinus Torvalds 	return res;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
926965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
927f0dd2136STrond Myklebust {
9286de1472fSAl Viro 	struct inode *inode = file_inode(filp);
929480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
930b84e06c5SChuck Lever 
9316de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9326de1472fSAl Viro 			filp, offset, whence);
933b84e06c5SChuck Lever 
934b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
935965c8e59SAndrew Morton 	switch (whence) {
936f0dd2136STrond Myklebust 		case 1:
937f0dd2136STrond Myklebust 			offset += filp->f_pos;
938f0dd2136STrond Myklebust 		case 0:
939f0dd2136STrond Myklebust 			if (offset >= 0)
940f0dd2136STrond Myklebust 				break;
941f0dd2136STrond Myklebust 		default:
942f0dd2136STrond Myklebust 			offset = -EINVAL;
943f0dd2136STrond Myklebust 			goto out;
944f0dd2136STrond Myklebust 	}
945f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
946f0dd2136STrond Myklebust 		filp->f_pos = offset;
947480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9488ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
949f0dd2136STrond Myklebust 	}
950f0dd2136STrond Myklebust out:
951b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
952f0dd2136STrond Myklebust 	return offset;
953f0dd2136STrond Myklebust }
954f0dd2136STrond Myklebust 
9551da177e4SLinus Torvalds /*
9561da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9571da177e4SLinus Torvalds  * is a dummy operation.
9581da177e4SLinus Torvalds  */
95902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
96002c24a82SJosef Bacik 			 int datasync)
9611da177e4SLinus Torvalds {
9626de1472fSAl Viro 	struct inode *inode = file_inode(filp);
9637ea80859SChristoph Hellwig 
9646de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
9651e7cb3dcSChuck Lever 
96602c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
9676de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
96802c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
9691da177e4SLinus Torvalds 	return 0;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
972bfc69a45STrond Myklebust /**
973bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
974bfc69a45STrond Myklebust  * @dir - pointer to directory inode
975bfc69a45STrond Myklebust  *
976bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
977bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
978bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
979bfc69a45STrond Myklebust  *
980bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
981bfc69a45STrond Myklebust  */
982bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
983bfc69a45STrond Myklebust {
984011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
985bfc69a45STrond Myklebust }
98689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
987bfc69a45STrond Myklebust 
9881da177e4SLinus Torvalds /*
9891da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9901da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9911da177e4SLinus Torvalds  * and may need to be looked up again.
9921da177e4SLinus Torvalds  */
993c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9961da177e4SLinus Torvalds 		return 1;
9974eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9984eec952eSTrond Myklebust 		return 0;
999f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10006ecc5e8fSTrond Myklebust 		return 0;
1001f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1002f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1003f2c77f4eSTrond Myklebust 		return 0;
1004f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1005f2c77f4eSTrond Myklebust 		return 0;
1006f2c77f4eSTrond Myklebust 	return 1;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds /*
1010a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1011a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1012a12802caSTrond Myklebust  */
1013fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1014a12802caSTrond Myklebust {
1015a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1016a12802caSTrond Myklebust 		return 0;
1017fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1018a12802caSTrond Myklebust }
1019a12802caSTrond Myklebust 
1020a12802caSTrond Myklebust /*
10211d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10221d6757fbSTrond Myklebust  *
10231d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10241d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10251d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10261d6757fbSTrond Myklebust  *
10271d6757fbSTrond Myklebust  */
102865a0c149STrond Myklebust static
1029fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10301da177e4SLinus Torvalds {
10311da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
103265a0c149STrond Myklebust 	int ret;
10331da177e4SLinus Torvalds 
103436d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10354e99a1ffSTrond Myklebust 		return 0;
10361da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1037fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10381da177e4SLinus Torvalds 		goto out_force;
10391da177e4SLinus Torvalds 	/* This is an open(2) */
1040fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1041fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10421da177e4SLinus Torvalds 		goto out_force;
104365a0c149STrond Myklebust out:
104465a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10451da177e4SLinus Torvalds out_force:
104665a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
104765a0c149STrond Myklebust 	if (ret != 0)
104865a0c149STrond Myklebust 		return ret;
104965a0c149STrond Myklebust 	goto out;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
10531da177e4SLinus Torvalds  * We judge how long we want to trust negative
10541da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10551da177e4SLinus Torvalds  *
10561da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10571da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10581da177e4SLinus Torvalds  */
10591da177e4SLinus Torvalds static inline
10601da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1061fa3c56bbSAl Viro 		       unsigned int flags)
10621da177e4SLinus Torvalds {
10631da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1064fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10651da177e4SLinus Torvalds 		return 0;
10664eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10674eec952eSTrond Myklebust 		return 1;
10681da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds /*
10721da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10731da177e4SLinus Torvalds  * and we should check whether we can really trust that
10741da177e4SLinus Torvalds  * lookup.
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10771da177e4SLinus Torvalds  * we have an inode!
10781da177e4SLinus Torvalds  *
10791da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10801da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10811da177e4SLinus Torvalds  */
10820b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	struct inode *dir;
10851da177e4SLinus Torvalds 	struct inode *inode;
10861da177e4SLinus Torvalds 	struct dentry *parent;
1087e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1088e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10891775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
10901da177e4SLinus Torvalds 	int error;
10911da177e4SLinus Torvalds 
1092fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
109334286d66SNick Piggin 		return -ECHILD;
109434286d66SNick Piggin 
10951da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10961da177e4SLinus Torvalds 	dir = parent->d_inode;
109791d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10981da177e4SLinus Torvalds 	inode = dentry->d_inode;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	if (!inode) {
1101fa3c56bbSAl Viro 		if (nfs_neg_need_reval(dir, dentry, flags))
11021da177e4SLinus Torvalds 			goto out_bad;
1103d69ee9b8STrond Myklebust 		goto out_valid_noent;
11041da177e4SLinus Torvalds 	}
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
11076de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
11086de1472fSAl Viro 				__func__, dentry);
11091da177e4SLinus Torvalds 		goto out_bad;
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
1112011e2a7fSBryan Schumaker 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
111315860ab1STrond Myklebust 		goto out_set_verifier;
111415860ab1STrond Myklebust 
11151da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1116fa3c56bbSAl Viro 	if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1117fa3c56bbSAl Viro 		if (nfs_lookup_verify_inode(inode, flags))
11181da177e4SLinus Torvalds 			goto out_zap_parent;
11191da177e4SLinus Torvalds 		goto out_valid;
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11231da177e4SLinus Torvalds 		goto out_bad;
11241da177e4SLinus Torvalds 
1125e1fb4d05STrond Myklebust 	error = -ENOMEM;
1126e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1127e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1128e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1129e1fb4d05STrond Myklebust 		goto out_error;
1130e1fb4d05STrond Myklebust 
113114c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
113214c43f76SDavid Quigley 	if (IS_ERR(label))
113314c43f76SDavid Quigley 		goto out_error;
113414c43f76SDavid Quigley 
11356e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
11361775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
11376e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
11381da177e4SLinus Torvalds 	if (error)
11391da177e4SLinus Torvalds 		goto out_bad;
1140e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11411da177e4SLinus Torvalds 		goto out_bad;
1142e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11431da177e4SLinus Torvalds 		goto out_bad;
11441da177e4SLinus Torvalds 
1145aa9c2669SDavid Quigley 	nfs_setsecurity(inode, fattr, label);
1146aa9c2669SDavid Quigley 
1147e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1148e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
114914c43f76SDavid Quigley 	nfs4_label_free(label);
115014c43f76SDavid Quigley 
115115860ab1STrond Myklebust out_set_verifier:
1152cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11531da177e4SLinus Torvalds  out_valid:
1154d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1155d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1156d69ee9b8STrond Myklebust  out_valid_noent:
11571da177e4SLinus Torvalds 	dput(parent);
11586de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11596de1472fSAl Viro 			__func__, dentry);
11601da177e4SLinus Torvalds 	return 1;
11611da177e4SLinus Torvalds out_zap_parent:
11621da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11631da177e4SLinus Torvalds  out_bad:
1164c44600c9SAl Viro 	nfs_free_fattr(fattr);
1165c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
116614c43f76SDavid Quigley 	nfs4_label_free(label);
1167a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11681da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11691da177e4SLinus Torvalds 		/* Purge readdir caches. */
11701da177e4SLinus Torvalds 		nfs_zap_caches(inode);
1171a3f432bfSJ. Bruce Fields 		/*
1172a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1173a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1174a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1175a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1176a3f432bfSJ. Bruce Fields 		 */
1177a3f432bfSJ. Bruce Fields 		if (IS_ROOT(dentry))
1178d9e80b7dSAl Viro 			goto out_valid;
11791da177e4SLinus Torvalds 	}
118013caa9fbSMiklos Szeredi 	/* If we have submounts, don't unhash ! */
118113caa9fbSMiklos Szeredi 	if (check_submounts_and_drop(dentry) != 0)
118213caa9fbSMiklos Szeredi 		goto out_valid;
118313caa9fbSMiklos Szeredi 
11841da177e4SLinus Torvalds 	dput(parent);
11856de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11866de1472fSAl Viro 			__func__, dentry);
11871da177e4SLinus Torvalds 	return 0;
1188e1fb4d05STrond Myklebust out_error:
1189e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1190e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
119114c43f76SDavid Quigley 	nfs4_label_free(label);
1192e1fb4d05STrond Myklebust 	dput(parent);
11936de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
11946de1472fSAl Viro 			__func__, dentry, error);
1195e1fb4d05STrond Myklebust 	return error;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds /*
1199ecf3d1f1SJeff Layton  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1200ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1201ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1202ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1203ecf3d1f1SJeff Layton  *
1204ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1205ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1206ecf3d1f1SJeff Layton  */
1207ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1208ecf3d1f1SJeff Layton {
1209ecf3d1f1SJeff Layton 	int error;
1210ecf3d1f1SJeff Layton 	struct inode *inode = dentry->d_inode;
1211ecf3d1f1SJeff Layton 
1212ecf3d1f1SJeff Layton 	/*
1213ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1214ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1215ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1216ecf3d1f1SJeff Layton 	 */
1217ecf3d1f1SJeff Layton 	if (!inode) {
12186de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
12196de1472fSAl Viro 				__func__, dentry);
1220ecf3d1f1SJeff Layton 		return 1;
1221ecf3d1f1SJeff Layton 	}
1222ecf3d1f1SJeff Layton 
1223ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
12246de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12256de1472fSAl Viro 				__func__, dentry);
1226ecf3d1f1SJeff Layton 		return 0;
1227ecf3d1f1SJeff Layton 	}
1228ecf3d1f1SJeff Layton 
1229ecf3d1f1SJeff Layton 	error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1230ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1231ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1232ecf3d1f1SJeff Layton 	return !error;
1233ecf3d1f1SJeff Layton }
1234ecf3d1f1SJeff Layton 
1235ecf3d1f1SJeff Layton /*
12361da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
12371da177e4SLinus Torvalds  */
1238fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
12391da177e4SLinus Torvalds {
12406de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
12416de1472fSAl Viro 		dentry, dentry->d_flags);
12421da177e4SLinus Torvalds 
124377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
124477f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
124577f11192STrond Myklebust 		return 1;
124677f11192STrond Myklebust 
12471da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12481da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12491da177e4SLinus Torvalds 		return 1;
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12521da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12531da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12541da177e4SLinus Torvalds 		return 1;
12551da177e4SLinus Torvalds 	}
12561da177e4SLinus Torvalds 	return 0;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
12601f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12611b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12621b83d707STrond Myklebust {
12631b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12641f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12651f018458STrond Myklebust 	if (inode->i_nlink == 1)
12661f018458STrond Myklebust 		clear_nlink(inode);
12671f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12681b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12691b83d707STrond Myklebust }
12701b83d707STrond Myklebust 
12711da177e4SLinus Torvalds /*
12721da177e4SLinus Torvalds  * Called when the dentry loses inode.
12731da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12741da177e4SLinus Torvalds  */
12751da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12761da177e4SLinus Torvalds {
127783672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
127883672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
127983672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
128083672d39SNeil Brown 
12811da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1282e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12831f018458STrond Myklebust 		nfs_drop_nlink(inode);
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 	iput(inode);
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
1288b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1289b1942c5fSAl Viro {
1290b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1291b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1292b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1293b1942c5fSAl Viro 			WARN_ON(1);
1294b1942c5fSAl Viro 		else
1295b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1296b1942c5fSAl Viro 	}
1297b1942c5fSAl Viro }
1298b1942c5fSAl Viro 
1299f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
13001da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1301ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
13021da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13031da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
130436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1305b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13061da177e4SLinus Torvalds };
1307ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
13081da177e4SLinus Torvalds 
1309597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
13101da177e4SLinus Torvalds {
13111da177e4SLinus Torvalds 	struct dentry *res;
1312565277f6STrond Myklebust 	struct dentry *parent;
13131da177e4SLinus Torvalds 	struct inode *inode = NULL;
1314e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1315e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
13161775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
13171da177e4SLinus Torvalds 	int error;
13181da177e4SLinus Torvalds 
13196de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
132091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
13231da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
13241da177e4SLinus Torvalds 		goto out;
13251da177e4SLinus Torvalds 
1326fd684071STrond Myklebust 	/*
1327fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1328fd684071STrond Myklebust 	 * but don't hash the dentry.
1329fd684071STrond Myklebust 	 */
133000cd8dd3SAl Viro 	if (nfs_is_exclusive_create(dir, flags)) {
1331fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1332fd684071STrond Myklebust 		res = NULL;
1333fc0f684cSTrond Myklebust 		goto out;
1334fd684071STrond Myklebust 	}
13351da177e4SLinus Torvalds 
1336e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1337e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1338e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1339e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1340e1fb4d05STrond Myklebust 		goto out;
1341e1fb4d05STrond Myklebust 
134214c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
134314c43f76SDavid Quigley 	if (IS_ERR(label))
134414c43f76SDavid Quigley 		goto out;
134514c43f76SDavid Quigley 
1346565277f6STrond Myklebust 	parent = dentry->d_parent;
1347565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
13486e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1349565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
13501775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
13511da177e4SLinus Torvalds 	if (error == -ENOENT)
13521da177e4SLinus Torvalds 		goto no_entry;
13531da177e4SLinus Torvalds 	if (error < 0) {
13541da177e4SLinus Torvalds 		res = ERR_PTR(error);
1355565277f6STrond Myklebust 		goto out_unblock_sillyrename;
13561da177e4SLinus Torvalds 	}
13571775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1358bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
135903f28e3aSTrond Myklebust 	if (IS_ERR(res))
1360565277f6STrond Myklebust 		goto out_unblock_sillyrename;
136154ceac45SDavid Howells 
1362d69ee9b8STrond Myklebust 	/* Success: notify readdir to use READDIRPLUS */
1363d69ee9b8STrond Myklebust 	nfs_advise_use_readdirplus(dir);
1364d69ee9b8STrond Myklebust 
13651da177e4SLinus Torvalds no_entry:
136654ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
13679eaef27bSTrond Myklebust 	if (res != NULL) {
13689eaef27bSTrond Myklebust 		if (IS_ERR(res))
1369565277f6STrond Myklebust 			goto out_unblock_sillyrename;
13701da177e4SLinus Torvalds 		dentry = res;
13719eaef27bSTrond Myklebust 	}
13721da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1373565277f6STrond Myklebust out_unblock_sillyrename:
1374565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
13756e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
137614c43f76SDavid Quigley 	nfs4_label_free(label);
13771da177e4SLinus Torvalds out:
1378e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1379e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13801da177e4SLinus Torvalds 	return res;
13811da177e4SLinus Torvalds }
1382ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
13831da177e4SLinus Torvalds 
138489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
13850b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13861da177e4SLinus Torvalds 
1387f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13880ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13891da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13901da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
139136d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1392b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13931da177e4SLinus Torvalds };
139489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
13951da177e4SLinus Torvalds 
13968a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
13978a5e929dSAl Viro {
13988a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
13998a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
14008a5e929dSAl Viro 		res |= FMODE_READ;
14018a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
14028a5e929dSAl Viro 		res |= FMODE_WRITE;
14038a5e929dSAl Viro 	return res;
14048a5e929dSAl Viro }
14058a5e929dSAl Viro 
140651141598SAl Viro static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1407cd9a1c0eSTrond Myklebust {
14085ede7b1cSAl Viro 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1409cd9a1c0eSTrond Myklebust }
1410cd9a1c0eSTrond Myklebust 
1411cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1412cd9a1c0eSTrond Myklebust {
1413f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1414cd9a1c0eSTrond Myklebust 	return 0;
1415cd9a1c0eSTrond Myklebust }
1416cd9a1c0eSTrond Myklebust 
1417d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
14180dd2b474SMiklos Szeredi 			   struct dentry *dentry,
141930d90494SAl Viro 			   struct file *file, unsigned open_flags,
142047237687SAl Viro 			   int *opened)
1421cd9a1c0eSTrond Myklebust {
14220dd2b474SMiklos Szeredi 	int err;
14230dd2b474SMiklos Szeredi 
142401c919abSMiklos Szeredi 	if ((open_flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
142501c919abSMiklos Szeredi 		*opened |= FILE_CREATED;
142601c919abSMiklos Szeredi 
142730d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
142830d90494SAl Viro 	if (err)
1429d9585277SAl Viro 		goto out;
143030d90494SAl Viro 	nfs_file_set_open_context(file, ctx);
14310dd2b474SMiklos Szeredi 
1432cd9a1c0eSTrond Myklebust out:
1433d9585277SAl Viro 	return err;
1434cd9a1c0eSTrond Myklebust }
1435cd9a1c0eSTrond Myklebust 
143673a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
143730d90494SAl Viro 		    struct file *file, unsigned open_flags,
143847237687SAl Viro 		    umode_t mode, int *opened)
14391da177e4SLinus Torvalds {
1440cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
14410dd2b474SMiklos Szeredi 	struct dentry *res;
14420dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1443f46e0bd3STrond Myklebust 	struct inode *inode;
14441472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1445898f635cSTrond Myklebust 	int err;
14461da177e4SLinus Torvalds 
14470dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14480dd2b474SMiklos Szeredi 	BUG_ON(dentry->d_inode);
14490dd2b474SMiklos Szeredi 
14501e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
14516de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
14521e7cb3dcSChuck Lever 
14539597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
14549597c13bSJeff Layton 	if (err)
14559597c13bSJeff Layton 		return err;
14569597c13bSJeff Layton 
14570dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14580dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
14590dd2b474SMiklos Szeredi 		if (!d_unhashed(dentry)) {
14600dd2b474SMiklos Szeredi 			/*
14610dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14620dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14630dd2b474SMiklos Szeredi 			 * again
14640dd2b474SMiklos Szeredi 			 */
1465d9585277SAl Viro 			return -ENOENT;
14660dd2b474SMiklos Szeredi 		}
14671472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
14681da177e4SLinus Torvalds 		goto no_open;
14691da177e4SLinus Torvalds 	}
14701da177e4SLinus Torvalds 
14710dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1472d9585277SAl Viro 		return -ENAMETOOLONG;
14731da177e4SLinus Torvalds 
14740dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1475536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
14760dd2b474SMiklos Szeredi 		attr.ia_mode = mode & ~current_umask();
14770dd2b474SMiklos Szeredi 	}
1478536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1479536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1480536e43d1STrond Myklebust 		attr.ia_size = 0;
1481cd9a1c0eSTrond Myklebust 	}
1482cd9a1c0eSTrond Myklebust 
14830dd2b474SMiklos Szeredi 	ctx = create_nfs_open_context(dentry, open_flags);
14840dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
14850dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1486d9585277SAl Viro 		goto out;
14870dd2b474SMiklos Szeredi 
14886e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
1489f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14905bc2afc2STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1491f46e0bd3STrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1492275bb307STrond Myklebust 	if (IS_ERR(inode)) {
14930dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
14946e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
14952d9db750STrond Myklebust 		put_nfs_open_context(ctx);
14960dd2b474SMiklos Szeredi 		switch (err) {
14971da177e4SLinus Torvalds 		case -ENOENT:
1498275bb307STrond Myklebust 			d_drop(dentry);
1499f46e0bd3STrond Myklebust 			d_add(dentry, NULL);
15000dd2b474SMiklos Szeredi 			break;
15011788ea6eSJeff Layton 		case -EISDIR:
15026f926b5bSTrond Myklebust 		case -ENOTDIR:
15036f926b5bSTrond Myklebust 			goto no_open;
15041da177e4SLinus Torvalds 		case -ELOOP:
15050dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
15061da177e4SLinus Torvalds 				goto no_open;
15070dd2b474SMiklos Szeredi 			break;
15081da177e4SLinus Torvalds 			/* case -EINVAL: */
15091da177e4SLinus Torvalds 		default:
15100dd2b474SMiklos Szeredi 			break;
15111da177e4SLinus Torvalds 		}
15121da177e4SLinus Torvalds 		goto out;
15131da177e4SLinus Torvalds 	}
15140dd2b474SMiklos Szeredi 
1515275bb307STrond Myklebust 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
15166e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15172d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1518d9585277SAl Viro out:
1519d9585277SAl Viro 	return err;
15200dd2b474SMiklos Szeredi 
15211da177e4SLinus Torvalds no_open:
15221472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
15230dd2b474SMiklos Szeredi 	err = PTR_ERR(res);
15240dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1525d9585277SAl Viro 		goto out;
15260dd2b474SMiklos Szeredi 
1527e45198a6SAl Viro 	return finish_no_open(file, res);
15281da177e4SLinus Torvalds }
152989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
15301da177e4SLinus Torvalds 
15310b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
15321da177e4SLinus Torvalds {
15331da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1534657e94b6SNick Piggin 	struct inode *inode;
15351da177e4SLinus Torvalds 	struct inode *dir;
153650de348cSMiklos Szeredi 	int ret = 0;
15371da177e4SLinus Torvalds 
1538fa3c56bbSAl Viro 	if (flags & LOOKUP_RCU)
1539657e94b6SNick Piggin 		return -ECHILD;
1540657e94b6SNick Piggin 
1541fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1542eda72afbSMiklos Szeredi 		goto no_open;
1543eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15445584c306STrond Myklebust 		goto no_open;
154549f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
154649f9a0faSTrond Myklebust 		goto no_open;
15472b484297STrond Myklebust 
1548eda72afbSMiklos Szeredi 	inode = dentry->d_inode;
15491da177e4SLinus Torvalds 	parent = dget_parent(dentry);
15501da177e4SLinus Torvalds 	dir = parent->d_inode;
15512b484297STrond Myklebust 
15521da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15531da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15541da177e4SLinus Torvalds 	 */
1555216d5d06STrond Myklebust 	if (inode == NULL) {
1556fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1557216d5d06STrond Myklebust 			ret = 1;
15581da177e4SLinus Torvalds 		goto out;
1559216d5d06STrond Myklebust 	}
1560216d5d06STrond Myklebust 
15611da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
15621da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15635584c306STrond Myklebust 		goto no_open_dput;
15641da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1565fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
15665584c306STrond Myklebust 		goto no_open_dput;
15671da177e4SLinus Torvalds 
15680ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1569898f635cSTrond Myklebust 	ret = 1;
15700ef97dcfSMiklos Szeredi 
15711da177e4SLinus Torvalds out:
15721da177e4SLinus Torvalds 	dput(parent);
15731da177e4SLinus Torvalds 	return ret;
1574535918f1STrond Myklebust 
15755584c306STrond Myklebust no_open_dput:
15761da177e4SLinus Torvalds 	dput(parent);
15775584c306STrond Myklebust no_open:
15780b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1579c0204fd2STrond Myklebust }
1580c0204fd2STrond Myklebust 
15811da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds /*
15841da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15851da177e4SLinus Torvalds  */
15861da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15871775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
15881775fd3eSDavid Quigley 				struct nfs4_label *label)
15891da177e4SLinus Torvalds {
1590fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1591fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15921da177e4SLinus Torvalds 	struct inode *inode;
15931da177e4SLinus Torvalds 	int error = -EACCES;
15941da177e4SLinus Torvalds 
1595fab728e1STrond Myklebust 	d_drop(dentry);
1596fab728e1STrond Myklebust 
15971da177e4SLinus Torvalds 	/* We may have been initialized further down */
15981da177e4SLinus Torvalds 	if (dentry->d_inode)
1599fab728e1STrond Myklebust 		goto out;
16001da177e4SLinus Torvalds 	if (fhandle->size == 0) {
16011775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
16021da177e4SLinus Torvalds 		if (error)
1603fab728e1STrond Myklebust 			goto out_error;
16041da177e4SLinus Torvalds 	}
16055724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16061da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
16071da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
16081775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
16091da177e4SLinus Torvalds 		if (error < 0)
1610fab728e1STrond Myklebust 			goto out_error;
16111da177e4SLinus Torvalds 	}
16121775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
161303f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
161403f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1615fab728e1STrond Myklebust 		goto out_error;
1616fab728e1STrond Myklebust 	d_add(dentry, inode);
1617fab728e1STrond Myklebust out:
1618fab728e1STrond Myklebust 	dput(parent);
16191da177e4SLinus Torvalds 	return 0;
1620fab728e1STrond Myklebust out_error:
1621fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1622fab728e1STrond Myklebust 	dput(parent);
1623fab728e1STrond Myklebust 	return error;
16241da177e4SLinus Torvalds }
1625ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds /*
16281da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
16291da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
16301da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
16311da177e4SLinus Torvalds  * reply path made it appear to have failed.
16321da177e4SLinus Torvalds  */
1633597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1634ebfc3b49SAl Viro 		umode_t mode, bool excl)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds 	struct iattr attr;
1637ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
16381da177e4SLinus Torvalds 	int error;
16391da177e4SLinus Torvalds 
16401e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
16416de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	attr.ia_mode = mode;
16441da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16451da177e4SLinus Torvalds 
16468b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
16478867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16488b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
16491da177e4SLinus Torvalds 	if (error != 0)
16501da177e4SLinus Torvalds 		goto out_err;
16511da177e4SLinus Torvalds 	return 0;
16521da177e4SLinus Torvalds out_err:
16531da177e4SLinus Torvalds 	d_drop(dentry);
16541da177e4SLinus Torvalds 	return error;
16551da177e4SLinus Torvalds }
1656ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds /*
16591da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16601da177e4SLinus Torvalds  */
1661597d9289SBryan Schumaker int
16621a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	struct iattr attr;
16651da177e4SLinus Torvalds 	int status;
16661da177e4SLinus Torvalds 
16671e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
16686de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16711da177e4SLinus Torvalds 		return -EINVAL;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	attr.ia_mode = mode;
16741da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16751da177e4SLinus Torvalds 
16761ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
16771da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16781ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
16791da177e4SLinus Torvalds 	if (status != 0)
16801da177e4SLinus Torvalds 		goto out_err;
16811da177e4SLinus Torvalds 	return 0;
16821da177e4SLinus Torvalds out_err:
16831da177e4SLinus Torvalds 	d_drop(dentry);
16841da177e4SLinus Torvalds 	return status;
16851da177e4SLinus Torvalds }
1686ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds /*
16891da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16901da177e4SLinus Torvalds  */
1691597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	struct iattr attr;
16941da177e4SLinus Torvalds 	int error;
16951da177e4SLinus Torvalds 
16961e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
16976de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17001da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
17011da177e4SLinus Torvalds 
17021ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
17031da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
17041ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
17051da177e4SLinus Torvalds 	if (error != 0)
17061da177e4SLinus Torvalds 		goto out_err;
17071da177e4SLinus Torvalds 	return 0;
17081da177e4SLinus Torvalds out_err:
17091da177e4SLinus Torvalds 	d_drop(dentry);
17101da177e4SLinus Torvalds 	return error;
17111da177e4SLinus Torvalds }
1712ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
17131da177e4SLinus Torvalds 
1714d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1715d45b9d8bSTrond Myklebust {
1716d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1717d45b9d8bSTrond Myklebust 		d_delete(dentry);
1718d45b9d8bSTrond Myklebust }
1719d45b9d8bSTrond Myklebust 
1720597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
17211da177e4SLinus Torvalds {
17221da177e4SLinus Torvalds 	int error;
17231da177e4SLinus Torvalds 
17241e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
17256de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17261da177e4SLinus Torvalds 
17271ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
1728ba6c0592STrond Myklebust 	if (dentry->d_inode) {
1729ba6c0592STrond Myklebust 		nfs_wait_on_sillyrename(dentry);
17301da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17311da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1732ba6c0592STrond Myklebust 		switch (error) {
1733ba6c0592STrond Myklebust 		case 0:
1734ce71ec36SDave Hansen 			clear_nlink(dentry->d_inode);
1735ba6c0592STrond Myklebust 			break;
1736ba6c0592STrond Myklebust 		case -ENOENT:
1737d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1738ba6c0592STrond Myklebust 		}
1739ba6c0592STrond Myklebust 	} else
1740ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17411ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	return error;
17441da177e4SLinus Torvalds }
1745ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds /*
17481da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17491da177e4SLinus Torvalds  * and after checking that the file has only one user.
17501da177e4SLinus Torvalds  *
17511da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17521da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17531da177e4SLinus Torvalds  */
17541da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17551da177e4SLinus Torvalds {
17561da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17571da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17581da177e4SLinus Torvalds 	int error = -EBUSY;
17591da177e4SLinus Torvalds 
17606de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
17631da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17641da177e4SLinus Torvalds 		error = 0;
17651da177e4SLinus Torvalds 		goto out;
17661da177e4SLinus Torvalds 	}
17671da177e4SLinus Torvalds 
17681ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
17691da177e4SLinus Torvalds 	if (inode != NULL) {
177057ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
17711da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17721da177e4SLinus Torvalds 		if (error == 0)
17731b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17741da177e4SLinus Torvalds 	} else
17751da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1776d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1777d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17781ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
17791da177e4SLinus Torvalds out:
17801da177e4SLinus Torvalds 	return error;
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17841da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17851da177e4SLinus Torvalds  *
17861da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17871da177e4SLinus Torvalds  */
1788597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	int error;
17911da177e4SLinus Torvalds 	int need_rehash = 0;
17921da177e4SLinus Torvalds 
17931e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
17946de1472fSAl Viro 		dir->i_ino, dentry);
17951da177e4SLinus Torvalds 
17961ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
17971da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
179884d08fa8SAl Viro 	if (d_count(dentry) > 1) {
17991da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1800ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1801ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
18021da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
18031ca42382STrond Myklebust 		goto out;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
18061da177e4SLinus Torvalds 		__d_drop(dentry);
18071da177e4SLinus Torvalds 		need_rehash = 1;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
18101da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1811d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
18121da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18131da177e4SLinus Torvalds 	} else if (need_rehash)
18141da177e4SLinus Torvalds 		d_rehash(dentry);
18151ca42382STrond Myklebust out:
18161ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
18171da177e4SLinus Torvalds 	return error;
18181da177e4SLinus Torvalds }
1819ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
18201da177e4SLinus Torvalds 
1821873101b3SChuck Lever /*
1822873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1823873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1824873101b3SChuck Lever  * using prepare_write/commit_write.
1825873101b3SChuck Lever  *
1826873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1827873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1828873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1829873101b3SChuck Lever  * symlink request has completed on the server.
1830873101b3SChuck Lever  *
1831873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1832873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1833873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1834873101b3SChuck Lever  * and move the raw page into its mapping.
1835873101b3SChuck Lever  */
1836597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18371da177e4SLinus Torvalds {
1838873101b3SChuck Lever 	struct page *page;
1839873101b3SChuck Lever 	char *kaddr;
18401da177e4SLinus Torvalds 	struct iattr attr;
1841873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
18421da177e4SLinus Torvalds 	int error;
18431da177e4SLinus Torvalds 
18441e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
18456de1472fSAl Viro 		dir->i_ino, dentry, symname);
18461da177e4SLinus Torvalds 
1847873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1848873101b3SChuck Lever 		return -ENAMETOOLONG;
18491da177e4SLinus Torvalds 
1850873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1851873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18521da177e4SLinus Torvalds 
185383d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
185476566991STrond Myklebust 	if (!page)
1855873101b3SChuck Lever 		return -ENOMEM;
1856873101b3SChuck Lever 
18572b86ce2dSCong Wang 	kaddr = kmap_atomic(page);
1858873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1859873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1860873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
18612b86ce2dSCong Wang 	kunmap_atomic(kaddr);
1862873101b3SChuck Lever 
18631ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
186494a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
18651ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1866873101b3SChuck Lever 	if (error != 0) {
18671e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1868873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
18696de1472fSAl Viro 			dentry, symname, error);
18701da177e4SLinus Torvalds 		d_drop(dentry);
1871873101b3SChuck Lever 		__free_page(page);
18721da177e4SLinus Torvalds 		return error;
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds 
1875873101b3SChuck Lever 	/*
1876873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1877873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1878873101b3SChuck Lever 	 */
1879a0b8cab3SMel Gorman 	if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
1880873101b3SChuck Lever 							GFP_KERNEL)) {
1881873101b3SChuck Lever 		SetPageUptodate(page);
1882873101b3SChuck Lever 		unlock_page(page);
1883873101b3SChuck Lever 	} else
1884873101b3SChuck Lever 		__free_page(page);
1885873101b3SChuck Lever 
1886873101b3SChuck Lever 	return 0;
1887873101b3SChuck Lever }
1888ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1889873101b3SChuck Lever 
1890597d9289SBryan Schumaker int
18911da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18941da177e4SLinus Torvalds 	int error;
18951da177e4SLinus Torvalds 
18966de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
18976de1472fSAl Viro 		old_dentry, dentry);
18981da177e4SLinus Torvalds 
18991fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
190057ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
19019a3936aaSTrond Myklebust 
19029697d234STrond Myklebust 	d_drop(dentry);
19031da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1904cf809556STrond Myklebust 	if (error == 0) {
19057de9c6eeSAl Viro 		ihold(inode);
19069697d234STrond Myklebust 		d_add(dentry, inode);
1907cf809556STrond Myklebust 	}
19081fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
19091da177e4SLinus Torvalds 	return error;
19101da177e4SLinus Torvalds }
1911ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds /*
19141da177e4SLinus Torvalds  * RENAME
19151da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
19161da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
19171da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
19181da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
19191da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
19201da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
19211da177e4SLinus Torvalds  *
19221da177e4SLinus Torvalds  * FIXED.
19231da177e4SLinus Torvalds  *
19241da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
19251da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
19261da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
19271da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
19281da177e4SLinus Torvalds  * using the inode layer
19291da177e4SLinus Torvalds  *
19301da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
19311da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
19321da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
19331da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
19341da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
19351da177e4SLinus Torvalds  * the rename.
19361da177e4SLinus Torvalds  */
1937597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
19381da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
19391da177e4SLinus Torvalds {
19401da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
19411da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
19421da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
19431da177e4SLinus Torvalds 	int error = -EBUSY;
19441da177e4SLinus Torvalds 
19456de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
19466de1472fSAl Viro 		 old_dentry, new_dentry,
194784d08fa8SAl Viro 		 d_count(new_dentry));
19481da177e4SLinus Torvalds 
194970ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
19501da177e4SLinus Torvalds 	/*
195128f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
195228f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
195328f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
195428f79a1aSMiklos Szeredi 	 * the new target.
19551da177e4SLinus Torvalds 	 */
195627226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
195727226104SMiklos Szeredi 		/*
195827226104SMiklos Szeredi 		 * To prevent any new references to the target during the
195927226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
196027226104SMiklos Szeredi 		 */
196127226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
196227226104SMiklos Szeredi 			d_drop(new_dentry);
196327226104SMiklos Szeredi 			rehash = new_dentry;
196427226104SMiklos Szeredi 		}
196527226104SMiklos Szeredi 
196684d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
19671da177e4SLinus Torvalds 			int err;
196827226104SMiklos Szeredi 
19691da177e4SLinus Torvalds 			/* copy the target dentry's name */
19701da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19711da177e4SLinus Torvalds 					 &new_dentry->d_name);
19721da177e4SLinus Torvalds 			if (!dentry)
19731da177e4SLinus Torvalds 				goto out;
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19761da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
197724e93025SMiklos Szeredi 			if (err)
19781da177e4SLinus Torvalds 				goto out;
197924e93025SMiklos Szeredi 
198024e93025SMiklos Szeredi 			new_dentry = dentry;
198156335936SOGAWA Hirofumi 			rehash = NULL;
198224e93025SMiklos Szeredi 			new_inode = NULL;
1983b1e4adf4STrond Myklebust 		}
198427226104SMiklos Szeredi 	}
19851da177e4SLinus Torvalds 
198657ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
1987b1e4adf4STrond Myklebust 	if (new_inode != NULL)
198857ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19911da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19925ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19931da177e4SLinus Torvalds out:
19941da177e4SLinus Torvalds 	if (rehash)
19951da177e4SLinus Torvalds 		d_rehash(rehash);
199670ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
199770ded201STrond Myklebust 			new_dir, new_dentry, error);
19981da177e4SLinus Torvalds 	if (!error) {
1999b1e4adf4STrond Myklebust 		if (new_inode != NULL)
2000b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
20011da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
20028fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
20038fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
2004d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
2005d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	/* new dentry created? */
20081da177e4SLinus Torvalds 	if (dentry)
20091da177e4SLinus Torvalds 		dput(dentry);
20101da177e4SLinus Torvalds 	return error;
20111da177e4SLinus Torvalds }
2012ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
20131da177e4SLinus Torvalds 
2014cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2015cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2016cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2017cfcea3e8STrond Myklebust 
20181c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
20191c3c07e9STrond Myklebust {
20201c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
20211c3c07e9STrond Myklebust 	kfree(entry);
2022cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
2023cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
2024cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
20251c3c07e9STrond Myklebust }
20261c3c07e9STrond Myklebust 
20271a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
20281a81bb8aSTrond Myklebust {
20291a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
20301a81bb8aSTrond Myklebust 
20311a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
20321a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
20331a81bb8aSTrond Myklebust 		list_del(&cache->lru);
20341a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
20351a81bb8aSTrond Myklebust 	}
20361a81bb8aSTrond Myklebust }
20371a81bb8aSTrond Myklebust 
20381ab6c499SDave Chinner unsigned long
20391ab6c499SDave Chinner nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2040979df72eSTrond Myklebust {
2041979df72eSTrond Myklebust 	LIST_HEAD(head);
2042aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2043979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
20441495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
20451495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
20461ab6c499SDave Chinner 	long freed = 0;
2047979df72eSTrond Myklebust 
204861d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
20491ab6c499SDave Chinner 		return SHRINK_STOP;
20509c7e7e23STrond Myklebust 
2051a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2052aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2053979df72eSTrond Myklebust 		struct inode *inode;
2054979df72eSTrond Myklebust 
2055979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2056979df72eSTrond Myklebust 			break;
20579c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2058979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2059979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2060979df72eSTrond Myklebust 			goto remove_lru_entry;
2061979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2062979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2063979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2064979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
20651ab6c499SDave Chinner 		freed++;
2066979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2067979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2068979df72eSTrond Myklebust 					&nfs_access_lru_list);
2069979df72eSTrond Myklebust 		else {
2070979df72eSTrond Myklebust remove_lru_entry:
2071979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
20729c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
2073979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
20749c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
2075979df72eSTrond Myklebust 		}
207659844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2077979df72eSTrond Myklebust 	}
2078979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20791a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20801ab6c499SDave Chinner 	return freed;
20811ab6c499SDave Chinner }
20821ab6c499SDave Chinner 
20831ab6c499SDave Chinner unsigned long
20841ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
20851ab6c499SDave Chinner {
208655f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2087979df72eSTrond Myklebust }
2088979df72eSTrond Myklebust 
20891a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20901c3c07e9STrond Myklebust {
20911c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20921a81bb8aSTrond Myklebust 	struct rb_node *n;
20931c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20941c3c07e9STrond Myklebust 
20951c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20961c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20971c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20981c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20991a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
21001c3c07e9STrond Myklebust 	}
21011c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
21021c3c07e9STrond Myklebust }
21031c3c07e9STrond Myklebust 
21041c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
21051c3c07e9STrond Myklebust {
21061a81bb8aSTrond Myklebust 	LIST_HEAD(head);
21071a81bb8aSTrond Myklebust 
21081a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
21091a81bb8aSTrond Myklebust 		return;
2110cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2111cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
21121a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2113cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2114cfcea3e8STrond Myklebust 
21151c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21161a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
21171a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21181a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
21191a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
21201c3c07e9STrond Myklebust }
21211c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
21221c3c07e9STrond Myklebust 
21231c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
21241c3c07e9STrond Myklebust {
21251c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
21261c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21271c3c07e9STrond Myklebust 
21281c3c07e9STrond Myklebust 	while (n != NULL) {
21291c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
21301c3c07e9STrond Myklebust 
21311c3c07e9STrond Myklebust 		if (cred < entry->cred)
21321c3c07e9STrond Myklebust 			n = n->rb_left;
21331c3c07e9STrond Myklebust 		else if (cred > entry->cred)
21341c3c07e9STrond Myklebust 			n = n->rb_right;
21351c3c07e9STrond Myklebust 		else
21361c3c07e9STrond Myklebust 			return entry;
21371c3c07e9STrond Myklebust 	}
21381c3c07e9STrond Myklebust 	return NULL;
21391c3c07e9STrond Myklebust }
21401c3c07e9STrond Myklebust 
2141af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
21421da177e4SLinus Torvalds {
214355296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
21441c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
21451c3c07e9STrond Myklebust 	int err = -ENOENT;
21461da177e4SLinus Torvalds 
21471c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21481c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
21491c3c07e9STrond Myklebust 		goto out_zap;
21501c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
21511c3c07e9STrond Myklebust 	if (cache == NULL)
21521c3c07e9STrond Myklebust 		goto out;
2153b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
215464672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
21551c3c07e9STrond Myklebust 		goto out_stale;
21561c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
21571c3c07e9STrond Myklebust 	res->cred = cache->cred;
21581c3c07e9STrond Myklebust 	res->mask = cache->mask;
2159cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
21601c3c07e9STrond Myklebust 	err = 0;
21611c3c07e9STrond Myklebust out:
21621c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21631c3c07e9STrond Myklebust 	return err;
21641c3c07e9STrond Myklebust out_stale:
21651c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2166cfcea3e8STrond Myklebust 	list_del(&cache->lru);
21671c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21681c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
21691da177e4SLinus Torvalds 	return -ENOENT;
21701c3c07e9STrond Myklebust out_zap:
21711a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21721a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
21731c3c07e9STrond Myklebust 	return -ENOENT;
21741c3c07e9STrond Myklebust }
21751c3c07e9STrond Myklebust 
21761c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
21771c3c07e9STrond Myklebust {
2178cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2179cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21801c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
21811c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
21821c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21831c3c07e9STrond Myklebust 
21841c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21851c3c07e9STrond Myklebust 	while (*p != NULL) {
21861c3c07e9STrond Myklebust 		parent = *p;
21871c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21881c3c07e9STrond Myklebust 
21891c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21901c3c07e9STrond Myklebust 			p = &parent->rb_left;
21911c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21921c3c07e9STrond Myklebust 			p = &parent->rb_right;
21931c3c07e9STrond Myklebust 		else
21941c3c07e9STrond Myklebust 			goto found;
21951c3c07e9STrond Myklebust 	}
21961c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21971c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2198cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21991c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
22001c3c07e9STrond Myklebust 	return;
22011c3c07e9STrond Myklebust found:
22021c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2203cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2204cfcea3e8STrond Myklebust 	list_del(&entry->lru);
22051c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
22061c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
22096168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
22101da177e4SLinus Torvalds {
22111c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
22121c3c07e9STrond Myklebust 	if (cache == NULL)
22131c3c07e9STrond Myklebust 		return;
22141c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
22151da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
22161c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
22171da177e4SLinus Torvalds 	cache->mask = set->mask;
22181c3c07e9STrond Myklebust 
22191c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2220cfcea3e8STrond Myklebust 
2221cfcea3e8STrond Myklebust 	/* Update accounting */
2222cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2223cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2224cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2225cfcea3e8STrond Myklebust 
2226cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
22271a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2228cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
22291a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
22301a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
22311a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2232cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2233cfcea3e8STrond Myklebust 	}
22341da177e4SLinus Torvalds }
22356168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
22366168f62cSWeston Andros Adamson 
22376168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
22386168f62cSWeston Andros Adamson {
22396168f62cSWeston Andros Adamson 	entry->mask = 0;
22406168f62cSWeston Andros Adamson 	if (access_result & NFS4_ACCESS_READ)
22416168f62cSWeston Andros Adamson 		entry->mask |= MAY_READ;
22426168f62cSWeston Andros Adamson 	if (access_result &
22436168f62cSWeston Andros Adamson 	    (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
22446168f62cSWeston Andros Adamson 		entry->mask |= MAY_WRITE;
22456168f62cSWeston Andros Adamson 	if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
22466168f62cSWeston Andros Adamson 		entry->mask |= MAY_EXEC;
22476168f62cSWeston Andros Adamson }
22486168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
22511da177e4SLinus Torvalds {
22521da177e4SLinus Torvalds 	struct nfs_access_entry cache;
22531da177e4SLinus Torvalds 	int status;
22541da177e4SLinus Torvalds 
2255f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2256f4ce1299STrond Myklebust 
22571da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
22581da177e4SLinus Torvalds 	if (status == 0)
2259f4ce1299STrond Myklebust 		goto out_cached;
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
22621da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
22631da177e4SLinus Torvalds 	cache.cred = cred;
22641da177e4SLinus Torvalds 	cache.jiffies = jiffies;
22651da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2266a71ee337SSuresh Jayaraman 	if (status != 0) {
2267a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2268a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2269a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2270a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2271a71ee337SSuresh Jayaraman 		}
2272f4ce1299STrond Myklebust 		goto out;
2273a71ee337SSuresh Jayaraman 	}
22741da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2275f4ce1299STrond Myklebust out_cached:
2276f4ce1299STrond Myklebust 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2277f4ce1299STrond Myklebust 		status = -EACCES;
22781da177e4SLinus Torvalds out:
2279f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2280f4ce1299STrond Myklebust 	return status;
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
2283af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2284af22f94aSTrond Myklebust {
2285af22f94aSTrond Myklebust 	int mask = 0;
2286af22f94aSTrond Myklebust 
2287f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2288f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2289f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2290f8d9a897SWeston Andros Adamson 	} else {
22918a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2292af22f94aSTrond Myklebust 			mask |= MAY_READ;
22938a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2294af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2295f8d9a897SWeston Andros Adamson 	}
2296f8d9a897SWeston Andros Adamson 
2297af22f94aSTrond Myklebust 	return mask;
2298af22f94aSTrond Myklebust }
2299af22f94aSTrond Myklebust 
2300af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2301af22f94aSTrond Myklebust {
2302af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2303af22f94aSTrond Myklebust }
230489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2305af22f94aSTrond Myklebust 
230610556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
23071da177e4SLinus Torvalds {
23081da177e4SLinus Torvalds 	struct rpc_cred *cred;
23091da177e4SLinus Torvalds 	int res = 0;
23101da177e4SLinus Torvalds 
231110556cb2SAl Viro 	if (mask & MAY_NOT_BLOCK)
2312b74c79e9SNick Piggin 		return -ECHILD;
2313b74c79e9SNick Piggin 
231491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
231591d5b470SChuck Lever 
2316e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
23171da177e4SLinus Torvalds 		goto out;
23181da177e4SLinus Torvalds 	/* Is this sys_access() ? */
23199cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
23201da177e4SLinus Torvalds 		goto force_lookup;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
23231da177e4SLinus Torvalds 		case S_IFLNK:
23241da177e4SLinus Torvalds 			goto out;
23251da177e4SLinus Torvalds 		case S_IFREG:
23261da177e4SLinus Torvalds 			break;
23271da177e4SLinus Torvalds 		case S_IFDIR:
23281da177e4SLinus Torvalds 			/*
23291da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
23301da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
23311da177e4SLinus Torvalds 			 */
23321da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
23331da177e4SLinus Torvalds 				goto out;
23341da177e4SLinus Torvalds 	}
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds force_lookup:
23371da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
23381da177e4SLinus Torvalds 		goto out_notsup;
23391da177e4SLinus Torvalds 
234098a8e323STrond Myklebust 	cred = rpc_lookup_cred();
23411da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
23421da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
23431da177e4SLinus Torvalds 		put_rpccred(cred);
23441da177e4SLinus Torvalds 	} else
23451da177e4SLinus Torvalds 		res = PTR_ERR(cred);
23461da177e4SLinus Torvalds out:
2347f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2348f696a365SMiklos Szeredi 		res = -EACCES;
2349f696a365SMiklos Szeredi 
23501e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
23511e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
23521da177e4SLinus Torvalds 	return res;
23531da177e4SLinus Torvalds out_notsup:
23541da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
23551da177e4SLinus Torvalds 	if (res == 0)
23562830ba7fSAl Viro 		res = generic_permission(inode, mask);
23571e7cb3dcSChuck Lever 	goto out;
23581da177e4SLinus Torvalds }
2359ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds /*
23621da177e4SLinus Torvalds  * Local variables:
23631da177e4SLinus Torvalds  *  version-control: t
23641da177e4SLinus Torvalds  *  kept-new-versions: 5
23651da177e4SLinus Torvalds  * End:
23661da177e4SLinus Torvalds  */
2367