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