xref: /openbmc/linux/fs/nfs/dir.c (revision 99424380)
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>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include "delegation.h"
3991d5b470SChuck Lever #include "iostat.h"
404c30d56eSAdrian Bunk #include "internal.h"
41cd9a1c0eSTrond Myklebust #include "fscache.h"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
461da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
471da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
481da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
491da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
501da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
511da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
521da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
531da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
541da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
551da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
561da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
577ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int);
58f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
59d1bacf9eSBryan Schumaker static int nfs_readdir_clear_array(struct page*, gfp_t);
601da177e4SLinus Torvalds 
614b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
62f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
631da177e4SLinus Torvalds 	.read		= generic_read_dir,
641da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
651da177e4SLinus Torvalds 	.open		= nfs_opendir,
661da177e4SLinus Torvalds 	.release	= nfs_release,
671da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
7092e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
711da177e4SLinus Torvalds 	.create		= nfs_create,
721da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
731da177e4SLinus Torvalds 	.link		= nfs_link,
741da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
751da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
761da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
771da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
781da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
791da177e4SLinus Torvalds 	.rename		= nfs_rename,
801da177e4SLinus Torvalds 	.permission	= nfs_permission,
811da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
821da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
85d1bacf9eSBryan Schumaker const struct address_space_operations nfs_dir_addr_space_ops = {
86d1bacf9eSBryan Schumaker 	.releasepage = nfs_readdir_clear_array,
87d1bacf9eSBryan Schumaker };
88d1bacf9eSBryan Schumaker 
89b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
9092e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
91b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
92b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
93b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
94b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
95b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
96b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
97b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
98b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
99b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
100b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
101b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
102b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
103b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
104b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
105b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
106b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
107b7fa0554SAndreas Gruenbacher };
108b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
109b7fa0554SAndreas Gruenbacher 
1101da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
113c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
11492e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
115c0204fd2STrond Myklebust 	.create		= nfs_open_create,
1161da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
1171da177e4SLinus Torvalds 	.link		= nfs_link,
1181da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1191da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1201da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1211da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1221da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1231da177e4SLinus Torvalds 	.rename		= nfs_rename,
1241da177e4SLinus Torvalds 	.permission	= nfs_permission,
1251da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1261da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
1276b3b5496SJ. Bruce Fields 	.getxattr       = nfs4_getxattr,
1286b3b5496SJ. Bruce Fields 	.setxattr       = nfs4_setxattr,
1296b3b5496SJ. Bruce Fields 	.listxattr      = nfs4_listxattr,
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * Open file
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds static int
1381da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1391da177e4SLinus Torvalds {
1407451c4f0SCarsten Otte 	int res;
1411da177e4SLinus Torvalds 
1426da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
143cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
144cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
145cc0dd2d1SChuck Lever 
146cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1471e7cb3dcSChuck Lever 
1481da177e4SLinus Torvalds 	/* Call generic open code in order to cache credentials */
1491da177e4SLinus Torvalds 	res = nfs_open(inode, filp);
150f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
151f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
152f5a73672SNeil Brown 		 * have been called, so we need to refresh the
153f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
154f5a73672SNeil Brown 		 */
155f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
156f5a73672SNeil Brown 	}
1571da177e4SLinus Torvalds 	return res;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
161d1bacf9eSBryan Schumaker 	u64 cookie;
162d1bacf9eSBryan Schumaker 	u64 ino;
163d1bacf9eSBryan Schumaker 	struct qstr string;
164d1bacf9eSBryan Schumaker };
165d1bacf9eSBryan Schumaker 
166d1bacf9eSBryan Schumaker struct nfs_cache_array {
167d1bacf9eSBryan Schumaker 	unsigned int size;
168d1bacf9eSBryan Schumaker 	int eof_index;
169d1bacf9eSBryan Schumaker 	u64 last_cookie;
170d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
171d1bacf9eSBryan Schumaker };
172d1bacf9eSBryan Schumaker 
173d1bacf9eSBryan Schumaker #define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
174d1bacf9eSBryan Schumaker 
175babddc72SBryan Schumaker typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1761da177e4SLinus Torvalds typedef struct {
1771da177e4SLinus Torvalds 	struct file	*file;
1781da177e4SLinus Torvalds 	struct page	*page;
1791da177e4SLinus Torvalds 	unsigned long	page_index;
180f0dd2136STrond Myklebust 	u64		*dir_cookie;
181f0dd2136STrond Myklebust 	loff_t		current_index;
1821da177e4SLinus Torvalds 	decode_dirent_t	decode;
183d1bacf9eSBryan Schumaker 
1841f4eab7eSNeil Brown 	unsigned long	timestamp;
1854704f0e2STrond Myklebust 	unsigned long	gencount;
186d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
187d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
188d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1891da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1901da177e4SLinus Torvalds 
191d1bacf9eSBryan Schumaker /*
192d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1931da177e4SLinus Torvalds  */
1941da177e4SLinus Torvalds static
195d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1961da177e4SLinus Torvalds {
197d1bacf9eSBryan Schumaker 	if (page == NULL)
198d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
199d1bacf9eSBryan Schumaker 	return (struct nfs_cache_array *)kmap(page);
200d1bacf9eSBryan Schumaker }
201d1bacf9eSBryan Schumaker 
202d1bacf9eSBryan Schumaker static
203d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
204d1bacf9eSBryan Schumaker {
205d1bacf9eSBryan Schumaker 	kunmap(page);
206d1bacf9eSBryan Schumaker }
207d1bacf9eSBryan Schumaker 
208d1bacf9eSBryan Schumaker /*
209d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
210d1bacf9eSBryan Schumaker  */
211d1bacf9eSBryan Schumaker static
212d1bacf9eSBryan Schumaker int nfs_readdir_clear_array(struct page *page, gfp_t mask)
213d1bacf9eSBryan Schumaker {
214d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
215d1bacf9eSBryan Schumaker 	int i;
216d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
217d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
218d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
219d1bacf9eSBryan Schumaker 	return 0;
220d1bacf9eSBryan Schumaker }
221d1bacf9eSBryan Schumaker 
222d1bacf9eSBryan Schumaker /*
223d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
224d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
225d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
226d1bacf9eSBryan Schumaker  */
227d1bacf9eSBryan Schumaker static
228d1bacf9eSBryan Schumaker void nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
229d1bacf9eSBryan Schumaker {
230d1bacf9eSBryan Schumaker 	string->len = len;
231d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
232d1bacf9eSBryan Schumaker 	string->hash = full_name_hash(string->name, string->len);
233d1bacf9eSBryan Schumaker }
234d1bacf9eSBryan Schumaker 
235d1bacf9eSBryan Schumaker static
236d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
237d1bacf9eSBryan Schumaker {
238d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
239d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
240d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
241d1bacf9eSBryan Schumaker 	if (array->size >= MAX_READDIR_ARRAY) {
242d1bacf9eSBryan Schumaker 		nfs_readdir_release_array(page);
243d1bacf9eSBryan Schumaker 		return -EIO;
244d1bacf9eSBryan Schumaker 	}
245d1bacf9eSBryan Schumaker 
246d1bacf9eSBryan Schumaker 	array->array[array->size].cookie = entry->prev_cookie;
247d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
248d1bacf9eSBryan Schumaker 	array->array[array->size].ino = entry->ino;
249d1bacf9eSBryan Schumaker 	nfs_readdir_make_qstr(&array->array[array->size].string, entry->name, entry->len);
250d1bacf9eSBryan Schumaker 	if (entry->eof == 1)
251d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
252d1bacf9eSBryan Schumaker 	array->size++;
253d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
254d1bacf9eSBryan Schumaker 	return 0;
255d1bacf9eSBryan Schumaker }
256d1bacf9eSBryan Schumaker 
257d1bacf9eSBryan Schumaker static
258d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
259d1bacf9eSBryan Schumaker {
260d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
261d1bacf9eSBryan Schumaker 	unsigned int index;
262d1bacf9eSBryan Schumaker 
263d1bacf9eSBryan Schumaker 	if (diff < 0)
264d1bacf9eSBryan Schumaker 		goto out_eof;
265d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
266d1bacf9eSBryan Schumaker 		if (array->eof_index > 0)
267d1bacf9eSBryan Schumaker 			goto out_eof;
268d1bacf9eSBryan Schumaker 		desc->current_index += array->size;
269d1bacf9eSBryan Schumaker 		return -EAGAIN;
270d1bacf9eSBryan Schumaker 	}
271d1bacf9eSBryan Schumaker 
272d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
273d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
274d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
275d1bacf9eSBryan Schumaker 	if (index == array->eof_index)
276d1bacf9eSBryan Schumaker 		desc->eof = 1;
277d1bacf9eSBryan Schumaker 	return 0;
278d1bacf9eSBryan Schumaker out_eof:
279d1bacf9eSBryan Schumaker 	desc->eof = 1;
280d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
281d1bacf9eSBryan Schumaker }
282d1bacf9eSBryan Schumaker 
283d1bacf9eSBryan Schumaker static
284d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
285d1bacf9eSBryan Schumaker {
286d1bacf9eSBryan Schumaker 	int i;
287d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
288d1bacf9eSBryan Schumaker 
289d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
290d1bacf9eSBryan Schumaker 		if (i == array->eof_index) {
291d1bacf9eSBryan Schumaker 			desc->eof = 1;
292d1bacf9eSBryan Schumaker 			status = -EBADCOOKIE;
293d1bacf9eSBryan Schumaker 		}
294d1bacf9eSBryan Schumaker 		if (array->array[i].cookie == *desc->dir_cookie) {
295d1bacf9eSBryan Schumaker 			desc->cache_entry_index = i;
296d1bacf9eSBryan Schumaker 			status = 0;
297d1bacf9eSBryan Schumaker 			break;
298d1bacf9eSBryan Schumaker 		}
299d1bacf9eSBryan Schumaker 	}
300d1bacf9eSBryan Schumaker 
301d1bacf9eSBryan Schumaker 	return status;
302d1bacf9eSBryan Schumaker }
303d1bacf9eSBryan Schumaker 
304d1bacf9eSBryan Schumaker static
305d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
306d1bacf9eSBryan Schumaker {
307d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
308d1bacf9eSBryan Schumaker 	int status = -EBADCOOKIE;
309d1bacf9eSBryan Schumaker 
310d1bacf9eSBryan Schumaker 	if (desc->dir_cookie == NULL)
311d1bacf9eSBryan Schumaker 		goto out;
312d1bacf9eSBryan Schumaker 
313d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
314d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
315d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
316d1bacf9eSBryan Schumaker 		goto out;
317d1bacf9eSBryan Schumaker 	}
318d1bacf9eSBryan Schumaker 
319d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
320d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
321d1bacf9eSBryan Schumaker 	else
322d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
323d1bacf9eSBryan Schumaker 
324d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
325d1bacf9eSBryan Schumaker out:
326d1bacf9eSBryan Schumaker 	return status;
327d1bacf9eSBryan Schumaker }
328d1bacf9eSBryan Schumaker 
329d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
330d1bacf9eSBryan Schumaker static
33156e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
332d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
333d1bacf9eSBryan Schumaker {
3341da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
3354704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3361da177e4SLinus Torvalds 	int		error;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds  again:
3391da177e4SLinus Torvalds 	timestamp = jiffies;
3404704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
34156e4ebf8SBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
3421da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3431da177e4SLinus Torvalds 	if (error < 0) {
3441da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3451da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3461da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3473a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3481da177e4SLinus Torvalds 			desc->plus = 0;
3491da177e4SLinus Torvalds 			goto again;
3501da177e4SLinus Torvalds 		}
3511da177e4SLinus Torvalds 		goto error;
3521da177e4SLinus Torvalds 	}
3531f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3544704f0e2STrond Myklebust 	desc->gencount = gencount;
355d1bacf9eSBryan Schumaker error:
356d1bacf9eSBryan Schumaker 	return error;
357d1bacf9eSBryan Schumaker }
358d1bacf9eSBryan Schumaker 
359d1bacf9eSBryan Schumaker /* Fill in an entry based on the xdr code stored in desc->page */
360d1bacf9eSBryan Schumaker static
361babddc72SBryan Schumaker int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
362d1bacf9eSBryan Schumaker {
363babddc72SBryan Schumaker 	__be32 *p = desc->decode(stream, entry, desc->plus);
364d1bacf9eSBryan Schumaker 	if (IS_ERR(p))
365d1bacf9eSBryan Schumaker 		return PTR_ERR(p);
366d1bacf9eSBryan Schumaker 
367d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
368d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
369d1bacf9eSBryan Schumaker 	return 0;
370d1bacf9eSBryan Schumaker }
371d1bacf9eSBryan Schumaker 
372d39ab9deSBryan Schumaker static
373d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
374d39ab9deSBryan Schumaker {
375d39ab9deSBryan Schumaker 	struct nfs_inode *node;
376d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
377d39ab9deSBryan Schumaker 		goto different;
378d39ab9deSBryan Schumaker 	node = NFS_I(dentry->d_inode);
379d39ab9deSBryan Schumaker 	if (node->fh.size != entry->fh->size)
380d39ab9deSBryan Schumaker 		goto different;
381d39ab9deSBryan Schumaker 	if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
382d39ab9deSBryan Schumaker 		goto different;
383d39ab9deSBryan Schumaker 	return 1;
384d39ab9deSBryan Schumaker different:
385d39ab9deSBryan Schumaker 	return 0;
386d39ab9deSBryan Schumaker }
387d39ab9deSBryan Schumaker 
388d39ab9deSBryan Schumaker static
389d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
390d39ab9deSBryan Schumaker {
391d39ab9deSBryan Schumaker 	struct qstr filename;
392d39ab9deSBryan Schumaker 	struct dentry *dentry = NULL;
393d39ab9deSBryan Schumaker 	struct dentry *alias = NULL;
394d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
395d39ab9deSBryan Schumaker 	struct inode *inode;
396d39ab9deSBryan Schumaker 
397d39ab9deSBryan Schumaker 	nfs_readdir_make_qstr(&filename, entry->name, entry->len);
398d39ab9deSBryan Schumaker 	if (filename.len == 1 && filename.name[0] == '.')
399d39ab9deSBryan Schumaker 		dentry = dget(parent);
400d39ab9deSBryan Schumaker 	else if (filename.len == 2 && filename.name[0] == '.'
401d39ab9deSBryan Schumaker 				   && filename.name[1] == '.')
402d39ab9deSBryan Schumaker 		dentry = dget_parent(parent);
403d39ab9deSBryan Schumaker 	else
404d39ab9deSBryan Schumaker 		dentry = d_lookup(parent, &filename);
405d39ab9deSBryan Schumaker 
406d39ab9deSBryan Schumaker 	if (dentry != NULL) {
407d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
408d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
409d39ab9deSBryan Schumaker 			goto out;
410d39ab9deSBryan Schumaker 		} else {
411d39ab9deSBryan Schumaker 			d_drop(dentry);
412d39ab9deSBryan Schumaker 			dput(dentry);
413d39ab9deSBryan Schumaker 		}
414d39ab9deSBryan Schumaker 	}
415d39ab9deSBryan Schumaker 
416d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
417d39ab9deSBryan Schumaker 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
418d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
419d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
420d39ab9deSBryan Schumaker 		goto out;
421d39ab9deSBryan Schumaker 
422d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
423d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
424d39ab9deSBryan Schumaker 		goto out;
425d39ab9deSBryan Schumaker 	else if (alias) {
426d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
427d39ab9deSBryan Schumaker 		dput(alias);
428d39ab9deSBryan Schumaker 	} else
429d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
430d39ab9deSBryan Schumaker 
431d39ab9deSBryan Schumaker out:
432d39ab9deSBryan Schumaker 	dput(dentry);
433d39ab9deSBryan Schumaker 	kfree(filename.name);
434d39ab9deSBryan Schumaker 	return;
435d39ab9deSBryan Schumaker }
436d39ab9deSBryan Schumaker 
437d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
438d1bacf9eSBryan Schumaker static
439d1bacf9eSBryan Schumaker void nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
44056e4ebf8SBryan Schumaker 				void *xdr_page, struct page *page, unsigned int buflen)
441d1bacf9eSBryan Schumaker {
442babddc72SBryan Schumaker 	struct xdr_stream stream;
443babddc72SBryan Schumaker 	struct xdr_buf buf;
44456e4ebf8SBryan Schumaker 	__be32 *ptr = xdr_page;
44599424380SBryan Schumaker 	int status;
44699424380SBryan Schumaker 	struct nfs_cache_array *array;
447babddc72SBryan Schumaker 
448babddc72SBryan Schumaker 	buf.head->iov_base = xdr_page;
449babddc72SBryan Schumaker 	buf.head->iov_len = buflen;
450babddc72SBryan Schumaker 	buf.tail->iov_len = 0;
451babddc72SBryan Schumaker 	buf.page_base = 0;
452babddc72SBryan Schumaker 	buf.page_len = 0;
453babddc72SBryan Schumaker 	buf.buflen = buf.head->iov_len;
454babddc72SBryan Schumaker 	buf.len = buf.head->iov_len;
455babddc72SBryan Schumaker 
456babddc72SBryan Schumaker 	xdr_init_decode(&stream, &buf, ptr);
457babddc72SBryan Schumaker 
45899424380SBryan Schumaker 
45999424380SBryan Schumaker 	do {
46099424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
46199424380SBryan Schumaker 		if (status != 0)
46299424380SBryan Schumaker 			break;
46399424380SBryan Schumaker 
464d1bacf9eSBryan Schumaker 		if (nfs_readdir_add_to_array(entry, page) == -1)
465d1bacf9eSBryan Schumaker 			break;
466d39ab9deSBryan Schumaker 		if (desc->plus == 1)
467d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
46899424380SBryan Schumaker 	} while (!entry->eof);
46999424380SBryan Schumaker 
47099424380SBryan Schumaker 	if (status == -EBADCOOKIE && entry->eof) {
47199424380SBryan Schumaker 		array = nfs_readdir_get_array(page);
47299424380SBryan Schumaker 		array->eof_index = array->size - 1;
47399424380SBryan Schumaker 		status = 0;
47499424380SBryan Schumaker 		nfs_readdir_release_array(page);
475d1bacf9eSBryan Schumaker 	}
47656e4ebf8SBryan Schumaker }
47756e4ebf8SBryan Schumaker 
47856e4ebf8SBryan Schumaker static
47956e4ebf8SBryan Schumaker void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
48056e4ebf8SBryan Schumaker {
48156e4ebf8SBryan Schumaker 	unsigned int i;
48256e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
48356e4ebf8SBryan Schumaker 		put_page(pages[i]);
48456e4ebf8SBryan Schumaker }
48556e4ebf8SBryan Schumaker 
48656e4ebf8SBryan Schumaker static
48756e4ebf8SBryan Schumaker void nfs_readdir_free_large_page(void *ptr, struct page **pages,
48856e4ebf8SBryan Schumaker 		unsigned int npages)
48956e4ebf8SBryan Schumaker {
49056e4ebf8SBryan Schumaker 	vm_unmap_ram(ptr, npages);
49156e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, npages);
49256e4ebf8SBryan Schumaker }
49356e4ebf8SBryan Schumaker 
49456e4ebf8SBryan Schumaker /*
49556e4ebf8SBryan Schumaker  * nfs_readdir_large_page will allocate pages that must be freed with a call
49656e4ebf8SBryan Schumaker  * to nfs_readdir_free_large_page
49756e4ebf8SBryan Schumaker  */
49856e4ebf8SBryan Schumaker static
49956e4ebf8SBryan Schumaker void *nfs_readdir_large_page(struct page **pages, unsigned int npages)
50056e4ebf8SBryan Schumaker {
50156e4ebf8SBryan Schumaker 	void *ptr;
50256e4ebf8SBryan Schumaker 	unsigned int i;
50356e4ebf8SBryan Schumaker 
50456e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
50556e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
50656e4ebf8SBryan Schumaker 		if (page == NULL)
50756e4ebf8SBryan Schumaker 			goto out_freepages;
50856e4ebf8SBryan Schumaker 		pages[i] = page;
50956e4ebf8SBryan Schumaker 	}
51056e4ebf8SBryan Schumaker 
51156e4ebf8SBryan Schumaker 	ptr = vm_map_ram(pages, NFS_MAX_READDIR_PAGES, 0, PAGE_KERNEL);
51256e4ebf8SBryan Schumaker 	if (!IS_ERR_OR_NULL(ptr))
51356e4ebf8SBryan Schumaker 		return ptr;
51456e4ebf8SBryan Schumaker out_freepages:
51556e4ebf8SBryan Schumaker 	nfs_readdir_free_pagearray(pages, i);
51656e4ebf8SBryan Schumaker 	return NULL;
517d1bacf9eSBryan Schumaker }
518d1bacf9eSBryan Schumaker 
519d1bacf9eSBryan Schumaker static
520d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
521d1bacf9eSBryan Schumaker {
52256e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
52356e4ebf8SBryan Schumaker 	void *pages_ptr = NULL;
524d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
525d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
526d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
527d1bacf9eSBryan Schumaker 	int status = 0;
52856e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
529d1bacf9eSBryan Schumaker 
530d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
531d1bacf9eSBryan Schumaker 	entry.cookie = *desc->dir_cookie;
532d1bacf9eSBryan Schumaker 	entry.eof = 0;
533d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
534d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
535d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
536d1bacf9eSBryan Schumaker 		goto out;
537d1bacf9eSBryan Schumaker 
538d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
539d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
540d1bacf9eSBryan Schumaker 	array->eof_index = -1;
541d1bacf9eSBryan Schumaker 
54256e4ebf8SBryan Schumaker 	pages_ptr = nfs_readdir_large_page(pages, array_size);
54356e4ebf8SBryan Schumaker 	if (!pages_ptr)
544d1bacf9eSBryan Schumaker 		goto out_release_array;
545d1bacf9eSBryan Schumaker 	do {
54656e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
547babddc72SBryan Schumaker 
548d1bacf9eSBryan Schumaker 		if (status < 0)
549d1bacf9eSBryan Schumaker 			break;
55056e4ebf8SBryan Schumaker 		nfs_readdir_page_filler(desc, &entry, pages_ptr, page, array_size * PAGE_SIZE);
551d1bacf9eSBryan Schumaker 	} while (array->eof_index < 0 && array->size < MAX_READDIR_ARRAY);
552d1bacf9eSBryan Schumaker 
55356e4ebf8SBryan Schumaker 	nfs_readdir_free_large_page(pages_ptr, pages, array_size);
554d1bacf9eSBryan Schumaker out_release_array:
555d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
556d1bacf9eSBryan Schumaker out:
557d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
558d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
559d1bacf9eSBryan Schumaker 	return status;
560d1bacf9eSBryan Schumaker }
561d1bacf9eSBryan Schumaker 
562d1bacf9eSBryan Schumaker /*
563d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
564d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
565d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
566d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
5671da177e4SLinus Torvalds  */
568d1bacf9eSBryan Schumaker static
569d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
570d1bacf9eSBryan Schumaker {
571d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
572d1bacf9eSBryan Schumaker 
5733c8a1aeeSBryan Schumaker 	if (nfs_readdir_xdr_to_array(desc, page, inode) < 0)
574d1bacf9eSBryan Schumaker 		goto error;
575d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
576d1bacf9eSBryan Schumaker 
5772aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
578cd9ae2b6STrond Myklebust 		/* Should never happen */
579cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
580cd9ae2b6STrond Myklebust 	}
5811da177e4SLinus Torvalds 	unlock_page(page);
5821da177e4SLinus Torvalds 	return 0;
5831da177e4SLinus Torvalds  error:
5841da177e4SLinus Torvalds 	unlock_page(page);
5851da177e4SLinus Torvalds 	return -EIO;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
588d1bacf9eSBryan Schumaker static
589d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	page_cache_release(desc->page);
5921da177e4SLinus Torvalds 	desc->page = NULL;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
595d1bacf9eSBryan Schumaker static
596d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	struct page *page;
599d1bacf9eSBryan Schumaker 	page = read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
600d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
601d1bacf9eSBryan Schumaker 	if (IS_ERR(page))
602d1bacf9eSBryan Schumaker 		desc->eof = 1;
603d1bacf9eSBryan Schumaker 	return page;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds /*
607d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
6081da177e4SLinus Torvalds  */
609d1bacf9eSBryan Schumaker static
610d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
611d1bacf9eSBryan Schumaker {
612d1bacf9eSBryan Schumaker 	int res;
613d1bacf9eSBryan Schumaker 
614d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
615d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
616d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
617d1bacf9eSBryan Schumaker 
618d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
619d1bacf9eSBryan Schumaker 	if (res == 0)
620d1bacf9eSBryan Schumaker 		return 0;
621d1bacf9eSBryan Schumaker 	cache_page_release(desc);
622d1bacf9eSBryan Schumaker 	return res;
623d1bacf9eSBryan Schumaker }
624d1bacf9eSBryan Schumaker 
625d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
6261da177e4SLinus Torvalds static inline
6271da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
6281da177e4SLinus Torvalds {
629d1bacf9eSBryan Schumaker 	int res = -EAGAIN;
630d1bacf9eSBryan Schumaker 
631d1bacf9eSBryan Schumaker 	while (1) {
632d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
6331da177e4SLinus Torvalds 		if (res != -EAGAIN)
6341da177e4SLinus Torvalds 			break;
6351da177e4SLinus Torvalds 		desc->page_index++;
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 	return res;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds /*
6461da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds static
6491da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
6501da177e4SLinus Torvalds 		   filldir_t filldir)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	struct file	*file = desc->file;
653d1bacf9eSBryan Schumaker 	int i = 0;
654d1bacf9eSBryan Schumaker 	int res = 0;
655d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
656d1bacf9eSBryan Schumaker 	unsigned int d_type = DT_UNKNOWN;
6571da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
6581da177e4SLinus Torvalds 
659d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
6601da177e4SLinus Torvalds 
661d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
662d1bacf9eSBryan Schumaker 		d_type = DT_UNKNOWN;
6631da177e4SLinus Torvalds 
664d1bacf9eSBryan Schumaker 		res = filldir(dirent, array->array[i].string.name,
665d1bacf9eSBryan Schumaker 			array->array[i].string.len, file->f_pos,
666d1bacf9eSBryan Schumaker 			nfs_compat_user_ino64(array->array[i].ino), d_type);
6671da177e4SLinus Torvalds 		if (res < 0)
6681da177e4SLinus Torvalds 			break;
66900a92642SOlivier Galibert 		file->f_pos++;
670d1bacf9eSBryan Schumaker 		desc->cache_entry_index = i;
671d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
672d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
673d1bacf9eSBryan Schumaker 		else
674d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
675d1bacf9eSBryan Schumaker 		if (i == array->eof_index) {
676d1bacf9eSBryan Schumaker 			desc->eof = 1;
6771da177e4SLinus Torvalds 			break;
6781da177e4SLinus Torvalds 		}
6791da177e4SLinus Torvalds 	}
680d1bacf9eSBryan Schumaker 
681d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
682d1bacf9eSBryan Schumaker 	cache_page_release(desc);
6831da177e4SLinus Torvalds 	if (dentry != NULL)
6841da177e4SLinus Torvalds 		dput(dentry);
6851e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
6861e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
6871da177e4SLinus Torvalds 	return res;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds /*
6911da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
6921da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
6931da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
6941da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
6951da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
6981da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
6991da177e4SLinus Torvalds  *	 we should already have a complete representation of the
7001da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
7011da177e4SLinus Torvalds  */
7021da177e4SLinus Torvalds static inline
7031da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
7041da177e4SLinus Torvalds 		     filldir_t filldir)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	struct page	*page = NULL;
7071da177e4SLinus Torvalds 	int		status;
708d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
7091da177e4SLinus Torvalds 
7101e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
7111e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
7141da177e4SLinus Torvalds 	if (!page) {
7151da177e4SLinus Torvalds 		status = -ENOMEM;
7161da177e4SLinus Torvalds 		goto out;
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
719d1bacf9eSBryan Schumaker 	if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
720d1bacf9eSBryan Schumaker 		status = -EIO;
721d1bacf9eSBryan Schumaker 		goto out_release;
722d1bacf9eSBryan Schumaker 	}
723d1bacf9eSBryan Schumaker 
724baf57a09STrond Myklebust 	desc->page_index = 0;
725d1bacf9eSBryan Schumaker 	desc->page = page;
7261da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds  out:
7291e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
7303110ff80SHarvey Harrison 			__func__, status);
7311da177e4SLinus Torvalds 	return status;
7321da177e4SLinus Torvalds  out_release:
733d1bacf9eSBryan Schumaker 	cache_page_release(desc);
7341da177e4SLinus Torvalds 	goto out;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
73700a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
73800a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
73900a92642SOlivier Galibert    whole directory.
7401da177e4SLinus Torvalds  */
7411da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
7421da177e4SLinus Torvalds {
74301cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
7441da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
7451da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
7461da177e4SLinus Torvalds 			*desc = &my_desc;
747aa49b4cfSTrond Myklebust 	int res = -ENOMEM;
7481da177e4SLinus Torvalds 
7496da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
7501e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
7511e7cb3dcSChuck Lever 			(long long)filp->f_pos);
75291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
75391d5b470SChuck Lever 
7541da177e4SLinus Torvalds 	/*
75500a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
756f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
75700a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
75800a92642SOlivier Galibert 	 * revalidate the cookie.
7591da177e4SLinus Torvalds 	 */
7601da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	desc->file = filp;
763cd3758e3STrond Myklebust 	desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
7641da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
7651da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
7661da177e4SLinus Torvalds 
767565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
7681cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
769fccca7fcSTrond Myklebust 	if (res < 0)
770fccca7fcSTrond Myklebust 		goto out;
771fccca7fcSTrond Myklebust 
772d1bacf9eSBryan Schumaker 	while (desc->eof != 1) {
7731da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
77400a92642SOlivier Galibert 
7751da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
7761da177e4SLinus Torvalds 			/* This means either end of directory */
777d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
7781da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
7791da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
7801da177e4SLinus Torvalds 				if (res >= 0)
7811da177e4SLinus Torvalds 					continue;
7821da177e4SLinus Torvalds 			}
7831da177e4SLinus Torvalds 			res = 0;
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 		}
7861da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
7873a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
7881da177e4SLinus Torvalds 			nfs_zap_caches(inode);
789baf57a09STrond Myklebust 			desc->page_index = 0;
7901da177e4SLinus Torvalds 			desc->plus = 0;
791d1bacf9eSBryan Schumaker 			desc->eof = 0;
7921da177e4SLinus Torvalds 			continue;
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 		if (res < 0)
7951da177e4SLinus Torvalds 			break;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
7981da177e4SLinus Torvalds 		if (res < 0) {
7991da177e4SLinus Torvalds 			res = 0;
8001da177e4SLinus Torvalds 			break;
8011da177e4SLinus Torvalds 		}
8021da177e4SLinus Torvalds 	}
803fccca7fcSTrond Myklebust out:
804565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
8051e7cb3dcSChuck Lever 	if (res > 0)
8061e7cb3dcSChuck Lever 		res = 0;
807aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
8081e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8091e7cb3dcSChuck Lever 			res);
8101da177e4SLinus Torvalds 	return res;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
81310afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
814f0dd2136STrond Myklebust {
815b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
816b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
817b84e06c5SChuck Lever 
8186da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
819b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
820b84e06c5SChuck Lever 			dentry->d_name.name,
821b84e06c5SChuck Lever 			offset, origin);
822b84e06c5SChuck Lever 
823b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
824f0dd2136STrond Myklebust 	switch (origin) {
825f0dd2136STrond Myklebust 		case 1:
826f0dd2136STrond Myklebust 			offset += filp->f_pos;
827f0dd2136STrond Myklebust 		case 0:
828f0dd2136STrond Myklebust 			if (offset >= 0)
829f0dd2136STrond Myklebust 				break;
830f0dd2136STrond Myklebust 		default:
831f0dd2136STrond Myklebust 			offset = -EINVAL;
832f0dd2136STrond Myklebust 			goto out;
833f0dd2136STrond Myklebust 	}
834f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
835f0dd2136STrond Myklebust 		filp->f_pos = offset;
836cd3758e3STrond Myklebust 		nfs_file_open_context(filp)->dir_cookie = 0;
837f0dd2136STrond Myklebust 	}
838f0dd2136STrond Myklebust out:
839b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
840f0dd2136STrond Myklebust 	return offset;
841f0dd2136STrond Myklebust }
842f0dd2136STrond Myklebust 
8431da177e4SLinus Torvalds /*
8441da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
8451da177e4SLinus Torvalds  * is a dummy operation.
8461da177e4SLinus Torvalds  */
8477ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync)
8481da177e4SLinus Torvalds {
8497ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
8507ea80859SChristoph Hellwig 
8516da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
8521e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
8531e7cb3dcSChuck Lever 			datasync);
8541e7cb3dcSChuck Lever 
85554917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
8561da177e4SLinus Torvalds 	return 0;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
859bfc69a45STrond Myklebust /**
860bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
861bfc69a45STrond Myklebust  * @dir - pointer to directory inode
862bfc69a45STrond Myklebust  *
863bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
864bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
865bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
866bfc69a45STrond Myklebust  *
867bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
868bfc69a45STrond Myklebust  */
869bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
870bfc69a45STrond Myklebust {
871011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
872bfc69a45STrond Myklebust }
873bfc69a45STrond Myklebust 
8741da177e4SLinus Torvalds /*
8751da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
8761da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
8771da177e4SLinus Torvalds  * and may need to be looked up again.
8781da177e4SLinus Torvalds  */
879c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
8821da177e4SLinus Torvalds 		return 1;
8834eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
8844eec952eSTrond Myklebust 		return 0;
885f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
8866ecc5e8fSTrond Myklebust 		return 0;
887f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
888f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
889f2c77f4eSTrond Myklebust 		return 0;
890f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
891f2c77f4eSTrond Myklebust 		return 0;
892f2c77f4eSTrond Myklebust 	return 1;
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds /*
8961d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
8971d6757fbSTrond Myklebust  *
8981d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
8991d6757fbSTrond Myklebust  * component of the path.
9001d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
9011d6757fbSTrond Myklebust  */
9021d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
9031d6757fbSTrond Myklebust {
9041d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
9051d6757fbSTrond Myklebust 		return 0;
9061d6757fbSTrond Myklebust 	return nd->flags & mask;
9071d6757fbSTrond Myklebust }
9081d6757fbSTrond Myklebust 
9091d6757fbSTrond Myklebust /*
910a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
911a12802caSTrond Myklebust  * an O_EXCL create using this path component.
912a12802caSTrond Myklebust  */
913a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
914a12802caSTrond Myklebust {
915a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
916a12802caSTrond Myklebust 		return 0;
9173516586aSAl Viro 	return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
918a12802caSTrond Myklebust }
919a12802caSTrond Myklebust 
920a12802caSTrond Myklebust /*
9211d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
9221d6757fbSTrond Myklebust  *
9231d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
9241d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
9251d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
9261d6757fbSTrond Myklebust  *
9271d6757fbSTrond Myklebust  */
9281da177e4SLinus Torvalds static inline
9291da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
9301da177e4SLinus Torvalds {
9311da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
9321da177e4SLinus Torvalds 
9334e99a1ffSTrond Myklebust 	if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
9344e99a1ffSTrond Myklebust 		return 0;
9351da177e4SLinus Torvalds 	if (nd != NULL) {
9361da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
9371d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
9381da177e4SLinus Torvalds 			goto out_force;
9391da177e4SLinus Torvalds 		/* This is an open(2) */
9401d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
9414e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
9424e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
9434e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
9441da177e4SLinus Torvalds 			goto out_force;
9454f48af45STrond Myklebust 		return 0;
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
9481da177e4SLinus Torvalds out_force:
9491da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds /*
9531da177e4SLinus Torvalds  * We judge how long we want to trust negative
9541da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
9551da177e4SLinus Torvalds  *
9561da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
9571da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
9581da177e4SLinus Torvalds  */
9591da177e4SLinus Torvalds static inline
9601da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
9611da177e4SLinus Torvalds 		       struct nameidata *nd)
9621da177e4SLinus Torvalds {
9631da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
9641d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
9651da177e4SLinus Torvalds 		return 0;
9664eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
9674eec952eSTrond Myklebust 		return 1;
9681da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds /*
9721da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
9731da177e4SLinus Torvalds  * and we should check whether we can really trust that
9741da177e4SLinus Torvalds  * lookup.
9751da177e4SLinus Torvalds  *
9761da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
9771da177e4SLinus Torvalds  * we have an inode!
9781da177e4SLinus Torvalds  *
9791da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
9801da177e4SLinus Torvalds  * cached dentry and do a new lookup.
9811da177e4SLinus Torvalds  */
9821da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	struct inode *dir;
9851da177e4SLinus Torvalds 	struct inode *inode;
9861da177e4SLinus Torvalds 	struct dentry *parent;
987e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
988e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
9891da177e4SLinus Torvalds 	int error;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	parent = dget_parent(dentry);
9921da177e4SLinus Torvalds 	dir = parent->d_inode;
99391d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
9941da177e4SLinus Torvalds 	inode = dentry->d_inode;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (!inode) {
9971da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
9981da177e4SLinus Torvalds 			goto out_bad;
9991da177e4SLinus Torvalds 		goto out_valid;
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
10031e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
10043110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
10051e7cb3dcSChuck Lever 				dentry->d_name.name);
10061da177e4SLinus Torvalds 		goto out_bad;
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
100915860ab1STrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
101015860ab1STrond Myklebust 		goto out_set_verifier;
101115860ab1STrond Myklebust 
10121da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
1013a12802caSTrond Myklebust 	if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
10141da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
10151da177e4SLinus Torvalds 			goto out_zap_parent;
10161da177e4SLinus Torvalds 		goto out_valid;
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (NFS_STALE(inode))
10201da177e4SLinus Torvalds 		goto out_bad;
10211da177e4SLinus Torvalds 
1022e1fb4d05STrond Myklebust 	error = -ENOMEM;
1023e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1024e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1025e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1026e1fb4d05STrond Myklebust 		goto out_error;
1027e1fb4d05STrond Myklebust 
1028e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
10291da177e4SLinus Torvalds 	if (error)
10301da177e4SLinus Torvalds 		goto out_bad;
1031e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
10321da177e4SLinus Torvalds 		goto out_bad;
1033e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
10341da177e4SLinus Torvalds 		goto out_bad;
10351da177e4SLinus Torvalds 
1036e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1037e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
103815860ab1STrond Myklebust out_set_verifier:
1039cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10401da177e4SLinus Torvalds  out_valid:
10411da177e4SLinus Torvalds 	dput(parent);
10421e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
10433110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
10441e7cb3dcSChuck Lever 			dentry->d_name.name);
10451da177e4SLinus Torvalds 	return 1;
10461da177e4SLinus Torvalds out_zap_parent:
10471da177e4SLinus Torvalds 	nfs_zap_caches(dir);
10481da177e4SLinus Torvalds  out_bad:
1049a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
10501da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
10511da177e4SLinus Torvalds 		/* Purge readdir caches. */
10521da177e4SLinus Torvalds 		nfs_zap_caches(inode);
10531da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
10541da177e4SLinus Torvalds 		if (have_submounts(dentry))
10551da177e4SLinus Torvalds 			goto out_valid;
1056d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1057d9e80b7dSAl Viro 			goto out_valid;
10581da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
10591da177e4SLinus Torvalds 	}
10601da177e4SLinus Torvalds 	d_drop(dentry);
1061e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1062e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
10631da177e4SLinus Torvalds 	dput(parent);
10641e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
10653110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
10661e7cb3dcSChuck Lever 			dentry->d_name.name);
10671da177e4SLinus Torvalds 	return 0;
1068e1fb4d05STrond Myklebust out_error:
1069e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1070e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1071e1fb4d05STrond Myklebust 	dput(parent);
1072e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1073e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1074e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1075e1fb4d05STrond Myklebust 	return error;
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds /*
10791da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
10801da177e4SLinus Torvalds  */
10811da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
10841da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
10851da177e4SLinus Torvalds 		dentry->d_flags);
10861da177e4SLinus Torvalds 
108777f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
108877f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
108977f11192STrond Myklebust 		return 1;
109077f11192STrond Myklebust 
10911da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
10921da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
10931da177e4SLinus Torvalds 		return 1;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
10961da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
10971da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
10981da177e4SLinus Torvalds 		return 1;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 	return 0;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
11051b83d707STrond Myklebust {
11061b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
11071b83d707STrond Myklebust 	if (inode->i_nlink > 0)
11081b83d707STrond Myklebust 		drop_nlink(inode);
11091b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
11101b83d707STrond Myklebust }
11111b83d707STrond Myklebust 
11121da177e4SLinus Torvalds /*
11131da177e4SLinus Torvalds  * Called when the dentry loses inode.
11141da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
11151da177e4SLinus Torvalds  */
11161da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
11171da177e4SLinus Torvalds {
111883672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
111983672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
112083672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
112183672d39SNeil Brown 
11221da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
11239a53c3a7SDave Hansen 		drop_nlink(inode);
1124e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 	iput(inode);
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
1129f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
11301da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
11311da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
11321da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
11331da177e4SLinus Torvalds };
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	struct dentry *res;
1138565277f6STrond Myklebust 	struct dentry *parent;
11391da177e4SLinus Torvalds 	struct inode *inode = NULL;
1140e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1141e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
11421da177e4SLinus Torvalds 	int error;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
11451da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
114691d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
11491da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
11501da177e4SLinus Torvalds 		goto out;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
11531da177e4SLinus Torvalds 
1154fd684071STrond Myklebust 	/*
1155fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1156fd684071STrond Myklebust 	 * but don't hash the dentry.
1157fd684071STrond Myklebust 	 */
1158fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
1159fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1160fd684071STrond Myklebust 		res = NULL;
1161fc0f684cSTrond Myklebust 		goto out;
1162fd684071STrond Myklebust 	}
11631da177e4SLinus Torvalds 
1164e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1165e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1166e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1167e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1168e1fb4d05STrond Myklebust 		goto out;
1169e1fb4d05STrond Myklebust 
1170565277f6STrond Myklebust 	parent = dentry->d_parent;
1171565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1172565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
1173e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
11741da177e4SLinus Torvalds 	if (error == -ENOENT)
11751da177e4SLinus Torvalds 		goto no_entry;
11761da177e4SLinus Torvalds 	if (error < 0) {
11771da177e4SLinus Torvalds 		res = ERR_PTR(error);
1178565277f6STrond Myklebust 		goto out_unblock_sillyrename;
11791da177e4SLinus Torvalds 	}
1180e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
118103f28e3aSTrond Myklebust 	res = (struct dentry *)inode;
118203f28e3aSTrond Myklebust 	if (IS_ERR(res))
1183565277f6STrond Myklebust 		goto out_unblock_sillyrename;
118454ceac45SDavid Howells 
11851da177e4SLinus Torvalds no_entry:
118654ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
11879eaef27bSTrond Myklebust 	if (res != NULL) {
11889eaef27bSTrond Myklebust 		if (IS_ERR(res))
1189565277f6STrond Myklebust 			goto out_unblock_sillyrename;
11901da177e4SLinus Torvalds 		dentry = res;
11919eaef27bSTrond Myklebust 	}
11921da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1193565277f6STrond Myklebust out_unblock_sillyrename:
1194565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
11951da177e4SLinus Torvalds out:
1196e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1197e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
11981da177e4SLinus Torvalds 	return res;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
12021da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
12031da177e4SLinus Torvalds 
1204f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
12051da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
12061da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
12071da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
12081da177e4SLinus Torvalds };
12091da177e4SLinus Torvalds 
12101d6757fbSTrond Myklebust /*
12111d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
12121d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
12131d6757fbSTrond Myklebust  */
12145584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd)
12151da177e4SLinus Torvalds {
12161d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
12171da177e4SLinus Torvalds 		return 0;
12181da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
12191da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
12201da177e4SLinus Torvalds 		return 0;
12211da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
12222c463e95SDave Hansen 	if (__mnt_is_readonly(nd->path.mnt) &&
12232c463e95SDave Hansen 	    (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
12241da177e4SLinus Torvalds 		return 0;
12251da177e4SLinus Torvalds 	return 1;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
1228cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1229cd9a1c0eSTrond Myklebust {
1230cd9a1c0eSTrond Myklebust 	struct path path = {
1231cd9a1c0eSTrond Myklebust 		.mnt = nd->path.mnt,
1232cd9a1c0eSTrond Myklebust 		.dentry = dentry,
1233cd9a1c0eSTrond Myklebust 	};
1234cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1235cd9a1c0eSTrond Myklebust 	struct rpc_cred *cred;
1236cd9a1c0eSTrond Myklebust 	fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1237cd9a1c0eSTrond Myklebust 
1238cd9a1c0eSTrond Myklebust 	cred = rpc_lookup_cred();
1239cd9a1c0eSTrond Myklebust 	if (IS_ERR(cred))
1240cd9a1c0eSTrond Myklebust 		return ERR_CAST(cred);
1241cd9a1c0eSTrond Myklebust 	ctx = alloc_nfs_open_context(&path, cred, fmode);
1242cd9a1c0eSTrond Myklebust 	put_rpccred(cred);
1243cd9a1c0eSTrond Myklebust 	if (ctx == NULL)
1244cd9a1c0eSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1245cd9a1c0eSTrond Myklebust 	return ctx;
1246cd9a1c0eSTrond Myklebust }
1247cd9a1c0eSTrond Myklebust 
1248cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1249cd9a1c0eSTrond Myklebust {
1250cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1251cd9a1c0eSTrond Myklebust 	return 0;
1252cd9a1c0eSTrond Myklebust }
1253cd9a1c0eSTrond Myklebust 
1254cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1255cd9a1c0eSTrond Myklebust {
1256cd9a1c0eSTrond Myklebust 	struct file *filp;
1257cd9a1c0eSTrond Myklebust 	int ret = 0;
1258cd9a1c0eSTrond Myklebust 
1259cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1260cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
1261cd9a1c0eSTrond Myklebust 		ret = nfs_may_open(ctx->path.dentry->d_inode,
1262cd9a1c0eSTrond Myklebust 				ctx->cred,
1263cd9a1c0eSTrond Myklebust 				nd->intent.open.flags);
1264cd9a1c0eSTrond Myklebust 		if (ret < 0)
1265cd9a1c0eSTrond Myklebust 			goto out;
1266cd9a1c0eSTrond Myklebust 	}
1267cd9a1c0eSTrond Myklebust 	filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1268cd9a1c0eSTrond Myklebust 	if (IS_ERR(filp))
1269cd9a1c0eSTrond Myklebust 		ret = PTR_ERR(filp);
1270cd9a1c0eSTrond Myklebust 	else
1271cd9a1c0eSTrond Myklebust 		nfs_file_set_open_context(filp, ctx);
1272cd9a1c0eSTrond Myklebust out:
1273cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1274cd9a1c0eSTrond Myklebust 	return ret;
1275cd9a1c0eSTrond Myklebust }
1276cd9a1c0eSTrond Myklebust 
12771da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
12781da177e4SLinus Torvalds {
1279cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1280cd9a1c0eSTrond Myklebust 	struct iattr attr;
12811da177e4SLinus Torvalds 	struct dentry *res = NULL;
1282f46e0bd3STrond Myklebust 	struct inode *inode;
1283cd9a1c0eSTrond Myklebust 	int open_flags;
1284898f635cSTrond Myklebust 	int err;
12851da177e4SLinus Torvalds 
12861e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
12871e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12881e7cb3dcSChuck Lever 
12891da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
12905584c306STrond Myklebust 	if (!is_atomic_open(nd))
12911da177e4SLinus Torvalds 		goto no_open;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
12941da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
12951da177e4SLinus Torvalds 		goto out;
12961da177e4SLinus Torvalds 	}
12971da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
12981da177e4SLinus Torvalds 
1299d4d9cdcbSTrond Myklebust 	/* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1300d4d9cdcbSTrond Myklebust 	 * the dentry. */
13013516586aSAl Viro 	if (nd->flags & LOOKUP_EXCL) {
1302d4d9cdcbSTrond Myklebust 		d_instantiate(dentry, NULL);
130302a913a7STrond Myklebust 		goto out;
130402a913a7STrond Myklebust 	}
13051da177e4SLinus Torvalds 
1306cd9a1c0eSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1307cd9a1c0eSTrond Myklebust 	res = ERR_CAST(ctx);
1308cd9a1c0eSTrond Myklebust 	if (IS_ERR(ctx))
1309cd9a1c0eSTrond Myklebust 		goto out;
1310cd9a1c0eSTrond Myklebust 
1311cd9a1c0eSTrond Myklebust 	open_flags = nd->intent.open.flags;
1312cd9a1c0eSTrond Myklebust 	if (nd->flags & LOOKUP_CREATE) {
1313cd9a1c0eSTrond Myklebust 		attr.ia_mode = nd->intent.open.create_mode;
1314cd9a1c0eSTrond Myklebust 		attr.ia_valid = ATTR_MODE;
1315cd9a1c0eSTrond Myklebust 		if (!IS_POSIXACL(dir))
1316cd9a1c0eSTrond Myklebust 			attr.ia_mode &= ~current_umask();
1317cd9a1c0eSTrond Myklebust 	} else {
1318898f635cSTrond Myklebust 		open_flags &= ~(O_EXCL | O_CREAT);
1319cd9a1c0eSTrond Myklebust 		attr.ia_valid = 0;
1320cd9a1c0eSTrond Myklebust 	}
1321cd9a1c0eSTrond Myklebust 
13221da177e4SLinus Torvalds 	/* Open the file on the server */
1323f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
13242b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1325f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1326f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1327cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
1328f46e0bd3STrond Myklebust 		switch (PTR_ERR(inode)) {
13291da177e4SLinus Torvalds 			/* Make a negative dentry */
13301da177e4SLinus Torvalds 			case -ENOENT:
1331f46e0bd3STrond Myklebust 				d_add(dentry, NULL);
133202a913a7STrond Myklebust 				res = NULL;
133302a913a7STrond Myklebust 				goto out;
13341da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
133580e60639STrond Myklebust 			case -EISDIR:
13366f926b5bSTrond Myklebust 			case -ENOTDIR:
13376f926b5bSTrond Myklebust 				goto no_open;
13381da177e4SLinus Torvalds 			case -ELOOP:
13391da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
13401da177e4SLinus Torvalds 					goto no_open;
13411da177e4SLinus Torvalds 			/* case -EINVAL: */
13421da177e4SLinus Torvalds 			default:
1343f46e0bd3STrond Myklebust 				res = ERR_CAST(inode);
13441da177e4SLinus Torvalds 				goto out;
13451da177e4SLinus Torvalds 		}
1346cd9a1c0eSTrond Myklebust 	}
1347f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1348898f635cSTrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1349f46e0bd3STrond Myklebust 	if (res != NULL) {
1350f46e0bd3STrond Myklebust 		dput(ctx->path.dentry);
1351f46e0bd3STrond Myklebust 		ctx->path.dentry = dget(res);
13521da177e4SLinus Torvalds 		dentry = res;
1353f46e0bd3STrond Myklebust 	}
1354898f635cSTrond Myklebust 	err = nfs_intent_set_file(nd, ctx);
1355898f635cSTrond Myklebust 	if (err < 0) {
1356898f635cSTrond Myklebust 		if (res != NULL)
1357898f635cSTrond Myklebust 			dput(res);
1358898f635cSTrond Myklebust 		return ERR_PTR(err);
1359898f635cSTrond Myklebust 	}
13601da177e4SLinus Torvalds out:
1361f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
13621da177e4SLinus Torvalds 	return res;
13631da177e4SLinus Torvalds no_open:
13641da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	struct dentry *parent = NULL;
13701da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
13711da177e4SLinus Torvalds 	struct inode *dir;
1372b8d4caddSTrond Myklebust 	struct nfs_open_context *ctx;
13731da177e4SLinus Torvalds 	int openflags, ret = 0;
13741da177e4SLinus Torvalds 
13751f063d2cSTrond Myklebust 	if (!is_atomic_open(nd) || d_mountpoint(dentry))
13765584c306STrond Myklebust 		goto no_open;
13772b484297STrond Myklebust 
13781da177e4SLinus Torvalds 	parent = dget_parent(dentry);
13791da177e4SLinus Torvalds 	dir = parent->d_inode;
13802b484297STrond Myklebust 
13811da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
13821da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
13831da177e4SLinus Torvalds 	 */
1384216d5d06STrond Myklebust 	if (inode == NULL) {
1385216d5d06STrond Myklebust 		if (!nfs_neg_need_reval(dir, dentry, nd))
1386216d5d06STrond Myklebust 			ret = 1;
13871da177e4SLinus Torvalds 		goto out;
1388216d5d06STrond Myklebust 	}
1389216d5d06STrond Myklebust 
13901da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
13911da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
13925584c306STrond Myklebust 		goto no_open_dput;
13931da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
13941da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
13951da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
13965584c306STrond Myklebust 		goto no_open_dput;
13971da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
13980a377cffSTrond Myklebust 	openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
13991da177e4SLinus Torvalds 
1400b8d4caddSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1401b8d4caddSTrond Myklebust 	ret = PTR_ERR(ctx);
1402b8d4caddSTrond Myklebust 	if (IS_ERR(ctx))
1403b8d4caddSTrond Myklebust 		goto out;
14041da177e4SLinus Torvalds 	/*
14051b1dcc1bSJes Sorensen 	 * Note: we're not holding inode->i_mutex and so may be racing with
14061da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
14071da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
14081da177e4SLinus Torvalds 	 */
14092b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
1410535918f1STrond Myklebust 	if (IS_ERR(inode)) {
1411535918f1STrond Myklebust 		ret = PTR_ERR(inode);
1412535918f1STrond Myklebust 		switch (ret) {
1413535918f1STrond Myklebust 		case -EPERM:
1414535918f1STrond Myklebust 		case -EACCES:
1415535918f1STrond Myklebust 		case -EDQUOT:
1416535918f1STrond Myklebust 		case -ENOSPC:
1417535918f1STrond Myklebust 		case -EROFS:
1418535918f1STrond Myklebust 			goto out_put_ctx;
1419535918f1STrond Myklebust 		default:
1420535918f1STrond Myklebust 			goto out_drop;
1421535918f1STrond Myklebust 		}
1422535918f1STrond Myklebust 	}
1423535918f1STrond Myklebust 	iput(inode);
1424898f635cSTrond Myklebust 	if (inode != dentry->d_inode)
1425535918f1STrond Myklebust 		goto out_drop;
1426898f635cSTrond Myklebust 
1427898f635cSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1428898f635cSTrond Myklebust 	ret = nfs_intent_set_file(nd, ctx);
1429898f635cSTrond Myklebust 	if (ret >= 0)
1430898f635cSTrond Myklebust 		ret = 1;
14311da177e4SLinus Torvalds out:
14321da177e4SLinus Torvalds 	dput(parent);
14331da177e4SLinus Torvalds 	return ret;
1434535918f1STrond Myklebust out_drop:
1435535918f1STrond Myklebust 	d_drop(dentry);
1436535918f1STrond Myklebust 	ret = 0;
1437535918f1STrond Myklebust out_put_ctx:
1438535918f1STrond Myklebust 	put_nfs_open_context(ctx);
1439535918f1STrond Myklebust 	goto out;
1440535918f1STrond Myklebust 
14415584c306STrond Myklebust no_open_dput:
14421da177e4SLinus Torvalds 	dput(parent);
14435584c306STrond Myklebust no_open:
14441da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
14451da177e4SLinus Torvalds }
1446c0204fd2STrond Myklebust 
1447c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1448c0204fd2STrond Myklebust 		struct nameidata *nd)
1449c0204fd2STrond Myklebust {
1450c0204fd2STrond Myklebust 	struct nfs_open_context *ctx = NULL;
1451c0204fd2STrond Myklebust 	struct iattr attr;
1452c0204fd2STrond Myklebust 	int error;
1453c0204fd2STrond Myklebust 	int open_flags = 0;
1454c0204fd2STrond Myklebust 
1455c0204fd2STrond Myklebust 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1456c0204fd2STrond Myklebust 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1457c0204fd2STrond Myklebust 
1458c0204fd2STrond Myklebust 	attr.ia_mode = mode;
1459c0204fd2STrond Myklebust 	attr.ia_valid = ATTR_MODE;
1460c0204fd2STrond Myklebust 
1461c0204fd2STrond Myklebust 	if ((nd->flags & LOOKUP_CREATE) != 0) {
1462c0204fd2STrond Myklebust 		open_flags = nd->intent.open.flags;
1463c0204fd2STrond Myklebust 
1464c0204fd2STrond Myklebust 		ctx = nameidata_to_nfs_open_context(dentry, nd);
1465c0204fd2STrond Myklebust 		error = PTR_ERR(ctx);
1466c0204fd2STrond Myklebust 		if (IS_ERR(ctx))
1467898f635cSTrond Myklebust 			goto out_err_drop;
1468c0204fd2STrond Myklebust 	}
1469c0204fd2STrond Myklebust 
1470c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1471c0204fd2STrond Myklebust 	if (error != 0)
1472c0204fd2STrond Myklebust 		goto out_put_ctx;
1473898f635cSTrond Myklebust 	if (ctx != NULL) {
1474898f635cSTrond Myklebust 		error = nfs_intent_set_file(nd, ctx);
1475898f635cSTrond Myklebust 		if (error < 0)
1476898f635cSTrond Myklebust 			goto out_err;
1477898f635cSTrond Myklebust 	}
1478c0204fd2STrond Myklebust 	return 0;
1479c0204fd2STrond Myklebust out_put_ctx:
1480c0204fd2STrond Myklebust 	if (ctx != NULL)
1481c0204fd2STrond Myklebust 		put_nfs_open_context(ctx);
1482898f635cSTrond Myklebust out_err_drop:
1483c0204fd2STrond Myklebust 	d_drop(dentry);
1484898f635cSTrond Myklebust out_err:
1485c0204fd2STrond Myklebust 	return error;
1486c0204fd2STrond Myklebust }
1487c0204fd2STrond Myklebust 
14881da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds /*
14911da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
14921da177e4SLinus Torvalds  */
14931da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
14941da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
14951da177e4SLinus Torvalds {
1496fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1497fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
14981da177e4SLinus Torvalds 	struct inode *inode;
14991da177e4SLinus Torvalds 	int error = -EACCES;
15001da177e4SLinus Torvalds 
1501fab728e1STrond Myklebust 	d_drop(dentry);
1502fab728e1STrond Myklebust 
15031da177e4SLinus Torvalds 	/* We may have been initialized further down */
15041da177e4SLinus Torvalds 	if (dentry->d_inode)
1505fab728e1STrond Myklebust 		goto out;
15061da177e4SLinus Torvalds 	if (fhandle->size == 0) {
15071da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
15081da177e4SLinus Torvalds 		if (error)
1509fab728e1STrond Myklebust 			goto out_error;
15101da177e4SLinus Torvalds 	}
15115724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
15121da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
15131da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
15148fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
15151da177e4SLinus Torvalds 		if (error < 0)
1516fab728e1STrond Myklebust 			goto out_error;
15171da177e4SLinus Torvalds 	}
15181da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
151903f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
152003f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1521fab728e1STrond Myklebust 		goto out_error;
1522fab728e1STrond Myklebust 	d_add(dentry, inode);
1523fab728e1STrond Myklebust out:
1524fab728e1STrond Myklebust 	dput(parent);
15251da177e4SLinus Torvalds 	return 0;
1526fab728e1STrond Myklebust out_error:
1527fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1528fab728e1STrond Myklebust 	dput(parent);
1529fab728e1STrond Myklebust 	return error;
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds /*
15331da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
15341da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
15351da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
15361da177e4SLinus Torvalds  * reply path made it appear to have failed.
15371da177e4SLinus Torvalds  */
15381da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
15391da177e4SLinus Torvalds 		struct nameidata *nd)
15401da177e4SLinus Torvalds {
15411da177e4SLinus Torvalds 	struct iattr attr;
15421da177e4SLinus Torvalds 	int error;
15431da177e4SLinus Torvalds 
15441e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
15451e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	attr.ia_mode = mode;
15481da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15491da177e4SLinus Torvalds 
1550c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
15511da177e4SLinus Torvalds 	if (error != 0)
15521da177e4SLinus Torvalds 		goto out_err;
15531da177e4SLinus Torvalds 	return 0;
15541da177e4SLinus Torvalds out_err:
15551da177e4SLinus Torvalds 	d_drop(dentry);
15561da177e4SLinus Torvalds 	return error;
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds /*
15601da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15611da177e4SLinus Torvalds  */
15621da177e4SLinus Torvalds static int
15631da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	struct iattr attr;
15661da177e4SLinus Torvalds 	int status;
15671da177e4SLinus Torvalds 
15681e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
15691e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
15721da177e4SLinus Torvalds 		return -EINVAL;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	attr.ia_mode = mode;
15751da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
15781da177e4SLinus Torvalds 	if (status != 0)
15791da177e4SLinus Torvalds 		goto out_err;
15801da177e4SLinus Torvalds 	return 0;
15811da177e4SLinus Torvalds out_err:
15821da177e4SLinus Torvalds 	d_drop(dentry);
15831da177e4SLinus Torvalds 	return status;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds /*
15871da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15881da177e4SLinus Torvalds  */
15891da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	struct iattr attr;
15921da177e4SLinus Torvalds 	int error;
15931da177e4SLinus Torvalds 
15941e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
15951e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15981da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
16011da177e4SLinus Torvalds 	if (error != 0)
16021da177e4SLinus Torvalds 		goto out_err;
16031da177e4SLinus Torvalds 	return 0;
16041da177e4SLinus Torvalds out_err:
16051da177e4SLinus Torvalds 	d_drop(dentry);
16061da177e4SLinus Torvalds 	return error;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
1609d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1610d45b9d8bSTrond Myklebust {
1611d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1612d45b9d8bSTrond Myklebust 		d_delete(dentry);
1613d45b9d8bSTrond Myklebust }
1614d45b9d8bSTrond Myklebust 
16151da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
16161da177e4SLinus Torvalds {
16171da177e4SLinus Torvalds 	int error;
16181da177e4SLinus Torvalds 
16191e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
16201e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
16231da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
16241da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1625ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1626d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1627d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	return error;
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds /*
16331da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
16341da177e4SLinus Torvalds  * and after checking that the file has only one user.
16351da177e4SLinus Torvalds  *
16361da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
16371da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
16381da177e4SLinus Torvalds  */
16391da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
16421da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16431da177e4SLinus Torvalds 	int error = -EBUSY;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
16461da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
16491da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16501da177e4SLinus Torvalds 		error = 0;
16511da177e4SLinus Torvalds 		goto out;
16521da177e4SLinus Torvalds 	}
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	if (inode != NULL) {
1655cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
16561da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
16571da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
16581da177e4SLinus Torvalds 		if (error == 0)
16591b83d707STrond Myklebust 			nfs_drop_nlink(inode);
16605ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
16611da177e4SLinus Torvalds 	} else
16621da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1663d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1664d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16651da177e4SLinus Torvalds out:
16661da177e4SLinus Torvalds 	return error;
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
16701da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
16711da177e4SLinus Torvalds  *
16721da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
16731da177e4SLinus Torvalds  */
16741da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
16751da177e4SLinus Torvalds {
16761da177e4SLinus Torvalds 	int error;
16771da177e4SLinus Torvalds 	int need_rehash = 0;
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
16801da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
16831da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
16841da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) > 1) {
16851da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
16861da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
1687ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1688ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
16891da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
16901da177e4SLinus Torvalds 		return error;
16911da177e4SLinus Torvalds 	}
16921da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
16931da177e4SLinus Torvalds 		__d_drop(dentry);
16941da177e4SLinus Torvalds 		need_rehash = 1;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
16971da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
16981da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1699d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
17001da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17011da177e4SLinus Torvalds 	} else if (need_rehash)
17021da177e4SLinus Torvalds 		d_rehash(dentry);
17031da177e4SLinus Torvalds 	return error;
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
1706873101b3SChuck Lever /*
1707873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1708873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1709873101b3SChuck Lever  * using prepare_write/commit_write.
1710873101b3SChuck Lever  *
1711873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1712873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1713873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1714873101b3SChuck Lever  * symlink request has completed on the server.
1715873101b3SChuck Lever  *
1716873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1717873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1718873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1719873101b3SChuck Lever  * and move the raw page into its mapping.
1720873101b3SChuck Lever  */
1721873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17221da177e4SLinus Torvalds {
1723873101b3SChuck Lever 	struct pagevec lru_pvec;
1724873101b3SChuck Lever 	struct page *page;
1725873101b3SChuck Lever 	char *kaddr;
17261da177e4SLinus Torvalds 	struct iattr attr;
1727873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
17281da177e4SLinus Torvalds 	int error;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
17311da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
17321da177e4SLinus Torvalds 
1733873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1734873101b3SChuck Lever 		return -ENAMETOOLONG;
17351da177e4SLinus Torvalds 
1736873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1737873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
17381da177e4SLinus Torvalds 
173983d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
174076566991STrond Myklebust 	if (!page)
1741873101b3SChuck Lever 		return -ENOMEM;
1742873101b3SChuck Lever 
1743873101b3SChuck Lever 	kaddr = kmap_atomic(page, KM_USER0);
1744873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1745873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1746873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1747873101b3SChuck Lever 	kunmap_atomic(kaddr, KM_USER0);
1748873101b3SChuck Lever 
174994a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1750873101b3SChuck Lever 	if (error != 0) {
1751873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1752873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1753873101b3SChuck Lever 			dentry->d_name.name, symname, error);
17541da177e4SLinus Torvalds 		d_drop(dentry);
1755873101b3SChuck Lever 		__free_page(page);
17561da177e4SLinus Torvalds 		return error;
17571da177e4SLinus Torvalds 	}
17581da177e4SLinus Torvalds 
1759873101b3SChuck Lever 	/*
1760873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1761873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1762873101b3SChuck Lever 	 */
1763873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1764873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1765873101b3SChuck Lever 							GFP_KERNEL)) {
176639cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
17674f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1768873101b3SChuck Lever 		SetPageUptodate(page);
1769873101b3SChuck Lever 		unlock_page(page);
1770873101b3SChuck Lever 	} else
1771873101b3SChuck Lever 		__free_page(page);
1772873101b3SChuck Lever 
1773873101b3SChuck Lever 	return 0;
1774873101b3SChuck Lever }
1775873101b3SChuck Lever 
17761da177e4SLinus Torvalds static int
17771da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
17781da177e4SLinus Torvalds {
17791da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17801da177e4SLinus Torvalds 	int error;
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
17831da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
17841da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17851da177e4SLinus Torvalds 
17869a3936aaSTrond Myklebust 	nfs_inode_return_delegation(inode);
17879a3936aaSTrond Myklebust 
17889697d234STrond Myklebust 	d_drop(dentry);
17891da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1790cf809556STrond Myklebust 	if (error == 0) {
1791cf809556STrond Myklebust 		atomic_inc(&inode->i_count);
17929697d234STrond Myklebust 		d_add(dentry, inode);
1793cf809556STrond Myklebust 	}
17941da177e4SLinus Torvalds 	return error;
17951da177e4SLinus Torvalds }
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds /*
17981da177e4SLinus Torvalds  * RENAME
17991da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
18001da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
18011da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
18021da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
18031da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
18041da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
18051da177e4SLinus Torvalds  *
18061da177e4SLinus Torvalds  * FIXED.
18071da177e4SLinus Torvalds  *
18081da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
18091da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
18101da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
18111da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
18121da177e4SLinus Torvalds  * using the inode layer
18131da177e4SLinus Torvalds  *
18141da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
18151da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
18161da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
18171da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
18181da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
18191da177e4SLinus Torvalds  * the rename.
18201da177e4SLinus Torvalds  */
18211da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
18221da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
18231da177e4SLinus Torvalds {
18241da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
18251da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
18261da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
18271da177e4SLinus Torvalds 	int error = -EBUSY;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
18301da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
18311da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
18321da177e4SLinus Torvalds 		 atomic_read(&new_dentry->d_count));
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	/*
183528f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
183628f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
183728f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
183828f79a1aSMiklos Szeredi 	 * the new target.
18391da177e4SLinus Torvalds 	 */
184027226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
184127226104SMiklos Szeredi 		/*
184227226104SMiklos Szeredi 		 * To prevent any new references to the target during the
184327226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
184427226104SMiklos Szeredi 		 */
184527226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
184627226104SMiklos Szeredi 			d_drop(new_dentry);
184727226104SMiklos Szeredi 			rehash = new_dentry;
184827226104SMiklos Szeredi 		}
184927226104SMiklos Szeredi 
185027226104SMiklos Szeredi 		if (atomic_read(&new_dentry->d_count) > 2) {
18511da177e4SLinus Torvalds 			int err;
185227226104SMiklos Szeredi 
18531da177e4SLinus Torvalds 			/* copy the target dentry's name */
18541da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
18551da177e4SLinus Torvalds 					 &new_dentry->d_name);
18561da177e4SLinus Torvalds 			if (!dentry)
18571da177e4SLinus Torvalds 				goto out;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
18601da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
186124e93025SMiklos Szeredi 			if (err)
18621da177e4SLinus Torvalds 				goto out;
186324e93025SMiklos Szeredi 
186424e93025SMiklos Szeredi 			new_dentry = dentry;
186556335936SOGAWA Hirofumi 			rehash = NULL;
186624e93025SMiklos Szeredi 			new_inode = NULL;
1867b1e4adf4STrond Myklebust 		}
186827226104SMiklos Szeredi 	}
18691da177e4SLinus Torvalds 
1870cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
1871b1e4adf4STrond Myklebust 	if (new_inode != NULL)
187224174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
18751da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
18765ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
18771da177e4SLinus Torvalds out:
18781da177e4SLinus Torvalds 	if (rehash)
18791da177e4SLinus Torvalds 		d_rehash(rehash);
18801da177e4SLinus Torvalds 	if (!error) {
1881b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1882b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
18831da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
18848fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
18858fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1886d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1887d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 	/* new dentry created? */
18901da177e4SLinus Torvalds 	if (dentry)
18911da177e4SLinus Torvalds 		dput(dentry);
18921da177e4SLinus Torvalds 	return error;
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds 
1895cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1896cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1897cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1898cfcea3e8STrond Myklebust 
18991c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
19001c3c07e9STrond Myklebust {
19011c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
19021c3c07e9STrond Myklebust 	kfree(entry);
1903cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1904cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1905cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
19061c3c07e9STrond Myklebust }
19071c3c07e9STrond Myklebust 
19081a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
19091a81bb8aSTrond Myklebust {
19101a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
19111a81bb8aSTrond Myklebust 
19121a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
19131a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
19141a81bb8aSTrond Myklebust 		list_del(&cache->lru);
19151a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
19161a81bb8aSTrond Myklebust 	}
19171a81bb8aSTrond Myklebust }
19181a81bb8aSTrond Myklebust 
19197f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
1920979df72eSTrond Myklebust {
1921979df72eSTrond Myklebust 	LIST_HEAD(head);
1922aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1923979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
1924979df72eSTrond Myklebust 
192561d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
192661d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
19279c7e7e23STrond Myklebust 
1928a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1929aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1930979df72eSTrond Myklebust 		struct inode *inode;
1931979df72eSTrond Myklebust 
1932979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1933979df72eSTrond Myklebust 			break;
19349c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
1935979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1936979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1937979df72eSTrond Myklebust 			goto remove_lru_entry;
1938979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1939979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1940979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1941979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1942979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1943979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1944979df72eSTrond Myklebust 					&nfs_access_lru_list);
1945979df72eSTrond Myklebust 		else {
1946979df72eSTrond Myklebust remove_lru_entry:
1947979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
19489c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
1949979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
19509c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
1951979df72eSTrond Myklebust 		}
195259844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
1953979df72eSTrond Myklebust 	}
1954979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19551a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
1956979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1957979df72eSTrond Myklebust }
1958979df72eSTrond Myklebust 
19591a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
19601c3c07e9STrond Myklebust {
19611c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
19621a81bb8aSTrond Myklebust 	struct rb_node *n;
19631c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19641c3c07e9STrond Myklebust 
19651c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
19661c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
19671c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19681c3c07e9STrond Myklebust 		rb_erase(n, root_node);
19691a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
19701c3c07e9STrond Myklebust 	}
19711c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
19721c3c07e9STrond Myklebust }
19731c3c07e9STrond Myklebust 
19741c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
19751c3c07e9STrond Myklebust {
19761a81bb8aSTrond Myklebust 	LIST_HEAD(head);
19771a81bb8aSTrond Myklebust 
19781a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
19791a81bb8aSTrond Myklebust 		return;
1980cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
1981cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
19821a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
1983cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1984cfcea3e8STrond Myklebust 
19851c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
19861a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
19871a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
19881a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19891a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
19901c3c07e9STrond Myklebust }
19911c3c07e9STrond Myklebust 
19921c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
19931c3c07e9STrond Myklebust {
19941c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
19951c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19961c3c07e9STrond Myklebust 
19971c3c07e9STrond Myklebust 	while (n != NULL) {
19981c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19991c3c07e9STrond Myklebust 
20001c3c07e9STrond Myklebust 		if (cred < entry->cred)
20011c3c07e9STrond Myklebust 			n = n->rb_left;
20021c3c07e9STrond Myklebust 		else if (cred > entry->cred)
20031c3c07e9STrond Myklebust 			n = n->rb_right;
20041c3c07e9STrond Myklebust 		else
20051c3c07e9STrond Myklebust 			return entry;
20061c3c07e9STrond Myklebust 	}
20071c3c07e9STrond Myklebust 	return NULL;
20081c3c07e9STrond Myklebust }
20091c3c07e9STrond Myklebust 
2010af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
20111da177e4SLinus Torvalds {
201255296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
20131c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
20141c3c07e9STrond Myklebust 	int err = -ENOENT;
20151da177e4SLinus Torvalds 
20161c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20171c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
20181c3c07e9STrond Myklebust 		goto out_zap;
20191c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
20201c3c07e9STrond Myklebust 	if (cache == NULL)
20211c3c07e9STrond Myklebust 		goto out;
2022b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
202364672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
20241c3c07e9STrond Myklebust 		goto out_stale;
20251c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
20261c3c07e9STrond Myklebust 	res->cred = cache->cred;
20271c3c07e9STrond Myklebust 	res->mask = cache->mask;
2028cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
20291c3c07e9STrond Myklebust 	err = 0;
20301c3c07e9STrond Myklebust out:
20311c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20321c3c07e9STrond Myklebust 	return err;
20331c3c07e9STrond Myklebust out_stale:
20341c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
2035cfcea3e8STrond Myklebust 	list_del(&cache->lru);
20361c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20371c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
20381da177e4SLinus Torvalds 	return -ENOENT;
20391c3c07e9STrond Myklebust out_zap:
20401a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
20411a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
20421c3c07e9STrond Myklebust 	return -ENOENT;
20431c3c07e9STrond Myklebust }
20441c3c07e9STrond Myklebust 
20451c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
20461c3c07e9STrond Myklebust {
2047cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2048cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
20491c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
20501c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
20511c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
20521c3c07e9STrond Myklebust 
20531c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
20541c3c07e9STrond Myklebust 	while (*p != NULL) {
20551c3c07e9STrond Myklebust 		parent = *p;
20561c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
20571c3c07e9STrond Myklebust 
20581c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
20591c3c07e9STrond Myklebust 			p = &parent->rb_left;
20601c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
20611c3c07e9STrond Myklebust 			p = &parent->rb_right;
20621c3c07e9STrond Myklebust 		else
20631c3c07e9STrond Myklebust 			goto found;
20641c3c07e9STrond Myklebust 	}
20651c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
20661c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2067cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
20681c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20691c3c07e9STrond Myklebust 	return;
20701c3c07e9STrond Myklebust found:
20711c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2072cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2073cfcea3e8STrond Myklebust 	list_del(&entry->lru);
20741c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20751c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
2078af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
20791da177e4SLinus Torvalds {
20801c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
20811c3c07e9STrond Myklebust 	if (cache == NULL)
20821c3c07e9STrond Myklebust 		return;
20831c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
20841da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
20851c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
20861da177e4SLinus Torvalds 	cache->mask = set->mask;
20871c3c07e9STrond Myklebust 
20881c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2089cfcea3e8STrond Myklebust 
2090cfcea3e8STrond Myklebust 	/* Update accounting */
2091cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2092cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2093cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2094cfcea3e8STrond Myklebust 
2095cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
20961a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2097cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
20981a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
20991a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
21001a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2101cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2102cfcea3e8STrond Myklebust 	}
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
21061da177e4SLinus Torvalds {
21071da177e4SLinus Torvalds 	struct nfs_access_entry cache;
21081da177e4SLinus Torvalds 	int status;
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
21111da177e4SLinus Torvalds 	if (status == 0)
21121da177e4SLinus Torvalds 		goto out;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
21151da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
21161da177e4SLinus Torvalds 	cache.cred = cred;
21171da177e4SLinus Torvalds 	cache.jiffies = jiffies;
21181da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2119a71ee337SSuresh Jayaraman 	if (status != 0) {
2120a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2121a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2122a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2123a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2124a71ee337SSuresh Jayaraman 		}
21251da177e4SLinus Torvalds 		return status;
2126a71ee337SSuresh Jayaraman 	}
21271da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
21281da177e4SLinus Torvalds out:
2129e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21301da177e4SLinus Torvalds 		return 0;
21311da177e4SLinus Torvalds 	return -EACCES;
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
2134af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2135af22f94aSTrond Myklebust {
2136af22f94aSTrond Myklebust 	int mask = 0;
2137af22f94aSTrond Myklebust 
2138af22f94aSTrond Myklebust 	if (openflags & FMODE_READ)
2139af22f94aSTrond Myklebust 		mask |= MAY_READ;
2140af22f94aSTrond Myklebust 	if (openflags & FMODE_WRITE)
2141af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
2142af22f94aSTrond Myklebust 	if (openflags & FMODE_EXEC)
2143af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2144af22f94aSTrond Myklebust 	return mask;
2145af22f94aSTrond Myklebust }
2146af22f94aSTrond Myklebust 
2147af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2148af22f94aSTrond Myklebust {
2149af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2150af22f94aSTrond Myklebust }
2151af22f94aSTrond Myklebust 
2152e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask)
21531da177e4SLinus Torvalds {
21541da177e4SLinus Torvalds 	struct rpc_cred *cred;
21551da177e4SLinus Torvalds 	int res = 0;
21561da177e4SLinus Torvalds 
215791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
215891d5b470SChuck Lever 
2159e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21601da177e4SLinus Torvalds 		goto out;
21611da177e4SLinus Torvalds 	/* Is this sys_access() ? */
21629cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
21631da177e4SLinus Torvalds 		goto force_lookup;
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
21661da177e4SLinus Torvalds 		case S_IFLNK:
21671da177e4SLinus Torvalds 			goto out;
21681da177e4SLinus Torvalds 		case S_IFREG:
21691da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
21701da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
21717ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
21727ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
21731da177e4SLinus Torvalds 				goto out;
21741da177e4SLinus Torvalds 			break;
21751da177e4SLinus Torvalds 		case S_IFDIR:
21761da177e4SLinus Torvalds 			/*
21771da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
21781da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
21791da177e4SLinus Torvalds 			 */
21801da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
21811da177e4SLinus Torvalds 				goto out;
21821da177e4SLinus Torvalds 	}
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds force_lookup:
21851da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
21861da177e4SLinus Torvalds 		goto out_notsup;
21871da177e4SLinus Torvalds 
218898a8e323STrond Myklebust 	cred = rpc_lookup_cred();
21891da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
21901da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
21911da177e4SLinus Torvalds 		put_rpccred(cred);
21921da177e4SLinus Torvalds 	} else
21931da177e4SLinus Torvalds 		res = PTR_ERR(cred);
21941da177e4SLinus Torvalds out:
2195f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2196f696a365SMiklos Szeredi 		res = -EACCES;
2197f696a365SMiklos Szeredi 
21981e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
21991e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
22001da177e4SLinus Torvalds 	return res;
22011da177e4SLinus Torvalds out_notsup:
22021da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
22031da177e4SLinus Torvalds 	if (res == 0)
22041da177e4SLinus Torvalds 		res = generic_permission(inode, mask, NULL);
22051e7cb3dcSChuck Lever 	goto out;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds /*
22091da177e4SLinus Torvalds  * Local variables:
22101da177e4SLinus Torvalds  *  version-control: t
22111da177e4SLinus Torvalds  *  kept-new-versions: 5
22121da177e4SLinus Torvalds  * End:
22131da177e4SLinus Torvalds  */
2214