xref: /openbmc/linux/fs/nfs/dir.c (revision 230bc98f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/nfs/dir.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  *
7  *  nfs directory handling functions
8  *
9  * 10 Apr 1996	Added silly rename for unlink	--okir
10  * 28 Sep 1996	Improved directory cache --okir
11  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
12  *              Re-implemented silly rename for unlink, newly implemented
13  *              silly rename for nfs_rename() following the suggestions
14  *              of Olaf Kirch (okir) found in this file.
15  *              Following Linus comments on my original hack, this version
16  *              depends only on the dcache stuff and doesn't touch the inode
17  *              layer (iput() and friends).
18  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
19  */
20 
21 #include <linux/compat.h>
22 #include <linux/module.h>
23 #include <linux/time.h>
24 #include <linux/errno.h>
25 #include <linux/stat.h>
26 #include <linux/fcntl.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <linux/sunrpc/clnt.h>
32 #include <linux/nfs_fs.h>
33 #include <linux/nfs_mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/pagevec.h>
36 #include <linux/namei.h>
37 #include <linux/mount.h>
38 #include <linux/swap.h>
39 #include <linux/sched.h>
40 #include <linux/kmemleak.h>
41 #include <linux/xattr.h>
42 
43 #include "delegation.h"
44 #include "iostat.h"
45 #include "internal.h"
46 #include "fscache.h"
47 
48 #include "nfstrace.h"
49 
50 /* #define NFS_DEBUG_VERBOSE 1 */
51 
52 static int nfs_opendir(struct inode *, struct file *);
53 static int nfs_closedir(struct inode *, struct file *);
54 static int nfs_readdir(struct file *, struct dir_context *);
55 static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
56 static loff_t nfs_llseek_dir(struct file *, loff_t, int);
57 static void nfs_readdir_clear_array(struct page*);
58 
59 const struct file_operations nfs_dir_operations = {
60 	.llseek		= nfs_llseek_dir,
61 	.read		= generic_read_dir,
62 	.iterate_shared	= nfs_readdir,
63 	.open		= nfs_opendir,
64 	.release	= nfs_closedir,
65 	.fsync		= nfs_fsync_dir,
66 };
67 
68 const struct address_space_operations nfs_dir_aops = {
69 	.freepage = nfs_readdir_clear_array,
70 };
71 
72 #define NFS_INIT_DTSIZE PAGE_SIZE
73 
74 static struct nfs_open_dir_context *
75 alloc_nfs_open_dir_context(struct inode *dir)
76 {
77 	struct nfs_inode *nfsi = NFS_I(dir);
78 	struct nfs_open_dir_context *ctx;
79 
80 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
81 	if (ctx != NULL) {
82 		ctx->attr_gencount = nfsi->attr_gencount;
83 		ctx->dtsize = NFS_INIT_DTSIZE;
84 		spin_lock(&dir->i_lock);
85 		if (list_empty(&nfsi->open_files) &&
86 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
87 			nfs_set_cache_invalid(dir,
88 					      NFS_INO_INVALID_DATA |
89 						      NFS_INO_REVAL_FORCED);
90 		list_add_tail_rcu(&ctx->list, &nfsi->open_files);
91 		memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
92 		spin_unlock(&dir->i_lock);
93 		return ctx;
94 	}
95 	return  ERR_PTR(-ENOMEM);
96 }
97 
98 static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
99 {
100 	spin_lock(&dir->i_lock);
101 	list_del_rcu(&ctx->list);
102 	spin_unlock(&dir->i_lock);
103 	kfree_rcu(ctx, rcu_head);
104 }
105 
106 /*
107  * Open file
108  */
109 static int
110 nfs_opendir(struct inode *inode, struct file *filp)
111 {
112 	int res = 0;
113 	struct nfs_open_dir_context *ctx;
114 
115 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
116 
117 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
118 
119 	ctx = alloc_nfs_open_dir_context(inode);
120 	if (IS_ERR(ctx)) {
121 		res = PTR_ERR(ctx);
122 		goto out;
123 	}
124 	filp->private_data = ctx;
125 out:
126 	return res;
127 }
128 
129 static int
130 nfs_closedir(struct inode *inode, struct file *filp)
131 {
132 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
133 	return 0;
134 }
135 
136 struct nfs_cache_array_entry {
137 	u64 cookie;
138 	u64 ino;
139 	const char *name;
140 	unsigned int name_len;
141 	unsigned char d_type;
142 };
143 
144 struct nfs_cache_array {
145 	u64 change_attr;
146 	u64 last_cookie;
147 	unsigned int size;
148 	unsigned char page_full : 1,
149 		      page_is_eof : 1,
150 		      cookies_are_ordered : 1;
151 	struct nfs_cache_array_entry array[];
152 };
153 
154 struct nfs_readdir_descriptor {
155 	struct file	*file;
156 	struct page	*page;
157 	struct dir_context *ctx;
158 	pgoff_t		page_index;
159 	pgoff_t		page_index_max;
160 	u64		dir_cookie;
161 	u64		last_cookie;
162 	u64		dup_cookie;
163 	loff_t		current_index;
164 	loff_t		prev_index;
165 
166 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
167 	unsigned long	dir_verifier;
168 	unsigned long	timestamp;
169 	unsigned long	gencount;
170 	unsigned long	attr_gencount;
171 	unsigned int	cache_entry_index;
172 	unsigned int	buffer_fills;
173 	unsigned int	dtsize;
174 	signed char duped;
175 	bool plus;
176 	bool eob;
177 	bool eof;
178 };
179 
180 static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
181 {
182 	struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
183 	unsigned int maxsize = server->dtsize;
184 
185 	if (sz > maxsize)
186 		sz = maxsize;
187 	if (sz < NFS_MIN_FILE_IO_SIZE)
188 		sz = NFS_MIN_FILE_IO_SIZE;
189 	desc->dtsize = sz;
190 }
191 
192 static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
193 {
194 	nfs_set_dtsize(desc, desc->dtsize >> 1);
195 }
196 
197 static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
198 {
199 	nfs_set_dtsize(desc, desc->dtsize << 1);
200 }
201 
202 static void nfs_readdir_array_init(struct nfs_cache_array *array)
203 {
204 	memset(array, 0, sizeof(struct nfs_cache_array));
205 }
206 
207 static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie,
208 					u64 change_attr)
209 {
210 	struct nfs_cache_array *array;
211 
212 	array = kmap_atomic(page);
213 	nfs_readdir_array_init(array);
214 	array->change_attr = change_attr;
215 	array->last_cookie = last_cookie;
216 	array->cookies_are_ordered = 1;
217 	kunmap_atomic(array);
218 }
219 
220 /*
221  * we are freeing strings created by nfs_add_to_readdir_array()
222  */
223 static
224 void nfs_readdir_clear_array(struct page *page)
225 {
226 	struct nfs_cache_array *array;
227 	int i;
228 
229 	array = kmap_atomic(page);
230 	for (i = 0; i < array->size; i++)
231 		kfree(array->array[i].name);
232 	nfs_readdir_array_init(array);
233 	kunmap_atomic(array);
234 }
235 
236 static struct page *
237 nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
238 {
239 	struct page *page = alloc_page(gfp_flags);
240 	if (page)
241 		nfs_readdir_page_init_array(page, last_cookie, 0);
242 	return page;
243 }
244 
245 static void nfs_readdir_page_array_free(struct page *page)
246 {
247 	if (page) {
248 		nfs_readdir_clear_array(page);
249 		put_page(page);
250 	}
251 }
252 
253 static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
254 {
255 	array->page_is_eof = 1;
256 	array->page_full = 1;
257 }
258 
259 static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
260 {
261 	return array->page_full;
262 }
263 
264 /*
265  * the caller is responsible for freeing qstr.name
266  * when called by nfs_readdir_add_to_array, the strings will be freed in
267  * nfs_clear_readdir_array()
268  */
269 static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
270 {
271 	const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
272 
273 	/*
274 	 * Avoid a kmemleak false positive. The pointer to the name is stored
275 	 * in a page cache page which kmemleak does not scan.
276 	 */
277 	if (ret != NULL)
278 		kmemleak_not_leak(ret);
279 	return ret;
280 }
281 
282 static size_t nfs_readdir_array_maxentries(void)
283 {
284 	return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
285 	       sizeof(struct nfs_cache_array_entry);
286 }
287 
288 /*
289  * Check that the next array entry lies entirely within the page bounds
290  */
291 static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
292 {
293 	if (array->page_full)
294 		return -ENOSPC;
295 	if (array->size == nfs_readdir_array_maxentries()) {
296 		array->page_full = 1;
297 		return -ENOSPC;
298 	}
299 	return 0;
300 }
301 
302 static
303 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
304 {
305 	struct nfs_cache_array *array;
306 	struct nfs_cache_array_entry *cache_entry;
307 	const char *name;
308 	int ret;
309 
310 	name = nfs_readdir_copy_name(entry->name, entry->len);
311 	if (!name)
312 		return -ENOMEM;
313 
314 	array = kmap_atomic(page);
315 	ret = nfs_readdir_array_can_expand(array);
316 	if (ret) {
317 		kfree(name);
318 		goto out;
319 	}
320 
321 	cache_entry = &array->array[array->size];
322 	cache_entry->cookie = entry->prev_cookie;
323 	cache_entry->ino = entry->ino;
324 	cache_entry->d_type = entry->d_type;
325 	cache_entry->name_len = entry->len;
326 	cache_entry->name = name;
327 	array->last_cookie = entry->cookie;
328 	if (array->last_cookie <= cache_entry->cookie)
329 		array->cookies_are_ordered = 0;
330 	array->size++;
331 	if (entry->eof != 0)
332 		nfs_readdir_array_set_eof(array);
333 out:
334 	kunmap_atomic(array);
335 	return ret;
336 }
337 
338 static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie,
339 				      u64 change_attr)
340 {
341 	struct nfs_cache_array *array = kmap_atomic(page);
342 	int ret = true;
343 
344 	if (array->change_attr != change_attr)
345 		ret = false;
346 	if (array->size > 0 && array->array[0].cookie != last_cookie)
347 		ret = false;
348 	kunmap_atomic(array);
349 	return ret;
350 }
351 
352 static void nfs_readdir_page_unlock_and_put(struct page *page)
353 {
354 	unlock_page(page);
355 	put_page(page);
356 }
357 
358 static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
359 						pgoff_t index, u64 last_cookie)
360 {
361 	struct page *page;
362 	u64 change_attr;
363 
364 	page = grab_cache_page(mapping, index);
365 	if (!page)
366 		return NULL;
367 	change_attr = inode_peek_iversion_raw(mapping->host);
368 	if (PageUptodate(page)) {
369 		if (nfs_readdir_page_validate(page, last_cookie, change_attr))
370 			return page;
371 		nfs_readdir_clear_array(page);
372 	}
373 	nfs_readdir_page_init_array(page, last_cookie, change_attr);
374 	SetPageUptodate(page);
375 	return page;
376 }
377 
378 static loff_t nfs_readdir_page_offset(struct page *page)
379 {
380 	return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries();
381 }
382 
383 static u64 nfs_readdir_page_last_cookie(struct page *page)
384 {
385 	struct nfs_cache_array *array;
386 	u64 ret;
387 
388 	array = kmap_atomic(page);
389 	ret = array->last_cookie;
390 	kunmap_atomic(array);
391 	return ret;
392 }
393 
394 static bool nfs_readdir_page_needs_filling(struct page *page)
395 {
396 	struct nfs_cache_array *array;
397 	bool ret;
398 
399 	array = kmap_atomic(page);
400 	ret = !nfs_readdir_array_is_full(array);
401 	kunmap_atomic(array);
402 	return ret;
403 }
404 
405 static void nfs_readdir_page_set_eof(struct page *page)
406 {
407 	struct nfs_cache_array *array;
408 
409 	array = kmap_atomic(page);
410 	nfs_readdir_array_set_eof(array);
411 	kunmap_atomic(array);
412 }
413 
414 static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
415 					      pgoff_t index, u64 cookie)
416 {
417 	struct page *page;
418 
419 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
420 	if (page) {
421 		if (nfs_readdir_page_last_cookie(page) == cookie)
422 			return page;
423 		nfs_readdir_page_unlock_and_put(page);
424 	}
425 	return NULL;
426 }
427 
428 static inline
429 int is_32bit_api(void)
430 {
431 #ifdef CONFIG_COMPAT
432 	return in_compat_syscall();
433 #else
434 	return (BITS_PER_LONG == 32);
435 #endif
436 }
437 
438 static
439 bool nfs_readdir_use_cookie(const struct file *filp)
440 {
441 	if ((filp->f_mode & FMODE_32BITHASH) ||
442 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
443 		return false;
444 	return true;
445 }
446 
447 static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
448 					struct nfs_readdir_descriptor *desc)
449 {
450 	if (array->page_full) {
451 		desc->last_cookie = array->last_cookie;
452 		desc->current_index += array->size;
453 		desc->cache_entry_index = 0;
454 		desc->page_index++;
455 	} else
456 		desc->last_cookie = array->array[0].cookie;
457 }
458 
459 static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
460 				      struct nfs_readdir_descriptor *desc)
461 {
462 	loff_t diff = desc->ctx->pos - desc->current_index;
463 	unsigned int index;
464 
465 	if (diff < 0)
466 		goto out_eof;
467 	if (diff >= array->size) {
468 		if (array->page_is_eof)
469 			goto out_eof;
470 		nfs_readdir_seek_next_array(array, desc);
471 		return -EAGAIN;
472 	}
473 
474 	index = (unsigned int)diff;
475 	desc->dir_cookie = array->array[index].cookie;
476 	desc->cache_entry_index = index;
477 	return 0;
478 out_eof:
479 	desc->eof = true;
480 	return -EBADCOOKIE;
481 }
482 
483 static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
484 					      u64 cookie)
485 {
486 	if (!array->cookies_are_ordered)
487 		return true;
488 	/* Optimisation for monotonically increasing cookies */
489 	if (cookie >= array->last_cookie)
490 		return false;
491 	if (array->size && cookie < array->array[0].cookie)
492 		return false;
493 	return true;
494 }
495 
496 static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
497 					 struct nfs_readdir_descriptor *desc)
498 {
499 	int i;
500 	loff_t new_pos;
501 	int status = -EAGAIN;
502 
503 	if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
504 		goto check_eof;
505 
506 	for (i = 0; i < array->size; i++) {
507 		if (array->array[i].cookie == desc->dir_cookie) {
508 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
509 
510 			new_pos = nfs_readdir_page_offset(desc->page) + i;
511 			if (desc->attr_gencount != nfsi->attr_gencount) {
512 				desc->duped = 0;
513 				desc->attr_gencount = nfsi->attr_gencount;
514 			} else if (new_pos < desc->prev_index) {
515 				if (desc->duped > 0
516 				    && desc->dup_cookie == desc->dir_cookie) {
517 					if (printk_ratelimit()) {
518 						pr_notice("NFS: directory %pD2 contains a readdir loop."
519 								"Please contact your server vendor.  "
520 								"The file: %s has duplicate cookie %llu\n",
521 								desc->file, array->array[i].name, desc->dir_cookie);
522 					}
523 					status = -ELOOP;
524 					goto out;
525 				}
526 				desc->dup_cookie = desc->dir_cookie;
527 				desc->duped = -1;
528 			}
529 			if (nfs_readdir_use_cookie(desc->file))
530 				desc->ctx->pos = desc->dir_cookie;
531 			else
532 				desc->ctx->pos = new_pos;
533 			desc->prev_index = new_pos;
534 			desc->cache_entry_index = i;
535 			return 0;
536 		}
537 	}
538 check_eof:
539 	if (array->page_is_eof) {
540 		status = -EBADCOOKIE;
541 		if (desc->dir_cookie == array->last_cookie)
542 			desc->eof = true;
543 	} else
544 		nfs_readdir_seek_next_array(array, desc);
545 out:
546 	return status;
547 }
548 
549 static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
550 {
551 	struct nfs_cache_array *array;
552 	int status;
553 
554 	array = kmap_atomic(desc->page);
555 
556 	if (desc->dir_cookie == 0)
557 		status = nfs_readdir_search_for_pos(array, desc);
558 	else
559 		status = nfs_readdir_search_for_cookie(array, desc);
560 
561 	kunmap_atomic(array);
562 	return status;
563 }
564 
565 /* Fill a page with xdr information before transferring to the cache page */
566 static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
567 				  __be32 *verf, u64 cookie,
568 				  struct page **pages, size_t bufsize,
569 				  __be32 *verf_res)
570 {
571 	struct inode *inode = file_inode(desc->file);
572 	struct nfs_readdir_arg arg = {
573 		.dentry = file_dentry(desc->file),
574 		.cred = desc->file->f_cred,
575 		.verf = verf,
576 		.cookie = cookie,
577 		.pages = pages,
578 		.page_len = bufsize,
579 		.plus = desc->plus,
580 	};
581 	struct nfs_readdir_res res = {
582 		.verf = verf_res,
583 	};
584 	unsigned long	timestamp, gencount;
585 	int		error;
586 
587  again:
588 	timestamp = jiffies;
589 	gencount = nfs_inc_attr_generation_counter();
590 	desc->dir_verifier = nfs_save_change_attribute(inode);
591 	error = NFS_PROTO(inode)->readdir(&arg, &res);
592 	if (error < 0) {
593 		/* We requested READDIRPLUS, but the server doesn't grok it */
594 		if (error == -ENOTSUPP && desc->plus) {
595 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
596 			desc->plus = arg.plus = false;
597 			goto again;
598 		}
599 		goto error;
600 	}
601 	desc->timestamp = timestamp;
602 	desc->gencount = gencount;
603 error:
604 	return error;
605 }
606 
607 static int xdr_decode(struct nfs_readdir_descriptor *desc,
608 		      struct nfs_entry *entry, struct xdr_stream *xdr)
609 {
610 	struct inode *inode = file_inode(desc->file);
611 	int error;
612 
613 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
614 	if (error)
615 		return error;
616 	entry->fattr->time_start = desc->timestamp;
617 	entry->fattr->gencount = desc->gencount;
618 	return 0;
619 }
620 
621 /* Match file and dirent using either filehandle or fileid
622  * Note: caller is responsible for checking the fsid
623  */
624 static
625 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
626 {
627 	struct inode *inode;
628 	struct nfs_inode *nfsi;
629 
630 	if (d_really_is_negative(dentry))
631 		return 0;
632 
633 	inode = d_inode(dentry);
634 	if (is_bad_inode(inode) || NFS_STALE(inode))
635 		return 0;
636 
637 	nfsi = NFS_I(inode);
638 	if (entry->fattr->fileid != nfsi->fileid)
639 		return 0;
640 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
641 		return 0;
642 	return 1;
643 }
644 
645 #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL)
646 
647 static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx,
648 				unsigned int cache_hits,
649 				unsigned int cache_misses)
650 {
651 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
652 		return false;
653 	if (ctx->pos == 0 ||
654 	    cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD)
655 		return true;
656 	return false;
657 }
658 
659 /*
660  * This function is called by the getattr code to request the
661  * use of readdirplus to accelerate any future lookups in the same
662  * directory.
663  */
664 void nfs_readdir_record_entry_cache_hit(struct inode *dir)
665 {
666 	struct nfs_inode *nfsi = NFS_I(dir);
667 	struct nfs_open_dir_context *ctx;
668 
669 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
670 	    S_ISDIR(dir->i_mode)) {
671 		rcu_read_lock();
672 		list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
673 			atomic_inc(&ctx->cache_hits);
674 		rcu_read_unlock();
675 	}
676 }
677 
678 /*
679  * This function is mainly for use by nfs_getattr().
680  *
681  * If this is an 'ls -l', we want to force use of readdirplus.
682  */
683 void nfs_readdir_record_entry_cache_miss(struct inode *dir)
684 {
685 	struct nfs_inode *nfsi = NFS_I(dir);
686 	struct nfs_open_dir_context *ctx;
687 
688 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
689 	    S_ISDIR(dir->i_mode)) {
690 		rcu_read_lock();
691 		list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
692 			atomic_inc(&ctx->cache_misses);
693 		rcu_read_unlock();
694 	}
695 }
696 
697 static void nfs_lookup_advise_force_readdirplus(struct inode *dir)
698 {
699 	nfs_readdir_record_entry_cache_miss(dir);
700 }
701 
702 static
703 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
704 		unsigned long dir_verifier)
705 {
706 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
707 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
708 	struct dentry *dentry;
709 	struct dentry *alias;
710 	struct inode *inode;
711 	int status;
712 
713 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
714 		return;
715 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
716 		return;
717 	if (filename.len == 0)
718 		return;
719 	/* Validate that the name doesn't contain any illegal '\0' */
720 	if (strnlen(filename.name, filename.len) != filename.len)
721 		return;
722 	/* ...or '/' */
723 	if (strnchr(filename.name, filename.len, '/'))
724 		return;
725 	if (filename.name[0] == '.') {
726 		if (filename.len == 1)
727 			return;
728 		if (filename.len == 2 && filename.name[1] == '.')
729 			return;
730 	}
731 	filename.hash = full_name_hash(parent, filename.name, filename.len);
732 
733 	dentry = d_lookup(parent, &filename);
734 again:
735 	if (!dentry) {
736 		dentry = d_alloc_parallel(parent, &filename, &wq);
737 		if (IS_ERR(dentry))
738 			return;
739 	}
740 	if (!d_in_lookup(dentry)) {
741 		/* Is there a mountpoint here? If so, just exit */
742 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
743 					&entry->fattr->fsid))
744 			goto out;
745 		if (nfs_same_file(dentry, entry)) {
746 			if (!entry->fh->size)
747 				goto out;
748 			nfs_set_verifier(dentry, dir_verifier);
749 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
750 			if (!status)
751 				nfs_setsecurity(d_inode(dentry), entry->fattr);
752 			goto out;
753 		} else {
754 			d_invalidate(dentry);
755 			dput(dentry);
756 			dentry = NULL;
757 			goto again;
758 		}
759 	}
760 	if (!entry->fh->size) {
761 		d_lookup_done(dentry);
762 		goto out;
763 	}
764 
765 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
766 	alias = d_splice_alias(inode, dentry);
767 	d_lookup_done(dentry);
768 	if (alias) {
769 		if (IS_ERR(alias))
770 			goto out;
771 		dput(dentry);
772 		dentry = alias;
773 	}
774 	nfs_set_verifier(dentry, dir_verifier);
775 out:
776 	dput(dentry);
777 }
778 
779 /* Perform conversion from xdr to cache array */
780 static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
781 				   struct nfs_entry *entry,
782 				   struct page **xdr_pages,
783 				   unsigned int buflen,
784 				   struct page **arrays,
785 				   size_t narrays)
786 {
787 	struct address_space *mapping = desc->file->f_mapping;
788 	struct xdr_stream stream;
789 	struct xdr_buf buf;
790 	struct page *scratch, *new, *page = *arrays;
791 	int status;
792 
793 	scratch = alloc_page(GFP_KERNEL);
794 	if (scratch == NULL)
795 		return -ENOMEM;
796 
797 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
798 	xdr_set_scratch_page(&stream, scratch);
799 
800 	do {
801 		if (entry->fattr->label)
802 			entry->fattr->label->len = NFS4_MAXLABELLEN;
803 
804 		status = xdr_decode(desc, entry, &stream);
805 		if (status != 0)
806 			break;
807 
808 		if (desc->plus)
809 			nfs_prime_dcache(file_dentry(desc->file), entry,
810 					desc->dir_verifier);
811 
812 		status = nfs_readdir_add_to_array(entry, page);
813 		if (status != -ENOSPC)
814 			continue;
815 
816 		if (page->mapping != mapping) {
817 			if (!--narrays)
818 				break;
819 			new = nfs_readdir_page_array_alloc(entry->prev_cookie,
820 							   GFP_KERNEL);
821 			if (!new)
822 				break;
823 			arrays++;
824 			*arrays = page = new;
825 			desc->page_index_max++;
826 		} else {
827 			new = nfs_readdir_page_get_next(mapping,
828 							page->index + 1,
829 							entry->prev_cookie);
830 			if (!new)
831 				break;
832 			if (page != *arrays)
833 				nfs_readdir_page_unlock_and_put(page);
834 			page = new;
835 			desc->page_index_max = new->index;
836 		}
837 		status = nfs_readdir_add_to_array(entry, page);
838 	} while (!status && !entry->eof);
839 
840 	switch (status) {
841 	case -EBADCOOKIE:
842 		if (entry->eof) {
843 			nfs_readdir_page_set_eof(page);
844 			status = 0;
845 		}
846 		break;
847 	case -ENOSPC:
848 	case -EAGAIN:
849 		status = 0;
850 		break;
851 	}
852 
853 	if (page != *arrays)
854 		nfs_readdir_page_unlock_and_put(page);
855 
856 	put_page(scratch);
857 	return status;
858 }
859 
860 static void nfs_readdir_free_pages(struct page **pages, size_t npages)
861 {
862 	while (npages--)
863 		put_page(pages[npages]);
864 	kfree(pages);
865 }
866 
867 /*
868  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
869  * to nfs_readdir_free_pages()
870  */
871 static struct page **nfs_readdir_alloc_pages(size_t npages)
872 {
873 	struct page **pages;
874 	size_t i;
875 
876 	pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
877 	if (!pages)
878 		return NULL;
879 	for (i = 0; i < npages; i++) {
880 		struct page *page = alloc_page(GFP_KERNEL);
881 		if (page == NULL)
882 			goto out_freepages;
883 		pages[i] = page;
884 	}
885 	return pages;
886 
887 out_freepages:
888 	nfs_readdir_free_pages(pages, i);
889 	return NULL;
890 }
891 
892 static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
893 				    __be32 *verf_arg, __be32 *verf_res,
894 				    struct page **arrays, size_t narrays)
895 {
896 	struct page **pages;
897 	struct page *page = *arrays;
898 	struct nfs_entry *entry;
899 	size_t array_size;
900 	struct inode *inode = file_inode(desc->file);
901 	unsigned int dtsize = desc->dtsize;
902 	unsigned int pglen;
903 	int status = -ENOMEM;
904 
905 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
906 	if (!entry)
907 		return -ENOMEM;
908 	entry->cookie = nfs_readdir_page_last_cookie(page);
909 	entry->fh = nfs_alloc_fhandle();
910 	entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
911 	entry->server = NFS_SERVER(inode);
912 	if (entry->fh == NULL || entry->fattr == NULL)
913 		goto out;
914 
915 	array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
916 	pages = nfs_readdir_alloc_pages(array_size);
917 	if (!pages)
918 		goto out;
919 
920 	status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
921 					dtsize, verf_res);
922 	if (status < 0)
923 		goto free_pages;
924 
925 	pglen = status;
926 	if (pglen != 0)
927 		status = nfs_readdir_page_filler(desc, entry, pages, pglen,
928 						 arrays, narrays);
929 	else
930 		nfs_readdir_page_set_eof(page);
931 	desc->buffer_fills++;
932 
933 free_pages:
934 	nfs_readdir_free_pages(pages, array_size);
935 out:
936 	nfs_free_fattr(entry->fattr);
937 	nfs_free_fhandle(entry->fh);
938 	kfree(entry);
939 	return status;
940 }
941 
942 static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
943 {
944 	put_page(desc->page);
945 	desc->page = NULL;
946 }
947 
948 static void
949 nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
950 {
951 	unlock_page(desc->page);
952 	nfs_readdir_page_put(desc);
953 }
954 
955 static struct page *
956 nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
957 {
958 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
959 					   desc->page_index,
960 					   desc->last_cookie);
961 }
962 
963 /*
964  * Returns 0 if desc->dir_cookie was found on page desc->page_index
965  * and locks the page to prevent removal from the page cache.
966  */
967 static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
968 {
969 	struct inode *inode = file_inode(desc->file);
970 	struct nfs_inode *nfsi = NFS_I(inode);
971 	__be32 verf[NFS_DIR_VERIFIER_SIZE];
972 	int res;
973 
974 	desc->page = nfs_readdir_page_get_cached(desc);
975 	if (!desc->page)
976 		return -ENOMEM;
977 	if (nfs_readdir_page_needs_filling(desc->page)) {
978 		/* Grow the dtsize if we had to go back for more pages */
979 		if (desc->page_index == desc->page_index_max)
980 			nfs_grow_dtsize(desc);
981 		desc->page_index_max = desc->page_index;
982 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
983 					       &desc->page, 1);
984 		if (res < 0) {
985 			nfs_readdir_page_unlock_and_put_cached(desc);
986 			if (res == -EBADCOOKIE || res == -ENOTSYNC) {
987 				invalidate_inode_pages2(desc->file->f_mapping);
988 				desc->page_index = 0;
989 				return -EAGAIN;
990 			}
991 			return res;
992 		}
993 		/*
994 		 * Set the cookie verifier if the page cache was empty
995 		 */
996 		if (desc->last_cookie == 0 &&
997 		    memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
998 			memcpy(nfsi->cookieverf, verf,
999 			       sizeof(nfsi->cookieverf));
1000 			invalidate_inode_pages2_range(desc->file->f_mapping,
1001 						      desc->page_index_max + 1,
1002 						      -1);
1003 		}
1004 	}
1005 	res = nfs_readdir_search_array(desc);
1006 	if (res == 0)
1007 		return 0;
1008 	nfs_readdir_page_unlock_and_put_cached(desc);
1009 	return res;
1010 }
1011 
1012 /* Search for desc->dir_cookie from the beginning of the page cache */
1013 static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
1014 {
1015 	int res;
1016 
1017 	do {
1018 		if (desc->page_index == 0) {
1019 			desc->current_index = 0;
1020 			desc->prev_index = 0;
1021 			desc->last_cookie = 0;
1022 		}
1023 		res = find_and_lock_cache_page(desc);
1024 	} while (res == -EAGAIN);
1025 	return res;
1026 }
1027 
1028 /*
1029  * Once we've found the start of the dirent within a page: fill 'er up...
1030  */
1031 static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
1032 			   const __be32 *verf)
1033 {
1034 	struct file	*file = desc->file;
1035 	struct nfs_cache_array *array;
1036 	unsigned int i;
1037 
1038 	array = kmap(desc->page);
1039 	for (i = desc->cache_entry_index; i < array->size; i++) {
1040 		struct nfs_cache_array_entry *ent;
1041 
1042 		ent = &array->array[i];
1043 		if (!dir_emit(desc->ctx, ent->name, ent->name_len,
1044 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1045 			desc->eob = true;
1046 			break;
1047 		}
1048 		memcpy(desc->verf, verf, sizeof(desc->verf));
1049 		if (i == array->size - 1) {
1050 			desc->dir_cookie = array->last_cookie;
1051 			nfs_readdir_seek_next_array(array, desc);
1052 		} else {
1053 			desc->dir_cookie = array->array[i + 1].cookie;
1054 			desc->last_cookie = array->array[0].cookie;
1055 		}
1056 		if (nfs_readdir_use_cookie(file))
1057 			desc->ctx->pos = desc->dir_cookie;
1058 		else
1059 			desc->ctx->pos++;
1060 		if (desc->duped != 0)
1061 			desc->duped = 1;
1062 	}
1063 	if (array->page_is_eof)
1064 		desc->eof = !desc->eob;
1065 
1066 	kunmap(desc->page);
1067 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1068 			(unsigned long long)desc->dir_cookie);
1069 }
1070 
1071 /*
1072  * If we cannot find a cookie in our cache, we suspect that this is
1073  * because it points to a deleted file, so we ask the server to return
1074  * whatever it thinks is the next entry. We then feed this to filldir.
1075  * If all goes well, we should then be able to find our way round the
1076  * cache on the next call to readdir_search_pagecache();
1077  *
1078  * NOTE: we cannot add the anonymous page to the pagecache because
1079  *	 the data it contains might not be page aligned. Besides,
1080  *	 we should already have a complete representation of the
1081  *	 directory in the page cache by the time we get here.
1082  */
1083 static int uncached_readdir(struct nfs_readdir_descriptor *desc)
1084 {
1085 	struct page	**arrays;
1086 	size_t		i, sz = 512;
1087 	__be32		verf[NFS_DIR_VERIFIER_SIZE];
1088 	int		status = -ENOMEM;
1089 
1090 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
1091 			(unsigned long long)desc->dir_cookie);
1092 
1093 	arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
1094 	if (!arrays)
1095 		goto out;
1096 	arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
1097 	if (!arrays[0])
1098 		goto out;
1099 
1100 	desc->page_index = 0;
1101 	desc->cache_entry_index = 0;
1102 	desc->last_cookie = desc->dir_cookie;
1103 	desc->duped = 0;
1104 	desc->page_index_max = 0;
1105 
1106 	status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1107 
1108 	for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
1109 		desc->page = arrays[i];
1110 		nfs_do_filldir(desc, verf);
1111 	}
1112 	desc->page = NULL;
1113 
1114 	/*
1115 	 * Grow the dtsize if we have to go back for more pages,
1116 	 * or shrink it if we're reading too many.
1117 	 */
1118 	if (!desc->eof) {
1119 		if (!desc->eob)
1120 			nfs_grow_dtsize(desc);
1121 		else if (desc->buffer_fills == 1 &&
1122 			 i < (desc->page_index_max >> 1))
1123 			nfs_shrink_dtsize(desc);
1124 	}
1125 
1126 	for (i = 0; i < sz && arrays[i]; i++)
1127 		nfs_readdir_page_array_free(arrays[i]);
1128 out:
1129 	desc->page_index_max = -1;
1130 	kfree(arrays);
1131 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
1132 	return status;
1133 }
1134 
1135 #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
1136 
1137 static void nfs_readdir_handle_cache_misses(struct inode *inode,
1138 					    struct nfs_readdir_descriptor *desc,
1139 					    pgoff_t page_index,
1140 					    unsigned int cache_misses)
1141 {
1142 	if (desc->ctx->pos == 0 ||
1143 	    cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD)
1144 		return;
1145 	invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1);
1146 }
1147 
1148 /* The file offset position represents the dirent entry number.  A
1149    last cookie cache takes care of the common case of reading the
1150    whole directory.
1151  */
1152 static int nfs_readdir(struct file *file, struct dir_context *ctx)
1153 {
1154 	struct dentry	*dentry = file_dentry(file);
1155 	struct inode	*inode = d_inode(dentry);
1156 	struct nfs_inode *nfsi = NFS_I(inode);
1157 	struct nfs_open_dir_context *dir_ctx = file->private_data;
1158 	struct nfs_readdir_descriptor *desc;
1159 	unsigned int cache_hits, cache_misses;
1160 	pgoff_t page_index;
1161 	int res;
1162 
1163 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
1164 			file, (long long)ctx->pos);
1165 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1166 
1167 	/*
1168 	 * ctx->pos points to the dirent entry number.
1169 	 * *desc->dir_cookie has the cookie for the next entry. We have
1170 	 * to either find the entry with the appropriate number or
1171 	 * revalidate the cookie.
1172 	 */
1173 	nfs_revalidate_mapping(inode, file->f_mapping);
1174 
1175 	res = -ENOMEM;
1176 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1177 	if (!desc)
1178 		goto out;
1179 	desc->file = file;
1180 	desc->ctx = ctx;
1181 	desc->page_index_max = -1;
1182 
1183 	spin_lock(&file->f_lock);
1184 	desc->dir_cookie = dir_ctx->dir_cookie;
1185 	desc->dup_cookie = dir_ctx->dup_cookie;
1186 	desc->duped = dir_ctx->duped;
1187 	page_index = dir_ctx->page_index;
1188 	desc->page_index = page_index;
1189 	desc->last_cookie = dir_ctx->last_cookie;
1190 	desc->attr_gencount = dir_ctx->attr_gencount;
1191 	desc->eof = dir_ctx->eof;
1192 	nfs_set_dtsize(desc, dir_ctx->dtsize);
1193 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
1194 	cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0);
1195 	cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0);
1196 	spin_unlock(&file->f_lock);
1197 
1198 	if (desc->eof) {
1199 		res = 0;
1200 		goto out_free;
1201 	}
1202 
1203 	desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses);
1204 	nfs_readdir_handle_cache_misses(inode, desc, page_index, cache_misses);
1205 
1206 	do {
1207 		res = readdir_search_pagecache(desc);
1208 
1209 		if (res == -EBADCOOKIE) {
1210 			res = 0;
1211 			/* This means either end of directory */
1212 			if (desc->dir_cookie && !desc->eof) {
1213 				/* Or that the server has 'lost' a cookie */
1214 				res = uncached_readdir(desc);
1215 				if (res == 0)
1216 					continue;
1217 				if (res == -EBADCOOKIE || res == -ENOTSYNC)
1218 					res = 0;
1219 			}
1220 			break;
1221 		}
1222 		if (res == -ETOOSMALL && desc->plus) {
1223 			nfs_zap_caches(inode);
1224 			desc->page_index = 0;
1225 			desc->plus = false;
1226 			desc->eof = false;
1227 			continue;
1228 		}
1229 		if (res < 0)
1230 			break;
1231 
1232 		nfs_do_filldir(desc, nfsi->cookieverf);
1233 		nfs_readdir_page_unlock_and_put_cached(desc);
1234 	} while (!desc->eob && !desc->eof);
1235 
1236 	spin_lock(&file->f_lock);
1237 	dir_ctx->dir_cookie = desc->dir_cookie;
1238 	dir_ctx->dup_cookie = desc->dup_cookie;
1239 	dir_ctx->last_cookie = desc->last_cookie;
1240 	dir_ctx->duped = desc->duped;
1241 	dir_ctx->attr_gencount = desc->attr_gencount;
1242 	dir_ctx->page_index = desc->page_index;
1243 	dir_ctx->eof = desc->eof;
1244 	dir_ctx->dtsize = desc->dtsize;
1245 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
1246 	spin_unlock(&file->f_lock);
1247 out_free:
1248 	kfree(desc);
1249 
1250 out:
1251 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
1252 	return res;
1253 }
1254 
1255 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1256 {
1257 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1258 
1259 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1260 			filp, offset, whence);
1261 
1262 	switch (whence) {
1263 	default:
1264 		return -EINVAL;
1265 	case SEEK_SET:
1266 		if (offset < 0)
1267 			return -EINVAL;
1268 		spin_lock(&filp->f_lock);
1269 		break;
1270 	case SEEK_CUR:
1271 		if (offset == 0)
1272 			return filp->f_pos;
1273 		spin_lock(&filp->f_lock);
1274 		offset += filp->f_pos;
1275 		if (offset < 0) {
1276 			spin_unlock(&filp->f_lock);
1277 			return -EINVAL;
1278 		}
1279 	}
1280 	if (offset != filp->f_pos) {
1281 		filp->f_pos = offset;
1282 		dir_ctx->page_index = 0;
1283 		if (!nfs_readdir_use_cookie(filp))
1284 			dir_ctx->dir_cookie = 0;
1285 		else
1286 			dir_ctx->dir_cookie = offset;
1287 		if (offset == 0)
1288 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
1289 		dir_ctx->duped = 0;
1290 		dir_ctx->eof = false;
1291 	}
1292 	spin_unlock(&filp->f_lock);
1293 	return offset;
1294 }
1295 
1296 /*
1297  * All directory operations under NFS are synchronous, so fsync()
1298  * is a dummy operation.
1299  */
1300 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1301 			 int datasync)
1302 {
1303 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
1304 
1305 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
1306 	return 0;
1307 }
1308 
1309 /**
1310  * nfs_force_lookup_revalidate - Mark the directory as having changed
1311  * @dir: pointer to directory inode
1312  *
1313  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1314  * full lookup on all child dentries of 'dir' whenever a change occurs
1315  * on the server that might have invalidated our dcache.
1316  *
1317  * Note that we reserve bit '0' as a tag to let us know when a dentry
1318  * was revalidated while holding a delegation on its inode.
1319  *
1320  * The caller should be holding dir->i_lock
1321  */
1322 void nfs_force_lookup_revalidate(struct inode *dir)
1323 {
1324 	NFS_I(dir)->cache_change_attribute += 2;
1325 }
1326 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1327 
1328 /**
1329  * nfs_verify_change_attribute - Detects NFS remote directory changes
1330  * @dir: pointer to parent directory inode
1331  * @verf: previously saved change attribute
1332  *
1333  * Return "false" if the verifiers doesn't match the change attribute.
1334  * This would usually indicate that the directory contents have changed on
1335  * the server, and that any dentries need revalidating.
1336  */
1337 static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1338 {
1339 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1340 }
1341 
1342 static void nfs_set_verifier_delegated(unsigned long *verf)
1343 {
1344 	*verf |= 1UL;
1345 }
1346 
1347 #if IS_ENABLED(CONFIG_NFS_V4)
1348 static void nfs_unset_verifier_delegated(unsigned long *verf)
1349 {
1350 	*verf &= ~1UL;
1351 }
1352 #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1353 
1354 static bool nfs_test_verifier_delegated(unsigned long verf)
1355 {
1356 	return verf & 1;
1357 }
1358 
1359 static bool nfs_verifier_is_delegated(struct dentry *dentry)
1360 {
1361 	return nfs_test_verifier_delegated(dentry->d_time);
1362 }
1363 
1364 static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1365 {
1366 	struct inode *inode = d_inode(dentry);
1367 	struct inode *dir = d_inode(dentry->d_parent);
1368 
1369 	if (!nfs_verify_change_attribute(dir, verf))
1370 		return;
1371 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1372 		nfs_set_verifier_delegated(&verf);
1373 	dentry->d_time = verf;
1374 }
1375 
1376 /**
1377  * nfs_set_verifier - save a parent directory verifier in the dentry
1378  * @dentry: pointer to dentry
1379  * @verf: verifier to save
1380  *
1381  * Saves the parent directory verifier in @dentry. If the inode has
1382  * a delegation, we also tag the dentry as having been revalidated
1383  * while holding a delegation so that we know we don't have to
1384  * look it up again after a directory change.
1385  */
1386 void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1387 {
1388 
1389 	spin_lock(&dentry->d_lock);
1390 	nfs_set_verifier_locked(dentry, verf);
1391 	spin_unlock(&dentry->d_lock);
1392 }
1393 EXPORT_SYMBOL_GPL(nfs_set_verifier);
1394 
1395 #if IS_ENABLED(CONFIG_NFS_V4)
1396 /**
1397  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1398  * @inode: pointer to inode
1399  *
1400  * Iterates through the dentries in the inode alias list and clears
1401  * the tag used to indicate that the dentry has been revalidated
1402  * while holding a delegation.
1403  * This function is intended for use when the delegation is being
1404  * returned or revoked.
1405  */
1406 void nfs_clear_verifier_delegated(struct inode *inode)
1407 {
1408 	struct dentry *alias;
1409 
1410 	if (!inode)
1411 		return;
1412 	spin_lock(&inode->i_lock);
1413 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1414 		spin_lock(&alias->d_lock);
1415 		nfs_unset_verifier_delegated(&alias->d_time);
1416 		spin_unlock(&alias->d_lock);
1417 	}
1418 	spin_unlock(&inode->i_lock);
1419 }
1420 EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1421 #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1422 
1423 static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
1424 {
1425 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
1426 	    d_really_is_negative(dentry))
1427 		return dentry->d_time == inode_peek_iversion_raw(dir);
1428 	return nfs_verify_change_attribute(dir, dentry->d_time);
1429 }
1430 
1431 /*
1432  * A check for whether or not the parent directory has changed.
1433  * In the case it has, we assume that the dentries are untrustworthy
1434  * and may need to be looked up again.
1435  * If rcu_walk prevents us from performing a full check, return 0.
1436  */
1437 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1438 			      int rcu_walk)
1439 {
1440 	if (IS_ROOT(dentry))
1441 		return 1;
1442 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1443 		return 0;
1444 	if (!nfs_dentry_verify_change(dir, dentry))
1445 		return 0;
1446 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
1447 	if (nfs_mapping_need_revalidate_inode(dir)) {
1448 		if (rcu_walk)
1449 			return 0;
1450 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1451 			return 0;
1452 	}
1453 	if (!nfs_dentry_verify_change(dir, dentry))
1454 		return 0;
1455 	return 1;
1456 }
1457 
1458 /*
1459  * Use intent information to check whether or not we're going to do
1460  * an O_EXCL create using this path component.
1461  */
1462 static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1463 {
1464 	if (NFS_PROTO(dir)->version == 2)
1465 		return 0;
1466 	return flags & LOOKUP_EXCL;
1467 }
1468 
1469 /*
1470  * Inode and filehandle revalidation for lookups.
1471  *
1472  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1473  * or if the intent information indicates that we're about to open this
1474  * particular file and the "nocto" mount flag is not set.
1475  *
1476  */
1477 static
1478 int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1479 {
1480 	struct nfs_server *server = NFS_SERVER(inode);
1481 	int ret;
1482 
1483 	if (IS_AUTOMOUNT(inode))
1484 		return 0;
1485 
1486 	if (flags & LOOKUP_OPEN) {
1487 		switch (inode->i_mode & S_IFMT) {
1488 		case S_IFREG:
1489 			/* A NFSv4 OPEN will revalidate later */
1490 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
1491 				goto out;
1492 			fallthrough;
1493 		case S_IFDIR:
1494 			if (server->flags & NFS_MOUNT_NOCTO)
1495 				break;
1496 			/* NFS close-to-open cache consistency validation */
1497 			goto out_force;
1498 		}
1499 	}
1500 
1501 	/* VFS wants an on-the-wire revalidation */
1502 	if (flags & LOOKUP_REVAL)
1503 		goto out_force;
1504 out:
1505 	if (inode->i_nlink > 0 ||
1506 	    (inode->i_nlink == 0 &&
1507 	     test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
1508 		return 0;
1509 	else
1510 		return -ESTALE;
1511 out_force:
1512 	if (flags & LOOKUP_RCU)
1513 		return -ECHILD;
1514 	ret = __nfs_revalidate_inode(server, inode);
1515 	if (ret != 0)
1516 		return ret;
1517 	goto out;
1518 }
1519 
1520 static void nfs_mark_dir_for_revalidate(struct inode *inode)
1521 {
1522 	spin_lock(&inode->i_lock);
1523 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
1524 	spin_unlock(&inode->i_lock);
1525 }
1526 
1527 /*
1528  * We judge how long we want to trust negative
1529  * dentries by looking at the parent inode mtime.
1530  *
1531  * If parent mtime has changed, we revalidate, else we wait for a
1532  * period corresponding to the parent's attribute cache timeout value.
1533  *
1534  * If LOOKUP_RCU prevents us from performing a full check, return 1
1535  * suggesting a reval is needed.
1536  *
1537  * Note that when creating a new file, or looking up a rename target,
1538  * then it shouldn't be necessary to revalidate a negative dentry.
1539  */
1540 static inline
1541 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1542 		       unsigned int flags)
1543 {
1544 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1545 		return 0;
1546 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1547 		return 1;
1548 	/* Case insensitive server? Revalidate negative dentries */
1549 	if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1550 		return 1;
1551 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
1552 }
1553 
1554 static int
1555 nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1556 			   struct inode *inode, int error)
1557 {
1558 	switch (error) {
1559 	case 1:
1560 		break;
1561 	case 0:
1562 		/*
1563 		 * We can't d_drop the root of a disconnected tree:
1564 		 * its d_hash is on the s_anon list and d_drop() would hide
1565 		 * it from shrink_dcache_for_unmount(), leading to busy
1566 		 * inodes on unmount and further oopses.
1567 		 */
1568 		if (inode && IS_ROOT(dentry))
1569 			error = 1;
1570 		break;
1571 	}
1572 	trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1573 	return error;
1574 }
1575 
1576 static int
1577 nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1578 			       unsigned int flags)
1579 {
1580 	int ret = 1;
1581 	if (nfs_neg_need_reval(dir, dentry, flags)) {
1582 		if (flags & LOOKUP_RCU)
1583 			return -ECHILD;
1584 		ret = 0;
1585 	}
1586 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1587 }
1588 
1589 static int
1590 nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1591 				struct inode *inode)
1592 {
1593 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1594 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1595 }
1596 
1597 static int
1598 nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
1599 			     struct inode *inode)
1600 {
1601 	struct nfs_fh *fhandle;
1602 	struct nfs_fattr *fattr;
1603 	unsigned long dir_verifier;
1604 	int ret;
1605 
1606 	ret = -ENOMEM;
1607 	fhandle = nfs_alloc_fhandle();
1608 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1609 	if (fhandle == NULL || fattr == NULL)
1610 		goto out;
1611 
1612 	dir_verifier = nfs_save_change_attribute(dir);
1613 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1614 	if (ret < 0) {
1615 		switch (ret) {
1616 		case -ESTALE:
1617 		case -ENOENT:
1618 			ret = 0;
1619 			break;
1620 		case -ETIMEDOUT:
1621 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1622 				ret = 1;
1623 		}
1624 		goto out;
1625 	}
1626 	ret = 0;
1627 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
1628 		goto out;
1629 	if (nfs_refresh_inode(inode, fattr) < 0)
1630 		goto out;
1631 
1632 	nfs_setsecurity(inode, fattr);
1633 	nfs_set_verifier(dentry, dir_verifier);
1634 
1635 	/* set a readdirplus hint that we had a cache miss */
1636 	nfs_lookup_advise_force_readdirplus(dir);
1637 	ret = 1;
1638 out:
1639 	nfs_free_fattr(fattr);
1640 	nfs_free_fhandle(fhandle);
1641 
1642 	/*
1643 	 * If the lookup failed despite the dentry change attribute being
1644 	 * a match, then we should revalidate the directory cache.
1645 	 */
1646 	if (!ret && nfs_dentry_verify_change(dir, dentry))
1647 		nfs_mark_dir_for_revalidate(dir);
1648 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1649 }
1650 
1651 /*
1652  * This is called every time the dcache has a lookup hit,
1653  * and we should check whether we can really trust that
1654  * lookup.
1655  *
1656  * NOTE! The hit can be a negative hit too, don't assume
1657  * we have an inode!
1658  *
1659  * If the parent directory is seen to have changed, we throw out the
1660  * cached dentry and do a new lookup.
1661  */
1662 static int
1663 nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1664 			 unsigned int flags)
1665 {
1666 	struct inode *inode;
1667 	int error;
1668 
1669 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1670 	inode = d_inode(dentry);
1671 
1672 	if (!inode)
1673 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
1674 
1675 	if (is_bad_inode(inode)) {
1676 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1677 				__func__, dentry);
1678 		goto out_bad;
1679 	}
1680 
1681 	if (nfs_verifier_is_delegated(dentry))
1682 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1683 
1684 	/* Force a full look up iff the parent directory has changed */
1685 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
1686 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1687 		error = nfs_lookup_verify_inode(inode, flags);
1688 		if (error) {
1689 			if (error == -ESTALE)
1690 				nfs_mark_dir_for_revalidate(dir);
1691 			goto out_bad;
1692 		}
1693 		goto out_valid;
1694 	}
1695 
1696 	if (flags & LOOKUP_RCU)
1697 		return -ECHILD;
1698 
1699 	if (NFS_STALE(inode))
1700 		goto out_bad;
1701 
1702 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1703 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
1704 out_valid:
1705 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1706 out_bad:
1707 	if (flags & LOOKUP_RCU)
1708 		return -ECHILD;
1709 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1710 }
1711 
1712 static int
1713 __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1714 			int (*reval)(struct inode *, struct dentry *, unsigned int))
1715 {
1716 	struct dentry *parent;
1717 	struct inode *dir;
1718 	int ret;
1719 
1720 	if (flags & LOOKUP_RCU) {
1721 		parent = READ_ONCE(dentry->d_parent);
1722 		dir = d_inode_rcu(parent);
1723 		if (!dir)
1724 			return -ECHILD;
1725 		ret = reval(dir, dentry, flags);
1726 		if (parent != READ_ONCE(dentry->d_parent))
1727 			return -ECHILD;
1728 	} else {
1729 		parent = dget_parent(dentry);
1730 		ret = reval(d_inode(parent), dentry, flags);
1731 		dput(parent);
1732 	}
1733 	return ret;
1734 }
1735 
1736 static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1737 {
1738 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1739 }
1740 
1741 /*
1742  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1743  * when we don't really care about the dentry name. This is called when a
1744  * pathwalk ends on a dentry that was not found via a normal lookup in the
1745  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1746  *
1747  * In this situation, we just want to verify that the inode itself is OK
1748  * since the dentry might have changed on the server.
1749  */
1750 static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1751 {
1752 	struct inode *inode = d_inode(dentry);
1753 	int error = 0;
1754 
1755 	/*
1756 	 * I believe we can only get a negative dentry here in the case of a
1757 	 * procfs-style symlink. Just assume it's correct for now, but we may
1758 	 * eventually need to do something more here.
1759 	 */
1760 	if (!inode) {
1761 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1762 				__func__, dentry);
1763 		return 1;
1764 	}
1765 
1766 	if (is_bad_inode(inode)) {
1767 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1768 				__func__, dentry);
1769 		return 0;
1770 	}
1771 
1772 	error = nfs_lookup_verify_inode(inode, flags);
1773 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1774 			__func__, inode->i_ino, error ? "invalid" : "valid");
1775 	return !error;
1776 }
1777 
1778 /*
1779  * This is called from dput() when d_count is going to 0.
1780  */
1781 static int nfs_dentry_delete(const struct dentry *dentry)
1782 {
1783 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1784 		dentry, dentry->d_flags);
1785 
1786 	/* Unhash any dentry with a stale inode */
1787 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
1788 		return 1;
1789 
1790 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1791 		/* Unhash it, so that ->d_iput() would be called */
1792 		return 1;
1793 	}
1794 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
1795 		/* Unhash it, so that ancestors of killed async unlink
1796 		 * files will be cleaned up during umount */
1797 		return 1;
1798 	}
1799 	return 0;
1800 
1801 }
1802 
1803 /* Ensure that we revalidate inode->i_nlink */
1804 static void nfs_drop_nlink(struct inode *inode)
1805 {
1806 	spin_lock(&inode->i_lock);
1807 	/* drop the inode if we're reasonably sure this is the last link */
1808 	if (inode->i_nlink > 0)
1809 		drop_nlink(inode);
1810 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1811 	nfs_set_cache_invalid(
1812 		inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
1813 			       NFS_INO_INVALID_NLINK);
1814 	spin_unlock(&inode->i_lock);
1815 }
1816 
1817 /*
1818  * Called when the dentry loses inode.
1819  * We use it to clean up silly-renamed files.
1820  */
1821 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1822 {
1823 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1824 		nfs_complete_unlink(dentry, inode);
1825 		nfs_drop_nlink(inode);
1826 	}
1827 	iput(inode);
1828 }
1829 
1830 static void nfs_d_release(struct dentry *dentry)
1831 {
1832 	/* free cached devname value, if it survived that far */
1833 	if (unlikely(dentry->d_fsdata)) {
1834 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1835 			WARN_ON(1);
1836 		else
1837 			kfree(dentry->d_fsdata);
1838 	}
1839 }
1840 
1841 const struct dentry_operations nfs_dentry_operations = {
1842 	.d_revalidate	= nfs_lookup_revalidate,
1843 	.d_weak_revalidate	= nfs_weak_revalidate,
1844 	.d_delete	= nfs_dentry_delete,
1845 	.d_iput		= nfs_dentry_iput,
1846 	.d_automount	= nfs_d_automount,
1847 	.d_release	= nfs_d_release,
1848 };
1849 EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1850 
1851 struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1852 {
1853 	struct dentry *res;
1854 	struct inode *inode = NULL;
1855 	struct nfs_fh *fhandle = NULL;
1856 	struct nfs_fattr *fattr = NULL;
1857 	unsigned long dir_verifier;
1858 	int error;
1859 
1860 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
1861 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1862 
1863 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1864 		return ERR_PTR(-ENAMETOOLONG);
1865 
1866 	/*
1867 	 * If we're doing an exclusive create, optimize away the lookup
1868 	 * but don't hash the dentry.
1869 	 */
1870 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1871 		return NULL;
1872 
1873 	res = ERR_PTR(-ENOMEM);
1874 	fhandle = nfs_alloc_fhandle();
1875 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1876 	if (fhandle == NULL || fattr == NULL)
1877 		goto out;
1878 
1879 	dir_verifier = nfs_save_change_attribute(dir);
1880 	trace_nfs_lookup_enter(dir, dentry, flags);
1881 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1882 	if (error == -ENOENT) {
1883 		if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1884 			dir_verifier = inode_peek_iversion_raw(dir);
1885 		goto no_entry;
1886 	}
1887 	if (error < 0) {
1888 		res = ERR_PTR(error);
1889 		goto out;
1890 	}
1891 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1892 	res = ERR_CAST(inode);
1893 	if (IS_ERR(res))
1894 		goto out;
1895 
1896 	/* Notify readdir to use READDIRPLUS */
1897 	nfs_lookup_advise_force_readdirplus(dir);
1898 
1899 no_entry:
1900 	res = d_splice_alias(inode, dentry);
1901 	if (res != NULL) {
1902 		if (IS_ERR(res))
1903 			goto out;
1904 		dentry = res;
1905 	}
1906 	nfs_set_verifier(dentry, dir_verifier);
1907 out:
1908 	trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1909 	nfs_free_fattr(fattr);
1910 	nfs_free_fhandle(fhandle);
1911 	return res;
1912 }
1913 EXPORT_SYMBOL_GPL(nfs_lookup);
1914 
1915 void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
1916 {
1917 	/* Case insensitive server? Revalidate dentries */
1918 	if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
1919 		d_prune_aliases(inode);
1920 }
1921 EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
1922 
1923 #if IS_ENABLED(CONFIG_NFS_V4)
1924 static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
1925 
1926 const struct dentry_operations nfs4_dentry_operations = {
1927 	.d_revalidate	= nfs4_lookup_revalidate,
1928 	.d_weak_revalidate	= nfs_weak_revalidate,
1929 	.d_delete	= nfs_dentry_delete,
1930 	.d_iput		= nfs_dentry_iput,
1931 	.d_automount	= nfs_d_automount,
1932 	.d_release	= nfs_d_release,
1933 };
1934 EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1935 
1936 static fmode_t flags_to_mode(int flags)
1937 {
1938 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1939 	if ((flags & O_ACCMODE) != O_WRONLY)
1940 		res |= FMODE_READ;
1941 	if ((flags & O_ACCMODE) != O_RDONLY)
1942 		res |= FMODE_WRITE;
1943 	return res;
1944 }
1945 
1946 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1947 {
1948 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1949 }
1950 
1951 static int do_open(struct inode *inode, struct file *filp)
1952 {
1953 	nfs_fscache_open_file(inode, filp);
1954 	return 0;
1955 }
1956 
1957 static int nfs_finish_open(struct nfs_open_context *ctx,
1958 			   struct dentry *dentry,
1959 			   struct file *file, unsigned open_flags)
1960 {
1961 	int err;
1962 
1963 	err = finish_open(file, dentry, do_open);
1964 	if (err)
1965 		goto out;
1966 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1967 		nfs_file_set_open_context(file, ctx);
1968 	else
1969 		err = -EOPENSTALE;
1970 out:
1971 	return err;
1972 }
1973 
1974 int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1975 		    struct file *file, unsigned open_flags,
1976 		    umode_t mode)
1977 {
1978 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1979 	struct nfs_open_context *ctx;
1980 	struct dentry *res;
1981 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1982 	struct inode *inode;
1983 	unsigned int lookup_flags = 0;
1984 	unsigned long dir_verifier;
1985 	bool switched = false;
1986 	int created = 0;
1987 	int err;
1988 
1989 	/* Expect a negative dentry */
1990 	BUG_ON(d_inode(dentry));
1991 
1992 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
1993 			dir->i_sb->s_id, dir->i_ino, dentry);
1994 
1995 	err = nfs_check_flags(open_flags);
1996 	if (err)
1997 		return err;
1998 
1999 	/* NFS only supports OPEN on regular files */
2000 	if ((open_flags & O_DIRECTORY)) {
2001 		if (!d_in_lookup(dentry)) {
2002 			/*
2003 			 * Hashed negative dentry with O_DIRECTORY: dentry was
2004 			 * revalidated and is fine, no need to perform lookup
2005 			 * again
2006 			 */
2007 			return -ENOENT;
2008 		}
2009 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
2010 		goto no_open;
2011 	}
2012 
2013 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
2014 		return -ENAMETOOLONG;
2015 
2016 	if (open_flags & O_CREAT) {
2017 		struct nfs_server *server = NFS_SERVER(dir);
2018 
2019 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
2020 			mode &= ~current_umask();
2021 
2022 		attr.ia_valid |= ATTR_MODE;
2023 		attr.ia_mode = mode;
2024 	}
2025 	if (open_flags & O_TRUNC) {
2026 		attr.ia_valid |= ATTR_SIZE;
2027 		attr.ia_size = 0;
2028 	}
2029 
2030 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
2031 		d_drop(dentry);
2032 		switched = true;
2033 		dentry = d_alloc_parallel(dentry->d_parent,
2034 					  &dentry->d_name, &wq);
2035 		if (IS_ERR(dentry))
2036 			return PTR_ERR(dentry);
2037 		if (unlikely(!d_in_lookup(dentry)))
2038 			return finish_no_open(file, dentry);
2039 	}
2040 
2041 	ctx = create_nfs_open_context(dentry, open_flags, file);
2042 	err = PTR_ERR(ctx);
2043 	if (IS_ERR(ctx))
2044 		goto out;
2045 
2046 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
2047 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
2048 	if (created)
2049 		file->f_mode |= FMODE_CREATED;
2050 	if (IS_ERR(inode)) {
2051 		err = PTR_ERR(inode);
2052 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2053 		put_nfs_open_context(ctx);
2054 		d_drop(dentry);
2055 		switch (err) {
2056 		case -ENOENT:
2057 			d_splice_alias(NULL, dentry);
2058 			if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
2059 				dir_verifier = inode_peek_iversion_raw(dir);
2060 			else
2061 				dir_verifier = nfs_save_change_attribute(dir);
2062 			nfs_set_verifier(dentry, dir_verifier);
2063 			break;
2064 		case -EISDIR:
2065 		case -ENOTDIR:
2066 			goto no_open;
2067 		case -ELOOP:
2068 			if (!(open_flags & O_NOFOLLOW))
2069 				goto no_open;
2070 			break;
2071 			/* case -EINVAL: */
2072 		default:
2073 			break;
2074 		}
2075 		goto out;
2076 	}
2077 
2078 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
2079 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2080 	put_nfs_open_context(ctx);
2081 out:
2082 	if (unlikely(switched)) {
2083 		d_lookup_done(dentry);
2084 		dput(dentry);
2085 	}
2086 	return err;
2087 
2088 no_open:
2089 	res = nfs_lookup(dir, dentry, lookup_flags);
2090 	if (!res) {
2091 		inode = d_inode(dentry);
2092 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2093 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2094 			res = ERR_PTR(-ENOTDIR);
2095 		else if (inode && S_ISREG(inode->i_mode))
2096 			res = ERR_PTR(-EOPENSTALE);
2097 	} else if (!IS_ERR(res)) {
2098 		inode = d_inode(res);
2099 		if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2100 		    !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2101 			dput(res);
2102 			res = ERR_PTR(-ENOTDIR);
2103 		} else if (inode && S_ISREG(inode->i_mode)) {
2104 			dput(res);
2105 			res = ERR_PTR(-EOPENSTALE);
2106 		}
2107 	}
2108 	if (switched) {
2109 		d_lookup_done(dentry);
2110 		if (!res)
2111 			res = dentry;
2112 		else
2113 			dput(dentry);
2114 	}
2115 	if (IS_ERR(res))
2116 		return PTR_ERR(res);
2117 	return finish_no_open(file, res);
2118 }
2119 EXPORT_SYMBOL_GPL(nfs_atomic_open);
2120 
2121 static int
2122 nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2123 			  unsigned int flags)
2124 {
2125 	struct inode *inode;
2126 
2127 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2128 		goto full_reval;
2129 	if (d_mountpoint(dentry))
2130 		goto full_reval;
2131 
2132 	inode = d_inode(dentry);
2133 
2134 	/* We can't create new files in nfs_open_revalidate(), so we
2135 	 * optimize away revalidation of negative dentries.
2136 	 */
2137 	if (inode == NULL)
2138 		goto full_reval;
2139 
2140 	if (nfs_verifier_is_delegated(dentry))
2141 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2142 
2143 	/* NFS only supports OPEN on regular files */
2144 	if (!S_ISREG(inode->i_mode))
2145 		goto full_reval;
2146 
2147 	/* We cannot do exclusive creation on a positive dentry */
2148 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2149 		goto reval_dentry;
2150 
2151 	/* Check if the directory changed */
2152 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2153 		goto reval_dentry;
2154 
2155 	/* Let f_op->open() actually open (and revalidate) the file */
2156 	return 1;
2157 reval_dentry:
2158 	if (flags & LOOKUP_RCU)
2159 		return -ECHILD;
2160 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
2161 
2162 full_reval:
2163 	return nfs_do_lookup_revalidate(dir, dentry, flags);
2164 }
2165 
2166 static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2167 {
2168 	return __nfs_lookup_revalidate(dentry, flags,
2169 			nfs4_do_lookup_revalidate);
2170 }
2171 
2172 #endif /* CONFIG_NFSV4 */
2173 
2174 struct dentry *
2175 nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2176 				struct nfs_fattr *fattr)
2177 {
2178 	struct dentry *parent = dget_parent(dentry);
2179 	struct inode *dir = d_inode(parent);
2180 	struct inode *inode;
2181 	struct dentry *d;
2182 	int error;
2183 
2184 	d_drop(dentry);
2185 
2186 	if (fhandle->size == 0) {
2187 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
2188 		if (error)
2189 			goto out_error;
2190 	}
2191 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2192 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
2193 		struct nfs_server *server = NFS_SB(dentry->d_sb);
2194 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
2195 				fattr, NULL);
2196 		if (error < 0)
2197 			goto out_error;
2198 	}
2199 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2200 	d = d_splice_alias(inode, dentry);
2201 out:
2202 	dput(parent);
2203 	return d;
2204 out_error:
2205 	d = ERR_PTR(error);
2206 	goto out;
2207 }
2208 EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2209 
2210 /*
2211  * Code common to create, mkdir, and mknod.
2212  */
2213 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2214 				struct nfs_fattr *fattr)
2215 {
2216 	struct dentry *d;
2217 
2218 	d = nfs_add_or_obtain(dentry, fhandle, fattr);
2219 	if (IS_ERR(d))
2220 		return PTR_ERR(d);
2221 
2222 	/* Callers don't care */
2223 	dput(d);
2224 	return 0;
2225 }
2226 EXPORT_SYMBOL_GPL(nfs_instantiate);
2227 
2228 /*
2229  * Following a failed create operation, we drop the dentry rather
2230  * than retain a negative dentry. This avoids a problem in the event
2231  * that the operation succeeded on the server, but an error in the
2232  * reply path made it appear to have failed.
2233  */
2234 int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2235 	       struct dentry *dentry, umode_t mode, bool excl)
2236 {
2237 	struct iattr attr;
2238 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
2239 	int error;
2240 
2241 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
2242 			dir->i_sb->s_id, dir->i_ino, dentry);
2243 
2244 	attr.ia_mode = mode;
2245 	attr.ia_valid = ATTR_MODE;
2246 
2247 	trace_nfs_create_enter(dir, dentry, open_flags);
2248 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
2249 	trace_nfs_create_exit(dir, dentry, open_flags, error);
2250 	if (error != 0)
2251 		goto out_err;
2252 	return 0;
2253 out_err:
2254 	d_drop(dentry);
2255 	return error;
2256 }
2257 EXPORT_SYMBOL_GPL(nfs_create);
2258 
2259 /*
2260  * See comments for nfs_proc_create regarding failed operations.
2261  */
2262 int
2263 nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2264 	  struct dentry *dentry, umode_t mode, dev_t rdev)
2265 {
2266 	struct iattr attr;
2267 	int status;
2268 
2269 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
2270 			dir->i_sb->s_id, dir->i_ino, dentry);
2271 
2272 	attr.ia_mode = mode;
2273 	attr.ia_valid = ATTR_MODE;
2274 
2275 	trace_nfs_mknod_enter(dir, dentry);
2276 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
2277 	trace_nfs_mknod_exit(dir, dentry, status);
2278 	if (status != 0)
2279 		goto out_err;
2280 	return 0;
2281 out_err:
2282 	d_drop(dentry);
2283 	return status;
2284 }
2285 EXPORT_SYMBOL_GPL(nfs_mknod);
2286 
2287 /*
2288  * See comments for nfs_proc_create regarding failed operations.
2289  */
2290 int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2291 	      struct dentry *dentry, umode_t mode)
2292 {
2293 	struct iattr attr;
2294 	int error;
2295 
2296 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
2297 			dir->i_sb->s_id, dir->i_ino, dentry);
2298 
2299 	attr.ia_valid = ATTR_MODE;
2300 	attr.ia_mode = mode | S_IFDIR;
2301 
2302 	trace_nfs_mkdir_enter(dir, dentry);
2303 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
2304 	trace_nfs_mkdir_exit(dir, dentry, error);
2305 	if (error != 0)
2306 		goto out_err;
2307 	return 0;
2308 out_err:
2309 	d_drop(dentry);
2310 	return error;
2311 }
2312 EXPORT_SYMBOL_GPL(nfs_mkdir);
2313 
2314 static void nfs_dentry_handle_enoent(struct dentry *dentry)
2315 {
2316 	if (simple_positive(dentry))
2317 		d_delete(dentry);
2318 }
2319 
2320 static void nfs_dentry_remove_handle_error(struct inode *dir,
2321 					   struct dentry *dentry, int error)
2322 {
2323 	switch (error) {
2324 	case -ENOENT:
2325 		d_delete(dentry);
2326 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2327 		break;
2328 	case 0:
2329 		nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
2330 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2331 	}
2332 }
2333 
2334 int nfs_rmdir(struct inode *dir, struct dentry *dentry)
2335 {
2336 	int error;
2337 
2338 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
2339 			dir->i_sb->s_id, dir->i_ino, dentry);
2340 
2341 	trace_nfs_rmdir_enter(dir, dentry);
2342 	if (d_really_is_positive(dentry)) {
2343 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2344 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2345 		/* Ensure the VFS deletes this inode */
2346 		switch (error) {
2347 		case 0:
2348 			clear_nlink(d_inode(dentry));
2349 			break;
2350 		case -ENOENT:
2351 			nfs_dentry_handle_enoent(dentry);
2352 		}
2353 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2354 	} else
2355 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2356 	nfs_dentry_remove_handle_error(dir, dentry, error);
2357 	trace_nfs_rmdir_exit(dir, dentry, error);
2358 
2359 	return error;
2360 }
2361 EXPORT_SYMBOL_GPL(nfs_rmdir);
2362 
2363 /*
2364  * Remove a file after making sure there are no pending writes,
2365  * and after checking that the file has only one user.
2366  *
2367  * We invalidate the attribute cache and free the inode prior to the operation
2368  * to avoid possible races if the server reuses the inode.
2369  */
2370 static int nfs_safe_remove(struct dentry *dentry)
2371 {
2372 	struct inode *dir = d_inode(dentry->d_parent);
2373 	struct inode *inode = d_inode(dentry);
2374 	int error = -EBUSY;
2375 
2376 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
2377 
2378 	/* If the dentry was sillyrenamed, we simply call d_delete() */
2379 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2380 		error = 0;
2381 		goto out;
2382 	}
2383 
2384 	trace_nfs_remove_enter(dir, dentry);
2385 	if (inode != NULL) {
2386 		error = NFS_PROTO(dir)->remove(dir, dentry);
2387 		if (error == 0)
2388 			nfs_drop_nlink(inode);
2389 	} else
2390 		error = NFS_PROTO(dir)->remove(dir, dentry);
2391 	if (error == -ENOENT)
2392 		nfs_dentry_handle_enoent(dentry);
2393 	trace_nfs_remove_exit(dir, dentry, error);
2394 out:
2395 	return error;
2396 }
2397 
2398 /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
2399  *  belongs to an active ".nfs..." file and we return -EBUSY.
2400  *
2401  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
2402  */
2403 int nfs_unlink(struct inode *dir, struct dentry *dentry)
2404 {
2405 	int error;
2406 	int need_rehash = 0;
2407 
2408 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
2409 		dir->i_ino, dentry);
2410 
2411 	trace_nfs_unlink_enter(dir, dentry);
2412 	spin_lock(&dentry->d_lock);
2413 	if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
2414 					     &NFS_I(d_inode(dentry))->flags)) {
2415 		spin_unlock(&dentry->d_lock);
2416 		/* Start asynchronous writeout of the inode */
2417 		write_inode_now(d_inode(dentry), 0);
2418 		error = nfs_sillyrename(dir, dentry);
2419 		goto out;
2420 	}
2421 	if (!d_unhashed(dentry)) {
2422 		__d_drop(dentry);
2423 		need_rehash = 1;
2424 	}
2425 	spin_unlock(&dentry->d_lock);
2426 	error = nfs_safe_remove(dentry);
2427 	nfs_dentry_remove_handle_error(dir, dentry, error);
2428 	if (need_rehash)
2429 		d_rehash(dentry);
2430 out:
2431 	trace_nfs_unlink_exit(dir, dentry, error);
2432 	return error;
2433 }
2434 EXPORT_SYMBOL_GPL(nfs_unlink);
2435 
2436 /*
2437  * To create a symbolic link, most file systems instantiate a new inode,
2438  * add a page to it containing the path, then write it out to the disk
2439  * using prepare_write/commit_write.
2440  *
2441  * Unfortunately the NFS client can't create the in-core inode first
2442  * because it needs a file handle to create an in-core inode (see
2443  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2444  * symlink request has completed on the server.
2445  *
2446  * So instead we allocate a raw page, copy the symname into it, then do
2447  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2448  * now have a new file handle and can instantiate an in-core NFS inode
2449  * and move the raw page into its mapping.
2450  */
2451 int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2452 		struct dentry *dentry, const char *symname)
2453 {
2454 	struct page *page;
2455 	char *kaddr;
2456 	struct iattr attr;
2457 	unsigned int pathlen = strlen(symname);
2458 	int error;
2459 
2460 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
2461 		dir->i_ino, dentry, symname);
2462 
2463 	if (pathlen > PAGE_SIZE)
2464 		return -ENAMETOOLONG;
2465 
2466 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2467 	attr.ia_valid = ATTR_MODE;
2468 
2469 	page = alloc_page(GFP_USER);
2470 	if (!page)
2471 		return -ENOMEM;
2472 
2473 	kaddr = page_address(page);
2474 	memcpy(kaddr, symname, pathlen);
2475 	if (pathlen < PAGE_SIZE)
2476 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2477 
2478 	trace_nfs_symlink_enter(dir, dentry);
2479 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
2480 	trace_nfs_symlink_exit(dir, dentry, error);
2481 	if (error != 0) {
2482 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2483 			dir->i_sb->s_id, dir->i_ino,
2484 			dentry, symname, error);
2485 		d_drop(dentry);
2486 		__free_page(page);
2487 		return error;
2488 	}
2489 
2490 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2491 
2492 	/*
2493 	 * No big deal if we can't add this page to the page cache here.
2494 	 * READLINK will get the missing page from the server if needed.
2495 	 */
2496 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2497 							GFP_KERNEL)) {
2498 		SetPageUptodate(page);
2499 		unlock_page(page);
2500 		/*
2501 		 * add_to_page_cache_lru() grabs an extra page refcount.
2502 		 * Drop it here to avoid leaking this page later.
2503 		 */
2504 		put_page(page);
2505 	} else
2506 		__free_page(page);
2507 
2508 	return 0;
2509 }
2510 EXPORT_SYMBOL_GPL(nfs_symlink);
2511 
2512 int
2513 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2514 {
2515 	struct inode *inode = d_inode(old_dentry);
2516 	int error;
2517 
2518 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2519 		old_dentry, dentry);
2520 
2521 	trace_nfs_link_enter(inode, dir, dentry);
2522 	d_drop(dentry);
2523 	if (S_ISREG(inode->i_mode))
2524 		nfs_sync_inode(inode);
2525 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2526 	if (error == 0) {
2527 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2528 		ihold(inode);
2529 		d_add(dentry, inode);
2530 	}
2531 	trace_nfs_link_exit(inode, dir, dentry, error);
2532 	return error;
2533 }
2534 EXPORT_SYMBOL_GPL(nfs_link);
2535 
2536 /*
2537  * RENAME
2538  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2539  * different file handle for the same inode after a rename (e.g. when
2540  * moving to a different directory). A fail-safe method to do so would
2541  * be to look up old_dir/old_name, create a link to new_dir/new_name and
2542  * rename the old file using the sillyrename stuff. This way, the original
2543  * file in old_dir will go away when the last process iput()s the inode.
2544  *
2545  * FIXED.
2546  *
2547  * It actually works quite well. One needs to have the possibility for
2548  * at least one ".nfs..." file in each directory the file ever gets
2549  * moved or linked to which happens automagically with the new
2550  * implementation that only depends on the dcache stuff instead of
2551  * using the inode layer
2552  *
2553  * Unfortunately, things are a little more complicated than indicated
2554  * above. For a cross-directory move, we want to make sure we can get
2555  * rid of the old inode after the operation.  This means there must be
2556  * no pending writes (if it's a file), and the use count must be 1.
2557  * If these conditions are met, we can drop the dentries before doing
2558  * the rename.
2559  */
2560 int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2561 	       struct dentry *old_dentry, struct inode *new_dir,
2562 	       struct dentry *new_dentry, unsigned int flags)
2563 {
2564 	struct inode *old_inode = d_inode(old_dentry);
2565 	struct inode *new_inode = d_inode(new_dentry);
2566 	struct dentry *dentry = NULL, *rehash = NULL;
2567 	struct rpc_task *task;
2568 	int error = -EBUSY;
2569 
2570 	if (flags)
2571 		return -EINVAL;
2572 
2573 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2574 		 old_dentry, new_dentry,
2575 		 d_count(new_dentry));
2576 
2577 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
2578 	/*
2579 	 * For non-directories, check whether the target is busy and if so,
2580 	 * make a copy of the dentry and then do a silly-rename. If the
2581 	 * silly-rename succeeds, the copied dentry is hashed and becomes
2582 	 * the new target.
2583 	 */
2584 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2585 		/*
2586 		 * To prevent any new references to the target during the
2587 		 * rename, we unhash the dentry in advance.
2588 		 */
2589 		if (!d_unhashed(new_dentry)) {
2590 			d_drop(new_dentry);
2591 			rehash = new_dentry;
2592 		}
2593 
2594 		if (d_count(new_dentry) > 2) {
2595 			int err;
2596 
2597 			/* copy the target dentry's name */
2598 			dentry = d_alloc(new_dentry->d_parent,
2599 					 &new_dentry->d_name);
2600 			if (!dentry)
2601 				goto out;
2602 
2603 			/* silly-rename the existing target ... */
2604 			err = nfs_sillyrename(new_dir, new_dentry);
2605 			if (err)
2606 				goto out;
2607 
2608 			new_dentry = dentry;
2609 			rehash = NULL;
2610 			new_inode = NULL;
2611 		}
2612 	}
2613 
2614 	if (S_ISREG(old_inode->i_mode))
2615 		nfs_sync_inode(old_inode);
2616 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
2617 	if (IS_ERR(task)) {
2618 		error = PTR_ERR(task);
2619 		goto out;
2620 	}
2621 
2622 	error = rpc_wait_for_completion_task(task);
2623 	if (error != 0) {
2624 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2625 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2626 		smp_wmb();
2627 	} else
2628 		error = task->tk_status;
2629 	rpc_put_task(task);
2630 	/* Ensure the inode attributes are revalidated */
2631 	if (error == 0) {
2632 		spin_lock(&old_inode->i_lock);
2633 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2634 		nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2635 							 NFS_INO_INVALID_CTIME |
2636 							 NFS_INO_REVAL_FORCED);
2637 		spin_unlock(&old_inode->i_lock);
2638 	}
2639 out:
2640 	if (rehash)
2641 		d_rehash(rehash);
2642 	trace_nfs_rename_exit(old_dir, old_dentry,
2643 			new_dir, new_dentry, error);
2644 	if (!error) {
2645 		if (new_inode != NULL)
2646 			nfs_drop_nlink(new_inode);
2647 		/*
2648 		 * The d_move() should be here instead of in an async RPC completion
2649 		 * handler because we need the proper locks to move the dentry.  If
2650 		 * we're interrupted by a signal, the async RPC completion handler
2651 		 * should mark the directories for revalidation.
2652 		 */
2653 		d_move(old_dentry, new_dentry);
2654 		nfs_set_verifier(old_dentry,
2655 					nfs_save_change_attribute(new_dir));
2656 	} else if (error == -ENOENT)
2657 		nfs_dentry_handle_enoent(old_dentry);
2658 
2659 	/* new dentry created? */
2660 	if (dentry)
2661 		dput(dentry);
2662 	return error;
2663 }
2664 EXPORT_SYMBOL_GPL(nfs_rename);
2665 
2666 static DEFINE_SPINLOCK(nfs_access_lru_lock);
2667 static LIST_HEAD(nfs_access_lru_list);
2668 static atomic_long_t nfs_access_nr_entries;
2669 
2670 static unsigned long nfs_access_max_cachesize = 4*1024*1024;
2671 module_param(nfs_access_max_cachesize, ulong, 0644);
2672 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2673 
2674 static void nfs_access_free_entry(struct nfs_access_entry *entry)
2675 {
2676 	put_group_info(entry->group_info);
2677 	kfree_rcu(entry, rcu_head);
2678 	smp_mb__before_atomic();
2679 	atomic_long_dec(&nfs_access_nr_entries);
2680 	smp_mb__after_atomic();
2681 }
2682 
2683 static void nfs_access_free_list(struct list_head *head)
2684 {
2685 	struct nfs_access_entry *cache;
2686 
2687 	while (!list_empty(head)) {
2688 		cache = list_entry(head->next, struct nfs_access_entry, lru);
2689 		list_del(&cache->lru);
2690 		nfs_access_free_entry(cache);
2691 	}
2692 }
2693 
2694 static unsigned long
2695 nfs_do_access_cache_scan(unsigned int nr_to_scan)
2696 {
2697 	LIST_HEAD(head);
2698 	struct nfs_inode *nfsi, *next;
2699 	struct nfs_access_entry *cache;
2700 	long freed = 0;
2701 
2702 	spin_lock(&nfs_access_lru_lock);
2703 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2704 		struct inode *inode;
2705 
2706 		if (nr_to_scan-- == 0)
2707 			break;
2708 		inode = &nfsi->vfs_inode;
2709 		spin_lock(&inode->i_lock);
2710 		if (list_empty(&nfsi->access_cache_entry_lru))
2711 			goto remove_lru_entry;
2712 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2713 				struct nfs_access_entry, lru);
2714 		list_move(&cache->lru, &head);
2715 		rb_erase(&cache->rb_node, &nfsi->access_cache);
2716 		freed++;
2717 		if (!list_empty(&nfsi->access_cache_entry_lru))
2718 			list_move_tail(&nfsi->access_cache_inode_lru,
2719 					&nfs_access_lru_list);
2720 		else {
2721 remove_lru_entry:
2722 			list_del_init(&nfsi->access_cache_inode_lru);
2723 			smp_mb__before_atomic();
2724 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
2725 			smp_mb__after_atomic();
2726 		}
2727 		spin_unlock(&inode->i_lock);
2728 	}
2729 	spin_unlock(&nfs_access_lru_lock);
2730 	nfs_access_free_list(&head);
2731 	return freed;
2732 }
2733 
2734 unsigned long
2735 nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2736 {
2737 	int nr_to_scan = sc->nr_to_scan;
2738 	gfp_t gfp_mask = sc->gfp_mask;
2739 
2740 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2741 		return SHRINK_STOP;
2742 	return nfs_do_access_cache_scan(nr_to_scan);
2743 }
2744 
2745 
2746 unsigned long
2747 nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2748 {
2749 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2750 }
2751 
2752 static void
2753 nfs_access_cache_enforce_limit(void)
2754 {
2755 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2756 	unsigned long diff;
2757 	unsigned int nr_to_scan;
2758 
2759 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2760 		return;
2761 	nr_to_scan = 100;
2762 	diff = nr_entries - nfs_access_max_cachesize;
2763 	if (diff < nr_to_scan)
2764 		nr_to_scan = diff;
2765 	nfs_do_access_cache_scan(nr_to_scan);
2766 }
2767 
2768 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
2769 {
2770 	struct rb_root *root_node = &nfsi->access_cache;
2771 	struct rb_node *n;
2772 	struct nfs_access_entry *entry;
2773 
2774 	/* Unhook entries from the cache */
2775 	while ((n = rb_first(root_node)) != NULL) {
2776 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
2777 		rb_erase(n, root_node);
2778 		list_move(&entry->lru, head);
2779 	}
2780 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
2781 }
2782 
2783 void nfs_access_zap_cache(struct inode *inode)
2784 {
2785 	LIST_HEAD(head);
2786 
2787 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2788 		return;
2789 	/* Remove from global LRU init */
2790 	spin_lock(&nfs_access_lru_lock);
2791 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2792 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2793 
2794 	spin_lock(&inode->i_lock);
2795 	__nfs_access_zap_cache(NFS_I(inode), &head);
2796 	spin_unlock(&inode->i_lock);
2797 	spin_unlock(&nfs_access_lru_lock);
2798 	nfs_access_free_list(&head);
2799 }
2800 EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
2801 
2802 static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
2803 {
2804 	struct group_info *ga, *gb;
2805 	int g;
2806 
2807 	if (uid_lt(a->fsuid, b->fsuid))
2808 		return -1;
2809 	if (uid_gt(a->fsuid, b->fsuid))
2810 		return 1;
2811 
2812 	if (gid_lt(a->fsgid, b->fsgid))
2813 		return -1;
2814 	if (gid_gt(a->fsgid, b->fsgid))
2815 		return 1;
2816 
2817 	ga = a->group_info;
2818 	gb = b->group_info;
2819 	if (ga == gb)
2820 		return 0;
2821 	if (ga == NULL)
2822 		return -1;
2823 	if (gb == NULL)
2824 		return 1;
2825 	if (ga->ngroups < gb->ngroups)
2826 		return -1;
2827 	if (ga->ngroups > gb->ngroups)
2828 		return 1;
2829 
2830 	for (g = 0; g < ga->ngroups; g++) {
2831 		if (gid_lt(ga->gid[g], gb->gid[g]))
2832 			return -1;
2833 		if (gid_gt(ga->gid[g], gb->gid[g]))
2834 			return 1;
2835 	}
2836 	return 0;
2837 }
2838 
2839 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
2840 {
2841 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2842 
2843 	while (n != NULL) {
2844 		struct nfs_access_entry *entry =
2845 			rb_entry(n, struct nfs_access_entry, rb_node);
2846 		int cmp = access_cmp(cred, entry);
2847 
2848 		if (cmp < 0)
2849 			n = n->rb_left;
2850 		else if (cmp > 0)
2851 			n = n->rb_right;
2852 		else
2853 			return entry;
2854 	}
2855 	return NULL;
2856 }
2857 
2858 static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
2859 {
2860 	struct nfs_inode *nfsi = NFS_I(inode);
2861 	struct nfs_access_entry *cache;
2862 	bool retry = true;
2863 	int err;
2864 
2865 	spin_lock(&inode->i_lock);
2866 	for(;;) {
2867 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2868 			goto out_zap;
2869 		cache = nfs_access_search_rbtree(inode, cred);
2870 		err = -ENOENT;
2871 		if (cache == NULL)
2872 			goto out;
2873 		/* Found an entry, is our attribute cache valid? */
2874 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2875 			break;
2876 		if (!retry)
2877 			break;
2878 		err = -ECHILD;
2879 		if (!may_block)
2880 			goto out;
2881 		spin_unlock(&inode->i_lock);
2882 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2883 		if (err)
2884 			return err;
2885 		spin_lock(&inode->i_lock);
2886 		retry = false;
2887 	}
2888 	*mask = cache->mask;
2889 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
2890 	err = 0;
2891 out:
2892 	spin_unlock(&inode->i_lock);
2893 	return err;
2894 out_zap:
2895 	spin_unlock(&inode->i_lock);
2896 	nfs_access_zap_cache(inode);
2897 	return -ENOENT;
2898 }
2899 
2900 static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2901 {
2902 	/* Only check the most recently returned cache entry,
2903 	 * but do it without locking.
2904 	 */
2905 	struct nfs_inode *nfsi = NFS_I(inode);
2906 	struct nfs_access_entry *cache;
2907 	int err = -ECHILD;
2908 	struct list_head *lh;
2909 
2910 	rcu_read_lock();
2911 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2912 		goto out;
2913 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2914 	cache = list_entry(lh, struct nfs_access_entry, lru);
2915 	if (lh == &nfsi->access_cache_entry_lru ||
2916 	    access_cmp(cred, cache) != 0)
2917 		cache = NULL;
2918 	if (cache == NULL)
2919 		goto out;
2920 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2921 		goto out;
2922 	*mask = cache->mask;
2923 	err = 0;
2924 out:
2925 	rcu_read_unlock();
2926 	return err;
2927 }
2928 
2929 int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2930 			  u32 *mask, bool may_block)
2931 {
2932 	int status;
2933 
2934 	status = nfs_access_get_cached_rcu(inode, cred, mask);
2935 	if (status != 0)
2936 		status = nfs_access_get_cached_locked(inode, cred, mask,
2937 		    may_block);
2938 
2939 	return status;
2940 }
2941 EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2942 
2943 static void nfs_access_add_rbtree(struct inode *inode,
2944 				  struct nfs_access_entry *set,
2945 				  const struct cred *cred)
2946 {
2947 	struct nfs_inode *nfsi = NFS_I(inode);
2948 	struct rb_root *root_node = &nfsi->access_cache;
2949 	struct rb_node **p = &root_node->rb_node;
2950 	struct rb_node *parent = NULL;
2951 	struct nfs_access_entry *entry;
2952 	int cmp;
2953 
2954 	spin_lock(&inode->i_lock);
2955 	while (*p != NULL) {
2956 		parent = *p;
2957 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2958 		cmp = access_cmp(cred, entry);
2959 
2960 		if (cmp < 0)
2961 			p = &parent->rb_left;
2962 		else if (cmp > 0)
2963 			p = &parent->rb_right;
2964 		else
2965 			goto found;
2966 	}
2967 	rb_link_node(&set->rb_node, parent, p);
2968 	rb_insert_color(&set->rb_node, root_node);
2969 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2970 	spin_unlock(&inode->i_lock);
2971 	return;
2972 found:
2973 	rb_replace_node(parent, &set->rb_node, root_node);
2974 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2975 	list_del(&entry->lru);
2976 	spin_unlock(&inode->i_lock);
2977 	nfs_access_free_entry(entry);
2978 }
2979 
2980 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
2981 			  const struct cred *cred)
2982 {
2983 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2984 	if (cache == NULL)
2985 		return;
2986 	RB_CLEAR_NODE(&cache->rb_node);
2987 	cache->fsuid = cred->fsuid;
2988 	cache->fsgid = cred->fsgid;
2989 	cache->group_info = get_group_info(cred->group_info);
2990 	cache->mask = set->mask;
2991 
2992 	/* The above field assignments must be visible
2993 	 * before this item appears on the lru.  We cannot easily
2994 	 * use rcu_assign_pointer, so just force the memory barrier.
2995 	 */
2996 	smp_wmb();
2997 	nfs_access_add_rbtree(inode, cache, cred);
2998 
2999 	/* Update accounting */
3000 	smp_mb__before_atomic();
3001 	atomic_long_inc(&nfs_access_nr_entries);
3002 	smp_mb__after_atomic();
3003 
3004 	/* Add inode to global LRU list */
3005 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
3006 		spin_lock(&nfs_access_lru_lock);
3007 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
3008 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
3009 					&nfs_access_lru_list);
3010 		spin_unlock(&nfs_access_lru_lock);
3011 	}
3012 	nfs_access_cache_enforce_limit();
3013 }
3014 EXPORT_SYMBOL_GPL(nfs_access_add_cache);
3015 
3016 #define NFS_MAY_READ (NFS_ACCESS_READ)
3017 #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
3018 		NFS_ACCESS_EXTEND | \
3019 		NFS_ACCESS_DELETE)
3020 #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
3021 		NFS_ACCESS_EXTEND)
3022 #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
3023 #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
3024 #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
3025 static int
3026 nfs_access_calc_mask(u32 access_result, umode_t umode)
3027 {
3028 	int mask = 0;
3029 
3030 	if (access_result & NFS_MAY_READ)
3031 		mask |= MAY_READ;
3032 	if (S_ISDIR(umode)) {
3033 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
3034 			mask |= MAY_WRITE;
3035 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
3036 			mask |= MAY_EXEC;
3037 	} else if (S_ISREG(umode)) {
3038 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
3039 			mask |= MAY_WRITE;
3040 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
3041 			mask |= MAY_EXEC;
3042 	} else if (access_result & NFS_MAY_WRITE)
3043 			mask |= MAY_WRITE;
3044 	return mask;
3045 }
3046 
3047 void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
3048 {
3049 	entry->mask = access_result;
3050 }
3051 EXPORT_SYMBOL_GPL(nfs_access_set_mask);
3052 
3053 static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
3054 {
3055 	struct nfs_access_entry cache;
3056 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3057 	int cache_mask = -1;
3058 	int status;
3059 
3060 	trace_nfs_access_enter(inode);
3061 
3062 	status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
3063 	if (status == 0)
3064 		goto out_cached;
3065 
3066 	status = -ECHILD;
3067 	if (!may_block)
3068 		goto out;
3069 
3070 	/*
3071 	 * Determine which access bits we want to ask for...
3072 	 */
3073 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
3074 		     nfs_access_xattr_mask(NFS_SERVER(inode));
3075 	if (S_ISDIR(inode->i_mode))
3076 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
3077 	else
3078 		cache.mask |= NFS_ACCESS_EXECUTE;
3079 	status = NFS_PROTO(inode)->access(inode, &cache, cred);
3080 	if (status != 0) {
3081 		if (status == -ESTALE) {
3082 			if (!S_ISDIR(inode->i_mode))
3083 				nfs_set_inode_stale(inode);
3084 			else
3085 				nfs_zap_caches(inode);
3086 		}
3087 		goto out;
3088 	}
3089 	nfs_access_add_cache(inode, &cache, cred);
3090 out_cached:
3091 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3092 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3093 		status = -EACCES;
3094 out:
3095 	trace_nfs_access_exit(inode, mask, cache_mask, status);
3096 	return status;
3097 }
3098 
3099 static int nfs_open_permission_mask(int openflags)
3100 {
3101 	int mask = 0;
3102 
3103 	if (openflags & __FMODE_EXEC) {
3104 		/* ONLY check exec rights */
3105 		mask = MAY_EXEC;
3106 	} else {
3107 		if ((openflags & O_ACCMODE) != O_WRONLY)
3108 			mask |= MAY_READ;
3109 		if ((openflags & O_ACCMODE) != O_RDONLY)
3110 			mask |= MAY_WRITE;
3111 	}
3112 
3113 	return mask;
3114 }
3115 
3116 int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3117 {
3118 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3119 }
3120 EXPORT_SYMBOL_GPL(nfs_may_open);
3121 
3122 static int nfs_execute_ok(struct inode *inode, int mask)
3123 {
3124 	struct nfs_server *server = NFS_SERVER(inode);
3125 	int ret = 0;
3126 
3127 	if (S_ISDIR(inode->i_mode))
3128 		return 0;
3129 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
3130 		if (mask & MAY_NOT_BLOCK)
3131 			return -ECHILD;
3132 		ret = __nfs_revalidate_inode(server, inode);
3133 	}
3134 	if (ret == 0 && !execute_ok(inode))
3135 		ret = -EACCES;
3136 	return ret;
3137 }
3138 
3139 int nfs_permission(struct user_namespace *mnt_userns,
3140 		   struct inode *inode,
3141 		   int mask)
3142 {
3143 	const struct cred *cred = current_cred();
3144 	int res = 0;
3145 
3146 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
3147 
3148 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
3149 		goto out;
3150 	/* Is this sys_access() ? */
3151 	if (mask & (MAY_ACCESS | MAY_CHDIR))
3152 		goto force_lookup;
3153 
3154 	switch (inode->i_mode & S_IFMT) {
3155 		case S_IFLNK:
3156 			goto out;
3157 		case S_IFREG:
3158 			if ((mask & MAY_OPEN) &&
3159 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3160 				return 0;
3161 			break;
3162 		case S_IFDIR:
3163 			/*
3164 			 * Optimize away all write operations, since the server
3165 			 * will check permissions when we perform the op.
3166 			 */
3167 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
3168 				goto out;
3169 	}
3170 
3171 force_lookup:
3172 	if (!NFS_PROTO(inode)->access)
3173 		goto out_notsup;
3174 
3175 	res = nfs_do_access(inode, cred, mask);
3176 out:
3177 	if (!res && (mask & MAY_EXEC))
3178 		res = nfs_execute_ok(inode, mask);
3179 
3180 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
3181 		inode->i_sb->s_id, inode->i_ino, mask, res);
3182 	return res;
3183 out_notsup:
3184 	if (mask & MAY_NOT_BLOCK)
3185 		return -ECHILD;
3186 
3187 	res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3188 						  NFS_INO_INVALID_OTHER);
3189 	if (res == 0)
3190 		res = generic_permission(&init_user_ns, inode, mask);
3191 	goto out;
3192 }
3193 EXPORT_SYMBOL_GPL(nfs_permission);
3194