xref: /openbmc/linux/fs/nfs/dir.c (revision 2067231a)
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 
21b6459415SJakub Kicinski #include <linux/compat.h>
22ddda8e0aSBryan Schumaker #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/time.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/stat.h>
261da177e4SLinus Torvalds #include <linux/fcntl.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
321da177e4SLinus Torvalds #include <linux/nfs_fs.h>
331da177e4SLinus Torvalds #include <linux/nfs_mount.h>
341da177e4SLinus Torvalds #include <linux/pagemap.h>
35873101b3SChuck Lever #include <linux/pagevec.h>
361da177e4SLinus Torvalds #include <linux/namei.h>
3754ceac45SDavid Howells #include <linux/mount.h>
38a0b8cab3SMel Gorman #include <linux/swap.h>
39e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
4004e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4164c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
42830f1111STrond Myklebust #include <linux/hash.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include "delegation.h"
4591d5b470SChuck Lever #include "iostat.h"
464c30d56eSAdrian Bunk #include "internal.h"
47cd9a1c0eSTrond Myklebust #include "fscache.h"
481da177e4SLinus Torvalds 
49f4ce1299STrond Myklebust #include "nfstrace.h"
50f4ce1299STrond Myklebust 
511da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
54480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5523db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5602c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
57f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
58aa5dc8c4SMatthew Wilcox (Oracle) static void nfs_readdir_free_folio(struct folio *);
591da177e4SLinus Torvalds 
604b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
61f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
621da177e4SLinus Torvalds 	.read		= generic_read_dir,
6393a6ab7bSTrond Myklebust 	.iterate_shared	= nfs_readdir,
641da177e4SLinus Torvalds 	.open		= nfs_opendir,
65480c2006SBryan Schumaker 	.release	= nfs_closedir,
661da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
6911de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
70aa5dc8c4SMatthew Wilcox (Oracle) 	.free_folio = nfs_readdir_free_folio,
71d1bacf9eSBryan Schumaker };
72d1bacf9eSBryan Schumaker 
73580f2367STrond Myklebust #define NFS_INIT_DTSIZE PAGE_SIZE
74580f2367STrond Myklebust 
75281f31b2STrond Myklebust static struct nfs_open_dir_context *
76281f31b2STrond Myklebust alloc_nfs_open_dir_context(struct inode *dir)
77480c2006SBryan Schumaker {
78311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
79480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
80281f31b2STrond Myklebust 
81281f31b2STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
82480c2006SBryan Schumaker 	if (ctx != NULL) {
83311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
84580f2367STrond Myklebust 		ctx->dtsize = NFS_INIT_DTSIZE;
85311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
861c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
871c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
88ac46b3d7STrond Myklebust 			nfs_set_cache_invalid(dir,
89ac46b3d7STrond Myklebust 					      NFS_INO_INVALID_DATA |
90ac46b3d7STrond Myklebust 						      NFS_INO_REVAL_FORCED);
91230bc98fSTrond Myklebust 		list_add_tail_rcu(&ctx->list, &nfsi->open_files);
92d1e32ea3STrond Myklebust 		memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
93311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
94480c2006SBryan Schumaker 		return ctx;
95480c2006SBryan Schumaker 	}
960c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
970c030806STrond Myklebust }
98480c2006SBryan Schumaker 
99311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
100480c2006SBryan Schumaker {
101311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
102230bc98fSTrond Myklebust 	list_del_rcu(&ctx->list);
103311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
104230bc98fSTrond Myklebust 	kfree_rcu(ctx, rcu_head);
105480c2006SBryan Schumaker }
106480c2006SBryan Schumaker 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * Open file
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds static int
1111da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1121da177e4SLinus Torvalds {
113480c2006SBryan Schumaker 	int res = 0;
114480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1151da177e4SLinus Torvalds 
1166de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
117cc0dd2d1SChuck Lever 
118cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1191e7cb3dcSChuck Lever 
12093b8959aSTrond Myklebust 	ctx = alloc_nfs_open_dir_context(inode);
121480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
122480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
123480c2006SBryan Schumaker 		goto out;
124480c2006SBryan Schumaker 	}
125480c2006SBryan Schumaker 	filp->private_data = ctx;
126480c2006SBryan Schumaker out:
1271da177e4SLinus Torvalds 	return res;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
130480c2006SBryan Schumaker static int
131480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
132480c2006SBryan Schumaker {
133a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
134480c2006SBryan Schumaker 	return 0;
135480c2006SBryan Schumaker }
136480c2006SBryan Schumaker 
137d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
138d1bacf9eSBryan Schumaker 	u64 cookie;
139d1bacf9eSBryan Schumaker 	u64 ino;
140a52a8a6aSTrond Myklebust 	const char *name;
141a52a8a6aSTrond Myklebust 	unsigned int name_len;
1420b26a0bfSTrond Myklebust 	unsigned char d_type;
143d1bacf9eSBryan Schumaker };
144d1bacf9eSBryan Schumaker 
145d1bacf9eSBryan Schumaker struct nfs_cache_array {
146d09e673fSTrond Myklebust 	u64 change_attr;
147d1bacf9eSBryan Schumaker 	u64 last_cookie;
148b1e21c97STrond Myklebust 	unsigned int size;
149b1e21c97STrond Myklebust 	unsigned char page_full : 1,
150762567b7STrond Myklebust 		      page_is_eof : 1,
151762567b7STrond Myklebust 		      cookies_are_ordered : 1;
1525601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
153d1bacf9eSBryan Schumaker };
154d1bacf9eSBryan Schumaker 
1556c981effSTrond Myklebust struct nfs_readdir_descriptor {
1561da177e4SLinus Torvalds 	struct file	*file;
1571da177e4SLinus Torvalds 	struct page	*page;
15823db8620SAl Viro 	struct dir_context *ctx;
1591f1d4aa4STrond Myklebust 	pgoff_t		page_index;
160580f2367STrond Myklebust 	pgoff_t		page_index_max;
1612e7a4641STrond Myklebust 	u64		dir_cookie;
1620aded708STrond Myklebust 	u64		last_cookie;
163f0dd2136STrond Myklebust 	loff_t		current_index;
164d1bacf9eSBryan Schumaker 
165b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
166a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1671f4eab7eSNeil Brown 	unsigned long	timestamp;
1684704f0e2STrond Myklebust 	unsigned long	gencount;
1692e7a4641STrond Myklebust 	unsigned long	attr_gencount;
170d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
171580f2367STrond Myklebust 	unsigned int	buffer_fills;
172580f2367STrond Myklebust 	unsigned int	dtsize;
173b0365ccbSTrond Myklebust 	bool clear_cache;
174a7a3b1e9SBenjamin Coddington 	bool plus;
175e1d2699bSTrond Myklebust 	bool eob;
176a7a3b1e9SBenjamin Coddington 	bool eof;
1776c981effSTrond Myklebust };
1781da177e4SLinus Torvalds 
179580f2367STrond Myklebust static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
180580f2367STrond Myklebust {
181580f2367STrond Myklebust 	struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
182580f2367STrond Myklebust 	unsigned int maxsize = server->dtsize;
183580f2367STrond Myklebust 
184580f2367STrond Myklebust 	if (sz > maxsize)
185580f2367STrond Myklebust 		sz = maxsize;
186580f2367STrond Myklebust 	if (sz < NFS_MIN_FILE_IO_SIZE)
187580f2367STrond Myklebust 		sz = NFS_MIN_FILE_IO_SIZE;
188580f2367STrond Myklebust 	desc->dtsize = sz;
189580f2367STrond Myklebust }
190580f2367STrond Myklebust 
191580f2367STrond Myklebust static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
192580f2367STrond Myklebust {
193580f2367STrond Myklebust 	nfs_set_dtsize(desc, desc->dtsize >> 1);
194580f2367STrond Myklebust }
195580f2367STrond Myklebust 
196580f2367STrond Myklebust static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
197580f2367STrond Myklebust {
198580f2367STrond Myklebust 	nfs_set_dtsize(desc, desc->dtsize << 1);
199580f2367STrond Myklebust }
200580f2367STrond Myklebust 
201d09e673fSTrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie,
202d09e673fSTrond Myklebust 					u64 change_attr)
2034b310319STrond Myklebust {
2044b310319STrond Myklebust 	struct nfs_cache_array *array;
2054b310319STrond Myklebust 
2064b310319STrond Myklebust 	array = kmap_atomic(page);
207d09e673fSTrond Myklebust 	array->change_attr = change_attr;
2081f1d4aa4STrond Myklebust 	array->last_cookie = last_cookie;
2099332cf14STrond Myklebust 	array->size = 0;
2109332cf14STrond Myklebust 	array->page_full = 0;
2119332cf14STrond Myklebust 	array->page_is_eof = 0;
212762567b7STrond Myklebust 	array->cookies_are_ordered = 1;
2134b310319STrond Myklebust 	kunmap_atomic(array);
2144b310319STrond Myklebust }
2154b310319STrond Myklebust 
216d1bacf9eSBryan Schumaker /*
217d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
218d1bacf9eSBryan Schumaker  */
2199332cf14STrond Myklebust static void nfs_readdir_clear_array(struct page *page)
220d1bacf9eSBryan Schumaker {
22111de3b11STrond Myklebust 	struct nfs_cache_array *array;
2229332cf14STrond Myklebust 	unsigned int i;
2238cd51a0cSTrond Myklebust 
2242b86ce2dSCong Wang 	array = kmap_atomic(page);
225d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
226a52a8a6aSTrond Myklebust 		kfree(array->array[i].name);
2279332cf14STrond Myklebust 	array->size = 0;
2282b86ce2dSCong Wang 	kunmap_atomic(array);
229d1bacf9eSBryan Schumaker }
230d1bacf9eSBryan Schumaker 
231aa5dc8c4SMatthew Wilcox (Oracle) static void nfs_readdir_free_folio(struct folio *folio)
232aa5dc8c4SMatthew Wilcox (Oracle) {
233aa5dc8c4SMatthew Wilcox (Oracle) 	nfs_readdir_clear_array(&folio->page);
234aa5dc8c4SMatthew Wilcox (Oracle) }
235aa5dc8c4SMatthew Wilcox (Oracle) 
236b0365ccbSTrond Myklebust static void nfs_readdir_page_reinit_array(struct page *page, u64 last_cookie,
237b0365ccbSTrond Myklebust 					  u64 change_attr)
238b0365ccbSTrond Myklebust {
239b0365ccbSTrond Myklebust 	nfs_readdir_clear_array(page);
240b0365ccbSTrond Myklebust 	nfs_readdir_page_init_array(page, last_cookie, change_attr);
241b0365ccbSTrond Myklebust }
242b0365ccbSTrond Myklebust 
24335df59d3STrond Myklebust static struct page *
24435df59d3STrond Myklebust nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
24535df59d3STrond Myklebust {
24635df59d3STrond Myklebust 	struct page *page = alloc_page(gfp_flags);
24735df59d3STrond Myklebust 	if (page)
248d09e673fSTrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie, 0);
24935df59d3STrond Myklebust 	return page;
25035df59d3STrond Myklebust }
25135df59d3STrond Myklebust 
25235df59d3STrond Myklebust static void nfs_readdir_page_array_free(struct page *page)
25335df59d3STrond Myklebust {
25435df59d3STrond Myklebust 	if (page) {
25535df59d3STrond Myklebust 		nfs_readdir_clear_array(page);
25635df59d3STrond Myklebust 		put_page(page);
25735df59d3STrond Myklebust 	}
25835df59d3STrond Myklebust }
25935df59d3STrond Myklebust 
260e47a62dfSTrond Myklebust static u64 nfs_readdir_array_index_cookie(struct nfs_cache_array *array)
261e47a62dfSTrond Myklebust {
262e47a62dfSTrond Myklebust 	return array->size == 0 ? array->last_cookie : array->array[0].cookie;
263e47a62dfSTrond Myklebust }
264e47a62dfSTrond Myklebust 
265b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
266b1e21c97STrond Myklebust {
267b1e21c97STrond Myklebust 	array->page_is_eof = 1;
268b1e21c97STrond Myklebust 	array->page_full = 1;
269b1e21c97STrond Myklebust }
270b1e21c97STrond Myklebust 
271b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
272b1e21c97STrond Myklebust {
273b1e21c97STrond Myklebust 	return array->page_full;
274b1e21c97STrond Myklebust }
275b1e21c97STrond Myklebust 
276d1bacf9eSBryan Schumaker /*
277d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
278d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
279d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
280d1bacf9eSBryan Schumaker  */
281a52a8a6aSTrond Myklebust static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
282d1bacf9eSBryan Schumaker {
283a52a8a6aSTrond Myklebust 	const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
284a52a8a6aSTrond Myklebust 
28504e4bd1cSCatalin Marinas 	/*
28604e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
28704e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
28804e4bd1cSCatalin Marinas 	 */
289a52a8a6aSTrond Myklebust 	if (ret != NULL)
290a52a8a6aSTrond Myklebust 		kmemleak_not_leak(ret);
291a52a8a6aSTrond Myklebust 	return ret;
292d1bacf9eSBryan Schumaker }
293d1bacf9eSBryan Schumaker 
2940b2662b7STrond Myklebust static size_t nfs_readdir_array_maxentries(void)
2950b2662b7STrond Myklebust {
2960b2662b7STrond Myklebust 	return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
2970b2662b7STrond Myklebust 	       sizeof(struct nfs_cache_array_entry);
2980b2662b7STrond Myklebust }
2990b2662b7STrond Myklebust 
300b1e21c97STrond Myklebust /*
301b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
302b1e21c97STrond Myklebust  */
303b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
304b1e21c97STrond Myklebust {
305b1e21c97STrond Myklebust 	if (array->page_full)
306b1e21c97STrond Myklebust 		return -ENOSPC;
3070b2662b7STrond Myklebust 	if (array->size == nfs_readdir_array_maxentries()) {
308b1e21c97STrond Myklebust 		array->page_full = 1;
309b1e21c97STrond Myklebust 		return -ENOSPC;
310b1e21c97STrond Myklebust 	}
311d1bacf9eSBryan Schumaker 	return 0;
312d1bacf9eSBryan Schumaker }
313d1bacf9eSBryan Schumaker 
3140adf85b4STrond Myklebust static int nfs_readdir_page_array_append(struct page *page,
3150adf85b4STrond Myklebust 					 const struct nfs_entry *entry,
3160adf85b4STrond Myklebust 					 u64 *cookie)
317d1bacf9eSBryan Schumaker {
318a52a8a6aSTrond Myklebust 	struct nfs_cache_array *array;
3194a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
320a52a8a6aSTrond Myklebust 	const char *name;
3210adf85b4STrond Myklebust 	int ret = -ENOMEM;
3224a201d6eSTrond Myklebust 
323a52a8a6aSTrond Myklebust 	name = nfs_readdir_copy_name(entry->name, entry->len);
3243020093fSTrond Myklebust 
325a52a8a6aSTrond Myklebust 	array = kmap_atomic(page);
3260adf85b4STrond Myklebust 	if (!name)
3270adf85b4STrond Myklebust 		goto out;
328b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
329a52a8a6aSTrond Myklebust 	if (ret) {
330a52a8a6aSTrond Myklebust 		kfree(name);
3313020093fSTrond Myklebust 		goto out;
332a52a8a6aSTrond Myklebust 	}
3333020093fSTrond Myklebust 
334b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
3350adf85b4STrond Myklebust 	cache_entry->cookie = array->last_cookie;
3364a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
3370b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
338a52a8a6aSTrond Myklebust 	cache_entry->name_len = entry->len;
339a52a8a6aSTrond Myklebust 	cache_entry->name = name;
340d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
341762567b7STrond Myklebust 	if (array->last_cookie <= cache_entry->cookie)
342762567b7STrond Myklebust 		array->cookies_are_ordered = 0;
3438cd51a0cSTrond Myklebust 	array->size++;
34447c716cbSTrond Myklebust 	if (entry->eof != 0)
345b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
3464a201d6eSTrond Myklebust out:
3470adf85b4STrond Myklebust 	*cookie = array->last_cookie;
348a52a8a6aSTrond Myklebust 	kunmap_atomic(array);
3494a201d6eSTrond Myklebust 	return ret;
350d1bacf9eSBryan Schumaker }
351d1bacf9eSBryan Schumaker 
352f648022fSTrond Myklebust #define NFS_READDIR_COOKIE_MASK (U32_MAX >> 14)
353f648022fSTrond Myklebust /*
354f648022fSTrond Myklebust  * Hash algorithm allowing content addressible access to sequences
355f648022fSTrond Myklebust  * of directory cookies. Content is addressed by the value of the
356f648022fSTrond Myklebust  * cookie index of the first readdir entry in a page.
357f648022fSTrond Myklebust  *
358830f1111STrond Myklebust  * We select only the first 18 bits to avoid issues with excessive
359f648022fSTrond Myklebust  * memory use for the page cache XArray. 18 bits should allow the caching
360f648022fSTrond Myklebust  * of 262144 pages of sequences of readdir entries. Since each page holds
361f648022fSTrond Myklebust  * 127 readdir entries for a typical 64-bit system, that works out to a
362f648022fSTrond Myklebust  * cache of ~ 33 million entries per directory.
363f648022fSTrond Myklebust  */
364f648022fSTrond Myklebust static pgoff_t nfs_readdir_page_cookie_hash(u64 cookie)
365f648022fSTrond Myklebust {
366f648022fSTrond Myklebust 	if (cookie == 0)
367f648022fSTrond Myklebust 		return 0;
368830f1111STrond Myklebust 	return hash_64(cookie, 18);
369f648022fSTrond Myklebust }
370f648022fSTrond Myklebust 
371d09e673fSTrond Myklebust static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie,
372d09e673fSTrond Myklebust 				      u64 change_attr)
373d09e673fSTrond Myklebust {
374d09e673fSTrond Myklebust 	struct nfs_cache_array *array = kmap_atomic(page);
375d09e673fSTrond Myklebust 	int ret = true;
376d09e673fSTrond Myklebust 
377d09e673fSTrond Myklebust 	if (array->change_attr != change_attr)
378d09e673fSTrond Myklebust 		ret = false;
379e47a62dfSTrond Myklebust 	if (nfs_readdir_array_index_cookie(array) != last_cookie)
380d09e673fSTrond Myklebust 		ret = false;
381d09e673fSTrond Myklebust 	kunmap_atomic(array);
382d09e673fSTrond Myklebust 	return ret;
383d09e673fSTrond Myklebust }
384d09e673fSTrond Myklebust 
385d09e673fSTrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
386d09e673fSTrond Myklebust {
387d09e673fSTrond Myklebust 	unlock_page(page);
388d09e673fSTrond Myklebust 	put_page(page);
389d09e673fSTrond Myklebust }
390d09e673fSTrond Myklebust 
391648a4548STrond Myklebust static void nfs_readdir_page_init_and_validate(struct page *page, u64 cookie,
392f648022fSTrond Myklebust 					       u64 change_attr)
3931f1d4aa4STrond Myklebust {
394648a4548STrond Myklebust 	if (PageUptodate(page)) {
395648a4548STrond Myklebust 		if (nfs_readdir_page_validate(page, cookie, change_attr))
396648a4548STrond Myklebust 			return;
397648a4548STrond Myklebust 		nfs_readdir_clear_array(page);
398648a4548STrond Myklebust 	}
399648a4548STrond Myklebust 	nfs_readdir_page_init_array(page, cookie, change_attr);
400648a4548STrond Myklebust 	SetPageUptodate(page);
401648a4548STrond Myklebust }
402648a4548STrond Myklebust 
403648a4548STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
404648a4548STrond Myklebust 						u64 cookie, u64 change_attr)
405648a4548STrond Myklebust {
406648a4548STrond Myklebust 	pgoff_t index = nfs_readdir_page_cookie_hash(cookie);
4071f1d4aa4STrond Myklebust 	struct page *page;
4081f1d4aa4STrond Myklebust 
4091f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
410d09e673fSTrond Myklebust 	if (!page)
411d09e673fSTrond Myklebust 		return NULL;
412648a4548STrond Myklebust 	nfs_readdir_page_init_and_validate(page, cookie, change_attr);
4131f1d4aa4STrond Myklebust 	return page;
4141f1d4aa4STrond Myklebust }
4151f1d4aa4STrond Myklebust 
4161f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
4171f1d4aa4STrond Myklebust {
4181f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
4191f1d4aa4STrond Myklebust 	u64 ret;
4201f1d4aa4STrond Myklebust 
4211f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
4221f1d4aa4STrond Myklebust 	ret = array->last_cookie;
4231f1d4aa4STrond Myklebust 	kunmap_atomic(array);
4241f1d4aa4STrond Myklebust 	return ret;
4251f1d4aa4STrond Myklebust }
4261f1d4aa4STrond Myklebust 
4271f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
4281f1d4aa4STrond Myklebust {
4291f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
4301f1d4aa4STrond Myklebust 	bool ret;
4311f1d4aa4STrond Myklebust 
4321f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
4331f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
4341f1d4aa4STrond Myklebust 	kunmap_atomic(array);
4351f1d4aa4STrond Myklebust 	return ret;
4361f1d4aa4STrond Myklebust }
4371f1d4aa4STrond Myklebust 
438b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
439b1e21c97STrond Myklebust {
440b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
441b1e21c97STrond Myklebust 
442b1e21c97STrond Myklebust 	array = kmap_atomic(page);
443b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
444b1e21c97STrond Myklebust 	kunmap_atomic(array);
445b1e21c97STrond Myklebust }
446b1e21c97STrond Myklebust 
4473b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
448f648022fSTrond Myklebust 					      u64 cookie, u64 change_attr)
4493b2a09f1STrond Myklebust {
450648a4548STrond Myklebust 	pgoff_t index = nfs_readdir_page_cookie_hash(cookie);
4513b2a09f1STrond Myklebust 	struct page *page;
4523b2a09f1STrond Myklebust 
453648a4548STrond Myklebust 	page = grab_cache_page_nowait(mapping, index);
454b0365ccbSTrond Myklebust 	if (!page)
4553b2a09f1STrond Myklebust 		return NULL;
456648a4548STrond Myklebust 	nfs_readdir_page_init_and_validate(page, cookie, change_attr);
457b0365ccbSTrond Myklebust 	if (nfs_readdir_page_last_cookie(page) != cookie)
458b0365ccbSTrond Myklebust 		nfs_readdir_page_reinit_array(page, cookie, change_attr);
459b0365ccbSTrond Myklebust 	return page;
4603b2a09f1STrond Myklebust }
4613b2a09f1STrond Myklebust 
46259e356a9STrond Myklebust static inline
46359e356a9STrond Myklebust int is_32bit_api(void)
46459e356a9STrond Myklebust {
46559e356a9STrond Myklebust #ifdef CONFIG_COMPAT
46659e356a9STrond Myklebust 	return in_compat_syscall();
46759e356a9STrond Myklebust #else
46859e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
46959e356a9STrond Myklebust #endif
47059e356a9STrond Myklebust }
47159e356a9STrond Myklebust 
47259e356a9STrond Myklebust static
47359e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
47459e356a9STrond Myklebust {
47559e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
47659e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
47759e356a9STrond Myklebust 		return false;
47859e356a9STrond Myklebust 	return true;
47959e356a9STrond Myklebust }
48059e356a9STrond Myklebust 
481c8f0523bSTrond Myklebust static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
482c8f0523bSTrond Myklebust 					struct nfs_readdir_descriptor *desc)
483c8f0523bSTrond Myklebust {
484c8f0523bSTrond Myklebust 	if (array->page_full) {
485c8f0523bSTrond Myklebust 		desc->last_cookie = array->last_cookie;
486c8f0523bSTrond Myklebust 		desc->current_index += array->size;
487c8f0523bSTrond Myklebust 		desc->cache_entry_index = 0;
488c8f0523bSTrond Myklebust 		desc->page_index++;
489c8f0523bSTrond Myklebust 	} else
490e47a62dfSTrond Myklebust 		desc->last_cookie = nfs_readdir_array_index_cookie(array);
491c8f0523bSTrond Myklebust }
492c8f0523bSTrond Myklebust 
493f648022fSTrond Myklebust static void nfs_readdir_rewind_search(struct nfs_readdir_descriptor *desc)
494f648022fSTrond Myklebust {
495f648022fSTrond Myklebust 	desc->current_index = 0;
496f648022fSTrond Myklebust 	desc->last_cookie = 0;
497f648022fSTrond Myklebust 	desc->page_index = 0;
498f648022fSTrond Myklebust }
499f648022fSTrond Myklebust 
5006c981effSTrond Myklebust static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
5016c981effSTrond Myklebust 				      struct nfs_readdir_descriptor *desc)
502d1bacf9eSBryan Schumaker {
50323db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
504d1bacf9eSBryan Schumaker 	unsigned int index;
505d1bacf9eSBryan Schumaker 
506d1bacf9eSBryan Schumaker 	if (diff < 0)
507d1bacf9eSBryan Schumaker 		goto out_eof;
508d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
509b1e21c97STrond Myklebust 		if (array->page_is_eof)
510d1bacf9eSBryan Schumaker 			goto out_eof;
511c8f0523bSTrond Myklebust 		nfs_readdir_seek_next_array(array, desc);
512d1bacf9eSBryan Schumaker 		return -EAGAIN;
513d1bacf9eSBryan Schumaker 	}
514d1bacf9eSBryan Schumaker 
515d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
5162e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
517d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
518d1bacf9eSBryan Schumaker 	return 0;
519d1bacf9eSBryan Schumaker out_eof:
5206089dd0dSThomas Meyer 	desc->eof = true;
521d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
522d1bacf9eSBryan Schumaker }
523d1bacf9eSBryan Schumaker 
524762567b7STrond Myklebust static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
525762567b7STrond Myklebust 					      u64 cookie)
526762567b7STrond Myklebust {
527762567b7STrond Myklebust 	if (!array->cookies_are_ordered)
528762567b7STrond Myklebust 		return true;
529762567b7STrond Myklebust 	/* Optimisation for monotonically increasing cookies */
530762567b7STrond Myklebust 	if (cookie >= array->last_cookie)
531762567b7STrond Myklebust 		return false;
532762567b7STrond Myklebust 	if (array->size && cookie < array->array[0].cookie)
533762567b7STrond Myklebust 		return false;
534762567b7STrond Myklebust 	return true;
535762567b7STrond Myklebust }
536762567b7STrond Myklebust 
5376c981effSTrond Myklebust static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
5386c981effSTrond Myklebust 					 struct nfs_readdir_descriptor *desc)
539d1bacf9eSBryan Schumaker {
540f648022fSTrond Myklebust 	unsigned int i;
541d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
542d1bacf9eSBryan Schumaker 
543762567b7STrond Myklebust 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
544762567b7STrond Myklebust 		goto check_eof;
545762567b7STrond Myklebust 
546d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
5472e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
54859e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
5492e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
55059e356a9STrond Myklebust 			else
551f648022fSTrond Myklebust 				desc->ctx->pos = desc->current_index + i;
5528cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
55347c716cbSTrond Myklebust 			return 0;
5548cd51a0cSTrond Myklebust 		}
5558cd51a0cSTrond Myklebust 	}
556762567b7STrond Myklebust check_eof:
557b1e21c97STrond Myklebust 	if (array->page_is_eof) {
558d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
5592e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
5606089dd0dSThomas Meyer 			desc->eof = true;
561c8f0523bSTrond Myklebust 	} else
562c8f0523bSTrond Myklebust 		nfs_readdir_seek_next_array(array, desc);
563d1bacf9eSBryan Schumaker 	return status;
564d1bacf9eSBryan Schumaker }
565d1bacf9eSBryan Schumaker 
5666c981effSTrond Myklebust static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
567d1bacf9eSBryan Schumaker {
568d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
56947c716cbSTrond Myklebust 	int status;
570d1bacf9eSBryan Schumaker 
571ed09222dSTrond Myklebust 	array = kmap_atomic(desc->page);
572d1bacf9eSBryan Schumaker 
5732e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
574d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
575d1bacf9eSBryan Schumaker 	else
576d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
577d1bacf9eSBryan Schumaker 
578ed09222dSTrond Myklebust 	kunmap_atomic(array);
579d1bacf9eSBryan Schumaker 	return status;
580d1bacf9eSBryan Schumaker }
581d1bacf9eSBryan Schumaker 
582d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
58393b8959aSTrond Myklebust static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
584b593c09fSTrond Myklebust 				  __be32 *verf, u64 cookie,
585b593c09fSTrond Myklebust 				  struct page **pages, size_t bufsize,
586b593c09fSTrond Myklebust 				  __be32 *verf_res)
587d1bacf9eSBryan Schumaker {
58882e22a5eSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
58982e22a5eSTrond Myklebust 	struct nfs_readdir_arg arg = {
59082e22a5eSTrond Myklebust 		.dentry = file_dentry(desc->file),
59182e22a5eSTrond Myklebust 		.cred = desc->file->f_cred,
592b593c09fSTrond Myklebust 		.verf = verf,
59382e22a5eSTrond Myklebust 		.cookie = cookie,
59482e22a5eSTrond Myklebust 		.pages = pages,
59582e22a5eSTrond Myklebust 		.page_len = bufsize,
59682e22a5eSTrond Myklebust 		.plus = desc->plus,
59782e22a5eSTrond Myklebust 	};
59882e22a5eSTrond Myklebust 	struct nfs_readdir_res res = {
59982e22a5eSTrond Myklebust 		.verf = verf_res,
60082e22a5eSTrond Myklebust 	};
6014704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
6021da177e4SLinus Torvalds 	int		error;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds  again:
6051da177e4SLinus Torvalds 	timestamp = jiffies;
6064704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
607a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
60882e22a5eSTrond Myklebust 	error = NFS_PROTO(inode)->readdir(&arg, &res);
6091da177e4SLinus Torvalds 	if (error < 0) {
6101da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
6111da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
6121da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
61382e22a5eSTrond Myklebust 			desc->plus = arg.plus = false;
6141da177e4SLinus Torvalds 			goto again;
6151da177e4SLinus Torvalds 		}
6161da177e4SLinus Torvalds 		goto error;
6171da177e4SLinus Torvalds 	}
6181f4eab7eSNeil Brown 	desc->timestamp = timestamp;
6194704f0e2STrond Myklebust 	desc->gencount = gencount;
620d1bacf9eSBryan Schumaker error:
621d1bacf9eSBryan Schumaker 	return error;
622d1bacf9eSBryan Schumaker }
623d1bacf9eSBryan Schumaker 
6246c981effSTrond Myklebust static int xdr_decode(struct nfs_readdir_descriptor *desc,
625573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
626d1bacf9eSBryan Schumaker {
62759e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
628573c4e1eSChuck Lever 	int error;
629d1bacf9eSBryan Schumaker 
63059e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
631573c4e1eSChuck Lever 	if (error)
632573c4e1eSChuck Lever 		return error;
633d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
634d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
635d1bacf9eSBryan Schumaker 	return 0;
636d1bacf9eSBryan Schumaker }
637d1bacf9eSBryan Schumaker 
638fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
639fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
640fa923369STrond Myklebust  */
641d39ab9deSBryan Schumaker static
642d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
643d39ab9deSBryan Schumaker {
644d8fdb47fSTrond Myklebust 	struct inode *inode;
645fa923369STrond Myklebust 	struct nfs_inode *nfsi;
646fa923369STrond Myklebust 
6472b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
6482b0143b5SDavid Howells 		return 0;
649fa923369STrond Myklebust 
650d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
651d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
652d8fdb47fSTrond Myklebust 		return 0;
653d8fdb47fSTrond Myklebust 
654d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
6557dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
656d39ab9deSBryan Schumaker 		return 0;
6577dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
6587dc72d5fSTrond Myklebust 		return 0;
6597dc72d5fSTrond Myklebust 	return 1;
660d39ab9deSBryan Schumaker }
661d39ab9deSBryan Schumaker 
662230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL)
663230bc98fSTrond Myklebust 
664230bc98fSTrond Myklebust static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx,
665230bc98fSTrond Myklebust 				unsigned int cache_hits,
666230bc98fSTrond Myklebust 				unsigned int cache_misses)
667d69ee9b8STrond Myklebust {
668d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
669d69ee9b8STrond Myklebust 		return false;
670230bc98fSTrond Myklebust 	if (ctx->pos == 0 ||
671230bc98fSTrond Myklebust 	    cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD)
672d69ee9b8STrond Myklebust 		return true;
673d69ee9b8STrond Myklebust 	return false;
674d69ee9b8STrond Myklebust }
675d69ee9b8STrond Myklebust 
676d69ee9b8STrond Myklebust /*
677230bc98fSTrond Myklebust  * This function is called by the getattr code to request the
67863519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
679d69ee9b8STrond Myklebust  * directory.
680d69ee9b8STrond Myklebust  */
681230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_hit(struct inode *dir)
682d69ee9b8STrond Myklebust {
68363519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
684230bc98fSTrond Myklebust 	struct nfs_open_dir_context *ctx;
68563519fbcSTrond Myklebust 
68663519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
687230bc98fSTrond Myklebust 	    S_ISDIR(dir->i_mode)) {
688230bc98fSTrond Myklebust 		rcu_read_lock();
689230bc98fSTrond Myklebust 		list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
690230bc98fSTrond Myklebust 			atomic_inc(&ctx->cache_hits);
691230bc98fSTrond Myklebust 		rcu_read_unlock();
692230bc98fSTrond Myklebust 	}
693d69ee9b8STrond Myklebust }
694d69ee9b8STrond Myklebust 
695311324adSTrond Myklebust /*
696311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
697311324adSTrond Myklebust  *
698311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
699311324adSTrond Myklebust  */
700230bc98fSTrond Myklebust void nfs_readdir_record_entry_cache_miss(struct inode *dir)
701311324adSTrond Myklebust {
70263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
703230bc98fSTrond Myklebust 	struct nfs_open_dir_context *ctx;
70463519fbcSTrond Myklebust 
70563519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
706230bc98fSTrond Myklebust 	    S_ISDIR(dir->i_mode)) {
707230bc98fSTrond Myklebust 		rcu_read_lock();
708230bc98fSTrond Myklebust 		list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
709230bc98fSTrond Myklebust 			atomic_inc(&ctx->cache_misses);
710230bc98fSTrond Myklebust 		rcu_read_unlock();
711311324adSTrond Myklebust 	}
712311324adSTrond Myklebust }
713311324adSTrond Myklebust 
7140b3cc71bSTrond Myklebust static void nfs_lookup_advise_force_readdirplus(struct inode *dir,
7150b3cc71bSTrond Myklebust 						unsigned int flags)
716230bc98fSTrond Myklebust {
7172c2c3365STrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
7182c2c3365STrond Myklebust 		return;
7190b3cc71bSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL))
7200b3cc71bSTrond Myklebust 		return;
721230bc98fSTrond Myklebust 	nfs_readdir_record_entry_cache_miss(dir);
722230bc98fSTrond Myklebust }
723230bc98fSTrond Myklebust 
724d69ee9b8STrond Myklebust static
725a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
726a1147b82STrond Myklebust 		unsigned long dir_verifier)
727d39ab9deSBryan Schumaker {
72826fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
7299ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
7304a201d6eSTrond Myklebust 	struct dentry *dentry;
7314a201d6eSTrond Myklebust 	struct dentry *alias;
732d39ab9deSBryan Schumaker 	struct inode *inode;
733aa9c2669SDavid Quigley 	int status;
734d39ab9deSBryan Schumaker 
735fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
736fa923369STrond Myklebust 		return;
7376c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
7386c441c25STrond Myklebust 		return;
73978d04af4STrond Myklebust 	if (filename.len == 0)
74078d04af4STrond Myklebust 		return;
74178d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
74278d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
74378d04af4STrond Myklebust 		return;
74478d04af4STrond Myklebust 	/* ...or '/' */
74578d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
74678d04af4STrond Myklebust 		return;
7474a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
7484a201d6eSTrond Myklebust 		if (filename.len == 1)
7494a201d6eSTrond Myklebust 			return;
7504a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
7514a201d6eSTrond Myklebust 			return;
7524a201d6eSTrond Myklebust 	}
7538387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
754d39ab9deSBryan Schumaker 
7554a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
7569ac3d3e8SAl Viro again:
7579ac3d3e8SAl Viro 	if (!dentry) {
7589ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
7599ac3d3e8SAl Viro 		if (IS_ERR(dentry))
7609ac3d3e8SAl Viro 			return;
7619ac3d3e8SAl Viro 	}
7629ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
7636c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
7646c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
7656c441c25STrond Myklebust 					&entry->fattr->fsid))
7666c441c25STrond Myklebust 			goto out;
767d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
7687dc72d5fSTrond Myklebust 			if (!entry->fh->size)
7697dc72d5fSTrond Myklebust 				goto out;
770a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
7712b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
772aa9c2669SDavid Quigley 			if (!status)
773dd225cb3SAnna Schumaker 				nfs_setsecurity(d_inode(dentry), entry->fattr);
774eace45a1STrond Myklebust 			trace_nfs_readdir_lookup_revalidate(d_inode(parent),
775eace45a1STrond Myklebust 							    dentry, 0, status);
776d39ab9deSBryan Schumaker 			goto out;
777d39ab9deSBryan Schumaker 		} else {
778eace45a1STrond Myklebust 			trace_nfs_readdir_lookup_revalidate_failed(
779eace45a1STrond Myklebust 				d_inode(parent), dentry, 0);
7805542aa2fSEric W. Biederman 			d_invalidate(dentry);
781d39ab9deSBryan Schumaker 			dput(dentry);
7829ac3d3e8SAl Viro 			dentry = NULL;
7839ac3d3e8SAl Viro 			goto again;
784d39ab9deSBryan Schumaker 		}
785d39ab9deSBryan Schumaker 	}
7867dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
7877dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
7887dc72d5fSTrond Myklebust 		goto out;
7897dc72d5fSTrond Myklebust 	}
790d39ab9deSBryan Schumaker 
791cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
79241d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
7939ac3d3e8SAl Viro 	d_lookup_done(dentry);
7949ac3d3e8SAl Viro 	if (alias) {
795d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
796d39ab9deSBryan Schumaker 			goto out;
7979ac3d3e8SAl Viro 		dput(dentry);
7989ac3d3e8SAl Viro 		dentry = alias;
7999ac3d3e8SAl Viro 	}
800a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
801eace45a1STrond Myklebust 	trace_nfs_readdir_lookup(d_inode(parent), dentry, 0);
802d39ab9deSBryan Schumaker out:
803d39ab9deSBryan Schumaker 	dput(dentry);
804d39ab9deSBryan Schumaker }
805d39ab9deSBryan Schumaker 
806612896ecSTrond Myklebust static int nfs_readdir_entry_decode(struct nfs_readdir_descriptor *desc,
807612896ecSTrond Myklebust 				    struct nfs_entry *entry,
808612896ecSTrond Myklebust 				    struct xdr_stream *stream)
809612896ecSTrond Myklebust {
810612896ecSTrond Myklebust 	int ret;
811612896ecSTrond Myklebust 
812612896ecSTrond Myklebust 	if (entry->fattr->label)
813612896ecSTrond Myklebust 		entry->fattr->label->len = NFS4_MAXLABELLEN;
814612896ecSTrond Myklebust 	ret = xdr_decode(desc, entry, stream);
815612896ecSTrond Myklebust 	if (ret || !desc->plus)
816612896ecSTrond Myklebust 		return ret;
817612896ecSTrond Myklebust 	nfs_prime_dcache(file_dentry(desc->file), entry, desc->dir_verifier);
818612896ecSTrond Myklebust 	return 0;
819612896ecSTrond Myklebust }
820612896ecSTrond Myklebust 
821d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
8223b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
8233b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
824f648022fSTrond Myklebust 				   struct page **xdr_pages, unsigned int buflen,
825f648022fSTrond Myklebust 				   struct page **arrays, size_t narrays,
826f648022fSTrond Myklebust 				   u64 change_attr)
827d1bacf9eSBryan Schumaker {
8283b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
829babddc72SBryan Schumaker 	struct xdr_stream stream;
830f7da7a12SBenny Halevy 	struct xdr_buf buf;
83135df59d3STrond Myklebust 	struct page *scratch, *new, *page = *arrays;
8320adf85b4STrond Myklebust 	u64 cookie;
8335c346854STrond Myklebust 	int status;
834babddc72SBryan Schumaker 
8356650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
8366650239aSTrond Myklebust 	if (scratch == NULL)
8376650239aSTrond Myklebust 		return -ENOMEM;
838babddc72SBryan Schumaker 
839f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
8400ae4c3e8SChuck Lever 	xdr_set_scratch_page(&stream, scratch);
84199424380SBryan Schumaker 
84299424380SBryan Schumaker 	do {
843612896ecSTrond Myklebust 		status = nfs_readdir_entry_decode(desc, entry, &stream);
844972bcdf2STrond Myklebust 		if (status != 0)
84599424380SBryan Schumaker 			break;
8465c346854STrond Myklebust 
8470adf85b4STrond Myklebust 		status = nfs_readdir_page_array_append(page, entry, &cookie);
8483b2a09f1STrond Myklebust 		if (status != -ENOSPC)
8493b2a09f1STrond Myklebust 			continue;
85099424380SBryan Schumaker 
85135df59d3STrond Myklebust 		if (page->mapping != mapping) {
85235df59d3STrond Myklebust 			if (!--narrays)
8533b2a09f1STrond Myklebust 				break;
8540adf85b4STrond Myklebust 			new = nfs_readdir_page_array_alloc(cookie, GFP_KERNEL);
85535df59d3STrond Myklebust 			if (!new)
85635df59d3STrond Myklebust 				break;
85735df59d3STrond Myklebust 			arrays++;
85835df59d3STrond Myklebust 			*arrays = page = new;
85935df59d3STrond Myklebust 		} else {
8600adf85b4STrond Myklebust 			new = nfs_readdir_page_get_next(mapping, cookie,
8610adf85b4STrond Myklebust 							change_attr);
8623b2a09f1STrond Myklebust 			if (!new)
8633b2a09f1STrond Myklebust 				break;
86435df59d3STrond Myklebust 			if (page != *arrays)
8653b2a09f1STrond Myklebust 				nfs_readdir_page_unlock_and_put(page);
8663b2a09f1STrond Myklebust 			page = new;
86756e4ebf8SBryan Schumaker 		}
868f648022fSTrond Myklebust 		desc->page_index_max++;
8690adf85b4STrond Myklebust 		status = nfs_readdir_page_array_append(page, entry, &cookie);
870972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
87156e4ebf8SBryan Schumaker 
872972bcdf2STrond Myklebust 	switch (status) {
873972bcdf2STrond Myklebust 	case -EBADCOOKIE:
874612896ecSTrond Myklebust 		if (!entry->eof)
875972bcdf2STrond Myklebust 			break;
876612896ecSTrond Myklebust 		nfs_readdir_page_set_eof(page);
877612896ecSTrond Myklebust 		fallthrough;
878972bcdf2STrond Myklebust 	case -EAGAIN:
879972bcdf2STrond Myklebust 		status = 0;
880972bcdf2STrond Myklebust 		break;
881612896ecSTrond Myklebust 	case -ENOSPC:
882612896ecSTrond Myklebust 		status = 0;
883612896ecSTrond Myklebust 		if (!desc->plus)
884612896ecSTrond Myklebust 			break;
885612896ecSTrond Myklebust 		while (!nfs_readdir_entry_decode(desc, entry, &stream))
886612896ecSTrond Myklebust 			;
887972bcdf2STrond Myklebust 	}
8886650239aSTrond Myklebust 
88935df59d3STrond Myklebust 	if (page != *arrays)
8903b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
8916650239aSTrond Myklebust 
8926650239aSTrond Myklebust 	put_page(scratch);
8938cd51a0cSTrond Myklebust 	return status;
8948cd51a0cSTrond Myklebust }
89556e4ebf8SBryan Schumaker 
8961a34c8c9STrond Myklebust static void nfs_readdir_free_pages(struct page **pages, size_t npages)
89756e4ebf8SBryan Schumaker {
8981a34c8c9STrond Myklebust 	while (npages--)
8991a34c8c9STrond Myklebust 		put_page(pages[npages]);
9001a34c8c9STrond Myklebust 	kfree(pages);
90156e4ebf8SBryan Schumaker }
90256e4ebf8SBryan Schumaker 
90356e4ebf8SBryan Schumaker /*
904bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
905bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
90656e4ebf8SBryan Schumaker  */
9071a34c8c9STrond Myklebust static struct page **nfs_readdir_alloc_pages(size_t npages)
90856e4ebf8SBryan Schumaker {
9091a34c8c9STrond Myklebust 	struct page **pages;
9101a34c8c9STrond Myklebust 	size_t i;
91156e4ebf8SBryan Schumaker 
9121a34c8c9STrond Myklebust 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
9131a34c8c9STrond Myklebust 	if (!pages)
9141a34c8c9STrond Myklebust 		return NULL;
91556e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
91656e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
91756e4ebf8SBryan Schumaker 		if (page == NULL)
91856e4ebf8SBryan Schumaker 			goto out_freepages;
91956e4ebf8SBryan Schumaker 		pages[i] = page;
92056e4ebf8SBryan Schumaker 	}
9211a34c8c9STrond Myklebust 	return pages;
92256e4ebf8SBryan Schumaker 
92356e4ebf8SBryan Schumaker out_freepages:
924c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
9251a34c8c9STrond Myklebust 	return NULL;
926d1bacf9eSBryan Schumaker }
927d1bacf9eSBryan Schumaker 
9286c981effSTrond Myklebust static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
92935df59d3STrond Myklebust 				    __be32 *verf_arg, __be32 *verf_res,
93035df59d3STrond Myklebust 				    struct page **arrays, size_t narrays)
931d1bacf9eSBryan Schumaker {
932f648022fSTrond Myklebust 	u64 change_attr;
9331a34c8c9STrond Myklebust 	struct page **pages;
93435df59d3STrond Myklebust 	struct page *page = *arrays;
9356b75cf9eSTrond Myklebust 	struct nfs_entry *entry;
9361a34c8c9STrond Myklebust 	size_t array_size;
937b593c09fSTrond Myklebust 	struct inode *inode = file_inode(desc->file);
938580f2367STrond Myklebust 	unsigned int dtsize = desc->dtsize;
9399ff89c25STrond Myklebust 	unsigned int pglen;
9408cd51a0cSTrond Myklebust 	int status = -ENOMEM;
941d1bacf9eSBryan Schumaker 
9426b75cf9eSTrond Myklebust 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
9436b75cf9eSTrond Myklebust 	if (!entry)
9446b75cf9eSTrond Myklebust 		return -ENOMEM;
9456b75cf9eSTrond Myklebust 	entry->cookie = nfs_readdir_page_last_cookie(page);
9466b75cf9eSTrond Myklebust 	entry->fh = nfs_alloc_fhandle();
947b1db9a40SAnna Schumaker 	entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
9486b75cf9eSTrond Myklebust 	entry->server = NFS_SERVER(inode);
9496b75cf9eSTrond Myklebust 	if (entry->fh == NULL || entry->fattr == NULL)
950d1bacf9eSBryan Schumaker 		goto out;
951d1bacf9eSBryan Schumaker 
9521a34c8c9STrond Myklebust 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
9531a34c8c9STrond Myklebust 	pages = nfs_readdir_alloc_pages(array_size);
9541a34c8c9STrond Myklebust 	if (!pages)
955b1db9a40SAnna Schumaker 		goto out;
956d1bacf9eSBryan Schumaker 
957f648022fSTrond Myklebust 	change_attr = inode_peek_iversion_raw(inode);
9589ff89c25STrond Myklebust 	status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
9599ff89c25STrond Myklebust 					dtsize, verf_res);
960d1bacf9eSBryan Schumaker 	if (status < 0)
9619ff89c25STrond Myklebust 		goto free_pages;
962972bcdf2STrond Myklebust 
963ac396128STrond Myklebust 	pglen = status;
9649ff89c25STrond Myklebust 	if (pglen != 0)
96535df59d3STrond Myklebust 		status = nfs_readdir_page_filler(desc, entry, pages, pglen,
966f648022fSTrond Myklebust 						 arrays, narrays, change_attr);
9679ff89c25STrond Myklebust 	else
9689ff89c25STrond Myklebust 		nfs_readdir_page_set_eof(page);
969580f2367STrond Myklebust 	desc->buffer_fills++;
970d1bacf9eSBryan Schumaker 
9719ff89c25STrond Myklebust free_pages:
972c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
973d1bacf9eSBryan Schumaker out:
9746b75cf9eSTrond Myklebust 	nfs_free_fattr(entry->fattr);
9756b75cf9eSTrond Myklebust 	nfs_free_fhandle(entry->fh);
9766b75cf9eSTrond Myklebust 	kfree(entry);
977d1bacf9eSBryan Schumaker 	return status;
978d1bacf9eSBryan Schumaker }
979d1bacf9eSBryan Schumaker 
9801f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
9811da177e4SLinus Torvalds {
98209cbfeafSKirill A. Shutemov 	put_page(desc->page);
9831da177e4SLinus Torvalds 	desc->page = NULL;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861f1d4aa4STrond Myklebust static void
9871f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
9881da177e4SLinus Torvalds {
9891f1d4aa4STrond Myklebust 	unlock_page(desc->page);
9901f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
9911f1d4aa4STrond Myklebust }
9921f1d4aa4STrond Myklebust 
9931f1d4aa4STrond Myklebust static struct page *
9941f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
9951f1d4aa4STrond Myklebust {
996f648022fSTrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
997f648022fSTrond Myklebust 	u64 change_attr = inode_peek_iversion_raw(mapping->host);
998b0365ccbSTrond Myklebust 	u64 cookie = desc->last_cookie;
999b0365ccbSTrond Myklebust 	struct page *page;
1000f648022fSTrond Myklebust 
1001b0365ccbSTrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, cookie, change_attr);
1002b0365ccbSTrond Myklebust 	if (!page)
1003b0365ccbSTrond Myklebust 		return NULL;
1004b0365ccbSTrond Myklebust 	if (desc->clear_cache && !nfs_readdir_page_needs_filling(page))
1005b0365ccbSTrond Myklebust 		nfs_readdir_page_reinit_array(page, cookie, change_attr);
1006b0365ccbSTrond Myklebust 	return page;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds /*
1010d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
1011114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
10121da177e4SLinus Torvalds  */
10136c981effSTrond Myklebust static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
1014d1bacf9eSBryan Schumaker {
1015227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
1016227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
1017b593c09fSTrond Myklebust 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
1018d1bacf9eSBryan Schumaker 	int res;
1019d1bacf9eSBryan Schumaker 
10201f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
10211f1d4aa4STrond Myklebust 	if (!desc->page)
10221f1d4aa4STrond Myklebust 		return -ENOMEM;
10231f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
1024580f2367STrond Myklebust 		/* Grow the dtsize if we had to go back for more pages */
1025580f2367STrond Myklebust 		if (desc->page_index == desc->page_index_max)
1026580f2367STrond Myklebust 			nfs_grow_dtsize(desc);
1027580f2367STrond Myklebust 		desc->page_index_max = desc->page_index;
1028310e3187STrond Myklebust 		trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf,
1029310e3187STrond Myklebust 					     desc->last_cookie,
1030310e3187STrond Myklebust 					     desc->page->index, desc->dtsize);
103135df59d3STrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
103235df59d3STrond Myklebust 					       &desc->page, 1);
10339fff59edSTrond Myklebust 		if (res < 0) {
10349fff59edSTrond Myklebust 			nfs_readdir_page_unlock_and_put_cached(desc);
1035310e3187STrond Myklebust 			trace_nfs_readdir_cache_fill_done(inode, res);
10369fff59edSTrond Myklebust 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
10379fff59edSTrond Myklebust 				invalidate_inode_pages2(desc->file->f_mapping);
1038f648022fSTrond Myklebust 				nfs_readdir_rewind_search(desc);
103911d03d0aSTrond Myklebust 				trace_nfs_readdir_invalidate_cache_range(
104011d03d0aSTrond Myklebust 					inode, 0, MAX_LFS_FILESIZE);
10419fff59edSTrond Myklebust 				return -EAGAIN;
10429fff59edSTrond Myklebust 			}
10439fff59edSTrond Myklebust 			return res;
10449fff59edSTrond Myklebust 		}
1045f892c41cSTrond Myklebust 		/*
1046f892c41cSTrond Myklebust 		 * Set the cookie verifier if the page cache was empty
1047f892c41cSTrond Myklebust 		 */
10486c34f05bSTrond Myklebust 		if (desc->last_cookie == 0 &&
10496c34f05bSTrond Myklebust 		    memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
1050f892c41cSTrond Myklebust 			memcpy(nfsi->cookieverf, verf,
1051f892c41cSTrond Myklebust 			       sizeof(nfsi->cookieverf));
1052f648022fSTrond Myklebust 			invalidate_inode_pages2_range(desc->file->f_mapping, 1,
10536c34f05bSTrond Myklebust 						      -1);
105411d03d0aSTrond Myklebust 			trace_nfs_readdir_invalidate_cache_range(
1055f648022fSTrond Myklebust 				inode, 1, MAX_LFS_FILESIZE);
10566c34f05bSTrond Myklebust 		}
1057b0365ccbSTrond Myklebust 		desc->clear_cache = false;
10581f1d4aa4STrond Myklebust 	}
1059114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
1060ff81dfb5STrond Myklebust 	if (res == 0)
1061114de382STrond Myklebust 		return 0;
10621f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
1063d1bacf9eSBryan Schumaker 	return res;
1064d1bacf9eSBryan Schumaker }
1065d1bacf9eSBryan Schumaker 
1066d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
10676c981effSTrond Myklebust static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
10681da177e4SLinus Torvalds {
10698cd51a0cSTrond Myklebust 	int res;
1070d1bacf9eSBryan Schumaker 
10719fff59edSTrond Myklebust 	do {
1072114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
107347c716cbSTrond Myklebust 	} while (res == -EAGAIN);
10741da177e4SLinus Torvalds 	return res;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds /*
10781da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
10791da177e4SLinus Torvalds  */
108013884ff2STrond Myklebust static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
108113884ff2STrond Myklebust 			   const __be32 *verf)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	struct file	*file = desc->file;
1084dbeaf8c9STrond Myklebust 	struct nfs_cache_array *array;
1085c8f0523bSTrond Myklebust 	unsigned int i;
10868ef2ce3eSBryan Schumaker 
1087c77c738cSFabio M. De Francesco 	array = kmap_local_page(desc->page);
1088d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
1089ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
10901da177e4SLinus Torvalds 
1091ece0b423STrond Myklebust 		ent = &array->array[i];
1092a52a8a6aSTrond Myklebust 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
109323db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1094e1d2699bSTrond Myklebust 			desc->eob = true;
10951da177e4SLinus Torvalds 			break;
1096ece0b423STrond Myklebust 		}
109713884ff2STrond Myklebust 		memcpy(desc->verf, verf, sizeof(desc->verf));
1098c8f0523bSTrond Myklebust 		if (i == array->size - 1) {
10992e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
1100c8f0523bSTrond Myklebust 			nfs_readdir_seek_next_array(array, desc);
1101c8f0523bSTrond Myklebust 		} else {
1102c8f0523bSTrond Myklebust 			desc->dir_cookie = array->array[i + 1].cookie;
1103c8f0523bSTrond Myklebust 			desc->last_cookie = array->array[0].cookie;
1104c8f0523bSTrond Myklebust 		}
110559e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
11062e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
110759e356a9STrond Myklebust 		else
110859e356a9STrond Myklebust 			desc->ctx->pos++;
11098cd51a0cSTrond Myklebust 	}
1110b1e21c97STrond Myklebust 	if (array->page_is_eof)
1111e1d2699bSTrond Myklebust 		desc->eof = !desc->eob;
1112d1bacf9eSBryan Schumaker 
1113c77c738cSFabio M. De Francesco 	kunmap_local(array);
1114dbeaf8c9STrond Myklebust 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1115dbeaf8c9STrond Myklebust 			(unsigned long long)desc->dir_cookie);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds /*
11191da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
11201da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
11211da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
11221da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
11231da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
11241da177e4SLinus Torvalds  *
11251da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
11261da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
11271da177e4SLinus Torvalds  *	 we should already have a complete representation of the
11281da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
11291da177e4SLinus Torvalds  */
11306c981effSTrond Myklebust static int uncached_readdir(struct nfs_readdir_descriptor *desc)
11311da177e4SLinus Torvalds {
113235df59d3STrond Myklebust 	struct page	**arrays;
113335df59d3STrond Myklebust 	size_t		i, sz = 512;
1134b593c09fSTrond Myklebust 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
113535df59d3STrond Myklebust 	int		status = -ENOMEM;
11361da177e4SLinus Torvalds 
113735df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
11382e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
11391da177e4SLinus Torvalds 
114035df59d3STrond Myklebust 	arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
114135df59d3STrond Myklebust 	if (!arrays)
11421da177e4SLinus Torvalds 		goto out;
114335df59d3STrond Myklebust 	arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
114435df59d3STrond Myklebust 	if (!arrays[0])
114535df59d3STrond Myklebust 		goto out;
11461da177e4SLinus Torvalds 
11477a8e1dc3STrond Myklebust 	desc->page_index = 0;
1148ce292d8fStrondmy@kernel.org 	desc->cache_entry_index = 0;
11492e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
1150580f2367STrond Myklebust 	desc->page_index_max = 0;
11517a8e1dc3STrond Myklebust 
1152310e3187STrond Myklebust 	trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie,
1153310e3187STrond Myklebust 				   -1, desc->dtsize);
1154310e3187STrond Myklebust 
115535df59d3STrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1156310e3187STrond Myklebust 	if (status < 0) {
1157310e3187STrond Myklebust 		trace_nfs_readdir_uncached_done(file_inode(desc->file), status);
1158310e3187STrond Myklebust 		goto out_free;
1159310e3187STrond Myklebust 	}
1160d1bacf9eSBryan Schumaker 
1161e1d2699bSTrond Myklebust 	for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
116235df59d3STrond Myklebust 		desc->page = arrays[i];
116313884ff2STrond Myklebust 		nfs_do_filldir(desc, verf);
116435df59d3STrond Myklebust 	}
116535df59d3STrond Myklebust 	desc->page = NULL;
11661da177e4SLinus Torvalds 
1167580f2367STrond Myklebust 	/*
1168580f2367STrond Myklebust 	 * Grow the dtsize if we have to go back for more pages,
1169580f2367STrond Myklebust 	 * or shrink it if we're reading too many.
1170580f2367STrond Myklebust 	 */
1171580f2367STrond Myklebust 	if (!desc->eof) {
1172580f2367STrond Myklebust 		if (!desc->eob)
1173580f2367STrond Myklebust 			nfs_grow_dtsize(desc);
1174580f2367STrond Myklebust 		else if (desc->buffer_fills == 1 &&
1175580f2367STrond Myklebust 			 i < (desc->page_index_max >> 1))
1176580f2367STrond Myklebust 			nfs_shrink_dtsize(desc);
1177580f2367STrond Myklebust 	}
1178310e3187STrond Myklebust out_free:
117935df59d3STrond Myklebust 	for (i = 0; i < sz && arrays[i]; i++)
118035df59d3STrond Myklebust 		nfs_readdir_page_array_free(arrays[i]);
11811da177e4SLinus Torvalds out:
1182f648022fSTrond Myklebust 	if (!nfs_readdir_use_cookie(desc->file))
1183f648022fSTrond Myklebust 		nfs_readdir_rewind_search(desc);
1184580f2367STrond Myklebust 	desc->page_index_max = -1;
118535df59d3STrond Myklebust 	kfree(arrays);
118635df59d3STrond Myklebust 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
11871da177e4SLinus Torvalds 	return status;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
1190230bc98fSTrond Myklebust #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
1191230bc98fSTrond Myklebust 
1192b0365ccbSTrond Myklebust static bool nfs_readdir_handle_cache_misses(struct inode *inode,
1193230bc98fSTrond Myklebust 					    struct nfs_readdir_descriptor *desc,
1194b0365ccbSTrond Myklebust 					    unsigned int cache_misses,
1195b0365ccbSTrond Myklebust 					    bool force_clear)
1196230bc98fSTrond Myklebust {
1197b0365ccbSTrond Myklebust 	if (desc->ctx->pos == 0 || !desc->plus)
1198b0365ccbSTrond Myklebust 		return false;
1199b0365ccbSTrond Myklebust 	if (cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD && !force_clear)
1200b0365ccbSTrond Myklebust 		return false;
1201b0365ccbSTrond Myklebust 	trace_nfs_readdir_force_readdirplus(inode);
1202b0365ccbSTrond Myklebust 	return true;
1203230bc98fSTrond Myklebust }
1204230bc98fSTrond Myklebust 
120500a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
120600a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
120700a92642SOlivier Galibert    whole directory.
12081da177e4SLinus Torvalds  */
120923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
12101da177e4SLinus Torvalds {
1211be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
12122b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
121313884ff2STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
121423db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
12156b75cf9eSTrond Myklebust 	struct nfs_readdir_descriptor *desc;
1216230bc98fSTrond Myklebust 	unsigned int cache_hits, cache_misses;
1217b0365ccbSTrond Myklebust 	bool force_clear;
12186b75cf9eSTrond Myklebust 	int res;
12191da177e4SLinus Torvalds 
12206de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
12216de1472fSAl Viro 			file, (long long)ctx->pos);
122291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
122391d5b470SChuck Lever 
12241da177e4SLinus Torvalds 	/*
122523db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
1226f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
122700a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
122800a92642SOlivier Galibert 	 * revalidate the cookie.
12291da177e4SLinus Torvalds 	 */
1230d09e673fSTrond Myklebust 	nfs_revalidate_mapping(inode, file->f_mapping);
12316b75cf9eSTrond Myklebust 
12326b75cf9eSTrond Myklebust 	res = -ENOMEM;
12336b75cf9eSTrond Myklebust 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
12346b75cf9eSTrond Myklebust 	if (!desc)
12356b75cf9eSTrond Myklebust 		goto out;
12366b75cf9eSTrond Myklebust 	desc->file = file;
12376b75cf9eSTrond Myklebust 	desc->ctx = ctx;
1238580f2367STrond Myklebust 	desc->page_index_max = -1;
1239fccca7fcSTrond Myklebust 
12402e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
12412e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
1242f648022fSTrond Myklebust 	desc->page_index = dir_ctx->page_index;
1243728dd0abSTrond Myklebust 	desc->last_cookie = dir_ctx->last_cookie;
12442e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
1245e1d2699bSTrond Myklebust 	desc->eof = dir_ctx->eof;
1246580f2367STrond Myklebust 	nfs_set_dtsize(desc, dir_ctx->dtsize);
1247b593c09fSTrond Myklebust 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
1248230bc98fSTrond Myklebust 	cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0);
1249230bc98fSTrond Myklebust 	cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0);
1250b0365ccbSTrond Myklebust 	force_clear = dir_ctx->force_clear;
12512e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1252565277f6STrond Myklebust 
1253e1d2699bSTrond Myklebust 	if (desc->eof) {
1254e1d2699bSTrond Myklebust 		res = 0;
1255e1d2699bSTrond Myklebust 		goto out_free;
1256e1d2699bSTrond Myklebust 	}
1257e1d2699bSTrond Myklebust 
1258230bc98fSTrond Myklebust 	desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses);
1259b0365ccbSTrond Myklebust 	force_clear = nfs_readdir_handle_cache_misses(inode, desc, cache_misses,
1260b0365ccbSTrond Myklebust 						      force_clear);
1261b0365ccbSTrond Myklebust 	desc->clear_cache = force_clear;
1262ff81dfb5STrond Myklebust 
126347c716cbSTrond Myklebust 	do {
12641da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
126500a92642SOlivier Galibert 
12661da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1267ece0b423STrond Myklebust 			res = 0;
12681da177e4SLinus Torvalds 			/* This means either end of directory */
12692e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
12701da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
127123db8620SAl Viro 				res = uncached_readdir(desc);
1272ece0b423STrond Myklebust 				if (res == 0)
12731da177e4SLinus Torvalds 					continue;
12749fff59edSTrond Myklebust 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
12759fff59edSTrond Myklebust 					res = 0;
12761da177e4SLinus Torvalds 			}
12771da177e4SLinus Torvalds 			break;
12781da177e4SLinus Torvalds 		}
12791da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
12801da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1281a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1282a7a3b1e9SBenjamin Coddington 			desc->eof = false;
12831da177e4SLinus Torvalds 			continue;
12841da177e4SLinus Torvalds 		}
12851da177e4SLinus Torvalds 		if (res < 0)
12861da177e4SLinus Torvalds 			break;
12871da177e4SLinus Torvalds 
128813884ff2STrond Myklebust 		nfs_do_filldir(desc, nfsi->cookieverf);
12891f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
1290b0365ccbSTrond Myklebust 		if (desc->page_index == desc->page_index_max)
1291b0365ccbSTrond Myklebust 			desc->clear_cache = force_clear;
1292e1d2699bSTrond Myklebust 	} while (!desc->eob && !desc->eof);
12932e7a4641STrond Myklebust 
12942e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
12952e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
1296728dd0abSTrond Myklebust 	dir_ctx->last_cookie = desc->last_cookie;
12972e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
1298ff81dfb5STrond Myklebust 	dir_ctx->page_index = desc->page_index;
1299b0365ccbSTrond Myklebust 	dir_ctx->force_clear = force_clear;
1300e1d2699bSTrond Myklebust 	dir_ctx->eof = desc->eof;
1301580f2367STrond Myklebust 	dir_ctx->dtsize = desc->dtsize;
1302b593c09fSTrond Myklebust 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
13032e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
1304e1d2699bSTrond Myklebust out_free:
13056b75cf9eSTrond Myklebust 	kfree(desc);
13066b75cf9eSTrond Myklebust 
1307fccca7fcSTrond Myklebust out:
13086de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
13091da177e4SLinus Torvalds 	return res;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
1312965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1313f0dd2136STrond Myklebust {
1314480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1315b84e06c5SChuck Lever 
13166de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
13176de1472fSAl Viro 			filp, offset, whence);
1318b84e06c5SChuck Lever 
1319965c8e59SAndrew Morton 	switch (whence) {
1320f0dd2136STrond Myklebust 	default:
1321b2b1ff3dSTrond Myklebust 		return -EINVAL;
1322b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1323b2b1ff3dSTrond Myklebust 		if (offset < 0)
1324b2b1ff3dSTrond Myklebust 			return -EINVAL;
132583f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1326b2b1ff3dSTrond Myklebust 		break;
1327b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1328b2b1ff3dSTrond Myklebust 		if (offset == 0)
1329b2b1ff3dSTrond Myklebust 			return filp->f_pos;
133083f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1331b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1332b2b1ff3dSTrond Myklebust 		if (offset < 0) {
133383f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1334b2b1ff3dSTrond Myklebust 			return -EINVAL;
1335b2b1ff3dSTrond Myklebust 		}
1336f0dd2136STrond Myklebust 	}
1337f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1338f0dd2136STrond Myklebust 		filp->f_pos = offset;
1339728dd0abSTrond Myklebust 		dir_ctx->page_index = 0;
1340f648022fSTrond Myklebust 		if (!nfs_readdir_use_cookie(filp)) {
13419c3f4d98STrond Myklebust 			dir_ctx->dir_cookie = 0;
1342f648022fSTrond Myklebust 			dir_ctx->last_cookie = 0;
1343f648022fSTrond Myklebust 		} else {
1344728dd0abSTrond Myklebust 			dir_ctx->dir_cookie = offset;
1345f648022fSTrond Myklebust 			dir_ctx->last_cookie = offset;
1346f648022fSTrond Myklebust 		}
1347e1d2699bSTrond Myklebust 		dir_ctx->eof = false;
1348f0dd2136STrond Myklebust 	}
134983f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1350f0dd2136STrond Myklebust 	return offset;
1351f0dd2136STrond Myklebust }
1352f0dd2136STrond Myklebust 
13531da177e4SLinus Torvalds /*
13541da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
13551da177e4SLinus Torvalds  * is a dummy operation.
13561da177e4SLinus Torvalds  */
135702c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
135802c24a82SJosef Bacik 			 int datasync)
13591da177e4SLinus Torvalds {
13606de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
13611e7cb3dcSChuck Lever 
136211decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
13631da177e4SLinus Torvalds 	return 0;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
1366bfc69a45STrond Myklebust /**
1367bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1368302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1369bfc69a45STrond Myklebust  *
1370bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1371bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1372bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1373bfc69a45STrond Myklebust  *
1374efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1375efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1376efeda80dSTrond Myklebust  *
1377bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1378bfc69a45STrond Myklebust  */
1379bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1380bfc69a45STrond Myklebust {
1381efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1382bfc69a45STrond Myklebust }
138389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1384bfc69a45STrond Myklebust 
1385efeda80dSTrond Myklebust /**
1386efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1387efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1388efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1389efeda80dSTrond Myklebust  *
1390efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1391efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1392efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1393efeda80dSTrond Myklebust  */
1394efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1395efeda80dSTrond Myklebust {
1396efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1397efeda80dSTrond Myklebust }
1398efeda80dSTrond Myklebust 
1399efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1400efeda80dSTrond Myklebust {
1401efeda80dSTrond Myklebust 	*verf |= 1UL;
1402efeda80dSTrond Myklebust }
1403efeda80dSTrond Myklebust 
1404efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1405efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1406efeda80dSTrond Myklebust {
1407efeda80dSTrond Myklebust 	*verf &= ~1UL;
1408efeda80dSTrond Myklebust }
1409efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1410efeda80dSTrond Myklebust 
1411efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1412efeda80dSTrond Myklebust {
1413efeda80dSTrond Myklebust 	return verf & 1;
1414efeda80dSTrond Myklebust }
1415efeda80dSTrond Myklebust 
1416efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1417efeda80dSTrond Myklebust {
1418efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1419efeda80dSTrond Myklebust }
1420efeda80dSTrond Myklebust 
1421efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1422efeda80dSTrond Myklebust {
1423efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1424cec08f45STrond Myklebust 	struct inode *dir = d_inode(dentry->d_parent);
1425efeda80dSTrond Myklebust 
1426cec08f45STrond Myklebust 	if (!nfs_verify_change_attribute(dir, verf))
1427cec08f45STrond Myklebust 		return;
1428efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1429efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1430efeda80dSTrond Myklebust 	dentry->d_time = verf;
1431efeda80dSTrond Myklebust }
1432efeda80dSTrond Myklebust 
1433efeda80dSTrond Myklebust /**
1434efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1435efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1436efeda80dSTrond Myklebust  * @verf: verifier to save
1437efeda80dSTrond Myklebust  *
1438efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1439efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1440efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1441efeda80dSTrond Myklebust  * look it up again after a directory change.
1442efeda80dSTrond Myklebust  */
1443efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1444efeda80dSTrond Myklebust {
1445efeda80dSTrond Myklebust 
1446efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1447efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1448efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1449efeda80dSTrond Myklebust }
1450efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1451efeda80dSTrond Myklebust 
1452efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1453efeda80dSTrond Myklebust /**
1454efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1455efeda80dSTrond Myklebust  * @inode: pointer to inode
1456efeda80dSTrond Myklebust  *
1457efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1458efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1459efeda80dSTrond Myklebust  * while holding a delegation.
1460efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1461efeda80dSTrond Myklebust  * returned or revoked.
1462efeda80dSTrond Myklebust  */
1463efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1464efeda80dSTrond Myklebust {
1465efeda80dSTrond Myklebust 	struct dentry *alias;
1466efeda80dSTrond Myklebust 
1467efeda80dSTrond Myklebust 	if (!inode)
1468efeda80dSTrond Myklebust 		return;
1469efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1470efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1471efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1472efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1473efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1474efeda80dSTrond Myklebust 	}
1475efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1476efeda80dSTrond Myklebust }
1477efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1478efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1479efeda80dSTrond Myklebust 
14808ce37abdSTrond Myklebust static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
14818ce37abdSTrond Myklebust {
14828ce37abdSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
14838ce37abdSTrond Myklebust 	    d_really_is_negative(dentry))
14848ce37abdSTrond Myklebust 		return dentry->d_time == inode_peek_iversion_raw(dir);
14858ce37abdSTrond Myklebust 	return nfs_verify_change_attribute(dir, dentry->d_time);
14868ce37abdSTrond Myklebust }
14878ce37abdSTrond Myklebust 
14881da177e4SLinus Torvalds /*
14891da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
14901da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
14911da177e4SLinus Torvalds  * and may need to be looked up again.
1492912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
14931da177e4SLinus Torvalds  */
1494912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1495912a108dSNeilBrown 			      int rcu_walk)
14961da177e4SLinus Torvalds {
14971da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
14981da177e4SLinus Torvalds 		return 1;
14994eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
15004eec952eSTrond Myklebust 		return 0;
15018ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
15026ecc5e8fSTrond Myklebust 		return 0;
1503f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
15041cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1505912a108dSNeilBrown 		if (rcu_walk)
1506f2c77f4eSTrond Myklebust 			return 0;
15071cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
15081cd9cb05STrond Myklebust 			return 0;
15091cd9cb05STrond Myklebust 	}
15108ce37abdSTrond Myklebust 	if (!nfs_dentry_verify_change(dir, dentry))
1511f2c77f4eSTrond Myklebust 		return 0;
1512f2c77f4eSTrond Myklebust 	return 1;
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds /*
1516a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1517a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1518a12802caSTrond Myklebust  */
1519fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1520a12802caSTrond Myklebust {
1521a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1522a12802caSTrond Myklebust 		return 0;
1523fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1524a12802caSTrond Myklebust }
1525a12802caSTrond Myklebust 
1526a12802caSTrond Myklebust /*
15271d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
15281d6757fbSTrond Myklebust  *
15291d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
15301d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
15311d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
15321d6757fbSTrond Myklebust  *
15331d6757fbSTrond Myklebust  */
153465a0c149STrond Myklebust static
1535fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
153865a0c149STrond Myklebust 	int ret;
15391da177e4SLinus Torvalds 
154036d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
15414e99a1ffSTrond Myklebust 		return 0;
154247921921STrond Myklebust 
154347921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
154447921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
154547921921STrond Myklebust 		case S_IFREG:
154647921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
154747921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
154847921921STrond Myklebust 				goto out;
1549df561f66SGustavo A. R. Silva 			fallthrough;
155047921921STrond Myklebust 		case S_IFDIR:
155147921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
155247921921STrond Myklebust 				break;
155347921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
155447921921STrond Myklebust 			goto out_force;
155547921921STrond Myklebust 		}
155647921921STrond Myklebust 	}
155747921921STrond Myklebust 
15581da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1559fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
15601da177e4SLinus Torvalds 		goto out_force;
156165a0c149STrond Myklebust out:
156243245ecaSOlga Kornievskaia 	if (inode->i_nlink > 0 ||
156343245ecaSOlga Kornievskaia 	    (inode->i_nlink == 0 &&
156443245ecaSOlga Kornievskaia 	     test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
156543245ecaSOlga Kornievskaia 		return 0;
156643245ecaSOlga Kornievskaia 	else
156743245ecaSOlga Kornievskaia 		return -ESTALE;
15681da177e4SLinus Torvalds out_force:
15691fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
15701fa1e384SNeilBrown 		return -ECHILD;
157165a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
157265a0c149STrond Myklebust 	if (ret != 0)
157365a0c149STrond Myklebust 		return ret;
157465a0c149STrond Myklebust 	goto out;
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
157782e7ca13STrond Myklebust static void nfs_mark_dir_for_revalidate(struct inode *inode)
157882e7ca13STrond Myklebust {
157982e7ca13STrond Myklebust 	spin_lock(&inode->i_lock);
1580a6a361c4STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
158182e7ca13STrond Myklebust 	spin_unlock(&inode->i_lock);
158282e7ca13STrond Myklebust }
158382e7ca13STrond Myklebust 
15841da177e4SLinus Torvalds /*
15851da177e4SLinus Torvalds  * We judge how long we want to trust negative
15861da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
15871da177e4SLinus Torvalds  *
15881da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
15891da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1590912a108dSNeilBrown  *
1591912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1592912a108dSNeilBrown  * suggesting a reval is needed.
15939f6d44d4STrond Myklebust  *
15949f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
15959f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
15961da177e4SLinus Torvalds  */
15971da177e4SLinus Torvalds static inline
15981da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1599fa3c56bbSAl Viro 		       unsigned int flags)
16001da177e4SLinus Torvalds {
16019f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
16021da177e4SLinus Torvalds 		return 0;
16034eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
16044eec952eSTrond Myklebust 		return 1;
160598ca3ee6STrond Myklebust 	/* Case insensitive server? Revalidate negative dentries */
160698ca3ee6STrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
160798ca3ee6STrond Myklebust 		return 1;
1608912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
16115ceb9d7fSTrond Myklebust static int
16125ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
16135ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
16141da177e4SLinus Torvalds {
16155ceb9d7fSTrond Myklebust 	switch (error) {
16165ceb9d7fSTrond Myklebust 	case 1:
16172eef8a31STrond Myklebust 		break;
16185ceb9d7fSTrond Myklebust 	case 0:
1619a3f432bfSJ. Bruce Fields 		/*
1620a3f432bfSJ. Bruce Fields 		 * We can't d_drop the root of a disconnected tree:
1621a3f432bfSJ. Bruce Fields 		 * its d_hash is on the s_anon list and d_drop() would hide
1622a3f432bfSJ. Bruce Fields 		 * it from shrink_dcache_for_unmount(), leading to busy
1623a3f432bfSJ. Bruce Fields 		 * inodes on unmount and further oopses.
1624a3f432bfSJ. Bruce Fields 		 */
162547397915STrond Myklebust 		if (inode && IS_ROOT(dentry))
16262eef8a31STrond Myklebust 			error = 1;
16272eef8a31STrond Myklebust 		break;
16285ceb9d7fSTrond Myklebust 	}
16292eef8a31STrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1630e1fb4d05STrond Myklebust 	return error;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16335ceb9d7fSTrond Myklebust static int
16345ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
16355ceb9d7fSTrond Myklebust 			       unsigned int flags)
16365ceb9d7fSTrond Myklebust {
16375ceb9d7fSTrond Myklebust 	int ret = 1;
16385ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
16395ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
16405ceb9d7fSTrond Myklebust 			return -ECHILD;
16415ceb9d7fSTrond Myklebust 		ret = 0;
16425ceb9d7fSTrond Myklebust 	}
16435ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
16445ceb9d7fSTrond Myklebust }
16455ceb9d7fSTrond Myklebust 
16465ceb9d7fSTrond Myklebust static int
16475ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
16485ceb9d7fSTrond Myklebust 				struct inode *inode)
16495ceb9d7fSTrond Myklebust {
16505ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16515ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
16525ceb9d7fSTrond Myklebust }
16535ceb9d7fSTrond Myklebust 
16540b3cc71bSTrond Myklebust static int nfs_lookup_revalidate_dentry(struct inode *dir,
16550b3cc71bSTrond Myklebust 					struct dentry *dentry,
16560b3cc71bSTrond Myklebust 					struct inode *inode, unsigned int flags)
16575ceb9d7fSTrond Myklebust {
16585ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
16595ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
1660a1147b82STrond Myklebust 	unsigned long dir_verifier;
16615ceb9d7fSTrond Myklebust 	int ret;
16625ceb9d7fSTrond Myklebust 
16630b3cc71bSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
16640b3cc71bSTrond Myklebust 
16655ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
16665ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
16679558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
16689558a007SAnna Schumaker 	if (fhandle == NULL || fattr == NULL)
16695ceb9d7fSTrond Myklebust 		goto out;
16705ceb9d7fSTrond Myklebust 
1671a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
16729558a007SAnna Schumaker 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
16735ceb9d7fSTrond Myklebust 	if (ret < 0) {
1674f7b37b8bSTrond Myklebust 		switch (ret) {
1675f7b37b8bSTrond Myklebust 		case -ESTALE:
1676f7b37b8bSTrond Myklebust 		case -ENOENT:
16775ceb9d7fSTrond Myklebust 			ret = 0;
1678f7b37b8bSTrond Myklebust 			break;
1679f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1680f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1681f7b37b8bSTrond Myklebust 				ret = 1;
1682f7b37b8bSTrond Myklebust 		}
16835ceb9d7fSTrond Myklebust 		goto out;
16845ceb9d7fSTrond Myklebust 	}
16850b3cc71bSTrond Myklebust 
16860b3cc71bSTrond Myklebust 	/* Request help from readdirplus */
16870b3cc71bSTrond Myklebust 	nfs_lookup_advise_force_readdirplus(dir, flags);
16880b3cc71bSTrond Myklebust 
16895ceb9d7fSTrond Myklebust 	ret = 0;
16905ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
16915ceb9d7fSTrond Myklebust 		goto out;
16925ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
16935ceb9d7fSTrond Myklebust 		goto out;
16945ceb9d7fSTrond Myklebust 
1695dd225cb3SAnna Schumaker 	nfs_setsecurity(inode, fattr);
1696a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
16975ceb9d7fSTrond Myklebust 
16985ceb9d7fSTrond Myklebust 	ret = 1;
16995ceb9d7fSTrond Myklebust out:
17005ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
17015ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
170282e7ca13STrond Myklebust 
170382e7ca13STrond Myklebust 	/*
170482e7ca13STrond Myklebust 	 * If the lookup failed despite the dentry change attribute being
170582e7ca13STrond Myklebust 	 * a match, then we should revalidate the directory cache.
170682e7ca13STrond Myklebust 	 */
17078ce37abdSTrond Myklebust 	if (!ret && nfs_dentry_verify_change(dir, dentry))
170882e7ca13STrond Myklebust 		nfs_mark_dir_for_revalidate(dir);
17095ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
17105ceb9d7fSTrond Myklebust }
17115ceb9d7fSTrond Myklebust 
17125ceb9d7fSTrond Myklebust /*
17135ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
17145ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
17155ceb9d7fSTrond Myklebust  * lookup.
17165ceb9d7fSTrond Myklebust  *
17175ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
17185ceb9d7fSTrond Myklebust  * we have an inode!
17195ceb9d7fSTrond Myklebust  *
17205ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
17215ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
17225ceb9d7fSTrond Myklebust  */
17235ceb9d7fSTrond Myklebust static int
17245ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
17255ceb9d7fSTrond Myklebust 			 unsigned int flags)
17265ceb9d7fSTrond Myklebust {
17275ceb9d7fSTrond Myklebust 	struct inode *inode;
17285ceb9d7fSTrond Myklebust 	int error;
17295ceb9d7fSTrond Myklebust 
17305ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
17315ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
17325ceb9d7fSTrond Myklebust 
17335ceb9d7fSTrond Myklebust 	if (!inode)
17345ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
17355ceb9d7fSTrond Myklebust 
17365ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
17375ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
17385ceb9d7fSTrond Myklebust 				__func__, dentry);
17395ceb9d7fSTrond Myklebust 		goto out_bad;
17405ceb9d7fSTrond Myklebust 	}
17415ceb9d7fSTrond Myklebust 
17426ca0a6f8STrond Myklebust 	if ((flags & LOOKUP_RENAME_TARGET) && d_count(dentry) < 2 &&
17436ca0a6f8STrond Myklebust 	    nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
17446ca0a6f8STrond Myklebust 		goto out_bad;
17456ca0a6f8STrond Myklebust 
1746efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
17475ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
17485ceb9d7fSTrond Myklebust 
17495ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
17505ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
17515ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
17525ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
17535ceb9d7fSTrond Myklebust 		if (error) {
17545ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
175582e7ca13STrond Myklebust 				nfs_mark_dir_for_revalidate(dir);
17565ceb9d7fSTrond Myklebust 			goto out_bad;
17575ceb9d7fSTrond Myklebust 		}
17585ceb9d7fSTrond Myklebust 		goto out_valid;
17595ceb9d7fSTrond Myklebust 	}
17605ceb9d7fSTrond Myklebust 
17615ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
17625ceb9d7fSTrond Myklebust 		return -ECHILD;
17635ceb9d7fSTrond Myklebust 
17645ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
17655ceb9d7fSTrond Myklebust 		goto out_bad;
17665ceb9d7fSTrond Myklebust 
17670b3cc71bSTrond Myklebust 	return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
17685ceb9d7fSTrond Myklebust out_valid:
17695ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
17705ceb9d7fSTrond Myklebust out_bad:
17715ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
17725ceb9d7fSTrond Myklebust 		return -ECHILD;
17735ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
17745ceb9d7fSTrond Myklebust }
17755ceb9d7fSTrond Myklebust 
17765ceb9d7fSTrond Myklebust static int
1777c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1778c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
17795ceb9d7fSTrond Myklebust {
17805ceb9d7fSTrond Myklebust 	struct dentry *parent;
17815ceb9d7fSTrond Myklebust 	struct inode *dir;
17825ceb9d7fSTrond Myklebust 	int ret;
17835ceb9d7fSTrond Myklebust 
17845ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
17853c59366cSNeilBrown 		if (dentry->d_fsdata == NFS_FSDATA_BLOCKED)
17863c59366cSNeilBrown 			return -ECHILD;
17875ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
17885ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
17895ceb9d7fSTrond Myklebust 		if (!dir)
17905ceb9d7fSTrond Myklebust 			return -ECHILD;
1791c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
17925ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
17935ceb9d7fSTrond Myklebust 			return -ECHILD;
17945ceb9d7fSTrond Myklebust 	} else {
17953c59366cSNeilBrown 		/* Wait for unlink to complete */
17963c59366cSNeilBrown 		wait_var_event(&dentry->d_fsdata,
17973c59366cSNeilBrown 			       dentry->d_fsdata != NFS_FSDATA_BLOCKED);
17985ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1799c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
18005ceb9d7fSTrond Myklebust 		dput(parent);
18015ceb9d7fSTrond Myklebust 	}
18025ceb9d7fSTrond Myklebust 	return ret;
18035ceb9d7fSTrond Myklebust }
18045ceb9d7fSTrond Myklebust 
1805c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1806c7944ebbSTrond Myklebust {
1807c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1808c7944ebbSTrond Myklebust }
1809c7944ebbSTrond Myklebust 
18101da177e4SLinus Torvalds /*
18112b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1812ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1813ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1814ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1815ecf3d1f1SJeff Layton  *
1816ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1817ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1818ecf3d1f1SJeff Layton  */
1819ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1820ecf3d1f1SJeff Layton {
18212b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
18229cdd1d3fSTrond Myklebust 	int error = 0;
1823ecf3d1f1SJeff Layton 
1824ecf3d1f1SJeff Layton 	/*
1825ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1826ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1827ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1828ecf3d1f1SJeff Layton 	 */
1829ecf3d1f1SJeff Layton 	if (!inode) {
18306de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
18316de1472fSAl Viro 				__func__, dentry);
1832ecf3d1f1SJeff Layton 		return 1;
1833ecf3d1f1SJeff Layton 	}
1834ecf3d1f1SJeff Layton 
1835ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
18366de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
18376de1472fSAl Viro 				__func__, dentry);
1838ecf3d1f1SJeff Layton 		return 0;
1839ecf3d1f1SJeff Layton 	}
1840ecf3d1f1SJeff Layton 
1841b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1842ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1843ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1844ecf3d1f1SJeff Layton 	return !error;
1845ecf3d1f1SJeff Layton }
1846ecf3d1f1SJeff Layton 
1847ecf3d1f1SJeff Layton /*
18481da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
18491da177e4SLinus Torvalds  */
1850fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
18511da177e4SLinus Torvalds {
18526de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
18536de1472fSAl Viro 		dentry, dentry->d_flags);
18541da177e4SLinus Torvalds 
185577f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
18562b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
185777f11192STrond Myklebust 		return 1;
185877f11192STrond Myklebust 
18591da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
18601da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
18611da177e4SLinus Torvalds 		return 1;
18621da177e4SLinus Torvalds 	}
18631751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
18641da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
18651da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
18661da177e4SLinus Torvalds 		return 1;
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 	return 0;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds }
18711da177e4SLinus Torvalds 
18721f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
18731b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
18741b83d707STrond Myklebust {
18751b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
18761f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
187759a707b0STrond Myklebust 	if (inode->i_nlink > 0)
187859a707b0STrond Myklebust 		drop_nlink(inode);
187959a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1880ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(
1881ac46b3d7STrond Myklebust 		inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
18821301e421STrond Myklebust 			       NFS_INO_INVALID_NLINK);
18831b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
18841b83d707STrond Myklebust }
18851b83d707STrond Myklebust 
18861da177e4SLinus Torvalds /*
18871da177e4SLinus Torvalds  * Called when the dentry loses inode.
18881da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
18891da177e4SLinus Torvalds  */
18901da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1893e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
18941f018458STrond Myklebust 		nfs_drop_nlink(inode);
18951da177e4SLinus Torvalds 	}
18961da177e4SLinus Torvalds 	iput(inode);
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
1899b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1900b1942c5fSAl Viro {
1901b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1902b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1903b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1904b1942c5fSAl Viro 			WARN_ON(1);
1905b1942c5fSAl Viro 		else
1906b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1907b1942c5fSAl Viro 	}
1908b1942c5fSAl Viro }
1909b1942c5fSAl Viro 
1910f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
19111da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1912ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
19131da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
19141da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
191536d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1916b1942c5fSAl Viro 	.d_release	= nfs_d_release,
19171da177e4SLinus Torvalds };
1918ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
19191da177e4SLinus Torvalds 
1920597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
19211da177e4SLinus Torvalds {
19221da177e4SLinus Torvalds 	struct dentry *res;
19231da177e4SLinus Torvalds 	struct inode *inode = NULL;
1924e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1925e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
1926a1147b82STrond Myklebust 	unsigned long dir_verifier;
19271da177e4SLinus Torvalds 	int error;
19281da177e4SLinus Torvalds 
19296de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
193091d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
19311da177e4SLinus Torvalds 
1932130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1933130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
19341da177e4SLinus Torvalds 
1935fd684071STrond Myklebust 	/*
1936fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1937fd684071STrond Myklebust 	 * but don't hash the dentry.
1938fd684071STrond Myklebust 	 */
19399f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1940130f9ab7SAl Viro 		return NULL;
19411da177e4SLinus Torvalds 
1942e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1943e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
19449558a007SAnna Schumaker 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1945e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1946e1fb4d05STrond Myklebust 		goto out;
1947e1fb4d05STrond Myklebust 
1948a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
19496e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
19509558a007SAnna Schumaker 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
19518ce37abdSTrond Myklebust 	if (error == -ENOENT) {
19528ce37abdSTrond Myklebust 		if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
19538ce37abdSTrond Myklebust 			dir_verifier = inode_peek_iversion_raw(dir);
19541da177e4SLinus Torvalds 		goto no_entry;
19558ce37abdSTrond Myklebust 	}
19561da177e4SLinus Torvalds 	if (error < 0) {
19571da177e4SLinus Torvalds 		res = ERR_PTR(error);
19589558a007SAnna Schumaker 		goto out;
19591da177e4SLinus Torvalds 	}
1960cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1961bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
196203f28e3aSTrond Myklebust 	if (IS_ERR(res))
19639558a007SAnna Schumaker 		goto out;
196454ceac45SDavid Howells 
196563519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
19660b3cc71bSTrond Myklebust 	nfs_lookup_advise_force_readdirplus(dir, flags);
1967d69ee9b8STrond Myklebust 
19681da177e4SLinus Torvalds no_entry:
196941d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
19709eaef27bSTrond Myklebust 	if (res != NULL) {
19719eaef27bSTrond Myklebust 		if (IS_ERR(res))
19729558a007SAnna Schumaker 			goto out;
19731da177e4SLinus Torvalds 		dentry = res;
19749eaef27bSTrond Myklebust 	}
1975a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
19761da177e4SLinus Torvalds out:
19779558a007SAnna Schumaker 	trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1978e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1979e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
19801da177e4SLinus Torvalds 	return res;
19811da177e4SLinus Torvalds }
1982ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
19831da177e4SLinus Torvalds 
198400bdadc7STrond Myklebust void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
198500bdadc7STrond Myklebust {
198600bdadc7STrond Myklebust 	/* Case insensitive server? Revalidate dentries */
198700bdadc7STrond Myklebust 	if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
198800bdadc7STrond Myklebust 		d_prune_aliases(inode);
198900bdadc7STrond Myklebust }
199000bdadc7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
199100bdadc7STrond Myklebust 
199289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
19930b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
19941da177e4SLinus Torvalds 
1995f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
19960ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1997b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
19981da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
19991da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
200036d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
2001b1942c5fSAl Viro 	.d_release	= nfs_d_release,
20021da177e4SLinus Torvalds };
200389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
20041da177e4SLinus Torvalds 
2005532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
2006cd9a1c0eSTrond Myklebust {
2007532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
2008cd9a1c0eSTrond Myklebust }
2009cd9a1c0eSTrond Myklebust 
2010cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
2011cd9a1c0eSTrond Myklebust {
2012f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
2013cd9a1c0eSTrond Myklebust 	return 0;
2014cd9a1c0eSTrond Myklebust }
2015cd9a1c0eSTrond Myklebust 
2016d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
20170dd2b474SMiklos Szeredi 			   struct dentry *dentry,
2018b452a458SAl Viro 			   struct file *file, unsigned open_flags)
2019cd9a1c0eSTrond Myklebust {
20200dd2b474SMiklos Szeredi 	int err;
20210dd2b474SMiklos Szeredi 
2022be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
202330d90494SAl Viro 	if (err)
2024d9585277SAl Viro 		goto out;
2025eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
202630d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
2027eaa2b82cSNeilBrown 	else
20289821421aSTrond Myklebust 		err = -EOPENSTALE;
2029cd9a1c0eSTrond Myklebust out:
2030d9585277SAl Viro 	return err;
2031cd9a1c0eSTrond Myklebust }
2032cd9a1c0eSTrond Myklebust 
203373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
203430d90494SAl Viro 		    struct file *file, unsigned open_flags,
203544907d79SAl Viro 		    umode_t mode)
20361da177e4SLinus Torvalds {
2037c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2038cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
20390dd2b474SMiklos Szeredi 	struct dentry *res;
20400dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
2041f46e0bd3STrond Myklebust 	struct inode *inode;
20421472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
204368eaba4cSTrond Myklebust 	unsigned long dir_verifier;
2044c94c0953SAl Viro 	bool switched = false;
204573a09dd9SAl Viro 	int created = 0;
2046898f635cSTrond Myklebust 	int err;
20471da177e4SLinus Torvalds 
20480dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
20492b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
20500dd2b474SMiklos Szeredi 
20511e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
20526de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20531e7cb3dcSChuck Lever 
20549597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
20559597c13bSJeff Layton 	if (err)
20569597c13bSJeff Layton 		return err;
20579597c13bSJeff Layton 
20580dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
20590dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
206000699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
20610dd2b474SMiklos Szeredi 			/*
20620dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
20630dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
20640dd2b474SMiklos Szeredi 			 * again
20650dd2b474SMiklos Szeredi 			 */
2066d9585277SAl Viro 			return -ENOENT;
20670dd2b474SMiklos Szeredi 		}
20681472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
20691da177e4SLinus Torvalds 		goto no_open;
20701da177e4SLinus Torvalds 	}
20711da177e4SLinus Torvalds 
20720dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
2073d9585277SAl Viro 		return -ENAMETOOLONG;
20741da177e4SLinus Torvalds 
20750dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
2076dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
2077dff25ddbSAndreas Gruenbacher 
2078dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
2079dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
2080dff25ddbSAndreas Gruenbacher 
2081536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
2082dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
20830dd2b474SMiklos Szeredi 	}
2084536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
2085536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
2086536e43d1STrond Myklebust 		attr.ia_size = 0;
2087cd9a1c0eSTrond Myklebust 	}
2088cd9a1c0eSTrond Myklebust 
2089c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
2090c94c0953SAl Viro 		d_drop(dentry);
2091c94c0953SAl Viro 		switched = true;
2092c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
2093c94c0953SAl Viro 					  &dentry->d_name, &wq);
2094c94c0953SAl Viro 		if (IS_ERR(dentry))
2095c94c0953SAl Viro 			return PTR_ERR(dentry);
2096c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
2097c94c0953SAl Viro 			return finish_no_open(file, dentry);
2098c94c0953SAl Viro 	}
2099c94c0953SAl Viro 
2100532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
21010dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
21020dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
2103d9585277SAl Viro 		goto out;
21040dd2b474SMiklos Szeredi 
21056e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
210673a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
210773a09dd9SAl Viro 	if (created)
210873a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
2109275bb307STrond Myklebust 	if (IS_ERR(inode)) {
21100dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
21116e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
21122d9db750STrond Myklebust 		put_nfs_open_context(ctx);
2113d20cb71dSAl Viro 		d_drop(dentry);
21140dd2b474SMiklos Szeredi 		switch (err) {
21151da177e4SLinus Torvalds 		case -ENOENT:
2116774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
211768eaba4cSTrond Myklebust 			if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
211868eaba4cSTrond Myklebust 				dir_verifier = inode_peek_iversion_raw(dir);
211968eaba4cSTrond Myklebust 			else
212068eaba4cSTrond Myklebust 				dir_verifier = nfs_save_change_attribute(dir);
212168eaba4cSTrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
21220dd2b474SMiklos Szeredi 			break;
21231788ea6eSJeff Layton 		case -EISDIR:
21246f926b5bSTrond Myklebust 		case -ENOTDIR:
21256f926b5bSTrond Myklebust 			goto no_open;
21261da177e4SLinus Torvalds 		case -ELOOP:
21270dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
21281da177e4SLinus Torvalds 				goto no_open;
21290dd2b474SMiklos Szeredi 			break;
21301da177e4SLinus Torvalds 			/* case -EINVAL: */
21311da177e4SLinus Torvalds 		default:
21320dd2b474SMiklos Szeredi 			break;
21331da177e4SLinus Torvalds 		}
21341da177e4SLinus Torvalds 		goto out;
21351da177e4SLinus Torvalds 	}
21365ee3d10fSDave Wysochanski 	file->f_mode |= FMODE_CAN_ODIRECT;
21370dd2b474SMiklos Szeredi 
2138b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
21396e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
21402d9db750STrond Myklebust 	put_nfs_open_context(ctx);
2141d9585277SAl Viro out:
2142c94c0953SAl Viro 	if (unlikely(switched)) {
2143c94c0953SAl Viro 		d_lookup_done(dentry);
2144c94c0953SAl Viro 		dput(dentry);
2145c94c0953SAl Viro 	}
2146d9585277SAl Viro 	return err;
21470dd2b474SMiklos Szeredi 
21481da177e4SLinus Torvalds no_open:
21491472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
2150ac795161STrond Myklebust 	if (!res) {
2151ac795161STrond Myklebust 		inode = d_inode(dentry);
2152ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2153e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2154ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
21551751fc1dSTrond Myklebust 		else if (inode && S_ISREG(inode->i_mode))
21561751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2157ac795161STrond Myklebust 	} else if (!IS_ERR(res)) {
2158ac795161STrond Myklebust 		inode = d_inode(res);
2159ac795161STrond Myklebust 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2160e0caaf75STrond Myklebust 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2161ac795161STrond Myklebust 			dput(res);
2162ac795161STrond Myklebust 			res = ERR_PTR(-ENOTDIR);
21631751fc1dSTrond Myklebust 		} else if (inode && S_ISREG(inode->i_mode)) {
21641751fc1dSTrond Myklebust 			dput(res);
21651751fc1dSTrond Myklebust 			res = ERR_PTR(-EOPENSTALE);
2166ac795161STrond Myklebust 		}
2167ac795161STrond Myklebust 	}
2168c94c0953SAl Viro 	if (switched) {
2169c94c0953SAl Viro 		d_lookup_done(dentry);
2170c94c0953SAl Viro 		if (!res)
2171c94c0953SAl Viro 			res = dentry;
2172c94c0953SAl Viro 		else
2173c94c0953SAl Viro 			dput(dentry);
2174c94c0953SAl Viro 	}
21750dd2b474SMiklos Szeredi 	if (IS_ERR(res))
2176c94c0953SAl Viro 		return PTR_ERR(res);
2177e45198a6SAl Viro 	return finish_no_open(file, res);
21781da177e4SLinus Torvalds }
217989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
21801da177e4SLinus Torvalds 
2181c7944ebbSTrond Myklebust static int
2182c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2183c7944ebbSTrond Myklebust 			  unsigned int flags)
21841da177e4SLinus Torvalds {
2185657e94b6SNick Piggin 	struct inode *inode;
21861da177e4SLinus Torvalds 
2187fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2188c7944ebbSTrond Myklebust 		goto full_reval;
2189eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
2190c7944ebbSTrond Myklebust 		goto full_reval;
21912b484297STrond Myklebust 
21922b0143b5SDavid Howells 	inode = d_inode(dentry);
21932b484297STrond Myklebust 
21941da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
21951da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
21961da177e4SLinus Torvalds 	 */
2197c7944ebbSTrond Myklebust 	if (inode == NULL)
2198c7944ebbSTrond Myklebust 		goto full_reval;
219949317a7fSNeilBrown 
2200efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
2201c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2202216d5d06STrond Myklebust 
22031da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
22041da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
2205c7944ebbSTrond Myklebust 		goto full_reval;
2206c7944ebbSTrond Myklebust 
22071da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
2208c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2209c7944ebbSTrond Myklebust 		goto reval_dentry;
2210c7944ebbSTrond Myklebust 
2211c7944ebbSTrond Myklebust 	/* Check if the directory changed */
2212c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2213c7944ebbSTrond Myklebust 		goto reval_dentry;
22141da177e4SLinus Torvalds 
22150ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
2216c7944ebbSTrond Myklebust 	return 1;
2217c7944ebbSTrond Myklebust reval_dentry:
2218c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
2219c7944ebbSTrond Myklebust 		return -ECHILD;
22200b3cc71bSTrond Myklebust 	return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
22210ef97dcfSMiklos Szeredi 
2222c7944ebbSTrond Myklebust full_reval:
2223c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
2224c7944ebbSTrond Myklebust }
2225535918f1STrond Myklebust 
2226c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2227c7944ebbSTrond Myklebust {
2228c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
2229c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
2230c0204fd2STrond Myklebust }
2231c0204fd2STrond Myklebust 
22321da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
22331da177e4SLinus Torvalds 
2234406cd915SBenjamin Coddington struct dentry *
2235406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2236cc6f3298SAnna Schumaker 				struct nfs_fattr *fattr)
22371da177e4SLinus Torvalds {
2238fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
22392b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
22401da177e4SLinus Torvalds 	struct inode *inode;
2241b0c6108eSAl Viro 	struct dentry *d;
2242406cd915SBenjamin Coddington 	int error;
22431da177e4SLinus Torvalds 
2244fab728e1STrond Myklebust 	d_drop(dentry);
2245fab728e1STrond Myklebust 
22461da177e4SLinus Torvalds 	if (fhandle->size == 0) {
22479558a007SAnna Schumaker 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
22481da177e4SLinus Torvalds 		if (error)
2249fab728e1STrond Myklebust 			goto out_error;
22501da177e4SLinus Torvalds 	}
22515724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
22521da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
22531da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2254a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
22552ef61e0eSAnna Schumaker 				fattr, NULL);
22561da177e4SLinus Torvalds 		if (error < 0)
2257fab728e1STrond Myklebust 			goto out_error;
22581da177e4SLinus Torvalds 	}
2259cf7ab00aSAnna Schumaker 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2260b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
2261fab728e1STrond Myklebust out:
2262fab728e1STrond Myklebust 	dput(parent);
2263406cd915SBenjamin Coddington 	return d;
2264fab728e1STrond Myklebust out_error:
2265406cd915SBenjamin Coddington 	d = ERR_PTR(error);
2266406cd915SBenjamin Coddington 	goto out;
2267406cd915SBenjamin Coddington }
2268406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2269406cd915SBenjamin Coddington 
2270406cd915SBenjamin Coddington /*
2271406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
2272406cd915SBenjamin Coddington  */
2273406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2274d91bfc46SAnna Schumaker 				struct nfs_fattr *fattr)
2275406cd915SBenjamin Coddington {
2276406cd915SBenjamin Coddington 	struct dentry *d;
2277406cd915SBenjamin Coddington 
2278cc6f3298SAnna Schumaker 	d = nfs_add_or_obtain(dentry, fhandle, fattr);
2279406cd915SBenjamin Coddington 	if (IS_ERR(d))
2280406cd915SBenjamin Coddington 		return PTR_ERR(d);
2281406cd915SBenjamin Coddington 
2282406cd915SBenjamin Coddington 	/* Callers don't care */
2283406cd915SBenjamin Coddington 	dput(d);
2284406cd915SBenjamin Coddington 	return 0;
22851da177e4SLinus Torvalds }
2286ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds /*
22891da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
22901da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
22911da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
22921da177e4SLinus Torvalds  * reply path made it appear to have failed.
22931da177e4SLinus Torvalds  */
2294549c7297SChristian Brauner int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2295549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, bool excl)
22961da177e4SLinus Torvalds {
22971da177e4SLinus Torvalds 	struct iattr attr;
2298ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
22991da177e4SLinus Torvalds 	int error;
23001da177e4SLinus Torvalds 
23011e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
23026de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	attr.ia_mode = mode;
23051da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
23061da177e4SLinus Torvalds 
23078b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
23088867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
23098b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
23101da177e4SLinus Torvalds 	if (error != 0)
23111da177e4SLinus Torvalds 		goto out_err;
23121da177e4SLinus Torvalds 	return 0;
23131da177e4SLinus Torvalds out_err:
23141da177e4SLinus Torvalds 	d_drop(dentry);
23151da177e4SLinus Torvalds 	return error;
23161da177e4SLinus Torvalds }
2317ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds /*
23201da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
23211da177e4SLinus Torvalds  */
2322597d9289SBryan Schumaker int
2323549c7297SChristian Brauner nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2324549c7297SChristian Brauner 	  struct dentry *dentry, umode_t mode, dev_t rdev)
23251da177e4SLinus Torvalds {
23261da177e4SLinus Torvalds 	struct iattr attr;
23271da177e4SLinus Torvalds 	int status;
23281da177e4SLinus Torvalds 
23291e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
23306de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	attr.ia_mode = mode;
23331da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
23341da177e4SLinus Torvalds 
23351ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
23361da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
23371ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
23381da177e4SLinus Torvalds 	if (status != 0)
23391da177e4SLinus Torvalds 		goto out_err;
23401da177e4SLinus Torvalds 	return 0;
23411da177e4SLinus Torvalds out_err:
23421da177e4SLinus Torvalds 	d_drop(dentry);
23431da177e4SLinus Torvalds 	return status;
23441da177e4SLinus Torvalds }
2345ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds /*
23481da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
23491da177e4SLinus Torvalds  */
2350549c7297SChristian Brauner int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2351549c7297SChristian Brauner 	      struct dentry *dentry, umode_t mode)
23521da177e4SLinus Torvalds {
23531da177e4SLinus Torvalds 	struct iattr attr;
23541da177e4SLinus Torvalds 	int error;
23551da177e4SLinus Torvalds 
23561e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
23576de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
23601da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
23611da177e4SLinus Torvalds 
23621ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
23631da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
23641ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
23651da177e4SLinus Torvalds 	if (error != 0)
23661da177e4SLinus Torvalds 		goto out_err;
23671da177e4SLinus Torvalds 	return 0;
23681da177e4SLinus Torvalds out_err:
23691da177e4SLinus Torvalds 	d_drop(dentry);
23701da177e4SLinus Torvalds 	return error;
23711da177e4SLinus Torvalds }
2372ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
23731da177e4SLinus Torvalds 
2374d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2375d45b9d8bSTrond Myklebust {
2376dc3f4198SAl Viro 	if (simple_positive(dentry))
2377d45b9d8bSTrond Myklebust 		d_delete(dentry);
2378d45b9d8bSTrond Myklebust }
2379d45b9d8bSTrond Myklebust 
23809019fb39STrond Myklebust static void nfs_dentry_remove_handle_error(struct inode *dir,
23819019fb39STrond Myklebust 					   struct dentry *dentry, int error)
23829019fb39STrond Myklebust {
23839019fb39STrond Myklebust 	switch (error) {
23849019fb39STrond Myklebust 	case -ENOENT:
23859019fb39STrond Myklebust 		d_delete(dentry);
238600bdadc7STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
238700bdadc7STrond Myklebust 		break;
23889019fb39STrond Myklebust 	case 0:
238900bdadc7STrond Myklebust 		nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
23909019fb39STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
23919019fb39STrond Myklebust 	}
23929019fb39STrond Myklebust }
23939019fb39STrond Myklebust 
2394597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
23951da177e4SLinus Torvalds {
23961da177e4SLinus Torvalds 	int error;
23971da177e4SLinus Torvalds 
23981e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
23996de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
24001da177e4SLinus Torvalds 
24011ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
24022b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2403884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
24041da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
24051da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2406ba6c0592STrond Myklebust 		switch (error) {
2407ba6c0592STrond Myklebust 		case 0:
24082b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2409ba6c0592STrond Myklebust 			break;
2410ba6c0592STrond Myklebust 		case -ENOENT:
2411d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2412ba6c0592STrond Myklebust 		}
2413884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2414ba6c0592STrond Myklebust 	} else
2415ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
24169019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
24171ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	return error;
24201da177e4SLinus Torvalds }
2421ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds /*
24241da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
24251da177e4SLinus Torvalds  * and after checking that the file has only one user.
24261da177e4SLinus Torvalds  *
24271da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
24281da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
24291da177e4SLinus Torvalds  */
24301da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
24311da177e4SLinus Torvalds {
24322b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
24332b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
24341da177e4SLinus Torvalds 	int error = -EBUSY;
24351da177e4SLinus Torvalds 
24366de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
24391da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
24401da177e4SLinus Torvalds 		error = 0;
24411da177e4SLinus Torvalds 		goto out;
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
24441ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
24451da177e4SLinus Torvalds 	if (inode != NULL) {
2446912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
24471da177e4SLinus Torvalds 		if (error == 0)
24481b83d707STrond Myklebust 			nfs_drop_nlink(inode);
24491da177e4SLinus Torvalds 	} else
2450912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2451d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2452d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
24531ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
24541da177e4SLinus Torvalds out:
24551da177e4SLinus Torvalds 	return error;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
24591da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
24601da177e4SLinus Torvalds  *
24611da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
24621da177e4SLinus Torvalds  */
2463597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
24641da177e4SLinus Torvalds {
24651da177e4SLinus Torvalds 	int error;
24661da177e4SLinus Torvalds 
24671e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
24686de1472fSAl Viro 		dir->i_ino, dentry);
24691da177e4SLinus Torvalds 
24701ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
24711da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
247243245ecaSOlga Kornievskaia 	if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
247343245ecaSOlga Kornievskaia 					     &NFS_I(d_inode(dentry))->flags)) {
24741da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2475ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
24762b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
24771da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
24781ca42382STrond Myklebust 		goto out;
24791da177e4SLinus Torvalds 	}
24803c59366cSNeilBrown 	/* We must prevent any concurrent open until the unlink
24813c59366cSNeilBrown 	 * completes.  ->d_revalidate will wait for ->d_fsdata
24823c59366cSNeilBrown 	 * to clear.  We set it here to ensure no lookup succeeds until
24833c59366cSNeilBrown 	 * the unlink is complete on the server.
24843c59366cSNeilBrown 	 */
24853c59366cSNeilBrown 	error = -ETXTBSY;
24863c59366cSNeilBrown 	if (WARN_ON(dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
2487*2067231aSSun Ke 	    WARN_ON(dentry->d_fsdata == NFS_FSDATA_BLOCKED)) {
2488*2067231aSSun Ke 		spin_unlock(&dentry->d_lock);
24893c59366cSNeilBrown 		goto out;
2490*2067231aSSun Ke 	}
24913c59366cSNeilBrown 	if (dentry->d_fsdata)
24923c59366cSNeilBrown 		/* old devname */
24933c59366cSNeilBrown 		kfree(dentry->d_fsdata);
24943c59366cSNeilBrown 	dentry->d_fsdata = NFS_FSDATA_BLOCKED;
24953c59366cSNeilBrown 
24961da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
24971da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
24989019fb39STrond Myklebust 	nfs_dentry_remove_handle_error(dir, dentry, error);
24993c59366cSNeilBrown 	dentry->d_fsdata = NULL;
25003c59366cSNeilBrown 	wake_up_var(&dentry->d_fsdata);
25011ca42382STrond Myklebust out:
25021ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
25031da177e4SLinus Torvalds 	return error;
25041da177e4SLinus Torvalds }
2505ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
25061da177e4SLinus Torvalds 
2507873101b3SChuck Lever /*
2508873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2509873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2510873101b3SChuck Lever  * using prepare_write/commit_write.
2511873101b3SChuck Lever  *
2512873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2513873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2514873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2515873101b3SChuck Lever  * symlink request has completed on the server.
2516873101b3SChuck Lever  *
2517873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2518873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2519873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2520873101b3SChuck Lever  * and move the raw page into its mapping.
2521873101b3SChuck Lever  */
2522549c7297SChristian Brauner int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2523549c7297SChristian Brauner 		struct dentry *dentry, const char *symname)
25241da177e4SLinus Torvalds {
2525873101b3SChuck Lever 	struct page *page;
2526873101b3SChuck Lever 	char *kaddr;
25271da177e4SLinus Torvalds 	struct iattr attr;
2528873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
25291da177e4SLinus Torvalds 	int error;
25301da177e4SLinus Torvalds 
25311e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
25326de1472fSAl Viro 		dir->i_ino, dentry, symname);
25331da177e4SLinus Torvalds 
2534873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2535873101b3SChuck Lever 		return -ENAMETOOLONG;
25361da177e4SLinus Torvalds 
2537873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2538873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
25391da177e4SLinus Torvalds 
2540e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
254176566991STrond Myklebust 	if (!page)
2542873101b3SChuck Lever 		return -ENOMEM;
2543873101b3SChuck Lever 
2544e8ecde25SAl Viro 	kaddr = page_address(page);
2545873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2546873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2547873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2548873101b3SChuck Lever 
25491ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
255094a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
25511ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2552873101b3SChuck Lever 	if (error != 0) {
25531e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2554873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
25556de1472fSAl Viro 			dentry, symname, error);
25561da177e4SLinus Torvalds 		d_drop(dentry);
2557873101b3SChuck Lever 		__free_page(page);
25581da177e4SLinus Torvalds 		return error;
25591da177e4SLinus Torvalds 	}
25601da177e4SLinus Torvalds 
2561342a67f0STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2562342a67f0STrond Myklebust 
2563873101b3SChuck Lever 	/*
2564873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2565873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2566873101b3SChuck Lever 	 */
25672b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2568873101b3SChuck Lever 							GFP_KERNEL)) {
2569873101b3SChuck Lever 		SetPageUptodate(page);
2570873101b3SChuck Lever 		unlock_page(page);
2571a0b54addSRafael Aquini 		/*
2572a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2573a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2574a0b54addSRafael Aquini 		 */
257509cbfeafSKirill A. Shutemov 		put_page(page);
2576873101b3SChuck Lever 	} else
2577873101b3SChuck Lever 		__free_page(page);
2578873101b3SChuck Lever 
2579873101b3SChuck Lever 	return 0;
2580873101b3SChuck Lever }
2581ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2582873101b3SChuck Lever 
2583597d9289SBryan Schumaker int
25841da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
25851da177e4SLinus Torvalds {
25862b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
25871da177e4SLinus Torvalds 	int error;
25881da177e4SLinus Torvalds 
25896de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
25906de1472fSAl Viro 		old_dentry, dentry);
25911da177e4SLinus Torvalds 
25921fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
25939697d234STrond Myklebust 	d_drop(dentry);
259420497503STrond Myklebust 	if (S_ISREG(inode->i_mode))
259520497503STrond Myklebust 		nfs_sync_inode(inode);
25961da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2597cf809556STrond Myklebust 	if (error == 0) {
2598342a67f0STrond Myklebust 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
25997de9c6eeSAl Viro 		ihold(inode);
26009697d234STrond Myklebust 		d_add(dentry, inode);
2601cf809556STrond Myklebust 	}
26021fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
26031da177e4SLinus Torvalds 	return error;
26041da177e4SLinus Torvalds }
2605ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
26061da177e4SLinus Torvalds 
26073c59366cSNeilBrown static void
26083c59366cSNeilBrown nfs_unblock_rename(struct rpc_task *task, struct nfs_renamedata *data)
26093c59366cSNeilBrown {
26103c59366cSNeilBrown 	struct dentry *new_dentry = data->new_dentry;
26113c59366cSNeilBrown 
26123c59366cSNeilBrown 	new_dentry->d_fsdata = NULL;
26133c59366cSNeilBrown 	wake_up_var(&new_dentry->d_fsdata);
26143c59366cSNeilBrown }
26153c59366cSNeilBrown 
26161da177e4SLinus Torvalds /*
26171da177e4SLinus Torvalds  * RENAME
26181da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
26191da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
26201da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
26211da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
26221da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
26231da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
26241da177e4SLinus Torvalds  *
26251da177e4SLinus Torvalds  * FIXED.
26261da177e4SLinus Torvalds  *
26271da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
26281da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
26291da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
26301da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
26311da177e4SLinus Torvalds  * using the inode layer
26321da177e4SLinus Torvalds  *
26331da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
26341da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
26351da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
26361da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
26371da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
26381da177e4SLinus Torvalds  * the rename.
26391da177e4SLinus Torvalds  */
2640549c7297SChristian Brauner int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2641549c7297SChristian Brauner 	       struct dentry *old_dentry, struct inode *new_dir,
2642549c7297SChristian Brauner 	       struct dentry *new_dentry, unsigned int flags)
26431da177e4SLinus Torvalds {
26442b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
26452b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
26463c59366cSNeilBrown 	struct dentry *dentry = NULL;
264780a491fdSJeff Layton 	struct rpc_task *task;
26483c59366cSNeilBrown 	bool must_unblock = false;
26491da177e4SLinus Torvalds 	int error = -EBUSY;
26501da177e4SLinus Torvalds 
26511cd66c93SMiklos Szeredi 	if (flags)
26521cd66c93SMiklos Szeredi 		return -EINVAL;
26531cd66c93SMiklos Szeredi 
26546de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
26556de1472fSAl Viro 		 old_dentry, new_dentry,
265684d08fa8SAl Viro 		 d_count(new_dentry));
26571da177e4SLinus Torvalds 
265870ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
26591da177e4SLinus Torvalds 	/*
266028f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
266128f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
266228f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
266328f79a1aSMiklos Szeredi 	 * the new target.
26641da177e4SLinus Torvalds 	 */
266527226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
26663c59366cSNeilBrown 		/* We must prevent any concurrent open until the unlink
26673c59366cSNeilBrown 		 * completes.  ->d_revalidate will wait for ->d_fsdata
26683c59366cSNeilBrown 		 * to clear.  We set it here to ensure no lookup succeeds until
26693c59366cSNeilBrown 		 * the unlink is complete on the server.
267027226104SMiklos Szeredi 		 */
26713c59366cSNeilBrown 		error = -ETXTBSY;
26723c59366cSNeilBrown 		if (WARN_ON(new_dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
26733c59366cSNeilBrown 		    WARN_ON(new_dentry->d_fsdata == NFS_FSDATA_BLOCKED))
26743c59366cSNeilBrown 			goto out;
26753c59366cSNeilBrown 		if (new_dentry->d_fsdata) {
26763c59366cSNeilBrown 			/* old devname */
26773c59366cSNeilBrown 			kfree(new_dentry->d_fsdata);
26783c59366cSNeilBrown 			new_dentry->d_fsdata = NULL;
2679d9f29500SBenjamin Coddington 		}
268027226104SMiklos Szeredi 
26813c59366cSNeilBrown 		spin_lock(&new_dentry->d_lock);
268284d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
26831da177e4SLinus Torvalds 			int err;
268427226104SMiklos Szeredi 
26853c59366cSNeilBrown 			spin_unlock(&new_dentry->d_lock);
26863c59366cSNeilBrown 
26871da177e4SLinus Torvalds 			/* copy the target dentry's name */
26881da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
26891da177e4SLinus Torvalds 					 &new_dentry->d_name);
26901da177e4SLinus Torvalds 			if (!dentry)
26911da177e4SLinus Torvalds 				goto out;
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
26941da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
269524e93025SMiklos Szeredi 			if (err)
26961da177e4SLinus Torvalds 				goto out;
269724e93025SMiklos Szeredi 
269824e93025SMiklos Szeredi 			new_dentry = dentry;
269924e93025SMiklos Szeredi 			new_inode = NULL;
27003c59366cSNeilBrown 		} else {
27013c59366cSNeilBrown 			new_dentry->d_fsdata = NFS_FSDATA_BLOCKED;
27023c59366cSNeilBrown 			must_unblock = true;
27033c59366cSNeilBrown 			spin_unlock(&new_dentry->d_lock);
2704b1e4adf4STrond Myklebust 		}
27053c59366cSNeilBrown 
270627226104SMiklos Szeredi 	}
27071da177e4SLinus Torvalds 
27086ff9d99bSTrond Myklebust 	if (S_ISREG(old_inode->i_mode))
27096ff9d99bSTrond Myklebust 		nfs_sync_inode(old_inode);
27103c59366cSNeilBrown 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry,
27113c59366cSNeilBrown 				must_unblock ? nfs_unblock_rename : NULL);
271280a491fdSJeff Layton 	if (IS_ERR(task)) {
271380a491fdSJeff Layton 		error = PTR_ERR(task);
271480a491fdSJeff Layton 		goto out;
271580a491fdSJeff Layton 	}
271680a491fdSJeff Layton 
271780a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2718818a8dbeSBenjamin Coddington 	if (error != 0) {
2719818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2720818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2721818a8dbeSBenjamin Coddington 		smp_wmb();
2722818a8dbeSBenjamin Coddington 	} else
272380a491fdSJeff Layton 		error = task->tk_status;
272480a491fdSJeff Layton 	rpc_put_task(task);
272559a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
272659a707b0STrond Myklebust 	if (error == 0) {
272759a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
272859a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2729ac46b3d7STrond Myklebust 		nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2730ac46b3d7STrond Myklebust 							 NFS_INO_INVALID_CTIME |
2731ac46b3d7STrond Myklebust 							 NFS_INO_REVAL_FORCED);
273259a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
273359a707b0STrond Myklebust 	}
27341da177e4SLinus Torvalds out:
273570ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
273670ded201STrond Myklebust 			new_dir, new_dentry, error);
2737d9f29500SBenjamin Coddington 	if (!error) {
2738d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2739d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2740d9f29500SBenjamin Coddington 		/*
2741d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2742d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2743d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2744d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2745d9f29500SBenjamin Coddington 		 */
2746d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2747d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2748d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2749d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2750d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2751d9f29500SBenjamin Coddington 
27521da177e4SLinus Torvalds 	/* new dentry created? */
27531da177e4SLinus Torvalds 	if (dentry)
27541da177e4SLinus Torvalds 		dput(dentry);
27551da177e4SLinus Torvalds 	return error;
27561da177e4SLinus Torvalds }
2757ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
27581da177e4SLinus Torvalds 
2759cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2760cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2761cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2762cfcea3e8STrond Myklebust 
2763a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
27643a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
27653a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
27663a505845STrond Myklebust 
27671c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
27681c3c07e9STrond Myklebust {
27696238aec8SNeilBrown 	put_group_info(entry->group_info);
2770f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
27714e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2772cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
27734e857c58SPeter Zijlstra 	smp_mb__after_atomic();
27741c3c07e9STrond Myklebust }
27751c3c07e9STrond Myklebust 
27761a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
27771a81bb8aSTrond Myklebust {
27781a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
27791a81bb8aSTrond Myklebust 
27801a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
27811a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
27821a81bb8aSTrond Myklebust 		list_del(&cache->lru);
27831a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
27841a81bb8aSTrond Myklebust 	}
27851a81bb8aSTrond Myklebust }
27861a81bb8aSTrond Myklebust 
27873a505845STrond Myklebust static unsigned long
27883a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2789979df72eSTrond Myklebust {
2790979df72eSTrond Myklebust 	LIST_HEAD(head);
2791aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2792979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
27931ab6c499SDave Chinner 	long freed = 0;
2794979df72eSTrond Myklebust 
2795a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2796aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2797979df72eSTrond Myklebust 		struct inode *inode;
2798979df72eSTrond Myklebust 
2799979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2800979df72eSTrond Myklebust 			break;
28019c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2802979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2803979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2804979df72eSTrond Myklebust 			goto remove_lru_entry;
2805979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2806979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2807979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2808979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
28091ab6c499SDave Chinner 		freed++;
2810979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2811979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2812979df72eSTrond Myklebust 					&nfs_access_lru_list);
2813979df72eSTrond Myklebust 		else {
2814979df72eSTrond Myklebust remove_lru_entry:
2815979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
28164e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2817979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
28184e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2819979df72eSTrond Myklebust 		}
282059844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2821979df72eSTrond Myklebust 	}
2822979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
28231a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
28241ab6c499SDave Chinner 	return freed;
28251ab6c499SDave Chinner }
28261ab6c499SDave Chinner 
28271ab6c499SDave Chinner unsigned long
28283a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
28293a505845STrond Myklebust {
28303a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
28313a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
28323a505845STrond Myklebust 
28333a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
28343a505845STrond Myklebust 		return SHRINK_STOP;
28353a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
28363a505845STrond Myklebust }
28373a505845STrond Myklebust 
28383a505845STrond Myklebust 
28393a505845STrond Myklebust unsigned long
28401ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
28411ab6c499SDave Chinner {
284255f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2843979df72eSTrond Myklebust }
2844979df72eSTrond Myklebust 
28453a505845STrond Myklebust static void
28463a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
28473a505845STrond Myklebust {
28483a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
28493a505845STrond Myklebust 	unsigned long diff;
28503a505845STrond Myklebust 	unsigned int nr_to_scan;
28513a505845STrond Myklebust 
28523a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
28533a505845STrond Myklebust 		return;
28543a505845STrond Myklebust 	nr_to_scan = 100;
28553a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
28563a505845STrond Myklebust 	if (diff < nr_to_scan)
28573a505845STrond Myklebust 		nr_to_scan = diff;
28583a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
28593a505845STrond Myklebust }
28603a505845STrond Myklebust 
28611a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
28621c3c07e9STrond Myklebust {
28631c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
28641a81bb8aSTrond Myklebust 	struct rb_node *n;
28651c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
28661c3c07e9STrond Myklebust 
28671c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
28681c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
28691c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
28701c3c07e9STrond Myklebust 		rb_erase(n, root_node);
28711a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
28721c3c07e9STrond Myklebust 	}
28731c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
28741c3c07e9STrond Myklebust }
28751c3c07e9STrond Myklebust 
28761c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
28771c3c07e9STrond Myklebust {
28781a81bb8aSTrond Myklebust 	LIST_HEAD(head);
28791a81bb8aSTrond Myklebust 
28801a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
28811a81bb8aSTrond Myklebust 		return;
2882cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2883cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
28841a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2885cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2886cfcea3e8STrond Myklebust 
28871c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
28881a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
28891a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
28901a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
28911a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
28921c3c07e9STrond Myklebust }
28931c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
28941c3c07e9STrond Myklebust 
28956238aec8SNeilBrown static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
28966238aec8SNeilBrown {
28976238aec8SNeilBrown 	struct group_info *ga, *gb;
28986238aec8SNeilBrown 	int g;
28996238aec8SNeilBrown 
29006238aec8SNeilBrown 	if (uid_lt(a->fsuid, b->fsuid))
29016238aec8SNeilBrown 		return -1;
29026238aec8SNeilBrown 	if (uid_gt(a->fsuid, b->fsuid))
29036238aec8SNeilBrown 		return 1;
29046238aec8SNeilBrown 
29056238aec8SNeilBrown 	if (gid_lt(a->fsgid, b->fsgid))
29066238aec8SNeilBrown 		return -1;
29076238aec8SNeilBrown 	if (gid_gt(a->fsgid, b->fsgid))
29086238aec8SNeilBrown 		return 1;
29096238aec8SNeilBrown 
29106238aec8SNeilBrown 	ga = a->group_info;
29116238aec8SNeilBrown 	gb = b->group_info;
29126238aec8SNeilBrown 	if (ga == gb)
29136238aec8SNeilBrown 		return 0;
29146238aec8SNeilBrown 	if (ga == NULL)
29156238aec8SNeilBrown 		return -1;
29166238aec8SNeilBrown 	if (gb == NULL)
29176238aec8SNeilBrown 		return 1;
29186238aec8SNeilBrown 	if (ga->ngroups < gb->ngroups)
29196238aec8SNeilBrown 		return -1;
29206238aec8SNeilBrown 	if (ga->ngroups > gb->ngroups)
29216238aec8SNeilBrown 		return 1;
29226238aec8SNeilBrown 
29236238aec8SNeilBrown 	for (g = 0; g < ga->ngroups; g++) {
29246238aec8SNeilBrown 		if (gid_lt(ga->gid[g], gb->gid[g]))
29256238aec8SNeilBrown 			return -1;
29266238aec8SNeilBrown 		if (gid_gt(ga->gid[g], gb->gid[g]))
29276238aec8SNeilBrown 			return 1;
29286238aec8SNeilBrown 	}
29296238aec8SNeilBrown 	return 0;
29306238aec8SNeilBrown }
29316238aec8SNeilBrown 
2932b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
29331c3c07e9STrond Myklebust {
29341c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
29351c3c07e9STrond Myklebust 
29361c3c07e9STrond Myklebust 	while (n != NULL) {
2937b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2938b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
29396238aec8SNeilBrown 		int cmp = access_cmp(cred, entry);
29401c3c07e9STrond Myklebust 
2941b68572e0SNeilBrown 		if (cmp < 0)
29421c3c07e9STrond Myklebust 			n = n->rb_left;
2943b68572e0SNeilBrown 		else if (cmp > 0)
29441c3c07e9STrond Myklebust 			n = n->rb_right;
29451c3c07e9STrond Myklebust 		else
29461c3c07e9STrond Myklebust 			return entry;
29471c3c07e9STrond Myklebust 	}
29481c3c07e9STrond Myklebust 	return NULL;
29491c3c07e9STrond Myklebust }
29501c3c07e9STrond Myklebust 
2951b5e7b59cSNeilBrown static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
29521da177e4SLinus Torvalds {
295355296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
29541c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
295557b69181STrond Myklebust 	bool retry = true;
295657b69181STrond Myklebust 	int err;
29571da177e4SLinus Torvalds 
29581c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
295957b69181STrond Myklebust 	for(;;) {
29601c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
29611c3c07e9STrond Myklebust 			goto out_zap;
29621c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
296357b69181STrond Myklebust 		err = -ENOENT;
29641c3c07e9STrond Myklebust 		if (cache == NULL)
29651c3c07e9STrond Myklebust 			goto out;
296657b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
296721c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
296857b69181STrond Myklebust 			break;
29695c965db8STrond Myklebust 		if (!retry)
29705c965db8STrond Myklebust 			break;
297157b69181STrond Myklebust 		err = -ECHILD;
297257b69181STrond Myklebust 		if (!may_block)
297357b69181STrond Myklebust 			goto out;
297457b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
297557b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
297657b69181STrond Myklebust 		if (err)
297757b69181STrond Myklebust 			return err;
297857b69181STrond Myklebust 		spin_lock(&inode->i_lock);
297957b69181STrond Myklebust 		retry = false;
298057b69181STrond Myklebust 	}
2981b5e7b59cSNeilBrown 	*mask = cache->mask;
2982cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
29831c3c07e9STrond Myklebust 	err = 0;
29841c3c07e9STrond Myklebust out:
29851c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
29861c3c07e9STrond Myklebust 	return err;
29871c3c07e9STrond Myklebust out_zap:
29881a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
29891a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
29901c3c07e9STrond Myklebust 	return -ENOENT;
29911c3c07e9STrond Myklebust }
29921c3c07e9STrond Myklebust 
2993b5e7b59cSNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2994f682a398SNeilBrown {
2995f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2996f682a398SNeilBrown 	 * but do it without locking.
2997f682a398SNeilBrown 	 */
2998f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2999f682a398SNeilBrown 	struct nfs_access_entry *cache;
3000f682a398SNeilBrown 	int err = -ECHILD;
3001f682a398SNeilBrown 	struct list_head *lh;
3002f682a398SNeilBrown 
3003f682a398SNeilBrown 	rcu_read_lock();
3004f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
3005f682a398SNeilBrown 		goto out;
30069f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
3007f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
3008f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
30096238aec8SNeilBrown 	    access_cmp(cred, cache) != 0)
3010f682a398SNeilBrown 		cache = NULL;
3011f682a398SNeilBrown 	if (cache == NULL)
3012f682a398SNeilBrown 		goto out;
301321c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
3014f682a398SNeilBrown 		goto out;
3015b5e7b59cSNeilBrown 	*mask = cache->mask;
301621c3ba7eSTrond Myklebust 	err = 0;
3017f682a398SNeilBrown out:
3018f682a398SNeilBrown 	rcu_read_unlock();
3019f682a398SNeilBrown 	return err;
3020f682a398SNeilBrown }
3021f682a398SNeilBrown 
3022b5e7b59cSNeilBrown int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
3023b5e7b59cSNeilBrown 			  u32 *mask, bool may_block)
3024d2ae4f8bSFrank van der Linden {
3025d2ae4f8bSFrank van der Linden 	int status;
3026d2ae4f8bSFrank van der Linden 
3027b5e7b59cSNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, mask);
3028d2ae4f8bSFrank van der Linden 	if (status != 0)
3029b5e7b59cSNeilBrown 		status = nfs_access_get_cached_locked(inode, cred, mask,
3030d2ae4f8bSFrank van der Linden 		    may_block);
3031d2ae4f8bSFrank van der Linden 
3032d2ae4f8bSFrank van der Linden 	return status;
3033d2ae4f8bSFrank van der Linden }
3034d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
3035d2ae4f8bSFrank van der Linden 
303673fbb3faSNeilBrown static void nfs_access_add_rbtree(struct inode *inode,
303773fbb3faSNeilBrown 				  struct nfs_access_entry *set,
303873fbb3faSNeilBrown 				  const struct cred *cred)
30391c3c07e9STrond Myklebust {
3040cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
3041cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
30421c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
30431c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
30441c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
3045b68572e0SNeilBrown 	int cmp;
30461c3c07e9STrond Myklebust 
30471c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
30481c3c07e9STrond Myklebust 	while (*p != NULL) {
30491c3c07e9STrond Myklebust 		parent = *p;
30501c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
30516238aec8SNeilBrown 		cmp = access_cmp(cred, entry);
30521c3c07e9STrond Myklebust 
3053b68572e0SNeilBrown 		if (cmp < 0)
30541c3c07e9STrond Myklebust 			p = &parent->rb_left;
3055b68572e0SNeilBrown 		else if (cmp > 0)
30561c3c07e9STrond Myklebust 			p = &parent->rb_right;
30571c3c07e9STrond Myklebust 		else
30581c3c07e9STrond Myklebust 			goto found;
30591c3c07e9STrond Myklebust 	}
30601c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
30611c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
3062cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
30631c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
30641c3c07e9STrond Myklebust 	return;
30651c3c07e9STrond Myklebust found:
30661c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
3067cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
3068cfcea3e8STrond Myklebust 	list_del(&entry->lru);
30691c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
30701c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
30711da177e4SLinus Torvalds }
30721da177e4SLinus Torvalds 
307373fbb3faSNeilBrown void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
307473fbb3faSNeilBrown 			  const struct cred *cred)
30751da177e4SLinus Torvalds {
30761c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
30771c3c07e9STrond Myklebust 	if (cache == NULL)
30781c3c07e9STrond Myklebust 		return;
30791c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
30806238aec8SNeilBrown 	cache->fsuid = cred->fsuid;
30816238aec8SNeilBrown 	cache->fsgid = cred->fsgid;
30826238aec8SNeilBrown 	cache->group_info = get_group_info(cred->group_info);
30831da177e4SLinus Torvalds 	cache->mask = set->mask;
30841c3c07e9STrond Myklebust 
3085f682a398SNeilBrown 	/* The above field assignments must be visible
3086f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
3087f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
3088f682a398SNeilBrown 	 */
3089f682a398SNeilBrown 	smp_wmb();
309073fbb3faSNeilBrown 	nfs_access_add_rbtree(inode, cache, cred);
3091cfcea3e8STrond Myklebust 
3092cfcea3e8STrond Myklebust 	/* Update accounting */
30934e857c58SPeter Zijlstra 	smp_mb__before_atomic();
3094cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
30954e857c58SPeter Zijlstra 	smp_mb__after_atomic();
3096cfcea3e8STrond Myklebust 
3097cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
30981a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
3099cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
31001a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
31011a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
31021a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
3103cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
3104cfcea3e8STrond Myklebust 	}
31053a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
31061da177e4SLinus Torvalds }
31076168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
31086168f62cSWeston Andros Adamson 
31093c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
31103c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
31113c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
31123c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
31133c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
31143c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
3115ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
31163c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
31173c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
311815d4b73aSTrond Myklebust static int
3119ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
312015d4b73aSTrond Myklebust {
312115d4b73aSTrond Myklebust 	int mask = 0;
312215d4b73aSTrond Myklebust 
312315d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
312415d4b73aSTrond Myklebust 		mask |= MAY_READ;
3125ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
3126ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
312715d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
3128ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
312915d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
3130ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
3131ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
3132ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
3133ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
313415d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
3135ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
3136ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
313715d4b73aSTrond Myklebust 	return mask;
313815d4b73aSTrond Myklebust }
313915d4b73aSTrond Myklebust 
31406168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
31416168f62cSWeston Andros Adamson {
3142bd8b2441STrond Myklebust 	entry->mask = access_result;
31436168f62cSWeston Andros Adamson }
31446168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
31451da177e4SLinus Torvalds 
3146b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
31471da177e4SLinus Torvalds {
31481da177e4SLinus Torvalds 	struct nfs_access_entry cache;
314957b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3150e8194b7dSTrond Myklebust 	int cache_mask = -1;
31511da177e4SLinus Torvalds 	int status;
31521da177e4SLinus Torvalds 
3153f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
3154f4ce1299STrond Myklebust 
3155b5e7b59cSNeilBrown 	status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
31561da177e4SLinus Torvalds 	if (status == 0)
3157f4ce1299STrond Myklebust 		goto out_cached;
31581da177e4SLinus Torvalds 
3159f3324a2aSNeilBrown 	status = -ECHILD;
316057b69181STrond Myklebust 	if (!may_block)
3161f3324a2aSNeilBrown 		goto out;
3162f3324a2aSNeilBrown 
31631750d929SAnna Schumaker 	/*
31641750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
31651750d929SAnna Schumaker 	 */
316684631f84STrond Myklebust 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
316784631f84STrond Myklebust 		     nfs_access_xattr_mask(NFS_SERVER(inode));
31681750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
31691750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
31701750d929SAnna Schumaker 	else
31711750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
317273fbb3faSNeilBrown 	status = NFS_PROTO(inode)->access(inode, &cache, cred);
3173a71ee337SSuresh Jayaraman 	if (status != 0) {
3174a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
3175a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
317693ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
317793ce4af7STrond Myklebust 			else
317893ce4af7STrond Myklebust 				nfs_zap_caches(inode);
3179a71ee337SSuresh Jayaraman 		}
3180f4ce1299STrond Myklebust 		goto out;
3181a71ee337SSuresh Jayaraman 	}
318273fbb3faSNeilBrown 	nfs_access_add_cache(inode, &cache, cred);
3183f4ce1299STrond Myklebust out_cached:
3184ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3185bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3186f4ce1299STrond Myklebust 		status = -EACCES;
31871da177e4SLinus Torvalds out:
3188e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
3189f4ce1299STrond Myklebust 	return status;
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
3192af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
3193af22f94aSTrond Myklebust {
3194af22f94aSTrond Myklebust 	int mask = 0;
3195af22f94aSTrond Myklebust 
3196f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
3197f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
3198f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
3199f8d9a897SWeston Andros Adamson 	} else {
32008a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
3201af22f94aSTrond Myklebust 			mask |= MAY_READ;
32028a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
3203af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
3204f8d9a897SWeston Andros Adamson 	}
3205f8d9a897SWeston Andros Adamson 
3206af22f94aSTrond Myklebust 	return mask;
3207af22f94aSTrond Myklebust }
3208af22f94aSTrond Myklebust 
3209b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3210af22f94aSTrond Myklebust {
3211af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3212af22f94aSTrond Myklebust }
321389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
3214af22f94aSTrond Myklebust 
32155c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
32165c5fc09aSTrond Myklebust {
32175c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
321821c3ba7eSTrond Myklebust 	int ret = 0;
32195c5fc09aSTrond Myklebust 
32203825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
32213825827eSTrond Myklebust 		return 0;
3222720869ebSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
32235c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
322421c3ba7eSTrond Myklebust 			return -ECHILD;
322521c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
322621c3ba7eSTrond Myklebust 	}
32275c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
32285c5fc09aSTrond Myklebust 		ret = -EACCES;
32295c5fc09aSTrond Myklebust 	return ret;
32305c5fc09aSTrond Myklebust }
32315c5fc09aSTrond Myklebust 
3232549c7297SChristian Brauner int nfs_permission(struct user_namespace *mnt_userns,
3233549c7297SChristian Brauner 		   struct inode *inode,
3234549c7297SChristian Brauner 		   int mask)
32351da177e4SLinus Torvalds {
3236b68572e0SNeilBrown 	const struct cred *cred = current_cred();
32371da177e4SLinus Torvalds 	int res = 0;
32381da177e4SLinus Torvalds 
323991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
324091d5b470SChuck Lever 
3241e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
32421da177e4SLinus Torvalds 		goto out;
32431da177e4SLinus Torvalds 	/* Is this sys_access() ? */
32449cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
32451da177e4SLinus Torvalds 		goto force_lookup;
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
32481da177e4SLinus Torvalds 		case S_IFLNK:
32491da177e4SLinus Torvalds 			goto out;
32501da177e4SLinus Torvalds 		case S_IFREG:
3251762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
3252762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3253762674f8STrond Myklebust 				return 0;
32541da177e4SLinus Torvalds 			break;
32551da177e4SLinus Torvalds 		case S_IFDIR:
32561da177e4SLinus Torvalds 			/*
32571da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
32581da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
32591da177e4SLinus Torvalds 			 */
32601da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
32611da177e4SLinus Torvalds 				goto out;
32621da177e4SLinus Torvalds 	}
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds force_lookup:
32651da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
32661da177e4SLinus Torvalds 		goto out_notsup;
32671da177e4SLinus Torvalds 
32681da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
32691da177e4SLinus Torvalds out:
32705c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
32715c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
3272f696a365SMiklos Szeredi 
32731e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
32741e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
32751da177e4SLinus Torvalds 	return res;
32761da177e4SLinus Torvalds out_notsup:
3277d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
3278d51ac1a8SNeilBrown 		return -ECHILD;
3279d51ac1a8SNeilBrown 
3280720869ebSTrond Myklebust 	res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3281720869ebSTrond Myklebust 						  NFS_INO_INVALID_OTHER);
32821da177e4SLinus Torvalds 	if (res == 0)
328347291baaSChristian Brauner 		res = generic_permission(&init_user_ns, inode, mask);
32841e7cb3dcSChuck Lever 	goto out;
32851da177e4SLinus Torvalds }
3286ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
3287