1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1992 Rick Sladkey
7 * nfs directory handling functions
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
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>
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>
43 #include "delegation.h"
50 /* #define NFS_DEBUG_VERBOSE 1 */
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*);
59 const struct file_operations nfs_dir_operations = {
60 .llseek = nfs_llseek_dir,
61 .read = generic_read_dir,
62 .iterate_shared = nfs_readdir,
64 .release = nfs_closedir,
65 .fsync = nfs_fsync_dir,
68 const struct address_space_operations nfs_dir_aops = {
69 .freepage = nfs_readdir_clear_array,
72 #define NFS_INIT_DTSIZE PAGE_SIZE
74 static struct nfs_open_dir_context *
75 alloc_nfs_open_dir_context(struct inode *dir)
77 struct nfs_inode *nfsi = NFS_I(dir);
78 struct nfs_open_dir_context *ctx;
80 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
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);
95 return ERR_PTR(-ENOMEM);
98 static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
100 spin_lock(&dir->i_lock);
101 list_del_rcu(&ctx->list);
102 spin_unlock(&dir->i_lock);
103 kfree_rcu(ctx, rcu_head);
110 nfs_opendir(struct inode *inode, struct file *filp)
113 struct nfs_open_dir_context *ctx;
115 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
117 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
119 ctx = alloc_nfs_open_dir_context(inode);
124 filp->private_data = ctx;
130 nfs_closedir(struct inode *inode, struct file *filp)
132 put_nfs_open_dir_context(file_inode(filp), filp->private_data);
136 struct nfs_cache_array_entry {
140 unsigned int name_len;
141 unsigned char d_type;
144 struct nfs_cache_array {
148 unsigned char page_full : 1,
150 cookies_are_ordered : 1;
151 struct nfs_cache_array_entry array[];
154 struct nfs_readdir_descriptor {
157 struct dir_context *ctx;
159 pgoff_t page_index_max;
163 loff_t current_index;
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;
180 static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
182 struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
183 unsigned int maxsize = server->dtsize;
187 if (sz < NFS_MIN_FILE_IO_SIZE)
188 sz = NFS_MIN_FILE_IO_SIZE;
192 static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
194 nfs_set_dtsize(desc, desc->dtsize >> 1);
197 static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
199 nfs_set_dtsize(desc, desc->dtsize << 1);
202 static void nfs_readdir_array_init(struct nfs_cache_array *array)
204 memset(array, 0, sizeof(struct nfs_cache_array));
207 static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie,
210 struct nfs_cache_array *array;
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);
221 * we are freeing strings created by nfs_add_to_readdir_array()
224 void nfs_readdir_clear_array(struct page *page)
226 struct nfs_cache_array *array;
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);
237 nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
239 struct page *page = alloc_page(gfp_flags);
241 nfs_readdir_page_init_array(page, last_cookie, 0);
245 static void nfs_readdir_page_array_free(struct page *page)
248 nfs_readdir_clear_array(page);
253 static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
255 array->page_is_eof = 1;
256 array->page_full = 1;
259 static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
261 return array->page_full;
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()
269 static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
271 const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
274 * Avoid a kmemleak false positive. The pointer to the name is stored
275 * in a page cache page which kmemleak does not scan.
278 kmemleak_not_leak(ret);
282 static size_t nfs_readdir_array_maxentries(void)
284 return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
285 sizeof(struct nfs_cache_array_entry);
289 * Check that the next array entry lies entirely within the page bounds
291 static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
293 if (array->page_full)
295 if (array->size == nfs_readdir_array_maxentries()) {
296 array->page_full = 1;
303 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
305 struct nfs_cache_array *array;
306 struct nfs_cache_array_entry *cache_entry;
310 name = nfs_readdir_copy_name(entry->name, entry->len);
314 array = kmap_atomic(page);
315 ret = nfs_readdir_array_can_expand(array);
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;
332 nfs_readdir_array_set_eof(array);
334 kunmap_atomic(array);
338 static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie,
341 struct nfs_cache_array *array = kmap_atomic(page);
344 if (array->change_attr != change_attr)
346 if (array->size > 0 && array->array[0].cookie != last_cookie)
348 kunmap_atomic(array);
352 static void nfs_readdir_page_unlock_and_put(struct page *page)
358 static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
359 pgoff_t index, u64 last_cookie)
364 page = grab_cache_page(mapping, index);
367 change_attr = inode_peek_iversion_raw(mapping->host);
368 if (PageUptodate(page)) {
369 if (nfs_readdir_page_validate(page, last_cookie, change_attr))
371 nfs_readdir_clear_array(page);
373 nfs_readdir_page_init_array(page, last_cookie, change_attr);
374 SetPageUptodate(page);
378 static loff_t nfs_readdir_page_offset(struct page *page)
380 return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries();
383 static u64 nfs_readdir_page_last_cookie(struct page *page)
385 struct nfs_cache_array *array;
388 array = kmap_atomic(page);
389 ret = array->last_cookie;
390 kunmap_atomic(array);
394 static bool nfs_readdir_page_needs_filling(struct page *page)
396 struct nfs_cache_array *array;
399 array = kmap_atomic(page);
400 ret = !nfs_readdir_array_is_full(array);
401 kunmap_atomic(array);
405 static void nfs_readdir_page_set_eof(struct page *page)
407 struct nfs_cache_array *array;
409 array = kmap_atomic(page);
410 nfs_readdir_array_set_eof(array);
411 kunmap_atomic(array);
414 static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
415 pgoff_t index, u64 cookie)
419 page = nfs_readdir_page_get_locked(mapping, index, cookie);
421 if (nfs_readdir_page_last_cookie(page) == cookie)
423 nfs_readdir_page_unlock_and_put(page);
429 int is_32bit_api(void)
432 return in_compat_syscall();
434 return (BITS_PER_LONG == 32);
439 bool nfs_readdir_use_cookie(const struct file *filp)
441 if ((filp->f_mode & FMODE_32BITHASH) ||
442 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
447 static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
448 struct nfs_readdir_descriptor *desc)
450 if (array->page_full) {
451 desc->last_cookie = array->last_cookie;
452 desc->current_index += array->size;
453 desc->cache_entry_index = 0;
456 desc->last_cookie = array->array[0].cookie;
459 static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
460 struct nfs_readdir_descriptor *desc)
462 loff_t diff = desc->ctx->pos - desc->current_index;
467 if (diff >= array->size) {
468 if (array->page_is_eof)
470 nfs_readdir_seek_next_array(array, desc);
474 index = (unsigned int)diff;
475 desc->dir_cookie = array->array[index].cookie;
476 desc->cache_entry_index = index;
483 static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
486 if (!array->cookies_are_ordered)
488 /* Optimisation for monotonically increasing cookies */
489 if (cookie >= array->last_cookie)
491 if (array->size && cookie < array->array[0].cookie)
496 static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
497 struct nfs_readdir_descriptor *desc)
501 int status = -EAGAIN;
503 if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
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));
510 new_pos = nfs_readdir_page_offset(desc->page) + i;
511 if (desc->attr_gencount != nfsi->attr_gencount) {
513 desc->attr_gencount = nfsi->attr_gencount;
514 } else if (new_pos < desc->prev_index) {
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);
526 desc->dup_cookie = desc->dir_cookie;
529 if (nfs_readdir_use_cookie(desc->file))
530 desc->ctx->pos = desc->dir_cookie;
532 desc->ctx->pos = new_pos;
533 desc->prev_index = new_pos;
534 desc->cache_entry_index = i;
539 if (array->page_is_eof) {
540 status = -EBADCOOKIE;
541 if (desc->dir_cookie == array->last_cookie)
544 nfs_readdir_seek_next_array(array, desc);
549 static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
551 struct nfs_cache_array *array;
554 array = kmap_atomic(desc->page);
556 if (desc->dir_cookie == 0)
557 status = nfs_readdir_search_for_pos(array, desc);
559 status = nfs_readdir_search_for_cookie(array, desc);
561 kunmap_atomic(array);
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,
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,
581 struct nfs_readdir_res res = {
584 unsigned long timestamp, gencount;
589 gencount = nfs_inc_attr_generation_counter();
590 desc->dir_verifier = nfs_save_change_attribute(inode);
591 error = NFS_PROTO(inode)->readdir(&arg, &res);
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;
601 desc->timestamp = timestamp;
602 desc->gencount = gencount;
607 static int xdr_decode(struct nfs_readdir_descriptor *desc,
608 struct nfs_entry *entry, struct xdr_stream *xdr)
610 struct inode *inode = file_inode(desc->file);
613 error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
616 entry->fattr->time_start = desc->timestamp;
617 entry->fattr->gencount = desc->gencount;
621 /* Match file and dirent using either filehandle or fileid
622 * Note: caller is responsible for checking the fsid
625 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
628 struct nfs_inode *nfsi;
630 if (d_really_is_negative(dentry))
633 inode = d_inode(dentry);
634 if (is_bad_inode(inode) || NFS_STALE(inode))
638 if (entry->fattr->fileid != nfsi->fileid)
640 if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
645 #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL)
647 static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx,
648 unsigned int cache_hits,
649 unsigned int cache_misses)
651 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
654 cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD)
660 * This function is called by the getattr code to request the
661 * use of readdirplus to accelerate any future lookups in the same
664 void nfs_readdir_record_entry_cache_hit(struct inode *dir)
666 struct nfs_inode *nfsi = NFS_I(dir);
667 struct nfs_open_dir_context *ctx;
669 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
670 S_ISDIR(dir->i_mode)) {
672 list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
673 atomic_inc(&ctx->cache_hits);
679 * This function is mainly for use by nfs_getattr().
681 * If this is an 'ls -l', we want to force use of readdirplus.
683 void nfs_readdir_record_entry_cache_miss(struct inode *dir)
685 struct nfs_inode *nfsi = NFS_I(dir);
686 struct nfs_open_dir_context *ctx;
688 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
689 S_ISDIR(dir->i_mode)) {
691 list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
692 atomic_inc(&ctx->cache_misses);
697 static void nfs_lookup_advise_force_readdirplus(struct inode *dir,
700 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
702 if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL))
704 nfs_readdir_record_entry_cache_miss(dir);
708 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
709 unsigned long dir_verifier)
711 struct qstr filename = QSTR_INIT(entry->name, entry->len);
712 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
713 struct dentry *dentry;
714 struct dentry *alias;
718 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
720 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
722 if (filename.len == 0)
724 /* Validate that the name doesn't contain any illegal '\0' */
725 if (strnlen(filename.name, filename.len) != filename.len)
728 if (strnchr(filename.name, filename.len, '/'))
730 if (filename.name[0] == '.') {
731 if (filename.len == 1)
733 if (filename.len == 2 && filename.name[1] == '.')
736 filename.hash = full_name_hash(parent, filename.name, filename.len);
738 dentry = d_lookup(parent, &filename);
741 dentry = d_alloc_parallel(parent, &filename, &wq);
745 if (!d_in_lookup(dentry)) {
746 /* Is there a mountpoint here? If so, just exit */
747 if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
748 &entry->fattr->fsid))
750 if (nfs_same_file(dentry, entry)) {
751 if (!entry->fh->size)
753 nfs_set_verifier(dentry, dir_verifier);
754 status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
756 nfs_setsecurity(d_inode(dentry), entry->fattr);
757 trace_nfs_readdir_lookup_revalidate(d_inode(parent),
761 trace_nfs_readdir_lookup_revalidate_failed(
762 d_inode(parent), dentry, 0);
763 d_invalidate(dentry);
769 if (!entry->fh->size) {
770 d_lookup_done(dentry);
774 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
775 alias = d_splice_alias(inode, dentry);
776 d_lookup_done(dentry);
783 nfs_set_verifier(dentry, dir_verifier);
784 trace_nfs_readdir_lookup(d_inode(parent), dentry, 0);
789 /* Perform conversion from xdr to cache array */
790 static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
791 struct nfs_entry *entry,
792 struct page **xdr_pages,
794 struct page **arrays,
797 struct address_space *mapping = desc->file->f_mapping;
798 struct xdr_stream stream;
800 struct page *scratch, *new, *page = *arrays;
803 scratch = alloc_page(GFP_KERNEL);
807 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
808 xdr_set_scratch_page(&stream, scratch);
811 if (entry->fattr->label)
812 entry->fattr->label->len = NFS4_MAXLABELLEN;
814 status = xdr_decode(desc, entry, &stream);
819 nfs_prime_dcache(file_dentry(desc->file), entry,
822 status = nfs_readdir_add_to_array(entry, page);
823 if (status != -ENOSPC)
826 if (page->mapping != mapping) {
829 new = nfs_readdir_page_array_alloc(entry->prev_cookie,
834 *arrays = page = new;
835 desc->page_index_max++;
837 new = nfs_readdir_page_get_next(mapping,
843 nfs_readdir_page_unlock_and_put(page);
845 desc->page_index_max = new->index;
847 status = nfs_readdir_add_to_array(entry, page);
848 } while (!status && !entry->eof);
853 nfs_readdir_page_set_eof(page);
864 nfs_readdir_page_unlock_and_put(page);
870 static void nfs_readdir_free_pages(struct page **pages, size_t npages)
873 put_page(pages[npages]);
878 * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
879 * to nfs_readdir_free_pages()
881 static struct page **nfs_readdir_alloc_pages(size_t npages)
886 pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
889 for (i = 0; i < npages; i++) {
890 struct page *page = alloc_page(GFP_KERNEL);
898 nfs_readdir_free_pages(pages, i);
902 static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
903 __be32 *verf_arg, __be32 *verf_res,
904 struct page **arrays, size_t narrays)
907 struct page *page = *arrays;
908 struct nfs_entry *entry;
910 struct inode *inode = file_inode(desc->file);
911 unsigned int dtsize = desc->dtsize;
913 int status = -ENOMEM;
915 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
918 entry->cookie = nfs_readdir_page_last_cookie(page);
919 entry->fh = nfs_alloc_fhandle();
920 entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
921 entry->server = NFS_SERVER(inode);
922 if (entry->fh == NULL || entry->fattr == NULL)
925 array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
926 pages = nfs_readdir_alloc_pages(array_size);
930 status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
937 status = nfs_readdir_page_filler(desc, entry, pages, pglen,
940 nfs_readdir_page_set_eof(page);
941 desc->buffer_fills++;
944 nfs_readdir_free_pages(pages, array_size);
946 nfs_free_fattr(entry->fattr);
947 nfs_free_fhandle(entry->fh);
952 static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
954 put_page(desc->page);
959 nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
961 unlock_page(desc->page);
962 nfs_readdir_page_put(desc);
966 nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
968 return nfs_readdir_page_get_locked(desc->file->f_mapping,
974 * Returns 0 if desc->dir_cookie was found on page desc->page_index
975 * and locks the page to prevent removal from the page cache.
977 static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
979 struct inode *inode = file_inode(desc->file);
980 struct nfs_inode *nfsi = NFS_I(inode);
981 __be32 verf[NFS_DIR_VERIFIER_SIZE];
984 desc->page = nfs_readdir_page_get_cached(desc);
987 if (nfs_readdir_page_needs_filling(desc->page)) {
988 /* Grow the dtsize if we had to go back for more pages */
989 if (desc->page_index == desc->page_index_max)
990 nfs_grow_dtsize(desc);
991 desc->page_index_max = desc->page_index;
992 trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf,
994 desc->page->index, desc->dtsize);
995 res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
998 nfs_readdir_page_unlock_and_put_cached(desc);
999 trace_nfs_readdir_cache_fill_done(inode, res);
1000 if (res == -EBADCOOKIE || res == -ENOTSYNC) {
1001 invalidate_inode_pages2(desc->file->f_mapping);
1002 desc->page_index = 0;
1003 trace_nfs_readdir_invalidate_cache_range(
1004 inode, 0, MAX_LFS_FILESIZE);
1010 * Set the cookie verifier if the page cache was empty
1012 if (desc->last_cookie == 0 &&
1013 memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
1014 memcpy(nfsi->cookieverf, verf,
1015 sizeof(nfsi->cookieverf));
1016 invalidate_inode_pages2_range(desc->file->f_mapping,
1017 desc->page_index_max + 1,
1019 trace_nfs_readdir_invalidate_cache_range(
1020 inode, desc->page_index_max + 1,
1024 res = nfs_readdir_search_array(desc);
1027 nfs_readdir_page_unlock_and_put_cached(desc);
1031 /* Search for desc->dir_cookie from the beginning of the page cache */
1032 static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
1037 if (desc->page_index == 0) {
1038 desc->current_index = 0;
1039 desc->prev_index = 0;
1040 desc->last_cookie = 0;
1042 res = find_and_lock_cache_page(desc);
1043 } while (res == -EAGAIN);
1048 * Once we've found the start of the dirent within a page: fill 'er up...
1050 static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
1053 struct file *file = desc->file;
1054 struct nfs_cache_array *array;
1057 array = kmap(desc->page);
1058 for (i = desc->cache_entry_index; i < array->size; i++) {
1059 struct nfs_cache_array_entry *ent;
1061 ent = &array->array[i];
1062 if (!dir_emit(desc->ctx, ent->name, ent->name_len,
1063 nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1067 memcpy(desc->verf, verf, sizeof(desc->verf));
1068 if (i == array->size - 1) {
1069 desc->dir_cookie = array->last_cookie;
1070 nfs_readdir_seek_next_array(array, desc);
1072 desc->dir_cookie = array->array[i + 1].cookie;
1073 desc->last_cookie = array->array[0].cookie;
1075 if (nfs_readdir_use_cookie(file))
1076 desc->ctx->pos = desc->dir_cookie;
1079 if (desc->duped != 0)
1082 if (array->page_is_eof)
1083 desc->eof = !desc->eob;
1086 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1087 (unsigned long long)desc->dir_cookie);
1091 * If we cannot find a cookie in our cache, we suspect that this is
1092 * because it points to a deleted file, so we ask the server to return
1093 * whatever it thinks is the next entry. We then feed this to filldir.
1094 * If all goes well, we should then be able to find our way round the
1095 * cache on the next call to readdir_search_pagecache();
1097 * NOTE: we cannot add the anonymous page to the pagecache because
1098 * the data it contains might not be page aligned. Besides,
1099 * we should already have a complete representation of the
1100 * directory in the page cache by the time we get here.
1102 static int uncached_readdir(struct nfs_readdir_descriptor *desc)
1104 struct page **arrays;
1106 __be32 verf[NFS_DIR_VERIFIER_SIZE];
1107 int status = -ENOMEM;
1109 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
1110 (unsigned long long)desc->dir_cookie);
1112 arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
1115 arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
1119 desc->page_index = 0;
1120 desc->cache_entry_index = 0;
1121 desc->last_cookie = desc->dir_cookie;
1123 desc->page_index_max = 0;
1125 trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie,
1128 status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1130 trace_nfs_readdir_uncached_done(file_inode(desc->file), status);
1134 for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
1135 desc->page = arrays[i];
1136 nfs_do_filldir(desc, verf);
1141 * Grow the dtsize if we have to go back for more pages,
1142 * or shrink it if we're reading too many.
1146 nfs_grow_dtsize(desc);
1147 else if (desc->buffer_fills == 1 &&
1148 i < (desc->page_index_max >> 1))
1149 nfs_shrink_dtsize(desc);
1152 for (i = 0; i < sz && arrays[i]; i++)
1153 nfs_readdir_page_array_free(arrays[i]);
1155 desc->page_index_max = -1;
1157 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
1161 #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
1163 static void nfs_readdir_handle_cache_misses(struct inode *inode,
1164 struct nfs_readdir_descriptor *desc,
1166 unsigned int cache_misses)
1168 if (desc->ctx->pos == 0 ||
1169 cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD)
1171 if (invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1) == 0)
1173 trace_nfs_readdir_invalidate_cache_range(
1174 inode, (loff_t)(page_index + 1) << PAGE_SHIFT,
1178 /* The file offset position represents the dirent entry number. A
1179 last cookie cache takes care of the common case of reading the
1182 static int nfs_readdir(struct file *file, struct dir_context *ctx)
1184 struct dentry *dentry = file_dentry(file);
1185 struct inode *inode = d_inode(dentry);
1186 struct nfs_inode *nfsi = NFS_I(inode);
1187 struct nfs_open_dir_context *dir_ctx = file->private_data;
1188 struct nfs_readdir_descriptor *desc;
1189 unsigned int cache_hits, cache_misses;
1193 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
1194 file, (long long)ctx->pos);
1195 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1198 * ctx->pos points to the dirent entry number.
1199 * *desc->dir_cookie has the cookie for the next entry. We have
1200 * to either find the entry with the appropriate number or
1201 * revalidate the cookie.
1203 nfs_revalidate_mapping(inode, file->f_mapping);
1206 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1211 desc->page_index_max = -1;
1213 spin_lock(&file->f_lock);
1214 desc->dir_cookie = dir_ctx->dir_cookie;
1215 desc->dup_cookie = dir_ctx->dup_cookie;
1216 desc->duped = dir_ctx->duped;
1217 page_index = dir_ctx->page_index;
1218 desc->page_index = page_index;
1219 desc->last_cookie = dir_ctx->last_cookie;
1220 desc->attr_gencount = dir_ctx->attr_gencount;
1221 desc->eof = dir_ctx->eof;
1222 nfs_set_dtsize(desc, dir_ctx->dtsize);
1223 memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
1224 cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0);
1225 cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0);
1226 spin_unlock(&file->f_lock);
1233 desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses);
1234 nfs_readdir_handle_cache_misses(inode, desc, page_index, cache_misses);
1237 res = readdir_search_pagecache(desc);
1239 if (res == -EBADCOOKIE) {
1241 /* This means either end of directory */
1242 if (desc->dir_cookie && !desc->eof) {
1243 /* Or that the server has 'lost' a cookie */
1244 res = uncached_readdir(desc);
1247 if (res == -EBADCOOKIE || res == -ENOTSYNC)
1252 if (res == -ETOOSMALL && desc->plus) {
1253 nfs_zap_caches(inode);
1254 desc->page_index = 0;
1262 nfs_do_filldir(desc, nfsi->cookieverf);
1263 nfs_readdir_page_unlock_and_put_cached(desc);
1264 } while (!desc->eob && !desc->eof);
1266 spin_lock(&file->f_lock);
1267 dir_ctx->dir_cookie = desc->dir_cookie;
1268 dir_ctx->dup_cookie = desc->dup_cookie;
1269 dir_ctx->last_cookie = desc->last_cookie;
1270 dir_ctx->duped = desc->duped;
1271 dir_ctx->attr_gencount = desc->attr_gencount;
1272 dir_ctx->page_index = desc->page_index;
1273 dir_ctx->eof = desc->eof;
1274 dir_ctx->dtsize = desc->dtsize;
1275 memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
1276 spin_unlock(&file->f_lock);
1281 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
1285 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1287 struct nfs_open_dir_context *dir_ctx = filp->private_data;
1289 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1290 filp, offset, whence);
1298 spin_lock(&filp->f_lock);
1303 spin_lock(&filp->f_lock);
1304 offset += filp->f_pos;
1306 spin_unlock(&filp->f_lock);
1310 if (offset != filp->f_pos) {
1311 filp->f_pos = offset;
1312 dir_ctx->page_index = 0;
1313 if (!nfs_readdir_use_cookie(filp))
1314 dir_ctx->dir_cookie = 0;
1316 dir_ctx->dir_cookie = offset;
1318 memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
1320 dir_ctx->eof = false;
1322 spin_unlock(&filp->f_lock);
1327 * All directory operations under NFS are synchronous, so fsync()
1328 * is a dummy operation.
1330 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1333 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
1335 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
1340 * nfs_force_lookup_revalidate - Mark the directory as having changed
1341 * @dir: pointer to directory inode
1343 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1344 * full lookup on all child dentries of 'dir' whenever a change occurs
1345 * on the server that might have invalidated our dcache.
1347 * Note that we reserve bit '0' as a tag to let us know when a dentry
1348 * was revalidated while holding a delegation on its inode.
1350 * The caller should be holding dir->i_lock
1352 void nfs_force_lookup_revalidate(struct inode *dir)
1354 NFS_I(dir)->cache_change_attribute += 2;
1356 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1359 * nfs_verify_change_attribute - Detects NFS remote directory changes
1360 * @dir: pointer to parent directory inode
1361 * @verf: previously saved change attribute
1363 * Return "false" if the verifiers doesn't match the change attribute.
1364 * This would usually indicate that the directory contents have changed on
1365 * the server, and that any dentries need revalidating.
1367 static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1369 return (verf & ~1UL) == nfs_save_change_attribute(dir);
1372 static void nfs_set_verifier_delegated(unsigned long *verf)
1377 #if IS_ENABLED(CONFIG_NFS_V4)
1378 static void nfs_unset_verifier_delegated(unsigned long *verf)
1382 #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1384 static bool nfs_test_verifier_delegated(unsigned long verf)
1389 static bool nfs_verifier_is_delegated(struct dentry *dentry)
1391 return nfs_test_verifier_delegated(dentry->d_time);
1394 static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1396 struct inode *inode = d_inode(dentry);
1397 struct inode *dir = d_inode(dentry->d_parent);
1399 if (!nfs_verify_change_attribute(dir, verf))
1401 if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1402 nfs_set_verifier_delegated(&verf);
1403 dentry->d_time = verf;
1407 * nfs_set_verifier - save a parent directory verifier in the dentry
1408 * @dentry: pointer to dentry
1409 * @verf: verifier to save
1411 * Saves the parent directory verifier in @dentry. If the inode has
1412 * a delegation, we also tag the dentry as having been revalidated
1413 * while holding a delegation so that we know we don't have to
1414 * look it up again after a directory change.
1416 void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1419 spin_lock(&dentry->d_lock);
1420 nfs_set_verifier_locked(dentry, verf);
1421 spin_unlock(&dentry->d_lock);
1423 EXPORT_SYMBOL_GPL(nfs_set_verifier);
1425 #if IS_ENABLED(CONFIG_NFS_V4)
1427 * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1428 * @inode: pointer to inode
1430 * Iterates through the dentries in the inode alias list and clears
1431 * the tag used to indicate that the dentry has been revalidated
1432 * while holding a delegation.
1433 * This function is intended for use when the delegation is being
1434 * returned or revoked.
1436 void nfs_clear_verifier_delegated(struct inode *inode)
1438 struct dentry *alias;
1442 spin_lock(&inode->i_lock);
1443 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1444 spin_lock(&alias->d_lock);
1445 nfs_unset_verifier_delegated(&alias->d_time);
1446 spin_unlock(&alias->d_lock);
1448 spin_unlock(&inode->i_lock);
1450 EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1451 #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1453 static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
1455 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
1456 d_really_is_negative(dentry))
1457 return dentry->d_time == inode_peek_iversion_raw(dir);
1458 return nfs_verify_change_attribute(dir, dentry->d_time);
1462 * A check for whether or not the parent directory has changed.
1463 * In the case it has, we assume that the dentries are untrustworthy
1464 * and may need to be looked up again.
1465 * If rcu_walk prevents us from performing a full check, return 0.
1467 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1470 if (IS_ROOT(dentry))
1472 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1474 if (!nfs_dentry_verify_change(dir, dentry))
1476 /* Revalidate nfsi->cache_change_attribute before we declare a match */
1477 if (nfs_mapping_need_revalidate_inode(dir)) {
1480 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1483 if (!nfs_dentry_verify_change(dir, dentry))
1489 * Use intent information to check whether or not we're going to do
1490 * an O_EXCL create using this path component.
1492 static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1494 if (NFS_PROTO(dir)->version == 2)
1496 return flags & LOOKUP_EXCL;
1500 * Inode and filehandle revalidation for lookups.
1502 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1503 * or if the intent information indicates that we're about to open this
1504 * particular file and the "nocto" mount flag is not set.
1508 int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1510 struct nfs_server *server = NFS_SERVER(inode);
1513 if (IS_AUTOMOUNT(inode))
1516 if (flags & LOOKUP_OPEN) {
1517 switch (inode->i_mode & S_IFMT) {
1519 /* A NFSv4 OPEN will revalidate later */
1520 if (server->caps & NFS_CAP_ATOMIC_OPEN)
1524 if (server->flags & NFS_MOUNT_NOCTO)
1526 /* NFS close-to-open cache consistency validation */
1531 /* VFS wants an on-the-wire revalidation */
1532 if (flags & LOOKUP_REVAL)
1535 if (inode->i_nlink > 0 ||
1536 (inode->i_nlink == 0 &&
1537 test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
1542 if (flags & LOOKUP_RCU)
1544 ret = __nfs_revalidate_inode(server, inode);
1550 static void nfs_mark_dir_for_revalidate(struct inode *inode)
1552 spin_lock(&inode->i_lock);
1553 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
1554 spin_unlock(&inode->i_lock);
1558 * We judge how long we want to trust negative
1559 * dentries by looking at the parent inode mtime.
1561 * If parent mtime has changed, we revalidate, else we wait for a
1562 * period corresponding to the parent's attribute cache timeout value.
1564 * If LOOKUP_RCU prevents us from performing a full check, return 1
1565 * suggesting a reval is needed.
1567 * Note that when creating a new file, or looking up a rename target,
1568 * then it shouldn't be necessary to revalidate a negative dentry.
1571 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1574 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1576 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1578 /* Case insensitive server? Revalidate negative dentries */
1579 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1581 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
1585 nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1586 struct inode *inode, int error)
1593 * We can't d_drop the root of a disconnected tree:
1594 * its d_hash is on the s_anon list and d_drop() would hide
1595 * it from shrink_dcache_for_unmount(), leading to busy
1596 * inodes on unmount and further oopses.
1598 if (inode && IS_ROOT(dentry))
1602 trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1607 nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1611 if (nfs_neg_need_reval(dir, dentry, flags)) {
1612 if (flags & LOOKUP_RCU)
1616 return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1620 nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1621 struct inode *inode)
1623 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1624 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1627 static int nfs_lookup_revalidate_dentry(struct inode *dir,
1628 struct dentry *dentry,
1629 struct inode *inode, unsigned int flags)
1631 struct nfs_fh *fhandle;
1632 struct nfs_fattr *fattr;
1633 unsigned long dir_verifier;
1636 trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1639 fhandle = nfs_alloc_fhandle();
1640 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1641 if (fhandle == NULL || fattr == NULL)
1644 dir_verifier = nfs_save_change_attribute(dir);
1645 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1653 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1659 /* Request help from readdirplus */
1660 nfs_lookup_advise_force_readdirplus(dir, flags);
1663 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1665 if (nfs_refresh_inode(inode, fattr) < 0)
1668 nfs_setsecurity(inode, fattr);
1669 nfs_set_verifier(dentry, dir_verifier);
1673 nfs_free_fattr(fattr);
1674 nfs_free_fhandle(fhandle);
1677 * If the lookup failed despite the dentry change attribute being
1678 * a match, then we should revalidate the directory cache.
1680 if (!ret && nfs_dentry_verify_change(dir, dentry))
1681 nfs_mark_dir_for_revalidate(dir);
1682 return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1686 * This is called every time the dcache has a lookup hit,
1687 * and we should check whether we can really trust that
1690 * NOTE! The hit can be a negative hit too, don't assume
1693 * If the parent directory is seen to have changed, we throw out the
1694 * cached dentry and do a new lookup.
1697 nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1700 struct inode *inode;
1703 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1704 inode = d_inode(dentry);
1707 return nfs_lookup_revalidate_negative(dir, dentry, flags);
1709 if (is_bad_inode(inode)) {
1710 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1715 if (nfs_verifier_is_delegated(dentry))
1716 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1718 /* Force a full look up iff the parent directory has changed */
1719 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
1720 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1721 error = nfs_lookup_verify_inode(inode, flags);
1723 if (error == -ESTALE)
1724 nfs_mark_dir_for_revalidate(dir);
1730 if (flags & LOOKUP_RCU)
1733 if (NFS_STALE(inode))
1736 return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
1738 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1740 if (flags & LOOKUP_RCU)
1742 return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1746 __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1747 int (*reval)(struct inode *, struct dentry *, unsigned int))
1749 struct dentry *parent;
1753 if (flags & LOOKUP_RCU) {
1754 parent = READ_ONCE(dentry->d_parent);
1755 dir = d_inode_rcu(parent);
1758 ret = reval(dir, dentry, flags);
1759 if (parent != READ_ONCE(dentry->d_parent))
1762 parent = dget_parent(dentry);
1763 ret = reval(d_inode(parent), dentry, flags);
1769 static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1771 return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1775 * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1776 * when we don't really care about the dentry name. This is called when a
1777 * pathwalk ends on a dentry that was not found via a normal lookup in the
1778 * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1780 * In this situation, we just want to verify that the inode itself is OK
1781 * since the dentry might have changed on the server.
1783 static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1785 struct inode *inode = d_inode(dentry);
1789 * I believe we can only get a negative dentry here in the case of a
1790 * procfs-style symlink. Just assume it's correct for now, but we may
1791 * eventually need to do something more here.
1794 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1799 if (is_bad_inode(inode)) {
1800 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1805 error = nfs_lookup_verify_inode(inode, flags);
1806 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1807 __func__, inode->i_ino, error ? "invalid" : "valid");
1812 * This is called from dput() when d_count is going to 0.
1814 static int nfs_dentry_delete(const struct dentry *dentry)
1816 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1817 dentry, dentry->d_flags);
1819 /* Unhash any dentry with a stale inode */
1820 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
1823 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1824 /* Unhash it, so that ->d_iput() would be called */
1827 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
1828 /* Unhash it, so that ancestors of killed async unlink
1829 * files will be cleaned up during umount */
1836 /* Ensure that we revalidate inode->i_nlink */
1837 static void nfs_drop_nlink(struct inode *inode)
1839 spin_lock(&inode->i_lock);
1840 /* drop the inode if we're reasonably sure this is the last link */
1841 if (inode->i_nlink > 0)
1843 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1844 nfs_set_cache_invalid(
1845 inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
1846 NFS_INO_INVALID_NLINK);
1847 spin_unlock(&inode->i_lock);
1851 * Called when the dentry loses inode.
1852 * We use it to clean up silly-renamed files.
1854 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1856 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1857 nfs_complete_unlink(dentry, inode);
1858 nfs_drop_nlink(inode);
1863 static void nfs_d_release(struct dentry *dentry)
1865 /* free cached devname value, if it survived that far */
1866 if (unlikely(dentry->d_fsdata)) {
1867 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1870 kfree(dentry->d_fsdata);
1874 const struct dentry_operations nfs_dentry_operations = {
1875 .d_revalidate = nfs_lookup_revalidate,
1876 .d_weak_revalidate = nfs_weak_revalidate,
1877 .d_delete = nfs_dentry_delete,
1878 .d_iput = nfs_dentry_iput,
1879 .d_automount = nfs_d_automount,
1880 .d_release = nfs_d_release,
1882 EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1884 struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1887 struct inode *inode = NULL;
1888 struct nfs_fh *fhandle = NULL;
1889 struct nfs_fattr *fattr = NULL;
1890 unsigned long dir_verifier;
1893 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
1894 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1896 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1897 return ERR_PTR(-ENAMETOOLONG);
1900 * If we're doing an exclusive create, optimize away the lookup
1901 * but don't hash the dentry.
1903 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1906 res = ERR_PTR(-ENOMEM);
1907 fhandle = nfs_alloc_fhandle();
1908 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1909 if (fhandle == NULL || fattr == NULL)
1912 dir_verifier = nfs_save_change_attribute(dir);
1913 trace_nfs_lookup_enter(dir, dentry, flags);
1914 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1915 if (error == -ENOENT) {
1916 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1917 dir_verifier = inode_peek_iversion_raw(dir);
1921 res = ERR_PTR(error);
1924 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1925 res = ERR_CAST(inode);
1929 /* Notify readdir to use READDIRPLUS */
1930 nfs_lookup_advise_force_readdirplus(dir, flags);
1933 res = d_splice_alias(inode, dentry);
1939 nfs_set_verifier(dentry, dir_verifier);
1941 trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1942 nfs_free_fattr(fattr);
1943 nfs_free_fhandle(fhandle);
1946 EXPORT_SYMBOL_GPL(nfs_lookup);
1948 void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
1950 /* Case insensitive server? Revalidate dentries */
1951 if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
1952 d_prune_aliases(inode);
1954 EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
1956 #if IS_ENABLED(CONFIG_NFS_V4)
1957 static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
1959 const struct dentry_operations nfs4_dentry_operations = {
1960 .d_revalidate = nfs4_lookup_revalidate,
1961 .d_weak_revalidate = nfs_weak_revalidate,
1962 .d_delete = nfs_dentry_delete,
1963 .d_iput = nfs_dentry_iput,
1964 .d_automount = nfs_d_automount,
1965 .d_release = nfs_d_release,
1967 EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1969 static fmode_t flags_to_mode(int flags)
1971 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1972 if ((flags & O_ACCMODE) != O_WRONLY)
1974 if ((flags & O_ACCMODE) != O_RDONLY)
1979 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1981 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1984 static int do_open(struct inode *inode, struct file *filp)
1986 nfs_fscache_open_file(inode, filp);
1990 static int nfs_finish_open(struct nfs_open_context *ctx,
1991 struct dentry *dentry,
1992 struct file *file, unsigned open_flags)
1996 err = finish_open(file, dentry, do_open);
1999 if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
2000 nfs_file_set_open_context(file, ctx);
2007 int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
2008 struct file *file, unsigned open_flags,
2011 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2012 struct nfs_open_context *ctx;
2014 struct iattr attr = { .ia_valid = ATTR_OPEN };
2015 struct inode *inode;
2016 unsigned int lookup_flags = 0;
2017 unsigned long dir_verifier;
2018 bool switched = false;
2022 /* Expect a negative dentry */
2023 BUG_ON(d_inode(dentry));
2025 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
2026 dir->i_sb->s_id, dir->i_ino, dentry);
2028 err = nfs_check_flags(open_flags);
2032 /* NFS only supports OPEN on regular files */
2033 if ((open_flags & O_DIRECTORY)) {
2034 if (!d_in_lookup(dentry)) {
2036 * Hashed negative dentry with O_DIRECTORY: dentry was
2037 * revalidated and is fine, no need to perform lookup
2042 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
2046 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
2047 return -ENAMETOOLONG;
2049 if (open_flags & O_CREAT) {
2050 struct nfs_server *server = NFS_SERVER(dir);
2052 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
2053 mode &= ~current_umask();
2055 attr.ia_valid |= ATTR_MODE;
2056 attr.ia_mode = mode;
2058 if (open_flags & O_TRUNC) {
2059 attr.ia_valid |= ATTR_SIZE;
2063 if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
2066 dentry = d_alloc_parallel(dentry->d_parent,
2067 &dentry->d_name, &wq);
2069 return PTR_ERR(dentry);
2070 if (unlikely(!d_in_lookup(dentry)))
2071 return finish_no_open(file, dentry);
2074 ctx = create_nfs_open_context(dentry, open_flags, file);
2079 trace_nfs_atomic_open_enter(dir, ctx, open_flags);
2080 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
2082 file->f_mode |= FMODE_CREATED;
2083 if (IS_ERR(inode)) {
2084 err = PTR_ERR(inode);
2085 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2086 put_nfs_open_context(ctx);
2090 d_splice_alias(NULL, dentry);
2091 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
2092 dir_verifier = inode_peek_iversion_raw(dir);
2094 dir_verifier = nfs_save_change_attribute(dir);
2095 nfs_set_verifier(dentry, dir_verifier);
2101 if (!(open_flags & O_NOFOLLOW))
2111 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
2112 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2113 put_nfs_open_context(ctx);
2115 if (unlikely(switched)) {
2116 d_lookup_done(dentry);
2122 res = nfs_lookup(dir, dentry, lookup_flags);
2124 inode = d_inode(dentry);
2125 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2126 !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2127 res = ERR_PTR(-ENOTDIR);
2128 else if (inode && S_ISREG(inode->i_mode))
2129 res = ERR_PTR(-EOPENSTALE);
2130 } else if (!IS_ERR(res)) {
2131 inode = d_inode(res);
2132 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2133 !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2135 res = ERR_PTR(-ENOTDIR);
2136 } else if (inode && S_ISREG(inode->i_mode)) {
2138 res = ERR_PTR(-EOPENSTALE);
2142 d_lookup_done(dentry);
2149 return PTR_ERR(res);
2150 return finish_no_open(file, res);
2152 EXPORT_SYMBOL_GPL(nfs_atomic_open);
2155 nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2158 struct inode *inode;
2160 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2162 if (d_mountpoint(dentry))
2165 inode = d_inode(dentry);
2167 /* We can't create new files in nfs_open_revalidate(), so we
2168 * optimize away revalidation of negative dentries.
2173 if (nfs_verifier_is_delegated(dentry))
2174 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2176 /* NFS only supports OPEN on regular files */
2177 if (!S_ISREG(inode->i_mode))
2180 /* We cannot do exclusive creation on a positive dentry */
2181 if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2184 /* Check if the directory changed */
2185 if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2188 /* Let f_op->open() actually open (and revalidate) the file */
2191 if (flags & LOOKUP_RCU)
2193 return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
2196 return nfs_do_lookup_revalidate(dir, dentry, flags);
2199 static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2201 return __nfs_lookup_revalidate(dentry, flags,
2202 nfs4_do_lookup_revalidate);
2205 #endif /* CONFIG_NFSV4 */
2208 nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2209 struct nfs_fattr *fattr)
2211 struct dentry *parent = dget_parent(dentry);
2212 struct inode *dir = d_inode(parent);
2213 struct inode *inode;
2219 if (fhandle->size == 0) {
2220 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
2224 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2225 if (!(fattr->valid & NFS_ATTR_FATTR)) {
2226 struct nfs_server *server = NFS_SB(dentry->d_sb);
2227 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
2232 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2233 d = d_splice_alias(inode, dentry);
2241 EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2244 * Code common to create, mkdir, and mknod.
2246 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2247 struct nfs_fattr *fattr)
2251 d = nfs_add_or_obtain(dentry, fhandle, fattr);
2255 /* Callers don't care */
2259 EXPORT_SYMBOL_GPL(nfs_instantiate);
2262 * Following a failed create operation, we drop the dentry rather
2263 * than retain a negative dentry. This avoids a problem in the event
2264 * that the operation succeeded on the server, but an error in the
2265 * reply path made it appear to have failed.
2267 int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2268 struct dentry *dentry, umode_t mode, bool excl)
2271 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
2274 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
2275 dir->i_sb->s_id, dir->i_ino, dentry);
2277 attr.ia_mode = mode;
2278 attr.ia_valid = ATTR_MODE;
2280 trace_nfs_create_enter(dir, dentry, open_flags);
2281 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
2282 trace_nfs_create_exit(dir, dentry, open_flags, error);
2290 EXPORT_SYMBOL_GPL(nfs_create);
2293 * See comments for nfs_proc_create regarding failed operations.
2296 nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2297 struct dentry *dentry, umode_t mode, dev_t rdev)
2302 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
2303 dir->i_sb->s_id, dir->i_ino, dentry);
2305 attr.ia_mode = mode;
2306 attr.ia_valid = ATTR_MODE;
2308 trace_nfs_mknod_enter(dir, dentry);
2309 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
2310 trace_nfs_mknod_exit(dir, dentry, status);
2318 EXPORT_SYMBOL_GPL(nfs_mknod);
2321 * See comments for nfs_proc_create regarding failed operations.
2323 int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2324 struct dentry *dentry, umode_t mode)
2329 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
2330 dir->i_sb->s_id, dir->i_ino, dentry);
2332 attr.ia_valid = ATTR_MODE;
2333 attr.ia_mode = mode | S_IFDIR;
2335 trace_nfs_mkdir_enter(dir, dentry);
2336 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
2337 trace_nfs_mkdir_exit(dir, dentry, error);
2345 EXPORT_SYMBOL_GPL(nfs_mkdir);
2347 static void nfs_dentry_handle_enoent(struct dentry *dentry)
2349 if (simple_positive(dentry))
2353 static void nfs_dentry_remove_handle_error(struct inode *dir,
2354 struct dentry *dentry, int error)
2359 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2362 nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
2363 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2367 int nfs_rmdir(struct inode *dir, struct dentry *dentry)
2371 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
2372 dir->i_sb->s_id, dir->i_ino, dentry);
2374 trace_nfs_rmdir_enter(dir, dentry);
2375 if (d_really_is_positive(dentry)) {
2376 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2377 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2378 /* Ensure the VFS deletes this inode */
2381 clear_nlink(d_inode(dentry));
2384 nfs_dentry_handle_enoent(dentry);
2386 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2388 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2389 nfs_dentry_remove_handle_error(dir, dentry, error);
2390 trace_nfs_rmdir_exit(dir, dentry, error);
2394 EXPORT_SYMBOL_GPL(nfs_rmdir);
2397 * Remove a file after making sure there are no pending writes,
2398 * and after checking that the file has only one user.
2400 * We invalidate the attribute cache and free the inode prior to the operation
2401 * to avoid possible races if the server reuses the inode.
2403 static int nfs_safe_remove(struct dentry *dentry)
2405 struct inode *dir = d_inode(dentry->d_parent);
2406 struct inode *inode = d_inode(dentry);
2409 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
2411 /* If the dentry was sillyrenamed, we simply call d_delete() */
2412 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2417 trace_nfs_remove_enter(dir, dentry);
2418 if (inode != NULL) {
2419 error = NFS_PROTO(dir)->remove(dir, dentry);
2421 nfs_drop_nlink(inode);
2423 error = NFS_PROTO(dir)->remove(dir, dentry);
2424 if (error == -ENOENT)
2425 nfs_dentry_handle_enoent(dentry);
2426 trace_nfs_remove_exit(dir, dentry, error);
2431 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
2432 * belongs to an active ".nfs..." file and we return -EBUSY.
2434 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
2436 int nfs_unlink(struct inode *dir, struct dentry *dentry)
2439 int need_rehash = 0;
2441 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
2442 dir->i_ino, dentry);
2444 trace_nfs_unlink_enter(dir, dentry);
2445 spin_lock(&dentry->d_lock);
2446 if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
2447 &NFS_I(d_inode(dentry))->flags)) {
2448 spin_unlock(&dentry->d_lock);
2449 /* Start asynchronous writeout of the inode */
2450 write_inode_now(d_inode(dentry), 0);
2451 error = nfs_sillyrename(dir, dentry);
2454 if (!d_unhashed(dentry)) {
2458 spin_unlock(&dentry->d_lock);
2459 error = nfs_safe_remove(dentry);
2460 nfs_dentry_remove_handle_error(dir, dentry, error);
2464 trace_nfs_unlink_exit(dir, dentry, error);
2467 EXPORT_SYMBOL_GPL(nfs_unlink);
2470 * To create a symbolic link, most file systems instantiate a new inode,
2471 * add a page to it containing the path, then write it out to the disk
2472 * using prepare_write/commit_write.
2474 * Unfortunately the NFS client can't create the in-core inode first
2475 * because it needs a file handle to create an in-core inode (see
2476 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
2477 * symlink request has completed on the server.
2479 * So instead we allocate a raw page, copy the symname into it, then do
2480 * the SYMLINK request with the page as the buffer. If it succeeds, we
2481 * now have a new file handle and can instantiate an in-core NFS inode
2482 * and move the raw page into its mapping.
2484 int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2485 struct dentry *dentry, const char *symname)
2490 unsigned int pathlen = strlen(symname);
2493 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
2494 dir->i_ino, dentry, symname);
2496 if (pathlen > PAGE_SIZE)
2497 return -ENAMETOOLONG;
2499 attr.ia_mode = S_IFLNK | S_IRWXUGO;
2500 attr.ia_valid = ATTR_MODE;
2502 page = alloc_page(GFP_USER);
2506 kaddr = page_address(page);
2507 memcpy(kaddr, symname, pathlen);
2508 if (pathlen < PAGE_SIZE)
2509 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2511 trace_nfs_symlink_enter(dir, dentry);
2512 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
2513 trace_nfs_symlink_exit(dir, dentry, error);
2515 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2516 dir->i_sb->s_id, dir->i_ino,
2517 dentry, symname, error);
2523 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2526 * No big deal if we can't add this page to the page cache here.
2527 * READLINK will get the missing page from the server if needed.
2529 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2531 SetPageUptodate(page);
2534 * add_to_page_cache_lru() grabs an extra page refcount.
2535 * Drop it here to avoid leaking this page later.
2543 EXPORT_SYMBOL_GPL(nfs_symlink);
2546 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2548 struct inode *inode = d_inode(old_dentry);
2551 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2552 old_dentry, dentry);
2554 trace_nfs_link_enter(inode, dir, dentry);
2556 if (S_ISREG(inode->i_mode))
2557 nfs_sync_inode(inode);
2558 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2560 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2562 d_add(dentry, inode);
2564 trace_nfs_link_exit(inode, dir, dentry, error);
2567 EXPORT_SYMBOL_GPL(nfs_link);
2571 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2572 * different file handle for the same inode after a rename (e.g. when
2573 * moving to a different directory). A fail-safe method to do so would
2574 * be to look up old_dir/old_name, create a link to new_dir/new_name and
2575 * rename the old file using the sillyrename stuff. This way, the original
2576 * file in old_dir will go away when the last process iput()s the inode.
2580 * It actually works quite well. One needs to have the possibility for
2581 * at least one ".nfs..." file in each directory the file ever gets
2582 * moved or linked to which happens automagically with the new
2583 * implementation that only depends on the dcache stuff instead of
2584 * using the inode layer
2586 * Unfortunately, things are a little more complicated than indicated
2587 * above. For a cross-directory move, we want to make sure we can get
2588 * rid of the old inode after the operation. This means there must be
2589 * no pending writes (if it's a file), and the use count must be 1.
2590 * If these conditions are met, we can drop the dentries before doing
2593 int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2594 struct dentry *old_dentry, struct inode *new_dir,
2595 struct dentry *new_dentry, unsigned int flags)
2597 struct inode *old_inode = d_inode(old_dentry);
2598 struct inode *new_inode = d_inode(new_dentry);
2599 struct dentry *dentry = NULL, *rehash = NULL;
2600 struct rpc_task *task;
2606 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2607 old_dentry, new_dentry,
2608 d_count(new_dentry));
2610 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
2612 * For non-directories, check whether the target is busy and if so,
2613 * make a copy of the dentry and then do a silly-rename. If the
2614 * silly-rename succeeds, the copied dentry is hashed and becomes
2617 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2619 * To prevent any new references to the target during the
2620 * rename, we unhash the dentry in advance.
2622 if (!d_unhashed(new_dentry)) {
2624 rehash = new_dentry;
2627 if (d_count(new_dentry) > 2) {
2630 /* copy the target dentry's name */
2631 dentry = d_alloc(new_dentry->d_parent,
2632 &new_dentry->d_name);
2636 /* silly-rename the existing target ... */
2637 err = nfs_sillyrename(new_dir, new_dentry);
2641 new_dentry = dentry;
2647 if (S_ISREG(old_inode->i_mode))
2648 nfs_sync_inode(old_inode);
2649 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
2651 error = PTR_ERR(task);
2655 error = rpc_wait_for_completion_task(task);
2657 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2658 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2661 error = task->tk_status;
2663 /* Ensure the inode attributes are revalidated */
2665 spin_lock(&old_inode->i_lock);
2666 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2667 nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2668 NFS_INO_INVALID_CTIME |
2669 NFS_INO_REVAL_FORCED);
2670 spin_unlock(&old_inode->i_lock);
2675 trace_nfs_rename_exit(old_dir, old_dentry,
2676 new_dir, new_dentry, error);
2678 if (new_inode != NULL)
2679 nfs_drop_nlink(new_inode);
2681 * The d_move() should be here instead of in an async RPC completion
2682 * handler because we need the proper locks to move the dentry. If
2683 * we're interrupted by a signal, the async RPC completion handler
2684 * should mark the directories for revalidation.
2686 d_move(old_dentry, new_dentry);
2687 nfs_set_verifier(old_dentry,
2688 nfs_save_change_attribute(new_dir));
2689 } else if (error == -ENOENT)
2690 nfs_dentry_handle_enoent(old_dentry);
2692 /* new dentry created? */
2697 EXPORT_SYMBOL_GPL(nfs_rename);
2699 static DEFINE_SPINLOCK(nfs_access_lru_lock);
2700 static LIST_HEAD(nfs_access_lru_list);
2701 static atomic_long_t nfs_access_nr_entries;
2703 static unsigned long nfs_access_max_cachesize = 4*1024*1024;
2704 module_param(nfs_access_max_cachesize, ulong, 0644);
2705 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2707 static void nfs_access_free_entry(struct nfs_access_entry *entry)
2709 put_group_info(entry->group_info);
2710 kfree_rcu(entry, rcu_head);
2711 smp_mb__before_atomic();
2712 atomic_long_dec(&nfs_access_nr_entries);
2713 smp_mb__after_atomic();
2716 static void nfs_access_free_list(struct list_head *head)
2718 struct nfs_access_entry *cache;
2720 while (!list_empty(head)) {
2721 cache = list_entry(head->next, struct nfs_access_entry, lru);
2722 list_del(&cache->lru);
2723 nfs_access_free_entry(cache);
2727 static unsigned long
2728 nfs_do_access_cache_scan(unsigned int nr_to_scan)
2731 struct nfs_inode *nfsi, *next;
2732 struct nfs_access_entry *cache;
2735 spin_lock(&nfs_access_lru_lock);
2736 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2737 struct inode *inode;
2739 if (nr_to_scan-- == 0)
2741 inode = &nfsi->vfs_inode;
2742 spin_lock(&inode->i_lock);
2743 if (list_empty(&nfsi->access_cache_entry_lru))
2744 goto remove_lru_entry;
2745 cache = list_entry(nfsi->access_cache_entry_lru.next,
2746 struct nfs_access_entry, lru);
2747 list_move(&cache->lru, &head);
2748 rb_erase(&cache->rb_node, &nfsi->access_cache);
2750 if (!list_empty(&nfsi->access_cache_entry_lru))
2751 list_move_tail(&nfsi->access_cache_inode_lru,
2752 &nfs_access_lru_list);
2755 list_del_init(&nfsi->access_cache_inode_lru);
2756 smp_mb__before_atomic();
2757 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
2758 smp_mb__after_atomic();
2760 spin_unlock(&inode->i_lock);
2762 spin_unlock(&nfs_access_lru_lock);
2763 nfs_access_free_list(&head);
2768 nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2770 int nr_to_scan = sc->nr_to_scan;
2771 gfp_t gfp_mask = sc->gfp_mask;
2773 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2775 return nfs_do_access_cache_scan(nr_to_scan);
2780 nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2782 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2786 nfs_access_cache_enforce_limit(void)
2788 long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2790 unsigned int nr_to_scan;
2792 if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2795 diff = nr_entries - nfs_access_max_cachesize;
2796 if (diff < nr_to_scan)
2798 nfs_do_access_cache_scan(nr_to_scan);
2801 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
2803 struct rb_root *root_node = &nfsi->access_cache;
2805 struct nfs_access_entry *entry;
2807 /* Unhook entries from the cache */
2808 while ((n = rb_first(root_node)) != NULL) {
2809 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2810 rb_erase(n, root_node);
2811 list_move(&entry->lru, head);
2813 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
2816 void nfs_access_zap_cache(struct inode *inode)
2820 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2822 /* Remove from global LRU init */
2823 spin_lock(&nfs_access_lru_lock);
2824 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2825 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2827 spin_lock(&inode->i_lock);
2828 __nfs_access_zap_cache(NFS_I(inode), &head);
2829 spin_unlock(&inode->i_lock);
2830 spin_unlock(&nfs_access_lru_lock);
2831 nfs_access_free_list(&head);
2833 EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
2835 static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
2837 struct group_info *ga, *gb;
2840 if (uid_lt(a->fsuid, b->fsuid))
2842 if (uid_gt(a->fsuid, b->fsuid))
2845 if (gid_lt(a->fsgid, b->fsgid))
2847 if (gid_gt(a->fsgid, b->fsgid))
2858 if (ga->ngroups < gb->ngroups)
2860 if (ga->ngroups > gb->ngroups)
2863 for (g = 0; g < ga->ngroups; g++) {
2864 if (gid_lt(ga->gid[g], gb->gid[g]))
2866 if (gid_gt(ga->gid[g], gb->gid[g]))
2872 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
2874 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2877 struct nfs_access_entry *entry =
2878 rb_entry(n, struct nfs_access_entry, rb_node);
2879 int cmp = access_cmp(cred, entry);
2891 static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
2893 struct nfs_inode *nfsi = NFS_I(inode);
2894 struct nfs_access_entry *cache;
2898 spin_lock(&inode->i_lock);
2900 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2902 cache = nfs_access_search_rbtree(inode, cred);
2906 /* Found an entry, is our attribute cache valid? */
2907 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2914 spin_unlock(&inode->i_lock);
2915 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2918 spin_lock(&inode->i_lock);
2921 *mask = cache->mask;
2922 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
2925 spin_unlock(&inode->i_lock);
2928 spin_unlock(&inode->i_lock);
2929 nfs_access_zap_cache(inode);
2933 static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2935 /* Only check the most recently returned cache entry,
2936 * but do it without locking.
2938 struct nfs_inode *nfsi = NFS_I(inode);
2939 struct nfs_access_entry *cache;
2941 struct list_head *lh;
2944 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2946 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2947 cache = list_entry(lh, struct nfs_access_entry, lru);
2948 if (lh == &nfsi->access_cache_entry_lru ||
2949 access_cmp(cred, cache) != 0)
2953 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2955 *mask = cache->mask;
2962 int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2963 u32 *mask, bool may_block)
2967 status = nfs_access_get_cached_rcu(inode, cred, mask);
2969 status = nfs_access_get_cached_locked(inode, cred, mask,
2974 EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2976 static void nfs_access_add_rbtree(struct inode *inode,
2977 struct nfs_access_entry *set,
2978 const struct cred *cred)
2980 struct nfs_inode *nfsi = NFS_I(inode);
2981 struct rb_root *root_node = &nfsi->access_cache;
2982 struct rb_node **p = &root_node->rb_node;
2983 struct rb_node *parent = NULL;
2984 struct nfs_access_entry *entry;
2987 spin_lock(&inode->i_lock);
2988 while (*p != NULL) {
2990 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2991 cmp = access_cmp(cred, entry);
2994 p = &parent->rb_left;
2996 p = &parent->rb_right;
3000 rb_link_node(&set->rb_node, parent, p);
3001 rb_insert_color(&set->rb_node, root_node);
3002 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
3003 spin_unlock(&inode->i_lock);
3006 rb_replace_node(parent, &set->rb_node, root_node);
3007 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
3008 list_del(&entry->lru);
3009 spin_unlock(&inode->i_lock);
3010 nfs_access_free_entry(entry);
3013 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
3014 const struct cred *cred)
3016 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
3019 RB_CLEAR_NODE(&cache->rb_node);
3020 cache->fsuid = cred->fsuid;
3021 cache->fsgid = cred->fsgid;
3022 cache->group_info = get_group_info(cred->group_info);
3023 cache->mask = set->mask;
3025 /* The above field assignments must be visible
3026 * before this item appears on the lru. We cannot easily
3027 * use rcu_assign_pointer, so just force the memory barrier.
3030 nfs_access_add_rbtree(inode, cache, cred);
3032 /* Update accounting */
3033 smp_mb__before_atomic();
3034 atomic_long_inc(&nfs_access_nr_entries);
3035 smp_mb__after_atomic();
3037 /* Add inode to global LRU list */
3038 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
3039 spin_lock(&nfs_access_lru_lock);
3040 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
3041 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
3042 &nfs_access_lru_list);
3043 spin_unlock(&nfs_access_lru_lock);
3045 nfs_access_cache_enforce_limit();
3047 EXPORT_SYMBOL_GPL(nfs_access_add_cache);
3049 #define NFS_MAY_READ (NFS_ACCESS_READ)
3050 #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
3051 NFS_ACCESS_EXTEND | \
3053 #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
3055 #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
3056 #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
3057 #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
3059 nfs_access_calc_mask(u32 access_result, umode_t umode)
3063 if (access_result & NFS_MAY_READ)
3065 if (S_ISDIR(umode)) {
3066 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
3068 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
3070 } else if (S_ISREG(umode)) {
3071 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
3073 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
3075 } else if (access_result & NFS_MAY_WRITE)
3080 void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
3082 entry->mask = access_result;
3084 EXPORT_SYMBOL_GPL(nfs_access_set_mask);
3086 static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
3088 struct nfs_access_entry cache;
3089 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3090 int cache_mask = -1;
3093 trace_nfs_access_enter(inode);
3095 status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
3104 * Determine which access bits we want to ask for...
3106 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
3107 nfs_access_xattr_mask(NFS_SERVER(inode));
3108 if (S_ISDIR(inode->i_mode))
3109 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
3111 cache.mask |= NFS_ACCESS_EXECUTE;
3112 status = NFS_PROTO(inode)->access(inode, &cache, cred);
3114 if (status == -ESTALE) {
3115 if (!S_ISDIR(inode->i_mode))
3116 nfs_set_inode_stale(inode);
3118 nfs_zap_caches(inode);
3122 nfs_access_add_cache(inode, &cache, cred);
3124 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3125 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3128 trace_nfs_access_exit(inode, mask, cache_mask, status);
3132 static int nfs_open_permission_mask(int openflags)
3136 if (openflags & __FMODE_EXEC) {
3137 /* ONLY check exec rights */
3140 if ((openflags & O_ACCMODE) != O_WRONLY)
3142 if ((openflags & O_ACCMODE) != O_RDONLY)
3149 int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3151 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3153 EXPORT_SYMBOL_GPL(nfs_may_open);
3155 static int nfs_execute_ok(struct inode *inode, int mask)
3157 struct nfs_server *server = NFS_SERVER(inode);
3160 if (S_ISDIR(inode->i_mode))
3162 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
3163 if (mask & MAY_NOT_BLOCK)
3165 ret = __nfs_revalidate_inode(server, inode);
3167 if (ret == 0 && !execute_ok(inode))
3172 int nfs_permission(struct user_namespace *mnt_userns,
3173 struct inode *inode,
3176 const struct cred *cred = current_cred();
3179 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
3181 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
3183 /* Is this sys_access() ? */
3184 if (mask & (MAY_ACCESS | MAY_CHDIR))
3187 switch (inode->i_mode & S_IFMT) {
3191 if ((mask & MAY_OPEN) &&
3192 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3197 * Optimize away all write operations, since the server
3198 * will check permissions when we perform the op.
3200 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
3205 if (!NFS_PROTO(inode)->access)
3208 res = nfs_do_access(inode, cred, mask);
3210 if (!res && (mask & MAY_EXEC))
3211 res = nfs_execute_ok(inode, mask);
3213 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
3214 inode->i_sb->s_id, inode->i_ino, mask, res);
3217 if (mask & MAY_NOT_BLOCK)
3220 res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3221 NFS_INO_INVALID_OTHER);
3223 res = generic_permission(&init_user_ns, inode, mask);
3226 EXPORT_SYMBOL_GPL(nfs_permission);