xref: /openbmc/linux/fs/nfs/dir.c (revision 35df59d3ef693292840a61cdb04b39d8c9412f4e)
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,
143762567b7STrond Myklebust 		      page_is_eof : 1,
144762567b7STrond 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;
182762567b7STrond 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 
202*35df59d3STrond Myklebust static struct page *
203*35df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
204*35df59d3STrond Myklebust {
205*35df59d3STrond Myklebust 	struct page *page = alloc_page(gfp_flags);
206*35df59d3STrond Myklebust 	if (page)
207*35df59d3STrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie);
208*35df59d3STrond Myklebust 	return page;
209*35df59d3STrond Myklebust }
210*35df59d3STrond Myklebust 
211*35df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page)
212*35df59d3STrond Myklebust {
213*35df59d3STrond Myklebust 	if (page) {
214*35df59d3STrond Myklebust 		nfs_readdir_clear_array(page);
215*35df59d3STrond Myklebust 		put_page(page);
216*35df59d3STrond Myklebust 	}
217*35df59d3STrond Myklebust }
218*35df59d3STrond Myklebust 
219b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
220b1e21c97STrond Myklebust {
221b1e21c97STrond Myklebust 	array->page_is_eof = 1;
222b1e21c97STrond Myklebust 	array->page_full = 1;
223b1e21c97STrond Myklebust }
224b1e21c97STrond Myklebust 
225b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
226b1e21c97STrond Myklebust {
227b1e21c97STrond Myklebust 	return array->page_full;
228b1e21c97STrond Myklebust }
229b1e21c97STrond Myklebust 
230d1bacf9eSBryan Schumaker /*
231d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
232d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
233d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
234d1bacf9eSBryan Schumaker  */
235a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
236d1bacf9eSBryan Schumaker {
237a52a8a6aSTrond Myklebust 	const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
238a52a8a6aSTrond Myklebust 
23904e4bd1cSCatalin Marinas 	/*
24004e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
24104e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
24204e4bd1cSCatalin Marinas 	 */
243a52a8a6aSTrond Myklebust 	if (ret != NULL)
244a52a8a6aSTrond Myklebust 		kmemleak_not_leak(ret);
245a52a8a6aSTrond Myklebust 	return ret;
246d1bacf9eSBryan Schumaker }
247d1bacf9eSBryan Schumaker 
248b1e21c97STrond Myklebust /*
249b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
250b1e21c97STrond Myklebust  */
251b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
252b1e21c97STrond Myklebust {
253b1e21c97STrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
254b1e21c97STrond Myklebust 
255b1e21c97STrond Myklebust 	if (array->page_full)
256b1e21c97STrond Myklebust 		return -ENOSPC;
257b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size + 1];
258b1e21c97STrond Myklebust 	if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
259b1e21c97STrond Myklebust 		array->page_full = 1;
260b1e21c97STrond Myklebust 		return -ENOSPC;
261b1e21c97STrond Myklebust 	}
262b1e21c97STrond Myklebust 	return 0;
263b1e21c97STrond Myklebust }
264b1e21c97STrond Myklebust 
265d1bacf9eSBryan Schumaker static
266d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
267d1bacf9eSBryan Schumaker {
268a52a8a6aSTrond Myklebust 	struct nfs_cache_array *array;
2694a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
270a52a8a6aSTrond Myklebust 	const char *name;
2714a201d6eSTrond Myklebust 	int ret;
2724a201d6eSTrond Myklebust 
273a52a8a6aSTrond Myklebust 	name = nfs_readdir_copy_name(entry->name, entry->len);
274a52a8a6aSTrond Myklebust 	if (!name)
275a52a8a6aSTrond Myklebust 		return -ENOMEM;
276a52a8a6aSTrond Myklebust 
277a52a8a6aSTrond Myklebust 	array = kmap_atomic(page);
278b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
279a52a8a6aSTrond Myklebust 	if (ret) {
280a52a8a6aSTrond Myklebust 		kfree(name);
2813020093fSTrond Myklebust 		goto out;
282a52a8a6aSTrond Myklebust 	}
2833020093fSTrond Myklebust 
284b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
2854a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2864a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2870b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
288a52a8a6aSTrond Myklebust 	cache_entry->name_len = entry->len;
289a52a8a6aSTrond Myklebust 	cache_entry->name = name;
290d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
291762567b7STrond Myklebust 	if (array->last_cookie <= cache_entry->cookie)
292762567b7STrond Myklebust 		array->cookies_are_ordered = 0;
2938cd51a0cSTrond Myklebust 	array->size++;
29447c716cbSTrond Myklebust 	if (entry->eof != 0)
295b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
2964a201d6eSTrond Myklebust out:
297a52a8a6aSTrond Myklebust 	kunmap_atomic(array);
2984a201d6eSTrond Myklebust 	return ret;
299d1bacf9eSBryan Schumaker }
300d1bacf9eSBryan Schumaker 
3011f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
3021f1d4aa4STrond Myklebust 						pgoff_t index, u64 last_cookie)
3031f1d4aa4STrond Myklebust {
3041f1d4aa4STrond Myklebust 	struct page *page;
3051f1d4aa4STrond Myklebust 
3061f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
3071f1d4aa4STrond Myklebust 	if (page && !PageUptodate(page)) {
3081f1d4aa4STrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie);
3091f1d4aa4STrond Myklebust 		if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
3101f1d4aa4STrond Myklebust 			nfs_zap_mapping(mapping->host, mapping);
3111f1d4aa4STrond Myklebust 		SetPageUptodate(page);
3121f1d4aa4STrond Myklebust 	}
3131f1d4aa4STrond Myklebust 
3141f1d4aa4STrond Myklebust 	return page;
3151f1d4aa4STrond Myklebust }
3161f1d4aa4STrond Myklebust 
3171f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
3181f1d4aa4STrond Myklebust {
3191f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3201f1d4aa4STrond Myklebust 	u64 ret;
3211f1d4aa4STrond Myklebust 
3221f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3231f1d4aa4STrond Myklebust 	ret = array->last_cookie;
3241f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3251f1d4aa4STrond Myklebust 	return ret;
3261f1d4aa4STrond Myklebust }
3271f1d4aa4STrond Myklebust 
3281f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
3291f1d4aa4STrond Myklebust {
3301f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3311f1d4aa4STrond Myklebust 	bool ret;
3321f1d4aa4STrond Myklebust 
3331f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3341f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
3351f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3361f1d4aa4STrond Myklebust 	return ret;
3371f1d4aa4STrond Myklebust }
3381f1d4aa4STrond Myklebust 
339b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
340b1e21c97STrond Myklebust {
341b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
342b1e21c97STrond Myklebust 
343b1e21c97STrond Myklebust 	array = kmap_atomic(page);
344b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
345b1e21c97STrond Myklebust 	kunmap_atomic(array);
346b1e21c97STrond Myklebust }
347b1e21c97STrond Myklebust 
3483b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
3493b2a09f1STrond Myklebust {
3503b2a09f1STrond Myklebust 	unlock_page(page);
3513b2a09f1STrond Myklebust 	put_page(page);
3523b2a09f1STrond Myklebust }
3533b2a09f1STrond Myklebust 
3543b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
3553b2a09f1STrond Myklebust 					      pgoff_t index, u64 cookie)
3563b2a09f1STrond Myklebust {
3573b2a09f1STrond Myklebust 	struct page *page;
3583b2a09f1STrond Myklebust 
3593b2a09f1STrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
3603b2a09f1STrond Myklebust 	if (page) {
3613b2a09f1STrond Myklebust 		if (nfs_readdir_page_last_cookie(page) == cookie)
3623b2a09f1STrond Myklebust 			return page;
3633b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
3643b2a09f1STrond Myklebust 	}
3653b2a09f1STrond Myklebust 	return NULL;
3663b2a09f1STrond Myklebust }
3673b2a09f1STrond Myklebust 
36859e356a9STrond Myklebust static inline
36959e356a9STrond Myklebust int is_32bit_api(void)
37059e356a9STrond Myklebust {
37159e356a9STrond Myklebust #ifdef CONFIG_COMPAT
37259e356a9STrond Myklebust 	return in_compat_syscall();
37359e356a9STrond Myklebust #else
37459e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
37559e356a9STrond Myklebust #endif
37659e356a9STrond Myklebust }
37759e356a9STrond Myklebust 
37859e356a9STrond Myklebust static
37959e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
38059e356a9STrond Myklebust {
38159e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
38259e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
38359e356a9STrond Myklebust 		return false;
38459e356a9STrond Myklebust 	return true;
38559e356a9STrond Myklebust }
38659e356a9STrond Myklebust 
3876c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
3886c981effSTrond Myklebust 				      struct nfs_readdir_descriptor *desc)
389d1bacf9eSBryan Schumaker {
39023db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
391d1bacf9eSBryan Schumaker 	unsigned int index;
392d1bacf9eSBryan Schumaker 
393d1bacf9eSBryan Schumaker 	if (diff < 0)
394d1bacf9eSBryan Schumaker 		goto out_eof;
395d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
396b1e21c97STrond Myklebust 		if (array->page_is_eof)
397d1bacf9eSBryan Schumaker 			goto out_eof;
398d1bacf9eSBryan Schumaker 		return -EAGAIN;
399d1bacf9eSBryan Schumaker 	}
400d1bacf9eSBryan Schumaker 
401d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
4022e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
403d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
404d1bacf9eSBryan Schumaker 	return 0;
405d1bacf9eSBryan Schumaker out_eof:
4066089dd0dSThomas Meyer 	desc->eof = true;
407d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
408d1bacf9eSBryan Schumaker }
409d1bacf9eSBryan Schumaker 
4104db72b40SJeff Layton static bool
4114db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
4124db72b40SJeff Layton {
4134db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
4144db72b40SJeff Layton 		return false;
4154db72b40SJeff Layton 	smp_rmb();
4164db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
4174db72b40SJeff Layton }
4184db72b40SJeff Layton 
419762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
420762567b7STrond Myklebust 					      u64 cookie)
421762567b7STrond Myklebust {
422762567b7STrond Myklebust 	if (!array->cookies_are_ordered)
423762567b7STrond Myklebust 		return true;
424762567b7STrond Myklebust 	/* Optimisation for monotonically increasing cookies */
425762567b7STrond Myklebust 	if (cookie >= array->last_cookie)
426762567b7STrond Myklebust 		return false;
427762567b7STrond Myklebust 	if (array->size && cookie < array->array[0].cookie)
428762567b7STrond Myklebust 		return false;
429762567b7STrond Myklebust 	return true;
430762567b7STrond Myklebust }
431762567b7STrond Myklebust 
4326c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
4336c981effSTrond Myklebust 					 struct nfs_readdir_descriptor *desc)
434d1bacf9eSBryan Schumaker {
435d1bacf9eSBryan Schumaker 	int i;
4368ef2ce3eSBryan Schumaker 	loff_t new_pos;
437d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
438d1bacf9eSBryan Schumaker 
439762567b7STrond Myklebust 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
440762567b7STrond Myklebust 		goto check_eof;
441762567b7STrond Myklebust 
442d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
4432e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
444496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
4450c030806STrond Myklebust 
4468ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
4472e7a4641STrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount ||
4484db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
4492e7a4641STrond Myklebust 				desc->duped = 0;
4502e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
45159e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
4522e7a4641STrond Myklebust 				if (desc->duped > 0
4532e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
4540c030806STrond Myklebust 					if (printk_ratelimit()) {
4556de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
4560c030806STrond Myklebust 								"Please contact your server vendor.  "
457a52a8a6aSTrond Myklebust 								"The file: %s has duplicate cookie %llu\n",
458a52a8a6aSTrond Myklebust 								desc->file, array->array[i].name, desc->dir_cookie);
4590c030806STrond Myklebust 					}
4600c030806STrond Myklebust 					status = -ELOOP;
4610c030806STrond Myklebust 					goto out;
4620c030806STrond Myklebust 				}
4632e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
4642e7a4641STrond Myklebust 				desc->duped = -1;
4658ef2ce3eSBryan Schumaker 			}
46659e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
4672e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
46859e356a9STrond Myklebust 			else
46923db8620SAl Viro 				desc->ctx->pos = new_pos;
47059e356a9STrond Myklebust 			desc->prev_index = new_pos;
4718cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
47247c716cbSTrond Myklebust 			return 0;
4738cd51a0cSTrond Myklebust 		}
4748cd51a0cSTrond Myklebust 	}
475762567b7STrond Myklebust check_eof:
476b1e21c97STrond Myklebust 	if (array->page_is_eof) {
477d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
4782e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
4796089dd0dSThomas Meyer 			desc->eof = true;
480d1bacf9eSBryan Schumaker 	}
4810c030806STrond Myklebust out:
482d1bacf9eSBryan Schumaker 	return status;
483d1bacf9eSBryan Schumaker }
484d1bacf9eSBryan Schumaker 
4856c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
486d1bacf9eSBryan Schumaker {
487d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
48847c716cbSTrond Myklebust 	int status;
489d1bacf9eSBryan Schumaker 
490ed09222dSTrond Myklebust 	array = kmap_atomic(desc->page);
491d1bacf9eSBryan Schumaker 
4922e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
493d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
494d1bacf9eSBryan Schumaker 	else
495d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
496d1bacf9eSBryan Schumaker 
49747c716cbSTrond Myklebust 	if (status == -EAGAIN) {
4980aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
499e47c085aSTrond Myklebust 		desc->current_index += array->size;
50047c716cbSTrond Myklebust 		desc->page_index++;
50147c716cbSTrond Myklebust 	}
502ed09222dSTrond Myklebust 	kunmap_atomic(array);
503d1bacf9eSBryan Schumaker 	return status;
504d1bacf9eSBryan Schumaker }
505d1bacf9eSBryan Schumaker 
506d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
50793b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
508b593c09fSTrond Myklebust 				  __be32 *verf, u64 cookie,
509b593c09fSTrond Myklebust 				  struct page **pages, size_t bufsize,
510b593c09fSTrond Myklebust 				  __be32 *verf_res)
511d1bacf9eSBryan Schumaker {
51282e22a5eSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
51382e22a5eSTrond Myklebust 	struct nfs_readdir_arg arg = {
51482e22a5eSTrond Myklebust 		.dentry = file_dentry(desc->file),
51582e22a5eSTrond Myklebust 		.cred = desc->file->f_cred,
516b593c09fSTrond Myklebust 		.verf = verf,
51782e22a5eSTrond Myklebust 		.cookie = cookie,
51882e22a5eSTrond Myklebust 		.pages = pages,
51982e22a5eSTrond Myklebust 		.page_len = bufsize,
52082e22a5eSTrond Myklebust 		.plus = desc->plus,
52182e22a5eSTrond Myklebust 	};
52282e22a5eSTrond Myklebust 	struct nfs_readdir_res res = {
52382e22a5eSTrond Myklebust 		.verf = verf_res,
52482e22a5eSTrond Myklebust 	};
5254704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
5261da177e4SLinus Torvalds 	int		error;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds  again:
5291da177e4SLinus Torvalds 	timestamp = jiffies;
5304704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
531a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
53282e22a5eSTrond Myklebust 	error = NFS_PROTO(inode)->readdir(&arg, &res);
5331da177e4SLinus Torvalds 	if (error < 0) {
5341da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
5351da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
5361da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
5373a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
53882e22a5eSTrond Myklebust 			desc->plus = arg.plus = false;
5391da177e4SLinus Torvalds 			goto again;
5401da177e4SLinus Torvalds 		}
5411da177e4SLinus Torvalds 		goto error;
5421da177e4SLinus Torvalds 	}
5431f4eab7eSNeil Brown 	desc->timestamp = timestamp;
5444704f0e2STrond Myklebust 	desc->gencount = gencount;
545d1bacf9eSBryan Schumaker error:
546d1bacf9eSBryan Schumaker 	return error;
547d1bacf9eSBryan Schumaker }
548d1bacf9eSBryan Schumaker 
5496c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
550573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
551d1bacf9eSBryan Schumaker {
55259e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
553573c4e1eSChuck Lever 	int error;
554d1bacf9eSBryan Schumaker 
55559e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
556573c4e1eSChuck Lever 	if (error)
557573c4e1eSChuck Lever 		return error;
558d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
559d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
560d1bacf9eSBryan Schumaker 	return 0;
561d1bacf9eSBryan Schumaker }
562d1bacf9eSBryan Schumaker 
563fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
564fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
565fa923369STrond Myklebust  */
566d39ab9deSBryan Schumaker static
567d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
568d39ab9deSBryan Schumaker {
569d8fdb47fSTrond Myklebust 	struct inode *inode;
570fa923369STrond Myklebust 	struct nfs_inode *nfsi;
571fa923369STrond Myklebust 
5722b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
5732b0143b5SDavid Howells 		return 0;
574fa923369STrond Myklebust 
575d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
576d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
577d8fdb47fSTrond Myklebust 		return 0;
578d8fdb47fSTrond Myklebust 
579d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
5807dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
581d39ab9deSBryan Schumaker 		return 0;
5827dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
5837dc72d5fSTrond Myklebust 		return 0;
5847dc72d5fSTrond Myklebust 	return 1;
585d39ab9deSBryan Schumaker }
586d39ab9deSBryan Schumaker 
587d39ab9deSBryan Schumaker static
58823db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
589d69ee9b8STrond Myklebust {
590d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
591d69ee9b8STrond Myklebust 		return false;
592d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
593d69ee9b8STrond Myklebust 		return true;
59423db8620SAl Viro 	if (ctx->pos == 0)
595d69ee9b8STrond Myklebust 		return true;
596d69ee9b8STrond Myklebust 	return false;
597d69ee9b8STrond Myklebust }
598d69ee9b8STrond Myklebust 
599d69ee9b8STrond Myklebust /*
60063519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
60163519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
602d69ee9b8STrond Myklebust  * directory.
603d69ee9b8STrond Myklebust  */
604d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
605d69ee9b8STrond 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);
611d69ee9b8STrond Myklebust }
612d69ee9b8STrond Myklebust 
613311324adSTrond Myklebust /*
614311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
615311324adSTrond Myklebust  *
616311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
617311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
618311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
619311324adSTrond Myklebust  * cache flush.
620311324adSTrond Myklebust  */
621311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
622311324adSTrond Myklebust {
62363519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
62463519fbcSTrond Myklebust 
62563519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
62663519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
62763519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
628227823d2SDai Ngo 		invalidate_mapping_pages(dir->i_mapping,
629227823d2SDai Ngo 			nfsi->page_index + 1, -1);
630311324adSTrond Myklebust 	}
631311324adSTrond Myklebust }
632311324adSTrond Myklebust 
633d69ee9b8STrond Myklebust static
634a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
635a1147b82STrond Myklebust 		unsigned long dir_verifier)
636d39ab9deSBryan Schumaker {
63726fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
6389ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
6394a201d6eSTrond Myklebust 	struct dentry *dentry;
6404a201d6eSTrond Myklebust 	struct dentry *alias;
641d39ab9deSBryan Schumaker 	struct inode *inode;
642aa9c2669SDavid Quigley 	int status;
643d39ab9deSBryan Schumaker 
644fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
645fa923369STrond Myklebust 		return;
6466c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
6476c441c25STrond Myklebust 		return;
64878d04af4STrond Myklebust 	if (filename.len == 0)
64978d04af4STrond Myklebust 		return;
65078d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
65178d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
65278d04af4STrond Myklebust 		return;
65378d04af4STrond Myklebust 	/* ...or '/' */
65478d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
65578d04af4STrond Myklebust 		return;
6564a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
6574a201d6eSTrond Myklebust 		if (filename.len == 1)
6584a201d6eSTrond Myklebust 			return;
6594a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
6604a201d6eSTrond Myklebust 			return;
6614a201d6eSTrond Myklebust 	}
6628387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
663d39ab9deSBryan Schumaker 
6644a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
6659ac3d3e8SAl Viro again:
6669ac3d3e8SAl Viro 	if (!dentry) {
6679ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
6689ac3d3e8SAl Viro 		if (IS_ERR(dentry))
6699ac3d3e8SAl Viro 			return;
6709ac3d3e8SAl Viro 	}
6719ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
6726c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
6736c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
6746c441c25STrond Myklebust 					&entry->fattr->fsid))
6756c441c25STrond Myklebust 			goto out;
676d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
6777dc72d5fSTrond Myklebust 			if (!entry->fh->size)
6787dc72d5fSTrond Myklebust 				goto out;
679a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
6802b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
681aa9c2669SDavid Quigley 			if (!status)
6822b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
683d39ab9deSBryan Schumaker 			goto out;
684d39ab9deSBryan Schumaker 		} else {
6855542aa2fSEric W. Biederman 			d_invalidate(dentry);
686d39ab9deSBryan Schumaker 			dput(dentry);
6879ac3d3e8SAl Viro 			dentry = NULL;
6889ac3d3e8SAl Viro 			goto again;
689d39ab9deSBryan Schumaker 		}
690d39ab9deSBryan Schumaker 	}
6917dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
6927dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
6937dc72d5fSTrond Myklebust 		goto out;
6947dc72d5fSTrond Myklebust 	}
695d39ab9deSBryan Schumaker 
6961775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
69741d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
6989ac3d3e8SAl Viro 	d_lookup_done(dentry);
6999ac3d3e8SAl Viro 	if (alias) {
700d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
701d39ab9deSBryan Schumaker 			goto out;
7029ac3d3e8SAl Viro 		dput(dentry);
7039ac3d3e8SAl Viro 		dentry = alias;
7049ac3d3e8SAl Viro 	}
705a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
706d39ab9deSBryan Schumaker out:
707d39ab9deSBryan Schumaker 	dput(dentry);
708d39ab9deSBryan Schumaker }
709d39ab9deSBryan Schumaker 
710d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
7113b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
7123b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
7133b2a09f1STrond Myklebust 				   struct page **xdr_pages,
714*35df59d3STrond Myklebust 				   unsigned int buflen,
715*35df59d3STrond Myklebust 				   struct page **arrays,
716*35df59d3STrond Myklebust 				   size_t narrays)
717d1bacf9eSBryan Schumaker {
7183b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
719babddc72SBryan Schumaker 	struct xdr_stream stream;
720f7da7a12SBenny Halevy 	struct xdr_buf buf;
721*35df59d3STrond Myklebust 	struct page *scratch, *new, *page = *arrays;
7225c346854STrond Myklebust 	int status;
723babddc72SBryan Schumaker 
7246650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
7256650239aSTrond Myklebust 	if (scratch == NULL)
7266650239aSTrond Myklebust 		return -ENOMEM;
727babddc72SBryan Schumaker 
728f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
7296650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
73099424380SBryan Schumaker 
73199424380SBryan Schumaker 	do {
732d33030e2SJeffrey Mitchell 		if (entry->label)
733d33030e2SJeffrey Mitchell 			entry->label->len = NFS4_MAXLABELLEN;
734d33030e2SJeffrey Mitchell 
73599424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
736972bcdf2STrond Myklebust 		if (status != 0)
73799424380SBryan Schumaker 			break;
7385c346854STrond Myklebust 
739a7a3b1e9SBenjamin Coddington 		if (desc->plus)
740a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
741a1147b82STrond Myklebust 					desc->dir_verifier);
7428cd51a0cSTrond Myklebust 
743db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
7443b2a09f1STrond Myklebust 		if (status != -ENOSPC)
7453b2a09f1STrond Myklebust 			continue;
7463b2a09f1STrond Myklebust 
747*35df59d3STrond Myklebust 		if (page->mapping != mapping) {
748*35df59d3STrond Myklebust 			if (!--narrays)
7493b2a09f1STrond Myklebust 				break;
750*35df59d3STrond Myklebust 			new = nfs_readdir_page_array_alloc(entry->prev_cookie,
751*35df59d3STrond Myklebust 							   GFP_KERNEL);
752*35df59d3STrond Myklebust 			if (!new)
753*35df59d3STrond Myklebust 				break;
754*35df59d3STrond Myklebust 			arrays++;
755*35df59d3STrond Myklebust 			*arrays = page = new;
756*35df59d3STrond Myklebust 		} else {
757*35df59d3STrond Myklebust 			new = nfs_readdir_page_get_next(mapping,
758*35df59d3STrond Myklebust 							page->index + 1,
7593b2a09f1STrond Myklebust 							entry->prev_cookie);
7603b2a09f1STrond Myklebust 			if (!new)
7613b2a09f1STrond Myklebust 				break;
762*35df59d3STrond Myklebust 			if (page != *arrays)
7633b2a09f1STrond Myklebust 				nfs_readdir_page_unlock_and_put(page);
7643b2a09f1STrond Myklebust 			page = new;
765*35df59d3STrond Myklebust 		}
7663b2a09f1STrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
767972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
76899424380SBryan Schumaker 
769972bcdf2STrond Myklebust 	switch (status) {
770972bcdf2STrond Myklebust 	case -EBADCOOKIE:
771972bcdf2STrond Myklebust 		if (entry->eof) {
772b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
77399424380SBryan Schumaker 			status = 0;
77456e4ebf8SBryan Schumaker 		}
775972bcdf2STrond Myklebust 		break;
776972bcdf2STrond Myklebust 	case -ENOSPC:
777972bcdf2STrond Myklebust 	case -EAGAIN:
778972bcdf2STrond Myklebust 		status = 0;
779972bcdf2STrond Myklebust 		break;
780972bcdf2STrond Myklebust 	}
7816650239aSTrond Myklebust 
782*35df59d3STrond Myklebust 	if (page != *arrays)
7833b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
7843b2a09f1STrond Myklebust 
7856650239aSTrond Myklebust 	put_page(scratch);
7868cd51a0cSTrond Myklebust 	return status;
7878cd51a0cSTrond Myklebust }
78856e4ebf8SBryan Schumaker 
7891a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
79056e4ebf8SBryan Schumaker {
7911a34c8c9STrond Myklebust 	while (npages--)
7921a34c8c9STrond Myklebust 		put_page(pages[npages]);
7931a34c8c9STrond Myklebust 	kfree(pages);
79456e4ebf8SBryan Schumaker }
79556e4ebf8SBryan Schumaker 
79656e4ebf8SBryan Schumaker /*
797bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
798bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
79956e4ebf8SBryan Schumaker  */
8001a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
80156e4ebf8SBryan Schumaker {
8021a34c8c9STrond Myklebust 	struct page **pages;
8031a34c8c9STrond Myklebust 	size_t i;
80456e4ebf8SBryan Schumaker 
8051a34c8c9STrond Myklebust 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
8061a34c8c9STrond Myklebust 	if (!pages)
8071a34c8c9STrond Myklebust 		return NULL;
80856e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
80956e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
81056e4ebf8SBryan Schumaker 		if (page == NULL)
81156e4ebf8SBryan Schumaker 			goto out_freepages;
81256e4ebf8SBryan Schumaker 		pages[i] = page;
81356e4ebf8SBryan Schumaker 	}
8141a34c8c9STrond Myklebust 	return pages;
81556e4ebf8SBryan Schumaker 
81656e4ebf8SBryan Schumaker out_freepages:
817c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
8181a34c8c9STrond Myklebust 	return NULL;
819d1bacf9eSBryan Schumaker }
820d1bacf9eSBryan Schumaker 
8216c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
822*35df59d3STrond Myklebust 				    __be32 *verf_arg, __be32 *verf_res,
823*35df59d3STrond Myklebust 				    struct page **arrays, size_t narrays)
824d1bacf9eSBryan Schumaker {
8251a34c8c9STrond Myklebust 	struct page **pages;
826*35df59d3STrond Myklebust 	struct page *page = *arrays;
8276b75cf9eSTrond Myklebust 	struct nfs_entry *entry;
8281a34c8c9STrond Myklebust 	size_t array_size;
829b593c09fSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
8301a34c8c9STrond Myklebust 	size_t dtsize = NFS_SERVER(inode)->dtsize;
8318cd51a0cSTrond Myklebust 	int status = -ENOMEM;
832d1bacf9eSBryan Schumaker 
8336b75cf9eSTrond Myklebust 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
8346b75cf9eSTrond Myklebust 	if (!entry)
8356b75cf9eSTrond Myklebust 		return -ENOMEM;
8366b75cf9eSTrond Myklebust 	entry->cookie = nfs_readdir_page_last_cookie(page);
8376b75cf9eSTrond Myklebust 	entry->fh = nfs_alloc_fhandle();
8386b75cf9eSTrond Myklebust 	entry->fattr = nfs_alloc_fattr();
8396b75cf9eSTrond Myklebust 	entry->server = NFS_SERVER(inode);
8406b75cf9eSTrond Myklebust 	if (entry->fh == NULL || entry->fattr == NULL)
841d1bacf9eSBryan Schumaker 		goto out;
842d1bacf9eSBryan Schumaker 
8436b75cf9eSTrond Myklebust 	entry->label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
8446b75cf9eSTrond Myklebust 	if (IS_ERR(entry->label)) {
8456b75cf9eSTrond Myklebust 		status = PTR_ERR(entry->label);
84614c43f76SDavid Quigley 		goto out;
84714c43f76SDavid Quigley 	}
84814c43f76SDavid Quigley 
8491a34c8c9STrond Myklebust 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
8501a34c8c9STrond Myklebust 	pages = nfs_readdir_alloc_pages(array_size);
8511a34c8c9STrond Myklebust 	if (!pages)
852e762a639STrond Myklebust 		goto out_release_label;
8531a34c8c9STrond Myklebust 
854d1bacf9eSBryan Schumaker 	do {
855ac396128STrond Myklebust 		unsigned int pglen;
856b593c09fSTrond Myklebust 		status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie,
857b593c09fSTrond Myklebust 						pages, dtsize,
858b593c09fSTrond Myklebust 						verf_res);
859d1bacf9eSBryan Schumaker 		if (status < 0)
860d1bacf9eSBryan Schumaker 			break;
861972bcdf2STrond Myklebust 
862ac396128STrond Myklebust 		pglen = status;
863972bcdf2STrond Myklebust 		if (pglen == 0) {
864972bcdf2STrond Myklebust 			nfs_readdir_page_set_eof(page);
8658cd51a0cSTrond Myklebust 			break;
8668cd51a0cSTrond Myklebust 		}
867972bcdf2STrond Myklebust 
868*35df59d3STrond Myklebust 		status = nfs_readdir_page_filler(desc, entry, pages, pglen,
869*35df59d3STrond Myklebust 						 arrays, narrays);
870e762a639STrond Myklebust 	} while (!status && nfs_readdir_page_needs_filling(page));
871d1bacf9eSBryan Schumaker 
872c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
873e762a639STrond Myklebust out_release_label:
8746b75cf9eSTrond Myklebust 	nfs4_label_free(entry->label);
875d1bacf9eSBryan Schumaker out:
8766b75cf9eSTrond Myklebust 	nfs_free_fattr(entry->fattr);
8776b75cf9eSTrond Myklebust 	nfs_free_fhandle(entry->fh);
8786b75cf9eSTrond Myklebust 	kfree(entry);
879d1bacf9eSBryan Schumaker 	return status;
880d1bacf9eSBryan Schumaker }
881d1bacf9eSBryan Schumaker 
8821f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
8831da177e4SLinus Torvalds {
88409cbfeafSKirill A. Shutemov 	put_page(desc->page);
8851da177e4SLinus Torvalds 	desc->page = NULL;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881f1d4aa4STrond Myklebust static void
8891f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
8901da177e4SLinus Torvalds {
8911f1d4aa4STrond Myklebust 	unlock_page(desc->page);
8921f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
8931f1d4aa4STrond Myklebust }
8941f1d4aa4STrond Myklebust 
8951f1d4aa4STrond Myklebust static struct page *
8961f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
8971f1d4aa4STrond Myklebust {
8981f1d4aa4STrond Myklebust 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
8991f1d4aa4STrond Myklebust 					   desc->page_index,
9001f1d4aa4STrond Myklebust 					   desc->last_cookie);
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /*
904d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
905114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
9061da177e4SLinus Torvalds  */
9076c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
908d1bacf9eSBryan Schumaker {
909227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
910227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
911b593c09fSTrond Myklebust 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
912d1bacf9eSBryan Schumaker 	int res;
913d1bacf9eSBryan Schumaker 
9141f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
9151f1d4aa4STrond Myklebust 	if (!desc->page)
9161f1d4aa4STrond Myklebust 		return -ENOMEM;
9171f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
918*35df59d3STrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
919*35df59d3STrond Myklebust 					       &desc->page, 1);
9209fff59edSTrond Myklebust 		if (res < 0) {
9219fff59edSTrond Myklebust 			nfs_readdir_page_unlock_and_put_cached(desc);
9229fff59edSTrond Myklebust 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
9239fff59edSTrond Myklebust 				invalidate_inode_pages2(desc->file->f_mapping);
9249fff59edSTrond Myklebust 				desc->page_index = 0;
9259fff59edSTrond Myklebust 				return -EAGAIN;
9269fff59edSTrond Myklebust 			}
9279fff59edSTrond Myklebust 			return res;
9289fff59edSTrond Myklebust 		}
929b593c09fSTrond Myklebust 		memcpy(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf));
9301f1d4aa4STrond Myklebust 	}
931114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
932227823d2SDai Ngo 	if (res == 0) {
933227823d2SDai Ngo 		nfsi->page_index = desc->page_index;
934114de382STrond Myklebust 		return 0;
935114de382STrond Myklebust 	}
9361f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
937d1bacf9eSBryan Schumaker 	return res;
938d1bacf9eSBryan Schumaker }
939d1bacf9eSBryan Schumaker 
940d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
9416c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
9421da177e4SLinus Torvalds {
9438cd51a0cSTrond Myklebust 	int res;
944d1bacf9eSBryan Schumaker 
9459fff59edSTrond Myklebust 	do {
9460aded708STrond Myklebust 		if (desc->page_index == 0) {
9478cd51a0cSTrond Myklebust 			desc->current_index = 0;
94859e356a9STrond Myklebust 			desc->prev_index = 0;
9490aded708STrond Myklebust 			desc->last_cookie = 0;
9500aded708STrond Myklebust 		}
951114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
95247c716cbSTrond Myklebust 	} while (res == -EAGAIN);
9531da177e4SLinus Torvalds 	return res;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
9581da177e4SLinus Torvalds  */
959dbeaf8c9STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	struct file	*file = desc->file;
962b593c09fSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(file_inode(file));
963dbeaf8c9STrond Myklebust 	struct nfs_cache_array *array;
964dbeaf8c9STrond Myklebust 	unsigned int i = 0;
9658ef2ce3eSBryan Schumaker 
9660795bf83SFabian Frederick 	array = kmap(desc->page);
967d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
968ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
9691da177e4SLinus Torvalds 
970ece0b423STrond Myklebust 		ent = &array->array[i];
971a52a8a6aSTrond Myklebust 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
97223db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
9736089dd0dSThomas Meyer 			desc->eof = true;
9741da177e4SLinus Torvalds 			break;
975ece0b423STrond Myklebust 		}
976b593c09fSTrond Myklebust 		memcpy(desc->verf, nfsi->cookieverf, sizeof(desc->verf));
977d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
9782e7a4641STrond Myklebust 			desc->dir_cookie = array->array[i+1].cookie;
979d1bacf9eSBryan Schumaker 		else
9802e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
98159e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
9822e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
98359e356a9STrond Myklebust 		else
98459e356a9STrond Myklebust 			desc->ctx->pos++;
9852e7a4641STrond Myklebust 		if (desc->duped != 0)
9862e7a4641STrond Myklebust 			desc->duped = 1;
9878cd51a0cSTrond Myklebust 	}
988b1e21c97STrond Myklebust 	if (array->page_is_eof)
9896089dd0dSThomas Meyer 		desc->eof = true;
990d1bacf9eSBryan Schumaker 
9910795bf83SFabian Frederick 	kunmap(desc->page);
992dbeaf8c9STrond Myklebust 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
993dbeaf8c9STrond Myklebust 			(unsigned long long)desc->dir_cookie);
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds /*
9971da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
9981da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
9991da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
10001da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
10011da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
10021da177e4SLinus Torvalds  *
10031da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
10041da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
10051da177e4SLinus Torvalds  *	 we should already have a complete representation of the
10061da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
10071da177e4SLinus Torvalds  */
10086c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
10091da177e4SLinus Torvalds {
1010*35df59d3STrond Myklebust 	struct page	**arrays;
1011*35df59d3STrond Myklebust 	size_t		i, sz = 512;
1012b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
1013*35df59d3STrond Myklebust 	int		status = -ENOMEM;
10141da177e4SLinus Torvalds 
1015*35df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
10162e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
10171da177e4SLinus Torvalds 
1018*35df59d3STrond Myklebust 	arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
1019*35df59d3STrond Myklebust 	if (!arrays)
10201da177e4SLinus Torvalds 		goto out;
1021*35df59d3STrond Myklebust 	arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
1022*35df59d3STrond Myklebust 	if (!arrays[0])
1023*35df59d3STrond Myklebust 		goto out;
10241da177e4SLinus Torvalds 
10257a8e1dc3STrond Myklebust 	desc->page_index = 0;
10262e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
10272e7a4641STrond Myklebust 	desc->duped = 0;
10287a8e1dc3STrond Myklebust 
1029*35df59d3STrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1030d1bacf9eSBryan Schumaker 
1031*35df59d3STrond Myklebust 	for (i = 0; !desc->eof && i < sz && arrays[i]; i++) {
1032*35df59d3STrond Myklebust 		desc->page = arrays[i];
1033dbeaf8c9STrond Myklebust 		nfs_do_filldir(desc);
1034*35df59d3STrond Myklebust 	}
1035*35df59d3STrond Myklebust 	desc->page = NULL;
10361da177e4SLinus Torvalds 
1037*35df59d3STrond Myklebust 
1038*35df59d3STrond Myklebust 	for (i = 0; i < sz && arrays[i]; i++)
1039*35df59d3STrond Myklebust 		nfs_readdir_page_array_free(arrays[i]);
10401da177e4SLinus Torvalds out:
1041*35df59d3STrond Myklebust 	kfree(arrays);
1042*35df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
10431da177e4SLinus Torvalds 	return status;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
104600a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
104700a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
104800a92642SOlivier Galibert    whole directory.
10491da177e4SLinus Torvalds  */
105023db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
10511da177e4SLinus Torvalds {
1052be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
10532b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
105423db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
10556b75cf9eSTrond Myklebust 	struct nfs_readdir_descriptor *desc;
10566b75cf9eSTrond Myklebust 	int res;
10571da177e4SLinus Torvalds 
10586de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
10596de1472fSAl Viro 			file, (long long)ctx->pos);
106091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
106191d5b470SChuck Lever 
10621da177e4SLinus Torvalds 	/*
106323db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
1064f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
106500a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
106600a92642SOlivier Galibert 	 * revalidate the cookie.
10671da177e4SLinus Torvalds 	 */
10686b75cf9eSTrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) {
106923db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
1070fccca7fcSTrond Myklebust 		if (res < 0)
1071fccca7fcSTrond Myklebust 			goto out;
10726b75cf9eSTrond Myklebust 	}
10736b75cf9eSTrond Myklebust 
10746b75cf9eSTrond Myklebust 	res = -ENOMEM;
10756b75cf9eSTrond Myklebust 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
10766b75cf9eSTrond Myklebust 	if (!desc)
10776b75cf9eSTrond Myklebust 		goto out;
10786b75cf9eSTrond Myklebust 	desc->file = file;
10796b75cf9eSTrond Myklebust 	desc->ctx = ctx;
10806b75cf9eSTrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, ctx);
1081fccca7fcSTrond Myklebust 
10822e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
10832e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
10842e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
10852e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
10862e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
1087b593c09fSTrond Myklebust 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
10882e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
10892e7a4641STrond Myklebust 
109047c716cbSTrond Myklebust 	do {
10911da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
109200a92642SOlivier Galibert 
10931da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1094ece0b423STrond Myklebust 			res = 0;
10951da177e4SLinus Torvalds 			/* This means either end of directory */
10962e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
10971da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
109823db8620SAl Viro 				res = uncached_readdir(desc);
1099ece0b423STrond Myklebust 				if (res == 0)
11001da177e4SLinus Torvalds 					continue;
11019fff59edSTrond Myklebust 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
11029fff59edSTrond Myklebust 					res = 0;
11031da177e4SLinus Torvalds 			}
11041da177e4SLinus Torvalds 			break;
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
11073a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
11081da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1109baf57a09STrond Myklebust 			desc->page_index = 0;
1110a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1111a7a3b1e9SBenjamin Coddington 			desc->eof = false;
11121da177e4SLinus Torvalds 			continue;
11131da177e4SLinus Torvalds 		}
11141da177e4SLinus Torvalds 		if (res < 0)
11151da177e4SLinus Torvalds 			break;
11161da177e4SLinus Torvalds 
1117dbeaf8c9STrond Myklebust 		nfs_do_filldir(desc);
11181f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
111947c716cbSTrond Myklebust 	} while (!desc->eof);
11202e7a4641STrond Myklebust 
11212e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
11222e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
11232e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
11242e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
11252e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
1126b593c09fSTrond Myklebust 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
11272e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
11282e7a4641STrond Myklebust 
11296b75cf9eSTrond Myklebust 	kfree(desc);
11306b75cf9eSTrond Myklebust 
1131fccca7fcSTrond Myklebust out:
11326de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
11331da177e4SLinus Torvalds 	return res;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
1136965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1137f0dd2136STrond Myklebust {
1138480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1139b84e06c5SChuck Lever 
11406de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
11416de1472fSAl Viro 			filp, offset, whence);
1142b84e06c5SChuck Lever 
1143965c8e59SAndrew Morton 	switch (whence) {
1144f0dd2136STrond Myklebust 	default:
1145b2b1ff3dSTrond Myklebust 		return -EINVAL;
1146b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1147b2b1ff3dSTrond Myklebust 		if (offset < 0)
1148b2b1ff3dSTrond Myklebust 			return -EINVAL;
114983f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1150b2b1ff3dSTrond Myklebust 		break;
1151b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1152b2b1ff3dSTrond Myklebust 		if (offset == 0)
1153b2b1ff3dSTrond Myklebust 			return filp->f_pos;
115483f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1155b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1156b2b1ff3dSTrond Myklebust 		if (offset < 0) {
115783f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1158b2b1ff3dSTrond Myklebust 			return -EINVAL;
1159b2b1ff3dSTrond Myklebust 		}
1160f0dd2136STrond Myklebust 	}
1161f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1162f0dd2136STrond Myklebust 		filp->f_pos = offset;
116359e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(filp))
116459e356a9STrond Myklebust 			dir_ctx->dir_cookie = offset;
116559e356a9STrond Myklebust 		else
1166480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
1167b593c09fSTrond Myklebust 		if (offset == 0)
1168b593c09fSTrond Myklebust 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
11698ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1170f0dd2136STrond Myklebust 	}
117183f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1172f0dd2136STrond Myklebust 	return offset;
1173f0dd2136STrond Myklebust }
1174f0dd2136STrond Myklebust 
11751da177e4SLinus Torvalds /*
11761da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
11771da177e4SLinus Torvalds  * is a dummy operation.
11781da177e4SLinus Torvalds  */
117902c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
118002c24a82SJosef Bacik 			 int datasync)
11811da177e4SLinus Torvalds {
11826de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
11831e7cb3dcSChuck Lever 
118411decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
11851da177e4SLinus Torvalds 	return 0;
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds 
1188bfc69a45STrond Myklebust /**
1189bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1190302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1191bfc69a45STrond Myklebust  *
1192bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1193bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1194bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1195bfc69a45STrond Myklebust  *
1196efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1197efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1198efeda80dSTrond Myklebust  *
1199bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1200bfc69a45STrond Myklebust  */
1201bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1202bfc69a45STrond Myklebust {
1203efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1204bfc69a45STrond Myklebust }
120589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1206bfc69a45STrond Myklebust 
1207efeda80dSTrond Myklebust /**
1208efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1209efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1210efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1211efeda80dSTrond Myklebust  *
1212efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1213efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1214efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1215efeda80dSTrond Myklebust  */
1216efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1217efeda80dSTrond Myklebust {
1218efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1219efeda80dSTrond Myklebust }
1220efeda80dSTrond Myklebust 
1221efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1222efeda80dSTrond Myklebust {
1223efeda80dSTrond Myklebust 	*verf |= 1UL;
1224efeda80dSTrond Myklebust }
1225efeda80dSTrond Myklebust 
1226efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1227efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1228efeda80dSTrond Myklebust {
1229efeda80dSTrond Myklebust 	*verf &= ~1UL;
1230efeda80dSTrond Myklebust }
1231efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1232efeda80dSTrond Myklebust 
1233efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1234efeda80dSTrond Myklebust {
1235efeda80dSTrond Myklebust 	return verf & 1;
1236efeda80dSTrond Myklebust }
1237efeda80dSTrond Myklebust 
1238efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1239efeda80dSTrond Myklebust {
1240efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1241efeda80dSTrond Myklebust }
1242efeda80dSTrond Myklebust 
1243efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1244efeda80dSTrond Myklebust {
1245efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1246efeda80dSTrond Myklebust 
1247efeda80dSTrond Myklebust 	if (!nfs_verifier_is_delegated(dentry) &&
1248efeda80dSTrond Myklebust 	    !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1249efeda80dSTrond Myklebust 		goto out;
1250efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1251efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1252efeda80dSTrond Myklebust out:
1253efeda80dSTrond Myklebust 	dentry->d_time = verf;
1254efeda80dSTrond Myklebust }
1255efeda80dSTrond Myklebust 
1256efeda80dSTrond Myklebust /**
1257efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1258efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1259efeda80dSTrond Myklebust  * @verf: verifier to save
1260efeda80dSTrond Myklebust  *
1261efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1262efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1263efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1264efeda80dSTrond Myklebust  * look it up again after a directory change.
1265efeda80dSTrond Myklebust  */
1266efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1267efeda80dSTrond Myklebust {
1268efeda80dSTrond Myklebust 
1269efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1270efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1271efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1272efeda80dSTrond Myklebust }
1273efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1274efeda80dSTrond Myklebust 
1275efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1276efeda80dSTrond Myklebust /**
1277efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1278efeda80dSTrond Myklebust  * @inode: pointer to inode
1279efeda80dSTrond Myklebust  *
1280efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1281efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1282efeda80dSTrond Myklebust  * while holding a delegation.
1283efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1284efeda80dSTrond Myklebust  * returned or revoked.
1285efeda80dSTrond Myklebust  */
1286efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1287efeda80dSTrond Myklebust {
1288efeda80dSTrond Myklebust 	struct dentry *alias;
1289efeda80dSTrond Myklebust 
1290efeda80dSTrond Myklebust 	if (!inode)
1291efeda80dSTrond Myklebust 		return;
1292efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1293efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1294efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1295efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1296efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1297efeda80dSTrond Myklebust 	}
1298efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1299efeda80dSTrond Myklebust }
1300efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1301efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1302efeda80dSTrond Myklebust 
13031da177e4SLinus Torvalds /*
13041da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
13051da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
13061da177e4SLinus Torvalds  * and may need to be looked up again.
1307912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
13081da177e4SLinus Torvalds  */
1309912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1310912a108dSNeilBrown 			      int rcu_walk)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
13131da177e4SLinus Torvalds 		return 1;
13144eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
13154eec952eSTrond Myklebust 		return 0;
1316f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
13176ecc5e8fSTrond Myklebust 		return 0;
1318f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
13191cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1320912a108dSNeilBrown 		if (rcu_walk)
1321f2c77f4eSTrond Myklebust 			return 0;
13221cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
13231cd9cb05STrond Myklebust 			return 0;
13241cd9cb05STrond Myklebust 	}
1325f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1326f2c77f4eSTrond Myklebust 		return 0;
1327f2c77f4eSTrond Myklebust 	return 1;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds /*
1331a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1332a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1333a12802caSTrond Myklebust  */
1334fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1335a12802caSTrond Myklebust {
1336a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1337a12802caSTrond Myklebust 		return 0;
1338fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1339a12802caSTrond Myklebust }
1340a12802caSTrond Myklebust 
1341a12802caSTrond Myklebust /*
13421d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
13431d6757fbSTrond Myklebust  *
13441d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
13451d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
13461d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
13471d6757fbSTrond Myklebust  *
13481d6757fbSTrond Myklebust  */
134965a0c149STrond Myklebust static
1350fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
13511da177e4SLinus Torvalds {
13521da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
135365a0c149STrond Myklebust 	int ret;
13541da177e4SLinus Torvalds 
135536d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
13564e99a1ffSTrond Myklebust 		return 0;
135747921921STrond Myklebust 
135847921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
135947921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
136047921921STrond Myklebust 		case S_IFREG:
136147921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
136247921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
136347921921STrond Myklebust 				goto out;
1364df561f66SGustavo A. R. Silva 			fallthrough;
136547921921STrond Myklebust 		case S_IFDIR:
136647921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
136747921921STrond Myklebust 				break;
136847921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
136947921921STrond Myklebust 			goto out_force;
137047921921STrond Myklebust 		}
137147921921STrond Myklebust 	}
137247921921STrond Myklebust 
13731da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1374fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
13751da177e4SLinus Torvalds 		goto out_force;
137665a0c149STrond Myklebust out:
1377a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
13781da177e4SLinus Torvalds out_force:
13791fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
13801fa1e384SNeilBrown 		return -ECHILD;
138165a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
138265a0c149STrond Myklebust 	if (ret != 0)
138365a0c149STrond Myklebust 		return ret;
138465a0c149STrond Myklebust 	goto out;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds /*
13881da177e4SLinus Torvalds  * We judge how long we want to trust negative
13891da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
13901da177e4SLinus Torvalds  *
13911da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
13921da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1393912a108dSNeilBrown  *
1394912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1395912a108dSNeilBrown  * suggesting a reval is needed.
13969f6d44d4STrond Myklebust  *
13979f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
13989f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
13991da177e4SLinus Torvalds  */
14001da177e4SLinus Torvalds static inline
14011da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1402fa3c56bbSAl Viro 		       unsigned int flags)
14031da177e4SLinus Torvalds {
14049f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
14051da177e4SLinus Torvalds 		return 0;
14064eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
14074eec952eSTrond Myklebust 		return 1;
1408912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
14115ceb9d7fSTrond Myklebust static int
14125ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
14135ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
14141da177e4SLinus Torvalds {
14155ceb9d7fSTrond Myklebust 	switch (error) {
14165ceb9d7fSTrond Myklebust 	case 1:
14176de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
14186de1472fSAl Viro 			__func__, dentry);
14191da177e4SLinus Torvalds 		return 1;
14205ceb9d7fSTrond Myklebust 	case 0:
1421a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
14221da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
14231da177e4SLinus Torvalds 			/* Purge readdir caches. */
14241da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1425a3f432bfSJ. Bruce Fields 			/*
1426a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1427a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1428a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1429a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1430a3f432bfSJ. Bruce Fields 			 */
1431a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
14325ceb9d7fSTrond Myklebust 				return 1;
14331da177e4SLinus Torvalds 		}
14346de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
14356de1472fSAl Viro 				__func__, dentry);
14361da177e4SLinus Torvalds 		return 0;
14375ceb9d7fSTrond Myklebust 	}
14386de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
14396de1472fSAl Viro 				__func__, dentry, error);
1440e1fb4d05STrond Myklebust 	return error;
14411da177e4SLinus Torvalds }
14421da177e4SLinus Torvalds 
14435ceb9d7fSTrond Myklebust static int
14445ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
14455ceb9d7fSTrond Myklebust 			       unsigned int flags)
14465ceb9d7fSTrond Myklebust {
14475ceb9d7fSTrond Myklebust 	int ret = 1;
14485ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
14495ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
14505ceb9d7fSTrond Myklebust 			return -ECHILD;
14515ceb9d7fSTrond Myklebust 		ret = 0;
14525ceb9d7fSTrond Myklebust 	}
14535ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
14545ceb9d7fSTrond Myklebust }
14555ceb9d7fSTrond Myklebust 
14565ceb9d7fSTrond Myklebust static int
14575ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
14585ceb9d7fSTrond Myklebust 				struct inode *inode)
14595ceb9d7fSTrond Myklebust {
14605ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
14615ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
14625ceb9d7fSTrond Myklebust }
14635ceb9d7fSTrond Myklebust 
14645ceb9d7fSTrond Myklebust static int
14655ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
14665ceb9d7fSTrond Myklebust 			     struct inode *inode)
14675ceb9d7fSTrond Myklebust {
14685ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
14695ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
14705ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
1471a1147b82STrond Myklebust 	unsigned long dir_verifier;
14725ceb9d7fSTrond Myklebust 	int ret;
14735ceb9d7fSTrond Myklebust 
14745ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
14755ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
14765ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
14775ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
14785ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
14795ceb9d7fSTrond Myklebust 		goto out;
14805ceb9d7fSTrond Myklebust 
1481a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
1482f7b37b8bSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
14835ceb9d7fSTrond Myklebust 	if (ret < 0) {
1484f7b37b8bSTrond Myklebust 		switch (ret) {
1485f7b37b8bSTrond Myklebust 		case -ESTALE:
1486f7b37b8bSTrond Myklebust 		case -ENOENT:
14875ceb9d7fSTrond Myklebust 			ret = 0;
1488f7b37b8bSTrond Myklebust 			break;
1489f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1490f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1491f7b37b8bSTrond Myklebust 				ret = 1;
1492f7b37b8bSTrond Myklebust 		}
14935ceb9d7fSTrond Myklebust 		goto out;
14945ceb9d7fSTrond Myklebust 	}
14955ceb9d7fSTrond Myklebust 	ret = 0;
14965ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
14975ceb9d7fSTrond Myklebust 		goto out;
14985ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
14995ceb9d7fSTrond Myklebust 		goto out;
15005ceb9d7fSTrond Myklebust 
15015ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
1502a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
15035ceb9d7fSTrond Myklebust 
15045ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
15055ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
15065ceb9d7fSTrond Myklebust 	ret = 1;
15075ceb9d7fSTrond Myklebust out:
15085ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
15095ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
15105ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
15115ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
15125ceb9d7fSTrond Myklebust }
15135ceb9d7fSTrond Myklebust 
15145ceb9d7fSTrond Myklebust /*
15155ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
15165ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
15175ceb9d7fSTrond Myklebust  * lookup.
15185ceb9d7fSTrond Myklebust  *
15195ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
15205ceb9d7fSTrond Myklebust  * we have an inode!
15215ceb9d7fSTrond Myklebust  *
15225ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
15235ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
15245ceb9d7fSTrond Myklebust  */
15255ceb9d7fSTrond Myklebust static int
15265ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
15275ceb9d7fSTrond Myklebust 			 unsigned int flags)
15285ceb9d7fSTrond Myklebust {
15295ceb9d7fSTrond Myklebust 	struct inode *inode;
15305ceb9d7fSTrond Myklebust 	int error;
15315ceb9d7fSTrond Myklebust 
15325ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
15335ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
15345ceb9d7fSTrond Myklebust 
15355ceb9d7fSTrond Myklebust 	if (!inode)
15365ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
15375ceb9d7fSTrond Myklebust 
15385ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
15395ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
15405ceb9d7fSTrond Myklebust 				__func__, dentry);
15415ceb9d7fSTrond Myklebust 		goto out_bad;
15425ceb9d7fSTrond Myklebust 	}
15435ceb9d7fSTrond Myklebust 
1544efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
15455ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
15465ceb9d7fSTrond Myklebust 
15475ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
15485ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
15495ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
15505ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
15515ceb9d7fSTrond Myklebust 		if (error) {
15525ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
15535ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
15545ceb9d7fSTrond Myklebust 			goto out_bad;
15555ceb9d7fSTrond Myklebust 		}
15565ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
15575ceb9d7fSTrond Myklebust 		goto out_valid;
15585ceb9d7fSTrond Myklebust 	}
15595ceb9d7fSTrond Myklebust 
15605ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
15615ceb9d7fSTrond Myklebust 		return -ECHILD;
15625ceb9d7fSTrond Myklebust 
15635ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
15645ceb9d7fSTrond Myklebust 		goto out_bad;
15655ceb9d7fSTrond Myklebust 
15665ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
15675ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
15685ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
15695ceb9d7fSTrond Myklebust 	return error;
15705ceb9d7fSTrond Myklebust out_valid:
15715ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
15725ceb9d7fSTrond Myklebust out_bad:
15735ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
15745ceb9d7fSTrond Myklebust 		return -ECHILD;
15755ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
15765ceb9d7fSTrond Myklebust }
15775ceb9d7fSTrond Myklebust 
15785ceb9d7fSTrond Myklebust static int
1579c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1580c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
15815ceb9d7fSTrond Myklebust {
15825ceb9d7fSTrond Myklebust 	struct dentry *parent;
15835ceb9d7fSTrond Myklebust 	struct inode *dir;
15845ceb9d7fSTrond Myklebust 	int ret;
15855ceb9d7fSTrond Myklebust 
15865ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
15875ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
15885ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
15895ceb9d7fSTrond Myklebust 		if (!dir)
15905ceb9d7fSTrond Myklebust 			return -ECHILD;
1591c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
15925ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
15935ceb9d7fSTrond Myklebust 			return -ECHILD;
15945ceb9d7fSTrond Myklebust 	} else {
15955ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1596c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
15975ceb9d7fSTrond Myklebust 		dput(parent);
15985ceb9d7fSTrond Myklebust 	}
15995ceb9d7fSTrond Myklebust 	return ret;
16005ceb9d7fSTrond Myklebust }
16015ceb9d7fSTrond Myklebust 
1602c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1603c7944ebbSTrond Myklebust {
1604c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1605c7944ebbSTrond Myklebust }
1606c7944ebbSTrond Myklebust 
16071da177e4SLinus Torvalds /*
16082b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1609ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1610ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1611ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1612ecf3d1f1SJeff Layton  *
1613ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1614ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1615ecf3d1f1SJeff Layton  */
1616ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1617ecf3d1f1SJeff Layton {
16182b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
16199cdd1d3fSTrond Myklebust 	int error = 0;
1620ecf3d1f1SJeff Layton 
1621ecf3d1f1SJeff Layton 	/*
1622ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1623ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1624ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1625ecf3d1f1SJeff Layton 	 */
1626ecf3d1f1SJeff Layton 	if (!inode) {
16276de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
16286de1472fSAl Viro 				__func__, dentry);
1629ecf3d1f1SJeff Layton 		return 1;
1630ecf3d1f1SJeff Layton 	}
1631ecf3d1f1SJeff Layton 
1632ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
16336de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
16346de1472fSAl Viro 				__func__, dentry);
1635ecf3d1f1SJeff Layton 		return 0;
1636ecf3d1f1SJeff Layton 	}
1637ecf3d1f1SJeff Layton 
1638b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1639ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1640ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1641ecf3d1f1SJeff Layton 	return !error;
1642ecf3d1f1SJeff Layton }
1643ecf3d1f1SJeff Layton 
1644ecf3d1f1SJeff Layton /*
16451da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
16461da177e4SLinus Torvalds  */
1647fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
16481da177e4SLinus Torvalds {
16496de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
16506de1472fSAl Viro 		dentry, dentry->d_flags);
16511da177e4SLinus Torvalds 
165277f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
16532b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
165477f11192STrond Myklebust 		return 1;
165577f11192STrond Myklebust 
16561da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
16571da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
16581da177e4SLinus Torvalds 		return 1;
16591da177e4SLinus Torvalds 	}
16601751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
16611da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
16621da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
16631da177e4SLinus Torvalds 		return 1;
16641da177e4SLinus Torvalds 	}
16651da177e4SLinus Torvalds 	return 0;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16691f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
16701b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
16711b83d707STrond Myklebust {
16721b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
16731f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
167459a707b0STrond Myklebust 	if (inode->i_nlink > 0)
167559a707b0STrond Myklebust 		drop_nlink(inode);
167659a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
167716e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
167816e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
167959a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
168059a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
16811b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
16821b83d707STrond Myklebust }
16831b83d707STrond Myklebust 
16841da177e4SLinus Torvalds /*
16851da177e4SLinus Torvalds  * Called when the dentry loses inode.
16861da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
16871da177e4SLinus Torvalds  */
16881da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
16891da177e4SLinus Torvalds {
169083672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
169183672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
169283672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
169383672d39SNeil Brown 
16941da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1695e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
16961f018458STrond Myklebust 		nfs_drop_nlink(inode);
16971da177e4SLinus Torvalds 	}
16981da177e4SLinus Torvalds 	iput(inode);
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
1701b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1702b1942c5fSAl Viro {
1703b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1704b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1705b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1706b1942c5fSAl Viro 			WARN_ON(1);
1707b1942c5fSAl Viro 		else
1708b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1709b1942c5fSAl Viro 	}
1710b1942c5fSAl Viro }
1711b1942c5fSAl Viro 
1712f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
17131da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1714ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
17151da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
17161da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
171736d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1718b1942c5fSAl Viro 	.d_release	= nfs_d_release,
17191da177e4SLinus Torvalds };
1720ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
17211da177e4SLinus Torvalds 
1722597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	struct dentry *res;
17251da177e4SLinus Torvalds 	struct inode *inode = NULL;
1726e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1727e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
17281775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
1729a1147b82STrond Myklebust 	unsigned long dir_verifier;
17301da177e4SLinus Torvalds 	int error;
17311da177e4SLinus Torvalds 
17326de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
173391d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
17341da177e4SLinus Torvalds 
1735130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1736130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
17371da177e4SLinus Torvalds 
1738fd684071STrond Myklebust 	/*
1739fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1740fd684071STrond Myklebust 	 * but don't hash the dentry.
1741fd684071STrond Myklebust 	 */
17429f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1743130f9ab7SAl Viro 		return NULL;
17441da177e4SLinus Torvalds 
1745e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1746e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1747e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1748e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1749e1fb4d05STrond Myklebust 		goto out;
1750e1fb4d05STrond Myklebust 
175114c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
175214c43f76SDavid Quigley 	if (IS_ERR(label))
175314c43f76SDavid Quigley 		goto out;
175414c43f76SDavid Quigley 
1755a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
17566e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1757f7b37b8bSTrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
17581da177e4SLinus Torvalds 	if (error == -ENOENT)
17591da177e4SLinus Torvalds 		goto no_entry;
17601da177e4SLinus Torvalds 	if (error < 0) {
17611da177e4SLinus Torvalds 		res = ERR_PTR(error);
1762bf130914SAl Viro 		goto out_label;
17631da177e4SLinus Torvalds 	}
17641775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1765bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
176603f28e3aSTrond Myklebust 	if (IS_ERR(res))
1767bf130914SAl Viro 		goto out_label;
176854ceac45SDavid Howells 
176963519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
177063519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1771d69ee9b8STrond Myklebust 
17721da177e4SLinus Torvalds no_entry:
177341d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
17749eaef27bSTrond Myklebust 	if (res != NULL) {
17759eaef27bSTrond Myklebust 		if (IS_ERR(res))
1776bf130914SAl Viro 			goto out_label;
17771da177e4SLinus Torvalds 		dentry = res;
17789eaef27bSTrond Myklebust 	}
1779a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
1780bf130914SAl Viro out_label:
17816e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
178214c43f76SDavid Quigley 	nfs4_label_free(label);
17831da177e4SLinus Torvalds out:
1784e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1785e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
17861da177e4SLinus Torvalds 	return res;
17871da177e4SLinus Torvalds }
1788ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
17891da177e4SLinus Torvalds 
179089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
17910b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
17921da177e4SLinus Torvalds 
1793f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
17940ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1795b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
17961da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
17971da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
179836d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1799b1942c5fSAl Viro 	.d_release	= nfs_d_release,
18001da177e4SLinus Torvalds };
180189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
18021da177e4SLinus Torvalds 
18038a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
18048a5e929dSAl Viro {
18058a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
18068a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
18078a5e929dSAl Viro 		res |= FMODE_READ;
18088a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
18098a5e929dSAl Viro 		res |= FMODE_WRITE;
18108a5e929dSAl Viro 	return res;
18118a5e929dSAl Viro }
18128a5e929dSAl Viro 
1813532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1814cd9a1c0eSTrond Myklebust {
1815532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1816cd9a1c0eSTrond Myklebust }
1817cd9a1c0eSTrond Myklebust 
1818cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1819cd9a1c0eSTrond Myklebust {
1820f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1821cd9a1c0eSTrond Myklebust 	return 0;
1822cd9a1c0eSTrond Myklebust }
1823cd9a1c0eSTrond Myklebust 
1824d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
18250dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1826b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1827cd9a1c0eSTrond Myklebust {
18280dd2b474SMiklos Szeredi 	int err;
18290dd2b474SMiklos Szeredi 
1830be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
183130d90494SAl Viro 	if (err)
1832d9585277SAl Viro 		goto out;
1833eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
183430d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1835eaa2b82cSNeilBrown 	else
18369821421aSTrond Myklebust 		err = -EOPENSTALE;
1837cd9a1c0eSTrond Myklebust out:
1838d9585277SAl Viro 	return err;
1839cd9a1c0eSTrond Myklebust }
1840cd9a1c0eSTrond Myklebust 
184173a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
184230d90494SAl Viro 		    struct file *file, unsigned open_flags,
184344907d79SAl Viro 		    umode_t mode)
18441da177e4SLinus Torvalds {
1845c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1846cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
18470dd2b474SMiklos Szeredi 	struct dentry *res;
18480dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1849f46e0bd3STrond Myklebust 	struct inode *inode;
18501472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1851c94c0953SAl Viro 	bool switched = false;
185273a09dd9SAl Viro 	int created = 0;
1853898f635cSTrond Myklebust 	int err;
18541da177e4SLinus Torvalds 
18550dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
18562b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
18570dd2b474SMiklos Szeredi 
18581e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
18596de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18601e7cb3dcSChuck Lever 
18619597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
18629597c13bSJeff Layton 	if (err)
18639597c13bSJeff Layton 		return err;
18649597c13bSJeff Layton 
18650dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
18660dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
186700699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
18680dd2b474SMiklos Szeredi 			/*
18690dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
18700dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
18710dd2b474SMiklos Szeredi 			 * again
18720dd2b474SMiklos Szeredi 			 */
1873d9585277SAl Viro 			return -ENOENT;
18740dd2b474SMiklos Szeredi 		}
18751472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
18761da177e4SLinus Torvalds 		goto no_open;
18771da177e4SLinus Torvalds 	}
18781da177e4SLinus Torvalds 
18790dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1880d9585277SAl Viro 		return -ENAMETOOLONG;
18811da177e4SLinus Torvalds 
18820dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1883dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1884dff25ddbSAndreas Gruenbacher 
1885dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1886dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1887dff25ddbSAndreas Gruenbacher 
1888536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1889dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
18900dd2b474SMiklos Szeredi 	}
1891536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1892536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1893536e43d1STrond Myklebust 		attr.ia_size = 0;
1894cd9a1c0eSTrond Myklebust 	}
1895cd9a1c0eSTrond Myklebust 
1896c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1897c94c0953SAl Viro 		d_drop(dentry);
1898c94c0953SAl Viro 		switched = true;
1899c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1900c94c0953SAl Viro 					  &dentry->d_name, &wq);
1901c94c0953SAl Viro 		if (IS_ERR(dentry))
1902c94c0953SAl Viro 			return PTR_ERR(dentry);
1903c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1904c94c0953SAl Viro 			return finish_no_open(file, dentry);
1905c94c0953SAl Viro 	}
1906c94c0953SAl Viro 
1907532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
19080dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
19090dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1910d9585277SAl Viro 		goto out;
19110dd2b474SMiklos Szeredi 
19126e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
191373a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
191473a09dd9SAl Viro 	if (created)
191573a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1916275bb307STrond Myklebust 	if (IS_ERR(inode)) {
19170dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
19186e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
19192d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1920d20cb71dSAl Viro 		d_drop(dentry);
19210dd2b474SMiklos Szeredi 		switch (err) {
19221da177e4SLinus Torvalds 		case -ENOENT:
1923774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1924809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
19250dd2b474SMiklos Szeredi 			break;
19261788ea6eSJeff Layton 		case -EISDIR:
19276f926b5bSTrond Myklebust 		case -ENOTDIR:
19286f926b5bSTrond Myklebust 			goto no_open;
19291da177e4SLinus Torvalds 		case -ELOOP:
19300dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
19311da177e4SLinus Torvalds 				goto no_open;
19320dd2b474SMiklos Szeredi 			break;
19331da177e4SLinus Torvalds 			/* case -EINVAL: */
19341da177e4SLinus Torvalds 		default:
19350dd2b474SMiklos Szeredi 			break;
19361da177e4SLinus Torvalds 		}
19371da177e4SLinus Torvalds 		goto out;
19381da177e4SLinus Torvalds 	}
19390dd2b474SMiklos Szeredi 
1940b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
19416e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
19422d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1943d9585277SAl Viro out:
1944c94c0953SAl Viro 	if (unlikely(switched)) {
1945c94c0953SAl Viro 		d_lookup_done(dentry);
1946c94c0953SAl Viro 		dput(dentry);
1947c94c0953SAl Viro 	}
1948d9585277SAl Viro 	return err;
19490dd2b474SMiklos Szeredi 
19501da177e4SLinus Torvalds no_open:
19511472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1952c94c0953SAl Viro 	if (switched) {
1953c94c0953SAl Viro 		d_lookup_done(dentry);
1954c94c0953SAl Viro 		if (!res)
1955c94c0953SAl Viro 			res = dentry;
1956c94c0953SAl Viro 		else
1957c94c0953SAl Viro 			dput(dentry);
1958c94c0953SAl Viro 	}
19590dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1960c94c0953SAl Viro 		return PTR_ERR(res);
1961e45198a6SAl Viro 	return finish_no_open(file, res);
19621da177e4SLinus Torvalds }
196389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
19641da177e4SLinus Torvalds 
1965c7944ebbSTrond Myklebust static int
1966c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1967c7944ebbSTrond Myklebust 			  unsigned int flags)
19681da177e4SLinus Torvalds {
1969657e94b6SNick Piggin 	struct inode *inode;
19701da177e4SLinus Torvalds 
1971fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1972c7944ebbSTrond Myklebust 		goto full_reval;
1973eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1974c7944ebbSTrond Myklebust 		goto full_reval;
19752b484297STrond Myklebust 
19762b0143b5SDavid Howells 	inode = d_inode(dentry);
19772b484297STrond Myklebust 
19781da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
19791da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
19801da177e4SLinus Torvalds 	 */
1981c7944ebbSTrond Myklebust 	if (inode == NULL)
1982c7944ebbSTrond Myklebust 		goto full_reval;
198349317a7fSNeilBrown 
1984efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
1985c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1986216d5d06STrond Myklebust 
19871da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
19881da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1989c7944ebbSTrond Myklebust 		goto full_reval;
1990c7944ebbSTrond Myklebust 
19911da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1992c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1993c7944ebbSTrond Myklebust 		goto reval_dentry;
1994c7944ebbSTrond Myklebust 
1995c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1996c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1997c7944ebbSTrond Myklebust 		goto reval_dentry;
19981da177e4SLinus Torvalds 
19990ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
2000c7944ebbSTrond Myklebust 	return 1;
2001c7944ebbSTrond Myklebust reval_dentry:
2002c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
2003c7944ebbSTrond Myklebust 		return -ECHILD;
200442f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
20050ef97dcfSMiklos Szeredi 
2006c7944ebbSTrond Myklebust full_reval:
2007c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
2008c7944ebbSTrond Myklebust }
2009535918f1STrond Myklebust 
2010c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2011c7944ebbSTrond Myklebust {
2012c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
2013c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
2014c0204fd2STrond Myklebust }
2015c0204fd2STrond Myklebust 
20161da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
20171da177e4SLinus Torvalds 
2018406cd915SBenjamin Coddington struct dentry *
2019406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
20201775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
20211775fd3eSDavid Quigley 				struct nfs4_label *label)
20221da177e4SLinus Torvalds {
2023fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
20242b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
20251da177e4SLinus Torvalds 	struct inode *inode;
2026b0c6108eSAl Viro 	struct dentry *d;
2027406cd915SBenjamin Coddington 	int error;
20281da177e4SLinus Torvalds 
2029fab728e1STrond Myklebust 	d_drop(dentry);
2030fab728e1STrond Myklebust 
20311da177e4SLinus Torvalds 	if (fhandle->size == 0) {
2032f7b37b8bSTrond Myklebust 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
20331da177e4SLinus Torvalds 		if (error)
2034fab728e1STrond Myklebust 			goto out_error;
20351da177e4SLinus Torvalds 	}
20365724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
20371da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
20381da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2039a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
2040a841b54dSTrond Myklebust 				fattr, NULL, NULL);
20411da177e4SLinus Torvalds 		if (error < 0)
2042fab728e1STrond Myklebust 			goto out_error;
20431da177e4SLinus Torvalds 	}
20441775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
2045b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
2046fab728e1STrond Myklebust out:
2047fab728e1STrond Myklebust 	dput(parent);
2048406cd915SBenjamin Coddington 	return d;
2049fab728e1STrond Myklebust out_error:
2050fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
2051406cd915SBenjamin Coddington 	d = ERR_PTR(error);
2052406cd915SBenjamin Coddington 	goto out;
2053406cd915SBenjamin Coddington }
2054406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2055406cd915SBenjamin Coddington 
2056406cd915SBenjamin Coddington /*
2057406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
2058406cd915SBenjamin Coddington  */
2059406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2060406cd915SBenjamin Coddington 				struct nfs_fattr *fattr,
2061406cd915SBenjamin Coddington 				struct nfs4_label *label)
2062406cd915SBenjamin Coddington {
2063406cd915SBenjamin Coddington 	struct dentry *d;
2064406cd915SBenjamin Coddington 
2065406cd915SBenjamin Coddington 	d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
2066406cd915SBenjamin Coddington 	if (IS_ERR(d))
2067406cd915SBenjamin Coddington 		return PTR_ERR(d);
2068406cd915SBenjamin Coddington 
2069406cd915SBenjamin Coddington 	/* Callers don't care */
2070406cd915SBenjamin Coddington 	dput(d);
2071406cd915SBenjamin Coddington 	return 0;
20721da177e4SLinus Torvalds }
2073ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds /*
20761da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
20771da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
20781da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
20791da177e4SLinus Torvalds  * reply path made it appear to have failed.
20801da177e4SLinus Torvalds  */
2081597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
2082ebfc3b49SAl Viro 		umode_t mode, bool excl)
20831da177e4SLinus Torvalds {
20841da177e4SLinus Torvalds 	struct iattr attr;
2085ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
20861da177e4SLinus Torvalds 	int error;
20871da177e4SLinus Torvalds 
20881e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
20896de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	attr.ia_mode = mode;
20921da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20931da177e4SLinus Torvalds 
20948b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
20958867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
20968b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
20971da177e4SLinus Torvalds 	if (error != 0)
20981da177e4SLinus Torvalds 		goto out_err;
20991da177e4SLinus Torvalds 	return 0;
21001da177e4SLinus Torvalds out_err:
21011da177e4SLinus Torvalds 	d_drop(dentry);
21021da177e4SLinus Torvalds 	return error;
21031da177e4SLinus Torvalds }
2104ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds /*
21071da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
21081da177e4SLinus Torvalds  */
2109597d9289SBryan Schumaker int
21101a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
21111da177e4SLinus Torvalds {
21121da177e4SLinus Torvalds 	struct iattr attr;
21131da177e4SLinus Torvalds 	int status;
21141da177e4SLinus Torvalds 
21151e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
21166de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	attr.ia_mode = mode;
21191da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
21201da177e4SLinus Torvalds 
21211ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
21221da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
21231ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
21241da177e4SLinus Torvalds 	if (status != 0)
21251da177e4SLinus Torvalds 		goto out_err;
21261da177e4SLinus Torvalds 	return 0;
21271da177e4SLinus Torvalds out_err:
21281da177e4SLinus Torvalds 	d_drop(dentry);
21291da177e4SLinus Torvalds 	return status;
21301da177e4SLinus Torvalds }
2131ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds /*
21341da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
21351da177e4SLinus Torvalds  */
2136597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
21371da177e4SLinus Torvalds {
21381da177e4SLinus Torvalds 	struct iattr attr;
21391da177e4SLinus Torvalds 	int error;
21401da177e4SLinus Torvalds 
21411e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
21426de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
21451da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
21461da177e4SLinus Torvalds 
21471ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
21481da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
21491ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
21501da177e4SLinus Torvalds 	if (error != 0)
21511da177e4SLinus Torvalds 		goto out_err;
21521da177e4SLinus Torvalds 	return 0;
21531da177e4SLinus Torvalds out_err:
21541da177e4SLinus Torvalds 	d_drop(dentry);
21551da177e4SLinus Torvalds 	return error;
21561da177e4SLinus Torvalds }
2157ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
21581da177e4SLinus Torvalds 
2159d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2160d45b9d8bSTrond Myklebust {
2161dc3f4198SAl Viro 	if (simple_positive(dentry))
2162d45b9d8bSTrond Myklebust 		d_delete(dentry);
2163d45b9d8bSTrond Myklebust }
2164d45b9d8bSTrond Myklebust 
2165597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	int error;
21681da177e4SLinus Torvalds 
21691e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
21706de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
21711da177e4SLinus Torvalds 
21721ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
21732b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2174884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
21751da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
21761da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2177ba6c0592STrond Myklebust 		switch (error) {
2178ba6c0592STrond Myklebust 		case 0:
21792b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2180ba6c0592STrond Myklebust 			break;
2181ba6c0592STrond Myklebust 		case -ENOENT:
2182d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2183ba6c0592STrond Myklebust 		}
2184884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2185ba6c0592STrond Myklebust 	} else
2186ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
21871ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	return error;
21901da177e4SLinus Torvalds }
2191ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds /*
21941da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
21951da177e4SLinus Torvalds  * and after checking that the file has only one user.
21961da177e4SLinus Torvalds  *
21971da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
21981da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
21991da177e4SLinus Torvalds  */
22001da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
22011da177e4SLinus Torvalds {
22022b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
22032b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
22041da177e4SLinus Torvalds 	int error = -EBUSY;
22051da177e4SLinus Torvalds 
22066de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
22091da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
22101da177e4SLinus Torvalds 		error = 0;
22111da177e4SLinus Torvalds 		goto out;
22121da177e4SLinus Torvalds 	}
22131da177e4SLinus Torvalds 
22141ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
22151da177e4SLinus Torvalds 	if (inode != NULL) {
2216912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
22171da177e4SLinus Torvalds 		if (error == 0)
22181b83d707STrond Myklebust 			nfs_drop_nlink(inode);
22191da177e4SLinus Torvalds 	} else
2220912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2221d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2222d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
22231ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
22241da177e4SLinus Torvalds out:
22251da177e4SLinus Torvalds 	return error;
22261da177e4SLinus Torvalds }
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
22291da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
22301da177e4SLinus Torvalds  *
22311da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
22321da177e4SLinus Torvalds  */
2233597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
22341da177e4SLinus Torvalds {
22351da177e4SLinus Torvalds 	int error;
22361da177e4SLinus Torvalds 	int need_rehash = 0;
22371da177e4SLinus Torvalds 
22381e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
22396de1472fSAl Viro 		dir->i_ino, dentry);
22401da177e4SLinus Torvalds 
22411ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
22421da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
224384d08fa8SAl Viro 	if (d_count(dentry) > 1) {
22441da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2245ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
22462b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
22471da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
22481ca42382STrond Myklebust 		goto out;
22491da177e4SLinus Torvalds 	}
22501da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
22511da177e4SLinus Torvalds 		__d_drop(dentry);
22521da177e4SLinus Torvalds 		need_rehash = 1;
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
22551da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
2256d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
22571da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
22581da177e4SLinus Torvalds 	} else if (need_rehash)
22591da177e4SLinus Torvalds 		d_rehash(dentry);
22601ca42382STrond Myklebust out:
22611ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
22621da177e4SLinus Torvalds 	return error;
22631da177e4SLinus Torvalds }
2264ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
22651da177e4SLinus Torvalds 
2266873101b3SChuck Lever /*
2267873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2268873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2269873101b3SChuck Lever  * using prepare_write/commit_write.
2270873101b3SChuck Lever  *
2271873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2272873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2273873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2274873101b3SChuck Lever  * symlink request has completed on the server.
2275873101b3SChuck Lever  *
2276873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2277873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2278873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2279873101b3SChuck Lever  * and move the raw page into its mapping.
2280873101b3SChuck Lever  */
2281597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
22821da177e4SLinus Torvalds {
2283873101b3SChuck Lever 	struct page *page;
2284873101b3SChuck Lever 	char *kaddr;
22851da177e4SLinus Torvalds 	struct iattr attr;
2286873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
22871da177e4SLinus Torvalds 	int error;
22881da177e4SLinus Torvalds 
22891e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
22906de1472fSAl Viro 		dir->i_ino, dentry, symname);
22911da177e4SLinus Torvalds 
2292873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2293873101b3SChuck Lever 		return -ENAMETOOLONG;
22941da177e4SLinus Torvalds 
2295873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2296873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
22971da177e4SLinus Torvalds 
2298e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
229976566991STrond Myklebust 	if (!page)
2300873101b3SChuck Lever 		return -ENOMEM;
2301873101b3SChuck Lever 
2302e8ecde25SAl Viro 	kaddr = page_address(page);
2303873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2304873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2305873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2306873101b3SChuck Lever 
23071ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
230894a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
23091ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2310873101b3SChuck Lever 	if (error != 0) {
23111e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2312873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
23136de1472fSAl Viro 			dentry, symname, error);
23141da177e4SLinus Torvalds 		d_drop(dentry);
2315873101b3SChuck Lever 		__free_page(page);
23161da177e4SLinus Torvalds 		return error;
23171da177e4SLinus Torvalds 	}
23181da177e4SLinus Torvalds 
2319873101b3SChuck Lever 	/*
2320873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2321873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2322873101b3SChuck Lever 	 */
23232b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2324873101b3SChuck Lever 							GFP_KERNEL)) {
2325873101b3SChuck Lever 		SetPageUptodate(page);
2326873101b3SChuck Lever 		unlock_page(page);
2327a0b54addSRafael Aquini 		/*
2328a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2329a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2330a0b54addSRafael Aquini 		 */
233109cbfeafSKirill A. Shutemov 		put_page(page);
2332873101b3SChuck Lever 	} else
2333873101b3SChuck Lever 		__free_page(page);
2334873101b3SChuck Lever 
2335873101b3SChuck Lever 	return 0;
2336873101b3SChuck Lever }
2337ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2338873101b3SChuck Lever 
2339597d9289SBryan Schumaker int
23401da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
23411da177e4SLinus Torvalds {
23422b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
23431da177e4SLinus Torvalds 	int error;
23441da177e4SLinus Torvalds 
23456de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
23466de1472fSAl Viro 		old_dentry, dentry);
23471da177e4SLinus Torvalds 
23481fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
23499697d234STrond Myklebust 	d_drop(dentry);
23501da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2351cf809556STrond Myklebust 	if (error == 0) {
23527de9c6eeSAl Viro 		ihold(inode);
23539697d234STrond Myklebust 		d_add(dentry, inode);
2354cf809556STrond Myklebust 	}
23551fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
23561da177e4SLinus Torvalds 	return error;
23571da177e4SLinus Torvalds }
2358ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds /*
23611da177e4SLinus Torvalds  * RENAME
23621da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
23631da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
23641da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
23651da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
23661da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
23671da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
23681da177e4SLinus Torvalds  *
23691da177e4SLinus Torvalds  * FIXED.
23701da177e4SLinus Torvalds  *
23711da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
23721da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
23731da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
23741da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
23751da177e4SLinus Torvalds  * using the inode layer
23761da177e4SLinus Torvalds  *
23771da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
23781da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
23791da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
23801da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
23811da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
23821da177e4SLinus Torvalds  * the rename.
23831da177e4SLinus Torvalds  */
2384597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
23851cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
23861cd66c93SMiklos Szeredi 	       unsigned int flags)
23871da177e4SLinus Torvalds {
23882b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
23892b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2390d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
239180a491fdSJeff Layton 	struct rpc_task *task;
23921da177e4SLinus Torvalds 	int error = -EBUSY;
23931da177e4SLinus Torvalds 
23941cd66c93SMiklos Szeredi 	if (flags)
23951cd66c93SMiklos Szeredi 		return -EINVAL;
23961cd66c93SMiklos Szeredi 
23976de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
23986de1472fSAl Viro 		 old_dentry, new_dentry,
239984d08fa8SAl Viro 		 d_count(new_dentry));
24001da177e4SLinus Torvalds 
240170ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
24021da177e4SLinus Torvalds 	/*
240328f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
240428f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
240528f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
240628f79a1aSMiklos Szeredi 	 * the new target.
24071da177e4SLinus Torvalds 	 */
240827226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
240927226104SMiklos Szeredi 		/*
241027226104SMiklos Szeredi 		 * To prevent any new references to the target during the
241127226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
241227226104SMiklos Szeredi 		 */
2413d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
241427226104SMiklos Szeredi 			d_drop(new_dentry);
2415d9f29500SBenjamin Coddington 			rehash = new_dentry;
2416d9f29500SBenjamin Coddington 		}
241727226104SMiklos Szeredi 
241884d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
24191da177e4SLinus Torvalds 			int err;
242027226104SMiklos Szeredi 
24211da177e4SLinus Torvalds 			/* copy the target dentry's name */
24221da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
24231da177e4SLinus Torvalds 					 &new_dentry->d_name);
24241da177e4SLinus Torvalds 			if (!dentry)
24251da177e4SLinus Torvalds 				goto out;
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
24281da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
242924e93025SMiklos Szeredi 			if (err)
24301da177e4SLinus Torvalds 				goto out;
243124e93025SMiklos Szeredi 
243224e93025SMiklos Szeredi 			new_dentry = dentry;
2433d9f29500SBenjamin Coddington 			rehash = NULL;
243424e93025SMiklos Szeredi 			new_inode = NULL;
2435b1e4adf4STrond Myklebust 		}
243627226104SMiklos Szeredi 	}
24371da177e4SLinus Torvalds 
2438d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
243980a491fdSJeff Layton 	if (IS_ERR(task)) {
244080a491fdSJeff Layton 		error = PTR_ERR(task);
244180a491fdSJeff Layton 		goto out;
244280a491fdSJeff Layton 	}
244380a491fdSJeff Layton 
244480a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2445818a8dbeSBenjamin Coddington 	if (error != 0) {
2446818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2447818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2448818a8dbeSBenjamin Coddington 		smp_wmb();
2449818a8dbeSBenjamin Coddington 	} else
245080a491fdSJeff Layton 		error = task->tk_status;
245180a491fdSJeff Layton 	rpc_put_task(task);
245259a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
245359a707b0STrond Myklebust 	if (error == 0) {
245459a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
245559a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
245659a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
245759a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
245859a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
245959a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
246059a707b0STrond Myklebust 	}
24611da177e4SLinus Torvalds out:
2462d9f29500SBenjamin Coddington 	if (rehash)
2463d9f29500SBenjamin Coddington 		d_rehash(rehash);
246470ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
246570ded201STrond Myklebust 			new_dir, new_dentry, error);
2466d9f29500SBenjamin Coddington 	if (!error) {
2467d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2468d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2469d9f29500SBenjamin Coddington 		/*
2470d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2471d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2472d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2473d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2474d9f29500SBenjamin Coddington 		 */
2475d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2476d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2477d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2478d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2479d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2480d9f29500SBenjamin Coddington 
24811da177e4SLinus Torvalds 	/* new dentry created? */
24821da177e4SLinus Torvalds 	if (dentry)
24831da177e4SLinus Torvalds 		dput(dentry);
24841da177e4SLinus Torvalds 	return error;
24851da177e4SLinus Torvalds }
2486ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
24871da177e4SLinus Torvalds 
2488cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2489cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2490cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2491cfcea3e8STrond Myklebust 
2492a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
24933a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
24943a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
24953a505845STrond Myklebust 
24961c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
24971c3c07e9STrond Myklebust {
2498b68572e0SNeilBrown 	put_cred(entry->cred);
2499f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
25004e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2501cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
25024e857c58SPeter Zijlstra 	smp_mb__after_atomic();
25031c3c07e9STrond Myklebust }
25041c3c07e9STrond Myklebust 
25051a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
25061a81bb8aSTrond Myklebust {
25071a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
25081a81bb8aSTrond Myklebust 
25091a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
25101a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
25111a81bb8aSTrond Myklebust 		list_del(&cache->lru);
25121a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
25131a81bb8aSTrond Myklebust 	}
25141a81bb8aSTrond Myklebust }
25151a81bb8aSTrond Myklebust 
25163a505845STrond Myklebust static unsigned long
25173a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2518979df72eSTrond Myklebust {
2519979df72eSTrond Myklebust 	LIST_HEAD(head);
2520aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2521979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
25221ab6c499SDave Chinner 	long freed = 0;
2523979df72eSTrond Myklebust 
2524a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2525aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2526979df72eSTrond Myklebust 		struct inode *inode;
2527979df72eSTrond Myklebust 
2528979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2529979df72eSTrond Myklebust 			break;
25309c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2531979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2532979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2533979df72eSTrond Myklebust 			goto remove_lru_entry;
2534979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2535979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2536979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2537979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
25381ab6c499SDave Chinner 		freed++;
2539979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2540979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2541979df72eSTrond Myklebust 					&nfs_access_lru_list);
2542979df72eSTrond Myklebust 		else {
2543979df72eSTrond Myklebust remove_lru_entry:
2544979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
25454e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2546979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
25474e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2548979df72eSTrond Myklebust 		}
254959844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2550979df72eSTrond Myklebust 	}
2551979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
25521a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
25531ab6c499SDave Chinner 	return freed;
25541ab6c499SDave Chinner }
25551ab6c499SDave Chinner 
25561ab6c499SDave Chinner unsigned long
25573a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
25583a505845STrond Myklebust {
25593a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
25603a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
25613a505845STrond Myklebust 
25623a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
25633a505845STrond Myklebust 		return SHRINK_STOP;
25643a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
25653a505845STrond Myklebust }
25663a505845STrond Myklebust 
25673a505845STrond Myklebust 
25683a505845STrond Myklebust unsigned long
25691ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
25701ab6c499SDave Chinner {
257155f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2572979df72eSTrond Myklebust }
2573979df72eSTrond Myklebust 
25743a505845STrond Myklebust static void
25753a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
25763a505845STrond Myklebust {
25773a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
25783a505845STrond Myklebust 	unsigned long diff;
25793a505845STrond Myklebust 	unsigned int nr_to_scan;
25803a505845STrond Myklebust 
25813a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
25823a505845STrond Myklebust 		return;
25833a505845STrond Myklebust 	nr_to_scan = 100;
25843a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
25853a505845STrond Myklebust 	if (diff < nr_to_scan)
25863a505845STrond Myklebust 		nr_to_scan = diff;
25873a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
25883a505845STrond Myklebust }
25893a505845STrond Myklebust 
25901a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
25911c3c07e9STrond Myklebust {
25921c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
25931a81bb8aSTrond Myklebust 	struct rb_node *n;
25941c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
25951c3c07e9STrond Myklebust 
25961c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
25971c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
25981c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
25991c3c07e9STrond Myklebust 		rb_erase(n, root_node);
26001a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
26011c3c07e9STrond Myklebust 	}
26021c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
26031c3c07e9STrond Myklebust }
26041c3c07e9STrond Myklebust 
26051c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
26061c3c07e9STrond Myklebust {
26071a81bb8aSTrond Myklebust 	LIST_HEAD(head);
26081a81bb8aSTrond Myklebust 
26091a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
26101a81bb8aSTrond Myklebust 		return;
2611cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2612cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
26131a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2614cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2615cfcea3e8STrond Myklebust 
26161c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
26171a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
26181a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
26191a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
26201a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
26211c3c07e9STrond Myklebust }
26221c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
26231c3c07e9STrond Myklebust 
2624b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
26251c3c07e9STrond Myklebust {
26261c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
26271c3c07e9STrond Myklebust 
26281c3c07e9STrond Myklebust 	while (n != NULL) {
2629b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2630b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2631b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
26321c3c07e9STrond Myklebust 
2633b68572e0SNeilBrown 		if (cmp < 0)
26341c3c07e9STrond Myklebust 			n = n->rb_left;
2635b68572e0SNeilBrown 		else if (cmp > 0)
26361c3c07e9STrond Myklebust 			n = n->rb_right;
26371c3c07e9STrond Myklebust 		else
26381c3c07e9STrond Myklebust 			return entry;
26391c3c07e9STrond Myklebust 	}
26401c3c07e9STrond Myklebust 	return NULL;
26411c3c07e9STrond Myklebust }
26421c3c07e9STrond Myklebust 
2643d2ae4f8bSFrank 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)
26441da177e4SLinus Torvalds {
264555296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
26461c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
264757b69181STrond Myklebust 	bool retry = true;
264857b69181STrond Myklebust 	int err;
26491da177e4SLinus Torvalds 
26501c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
265157b69181STrond Myklebust 	for(;;) {
26521c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
26531c3c07e9STrond Myklebust 			goto out_zap;
26541c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
265557b69181STrond Myklebust 		err = -ENOENT;
26561c3c07e9STrond Myklebust 		if (cache == NULL)
26571c3c07e9STrond Myklebust 			goto out;
265857b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
265921c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
266057b69181STrond Myklebust 			break;
26615c965db8STrond Myklebust 		if (!retry)
26625c965db8STrond Myklebust 			break;
266357b69181STrond Myklebust 		err = -ECHILD;
266457b69181STrond Myklebust 		if (!may_block)
266557b69181STrond Myklebust 			goto out;
266657b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
266757b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
266857b69181STrond Myklebust 		if (err)
266957b69181STrond Myklebust 			return err;
267057b69181STrond Myklebust 		spin_lock(&inode->i_lock);
267157b69181STrond Myklebust 		retry = false;
267257b69181STrond Myklebust 	}
26731c3c07e9STrond Myklebust 	res->cred = cache->cred;
26741c3c07e9STrond Myklebust 	res->mask = cache->mask;
2675cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
26761c3c07e9STrond Myklebust 	err = 0;
26771c3c07e9STrond Myklebust out:
26781c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
26791c3c07e9STrond Myklebust 	return err;
26801c3c07e9STrond Myklebust out_zap:
26811a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
26821a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
26831c3c07e9STrond Myklebust 	return -ENOENT;
26841c3c07e9STrond Myklebust }
26851c3c07e9STrond Myklebust 
2686b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2687f682a398SNeilBrown {
2688f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2689f682a398SNeilBrown 	 * but do it without locking.
2690f682a398SNeilBrown 	 */
2691f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2692f682a398SNeilBrown 	struct nfs_access_entry *cache;
2693f682a398SNeilBrown 	int err = -ECHILD;
2694f682a398SNeilBrown 	struct list_head *lh;
2695f682a398SNeilBrown 
2696f682a398SNeilBrown 	rcu_read_lock();
2697f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2698f682a398SNeilBrown 		goto out;
26999f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2700f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2701f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
27029a206de2STrond Myklebust 	    cred_fscmp(cred, cache->cred) != 0)
2703f682a398SNeilBrown 		cache = NULL;
2704f682a398SNeilBrown 	if (cache == NULL)
2705f682a398SNeilBrown 		goto out;
270621c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2707f682a398SNeilBrown 		goto out;
2708f682a398SNeilBrown 	res->cred = cache->cred;
2709f682a398SNeilBrown 	res->mask = cache->mask;
271021c3ba7eSTrond Myklebust 	err = 0;
2711f682a398SNeilBrown out:
2712f682a398SNeilBrown 	rcu_read_unlock();
2713f682a398SNeilBrown 	return err;
2714f682a398SNeilBrown }
2715f682a398SNeilBrown 
2716d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2717d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block)
2718d2ae4f8bSFrank van der Linden {
2719d2ae4f8bSFrank van der Linden 	int status;
2720d2ae4f8bSFrank van der Linden 
2721d2ae4f8bSFrank van der Linden 	status = nfs_access_get_cached_rcu(inode, cred, res);
2722d2ae4f8bSFrank van der Linden 	if (status != 0)
2723d2ae4f8bSFrank van der Linden 		status = nfs_access_get_cached_locked(inode, cred, res,
2724d2ae4f8bSFrank van der Linden 		    may_block);
2725d2ae4f8bSFrank van der Linden 
2726d2ae4f8bSFrank van der Linden 	return status;
2727d2ae4f8bSFrank van der Linden }
2728d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2729d2ae4f8bSFrank van der Linden 
27301c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
27311c3c07e9STrond Myklebust {
2732cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2733cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
27341c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
27351c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
27361c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2737b68572e0SNeilBrown 	int cmp;
27381c3c07e9STrond Myklebust 
27391c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
27401c3c07e9STrond Myklebust 	while (*p != NULL) {
27411c3c07e9STrond Myklebust 		parent = *p;
27421c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2743b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
27441c3c07e9STrond Myklebust 
2745b68572e0SNeilBrown 		if (cmp < 0)
27461c3c07e9STrond Myklebust 			p = &parent->rb_left;
2747b68572e0SNeilBrown 		else if (cmp > 0)
27481c3c07e9STrond Myklebust 			p = &parent->rb_right;
27491c3c07e9STrond Myklebust 		else
27501c3c07e9STrond Myklebust 			goto found;
27511c3c07e9STrond Myklebust 	}
27521c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
27531c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2754cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
27551c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
27561c3c07e9STrond Myklebust 	return;
27571c3c07e9STrond Myklebust found:
27581c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2759cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2760cfcea3e8STrond Myklebust 	list_del(&entry->lru);
27611c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
27621c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds 
27656168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
27661da177e4SLinus Torvalds {
27671c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
27681c3c07e9STrond Myklebust 	if (cache == NULL)
27691c3c07e9STrond Myklebust 		return;
27701c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2771b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
27721da177e4SLinus Torvalds 	cache->mask = set->mask;
27731c3c07e9STrond Myklebust 
2774f682a398SNeilBrown 	/* The above field assignments must be visible
2775f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2776f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2777f682a398SNeilBrown 	 */
2778f682a398SNeilBrown 	smp_wmb();
27791c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2780cfcea3e8STrond Myklebust 
2781cfcea3e8STrond Myklebust 	/* Update accounting */
27824e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2783cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
27844e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2785cfcea3e8STrond Myklebust 
2786cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
27871a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2788cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
27891a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
27901a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
27911a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2792cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2793cfcea3e8STrond Myklebust 	}
27943a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
27951da177e4SLinus Torvalds }
27966168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
27976168f62cSWeston Andros Adamson 
27983c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
27993c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
28003c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
28013c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
28023c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
28033c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2804ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
28053c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
28063c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
280715d4b73aSTrond Myklebust static int
2808ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
280915d4b73aSTrond Myklebust {
281015d4b73aSTrond Myklebust 	int mask = 0;
281115d4b73aSTrond Myklebust 
281215d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
281315d4b73aSTrond Myklebust 		mask |= MAY_READ;
2814ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2815ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
281615d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2817ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
281815d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2819ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2820ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2821ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2822ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
282315d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2824ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2825ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
282615d4b73aSTrond Myklebust 	return mask;
282715d4b73aSTrond Myklebust }
282815d4b73aSTrond Myklebust 
28296168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
28306168f62cSWeston Andros Adamson {
2831bd8b2441STrond Myklebust 	entry->mask = access_result;
28326168f62cSWeston Andros Adamson }
28336168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
28341da177e4SLinus Torvalds 
2835b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
28361da177e4SLinus Torvalds {
28371da177e4SLinus Torvalds 	struct nfs_access_entry cache;
283857b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2839e8194b7dSTrond Myklebust 	int cache_mask = -1;
28401da177e4SLinus Torvalds 	int status;
28411da177e4SLinus Torvalds 
2842f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2843f4ce1299STrond Myklebust 
284457b69181STrond Myklebust 	status = nfs_access_get_cached(inode, cred, &cache, may_block);
28451da177e4SLinus Torvalds 	if (status == 0)
2846f4ce1299STrond Myklebust 		goto out_cached;
28471da177e4SLinus Torvalds 
2848f3324a2aSNeilBrown 	status = -ECHILD;
284957b69181STrond Myklebust 	if (!may_block)
2850f3324a2aSNeilBrown 		goto out;
2851f3324a2aSNeilBrown 
28521750d929SAnna Schumaker 	/*
28531750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
28541750d929SAnna Schumaker 	 */
28551750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
285672832a24SFrank van der Linden 	if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
285772832a24SFrank van der Linden 		cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
285872832a24SFrank van der Linden 		    NFS_ACCESS_XALIST;
285972832a24SFrank van der Linden 	}
28601750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
28611750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
28621750d929SAnna Schumaker 	else
28631750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
28641da177e4SLinus Torvalds 	cache.cred = cred;
28651da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2866a71ee337SSuresh Jayaraman 	if (status != 0) {
2867a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2868a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
286993ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
287093ce4af7STrond Myklebust 			else
287193ce4af7STrond Myklebust 				nfs_zap_caches(inode);
2872a71ee337SSuresh Jayaraman 		}
2873f4ce1299STrond Myklebust 		goto out;
2874a71ee337SSuresh Jayaraman 	}
28751da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2876f4ce1299STrond Myklebust out_cached:
2877ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2878bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2879f4ce1299STrond Myklebust 		status = -EACCES;
28801da177e4SLinus Torvalds out:
2881e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
2882f4ce1299STrond Myklebust 	return status;
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
2885af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2886af22f94aSTrond Myklebust {
2887af22f94aSTrond Myklebust 	int mask = 0;
2888af22f94aSTrond Myklebust 
2889f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2890f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2891f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2892f8d9a897SWeston Andros Adamson 	} else {
28938a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2894af22f94aSTrond Myklebust 			mask |= MAY_READ;
28958a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2896af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2897f8d9a897SWeston Andros Adamson 	}
2898f8d9a897SWeston Andros Adamson 
2899af22f94aSTrond Myklebust 	return mask;
2900af22f94aSTrond Myklebust }
2901af22f94aSTrond Myklebust 
2902b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2903af22f94aSTrond Myklebust {
2904af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2905af22f94aSTrond Myklebust }
290689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2907af22f94aSTrond Myklebust 
29085c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
29095c5fc09aSTrond Myklebust {
29105c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
291121c3ba7eSTrond Myklebust 	int ret = 0;
29125c5fc09aSTrond Myklebust 
29133825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
29143825827eSTrond Myklebust 		return 0;
2915cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
29165c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
291721c3ba7eSTrond Myklebust 			return -ECHILD;
291821c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
291921c3ba7eSTrond Myklebust 	}
29205c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
29215c5fc09aSTrond Myklebust 		ret = -EACCES;
29225c5fc09aSTrond Myklebust 	return ret;
29235c5fc09aSTrond Myklebust }
29245c5fc09aSTrond Myklebust 
292510556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
29261da177e4SLinus Torvalds {
2927b68572e0SNeilBrown 	const struct cred *cred = current_cred();
29281da177e4SLinus Torvalds 	int res = 0;
29291da177e4SLinus Torvalds 
293091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
293191d5b470SChuck Lever 
2932e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
29331da177e4SLinus Torvalds 		goto out;
29341da177e4SLinus Torvalds 	/* Is this sys_access() ? */
29359cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
29361da177e4SLinus Torvalds 		goto force_lookup;
29371da177e4SLinus Torvalds 
29381da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
29391da177e4SLinus Torvalds 		case S_IFLNK:
29401da177e4SLinus Torvalds 			goto out;
29411da177e4SLinus Torvalds 		case S_IFREG:
2942762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2943762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2944762674f8STrond Myklebust 				return 0;
29451da177e4SLinus Torvalds 			break;
29461da177e4SLinus Torvalds 		case S_IFDIR:
29471da177e4SLinus Torvalds 			/*
29481da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
29491da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
29501da177e4SLinus Torvalds 			 */
29511da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
29521da177e4SLinus Torvalds 				goto out;
29531da177e4SLinus Torvalds 	}
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds force_lookup:
29561da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
29571da177e4SLinus Torvalds 		goto out_notsup;
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
29601da177e4SLinus Torvalds out:
29615c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
29625c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2963f696a365SMiklos Szeredi 
29641e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
29651e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
29661da177e4SLinus Torvalds 	return res;
29671da177e4SLinus Torvalds out_notsup:
2968d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2969d51ac1a8SNeilBrown 		return -ECHILD;
2970d51ac1a8SNeilBrown 
29711da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
29721da177e4SLinus Torvalds 	if (res == 0)
29732830ba7fSAl Viro 		res = generic_permission(inode, mask);
29741e7cb3dcSChuck Lever 	goto out;
29751da177e4SLinus Torvalds }
2976ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds /*
29791da177e4SLinus Torvalds  * Local variables:
29801da177e4SLinus Torvalds  *  version-control: t
29811da177e4SLinus Torvalds  *  kept-new-versions: 5
29821da177e4SLinus Torvalds  * End:
29831da177e4SLinus Torvalds  */
2984