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