xref: /openbmc/linux/fs/nfs/dir.c (revision 15d4b73a)
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,
60b044f645SBenjamin Coddington 	.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 {
136a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), 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 
154a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
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;
168a7a3b1e9SBenjamin Coddington 	bool plus;
169a7a3b1e9SBenjamin Coddington 	bool eof;
1701da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1711da177e4SLinus Torvalds 
172d1bacf9eSBryan Schumaker /*
173d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
174d1bacf9eSBryan Schumaker  */
175d1bacf9eSBryan Schumaker static
17611de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
177d1bacf9eSBryan Schumaker {
17811de3b11STrond Myklebust 	struct nfs_cache_array *array;
179d1bacf9eSBryan Schumaker 	int i;
1808cd51a0cSTrond Myklebust 
1812b86ce2dSCong Wang 	array = kmap_atomic(page);
182d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
183d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1842b86ce2dSCong Wang 	kunmap_atomic(array);
185d1bacf9eSBryan Schumaker }
186d1bacf9eSBryan Schumaker 
187d1bacf9eSBryan Schumaker /*
188d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
189d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
190d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
191d1bacf9eSBryan Schumaker  */
192d1bacf9eSBryan Schumaker static
1934a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
194d1bacf9eSBryan Schumaker {
195d1bacf9eSBryan Schumaker 	string->len = len;
196d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
1974a201d6eSTrond Myklebust 	if (string->name == NULL)
1984a201d6eSTrond Myklebust 		return -ENOMEM;
19904e4bd1cSCatalin Marinas 	/*
20004e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
20104e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
20204e4bd1cSCatalin Marinas 	 */
20304e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2048387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2054a201d6eSTrond Myklebust 	return 0;
206d1bacf9eSBryan Schumaker }
207d1bacf9eSBryan Schumaker 
208d1bacf9eSBryan Schumaker static
209d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
210d1bacf9eSBryan Schumaker {
2110795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2124a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2134a201d6eSTrond Myklebust 	int ret;
2144a201d6eSTrond Myklebust 
2154a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2163020093fSTrond Myklebust 
2173020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2183020093fSTrond Myklebust 	ret = -ENOSPC;
2193020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2203020093fSTrond Myklebust 		goto out;
2213020093fSTrond Myklebust 
2224a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2234a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2240b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2254a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2264a201d6eSTrond Myklebust 	if (ret)
2274a201d6eSTrond Myklebust 		goto out;
228d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2298cd51a0cSTrond Myklebust 	array->size++;
23047c716cbSTrond Myklebust 	if (entry->eof != 0)
231d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2324a201d6eSTrond Myklebust out:
2330795bf83SFabian Frederick 	kunmap(page);
2344a201d6eSTrond Myklebust 	return ret;
235d1bacf9eSBryan Schumaker }
236d1bacf9eSBryan Schumaker 
237d1bacf9eSBryan Schumaker static
238d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
239d1bacf9eSBryan Schumaker {
24023db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
241d1bacf9eSBryan Schumaker 	unsigned int index;
242d1bacf9eSBryan Schumaker 
243d1bacf9eSBryan Schumaker 	if (diff < 0)
244d1bacf9eSBryan Schumaker 		goto out_eof;
245d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2468cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
247d1bacf9eSBryan Schumaker 			goto out_eof;
248d1bacf9eSBryan Schumaker 		return -EAGAIN;
249d1bacf9eSBryan Schumaker 	}
250d1bacf9eSBryan Schumaker 
251d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
252d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
253d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
254d1bacf9eSBryan Schumaker 	return 0;
255d1bacf9eSBryan Schumaker out_eof:
256d1bacf9eSBryan Schumaker 	desc->eof = 1;
257d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
258d1bacf9eSBryan Schumaker }
259d1bacf9eSBryan Schumaker 
2604db72b40SJeff Layton static bool
2614db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2624db72b40SJeff Layton {
2634db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2644db72b40SJeff Layton 		return false;
2654db72b40SJeff Layton 	smp_rmb();
2664db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2674db72b40SJeff Layton }
2684db72b40SJeff Layton 
269d1bacf9eSBryan Schumaker static
270d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
271d1bacf9eSBryan Schumaker {
272d1bacf9eSBryan Schumaker 	int i;
2738ef2ce3eSBryan Schumaker 	loff_t new_pos;
274d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
275d1bacf9eSBryan Schumaker 
276d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2778cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
278496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2790c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2800c030806STrond Myklebust 
2818ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2824db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
2834db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
2840c030806STrond Myklebust 				ctx->duped = 0;
2850c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
28623db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2870c030806STrond Myklebust 				if (ctx->duped > 0
2880c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2890c030806STrond Myklebust 					if (printk_ratelimit()) {
2906de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
2910c030806STrond Myklebust 								"Please contact your server vendor.  "
2929581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
2939581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
2949581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
2950c030806STrond Myklebust 					}
2960c030806STrond Myklebust 					status = -ELOOP;
2970c030806STrond Myklebust 					goto out;
2980c030806STrond Myklebust 				}
2998ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3000c030806STrond Myklebust 				ctx->duped = -1;
3018ef2ce3eSBryan Schumaker 			}
30223db8620SAl Viro 			desc->ctx->pos = new_pos;
3038cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
30447c716cbSTrond Myklebust 			return 0;
3058cd51a0cSTrond Myklebust 		}
3068cd51a0cSTrond Myklebust 	}
30747c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
308d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
30918fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
31018fb5fe4STrond Myklebust 			desc->eof = 1;
311d1bacf9eSBryan Schumaker 	}
3120c030806STrond Myklebust out:
313d1bacf9eSBryan Schumaker 	return status;
314d1bacf9eSBryan Schumaker }
315d1bacf9eSBryan Schumaker 
316d1bacf9eSBryan Schumaker static
317d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
318d1bacf9eSBryan Schumaker {
319d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
32047c716cbSTrond Myklebust 	int status;
321d1bacf9eSBryan Schumaker 
3220795bf83SFabian Frederick 	array = kmap(desc->page);
323d1bacf9eSBryan Schumaker 
324d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
325d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
326d1bacf9eSBryan Schumaker 	else
327d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
328d1bacf9eSBryan Schumaker 
32947c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3300aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
331e47c085aSTrond Myklebust 		desc->current_index += array->size;
33247c716cbSTrond Myklebust 		desc->page_index++;
33347c716cbSTrond Myklebust 	}
3340795bf83SFabian Frederick 	kunmap(desc->page);
335d1bacf9eSBryan Schumaker 	return status;
336d1bacf9eSBryan Schumaker }
337d1bacf9eSBryan Schumaker 
338d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
339d1bacf9eSBryan Schumaker static
34056e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
341d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
342d1bacf9eSBryan Schumaker {
343480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
344480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3454704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3461da177e4SLinus Torvalds 	int		error;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds  again:
3491da177e4SLinus Torvalds 	timestamp = jiffies;
3504704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
351be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3521da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3531da177e4SLinus Torvalds 	if (error < 0) {
3541da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3551da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3561da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3573a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
358a7a3b1e9SBenjamin Coddington 			desc->plus = false;
3591da177e4SLinus Torvalds 			goto again;
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 		goto error;
3621da177e4SLinus Torvalds 	}
3631f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3644704f0e2STrond Myklebust 	desc->gencount = gencount;
365d1bacf9eSBryan Schumaker error:
366d1bacf9eSBryan Schumaker 	return error;
367d1bacf9eSBryan Schumaker }
368d1bacf9eSBryan Schumaker 
369573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
370573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
371d1bacf9eSBryan Schumaker {
372573c4e1eSChuck Lever 	int error;
373d1bacf9eSBryan Schumaker 
374573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
375573c4e1eSChuck Lever 	if (error)
376573c4e1eSChuck Lever 		return error;
377d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
378d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
379d1bacf9eSBryan Schumaker 	return 0;
380d1bacf9eSBryan Schumaker }
381d1bacf9eSBryan Schumaker 
382fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
383fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
384fa923369STrond Myklebust  */
385d39ab9deSBryan Schumaker static
386d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
387d39ab9deSBryan Schumaker {
388d8fdb47fSTrond Myklebust 	struct inode *inode;
389fa923369STrond Myklebust 	struct nfs_inode *nfsi;
390fa923369STrond Myklebust 
3912b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
3922b0143b5SDavid Howells 		return 0;
393fa923369STrond Myklebust 
394d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
395d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
396d8fdb47fSTrond Myklebust 		return 0;
397d8fdb47fSTrond Myklebust 
398d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
3997dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
400d39ab9deSBryan Schumaker 		return 0;
4017dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
4027dc72d5fSTrond Myklebust 		return 0;
4037dc72d5fSTrond Myklebust 	return 1;
404d39ab9deSBryan Schumaker }
405d39ab9deSBryan Schumaker 
406d39ab9deSBryan Schumaker static
40723db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
408d69ee9b8STrond Myklebust {
409d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
410d69ee9b8STrond Myklebust 		return false;
411d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
412d69ee9b8STrond Myklebust 		return true;
41323db8620SAl Viro 	if (ctx->pos == 0)
414d69ee9b8STrond Myklebust 		return true;
415d69ee9b8STrond Myklebust 	return false;
416d69ee9b8STrond Myklebust }
417d69ee9b8STrond Myklebust 
418d69ee9b8STrond Myklebust /*
41963519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
42063519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
421d69ee9b8STrond Myklebust  * directory.
422d69ee9b8STrond Myklebust  */
423d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
424d69ee9b8STrond Myklebust {
42563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
42663519fbcSTrond Myklebust 
42763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
42863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
42963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
430d69ee9b8STrond Myklebust }
431d69ee9b8STrond Myklebust 
432311324adSTrond Myklebust /*
433311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
434311324adSTrond Myklebust  *
435311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
436311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
437311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
438311324adSTrond Myklebust  * cache flush.
439311324adSTrond Myklebust  */
440311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
441311324adSTrond Myklebust {
44263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
44363519fbcSTrond Myklebust 
44463519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
44563519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
44663519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
44779f687a3STrond Myklebust 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
448311324adSTrond Myklebust 	}
449311324adSTrond Myklebust }
450311324adSTrond Myklebust 
451d69ee9b8STrond Myklebust static
452d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
453d39ab9deSBryan Schumaker {
45426fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4559ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4564a201d6eSTrond Myklebust 	struct dentry *dentry;
4574a201d6eSTrond Myklebust 	struct dentry *alias;
4582b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
459d39ab9deSBryan Schumaker 	struct inode *inode;
460aa9c2669SDavid Quigley 	int status;
461d39ab9deSBryan Schumaker 
462fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
463fa923369STrond Myklebust 		return;
4646c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
4656c441c25STrond Myklebust 		return;
46678d04af4STrond Myklebust 	if (filename.len == 0)
46778d04af4STrond Myklebust 		return;
46878d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
46978d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
47078d04af4STrond Myklebust 		return;
47178d04af4STrond Myklebust 	/* ...or '/' */
47278d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
47378d04af4STrond Myklebust 		return;
4744a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4754a201d6eSTrond Myklebust 		if (filename.len == 1)
4764a201d6eSTrond Myklebust 			return;
4774a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4784a201d6eSTrond Myklebust 			return;
4794a201d6eSTrond Myklebust 	}
4808387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
481d39ab9deSBryan Schumaker 
4824a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
4839ac3d3e8SAl Viro again:
4849ac3d3e8SAl Viro 	if (!dentry) {
4859ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
4869ac3d3e8SAl Viro 		if (IS_ERR(dentry))
4879ac3d3e8SAl Viro 			return;
4889ac3d3e8SAl Viro 	}
4899ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
4906c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
4916c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
4926c441c25STrond Myklebust 					&entry->fattr->fsid))
4936c441c25STrond Myklebust 			goto out;
494d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
4957dc72d5fSTrond Myklebust 			if (!entry->fh->size)
4967dc72d5fSTrond Myklebust 				goto out;
497cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
4982b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
499aa9c2669SDavid Quigley 			if (!status)
5002b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
501d39ab9deSBryan Schumaker 			goto out;
502d39ab9deSBryan Schumaker 		} else {
5035542aa2fSEric W. Biederman 			d_invalidate(dentry);
504d39ab9deSBryan Schumaker 			dput(dentry);
5059ac3d3e8SAl Viro 			dentry = NULL;
5069ac3d3e8SAl Viro 			goto again;
507d39ab9deSBryan Schumaker 		}
508d39ab9deSBryan Schumaker 	}
5097dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5107dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5117dc72d5fSTrond Myklebust 		goto out;
5127dc72d5fSTrond Myklebust 	}
513d39ab9deSBryan Schumaker 
5141775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
51541d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5169ac3d3e8SAl Viro 	d_lookup_done(dentry);
5179ac3d3e8SAl Viro 	if (alias) {
518d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
519d39ab9deSBryan Schumaker 			goto out;
5209ac3d3e8SAl Viro 		dput(dentry);
5219ac3d3e8SAl Viro 		dentry = alias;
5229ac3d3e8SAl Viro 	}
523d39ab9deSBryan Schumaker 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
524d39ab9deSBryan Schumaker out:
525d39ab9deSBryan Schumaker 	dput(dentry);
526d39ab9deSBryan Schumaker }
527d39ab9deSBryan Schumaker 
528d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
529d1bacf9eSBryan Schumaker static
5308cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5316650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
532d1bacf9eSBryan Schumaker {
533babddc72SBryan Schumaker 	struct xdr_stream stream;
534f7da7a12SBenny Halevy 	struct xdr_buf buf;
5356650239aSTrond Myklebust 	struct page *scratch;
53699424380SBryan Schumaker 	struct nfs_cache_array *array;
5375c346854STrond Myklebust 	unsigned int count = 0;
5385c346854STrond Myklebust 	int status;
539babddc72SBryan Schumaker 
5406650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5416650239aSTrond Myklebust 	if (scratch == NULL)
5426650239aSTrond Myklebust 		return -ENOMEM;
543babddc72SBryan Schumaker 
544ce85cfbeSBenjamin Coddington 	if (buflen == 0)
545ce85cfbeSBenjamin Coddington 		goto out_nopages;
546ce85cfbeSBenjamin Coddington 
547f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5486650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
54999424380SBryan Schumaker 
55099424380SBryan Schumaker 	do {
55199424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5528cd51a0cSTrond Myklebust 		if (status != 0) {
5538cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5548cd51a0cSTrond Myklebust 				status = 0;
55599424380SBryan Schumaker 			break;
5568cd51a0cSTrond Myklebust 		}
55799424380SBryan Schumaker 
5585c346854STrond Myklebust 		count++;
5595c346854STrond Myklebust 
560a7a3b1e9SBenjamin Coddington 		if (desc->plus)
561be62a1a8SMiklos Szeredi 			nfs_prime_dcache(file_dentry(desc->file), entry);
5628cd51a0cSTrond Myklebust 
5638cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5648cd51a0cSTrond Myklebust 		if (status != 0)
5658cd51a0cSTrond Myklebust 			break;
56699424380SBryan Schumaker 	} while (!entry->eof);
56799424380SBryan Schumaker 
568ce85cfbeSBenjamin Coddington out_nopages:
56947c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
5700795bf83SFabian Frederick 		array = kmap(page);
5718cd51a0cSTrond Myklebust 		array->eof_index = array->size;
57299424380SBryan Schumaker 		status = 0;
5730795bf83SFabian Frederick 		kunmap(page);
57456e4ebf8SBryan Schumaker 	}
5756650239aSTrond Myklebust 
5766650239aSTrond Myklebust 	put_page(scratch);
5778cd51a0cSTrond Myklebust 	return status;
5788cd51a0cSTrond Myklebust }
57956e4ebf8SBryan Schumaker 
58056e4ebf8SBryan Schumaker static
581c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
58256e4ebf8SBryan Schumaker {
58356e4ebf8SBryan Schumaker 	unsigned int i;
58456e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
58556e4ebf8SBryan Schumaker 		put_page(pages[i]);
58656e4ebf8SBryan Schumaker }
58756e4ebf8SBryan Schumaker 
58856e4ebf8SBryan Schumaker /*
58956e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
5900b936e37SAnna Schumaker  * to nfs_readdir_free_pagearray
59156e4ebf8SBryan Schumaker  */
59256e4ebf8SBryan Schumaker static
593c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
59456e4ebf8SBryan Schumaker {
59556e4ebf8SBryan Schumaker 	unsigned int i;
59656e4ebf8SBryan Schumaker 
59756e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
59856e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
59956e4ebf8SBryan Schumaker 		if (page == NULL)
60056e4ebf8SBryan Schumaker 			goto out_freepages;
60156e4ebf8SBryan Schumaker 		pages[i] = page;
60256e4ebf8SBryan Schumaker 	}
6036650239aSTrond Myklebust 	return 0;
60456e4ebf8SBryan Schumaker 
60556e4ebf8SBryan Schumaker out_freepages:
606c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6076650239aSTrond Myklebust 	return -ENOMEM;
608d1bacf9eSBryan Schumaker }
609d1bacf9eSBryan Schumaker 
610d1bacf9eSBryan Schumaker static
611d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
612d1bacf9eSBryan Schumaker {
61356e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
614d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
615d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
616d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6178cd51a0cSTrond Myklebust 	int status = -ENOMEM;
61856e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
619d1bacf9eSBryan Schumaker 
620d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6210aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
622d1bacf9eSBryan Schumaker 	entry.eof = 0;
623d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
624d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
625573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
626d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
627d1bacf9eSBryan Schumaker 		goto out;
628d1bacf9eSBryan Schumaker 
62914c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
63014c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
63114c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
63214c43f76SDavid Quigley 		goto out;
63314c43f76SDavid Quigley 	}
63414c43f76SDavid Quigley 
6350795bf83SFabian Frederick 	array = kmap(page);
636d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
637d1bacf9eSBryan Schumaker 	array->eof_index = -1;
638d1bacf9eSBryan Schumaker 
639c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(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 
657c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
658d1bacf9eSBryan Schumaker out_release_array:
6590795bf83SFabian Frederick 	kunmap(page);
66014c43f76SDavid Quigley 	nfs4_label_free(entry.label);
661d1bacf9eSBryan Schumaker out:
662d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
663d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
664d1bacf9eSBryan Schumaker 	return status;
665d1bacf9eSBryan Schumaker }
666d1bacf9eSBryan Schumaker 
667d1bacf9eSBryan Schumaker /*
668d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
669d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
670d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
671d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6721da177e4SLinus Torvalds  */
673d1bacf9eSBryan Schumaker static
674d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
675d1bacf9eSBryan Schumaker {
676496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
6778cd51a0cSTrond Myklebust 	int ret;
678d1bacf9eSBryan Schumaker 
6798cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6808cd51a0cSTrond Myklebust 	if (ret < 0)
681d1bacf9eSBryan Schumaker 		goto error;
682d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
683d1bacf9eSBryan Schumaker 
6842aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
685cd9ae2b6STrond Myklebust 		/* Should never happen */
686cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
687cd9ae2b6STrond Myklebust 	}
6881da177e4SLinus Torvalds 	unlock_page(page);
6891da177e4SLinus Torvalds 	return 0;
6901da177e4SLinus Torvalds  error:
6911da177e4SLinus Torvalds 	unlock_page(page);
6928cd51a0cSTrond Myklebust 	return ret;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
695d1bacf9eSBryan Schumaker static
696d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6971da177e4SLinus Torvalds {
698b044f645SBenjamin Coddington 	if (!desc->page->mapping)
69911de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
70009cbfeafSKirill A. Shutemov 	put_page(desc->page);
7011da177e4SLinus Torvalds 	desc->page = NULL;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
704d1bacf9eSBryan Schumaker static
705d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7061da177e4SLinus Torvalds {
707b044f645SBenjamin Coddington 	return read_cache_page(desc->file->f_mapping,
708d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds /*
712d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7131da177e4SLinus Torvalds  */
714d1bacf9eSBryan Schumaker static
715d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
716d1bacf9eSBryan Schumaker {
717d1bacf9eSBryan Schumaker 	int res;
718d1bacf9eSBryan Schumaker 
719d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
720d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
721d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
722d1bacf9eSBryan Schumaker 
723d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
72447c716cbSTrond Myklebust 	if (res != 0)
725d1bacf9eSBryan Schumaker 		cache_page_release(desc);
726d1bacf9eSBryan Schumaker 	return res;
727d1bacf9eSBryan Schumaker }
728d1bacf9eSBryan Schumaker 
729d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7301da177e4SLinus Torvalds static inline
7311da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7321da177e4SLinus Torvalds {
7338cd51a0cSTrond Myklebust 	int res;
734d1bacf9eSBryan Schumaker 
7350aded708STrond Myklebust 	if (desc->page_index == 0) {
7368cd51a0cSTrond Myklebust 		desc->current_index = 0;
7370aded708STrond Myklebust 		desc->last_cookie = 0;
7380aded708STrond Myklebust 	}
73947c716cbSTrond Myklebust 	do {
740d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
74147c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7421da177e4SLinus Torvalds 	return res;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds /*
7461da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7471da177e4SLinus Torvalds  */
7481da177e4SLinus Torvalds static
74923db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct file	*file = desc->file;
752d1bacf9eSBryan Schumaker 	int i = 0;
753d1bacf9eSBryan Schumaker 	int res = 0;
754d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7558ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
7568ef2ce3eSBryan Schumaker 
7570795bf83SFabian Frederick 	array = kmap(desc->page);
758d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
759ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7601da177e4SLinus Torvalds 
761ece0b423STrond Myklebust 		ent = &array->array[i];
76223db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
76323db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
764ece0b423STrond Myklebust 			desc->eof = 1;
7651da177e4SLinus Torvalds 			break;
766ece0b423STrond Myklebust 		}
76723db8620SAl Viro 		desc->ctx->pos++;
768d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
769d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
770d1bacf9eSBryan Schumaker 		else
771d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7720c030806STrond Myklebust 		if (ctx->duped != 0)
7730c030806STrond Myklebust 			ctx->duped = 1;
7748cd51a0cSTrond Myklebust 	}
77547c716cbSTrond Myklebust 	if (array->eof_index >= 0)
776d1bacf9eSBryan Schumaker 		desc->eof = 1;
777d1bacf9eSBryan Schumaker 
7780795bf83SFabian Frederick 	kunmap(desc->page);
779d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7801e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7811e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7821da177e4SLinus Torvalds 	return res;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /*
7861da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7871da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7881da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7891da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7901da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7911da177e4SLinus Torvalds  *
7921da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7931da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7941da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7951da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7961da177e4SLinus Torvalds  */
7971da177e4SLinus Torvalds static inline
79823db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
7991da177e4SLinus Torvalds {
8001da177e4SLinus Torvalds 	struct page	*page = NULL;
8011da177e4SLinus Torvalds 	int		status;
802496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8030c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8041da177e4SLinus Torvalds 
8051e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8061e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8091da177e4SLinus Torvalds 	if (!page) {
8101da177e4SLinus Torvalds 		status = -ENOMEM;
8111da177e4SLinus Torvalds 		goto out;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 
8147a8e1dc3STrond Myklebust 	desc->page_index = 0;
8150aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8167a8e1dc3STrond Myklebust 	desc->page = page;
8170c030806STrond Myklebust 	ctx->duped = 0;
8187a8e1dc3STrond Myklebust 
81985f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
82085f8607eSTrond Myklebust 	if (status < 0)
821d1bacf9eSBryan Schumaker 		goto out_release;
822d1bacf9eSBryan Schumaker 
82323db8620SAl Viro 	status = nfs_do_filldir(desc);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds  out:
8261e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8273110ff80SHarvey Harrison 			__func__, status);
8281da177e4SLinus Torvalds 	return status;
8291da177e4SLinus Torvalds  out_release:
830d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8311da177e4SLinus Torvalds 	goto out;
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
83400a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
83500a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
83600a92642SOlivier Galibert    whole directory.
8371da177e4SLinus Torvalds  */
83823db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
8391da177e4SLinus Torvalds {
840be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
8412b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
8421da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8431da177e4SLinus Torvalds 			*desc = &my_desc;
84423db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
84507b5ce8eSScott Mayhew 	int res = 0;
8461da177e4SLinus Torvalds 
8476de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
8486de1472fSAl Viro 			file, (long long)ctx->pos);
84991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
85091d5b470SChuck Lever 
8511da177e4SLinus Torvalds 	/*
85223db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
853f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
85400a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
85500a92642SOlivier Galibert 	 * revalidate the cookie.
8561da177e4SLinus Torvalds 	 */
8571da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8581da177e4SLinus Torvalds 
85923db8620SAl Viro 	desc->file = file;
86023db8620SAl Viro 	desc->ctx = ctx;
861480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8621da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
863a7a3b1e9SBenjamin Coddington 	desc->plus = nfs_use_readdirplus(inode, ctx);
8641da177e4SLinus Torvalds 
86579f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
86623db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
867fccca7fcSTrond Myklebust 	if (res < 0)
868fccca7fcSTrond Myklebust 		goto out;
869fccca7fcSTrond Myklebust 
87047c716cbSTrond Myklebust 	do {
8711da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
87200a92642SOlivier Galibert 
8731da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
874ece0b423STrond Myklebust 			res = 0;
8751da177e4SLinus Torvalds 			/* This means either end of directory */
876d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8771da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
87823db8620SAl Viro 				res = uncached_readdir(desc);
879ece0b423STrond Myklebust 				if (res == 0)
8801da177e4SLinus Torvalds 					continue;
8811da177e4SLinus Torvalds 			}
8821da177e4SLinus Torvalds 			break;
8831da177e4SLinus Torvalds 		}
8841da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8853a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8861da177e4SLinus Torvalds 			nfs_zap_caches(inode);
887baf57a09STrond Myklebust 			desc->page_index = 0;
888a7a3b1e9SBenjamin Coddington 			desc->plus = false;
889a7a3b1e9SBenjamin Coddington 			desc->eof = false;
8901da177e4SLinus Torvalds 			continue;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 		if (res < 0)
8931da177e4SLinus Torvalds 			break;
8941da177e4SLinus Torvalds 
89523db8620SAl Viro 		res = nfs_do_filldir(desc);
896ece0b423STrond Myklebust 		if (res < 0)
8971da177e4SLinus Torvalds 			break;
89847c716cbSTrond Myklebust 	} while (!desc->eof);
899fccca7fcSTrond Myklebust out:
9001e7cb3dcSChuck Lever 	if (res > 0)
9011e7cb3dcSChuck Lever 		res = 0;
9026de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9031da177e4SLinus Torvalds 	return res;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
906965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
907f0dd2136STrond Myklebust {
908b044f645SBenjamin Coddington 	struct inode *inode = file_inode(filp);
909480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
910b84e06c5SChuck Lever 
9116de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9126de1472fSAl Viro 			filp, offset, whence);
913b84e06c5SChuck Lever 
914b044f645SBenjamin Coddington 	inode_lock(inode);
915965c8e59SAndrew Morton 	switch (whence) {
916f0dd2136STrond Myklebust 		case 1:
917f0dd2136STrond Myklebust 			offset += filp->f_pos;
918f0dd2136STrond Myklebust 		case 0:
919f0dd2136STrond Myklebust 			if (offset >= 0)
920f0dd2136STrond Myklebust 				break;
921f0dd2136STrond Myklebust 		default:
922b044f645SBenjamin Coddington 			offset = -EINVAL;
923b044f645SBenjamin Coddington 			goto out;
924f0dd2136STrond Myklebust 	}
925f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
926f0dd2136STrond Myklebust 		filp->f_pos = offset;
927480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
9288ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
929f0dd2136STrond Myklebust 	}
930b044f645SBenjamin Coddington out:
931b044f645SBenjamin Coddington 	inode_unlock(inode);
932f0dd2136STrond Myklebust 	return offset;
933f0dd2136STrond Myklebust }
934f0dd2136STrond Myklebust 
9351da177e4SLinus Torvalds /*
9361da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9371da177e4SLinus Torvalds  * is a dummy operation.
9381da177e4SLinus Torvalds  */
93902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
94002c24a82SJosef Bacik 			 int datasync)
9411da177e4SLinus Torvalds {
9426de1472fSAl Viro 	struct inode *inode = file_inode(filp);
9437ea80859SChristoph Hellwig 
9446de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
9451e7cb3dcSChuck Lever 
9465955102cSAl Viro 	inode_lock(inode);
9476de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
9485955102cSAl Viro 	inode_unlock(inode);
9491da177e4SLinus Torvalds 	return 0;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
952bfc69a45STrond Myklebust /**
953bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
954bfc69a45STrond Myklebust  * @dir - pointer to directory inode
955bfc69a45STrond Myklebust  *
956bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
957bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
958bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
959bfc69a45STrond Myklebust  *
960bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
961bfc69a45STrond Myklebust  */
962bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
963bfc69a45STrond Myklebust {
964011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
965bfc69a45STrond Myklebust }
96689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
967bfc69a45STrond Myklebust 
9681da177e4SLinus Torvalds /*
9691da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9701da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9711da177e4SLinus Torvalds  * and may need to be looked up again.
972912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
9731da177e4SLinus Torvalds  */
974912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
975912a108dSNeilBrown 			      int rcu_walk)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9781da177e4SLinus Torvalds 		return 1;
9794eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9804eec952eSTrond Myklebust 		return 0;
981f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9826ecc5e8fSTrond Myklebust 		return 0;
983f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
9841cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
985912a108dSNeilBrown 		if (rcu_walk)
986f2c77f4eSTrond Myklebust 			return 0;
9871cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
9881cd9cb05STrond Myklebust 			return 0;
9891cd9cb05STrond Myklebust 	}
990f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
991f2c77f4eSTrond Myklebust 		return 0;
992f2c77f4eSTrond Myklebust 	return 1;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
996a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
997a12802caSTrond Myklebust  * an O_EXCL create using this path component.
998a12802caSTrond Myklebust  */
999fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1000a12802caSTrond Myklebust {
1001a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1002a12802caSTrond Myklebust 		return 0;
1003fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1004a12802caSTrond Myklebust }
1005a12802caSTrond Myklebust 
1006a12802caSTrond Myklebust /*
10071d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10081d6757fbSTrond Myklebust  *
10091d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10101d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10111d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10121d6757fbSTrond Myklebust  *
10131d6757fbSTrond Myklebust  */
101465a0c149STrond Myklebust static
1015fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10161da177e4SLinus Torvalds {
10171da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
101865a0c149STrond Myklebust 	int ret;
10191da177e4SLinus Torvalds 
102036d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10214e99a1ffSTrond Myklebust 		return 0;
10221da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1023fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
10241da177e4SLinus Torvalds 		goto out_force;
10251da177e4SLinus Torvalds 	/* This is an open(2) */
1026fa3c56bbSAl Viro 	if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1027fa3c56bbSAl Viro 	    (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
10281da177e4SLinus Torvalds 		goto out_force;
102965a0c149STrond Myklebust out:
103065a0c149STrond Myklebust 	return (inode->i_nlink == 0) ? -ENOENT : 0;
10311da177e4SLinus Torvalds out_force:
10321fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
10331fa1e384SNeilBrown 		return -ECHILD;
103465a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
103565a0c149STrond Myklebust 	if (ret != 0)
103665a0c149STrond Myklebust 		return ret;
103765a0c149STrond Myklebust 	goto out;
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds /*
10411da177e4SLinus Torvalds  * We judge how long we want to trust negative
10421da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10431da177e4SLinus Torvalds  *
10441da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10451da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1046912a108dSNeilBrown  *
1047912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1048912a108dSNeilBrown  * suggesting a reval is needed.
10491da177e4SLinus Torvalds  */
10501da177e4SLinus Torvalds static inline
10511da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1052fa3c56bbSAl Viro 		       unsigned int flags)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
1055fa3c56bbSAl Viro 	if (flags & LOOKUP_CREATE)
10561da177e4SLinus Torvalds 		return 0;
10574eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10584eec952eSTrond Myklebust 		return 1;
1059912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10641da177e4SLinus Torvalds  * and we should check whether we can really trust that
10651da177e4SLinus Torvalds  * lookup.
10661da177e4SLinus Torvalds  *
10671da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10681da177e4SLinus Torvalds  * we have an inode!
10691da177e4SLinus Torvalds  *
10701da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10711da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10721da177e4SLinus Torvalds  */
10730b728e19SAl Viro static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	struct inode *dir;
10761da177e4SLinus Torvalds 	struct inode *inode;
10771da177e4SLinus Torvalds 	struct dentry *parent;
1078e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1079e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10801775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
10811da177e4SLinus Torvalds 	int error;
10821da177e4SLinus Torvalds 
1083d51ac1a8SNeilBrown 	if (flags & LOOKUP_RCU) {
108450d77739SNeilBrown 		parent = ACCESS_ONCE(dentry->d_parent);
10852b0143b5SDavid Howells 		dir = d_inode_rcu(parent);
1086d51ac1a8SNeilBrown 		if (!dir)
108734286d66SNick Piggin 			return -ECHILD;
1088d51ac1a8SNeilBrown 	} else {
10891da177e4SLinus Torvalds 		parent = dget_parent(dentry);
10902b0143b5SDavid Howells 		dir = d_inode(parent);
1091d51ac1a8SNeilBrown 	}
109291d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10932b0143b5SDavid Howells 	inode = d_inode(dentry);
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	if (!inode) {
1096912a108dSNeilBrown 		if (nfs_neg_need_reval(dir, dentry, flags)) {
1097d51ac1a8SNeilBrown 			if (flags & LOOKUP_RCU)
1098d51ac1a8SNeilBrown 				return -ECHILD;
10991da177e4SLinus Torvalds 			goto out_bad;
1100912a108dSNeilBrown 		}
110163519fbcSTrond Myklebust 		goto out_valid;
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
1105d51ac1a8SNeilBrown 		if (flags & LOOKUP_RCU)
1106d51ac1a8SNeilBrown 			return -ECHILD;
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 
1115912a108dSNeilBrown 	/* Force a full look up iff the parent directory has changed */
1116912a108dSNeilBrown 	if (!nfs_is_exclusive_create(dir, flags) &&
1117912a108dSNeilBrown 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1118cc89684cSNeilBrown 		error = nfs_lookup_verify_inode(inode, flags);
1119cc89684cSNeilBrown 		if (error) {
1120d51ac1a8SNeilBrown 			if (flags & LOOKUP_RCU)
1121d51ac1a8SNeilBrown 				return -ECHILD;
1122cc89684cSNeilBrown 			if (error == -ESTALE)
11231da177e4SLinus Torvalds 				goto out_zap_parent;
1124cc89684cSNeilBrown 			goto out_error;
11251fa1e384SNeilBrown 		}
112663519fbcSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
11271da177e4SLinus Torvalds 		goto out_valid;
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 
1130912a108dSNeilBrown 	if (flags & LOOKUP_RCU)
1131912a108dSNeilBrown 		return -ECHILD;
1132912a108dSNeilBrown 
11331da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11341da177e4SLinus Torvalds 		goto out_bad;
11351da177e4SLinus Torvalds 
1136e1fb4d05STrond Myklebust 	error = -ENOMEM;
1137e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1138e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1139e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1140e1fb4d05STrond Myklebust 		goto out_error;
1141e1fb4d05STrond Myklebust 
114214c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
114314c43f76SDavid Quigley 	if (IS_ERR(label))
114414c43f76SDavid Quigley 		goto out_error;
114514c43f76SDavid Quigley 
11466e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
11471775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
11486e0d0be7STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
1149cc89684cSNeilBrown 	if (error == -ESTALE || error == -ENOENT)
11501da177e4SLinus Torvalds 		goto out_bad;
1151cc89684cSNeilBrown 	if (error)
1152cc89684cSNeilBrown 		goto out_error;
1153e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11541da177e4SLinus Torvalds 		goto out_bad;
1155e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11561da177e4SLinus Torvalds 		goto out_bad;
11571da177e4SLinus Torvalds 
1158aa9c2669SDavid Quigley 	nfs_setsecurity(inode, fattr, label);
1159aa9c2669SDavid Quigley 
1160e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1161e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
116214c43f76SDavid Quigley 	nfs4_label_free(label);
116314c43f76SDavid Quigley 
116463519fbcSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
116563519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
116663519fbcSTrond Myklebust 
116715860ab1STrond Myklebust out_set_verifier:
1168cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11691da177e4SLinus Torvalds  out_valid:
1170d51ac1a8SNeilBrown 	if (flags & LOOKUP_RCU) {
117150d77739SNeilBrown 		if (parent != ACCESS_ONCE(dentry->d_parent))
1172d51ac1a8SNeilBrown 			return -ECHILD;
1173d51ac1a8SNeilBrown 	} else
11741da177e4SLinus Torvalds 		dput(parent);
11756de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11766de1472fSAl Viro 			__func__, dentry);
11771da177e4SLinus Torvalds 	return 1;
11781da177e4SLinus Torvalds out_zap_parent:
11791da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11801da177e4SLinus Torvalds  out_bad:
1181d51ac1a8SNeilBrown 	WARN_ON(flags & LOOKUP_RCU);
1182c44600c9SAl Viro 	nfs_free_fattr(fattr);
1183c44600c9SAl Viro 	nfs_free_fhandle(fhandle);
118414c43f76SDavid Quigley 	nfs4_label_free(label);
1185a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11861da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11871da177e4SLinus Torvalds 		/* Purge readdir caches. */
11881da177e4SLinus Torvalds 		nfs_zap_caches(inode);
1189a3f432bfSJ. Bruce Fields 		/*
1190a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1191a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1192a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1193a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1194a3f432bfSJ. Bruce Fields 		 */
1195a3f432bfSJ. Bruce Fields 		if (IS_ROOT(dentry))
1196d9e80b7dSAl Viro 			goto out_valid;
11971da177e4SLinus Torvalds 	}
11981da177e4SLinus Torvalds 	dput(parent);
11996de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
12006de1472fSAl Viro 			__func__, dentry);
12011da177e4SLinus Torvalds 	return 0;
1202e1fb4d05STrond Myklebust out_error:
1203d51ac1a8SNeilBrown 	WARN_ON(flags & LOOKUP_RCU);
1204e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1205e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
120614c43f76SDavid Quigley 	nfs4_label_free(label);
1207e1fb4d05STrond Myklebust 	dput(parent);
12086de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
12096de1472fSAl Viro 			__func__, dentry, error);
1210e1fb4d05STrond Myklebust 	return error;
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds /*
12142b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1215ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1216ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1217ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1218ecf3d1f1SJeff Layton  *
1219ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1220ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1221ecf3d1f1SJeff Layton  */
1222ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1223ecf3d1f1SJeff Layton {
12242b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
12259cdd1d3fSTrond Myklebust 	int error = 0;
1226ecf3d1f1SJeff Layton 
1227ecf3d1f1SJeff Layton 	/*
1228ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1229ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1230ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1231ecf3d1f1SJeff Layton 	 */
1232ecf3d1f1SJeff Layton 	if (!inode) {
12336de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
12346de1472fSAl Viro 				__func__, dentry);
1235ecf3d1f1SJeff Layton 		return 1;
1236ecf3d1f1SJeff Layton 	}
1237ecf3d1f1SJeff Layton 
1238ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
12396de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12406de1472fSAl Viro 				__func__, dentry);
1241ecf3d1f1SJeff Layton 		return 0;
1242ecf3d1f1SJeff Layton 	}
1243ecf3d1f1SJeff Layton 
12449cdd1d3fSTrond Myklebust 	if (nfs_mapping_need_revalidate_inode(inode))
12459cdd1d3fSTrond Myklebust 		error = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1246ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1247ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1248ecf3d1f1SJeff Layton 	return !error;
1249ecf3d1f1SJeff Layton }
1250ecf3d1f1SJeff Layton 
1251ecf3d1f1SJeff Layton /*
12521da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
12531da177e4SLinus Torvalds  */
1254fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
12551da177e4SLinus Torvalds {
12566de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
12576de1472fSAl Viro 		dentry, dentry->d_flags);
12581da177e4SLinus Torvalds 
125977f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
12602b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
126177f11192STrond Myklebust 		return 1;
126277f11192STrond Myklebust 
12631da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12641da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
12651da177e4SLinus Torvalds 		return 1;
12661da177e4SLinus Torvalds 	}
12671da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
12681da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
12691da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
12701da177e4SLinus Torvalds 		return 1;
12711da177e4SLinus Torvalds 	}
12721da177e4SLinus Torvalds 	return 0;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
12771b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
12781b83d707STrond Myklebust {
12791b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
12801f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
12811f018458STrond Myklebust 	if (inode->i_nlink == 1)
12821f018458STrond Myklebust 		clear_nlink(inode);
12831f018458STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
12841b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
12851b83d707STrond Myklebust }
12861b83d707STrond Myklebust 
12871da177e4SLinus Torvalds /*
12881da177e4SLinus Torvalds  * Called when the dentry loses inode.
12891da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
12901da177e4SLinus Torvalds  */
12911da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12921da177e4SLinus Torvalds {
129383672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
129483672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
129583672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
129683672d39SNeil Brown 
12971da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1298e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12991f018458STrond Myklebust 		nfs_drop_nlink(inode);
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	iput(inode);
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
1304b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1305b1942c5fSAl Viro {
1306b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1307b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1308b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1309b1942c5fSAl Viro 			WARN_ON(1);
1310b1942c5fSAl Viro 		else
1311b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1312b1942c5fSAl Viro 	}
1313b1942c5fSAl Viro }
1314b1942c5fSAl Viro 
1315f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
13161da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1317ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
13181da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13191da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
132036d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1321b1942c5fSAl Viro 	.d_release	= nfs_d_release,
13221da177e4SLinus Torvalds };
1323ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
13241da177e4SLinus Torvalds 
1325597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
13261da177e4SLinus Torvalds {
13271da177e4SLinus Torvalds 	struct dentry *res;
13281da177e4SLinus Torvalds 	struct inode *inode = NULL;
1329e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1330e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
13311775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
13321da177e4SLinus Torvalds 	int error;
13331da177e4SLinus Torvalds 
13346de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
133591d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
13361da177e4SLinus Torvalds 
1337130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1338130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
13391da177e4SLinus Torvalds 
1340fd684071STrond Myklebust 	/*
1341fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1342fd684071STrond Myklebust 	 * but don't hash the dentry.
1343fd684071STrond Myklebust 	 */
1344130f9ab7SAl Viro 	if (nfs_is_exclusive_create(dir, flags))
1345130f9ab7SAl Viro 		return NULL;
13461da177e4SLinus Torvalds 
1347e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1348e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1349e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1350e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1351e1fb4d05STrond Myklebust 		goto out;
1352e1fb4d05STrond Myklebust 
135314c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
135414c43f76SDavid Quigley 	if (IS_ERR(label))
135514c43f76SDavid Quigley 		goto out;
135614c43f76SDavid Quigley 
13576e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
13581775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
13591da177e4SLinus Torvalds 	if (error == -ENOENT)
13601da177e4SLinus Torvalds 		goto no_entry;
13611da177e4SLinus Torvalds 	if (error < 0) {
13621da177e4SLinus Torvalds 		res = ERR_PTR(error);
1363bf130914SAl Viro 		goto out_label;
13641da177e4SLinus Torvalds 	}
13651775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1366bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
136703f28e3aSTrond Myklebust 	if (IS_ERR(res))
1368bf130914SAl Viro 		goto out_label;
136954ceac45SDavid Howells 
137063519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
137163519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1372d69ee9b8STrond Myklebust 
13731da177e4SLinus Torvalds no_entry:
137441d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
13759eaef27bSTrond Myklebust 	if (res != NULL) {
13769eaef27bSTrond Myklebust 		if (IS_ERR(res))
1377bf130914SAl Viro 			goto out_label;
13781da177e4SLinus Torvalds 		dentry = res;
13799eaef27bSTrond Myklebust 	}
13801da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1381bf130914SAl Viro out_label:
13826e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
138314c43f76SDavid Quigley 	nfs4_label_free(label);
13841da177e4SLinus Torvalds out:
1385e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1386e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
13871da177e4SLinus Torvalds 	return res;
13881da177e4SLinus Torvalds }
1389ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
13901da177e4SLinus Torvalds 
139189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
13920b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
13931da177e4SLinus Torvalds 
1394f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
13950ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
13961da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
13971da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
139836d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1399b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14001da177e4SLinus Torvalds };
140189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
14021da177e4SLinus Torvalds 
14038a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
14048a5e929dSAl Viro {
14058a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
14068a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
14078a5e929dSAl Viro 		res |= FMODE_READ;
14088a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
14098a5e929dSAl Viro 		res |= FMODE_WRITE;
14108a5e929dSAl Viro 	return res;
14118a5e929dSAl Viro }
14128a5e929dSAl Viro 
1413532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1414cd9a1c0eSTrond Myklebust {
1415532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1416cd9a1c0eSTrond Myklebust }
1417cd9a1c0eSTrond Myklebust 
1418cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1419cd9a1c0eSTrond Myklebust {
1420f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1421cd9a1c0eSTrond Myklebust 	return 0;
1422cd9a1c0eSTrond Myklebust }
1423cd9a1c0eSTrond Myklebust 
1424d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
14250dd2b474SMiklos Szeredi 			   struct dentry *dentry,
142630d90494SAl Viro 			   struct file *file, unsigned open_flags,
142747237687SAl Viro 			   int *opened)
1428cd9a1c0eSTrond Myklebust {
14290dd2b474SMiklos Szeredi 	int err;
14300dd2b474SMiklos Szeredi 
143130d90494SAl Viro 	err = finish_open(file, dentry, do_open, opened);
143230d90494SAl Viro 	if (err)
1433d9585277SAl Viro 		goto out;
1434eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
143530d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1436eaa2b82cSNeilBrown 	else
1437eaa2b82cSNeilBrown 		err = -ESTALE;
1438cd9a1c0eSTrond Myklebust out:
1439d9585277SAl Viro 	return err;
1440cd9a1c0eSTrond Myklebust }
1441cd9a1c0eSTrond Myklebust 
144273a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
144330d90494SAl Viro 		    struct file *file, unsigned open_flags,
144447237687SAl Viro 		    umode_t mode, int *opened)
14451da177e4SLinus Torvalds {
1446c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1447cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
14480dd2b474SMiklos Szeredi 	struct dentry *res;
14490dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1450f46e0bd3STrond Myklebust 	struct inode *inode;
14511472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1452c94c0953SAl Viro 	bool switched = false;
1453898f635cSTrond Myklebust 	int err;
14541da177e4SLinus Torvalds 
14550dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
14562b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
14570dd2b474SMiklos Szeredi 
14581e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
14596de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
14601e7cb3dcSChuck Lever 
14619597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
14629597c13bSJeff Layton 	if (err)
14639597c13bSJeff Layton 		return err;
14649597c13bSJeff Layton 
14650dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
14660dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
146700699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
14680dd2b474SMiklos Szeredi 			/*
14690dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
14700dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
14710dd2b474SMiklos Szeredi 			 * again
14720dd2b474SMiklos Szeredi 			 */
1473d9585277SAl Viro 			return -ENOENT;
14740dd2b474SMiklos Szeredi 		}
14751472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
14761da177e4SLinus Torvalds 		goto no_open;
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 
14790dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1480d9585277SAl Viro 		return -ENAMETOOLONG;
14811da177e4SLinus Torvalds 
14820dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1483dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1484dff25ddbSAndreas Gruenbacher 
1485dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1486dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1487dff25ddbSAndreas Gruenbacher 
1488536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1489dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
14900dd2b474SMiklos Szeredi 	}
1491536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1492536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1493536e43d1STrond Myklebust 		attr.ia_size = 0;
1494cd9a1c0eSTrond Myklebust 	}
1495cd9a1c0eSTrond Myklebust 
1496c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1497c94c0953SAl Viro 		d_drop(dentry);
1498c94c0953SAl Viro 		switched = true;
1499c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1500c94c0953SAl Viro 					  &dentry->d_name, &wq);
1501c94c0953SAl Viro 		if (IS_ERR(dentry))
1502c94c0953SAl Viro 			return PTR_ERR(dentry);
1503c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1504c94c0953SAl Viro 			return finish_no_open(file, dentry);
1505c94c0953SAl Viro 	}
1506c94c0953SAl Viro 
1507532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
15080dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
15090dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1510d9585277SAl Viro 		goto out;
15110dd2b474SMiklos Szeredi 
15126e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
15135bc2afc2STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1514275bb307STrond Myklebust 	if (IS_ERR(inode)) {
15150dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
15166e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15172d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1518d20cb71dSAl Viro 		d_drop(dentry);
15190dd2b474SMiklos Szeredi 		switch (err) {
15201da177e4SLinus Torvalds 		case -ENOENT:
1521774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1522809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15230dd2b474SMiklos Szeredi 			break;
15241788ea6eSJeff Layton 		case -EISDIR:
15256f926b5bSTrond Myklebust 		case -ENOTDIR:
15266f926b5bSTrond Myklebust 			goto no_open;
15271da177e4SLinus Torvalds 		case -ELOOP:
15280dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
15291da177e4SLinus Torvalds 				goto no_open;
15300dd2b474SMiklos Szeredi 			break;
15311da177e4SLinus Torvalds 			/* case -EINVAL: */
15321da177e4SLinus Torvalds 		default:
15330dd2b474SMiklos Szeredi 			break;
15341da177e4SLinus Torvalds 		}
15351da177e4SLinus Torvalds 		goto out;
15361da177e4SLinus Torvalds 	}
15370dd2b474SMiklos Szeredi 
1538275bb307STrond Myklebust 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
15396e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
15402d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1541d9585277SAl Viro out:
1542c94c0953SAl Viro 	if (unlikely(switched)) {
1543c94c0953SAl Viro 		d_lookup_done(dentry);
1544c94c0953SAl Viro 		dput(dentry);
1545c94c0953SAl Viro 	}
1546d9585277SAl Viro 	return err;
15470dd2b474SMiklos Szeredi 
15481da177e4SLinus Torvalds no_open:
15491472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1550c94c0953SAl Viro 	if (switched) {
1551c94c0953SAl Viro 		d_lookup_done(dentry);
1552c94c0953SAl Viro 		if (!res)
1553c94c0953SAl Viro 			res = dentry;
1554c94c0953SAl Viro 		else
1555c94c0953SAl Viro 			dput(dentry);
1556c94c0953SAl Viro 	}
15570dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1558c94c0953SAl Viro 		return PTR_ERR(res);
1559e45198a6SAl Viro 	return finish_no_open(file, res);
15601da177e4SLinus Torvalds }
156189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
15621da177e4SLinus Torvalds 
15630b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
15641da177e4SLinus Torvalds {
1565657e94b6SNick Piggin 	struct inode *inode;
156650de348cSMiklos Szeredi 	int ret = 0;
15671da177e4SLinus Torvalds 
1568fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1569eda72afbSMiklos Szeredi 		goto no_open;
1570eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
15715584c306STrond Myklebust 		goto no_open;
157249f9a0faSTrond Myklebust 	if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
157349f9a0faSTrond Myklebust 		goto no_open;
15742b484297STrond Myklebust 
15752b0143b5SDavid Howells 	inode = d_inode(dentry);
15762b484297STrond Myklebust 
15771da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
15781da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
15791da177e4SLinus Torvalds 	 */
1580216d5d06STrond Myklebust 	if (inode == NULL) {
158149317a7fSNeilBrown 		struct dentry *parent;
158249317a7fSNeilBrown 		struct inode *dir;
158349317a7fSNeilBrown 
1584912a108dSNeilBrown 		if (flags & LOOKUP_RCU) {
158550d77739SNeilBrown 			parent = ACCESS_ONCE(dentry->d_parent);
15862b0143b5SDavid Howells 			dir = d_inode_rcu(parent);
1587912a108dSNeilBrown 			if (!dir)
1588d51ac1a8SNeilBrown 				return -ECHILD;
1589912a108dSNeilBrown 		} else {
159049317a7fSNeilBrown 			parent = dget_parent(dentry);
15912b0143b5SDavid Howells 			dir = d_inode(parent);
1592912a108dSNeilBrown 		}
1593fa3c56bbSAl Viro 		if (!nfs_neg_need_reval(dir, dentry, flags))
1594216d5d06STrond Myklebust 			ret = 1;
1595912a108dSNeilBrown 		else if (flags & LOOKUP_RCU)
1596912a108dSNeilBrown 			ret = -ECHILD;
1597912a108dSNeilBrown 		if (!(flags & LOOKUP_RCU))
159849317a7fSNeilBrown 			dput(parent);
159950d77739SNeilBrown 		else if (parent != ACCESS_ONCE(dentry->d_parent))
1600912a108dSNeilBrown 			return -ECHILD;
16011da177e4SLinus Torvalds 		goto out;
1602216d5d06STrond Myklebust 	}
1603216d5d06STrond Myklebust 
16041da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
16051da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
160649317a7fSNeilBrown 		goto no_open;
16071da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1608fa3c56bbSAl Viro 	if (flags & LOOKUP_EXCL)
160949317a7fSNeilBrown 		goto no_open;
16101da177e4SLinus Torvalds 
16110ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1612898f635cSTrond Myklebust 	ret = 1;
16130ef97dcfSMiklos Szeredi 
16141da177e4SLinus Torvalds out:
16151da177e4SLinus Torvalds 	return ret;
1616535918f1STrond Myklebust 
16175584c306STrond Myklebust no_open:
16180b728e19SAl Viro 	return nfs_lookup_revalidate(dentry, flags);
1619c0204fd2STrond Myklebust }
1620c0204fd2STrond Myklebust 
16211da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds /*
16241da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
16251da177e4SLinus Torvalds  */
16261da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
16271775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
16281775fd3eSDavid Quigley 				struct nfs4_label *label)
16291da177e4SLinus Torvalds {
1630fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
16312b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
16321da177e4SLinus Torvalds 	struct inode *inode;
16331da177e4SLinus Torvalds 	int error = -EACCES;
16341da177e4SLinus Torvalds 
1635fab728e1STrond Myklebust 	d_drop(dentry);
1636fab728e1STrond Myklebust 
16371da177e4SLinus Torvalds 	/* We may have been initialized further down */
16382b0143b5SDavid Howells 	if (d_really_is_positive(dentry))
1639fab728e1STrond Myklebust 		goto out;
16401da177e4SLinus Torvalds 	if (fhandle->size == 0) {
16411775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
16421da177e4SLinus Torvalds 		if (error)
1643fab728e1STrond Myklebust 			goto out_error;
16441da177e4SLinus Torvalds 	}
16455724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16461da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
16471da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
16481775fd3eSDavid Quigley 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
16491da177e4SLinus Torvalds 		if (error < 0)
1650fab728e1STrond Myklebust 			goto out_error;
16511da177e4SLinus Torvalds 	}
16521775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
165303f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
165403f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1655fab728e1STrond Myklebust 		goto out_error;
1656fab728e1STrond Myklebust 	d_add(dentry, inode);
1657fab728e1STrond Myklebust out:
1658fab728e1STrond Myklebust 	dput(parent);
16591da177e4SLinus Torvalds 	return 0;
1660fab728e1STrond Myklebust out_error:
1661fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1662fab728e1STrond Myklebust 	dput(parent);
1663fab728e1STrond Myklebust 	return error;
16641da177e4SLinus Torvalds }
1665ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds /*
16681da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
16691da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
16701da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
16711da177e4SLinus Torvalds  * reply path made it appear to have failed.
16721da177e4SLinus Torvalds  */
1673597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1674ebfc3b49SAl Viro 		umode_t mode, bool excl)
16751da177e4SLinus Torvalds {
16761da177e4SLinus Torvalds 	struct iattr attr;
1677ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
16781da177e4SLinus Torvalds 	int error;
16791da177e4SLinus Torvalds 
16801e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
16816de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	attr.ia_mode = mode;
16841da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16851da177e4SLinus Torvalds 
16868b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
16878867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
16888b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
16891da177e4SLinus Torvalds 	if (error != 0)
16901da177e4SLinus Torvalds 		goto out_err;
16911da177e4SLinus Torvalds 	return 0;
16921da177e4SLinus Torvalds out_err:
16931da177e4SLinus Torvalds 	d_drop(dentry);
16941da177e4SLinus Torvalds 	return error;
16951da177e4SLinus Torvalds }
1696ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds /*
16991da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17001da177e4SLinus Torvalds  */
1701597d9289SBryan Schumaker int
17021a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	struct iattr attr;
17051da177e4SLinus Torvalds 	int status;
17061da177e4SLinus Torvalds 
17071e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
17086de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	attr.ia_mode = mode;
17111da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17121da177e4SLinus Torvalds 
17131ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
17141da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
17151ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
17161da177e4SLinus Torvalds 	if (status != 0)
17171da177e4SLinus Torvalds 		goto out_err;
17181da177e4SLinus Torvalds 	return 0;
17191da177e4SLinus Torvalds out_err:
17201da177e4SLinus Torvalds 	d_drop(dentry);
17211da177e4SLinus Torvalds 	return status;
17221da177e4SLinus Torvalds }
1723ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds /*
17261da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
17271da177e4SLinus Torvalds  */
1728597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17291da177e4SLinus Torvalds {
17301da177e4SLinus Torvalds 	struct iattr attr;
17311da177e4SLinus Torvalds 	int error;
17321da177e4SLinus Torvalds 
17331e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
17346de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
17371da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
17381da177e4SLinus Torvalds 
17391ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
17401da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
17411ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
17421da177e4SLinus Torvalds 	if (error != 0)
17431da177e4SLinus Torvalds 		goto out_err;
17441da177e4SLinus Torvalds 	return 0;
17451da177e4SLinus Torvalds out_err:
17461da177e4SLinus Torvalds 	d_drop(dentry);
17471da177e4SLinus Torvalds 	return error;
17481da177e4SLinus Torvalds }
1749ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
17501da177e4SLinus Torvalds 
1751d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1752d45b9d8bSTrond Myklebust {
1753dc3f4198SAl Viro 	if (simple_positive(dentry))
1754d45b9d8bSTrond Myklebust 		d_delete(dentry);
1755d45b9d8bSTrond Myklebust }
1756d45b9d8bSTrond Myklebust 
1757597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
17581da177e4SLinus Torvalds {
17591da177e4SLinus Torvalds 	int error;
17601da177e4SLinus Torvalds 
17611e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
17626de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17631da177e4SLinus Torvalds 
17641ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
17652b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1766884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
17671da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17681da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1769ba6c0592STrond Myklebust 		switch (error) {
1770ba6c0592STrond Myklebust 		case 0:
17712b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1772ba6c0592STrond Myklebust 			break;
1773ba6c0592STrond Myklebust 		case -ENOENT:
1774d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1775ba6c0592STrond Myklebust 		}
1776884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1777ba6c0592STrond Myklebust 	} else
1778ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17791ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	return error;
17821da177e4SLinus Torvalds }
1783ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds /*
17861da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17871da177e4SLinus Torvalds  * and after checking that the file has only one user.
17881da177e4SLinus Torvalds  *
17891da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17901da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17911da177e4SLinus Torvalds  */
17921da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17931da177e4SLinus Torvalds {
17942b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
17952b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
17961da177e4SLinus Torvalds 	int error = -EBUSY;
17971da177e4SLinus Torvalds 
17986de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
18011da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
18021da177e4SLinus Torvalds 		error = 0;
18031da177e4SLinus Torvalds 		goto out;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 
18061ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
18071da177e4SLinus Torvalds 	if (inode != NULL) {
180857ec14c5SBryan Schumaker 		NFS_PROTO(inode)->return_delegation(inode);
18091da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
18101da177e4SLinus Torvalds 		if (error == 0)
18111b83d707STrond Myklebust 			nfs_drop_nlink(inode);
18121da177e4SLinus Torvalds 	} else
18131da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1814d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1815d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
18161ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
18171da177e4SLinus Torvalds out:
18181da177e4SLinus Torvalds 	return error;
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
18221da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
18231da177e4SLinus Torvalds  *
18241da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
18251da177e4SLinus Torvalds  */
1826597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
18271da177e4SLinus Torvalds {
18281da177e4SLinus Torvalds 	int error;
18291da177e4SLinus Torvalds 	int need_rehash = 0;
18301da177e4SLinus Torvalds 
18311e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
18326de1472fSAl Viro 		dir->i_ino, dentry);
18331da177e4SLinus Torvalds 
18341ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
18351da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
183684d08fa8SAl Viro 	if (d_count(dentry) > 1) {
18371da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1838ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
18392b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
18401da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
18411ca42382STrond Myklebust 		goto out;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
18441da177e4SLinus Torvalds 		__d_drop(dentry);
18451da177e4SLinus Torvalds 		need_rehash = 1;
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
18481da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1849d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
18501da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18511da177e4SLinus Torvalds 	} else if (need_rehash)
18521da177e4SLinus Torvalds 		d_rehash(dentry);
18531ca42382STrond Myklebust out:
18541ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
18551da177e4SLinus Torvalds 	return error;
18561da177e4SLinus Torvalds }
1857ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
18581da177e4SLinus Torvalds 
1859873101b3SChuck Lever /*
1860873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1861873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1862873101b3SChuck Lever  * using prepare_write/commit_write.
1863873101b3SChuck Lever  *
1864873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1865873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1866873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1867873101b3SChuck Lever  * symlink request has completed on the server.
1868873101b3SChuck Lever  *
1869873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1870873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1871873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1872873101b3SChuck Lever  * and move the raw page into its mapping.
1873873101b3SChuck Lever  */
1874597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18751da177e4SLinus Torvalds {
1876873101b3SChuck Lever 	struct page *page;
1877873101b3SChuck Lever 	char *kaddr;
18781da177e4SLinus Torvalds 	struct iattr attr;
1879873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
18801da177e4SLinus Torvalds 	int error;
18811da177e4SLinus Torvalds 
18821e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
18836de1472fSAl Viro 		dir->i_ino, dentry, symname);
18841da177e4SLinus Torvalds 
1885873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1886873101b3SChuck Lever 		return -ENAMETOOLONG;
18871da177e4SLinus Torvalds 
1888873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1889873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18901da177e4SLinus Torvalds 
1891e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
189276566991STrond Myklebust 	if (!page)
1893873101b3SChuck Lever 		return -ENOMEM;
1894873101b3SChuck Lever 
1895e8ecde25SAl Viro 	kaddr = page_address(page);
1896873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1897873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1898873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1899873101b3SChuck Lever 
19001ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
190194a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
19021ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
1903873101b3SChuck Lever 	if (error != 0) {
19041e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
1905873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
19066de1472fSAl Viro 			dentry, symname, error);
19071da177e4SLinus Torvalds 		d_drop(dentry);
1908873101b3SChuck Lever 		__free_page(page);
19091da177e4SLinus Torvalds 		return error;
19101da177e4SLinus Torvalds 	}
19111da177e4SLinus Torvalds 
1912873101b3SChuck Lever 	/*
1913873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1914873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1915873101b3SChuck Lever 	 */
19162b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
1917873101b3SChuck Lever 							GFP_KERNEL)) {
1918873101b3SChuck Lever 		SetPageUptodate(page);
1919873101b3SChuck Lever 		unlock_page(page);
1920a0b54addSRafael Aquini 		/*
1921a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
1922a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
1923a0b54addSRafael Aquini 		 */
192409cbfeafSKirill A. Shutemov 		put_page(page);
1925873101b3SChuck Lever 	} else
1926873101b3SChuck Lever 		__free_page(page);
1927873101b3SChuck Lever 
1928873101b3SChuck Lever 	return 0;
1929873101b3SChuck Lever }
1930ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
1931873101b3SChuck Lever 
1932597d9289SBryan Schumaker int
19331da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
19341da177e4SLinus Torvalds {
19352b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
19361da177e4SLinus Torvalds 	int error;
19371da177e4SLinus Torvalds 
19386de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
19396de1472fSAl Viro 		old_dentry, dentry);
19401da177e4SLinus Torvalds 
19411fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
194257ec14c5SBryan Schumaker 	NFS_PROTO(inode)->return_delegation(inode);
19439a3936aaSTrond Myklebust 
19449697d234STrond Myklebust 	d_drop(dentry);
19451da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1946cf809556STrond Myklebust 	if (error == 0) {
19477de9c6eeSAl Viro 		ihold(inode);
19489697d234STrond Myklebust 		d_add(dentry, inode);
1949cf809556STrond Myklebust 	}
19501fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
19511da177e4SLinus Torvalds 	return error;
19521da177e4SLinus Torvalds }
1953ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
19541da177e4SLinus Torvalds 
1955920b4530SBenjamin Coddington static void
1956920b4530SBenjamin Coddington nfs_complete_rename(struct rpc_task *task, struct nfs_renamedata *data)
1957920b4530SBenjamin Coddington {
1958920b4530SBenjamin Coddington 	struct dentry *old_dentry = data->old_dentry;
1959920b4530SBenjamin Coddington 	struct dentry *new_dentry = data->new_dentry;
1960920b4530SBenjamin Coddington 	struct inode *old_inode = d_inode(old_dentry);
1961920b4530SBenjamin Coddington 	struct inode *new_inode = d_inode(new_dentry);
1962920b4530SBenjamin Coddington 
1963920b4530SBenjamin Coddington 	nfs_mark_for_revalidate(old_inode);
1964920b4530SBenjamin Coddington 
1965920b4530SBenjamin Coddington 	switch (task->tk_status) {
1966920b4530SBenjamin Coddington 	case 0:
1967920b4530SBenjamin Coddington 		if (new_inode != NULL)
1968920b4530SBenjamin Coddington 			nfs_drop_nlink(new_inode);
1969920b4530SBenjamin Coddington 		d_move(old_dentry, new_dentry);
1970920b4530SBenjamin Coddington 		nfs_set_verifier(new_dentry,
1971920b4530SBenjamin Coddington 					nfs_save_change_attribute(data->new_dir));
1972920b4530SBenjamin Coddington 		break;
1973920b4530SBenjamin Coddington 	case -ENOENT:
1974920b4530SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
1975920b4530SBenjamin Coddington 	}
1976920b4530SBenjamin Coddington }
1977920b4530SBenjamin Coddington 
19781da177e4SLinus Torvalds /*
19791da177e4SLinus Torvalds  * RENAME
19801da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
19811da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
19821da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
19831da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
19841da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
19851da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
19861da177e4SLinus Torvalds  *
19871da177e4SLinus Torvalds  * FIXED.
19881da177e4SLinus Torvalds  *
19891da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
19901da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
19911da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
19921da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
19931da177e4SLinus Torvalds  * using the inode layer
19941da177e4SLinus Torvalds  *
19951da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
19961da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
19971da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
19981da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
19991da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
20001da177e4SLinus Torvalds  * the rename.
20011da177e4SLinus Torvalds  */
2002597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
20031cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
20041cd66c93SMiklos Szeredi 	       unsigned int flags)
20051da177e4SLinus Torvalds {
20062b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
20072b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2008d4ea7e3cSBenjamin Coddington 	struct dentry *dentry = NULL;
200980a491fdSJeff Layton 	struct rpc_task *task;
20101da177e4SLinus Torvalds 	int error = -EBUSY;
20111da177e4SLinus Torvalds 
20121cd66c93SMiklos Szeredi 	if (flags)
20131cd66c93SMiklos Szeredi 		return -EINVAL;
20141cd66c93SMiklos Szeredi 
20156de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
20166de1472fSAl Viro 		 old_dentry, new_dentry,
201784d08fa8SAl Viro 		 d_count(new_dentry));
20181da177e4SLinus Torvalds 
201970ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
20201da177e4SLinus Torvalds 	/*
202128f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
202228f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
202328f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
202428f79a1aSMiklos Szeredi 	 * the new target.
20251da177e4SLinus Torvalds 	 */
202627226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
202727226104SMiklos Szeredi 		/*
202827226104SMiklos Szeredi 		 * To prevent any new references to the target during the
202927226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
203027226104SMiklos Szeredi 		 */
2031d4ea7e3cSBenjamin Coddington 		if (!d_unhashed(new_dentry))
203227226104SMiklos Szeredi 			d_drop(new_dentry);
203327226104SMiklos Szeredi 
203484d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
20351da177e4SLinus Torvalds 			int err;
203627226104SMiklos Szeredi 
20371da177e4SLinus Torvalds 			/* copy the target dentry's name */
20381da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
20391da177e4SLinus Torvalds 					 &new_dentry->d_name);
20401da177e4SLinus Torvalds 			if (!dentry)
20411da177e4SLinus Torvalds 				goto out;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
20441da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
204524e93025SMiklos Szeredi 			if (err)
20461da177e4SLinus Torvalds 				goto out;
204724e93025SMiklos Szeredi 
204824e93025SMiklos Szeredi 			new_dentry = dentry;
204924e93025SMiklos Szeredi 			new_inode = NULL;
2050b1e4adf4STrond Myklebust 		}
205127226104SMiklos Szeredi 	}
20521da177e4SLinus Torvalds 
205357ec14c5SBryan Schumaker 	NFS_PROTO(old_inode)->return_delegation(old_inode);
2054b1e4adf4STrond Myklebust 	if (new_inode != NULL)
205557ec14c5SBryan Schumaker 		NFS_PROTO(new_inode)->return_delegation(new_inode);
20561da177e4SLinus Torvalds 
2057920b4530SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry,
2058920b4530SBenjamin Coddington 					nfs_complete_rename);
205980a491fdSJeff Layton 	if (IS_ERR(task)) {
206080a491fdSJeff Layton 		error = PTR_ERR(task);
206180a491fdSJeff Layton 		goto out;
206280a491fdSJeff Layton 	}
206380a491fdSJeff Layton 
206480a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2065818a8dbeSBenjamin Coddington 	if (error != 0) {
2066818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2067818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2068818a8dbeSBenjamin Coddington 		smp_wmb();
2069818a8dbeSBenjamin Coddington 	} else
207080a491fdSJeff Layton 		error = task->tk_status;
207180a491fdSJeff Layton 	rpc_put_task(task);
20721da177e4SLinus Torvalds out:
207370ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
207470ded201STrond Myklebust 			new_dir, new_dentry, error);
20751da177e4SLinus Torvalds 	/* new dentry created? */
20761da177e4SLinus Torvalds 	if (dentry)
20771da177e4SLinus Torvalds 		dput(dentry);
20781da177e4SLinus Torvalds 	return error;
20791da177e4SLinus Torvalds }
2080ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
20811da177e4SLinus Torvalds 
2082cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2083cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2084cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2085cfcea3e8STrond Myklebust 
20863a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
20873a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
20883a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
20893a505845STrond Myklebust 
20901c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
20911c3c07e9STrond Myklebust {
20921c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
2093f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
20944e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2095cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
20964e857c58SPeter Zijlstra 	smp_mb__after_atomic();
20971c3c07e9STrond Myklebust }
20981c3c07e9STrond Myklebust 
20991a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
21001a81bb8aSTrond Myklebust {
21011a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
21021a81bb8aSTrond Myklebust 
21031a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
21041a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
21051a81bb8aSTrond Myklebust 		list_del(&cache->lru);
21061a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
21071a81bb8aSTrond Myklebust 	}
21081a81bb8aSTrond Myklebust }
21091a81bb8aSTrond Myklebust 
21103a505845STrond Myklebust static unsigned long
21113a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2112979df72eSTrond Myklebust {
2113979df72eSTrond Myklebust 	LIST_HEAD(head);
2114aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2115979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
21161ab6c499SDave Chinner 	long freed = 0;
2117979df72eSTrond Myklebust 
2118a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2119aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2120979df72eSTrond Myklebust 		struct inode *inode;
2121979df72eSTrond Myklebust 
2122979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2123979df72eSTrond Myklebust 			break;
21249c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2125979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2126979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2127979df72eSTrond Myklebust 			goto remove_lru_entry;
2128979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2129979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2130979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2131979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
21321ab6c499SDave Chinner 		freed++;
2133979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2134979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2135979df72eSTrond Myklebust 					&nfs_access_lru_list);
2136979df72eSTrond Myklebust 		else {
2137979df72eSTrond Myklebust remove_lru_entry:
2138979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
21394e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2140979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
21414e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2142979df72eSTrond Myklebust 		}
214359844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2144979df72eSTrond Myklebust 	}
2145979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
21461a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
21471ab6c499SDave Chinner 	return freed;
21481ab6c499SDave Chinner }
21491ab6c499SDave Chinner 
21501ab6c499SDave Chinner unsigned long
21513a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
21523a505845STrond Myklebust {
21533a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
21543a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
21553a505845STrond Myklebust 
21563a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
21573a505845STrond Myklebust 		return SHRINK_STOP;
21583a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
21593a505845STrond Myklebust }
21603a505845STrond Myklebust 
21613a505845STrond Myklebust 
21623a505845STrond Myklebust unsigned long
21631ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
21641ab6c499SDave Chinner {
216555f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2166979df72eSTrond Myklebust }
2167979df72eSTrond Myklebust 
21683a505845STrond Myklebust static void
21693a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
21703a505845STrond Myklebust {
21713a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
21723a505845STrond Myklebust 	unsigned long diff;
21733a505845STrond Myklebust 	unsigned int nr_to_scan;
21743a505845STrond Myklebust 
21753a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
21763a505845STrond Myklebust 		return;
21773a505845STrond Myklebust 	nr_to_scan = 100;
21783a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
21793a505845STrond Myklebust 	if (diff < nr_to_scan)
21803a505845STrond Myklebust 		nr_to_scan = diff;
21813a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
21823a505845STrond Myklebust }
21833a505845STrond Myklebust 
21841a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
21851c3c07e9STrond Myklebust {
21861c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21871a81bb8aSTrond Myklebust 	struct rb_node *n;
21881c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21891c3c07e9STrond Myklebust 
21901c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
21911c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
21921c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
21931c3c07e9STrond Myklebust 		rb_erase(n, root_node);
21941a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
21951c3c07e9STrond Myklebust 	}
21961c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
21971c3c07e9STrond Myklebust }
21981c3c07e9STrond Myklebust 
21991c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
22001c3c07e9STrond Myklebust {
22011a81bb8aSTrond Myklebust 	LIST_HEAD(head);
22021a81bb8aSTrond Myklebust 
22031a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
22041a81bb8aSTrond Myklebust 		return;
2205cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2206cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
22071a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2208cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2209cfcea3e8STrond Myklebust 
22101c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
22111a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
22121a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
22131a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22141a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22151c3c07e9STrond Myklebust }
22161c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
22171c3c07e9STrond Myklebust 
22181c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
22191c3c07e9STrond Myklebust {
22201c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
22211c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
22221c3c07e9STrond Myklebust 
22231c3c07e9STrond Myklebust 	while (n != NULL) {
22241c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
22251c3c07e9STrond Myklebust 
22261c3c07e9STrond Myklebust 		if (cred < entry->cred)
22271c3c07e9STrond Myklebust 			n = n->rb_left;
22281c3c07e9STrond Myklebust 		else if (cred > entry->cred)
22291c3c07e9STrond Myklebust 			n = n->rb_right;
22301c3c07e9STrond Myklebust 		else
22311c3c07e9STrond Myklebust 			return entry;
22321c3c07e9STrond Myklebust 	}
22331c3c07e9STrond Myklebust 	return NULL;
22341c3c07e9STrond Myklebust }
22351c3c07e9STrond Myklebust 
223657b69181STrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block)
22371da177e4SLinus Torvalds {
223855296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
22391c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
224057b69181STrond Myklebust 	bool retry = true;
224157b69181STrond Myklebust 	int err;
22421da177e4SLinus Torvalds 
22431c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
224457b69181STrond Myklebust 	for(;;) {
22451c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
22461c3c07e9STrond Myklebust 			goto out_zap;
22471c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
224857b69181STrond Myklebust 		err = -ENOENT;
22491c3c07e9STrond Myklebust 		if (cache == NULL)
22501c3c07e9STrond Myklebust 			goto out;
225157b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
225221c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
225357b69181STrond Myklebust 			break;
225457b69181STrond Myklebust 		err = -ECHILD;
225557b69181STrond Myklebust 		if (!may_block)
225657b69181STrond Myklebust 			goto out;
225757b69181STrond Myklebust 		if (!retry)
225857b69181STrond Myklebust 			goto out_zap;
225957b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
226057b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
226157b69181STrond Myklebust 		if (err)
226257b69181STrond Myklebust 			return err;
226357b69181STrond Myklebust 		spin_lock(&inode->i_lock);
226457b69181STrond Myklebust 		retry = false;
226557b69181STrond Myklebust 	}
22661c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
22671c3c07e9STrond Myklebust 	res->cred = cache->cred;
22681c3c07e9STrond Myklebust 	res->mask = cache->mask;
2269cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
22701c3c07e9STrond Myklebust 	err = 0;
22711c3c07e9STrond Myklebust out:
22721c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
22731c3c07e9STrond Myklebust 	return err;
22741c3c07e9STrond Myklebust out_zap:
22751a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
22761a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
22771c3c07e9STrond Myklebust 	return -ENOENT;
22781c3c07e9STrond Myklebust }
22791c3c07e9STrond Myklebust 
2280f682a398SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2281f682a398SNeilBrown {
2282f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2283f682a398SNeilBrown 	 * but do it without locking.
2284f682a398SNeilBrown 	 */
2285f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2286f682a398SNeilBrown 	struct nfs_access_entry *cache;
2287f682a398SNeilBrown 	int err = -ECHILD;
2288f682a398SNeilBrown 	struct list_head *lh;
2289f682a398SNeilBrown 
2290f682a398SNeilBrown 	rcu_read_lock();
2291f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2292f682a398SNeilBrown 		goto out;
2293f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2294f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2295f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2296f682a398SNeilBrown 	    cred != cache->cred)
2297f682a398SNeilBrown 		cache = NULL;
2298f682a398SNeilBrown 	if (cache == NULL)
2299f682a398SNeilBrown 		goto out;
230021c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2301f682a398SNeilBrown 		goto out;
2302f682a398SNeilBrown 	res->jiffies = cache->jiffies;
2303f682a398SNeilBrown 	res->cred = cache->cred;
2304f682a398SNeilBrown 	res->mask = cache->mask;
230521c3ba7eSTrond Myklebust 	err = 0;
2306f682a398SNeilBrown out:
2307f682a398SNeilBrown 	rcu_read_unlock();
2308f682a398SNeilBrown 	return err;
2309f682a398SNeilBrown }
2310f682a398SNeilBrown 
23111c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
23121c3c07e9STrond Myklebust {
2313cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2314cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
23151c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
23161c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
23171c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
23181c3c07e9STrond Myklebust 
23191c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
23201c3c07e9STrond Myklebust 	while (*p != NULL) {
23211c3c07e9STrond Myklebust 		parent = *p;
23221c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
23231c3c07e9STrond Myklebust 
23241c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
23251c3c07e9STrond Myklebust 			p = &parent->rb_left;
23261c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
23271c3c07e9STrond Myklebust 			p = &parent->rb_right;
23281c3c07e9STrond Myklebust 		else
23291c3c07e9STrond Myklebust 			goto found;
23301c3c07e9STrond Myklebust 	}
23311c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
23321c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2333cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
23341c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23351c3c07e9STrond Myklebust 	return;
23361c3c07e9STrond Myklebust found:
23371c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2338cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2339cfcea3e8STrond Myklebust 	list_del(&entry->lru);
23401c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
23411c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
23446168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
23451da177e4SLinus Torvalds {
23461c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
23471c3c07e9STrond Myklebust 	if (cache == NULL)
23481c3c07e9STrond Myklebust 		return;
23491c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
23501da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
23511c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
23521da177e4SLinus Torvalds 	cache->mask = set->mask;
23531c3c07e9STrond Myklebust 
2354f682a398SNeilBrown 	/* The above field assignments must be visible
2355f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2356f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2357f682a398SNeilBrown 	 */
2358f682a398SNeilBrown 	smp_wmb();
23591c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2360cfcea3e8STrond Myklebust 
2361cfcea3e8STrond Myklebust 	/* Update accounting */
23624e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2363cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
23644e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2365cfcea3e8STrond Myklebust 
2366cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
23671a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2368cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
23691a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
23701a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
23711a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2372cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2373cfcea3e8STrond Myklebust 	}
23743a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
23751da177e4SLinus Torvalds }
23766168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
23776168f62cSWeston Andros Adamson 
2378*15d4b73aSTrond Myklebust #define NFS_MAY_READ (NFS4_ACCESS_READ)
2379*15d4b73aSTrond Myklebust #define NFS_MAY_WRITE (NFS4_ACCESS_MODIFY | \
2380*15d4b73aSTrond Myklebust 		NFS4_ACCESS_EXTEND | \
2381*15d4b73aSTrond Myklebust 		NFS4_ACCESS_DELETE)
2382*15d4b73aSTrond Myklebust #define NFS_MAY_LOOKUP (NFS4_ACCESS_LOOKUP)
2383*15d4b73aSTrond Myklebust #define NFS_MAY_EXECUTE (NFS4_ACCESS_EXECUTE)
2384*15d4b73aSTrond Myklebust static int
2385*15d4b73aSTrond Myklebust nfs_access_calc_mask(u32 access_result)
2386*15d4b73aSTrond Myklebust {
2387*15d4b73aSTrond Myklebust 	int mask = 0;
2388*15d4b73aSTrond Myklebust 
2389*15d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
2390*15d4b73aSTrond Myklebust 		mask |= MAY_READ;
2391*15d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_WRITE)
2392*15d4b73aSTrond Myklebust 		mask |= MAY_WRITE;
2393*15d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_LOOKUP)
2394*15d4b73aSTrond Myklebust 		mask |= MAY_EXEC;
2395*15d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_EXECUTE)
2396*15d4b73aSTrond Myklebust 		mask |= MAY_EXEC;
2397*15d4b73aSTrond Myklebust 	return mask;
2398*15d4b73aSTrond Myklebust }
2399*15d4b73aSTrond Myklebust 
24006168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
24016168f62cSWeston Andros Adamson {
2402*15d4b73aSTrond Myklebust 	entry->mask = nfs_access_calc_mask(access_result);
24036168f62cSWeston Andros Adamson }
24046168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
24071da177e4SLinus Torvalds {
24081da177e4SLinus Torvalds 	struct nfs_access_entry cache;
240957b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
24101da177e4SLinus Torvalds 	int status;
24111da177e4SLinus Torvalds 
2412f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2413f4ce1299STrond Myklebust 
2414f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2415f682a398SNeilBrown 	if (status != 0)
241657b69181STrond Myklebust 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
24171da177e4SLinus Torvalds 	if (status == 0)
2418f4ce1299STrond Myklebust 		goto out_cached;
24191da177e4SLinus Torvalds 
2420f3324a2aSNeilBrown 	status = -ECHILD;
242157b69181STrond Myklebust 	if (!may_block)
2422f3324a2aSNeilBrown 		goto out;
2423f3324a2aSNeilBrown 
24241da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
24251da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
24261da177e4SLinus Torvalds 	cache.cred = cred;
24271da177e4SLinus Torvalds 	cache.jiffies = jiffies;
24281da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2429a71ee337SSuresh Jayaraman 	if (status != 0) {
2430a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2431a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2432a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2433a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2434a71ee337SSuresh Jayaraman 		}
2435f4ce1299STrond Myklebust 		goto out;
2436a71ee337SSuresh Jayaraman 	}
24371da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2438f4ce1299STrond Myklebust out_cached:
2439f4ce1299STrond Myklebust 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2440f4ce1299STrond Myklebust 		status = -EACCES;
24411da177e4SLinus Torvalds out:
2442f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2443f4ce1299STrond Myklebust 	return status;
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds 
2446af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2447af22f94aSTrond Myklebust {
2448af22f94aSTrond Myklebust 	int mask = 0;
2449af22f94aSTrond Myklebust 
2450f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2451f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2452f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2453f8d9a897SWeston Andros Adamson 	} else {
24548a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2455af22f94aSTrond Myklebust 			mask |= MAY_READ;
24568a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2457af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2458f8d9a897SWeston Andros Adamson 	}
2459f8d9a897SWeston Andros Adamson 
2460af22f94aSTrond Myklebust 	return mask;
2461af22f94aSTrond Myklebust }
2462af22f94aSTrond Myklebust 
2463af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2464af22f94aSTrond Myklebust {
2465af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2466af22f94aSTrond Myklebust }
246789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2468af22f94aSTrond Myklebust 
24695c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
24705c5fc09aSTrond Myklebust {
24715c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
247221c3ba7eSTrond Myklebust 	int ret = 0;
24735c5fc09aSTrond Myklebust 
247421c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) {
24755c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
247621c3ba7eSTrond Myklebust 			return -ECHILD;
247721c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
247821c3ba7eSTrond Myklebust 	}
24795c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
24805c5fc09aSTrond Myklebust 		ret = -EACCES;
24815c5fc09aSTrond Myklebust 	return ret;
24825c5fc09aSTrond Myklebust }
24835c5fc09aSTrond Myklebust 
248410556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
24851da177e4SLinus Torvalds {
24861da177e4SLinus Torvalds 	struct rpc_cred *cred;
24871da177e4SLinus Torvalds 	int res = 0;
24881da177e4SLinus Torvalds 
248991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
249091d5b470SChuck Lever 
2491e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
24921da177e4SLinus Torvalds 		goto out;
24931da177e4SLinus Torvalds 	/* Is this sys_access() ? */
24949cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
24951da177e4SLinus Torvalds 		goto force_lookup;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
24981da177e4SLinus Torvalds 		case S_IFLNK:
24991da177e4SLinus Torvalds 			goto out;
25001da177e4SLinus Torvalds 		case S_IFREG:
2501762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2502762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2503762674f8STrond Myklebust 				return 0;
25041da177e4SLinus Torvalds 			break;
25051da177e4SLinus Torvalds 		case S_IFDIR:
25061da177e4SLinus Torvalds 			/*
25071da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
25081da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
25091da177e4SLinus Torvalds 			 */
25101da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
25111da177e4SLinus Torvalds 				goto out;
25121da177e4SLinus Torvalds 	}
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds force_lookup:
25151da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
25161da177e4SLinus Torvalds 		goto out_notsup;
25171da177e4SLinus Torvalds 
2518f3324a2aSNeilBrown 	/* Always try fast lookups first */
2519f3324a2aSNeilBrown 	rcu_read_lock();
2520f3324a2aSNeilBrown 	cred = rpc_lookup_cred_nonblock();
2521f3324a2aSNeilBrown 	if (!IS_ERR(cred))
2522f3324a2aSNeilBrown 		res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2523f3324a2aSNeilBrown 	else
2524f3324a2aSNeilBrown 		res = PTR_ERR(cred);
2525f3324a2aSNeilBrown 	rcu_read_unlock();
2526f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2527f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
252898a8e323STrond Myklebust 		cred = rpc_lookup_cred();
25291da177e4SLinus Torvalds 		if (!IS_ERR(cred)) {
25301da177e4SLinus Torvalds 			res = nfs_do_access(inode, cred, mask);
25311da177e4SLinus Torvalds 			put_rpccred(cred);
25321da177e4SLinus Torvalds 		} else
25331da177e4SLinus Torvalds 			res = PTR_ERR(cred);
2534f3324a2aSNeilBrown 	}
25351da177e4SLinus Torvalds out:
25365c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
25375c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2538f696a365SMiklos Szeredi 
25391e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
25401e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
25411da177e4SLinus Torvalds 	return res;
25421da177e4SLinus Torvalds out_notsup:
2543d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2544d51ac1a8SNeilBrown 		return -ECHILD;
2545d51ac1a8SNeilBrown 
25461da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
25471da177e4SLinus Torvalds 	if (res == 0)
25482830ba7fSAl Viro 		res = generic_permission(inode, mask);
25491e7cb3dcSChuck Lever 	goto out;
25501da177e4SLinus Torvalds }
2551ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
25521da177e4SLinus Torvalds 
25531da177e4SLinus Torvalds /*
25541da177e4SLinus Torvalds  * Local variables:
25551da177e4SLinus Torvalds  *  version-control: t
25561da177e4SLinus Torvalds  *  kept-new-versions: 5
25571da177e4SLinus Torvalds  * End:
25581da177e4SLinus Torvalds  */
2559