xref: /openbmc/linux/fs/nfs/dir.c (revision 762567b7c798afd08c22811ecfc66885a2b50f91)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  nfs directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
101da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
111da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
121da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
131da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
141da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
151da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
161da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
171da177e4SLinus Torvalds  *              layer (iput() and friends).
181da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21ddda8e0aSBryan Schumaker #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
311da177e4SLinus Torvalds #include <linux/nfs_fs.h>
321da177e4SLinus Torvalds #include <linux/nfs_mount.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
34873101b3SChuck Lever #include <linux/pagevec.h>
351da177e4SLinus Torvalds #include <linux/namei.h>
3654ceac45SDavid Howells #include <linux/mount.h>
37a0b8cab3SMel Gorman #include <linux/swap.h>
38e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "delegation.h"
4391d5b470SChuck Lever #include "iostat.h"
444c30d56eSAdrian Bunk #include "internal.h"
45cd9a1c0eSTrond Myklebust #include "fscache.h"
461da177e4SLinus Torvalds 
47f4ce1299STrond Myklebust #include "nfstrace.h"
48f4ce1299STrond Myklebust 
491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
571da177e4SLinus Torvalds 
584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
59f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
601da177e4SLinus Torvalds 	.read		= generic_read_dir,
6193a6ab7bSTrond Myklebust 	.iterate_shared	= nfs_readdir,
621da177e4SLinus Torvalds 	.open		= nfs_opendir,
63480c2006SBryan Schumaker 	.release	= nfs_closedir,
641da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
69d1bacf9eSBryan Schumaker };
70d1bacf9eSBryan Schumaker 
7193b8959aSTrond Myklebust static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir)
72480c2006SBryan Schumaker {
73311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
74480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
75480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76480c2006SBryan Schumaker 	if (ctx != NULL) {
778ef2ce3eSBryan Schumaker 		ctx->duped = 0;
78311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
79480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
808ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
81311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
821c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
831c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
841c341b77STrond Myklebust 			nfsi->cache_validity |= NFS_INO_INVALID_DATA |
851c341b77STrond Myklebust 				NFS_INO_REVAL_FORCED;
86311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
87311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
88480c2006SBryan Schumaker 		return ctx;
89480c2006SBryan Schumaker 	}
900c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
910c030806STrond Myklebust }
92480c2006SBryan Schumaker 
93311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
94480c2006SBryan Schumaker {
95311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
96311324adSTrond Myklebust 	list_del(&ctx->list);
97311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
98480c2006SBryan Schumaker 	kfree(ctx);
99480c2006SBryan Schumaker }
100480c2006SBryan Schumaker 
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds  * Open file
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds static int
1051da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1061da177e4SLinus Torvalds {
107480c2006SBryan Schumaker 	int res = 0;
108480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1091da177e4SLinus Torvalds 
1106de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
111cc0dd2d1SChuck Lever 
112cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1131e7cb3dcSChuck Lever 
11493b8959aSTrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode);
115480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
116480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
117480c2006SBryan Schumaker 		goto out;
118480c2006SBryan Schumaker 	}
119480c2006SBryan Schumaker 	filp->private_data = ctx;
120480c2006SBryan Schumaker out:
1211da177e4SLinus Torvalds 	return res;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124480c2006SBryan Schumaker static int
125480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
126480c2006SBryan Schumaker {
127a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
128480c2006SBryan Schumaker 	return 0;
129480c2006SBryan Schumaker }
130480c2006SBryan Schumaker 
131d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
132d1bacf9eSBryan Schumaker 	u64 cookie;
133d1bacf9eSBryan Schumaker 	u64 ino;
134a52a8a6aSTrond Myklebust 	const char *name;
135a52a8a6aSTrond Myklebust 	unsigned int name_len;
1360b26a0bfSTrond Myklebust 	unsigned char d_type;
137d1bacf9eSBryan Schumaker };
138d1bacf9eSBryan Schumaker 
139d1bacf9eSBryan Schumaker struct nfs_cache_array {
140d1bacf9eSBryan Schumaker 	u64 last_cookie;
141b1e21c97STrond Myklebust 	unsigned int size;
142b1e21c97STrond Myklebust 	unsigned char page_full : 1,
143*762567b7STrond Myklebust 		      page_is_eof : 1,
144*762567b7STrond Myklebust 		      cookies_are_ordered : 1;
1455601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
146d1bacf9eSBryan Schumaker };
147d1bacf9eSBryan Schumaker 
1486c981effSTrond Myklebust struct nfs_readdir_descriptor {
1491da177e4SLinus Torvalds 	struct file	*file;
1501da177e4SLinus Torvalds 	struct page	*page;
15123db8620SAl Viro 	struct dir_context *ctx;
1521f1d4aa4STrond Myklebust 	pgoff_t		page_index;
1532e7a4641STrond Myklebust 	u64		dir_cookie;
1540aded708STrond Myklebust 	u64		last_cookie;
1552e7a4641STrond Myklebust 	u64		dup_cookie;
156f0dd2136STrond Myklebust 	loff_t		current_index;
15759e356a9STrond Myklebust 	loff_t		prev_index;
158d1bacf9eSBryan Schumaker 
159b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
160a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1611f4eab7eSNeil Brown 	unsigned long	timestamp;
1624704f0e2STrond Myklebust 	unsigned long	gencount;
1632e7a4641STrond Myklebust 	unsigned long	attr_gencount;
164d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
1652e7a4641STrond Myklebust 	signed char duped;
166a7a3b1e9SBenjamin Coddington 	bool plus;
167a7a3b1e9SBenjamin Coddington 	bool eof;
1686c981effSTrond Myklebust };
1691da177e4SLinus Torvalds 
1701f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array)
1711f1d4aa4STrond Myklebust {
1721f1d4aa4STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
1731f1d4aa4STrond Myklebust }
1741f1d4aa4STrond Myklebust 
1751f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie)
1764b310319STrond Myklebust {
1774b310319STrond Myklebust 	struct nfs_cache_array *array;
1784b310319STrond Myklebust 
1794b310319STrond Myklebust 	array = kmap_atomic(page);
1801f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
1811f1d4aa4STrond Myklebust 	array->last_cookie = last_cookie;
182*762567b7STrond Myklebust 	array->cookies_are_ordered = 1;
1834b310319STrond Myklebust 	kunmap_atomic(array);
1844b310319STrond Myklebust }
1854b310319STrond Myklebust 
186d1bacf9eSBryan Schumaker /*
187d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
188d1bacf9eSBryan Schumaker  */
189d1bacf9eSBryan Schumaker static
19011de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
191d1bacf9eSBryan Schumaker {
19211de3b11STrond Myklebust 	struct nfs_cache_array *array;
193d1bacf9eSBryan Schumaker 	int i;
1948cd51a0cSTrond Myklebust 
1952b86ce2dSCong Wang 	array = kmap_atomic(page);
196d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
197a52a8a6aSTrond Myklebust 		kfree(array->array[i].name);
1981f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
1992b86ce2dSCong Wang 	kunmap_atomic(array);
200d1bacf9eSBryan Schumaker }
201d1bacf9eSBryan Schumaker 
202b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
203b1e21c97STrond Myklebust {
204b1e21c97STrond Myklebust 	array->page_is_eof = 1;
205b1e21c97STrond Myklebust 	array->page_full = 1;
206b1e21c97STrond Myklebust }
207b1e21c97STrond Myklebust 
208b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
209b1e21c97STrond Myklebust {
210b1e21c97STrond Myklebust 	return array->page_full;
211b1e21c97STrond Myklebust }
212b1e21c97STrond Myklebust 
213d1bacf9eSBryan Schumaker /*
214d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
215d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
216d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
217d1bacf9eSBryan Schumaker  */
218a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
219d1bacf9eSBryan Schumaker {
220a52a8a6aSTrond Myklebust 	const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
221a52a8a6aSTrond Myklebust 
22204e4bd1cSCatalin Marinas 	/*
22304e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
22404e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
22504e4bd1cSCatalin Marinas 	 */
226a52a8a6aSTrond Myklebust 	if (ret != NULL)
227a52a8a6aSTrond Myklebust 		kmemleak_not_leak(ret);
228a52a8a6aSTrond Myklebust 	return ret;
229d1bacf9eSBryan Schumaker }
230d1bacf9eSBryan Schumaker 
231b1e21c97STrond Myklebust /*
232b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
233b1e21c97STrond Myklebust  */
234b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
235b1e21c97STrond Myklebust {
236b1e21c97STrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
237b1e21c97STrond Myklebust 
238b1e21c97STrond Myklebust 	if (array->page_full)
239b1e21c97STrond Myklebust 		return -ENOSPC;
240b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size + 1];
241b1e21c97STrond Myklebust 	if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
242b1e21c97STrond Myklebust 		array->page_full = 1;
243b1e21c97STrond Myklebust 		return -ENOSPC;
244b1e21c97STrond Myklebust 	}
245b1e21c97STrond Myklebust 	return 0;
246b1e21c97STrond Myklebust }
247b1e21c97STrond Myklebust 
248d1bacf9eSBryan Schumaker static
249d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
250d1bacf9eSBryan Schumaker {
251a52a8a6aSTrond Myklebust 	struct nfs_cache_array *array;
2524a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
253a52a8a6aSTrond Myklebust 	const char *name;
2544a201d6eSTrond Myklebust 	int ret;
2554a201d6eSTrond Myklebust 
256a52a8a6aSTrond Myklebust 	name = nfs_readdir_copy_name(entry->name, entry->len);
257a52a8a6aSTrond Myklebust 	if (!name)
258a52a8a6aSTrond Myklebust 		return -ENOMEM;
259a52a8a6aSTrond Myklebust 
260a52a8a6aSTrond Myklebust 	array = kmap_atomic(page);
261b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
262a52a8a6aSTrond Myklebust 	if (ret) {
263a52a8a6aSTrond Myklebust 		kfree(name);
2643020093fSTrond Myklebust 		goto out;
265a52a8a6aSTrond Myklebust 	}
2663020093fSTrond Myklebust 
267b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
2684a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2694a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2700b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
271a52a8a6aSTrond Myklebust 	cache_entry->name_len = entry->len;
272a52a8a6aSTrond Myklebust 	cache_entry->name = name;
273d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
274*762567b7STrond Myklebust 	if (array->last_cookie <= cache_entry->cookie)
275*762567b7STrond Myklebust 		array->cookies_are_ordered = 0;
2768cd51a0cSTrond Myklebust 	array->size++;
27747c716cbSTrond Myklebust 	if (entry->eof != 0)
278b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
2794a201d6eSTrond Myklebust out:
280a52a8a6aSTrond Myklebust 	kunmap_atomic(array);
2814a201d6eSTrond Myklebust 	return ret;
282d1bacf9eSBryan Schumaker }
283d1bacf9eSBryan Schumaker 
2841f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
2851f1d4aa4STrond Myklebust 						pgoff_t index, u64 last_cookie)
2861f1d4aa4STrond Myklebust {
2871f1d4aa4STrond Myklebust 	struct page *page;
2881f1d4aa4STrond Myklebust 
2891f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
2901f1d4aa4STrond Myklebust 	if (page && !PageUptodate(page)) {
2911f1d4aa4STrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie);
2921f1d4aa4STrond Myklebust 		if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
2931f1d4aa4STrond Myklebust 			nfs_zap_mapping(mapping->host, mapping);
2941f1d4aa4STrond Myklebust 		SetPageUptodate(page);
2951f1d4aa4STrond Myklebust 	}
2961f1d4aa4STrond Myklebust 
2971f1d4aa4STrond Myklebust 	return page;
2981f1d4aa4STrond Myklebust }
2991f1d4aa4STrond Myklebust 
3001f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
3011f1d4aa4STrond Myklebust {
3021f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3031f1d4aa4STrond Myklebust 	u64 ret;
3041f1d4aa4STrond Myklebust 
3051f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3061f1d4aa4STrond Myklebust 	ret = array->last_cookie;
3071f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3081f1d4aa4STrond Myklebust 	return ret;
3091f1d4aa4STrond Myklebust }
3101f1d4aa4STrond Myklebust 
3111f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
3121f1d4aa4STrond Myklebust {
3131f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3141f1d4aa4STrond Myklebust 	bool ret;
3151f1d4aa4STrond Myklebust 
3161f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3171f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
3181f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3191f1d4aa4STrond Myklebust 	return ret;
3201f1d4aa4STrond Myklebust }
3211f1d4aa4STrond Myklebust 
322b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
323b1e21c97STrond Myklebust {
324b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
325b1e21c97STrond Myklebust 
326b1e21c97STrond Myklebust 	array = kmap_atomic(page);
327b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
328b1e21c97STrond Myklebust 	kunmap_atomic(array);
329b1e21c97STrond Myklebust }
330b1e21c97STrond Myklebust 
3313b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
3323b2a09f1STrond Myklebust {
3333b2a09f1STrond Myklebust 	unlock_page(page);
3343b2a09f1STrond Myklebust 	put_page(page);
3353b2a09f1STrond Myklebust }
3363b2a09f1STrond Myklebust 
3373b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
3383b2a09f1STrond Myklebust 					      pgoff_t index, u64 cookie)
3393b2a09f1STrond Myklebust {
3403b2a09f1STrond Myklebust 	struct page *page;
3413b2a09f1STrond Myklebust 
3423b2a09f1STrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
3433b2a09f1STrond Myklebust 	if (page) {
3443b2a09f1STrond Myklebust 		if (nfs_readdir_page_last_cookie(page) == cookie)
3453b2a09f1STrond Myklebust 			return page;
3463b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
3473b2a09f1STrond Myklebust 	}
3483b2a09f1STrond Myklebust 	return NULL;
3493b2a09f1STrond Myklebust }
3503b2a09f1STrond Myklebust 
35159e356a9STrond Myklebust static inline
35259e356a9STrond Myklebust int is_32bit_api(void)
35359e356a9STrond Myklebust {
35459e356a9STrond Myklebust #ifdef CONFIG_COMPAT
35559e356a9STrond Myklebust 	return in_compat_syscall();
35659e356a9STrond Myklebust #else
35759e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
35859e356a9STrond Myklebust #endif
35959e356a9STrond Myklebust }
36059e356a9STrond Myklebust 
36159e356a9STrond Myklebust static
36259e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
36359e356a9STrond Myklebust {
36459e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
36559e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
36659e356a9STrond Myklebust 		return false;
36759e356a9STrond Myklebust 	return true;
36859e356a9STrond Myklebust }
36959e356a9STrond Myklebust 
3706c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
3716c981effSTrond Myklebust 				      struct nfs_readdir_descriptor *desc)
372d1bacf9eSBryan Schumaker {
37323db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
374d1bacf9eSBryan Schumaker 	unsigned int index;
375d1bacf9eSBryan Schumaker 
376d1bacf9eSBryan Schumaker 	if (diff < 0)
377d1bacf9eSBryan Schumaker 		goto out_eof;
378d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
379b1e21c97STrond Myklebust 		if (array->page_is_eof)
380d1bacf9eSBryan Schumaker 			goto out_eof;
381d1bacf9eSBryan Schumaker 		return -EAGAIN;
382d1bacf9eSBryan Schumaker 	}
383d1bacf9eSBryan Schumaker 
384d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
3852e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
386d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
387d1bacf9eSBryan Schumaker 	return 0;
388d1bacf9eSBryan Schumaker out_eof:
3896089dd0dSThomas Meyer 	desc->eof = true;
390d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
391d1bacf9eSBryan Schumaker }
392d1bacf9eSBryan Schumaker 
3934db72b40SJeff Layton static bool
3944db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
3954db72b40SJeff Layton {
3964db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
3974db72b40SJeff Layton 		return false;
3984db72b40SJeff Layton 	smp_rmb();
3994db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
4004db72b40SJeff Layton }
4014db72b40SJeff Layton 
402*762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
403*762567b7STrond Myklebust 					      u64 cookie)
404*762567b7STrond Myklebust {
405*762567b7STrond Myklebust 	if (!array->cookies_are_ordered)
406*762567b7STrond Myklebust 		return true;
407*762567b7STrond Myklebust 	/* Optimisation for monotonically increasing cookies */
408*762567b7STrond Myklebust 	if (cookie >= array->last_cookie)
409*762567b7STrond Myklebust 		return false;
410*762567b7STrond Myklebust 	if (array->size && cookie < array->array[0].cookie)
411*762567b7STrond Myklebust 		return false;
412*762567b7STrond Myklebust 	return true;
413*762567b7STrond Myklebust }
414*762567b7STrond Myklebust 
4156c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
4166c981effSTrond Myklebust 					 struct nfs_readdir_descriptor *desc)
417d1bacf9eSBryan Schumaker {
418d1bacf9eSBryan Schumaker 	int i;
4198ef2ce3eSBryan Schumaker 	loff_t new_pos;
420d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
421d1bacf9eSBryan Schumaker 
422*762567b7STrond Myklebust 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
423*762567b7STrond Myklebust 		goto check_eof;
424*762567b7STrond Myklebust 
425d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
4262e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
427496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
4280c030806STrond Myklebust 
4298ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
4302e7a4641STrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount ||
4314db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
4322e7a4641STrond Myklebust 				desc->duped = 0;
4332e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
43459e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
4352e7a4641STrond Myklebust 				if (desc->duped > 0
4362e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
4370c030806STrond Myklebust 					if (printk_ratelimit()) {
4386de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
4390c030806STrond Myklebust 								"Please contact your server vendor.  "
440a52a8a6aSTrond Myklebust 								"The file: %s has duplicate cookie %llu\n",
441a52a8a6aSTrond Myklebust 								desc->file, array->array[i].name, desc->dir_cookie);
4420c030806STrond Myklebust 					}
4430c030806STrond Myklebust 					status = -ELOOP;
4440c030806STrond Myklebust 					goto out;
4450c030806STrond Myklebust 				}
4462e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
4472e7a4641STrond Myklebust 				desc->duped = -1;
4488ef2ce3eSBryan Schumaker 			}
44959e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
4502e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
45159e356a9STrond Myklebust 			else
45223db8620SAl Viro 				desc->ctx->pos = new_pos;
45359e356a9STrond Myklebust 			desc->prev_index = new_pos;
4548cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
45547c716cbSTrond Myklebust 			return 0;
4568cd51a0cSTrond Myklebust 		}
4578cd51a0cSTrond Myklebust 	}
458*762567b7STrond Myklebust check_eof:
459b1e21c97STrond Myklebust 	if (array->page_is_eof) {
460d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
4612e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
4626089dd0dSThomas Meyer 			desc->eof = true;
463d1bacf9eSBryan Schumaker 	}
4640c030806STrond Myklebust out:
465d1bacf9eSBryan Schumaker 	return status;
466d1bacf9eSBryan Schumaker }
467d1bacf9eSBryan Schumaker 
4686c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
469d1bacf9eSBryan Schumaker {
470d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
47147c716cbSTrond Myklebust 	int status;
472d1bacf9eSBryan Schumaker 
473ed09222dSTrond Myklebust 	array = kmap_atomic(desc->page);
474d1bacf9eSBryan Schumaker 
4752e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
476d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
477d1bacf9eSBryan Schumaker 	else
478d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
479d1bacf9eSBryan Schumaker 
48047c716cbSTrond Myklebust 	if (status == -EAGAIN) {
4810aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
482e47c085aSTrond Myklebust 		desc->current_index += array->size;
48347c716cbSTrond Myklebust 		desc->page_index++;
48447c716cbSTrond Myklebust 	}
485ed09222dSTrond Myklebust 	kunmap_atomic(array);
486d1bacf9eSBryan Schumaker 	return status;
487d1bacf9eSBryan Schumaker }
488d1bacf9eSBryan Schumaker 
489d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
49093b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
491b593c09fSTrond Myklebust 				  __be32 *verf, u64 cookie,
492b593c09fSTrond Myklebust 				  struct page **pages, size_t bufsize,
493b593c09fSTrond Myklebust 				  __be32 *verf_res)
494d1bacf9eSBryan Schumaker {
49582e22a5eSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
49682e22a5eSTrond Myklebust 	struct nfs_readdir_arg arg = {
49782e22a5eSTrond Myklebust 		.dentry = file_dentry(desc->file),
49882e22a5eSTrond Myklebust 		.cred = desc->file->f_cred,
499b593c09fSTrond Myklebust 		.verf = verf,
50082e22a5eSTrond Myklebust 		.cookie = cookie,
50182e22a5eSTrond Myklebust 		.pages = pages,
50282e22a5eSTrond Myklebust 		.page_len = bufsize,
50382e22a5eSTrond Myklebust 		.plus = desc->plus,
50482e22a5eSTrond Myklebust 	};
50582e22a5eSTrond Myklebust 	struct nfs_readdir_res res = {
50682e22a5eSTrond Myklebust 		.verf = verf_res,
50782e22a5eSTrond Myklebust 	};
5084704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
5091da177e4SLinus Torvalds 	int		error;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds  again:
5121da177e4SLinus Torvalds 	timestamp = jiffies;
5134704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
514a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
51582e22a5eSTrond Myklebust 	error = NFS_PROTO(inode)->readdir(&arg, &res);
5161da177e4SLinus Torvalds 	if (error < 0) {
5171da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
5181da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
5191da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
5203a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
52182e22a5eSTrond Myklebust 			desc->plus = arg.plus = false;
5221da177e4SLinus Torvalds 			goto again;
5231da177e4SLinus Torvalds 		}
5241da177e4SLinus Torvalds 		goto error;
5251da177e4SLinus Torvalds 	}
5261f4eab7eSNeil Brown 	desc->timestamp = timestamp;
5274704f0e2STrond Myklebust 	desc->gencount = gencount;
528d1bacf9eSBryan Schumaker error:
529d1bacf9eSBryan Schumaker 	return error;
530d1bacf9eSBryan Schumaker }
531d1bacf9eSBryan Schumaker 
5326c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
533573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
534d1bacf9eSBryan Schumaker {
53559e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
536573c4e1eSChuck Lever 	int error;
537d1bacf9eSBryan Schumaker 
53859e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
539573c4e1eSChuck Lever 	if (error)
540573c4e1eSChuck Lever 		return error;
541d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
542d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
543d1bacf9eSBryan Schumaker 	return 0;
544d1bacf9eSBryan Schumaker }
545d1bacf9eSBryan Schumaker 
546fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
547fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
548fa923369STrond Myklebust  */
549d39ab9deSBryan Schumaker static
550d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
551d39ab9deSBryan Schumaker {
552d8fdb47fSTrond Myklebust 	struct inode *inode;
553fa923369STrond Myklebust 	struct nfs_inode *nfsi;
554fa923369STrond Myklebust 
5552b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
5562b0143b5SDavid Howells 		return 0;
557fa923369STrond Myklebust 
558d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
559d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
560d8fdb47fSTrond Myklebust 		return 0;
561d8fdb47fSTrond Myklebust 
562d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
5637dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
564d39ab9deSBryan Schumaker 		return 0;
5657dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
5667dc72d5fSTrond Myklebust 		return 0;
5677dc72d5fSTrond Myklebust 	return 1;
568d39ab9deSBryan Schumaker }
569d39ab9deSBryan Schumaker 
570d39ab9deSBryan Schumaker static
57123db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
572d69ee9b8STrond Myklebust {
573d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
574d69ee9b8STrond Myklebust 		return false;
575d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
576d69ee9b8STrond Myklebust 		return true;
57723db8620SAl Viro 	if (ctx->pos == 0)
578d69ee9b8STrond Myklebust 		return true;
579d69ee9b8STrond Myklebust 	return false;
580d69ee9b8STrond Myklebust }
581d69ee9b8STrond Myklebust 
582d69ee9b8STrond Myklebust /*
58363519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
58463519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
585d69ee9b8STrond Myklebust  * directory.
586d69ee9b8STrond Myklebust  */
587d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
588d69ee9b8STrond Myklebust {
58963519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
59063519fbcSTrond Myklebust 
59163519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
59263519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
59363519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
594d69ee9b8STrond Myklebust }
595d69ee9b8STrond Myklebust 
596311324adSTrond Myklebust /*
597311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
598311324adSTrond Myklebust  *
599311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
600311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
601311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
602311324adSTrond Myklebust  * cache flush.
603311324adSTrond Myklebust  */
604311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
605311324adSTrond Myklebust {
60663519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
60763519fbcSTrond Myklebust 
60863519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
60963519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
61063519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
611227823d2SDai Ngo 		invalidate_mapping_pages(dir->i_mapping,
612227823d2SDai Ngo 			nfsi->page_index + 1, -1);
613311324adSTrond Myklebust 	}
614311324adSTrond Myklebust }
615311324adSTrond Myklebust 
616d69ee9b8STrond Myklebust static
617a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
618a1147b82STrond Myklebust 		unsigned long dir_verifier)
619d39ab9deSBryan Schumaker {
62026fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
6219ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
6224a201d6eSTrond Myklebust 	struct dentry *dentry;
6234a201d6eSTrond Myklebust 	struct dentry *alias;
624d39ab9deSBryan Schumaker 	struct inode *inode;
625aa9c2669SDavid Quigley 	int status;
626d39ab9deSBryan Schumaker 
627fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
628fa923369STrond Myklebust 		return;
6296c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
6306c441c25STrond Myklebust 		return;
63178d04af4STrond Myklebust 	if (filename.len == 0)
63278d04af4STrond Myklebust 		return;
63378d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
63478d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
63578d04af4STrond Myklebust 		return;
63678d04af4STrond Myklebust 	/* ...or '/' */
63778d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
63878d04af4STrond Myklebust 		return;
6394a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
6404a201d6eSTrond Myklebust 		if (filename.len == 1)
6414a201d6eSTrond Myklebust 			return;
6424a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
6434a201d6eSTrond Myklebust 			return;
6444a201d6eSTrond Myklebust 	}
6458387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
646d39ab9deSBryan Schumaker 
6474a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
6489ac3d3e8SAl Viro again:
6499ac3d3e8SAl Viro 	if (!dentry) {
6509ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
6519ac3d3e8SAl Viro 		if (IS_ERR(dentry))
6529ac3d3e8SAl Viro 			return;
6539ac3d3e8SAl Viro 	}
6549ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
6556c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
6566c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
6576c441c25STrond Myklebust 					&entry->fattr->fsid))
6586c441c25STrond Myklebust 			goto out;
659d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
6607dc72d5fSTrond Myklebust 			if (!entry->fh->size)
6617dc72d5fSTrond Myklebust 				goto out;
662a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
6632b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
664aa9c2669SDavid Quigley 			if (!status)
6652b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
666d39ab9deSBryan Schumaker 			goto out;
667d39ab9deSBryan Schumaker 		} else {
6685542aa2fSEric W. Biederman 			d_invalidate(dentry);
669d39ab9deSBryan Schumaker 			dput(dentry);
6709ac3d3e8SAl Viro 			dentry = NULL;
6719ac3d3e8SAl Viro 			goto again;
672d39ab9deSBryan Schumaker 		}
673d39ab9deSBryan Schumaker 	}
6747dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
6757dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
6767dc72d5fSTrond Myklebust 		goto out;
6777dc72d5fSTrond Myklebust 	}
678d39ab9deSBryan Schumaker 
6791775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
68041d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
6819ac3d3e8SAl Viro 	d_lookup_done(dentry);
6829ac3d3e8SAl Viro 	if (alias) {
683d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
684d39ab9deSBryan Schumaker 			goto out;
6859ac3d3e8SAl Viro 		dput(dentry);
6869ac3d3e8SAl Viro 		dentry = alias;
6879ac3d3e8SAl Viro 	}
688a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
689d39ab9deSBryan Schumaker out:
690d39ab9deSBryan Schumaker 	dput(dentry);
691d39ab9deSBryan Schumaker }
692d39ab9deSBryan Schumaker 
693d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
6943b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
6953b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
6963b2a09f1STrond Myklebust 				   struct page **xdr_pages,
6973b2a09f1STrond Myklebust 				   struct page *fillme, unsigned int buflen)
698d1bacf9eSBryan Schumaker {
6993b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
700babddc72SBryan Schumaker 	struct xdr_stream stream;
701f7da7a12SBenny Halevy 	struct xdr_buf buf;
7023b2a09f1STrond Myklebust 	struct page *scratch, *new, *page = fillme;
7035c346854STrond Myklebust 	int status;
704babddc72SBryan Schumaker 
7056650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
7066650239aSTrond Myklebust 	if (scratch == NULL)
7076650239aSTrond Myklebust 		return -ENOMEM;
708babddc72SBryan Schumaker 
709f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
7106650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
71199424380SBryan Schumaker 
71299424380SBryan Schumaker 	do {
713d33030e2SJeffrey Mitchell 		if (entry->label)
714d33030e2SJeffrey Mitchell 			entry->label->len = NFS4_MAXLABELLEN;
715d33030e2SJeffrey Mitchell 
71699424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
717972bcdf2STrond Myklebust 		if (status != 0)
71899424380SBryan Schumaker 			break;
7195c346854STrond Myklebust 
720a7a3b1e9SBenjamin Coddington 		if (desc->plus)
721a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
722a1147b82STrond Myklebust 					desc->dir_verifier);
7238cd51a0cSTrond Myklebust 
724db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
7253b2a09f1STrond Myklebust 		if (status != -ENOSPC)
7263b2a09f1STrond Myklebust 			continue;
7273b2a09f1STrond Myklebust 
7283b2a09f1STrond Myklebust 		if (page->mapping != mapping)
7293b2a09f1STrond Myklebust 			break;
7303b2a09f1STrond Myklebust 		new = nfs_readdir_page_get_next(mapping, page->index + 1,
7313b2a09f1STrond Myklebust 						entry->prev_cookie);
7323b2a09f1STrond Myklebust 		if (!new)
7333b2a09f1STrond Myklebust 			break;
7343b2a09f1STrond Myklebust 		if (page != fillme)
7353b2a09f1STrond Myklebust 			nfs_readdir_page_unlock_and_put(page);
7363b2a09f1STrond Myklebust 		page = new;
7373b2a09f1STrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
738972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
73999424380SBryan Schumaker 
740972bcdf2STrond Myklebust 	switch (status) {
741972bcdf2STrond Myklebust 	case -EBADCOOKIE:
742972bcdf2STrond Myklebust 		if (entry->eof) {
743b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
74499424380SBryan Schumaker 			status = 0;
74556e4ebf8SBryan Schumaker 		}
746972bcdf2STrond Myklebust 		break;
747972bcdf2STrond Myklebust 	case -ENOSPC:
748972bcdf2STrond Myklebust 	case -EAGAIN:
749972bcdf2STrond Myklebust 		status = 0;
750972bcdf2STrond Myklebust 		break;
751972bcdf2STrond Myklebust 	}
7526650239aSTrond Myklebust 
7533b2a09f1STrond Myklebust 	if (page != fillme)
7543b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
7553b2a09f1STrond Myklebust 
7566650239aSTrond Myklebust 	put_page(scratch);
7578cd51a0cSTrond Myklebust 	return status;
7588cd51a0cSTrond Myklebust }
75956e4ebf8SBryan Schumaker 
7601a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
76156e4ebf8SBryan Schumaker {
7621a34c8c9STrond Myklebust 	while (npages--)
7631a34c8c9STrond Myklebust 		put_page(pages[npages]);
7641a34c8c9STrond Myklebust 	kfree(pages);
76556e4ebf8SBryan Schumaker }
76656e4ebf8SBryan Schumaker 
76756e4ebf8SBryan Schumaker /*
768bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
769bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
77056e4ebf8SBryan Schumaker  */
7711a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
77256e4ebf8SBryan Schumaker {
7731a34c8c9STrond Myklebust 	struct page **pages;
7741a34c8c9STrond Myklebust 	size_t i;
77556e4ebf8SBryan Schumaker 
7761a34c8c9STrond Myklebust 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
7771a34c8c9STrond Myklebust 	if (!pages)
7781a34c8c9STrond Myklebust 		return NULL;
77956e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
78056e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
78156e4ebf8SBryan Schumaker 		if (page == NULL)
78256e4ebf8SBryan Schumaker 			goto out_freepages;
78356e4ebf8SBryan Schumaker 		pages[i] = page;
78456e4ebf8SBryan Schumaker 	}
7851a34c8c9STrond Myklebust 	return pages;
78656e4ebf8SBryan Schumaker 
78756e4ebf8SBryan Schumaker out_freepages:
788c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
7891a34c8c9STrond Myklebust 	return NULL;
790d1bacf9eSBryan Schumaker }
791d1bacf9eSBryan Schumaker 
7926c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
793b593c09fSTrond Myklebust 				    struct page *page, __be32 *verf_arg,
794b593c09fSTrond Myklebust 				    __be32 *verf_res)
795d1bacf9eSBryan Schumaker {
7961a34c8c9STrond Myklebust 	struct page **pages;
7976b75cf9eSTrond Myklebust 	struct nfs_entry *entry;
7981a34c8c9STrond Myklebust 	size_t array_size;
799b593c09fSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
8001a34c8c9STrond Myklebust 	size_t dtsize = NFS_SERVER(inode)->dtsize;
8018cd51a0cSTrond Myklebust 	int status = -ENOMEM;
802d1bacf9eSBryan Schumaker 
8036b75cf9eSTrond Myklebust 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
8046b75cf9eSTrond Myklebust 	if (!entry)
8056b75cf9eSTrond Myklebust 		return -ENOMEM;
8066b75cf9eSTrond Myklebust 	entry->cookie = nfs_readdir_page_last_cookie(page);
8076b75cf9eSTrond Myklebust 	entry->fh = nfs_alloc_fhandle();
8086b75cf9eSTrond Myklebust 	entry->fattr = nfs_alloc_fattr();
8096b75cf9eSTrond Myklebust 	entry->server = NFS_SERVER(inode);
8106b75cf9eSTrond Myklebust 	if (entry->fh == NULL || entry->fattr == NULL)
811d1bacf9eSBryan Schumaker 		goto out;
812d1bacf9eSBryan Schumaker 
8136b75cf9eSTrond Myklebust 	entry->label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
8146b75cf9eSTrond Myklebust 	if (IS_ERR(entry->label)) {
8156b75cf9eSTrond Myklebust 		status = PTR_ERR(entry->label);
81614c43f76SDavid Quigley 		goto out;
81714c43f76SDavid Quigley 	}
81814c43f76SDavid Quigley 
8191a34c8c9STrond Myklebust 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
8201a34c8c9STrond Myklebust 	pages = nfs_readdir_alloc_pages(array_size);
8211a34c8c9STrond Myklebust 	if (!pages)
822e762a639STrond Myklebust 		goto out_release_label;
8231a34c8c9STrond Myklebust 
824d1bacf9eSBryan Schumaker 	do {
825ac396128STrond Myklebust 		unsigned int pglen;
826b593c09fSTrond Myklebust 		status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie,
827b593c09fSTrond Myklebust 						pages, dtsize,
828b593c09fSTrond Myklebust 						verf_res);
829d1bacf9eSBryan Schumaker 		if (status < 0)
830d1bacf9eSBryan Schumaker 			break;
831972bcdf2STrond Myklebust 
832ac396128STrond Myklebust 		pglen = status;
833972bcdf2STrond Myklebust 		if (pglen == 0) {
834972bcdf2STrond Myklebust 			nfs_readdir_page_set_eof(page);
8358cd51a0cSTrond Myklebust 			break;
8368cd51a0cSTrond Myklebust 		}
837972bcdf2STrond Myklebust 
8386b75cf9eSTrond Myklebust 		status = nfs_readdir_page_filler(desc, entry, pages, page, pglen);
839e762a639STrond Myklebust 	} while (!status && nfs_readdir_page_needs_filling(page));
840d1bacf9eSBryan Schumaker 
841c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
842e762a639STrond Myklebust out_release_label:
8436b75cf9eSTrond Myklebust 	nfs4_label_free(entry->label);
844d1bacf9eSBryan Schumaker out:
8456b75cf9eSTrond Myklebust 	nfs_free_fattr(entry->fattr);
8466b75cf9eSTrond Myklebust 	nfs_free_fhandle(entry->fh);
8476b75cf9eSTrond Myklebust 	kfree(entry);
848d1bacf9eSBryan Schumaker 	return status;
849d1bacf9eSBryan Schumaker }
850d1bacf9eSBryan Schumaker 
8511f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
8521da177e4SLinus Torvalds {
85309cbfeafSKirill A. Shutemov 	put_page(desc->page);
8541da177e4SLinus Torvalds 	desc->page = NULL;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571f1d4aa4STrond Myklebust static void
8581f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
8591da177e4SLinus Torvalds {
8601f1d4aa4STrond Myklebust 	unlock_page(desc->page);
8611f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
8621f1d4aa4STrond Myklebust }
8631f1d4aa4STrond Myklebust 
8641f1d4aa4STrond Myklebust static struct page *
8651f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
8661f1d4aa4STrond Myklebust {
8671f1d4aa4STrond Myklebust 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
8681f1d4aa4STrond Myklebust 					   desc->page_index,
8691f1d4aa4STrond Myklebust 					   desc->last_cookie);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /*
873d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
874114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
8751da177e4SLinus Torvalds  */
8766c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
877d1bacf9eSBryan Schumaker {
878227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
879227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
880b593c09fSTrond Myklebust 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
881d1bacf9eSBryan Schumaker 	int res;
882d1bacf9eSBryan Schumaker 
8831f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
8841f1d4aa4STrond Myklebust 	if (!desc->page)
8851f1d4aa4STrond Myklebust 		return -ENOMEM;
8861f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
887b593c09fSTrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, desc->page,
888b593c09fSTrond Myklebust 					       nfsi->cookieverf, verf);
8899fff59edSTrond Myklebust 		if (res < 0) {
8909fff59edSTrond Myklebust 			nfs_readdir_page_unlock_and_put_cached(desc);
8919fff59edSTrond Myklebust 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
8929fff59edSTrond Myklebust 				invalidate_inode_pages2(desc->file->f_mapping);
8939fff59edSTrond Myklebust 				desc->page_index = 0;
8949fff59edSTrond Myklebust 				return -EAGAIN;
8959fff59edSTrond Myklebust 			}
8969fff59edSTrond Myklebust 			return res;
8979fff59edSTrond Myklebust 		}
898b593c09fSTrond Myklebust 		memcpy(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf));
8991f1d4aa4STrond Myklebust 	}
900114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
901227823d2SDai Ngo 	if (res == 0) {
902227823d2SDai Ngo 		nfsi->page_index = desc->page_index;
903114de382STrond Myklebust 		return 0;
904114de382STrond Myklebust 	}
9051f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
906d1bacf9eSBryan Schumaker 	return res;
907d1bacf9eSBryan Schumaker }
908d1bacf9eSBryan Schumaker 
909d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
9106c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
9111da177e4SLinus Torvalds {
9128cd51a0cSTrond Myklebust 	int res;
913d1bacf9eSBryan Schumaker 
9149fff59edSTrond Myklebust 	do {
9150aded708STrond Myklebust 		if (desc->page_index == 0) {
9168cd51a0cSTrond Myklebust 			desc->current_index = 0;
91759e356a9STrond Myklebust 			desc->prev_index = 0;
9180aded708STrond Myklebust 			desc->last_cookie = 0;
9190aded708STrond Myklebust 		}
920114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
92147c716cbSTrond Myklebust 	} while (res == -EAGAIN);
9221da177e4SLinus Torvalds 	return res;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds /*
9261da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
9271da177e4SLinus Torvalds  */
928dbeaf8c9STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	struct file	*file = desc->file;
931b593c09fSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(file_inode(file));
932dbeaf8c9STrond Myklebust 	struct nfs_cache_array *array;
933dbeaf8c9STrond Myklebust 	unsigned int i = 0;
9348ef2ce3eSBryan Schumaker 
9350795bf83SFabian Frederick 	array = kmap(desc->page);
936d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
937ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
9381da177e4SLinus Torvalds 
939ece0b423STrond Myklebust 		ent = &array->array[i];
940a52a8a6aSTrond Myklebust 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
94123db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
9426089dd0dSThomas Meyer 			desc->eof = true;
9431da177e4SLinus Torvalds 			break;
944ece0b423STrond Myklebust 		}
945b593c09fSTrond Myklebust 		memcpy(desc->verf, nfsi->cookieverf, sizeof(desc->verf));
946d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
9472e7a4641STrond Myklebust 			desc->dir_cookie = array->array[i+1].cookie;
948d1bacf9eSBryan Schumaker 		else
9492e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
95059e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
9512e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
95259e356a9STrond Myklebust 		else
95359e356a9STrond Myklebust 			desc->ctx->pos++;
9542e7a4641STrond Myklebust 		if (desc->duped != 0)
9552e7a4641STrond Myklebust 			desc->duped = 1;
9568cd51a0cSTrond Myklebust 	}
957b1e21c97STrond Myklebust 	if (array->page_is_eof)
9586089dd0dSThomas Meyer 		desc->eof = true;
959d1bacf9eSBryan Schumaker 
9600795bf83SFabian Frederick 	kunmap(desc->page);
961dbeaf8c9STrond Myklebust 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
962dbeaf8c9STrond Myklebust 			(unsigned long long)desc->dir_cookie);
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds /*
9661da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
9671da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
9681da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
9691da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
9701da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
9711da177e4SLinus Torvalds  *
9721da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
9731da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
9741da177e4SLinus Torvalds  *	 we should already have a complete representation of the
9751da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
9761da177e4SLinus Torvalds  */
9776c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	struct page	*page = NULL;
980b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
9811da177e4SLinus Torvalds 	int		status;
9821da177e4SLinus Torvalds 
9831e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
9842e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
9871da177e4SLinus Torvalds 	if (!page) {
9881da177e4SLinus Torvalds 		status = -ENOMEM;
9891da177e4SLinus Torvalds 		goto out;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
9927a8e1dc3STrond Myklebust 	desc->page_index = 0;
9932e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
9947a8e1dc3STrond Myklebust 	desc->page = page;
9952e7a4641STrond Myklebust 	desc->duped = 0;
9967a8e1dc3STrond Myklebust 
9971f1d4aa4STrond Myklebust 	nfs_readdir_page_init_array(page, desc->dir_cookie);
998b593c09fSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, desc->verf, verf);
99985f8607eSTrond Myklebust 	if (status < 0)
1000d1bacf9eSBryan Schumaker 		goto out_release;
1001d1bacf9eSBryan Schumaker 
1002dbeaf8c9STrond Myklebust 	nfs_do_filldir(desc);
10031da177e4SLinus Torvalds 
1004114de382STrond Myklebust  out_release:
1005114de382STrond Myklebust 	nfs_readdir_clear_array(desc->page);
10061f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
10071da177e4SLinus Torvalds  out:
10081e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
10093110ff80SHarvey Harrison 			__func__, status);
10101da177e4SLinus Torvalds 	return status;
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
101300a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
101400a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
101500a92642SOlivier Galibert    whole directory.
10161da177e4SLinus Torvalds  */
101723db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
10181da177e4SLinus Torvalds {
1019be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
10202b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
102123db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
10226b75cf9eSTrond Myklebust 	struct nfs_readdir_descriptor *desc;
10236b75cf9eSTrond Myklebust 	int res;
10241da177e4SLinus Torvalds 
10256de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
10266de1472fSAl Viro 			file, (long long)ctx->pos);
102791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
102891d5b470SChuck Lever 
10291da177e4SLinus Torvalds 	/*
103023db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
1031f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
103200a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
103300a92642SOlivier Galibert 	 * revalidate the cookie.
10341da177e4SLinus Torvalds 	 */
10356b75cf9eSTrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) {
103623db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
1037fccca7fcSTrond Myklebust 		if (res < 0)
1038fccca7fcSTrond Myklebust 			goto out;
10396b75cf9eSTrond Myklebust 	}
10406b75cf9eSTrond Myklebust 
10416b75cf9eSTrond Myklebust 	res = -ENOMEM;
10426b75cf9eSTrond Myklebust 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
10436b75cf9eSTrond Myklebust 	if (!desc)
10446b75cf9eSTrond Myklebust 		goto out;
10456b75cf9eSTrond Myklebust 	desc->file = file;
10466b75cf9eSTrond Myklebust 	desc->ctx = ctx;
10476b75cf9eSTrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, ctx);
1048fccca7fcSTrond Myklebust 
10492e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
10502e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
10512e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
10522e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
10532e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
1054b593c09fSTrond Myklebust 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
10552e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
10562e7a4641STrond Myklebust 
105747c716cbSTrond Myklebust 	do {
10581da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
105900a92642SOlivier Galibert 
10601da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1061ece0b423STrond Myklebust 			res = 0;
10621da177e4SLinus Torvalds 			/* This means either end of directory */
10632e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
10641da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
106523db8620SAl Viro 				res = uncached_readdir(desc);
1066ece0b423STrond Myklebust 				if (res == 0)
10671da177e4SLinus Torvalds 					continue;
10689fff59edSTrond Myklebust 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
10699fff59edSTrond Myklebust 					res = 0;
10701da177e4SLinus Torvalds 			}
10711da177e4SLinus Torvalds 			break;
10721da177e4SLinus Torvalds 		}
10731da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
10743a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
10751da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1076baf57a09STrond Myklebust 			desc->page_index = 0;
1077a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1078a7a3b1e9SBenjamin Coddington 			desc->eof = false;
10791da177e4SLinus Torvalds 			continue;
10801da177e4SLinus Torvalds 		}
10811da177e4SLinus Torvalds 		if (res < 0)
10821da177e4SLinus Torvalds 			break;
10831da177e4SLinus Torvalds 
1084dbeaf8c9STrond Myklebust 		nfs_do_filldir(desc);
10851f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
108647c716cbSTrond Myklebust 	} while (!desc->eof);
10872e7a4641STrond Myklebust 
10882e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
10892e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
10902e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
10912e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
10922e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
1093b593c09fSTrond Myklebust 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
10942e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
10952e7a4641STrond Myklebust 
10966b75cf9eSTrond Myklebust 	kfree(desc);
10976b75cf9eSTrond Myklebust 
1098fccca7fcSTrond Myklebust out:
10996de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
11001da177e4SLinus Torvalds 	return res;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
1103965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1104f0dd2136STrond Myklebust {
1105480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1106b84e06c5SChuck Lever 
11076de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
11086de1472fSAl Viro 			filp, offset, whence);
1109b84e06c5SChuck Lever 
1110965c8e59SAndrew Morton 	switch (whence) {
1111f0dd2136STrond Myklebust 	default:
1112b2b1ff3dSTrond Myklebust 		return -EINVAL;
1113b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1114b2b1ff3dSTrond Myklebust 		if (offset < 0)
1115b2b1ff3dSTrond Myklebust 			return -EINVAL;
111683f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1117b2b1ff3dSTrond Myklebust 		break;
1118b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1119b2b1ff3dSTrond Myklebust 		if (offset == 0)
1120b2b1ff3dSTrond Myklebust 			return filp->f_pos;
112183f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1122b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1123b2b1ff3dSTrond Myklebust 		if (offset < 0) {
112483f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1125b2b1ff3dSTrond Myklebust 			return -EINVAL;
1126b2b1ff3dSTrond Myklebust 		}
1127f0dd2136STrond Myklebust 	}
1128f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1129f0dd2136STrond Myklebust 		filp->f_pos = offset;
113059e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(filp))
113159e356a9STrond Myklebust 			dir_ctx->dir_cookie = offset;
113259e356a9STrond Myklebust 		else
1133480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
1134b593c09fSTrond Myklebust 		if (offset == 0)
1135b593c09fSTrond Myklebust 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
11368ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1137f0dd2136STrond Myklebust 	}
113883f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1139f0dd2136STrond Myklebust 	return offset;
1140f0dd2136STrond Myklebust }
1141f0dd2136STrond Myklebust 
11421da177e4SLinus Torvalds /*
11431da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
11441da177e4SLinus Torvalds  * is a dummy operation.
11451da177e4SLinus Torvalds  */
114602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
114702c24a82SJosef Bacik 			 int datasync)
11481da177e4SLinus Torvalds {
11496de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
11501e7cb3dcSChuck Lever 
115111decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
11521da177e4SLinus Torvalds 	return 0;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
1155bfc69a45STrond Myklebust /**
1156bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1157302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1158bfc69a45STrond Myklebust  *
1159bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1160bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1161bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1162bfc69a45STrond Myklebust  *
1163efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1164efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1165efeda80dSTrond Myklebust  *
1166bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1167bfc69a45STrond Myklebust  */
1168bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1169bfc69a45STrond Myklebust {
1170efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1171bfc69a45STrond Myklebust }
117289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1173bfc69a45STrond Myklebust 
1174efeda80dSTrond Myklebust /**
1175efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1176efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1177efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1178efeda80dSTrond Myklebust  *
1179efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1180efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1181efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1182efeda80dSTrond Myklebust  */
1183efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1184efeda80dSTrond Myklebust {
1185efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1186efeda80dSTrond Myklebust }
1187efeda80dSTrond Myklebust 
1188efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1189efeda80dSTrond Myklebust {
1190efeda80dSTrond Myklebust 	*verf |= 1UL;
1191efeda80dSTrond Myklebust }
1192efeda80dSTrond Myklebust 
1193efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1194efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1195efeda80dSTrond Myklebust {
1196efeda80dSTrond Myklebust 	*verf &= ~1UL;
1197efeda80dSTrond Myklebust }
1198efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1199efeda80dSTrond Myklebust 
1200efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1201efeda80dSTrond Myklebust {
1202efeda80dSTrond Myklebust 	return verf & 1;
1203efeda80dSTrond Myklebust }
1204efeda80dSTrond Myklebust 
1205efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1206efeda80dSTrond Myklebust {
1207efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1208efeda80dSTrond Myklebust }
1209efeda80dSTrond Myklebust 
1210efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1211efeda80dSTrond Myklebust {
1212efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1213efeda80dSTrond Myklebust 
1214efeda80dSTrond Myklebust 	if (!nfs_verifier_is_delegated(dentry) &&
1215efeda80dSTrond Myklebust 	    !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1216efeda80dSTrond Myklebust 		goto out;
1217efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1218efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1219efeda80dSTrond Myklebust out:
1220efeda80dSTrond Myklebust 	dentry->d_time = verf;
1221efeda80dSTrond Myklebust }
1222efeda80dSTrond Myklebust 
1223efeda80dSTrond Myklebust /**
1224efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1225efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1226efeda80dSTrond Myklebust  * @verf: verifier to save
1227efeda80dSTrond Myklebust  *
1228efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1229efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1230efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1231efeda80dSTrond Myklebust  * look it up again after a directory change.
1232efeda80dSTrond Myklebust  */
1233efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1234efeda80dSTrond Myklebust {
1235efeda80dSTrond Myklebust 
1236efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1237efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1238efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1239efeda80dSTrond Myklebust }
1240efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1241efeda80dSTrond Myklebust 
1242efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1243efeda80dSTrond Myklebust /**
1244efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1245efeda80dSTrond Myklebust  * @inode: pointer to inode
1246efeda80dSTrond Myklebust  *
1247efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1248efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1249efeda80dSTrond Myklebust  * while holding a delegation.
1250efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1251efeda80dSTrond Myklebust  * returned or revoked.
1252efeda80dSTrond Myklebust  */
1253efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1254efeda80dSTrond Myklebust {
1255efeda80dSTrond Myklebust 	struct dentry *alias;
1256efeda80dSTrond Myklebust 
1257efeda80dSTrond Myklebust 	if (!inode)
1258efeda80dSTrond Myklebust 		return;
1259efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1260efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1261efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1262efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1263efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1264efeda80dSTrond Myklebust 	}
1265efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1266efeda80dSTrond Myklebust }
1267efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1268efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1269efeda80dSTrond Myklebust 
12701da177e4SLinus Torvalds /*
12711da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
12721da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
12731da177e4SLinus Torvalds  * and may need to be looked up again.
1274912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
12751da177e4SLinus Torvalds  */
1276912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1277912a108dSNeilBrown 			      int rcu_walk)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
12801da177e4SLinus Torvalds 		return 1;
12814eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
12824eec952eSTrond Myklebust 		return 0;
1283f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
12846ecc5e8fSTrond Myklebust 		return 0;
1285f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
12861cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1287912a108dSNeilBrown 		if (rcu_walk)
1288f2c77f4eSTrond Myklebust 			return 0;
12891cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
12901cd9cb05STrond Myklebust 			return 0;
12911cd9cb05STrond Myklebust 	}
1292f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1293f2c77f4eSTrond Myklebust 		return 0;
1294f2c77f4eSTrond Myklebust 	return 1;
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds /*
1298a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1299a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1300a12802caSTrond Myklebust  */
1301fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1302a12802caSTrond Myklebust {
1303a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1304a12802caSTrond Myklebust 		return 0;
1305fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1306a12802caSTrond Myklebust }
1307a12802caSTrond Myklebust 
1308a12802caSTrond Myklebust /*
13091d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
13101d6757fbSTrond Myklebust  *
13111d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
13121d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
13131d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
13141d6757fbSTrond Myklebust  *
13151d6757fbSTrond Myklebust  */
131665a0c149STrond Myklebust static
1317fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
132065a0c149STrond Myklebust 	int ret;
13211da177e4SLinus Torvalds 
132236d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
13234e99a1ffSTrond Myklebust 		return 0;
132447921921STrond Myklebust 
132547921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
132647921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
132747921921STrond Myklebust 		case S_IFREG:
132847921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
132947921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
133047921921STrond Myklebust 				goto out;
1331df561f66SGustavo A. R. Silva 			fallthrough;
133247921921STrond Myklebust 		case S_IFDIR:
133347921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
133447921921STrond Myklebust 				break;
133547921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
133647921921STrond Myklebust 			goto out_force;
133747921921STrond Myklebust 		}
133847921921STrond Myklebust 	}
133947921921STrond Myklebust 
13401da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1341fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
13421da177e4SLinus Torvalds 		goto out_force;
134365a0c149STrond Myklebust out:
1344a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
13451da177e4SLinus Torvalds out_force:
13461fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
13471fa1e384SNeilBrown 		return -ECHILD;
134865a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
134965a0c149STrond Myklebust 	if (ret != 0)
135065a0c149STrond Myklebust 		return ret;
135165a0c149STrond Myklebust 	goto out;
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds /*
13551da177e4SLinus Torvalds  * We judge how long we want to trust negative
13561da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
13571da177e4SLinus Torvalds  *
13581da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
13591da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1360912a108dSNeilBrown  *
1361912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1362912a108dSNeilBrown  * suggesting a reval is needed.
13639f6d44d4STrond Myklebust  *
13649f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
13659f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
13661da177e4SLinus Torvalds  */
13671da177e4SLinus Torvalds static inline
13681da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1369fa3c56bbSAl Viro 		       unsigned int flags)
13701da177e4SLinus Torvalds {
13719f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
13721da177e4SLinus Torvalds 		return 0;
13734eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
13744eec952eSTrond Myklebust 		return 1;
1375912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13785ceb9d7fSTrond Myklebust static int
13795ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
13805ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
13811da177e4SLinus Torvalds {
13825ceb9d7fSTrond Myklebust 	switch (error) {
13835ceb9d7fSTrond Myklebust 	case 1:
13846de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
13856de1472fSAl Viro 			__func__, dentry);
13861da177e4SLinus Torvalds 		return 1;
13875ceb9d7fSTrond Myklebust 	case 0:
1388a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
13891da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
13901da177e4SLinus Torvalds 			/* Purge readdir caches. */
13911da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1392a3f432bfSJ. Bruce Fields 			/*
1393a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1394a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1395a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1396a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1397a3f432bfSJ. Bruce Fields 			 */
1398a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
13995ceb9d7fSTrond Myklebust 				return 1;
14001da177e4SLinus Torvalds 		}
14016de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
14026de1472fSAl Viro 				__func__, dentry);
14031da177e4SLinus Torvalds 		return 0;
14045ceb9d7fSTrond Myklebust 	}
14056de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
14066de1472fSAl Viro 				__func__, dentry, error);
1407e1fb4d05STrond Myklebust 	return error;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14105ceb9d7fSTrond Myklebust static int
14115ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
14125ceb9d7fSTrond Myklebust 			       unsigned int flags)
14135ceb9d7fSTrond Myklebust {
14145ceb9d7fSTrond Myklebust 	int ret = 1;
14155ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
14165ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
14175ceb9d7fSTrond Myklebust 			return -ECHILD;
14185ceb9d7fSTrond Myklebust 		ret = 0;
14195ceb9d7fSTrond Myklebust 	}
14205ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
14215ceb9d7fSTrond Myklebust }
14225ceb9d7fSTrond Myklebust 
14235ceb9d7fSTrond Myklebust static int
14245ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
14255ceb9d7fSTrond Myklebust 				struct inode *inode)
14265ceb9d7fSTrond Myklebust {
14275ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14285ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
14295ceb9d7fSTrond Myklebust }
14305ceb9d7fSTrond Myklebust 
14315ceb9d7fSTrond Myklebust static int
14325ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
14335ceb9d7fSTrond Myklebust 			     struct inode *inode)
14345ceb9d7fSTrond Myklebust {
14355ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
14365ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
14375ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
1438a1147b82STrond Myklebust 	unsigned long dir_verifier;
14395ceb9d7fSTrond Myklebust 	int ret;
14405ceb9d7fSTrond Myklebust 
14415ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
14425ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
14435ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
14445ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
14455ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
14465ceb9d7fSTrond Myklebust 		goto out;
14475ceb9d7fSTrond Myklebust 
1448a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
1449f7b37b8bSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
14505ceb9d7fSTrond Myklebust 	if (ret < 0) {
1451f7b37b8bSTrond Myklebust 		switch (ret) {
1452f7b37b8bSTrond Myklebust 		case -ESTALE:
1453f7b37b8bSTrond Myklebust 		case -ENOENT:
14545ceb9d7fSTrond Myklebust 			ret = 0;
1455f7b37b8bSTrond Myklebust 			break;
1456f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1457f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1458f7b37b8bSTrond Myklebust 				ret = 1;
1459f7b37b8bSTrond Myklebust 		}
14605ceb9d7fSTrond Myklebust 		goto out;
14615ceb9d7fSTrond Myklebust 	}
14625ceb9d7fSTrond Myklebust 	ret = 0;
14635ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
14645ceb9d7fSTrond Myklebust 		goto out;
14655ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
14665ceb9d7fSTrond Myklebust 		goto out;
14675ceb9d7fSTrond Myklebust 
14685ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
1469a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
14705ceb9d7fSTrond Myklebust 
14715ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
14725ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
14735ceb9d7fSTrond Myklebust 	ret = 1;
14745ceb9d7fSTrond Myklebust out:
14755ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
14765ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
14775ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
14785ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
14795ceb9d7fSTrond Myklebust }
14805ceb9d7fSTrond Myklebust 
14815ceb9d7fSTrond Myklebust /*
14825ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
14835ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
14845ceb9d7fSTrond Myklebust  * lookup.
14855ceb9d7fSTrond Myklebust  *
14865ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
14875ceb9d7fSTrond Myklebust  * we have an inode!
14885ceb9d7fSTrond Myklebust  *
14895ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
14905ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
14915ceb9d7fSTrond Myklebust  */
14925ceb9d7fSTrond Myklebust static int
14935ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
14945ceb9d7fSTrond Myklebust 			 unsigned int flags)
14955ceb9d7fSTrond Myklebust {
14965ceb9d7fSTrond Myklebust 	struct inode *inode;
14975ceb9d7fSTrond Myklebust 	int error;
14985ceb9d7fSTrond Myklebust 
14995ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
15005ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
15015ceb9d7fSTrond Myklebust 
15025ceb9d7fSTrond Myklebust 	if (!inode)
15035ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
15045ceb9d7fSTrond Myklebust 
15055ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
15065ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
15075ceb9d7fSTrond Myklebust 				__func__, dentry);
15085ceb9d7fSTrond Myklebust 		goto out_bad;
15095ceb9d7fSTrond Myklebust 	}
15105ceb9d7fSTrond Myklebust 
1511efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
15125ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
15135ceb9d7fSTrond Myklebust 
15145ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
15155ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
15165ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
15175ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
15185ceb9d7fSTrond Myklebust 		if (error) {
15195ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
15205ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
15215ceb9d7fSTrond Myklebust 			goto out_bad;
15225ceb9d7fSTrond Myklebust 		}
15235ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
15245ceb9d7fSTrond Myklebust 		goto out_valid;
15255ceb9d7fSTrond Myklebust 	}
15265ceb9d7fSTrond Myklebust 
15275ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
15285ceb9d7fSTrond Myklebust 		return -ECHILD;
15295ceb9d7fSTrond Myklebust 
15305ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
15315ceb9d7fSTrond Myklebust 		goto out_bad;
15325ceb9d7fSTrond Myklebust 
15335ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
15345ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
15355ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
15365ceb9d7fSTrond Myklebust 	return error;
15375ceb9d7fSTrond Myklebust out_valid:
15385ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
15395ceb9d7fSTrond Myklebust out_bad:
15405ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
15415ceb9d7fSTrond Myklebust 		return -ECHILD;
15425ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
15435ceb9d7fSTrond Myklebust }
15445ceb9d7fSTrond Myklebust 
15455ceb9d7fSTrond Myklebust static int
1546c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1547c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
15485ceb9d7fSTrond Myklebust {
15495ceb9d7fSTrond Myklebust 	struct dentry *parent;
15505ceb9d7fSTrond Myklebust 	struct inode *dir;
15515ceb9d7fSTrond Myklebust 	int ret;
15525ceb9d7fSTrond Myklebust 
15535ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
15545ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
15555ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
15565ceb9d7fSTrond Myklebust 		if (!dir)
15575ceb9d7fSTrond Myklebust 			return -ECHILD;
1558c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
15595ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
15605ceb9d7fSTrond Myklebust 			return -ECHILD;
15615ceb9d7fSTrond Myklebust 	} else {
15625ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1563c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
15645ceb9d7fSTrond Myklebust 		dput(parent);
15655ceb9d7fSTrond Myklebust 	}
15665ceb9d7fSTrond Myklebust 	return ret;
15675ceb9d7fSTrond Myklebust }
15685ceb9d7fSTrond Myklebust 
1569c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1570c7944ebbSTrond Myklebust {
1571c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1572c7944ebbSTrond Myklebust }
1573c7944ebbSTrond Myklebust 
15741da177e4SLinus Torvalds /*
15752b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1576ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1577ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1578ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1579ecf3d1f1SJeff Layton  *
1580ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1581ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1582ecf3d1f1SJeff Layton  */
1583ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1584ecf3d1f1SJeff Layton {
15852b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
15869cdd1d3fSTrond Myklebust 	int error = 0;
1587ecf3d1f1SJeff Layton 
1588ecf3d1f1SJeff Layton 	/*
1589ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1590ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1591ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1592ecf3d1f1SJeff Layton 	 */
1593ecf3d1f1SJeff Layton 	if (!inode) {
15946de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
15956de1472fSAl Viro 				__func__, dentry);
1596ecf3d1f1SJeff Layton 		return 1;
1597ecf3d1f1SJeff Layton 	}
1598ecf3d1f1SJeff Layton 
1599ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
16006de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
16016de1472fSAl Viro 				__func__, dentry);
1602ecf3d1f1SJeff Layton 		return 0;
1603ecf3d1f1SJeff Layton 	}
1604ecf3d1f1SJeff Layton 
1605b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1606ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1607ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1608ecf3d1f1SJeff Layton 	return !error;
1609ecf3d1f1SJeff Layton }
1610ecf3d1f1SJeff Layton 
1611ecf3d1f1SJeff Layton /*
16121da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
16131da177e4SLinus Torvalds  */
1614fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
16151da177e4SLinus Torvalds {
16166de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
16176de1472fSAl Viro 		dentry, dentry->d_flags);
16181da177e4SLinus Torvalds 
161977f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
16202b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
162177f11192STrond Myklebust 		return 1;
162277f11192STrond Myklebust 
16231da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16241da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
16251da177e4SLinus Torvalds 		return 1;
16261da177e4SLinus Torvalds 	}
16271751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
16281da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
16291da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
16301da177e4SLinus Torvalds 		return 1;
16311da177e4SLinus Torvalds 	}
16321da177e4SLinus Torvalds 	return 0;
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
16361f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
16371b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
16381b83d707STrond Myklebust {
16391b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
16401f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
164159a707b0STrond Myklebust 	if (inode->i_nlink > 0)
164259a707b0STrond Myklebust 		drop_nlink(inode);
164359a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
164416e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
164516e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
164659a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
164759a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
16481b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
16491b83d707STrond Myklebust }
16501b83d707STrond Myklebust 
16511da177e4SLinus Torvalds /*
16521da177e4SLinus Torvalds  * Called when the dentry loses inode.
16531da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
16541da177e4SLinus Torvalds  */
16551da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
16561da177e4SLinus Torvalds {
165783672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
165883672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
165983672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
166083672d39SNeil Brown 
16611da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1662e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
16631f018458STrond Myklebust 		nfs_drop_nlink(inode);
16641da177e4SLinus Torvalds 	}
16651da177e4SLinus Torvalds 	iput(inode);
16661da177e4SLinus Torvalds }
16671da177e4SLinus Torvalds 
1668b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1669b1942c5fSAl Viro {
1670b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1671b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1672b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1673b1942c5fSAl Viro 			WARN_ON(1);
1674b1942c5fSAl Viro 		else
1675b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1676b1942c5fSAl Viro 	}
1677b1942c5fSAl Viro }
1678b1942c5fSAl Viro 
1679f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
16801da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1681ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
16821da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
16831da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
168436d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1685b1942c5fSAl Viro 	.d_release	= nfs_d_release,
16861da177e4SLinus Torvalds };
1687ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
16881da177e4SLinus Torvalds 
1689597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	struct dentry *res;
16921da177e4SLinus Torvalds 	struct inode *inode = NULL;
1693e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1694e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
16951775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
1696a1147b82STrond Myklebust 	unsigned long dir_verifier;
16971da177e4SLinus Torvalds 	int error;
16981da177e4SLinus Torvalds 
16996de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
170091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
17011da177e4SLinus Torvalds 
1702130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1703130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
17041da177e4SLinus Torvalds 
1705fd684071STrond Myklebust 	/*
1706fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1707fd684071STrond Myklebust 	 * but don't hash the dentry.
1708fd684071STrond Myklebust 	 */
17099f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1710130f9ab7SAl Viro 		return NULL;
17111da177e4SLinus Torvalds 
1712e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1713e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1714e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1715e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1716e1fb4d05STrond Myklebust 		goto out;
1717e1fb4d05STrond Myklebust 
171814c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
171914c43f76SDavid Quigley 	if (IS_ERR(label))
172014c43f76SDavid Quigley 		goto out;
172114c43f76SDavid Quigley 
1722a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
17236e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1724f7b37b8bSTrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
17251da177e4SLinus Torvalds 	if (error == -ENOENT)
17261da177e4SLinus Torvalds 		goto no_entry;
17271da177e4SLinus Torvalds 	if (error < 0) {
17281da177e4SLinus Torvalds 		res = ERR_PTR(error);
1729bf130914SAl Viro 		goto out_label;
17301da177e4SLinus Torvalds 	}
17311775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1732bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
173303f28e3aSTrond Myklebust 	if (IS_ERR(res))
1734bf130914SAl Viro 		goto out_label;
173554ceac45SDavid Howells 
173663519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
173763519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1738d69ee9b8STrond Myklebust 
17391da177e4SLinus Torvalds no_entry:
174041d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
17419eaef27bSTrond Myklebust 	if (res != NULL) {
17429eaef27bSTrond Myklebust 		if (IS_ERR(res))
1743bf130914SAl Viro 			goto out_label;
17441da177e4SLinus Torvalds 		dentry = res;
17459eaef27bSTrond Myklebust 	}
1746a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
1747bf130914SAl Viro out_label:
17486e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
174914c43f76SDavid Quigley 	nfs4_label_free(label);
17501da177e4SLinus Torvalds out:
1751e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1752e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
17531da177e4SLinus Torvalds 	return res;
17541da177e4SLinus Torvalds }
1755ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
17561da177e4SLinus Torvalds 
175789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
17580b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
17591da177e4SLinus Torvalds 
1760f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
17610ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1762b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
17631da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
17641da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
176536d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1766b1942c5fSAl Viro 	.d_release	= nfs_d_release,
17671da177e4SLinus Torvalds };
176889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
17691da177e4SLinus Torvalds 
17708a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
17718a5e929dSAl Viro {
17728a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
17738a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
17748a5e929dSAl Viro 		res |= FMODE_READ;
17758a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
17768a5e929dSAl Viro 		res |= FMODE_WRITE;
17778a5e929dSAl Viro 	return res;
17788a5e929dSAl Viro }
17798a5e929dSAl Viro 
1780532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1781cd9a1c0eSTrond Myklebust {
1782532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1783cd9a1c0eSTrond Myklebust }
1784cd9a1c0eSTrond Myklebust 
1785cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1786cd9a1c0eSTrond Myklebust {
1787f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1788cd9a1c0eSTrond Myklebust 	return 0;
1789cd9a1c0eSTrond Myklebust }
1790cd9a1c0eSTrond Myklebust 
1791d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
17920dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1793b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1794cd9a1c0eSTrond Myklebust {
17950dd2b474SMiklos Szeredi 	int err;
17960dd2b474SMiklos Szeredi 
1797be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
179830d90494SAl Viro 	if (err)
1799d9585277SAl Viro 		goto out;
1800eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
180130d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1802eaa2b82cSNeilBrown 	else
18039821421aSTrond Myklebust 		err = -EOPENSTALE;
1804cd9a1c0eSTrond Myklebust out:
1805d9585277SAl Viro 	return err;
1806cd9a1c0eSTrond Myklebust }
1807cd9a1c0eSTrond Myklebust 
180873a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
180930d90494SAl Viro 		    struct file *file, unsigned open_flags,
181044907d79SAl Viro 		    umode_t mode)
18111da177e4SLinus Torvalds {
1812c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1813cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
18140dd2b474SMiklos Szeredi 	struct dentry *res;
18150dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1816f46e0bd3STrond Myklebust 	struct inode *inode;
18171472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1818c94c0953SAl Viro 	bool switched = false;
181973a09dd9SAl Viro 	int created = 0;
1820898f635cSTrond Myklebust 	int err;
18211da177e4SLinus Torvalds 
18220dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
18232b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
18240dd2b474SMiklos Szeredi 
18251e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
18266de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18271e7cb3dcSChuck Lever 
18289597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
18299597c13bSJeff Layton 	if (err)
18309597c13bSJeff Layton 		return err;
18319597c13bSJeff Layton 
18320dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
18330dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
183400699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
18350dd2b474SMiklos Szeredi 			/*
18360dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
18370dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
18380dd2b474SMiklos Szeredi 			 * again
18390dd2b474SMiklos Szeredi 			 */
1840d9585277SAl Viro 			return -ENOENT;
18410dd2b474SMiklos Szeredi 		}
18421472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
18431da177e4SLinus Torvalds 		goto no_open;
18441da177e4SLinus Torvalds 	}
18451da177e4SLinus Torvalds 
18460dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1847d9585277SAl Viro 		return -ENAMETOOLONG;
18481da177e4SLinus Torvalds 
18490dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1850dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1851dff25ddbSAndreas Gruenbacher 
1852dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1853dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1854dff25ddbSAndreas Gruenbacher 
1855536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1856dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
18570dd2b474SMiklos Szeredi 	}
1858536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1859536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1860536e43d1STrond Myklebust 		attr.ia_size = 0;
1861cd9a1c0eSTrond Myklebust 	}
1862cd9a1c0eSTrond Myklebust 
1863c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1864c94c0953SAl Viro 		d_drop(dentry);
1865c94c0953SAl Viro 		switched = true;
1866c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1867c94c0953SAl Viro 					  &dentry->d_name, &wq);
1868c94c0953SAl Viro 		if (IS_ERR(dentry))
1869c94c0953SAl Viro 			return PTR_ERR(dentry);
1870c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1871c94c0953SAl Viro 			return finish_no_open(file, dentry);
1872c94c0953SAl Viro 	}
1873c94c0953SAl Viro 
1874532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
18750dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
18760dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1877d9585277SAl Viro 		goto out;
18780dd2b474SMiklos Szeredi 
18796e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
188073a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
188173a09dd9SAl Viro 	if (created)
188273a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1883275bb307STrond Myklebust 	if (IS_ERR(inode)) {
18840dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
18856e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
18862d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1887d20cb71dSAl Viro 		d_drop(dentry);
18880dd2b474SMiklos Szeredi 		switch (err) {
18891da177e4SLinus Torvalds 		case -ENOENT:
1890774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1891809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18920dd2b474SMiklos Szeredi 			break;
18931788ea6eSJeff Layton 		case -EISDIR:
18946f926b5bSTrond Myklebust 		case -ENOTDIR:
18956f926b5bSTrond Myklebust 			goto no_open;
18961da177e4SLinus Torvalds 		case -ELOOP:
18970dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
18981da177e4SLinus Torvalds 				goto no_open;
18990dd2b474SMiklos Szeredi 			break;
19001da177e4SLinus Torvalds 			/* case -EINVAL: */
19011da177e4SLinus Torvalds 		default:
19020dd2b474SMiklos Szeredi 			break;
19031da177e4SLinus Torvalds 		}
19041da177e4SLinus Torvalds 		goto out;
19051da177e4SLinus Torvalds 	}
19060dd2b474SMiklos Szeredi 
1907b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
19086e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
19092d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1910d9585277SAl Viro out:
1911c94c0953SAl Viro 	if (unlikely(switched)) {
1912c94c0953SAl Viro 		d_lookup_done(dentry);
1913c94c0953SAl Viro 		dput(dentry);
1914c94c0953SAl Viro 	}
1915d9585277SAl Viro 	return err;
19160dd2b474SMiklos Szeredi 
19171da177e4SLinus Torvalds no_open:
19181472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1919c94c0953SAl Viro 	if (switched) {
1920c94c0953SAl Viro 		d_lookup_done(dentry);
1921c94c0953SAl Viro 		if (!res)
1922c94c0953SAl Viro 			res = dentry;
1923c94c0953SAl Viro 		else
1924c94c0953SAl Viro 			dput(dentry);
1925c94c0953SAl Viro 	}
19260dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1927c94c0953SAl Viro 		return PTR_ERR(res);
1928e45198a6SAl Viro 	return finish_no_open(file, res);
19291da177e4SLinus Torvalds }
193089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
19311da177e4SLinus Torvalds 
1932c7944ebbSTrond Myklebust static int
1933c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1934c7944ebbSTrond Myklebust 			  unsigned int flags)
19351da177e4SLinus Torvalds {
1936657e94b6SNick Piggin 	struct inode *inode;
19371da177e4SLinus Torvalds 
1938fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1939c7944ebbSTrond Myklebust 		goto full_reval;
1940eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1941c7944ebbSTrond Myklebust 		goto full_reval;
19422b484297STrond Myklebust 
19432b0143b5SDavid Howells 	inode = d_inode(dentry);
19442b484297STrond Myklebust 
19451da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
19461da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
19471da177e4SLinus Torvalds 	 */
1948c7944ebbSTrond Myklebust 	if (inode == NULL)
1949c7944ebbSTrond Myklebust 		goto full_reval;
195049317a7fSNeilBrown 
1951efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
1952c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1953216d5d06STrond Myklebust 
19541da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
19551da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1956c7944ebbSTrond Myklebust 		goto full_reval;
1957c7944ebbSTrond Myklebust 
19581da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1959c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1960c7944ebbSTrond Myklebust 		goto reval_dentry;
1961c7944ebbSTrond Myklebust 
1962c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1963c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1964c7944ebbSTrond Myklebust 		goto reval_dentry;
19651da177e4SLinus Torvalds 
19660ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1967c7944ebbSTrond Myklebust 	return 1;
1968c7944ebbSTrond Myklebust reval_dentry:
1969c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1970c7944ebbSTrond Myklebust 		return -ECHILD;
197142f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
19720ef97dcfSMiklos Szeredi 
1973c7944ebbSTrond Myklebust full_reval:
1974c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1975c7944ebbSTrond Myklebust }
1976535918f1STrond Myklebust 
1977c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1978c7944ebbSTrond Myklebust {
1979c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1980c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1981c0204fd2STrond Myklebust }
1982c0204fd2STrond Myklebust 
19831da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
19841da177e4SLinus Torvalds 
1985406cd915SBenjamin Coddington struct dentry *
1986406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
19871775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
19881775fd3eSDavid Quigley 				struct nfs4_label *label)
19891da177e4SLinus Torvalds {
1990fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
19912b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
19921da177e4SLinus Torvalds 	struct inode *inode;
1993b0c6108eSAl Viro 	struct dentry *d;
1994406cd915SBenjamin Coddington 	int error;
19951da177e4SLinus Torvalds 
1996fab728e1STrond Myklebust 	d_drop(dentry);
1997fab728e1STrond Myklebust 
19981da177e4SLinus Torvalds 	if (fhandle->size == 0) {
1999f7b37b8bSTrond Myklebust 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
20001da177e4SLinus Torvalds 		if (error)
2001fab728e1STrond Myklebust 			goto out_error;
20021da177e4SLinus Torvalds 	}
20035724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
20041da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
20051da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2006a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
2007a841b54dSTrond Myklebust 				fattr, NULL, NULL);
20081da177e4SLinus Torvalds 		if (error < 0)
2009fab728e1STrond Myklebust 			goto out_error;
20101da177e4SLinus Torvalds 	}
20111775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
2012b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
2013fab728e1STrond Myklebust out:
2014fab728e1STrond Myklebust 	dput(parent);
2015406cd915SBenjamin Coddington 	return d;
2016fab728e1STrond Myklebust out_error:
2017fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
2018406cd915SBenjamin Coddington 	d = ERR_PTR(error);
2019406cd915SBenjamin Coddington 	goto out;
2020406cd915SBenjamin Coddington }
2021406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2022406cd915SBenjamin Coddington 
2023406cd915SBenjamin Coddington /*
2024406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
2025406cd915SBenjamin Coddington  */
2026406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2027406cd915SBenjamin Coddington 				struct nfs_fattr *fattr,
2028406cd915SBenjamin Coddington 				struct nfs4_label *label)
2029406cd915SBenjamin Coddington {
2030406cd915SBenjamin Coddington 	struct dentry *d;
2031406cd915SBenjamin Coddington 
2032406cd915SBenjamin Coddington 	d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
2033406cd915SBenjamin Coddington 	if (IS_ERR(d))
2034406cd915SBenjamin Coddington 		return PTR_ERR(d);
2035406cd915SBenjamin Coddington 
2036406cd915SBenjamin Coddington 	/* Callers don't care */
2037406cd915SBenjamin Coddington 	dput(d);
2038406cd915SBenjamin Coddington 	return 0;
20391da177e4SLinus Torvalds }
2040ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds /*
20431da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
20441da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
20451da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
20461da177e4SLinus Torvalds  * reply path made it appear to have failed.
20471da177e4SLinus Torvalds  */
2048597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
2049ebfc3b49SAl Viro 		umode_t mode, bool excl)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	struct iattr attr;
2052ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
20531da177e4SLinus Torvalds 	int error;
20541da177e4SLinus Torvalds 
20551e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
20566de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	attr.ia_mode = mode;
20591da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20601da177e4SLinus Torvalds 
20618b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
20628867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
20638b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
20641da177e4SLinus Torvalds 	if (error != 0)
20651da177e4SLinus Torvalds 		goto out_err;
20661da177e4SLinus Torvalds 	return 0;
20671da177e4SLinus Torvalds out_err:
20681da177e4SLinus Torvalds 	d_drop(dentry);
20691da177e4SLinus Torvalds 	return error;
20701da177e4SLinus Torvalds }
2071ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds /*
20741da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
20751da177e4SLinus Torvalds  */
2076597d9289SBryan Schumaker int
20771a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	struct iattr attr;
20801da177e4SLinus Torvalds 	int status;
20811da177e4SLinus Torvalds 
20821e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
20836de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	attr.ia_mode = mode;
20861da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20871da177e4SLinus Torvalds 
20881ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
20891da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
20901ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
20911da177e4SLinus Torvalds 	if (status != 0)
20921da177e4SLinus Torvalds 		goto out_err;
20931da177e4SLinus Torvalds 	return 0;
20941da177e4SLinus Torvalds out_err:
20951da177e4SLinus Torvalds 	d_drop(dentry);
20961da177e4SLinus Torvalds 	return status;
20971da177e4SLinus Torvalds }
2098ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds /*
21011da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
21021da177e4SLinus Torvalds  */
2103597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds 	struct iattr attr;
21061da177e4SLinus Torvalds 	int error;
21071da177e4SLinus Torvalds 
21081e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
21096de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
21121da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
21131da177e4SLinus Torvalds 
21141ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
21151da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
21161ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
21171da177e4SLinus Torvalds 	if (error != 0)
21181da177e4SLinus Torvalds 		goto out_err;
21191da177e4SLinus Torvalds 	return 0;
21201da177e4SLinus Torvalds out_err:
21211da177e4SLinus Torvalds 	d_drop(dentry);
21221da177e4SLinus Torvalds 	return error;
21231da177e4SLinus Torvalds }
2124ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
21251da177e4SLinus Torvalds 
2126d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2127d45b9d8bSTrond Myklebust {
2128dc3f4198SAl Viro 	if (simple_positive(dentry))
2129d45b9d8bSTrond Myklebust 		d_delete(dentry);
2130d45b9d8bSTrond Myklebust }
2131d45b9d8bSTrond Myklebust 
2132597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
21331da177e4SLinus Torvalds {
21341da177e4SLinus Torvalds 	int error;
21351da177e4SLinus Torvalds 
21361e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
21376de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21381da177e4SLinus Torvalds 
21391ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
21402b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2141884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
21421da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
21431da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2144ba6c0592STrond Myklebust 		switch (error) {
2145ba6c0592STrond Myklebust 		case 0:
21462b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2147ba6c0592STrond Myklebust 			break;
2148ba6c0592STrond Myklebust 		case -ENOENT:
2149d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2150ba6c0592STrond Myklebust 		}
2151884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2152ba6c0592STrond Myklebust 	} else
2153ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
21541ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	return error;
21571da177e4SLinus Torvalds }
2158ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds /*
21611da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
21621da177e4SLinus Torvalds  * and after checking that the file has only one user.
21631da177e4SLinus Torvalds  *
21641da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
21651da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
21661da177e4SLinus Torvalds  */
21671da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
21681da177e4SLinus Torvalds {
21692b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
21702b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
21711da177e4SLinus Torvalds 	int error = -EBUSY;
21721da177e4SLinus Torvalds 
21736de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
21761da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
21771da177e4SLinus Torvalds 		error = 0;
21781da177e4SLinus Torvalds 		goto out;
21791da177e4SLinus Torvalds 	}
21801da177e4SLinus Torvalds 
21811ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
21821da177e4SLinus Torvalds 	if (inode != NULL) {
2183912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
21841da177e4SLinus Torvalds 		if (error == 0)
21851b83d707STrond Myklebust 			nfs_drop_nlink(inode);
21861da177e4SLinus Torvalds 	} else
2187912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2188d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2189d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
21901ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
21911da177e4SLinus Torvalds out:
21921da177e4SLinus Torvalds 	return error;
21931da177e4SLinus Torvalds }
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
21961da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
21971da177e4SLinus Torvalds  *
21981da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
21991da177e4SLinus Torvalds  */
2200597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
22011da177e4SLinus Torvalds {
22021da177e4SLinus Torvalds 	int error;
22031da177e4SLinus Torvalds 	int need_rehash = 0;
22041da177e4SLinus Torvalds 
22051e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
22066de1472fSAl Viro 		dir->i_ino, dentry);
22071da177e4SLinus Torvalds 
22081ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
22091da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
221084d08fa8SAl Viro 	if (d_count(dentry) > 1) {
22111da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2212ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
22132b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
22141da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
22151ca42382STrond Myklebust 		goto out;
22161da177e4SLinus Torvalds 	}
22171da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
22181da177e4SLinus Torvalds 		__d_drop(dentry);
22191da177e4SLinus Torvalds 		need_rehash = 1;
22201da177e4SLinus Torvalds 	}
22211da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
22221da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
2223d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
22241da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
22251da177e4SLinus Torvalds 	} else if (need_rehash)
22261da177e4SLinus Torvalds 		d_rehash(dentry);
22271ca42382STrond Myklebust out:
22281ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
22291da177e4SLinus Torvalds 	return error;
22301da177e4SLinus Torvalds }
2231ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
22321da177e4SLinus Torvalds 
2233873101b3SChuck Lever /*
2234873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2235873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2236873101b3SChuck Lever  * using prepare_write/commit_write.
2237873101b3SChuck Lever  *
2238873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2239873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2240873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2241873101b3SChuck Lever  * symlink request has completed on the server.
2242873101b3SChuck Lever  *
2243873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2244873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2245873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2246873101b3SChuck Lever  * and move the raw page into its mapping.
2247873101b3SChuck Lever  */
2248597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
22491da177e4SLinus Torvalds {
2250873101b3SChuck Lever 	struct page *page;
2251873101b3SChuck Lever 	char *kaddr;
22521da177e4SLinus Torvalds 	struct iattr attr;
2253873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
22541da177e4SLinus Torvalds 	int error;
22551da177e4SLinus Torvalds 
22561e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
22576de1472fSAl Viro 		dir->i_ino, dentry, symname);
22581da177e4SLinus Torvalds 
2259873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2260873101b3SChuck Lever 		return -ENAMETOOLONG;
22611da177e4SLinus Torvalds 
2262873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2263873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
22641da177e4SLinus Torvalds 
2265e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
226676566991STrond Myklebust 	if (!page)
2267873101b3SChuck Lever 		return -ENOMEM;
2268873101b3SChuck Lever 
2269e8ecde25SAl Viro 	kaddr = page_address(page);
2270873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2271873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2272873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2273873101b3SChuck Lever 
22741ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
227594a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
22761ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2277873101b3SChuck Lever 	if (error != 0) {
22781e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2279873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
22806de1472fSAl Viro 			dentry, symname, error);
22811da177e4SLinus Torvalds 		d_drop(dentry);
2282873101b3SChuck Lever 		__free_page(page);
22831da177e4SLinus Torvalds 		return error;
22841da177e4SLinus Torvalds 	}
22851da177e4SLinus Torvalds 
2286873101b3SChuck Lever 	/*
2287873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2288873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2289873101b3SChuck Lever 	 */
22902b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2291873101b3SChuck Lever 							GFP_KERNEL)) {
2292873101b3SChuck Lever 		SetPageUptodate(page);
2293873101b3SChuck Lever 		unlock_page(page);
2294a0b54addSRafael Aquini 		/*
2295a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2296a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2297a0b54addSRafael Aquini 		 */
229809cbfeafSKirill A. Shutemov 		put_page(page);
2299873101b3SChuck Lever 	} else
2300873101b3SChuck Lever 		__free_page(page);
2301873101b3SChuck Lever 
2302873101b3SChuck Lever 	return 0;
2303873101b3SChuck Lever }
2304ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2305873101b3SChuck Lever 
2306597d9289SBryan Schumaker int
23071da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
23081da177e4SLinus Torvalds {
23092b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
23101da177e4SLinus Torvalds 	int error;
23111da177e4SLinus Torvalds 
23126de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
23136de1472fSAl Viro 		old_dentry, dentry);
23141da177e4SLinus Torvalds 
23151fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
23169697d234STrond Myklebust 	d_drop(dentry);
23171da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2318cf809556STrond Myklebust 	if (error == 0) {
23197de9c6eeSAl Viro 		ihold(inode);
23209697d234STrond Myklebust 		d_add(dentry, inode);
2321cf809556STrond Myklebust 	}
23221fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
23231da177e4SLinus Torvalds 	return error;
23241da177e4SLinus Torvalds }
2325ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds /*
23281da177e4SLinus Torvalds  * RENAME
23291da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
23301da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
23311da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
23321da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
23331da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
23341da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
23351da177e4SLinus Torvalds  *
23361da177e4SLinus Torvalds  * FIXED.
23371da177e4SLinus Torvalds  *
23381da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
23391da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
23401da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
23411da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
23421da177e4SLinus Torvalds  * using the inode layer
23431da177e4SLinus Torvalds  *
23441da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
23451da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
23461da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
23471da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
23481da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
23491da177e4SLinus Torvalds  * the rename.
23501da177e4SLinus Torvalds  */
2351597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
23521cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
23531cd66c93SMiklos Szeredi 	       unsigned int flags)
23541da177e4SLinus Torvalds {
23552b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
23562b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2357d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
235880a491fdSJeff Layton 	struct rpc_task *task;
23591da177e4SLinus Torvalds 	int error = -EBUSY;
23601da177e4SLinus Torvalds 
23611cd66c93SMiklos Szeredi 	if (flags)
23621cd66c93SMiklos Szeredi 		return -EINVAL;
23631cd66c93SMiklos Szeredi 
23646de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
23656de1472fSAl Viro 		 old_dentry, new_dentry,
236684d08fa8SAl Viro 		 d_count(new_dentry));
23671da177e4SLinus Torvalds 
236870ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
23691da177e4SLinus Torvalds 	/*
237028f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
237128f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
237228f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
237328f79a1aSMiklos Szeredi 	 * the new target.
23741da177e4SLinus Torvalds 	 */
237527226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
237627226104SMiklos Szeredi 		/*
237727226104SMiklos Szeredi 		 * To prevent any new references to the target during the
237827226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
237927226104SMiklos Szeredi 		 */
2380d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
238127226104SMiklos Szeredi 			d_drop(new_dentry);
2382d9f29500SBenjamin Coddington 			rehash = new_dentry;
2383d9f29500SBenjamin Coddington 		}
238427226104SMiklos Szeredi 
238584d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
23861da177e4SLinus Torvalds 			int err;
238727226104SMiklos Szeredi 
23881da177e4SLinus Torvalds 			/* copy the target dentry's name */
23891da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
23901da177e4SLinus Torvalds 					 &new_dentry->d_name);
23911da177e4SLinus Torvalds 			if (!dentry)
23921da177e4SLinus Torvalds 				goto out;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
23951da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
239624e93025SMiklos Szeredi 			if (err)
23971da177e4SLinus Torvalds 				goto out;
239824e93025SMiklos Szeredi 
239924e93025SMiklos Szeredi 			new_dentry = dentry;
2400d9f29500SBenjamin Coddington 			rehash = NULL;
240124e93025SMiklos Szeredi 			new_inode = NULL;
2402b1e4adf4STrond Myklebust 		}
240327226104SMiklos Szeredi 	}
24041da177e4SLinus Torvalds 
2405d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
240680a491fdSJeff Layton 	if (IS_ERR(task)) {
240780a491fdSJeff Layton 		error = PTR_ERR(task);
240880a491fdSJeff Layton 		goto out;
240980a491fdSJeff Layton 	}
241080a491fdSJeff Layton 
241180a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2412818a8dbeSBenjamin Coddington 	if (error != 0) {
2413818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2414818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2415818a8dbeSBenjamin Coddington 		smp_wmb();
2416818a8dbeSBenjamin Coddington 	} else
241780a491fdSJeff Layton 		error = task->tk_status;
241880a491fdSJeff Layton 	rpc_put_task(task);
241959a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
242059a707b0STrond Myklebust 	if (error == 0) {
242159a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
242259a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
242359a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
242459a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
242559a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
242659a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
242759a707b0STrond Myklebust 	}
24281da177e4SLinus Torvalds out:
2429d9f29500SBenjamin Coddington 	if (rehash)
2430d9f29500SBenjamin Coddington 		d_rehash(rehash);
243170ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
243270ded201STrond Myklebust 			new_dir, new_dentry, error);
2433d9f29500SBenjamin Coddington 	if (!error) {
2434d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2435d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2436d9f29500SBenjamin Coddington 		/*
2437d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2438d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2439d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2440d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2441d9f29500SBenjamin Coddington 		 */
2442d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2443d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2444d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2445d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2446d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2447d9f29500SBenjamin Coddington 
24481da177e4SLinus Torvalds 	/* new dentry created? */
24491da177e4SLinus Torvalds 	if (dentry)
24501da177e4SLinus Torvalds 		dput(dentry);
24511da177e4SLinus Torvalds 	return error;
24521da177e4SLinus Torvalds }
2453ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
24541da177e4SLinus Torvalds 
2455cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2456cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2457cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2458cfcea3e8STrond Myklebust 
2459a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
24603a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
24613a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
24623a505845STrond Myklebust 
24631c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
24641c3c07e9STrond Myklebust {
2465b68572e0SNeilBrown 	put_cred(entry->cred);
2466f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
24674e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2468cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
24694e857c58SPeter Zijlstra 	smp_mb__after_atomic();
24701c3c07e9STrond Myklebust }
24711c3c07e9STrond Myklebust 
24721a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
24731a81bb8aSTrond Myklebust {
24741a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
24751a81bb8aSTrond Myklebust 
24761a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
24771a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
24781a81bb8aSTrond Myklebust 		list_del(&cache->lru);
24791a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
24801a81bb8aSTrond Myklebust 	}
24811a81bb8aSTrond Myklebust }
24821a81bb8aSTrond Myklebust 
24833a505845STrond Myklebust static unsigned long
24843a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2485979df72eSTrond Myklebust {
2486979df72eSTrond Myklebust 	LIST_HEAD(head);
2487aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2488979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
24891ab6c499SDave Chinner 	long freed = 0;
2490979df72eSTrond Myklebust 
2491a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2492aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2493979df72eSTrond Myklebust 		struct inode *inode;
2494979df72eSTrond Myklebust 
2495979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2496979df72eSTrond Myklebust 			break;
24979c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2498979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2499979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2500979df72eSTrond Myklebust 			goto remove_lru_entry;
2501979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2502979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2503979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2504979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
25051ab6c499SDave Chinner 		freed++;
2506979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2507979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2508979df72eSTrond Myklebust 					&nfs_access_lru_list);
2509979df72eSTrond Myklebust 		else {
2510979df72eSTrond Myklebust remove_lru_entry:
2511979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
25124e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2513979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
25144e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2515979df72eSTrond Myklebust 		}
251659844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2517979df72eSTrond Myklebust 	}
2518979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
25191a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
25201ab6c499SDave Chinner 	return freed;
25211ab6c499SDave Chinner }
25221ab6c499SDave Chinner 
25231ab6c499SDave Chinner unsigned long
25243a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
25253a505845STrond Myklebust {
25263a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
25273a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
25283a505845STrond Myklebust 
25293a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
25303a505845STrond Myklebust 		return SHRINK_STOP;
25313a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
25323a505845STrond Myklebust }
25333a505845STrond Myklebust 
25343a505845STrond Myklebust 
25353a505845STrond Myklebust unsigned long
25361ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
25371ab6c499SDave Chinner {
253855f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2539979df72eSTrond Myklebust }
2540979df72eSTrond Myklebust 
25413a505845STrond Myklebust static void
25423a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
25433a505845STrond Myklebust {
25443a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
25453a505845STrond Myklebust 	unsigned long diff;
25463a505845STrond Myklebust 	unsigned int nr_to_scan;
25473a505845STrond Myklebust 
25483a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
25493a505845STrond Myklebust 		return;
25503a505845STrond Myklebust 	nr_to_scan = 100;
25513a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
25523a505845STrond Myklebust 	if (diff < nr_to_scan)
25533a505845STrond Myklebust 		nr_to_scan = diff;
25543a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
25553a505845STrond Myklebust }
25563a505845STrond Myklebust 
25571a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
25581c3c07e9STrond Myklebust {
25591c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
25601a81bb8aSTrond Myklebust 	struct rb_node *n;
25611c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
25621c3c07e9STrond Myklebust 
25631c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
25641c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
25651c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
25661c3c07e9STrond Myklebust 		rb_erase(n, root_node);
25671a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
25681c3c07e9STrond Myklebust 	}
25691c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
25701c3c07e9STrond Myklebust }
25711c3c07e9STrond Myklebust 
25721c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
25731c3c07e9STrond Myklebust {
25741a81bb8aSTrond Myklebust 	LIST_HEAD(head);
25751a81bb8aSTrond Myklebust 
25761a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
25771a81bb8aSTrond Myklebust 		return;
2578cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2579cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
25801a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2581cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2582cfcea3e8STrond Myklebust 
25831c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
25841a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
25851a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
25861a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
25871a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
25881c3c07e9STrond Myklebust }
25891c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
25901c3c07e9STrond Myklebust 
2591b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
25921c3c07e9STrond Myklebust {
25931c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
25941c3c07e9STrond Myklebust 
25951c3c07e9STrond Myklebust 	while (n != NULL) {
2596b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2597b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2598b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
25991c3c07e9STrond Myklebust 
2600b68572e0SNeilBrown 		if (cmp < 0)
26011c3c07e9STrond Myklebust 			n = n->rb_left;
2602b68572e0SNeilBrown 		else if (cmp > 0)
26031c3c07e9STrond Myklebust 			n = n->rb_right;
26041c3c07e9STrond Myklebust 		else
26051c3c07e9STrond Myklebust 			return entry;
26061c3c07e9STrond Myklebust 	}
26071c3c07e9STrond Myklebust 	return NULL;
26081c3c07e9STrond Myklebust }
26091c3c07e9STrond Myklebust 
2610d2ae4f8bSFrank van der Linden static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
26111da177e4SLinus Torvalds {
261255296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
26131c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
261457b69181STrond Myklebust 	bool retry = true;
261557b69181STrond Myklebust 	int err;
26161da177e4SLinus Torvalds 
26171c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
261857b69181STrond Myklebust 	for(;;) {
26191c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
26201c3c07e9STrond Myklebust 			goto out_zap;
26211c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
262257b69181STrond Myklebust 		err = -ENOENT;
26231c3c07e9STrond Myklebust 		if (cache == NULL)
26241c3c07e9STrond Myklebust 			goto out;
262557b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
262621c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
262757b69181STrond Myklebust 			break;
26285c965db8STrond Myklebust 		if (!retry)
26295c965db8STrond Myklebust 			break;
263057b69181STrond Myklebust 		err = -ECHILD;
263157b69181STrond Myklebust 		if (!may_block)
263257b69181STrond Myklebust 			goto out;
263357b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
263457b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
263557b69181STrond Myklebust 		if (err)
263657b69181STrond Myklebust 			return err;
263757b69181STrond Myklebust 		spin_lock(&inode->i_lock);
263857b69181STrond Myklebust 		retry = false;
263957b69181STrond Myklebust 	}
26401c3c07e9STrond Myklebust 	res->cred = cache->cred;
26411c3c07e9STrond Myklebust 	res->mask = cache->mask;
2642cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
26431c3c07e9STrond Myklebust 	err = 0;
26441c3c07e9STrond Myklebust out:
26451c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
26461c3c07e9STrond Myklebust 	return err;
26471c3c07e9STrond Myklebust out_zap:
26481a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
26491a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
26501c3c07e9STrond Myklebust 	return -ENOENT;
26511c3c07e9STrond Myklebust }
26521c3c07e9STrond Myklebust 
2653b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2654f682a398SNeilBrown {
2655f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2656f682a398SNeilBrown 	 * but do it without locking.
2657f682a398SNeilBrown 	 */
2658f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2659f682a398SNeilBrown 	struct nfs_access_entry *cache;
2660f682a398SNeilBrown 	int err = -ECHILD;
2661f682a398SNeilBrown 	struct list_head *lh;
2662f682a398SNeilBrown 
2663f682a398SNeilBrown 	rcu_read_lock();
2664f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2665f682a398SNeilBrown 		goto out;
26669f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2667f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2668f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
26699a206de2STrond Myklebust 	    cred_fscmp(cred, cache->cred) != 0)
2670f682a398SNeilBrown 		cache = NULL;
2671f682a398SNeilBrown 	if (cache == NULL)
2672f682a398SNeilBrown 		goto out;
267321c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2674f682a398SNeilBrown 		goto out;
2675f682a398SNeilBrown 	res->cred = cache->cred;
2676f682a398SNeilBrown 	res->mask = cache->mask;
267721c3ba7eSTrond Myklebust 	err = 0;
2678f682a398SNeilBrown out:
2679f682a398SNeilBrown 	rcu_read_unlock();
2680f682a398SNeilBrown 	return err;
2681f682a398SNeilBrown }
2682f682a398SNeilBrown 
2683d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2684d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block)
2685d2ae4f8bSFrank van der Linden {
2686d2ae4f8bSFrank van der Linden 	int status;
2687d2ae4f8bSFrank van der Linden 
2688d2ae4f8bSFrank van der Linden 	status = nfs_access_get_cached_rcu(inode, cred, res);
2689d2ae4f8bSFrank van der Linden 	if (status != 0)
2690d2ae4f8bSFrank van der Linden 		status = nfs_access_get_cached_locked(inode, cred, res,
2691d2ae4f8bSFrank van der Linden 		    may_block);
2692d2ae4f8bSFrank van der Linden 
2693d2ae4f8bSFrank van der Linden 	return status;
2694d2ae4f8bSFrank van der Linden }
2695d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2696d2ae4f8bSFrank van der Linden 
26971c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
26981c3c07e9STrond Myklebust {
2699cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2700cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
27011c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
27021c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
27031c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2704b68572e0SNeilBrown 	int cmp;
27051c3c07e9STrond Myklebust 
27061c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
27071c3c07e9STrond Myklebust 	while (*p != NULL) {
27081c3c07e9STrond Myklebust 		parent = *p;
27091c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2710b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
27111c3c07e9STrond Myklebust 
2712b68572e0SNeilBrown 		if (cmp < 0)
27131c3c07e9STrond Myklebust 			p = &parent->rb_left;
2714b68572e0SNeilBrown 		else if (cmp > 0)
27151c3c07e9STrond Myklebust 			p = &parent->rb_right;
27161c3c07e9STrond Myklebust 		else
27171c3c07e9STrond Myklebust 			goto found;
27181c3c07e9STrond Myklebust 	}
27191c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
27201c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2721cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
27221c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
27231c3c07e9STrond Myklebust 	return;
27241c3c07e9STrond Myklebust found:
27251c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2726cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2727cfcea3e8STrond Myklebust 	list_del(&entry->lru);
27281c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
27291c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds 
27326168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
27331da177e4SLinus Torvalds {
27341c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
27351c3c07e9STrond Myklebust 	if (cache == NULL)
27361c3c07e9STrond Myklebust 		return;
27371c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2738b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
27391da177e4SLinus Torvalds 	cache->mask = set->mask;
27401c3c07e9STrond Myklebust 
2741f682a398SNeilBrown 	/* The above field assignments must be visible
2742f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2743f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2744f682a398SNeilBrown 	 */
2745f682a398SNeilBrown 	smp_wmb();
27461c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2747cfcea3e8STrond Myklebust 
2748cfcea3e8STrond Myklebust 	/* Update accounting */
27494e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2750cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
27514e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2752cfcea3e8STrond Myklebust 
2753cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
27541a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2755cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
27561a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
27571a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
27581a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2759cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2760cfcea3e8STrond Myklebust 	}
27613a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
27621da177e4SLinus Torvalds }
27636168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
27646168f62cSWeston Andros Adamson 
27653c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
27663c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
27673c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
27683c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
27693c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
27703c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2771ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
27723c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
27733c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
277415d4b73aSTrond Myklebust static int
2775ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
277615d4b73aSTrond Myklebust {
277715d4b73aSTrond Myklebust 	int mask = 0;
277815d4b73aSTrond Myklebust 
277915d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
278015d4b73aSTrond Myklebust 		mask |= MAY_READ;
2781ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2782ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
278315d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2784ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
278515d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2786ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2787ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2788ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2789ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
279015d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2791ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2792ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
279315d4b73aSTrond Myklebust 	return mask;
279415d4b73aSTrond Myklebust }
279515d4b73aSTrond Myklebust 
27966168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
27976168f62cSWeston Andros Adamson {
2798bd8b2441STrond Myklebust 	entry->mask = access_result;
27996168f62cSWeston Andros Adamson }
28006168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
28011da177e4SLinus Torvalds 
2802b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
28031da177e4SLinus Torvalds {
28041da177e4SLinus Torvalds 	struct nfs_access_entry cache;
280557b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2806e8194b7dSTrond Myklebust 	int cache_mask = -1;
28071da177e4SLinus Torvalds 	int status;
28081da177e4SLinus Torvalds 
2809f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2810f4ce1299STrond Myklebust 
281157b69181STrond Myklebust 	status = nfs_access_get_cached(inode, cred, &cache, may_block);
28121da177e4SLinus Torvalds 	if (status == 0)
2813f4ce1299STrond Myklebust 		goto out_cached;
28141da177e4SLinus Torvalds 
2815f3324a2aSNeilBrown 	status = -ECHILD;
281657b69181STrond Myklebust 	if (!may_block)
2817f3324a2aSNeilBrown 		goto out;
2818f3324a2aSNeilBrown 
28191750d929SAnna Schumaker 	/*
28201750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
28211750d929SAnna Schumaker 	 */
28221750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
282372832a24SFrank van der Linden 	if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
282472832a24SFrank van der Linden 		cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
282572832a24SFrank van der Linden 		    NFS_ACCESS_XALIST;
282672832a24SFrank van der Linden 	}
28271750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
28281750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
28291750d929SAnna Schumaker 	else
28301750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
28311da177e4SLinus Torvalds 	cache.cred = cred;
28321da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2833a71ee337SSuresh Jayaraman 	if (status != 0) {
2834a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2835a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
283693ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
283793ce4af7STrond Myklebust 			else
283893ce4af7STrond Myklebust 				nfs_zap_caches(inode);
2839a71ee337SSuresh Jayaraman 		}
2840f4ce1299STrond Myklebust 		goto out;
2841a71ee337SSuresh Jayaraman 	}
28421da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2843f4ce1299STrond Myklebust out_cached:
2844ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2845bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2846f4ce1299STrond Myklebust 		status = -EACCES;
28471da177e4SLinus Torvalds out:
2848e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
2849f4ce1299STrond Myklebust 	return status;
28501da177e4SLinus Torvalds }
28511da177e4SLinus Torvalds 
2852af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2853af22f94aSTrond Myklebust {
2854af22f94aSTrond Myklebust 	int mask = 0;
2855af22f94aSTrond Myklebust 
2856f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2857f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2858f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2859f8d9a897SWeston Andros Adamson 	} else {
28608a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2861af22f94aSTrond Myklebust 			mask |= MAY_READ;
28628a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2863af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2864f8d9a897SWeston Andros Adamson 	}
2865f8d9a897SWeston Andros Adamson 
2866af22f94aSTrond Myklebust 	return mask;
2867af22f94aSTrond Myklebust }
2868af22f94aSTrond Myklebust 
2869b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2870af22f94aSTrond Myklebust {
2871af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2872af22f94aSTrond Myklebust }
287389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2874af22f94aSTrond Myklebust 
28755c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
28765c5fc09aSTrond Myklebust {
28775c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
287821c3ba7eSTrond Myklebust 	int ret = 0;
28795c5fc09aSTrond Myklebust 
28803825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
28813825827eSTrond Myklebust 		return 0;
2882cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
28835c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
288421c3ba7eSTrond Myklebust 			return -ECHILD;
288521c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
288621c3ba7eSTrond Myklebust 	}
28875c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
28885c5fc09aSTrond Myklebust 		ret = -EACCES;
28895c5fc09aSTrond Myklebust 	return ret;
28905c5fc09aSTrond Myklebust }
28915c5fc09aSTrond Myklebust 
289210556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
28931da177e4SLinus Torvalds {
2894b68572e0SNeilBrown 	const struct cred *cred = current_cred();
28951da177e4SLinus Torvalds 	int res = 0;
28961da177e4SLinus Torvalds 
289791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
289891d5b470SChuck Lever 
2899e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
29001da177e4SLinus Torvalds 		goto out;
29011da177e4SLinus Torvalds 	/* Is this sys_access() ? */
29029cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
29031da177e4SLinus Torvalds 		goto force_lookup;
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
29061da177e4SLinus Torvalds 		case S_IFLNK:
29071da177e4SLinus Torvalds 			goto out;
29081da177e4SLinus Torvalds 		case S_IFREG:
2909762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2910762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2911762674f8STrond Myklebust 				return 0;
29121da177e4SLinus Torvalds 			break;
29131da177e4SLinus Torvalds 		case S_IFDIR:
29141da177e4SLinus Torvalds 			/*
29151da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
29161da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
29171da177e4SLinus Torvalds 			 */
29181da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
29191da177e4SLinus Torvalds 				goto out;
29201da177e4SLinus Torvalds 	}
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds force_lookup:
29231da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
29241da177e4SLinus Torvalds 		goto out_notsup;
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
29271da177e4SLinus Torvalds out:
29285c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
29295c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2930f696a365SMiklos Szeredi 
29311e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
29321e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
29331da177e4SLinus Torvalds 	return res;
29341da177e4SLinus Torvalds out_notsup:
2935d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2936d51ac1a8SNeilBrown 		return -ECHILD;
2937d51ac1a8SNeilBrown 
29381da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
29391da177e4SLinus Torvalds 	if (res == 0)
29402830ba7fSAl Viro 		res = generic_permission(inode, mask);
29411e7cb3dcSChuck Lever 	goto out;
29421da177e4SLinus Torvalds }
2943ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
29441da177e4SLinus Torvalds 
29451da177e4SLinus Torvalds /*
29461da177e4SLinus Torvalds  * Local variables:
29471da177e4SLinus Torvalds  *  version-control: t
29481da177e4SLinus Torvalds  *  kept-new-versions: 5
29491da177e4SLinus Torvalds  * End:
29501da177e4SLinus Torvalds  */
2951