xref: /openbmc/linux/fs/nfs/dir.c (revision 7a8e1dc3)
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>
3656e4ebf8SBryan Schumaker #include <linux/vmalloc.h>
3704e4bd1cSCatalin Marinas #include <linux/kmemleak.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 *);
471da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
481da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
491da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
501da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
511da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
521da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
531da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
541da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
551da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
561da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
571da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
587ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int);
59f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
60d1bacf9eSBryan Schumaker static int nfs_readdir_clear_array(struct page*, gfp_t);
611da177e4SLinus Torvalds 
624b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
63f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
641da177e4SLinus Torvalds 	.read		= generic_read_dir,
651da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
661da177e4SLinus Torvalds 	.open		= nfs_opendir,
671da177e4SLinus Torvalds 	.release	= nfs_release,
681da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds 
7192e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
721da177e4SLinus Torvalds 	.create		= nfs_create,
731da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
741da177e4SLinus Torvalds 	.link		= nfs_link,
751da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
761da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
771da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
781da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
791da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
801da177e4SLinus Torvalds 	.rename		= nfs_rename,
811da177e4SLinus Torvalds 	.permission	= nfs_permission,
821da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
831da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
86d1bacf9eSBryan Schumaker const struct address_space_operations nfs_dir_addr_space_ops = {
87d1bacf9eSBryan Schumaker 	.releasepage = nfs_readdir_clear_array,
88d1bacf9eSBryan Schumaker };
89d1bacf9eSBryan Schumaker 
90b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
9192e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
92b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
93b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
94b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
95b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
96b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
97b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
98b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
99b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
100b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
101b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
102b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
103b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
104b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
105b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
106b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
107b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
108b7fa0554SAndreas Gruenbacher };
109b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
110b7fa0554SAndreas Gruenbacher 
1111da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
114c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
11592e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
116c0204fd2STrond Myklebust 	.create		= nfs_open_create,
1171da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
1181da177e4SLinus Torvalds 	.link		= nfs_link,
1191da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1201da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1211da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1221da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1231da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1241da177e4SLinus Torvalds 	.rename		= nfs_rename,
1251da177e4SLinus Torvalds 	.permission	= nfs_permission,
1261da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1271da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
1286b3b5496SJ. Bruce Fields 	.getxattr       = nfs4_getxattr,
1296b3b5496SJ. Bruce Fields 	.setxattr       = nfs4_setxattr,
1306b3b5496SJ. Bruce Fields 	.listxattr      = nfs4_listxattr,
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds  * Open file
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds static int
1391da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1401da177e4SLinus Torvalds {
1417451c4f0SCarsten Otte 	int res;
1421da177e4SLinus Torvalds 
1436da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
144cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
145cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
146cc0dd2d1SChuck Lever 
147cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1481e7cb3dcSChuck Lever 
1491da177e4SLinus Torvalds 	/* Call generic open code in order to cache credentials */
1501da177e4SLinus Torvalds 	res = nfs_open(inode, filp);
151f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
152f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
153f5a73672SNeil Brown 		 * have been called, so we need to refresh the
154f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
155f5a73672SNeil Brown 		 */
156f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
157f5a73672SNeil Brown 	}
1581da177e4SLinus Torvalds 	return res;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
161d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
162d1bacf9eSBryan Schumaker 	u64 cookie;
163d1bacf9eSBryan Schumaker 	u64 ino;
164d1bacf9eSBryan Schumaker 	struct qstr string;
165d1bacf9eSBryan Schumaker };
166d1bacf9eSBryan Schumaker 
167d1bacf9eSBryan Schumaker struct nfs_cache_array {
168d1bacf9eSBryan Schumaker 	unsigned int size;
169d1bacf9eSBryan Schumaker 	int eof_index;
170d1bacf9eSBryan Schumaker 	u64 last_cookie;
171d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
172d1bacf9eSBryan Schumaker };
173d1bacf9eSBryan Schumaker 
174d1bacf9eSBryan Schumaker #define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
175d1bacf9eSBryan Schumaker 
17682f2e547SBryan Schumaker typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, struct nfs_server *, int);
1771da177e4SLinus Torvalds typedef struct {
1781da177e4SLinus Torvalds 	struct file	*file;
1791da177e4SLinus Torvalds 	struct page	*page;
1801da177e4SLinus Torvalds 	unsigned long	page_index;
181f0dd2136STrond Myklebust 	u64		*dir_cookie;
182f0dd2136STrond Myklebust 	loff_t		current_index;
1831da177e4SLinus Torvalds 	decode_dirent_t	decode;
184d1bacf9eSBryan Schumaker 
1851f4eab7eSNeil Brown 	unsigned long	timestamp;
1864704f0e2STrond Myklebust 	unsigned long	gencount;
187d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
188d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
189d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1901da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1911da177e4SLinus Torvalds 
192d1bacf9eSBryan Schumaker /*
193d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static
196d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1971da177e4SLinus Torvalds {
1988cd51a0cSTrond Myklebust 	void *ptr;
199d1bacf9eSBryan Schumaker 	if (page == NULL)
200d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
2018cd51a0cSTrond Myklebust 	ptr = kmap(page);
2028cd51a0cSTrond Myklebust 	if (ptr == NULL)
2038cd51a0cSTrond Myklebust 		return ERR_PTR(-ENOMEM);
2048cd51a0cSTrond Myklebust 	return ptr;
205d1bacf9eSBryan Schumaker }
206d1bacf9eSBryan Schumaker 
207d1bacf9eSBryan Schumaker static
208d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
209d1bacf9eSBryan Schumaker {
210d1bacf9eSBryan Schumaker 	kunmap(page);
211d1bacf9eSBryan Schumaker }
212d1bacf9eSBryan Schumaker 
213d1bacf9eSBryan Schumaker /*
214d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
215d1bacf9eSBryan Schumaker  */
216d1bacf9eSBryan Schumaker static
217d1bacf9eSBryan Schumaker int nfs_readdir_clear_array(struct page *page, gfp_t mask)
218d1bacf9eSBryan Schumaker {
219d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
220d1bacf9eSBryan Schumaker 	int i;
2218cd51a0cSTrond Myklebust 
2228cd51a0cSTrond Myklebust 	if (IS_ERR(array))
2238cd51a0cSTrond Myklebust 		return PTR_ERR(array);
224d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
225d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
226d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
227d1bacf9eSBryan Schumaker 	return 0;
228d1bacf9eSBryan Schumaker }
229d1bacf9eSBryan Schumaker 
230d1bacf9eSBryan Schumaker /*
231d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
232d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
233d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
234d1bacf9eSBryan Schumaker  */
235d1bacf9eSBryan Schumaker static
2364a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
237d1bacf9eSBryan Schumaker {
238d1bacf9eSBryan Schumaker 	string->len = len;
239d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
2404a201d6eSTrond Myklebust 	if (string->name == NULL)
2414a201d6eSTrond Myklebust 		return -ENOMEM;
24204e4bd1cSCatalin Marinas 	/*
24304e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
24404e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
24504e4bd1cSCatalin Marinas 	 */
24604e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2474a201d6eSTrond Myklebust 	string->hash = full_name_hash(name, len);
2484a201d6eSTrond Myklebust 	return 0;
249d1bacf9eSBryan Schumaker }
250d1bacf9eSBryan Schumaker 
251d1bacf9eSBryan Schumaker static
252d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
253d1bacf9eSBryan Schumaker {
254d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
2554a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2564a201d6eSTrond Myklebust 	int ret;
2574a201d6eSTrond Myklebust 
258d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
259d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
2608cd51a0cSTrond Myklebust 	ret = -ENOSPC;
2614a201d6eSTrond Myklebust 	if (array->size >= MAX_READDIR_ARRAY)
2624a201d6eSTrond Myklebust 		goto out;
263d1bacf9eSBryan Schumaker 
2644a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2654a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2664a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2674a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2684a201d6eSTrond Myklebust 	if (ret)
2694a201d6eSTrond Myklebust 		goto out;
270d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2718cd51a0cSTrond Myklebust 	array->size++;
272d1bacf9eSBryan Schumaker 	if (entry->eof == 1)
273d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2744a201d6eSTrond Myklebust out:
275d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
2764a201d6eSTrond Myklebust 	return ret;
277d1bacf9eSBryan Schumaker }
278d1bacf9eSBryan Schumaker 
279d1bacf9eSBryan Schumaker static
280d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
281d1bacf9eSBryan Schumaker {
282d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
283d1bacf9eSBryan Schumaker 	unsigned int index;
284d1bacf9eSBryan Schumaker 
285d1bacf9eSBryan Schumaker 	if (diff < 0)
286d1bacf9eSBryan Schumaker 		goto out_eof;
287d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2888cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
289d1bacf9eSBryan Schumaker 			goto out_eof;
290d1bacf9eSBryan Schumaker 		desc->current_index += array->size;
291d1bacf9eSBryan Schumaker 		return -EAGAIN;
292d1bacf9eSBryan Schumaker 	}
293d1bacf9eSBryan Schumaker 
294d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
295d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
296d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
297d1bacf9eSBryan Schumaker 	return 0;
298d1bacf9eSBryan Schumaker out_eof:
299d1bacf9eSBryan Schumaker 	desc->eof = 1;
300d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
301d1bacf9eSBryan Schumaker }
302d1bacf9eSBryan Schumaker 
303d1bacf9eSBryan Schumaker static
304d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
305d1bacf9eSBryan Schumaker {
306d1bacf9eSBryan Schumaker 	int i;
307d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
308d1bacf9eSBryan Schumaker 
309d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
3108cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
3118cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
3128cd51a0cSTrond Myklebust 			status = 0;
3138cd51a0cSTrond Myklebust 			goto out;
3148cd51a0cSTrond Myklebust 		}
3158cd51a0cSTrond Myklebust 	}
316d1bacf9eSBryan Schumaker 	if (i == array->eof_index) {
317d1bacf9eSBryan Schumaker 		desc->eof = 1;
318d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
319d1bacf9eSBryan Schumaker 	}
3208cd51a0cSTrond Myklebust out:
321d1bacf9eSBryan Schumaker 	return status;
322d1bacf9eSBryan Schumaker }
323d1bacf9eSBryan Schumaker 
324d1bacf9eSBryan Schumaker static
325d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
326d1bacf9eSBryan Schumaker {
327d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
328d1bacf9eSBryan Schumaker 	int status = -EBADCOOKIE;
329d1bacf9eSBryan Schumaker 
330d1bacf9eSBryan Schumaker 	if (desc->dir_cookie == NULL)
331d1bacf9eSBryan Schumaker 		goto out;
332d1bacf9eSBryan Schumaker 
333d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
334d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
335d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
336d1bacf9eSBryan Schumaker 		goto out;
337d1bacf9eSBryan Schumaker 	}
338d1bacf9eSBryan Schumaker 
339d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
340d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
341d1bacf9eSBryan Schumaker 	else
342d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
343d1bacf9eSBryan Schumaker 
344d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
345d1bacf9eSBryan Schumaker out:
346d1bacf9eSBryan Schumaker 	return status;
347d1bacf9eSBryan Schumaker }
348d1bacf9eSBryan Schumaker 
349d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
350d1bacf9eSBryan Schumaker static
35156e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
352d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
353d1bacf9eSBryan Schumaker {
3541da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
3554704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3561da177e4SLinus Torvalds 	int		error;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds  again:
3591da177e4SLinus Torvalds 	timestamp = jiffies;
3604704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
36156e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3621da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3631da177e4SLinus Torvalds 	if (error < 0) {
3641da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3651da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3661da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3673a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3681da177e4SLinus Torvalds 			desc->plus = 0;
3691da177e4SLinus Torvalds 			goto again;
3701da177e4SLinus Torvalds 		}
3711da177e4SLinus Torvalds 		goto error;
3721da177e4SLinus Torvalds 	}
3731f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3744704f0e2STrond Myklebust 	desc->gencount = gencount;
375d1bacf9eSBryan Schumaker error:
376d1bacf9eSBryan Schumaker 	return error;
377d1bacf9eSBryan Schumaker }
378d1bacf9eSBryan Schumaker 
379d1bacf9eSBryan Schumaker /* Fill in an entry based on the xdr code stored in desc->page */
380d1bacf9eSBryan Schumaker static
381babddc72SBryan Schumaker int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
382d1bacf9eSBryan Schumaker {
38382f2e547SBryan Schumaker 	__be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus);
384d1bacf9eSBryan Schumaker 	if (IS_ERR(p))
385d1bacf9eSBryan Schumaker 		return PTR_ERR(p);
386d1bacf9eSBryan Schumaker 
387d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
388d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
389d1bacf9eSBryan Schumaker 	return 0;
390d1bacf9eSBryan Schumaker }
391d1bacf9eSBryan Schumaker 
392d39ab9deSBryan Schumaker static
393d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
394d39ab9deSBryan Schumaker {
395d39ab9deSBryan Schumaker 	struct nfs_inode *node;
396d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
397d39ab9deSBryan Schumaker 		goto different;
398d39ab9deSBryan Schumaker 	node = NFS_I(dentry->d_inode);
399d39ab9deSBryan Schumaker 	if (node->fh.size != entry->fh->size)
400d39ab9deSBryan Schumaker 		goto different;
401d39ab9deSBryan Schumaker 	if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
402d39ab9deSBryan Schumaker 		goto different;
403d39ab9deSBryan Schumaker 	return 1;
404d39ab9deSBryan Schumaker different:
405d39ab9deSBryan Schumaker 	return 0;
406d39ab9deSBryan Schumaker }
407d39ab9deSBryan Schumaker 
408d39ab9deSBryan Schumaker static
409d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
410d39ab9deSBryan Schumaker {
4114a201d6eSTrond Myklebust 	struct qstr filename = {
4124a201d6eSTrond Myklebust 		.len = entry->len,
4134a201d6eSTrond Myklebust 		.name = entry->name,
4144a201d6eSTrond Myklebust 	};
4154a201d6eSTrond Myklebust 	struct dentry *dentry;
4164a201d6eSTrond Myklebust 	struct dentry *alias;
417d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
418d39ab9deSBryan Schumaker 	struct inode *inode;
419d39ab9deSBryan Schumaker 
4204a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4214a201d6eSTrond Myklebust 		if (filename.len == 1)
4224a201d6eSTrond Myklebust 			return;
4234a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4244a201d6eSTrond Myklebust 			return;
4254a201d6eSTrond Myklebust 	}
4264a201d6eSTrond Myklebust 	filename.hash = full_name_hash(filename.name, filename.len);
427d39ab9deSBryan Schumaker 
4284a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
429d39ab9deSBryan Schumaker 	if (dentry != NULL) {
430d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
431d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
432d39ab9deSBryan Schumaker 			goto out;
433d39ab9deSBryan Schumaker 		} else {
434d39ab9deSBryan Schumaker 			d_drop(dentry);
435d39ab9deSBryan Schumaker 			dput(dentry);
436d39ab9deSBryan Schumaker 		}
437d39ab9deSBryan Schumaker 	}
438d39ab9deSBryan Schumaker 
439d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
4404a201d6eSTrond Myklebust 	if (dentry == NULL)
4414a201d6eSTrond Myklebust 		return;
4424a201d6eSTrond Myklebust 
443d39ab9deSBryan Schumaker 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
444d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
445d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
446d39ab9deSBryan Schumaker 		goto out;
447d39ab9deSBryan Schumaker 
448d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
449d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
450d39ab9deSBryan Schumaker 		goto out;
451d39ab9deSBryan Schumaker 	else if (alias) {
452d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
453d39ab9deSBryan Schumaker 		dput(alias);
454d39ab9deSBryan Schumaker 	} else
455d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
456d39ab9deSBryan Schumaker 
457d39ab9deSBryan Schumaker out:
458d39ab9deSBryan Schumaker 	dput(dentry);
459d39ab9deSBryan Schumaker }
460d39ab9deSBryan Schumaker 
461d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
462d1bacf9eSBryan Schumaker static
4638cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
46456e4ebf8SBryan Schumaker 				void *xdr_page, struct page *page, unsigned int buflen)
465d1bacf9eSBryan Schumaker {
466babddc72SBryan Schumaker 	struct xdr_stream stream;
467babddc72SBryan Schumaker 	struct xdr_buf buf;
46856e4ebf8SBryan Schumaker 	__be32 *ptr = xdr_page;
46999424380SBryan Schumaker 	struct nfs_cache_array *array;
4705c346854STrond Myklebust 	unsigned int count = 0;
4715c346854STrond Myklebust 	int status;
472babddc72SBryan Schumaker 
473babddc72SBryan Schumaker 	buf.head->iov_base = xdr_page;
474babddc72SBryan Schumaker 	buf.head->iov_len = buflen;
475babddc72SBryan Schumaker 	buf.tail->iov_len = 0;
476babddc72SBryan Schumaker 	buf.page_base = 0;
477babddc72SBryan Schumaker 	buf.page_len = 0;
478babddc72SBryan Schumaker 	buf.buflen = buf.head->iov_len;
479babddc72SBryan Schumaker 	buf.len = buf.head->iov_len;
480babddc72SBryan Schumaker 
481babddc72SBryan Schumaker 	xdr_init_decode(&stream, &buf, ptr);
482babddc72SBryan Schumaker 
48399424380SBryan Schumaker 
48499424380SBryan Schumaker 	do {
48599424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
4868cd51a0cSTrond Myklebust 		if (status != 0) {
4878cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
4888cd51a0cSTrond Myklebust 				status = 0;
48999424380SBryan Schumaker 			break;
4908cd51a0cSTrond Myklebust 		}
49199424380SBryan Schumaker 
4925c346854STrond Myklebust 		count++;
4935c346854STrond Myklebust 
494d39ab9deSBryan Schumaker 		if (desc->plus == 1)
495d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
4968cd51a0cSTrond Myklebust 
4978cd51a0cSTrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
4988cd51a0cSTrond Myklebust 		if (status != 0)
4998cd51a0cSTrond Myklebust 			break;
50099424380SBryan Schumaker 	} while (!entry->eof);
50199424380SBryan Schumaker 
5025c346854STrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof == 1)) {
50399424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
5048cd51a0cSTrond Myklebust 		if (!IS_ERR(array)) {
5058cd51a0cSTrond Myklebust 			array->eof_index = array->size;
50699424380SBryan Schumaker 			status = 0;
50799424380SBryan Schumaker 			nfs_readdir_release_array(page);
5085c346854STrond Myklebust 		} else
5095c346854STrond Myklebust 			status = PTR_ERR(array);
51056e4ebf8SBryan Schumaker 	}
5118cd51a0cSTrond Myklebust 	return status;
5128cd51a0cSTrond Myklebust }
51356e4ebf8SBryan Schumaker 
51456e4ebf8SBryan Schumaker static
51556e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
51656e4ebf8SBryan Schumaker {
51756e4ebf8SBryan Schumaker 	unsigned int i;
51856e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
51956e4ebf8SBryan Schumaker 		put_page(pages[i]);
52056e4ebf8SBryan Schumaker }
52156e4ebf8SBryan Schumaker 
52256e4ebf8SBryan Schumaker static
52356e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
52456e4ebf8SBryan Schumaker 		unsigned int npages)
52556e4ebf8SBryan Schumaker {
52656e4ebf8SBryan Schumaker 	vm_unmap_ram(ptr, npages);
52756e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
52856e4ebf8SBryan Schumaker }
52956e4ebf8SBryan Schumaker 
53056e4ebf8SBryan Schumaker /*
53156e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
53256e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
53356e4ebf8SBryan Schumaker  */
53456e4ebf8SBryan Schumaker static
53556e4ebf8SBryan Schumaker void *nfs_readdir_large_page(struct page **pages, unsigned int npages)
53656e4ebf8SBryan Schumaker {
53756e4ebf8SBryan Schumaker 	void *ptr;
53856e4ebf8SBryan Schumaker 	unsigned int i;
53956e4ebf8SBryan Schumaker 
54056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
54156e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
54256e4ebf8SBryan Schumaker 		if (page == NULL)
54356e4ebf8SBryan Schumaker 			goto out_freepages;
54456e4ebf8SBryan Schumaker 		pages[i] = page;
54556e4ebf8SBryan Schumaker 	}
54656e4ebf8SBryan Schumaker 
5474a201d6eSTrond Myklebust 	ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL);
54856e4ebf8SBryan Schumaker 	if (!IS_ERR_OR_NULL(ptr))
54956e4ebf8SBryan Schumaker 		return ptr;
55056e4ebf8SBryan Schumaker out_freepages:
55156e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
55256e4ebf8SBryan Schumaker 	return NULL;
553d1bacf9eSBryan Schumaker }
554d1bacf9eSBryan Schumaker 
555d1bacf9eSBryan Schumaker static
556d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
557d1bacf9eSBryan Schumaker {
55856e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
55956e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
560d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
561d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
562d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
5638cd51a0cSTrond Myklebust 	int status = -ENOMEM;
56456e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
565d1bacf9eSBryan Schumaker 
566d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
567d1bacf9eSBryan Schumaker 	entry.cookie = *desc->dir_cookie;
568d1bacf9eSBryan Schumaker 	entry.eof = 0;
569d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
570d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
571d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
572d1bacf9eSBryan Schumaker 		goto out;
573d1bacf9eSBryan Schumaker 
574d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
5758cd51a0cSTrond Myklebust 	if (IS_ERR(array)) {
5768cd51a0cSTrond Myklebust 		status = PTR_ERR(array);
5778cd51a0cSTrond Myklebust 		goto out;
5788cd51a0cSTrond Myklebust 	}
579d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
580d1bacf9eSBryan Schumaker 	array->eof_index = -1;
581d1bacf9eSBryan Schumaker 
58256e4ebf8SBryan Schumaker 	pages_ptr = nfs_readdir_large_page(pages, array_size);
58356e4ebf8SBryan Schumaker 	if (!pages_ptr)
584d1bacf9eSBryan Schumaker 		goto out_release_array;
585d1bacf9eSBryan Schumaker 	do {
586ac396128STrond Myklebust 		unsigned int pglen;
58756e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
588babddc72SBryan Schumaker 
589d1bacf9eSBryan Schumaker 		if (status < 0)
590d1bacf9eSBryan Schumaker 			break;
591ac396128STrond Myklebust 		pglen = status;
592ac396128STrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, pglen);
5938cd51a0cSTrond Myklebust 		if (status < 0) {
5948cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
5958cd51a0cSTrond Myklebust 				status = 0;
5968cd51a0cSTrond Myklebust 			break;
5978cd51a0cSTrond Myklebust 		}
5988cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
599d1bacf9eSBryan Schumaker 
60056e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
601d1bacf9eSBryan Schumaker out_release_array:
602d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
603d1bacf9eSBryan Schumaker out:
604d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
605d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
606d1bacf9eSBryan Schumaker 	return status;
607d1bacf9eSBryan Schumaker }
608d1bacf9eSBryan Schumaker 
609d1bacf9eSBryan Schumaker /*
610d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
611d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
612d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
613d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
6141da177e4SLinus Torvalds  */
615d1bacf9eSBryan Schumaker static
616d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
617d1bacf9eSBryan Schumaker {
618d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
6198cd51a0cSTrond Myklebust 	int ret;
620d1bacf9eSBryan Schumaker 
6218cd51a0cSTrond Myklebust 	ret = nfs_readdir_xdr_to_array(desc, page, inode);
6228cd51a0cSTrond Myklebust 	if (ret < 0)
623d1bacf9eSBryan Schumaker 		goto error;
624d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
625d1bacf9eSBryan Schumaker 
6262aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
627cd9ae2b6STrond Myklebust 		/* Should never happen */
628cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
629cd9ae2b6STrond Myklebust 	}
6301da177e4SLinus Torvalds 	unlock_page(page);
6311da177e4SLinus Torvalds 	return 0;
6321da177e4SLinus Torvalds  error:
6331da177e4SLinus Torvalds 	unlock_page(page);
6348cd51a0cSTrond Myklebust 	return ret;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637d1bacf9eSBryan Schumaker static
638d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	page_cache_release(desc->page);
6411da177e4SLinus Torvalds 	desc->page = NULL;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
644d1bacf9eSBryan Schumaker static
645d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
6461da177e4SLinus Torvalds {
6478cd51a0cSTrond Myklebust 	return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
648d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds /*
652d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6531da177e4SLinus Torvalds  */
654d1bacf9eSBryan Schumaker static
655d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
656d1bacf9eSBryan Schumaker {
657d1bacf9eSBryan Schumaker 	int res;
658d1bacf9eSBryan Schumaker 
659d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
660d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
661d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
662d1bacf9eSBryan Schumaker 
663d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
664d1bacf9eSBryan Schumaker 	if (res == 0)
665d1bacf9eSBryan Schumaker 		return 0;
666d1bacf9eSBryan Schumaker 	cache_page_release(desc);
667d1bacf9eSBryan Schumaker 	return res;
668d1bacf9eSBryan Schumaker }
669d1bacf9eSBryan Schumaker 
670d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
6711da177e4SLinus Torvalds static inline
6721da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
6731da177e4SLinus Torvalds {
6748cd51a0cSTrond Myklebust 	int res;
675d1bacf9eSBryan Schumaker 
6768cd51a0cSTrond Myklebust 	if (desc->page_index == 0)
6778cd51a0cSTrond Myklebust 		desc->current_index = 0;
678d1bacf9eSBryan Schumaker 	while (1) {
679d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
6801da177e4SLinus Torvalds 		if (res != -EAGAIN)
6811da177e4SLinus Torvalds 			break;
6821da177e4SLinus Torvalds 		desc->page_index++;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	return res;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode)
6881da177e4SLinus Torvalds {
6891da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /*
6931da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
6941da177e4SLinus Torvalds  */
6951da177e4SLinus Torvalds static
6961da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
6971da177e4SLinus Torvalds 		   filldir_t filldir)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct file	*file = desc->file;
700d1bacf9eSBryan Schumaker 	int i = 0;
701d1bacf9eSBryan Schumaker 	int res = 0;
702d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
703d1bacf9eSBryan Schumaker 	unsigned int d_type = DT_UNKNOWN;
7041da177e4SLinus Torvalds 
705d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
706e7c58e97STrond Myklebust 	if (IS_ERR(array)) {
707e7c58e97STrond Myklebust 		res = PTR_ERR(array);
708e7c58e97STrond Myklebust 		goto out;
709e7c58e97STrond Myklebust 	}
7101da177e4SLinus Torvalds 
711d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
712d1bacf9eSBryan Schumaker 		d_type = DT_UNKNOWN;
7131da177e4SLinus Torvalds 
714d1bacf9eSBryan Schumaker 		res = filldir(dirent, array->array[i].string.name,
715d1bacf9eSBryan Schumaker 			array->array[i].string.len, file->f_pos,
716d1bacf9eSBryan Schumaker 			nfs_compat_user_ino64(array->array[i].ino), d_type);
7171da177e4SLinus Torvalds 		if (res < 0)
7181da177e4SLinus Torvalds 			break;
71900a92642SOlivier Galibert 		file->f_pos++;
720d1bacf9eSBryan Schumaker 		desc->cache_entry_index = i;
721d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
722d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
723d1bacf9eSBryan Schumaker 		else
724d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
7258cd51a0cSTrond Myklebust 	}
7268cd51a0cSTrond Myklebust 	if (i == array->eof_index)
727d1bacf9eSBryan Schumaker 		desc->eof = 1;
728d1bacf9eSBryan Schumaker 
729d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
730e7c58e97STrond Myklebust out:
731d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7321e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
7331e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
7341da177e4SLinus Torvalds 	return res;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds /*
7381da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
7391da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
7401da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
7411da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
7421da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
7431da177e4SLinus Torvalds  *
7441da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
7451da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
7461da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7471da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static inline
7501da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
7511da177e4SLinus Torvalds 		     filldir_t filldir)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	struct page	*page = NULL;
7541da177e4SLinus Torvalds 	int		status;
755d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
7561da177e4SLinus Torvalds 
7571e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7581e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7611da177e4SLinus Torvalds 	if (!page) {
7621da177e4SLinus Torvalds 		status = -ENOMEM;
7631da177e4SLinus Torvalds 		goto out;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 
7667a8e1dc3STrond Myklebust 	desc->page_index = 0;
7677a8e1dc3STrond Myklebust 	desc->page = page;
7687a8e1dc3STrond Myklebust 
769d1bacf9eSBryan Schumaker 	if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
770d1bacf9eSBryan Schumaker 		status = -EIO;
771d1bacf9eSBryan Schumaker 		goto out_release;
772d1bacf9eSBryan Schumaker 	}
773d1bacf9eSBryan Schumaker 
7741da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds  out:
7771e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
7783110ff80SHarvey Harrison 			__func__, status);
7791da177e4SLinus Torvalds 	return status;
7801da177e4SLinus Torvalds  out_release:
781d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7821da177e4SLinus Torvalds 	goto out;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
78500a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
78600a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
78700a92642SOlivier Galibert    whole directory.
7881da177e4SLinus Torvalds  */
7891da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
7901da177e4SLinus Torvalds {
79101cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
7921da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
7931da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
7941da177e4SLinus Torvalds 			*desc = &my_desc;
795aa49b4cfSTrond Myklebust 	int res = -ENOMEM;
7961da177e4SLinus Torvalds 
7976da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
7981e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
7991e7cb3dcSChuck Lever 			(long long)filp->f_pos);
80091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
80191d5b470SChuck Lever 
8021da177e4SLinus Torvalds 	/*
80300a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
804f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
80500a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
80600a92642SOlivier Galibert 	 * revalidate the cookie.
8071da177e4SLinus Torvalds 	 */
8081da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	desc->file = filp;
811cd3758e3STrond Myklebust 	desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
8121da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
8131da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
8141da177e4SLinus Torvalds 
815565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
8161cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
817fccca7fcSTrond Myklebust 	if (res < 0)
818fccca7fcSTrond Myklebust 		goto out;
819fccca7fcSTrond Myklebust 
820d1bacf9eSBryan Schumaker 	while (desc->eof != 1) {
8211da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
82200a92642SOlivier Galibert 
8231da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
8241da177e4SLinus Torvalds 			/* This means either end of directory */
825d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
8261da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
8271da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
8281da177e4SLinus Torvalds 				if (res >= 0)
8291da177e4SLinus Torvalds 					continue;
8301da177e4SLinus Torvalds 			}
8311da177e4SLinus Torvalds 			res = 0;
8321da177e4SLinus Torvalds 			break;
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
8353a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
8361da177e4SLinus Torvalds 			nfs_zap_caches(inode);
837baf57a09STrond Myklebust 			desc->page_index = 0;
8381da177e4SLinus Torvalds 			desc->plus = 0;
839d1bacf9eSBryan Schumaker 			desc->eof = 0;
8401da177e4SLinus Torvalds 			continue;
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 		if (res < 0)
8431da177e4SLinus Torvalds 			break;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
8461da177e4SLinus Torvalds 		if (res < 0) {
8471da177e4SLinus Torvalds 			res = 0;
8481da177e4SLinus Torvalds 			break;
8491da177e4SLinus Torvalds 		}
8501da177e4SLinus Torvalds 	}
851fccca7fcSTrond Myklebust out:
852565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8531e7cb3dcSChuck Lever 	if (res > 0)
8541e7cb3dcSChuck Lever 		res = 0;
855aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8561e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8571e7cb3dcSChuck Lever 			res);
8581da177e4SLinus Torvalds 	return res;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
86110afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
862f0dd2136STrond Myklebust {
863b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
864b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
865b84e06c5SChuck Lever 
8666da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
867b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
868b84e06c5SChuck Lever 			dentry->d_name.name,
869b84e06c5SChuck Lever 			offset, origin);
870b84e06c5SChuck Lever 
871b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
872f0dd2136STrond Myklebust 	switch (origin) {
873f0dd2136STrond Myklebust 		case 1:
874f0dd2136STrond Myklebust 			offset += filp->f_pos;
875f0dd2136STrond Myklebust 		case 0:
876f0dd2136STrond Myklebust 			if (offset >= 0)
877f0dd2136STrond Myklebust 				break;
878f0dd2136STrond Myklebust 		default:
879f0dd2136STrond Myklebust 			offset = -EINVAL;
880f0dd2136STrond Myklebust 			goto out;
881f0dd2136STrond Myklebust 	}
882f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
883f0dd2136STrond Myklebust 		filp->f_pos = offset;
884cd3758e3STrond Myklebust 		nfs_file_open_context(filp)->dir_cookie = 0;
885f0dd2136STrond Myklebust 	}
886f0dd2136STrond Myklebust out:
887b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
888f0dd2136STrond Myklebust 	return offset;
889f0dd2136STrond Myklebust }
890f0dd2136STrond Myklebust 
8911da177e4SLinus Torvalds /*
8921da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
8931da177e4SLinus Torvalds  * is a dummy operation.
8941da177e4SLinus Torvalds  */
8957ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync)
8961da177e4SLinus Torvalds {
8977ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
8987ea80859SChristoph Hellwig 
8996da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
9001e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
9011e7cb3dcSChuck Lever 			datasync);
9021e7cb3dcSChuck Lever 
90354917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
9041da177e4SLinus Torvalds 	return 0;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
907bfc69a45STrond Myklebust /**
908bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
909bfc69a45STrond Myklebust  * @dir - pointer to directory inode
910bfc69a45STrond Myklebust  *
911bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
912bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
913bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
914bfc69a45STrond Myklebust  *
915bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
916bfc69a45STrond Myklebust  */
917bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
918bfc69a45STrond Myklebust {
919011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
920bfc69a45STrond Myklebust }
921bfc69a45STrond Myklebust 
9221da177e4SLinus Torvalds /*
9231da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
9241da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
9251da177e4SLinus Torvalds  * and may need to be looked up again.
9261da177e4SLinus Torvalds  */
927c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
9301da177e4SLinus Torvalds 		return 1;
9314eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
9324eec952eSTrond Myklebust 		return 0;
933f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
9346ecc5e8fSTrond Myklebust 		return 0;
935f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
936f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
937f2c77f4eSTrond Myklebust 		return 0;
938f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
939f2c77f4eSTrond Myklebust 		return 0;
940f2c77f4eSTrond Myklebust 	return 1;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds /*
9441d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
9451d6757fbSTrond Myklebust  *
9461d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
9471d6757fbSTrond Myklebust  * component of the path.
9481d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
9491d6757fbSTrond Myklebust  */
9501d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
9511d6757fbSTrond Myklebust {
9521d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
9531d6757fbSTrond Myklebust 		return 0;
9541d6757fbSTrond Myklebust 	return nd->flags & mask;
9551d6757fbSTrond Myklebust }
9561d6757fbSTrond Myklebust 
9571d6757fbSTrond Myklebust /*
958a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
959a12802caSTrond Myklebust  * an O_EXCL create using this path component.
960a12802caSTrond Myklebust  */
961a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
962a12802caSTrond Myklebust {
963a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
964a12802caSTrond Myklebust 		return 0;
9653516586aSAl Viro 	return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
966a12802caSTrond Myklebust }
967a12802caSTrond Myklebust 
968a12802caSTrond Myklebust /*
9691d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9701d6757fbSTrond Myklebust  *
9711d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9721d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9731d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9741d6757fbSTrond Myklebust  *
9751d6757fbSTrond Myklebust  */
9761da177e4SLinus Torvalds static inline
9771da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
9801da177e4SLinus Torvalds 
9814e99a1ffSTrond Myklebust 	if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
9824e99a1ffSTrond Myklebust 		return 0;
9831da177e4SLinus Torvalds 	if (nd != NULL) {
9841da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
9851d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
9861da177e4SLinus Torvalds 			goto out_force;
9871da177e4SLinus Torvalds 		/* This is an open(2) */
9881d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
9894e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
9904e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
9914e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
9921da177e4SLinus Torvalds 			goto out_force;
9934f48af45STrond Myklebust 		return 0;
9941da177e4SLinus Torvalds 	}
9951da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
9961da177e4SLinus Torvalds out_force:
9971da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /*
10011da177e4SLinus Torvalds  * We judge how long we want to trust negative
10021da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
10031da177e4SLinus Torvalds  *
10041da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
10051da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
10061da177e4SLinus Torvalds  */
10071da177e4SLinus Torvalds static inline
10081da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
10091da177e4SLinus Torvalds 		       struct nameidata *nd)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
10121d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
10131da177e4SLinus Torvalds 		return 0;
10144eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
10154eec952eSTrond Myklebust 		return 1;
10161da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds /*
10201da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
10211da177e4SLinus Torvalds  * and we should check whether we can really trust that
10221da177e4SLinus Torvalds  * lookup.
10231da177e4SLinus Torvalds  *
10241da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
10251da177e4SLinus Torvalds  * we have an inode!
10261da177e4SLinus Torvalds  *
10271da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
10281da177e4SLinus Torvalds  * cached dentry and do a new lookup.
10291da177e4SLinus Torvalds  */
10301da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	struct inode *dir;
10331da177e4SLinus Torvalds 	struct inode *inode;
10341da177e4SLinus Torvalds 	struct dentry *parent;
1035e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1036e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10371da177e4SLinus Torvalds 	int error;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	parent = dget_parent(dentry);
10401da177e4SLinus Torvalds 	dir = parent->d_inode;
104191d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
10421da177e4SLinus Torvalds 	inode = dentry->d_inode;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	if (!inode) {
10451da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
10461da177e4SLinus Torvalds 			goto out_bad;
10471da177e4SLinus Torvalds 		goto out_valid;
10481da177e4SLinus Torvalds 	}
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10511e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10523110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10531e7cb3dcSChuck Lever 				dentry->d_name.name);
10541da177e4SLinus Torvalds 		goto out_bad;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
105715860ab1STrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
105815860ab1STrond Myklebust 		goto out_set_verifier;
105915860ab1STrond Myklebust 
10601da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1061a12802caSTrond Myklebust 	if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
10621da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
10631da177e4SLinus Torvalds 			goto out_zap_parent;
10641da177e4SLinus Torvalds 		goto out_valid;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10681da177e4SLinus Torvalds 		goto out_bad;
10691da177e4SLinus Torvalds 
1070e1fb4d05STrond Myklebust 	error = -ENOMEM;
1071e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1072e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1073e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1074e1fb4d05STrond Myklebust 		goto out_error;
1075e1fb4d05STrond Myklebust 
1076e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
10771da177e4SLinus Torvalds 	if (error)
10781da177e4SLinus Torvalds 		goto out_bad;
1079e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
10801da177e4SLinus Torvalds 		goto out_bad;
1081e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
10821da177e4SLinus Torvalds 		goto out_bad;
10831da177e4SLinus Torvalds 
1084e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1085e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
108615860ab1STrond Myklebust out_set_verifier:
1087cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10881da177e4SLinus Torvalds  out_valid:
10891da177e4SLinus Torvalds 	dput(parent);
10901e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
10913110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
10921e7cb3dcSChuck Lever 			dentry->d_name.name);
10931da177e4SLinus Torvalds 	return 1;
10941da177e4SLinus Torvalds out_zap_parent:
10951da177e4SLinus Torvalds 	nfs_zap_caches(dir);
10961da177e4SLinus Torvalds  out_bad:
1097a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
10981da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
10991da177e4SLinus Torvalds 		/* Purge readdir caches. */
11001da177e4SLinus Torvalds 		nfs_zap_caches(inode);
11011da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
11021da177e4SLinus Torvalds 		if (have_submounts(dentry))
11031da177e4SLinus Torvalds 			goto out_valid;
1104d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1105d9e80b7dSAl Viro 			goto out_valid;
11061da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 	d_drop(dentry);
1109e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1110e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
11111da177e4SLinus Torvalds 	dput(parent);
11121e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
11133110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
11141e7cb3dcSChuck Lever 			dentry->d_name.name);
11151da177e4SLinus Torvalds 	return 0;
1116e1fb4d05STrond Myklebust out_error:
1117e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1118e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1119e1fb4d05STrond Myklebust 	dput(parent);
1120e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1121e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1122e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1123e1fb4d05STrond Myklebust 	return error;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds /*
11271da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
11281da177e4SLinus Torvalds  */
11291da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
11321da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
11331da177e4SLinus Torvalds 		dentry->d_flags);
11341da177e4SLinus Torvalds 
113577f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
113677f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
113777f11192STrond Myklebust 		return 1;
113877f11192STrond Myklebust 
11391da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11401da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
11411da177e4SLinus Torvalds 		return 1;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
11441da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
11451da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
11461da177e4SLinus Torvalds 		return 1;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 	return 0;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
11531b83d707STrond Myklebust {
11541b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
11551b83d707STrond Myklebust 	if (inode->i_nlink > 0)
11561b83d707STrond Myklebust 		drop_nlink(inode);
11571b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
11581b83d707STrond Myklebust }
11591b83d707STrond Myklebust 
11601da177e4SLinus Torvalds /*
11611da177e4SLinus Torvalds  * Called when the dentry loses inode.
11621da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
11631da177e4SLinus Torvalds  */
11641da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
11651da177e4SLinus Torvalds {
116683672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
116783672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
116883672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
116983672d39SNeil Brown 
11701da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11719a53c3a7SDave Hansen 		drop_nlink(inode);
1172e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 	iput(inode);
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
1177f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
11781da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
11791da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
11801da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
11811da177e4SLinus Torvalds };
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
11841da177e4SLinus Torvalds {
11851da177e4SLinus Torvalds 	struct dentry *res;
1186565277f6STrond Myklebust 	struct dentry *parent;
11871da177e4SLinus Torvalds 	struct inode *inode = NULL;
1188e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1189e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
11901da177e4SLinus Torvalds 	int error;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
11931da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
119491d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
11971da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
11981da177e4SLinus Torvalds 		goto out;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
12011da177e4SLinus Torvalds 
1202fd684071STrond Myklebust 	/*
1203fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1204fd684071STrond Myklebust 	 * but don't hash the dentry.
1205fd684071STrond Myklebust 	 */
1206fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
1207fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1208fd684071STrond Myklebust 		res = NULL;
1209fc0f684cSTrond Myklebust 		goto out;
1210fd684071STrond Myklebust 	}
12111da177e4SLinus Torvalds 
1212e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1213e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1214e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1215e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1216e1fb4d05STrond Myklebust 		goto out;
1217e1fb4d05STrond Myklebust 
1218565277f6STrond Myklebust 	parent = dentry->d_parent;
1219565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1220565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
1221e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
12221da177e4SLinus Torvalds 	if (error == -ENOENT)
12231da177e4SLinus Torvalds 		goto no_entry;
12241da177e4SLinus Torvalds 	if (error < 0) {
12251da177e4SLinus Torvalds 		res = ERR_PTR(error);
1226565277f6STrond Myklebust 		goto out_unblock_sillyrename;
12271da177e4SLinus Torvalds 	}
1228e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
122903f28e3aSTrond Myklebust 	res = (struct dentry *)inode;
123003f28e3aSTrond Myklebust 	if (IS_ERR(res))
1231565277f6STrond Myklebust 		goto out_unblock_sillyrename;
123254ceac45SDavid Howells 
12331da177e4SLinus Torvalds no_entry:
123454ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
12359eaef27bSTrond Myklebust 	if (res != NULL) {
12369eaef27bSTrond Myklebust 		if (IS_ERR(res))
1237565277f6STrond Myklebust 			goto out_unblock_sillyrename;
12381da177e4SLinus Torvalds 		dentry = res;
12399eaef27bSTrond Myklebust 	}
12401da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1241565277f6STrond Myklebust out_unblock_sillyrename:
1242565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
12431da177e4SLinus Torvalds out:
1244e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1245e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
12461da177e4SLinus Torvalds 	return res;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
12501da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
12511da177e4SLinus Torvalds 
1252f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
12531da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
12541da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12551da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
12561da177e4SLinus Torvalds };
12571da177e4SLinus Torvalds 
12581d6757fbSTrond Myklebust /*
12591d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
12601d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
12611d6757fbSTrond Myklebust  */
12625584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd)
12631da177e4SLinus Torvalds {
12641d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
12651da177e4SLinus Torvalds 		return 0;
12661da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
12671da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
12681da177e4SLinus Torvalds 		return 0;
12691da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
12702c463e95SDave Hansen 	if (__mnt_is_readonly(nd->path.mnt) &&
12712c463e95SDave Hansen 	    (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
12721da177e4SLinus Torvalds 		return 0;
12731da177e4SLinus Torvalds 	return 1;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
1276cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1277cd9a1c0eSTrond Myklebust {
1278cd9a1c0eSTrond Myklebust 	struct path path = {
1279cd9a1c0eSTrond Myklebust 		.mnt = nd->path.mnt,
1280cd9a1c0eSTrond Myklebust 		.dentry = dentry,
1281cd9a1c0eSTrond Myklebust 	};
1282cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1283cd9a1c0eSTrond Myklebust 	struct rpc_cred *cred;
1284cd9a1c0eSTrond Myklebust 	fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1285cd9a1c0eSTrond Myklebust 
1286cd9a1c0eSTrond Myklebust 	cred = rpc_lookup_cred();
1287cd9a1c0eSTrond Myklebust 	if (IS_ERR(cred))
1288cd9a1c0eSTrond Myklebust 		return ERR_CAST(cred);
1289cd9a1c0eSTrond Myklebust 	ctx = alloc_nfs_open_context(&path, cred, fmode);
1290cd9a1c0eSTrond Myklebust 	put_rpccred(cred);
1291cd9a1c0eSTrond Myklebust 	if (ctx == NULL)
1292cd9a1c0eSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1293cd9a1c0eSTrond Myklebust 	return ctx;
1294cd9a1c0eSTrond Myklebust }
1295cd9a1c0eSTrond Myklebust 
1296cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1297cd9a1c0eSTrond Myklebust {
1298cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1299cd9a1c0eSTrond Myklebust 	return 0;
1300cd9a1c0eSTrond Myklebust }
1301cd9a1c0eSTrond Myklebust 
1302cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1303cd9a1c0eSTrond Myklebust {
1304cd9a1c0eSTrond Myklebust 	struct file *filp;
1305cd9a1c0eSTrond Myklebust 	int ret = 0;
1306cd9a1c0eSTrond Myklebust 
1307cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1308cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
1309cd9a1c0eSTrond Myklebust 		ret = nfs_may_open(ctx->path.dentry->d_inode,
1310cd9a1c0eSTrond Myklebust 				ctx->cred,
1311cd9a1c0eSTrond Myklebust 				nd->intent.open.flags);
1312cd9a1c0eSTrond Myklebust 		if (ret < 0)
1313cd9a1c0eSTrond Myklebust 			goto out;
1314cd9a1c0eSTrond Myklebust 	}
1315cd9a1c0eSTrond Myklebust 	filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1316cd9a1c0eSTrond Myklebust 	if (IS_ERR(filp))
1317cd9a1c0eSTrond Myklebust 		ret = PTR_ERR(filp);
1318cd9a1c0eSTrond Myklebust 	else
1319cd9a1c0eSTrond Myklebust 		nfs_file_set_open_context(filp, ctx);
1320cd9a1c0eSTrond Myklebust out:
1321cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1322cd9a1c0eSTrond Myklebust 	return ret;
1323cd9a1c0eSTrond Myklebust }
1324cd9a1c0eSTrond Myklebust 
13251da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
13261da177e4SLinus Torvalds {
1327cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1328cd9a1c0eSTrond Myklebust 	struct iattr attr;
13291da177e4SLinus Torvalds 	struct dentry *res = NULL;
1330f46e0bd3STrond Myklebust 	struct inode *inode;
1331cd9a1c0eSTrond Myklebust 	int open_flags;
1332898f635cSTrond Myklebust 	int err;
13331da177e4SLinus Torvalds 
13341e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
13351e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
13361e7cb3dcSChuck Lever 
13371da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
13385584c306STrond Myklebust 	if (!is_atomic_open(nd))
13391da177e4SLinus Torvalds 		goto no_open;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
13421da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
13431da177e4SLinus Torvalds 		goto out;
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
13461da177e4SLinus Torvalds 
1347d4d9cdcbSTrond Myklebust 	/* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1348d4d9cdcbSTrond Myklebust 	 * the dentry. */
13493516586aSAl Viro 	if (nd->flags & LOOKUP_EXCL) {
1350d4d9cdcbSTrond Myklebust 		d_instantiate(dentry, NULL);
135102a913a7STrond Myklebust 		goto out;
135202a913a7STrond Myklebust 	}
13531da177e4SLinus Torvalds 
1354cd9a1c0eSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1355cd9a1c0eSTrond Myklebust 	res = ERR_CAST(ctx);
1356cd9a1c0eSTrond Myklebust 	if (IS_ERR(ctx))
1357cd9a1c0eSTrond Myklebust 		goto out;
1358cd9a1c0eSTrond Myklebust 
1359cd9a1c0eSTrond Myklebust 	open_flags = nd->intent.open.flags;
1360cd9a1c0eSTrond Myklebust 	if (nd->flags & LOOKUP_CREATE) {
1361cd9a1c0eSTrond Myklebust 		attr.ia_mode = nd->intent.open.create_mode;
1362cd9a1c0eSTrond Myklebust 		attr.ia_valid = ATTR_MODE;
1363cd9a1c0eSTrond Myklebust 		if (!IS_POSIXACL(dir))
1364cd9a1c0eSTrond Myklebust 			attr.ia_mode &= ~current_umask();
1365cd9a1c0eSTrond Myklebust 	} else {
1366898f635cSTrond Myklebust 		open_flags &= ~(O_EXCL | O_CREAT);
1367cd9a1c0eSTrond Myklebust 		attr.ia_valid = 0;
1368cd9a1c0eSTrond Myklebust 	}
1369cd9a1c0eSTrond Myklebust 
13701da177e4SLinus Torvalds 	/* Open the file on the server */
1371f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
13722b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1373f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1374f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1375cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
1376f46e0bd3STrond Myklebust 		switch (PTR_ERR(inode)) {
13771da177e4SLinus Torvalds 			/* Make a negative dentry */
13781da177e4SLinus Torvalds 			case -ENOENT:
1379f46e0bd3STrond Myklebust 				d_add(dentry, NULL);
138002a913a7STrond Myklebust 				res = NULL;
138102a913a7STrond Myklebust 				goto out;
13821da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
13836f926b5bSTrond Myklebust 			case -ENOTDIR:
13846f926b5bSTrond Myklebust 				goto no_open;
13851da177e4SLinus Torvalds 			case -ELOOP:
13861da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
13871da177e4SLinus Torvalds 					goto no_open;
138823ebbd9aSTrond Myklebust 			/* case -EISDIR: */
13891da177e4SLinus Torvalds 			/* case -EINVAL: */
13901da177e4SLinus Torvalds 			default:
1391f46e0bd3STrond Myklebust 				res = ERR_CAST(inode);
13921da177e4SLinus Torvalds 				goto out;
13931da177e4SLinus Torvalds 		}
1394cd9a1c0eSTrond Myklebust 	}
1395f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1396898f635cSTrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1397f46e0bd3STrond Myklebust 	if (res != NULL) {
1398f46e0bd3STrond Myklebust 		dput(ctx->path.dentry);
1399f46e0bd3STrond Myklebust 		ctx->path.dentry = dget(res);
14001da177e4SLinus Torvalds 		dentry = res;
1401f46e0bd3STrond Myklebust 	}
1402898f635cSTrond Myklebust 	err = nfs_intent_set_file(nd, ctx);
1403898f635cSTrond Myklebust 	if (err < 0) {
1404898f635cSTrond Myklebust 		if (res != NULL)
1405898f635cSTrond Myklebust 			dput(res);
1406898f635cSTrond Myklebust 		return ERR_PTR(err);
1407898f635cSTrond Myklebust 	}
14081da177e4SLinus Torvalds out:
1409f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14101da177e4SLinus Torvalds 	return res;
14111da177e4SLinus Torvalds no_open:
14121da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
14161da177e4SLinus Torvalds {
14171da177e4SLinus Torvalds 	struct dentry *parent = NULL;
14181da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14191da177e4SLinus Torvalds 	struct inode *dir;
1420b8d4caddSTrond Myklebust 	struct nfs_open_context *ctx;
14211da177e4SLinus Torvalds 	int openflags, ret = 0;
14221da177e4SLinus Torvalds 
14231f063d2cSTrond Myklebust 	if (!is_atomic_open(nd) || d_mountpoint(dentry))
14245584c306STrond Myklebust 		goto no_open;
14252b484297STrond Myklebust 
14261da177e4SLinus Torvalds 	parent = dget_parent(dentry);
14271da177e4SLinus Torvalds 	dir = parent->d_inode;
14282b484297STrond Myklebust 
14291da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
14301da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
14311da177e4SLinus Torvalds 	 */
1432216d5d06STrond Myklebust 	if (inode == NULL) {
1433216d5d06STrond Myklebust 		if (!nfs_neg_need_reval(dir, dentry, nd))
1434216d5d06STrond Myklebust 			ret = 1;
14351da177e4SLinus Torvalds 		goto out;
1436216d5d06STrond Myklebust 	}
1437216d5d06STrond Myklebust 
14381da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
14391da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
14405584c306STrond Myklebust 		goto no_open_dput;
14411da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
14421da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
14431da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
14445584c306STrond Myklebust 		goto no_open_dput;
14451da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
14460a377cffSTrond Myklebust 	openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
14471da177e4SLinus Torvalds 
1448b8d4caddSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1449b8d4caddSTrond Myklebust 	ret = PTR_ERR(ctx);
1450b8d4caddSTrond Myklebust 	if (IS_ERR(ctx))
1451b8d4caddSTrond Myklebust 		goto out;
14521da177e4SLinus Torvalds 	/*
14531b1dcc1bSJes Sorensen 	 * Note: we're not holding inode->i_mutex and so may be racing with
14541da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
14551da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
14561da177e4SLinus Torvalds 	 */
14572b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
1458535918f1STrond Myklebust 	if (IS_ERR(inode)) {
1459535918f1STrond Myklebust 		ret = PTR_ERR(inode);
1460535918f1STrond Myklebust 		switch (ret) {
1461535918f1STrond Myklebust 		case -EPERM:
1462535918f1STrond Myklebust 		case -EACCES:
1463535918f1STrond Myklebust 		case -EDQUOT:
1464535918f1STrond Myklebust 		case -ENOSPC:
1465535918f1STrond Myklebust 		case -EROFS:
1466535918f1STrond Myklebust 			goto out_put_ctx;
1467535918f1STrond Myklebust 		default:
1468535918f1STrond Myklebust 			goto out_drop;
1469535918f1STrond Myklebust 		}
1470535918f1STrond Myklebust 	}
1471535918f1STrond Myklebust 	iput(inode);
1472898f635cSTrond Myklebust 	if (inode != dentry->d_inode)
1473535918f1STrond Myklebust 		goto out_drop;
1474898f635cSTrond Myklebust 
1475898f635cSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1476898f635cSTrond Myklebust 	ret = nfs_intent_set_file(nd, ctx);
1477898f635cSTrond Myklebust 	if (ret >= 0)
1478898f635cSTrond Myklebust 		ret = 1;
14791da177e4SLinus Torvalds out:
14801da177e4SLinus Torvalds 	dput(parent);
14811da177e4SLinus Torvalds 	return ret;
1482535918f1STrond Myklebust out_drop:
1483535918f1STrond Myklebust 	d_drop(dentry);
1484535918f1STrond Myklebust 	ret = 0;
1485535918f1STrond Myklebust out_put_ctx:
1486535918f1STrond Myklebust 	put_nfs_open_context(ctx);
1487535918f1STrond Myklebust 	goto out;
1488535918f1STrond Myklebust 
14895584c306STrond Myklebust no_open_dput:
14901da177e4SLinus Torvalds 	dput(parent);
14915584c306STrond Myklebust no_open:
14921da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
14931da177e4SLinus Torvalds }
1494c0204fd2STrond Myklebust 
1495c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1496c0204fd2STrond Myklebust 		struct nameidata *nd)
1497c0204fd2STrond Myklebust {
1498c0204fd2STrond Myklebust 	struct nfs_open_context *ctx = NULL;
1499c0204fd2STrond Myklebust 	struct iattr attr;
1500c0204fd2STrond Myklebust 	int error;
1501c0204fd2STrond Myklebust 	int open_flags = 0;
1502c0204fd2STrond Myklebust 
1503c0204fd2STrond Myklebust 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1504c0204fd2STrond Myklebust 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1505c0204fd2STrond Myklebust 
1506c0204fd2STrond Myklebust 	attr.ia_mode = mode;
1507c0204fd2STrond Myklebust 	attr.ia_valid = ATTR_MODE;
1508c0204fd2STrond Myklebust 
1509c0204fd2STrond Myklebust 	if ((nd->flags & LOOKUP_CREATE) != 0) {
1510c0204fd2STrond Myklebust 		open_flags = nd->intent.open.flags;
1511c0204fd2STrond Myklebust 
1512c0204fd2STrond Myklebust 		ctx = nameidata_to_nfs_open_context(dentry, nd);
1513c0204fd2STrond Myklebust 		error = PTR_ERR(ctx);
1514c0204fd2STrond Myklebust 		if (IS_ERR(ctx))
1515898f635cSTrond Myklebust 			goto out_err_drop;
1516c0204fd2STrond Myklebust 	}
1517c0204fd2STrond Myklebust 
1518c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1519c0204fd2STrond Myklebust 	if (error != 0)
1520c0204fd2STrond Myklebust 		goto out_put_ctx;
1521898f635cSTrond Myklebust 	if (ctx != NULL) {
1522898f635cSTrond Myklebust 		error = nfs_intent_set_file(nd, ctx);
1523898f635cSTrond Myklebust 		if (error < 0)
1524898f635cSTrond Myklebust 			goto out_err;
1525898f635cSTrond Myklebust 	}
1526c0204fd2STrond Myklebust 	return 0;
1527c0204fd2STrond Myklebust out_put_ctx:
1528c0204fd2STrond Myklebust 	if (ctx != NULL)
1529c0204fd2STrond Myklebust 		put_nfs_open_context(ctx);
1530898f635cSTrond Myklebust out_err_drop:
1531c0204fd2STrond Myklebust 	d_drop(dentry);
1532898f635cSTrond Myklebust out_err:
1533c0204fd2STrond Myklebust 	return error;
1534c0204fd2STrond Myklebust }
1535c0204fd2STrond Myklebust 
15361da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds /*
15391da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
15401da177e4SLinus Torvalds  */
15411da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
15421da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
15431da177e4SLinus Torvalds {
1544fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1545fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
15461da177e4SLinus Torvalds 	struct inode *inode;
15471da177e4SLinus Torvalds 	int error = -EACCES;
15481da177e4SLinus Torvalds 
1549fab728e1STrond Myklebust 	d_drop(dentry);
1550fab728e1STrond Myklebust 
15511da177e4SLinus Torvalds 	/* We may have been initialized further down */
15521da177e4SLinus Torvalds 	if (dentry->d_inode)
1553fab728e1STrond Myklebust 		goto out;
15541da177e4SLinus Torvalds 	if (fhandle->size == 0) {
15551da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
15561da177e4SLinus Torvalds 		if (error)
1557fab728e1STrond Myklebust 			goto out_error;
15581da177e4SLinus Torvalds 	}
15595724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15601da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15611da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15628fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
15631da177e4SLinus Torvalds 		if (error < 0)
1564fab728e1STrond Myklebust 			goto out_error;
15651da177e4SLinus Torvalds 	}
15661da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
156703f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
156803f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1569fab728e1STrond Myklebust 		goto out_error;
1570fab728e1STrond Myklebust 	d_add(dentry, inode);
1571fab728e1STrond Myklebust out:
1572fab728e1STrond Myklebust 	dput(parent);
15731da177e4SLinus Torvalds 	return 0;
1574fab728e1STrond Myklebust out_error:
1575fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1576fab728e1STrond Myklebust 	dput(parent);
1577fab728e1STrond Myklebust 	return error;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds /*
15811da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15821da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15831da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15841da177e4SLinus Torvalds  * reply path made it appear to have failed.
15851da177e4SLinus Torvalds  */
15861da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
15871da177e4SLinus Torvalds 		struct nameidata *nd)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	struct iattr attr;
15901da177e4SLinus Torvalds 	int error;
15911da177e4SLinus Torvalds 
15921e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
15931e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	attr.ia_mode = mode;
15961da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15971da177e4SLinus Torvalds 
1598c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
15991da177e4SLinus Torvalds 	if (error != 0)
16001da177e4SLinus Torvalds 		goto out_err;
16011da177e4SLinus Torvalds 	return 0;
16021da177e4SLinus Torvalds out_err:
16031da177e4SLinus Torvalds 	d_drop(dentry);
16041da177e4SLinus Torvalds 	return error;
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds /*
16081da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16091da177e4SLinus Torvalds  */
16101da177e4SLinus Torvalds static int
16111da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
16121da177e4SLinus Torvalds {
16131da177e4SLinus Torvalds 	struct iattr attr;
16141da177e4SLinus Torvalds 	int status;
16151da177e4SLinus Torvalds 
16161e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
16171e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
16201da177e4SLinus Torvalds 		return -EINVAL;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	attr.ia_mode = mode;
16231da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
16261da177e4SLinus Torvalds 	if (status != 0)
16271da177e4SLinus Torvalds 		goto out_err;
16281da177e4SLinus Torvalds 	return 0;
16291da177e4SLinus Torvalds out_err:
16301da177e4SLinus Torvalds 	d_drop(dentry);
16311da177e4SLinus Torvalds 	return status;
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds /*
16351da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
16361da177e4SLinus Torvalds  */
16371da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
16381da177e4SLinus Torvalds {
16391da177e4SLinus Torvalds 	struct iattr attr;
16401da177e4SLinus Torvalds 	int error;
16411da177e4SLinus Torvalds 
16421e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
16431e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
16461da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16491da177e4SLinus Torvalds 	if (error != 0)
16501da177e4SLinus Torvalds 		goto out_err;
16511da177e4SLinus Torvalds 	return 0;
16521da177e4SLinus Torvalds out_err:
16531da177e4SLinus Torvalds 	d_drop(dentry);
16541da177e4SLinus Torvalds 	return error;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
1657d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1658d45b9d8bSTrond Myklebust {
1659d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1660d45b9d8bSTrond Myklebust 		d_delete(dentry);
1661d45b9d8bSTrond Myklebust }
1662d45b9d8bSTrond Myklebust 
16631da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16641da177e4SLinus Torvalds {
16651da177e4SLinus Torvalds 	int error;
16661da177e4SLinus Torvalds 
16671e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16681e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16711da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
16721da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1673ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1674d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1675d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	return error;
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds /*
16811da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
16821da177e4SLinus Torvalds  * and after checking that the file has only one user.
16831da177e4SLinus Torvalds  *
16841da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
16851da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
16861da177e4SLinus Torvalds  */
16871da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
16881da177e4SLinus Torvalds {
16891da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
16901da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16911da177e4SLinus Torvalds 	int error = -EBUSY;
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
16941da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
16971da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16981da177e4SLinus Torvalds 		error = 0;
16991da177e4SLinus Torvalds 		goto out;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	if (inode != NULL) {
1703cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
17041da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
17051da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
17061da177e4SLinus Torvalds 		if (error == 0)
17071b83d707STrond Myklebust 			nfs_drop_nlink(inode);
17085ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
17091da177e4SLinus Torvalds 	} else
17101da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1711d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1712d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
17131da177e4SLinus Torvalds out:
17141da177e4SLinus Torvalds 	return error;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
17181da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
17191da177e4SLinus Torvalds  *
17201da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
17211da177e4SLinus Torvalds  */
17221da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	int error;
17251da177e4SLinus Torvalds 	int need_rehash = 0;
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
17281da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
17311da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
17321da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) > 1) {
17331da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
17341da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
1735ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1736ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
17371da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
17381da177e4SLinus Torvalds 		return error;
17391da177e4SLinus Torvalds 	}
17401da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
17411da177e4SLinus Torvalds 		__d_drop(dentry);
17421da177e4SLinus Torvalds 		need_rehash = 1;
17431da177e4SLinus Torvalds 	}
17441da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
17451da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
17461da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1747d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17481da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17491da177e4SLinus Torvalds 	} else if (need_rehash)
17501da177e4SLinus Torvalds 		d_rehash(dentry);
17511da177e4SLinus Torvalds 	return error;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
1754873101b3SChuck Lever /*
1755873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1756873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1757873101b3SChuck Lever  * using prepare_write/commit_write.
1758873101b3SChuck Lever  *
1759873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1760873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1761873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1762873101b3SChuck Lever  * symlink request has completed on the server.
1763873101b3SChuck Lever  *
1764873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1765873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1766873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1767873101b3SChuck Lever  * and move the raw page into its mapping.
1768873101b3SChuck Lever  */
1769873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17701da177e4SLinus Torvalds {
1771873101b3SChuck Lever 	struct pagevec lru_pvec;
1772873101b3SChuck Lever 	struct page *page;
1773873101b3SChuck Lever 	char *kaddr;
17741da177e4SLinus Torvalds 	struct iattr attr;
1775873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17761da177e4SLinus Torvalds 	int error;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
17791da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
17801da177e4SLinus Torvalds 
1781873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1782873101b3SChuck Lever 		return -ENAMETOOLONG;
17831da177e4SLinus Torvalds 
1784873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1785873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
17861da177e4SLinus Torvalds 
178783d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
178876566991STrond Myklebust 	if (!page)
1789873101b3SChuck Lever 		return -ENOMEM;
1790873101b3SChuck Lever 
1791873101b3SChuck Lever 	kaddr = kmap_atomic(page, KM_USER0);
1792873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1793873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1794873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1795873101b3SChuck Lever 	kunmap_atomic(kaddr, KM_USER0);
1796873101b3SChuck Lever 
179794a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1798873101b3SChuck Lever 	if (error != 0) {
1799873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1800873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1801873101b3SChuck Lever 			dentry->d_name.name, symname, error);
18021da177e4SLinus Torvalds 		d_drop(dentry);
1803873101b3SChuck Lever 		__free_page(page);
18041da177e4SLinus Torvalds 		return error;
18051da177e4SLinus Torvalds 	}
18061da177e4SLinus Torvalds 
1807873101b3SChuck Lever 	/*
1808873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1809873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1810873101b3SChuck Lever 	 */
1811873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1812873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1813873101b3SChuck Lever 							GFP_KERNEL)) {
181439cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
18154f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1816873101b3SChuck Lever 		SetPageUptodate(page);
1817873101b3SChuck Lever 		unlock_page(page);
1818873101b3SChuck Lever 	} else
1819873101b3SChuck Lever 		__free_page(page);
1820873101b3SChuck Lever 
1821873101b3SChuck Lever 	return 0;
1822873101b3SChuck Lever }
1823873101b3SChuck Lever 
18241da177e4SLinus Torvalds static int
18251da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
18281da177e4SLinus Torvalds 	int error;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
18311da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18321da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
18331da177e4SLinus Torvalds 
18349a3936aaSTrond Myklebust 	nfs_inode_return_delegation(inode);
18359a3936aaSTrond Myklebust 
18369697d234STrond Myklebust 	d_drop(dentry);
18371da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1838cf809556STrond Myklebust 	if (error == 0) {
18397de9c6eeSAl Viro 		ihold(inode);
18409697d234STrond Myklebust 		d_add(dentry, inode);
1841cf809556STrond Myklebust 	}
18421da177e4SLinus Torvalds 	return error;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds /*
18461da177e4SLinus Torvalds  * RENAME
18471da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18481da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18491da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18501da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18511da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18521da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18531da177e4SLinus Torvalds  *
18541da177e4SLinus Torvalds  * FIXED.
18551da177e4SLinus Torvalds  *
18561da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18571da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18581da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18591da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18601da177e4SLinus Torvalds  * using the inode layer
18611da177e4SLinus Torvalds  *
18621da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18631da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18641da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18651da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18661da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18671da177e4SLinus Torvalds  * the rename.
18681da177e4SLinus Torvalds  */
18691da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18701da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18711da177e4SLinus Torvalds {
18721da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18731da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18741da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
18751da177e4SLinus Torvalds 	int error = -EBUSY;
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
18781da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18791da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
18801da177e4SLinus Torvalds 		 atomic_read(&new_dentry->d_count));
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	/*
188328f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
188428f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
188528f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
188628f79a1aSMiklos Szeredi 	 * the new target.
18871da177e4SLinus Torvalds 	 */
188827226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
188927226104SMiklos Szeredi 		/*
189027226104SMiklos Szeredi 		 * To prevent any new references to the target during the
189127226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
189227226104SMiklos Szeredi 		 */
189327226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
189427226104SMiklos Szeredi 			d_drop(new_dentry);
189527226104SMiklos Szeredi 			rehash = new_dentry;
189627226104SMiklos Szeredi 		}
189727226104SMiklos Szeredi 
189827226104SMiklos Szeredi 		if (atomic_read(&new_dentry->d_count) > 2) {
18991da177e4SLinus Torvalds 			int err;
190027226104SMiklos Szeredi 
19011da177e4SLinus Torvalds 			/* copy the target dentry's name */
19021da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
19031da177e4SLinus Torvalds 					 &new_dentry->d_name);
19041da177e4SLinus Torvalds 			if (!dentry)
19051da177e4SLinus Torvalds 				goto out;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
19081da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
190924e93025SMiklos Szeredi 			if (err)
19101da177e4SLinus Torvalds 				goto out;
191124e93025SMiklos Szeredi 
191224e93025SMiklos Szeredi 			new_dentry = dentry;
191356335936SOGAWA Hirofumi 			rehash = NULL;
191424e93025SMiklos Szeredi 			new_inode = NULL;
1915b1e4adf4STrond Myklebust 		}
191627226104SMiklos Szeredi 	}
19171da177e4SLinus Torvalds 
1918cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
1919b1e4adf4STrond Myklebust 	if (new_inode != NULL)
192024174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
19231da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
19245ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
19251da177e4SLinus Torvalds out:
19261da177e4SLinus Torvalds 	if (rehash)
19271da177e4SLinus Torvalds 		d_rehash(rehash);
19281da177e4SLinus Torvalds 	if (!error) {
1929b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1930b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
19311da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
19328fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
19338fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1934d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1935d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	/* new dentry created? */
19381da177e4SLinus Torvalds 	if (dentry)
19391da177e4SLinus Torvalds 		dput(dentry);
19401da177e4SLinus Torvalds 	return error;
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
1943cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1944cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1945cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1946cfcea3e8STrond Myklebust 
19471c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19481c3c07e9STrond Myklebust {
19491c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19501c3c07e9STrond Myklebust 	kfree(entry);
1951cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1952cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1953cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19541c3c07e9STrond Myklebust }
19551c3c07e9STrond Myklebust 
19561a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19571a81bb8aSTrond Myklebust {
19581a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19591a81bb8aSTrond Myklebust 
19601a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19611a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19621a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19631a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19641a81bb8aSTrond Myklebust 	}
19651a81bb8aSTrond Myklebust }
19661a81bb8aSTrond Myklebust 
19677f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
1968979df72eSTrond Myklebust {
1969979df72eSTrond Myklebust 	LIST_HEAD(head);
1970aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1971979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
1972979df72eSTrond Myklebust 
197361d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
197461d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
19759c7e7e23STrond Myklebust 
1976a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1977aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1978979df72eSTrond Myklebust 		struct inode *inode;
1979979df72eSTrond Myklebust 
1980979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1981979df72eSTrond Myklebust 			break;
19829c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
1983979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1984979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1985979df72eSTrond Myklebust 			goto remove_lru_entry;
1986979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1987979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1988979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1989979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1990979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1991979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1992979df72eSTrond Myklebust 					&nfs_access_lru_list);
1993979df72eSTrond Myklebust 		else {
1994979df72eSTrond Myklebust remove_lru_entry:
1995979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
19969c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
1997979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
19989c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
1999979df72eSTrond Myklebust 		}
200059844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2001979df72eSTrond Myklebust 	}
2002979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20031a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
2004979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2005979df72eSTrond Myklebust }
2006979df72eSTrond Myklebust 
20071a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
20081c3c07e9STrond Myklebust {
20091c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20101a81bb8aSTrond Myklebust 	struct rb_node *n;
20111c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20121c3c07e9STrond Myklebust 
20131c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
20141c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
20151c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20161c3c07e9STrond Myklebust 		rb_erase(n, root_node);
20171a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
20181c3c07e9STrond Myklebust 	}
20191c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
20201c3c07e9STrond Myklebust }
20211c3c07e9STrond Myklebust 
20221c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
20231c3c07e9STrond Myklebust {
20241a81bb8aSTrond Myklebust 	LIST_HEAD(head);
20251a81bb8aSTrond Myklebust 
20261a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
20271a81bb8aSTrond Myklebust 		return;
2028cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2029cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
20301a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2031cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2032cfcea3e8STrond Myklebust 
20331c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20341a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
20351a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20361a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
20371a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
20381c3c07e9STrond Myklebust }
20391c3c07e9STrond Myklebust 
20401c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
20411c3c07e9STrond Myklebust {
20421c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
20431c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20441c3c07e9STrond Myklebust 
20451c3c07e9STrond Myklebust 	while (n != NULL) {
20461c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
20471c3c07e9STrond Myklebust 
20481c3c07e9STrond Myklebust 		if (cred < entry->cred)
20491c3c07e9STrond Myklebust 			n = n->rb_left;
20501c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20511c3c07e9STrond Myklebust 			n = n->rb_right;
20521c3c07e9STrond Myklebust 		else
20531c3c07e9STrond Myklebust 			return entry;
20541c3c07e9STrond Myklebust 	}
20551c3c07e9STrond Myklebust 	return NULL;
20561c3c07e9STrond Myklebust }
20571c3c07e9STrond Myklebust 
2058af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20591da177e4SLinus Torvalds {
206055296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
20611c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
20621c3c07e9STrond Myklebust 	int err = -ENOENT;
20631da177e4SLinus Torvalds 
20641c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20651c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
20661c3c07e9STrond Myklebust 		goto out_zap;
20671c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20681c3c07e9STrond Myklebust 	if (cache == NULL)
20691c3c07e9STrond Myklebust 		goto out;
2070b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
207164672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
20721c3c07e9STrond Myklebust 		goto out_stale;
20731c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
20741c3c07e9STrond Myklebust 	res->cred = cache->cred;
20751c3c07e9STrond Myklebust 	res->mask = cache->mask;
2076cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
20771c3c07e9STrond Myklebust 	err = 0;
20781c3c07e9STrond Myklebust out:
20791c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20801c3c07e9STrond Myklebust 	return err;
20811c3c07e9STrond Myklebust out_stale:
20821c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2083cfcea3e8STrond Myklebust 	list_del(&cache->lru);
20841c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20851c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
20861da177e4SLinus Torvalds 	return -ENOENT;
20871c3c07e9STrond Myklebust out_zap:
20881a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20891a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
20901c3c07e9STrond Myklebust 	return -ENOENT;
20911c3c07e9STrond Myklebust }
20921c3c07e9STrond Myklebust 
20931c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
20941c3c07e9STrond Myklebust {
2095cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2096cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20971c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
20981c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
20991c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
21001c3c07e9STrond Myklebust 
21011c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
21021c3c07e9STrond Myklebust 	while (*p != NULL) {
21031c3c07e9STrond Myklebust 		parent = *p;
21041c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
21051c3c07e9STrond Myklebust 
21061c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
21071c3c07e9STrond Myklebust 			p = &parent->rb_left;
21081c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
21091c3c07e9STrond Myklebust 			p = &parent->rb_right;
21101c3c07e9STrond Myklebust 		else
21111c3c07e9STrond Myklebust 			goto found;
21121c3c07e9STrond Myklebust 	}
21131c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
21141c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2115cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
21161c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21171c3c07e9STrond Myklebust 	return;
21181c3c07e9STrond Myklebust found:
21191c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2120cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2121cfcea3e8STrond Myklebust 	list_del(&entry->lru);
21221c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
21231c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
2126af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
21271da177e4SLinus Torvalds {
21281c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
21291c3c07e9STrond Myklebust 	if (cache == NULL)
21301c3c07e9STrond Myklebust 		return;
21311c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
21321da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
21331c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
21341da177e4SLinus Torvalds 	cache->mask = set->mask;
21351c3c07e9STrond Myklebust 
21361c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2137cfcea3e8STrond Myklebust 
2138cfcea3e8STrond Myklebust 	/* Update accounting */
2139cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2140cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2141cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2142cfcea3e8STrond Myklebust 
2143cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
21441a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2145cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
21461a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
21471a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21481a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2149cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2150cfcea3e8STrond Myklebust 	}
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21541da177e4SLinus Torvalds {
21551da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21561da177e4SLinus Torvalds 	int status;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21591da177e4SLinus Torvalds 	if (status == 0)
21601da177e4SLinus Torvalds 		goto out;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
21631da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
21641da177e4SLinus Torvalds 	cache.cred = cred;
21651da177e4SLinus Torvalds 	cache.jiffies = jiffies;
21661da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2167a71ee337SSuresh Jayaraman 	if (status != 0) {
2168a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2169a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2170a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2171a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2172a71ee337SSuresh Jayaraman 		}
21731da177e4SLinus Torvalds 		return status;
2174a71ee337SSuresh Jayaraman 	}
21751da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
21761da177e4SLinus Torvalds out:
2177e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21781da177e4SLinus Torvalds 		return 0;
21791da177e4SLinus Torvalds 	return -EACCES;
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds 
2182af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2183af22f94aSTrond Myklebust {
2184af22f94aSTrond Myklebust 	int mask = 0;
2185af22f94aSTrond Myklebust 
2186af22f94aSTrond Myklebust 	if (openflags & FMODE_READ)
2187af22f94aSTrond Myklebust 		mask |= MAY_READ;
2188af22f94aSTrond Myklebust 	if (openflags & FMODE_WRITE)
2189af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
2190af22f94aSTrond Myklebust 	if (openflags & FMODE_EXEC)
2191af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2192af22f94aSTrond Myklebust 	return mask;
2193af22f94aSTrond Myklebust }
2194af22f94aSTrond Myklebust 
2195af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2196af22f94aSTrond Myklebust {
2197af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2198af22f94aSTrond Myklebust }
2199af22f94aSTrond Myklebust 
2200e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask)
22011da177e4SLinus Torvalds {
22021da177e4SLinus Torvalds 	struct rpc_cred *cred;
22031da177e4SLinus Torvalds 	int res = 0;
22041da177e4SLinus Torvalds 
220591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
220691d5b470SChuck Lever 
2207e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
22081da177e4SLinus Torvalds 		goto out;
22091da177e4SLinus Torvalds 	/* Is this sys_access() ? */
22109cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
22111da177e4SLinus Torvalds 		goto force_lookup;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
22141da177e4SLinus Torvalds 		case S_IFLNK:
22151da177e4SLinus Torvalds 			goto out;
22161da177e4SLinus Torvalds 		case S_IFREG:
22171da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
22181da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
22197ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
22207ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
22211da177e4SLinus Torvalds 				goto out;
22221da177e4SLinus Torvalds 			break;
22231da177e4SLinus Torvalds 		case S_IFDIR:
22241da177e4SLinus Torvalds 			/*
22251da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
22261da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
22271da177e4SLinus Torvalds 			 */
22281da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
22291da177e4SLinus Torvalds 				goto out;
22301da177e4SLinus Torvalds 	}
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds force_lookup:
22331da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
22341da177e4SLinus Torvalds 		goto out_notsup;
22351da177e4SLinus Torvalds 
223698a8e323STrond Myklebust 	cred = rpc_lookup_cred();
22371da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
22381da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
22391da177e4SLinus Torvalds 		put_rpccred(cred);
22401da177e4SLinus Torvalds 	} else
22411da177e4SLinus Torvalds 		res = PTR_ERR(cred);
22421da177e4SLinus Torvalds out:
2243f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2244f696a365SMiklos Szeredi 		res = -EACCES;
2245f696a365SMiklos Szeredi 
22461e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
22471e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22481da177e4SLinus Torvalds 	return res;
22491da177e4SLinus Torvalds out_notsup:
22501da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22511da177e4SLinus Torvalds 	if (res == 0)
22521da177e4SLinus Torvalds 		res = generic_permission(inode, mask, NULL);
22531e7cb3dcSChuck Lever 	goto out;
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds /*
22571da177e4SLinus Torvalds  * Local variables:
22581da177e4SLinus Torvalds  *  version-control: t
22591da177e4SLinus Torvalds  *  kept-new-versions: 5
22601da177e4SLinus Torvalds  * End:
22611da177e4SLinus Torvalds  */
2262