xref: /openbmc/linux/fs/nfs/dir.c (revision 480c2006ebb44ae03165695db7b3e38c04e0d102)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/stat.h>
231da177e4SLinus Torvalds #include <linux/fcntl.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
291da177e4SLinus Torvalds #include <linux/nfs_fs.h>
301da177e4SLinus Torvalds #include <linux/nfs_mount.h>
311da177e4SLinus Torvalds #include <linux/pagemap.h>
32873101b3SChuck Lever #include <linux/pagevec.h>
331da177e4SLinus Torvalds #include <linux/namei.h>
3454ceac45SDavid Howells #include <linux/mount.h>
35e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3604e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
3764c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include "delegation.h"
4091d5b470SChuck Lever #include "iostat.h"
414c30d56eSAdrian Bunk #include "internal.h"
42cd9a1c0eSTrond Myklebust #include "fscache.h"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
47*480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
481da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
491da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
501da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
511da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
521da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
531da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
541da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
551da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
561da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
571da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
581da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
597ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int);
60f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
6111de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
621da177e4SLinus Torvalds 
634b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
64f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
651da177e4SLinus Torvalds 	.read		= generic_read_dir,
661da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
671da177e4SLinus Torvalds 	.open		= nfs_opendir,
68*480c2006SBryan Schumaker 	.release	= nfs_closedir,
691da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
7292e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
731da177e4SLinus Torvalds 	.create		= nfs_create,
741da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
751da177e4SLinus Torvalds 	.link		= nfs_link,
761da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
771da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
781da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
791da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
801da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
811da177e4SLinus Torvalds 	.rename		= nfs_rename,
821da177e4SLinus Torvalds 	.permission	= nfs_permission,
831da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
841da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
8711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
8811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
89d1bacf9eSBryan Schumaker };
90d1bacf9eSBryan Schumaker 
91b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
9292e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
93b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
94b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
95b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
96b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
97b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
98b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
99b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
100b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
101b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
102b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
103b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
104b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
105b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
106b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
107b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
108b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
109b7fa0554SAndreas Gruenbacher };
110b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
111b7fa0554SAndreas Gruenbacher 
1121da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
115c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
11692e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
117c0204fd2STrond Myklebust 	.create		= nfs_open_create,
1181da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
1191da177e4SLinus Torvalds 	.link		= nfs_link,
1201da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1211da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1221da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1231da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1241da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1251da177e4SLinus Torvalds 	.rename		= nfs_rename,
1261da177e4SLinus Torvalds 	.permission	= nfs_permission,
1271da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1281da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
12964c2ce8bSAneesh Kumar K.V 	.getxattr	= generic_getxattr,
13064c2ce8bSAneesh Kumar K.V 	.setxattr	= generic_setxattr,
13164c2ce8bSAneesh Kumar K.V 	.listxattr	= generic_listxattr,
13264c2ce8bSAneesh Kumar K.V 	.removexattr	= generic_removexattr,
1331da177e4SLinus Torvalds };
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1361da177e4SLinus Torvalds 
137*480c2006SBryan Schumaker static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct rpc_cred *cred)
138*480c2006SBryan Schumaker {
139*480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
140*480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
141*480c2006SBryan Schumaker 	if (ctx != NULL) {
142*480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
143*480c2006SBryan Schumaker 		ctx->cred = get_rpccred(cred);
144*480c2006SBryan Schumaker 	} else
145*480c2006SBryan Schumaker 		ctx = ERR_PTR(-ENOMEM);
146*480c2006SBryan Schumaker 	return ctx;
147*480c2006SBryan Schumaker }
148*480c2006SBryan Schumaker 
149*480c2006SBryan Schumaker static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
150*480c2006SBryan Schumaker {
151*480c2006SBryan Schumaker 	put_rpccred(ctx->cred);
152*480c2006SBryan Schumaker 	kfree(ctx);
153*480c2006SBryan Schumaker }
154*480c2006SBryan Schumaker 
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  * Open file
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds static int
1591da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1601da177e4SLinus Torvalds {
161*480c2006SBryan Schumaker 	int res = 0;
162*480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
163*480c2006SBryan Schumaker 	struct rpc_cred *cred;
1641da177e4SLinus Torvalds 
1656da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
166cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
167cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
168cc0dd2d1SChuck Lever 
169cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1701e7cb3dcSChuck Lever 
171*480c2006SBryan Schumaker 	cred = rpc_lookup_cred();
172*480c2006SBryan Schumaker 	if (IS_ERR(cred))
173*480c2006SBryan Schumaker 		return PTR_ERR(cred);
174*480c2006SBryan Schumaker 	ctx = alloc_nfs_open_dir_context(cred);
175*480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
176*480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
177*480c2006SBryan Schumaker 		goto out;
178*480c2006SBryan Schumaker 	}
179*480c2006SBryan Schumaker 	filp->private_data = ctx;
180f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
181f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
182f5a73672SNeil Brown 		 * have been called, so we need to refresh the
183f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
184f5a73672SNeil Brown 		 */
185f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
186f5a73672SNeil Brown 	}
187*480c2006SBryan Schumaker out:
188*480c2006SBryan Schumaker 	put_rpccred(cred);
1891da177e4SLinus Torvalds 	return res;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
192*480c2006SBryan Schumaker static int
193*480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
194*480c2006SBryan Schumaker {
195*480c2006SBryan Schumaker 	put_nfs_open_dir_context(filp->private_data);
196*480c2006SBryan Schumaker 	return 0;
197*480c2006SBryan Schumaker }
198*480c2006SBryan Schumaker 
199d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
200d1bacf9eSBryan Schumaker 	u64 cookie;
201d1bacf9eSBryan Schumaker 	u64 ino;
202d1bacf9eSBryan Schumaker 	struct qstr string;
2030b26a0bfSTrond Myklebust 	unsigned char d_type;
204d1bacf9eSBryan Schumaker };
205d1bacf9eSBryan Schumaker 
206d1bacf9eSBryan Schumaker struct nfs_cache_array {
207d1bacf9eSBryan Schumaker 	unsigned int size;
208d1bacf9eSBryan Schumaker 	int eof_index;
209d1bacf9eSBryan Schumaker 	u64 last_cookie;
210d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
211d1bacf9eSBryan Schumaker };
212d1bacf9eSBryan Schumaker 
213573c4e1eSChuck Lever typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
2141da177e4SLinus Torvalds typedef struct {
2151da177e4SLinus Torvalds 	struct file	*file;
2161da177e4SLinus Torvalds 	struct page	*page;
2171da177e4SLinus Torvalds 	unsigned long	page_index;
218f0dd2136STrond Myklebust 	u64		*dir_cookie;
2190aded708STrond Myklebust 	u64		last_cookie;
220f0dd2136STrond Myklebust 	loff_t		current_index;
2211da177e4SLinus Torvalds 	decode_dirent_t	decode;
222d1bacf9eSBryan Schumaker 
2231f4eab7eSNeil Brown 	unsigned long	timestamp;
2244704f0e2STrond Myklebust 	unsigned long	gencount;
225d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
226d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
227d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
2281da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
2291da177e4SLinus Torvalds 
230d1bacf9eSBryan Schumaker /*
231d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
2321da177e4SLinus Torvalds  */
2331da177e4SLinus Torvalds static
234d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
2351da177e4SLinus Torvalds {
2368cd51a0cSTrond Myklebust 	void *ptr;
237d1bacf9eSBryan Schumaker 	if (page == NULL)
238d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
2398cd51a0cSTrond Myklebust 	ptr = kmap(page);
2408cd51a0cSTrond Myklebust 	if (ptr == NULL)
2418cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
2428cd51a0cSTrond Myklebust 	return ptr;
243d1bacf9eSBryan Schumaker }
244d1bacf9eSBryan Schumaker 
245d1bacf9eSBryan Schumaker static
246d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
247d1bacf9eSBryan Schumaker {
248d1bacf9eSBryan Schumaker 	kunmap(page);
249d1bacf9eSBryan Schumaker }
250d1bacf9eSBryan Schumaker 
251d1bacf9eSBryan Schumaker /*
252d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
253d1bacf9eSBryan Schumaker  */
254d1bacf9eSBryan Schumaker static
25511de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
256d1bacf9eSBryan Schumaker {
25711de3b11STrond Myklebust 	struct nfs_cache_array *array;
258d1bacf9eSBryan Schumaker 	int i;
2598cd51a0cSTrond Myklebust 
26011de3b11STrond Myklebust 	array = kmap_atomic(page, KM_USER0);
261d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
262d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
26311de3b11STrond Myklebust 	kunmap_atomic(array, KM_USER0);
264d1bacf9eSBryan Schumaker }
265d1bacf9eSBryan Schumaker 
266d1bacf9eSBryan Schumaker /*
267d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
268d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
269d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
270d1bacf9eSBryan Schumaker  */
271d1bacf9eSBryan Schumaker static
2724a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
273d1bacf9eSBryan Schumaker {
274d1bacf9eSBryan Schumaker 	string->len = len;
275d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2764a201d6eSTrond Myklebust 	if (string->name == NULL)
2774a201d6eSTrond Myklebust 		return -ENOMEM;
27804e4bd1cSCatalin Marinas 	/*
27904e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
28004e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
28104e4bd1cSCatalin Marinas 	 */
28204e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2834a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2844a201d6eSTrond Myklebust 	return 0;
285d1bacf9eSBryan Schumaker }
286d1bacf9eSBryan Schumaker 
287d1bacf9eSBryan Schumaker static
288d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
289d1bacf9eSBryan Schumaker {
290d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2914a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2924a201d6eSTrond Myklebust 	int ret;
2934a201d6eSTrond Myklebust 
294d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
295d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
296d1bacf9eSBryan Schumaker 
2974a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2983020093fSTrond Myklebust 
2993020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
3003020093fSTrond Myklebust 	ret = -ENOSPC;
3013020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
3023020093fSTrond Myklebust 		goto out;
3033020093fSTrond Myklebust 
3044a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
3054a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
3060b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
3074a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
3084a201d6eSTrond Myklebust 	if (ret)
3094a201d6eSTrond Myklebust 		goto out;
310d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
3118cd51a0cSTrond Myklebust 	array->size++;
31247c716cbSTrond Myklebust 	if (entry->eof != 0)
313d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
3144a201d6eSTrond Myklebust out:
315d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
3164a201d6eSTrond Myklebust 	return ret;
317d1bacf9eSBryan Schumaker }
318d1bacf9eSBryan Schumaker 
319d1bacf9eSBryan Schumaker static
320d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
321d1bacf9eSBryan Schumaker {
322d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
323d1bacf9eSBryan Schumaker 	unsigned int index;
324d1bacf9eSBryan Schumaker 
325d1bacf9eSBryan Schumaker 	if (diff < 0)
326d1bacf9eSBryan Schumaker 		goto out_eof;
327d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
3288cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
329d1bacf9eSBryan Schumaker 			goto out_eof;
330d1bacf9eSBryan Schumaker 		return -EAGAIN;
331d1bacf9eSBryan Schumaker 	}
332d1bacf9eSBryan Schumaker 
333d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
334d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
335d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
336d1bacf9eSBryan Schumaker 	return 0;
337d1bacf9eSBryan Schumaker out_eof:
338d1bacf9eSBryan Schumaker 	desc->eof = 1;
339d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
340d1bacf9eSBryan Schumaker }
341d1bacf9eSBryan Schumaker 
342d1bacf9eSBryan Schumaker static
343d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
344d1bacf9eSBryan Schumaker {
345d1bacf9eSBryan Schumaker 	int i;
346d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
347d1bacf9eSBryan Schumaker 
348d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3498cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
350e47c085aSTrond Myklebust 			desc->file->f_pos = desc->current_index + i;
3518cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
35247c716cbSTrond Myklebust 			return 0;
3538cd51a0cSTrond Myklebust 		}
3548cd51a0cSTrond Myklebust 	}
35547c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
356d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
35718fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
35818fb5fe4STrond Myklebust 			desc->eof = 1;
359d1bacf9eSBryan Schumaker 	}
360d1bacf9eSBryan Schumaker 	return status;
361d1bacf9eSBryan Schumaker }
362d1bacf9eSBryan Schumaker 
363d1bacf9eSBryan Schumaker static
364d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
365d1bacf9eSBryan Schumaker {
366d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
36747c716cbSTrond Myklebust 	int status;
368d1bacf9eSBryan Schumaker 
369d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
370d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
371d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
372d1bacf9eSBryan Schumaker 		goto out;
373d1bacf9eSBryan Schumaker 	}
374d1bacf9eSBryan Schumaker 
375d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
376d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
377d1bacf9eSBryan Schumaker 	else
378d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
379d1bacf9eSBryan Schumaker 
38047c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3810aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
382e47c085aSTrond Myklebust 		desc->current_index += array->size;
38347c716cbSTrond Myklebust 		desc->page_index++;
38447c716cbSTrond Myklebust 	}
385d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
386d1bacf9eSBryan Schumaker out:
387d1bacf9eSBryan Schumaker 	return status;
388d1bacf9eSBryan Schumaker }
389d1bacf9eSBryan Schumaker 
390d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
391d1bacf9eSBryan Schumaker static
39256e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
393d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
394d1bacf9eSBryan Schumaker {
395*480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
396*480c2006SBryan Schumaker 	struct rpc_cred	*cred = ctx->cred;
3974704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3981da177e4SLinus Torvalds 	int		error;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds  again:
4011da177e4SLinus Torvalds 	timestamp = jiffies;
4024704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
40356e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
4041da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
4051da177e4SLinus Torvalds 	if (error < 0) {
4061da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
4071da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
4081da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
4093a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
4101da177e4SLinus Torvalds 			desc->plus = 0;
4111da177e4SLinus Torvalds 			goto again;
4121da177e4SLinus Torvalds 		}
4131da177e4SLinus Torvalds 		goto error;
4141da177e4SLinus Torvalds 	}
4151f4eab7eSNeil Brown 	desc->timestamp = timestamp;
4164704f0e2STrond Myklebust 	desc->gencount = gencount;
417d1bacf9eSBryan Schumaker error:
418d1bacf9eSBryan Schumaker 	return error;
419d1bacf9eSBryan Schumaker }
420d1bacf9eSBryan Schumaker 
421573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
422573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
423d1bacf9eSBryan Schumaker {
424573c4e1eSChuck Lever 	int error;
425d1bacf9eSBryan Schumaker 
426573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
427573c4e1eSChuck Lever 	if (error)
428573c4e1eSChuck Lever 		return error;
429d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
430d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
431d1bacf9eSBryan Schumaker 	return 0;
432d1bacf9eSBryan Schumaker }
433d1bacf9eSBryan Schumaker 
434d39ab9deSBryan Schumaker static
435d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
436d39ab9deSBryan Schumaker {
437d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
438d39ab9deSBryan Schumaker 		goto different;
43937a09f07STrond Myklebust 	if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
440d39ab9deSBryan Schumaker 		goto different;
441d39ab9deSBryan Schumaker 	return 1;
442d39ab9deSBryan Schumaker different:
443d39ab9deSBryan Schumaker 	return 0;
444d39ab9deSBryan Schumaker }
445d39ab9deSBryan Schumaker 
446d39ab9deSBryan Schumaker static
447d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
448d39ab9deSBryan Schumaker {
4494a201d6eSTrond Myklebust 	struct qstr filename = {
4504a201d6eSTrond Myklebust 		.len = entry->len,
4514a201d6eSTrond Myklebust 		.name = entry->name,
4524a201d6eSTrond Myklebust 	};
4534a201d6eSTrond Myklebust 	struct dentry *dentry;
4544a201d6eSTrond Myklebust 	struct dentry *alias;
455d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
456d39ab9deSBryan Schumaker 	struct inode *inode;
457d39ab9deSBryan Schumaker 
4584a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4594a201d6eSTrond Myklebust 		if (filename.len == 1)
4604a201d6eSTrond Myklebust 			return;
4614a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4624a201d6eSTrond Myklebust 			return;
4634a201d6eSTrond Myklebust 	}
4644a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
465d39ab9deSBryan Schumaker 
4664a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
467d39ab9deSBryan Schumaker 	if (dentry != NULL) {
468d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
469d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
470d39ab9deSBryan Schumaker 			goto out;
471d39ab9deSBryan Schumaker 		} else {
472d39ab9deSBryan Schumaker 			d_drop(dentry);
473d39ab9deSBryan Schumaker 			dput(dentry);
474d39ab9deSBryan Schumaker 		}
475d39ab9deSBryan Schumaker 	}
476d39ab9deSBryan Schumaker 
477d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4784a201d6eSTrond Myklebust 	if (dentry == NULL)
4794a201d6eSTrond Myklebust 		return;
4804a201d6eSTrond Myklebust 
481d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
482d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
483d39ab9deSBryan Schumaker 		goto out;
484d39ab9deSBryan Schumaker 
485d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
486d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
487d39ab9deSBryan Schumaker 		goto out;
488d39ab9deSBryan Schumaker 	else if (alias) {
489d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
490d39ab9deSBryan Schumaker 		dput(alias);
491d39ab9deSBryan Schumaker 	} else
492d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
493d39ab9deSBryan Schumaker 
494d39ab9deSBryan Schumaker out:
495d39ab9deSBryan Schumaker 	dput(dentry);
496d39ab9deSBryan Schumaker }
497d39ab9deSBryan Schumaker 
498d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
499d1bacf9eSBryan Schumaker static
5008cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5016650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
502d1bacf9eSBryan Schumaker {
503babddc72SBryan Schumaker 	struct xdr_stream stream;
5046650239aSTrond Myklebust 	struct xdr_buf buf = {
5056650239aSTrond Myklebust 		.pages = xdr_pages,
5066650239aSTrond Myklebust 		.page_len = buflen,
5076650239aSTrond Myklebust 		.buflen = buflen,
5086650239aSTrond Myklebust 		.len = buflen,
5096650239aSTrond Myklebust 	};
5106650239aSTrond Myklebust 	struct page *scratch;
51199424380SBryan Schumaker 	struct nfs_cache_array *array;
5125c346854STrond Myklebust 	unsigned int count = 0;
5135c346854STrond Myklebust 	int status;
514babddc72SBryan Schumaker 
5156650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5166650239aSTrond Myklebust 	if (scratch == NULL)
5176650239aSTrond Myklebust 		return -ENOMEM;
518babddc72SBryan Schumaker 
5196650239aSTrond Myklebust 	xdr_init_decode(&stream, &buf, NULL);
5206650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
52199424380SBryan Schumaker 
52299424380SBryan Schumaker 	do {
52399424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5248cd51a0cSTrond Myklebust 		if (status != 0) {
5258cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5268cd51a0cSTrond Myklebust 				status = 0;
52799424380SBryan Schumaker 			break;
5288cd51a0cSTrond Myklebust 		}
52999424380SBryan Schumaker 
5305c346854STrond Myklebust 		count++;
5315c346854STrond Myklebust 
53247c716cbSTrond Myklebust 		if (desc->plus != 0)
533d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
5348cd51a0cSTrond Myklebust 
5358cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
5368cd51a0cSTrond Myklebust 		if (status != 0)
5378cd51a0cSTrond Myklebust 			break;
53899424380SBryan Schumaker 	} while (!entry->eof);
53999424380SBryan Schumaker 
54047c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
54199424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5428cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5438cd51a0cSTrond Myklebust 			array->eof_index = array->size;
54499424380SBryan Schumaker 			status = 0;
54599424380SBryan Schumaker 			nfs_readdir_release_array(page);
5465c346854STrond Myklebust 		} else
5475c346854STrond Myklebust 			status = PTR_ERR(array);
54856e4ebf8SBryan Schumaker 	}
5496650239aSTrond Myklebust 
5506650239aSTrond Myklebust 	put_page(scratch);
5518cd51a0cSTrond Myklebust 	return status;
5528cd51a0cSTrond Myklebust }
55356e4ebf8SBryan Schumaker 
55456e4ebf8SBryan Schumaker static
55556e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
55656e4ebf8SBryan Schumaker {
55756e4ebf8SBryan Schumaker 	unsigned int i;
55856e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
55956e4ebf8SBryan Schumaker 		put_page(pages[i]);
56056e4ebf8SBryan Schumaker }
56156e4ebf8SBryan Schumaker 
56256e4ebf8SBryan Schumaker static
56356e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
56456e4ebf8SBryan Schumaker 		unsigned int npages)
56556e4ebf8SBryan Schumaker {
56656e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
56756e4ebf8SBryan Schumaker }
56856e4ebf8SBryan Schumaker 
56956e4ebf8SBryan Schumaker /*
57056e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
57156e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
57256e4ebf8SBryan Schumaker  */
57356e4ebf8SBryan Schumaker static
5746650239aSTrond Myklebust int nfs_readdir_large_page(struct page **pages, unsigned int npages)
57556e4ebf8SBryan Schumaker {
57656e4ebf8SBryan Schumaker 	unsigned int i;
57756e4ebf8SBryan Schumaker 
57856e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
57956e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
58056e4ebf8SBryan Schumaker 		if (page == NULL)
58156e4ebf8SBryan Schumaker 			goto out_freepages;
58256e4ebf8SBryan Schumaker 		pages[i] = page;
58356e4ebf8SBryan Schumaker 	}
5846650239aSTrond Myklebust 	return 0;
58556e4ebf8SBryan Schumaker 
58656e4ebf8SBryan Schumaker out_freepages:
58756e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
5886650239aSTrond Myklebust 	return -ENOMEM;
589d1bacf9eSBryan Schumaker }
590d1bacf9eSBryan Schumaker 
591d1bacf9eSBryan Schumaker static
592d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
593d1bacf9eSBryan Schumaker {
59456e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
59556e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
596d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
597d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
598d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5998cd51a0cSTrond Myklebust 	int status = -ENOMEM;
60056e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
601d1bacf9eSBryan Schumaker 
602d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6030aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
604d1bacf9eSBryan Schumaker 	entry.eof = 0;
605d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
606d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
607573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
608d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
609d1bacf9eSBryan Schumaker 		goto out;
610d1bacf9eSBryan Schumaker 
611d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
6128cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
6138cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
6148cd51a0cSTrond Myklebust 		goto out;
6158cd51a0cSTrond Myklebust 	}
616d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
617d1bacf9eSBryan Schumaker 	array->eof_index = -1;
618d1bacf9eSBryan Schumaker 
6196650239aSTrond Myklebust 	status = nfs_readdir_large_page(pages, array_size);
6206650239aSTrond Myklebust 	if (status < 0)
621d1bacf9eSBryan Schumaker 		goto out_release_array;
622d1bacf9eSBryan Schumaker 	do {
623ac396128STrond Myklebust 		unsigned int pglen;
62456e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
625babddc72SBryan Schumaker 
626d1bacf9eSBryan Schumaker 		if (status < 0)
627d1bacf9eSBryan Schumaker 			break;
628ac396128STrond Myklebust 		pglen = status;
6296650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6308cd51a0cSTrond Myklebust 		if (status < 0) {
6318cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
6328cd51a0cSTrond Myklebust 				status = 0;
6338cd51a0cSTrond Myklebust 			break;
6348cd51a0cSTrond Myklebust 		}
6358cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
636d1bacf9eSBryan Schumaker 
63756e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
638d1bacf9eSBryan Schumaker out_release_array:
639d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
640d1bacf9eSBryan Schumaker out:
641d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
642d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
643d1bacf9eSBryan Schumaker 	return status;
644d1bacf9eSBryan Schumaker }
645d1bacf9eSBryan Schumaker 
646d1bacf9eSBryan Schumaker /*
647d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
648d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
649d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
650d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6511da177e4SLinus Torvalds  */
652d1bacf9eSBryan Schumaker static
653d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
654d1bacf9eSBryan Schumaker {
655d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
6568cd51a0cSTrond Myklebust 	int ret;
657d1bacf9eSBryan Schumaker 
6588cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6598cd51a0cSTrond Myklebust 	if (ret < 0)
660d1bacf9eSBryan Schumaker 		goto error;
661d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
662d1bacf9eSBryan Schumaker 
6632aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
664cd9ae2b6STrond Myklebust 		/* Should never happen */
665cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
666cd9ae2b6STrond Myklebust 	}
6671da177e4SLinus Torvalds 	unlock_page(page);
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds  error:
6701da177e4SLinus Torvalds 	unlock_page(page);
6718cd51a0cSTrond Myklebust 	return ret;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674d1bacf9eSBryan Schumaker static
675d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6761da177e4SLinus Torvalds {
67711de3b11STrond Myklebust 	if (!desc->page->mapping)
67811de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
6791da177e4SLinus Torvalds 	page_cache_release(desc->page);
6801da177e4SLinus Torvalds 	desc->page = NULL;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
683d1bacf9eSBryan Schumaker static
684d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6851da177e4SLinus Torvalds {
6868cd51a0cSTrond Myklebust 	return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
687d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds /*
691d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6921da177e4SLinus Torvalds  */
693d1bacf9eSBryan Schumaker static
694d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
695d1bacf9eSBryan Schumaker {
696d1bacf9eSBryan Schumaker 	int res;
697d1bacf9eSBryan Schumaker 
698d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
699d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
700d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
701d1bacf9eSBryan Schumaker 
702d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
70347c716cbSTrond Myklebust 	if (res != 0)
704d1bacf9eSBryan Schumaker 		cache_page_release(desc);
705d1bacf9eSBryan Schumaker 	return res;
706d1bacf9eSBryan Schumaker }
707d1bacf9eSBryan Schumaker 
708d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7091da177e4SLinus Torvalds static inline
7101da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7111da177e4SLinus Torvalds {
7128cd51a0cSTrond Myklebust 	int res;
713d1bacf9eSBryan Schumaker 
7140aded708STrond Myklebust 	if (desc->page_index == 0) {
7158cd51a0cSTrond Myklebust 		desc->current_index = 0;
7160aded708STrond Myklebust 		desc->last_cookie = 0;
7170aded708STrond Myklebust 	}
71847c716cbSTrond Myklebust 	do {
719d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
72047c716cbSTrond Myklebust 	} while (res == -EAGAIN);
7211da177e4SLinus Torvalds 	return res;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds /*
7251da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
7261da177e4SLinus Torvalds  */
7271da177e4SLinus Torvalds static
7281da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
7291da177e4SLinus Torvalds 		   filldir_t filldir)
7301da177e4SLinus Torvalds {
7311da177e4SLinus Torvalds 	struct file	*file = desc->file;
732d1bacf9eSBryan Schumaker 	int i = 0;
733d1bacf9eSBryan Schumaker 	int res = 0;
734d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
7351da177e4SLinus Torvalds 
736d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
737e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
738e7c58e97STrond Myklebust 		res = PTR_ERR(array);
739e7c58e97STrond Myklebust 		goto out;
740e7c58e97STrond Myklebust 	}
7411da177e4SLinus Torvalds 
742d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
743ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
7441da177e4SLinus Torvalds 
745ece0b423STrond Myklebust 		ent = &array->array[i];
746ece0b423STrond Myklebust 		if (filldir(dirent, ent->string.name, ent->string.len,
7470b26a0bfSTrond Myklebust 		    file->f_pos, nfs_compat_user_ino64(ent->ino),
7480b26a0bfSTrond Myklebust 		    ent->d_type) < 0) {
749ece0b423STrond Myklebust 			desc->eof = 1;
7501da177e4SLinus Torvalds 			break;
751ece0b423STrond Myklebust 		}
75200a92642SOlivier Galibert 		file->f_pos++;
753d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
754d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
755d1bacf9eSBryan Schumaker 		else
756d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7578cd51a0cSTrond Myklebust 	}
75847c716cbSTrond Myklebust 	if (array->eof_index >= 0)
759d1bacf9eSBryan Schumaker 		desc->eof = 1;
760d1bacf9eSBryan Schumaker 
761d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
762e7c58e97STrond Myklebust out:
763d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7641e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7651e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7661da177e4SLinus Torvalds 	return res;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds /*
7701da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7711da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7721da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7731da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7741da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7751da177e4SLinus Torvalds  *
7761da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7771da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7781da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7791da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7801da177e4SLinus Torvalds  */
7811da177e4SLinus Torvalds static inline
7821da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
7831da177e4SLinus Torvalds 		     filldir_t filldir)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct page	*page = NULL;
7861da177e4SLinus Torvalds 	int		status;
787d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
7881da177e4SLinus Torvalds 
7891e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7901e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7931da177e4SLinus Torvalds 	if (!page) {
7941da177e4SLinus Torvalds 		status = -ENOMEM;
7951da177e4SLinus Torvalds 		goto out;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 
7987a8e1dc3STrond Myklebust 	desc->page_index = 0;
7990aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8007a8e1dc3STrond Myklebust 	desc->page = page;
8017a8e1dc3STrond Myklebust 
80285f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
80385f8607eSTrond Myklebust 	if (status < 0)
804d1bacf9eSBryan Schumaker 		goto out_release;
805d1bacf9eSBryan Schumaker 
8061da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds  out:
8091e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8103110ff80SHarvey Harrison 			__func__, status);
8111da177e4SLinus Torvalds 	return status;
8121da177e4SLinus Torvalds  out_release:
813d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8141da177e4SLinus Torvalds 	goto out;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
81700a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
81800a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
81900a92642SOlivier Galibert    whole directory.
8201da177e4SLinus Torvalds  */
8211da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
8221da177e4SLinus Torvalds {
82301cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
8241da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
8251da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
8261da177e4SLinus Torvalds 			*desc = &my_desc;
827*480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
82847c716cbSTrond Myklebust 	int res;
8291da177e4SLinus Torvalds 
8306da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
8311e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8321e7cb3dcSChuck Lever 			(long long)filp->f_pos);
83391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
83491d5b470SChuck Lever 
8351da177e4SLinus Torvalds 	/*
83600a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
837f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
83800a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
83900a92642SOlivier Galibert 	 * revalidate the cookie.
8401da177e4SLinus Torvalds 	 */
8411da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	desc->file = filp;
844*480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
8451da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
8461da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
8471da177e4SLinus Torvalds 
848565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
8491cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
850fccca7fcSTrond Myklebust 	if (res < 0)
851fccca7fcSTrond Myklebust 		goto out;
852fccca7fcSTrond Myklebust 
85347c716cbSTrond Myklebust 	do {
8541da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
85500a92642SOlivier Galibert 
8561da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
857ece0b423STrond Myklebust 			res = 0;
8581da177e4SLinus Torvalds 			/* This means either end of directory */
859d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8601da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
8611da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
862ece0b423STrond Myklebust 				if (res == 0)
8631da177e4SLinus Torvalds 					continue;
8641da177e4SLinus Torvalds 			}
8651da177e4SLinus Torvalds 			break;
8661da177e4SLinus Torvalds 		}
8671da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8683a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8691da177e4SLinus Torvalds 			nfs_zap_caches(inode);
870baf57a09STrond Myklebust 			desc->page_index = 0;
8711da177e4SLinus Torvalds 			desc->plus = 0;
872d1bacf9eSBryan Schumaker 			desc->eof = 0;
8731da177e4SLinus Torvalds 			continue;
8741da177e4SLinus Torvalds 		}
8751da177e4SLinus Torvalds 		if (res < 0)
8761da177e4SLinus Torvalds 			break;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
879ece0b423STrond Myklebust 		if (res < 0)
8801da177e4SLinus Torvalds 			break;
88147c716cbSTrond Myklebust 	} while (!desc->eof);
882fccca7fcSTrond Myklebust out:
883565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8841e7cb3dcSChuck Lever 	if (res > 0)
8851e7cb3dcSChuck Lever 		res = 0;
886aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8871e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8881e7cb3dcSChuck Lever 			res);
8891da177e4SLinus Torvalds 	return res;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
89210afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
893f0dd2136STrond Myklebust {
894b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
895b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
896*480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
897b84e06c5SChuck Lever 
8986da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
899b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
900b84e06c5SChuck Lever 			dentry->d_name.name,
901b84e06c5SChuck Lever 			offset, origin);
902b84e06c5SChuck Lever 
903b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
904f0dd2136STrond Myklebust 	switch (origin) {
905f0dd2136STrond Myklebust 		case 1:
906f0dd2136STrond Myklebust 			offset += filp->f_pos;
907f0dd2136STrond Myklebust 		case 0:
908f0dd2136STrond Myklebust 			if (offset >= 0)
909f0dd2136STrond Myklebust 				break;
910f0dd2136STrond Myklebust 		default:
911f0dd2136STrond Myklebust 			offset = -EINVAL;
912f0dd2136STrond Myklebust 			goto out;
913f0dd2136STrond Myklebust 	}
914f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
915f0dd2136STrond Myklebust 		filp->f_pos = offset;
916*480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
917f0dd2136STrond Myklebust 	}
918f0dd2136STrond Myklebust out:
919b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
920f0dd2136STrond Myklebust 	return offset;
921f0dd2136STrond Myklebust }
922f0dd2136STrond Myklebust 
9231da177e4SLinus Torvalds /*
9241da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
9251da177e4SLinus Torvalds  * is a dummy operation.
9261da177e4SLinus Torvalds  */
9277ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync)
9281da177e4SLinus Torvalds {
9297ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
9307ea80859SChristoph Hellwig 
9316da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9321e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9331e7cb3dcSChuck Lever 			datasync);
9341e7cb3dcSChuck Lever 
93554917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
9361da177e4SLinus Torvalds 	return 0;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
939bfc69a45STrond Myklebust /**
940bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
941bfc69a45STrond Myklebust  * @dir - pointer to directory inode
942bfc69a45STrond Myklebust  *
943bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
944bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
945bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
946bfc69a45STrond Myklebust  *
947bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
948bfc69a45STrond Myklebust  */
949bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
950bfc69a45STrond Myklebust {
951011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
952bfc69a45STrond Myklebust }
953bfc69a45STrond Myklebust 
9541da177e4SLinus Torvalds /*
9551da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9561da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9571da177e4SLinus Torvalds  * and may need to be looked up again.
9581da177e4SLinus Torvalds  */
959c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9621da177e4SLinus Torvalds 		return 1;
9634eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9644eec952eSTrond Myklebust 		return 0;
965f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9666ecc5e8fSTrond Myklebust 		return 0;
967f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
968f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
969f2c77f4eSTrond Myklebust 		return 0;
970f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
971f2c77f4eSTrond Myklebust 		return 0;
972f2c77f4eSTrond Myklebust 	return 1;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds /*
9761d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
9771d6757fbSTrond Myklebust  *
9781d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
9791d6757fbSTrond Myklebust  * component of the path.
9801d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
9811d6757fbSTrond Myklebust  */
98234286d66SNick Piggin static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
98334286d66SNick Piggin 						unsigned int mask)
9841d6757fbSTrond Myklebust {
9851d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
9861d6757fbSTrond Myklebust 		return 0;
9871d6757fbSTrond Myklebust 	return nd->flags & mask;
9881d6757fbSTrond Myklebust }
9891d6757fbSTrond Myklebust 
9901d6757fbSTrond Myklebust /*
991a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
992a12802caSTrond Myklebust  * an O_EXCL create using this path component.
993a12802caSTrond Myklebust  */
994a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
995a12802caSTrond Myklebust {
996a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
997a12802caSTrond Myklebust 		return 0;
9983516586aSAl Viro 	return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
999a12802caSTrond Myklebust }
1000a12802caSTrond Myklebust 
1001a12802caSTrond Myklebust /*
10021d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10031d6757fbSTrond Myklebust  *
10041d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10051d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10061d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10071d6757fbSTrond Myklebust  *
10081d6757fbSTrond Myklebust  */
10091da177e4SLinus Torvalds static inline
10101da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
10111da177e4SLinus Torvalds {
10121da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
10131da177e4SLinus Torvalds 
101436d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10154e99a1ffSTrond Myklebust 		return 0;
10161da177e4SLinus Torvalds 	if (nd != NULL) {
10171da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
10181d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
10191da177e4SLinus Torvalds 			goto out_force;
10201da177e4SLinus Torvalds 		/* This is an open(2) */
10211d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
10224e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
10234e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
10244e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
10251da177e4SLinus Torvalds 			goto out_force;
10264f48af45STrond Myklebust 		return 0;
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
10291da177e4SLinus Torvalds out_force:
10301da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /*
10341da177e4SLinus Torvalds  * We judge how long we want to trust negative
10351da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10361da177e4SLinus Torvalds  *
10371da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10381da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10391da177e4SLinus Torvalds  */
10401da177e4SLinus Torvalds static inline
10411da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
10421da177e4SLinus Torvalds 		       struct nameidata *nd)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
10451d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
10461da177e4SLinus Torvalds 		return 0;
10474eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10484eec952eSTrond Myklebust 		return 1;
10491da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
10531da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10541da177e4SLinus Torvalds  * and we should check whether we can really trust that
10551da177e4SLinus Torvalds  * lookup.
10561da177e4SLinus Torvalds  *
10571da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10581da177e4SLinus Torvalds  * we have an inode!
10591da177e4SLinus Torvalds  *
10601da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10611da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10621da177e4SLinus Torvalds  */
10631da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
10641da177e4SLinus Torvalds {
10651da177e4SLinus Torvalds 	struct inode *dir;
10661da177e4SLinus Torvalds 	struct inode *inode;
10671da177e4SLinus Torvalds 	struct dentry *parent;
1068e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1069e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10701da177e4SLinus Torvalds 	int error;
10711da177e4SLinus Torvalds 
107234286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
107334286d66SNick Piggin 		return -ECHILD;
107434286d66SNick Piggin 
10751da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10761da177e4SLinus Torvalds 	dir = parent->d_inode;
107791d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10781da177e4SLinus Torvalds 	inode = dentry->d_inode;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	if (!inode) {
10811da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
10821da177e4SLinus Torvalds 			goto out_bad;
10831da177e4SLinus Torvalds 		goto out_valid;
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10871e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10883110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10891e7cb3dcSChuck Lever 				dentry->d_name.name);
10901da177e4SLinus Torvalds 		goto out_bad;
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds 
109315860ab1STrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
109415860ab1STrond Myklebust 		goto out_set_verifier;
109515860ab1STrond Myklebust 
10961da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1097a12802caSTrond Myklebust 	if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
10981da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
10991da177e4SLinus Torvalds 			goto out_zap_parent;
11001da177e4SLinus Torvalds 		goto out_valid;
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	if (NFS_STALE(inode))
11041da177e4SLinus Torvalds 		goto out_bad;
11051da177e4SLinus Torvalds 
1106e1fb4d05STrond Myklebust 	error = -ENOMEM;
1107e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1108e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1109e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1110e1fb4d05STrond Myklebust 		goto out_error;
1111e1fb4d05STrond Myklebust 
1112e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
11131da177e4SLinus Torvalds 	if (error)
11141da177e4SLinus Torvalds 		goto out_bad;
1115e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
11161da177e4SLinus Torvalds 		goto out_bad;
1117e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
11181da177e4SLinus Torvalds 		goto out_bad;
11191da177e4SLinus Torvalds 
1120e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1121e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
112215860ab1STrond Myklebust out_set_verifier:
1123cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11241da177e4SLinus Torvalds  out_valid:
11251da177e4SLinus Torvalds 	dput(parent);
11261e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
11273110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11281e7cb3dcSChuck Lever 			dentry->d_name.name);
11291da177e4SLinus Torvalds 	return 1;
11301da177e4SLinus Torvalds out_zap_parent:
11311da177e4SLinus Torvalds 	nfs_zap_caches(dir);
11321da177e4SLinus Torvalds  out_bad:
1133a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
11341da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
11351da177e4SLinus Torvalds 		/* Purge readdir caches. */
11361da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11371da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11381da177e4SLinus Torvalds 		if (have_submounts(dentry))
11391da177e4SLinus Torvalds 			goto out_valid;
1140d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1141d9e80b7dSAl Viro 			goto out_valid;
11421da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 	d_drop(dentry);
1145e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1146e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
11471da177e4SLinus Torvalds 	dput(parent);
11481e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11493110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11501e7cb3dcSChuck Lever 			dentry->d_name.name);
11511da177e4SLinus Torvalds 	return 0;
1152e1fb4d05STrond Myklebust out_error:
1153e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1154e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1155e1fb4d05STrond Myklebust 	dput(parent);
1156e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1157e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1158e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1159e1fb4d05STrond Myklebust 	return error;
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds /*
11631da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11641da177e4SLinus Torvalds  */
1165fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
11661da177e4SLinus Torvalds {
11671da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
11681da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
11691da177e4SLinus Torvalds 		dentry->d_flags);
11701da177e4SLinus Torvalds 
117177f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
117277f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
117377f11192STrond Myklebust 		return 1;
117477f11192STrond Myklebust 
11751da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11761da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
11771da177e4SLinus Torvalds 		return 1;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
11801da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
11811da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
11821da177e4SLinus Torvalds 		return 1;
11831da177e4SLinus Torvalds 	}
11841da177e4SLinus Torvalds 	return 0;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds 
11881b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
11891b83d707STrond Myklebust {
11901b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
11911b83d707STrond Myklebust 	if (inode->i_nlink > 0)
11921b83d707STrond Myklebust 		drop_nlink(inode);
11931b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
11941b83d707STrond Myklebust }
11951b83d707STrond Myklebust 
11961da177e4SLinus Torvalds /*
11971da177e4SLinus Torvalds  * Called when the dentry loses inode.
11981da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
11991da177e4SLinus Torvalds  */
12001da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
12011da177e4SLinus Torvalds {
120283672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
120383672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
120483672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
120583672d39SNeil Brown 
12061da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12079a53c3a7SDave Hansen 		drop_nlink(inode);
1208e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 	iput(inode);
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
1213f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
12141da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
12151da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12161da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
121736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
12181da177e4SLinus Torvalds };
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
12211da177e4SLinus Torvalds {
12221da177e4SLinus Torvalds 	struct dentry *res;
1223565277f6STrond Myklebust 	struct dentry *parent;
12241da177e4SLinus Torvalds 	struct inode *inode = NULL;
1225e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1226e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
12271da177e4SLinus Torvalds 	int error;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
12301da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
123191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
12341da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
12351da177e4SLinus Torvalds 		goto out;
12361da177e4SLinus Torvalds 
1237fd684071STrond Myklebust 	/*
1238fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1239fd684071STrond Myklebust 	 * but don't hash the dentry.
1240fd684071STrond Myklebust 	 */
1241fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
1242fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1243fd684071STrond Myklebust 		res = NULL;
1244fc0f684cSTrond Myklebust 		goto out;
1245fd684071STrond Myklebust 	}
12461da177e4SLinus Torvalds 
1247e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1248e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1249e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1250e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1251e1fb4d05STrond Myklebust 		goto out;
1252e1fb4d05STrond Myklebust 
1253565277f6STrond Myklebust 	parent = dentry->d_parent;
1254565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1255565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
1256e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
12571da177e4SLinus Torvalds 	if (error == -ENOENT)
12581da177e4SLinus Torvalds 		goto no_entry;
12591da177e4SLinus Torvalds 	if (error < 0) {
12601da177e4SLinus Torvalds 		res = ERR_PTR(error);
1261565277f6STrond Myklebust 		goto out_unblock_sillyrename;
12621da177e4SLinus Torvalds 	}
1263e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1264bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
126503f28e3aSTrond Myklebust 	if (IS_ERR(res))
1266565277f6STrond Myklebust 		goto out_unblock_sillyrename;
126754ceac45SDavid Howells 
12681da177e4SLinus Torvalds no_entry:
126954ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
12709eaef27bSTrond Myklebust 	if (res != NULL) {
12719eaef27bSTrond Myklebust 		if (IS_ERR(res))
1272565277f6STrond Myklebust 			goto out_unblock_sillyrename;
12731da177e4SLinus Torvalds 		dentry = res;
12749eaef27bSTrond Myklebust 	}
12751da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1276565277f6STrond Myklebust out_unblock_sillyrename:
1277565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
12781da177e4SLinus Torvalds out:
1279e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1280e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
12811da177e4SLinus Torvalds 	return res;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
12851da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
12861da177e4SLinus Torvalds 
1287f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
12881da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
12891da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12901da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
129136d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
12921da177e4SLinus Torvalds };
12931da177e4SLinus Torvalds 
12941d6757fbSTrond Myklebust /*
12951d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
12961d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
12971d6757fbSTrond Myklebust  */
12985584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd)
12991da177e4SLinus Torvalds {
13001d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
13011da177e4SLinus Torvalds 		return 0;
13021da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
13031da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
13041da177e4SLinus Torvalds 		return 0;
13051da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
13062c463e95SDave Hansen 	if (__mnt_is_readonly(nd->path.mnt) &&
13072c463e95SDave Hansen 	    (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
13081da177e4SLinus Torvalds 		return 0;
13091da177e4SLinus Torvalds 	return 1;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
1312cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1313cd9a1c0eSTrond Myklebust {
1314cd9a1c0eSTrond Myklebust 	struct path path = {
1315cd9a1c0eSTrond Myklebust 		.mnt = nd->path.mnt,
1316cd9a1c0eSTrond Myklebust 		.dentry = dentry,
1317cd9a1c0eSTrond Myklebust 	};
1318cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1319cd9a1c0eSTrond Myklebust 	struct rpc_cred *cred;
1320cd9a1c0eSTrond Myklebust 	fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1321cd9a1c0eSTrond Myklebust 
1322cd9a1c0eSTrond Myklebust 	cred = rpc_lookup_cred();
1323cd9a1c0eSTrond Myklebust 	if (IS_ERR(cred))
1324cd9a1c0eSTrond Myklebust 		return ERR_CAST(cred);
1325cd9a1c0eSTrond Myklebust 	ctx = alloc_nfs_open_context(&path, cred, fmode);
1326cd9a1c0eSTrond Myklebust 	put_rpccred(cred);
1327cd9a1c0eSTrond Myklebust 	if (ctx == NULL)
1328cd9a1c0eSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1329cd9a1c0eSTrond Myklebust 	return ctx;
1330cd9a1c0eSTrond Myklebust }
1331cd9a1c0eSTrond Myklebust 
1332cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1333cd9a1c0eSTrond Myklebust {
1334cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1335cd9a1c0eSTrond Myklebust 	return 0;
1336cd9a1c0eSTrond Myklebust }
1337cd9a1c0eSTrond Myklebust 
1338cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1339cd9a1c0eSTrond Myklebust {
1340cd9a1c0eSTrond Myklebust 	struct file *filp;
1341cd9a1c0eSTrond Myklebust 	int ret = 0;
1342cd9a1c0eSTrond Myklebust 
1343cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1344cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
1345cd9a1c0eSTrond Myklebust 		ret = nfs_may_open(ctx->path.dentry->d_inode,
1346cd9a1c0eSTrond Myklebust 				ctx->cred,
1347cd9a1c0eSTrond Myklebust 				nd->intent.open.flags);
1348cd9a1c0eSTrond Myklebust 		if (ret < 0)
1349cd9a1c0eSTrond Myklebust 			goto out;
1350cd9a1c0eSTrond Myklebust 	}
1351cd9a1c0eSTrond Myklebust 	filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1352cd9a1c0eSTrond Myklebust 	if (IS_ERR(filp))
1353cd9a1c0eSTrond Myklebust 		ret = PTR_ERR(filp);
1354cd9a1c0eSTrond Myklebust 	else
1355cd9a1c0eSTrond Myklebust 		nfs_file_set_open_context(filp, ctx);
1356cd9a1c0eSTrond Myklebust out:
1357cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1358cd9a1c0eSTrond Myklebust 	return ret;
1359cd9a1c0eSTrond Myklebust }
1360cd9a1c0eSTrond Myklebust 
13611da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
13621da177e4SLinus Torvalds {
1363cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1364cd9a1c0eSTrond Myklebust 	struct iattr attr;
13651da177e4SLinus Torvalds 	struct dentry *res = NULL;
1366f46e0bd3STrond Myklebust 	struct inode *inode;
1367cd9a1c0eSTrond Myklebust 	int open_flags;
1368898f635cSTrond Myklebust 	int err;
13691da177e4SLinus Torvalds 
13701e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
13711e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13721e7cb3dcSChuck Lever 
13731da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
13745584c306STrond Myklebust 	if (!is_atomic_open(nd))
13751da177e4SLinus Torvalds 		goto no_open;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
13781da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
13791da177e4SLinus Torvalds 		goto out;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 
1382d4d9cdcbSTrond Myklebust 	/* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1383d4d9cdcbSTrond Myklebust 	 * the dentry. */
13843516586aSAl Viro 	if (nd->flags & LOOKUP_EXCL) {
1385d4d9cdcbSTrond Myklebust 		d_instantiate(dentry, NULL);
138602a913a7STrond Myklebust 		goto out;
138702a913a7STrond Myklebust 	}
13881da177e4SLinus Torvalds 
1389cd9a1c0eSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1390cd9a1c0eSTrond Myklebust 	res = ERR_CAST(ctx);
1391cd9a1c0eSTrond Myklebust 	if (IS_ERR(ctx))
1392cd9a1c0eSTrond Myklebust 		goto out;
1393cd9a1c0eSTrond Myklebust 
1394cd9a1c0eSTrond Myklebust 	open_flags = nd->intent.open.flags;
1395cd9a1c0eSTrond Myklebust 	if (nd->flags & LOOKUP_CREATE) {
1396cd9a1c0eSTrond Myklebust 		attr.ia_mode = nd->intent.open.create_mode;
1397cd9a1c0eSTrond Myklebust 		attr.ia_valid = ATTR_MODE;
1398cd9a1c0eSTrond Myklebust 		attr.ia_mode &= ~current_umask();
1399cd9a1c0eSTrond Myklebust 	} else {
1400898f635cSTrond Myklebust 		open_flags &= ~(O_EXCL | O_CREAT);
1401cd9a1c0eSTrond Myklebust 		attr.ia_valid = 0;
1402cd9a1c0eSTrond Myklebust 	}
1403cd9a1c0eSTrond Myklebust 
14041da177e4SLinus Torvalds 	/* Open the file on the server */
1405f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
14062b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1407f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1408f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1409cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
1410f46e0bd3STrond Myklebust 		switch (PTR_ERR(inode)) {
14111da177e4SLinus Torvalds 			/* Make a negative dentry */
14121da177e4SLinus Torvalds 			case -ENOENT:
1413f46e0bd3STrond Myklebust 				d_add(dentry, NULL);
141402a913a7STrond Myklebust 				res = NULL;
141502a913a7STrond Myklebust 				goto out;
14161da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
14176f926b5bSTrond Myklebust 			case -ENOTDIR:
14186f926b5bSTrond Myklebust 				goto no_open;
14191da177e4SLinus Torvalds 			case -ELOOP:
14201da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
14211da177e4SLinus Torvalds 					goto no_open;
142223ebbd9aSTrond Myklebust 			/* case -EISDIR: */
14231da177e4SLinus Torvalds 			/* case -EINVAL: */
14241da177e4SLinus Torvalds 			default:
1425f46e0bd3STrond Myklebust 				res = ERR_CAST(inode);
14261da177e4SLinus Torvalds 				goto out;
14271da177e4SLinus Torvalds 		}
1428cd9a1c0eSTrond Myklebust 	}
1429f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1430898f635cSTrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1431f46e0bd3STrond Myklebust 	if (res != NULL) {
1432f46e0bd3STrond Myklebust 		dput(ctx->path.dentry);
1433f46e0bd3STrond Myklebust 		ctx->path.dentry = dget(res);
14341da177e4SLinus Torvalds 		dentry = res;
1435f46e0bd3STrond Myklebust 	}
1436898f635cSTrond Myklebust 	err = nfs_intent_set_file(nd, ctx);
1437898f635cSTrond Myklebust 	if (err < 0) {
1438898f635cSTrond Myklebust 		if (res != NULL)
1439898f635cSTrond Myklebust 			dput(res);
1440898f635cSTrond Myklebust 		return ERR_PTR(err);
1441898f635cSTrond Myklebust 	}
14421da177e4SLinus Torvalds out:
1443f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14441da177e4SLinus Torvalds 	return res;
14451da177e4SLinus Torvalds no_open:
14461da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	struct dentry *parent = NULL;
1452657e94b6SNick Piggin 	struct inode *inode;
14531da177e4SLinus Torvalds 	struct inode *dir;
1454b8d4caddSTrond Myklebust 	struct nfs_open_context *ctx;
14551da177e4SLinus Torvalds 	int openflags, ret = 0;
14561da177e4SLinus Torvalds 
1457657e94b6SNick Piggin 	if (nd->flags & LOOKUP_RCU)
1458657e94b6SNick Piggin 		return -ECHILD;
1459657e94b6SNick Piggin 
1460657e94b6SNick Piggin 	inode = dentry->d_inode;
14611f063d2cSTrond Myklebust 	if (!is_atomic_open(nd) || d_mountpoint(dentry))
14625584c306STrond Myklebust 		goto no_open;
14632b484297STrond Myklebust 
14641da177e4SLinus Torvalds 	parent = dget_parent(dentry);
14651da177e4SLinus Torvalds 	dir = parent->d_inode;
14662b484297STrond Myklebust 
14671da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
14681da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
14691da177e4SLinus Torvalds 	 */
1470216d5d06STrond Myklebust 	if (inode == NULL) {
1471216d5d06STrond Myklebust 		if (!nfs_neg_need_reval(dir, dentry, nd))
1472216d5d06STrond Myklebust 			ret = 1;
14731da177e4SLinus Torvalds 		goto out;
1474216d5d06STrond Myklebust 	}
1475216d5d06STrond Myklebust 
14761da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
14771da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
14785584c306STrond Myklebust 		goto no_open_dput;
14791da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
14801da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
14811da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
14825584c306STrond Myklebust 		goto no_open_dput;
14831da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
14840a377cffSTrond Myklebust 	openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
14851da177e4SLinus Torvalds 
1486b8d4caddSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1487b8d4caddSTrond Myklebust 	ret = PTR_ERR(ctx);
1488b8d4caddSTrond Myklebust 	if (IS_ERR(ctx))
1489b8d4caddSTrond Myklebust 		goto out;
14901da177e4SLinus Torvalds 	/*
14911b1dcc1bSJes Sorensen 	 * Note: we're not holding inode->i_mutex and so may be racing with
14921da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
14931da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
14941da177e4SLinus Torvalds 	 */
14952b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
1496535918f1STrond Myklebust 	if (IS_ERR(inode)) {
1497535918f1STrond Myklebust 		ret = PTR_ERR(inode);
1498535918f1STrond Myklebust 		switch (ret) {
1499535918f1STrond Myklebust 		case -EPERM:
1500535918f1STrond Myklebust 		case -EACCES:
1501535918f1STrond Myklebust 		case -EDQUOT:
1502535918f1STrond Myklebust 		case -ENOSPC:
1503535918f1STrond Myklebust 		case -EROFS:
1504535918f1STrond Myklebust 			goto out_put_ctx;
1505535918f1STrond Myklebust 		default:
1506535918f1STrond Myklebust 			goto out_drop;
1507535918f1STrond Myklebust 		}
1508535918f1STrond Myklebust 	}
1509535918f1STrond Myklebust 	iput(inode);
1510898f635cSTrond Myklebust 	if (inode != dentry->d_inode)
1511535918f1STrond Myklebust 		goto out_drop;
1512898f635cSTrond Myklebust 
1513898f635cSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1514898f635cSTrond Myklebust 	ret = nfs_intent_set_file(nd, ctx);
1515898f635cSTrond Myklebust 	if (ret >= 0)
1516898f635cSTrond Myklebust 		ret = 1;
15171da177e4SLinus Torvalds out:
15181da177e4SLinus Torvalds 	dput(parent);
15191da177e4SLinus Torvalds 	return ret;
1520535918f1STrond Myklebust out_drop:
1521535918f1STrond Myklebust 	d_drop(dentry);
1522535918f1STrond Myklebust 	ret = 0;
1523535918f1STrond Myklebust out_put_ctx:
1524535918f1STrond Myklebust 	put_nfs_open_context(ctx);
1525535918f1STrond Myklebust 	goto out;
1526535918f1STrond Myklebust 
15275584c306STrond Myklebust no_open_dput:
15281da177e4SLinus Torvalds 	dput(parent);
15295584c306STrond Myklebust no_open:
15301da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
15311da177e4SLinus Torvalds }
1532c0204fd2STrond Myklebust 
1533c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1534c0204fd2STrond Myklebust 		struct nameidata *nd)
1535c0204fd2STrond Myklebust {
1536c0204fd2STrond Myklebust 	struct nfs_open_context *ctx = NULL;
1537c0204fd2STrond Myklebust 	struct iattr attr;
1538c0204fd2STrond Myklebust 	int error;
1539c0204fd2STrond Myklebust 	int open_flags = 0;
1540c0204fd2STrond Myklebust 
1541c0204fd2STrond Myklebust 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1542c0204fd2STrond Myklebust 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1543c0204fd2STrond Myklebust 
1544c0204fd2STrond Myklebust 	attr.ia_mode = mode;
1545c0204fd2STrond Myklebust 	attr.ia_valid = ATTR_MODE;
1546c0204fd2STrond Myklebust 
1547c0204fd2STrond Myklebust 	if ((nd->flags & LOOKUP_CREATE) != 0) {
1548c0204fd2STrond Myklebust 		open_flags = nd->intent.open.flags;
1549c0204fd2STrond Myklebust 
1550c0204fd2STrond Myklebust 		ctx = nameidata_to_nfs_open_context(dentry, nd);
1551c0204fd2STrond Myklebust 		error = PTR_ERR(ctx);
1552c0204fd2STrond Myklebust 		if (IS_ERR(ctx))
1553898f635cSTrond Myklebust 			goto out_err_drop;
1554c0204fd2STrond Myklebust 	}
1555c0204fd2STrond Myklebust 
1556c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1557c0204fd2STrond Myklebust 	if (error != 0)
1558c0204fd2STrond Myklebust 		goto out_put_ctx;
1559898f635cSTrond Myklebust 	if (ctx != NULL) {
1560898f635cSTrond Myklebust 		error = nfs_intent_set_file(nd, ctx);
1561898f635cSTrond Myklebust 		if (error < 0)
1562898f635cSTrond Myklebust 			goto out_err;
1563898f635cSTrond Myklebust 	}
1564c0204fd2STrond Myklebust 	return 0;
1565c0204fd2STrond Myklebust out_put_ctx:
1566c0204fd2STrond Myklebust 	if (ctx != NULL)
1567c0204fd2STrond Myklebust 		put_nfs_open_context(ctx);
1568898f635cSTrond Myklebust out_err_drop:
1569c0204fd2STrond Myklebust 	d_drop(dentry);
1570898f635cSTrond Myklebust out_err:
1571c0204fd2STrond Myklebust 	return error;
1572c0204fd2STrond Myklebust }
1573c0204fd2STrond Myklebust 
15741da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds /*
15771da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15781da177e4SLinus Torvalds  */
15791da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15801da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
15811da177e4SLinus Torvalds {
1582fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1583fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15841da177e4SLinus Torvalds 	struct inode *inode;
15851da177e4SLinus Torvalds 	int error = -EACCES;
15861da177e4SLinus Torvalds 
1587fab728e1STrond Myklebust 	d_drop(dentry);
1588fab728e1STrond Myklebust 
15891da177e4SLinus Torvalds 	/* We may have been initialized further down */
15901da177e4SLinus Torvalds 	if (dentry->d_inode)
1591fab728e1STrond Myklebust 		goto out;
15921da177e4SLinus Torvalds 	if (fhandle->size == 0) {
15931da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
15941da177e4SLinus Torvalds 		if (error)
1595fab728e1STrond Myklebust 			goto out_error;
15961da177e4SLinus Torvalds 	}
15975724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15981da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15991da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
16008fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
16011da177e4SLinus Torvalds 		if (error < 0)
1602fab728e1STrond Myklebust 			goto out_error;
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
160503f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
160603f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1607fab728e1STrond Myklebust 		goto out_error;
1608fab728e1STrond Myklebust 	d_add(dentry, inode);
1609fab728e1STrond Myklebust out:
1610fab728e1STrond Myklebust 	dput(parent);
16111da177e4SLinus Torvalds 	return 0;
1612fab728e1STrond Myklebust out_error:
1613fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1614fab728e1STrond Myklebust 	dput(parent);
1615fab728e1STrond Myklebust 	return error;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds /*
16191da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
16201da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
16211da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
16221da177e4SLinus Torvalds  * reply path made it appear to have failed.
16231da177e4SLinus Torvalds  */
16241da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
16251da177e4SLinus Torvalds 		struct nameidata *nd)
16261da177e4SLinus Torvalds {
16271da177e4SLinus Torvalds 	struct iattr attr;
16281da177e4SLinus Torvalds 	int error;
16298a0eebf6STrond Myklebust 	int open_flags = 0;
16301da177e4SLinus Torvalds 
16311e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
16321e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	attr.ia_mode = mode;
16351da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16361da177e4SLinus Torvalds 
16378a0eebf6STrond Myklebust 	if ((nd->flags & LOOKUP_CREATE) != 0)
16388a0eebf6STrond Myklebust 		open_flags = nd->intent.open.flags;
16398a0eebf6STrond Myklebust 
16408a0eebf6STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
16411da177e4SLinus Torvalds 	if (error != 0)
16421da177e4SLinus Torvalds 		goto out_err;
16431da177e4SLinus Torvalds 	return 0;
16441da177e4SLinus Torvalds out_err:
16451da177e4SLinus Torvalds 	d_drop(dentry);
16461da177e4SLinus Torvalds 	return error;
16471da177e4SLinus Torvalds }
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds /*
16501da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16511da177e4SLinus Torvalds  */
16521da177e4SLinus Torvalds static int
16531da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
16541da177e4SLinus Torvalds {
16551da177e4SLinus Torvalds 	struct iattr attr;
16561da177e4SLinus Torvalds 	int status;
16571da177e4SLinus Torvalds 
16581e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
16591e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16621da177e4SLinus Torvalds 		return -EINVAL;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	attr.ia_mode = mode;
16651da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16681da177e4SLinus Torvalds 	if (status != 0)
16691da177e4SLinus Torvalds 		goto out_err;
16701da177e4SLinus Torvalds 	return 0;
16711da177e4SLinus Torvalds out_err:
16721da177e4SLinus Torvalds 	d_drop(dentry);
16731da177e4SLinus Torvalds 	return status;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds /*
16771da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16781da177e4SLinus Torvalds  */
16791da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
16801da177e4SLinus Torvalds {
16811da177e4SLinus Torvalds 	struct iattr attr;
16821da177e4SLinus Torvalds 	int error;
16831da177e4SLinus Torvalds 
16841e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16851e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16881da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16911da177e4SLinus Torvalds 	if (error != 0)
16921da177e4SLinus Torvalds 		goto out_err;
16931da177e4SLinus Torvalds 	return 0;
16941da177e4SLinus Torvalds out_err:
16951da177e4SLinus Torvalds 	d_drop(dentry);
16961da177e4SLinus Torvalds 	return error;
16971da177e4SLinus Torvalds }
16981da177e4SLinus Torvalds 
1699d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1700d45b9d8bSTrond Myklebust {
1701d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1702d45b9d8bSTrond Myklebust 		d_delete(dentry);
1703d45b9d8bSTrond Myklebust }
1704d45b9d8bSTrond Myklebust 
17051da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	int error;
17081da177e4SLinus Torvalds 
17091e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
17101e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
17131da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
17141da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1715ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1716d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1717d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	return error;
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds /*
17231da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
17241da177e4SLinus Torvalds  * and after checking that the file has only one user.
17251da177e4SLinus Torvalds  *
17261da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
17271da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
17281da177e4SLinus Torvalds  */
17291da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
17301da177e4SLinus Torvalds {
17311da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
17321da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17331da177e4SLinus Torvalds 	int error = -EBUSY;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
17361da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
17391da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
17401da177e4SLinus Torvalds 		error = 0;
17411da177e4SLinus Torvalds 		goto out;
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	if (inode != NULL) {
1745cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
17461da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17471da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
17481da177e4SLinus Torvalds 		if (error == 0)
17491b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17505ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
17511da177e4SLinus Torvalds 	} else
17521da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1753d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1754d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17551da177e4SLinus Torvalds out:
17561da177e4SLinus Torvalds 	return error;
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17601da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17611da177e4SLinus Torvalds  *
17621da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17631da177e4SLinus Torvalds  */
17641da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
17651da177e4SLinus Torvalds {
17661da177e4SLinus Torvalds 	int error;
17671da177e4SLinus Torvalds 	int need_rehash = 0;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17701da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
1773b7ab39f6SNick Piggin 	if (dentry->d_count > 1) {
17741da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1775ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1776ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17771da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17781da177e4SLinus Torvalds 		return error;
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17811da177e4SLinus Torvalds 		__d_drop(dentry);
17821da177e4SLinus Torvalds 		need_rehash = 1;
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17851da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1786d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17871da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17881da177e4SLinus Torvalds 	} else if (need_rehash)
17891da177e4SLinus Torvalds 		d_rehash(dentry);
17901da177e4SLinus Torvalds 	return error;
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
1793873101b3SChuck Lever /*
1794873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1795873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1796873101b3SChuck Lever  * using prepare_write/commit_write.
1797873101b3SChuck Lever  *
1798873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1799873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1800873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1801873101b3SChuck Lever  * symlink request has completed on the server.
1802873101b3SChuck Lever  *
1803873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1804873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1805873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1806873101b3SChuck Lever  * and move the raw page into its mapping.
1807873101b3SChuck Lever  */
1808873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18091da177e4SLinus Torvalds {
1810873101b3SChuck Lever 	struct pagevec lru_pvec;
1811873101b3SChuck Lever 	struct page *page;
1812873101b3SChuck Lever 	char *kaddr;
18131da177e4SLinus Torvalds 	struct iattr attr;
1814873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
18151da177e4SLinus Torvalds 	int error;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
18181da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
18191da177e4SLinus Torvalds 
1820873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1821873101b3SChuck Lever 		return -ENAMETOOLONG;
18221da177e4SLinus Torvalds 
1823873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1824873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
18251da177e4SLinus Torvalds 
182683d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
182776566991STrond Myklebust 	if (!page)
1828873101b3SChuck Lever 		return -ENOMEM;
1829873101b3SChuck Lever 
1830873101b3SChuck Lever 	kaddr = kmap_atomic(page, KM_USER0);
1831873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1832873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1833873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1834873101b3SChuck Lever 	kunmap_atomic(kaddr, KM_USER0);
1835873101b3SChuck Lever 
183694a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1837873101b3SChuck Lever 	if (error != 0) {
1838873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1839873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1840873101b3SChuck Lever 			dentry->d_name.name, symname, error);
18411da177e4SLinus Torvalds 		d_drop(dentry);
1842873101b3SChuck Lever 		__free_page(page);
18431da177e4SLinus Torvalds 		return error;
18441da177e4SLinus Torvalds 	}
18451da177e4SLinus Torvalds 
1846873101b3SChuck Lever 	/*
1847873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1848873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1849873101b3SChuck Lever 	 */
1850873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1851873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1852873101b3SChuck Lever 							GFP_KERNEL)) {
185339cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
18544f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1855873101b3SChuck Lever 		SetPageUptodate(page);
1856873101b3SChuck Lever 		unlock_page(page);
1857873101b3SChuck Lever 	} else
1858873101b3SChuck Lever 		__free_page(page);
1859873101b3SChuck Lever 
1860873101b3SChuck Lever 	return 0;
1861873101b3SChuck Lever }
1862873101b3SChuck Lever 
18631da177e4SLinus Torvalds static int
18641da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18651da177e4SLinus Torvalds {
18661da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18671da177e4SLinus Torvalds 	int error;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18701da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18711da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18721da177e4SLinus Torvalds 
18739a3936aaSTrond Myklebust 	nfs_inode_return_delegation(inode);
18749a3936aaSTrond Myklebust 
18759697d234STrond Myklebust 	d_drop(dentry);
18761da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1877cf809556STrond Myklebust 	if (error == 0) {
18787de9c6eeSAl Viro 		ihold(inode);
18799697d234STrond Myklebust 		d_add(dentry, inode);
1880cf809556STrond Myklebust 	}
18811da177e4SLinus Torvalds 	return error;
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds /*
18851da177e4SLinus Torvalds  * RENAME
18861da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18871da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18881da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18891da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18901da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18911da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18921da177e4SLinus Torvalds  *
18931da177e4SLinus Torvalds  * FIXED.
18941da177e4SLinus Torvalds  *
18951da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18961da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18971da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18981da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18991da177e4SLinus Torvalds  * using the inode layer
19001da177e4SLinus Torvalds  *
19011da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
19021da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
19031da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
19041da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
19051da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
19061da177e4SLinus Torvalds  * the rename.
19071da177e4SLinus Torvalds  */
19081da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
19091da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
19121da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
19131da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
19141da177e4SLinus Torvalds 	int error = -EBUSY;
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
19171da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
19181da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1919b7ab39f6SNick Piggin 		 new_dentry->d_count);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	/*
192228f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
192328f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
192428f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
192528f79a1aSMiklos Szeredi 	 * the new target.
19261da177e4SLinus Torvalds 	 */
192727226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
192827226104SMiklos Szeredi 		/*
192927226104SMiklos Szeredi 		 * To prevent any new references to the target during the
193027226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
193127226104SMiklos Szeredi 		 */
193227226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
193327226104SMiklos Szeredi 			d_drop(new_dentry);
193427226104SMiklos Szeredi 			rehash = new_dentry;
193527226104SMiklos Szeredi 		}
193627226104SMiklos Szeredi 
1937b7ab39f6SNick Piggin 		if (new_dentry->d_count > 2) {
19381da177e4SLinus Torvalds 			int err;
193927226104SMiklos Szeredi 
19401da177e4SLinus Torvalds 			/* copy the target dentry's name */
19411da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19421da177e4SLinus Torvalds 					 &new_dentry->d_name);
19431da177e4SLinus Torvalds 			if (!dentry)
19441da177e4SLinus Torvalds 				goto out;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19471da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
194824e93025SMiklos Szeredi 			if (err)
19491da177e4SLinus Torvalds 				goto out;
195024e93025SMiklos Szeredi 
195124e93025SMiklos Szeredi 			new_dentry = dentry;
195256335936SOGAWA Hirofumi 			rehash = NULL;
195324e93025SMiklos Szeredi 			new_inode = NULL;
1954b1e4adf4STrond Myklebust 		}
195527226104SMiklos Szeredi 	}
19561da177e4SLinus Torvalds 
1957cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
1958b1e4adf4STrond Myklebust 	if (new_inode != NULL)
195924174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19621da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19635ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19641da177e4SLinus Torvalds out:
19651da177e4SLinus Torvalds 	if (rehash)
19661da177e4SLinus Torvalds 		d_rehash(rehash);
19671da177e4SLinus Torvalds 	if (!error) {
1968b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1969b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19701da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19718fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19728fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1973d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1974d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	/* new dentry created? */
19771da177e4SLinus Torvalds 	if (dentry)
19781da177e4SLinus Torvalds 		dput(dentry);
19791da177e4SLinus Torvalds 	return error;
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
1982cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1983cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1984cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1985cfcea3e8STrond Myklebust 
19861c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19871c3c07e9STrond Myklebust {
19881c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19891c3c07e9STrond Myklebust 	kfree(entry);
1990cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1991cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1992cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19931c3c07e9STrond Myklebust }
19941c3c07e9STrond Myklebust 
19951a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19961a81bb8aSTrond Myklebust {
19971a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19981a81bb8aSTrond Myklebust 
19991a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
20001a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
20011a81bb8aSTrond Myklebust 		list_del(&cache->lru);
20021a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
20031a81bb8aSTrond Myklebust 	}
20041a81bb8aSTrond Myklebust }
20051a81bb8aSTrond Myklebust 
20067f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
2007979df72eSTrond Myklebust {
2008979df72eSTrond Myklebust 	LIST_HEAD(head);
2009aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2010979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
2011979df72eSTrond Myklebust 
201261d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
201361d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
20149c7e7e23STrond Myklebust 
2015a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2016aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2017979df72eSTrond Myklebust 		struct inode *inode;
2018979df72eSTrond Myklebust 
2019979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2020979df72eSTrond Myklebust 			break;
20219c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2022979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2023979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2024979df72eSTrond Myklebust 			goto remove_lru_entry;
2025979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2026979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2027979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2028979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
2029979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2030979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2031979df72eSTrond Myklebust 					&nfs_access_lru_list);
2032979df72eSTrond Myklebust 		else {
2033979df72eSTrond Myklebust remove_lru_entry:
2034979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
20359c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
2036979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
20379c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
2038979df72eSTrond Myklebust 		}
203959844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2040979df72eSTrond Myklebust 	}
2041979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20421a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
2043979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2044979df72eSTrond Myklebust }
2045979df72eSTrond Myklebust 
20461a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20471c3c07e9STrond Myklebust {
20481c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20491a81bb8aSTrond Myklebust 	struct rb_node *n;
20501c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20511c3c07e9STrond Myklebust 
20521c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20531c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20541c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20551c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20561a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20571c3c07e9STrond Myklebust 	}
20581c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20591c3c07e9STrond Myklebust }
20601c3c07e9STrond Myklebust 
20611c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20621c3c07e9STrond Myklebust {
20631a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20641a81bb8aSTrond Myklebust 
20651a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20661a81bb8aSTrond Myklebust 		return;
2067cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2068cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20691a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2070cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2071cfcea3e8STrond Myklebust 
20721c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20731a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20741a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20751a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20761a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20771c3c07e9STrond Myklebust }
20781c3c07e9STrond Myklebust 
20791c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20801c3c07e9STrond Myklebust {
20811c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20821c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20831c3c07e9STrond Myklebust 
20841c3c07e9STrond Myklebust 	while (n != NULL) {
20851c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20861c3c07e9STrond Myklebust 
20871c3c07e9STrond Myklebust 		if (cred < entry->cred)
20881c3c07e9STrond Myklebust 			n = n->rb_left;
20891c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20901c3c07e9STrond Myklebust 			n = n->rb_right;
20911c3c07e9STrond Myklebust 		else
20921c3c07e9STrond Myklebust 			return entry;
20931c3c07e9STrond Myklebust 	}
20941c3c07e9STrond Myklebust 	return NULL;
20951c3c07e9STrond Myklebust }
20961c3c07e9STrond Myklebust 
2097af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20981da177e4SLinus Torvalds {
209955296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
21001c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
21011c3c07e9STrond Myklebust 	int err = -ENOENT;
21021da177e4SLinus Torvalds 
21031c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21041c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
21051c3c07e9STrond Myklebust 		goto out_zap;
21061c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
21071c3c07e9STrond Myklebust 	if (cache == NULL)
21081c3c07e9STrond Myklebust 		goto out;
2109b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
211064672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
21111c3c07e9STrond Myklebust 		goto out_stale;
21121c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
21131c3c07e9STrond Myklebust 	res->cred = cache->cred;
21141c3c07e9STrond Myklebust 	res->mask = cache->mask;
2115cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
21161c3c07e9STrond Myklebust 	err = 0;
21171c3c07e9STrond Myklebust out:
21181c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21191c3c07e9STrond Myklebust 	return err;
21201c3c07e9STrond Myklebust out_stale:
21211c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2122cfcea3e8STrond Myklebust 	list_del(&cache->lru);
21231c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21241c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
21251da177e4SLinus Torvalds 	return -ENOENT;
21261c3c07e9STrond Myklebust out_zap:
21271a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
21281a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
21291c3c07e9STrond Myklebust 	return -ENOENT;
21301c3c07e9STrond Myklebust }
21311c3c07e9STrond Myklebust 
21321c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
21331c3c07e9STrond Myklebust {
2134cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2135cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
21361c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
21371c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
21381c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21391c3c07e9STrond Myklebust 
21401c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21411c3c07e9STrond Myklebust 	while (*p != NULL) {
21421c3c07e9STrond Myklebust 		parent = *p;
21431c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21441c3c07e9STrond Myklebust 
21451c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21461c3c07e9STrond Myklebust 			p = &parent->rb_left;
21471c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21481c3c07e9STrond Myklebust 			p = &parent->rb_right;
21491c3c07e9STrond Myklebust 		else
21501c3c07e9STrond Myklebust 			goto found;
21511c3c07e9STrond Myklebust 	}
21521c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21531c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2154cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21551c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21561c3c07e9STrond Myklebust 	return;
21571c3c07e9STrond Myklebust found:
21581c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2159cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2160cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21611c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21621c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
2165af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21661da177e4SLinus Torvalds {
21671c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21681c3c07e9STrond Myklebust 	if (cache == NULL)
21691c3c07e9STrond Myklebust 		return;
21701c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21711da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21721c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21731da177e4SLinus Torvalds 	cache->mask = set->mask;
21741c3c07e9STrond Myklebust 
21751c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2176cfcea3e8STrond Myklebust 
2177cfcea3e8STrond Myklebust 	/* Update accounting */
2178cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2179cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2180cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2181cfcea3e8STrond Myklebust 
2182cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21831a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2184cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21851a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21861a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21871a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2188cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2189cfcea3e8STrond Myklebust 	}
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21931da177e4SLinus Torvalds {
21941da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21951da177e4SLinus Torvalds 	int status;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21981da177e4SLinus Torvalds 	if (status == 0)
21991da177e4SLinus Torvalds 		goto out;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
22021da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
22031da177e4SLinus Torvalds 	cache.cred = cred;
22041da177e4SLinus Torvalds 	cache.jiffies = jiffies;
22051da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2206a71ee337SSuresh Jayaraman 	if (status != 0) {
2207a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2208a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2209a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2210a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2211a71ee337SSuresh Jayaraman 		}
22121da177e4SLinus Torvalds 		return status;
2213a71ee337SSuresh Jayaraman 	}
22141da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
22151da177e4SLinus Torvalds out:
2216e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22171da177e4SLinus Torvalds 		return 0;
22181da177e4SLinus Torvalds 	return -EACCES;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
2221af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2222af22f94aSTrond Myklebust {
2223af22f94aSTrond Myklebust 	int mask = 0;
2224af22f94aSTrond Myklebust 
2225af22f94aSTrond Myklebust 	if (openflags & FMODE_READ)
2226af22f94aSTrond Myklebust 		mask |= MAY_READ;
2227af22f94aSTrond Myklebust 	if (openflags & FMODE_WRITE)
2228af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
2229af22f94aSTrond Myklebust 	if (openflags & FMODE_EXEC)
2230af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2231af22f94aSTrond Myklebust 	return mask;
2232af22f94aSTrond Myklebust }
2233af22f94aSTrond Myklebust 
2234af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2235af22f94aSTrond Myklebust {
2236af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2237af22f94aSTrond Myklebust }
2238af22f94aSTrond Myklebust 
2239b74c79e9SNick Piggin int nfs_permission(struct inode *inode, int mask, unsigned int flags)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct rpc_cred *cred;
22421da177e4SLinus Torvalds 	int res = 0;
22431da177e4SLinus Torvalds 
2244b74c79e9SNick Piggin 	if (flags & IPERM_FLAG_RCU)
2245b74c79e9SNick Piggin 		return -ECHILD;
2246b74c79e9SNick Piggin 
224791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
224891d5b470SChuck Lever 
2249e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22501da177e4SLinus Torvalds 		goto out;
22511da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22529cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22531da177e4SLinus Torvalds 		goto force_lookup;
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22561da177e4SLinus Torvalds 		case S_IFLNK:
22571da177e4SLinus Torvalds 			goto out;
22581da177e4SLinus Torvalds 		case S_IFREG:
22591da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
22601da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
22617ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
22627ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
22631da177e4SLinus Torvalds 				goto out;
22641da177e4SLinus Torvalds 			break;
22651da177e4SLinus Torvalds 		case S_IFDIR:
22661da177e4SLinus Torvalds 			/*
22671da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22681da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22691da177e4SLinus Torvalds 			 */
22701da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22711da177e4SLinus Torvalds 				goto out;
22721da177e4SLinus Torvalds 	}
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds force_lookup:
22751da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22761da177e4SLinus Torvalds 		goto out_notsup;
22771da177e4SLinus Torvalds 
227898a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22791da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22801da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
22811da177e4SLinus Torvalds 		put_rpccred(cred);
22821da177e4SLinus Torvalds 	} else
22831da177e4SLinus Torvalds 		res = PTR_ERR(cred);
22841da177e4SLinus Torvalds out:
2285f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2286f696a365SMiklos Szeredi 		res = -EACCES;
2287f696a365SMiklos Szeredi 
22881e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
22891e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22901da177e4SLinus Torvalds 	return res;
22911da177e4SLinus Torvalds out_notsup:
22921da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22931da177e4SLinus Torvalds 	if (res == 0)
2294b74c79e9SNick Piggin 		res = generic_permission(inode, mask, flags, NULL);
22951e7cb3dcSChuck Lever 	goto out;
22961da177e4SLinus Torvalds }
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds /*
22991da177e4SLinus Torvalds  * Local variables:
23001da177e4SLinus Torvalds  *  version-control: t
23011da177e4SLinus Torvalds  *  kept-new-versions: 5
23021da177e4SLinus Torvalds  * End:
23031da177e4SLinus Torvalds  */
2304