xref: /openbmc/linux/fs/nfs/dir.c (revision babddc72a9468884ce1a23db3c3d54b0afa299f0)
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>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include "delegation.h"
3891d5b470SChuck Lever #include "iostat.h"
394c30d56eSAdrian Bunk #include "internal.h"
40cd9a1c0eSTrond Myklebust #include "fscache.h"
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
451da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
461da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
471da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
481da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
491da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
501da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
511da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
521da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
531da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
541da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
551da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
567ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *, int);
57f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
58d1bacf9eSBryan Schumaker static int nfs_readdir_clear_array(struct page*, gfp_t);
591da177e4SLinus Torvalds 
604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
61f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
621da177e4SLinus Torvalds 	.read		= generic_read_dir,
631da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
641da177e4SLinus Torvalds 	.open		= nfs_opendir,
651da177e4SLinus Torvalds 	.release	= nfs_release,
661da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
6992e1d5beSArjan van de Ven const struct inode_operations nfs_dir_inode_operations = {
701da177e4SLinus Torvalds 	.create		= nfs_create,
711da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
721da177e4SLinus Torvalds 	.link		= nfs_link,
731da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
741da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
751da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
761da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
771da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
781da177e4SLinus Torvalds 	.rename		= nfs_rename,
791da177e4SLinus Torvalds 	.permission	= nfs_permission,
801da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
811da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
84d1bacf9eSBryan Schumaker const struct address_space_operations nfs_dir_addr_space_ops = {
85d1bacf9eSBryan Schumaker 	.releasepage = nfs_readdir_clear_array,
86d1bacf9eSBryan Schumaker };
87d1bacf9eSBryan Schumaker 
88b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
8992e1d5beSArjan van de Ven const struct inode_operations nfs3_dir_inode_operations = {
90b7fa0554SAndreas Gruenbacher 	.create		= nfs_create,
91b7fa0554SAndreas Gruenbacher 	.lookup		= nfs_lookup,
92b7fa0554SAndreas Gruenbacher 	.link		= nfs_link,
93b7fa0554SAndreas Gruenbacher 	.unlink		= nfs_unlink,
94b7fa0554SAndreas Gruenbacher 	.symlink	= nfs_symlink,
95b7fa0554SAndreas Gruenbacher 	.mkdir		= nfs_mkdir,
96b7fa0554SAndreas Gruenbacher 	.rmdir		= nfs_rmdir,
97b7fa0554SAndreas Gruenbacher 	.mknod		= nfs_mknod,
98b7fa0554SAndreas Gruenbacher 	.rename		= nfs_rename,
99b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
100b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
101b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
102b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
103b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
104b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
105b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
106b7fa0554SAndreas Gruenbacher };
107b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_V3 */
108b7fa0554SAndreas Gruenbacher 
1091da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
112c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
11392e1d5beSArjan van de Ven const struct inode_operations nfs4_dir_inode_operations = {
114c0204fd2STrond Myklebust 	.create		= nfs_open_create,
1151da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
1161da177e4SLinus Torvalds 	.link		= nfs_link,
1171da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
1181da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
1191da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
1201da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
1211da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
1221da177e4SLinus Torvalds 	.rename		= nfs_rename,
1231da177e4SLinus Torvalds 	.permission	= nfs_permission,
1241da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
1251da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
1266b3b5496SJ. Bruce Fields 	.getxattr       = nfs4_getxattr,
1276b3b5496SJ. Bruce Fields 	.setxattr       = nfs4_setxattr,
1286b3b5496SJ. Bruce Fields 	.listxattr      = nfs4_listxattr,
1291da177e4SLinus Torvalds };
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
1341da177e4SLinus Torvalds  * Open file
1351da177e4SLinus Torvalds  */
1361da177e4SLinus Torvalds static int
1371da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1381da177e4SLinus Torvalds {
1397451c4f0SCarsten Otte 	int res;
1401da177e4SLinus Torvalds 
1416da24bc9SChuck Lever 	dfprintk(FILE, "NFS: open dir(%s/%s)\n",
142cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
143cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
144cc0dd2d1SChuck Lever 
145cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1461e7cb3dcSChuck Lever 
1471da177e4SLinus Torvalds 	/* Call generic open code in order to cache credentials */
1481da177e4SLinus Torvalds 	res = nfs_open(inode, filp);
149f5a73672SNeil Brown 	if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
150f5a73672SNeil Brown 		/* This is a mountpoint, so d_revalidate will never
151f5a73672SNeil Brown 		 * have been called, so we need to refresh the
152f5a73672SNeil Brown 		 * inode (for close-open consistency) ourselves.
153f5a73672SNeil Brown 		 */
154f5a73672SNeil Brown 		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
155f5a73672SNeil Brown 	}
1561da177e4SLinus Torvalds 	return res;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
160d1bacf9eSBryan Schumaker 	u64 cookie;
161d1bacf9eSBryan Schumaker 	u64 ino;
162d1bacf9eSBryan Schumaker 	struct qstr string;
163d1bacf9eSBryan Schumaker };
164d1bacf9eSBryan Schumaker 
165d1bacf9eSBryan Schumaker struct nfs_cache_array {
166d1bacf9eSBryan Schumaker 	unsigned int size;
167d1bacf9eSBryan Schumaker 	int eof_index;
168d1bacf9eSBryan Schumaker 	u64 last_cookie;
169d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
170d1bacf9eSBryan Schumaker };
171d1bacf9eSBryan Schumaker 
172d1bacf9eSBryan Schumaker #define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
173d1bacf9eSBryan Schumaker 
174*babddc72SBryan Schumaker typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1751da177e4SLinus Torvalds typedef struct {
1761da177e4SLinus Torvalds 	struct file	*file;
1771da177e4SLinus Torvalds 	struct page	*page;
1781da177e4SLinus Torvalds 	unsigned long	page_index;
179f0dd2136STrond Myklebust 	u64		*dir_cookie;
180f0dd2136STrond Myklebust 	loff_t		current_index;
1811da177e4SLinus Torvalds 	decode_dirent_t	decode;
182d1bacf9eSBryan Schumaker 
1831f4eab7eSNeil Brown 	unsigned long	timestamp;
1844704f0e2STrond Myklebust 	unsigned long	gencount;
185d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
186d1bacf9eSBryan Schumaker 	unsigned int	plus:1;
187d1bacf9eSBryan Schumaker 	unsigned int	eof:1;
1881da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1891da177e4SLinus Torvalds 
190d1bacf9eSBryan Schumaker /*
191d1bacf9eSBryan Schumaker  * The caller is responsible for calling nfs_readdir_release_array(page)
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds static
194d1bacf9eSBryan Schumaker struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
1951da177e4SLinus Torvalds {
196d1bacf9eSBryan Schumaker 	if (page == NULL)
197d1bacf9eSBryan Schumaker 		return ERR_PTR(-EIO);
198d1bacf9eSBryan Schumaker 	return (struct nfs_cache_array *)kmap(page);
199d1bacf9eSBryan Schumaker }
200d1bacf9eSBryan Schumaker 
201d1bacf9eSBryan Schumaker static
202d1bacf9eSBryan Schumaker void nfs_readdir_release_array(struct page *page)
203d1bacf9eSBryan Schumaker {
204d1bacf9eSBryan Schumaker 	kunmap(page);
205d1bacf9eSBryan Schumaker }
206d1bacf9eSBryan Schumaker 
207d1bacf9eSBryan Schumaker /*
208d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
209d1bacf9eSBryan Schumaker  */
210d1bacf9eSBryan Schumaker static
211d1bacf9eSBryan Schumaker int nfs_readdir_clear_array(struct page *page, gfp_t mask)
212d1bacf9eSBryan Schumaker {
213d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
214d1bacf9eSBryan Schumaker 	int i;
215d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
216d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
217d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
218d1bacf9eSBryan Schumaker 	return 0;
219d1bacf9eSBryan Schumaker }
220d1bacf9eSBryan Schumaker 
221d1bacf9eSBryan Schumaker /*
222d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
223d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
224d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
225d1bacf9eSBryan Schumaker  */
226d1bacf9eSBryan Schumaker static
227d1bacf9eSBryan Schumaker void nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
228d1bacf9eSBryan Schumaker {
229d1bacf9eSBryan Schumaker 	string->len = len;
230d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
231d1bacf9eSBryan Schumaker 	string->hash = full_name_hash(string->name, string->len);
232d1bacf9eSBryan Schumaker }
233d1bacf9eSBryan Schumaker 
234d1bacf9eSBryan Schumaker static
235d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
236d1bacf9eSBryan Schumaker {
237d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = nfs_readdir_get_array(page);
238d1bacf9eSBryan Schumaker 	if (IS_ERR(array))
239d1bacf9eSBryan Schumaker 		return PTR_ERR(array);
240d1bacf9eSBryan Schumaker 	if (array->size >= MAX_READDIR_ARRAY) {
241d1bacf9eSBryan Schumaker 		nfs_readdir_release_array(page);
242d1bacf9eSBryan Schumaker 		return -EIO;
243d1bacf9eSBryan Schumaker 	}
244d1bacf9eSBryan Schumaker 
245d1bacf9eSBryan Schumaker 	array->array[array->size].cookie = entry->prev_cookie;
246d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
247d1bacf9eSBryan Schumaker 	array->array[array->size].ino = entry->ino;
248d1bacf9eSBryan Schumaker 	nfs_readdir_make_qstr(&array->array[array->size].string, entry->name, entry->len);
249d1bacf9eSBryan Schumaker 	if (entry->eof == 1)
250d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
251d1bacf9eSBryan Schumaker 	array->size++;
252d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
253d1bacf9eSBryan Schumaker 	return 0;
254d1bacf9eSBryan Schumaker }
255d1bacf9eSBryan Schumaker 
256d1bacf9eSBryan Schumaker static
257d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
258d1bacf9eSBryan Schumaker {
259d1bacf9eSBryan Schumaker 	loff_t diff = desc->file->f_pos - desc->current_index;
260d1bacf9eSBryan Schumaker 	unsigned int index;
261d1bacf9eSBryan Schumaker 
262d1bacf9eSBryan Schumaker 	if (diff < 0)
263d1bacf9eSBryan Schumaker 		goto out_eof;
264d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
265d1bacf9eSBryan Schumaker 		if (array->eof_index > 0)
266d1bacf9eSBryan Schumaker 			goto out_eof;
267d1bacf9eSBryan Schumaker 		desc->current_index += array->size;
268d1bacf9eSBryan Schumaker 		return -EAGAIN;
269d1bacf9eSBryan Schumaker 	}
270d1bacf9eSBryan Schumaker 
271d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
272d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
273d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
274d1bacf9eSBryan Schumaker 	if (index == array->eof_index)
275d1bacf9eSBryan Schumaker 		desc->eof = 1;
276d1bacf9eSBryan Schumaker 	return 0;
277d1bacf9eSBryan Schumaker out_eof:
278d1bacf9eSBryan Schumaker 	desc->eof = 1;
279d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
280d1bacf9eSBryan Schumaker }
281d1bacf9eSBryan Schumaker 
282d1bacf9eSBryan Schumaker static
283d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
284d1bacf9eSBryan Schumaker {
285d1bacf9eSBryan Schumaker 	int i;
286d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
287d1bacf9eSBryan Schumaker 
288d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
289d1bacf9eSBryan Schumaker 		if (i == array->eof_index) {
290d1bacf9eSBryan Schumaker 			desc->eof = 1;
291d1bacf9eSBryan Schumaker 			status = -EBADCOOKIE;
292d1bacf9eSBryan Schumaker 		}
293d1bacf9eSBryan Schumaker 		if (array->array[i].cookie == *desc->dir_cookie) {
294d1bacf9eSBryan Schumaker 			desc->cache_entry_index = i;
295d1bacf9eSBryan Schumaker 			status = 0;
296d1bacf9eSBryan Schumaker 			break;
297d1bacf9eSBryan Schumaker 		}
298d1bacf9eSBryan Schumaker 	}
299d1bacf9eSBryan Schumaker 
300d1bacf9eSBryan Schumaker 	return status;
301d1bacf9eSBryan Schumaker }
302d1bacf9eSBryan Schumaker 
303d1bacf9eSBryan Schumaker static
304d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
305d1bacf9eSBryan Schumaker {
306d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
307d1bacf9eSBryan Schumaker 	int status = -EBADCOOKIE;
308d1bacf9eSBryan Schumaker 
309d1bacf9eSBryan Schumaker 	if (desc->dir_cookie == NULL)
310d1bacf9eSBryan Schumaker 		goto out;
311d1bacf9eSBryan Schumaker 
312d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
313d1bacf9eSBryan Schumaker 	if (IS_ERR(array)) {
314d1bacf9eSBryan Schumaker 		status = PTR_ERR(array);
315d1bacf9eSBryan Schumaker 		goto out;
316d1bacf9eSBryan Schumaker 	}
317d1bacf9eSBryan Schumaker 
318d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
319d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
320d1bacf9eSBryan Schumaker 	else
321d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
322d1bacf9eSBryan Schumaker 
323d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
324d1bacf9eSBryan Schumaker out:
325d1bacf9eSBryan Schumaker 	return status;
326d1bacf9eSBryan Schumaker }
327d1bacf9eSBryan Schumaker 
328d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
329d1bacf9eSBryan Schumaker static
330d1bacf9eSBryan Schumaker int nfs_readdir_xdr_filler(struct page *xdr_page, nfs_readdir_descriptor_t *desc,
331d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
332d1bacf9eSBryan Schumaker {
3331da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
3344704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3351da177e4SLinus Torvalds 	int		error;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds  again:
3381da177e4SLinus Torvalds 	timestamp = jiffies;
3394704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
340d1bacf9eSBryan Schumaker 	error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, xdr_page,
3411da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3421da177e4SLinus Torvalds 	if (error < 0) {
3431da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3441da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3451da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3463a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
3471da177e4SLinus Torvalds 			desc->plus = 0;
3481da177e4SLinus Torvalds 			goto again;
3491da177e4SLinus Torvalds 		}
3501da177e4SLinus Torvalds 		goto error;
3511da177e4SLinus Torvalds 	}
3521f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3534704f0e2STrond Myklebust 	desc->gencount = gencount;
354d1bacf9eSBryan Schumaker error:
355d1bacf9eSBryan Schumaker 	return error;
356d1bacf9eSBryan Schumaker }
357d1bacf9eSBryan Schumaker 
358d1bacf9eSBryan Schumaker /* Fill in an entry based on the xdr code stored in desc->page */
359d1bacf9eSBryan Schumaker static
360*babddc72SBryan Schumaker int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
361d1bacf9eSBryan Schumaker {
362*babddc72SBryan Schumaker 	__be32 *p = desc->decode(stream, entry, desc->plus);
363d1bacf9eSBryan Schumaker 	if (IS_ERR(p))
364d1bacf9eSBryan Schumaker 		return PTR_ERR(p);
365d1bacf9eSBryan Schumaker 
366d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
367d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
368d1bacf9eSBryan Schumaker 	return 0;
369d1bacf9eSBryan Schumaker }
370d1bacf9eSBryan Schumaker 
371d39ab9deSBryan Schumaker static
372d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
373d39ab9deSBryan Schumaker {
374d39ab9deSBryan Schumaker 	struct nfs_inode *node;
375d39ab9deSBryan Schumaker 	if (dentry->d_inode == NULL)
376d39ab9deSBryan Schumaker 		goto different;
377d39ab9deSBryan Schumaker 	node = NFS_I(dentry->d_inode);
378d39ab9deSBryan Schumaker 	if (node->fh.size != entry->fh->size)
379d39ab9deSBryan Schumaker 		goto different;
380d39ab9deSBryan Schumaker 	if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
381d39ab9deSBryan Schumaker 		goto different;
382d39ab9deSBryan Schumaker 	return 1;
383d39ab9deSBryan Schumaker different:
384d39ab9deSBryan Schumaker 	return 0;
385d39ab9deSBryan Schumaker }
386d39ab9deSBryan Schumaker 
387d39ab9deSBryan Schumaker static
388d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
389d39ab9deSBryan Schumaker {
390d39ab9deSBryan Schumaker 	struct qstr filename;
391d39ab9deSBryan Schumaker 	struct dentry *dentry = NULL;
392d39ab9deSBryan Schumaker 	struct dentry *alias = NULL;
393d39ab9deSBryan Schumaker 	struct inode *dir = parent->d_inode;
394d39ab9deSBryan Schumaker 	struct inode *inode;
395d39ab9deSBryan Schumaker 
396d39ab9deSBryan Schumaker 	nfs_readdir_make_qstr(&filename, entry->name, entry->len);
397d39ab9deSBryan Schumaker 	if (filename.len == 1 && filename.name[0] == '.')
398d39ab9deSBryan Schumaker 		dentry = dget(parent);
399d39ab9deSBryan Schumaker 	else if (filename.len == 2 && filename.name[0] == '.'
400d39ab9deSBryan Schumaker 				   && filename.name[1] == '.')
401d39ab9deSBryan Schumaker 		dentry = dget_parent(parent);
402d39ab9deSBryan Schumaker 	else
403d39ab9deSBryan Schumaker 		dentry = d_lookup(parent, &filename);
404d39ab9deSBryan Schumaker 
405d39ab9deSBryan Schumaker 	if (dentry != NULL) {
406d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
407d39ab9deSBryan Schumaker 			nfs_refresh_inode(dentry->d_inode, entry->fattr);
408d39ab9deSBryan Schumaker 			goto out;
409d39ab9deSBryan Schumaker 		} else {
410d39ab9deSBryan Schumaker 			d_drop(dentry);
411d39ab9deSBryan Schumaker 			dput(dentry);
412d39ab9deSBryan Schumaker 		}
413d39ab9deSBryan Schumaker 	}
414d39ab9deSBryan Schumaker 
415d39ab9deSBryan Schumaker 	dentry = d_alloc(parent, &filename);
416d39ab9deSBryan Schumaker 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
417d39ab9deSBryan Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
418d39ab9deSBryan Schumaker 	if (IS_ERR(inode))
419d39ab9deSBryan Schumaker 		goto out;
420d39ab9deSBryan Schumaker 
421d39ab9deSBryan Schumaker 	alias = d_materialise_unique(dentry, inode);
422d39ab9deSBryan Schumaker 	if (IS_ERR(alias))
423d39ab9deSBryan Schumaker 		goto out;
424d39ab9deSBryan Schumaker 	else if (alias) {
425d39ab9deSBryan Schumaker 		nfs_set_verifier(alias, nfs_save_change_attribute(dir));
426d39ab9deSBryan Schumaker 		dput(alias);
427d39ab9deSBryan Schumaker 	} else
428d39ab9deSBryan Schumaker 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
429d39ab9deSBryan Schumaker 
430d39ab9deSBryan Schumaker out:
431d39ab9deSBryan Schumaker 	dput(dentry);
432d39ab9deSBryan Schumaker 	kfree(filename.name);
433d39ab9deSBryan Schumaker 	return;
434d39ab9deSBryan Schumaker }
435d39ab9deSBryan Schumaker 
436d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
437d1bacf9eSBryan Schumaker static
438d1bacf9eSBryan Schumaker void nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
439*babddc72SBryan Schumaker 				struct page *xdr_page, struct page *page, unsigned int buflen)
440d1bacf9eSBryan Schumaker {
441*babddc72SBryan Schumaker 	struct xdr_stream stream;
442*babddc72SBryan Schumaker 	struct xdr_buf buf;
443d1bacf9eSBryan Schumaker 	__be32 *ptr = kmap(xdr_page);
444*babddc72SBryan Schumaker 
445*babddc72SBryan Schumaker 	buf.head->iov_base = xdr_page;
446*babddc72SBryan Schumaker 	buf.head->iov_len = buflen;
447*babddc72SBryan Schumaker 	buf.tail->iov_len = 0;
448*babddc72SBryan Schumaker 	buf.page_base = 0;
449*babddc72SBryan Schumaker 	buf.page_len = 0;
450*babddc72SBryan Schumaker 	buf.buflen = buf.head->iov_len;
451*babddc72SBryan Schumaker 	buf.len = buf.head->iov_len;
452*babddc72SBryan Schumaker 
453*babddc72SBryan Schumaker 	xdr_init_decode(&stream, &buf, ptr);
454*babddc72SBryan Schumaker 
455*babddc72SBryan Schumaker 	while (xdr_decode(desc, entry, &stream) == 0) {
456d1bacf9eSBryan Schumaker 		if (nfs_readdir_add_to_array(entry, page) == -1)
457d1bacf9eSBryan Schumaker 			break;
458d39ab9deSBryan Schumaker 		if (desc->plus == 1)
459d39ab9deSBryan Schumaker 			nfs_prime_dcache(desc->file->f_path.dentry, entry);
460d1bacf9eSBryan Schumaker 	}
461d1bacf9eSBryan Schumaker 	kunmap(xdr_page);
462d1bacf9eSBryan Schumaker }
463d1bacf9eSBryan Schumaker 
464d1bacf9eSBryan Schumaker static
465d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
466d1bacf9eSBryan Schumaker {
467d1bacf9eSBryan Schumaker 	struct page *xdr_page;
468d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
469d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
470d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
471d1bacf9eSBryan Schumaker 	int status = 0;
472*babddc72SBryan Schumaker 	unsigned int array_size = 1;
473d1bacf9eSBryan Schumaker 
474d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
475d1bacf9eSBryan Schumaker 	entry.cookie = *desc->dir_cookie;
476d1bacf9eSBryan Schumaker 	entry.eof = 0;
477d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
478d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
479d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
480d1bacf9eSBryan Schumaker 		goto out;
481d1bacf9eSBryan Schumaker 
482d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(page);
483d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
484d1bacf9eSBryan Schumaker 	array->eof_index = -1;
485d1bacf9eSBryan Schumaker 
486d1bacf9eSBryan Schumaker 	xdr_page = alloc_page(GFP_KERNEL);
487d1bacf9eSBryan Schumaker 	if (!xdr_page)
488d1bacf9eSBryan Schumaker 		goto out_release_array;
489d1bacf9eSBryan Schumaker 	do {
490d1bacf9eSBryan Schumaker 		status = nfs_readdir_xdr_filler(xdr_page, desc, &entry, file, inode);
491*babddc72SBryan Schumaker 
492d1bacf9eSBryan Schumaker 		if (status < 0)
493d1bacf9eSBryan Schumaker 			break;
494*babddc72SBryan Schumaker 		nfs_readdir_page_filler(desc, &entry, xdr_page, page, array_size * PAGE_SIZE);
495d1bacf9eSBryan Schumaker 	} while (array->eof_index < 0 && array->size < MAX_READDIR_ARRAY);
496d1bacf9eSBryan Schumaker 
497d1bacf9eSBryan Schumaker 	put_page(xdr_page);
498d1bacf9eSBryan Schumaker out_release_array:
499d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(page);
500d1bacf9eSBryan Schumaker out:
501d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
502d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
503d1bacf9eSBryan Schumaker 	return status;
504d1bacf9eSBryan Schumaker }
505d1bacf9eSBryan Schumaker 
506d1bacf9eSBryan Schumaker /*
507d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
508d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
509d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
510d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
5111da177e4SLinus Torvalds  */
512d1bacf9eSBryan Schumaker static
513d1bacf9eSBryan Schumaker int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
514d1bacf9eSBryan Schumaker {
515d1bacf9eSBryan Schumaker 	struct inode	*inode = desc->file->f_path.dentry->d_inode;
516d1bacf9eSBryan Schumaker 
517d1bacf9eSBryan Schumaker 	if (nfs_readdir_xdr_to_array(desc, page, inode) == -1)
518d1bacf9eSBryan Schumaker 		goto error;
519d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
520d1bacf9eSBryan Schumaker 
5212aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
522cd9ae2b6STrond Myklebust 		/* Should never happen */
523cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
524cd9ae2b6STrond Myklebust 	}
5251da177e4SLinus Torvalds 	unlock_page(page);
5261da177e4SLinus Torvalds 	return 0;
5271da177e4SLinus Torvalds  error:
5281da177e4SLinus Torvalds 	unlock_page(page);
5291da177e4SLinus Torvalds 	return -EIO;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
532d1bacf9eSBryan Schumaker static
533d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	page_cache_release(desc->page);
5361da177e4SLinus Torvalds 	desc->page = NULL;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
539d1bacf9eSBryan Schumaker static
540d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	struct page *page;
543d1bacf9eSBryan Schumaker 	page = read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
544d1bacf9eSBryan Schumaker 			desc->page_index, (filler_t *)nfs_readdir_filler, desc);
545d1bacf9eSBryan Schumaker 	if (IS_ERR(page))
546d1bacf9eSBryan Schumaker 		desc->eof = 1;
547d1bacf9eSBryan Schumaker 	return page;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /*
551d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
5521da177e4SLinus Torvalds  */
553d1bacf9eSBryan Schumaker static
554d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
555d1bacf9eSBryan Schumaker {
556d1bacf9eSBryan Schumaker 	int res;
557d1bacf9eSBryan Schumaker 
558d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
559d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
560d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
561d1bacf9eSBryan Schumaker 
562d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
563d1bacf9eSBryan Schumaker 	if (res == 0)
564d1bacf9eSBryan Schumaker 		return 0;
565d1bacf9eSBryan Schumaker 	cache_page_release(desc);
566d1bacf9eSBryan Schumaker 	return res;
567d1bacf9eSBryan Schumaker }
568d1bacf9eSBryan Schumaker 
569d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
5701da177e4SLinus Torvalds static inline
5711da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
5721da177e4SLinus Torvalds {
573d1bacf9eSBryan Schumaker 	int res = -EAGAIN;
574d1bacf9eSBryan Schumaker 
575d1bacf9eSBryan Schumaker 	while (1) {
576d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
5771da177e4SLinus Torvalds 		if (res != -EAGAIN)
5781da177e4SLinus Torvalds 			break;
5791da177e4SLinus Torvalds 		desc->page_index++;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	return res;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds /*
5901da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
5911da177e4SLinus Torvalds  */
5921da177e4SLinus Torvalds static
5931da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
5941da177e4SLinus Torvalds 		   filldir_t filldir)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	struct file	*file = desc->file;
597d1bacf9eSBryan Schumaker 	int i = 0;
598d1bacf9eSBryan Schumaker 	int res = 0;
599d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
600d1bacf9eSBryan Schumaker 	unsigned int d_type = DT_UNKNOWN;
6011da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
6021da177e4SLinus Torvalds 
603d1bacf9eSBryan Schumaker 	array = nfs_readdir_get_array(desc->page);
6041da177e4SLinus Torvalds 
605d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
606d1bacf9eSBryan Schumaker 		d_type = DT_UNKNOWN;
6071da177e4SLinus Torvalds 
608d1bacf9eSBryan Schumaker 		res = filldir(dirent, array->array[i].string.name,
609d1bacf9eSBryan Schumaker 			array->array[i].string.len, file->f_pos,
610d1bacf9eSBryan Schumaker 			nfs_compat_user_ino64(array->array[i].ino), d_type);
6111da177e4SLinus Torvalds 		if (res < 0)
6121da177e4SLinus Torvalds 			break;
61300a92642SOlivier Galibert 		file->f_pos++;
614d1bacf9eSBryan Schumaker 		desc->cache_entry_index = i;
615d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
616d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
617d1bacf9eSBryan Schumaker 		else
618d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
619d1bacf9eSBryan Schumaker 		if (i == array->eof_index) {
620d1bacf9eSBryan Schumaker 			desc->eof = 1;
6211da177e4SLinus Torvalds 			break;
6221da177e4SLinus Torvalds 		}
6231da177e4SLinus Torvalds 	}
624d1bacf9eSBryan Schumaker 
625d1bacf9eSBryan Schumaker 	nfs_readdir_release_array(desc->page);
626d1bacf9eSBryan Schumaker 	cache_page_release(desc);
6271da177e4SLinus Torvalds 	if (dentry != NULL)
6281da177e4SLinus Torvalds 		dput(dentry);
6291e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
6301e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
6311da177e4SLinus Torvalds 	return res;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds /*
6351da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
6361da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
6371da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
6381da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
6391da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
6401da177e4SLinus Torvalds  *
6411da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
6421da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
6431da177e4SLinus Torvalds  *	 we should already have a complete representation of the
6441da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
6451da177e4SLinus Torvalds  */
6461da177e4SLinus Torvalds static inline
6471da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
6481da177e4SLinus Torvalds 		     filldir_t filldir)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	struct page	*page = NULL;
6511da177e4SLinus Torvalds 	int		status;
652d1bacf9eSBryan Schumaker 	struct inode *inode = desc->file->f_path.dentry->d_inode;
6531da177e4SLinus Torvalds 
6541e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
6551e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
6581da177e4SLinus Torvalds 	if (!page) {
6591da177e4SLinus Torvalds 		status = -ENOMEM;
6601da177e4SLinus Torvalds 		goto out;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
663d1bacf9eSBryan Schumaker 	if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
664d1bacf9eSBryan Schumaker 		status = -EIO;
665d1bacf9eSBryan Schumaker 		goto out_release;
666d1bacf9eSBryan Schumaker 	}
667d1bacf9eSBryan Schumaker 
668baf57a09STrond Myklebust 	desc->page_index = 0;
669d1bacf9eSBryan Schumaker 	desc->page = page;
6701da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds  out:
6731e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
6743110ff80SHarvey Harrison 			__func__, status);
6751da177e4SLinus Torvalds 	return status;
6761da177e4SLinus Torvalds  out_release:
677d1bacf9eSBryan Schumaker 	cache_page_release(desc);
6781da177e4SLinus Torvalds 	goto out;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
68100a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
68200a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
68300a92642SOlivier Galibert    whole directory.
6841da177e4SLinus Torvalds  */
6851da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
6861da177e4SLinus Torvalds {
68701cce933SJosef "Jeff" Sipek 	struct dentry	*dentry = filp->f_path.dentry;
6881da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
6891da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
6901da177e4SLinus Torvalds 			*desc = &my_desc;
691aa49b4cfSTrond Myklebust 	int res = -ENOMEM;
6921da177e4SLinus Torvalds 
6936da24bc9SChuck Lever 	dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
6941e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
6951e7cb3dcSChuck Lever 			(long long)filp->f_pos);
69691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
69791d5b470SChuck Lever 
6981da177e4SLinus Torvalds 	/*
69900a92642SOlivier Galibert 	 * filp->f_pos points to the dirent entry number.
700f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
70100a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
70200a92642SOlivier Galibert 	 * revalidate the cookie.
7031da177e4SLinus Torvalds 	 */
7041da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	desc->file = filp;
707cd3758e3STrond Myklebust 	desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
7081da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
7091da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
7101da177e4SLinus Torvalds 
711565277f6STrond Myklebust 	nfs_block_sillyrename(dentry);
7121cda707dSTrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
713fccca7fcSTrond Myklebust 	if (res < 0)
714fccca7fcSTrond Myklebust 		goto out;
715fccca7fcSTrond Myklebust 
716d1bacf9eSBryan Schumaker 	while (desc->eof != 1) {
7171da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
71800a92642SOlivier Galibert 
7191da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
7201da177e4SLinus Torvalds 			/* This means either end of directory */
721d1bacf9eSBryan Schumaker 			if (*desc->dir_cookie && desc->eof == 0) {
7221da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
7231da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
7241da177e4SLinus Torvalds 				if (res >= 0)
7251da177e4SLinus Torvalds 					continue;
7261da177e4SLinus Torvalds 			}
7271da177e4SLinus Torvalds 			res = 0;
7281da177e4SLinus Torvalds 			break;
7291da177e4SLinus Torvalds 		}
7301da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
7313a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
7321da177e4SLinus Torvalds 			nfs_zap_caches(inode);
733baf57a09STrond Myklebust 			desc->page_index = 0;
7341da177e4SLinus Torvalds 			desc->plus = 0;
735d1bacf9eSBryan Schumaker 			desc->eof = 0;
7361da177e4SLinus Torvalds 			continue;
7371da177e4SLinus Torvalds 		}
7381da177e4SLinus Torvalds 		if (res < 0)
7391da177e4SLinus Torvalds 			break;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
7421da177e4SLinus Torvalds 		if (res < 0) {
7431da177e4SLinus Torvalds 			res = 0;
7441da177e4SLinus Torvalds 			break;
7451da177e4SLinus Torvalds 		}
7461da177e4SLinus Torvalds 	}
747fccca7fcSTrond Myklebust out:
748565277f6STrond Myklebust 	nfs_unblock_sillyrename(dentry);
7491e7cb3dcSChuck Lever 	if (res > 0)
7501e7cb3dcSChuck Lever 		res = 0;
751aa49b4cfSTrond Myklebust 	dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
7521e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
7531e7cb3dcSChuck Lever 			res);
7541da177e4SLinus Torvalds 	return res;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
75710afec90STrond Myklebust static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
758f0dd2136STrond Myklebust {
759b84e06c5SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
760b84e06c5SChuck Lever 	struct inode *inode = dentry->d_inode;
761b84e06c5SChuck Lever 
7626da24bc9SChuck Lever 	dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
763b84e06c5SChuck Lever 			dentry->d_parent->d_name.name,
764b84e06c5SChuck Lever 			dentry->d_name.name,
765b84e06c5SChuck Lever 			offset, origin);
766b84e06c5SChuck Lever 
767b84e06c5SChuck Lever 	mutex_lock(&inode->i_mutex);
768f0dd2136STrond Myklebust 	switch (origin) {
769f0dd2136STrond Myklebust 		case 1:
770f0dd2136STrond Myklebust 			offset += filp->f_pos;
771f0dd2136STrond Myklebust 		case 0:
772f0dd2136STrond Myklebust 			if (offset >= 0)
773f0dd2136STrond Myklebust 				break;
774f0dd2136STrond Myklebust 		default:
775f0dd2136STrond Myklebust 			offset = -EINVAL;
776f0dd2136STrond Myklebust 			goto out;
777f0dd2136STrond Myklebust 	}
778f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
779f0dd2136STrond Myklebust 		filp->f_pos = offset;
780cd3758e3STrond Myklebust 		nfs_file_open_context(filp)->dir_cookie = 0;
781f0dd2136STrond Myklebust 	}
782f0dd2136STrond Myklebust out:
783b84e06c5SChuck Lever 	mutex_unlock(&inode->i_mutex);
784f0dd2136STrond Myklebust 	return offset;
785f0dd2136STrond Myklebust }
786f0dd2136STrond Myklebust 
7871da177e4SLinus Torvalds /*
7881da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
7891da177e4SLinus Torvalds  * is a dummy operation.
7901da177e4SLinus Torvalds  */
7917ea80859SChristoph Hellwig static int nfs_fsync_dir(struct file *filp, int datasync)
7921da177e4SLinus Torvalds {
7937ea80859SChristoph Hellwig 	struct dentry *dentry = filp->f_path.dentry;
7947ea80859SChristoph Hellwig 
7956da24bc9SChuck Lever 	dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
7961e7cb3dcSChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
7971e7cb3dcSChuck Lever 			datasync);
7981e7cb3dcSChuck Lever 
79954917786SChuck Lever 	nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
8001da177e4SLinus Torvalds 	return 0;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
803bfc69a45STrond Myklebust /**
804bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
805bfc69a45STrond Myklebust  * @dir - pointer to directory inode
806bfc69a45STrond Myklebust  *
807bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
808bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
809bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
810bfc69a45STrond Myklebust  *
811bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
812bfc69a45STrond Myklebust  */
813bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
814bfc69a45STrond Myklebust {
815011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
816bfc69a45STrond Myklebust }
817bfc69a45STrond Myklebust 
8181da177e4SLinus Torvalds /*
8191da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
8201da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
8211da177e4SLinus Torvalds  * and may need to be looked up again.
8221da177e4SLinus Torvalds  */
823c79ba787STrond Myklebust static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
8261da177e4SLinus Torvalds 		return 1;
8274eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
8284eec952eSTrond Myklebust 		return 0;
829f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
8306ecc5e8fSTrond Myklebust 		return 0;
831f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
832f2c77f4eSTrond Myklebust 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
833f2c77f4eSTrond Myklebust 		return 0;
834f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
835f2c77f4eSTrond Myklebust 		return 0;
836f2c77f4eSTrond Myklebust 	return 1;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds /*
8401d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
8411d6757fbSTrond Myklebust  *
8421d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
8431d6757fbSTrond Myklebust  * component of the path.
8441d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
8451d6757fbSTrond Myklebust  */
8461d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
8471d6757fbSTrond Myklebust {
8481d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
8491d6757fbSTrond Myklebust 		return 0;
8501d6757fbSTrond Myklebust 	return nd->flags & mask;
8511d6757fbSTrond Myklebust }
8521d6757fbSTrond Myklebust 
8531d6757fbSTrond Myklebust /*
854a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
855a12802caSTrond Myklebust  * an O_EXCL create using this path component.
856a12802caSTrond Myklebust  */
857a12802caSTrond Myklebust static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
858a12802caSTrond Myklebust {
859a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
860a12802caSTrond Myklebust 		return 0;
8613516586aSAl Viro 	return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
862a12802caSTrond Myklebust }
863a12802caSTrond Myklebust 
864a12802caSTrond Myklebust /*
8651d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
8661d6757fbSTrond Myklebust  *
8671d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
8681d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
8691d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
8701d6757fbSTrond Myklebust  *
8711d6757fbSTrond Myklebust  */
8721da177e4SLinus Torvalds static inline
8731da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
8761da177e4SLinus Torvalds 
8774e99a1ffSTrond Myklebust 	if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
8784e99a1ffSTrond Myklebust 		return 0;
8791da177e4SLinus Torvalds 	if (nd != NULL) {
8801da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
8811d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
8821da177e4SLinus Torvalds 			goto out_force;
8831da177e4SLinus Torvalds 		/* This is an open(2) */
8841d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
8854e0641a7STrond Myklebust 				!(server->flags & NFS_MOUNT_NOCTO) &&
8864e0641a7STrond Myklebust 				(S_ISREG(inode->i_mode) ||
8874e0641a7STrond Myklebust 				 S_ISDIR(inode->i_mode)))
8881da177e4SLinus Torvalds 			goto out_force;
8894f48af45STrond Myklebust 		return 0;
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
8921da177e4SLinus Torvalds out_force:
8931da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds /*
8971da177e4SLinus Torvalds  * We judge how long we want to trust negative
8981da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
8991da177e4SLinus Torvalds  *
9001da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
9011da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
9021da177e4SLinus Torvalds  */
9031da177e4SLinus Torvalds static inline
9041da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
9051da177e4SLinus Torvalds 		       struct nameidata *nd)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
9081d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
9091da177e4SLinus Torvalds 		return 0;
9104eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
9114eec952eSTrond Myklebust 		return 1;
9121da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds /*
9161da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
9171da177e4SLinus Torvalds  * and we should check whether we can really trust that
9181da177e4SLinus Torvalds  * lookup.
9191da177e4SLinus Torvalds  *
9201da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
9211da177e4SLinus Torvalds  * we have an inode!
9221da177e4SLinus Torvalds  *
9231da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
9241da177e4SLinus Torvalds  * cached dentry and do a new lookup.
9251da177e4SLinus Torvalds  */
9261da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	struct inode *dir;
9291da177e4SLinus Torvalds 	struct inode *inode;
9301da177e4SLinus Torvalds 	struct dentry *parent;
931e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
932e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
9331da177e4SLinus Torvalds 	int error;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	parent = dget_parent(dentry);
9361da177e4SLinus Torvalds 	dir = parent->d_inode;
93791d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
9381da177e4SLinus Torvalds 	inode = dentry->d_inode;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	if (!inode) {
9411da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
9421da177e4SLinus Torvalds 			goto out_bad;
9431da177e4SLinus Torvalds 		goto out_valid;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
9471e7cb3dcSChuck Lever 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
9483110ff80SHarvey Harrison 				__func__, dentry->d_parent->d_name.name,
9491e7cb3dcSChuck Lever 				dentry->d_name.name);
9501da177e4SLinus Torvalds 		goto out_bad;
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
95315860ab1STrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
95415860ab1STrond Myklebust 		goto out_set_verifier;
95515860ab1STrond Myklebust 
9561da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
957a12802caSTrond Myklebust 	if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
9581da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
9591da177e4SLinus Torvalds 			goto out_zap_parent;
9601da177e4SLinus Torvalds 		goto out_valid;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (NFS_STALE(inode))
9641da177e4SLinus Torvalds 		goto out_bad;
9651da177e4SLinus Torvalds 
966e1fb4d05STrond Myklebust 	error = -ENOMEM;
967e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
968e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
969e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
970e1fb4d05STrond Myklebust 		goto out_error;
971e1fb4d05STrond Myklebust 
972e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
9731da177e4SLinus Torvalds 	if (error)
9741da177e4SLinus Torvalds 		goto out_bad;
975e1fb4d05STrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
9761da177e4SLinus Torvalds 		goto out_bad;
977e1fb4d05STrond Myklebust 	if ((error = nfs_refresh_inode(inode, fattr)) != 0)
9781da177e4SLinus Torvalds 		goto out_bad;
9791da177e4SLinus Torvalds 
980e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
981e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
98215860ab1STrond Myklebust out_set_verifier:
983cf8ba45eSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
9841da177e4SLinus Torvalds  out_valid:
9851da177e4SLinus Torvalds 	dput(parent);
9861e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
9873110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
9881e7cb3dcSChuck Lever 			dentry->d_name.name);
9891da177e4SLinus Torvalds 	return 1;
9901da177e4SLinus Torvalds out_zap_parent:
9911da177e4SLinus Torvalds 	nfs_zap_caches(dir);
9921da177e4SLinus Torvalds  out_bad:
993a1643a92STrond Myklebust 	nfs_mark_for_revalidate(dir);
9941da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
9951da177e4SLinus Torvalds 		/* Purge readdir caches. */
9961da177e4SLinus Torvalds 		nfs_zap_caches(inode);
9971da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
9981da177e4SLinus Torvalds 		if (have_submounts(dentry))
9991da177e4SLinus Torvalds 			goto out_valid;
1000d9e80b7dSAl Viro 		if (dentry->d_flags & DCACHE_DISCONNECTED)
1001d9e80b7dSAl Viro 			goto out_valid;
10021da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 	d_drop(dentry);
1005e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1006e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
10071da177e4SLinus Torvalds 	dput(parent);
10081e7cb3dcSChuck Lever 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
10093110ff80SHarvey Harrison 			__func__, dentry->d_parent->d_name.name,
10101e7cb3dcSChuck Lever 			dentry->d_name.name);
10111da177e4SLinus Torvalds 	return 0;
1012e1fb4d05STrond Myklebust out_error:
1013e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1014e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
1015e1fb4d05STrond Myklebust 	dput(parent);
1016e1fb4d05STrond Myklebust 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1017e1fb4d05STrond Myklebust 			__func__, dentry->d_parent->d_name.name,
1018e1fb4d05STrond Myklebust 			dentry->d_name.name, error);
1019e1fb4d05STrond Myklebust 	return error;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /*
10231da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
10241da177e4SLinus Torvalds  */
10251da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
10281da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
10291da177e4SLinus Torvalds 		dentry->d_flags);
10301da177e4SLinus Torvalds 
103177f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
103277f11192STrond Myklebust 	if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
103377f11192STrond Myklebust 		return 1;
103477f11192STrond Myklebust 
10351da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
10361da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
10371da177e4SLinus Torvalds 		return 1;
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
10401da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
10411da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
10421da177e4SLinus Torvalds 		return 1;
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 	return 0;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
10481b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
10491b83d707STrond Myklebust {
10501b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
10511b83d707STrond Myklebust 	if (inode->i_nlink > 0)
10521b83d707STrond Myklebust 		drop_nlink(inode);
10531b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
10541b83d707STrond Myklebust }
10551b83d707STrond Myklebust 
10561da177e4SLinus Torvalds /*
10571da177e4SLinus Torvalds  * Called when the dentry loses inode.
10581da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
10591da177e4SLinus Torvalds  */
10601da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
10611da177e4SLinus Torvalds {
106283672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
106383672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
106483672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
106583672d39SNeil Brown 
10661da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
10679a53c3a7SDave Hansen 		drop_nlink(inode);
1068e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	iput(inode);
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
1073f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
10741da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
10751da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
10761da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
10771da177e4SLinus Torvalds };
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	struct dentry *res;
1082565277f6STrond Myklebust 	struct dentry *parent;
10831da177e4SLinus Torvalds 	struct inode *inode = NULL;
1084e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1085e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
10861da177e4SLinus Torvalds 	int error;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
10891da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
109091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
10931da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
10941da177e4SLinus Torvalds 		goto out;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
10971da177e4SLinus Torvalds 
1098fd684071STrond Myklebust 	/*
1099fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1100fd684071STrond Myklebust 	 * but don't hash the dentry.
1101fd684071STrond Myklebust 	 */
1102fd684071STrond Myklebust 	if (nfs_is_exclusive_create(dir, nd)) {
1103fd684071STrond Myklebust 		d_instantiate(dentry, NULL);
1104fd684071STrond Myklebust 		res = NULL;
1105fc0f684cSTrond Myklebust 		goto out;
1106fd684071STrond Myklebust 	}
11071da177e4SLinus Torvalds 
1108e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1109e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1110e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1111e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1112e1fb4d05STrond Myklebust 		goto out;
1113e1fb4d05STrond Myklebust 
1114565277f6STrond Myklebust 	parent = dentry->d_parent;
1115565277f6STrond Myklebust 	/* Protect against concurrent sillydeletes */
1116565277f6STrond Myklebust 	nfs_block_sillyrename(parent);
1117e1fb4d05STrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
11181da177e4SLinus Torvalds 	if (error == -ENOENT)
11191da177e4SLinus Torvalds 		goto no_entry;
11201da177e4SLinus Torvalds 	if (error < 0) {
11211da177e4SLinus Torvalds 		res = ERR_PTR(error);
1122565277f6STrond Myklebust 		goto out_unblock_sillyrename;
11231da177e4SLinus Torvalds 	}
1124e1fb4d05STrond Myklebust 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
112503f28e3aSTrond Myklebust 	res = (struct dentry *)inode;
112603f28e3aSTrond Myklebust 	if (IS_ERR(res))
1127565277f6STrond Myklebust 		goto out_unblock_sillyrename;
112854ceac45SDavid Howells 
11291da177e4SLinus Torvalds no_entry:
113054ceac45SDavid Howells 	res = d_materialise_unique(dentry, inode);
11319eaef27bSTrond Myklebust 	if (res != NULL) {
11329eaef27bSTrond Myklebust 		if (IS_ERR(res))
1133565277f6STrond Myklebust 			goto out_unblock_sillyrename;
11341da177e4SLinus Torvalds 		dentry = res;
11359eaef27bSTrond Myklebust 	}
11361da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1137565277f6STrond Myklebust out_unblock_sillyrename:
1138565277f6STrond Myklebust 	nfs_unblock_sillyrename(parent);
11391da177e4SLinus Torvalds out:
1140e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1141e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
11421da177e4SLinus Torvalds 	return res;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
11461da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
11471da177e4SLinus Torvalds 
1148f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
11491da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
11501da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
11511da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
11521da177e4SLinus Torvalds };
11531da177e4SLinus Torvalds 
11541d6757fbSTrond Myklebust /*
11551d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
11561d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
11571d6757fbSTrond Myklebust  */
11585584c306STrond Myklebust static int is_atomic_open(struct nameidata *nd)
11591da177e4SLinus Torvalds {
11601d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
11611da177e4SLinus Torvalds 		return 0;
11621da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
11631da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
11641da177e4SLinus Torvalds 		return 0;
11651da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
11662c463e95SDave Hansen 	if (__mnt_is_readonly(nd->path.mnt) &&
11672c463e95SDave Hansen 	    (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
11681da177e4SLinus Torvalds 		return 0;
11691da177e4SLinus Torvalds 	return 1;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
1172cd9a1c0eSTrond Myklebust static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1173cd9a1c0eSTrond Myklebust {
1174cd9a1c0eSTrond Myklebust 	struct path path = {
1175cd9a1c0eSTrond Myklebust 		.mnt = nd->path.mnt,
1176cd9a1c0eSTrond Myklebust 		.dentry = dentry,
1177cd9a1c0eSTrond Myklebust 	};
1178cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1179cd9a1c0eSTrond Myklebust 	struct rpc_cred *cred;
1180cd9a1c0eSTrond Myklebust 	fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1181cd9a1c0eSTrond Myklebust 
1182cd9a1c0eSTrond Myklebust 	cred = rpc_lookup_cred();
1183cd9a1c0eSTrond Myklebust 	if (IS_ERR(cred))
1184cd9a1c0eSTrond Myklebust 		return ERR_CAST(cred);
1185cd9a1c0eSTrond Myklebust 	ctx = alloc_nfs_open_context(&path, cred, fmode);
1186cd9a1c0eSTrond Myklebust 	put_rpccred(cred);
1187cd9a1c0eSTrond Myklebust 	if (ctx == NULL)
1188cd9a1c0eSTrond Myklebust 		return ERR_PTR(-ENOMEM);
1189cd9a1c0eSTrond Myklebust 	return ctx;
1190cd9a1c0eSTrond Myklebust }
1191cd9a1c0eSTrond Myklebust 
1192cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1193cd9a1c0eSTrond Myklebust {
1194cd9a1c0eSTrond Myklebust 	nfs_fscache_set_inode_cookie(inode, filp);
1195cd9a1c0eSTrond Myklebust 	return 0;
1196cd9a1c0eSTrond Myklebust }
1197cd9a1c0eSTrond Myklebust 
1198cd9a1c0eSTrond Myklebust static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1199cd9a1c0eSTrond Myklebust {
1200cd9a1c0eSTrond Myklebust 	struct file *filp;
1201cd9a1c0eSTrond Myklebust 	int ret = 0;
1202cd9a1c0eSTrond Myklebust 
1203cd9a1c0eSTrond Myklebust 	/* If the open_intent is for execute, we have an extra check to make */
1204cd9a1c0eSTrond Myklebust 	if (ctx->mode & FMODE_EXEC) {
1205cd9a1c0eSTrond Myklebust 		ret = nfs_may_open(ctx->path.dentry->d_inode,
1206cd9a1c0eSTrond Myklebust 				ctx->cred,
1207cd9a1c0eSTrond Myklebust 				nd->intent.open.flags);
1208cd9a1c0eSTrond Myklebust 		if (ret < 0)
1209cd9a1c0eSTrond Myklebust 			goto out;
1210cd9a1c0eSTrond Myklebust 	}
1211cd9a1c0eSTrond Myklebust 	filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1212cd9a1c0eSTrond Myklebust 	if (IS_ERR(filp))
1213cd9a1c0eSTrond Myklebust 		ret = PTR_ERR(filp);
1214cd9a1c0eSTrond Myklebust 	else
1215cd9a1c0eSTrond Myklebust 		nfs_file_set_open_context(filp, ctx);
1216cd9a1c0eSTrond Myklebust out:
1217cd9a1c0eSTrond Myklebust 	put_nfs_open_context(ctx);
1218cd9a1c0eSTrond Myklebust 	return ret;
1219cd9a1c0eSTrond Myklebust }
1220cd9a1c0eSTrond Myklebust 
12211da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
12221da177e4SLinus Torvalds {
1223cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
1224cd9a1c0eSTrond Myklebust 	struct iattr attr;
12251da177e4SLinus Torvalds 	struct dentry *res = NULL;
1226f46e0bd3STrond Myklebust 	struct inode *inode;
1227cd9a1c0eSTrond Myklebust 	int open_flags;
1228898f635cSTrond Myklebust 	int err;
12291da177e4SLinus Torvalds 
12301e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
12311e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
12321e7cb3dcSChuck Lever 
12331da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
12345584c306STrond Myklebust 	if (!is_atomic_open(nd))
12351da177e4SLinus Torvalds 		goto no_open;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
12381da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
12391da177e4SLinus Torvalds 		goto out;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
12421da177e4SLinus Torvalds 
1243d4d9cdcbSTrond Myklebust 	/* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1244d4d9cdcbSTrond Myklebust 	 * the dentry. */
12453516586aSAl Viro 	if (nd->flags & LOOKUP_EXCL) {
1246d4d9cdcbSTrond Myklebust 		d_instantiate(dentry, NULL);
124702a913a7STrond Myklebust 		goto out;
124802a913a7STrond Myklebust 	}
12491da177e4SLinus Torvalds 
1250cd9a1c0eSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1251cd9a1c0eSTrond Myklebust 	res = ERR_CAST(ctx);
1252cd9a1c0eSTrond Myklebust 	if (IS_ERR(ctx))
1253cd9a1c0eSTrond Myklebust 		goto out;
1254cd9a1c0eSTrond Myklebust 
1255cd9a1c0eSTrond Myklebust 	open_flags = nd->intent.open.flags;
1256cd9a1c0eSTrond Myklebust 	if (nd->flags & LOOKUP_CREATE) {
1257cd9a1c0eSTrond Myklebust 		attr.ia_mode = nd->intent.open.create_mode;
1258cd9a1c0eSTrond Myklebust 		attr.ia_valid = ATTR_MODE;
1259cd9a1c0eSTrond Myklebust 		if (!IS_POSIXACL(dir))
1260cd9a1c0eSTrond Myklebust 			attr.ia_mode &= ~current_umask();
1261cd9a1c0eSTrond Myklebust 	} else {
1262898f635cSTrond Myklebust 		open_flags &= ~(O_EXCL | O_CREAT);
1263cd9a1c0eSTrond Myklebust 		attr.ia_valid = 0;
1264cd9a1c0eSTrond Myklebust 	}
1265cd9a1c0eSTrond Myklebust 
12661da177e4SLinus Torvalds 	/* Open the file on the server */
1267f46e0bd3STrond Myklebust 	nfs_block_sillyrename(dentry->d_parent);
12682b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1269f46e0bd3STrond Myklebust 	if (IS_ERR(inode)) {
1270f46e0bd3STrond Myklebust 		nfs_unblock_sillyrename(dentry->d_parent);
1271cd9a1c0eSTrond Myklebust 		put_nfs_open_context(ctx);
1272f46e0bd3STrond Myklebust 		switch (PTR_ERR(inode)) {
12731da177e4SLinus Torvalds 			/* Make a negative dentry */
12741da177e4SLinus Torvalds 			case -ENOENT:
1275f46e0bd3STrond Myklebust 				d_add(dentry, NULL);
127602a913a7STrond Myklebust 				res = NULL;
127702a913a7STrond Myklebust 				goto out;
12781da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
127980e60639STrond Myklebust 			case -EISDIR:
12806f926b5bSTrond Myklebust 			case -ENOTDIR:
12816f926b5bSTrond Myklebust 				goto no_open;
12821da177e4SLinus Torvalds 			case -ELOOP:
12831da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
12841da177e4SLinus Torvalds 					goto no_open;
12851da177e4SLinus Torvalds 			/* case -EINVAL: */
12861da177e4SLinus Torvalds 			default:
1287f46e0bd3STrond Myklebust 				res = ERR_CAST(inode);
12881da177e4SLinus Torvalds 				goto out;
12891da177e4SLinus Torvalds 		}
1290cd9a1c0eSTrond Myklebust 	}
1291f46e0bd3STrond Myklebust 	res = d_add_unique(dentry, inode);
1292898f635cSTrond Myklebust 	nfs_unblock_sillyrename(dentry->d_parent);
1293f46e0bd3STrond Myklebust 	if (res != NULL) {
1294f46e0bd3STrond Myklebust 		dput(ctx->path.dentry);
1295f46e0bd3STrond Myklebust 		ctx->path.dentry = dget(res);
12961da177e4SLinus Torvalds 		dentry = res;
1297f46e0bd3STrond Myklebust 	}
1298898f635cSTrond Myklebust 	err = nfs_intent_set_file(nd, ctx);
1299898f635cSTrond Myklebust 	if (err < 0) {
1300898f635cSTrond Myklebust 		if (res != NULL)
1301898f635cSTrond Myklebust 			dput(res);
1302898f635cSTrond Myklebust 		return ERR_PTR(err);
1303898f635cSTrond Myklebust 	}
13041da177e4SLinus Torvalds out:
1305f46e0bd3STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
13061da177e4SLinus Torvalds 	return res;
13071da177e4SLinus Torvalds no_open:
13081da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
13121da177e4SLinus Torvalds {
13131da177e4SLinus Torvalds 	struct dentry *parent = NULL;
13141da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
13151da177e4SLinus Torvalds 	struct inode *dir;
1316b8d4caddSTrond Myklebust 	struct nfs_open_context *ctx;
13171da177e4SLinus Torvalds 	int openflags, ret = 0;
13181da177e4SLinus Torvalds 
13191f063d2cSTrond Myklebust 	if (!is_atomic_open(nd) || d_mountpoint(dentry))
13205584c306STrond Myklebust 		goto no_open;
13212b484297STrond Myklebust 
13221da177e4SLinus Torvalds 	parent = dget_parent(dentry);
13231da177e4SLinus Torvalds 	dir = parent->d_inode;
13242b484297STrond Myklebust 
13251da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
13261da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
13271da177e4SLinus Torvalds 	 */
1328216d5d06STrond Myklebust 	if (inode == NULL) {
1329216d5d06STrond Myklebust 		if (!nfs_neg_need_reval(dir, dentry, nd))
1330216d5d06STrond Myklebust 			ret = 1;
13311da177e4SLinus Torvalds 		goto out;
1332216d5d06STrond Myklebust 	}
1333216d5d06STrond Myklebust 
13341da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
13351da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
13365584c306STrond Myklebust 		goto no_open_dput;
13371da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
13381da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
13391da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
13405584c306STrond Myklebust 		goto no_open_dput;
13411da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
13420a377cffSTrond Myklebust 	openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
13431da177e4SLinus Torvalds 
1344b8d4caddSTrond Myklebust 	ctx = nameidata_to_nfs_open_context(dentry, nd);
1345b8d4caddSTrond Myklebust 	ret = PTR_ERR(ctx);
1346b8d4caddSTrond Myklebust 	if (IS_ERR(ctx))
1347b8d4caddSTrond Myklebust 		goto out;
13481da177e4SLinus Torvalds 	/*
13491b1dcc1bSJes Sorensen 	 * Note: we're not holding inode->i_mutex and so may be racing with
13501da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
13511da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
13521da177e4SLinus Torvalds 	 */
13532b484297STrond Myklebust 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
1354535918f1STrond Myklebust 	if (IS_ERR(inode)) {
1355535918f1STrond Myklebust 		ret = PTR_ERR(inode);
1356535918f1STrond Myklebust 		switch (ret) {
1357535918f1STrond Myklebust 		case -EPERM:
1358535918f1STrond Myklebust 		case -EACCES:
1359535918f1STrond Myklebust 		case -EDQUOT:
1360535918f1STrond Myklebust 		case -ENOSPC:
1361535918f1STrond Myklebust 		case -EROFS:
1362535918f1STrond Myklebust 			goto out_put_ctx;
1363535918f1STrond Myklebust 		default:
1364535918f1STrond Myklebust 			goto out_drop;
1365535918f1STrond Myklebust 		}
1366535918f1STrond Myklebust 	}
1367535918f1STrond Myklebust 	iput(inode);
1368898f635cSTrond Myklebust 	if (inode != dentry->d_inode)
1369535918f1STrond Myklebust 		goto out_drop;
1370898f635cSTrond Myklebust 
1371898f635cSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1372898f635cSTrond Myklebust 	ret = nfs_intent_set_file(nd, ctx);
1373898f635cSTrond Myklebust 	if (ret >= 0)
1374898f635cSTrond Myklebust 		ret = 1;
13751da177e4SLinus Torvalds out:
13761da177e4SLinus Torvalds 	dput(parent);
13771da177e4SLinus Torvalds 	return ret;
1378535918f1STrond Myklebust out_drop:
1379535918f1STrond Myklebust 	d_drop(dentry);
1380535918f1STrond Myklebust 	ret = 0;
1381535918f1STrond Myklebust out_put_ctx:
1382535918f1STrond Myklebust 	put_nfs_open_context(ctx);
1383535918f1STrond Myklebust 	goto out;
1384535918f1STrond Myklebust 
13855584c306STrond Myklebust no_open_dput:
13861da177e4SLinus Torvalds 	dput(parent);
13875584c306STrond Myklebust no_open:
13881da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
13891da177e4SLinus Torvalds }
1390c0204fd2STrond Myklebust 
1391c0204fd2STrond Myklebust static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1392c0204fd2STrond Myklebust 		struct nameidata *nd)
1393c0204fd2STrond Myklebust {
1394c0204fd2STrond Myklebust 	struct nfs_open_context *ctx = NULL;
1395c0204fd2STrond Myklebust 	struct iattr attr;
1396c0204fd2STrond Myklebust 	int error;
1397c0204fd2STrond Myklebust 	int open_flags = 0;
1398c0204fd2STrond Myklebust 
1399c0204fd2STrond Myklebust 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1400c0204fd2STrond Myklebust 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1401c0204fd2STrond Myklebust 
1402c0204fd2STrond Myklebust 	attr.ia_mode = mode;
1403c0204fd2STrond Myklebust 	attr.ia_valid = ATTR_MODE;
1404c0204fd2STrond Myklebust 
1405c0204fd2STrond Myklebust 	if ((nd->flags & LOOKUP_CREATE) != 0) {
1406c0204fd2STrond Myklebust 		open_flags = nd->intent.open.flags;
1407c0204fd2STrond Myklebust 
1408c0204fd2STrond Myklebust 		ctx = nameidata_to_nfs_open_context(dentry, nd);
1409c0204fd2STrond Myklebust 		error = PTR_ERR(ctx);
1410c0204fd2STrond Myklebust 		if (IS_ERR(ctx))
1411898f635cSTrond Myklebust 			goto out_err_drop;
1412c0204fd2STrond Myklebust 	}
1413c0204fd2STrond Myklebust 
1414c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1415c0204fd2STrond Myklebust 	if (error != 0)
1416c0204fd2STrond Myklebust 		goto out_put_ctx;
1417898f635cSTrond Myklebust 	if (ctx != NULL) {
1418898f635cSTrond Myklebust 		error = nfs_intent_set_file(nd, ctx);
1419898f635cSTrond Myklebust 		if (error < 0)
1420898f635cSTrond Myklebust 			goto out_err;
1421898f635cSTrond Myklebust 	}
1422c0204fd2STrond Myklebust 	return 0;
1423c0204fd2STrond Myklebust out_put_ctx:
1424c0204fd2STrond Myklebust 	if (ctx != NULL)
1425c0204fd2STrond Myklebust 		put_nfs_open_context(ctx);
1426898f635cSTrond Myklebust out_err_drop:
1427c0204fd2STrond Myklebust 	d_drop(dentry);
1428898f635cSTrond Myklebust out_err:
1429c0204fd2STrond Myklebust 	return error;
1430c0204fd2STrond Myklebust }
1431c0204fd2STrond Myklebust 
14321da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds /*
14351da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
14361da177e4SLinus Torvalds  */
14371da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
14381da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
14391da177e4SLinus Torvalds {
1440fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
1441fab728e1STrond Myklebust 	struct inode *dir = parent->d_inode;
14421da177e4SLinus Torvalds 	struct inode *inode;
14431da177e4SLinus Torvalds 	int error = -EACCES;
14441da177e4SLinus Torvalds 
1445fab728e1STrond Myklebust 	d_drop(dentry);
1446fab728e1STrond Myklebust 
14471da177e4SLinus Torvalds 	/* We may have been initialized further down */
14481da177e4SLinus Torvalds 	if (dentry->d_inode)
1449fab728e1STrond Myklebust 		goto out;
14501da177e4SLinus Torvalds 	if (fhandle->size == 0) {
14511da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
14521da177e4SLinus Torvalds 		if (error)
1453fab728e1STrond Myklebust 			goto out_error;
14541da177e4SLinus Torvalds 	}
14555724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14561da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
14571da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
14588fa5c000SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
14591da177e4SLinus Torvalds 		if (error < 0)
1460fab728e1STrond Myklebust 			goto out_error;
14611da177e4SLinus Torvalds 	}
14621da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
146303f28e3aSTrond Myklebust 	error = PTR_ERR(inode);
146403f28e3aSTrond Myklebust 	if (IS_ERR(inode))
1465fab728e1STrond Myklebust 		goto out_error;
1466fab728e1STrond Myklebust 	d_add(dentry, inode);
1467fab728e1STrond Myklebust out:
1468fab728e1STrond Myklebust 	dput(parent);
14691da177e4SLinus Torvalds 	return 0;
1470fab728e1STrond Myklebust out_error:
1471fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1472fab728e1STrond Myklebust 	dput(parent);
1473fab728e1STrond Myklebust 	return error;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds /*
14771da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
14781da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
14791da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
14801da177e4SLinus Torvalds  * reply path made it appear to have failed.
14811da177e4SLinus Torvalds  */
14821da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
14831da177e4SLinus Torvalds 		struct nameidata *nd)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	struct iattr attr;
14861da177e4SLinus Torvalds 	int error;
14871da177e4SLinus Torvalds 
14881e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
14891e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	attr.ia_mode = mode;
14921da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
14931da177e4SLinus Torvalds 
1494c0204fd2STrond Myklebust 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
14951da177e4SLinus Torvalds 	if (error != 0)
14961da177e4SLinus Torvalds 		goto out_err;
14971da177e4SLinus Torvalds 	return 0;
14981da177e4SLinus Torvalds out_err:
14991da177e4SLinus Torvalds 	d_drop(dentry);
15001da177e4SLinus Torvalds 	return error;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds /*
15041da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15051da177e4SLinus Torvalds  */
15061da177e4SLinus Torvalds static int
15071da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	struct iattr attr;
15101da177e4SLinus Torvalds 	int status;
15111da177e4SLinus Torvalds 
15121e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
15131e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
15161da177e4SLinus Torvalds 		return -EINVAL;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	attr.ia_mode = mode;
15191da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
15221da177e4SLinus Torvalds 	if (status != 0)
15231da177e4SLinus Torvalds 		goto out_err;
15241da177e4SLinus Torvalds 	return 0;
15251da177e4SLinus Torvalds out_err:
15261da177e4SLinus Torvalds 	d_drop(dentry);
15271da177e4SLinus Torvalds 	return status;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /*
15311da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
15321da177e4SLinus Torvalds  */
15331da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
15341da177e4SLinus Torvalds {
15351da177e4SLinus Torvalds 	struct iattr attr;
15361da177e4SLinus Torvalds 	int error;
15371da177e4SLinus Torvalds 
15381e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
15391e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
15421da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
15451da177e4SLinus Torvalds 	if (error != 0)
15461da177e4SLinus Torvalds 		goto out_err;
15471da177e4SLinus Torvalds 	return 0;
15481da177e4SLinus Torvalds out_err:
15491da177e4SLinus Torvalds 	d_drop(dentry);
15501da177e4SLinus Torvalds 	return error;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
1553d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1554d45b9d8bSTrond Myklebust {
1555d45b9d8bSTrond Myklebust 	if (dentry->d_inode != NULL && !d_unhashed(dentry))
1556d45b9d8bSTrond Myklebust 		d_delete(dentry);
1557d45b9d8bSTrond Myklebust }
1558d45b9d8bSTrond Myklebust 
15591da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
15601da177e4SLinus Torvalds {
15611da177e4SLinus Torvalds 	int error;
15621da177e4SLinus Torvalds 
15631e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
15641e7cb3dcSChuck Lever 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
15671da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
15681da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
1569ce71ec36SDave Hansen 		clear_nlink(dentry->d_inode);
1570d45b9d8bSTrond Myklebust 	else if (error == -ENOENT)
1571d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	return error;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds /*
15771da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
15781da177e4SLinus Torvalds  * and after checking that the file has only one user.
15791da177e4SLinus Torvalds  *
15801da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
15811da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
15821da177e4SLinus Torvalds  */
15831da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
15841da177e4SLinus Torvalds {
15851da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
15861da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15871da177e4SLinus Torvalds 	int error = -EBUSY;
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
15901da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
15931da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
15941da177e4SLinus Torvalds 		error = 0;
15951da177e4SLinus Torvalds 		goto out;
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	if (inode != NULL) {
1599cae7a073STrond Myklebust 		nfs_inode_return_delegation(inode);
16001da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
16011da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
16021da177e4SLinus Torvalds 		if (error == 0)
16031b83d707STrond Myklebust 			nfs_drop_nlink(inode);
16045ba7cc48STrond Myklebust 		nfs_mark_for_revalidate(inode);
16051da177e4SLinus Torvalds 	} else
16061da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1607d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1608d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
16091da177e4SLinus Torvalds out:
16101da177e4SLinus Torvalds 	return error;
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
16141da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
16151da177e4SLinus Torvalds  *
16161da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
16171da177e4SLinus Torvalds  */
16181da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	int error;
16211da177e4SLinus Torvalds 	int need_rehash = 0;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
16241da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
16271da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
16281da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) > 1) {
16291da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
16301da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
1631ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
1632ccfeb506STrond Myklebust 		write_inode_now(dentry->d_inode, 0);
16331da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
16341da177e4SLinus Torvalds 		return error;
16351da177e4SLinus Torvalds 	}
16361da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
16371da177e4SLinus Torvalds 		__d_drop(dentry);
16381da177e4SLinus Torvalds 		need_rehash = 1;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
16411da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
16421da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1643d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
16441da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16451da177e4SLinus Torvalds 	} else if (need_rehash)
16461da177e4SLinus Torvalds 		d_rehash(dentry);
16471da177e4SLinus Torvalds 	return error;
16481da177e4SLinus Torvalds }
16491da177e4SLinus Torvalds 
1650873101b3SChuck Lever /*
1651873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1652873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1653873101b3SChuck Lever  * using prepare_write/commit_write.
1654873101b3SChuck Lever  *
1655873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1656873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1657873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1658873101b3SChuck Lever  * symlink request has completed on the server.
1659873101b3SChuck Lever  *
1660873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
1661873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1662873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
1663873101b3SChuck Lever  * and move the raw page into its mapping.
1664873101b3SChuck Lever  */
1665873101b3SChuck Lever static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
16661da177e4SLinus Torvalds {
1667873101b3SChuck Lever 	struct pagevec lru_pvec;
1668873101b3SChuck Lever 	struct page *page;
1669873101b3SChuck Lever 	char *kaddr;
16701da177e4SLinus Torvalds 	struct iattr attr;
1671873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
16721da177e4SLinus Torvalds 	int error;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
16751da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
16761da177e4SLinus Torvalds 
1677873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
1678873101b3SChuck Lever 		return -ENAMETOOLONG;
16791da177e4SLinus Torvalds 
1680873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1681873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
16821da177e4SLinus Torvalds 
168383d93f22SJeff Layton 	page = alloc_page(GFP_HIGHUSER);
168476566991STrond Myklebust 	if (!page)
1685873101b3SChuck Lever 		return -ENOMEM;
1686873101b3SChuck Lever 
1687873101b3SChuck Lever 	kaddr = kmap_atomic(page, KM_USER0);
1688873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
1689873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
1690873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1691873101b3SChuck Lever 	kunmap_atomic(kaddr, KM_USER0);
1692873101b3SChuck Lever 
169394a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1694873101b3SChuck Lever 	if (error != 0) {
1695873101b3SChuck Lever 		dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1696873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
1697873101b3SChuck Lever 			dentry->d_name.name, symname, error);
16981da177e4SLinus Torvalds 		d_drop(dentry);
1699873101b3SChuck Lever 		__free_page(page);
17001da177e4SLinus Torvalds 		return error;
17011da177e4SLinus Torvalds 	}
17021da177e4SLinus Torvalds 
1703873101b3SChuck Lever 	/*
1704873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
1705873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
1706873101b3SChuck Lever 	 */
1707873101b3SChuck Lever 	pagevec_init(&lru_pvec, 0);
1708873101b3SChuck Lever 	if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1709873101b3SChuck Lever 							GFP_KERNEL)) {
171039cf8a13SChuck Lever 		pagevec_add(&lru_pvec, page);
17114f98a2feSRik van Riel 		pagevec_lru_add_file(&lru_pvec);
1712873101b3SChuck Lever 		SetPageUptodate(page);
1713873101b3SChuck Lever 		unlock_page(page);
1714873101b3SChuck Lever 	} else
1715873101b3SChuck Lever 		__free_page(page);
1716873101b3SChuck Lever 
1717873101b3SChuck Lever 	return 0;
1718873101b3SChuck Lever }
1719873101b3SChuck Lever 
17201da177e4SLinus Torvalds static int
17211da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
17221da177e4SLinus Torvalds {
17231da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
17241da177e4SLinus Torvalds 	int error;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
17271da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
17281da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
17291da177e4SLinus Torvalds 
17309a3936aaSTrond Myklebust 	nfs_inode_return_delegation(inode);
17319a3936aaSTrond Myklebust 
17329697d234STrond Myklebust 	d_drop(dentry);
17331da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1734cf809556STrond Myklebust 	if (error == 0) {
1735cf809556STrond Myklebust 		atomic_inc(&inode->i_count);
17369697d234STrond Myklebust 		d_add(dentry, inode);
1737cf809556STrond Myklebust 	}
17381da177e4SLinus Torvalds 	return error;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /*
17421da177e4SLinus Torvalds  * RENAME
17431da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
17441da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
17451da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
17461da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
17471da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
17481da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
17491da177e4SLinus Torvalds  *
17501da177e4SLinus Torvalds  * FIXED.
17511da177e4SLinus Torvalds  *
17521da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
17531da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
17541da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
17551da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
17561da177e4SLinus Torvalds  * using the inode layer
17571da177e4SLinus Torvalds  *
17581da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
17591da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
17601da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
17611da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
17621da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
17631da177e4SLinus Torvalds  * the rename.
17641da177e4SLinus Torvalds  */
17651da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
17661da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
17671da177e4SLinus Torvalds {
17681da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
17691da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
17701da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
17711da177e4SLinus Torvalds 	int error = -EBUSY;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
17741da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
17751da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
17761da177e4SLinus Torvalds 		 atomic_read(&new_dentry->d_count));
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	/*
177928f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
178028f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
178128f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
178228f79a1aSMiklos Szeredi 	 * the new target.
17831da177e4SLinus Torvalds 	 */
178427226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
178527226104SMiklos Szeredi 		/*
178627226104SMiklos Szeredi 		 * To prevent any new references to the target during the
178727226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
178827226104SMiklos Szeredi 		 */
178927226104SMiklos Szeredi 		if (!d_unhashed(new_dentry)) {
179027226104SMiklos Szeredi 			d_drop(new_dentry);
179127226104SMiklos Szeredi 			rehash = new_dentry;
179227226104SMiklos Szeredi 		}
179327226104SMiklos Szeredi 
179427226104SMiklos Szeredi 		if (atomic_read(&new_dentry->d_count) > 2) {
17951da177e4SLinus Torvalds 			int err;
179627226104SMiklos Szeredi 
17971da177e4SLinus Torvalds 			/* copy the target dentry's name */
17981da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
17991da177e4SLinus Torvalds 					 &new_dentry->d_name);
18001da177e4SLinus Torvalds 			if (!dentry)
18011da177e4SLinus Torvalds 				goto out;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
18041da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
180524e93025SMiklos Szeredi 			if (err)
18061da177e4SLinus Torvalds 				goto out;
180724e93025SMiklos Szeredi 
180824e93025SMiklos Szeredi 			new_dentry = dentry;
180956335936SOGAWA Hirofumi 			rehash = NULL;
181024e93025SMiklos Szeredi 			new_inode = NULL;
1811b1e4adf4STrond Myklebust 		}
181227226104SMiklos Szeredi 	}
18131da177e4SLinus Torvalds 
1814cae7a073STrond Myklebust 	nfs_inode_return_delegation(old_inode);
1815b1e4adf4STrond Myklebust 	if (new_inode != NULL)
181624174119STrond Myklebust 		nfs_inode_return_delegation(new_inode);
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
18191da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
18205ba7cc48STrond Myklebust 	nfs_mark_for_revalidate(old_inode);
18211da177e4SLinus Torvalds out:
18221da177e4SLinus Torvalds 	if (rehash)
18231da177e4SLinus Torvalds 		d_rehash(rehash);
18241da177e4SLinus Torvalds 	if (!error) {
1825b1e4adf4STrond Myklebust 		if (new_inode != NULL)
1826b1e4adf4STrond Myklebust 			nfs_drop_nlink(new_inode);
18271da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
18288fb559f8SChuck Lever 		nfs_set_verifier(new_dentry,
18298fb559f8SChuck Lever 					nfs_save_change_attribute(new_dir));
1830d45b9d8bSTrond Myklebust 	} else if (error == -ENOENT)
1831d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(old_dentry);
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	/* new dentry created? */
18341da177e4SLinus Torvalds 	if (dentry)
18351da177e4SLinus Torvalds 		dput(dentry);
18361da177e4SLinus Torvalds 	return error;
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds 
1839cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
1840cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
1841cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
1842cfcea3e8STrond Myklebust 
18431c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
18441c3c07e9STrond Myklebust {
18451c3c07e9STrond Myklebust 	put_rpccred(entry->cred);
18461c3c07e9STrond Myklebust 	kfree(entry);
1847cfcea3e8STrond Myklebust 	smp_mb__before_atomic_dec();
1848cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
1849cfcea3e8STrond Myklebust 	smp_mb__after_atomic_dec();
18501c3c07e9STrond Myklebust }
18511c3c07e9STrond Myklebust 
18521a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
18531a81bb8aSTrond Myklebust {
18541a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
18551a81bb8aSTrond Myklebust 
18561a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
18571a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
18581a81bb8aSTrond Myklebust 		list_del(&cache->lru);
18591a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
18601a81bb8aSTrond Myklebust 	}
18611a81bb8aSTrond Myklebust }
18621a81bb8aSTrond Myklebust 
18637f8275d0SDave Chinner int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
1864979df72eSTrond Myklebust {
1865979df72eSTrond Myklebust 	LIST_HEAD(head);
1866aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
1867979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
1868979df72eSTrond Myklebust 
186961d5eb29STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
187061d5eb29STrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
18719c7e7e23STrond Myklebust 
1872a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
1873aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1874979df72eSTrond Myklebust 		struct inode *inode;
1875979df72eSTrond Myklebust 
1876979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
1877979df72eSTrond Myklebust 			break;
18789c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
1879979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
1880979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
1881979df72eSTrond Myklebust 			goto remove_lru_entry;
1882979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
1883979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
1884979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
1885979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
1886979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
1887979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
1888979df72eSTrond Myklebust 					&nfs_access_lru_list);
1889979df72eSTrond Myklebust 		else {
1890979df72eSTrond Myklebust remove_lru_entry:
1891979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
18929c7e7e23STrond Myklebust 			smp_mb__before_clear_bit();
1893979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
18949c7e7e23STrond Myklebust 			smp_mb__after_clear_bit();
1895979df72eSTrond Myklebust 		}
189659844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
1897979df72eSTrond Myklebust 	}
1898979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
18991a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
1900979df72eSTrond Myklebust 	return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1901979df72eSTrond Myklebust }
1902979df72eSTrond Myklebust 
19031a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
19041c3c07e9STrond Myklebust {
19051c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
19061a81bb8aSTrond Myklebust 	struct rb_node *n;
19071c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19081c3c07e9STrond Myklebust 
19091c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
19101c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
19111c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19121c3c07e9STrond Myklebust 		rb_erase(n, root_node);
19131a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
19141c3c07e9STrond Myklebust 	}
19151c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
19161c3c07e9STrond Myklebust }
19171c3c07e9STrond Myklebust 
19181c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
19191c3c07e9STrond Myklebust {
19201a81bb8aSTrond Myklebust 	LIST_HEAD(head);
19211a81bb8aSTrond Myklebust 
19221a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
19231a81bb8aSTrond Myklebust 		return;
1924cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
1925cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
19261a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
1927cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1928cfcea3e8STrond Myklebust 
19291c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
19301a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
19311a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
19321a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
19331a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
19341c3c07e9STrond Myklebust }
19351c3c07e9STrond Myklebust 
19361c3c07e9STrond Myklebust static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
19371c3c07e9STrond Myklebust {
19381c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
19391c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19401c3c07e9STrond Myklebust 
19411c3c07e9STrond Myklebust 	while (n != NULL) {
19421c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
19431c3c07e9STrond Myklebust 
19441c3c07e9STrond Myklebust 		if (cred < entry->cred)
19451c3c07e9STrond Myklebust 			n = n->rb_left;
19461c3c07e9STrond Myklebust 		else if (cred > entry->cred)
19471c3c07e9STrond Myklebust 			n = n->rb_right;
19481c3c07e9STrond Myklebust 		else
19491c3c07e9STrond Myklebust 			return entry;
19501c3c07e9STrond Myklebust 	}
19511c3c07e9STrond Myklebust 	return NULL;
19521c3c07e9STrond Myklebust }
19531c3c07e9STrond Myklebust 
1954af22f94aSTrond Myklebust static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
19551da177e4SLinus Torvalds {
195655296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
19571c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
19581c3c07e9STrond Myklebust 	int err = -ENOENT;
19591da177e4SLinus Torvalds 
19601c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
19611c3c07e9STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
19621c3c07e9STrond Myklebust 		goto out_zap;
19631c3c07e9STrond Myklebust 	cache = nfs_access_search_rbtree(inode, cred);
19641c3c07e9STrond Myklebust 	if (cache == NULL)
19651c3c07e9STrond Myklebust 		goto out;
1966b4d2314bSTrond Myklebust 	if (!nfs_have_delegated_attributes(inode) &&
196764672d55SPeter Staubach 	    !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
19681c3c07e9STrond Myklebust 		goto out_stale;
19691c3c07e9STrond Myklebust 	res->jiffies = cache->jiffies;
19701c3c07e9STrond Myklebust 	res->cred = cache->cred;
19711c3c07e9STrond Myklebust 	res->mask = cache->mask;
1972cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
19731c3c07e9STrond Myklebust 	err = 0;
19741c3c07e9STrond Myklebust out:
19751c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
19761c3c07e9STrond Myklebust 	return err;
19771c3c07e9STrond Myklebust out_stale:
19781c3c07e9STrond Myklebust 	rb_erase(&cache->rb_node, &nfsi->access_cache);
1979cfcea3e8STrond Myklebust 	list_del(&cache->lru);
19801c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
19811c3c07e9STrond Myklebust 	nfs_access_free_entry(cache);
19821da177e4SLinus Torvalds 	return -ENOENT;
19831c3c07e9STrond Myklebust out_zap:
19841a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
19851a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
19861c3c07e9STrond Myklebust 	return -ENOENT;
19871c3c07e9STrond Myklebust }
19881c3c07e9STrond Myklebust 
19891c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
19901c3c07e9STrond Myklebust {
1991cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1992cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
19931c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
19941c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
19951c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
19961c3c07e9STrond Myklebust 
19971c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
19981c3c07e9STrond Myklebust 	while (*p != NULL) {
19991c3c07e9STrond Myklebust 		parent = *p;
20001c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
20011c3c07e9STrond Myklebust 
20021c3c07e9STrond Myklebust 		if (set->cred < entry->cred)
20031c3c07e9STrond Myklebust 			p = &parent->rb_left;
20041c3c07e9STrond Myklebust 		else if (set->cred > entry->cred)
20051c3c07e9STrond Myklebust 			p = &parent->rb_right;
20061c3c07e9STrond Myklebust 		else
20071c3c07e9STrond Myklebust 			goto found;
20081c3c07e9STrond Myklebust 	}
20091c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
20101c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2011cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
20121c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20131c3c07e9STrond Myklebust 	return;
20141c3c07e9STrond Myklebust found:
20151c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2016cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2017cfcea3e8STrond Myklebust 	list_del(&entry->lru);
20181c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
20191c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
2022af22f94aSTrond Myklebust static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
20231da177e4SLinus Torvalds {
20241c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
20251c3c07e9STrond Myklebust 	if (cache == NULL)
20261c3c07e9STrond Myklebust 		return;
20271c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
20281da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
20291c3c07e9STrond Myklebust 	cache->cred = get_rpccred(set->cred);
20301da177e4SLinus Torvalds 	cache->mask = set->mask;
20311c3c07e9STrond Myklebust 
20321c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2033cfcea3e8STrond Myklebust 
2034cfcea3e8STrond Myklebust 	/* Update accounting */
2035cfcea3e8STrond Myklebust 	smp_mb__before_atomic_inc();
2036cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
2037cfcea3e8STrond Myklebust 	smp_mb__after_atomic_inc();
2038cfcea3e8STrond Myklebust 
2039cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
20401a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2041cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
20421a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
20431a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
20441a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2045cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2046cfcea3e8STrond Myklebust 	}
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	struct nfs_access_entry cache;
20521da177e4SLinus Torvalds 	int status;
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
20551da177e4SLinus Torvalds 	if (status == 0)
20561da177e4SLinus Torvalds 		goto out;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
20591da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
20601da177e4SLinus Torvalds 	cache.cred = cred;
20611da177e4SLinus Torvalds 	cache.jiffies = jiffies;
20621da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2063a71ee337SSuresh Jayaraman 	if (status != 0) {
2064a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2065a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2066a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2067a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2068a71ee337SSuresh Jayaraman 		}
20691da177e4SLinus Torvalds 		return status;
2070a71ee337SSuresh Jayaraman 	}
20711da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
20721da177e4SLinus Torvalds out:
2073e6305c43SAl Viro 	if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
20741da177e4SLinus Torvalds 		return 0;
20751da177e4SLinus Torvalds 	return -EACCES;
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
2078af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2079af22f94aSTrond Myklebust {
2080af22f94aSTrond Myklebust 	int mask = 0;
2081af22f94aSTrond Myklebust 
2082af22f94aSTrond Myklebust 	if (openflags & FMODE_READ)
2083af22f94aSTrond Myklebust 		mask |= MAY_READ;
2084af22f94aSTrond Myklebust 	if (openflags & FMODE_WRITE)
2085af22f94aSTrond Myklebust 		mask |= MAY_WRITE;
2086af22f94aSTrond Myklebust 	if (openflags & FMODE_EXEC)
2087af22f94aSTrond Myklebust 		mask |= MAY_EXEC;
2088af22f94aSTrond Myklebust 	return mask;
2089af22f94aSTrond Myklebust }
2090af22f94aSTrond Myklebust 
2091af22f94aSTrond Myklebust int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2092af22f94aSTrond Myklebust {
2093af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2094af22f94aSTrond Myklebust }
2095af22f94aSTrond Myklebust 
2096e6305c43SAl Viro int nfs_permission(struct inode *inode, int mask)
20971da177e4SLinus Torvalds {
20981da177e4SLinus Torvalds 	struct rpc_cred *cred;
20991da177e4SLinus Torvalds 	int res = 0;
21001da177e4SLinus Torvalds 
210191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
210291d5b470SChuck Lever 
2103e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
21041da177e4SLinus Torvalds 		goto out;
21051da177e4SLinus Torvalds 	/* Is this sys_access() ? */
21069cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
21071da177e4SLinus Torvalds 		goto force_lookup;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
21101da177e4SLinus Torvalds 		case S_IFLNK:
21111da177e4SLinus Torvalds 			goto out;
21121da177e4SLinus Torvalds 		case S_IFREG:
21131da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
21141da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
21157ee2cb7fSFrank Filz 					&& (mask & MAY_OPEN)
21167ee2cb7fSFrank Filz 					&& !(mask & MAY_EXEC))
21171da177e4SLinus Torvalds 				goto out;
21181da177e4SLinus Torvalds 			break;
21191da177e4SLinus Torvalds 		case S_IFDIR:
21201da177e4SLinus Torvalds 			/*
21211da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
21221da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
21231da177e4SLinus Torvalds 			 */
21241da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
21251da177e4SLinus Torvalds 				goto out;
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds force_lookup:
21291da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
21301da177e4SLinus Torvalds 		goto out_notsup;
21311da177e4SLinus Torvalds 
213298a8e323STrond Myklebust 	cred = rpc_lookup_cred();
21331da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
21341da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
21351da177e4SLinus Torvalds 		put_rpccred(cred);
21361da177e4SLinus Torvalds 	} else
21371da177e4SLinus Torvalds 		res = PTR_ERR(cred);
21381da177e4SLinus Torvalds out:
2139f696a365SMiklos Szeredi 	if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2140f696a365SMiklos Szeredi 		res = -EACCES;
2141f696a365SMiklos Szeredi 
21421e7cb3dcSChuck Lever 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
21431e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
21441da177e4SLinus Torvalds 	return res;
21451da177e4SLinus Torvalds out_notsup:
21461da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
21471da177e4SLinus Torvalds 	if (res == 0)
21481da177e4SLinus Torvalds 		res = generic_permission(inode, mask, NULL);
21491e7cb3dcSChuck Lever 	goto out;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds /*
21531da177e4SLinus Torvalds  * Local variables:
21541da177e4SLinus Torvalds  *  version-control: t
21551da177e4SLinus Torvalds  *  kept-new-versions: 5
21561da177e4SLinus Torvalds  * End:
21571da177e4SLinus Torvalds  */
2158