blob: 604ebe0153870d141ebfc0c169c3905625872e52 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Schumakerddda8e02012-07-30 16:05:23 -040021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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 Lever873101b2006-08-22 20:06:23 -040034#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/namei.h>
David Howells54ceac42006-08-22 20:06:13 -040036#include <linux/mount.h>
Mel Gormana0b8cab32013-07-03 15:02:32 -070037#include <linux/swap.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040038#include <linux/sched.h>
Catalin Marinas04e4bd12010-11-11 12:53:47 +000039#include <linux/kmemleak.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000040#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050043#include "iostat.h"
Adrian Bunk4c30d562007-11-21 15:04:31 -080044#include "internal.h"
Trond Myklebustcd9a1c02010-09-17 10:56:50 -040045#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Trond Myklebustf4ce1292013-08-19 18:59:33 -040047#include "nfstrace.h"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* #define NFS_DEBUG_VERBOSE 1 */
50
51static int nfs_opendir(struct inode *, struct file *);
Bryan Schumaker480c2002011-03-23 14:48:29 -040052static int nfs_closedir(struct inode *, struct file *);
Al Viro23db8622013-05-17 16:34:50 -040053static int nfs_readdir(struct file *, struct dir_context *);
Josef Bacik02c24a82011-07-16 20:44:56 -040054static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
Trond Myklebustf0dd2132005-06-22 17:16:29 +000055static loff_t nfs_llseek_dir(struct file *, loff_t, int);
Trond Myklebust11de3b12010-12-01 14:17:06 -050056static void nfs_readdir_clear_array(struct page*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080058const struct file_operations nfs_dir_operations = {
Trond Myklebustf0dd2132005-06-22 17:16:29 +000059 .llseek = nfs_llseek_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .read = generic_read_dir,
Trond Myklebust93a6ab72020-02-02 17:53:56 -050061 .iterate_shared = nfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .open = nfs_opendir,
Bryan Schumaker480c2002011-03-23 14:48:29 -040063 .release = nfs_closedir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .fsync = nfs_fsync_dir,
65};
66
Trond Myklebust11de3b12010-12-01 14:17:06 -050067const struct address_space_operations nfs_dir_aops = {
68 .freepage = nfs_readdir_clear_array,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -040069};
70
NeilBrown684f39b2018-12-03 11:30:30 +110071static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
Bryan Schumaker480c2002011-03-23 14:48:29 -040072{
Trond Myklebust311324a2014-02-07 17:02:08 -050073 struct nfs_inode *nfsi = NFS_I(dir);
Bryan Schumaker480c2002011-03-23 14:48:29 -040074 struct nfs_open_dir_context *ctx;
75 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76 if (ctx != NULL) {
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -040077 ctx->duped = 0;
Trond Myklebust311324a2014-02-07 17:02:08 -050078 ctx->attr_gencount = nfsi->attr_gencount;
Bryan Schumaker480c2002011-03-23 14:48:29 -040079 ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -040080 ctx->dup_cookie = 0;
NeilBrown684f39b2018-12-03 11:30:30 +110081 ctx->cred = get_cred(cred);
Trond Myklebust311324a2014-02-07 17:02:08 -050082 spin_lock(&dir->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -040083 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 Myklebust311324a2014-02-07 17:02:08 -050087 list_add(&ctx->list, &nfsi->open_files);
88 spin_unlock(&dir->i_lock);
Trond Myklebust0c030802011-07-30 12:45:35 -040089 return ctx;
90 }
91 return ERR_PTR(-ENOMEM);
Bryan Schumaker480c2002011-03-23 14:48:29 -040092}
93
Trond Myklebust311324a2014-02-07 17:02:08 -050094static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
Bryan Schumaker480c2002011-03-23 14:48:29 -040095{
Trond Myklebust311324a2014-02-07 17:02:08 -050096 spin_lock(&dir->i_lock);
97 list_del(&ctx->list);
98 spin_unlock(&dir->i_lock);
NeilBrown684f39b2018-12-03 11:30:30 +110099 put_cred(ctx->cred);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400100 kfree(ctx);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Open file
105 */
106static int
107nfs_opendir(struct inode *inode, struct file *filp)
108{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400109 int res = 0;
110 struct nfs_open_dir_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Al Viro6de14722013-09-16 10:53:17 -0400112 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
Chuck Levercc0dd2d2008-06-11 17:55:42 -0400113
114 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500115
NeilBrown684f39b2018-12-03 11:30:30 +1100116 ctx = alloc_nfs_open_dir_context(inode, current_cred());
Bryan Schumaker480c2002011-03-23 14:48:29 -0400117 if (IS_ERR(ctx)) {
118 res = PTR_ERR(ctx);
119 goto out;
120 }
121 filp->private_data = ctx;
Bryan Schumaker480c2002011-03-23 14:48:29 -0400122out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return res;
124}
125
Bryan Schumaker480c2002011-03-23 14:48:29 -0400126static int
127nfs_closedir(struct inode *inode, struct file *filp)
128{
Al Viroa4555892014-10-21 20:11:25 -0400129 put_nfs_open_dir_context(file_inode(filp), filp->private_data);
Bryan Schumaker480c2002011-03-23 14:48:29 -0400130 return 0;
131}
132
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400133struct nfs_cache_array_entry {
134 u64 cookie;
135 u64 ino;
136 struct qstr string;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500137 unsigned char d_type;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400138};
139
140struct nfs_cache_array {
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400141 u64 last_cookie;
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500142 unsigned int size;
143 unsigned char page_full : 1,
144 page_is_eof : 1;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -0500145 struct nfs_cache_array_entry array[];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400146};
147
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500148typedef struct nfs_readdir_descriptor {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct file *file;
150 struct page *page;
Al Viro23db8622013-05-17 16:34:50 -0400151 struct dir_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned long page_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500153 u64 dir_cookie;
Trond Myklebust0aded702010-11-30 21:56:32 -0500154 u64 last_cookie;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500155 u64 dup_cookie;
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000156 loff_t current_index;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500157 loff_t prev_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400158
Trond Myklebusta1147b82020-02-05 09:01:52 -0500159 unsigned long dir_verifier;
Neil Brown1f4eab72007-04-16 09:35:27 +1000160 unsigned long timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400161 unsigned long gencount;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500162 unsigned long attr_gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400163 unsigned int cache_entry_index;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500164 signed char duped;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400165 bool plus;
166 bool eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167} nfs_readdir_descriptor_t;
168
Trond Myklebust4b310312020-02-02 17:53:53 -0500169static
170void 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 Myklebust4b310312020-02-02 17:53:53 -0500176 kunmap_atomic(array);
177}
178
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400179/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400180 * we are freeing strings created by nfs_add_to_readdir_array()
181 */
182static
Trond Myklebust11de3b12010-12-01 14:17:06 -0500183void nfs_readdir_clear_array(struct page *page)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400184{
Trond Myklebust11de3b12010-12-01 14:17:06 -0500185 struct nfs_cache_array *array;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400186 int i;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500187
Cong Wang2b86ce22011-11-25 23:14:33 +0800188 array = kmap_atomic(page);
Benjamin Coddingtonb044f642017-03-10 17:07:46 -0500189 for (i = 0; i < array->size; i++)
190 kfree(array->array[i].string.name);
Trond Myklebust4b310312020-02-02 17:53:53 -0500191 array->size = 0;
Cong Wang2b86ce22011-11-25 23:14:33 +0800192 kunmap_atomic(array);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400193}
194
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500195static 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
201static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
202{
203 return array->page_full;
204}
205
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400206/*
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 */
211static
Trond Myklebust4a201d62010-10-23 14:53:23 -0400212int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400213{
214 string->len = len;
Trond Myklebust3803d672020-02-02 17:53:55 -0500215 string->name = kmemdup_nul(name, len, GFP_KERNEL);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400216 if (string->name == NULL)
217 return -ENOMEM;
Catalin Marinas04e4bd12010-11-11 12:53:47 +0000218 /*
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 Torvalds8387ff22016-06-10 07:51:30 -0700223 string->hash = full_name_hash(NULL, name, len);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400224 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400225}
226
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500227/*
228 * Check that the next array entry lies entirely within the page bounds
229 */
230static 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 Schumakerd1bacf92010-09-24 14:48:42 -0400244static
245int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
246{
Fabian Frederick0795bf82017-05-03 20:52:21 +0200247 struct nfs_cache_array *array = kmap(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400248 struct nfs_cache_array_entry *cache_entry;
249 int ret;
250
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500251 ret = nfs_readdir_array_can_expand(array);
252 if (ret)
Trond Myklebust30200932010-11-20 15:18:22 -0500253 goto out;
254
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500255 cache_entry = &array->array[array->size];
Trond Myklebust4a201d62010-10-23 14:53:23 -0400256 cache_entry->cookie = entry->prev_cookie;
257 cache_entry->ino = entry->ino;
Trond Myklebust0b26a0b2010-11-20 14:26:44 -0500258 cache_entry->d_type = entry->d_type;
Trond Myklebust4a201d62010-10-23 14:53:23 -0400259 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
260 if (ret)
261 goto out;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400262 array->last_cookie = entry->cookie;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500263 array->size++;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500264 if (entry->eof != 0)
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500265 nfs_readdir_array_set_eof(array);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400266out:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200267 kunmap(page);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400268 return ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400269}
270
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500271static 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 Myklebust59e356a2020-02-03 14:49:33 -0500280static inline
281int 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
290static
291bool 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 Schumakerd1bacf92010-09-24 14:48:42 -0400299static
300int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
301{
Al Viro23db8622013-05-17 16:34:50 -0400302 loff_t diff = desc->ctx->pos - desc->current_index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400303 unsigned int index;
304
305 if (diff < 0)
306 goto out_eof;
307 if (diff >= array->size) {
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500308 if (array->page_is_eof)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400309 goto out_eof;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400310 return -EAGAIN;
311 }
312
313 index = (unsigned int)diff;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500314 desc->dir_cookie = array->array[index].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400315 desc->cache_entry_index = index;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400316 return 0;
317out_eof:
Thomas Meyer6089dd02017-10-07 16:02:21 +0200318 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400319 return -EBADCOOKIE;
320}
321
Jeff Layton4db72b42014-01-28 13:47:46 -0500322static bool
323nfs_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 Schumakerd1bacf92010-09-24 14:48:42 -0400331static
332int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
333{
334 int i;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400335 loff_t new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400336 int status = -EAGAIN;
337
338 for (i = 0; i < array->size; i++) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500339 if (array->array[i].cookie == desc->dir_cookie) {
Al Viro496ad9a2013-01-23 17:07:38 -0500340 struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
Trond Myklebust0c030802011-07-30 12:45:35 -0400341
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400342 new_pos = desc->current_index + i;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500343 if (desc->attr_gencount != nfsi->attr_gencount ||
Jeff Layton4db72b42014-01-28 13:47:46 -0500344 !nfs_readdir_inode_mapping_valid(nfsi)) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500345 desc->duped = 0;
346 desc->attr_gencount = nfsi->attr_gencount;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500347 } else if (new_pos < desc->prev_index) {
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500348 if (desc->duped > 0
349 && desc->dup_cookie == desc->dir_cookie) {
Trond Myklebust0c030802011-07-30 12:45:35 -0400350 if (printk_ratelimit()) {
Al Viro6de14722013-09-16 10:53:17 -0400351 pr_notice("NFS: directory %pD2 contains a readdir loop."
Trond Myklebust0c030802011-07-30 12:45:35 -0400352 "Please contact your server vendor. "
Jeff Layton9581a4a2014-04-05 08:45:57 -0400353 "The file: %.*s has duplicate cookie %llu\n",
354 desc->file, array->array[i].string.len,
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500355 array->array[i].string.name, desc->dir_cookie);
Trond Myklebust0c030802011-07-30 12:45:35 -0400356 }
357 status = -ELOOP;
358 goto out;
359 }
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500360 desc->dup_cookie = desc->dir_cookie;
361 desc->duped = -1;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400362 }
Trond Myklebust59e356a2020-02-03 14:49:33 -0500363 if (nfs_readdir_use_cookie(desc->file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500364 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500365 else
366 desc->ctx->pos = new_pos;
367 desc->prev_index = new_pos;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400368 desc->cache_entry_index = i;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500369 return 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400370 }
371 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500372 if (array->page_is_eof) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500373 status = -EBADCOOKIE;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500374 if (desc->dir_cookie == array->last_cookie)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200375 desc->eof = true;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500376 }
Trond Myklebust0c030802011-07-30 12:45:35 -0400377out:
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400378 return status;
379}
380
381static
382int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
383{
384 struct nfs_cache_array *array;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500385 int status;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400386
Fabian Frederick0795bf82017-05-03 20:52:21 +0200387 array = kmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400388
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500389 if (desc->dir_cookie == 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400390 status = nfs_readdir_search_for_pos(array, desc);
391 else
392 status = nfs_readdir_search_for_cookie(array, desc);
393
Trond Myklebust47c716c2010-12-07 12:44:56 -0500394 if (status == -EAGAIN) {
Trond Myklebust0aded702010-11-30 21:56:32 -0500395 desc->last_cookie = array->last_cookie;
Trond Myklebuste47c0852011-03-23 08:43:09 -0400396 desc->current_index += array->size;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500397 desc->page_index++;
398 }
Fabian Frederick0795bf82017-05-03 20:52:21 +0200399 kunmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400400 return status;
401}
402
403/* Fill a page with xdr information before transferring to the cache page */
404static
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400405int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400406 struct nfs_entry *entry, struct file *file, struct inode *inode)
407{
Bryan Schumaker480c2002011-03-23 14:48:29 -0400408 struct nfs_open_dir_context *ctx = file->private_data;
NeilBrown684f39b2018-12-03 11:30:30 +1100409 const struct cred *cred = ctx->cred;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400410 unsigned long timestamp, gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int error;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 again:
414 timestamp = jiffies;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400415 gencount = nfs_inc_attr_generation_counter();
Trond Myklebusta1147b82020-02-05 09:01:52 -0500416 desc->dir_verifier = nfs_save_change_attribute(inode);
Miklos Szeredibe62a1a2016-03-26 16:14:39 -0400417 error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 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 Halevy3a10c302008-01-23 08:58:59 +0200423 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400424 desc->plus = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto again;
426 }
427 goto error;
428 }
Neil Brown1f4eab72007-04-16 09:35:27 +1000429 desc->timestamp = timestamp;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400430 desc->gencount = gencount;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400431error:
432 return error;
433}
434
Chuck Lever573c4e12010-12-14 14:58:11 +0000435static int xdr_decode(nfs_readdir_descriptor_t *desc,
436 struct nfs_entry *entry, struct xdr_stream *xdr)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400437{
Trond Myklebust59e356a2020-02-03 14:49:33 -0500438 struct inode *inode = file_inode(desc->file);
Chuck Lever573c4e12010-12-14 14:58:11 +0000439 int error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400440
Trond Myklebust59e356a2020-02-03 14:49:33 -0500441 error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
Chuck Lever573c4e12010-12-14 14:58:11 +0000442 if (error)
443 return error;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400444 entry->fattr->time_start = desc->timestamp;
445 entry->fattr->gencount = desc->gencount;
446 return 0;
447}
448
Trond Myklebustfa923362015-02-23 18:51:32 -0500449/* Match file and dirent using either filehandle or fileid
450 * Note: caller is responsible for checking the fsid
451 */
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400452static
453int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
454{
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400455 struct inode *inode;
Trond Myklebustfa923362015-02-23 18:51:32 -0500456 struct nfs_inode *nfsi;
457
David Howells2b0143b2015-03-17 22:25:59 +0000458 if (d_really_is_negative(dentry))
459 return 0;
Trond Myklebustfa923362015-02-23 18:51:32 -0500460
Trond Myklebustd8fdb472016-06-17 16:48:27 -0400461 inode = d_inode(dentry);
462 if (is_bad_inode(inode) || NFS_STALE(inode))
463 return 0;
464
465 nfsi = NFS_I(inode);
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400466 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 Schumakerd39ab9d2010-09-24 18:50:01 -0400471}
472
473static
Al Viro23db8622013-05-17 16:34:50 -0400474bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400475{
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 Viro23db8622013-05-17 16:34:50 -0400480 if (ctx->pos == 0)
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400481 return true;
482 return false;
483}
484
485/*
Trond Myklebust63519fb2016-11-19 11:21:54 -0500486 * 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 Myklebustd69ee9b2012-05-01 17:37:59 -0400488 * directory.
489 */
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400490void nfs_advise_use_readdirplus(struct inode *dir)
491{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500492 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 Myklebustd69ee9b2012-05-01 17:37:59 -0400497}
498
Trond Myklebust311324a2014-02-07 17:02:08 -0500499/*
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 */
507void nfs_force_use_readdirplus(struct inode *dir)
508{
Trond Myklebust63519fb2016-11-19 11:21:54 -0500509 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 Ngo227823d2020-01-22 20:45:39 -0500514 invalidate_mapping_pages(dir->i_mapping,
515 nfsi->page_index + 1, -1);
Trond Myklebust311324a2014-02-07 17:02:08 -0500516 }
517}
518
Trond Myklebustd69ee9b2012-05-01 17:37:59 -0400519static
Trond Myklebusta1147b82020-02-05 09:01:52 -0500520void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
521 unsigned long dir_verifier)
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400522{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700523 struct qstr filename = QSTR_INIT(entry->name, entry->len);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400524 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebust4a201d62010-10-23 14:53:23 -0400525 struct dentry *dentry;
526 struct dentry *alias;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400527 struct inode *inode;
David Quigleyaa9c2662013-05-22 12:50:44 -0400528 int status;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400529
Trond Myklebustfa923362015-02-23 18:51:32 -0500530 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
531 return;
Trond Myklebust6c441c22015-02-22 16:35:36 -0500532 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
533 return;
Trond Myklebust78d04af2016-09-20 14:34:24 -0400534 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 Myklebust4a201d62010-10-23 14:53:23 -0400542 if (filename.name[0] == '.') {
543 if (filename.len == 1)
544 return;
545 if (filename.len == 2 && filename.name[1] == '.')
546 return;
547 }
Linus Torvalds8387ff22016-06-10 07:51:30 -0700548 filename.hash = full_name_hash(parent, filename.name, filename.len);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400549
Trond Myklebust4a201d62010-10-23 14:53:23 -0400550 dentry = d_lookup(parent, &filename);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400551again:
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 Myklebust6c441c22015-02-22 16:35:36 -0500558 /* 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 Schumakerd39ab9d2010-09-24 18:50:01 -0400562 if (nfs_same_file(dentry, entry)) {
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400563 if (!entry->fh->size)
564 goto out;
Trond Myklebusta1147b82020-02-05 09:01:52 -0500565 nfs_set_verifier(dentry, dir_verifier);
David Howells2b0143b2015-03-17 22:25:59 +0000566 status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -0400567 if (!status)
David Howells2b0143b2015-03-17 22:25:59 +0000568 nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400569 goto out;
570 } else {
Eric W. Biederman5542aa22014-02-13 09:46:25 -0800571 d_invalidate(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400572 dput(dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400573 dentry = NULL;
574 goto again;
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400575 }
576 }
Trond Myklebust7dc72d52016-09-22 13:38:52 -0400577 if (!entry->fh->size) {
578 d_lookup_done(dentry);
579 goto out;
580 }
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400581
David Quigley1775fd32013-05-22 12:50:42 -0400582 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
Al Viro41d28bc2014-10-12 22:24:21 -0400583 alias = d_splice_alias(inode, dentry);
Al Viro9ac3d3e2016-04-28 19:52:56 -0400584 d_lookup_done(dentry);
585 if (alias) {
586 if (IS_ERR(alias))
587 goto out;
588 dput(dentry);
589 dentry = alias;
590 }
Trond Myklebusta1147b82020-02-05 09:01:52 -0500591 nfs_set_verifier(dentry, dir_verifier);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400592out:
593 dput(dentry);
Bryan Schumakerd39ab9d2010-09-24 18:50:01 -0400594}
595
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400596/* Perform conversion from xdr to cache array */
597static
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500598int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
Trond Myklebust66502392011-01-08 17:45:38 -0500599 struct page **xdr_pages, struct page *page, unsigned int buflen)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400600{
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400601 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400602 struct xdr_buf buf;
Trond Myklebust66502392011-01-08 17:45:38 -0500603 struct page *scratch;
Trond Myklebust5c346852010-11-20 12:43:45 -0500604 unsigned int count = 0;
605 int status;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400606
Trond Myklebust66502392011-01-08 17:45:38 -0500607 scratch = alloc_page(GFP_KERNEL);
608 if (scratch == NULL)
609 return -ENOMEM;
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400610
Benjamin Coddingtonce85cfb2015-04-21 14:17:35 -0400611 if (buflen == 0)
612 goto out_nopages;
613
Benny Halevyf7da7a12011-05-19 14:16:47 -0400614 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
Trond Myklebust66502392011-01-08 17:45:38 -0500615 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Bryan Schumaker99424382010-10-21 16:33:16 -0400616
617 do {
Jeffrey Mitchelld33030e2020-09-15 16:42:52 -0500618 if (entry->label)
619 entry->label->len = NFS4_MAXLABELLEN;
620
Bryan Schumaker99424382010-10-21 16:33:16 -0400621 status = xdr_decode(desc, entry, &stream);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500622 if (status != 0) {
623 if (status == -EAGAIN)
624 status = 0;
Bryan Schumaker99424382010-10-21 16:33:16 -0400625 break;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500626 }
Bryan Schumaker99424382010-10-21 16:33:16 -0400627
Trond Myklebust5c346852010-11-20 12:43:45 -0500628 count++;
629
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400630 if (desc->plus)
Trond Myklebusta1147b82020-02-05 09:01:52 -0500631 nfs_prime_dcache(file_dentry(desc->file), entry,
632 desc->dir_verifier);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500633
Max Kellermanndb531db2019-07-12 16:18:06 +0200634 status = nfs_readdir_add_to_array(entry, page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500635 if (status != 0)
636 break;
Bryan Schumaker99424382010-10-21 16:33:16 -0400637 } while (!entry->eof);
638
Benjamin Coddingtonce85cfb2015-04-21 14:17:35 -0400639out_nopages:
Trond Myklebust47c716c2010-12-07 12:44:56 -0500640 if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500641 nfs_readdir_page_set_eof(page);
Fabian Frederick0795bf82017-05-03 20:52:21 +0200642 status = 0;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400643 }
Trond Myklebust66502392011-01-08 17:45:38 -0500644
645 put_page(scratch);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500646 return status;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400647}
648
649static
Anna Schumakerc7e96682015-07-13 14:01:25 -0400650void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400651{
652 unsigned int i;
653 for (i = 0; i < npages; i++)
654 put_page(pages[i]);
655}
656
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400657/*
zhangliguangbf211ca2019-02-16 08:38:40 +0800658 * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
659 * to nfs_readdir_free_pages()
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400660 */
661static
Anna Schumakerc7e96682015-07-13 14:01:25 -0400662int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400663{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400664 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 Myklebust66502392011-01-08 17:45:38 -0500672 return 0;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400673
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400674out_freepages:
Anna Schumakerc7e96682015-07-13 14:01:25 -0400675 nfs_readdir_free_pages(pages, i);
Trond Myklebust66502392011-01-08 17:45:38 -0500676 return -ENOMEM;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400677}
678
679static
680int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
681{
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400682 struct page *pages[NFS_MAX_READDIR_PAGES];
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400683 struct nfs_entry entry;
684 struct file *file = desc->file;
685 struct nfs_cache_array *array;
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500686 int status = -ENOMEM;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400687 unsigned int array_size = ARRAY_SIZE(pages);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400688
Trond Myklebust4b310312020-02-02 17:53:53 -0500689 nfs_readdir_init_array(page);
690
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400691 entry.prev_cookie = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500692 entry.cookie = desc->last_cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400693 entry.eof = 0;
694 entry.fh = nfs_alloc_fhandle();
695 entry.fattr = nfs_alloc_fattr();
Chuck Lever573c4e12010-12-14 14:58:11 +0000696 entry.server = NFS_SERVER(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400697 if (entry.fh == NULL || entry.fattr == NULL)
698 goto out;
699
David Quigley14c43f72013-05-22 12:50:43 -0400700 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 Frederick0795bf82017-05-03 20:52:21 +0200706 array = kmap(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400707
Anna Schumakerc7e96682015-07-13 14:01:25 -0400708 status = nfs_readdir_alloc_pages(pages, array_size);
Trond Myklebust66502392011-01-08 17:45:38 -0500709 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400710 goto out_release_array;
711 do {
Trond Myklebustac396122010-11-15 20:26:22 -0500712 unsigned int pglen;
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400713 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
Bryan Schumakerbabddc72010-10-20 15:44:29 -0400714
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400715 if (status < 0)
716 break;
Trond Myklebustac396122010-11-15 20:26:22 -0500717 pglen = status;
Trond Myklebust66502392011-01-08 17:45:38 -0500718 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500719 if (status < 0) {
720 if (status == -ENOSPC)
721 status = 0;
722 break;
723 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500724 } while (!nfs_readdir_array_is_full(array));
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400725
Anna Schumakerc7e96682015-07-13 14:01:25 -0400726 nfs_readdir_free_pages(pages, array_size);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400727out_release_array:
Fabian Frederick0795bf82017-05-03 20:52:21 +0200728 kunmap(page);
David Quigley14c43f72013-05-22 12:50:43 -0400729 nfs4_label_free(entry.label);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400730out:
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 */
742static
Christoph Hellwiga46126c2019-05-01 12:06:35 -0400743int nfs_readdir_filler(void *data, struct page* page)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400744{
Christoph Hellwiga46126c2019-05-01 12:06:35 -0400745 nfs_readdir_descriptor_t *desc = data;
Al Viro496ad9a2013-01-23 17:07:38 -0500746 struct inode *inode = file_inode(desc->file);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500747 int ret;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400748
Max Kellermanndb531db2019-07-12 16:18:06 +0200749 ret = nfs_readdir_xdr_to_array(desc, page, inode);
750 if (ret < 0)
751 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 SetPageUptodate(page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400753
Trond Myklebust2aac05a2008-07-07 13:26:10 -0400754 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700755 /* Should never happen */
756 nfs_zap_mapping(inode, inode->i_mapping);
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 unlock_page(page);
759 return 0;
760 error:
Trond Myklebust4b310312020-02-02 17:53:53 -0500761 nfs_readdir_clear_array(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 unlock_page(page);
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400766static
767void cache_page_release(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300769 put_page(desc->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 desc->page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400773static
774struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Christoph Hellwiga46126c2019-05-01 12:06:35 -0400776 return read_cache_page(desc->file->f_mapping, desc->page_index,
777 nfs_readdir_filler, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/*
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400781 * Returns 0 if desc->dir_cookie was found on page desc->page_index
Trond Myklebust114de382020-02-02 17:53:54 -0500782 * and locks the page to prevent removal from the page cache.
Olivier Galibert00a92642005-06-22 17:16:29 +0000783 */
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400784static
Trond Myklebust114de382020-02-02 17:53:54 -0500785int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
Olivier Galibert00a92642005-06-22 17:16:29 +0000786{
Dai Ngo227823d2020-01-22 20:45:39 -0500787 struct inode *inode = file_inode(desc->file);
788 struct nfs_inode *nfsi = NFS_I(inode);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400789 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000790
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400791 desc->page = get_cache_page(desc);
792 if (IS_ERR(desc->page))
793 return PTR_ERR(desc->page);
Trond Myklebust114de382020-02-02 17:53:54 -0500794 res = lock_page_killable(desc->page);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500795 if (res != 0)
Trond Myklebust114de382020-02-02 17:53:54 -0500796 goto error;
797 res = -EAGAIN;
798 if (desc->page->mapping != NULL) {
799 res = nfs_readdir_search_array(desc);
Dai Ngo227823d2020-01-22 20:45:39 -0500800 if (res == 0) {
801 nfsi->page_index = desc->page_index;
Trond Myklebust114de382020-02-02 17:53:54 -0500802 return 0;
Dai Ngo227823d2020-01-22 20:45:39 -0500803 }
Trond Myklebust114de382020-02-02 17:53:54 -0500804 }
805 unlock_page(desc->page);
806error:
807 cache_page_release(desc);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400808 return res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000809}
810
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400811/* Search for desc->dir_cookie from the beginning of the page cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812static inline
813int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
814{
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500815 int res;
Olivier Galibert00a92642005-06-22 17:16:29 +0000816
Trond Myklebust0aded702010-11-30 21:56:32 -0500817 if (desc->page_index == 0) {
Trond Myklebust8cd51a02010-11-15 20:26:22 -0500818 desc->current_index = 0;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500819 desc->prev_index = 0;
Trond Myklebust0aded702010-11-30 21:56:32 -0500820 desc->last_cookie = 0;
821 }
Trond Myklebust47c716c2010-12-07 12:44:56 -0500822 do {
Trond Myklebust114de382020-02-02 17:53:54 -0500823 res = find_and_lock_cache_page(desc);
Trond Myklebust47c716c2010-12-07 12:44:56 -0500824 } while (res == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return res;
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/*
829 * Once we've found the start of the dirent within a page: fill 'er up...
830 */
831static
Al Viro23db8622013-05-17 16:34:50 -0400832int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct file *file = desc->file;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400835 int i = 0;
836 int res = 0;
837 struct nfs_cache_array *array = NULL;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -0400838
Fabian Frederick0795bf82017-05-03 20:52:21 +0200839 array = kmap(desc->page);
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400840 for (i = desc->cache_entry_index; i < array->size; i++) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500841 struct nfs_cache_array_entry *ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Trond Myklebustece0b422010-11-20 13:55:33 -0500843 ent = &array->array[i];
Al Viro23db8622013-05-17 16:34:50 -0400844 if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
845 nfs_compat_user_ino64(ent->ino), ent->d_type)) {
Thomas Meyer6089dd02017-10-07 16:02:21 +0200846 desc->eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 break;
Trond Myklebustece0b422010-11-20 13:55:33 -0500848 }
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400849 if (i < (array->size-1))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500850 desc->dir_cookie = array->array[i+1].cookie;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400851 else
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500852 desc->dir_cookie = array->last_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500853 if (nfs_readdir_use_cookie(file))
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500854 desc->ctx->pos = desc->dir_cookie;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500855 else
856 desc->ctx->pos++;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500857 if (desc->duped != 0)
858 desc->duped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Trond Myklebustb1e21c92020-11-01 13:45:55 -0500860 if (array->page_is_eof)
Thomas Meyer6089dd02017-10-07 16:02:21 +0200861 desc->eof = true;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400862
Fabian Frederick0795bf82017-05-03 20:52:21 +0200863 kunmap(desc->page);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500864 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500865 (unsigned long long)desc->dir_cookie, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 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 */
881static inline
Al Viro23db8622013-05-17 16:34:50 -0400882int uncached_readdir(nfs_readdir_descriptor_t *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct page *page = NULL;
885 int status;
Al Viro496ad9a2013-01-23 17:07:38 -0500886 struct inode *inode = file_inode(desc->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500888 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500889 (unsigned long long)desc->dir_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 page = alloc_page(GFP_HIGHUSER);
892 if (!page) {
893 status = -ENOMEM;
894 goto out;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500897 desc->page_index = 0;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500898 desc->last_cookie = desc->dir_cookie;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500899 desc->page = page;
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500900 desc->duped = 0;
Trond Myklebust7a8e1dc2010-11-20 13:24:46 -0500901
Trond Myklebust85f86072010-11-20 13:24:49 -0500902 status = nfs_readdir_xdr_to_array(desc, page, inode);
903 if (status < 0)
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400904 goto out_release;
Bryan Schumakerd1bacf92010-09-24 14:48:42 -0400905
Al Viro23db8622013-05-17 16:34:50 -0400906 status = nfs_do_filldir(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trond Myklebust114de382020-02-02 17:53:54 -0500908 out_release:
909 nfs_readdir_clear_array(desc->page);
910 cache_page_release(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500912 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700913 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Olivier Galibert00a92642005-06-22 17:16:29 +0000917/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700920 */
Al Viro23db8622013-05-17 16:34:50 -0400921static int nfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Miklos Szeredibe62a1a2016-03-26 16:14:39 -0400923 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +0000924 struct inode *inode = d_inode(dentry);
Al Viro23db8622013-05-17 16:34:50 -0400925 struct nfs_open_dir_context *dir_ctx = file->private_data;
Trond Myklebust59e356a2020-02-03 14:49:33 -0500926 nfs_readdir_descriptor_t my_desc = {
927 .file = file,
928 .ctx = ctx,
Trond Myklebust59e356a2020-02-03 14:49:33 -0500929 .plus = nfs_use_readdirplus(inode, ctx),
930 },
931 *desc = &my_desc;
Scott Mayhew07b5ce82013-07-05 17:49:31 -0400932 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Al Viro6de14722013-09-16 10:53:17 -0400934 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
935 file, (long long)ctx->pos);
Chuck Lever91d5b472006-03-20 13:44:14 -0500936 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
Al Viro23db8622013-05-17 16:34:50 -0400939 * ctx->pos points to the dirent entry number.
Trond Myklebustf0dd2132005-06-22 17:16:29 +0000940 * *desc->dir_cookie has the cookie for the next entry. We have
Olivier Galibert00a92642005-06-22 17:16:29 +0000941 * to either find the entry with the appropriate number or
942 * revalidate the cookie.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
Trond Myklebust79f687a32016-11-19 10:54:55 -0500944 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
Scott Mayhew07b5ce82013-07-05 17:49:31 -0400945 res = nfs_revalidate_mapping(inode, file->f_mapping);
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500946 if (res < 0)
947 goto out;
948
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500949 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 Myklebust47c716c2010-12-07 12:44:56 -0500956 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 res = readdir_search_pagecache(desc);
Olivier Galibert00a92642005-06-22 17:16:29 +0000958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (res == -EBADCOOKIE) {
Trond Myklebustece0b422010-11-20 13:55:33 -0500960 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* This means either end of directory */
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500962 if (desc->dir_cookie && !desc->eof) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* Or that the server has 'lost' a cookie */
Al Viro23db8622013-05-17 16:34:50 -0400964 res = uncached_readdir(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -0500965 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 continue;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 break;
969 }
970 if (res == -ETOOSMALL && desc->plus) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200971 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 nfs_zap_caches(inode);
Trond Myklebustbaf57a02010-09-24 18:49:43 -0400973 desc->page_index = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400974 desc->plus = false;
975 desc->eof = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 continue;
977 }
978 if (res < 0)
979 break;
980
Al Viro23db8622013-05-17 16:34:50 -0400981 res = nfs_do_filldir(desc);
Trond Myklebust114de382020-02-02 17:53:54 -0500982 unlock_page(desc->page);
983 cache_page_release(desc);
Trond Myklebustece0b422010-11-20 13:55:33 -0500984 if (res < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
Trond Myklebust47c716c2010-12-07 12:44:56 -0500986 } while (!desc->eof);
Trond Myklebust2e7a4642020-11-01 09:56:18 -0500987
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 Myklebustfccca7f2008-01-26 17:37:47 -0500995out:
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500996 if (res > 0)
997 res = 0;
Al Viro6de14722013-09-16 10:53:17 -0400998 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -0500999 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Andrew Morton965c8e52012-12-17 15:59:39 -08001002static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001003{
Bryan Schumaker480c2002011-03-23 14:48:29 -04001004 struct nfs_open_dir_context *dir_ctx = filp->private_data;
Chuck Leverb84e06c2008-06-11 17:55:34 -04001005
Al Viro6de14722013-09-16 10:53:17 -04001006 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1007 filp, offset, whence);
Chuck Leverb84e06c2008-06-11 17:55:34 -04001008
Andrew Morton965c8e52012-12-17 15:59:39 -08001009 switch (whence) {
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001010 default:
1011 return -EINVAL;
1012 case SEEK_SET:
1013 if (offset < 0)
1014 return -EINVAL;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001015 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001016 break;
1017 case SEEK_CUR:
1018 if (offset == 0)
1019 return filp->f_pos;
Trond Myklebust83f2c452020-10-30 17:57:29 -04001020 spin_lock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001021 offset += filp->f_pos;
1022 if (offset < 0) {
Trond Myklebust83f2c452020-10-30 17:57:29 -04001023 spin_unlock(&filp->f_lock);
Trond Myklebustb2b1ff32018-06-27 16:25:40 -04001024 return -EINVAL;
1025 }
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001026 }
1027 if (offset != filp->f_pos) {
1028 filp->f_pos = offset;
Trond Myklebust59e356a2020-02-03 14:49:33 -05001029 if (nfs_readdir_use_cookie(filp))
1030 dir_ctx->dir_cookie = offset;
1031 else
1032 dir_ctx->dir_cookie = 0;
Bryan Schumaker8ef2ce32011-03-23 15:04:31 -04001033 dir_ctx->duped = 0;
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001034 }
Trond Myklebust83f2c452020-10-30 17:57:29 -04001035 spin_unlock(&filp->f_lock);
Trond Myklebustf0dd2132005-06-22 17:16:29 +00001036 return offset;
1037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/*
1040 * All directory operations under NFS are synchronous, so fsync()
1041 * is a dummy operation.
1042 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001043static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1044 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Al Viro6de14722013-09-16 10:53:17 -04001046 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001047
Trond Myklebust11decaf2020-10-30 17:57:30 -04001048 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050}
1051
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001052/**
1053 * nfs_force_lookup_revalidate - Mark the directory as having changed
Trond Myklebust302fad72019-02-18 13:32:38 -05001054 * @dir: pointer to directory inode
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001055 *
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 Myklebustefeda802020-02-05 09:01:54 -05001060 * 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 Myklebustbfc69a42007-10-15 18:18:29 -04001063 * The caller should be holding dir->i_lock
1064 */
1065void nfs_force_lookup_revalidate(struct inode *dir)
1066{
Trond Myklebustefeda802020-02-05 09:01:54 -05001067 NFS_I(dir)->cache_change_attribute += 2;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001068}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001069EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001070
Trond Myklebustefeda802020-02-05 09:01:54 -05001071/**
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 */
1080static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1081{
1082 return (verf & ~1UL) == nfs_save_change_attribute(dir);
1083}
1084
1085static void nfs_set_verifier_delegated(unsigned long *verf)
1086{
1087 *verf |= 1UL;
1088}
1089
1090#if IS_ENABLED(CONFIG_NFS_V4)
1091static void nfs_unset_verifier_delegated(unsigned long *verf)
1092{
1093 *verf &= ~1UL;
1094}
1095#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1096
1097static bool nfs_test_verifier_delegated(unsigned long verf)
1098{
1099 return verf & 1;
1100}
1101
1102static bool nfs_verifier_is_delegated(struct dentry *dentry)
1103{
1104 return nfs_test_verifier_delegated(dentry->d_time);
1105}
1106
1107static 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);
1116out:
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 */
1130void 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}
1137EXPORT_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 */
1150void 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}
1164EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1165#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167/*
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.
NeilBrown912a1082014-07-14 11:28:20 +10001171 * If rcu_walk prevents us from performing a full check, return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
NeilBrown912a1082014-07-14 11:28:20 +10001173static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1174 int rcu_walk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 if (IS_ROOT(dentry))
1177 return 1;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001178 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1179 return 0;
Trond Myklebustf2c77f42007-10-02 12:54:39 -04001180 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1181 return 0;
1182 /* Revalidate nfsi->cache_change_attribute before we declare a match */
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001183 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 Myklebustf2c77f42007-10-02 12:54:39 -04001189 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1190 return 0;
1191 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
Trond Myklebusta12802c2007-10-02 19:13:04 -04001195 * Use intent information to check whether or not we're going to do
1196 * an O_EXCL create using this path component.
1197 */
Al Virofa3c56b2012-06-10 15:36:40 -04001198static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
Trond Myklebusta12802c2007-10-02 19:13:04 -04001199{
1200 if (NFS_PROTO(dir)->version == 2)
1201 return 0;
Al Virofa3c56b2012-06-10 15:36:40 -04001202 return flags & LOOKUP_EXCL;
Trond Myklebusta12802c2007-10-02 19:13:04 -04001203}
1204
1205/*
Trond Myklebust1d6757f2005-06-07 18:37:01 -04001206 * 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 Myklebust65a0c142012-12-14 17:51:40 -05001213static
Al Virofa3c56b2012-06-10 15:36:40 -04001214int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust65a0c142012-12-14 17:51:40 -05001217 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
David Howells36d43a42011-01-14 18:45:42 +00001219 if (IS_AUTOMOUNT(inode))
Trond Myklebust4e99a1f2008-03-06 12:34:59 -05001220 return 0;
Trond Myklebust47921922018-05-10 10:08:36 -04001221
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. Silvadf561f662020-08-23 17:36:59 -05001228 fallthrough;
Trond Myklebust47921922018-05-10 10:08:36 -04001229 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 Virofacc3532012-06-10 15:33:51 -04001237 /* VFS wants an on-the-wire revalidation */
Al Virofa3c56b2012-06-10 15:36:40 -04001238 if (flags & LOOKUP_REVAL)
Al Virofacc3532012-06-10 15:33:51 -04001239 goto out_force;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001240out:
Lance Sheltona61246c2018-07-16 13:05:36 -04001241 return (inode->i_nlink == 0) ? -ESTALE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242out_force:
NeilBrown1fa1e382014-07-14 11:28:20 +10001243 if (flags & LOOKUP_RCU)
1244 return -ECHILD;
Trond Myklebust65a0c142012-12-14 17:51:40 -05001245 ret = __nfs_revalidate_inode(server, inode);
1246 if (ret != 0)
1247 return ret;
1248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
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.
NeilBrown912a1082014-07-14 11:28:20 +10001257 *
1258 * If LOOKUP_RCU prevents us from performing a full check, return 1
1259 * suggesting a reval is needed.
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001260 *
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 Torvalds1da177e2005-04-16 15:20:36 -07001263 */
1264static inline
1265int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
Al Virofa3c56b2012-06-10 15:36:40 -04001266 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001268 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
Trond Myklebust4eec9522008-07-15 17:58:13 -04001270 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1271 return 1;
NeilBrown912a1082014-07-14 11:28:20 +10001272 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001275static int
1276nfs_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
1307static int
1308nfs_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
1320static int
1321nfs_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
1328static int
1329nfs_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 Myklebusta1147b82020-02-05 09:01:52 -05001335 unsigned long dir_verifier;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001336 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 Myklebusta1147b82020-02-05 09:01:52 -05001345 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001346 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001347 if (ret < 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001348 switch (ret) {
1349 case -ESTALE:
1350 case -ENOENT:
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001351 ret = 0;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001352 break;
1353 case -ETIMEDOUT:
1354 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1355 ret = 1;
1356 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001357 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 Myklebusta1147b82020-02-05 09:01:52 -05001366 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001367
1368 /* set a readdirplus hint that we had a cache miss */
1369 nfs_force_use_readdirplus(dir);
1370 ret = 1;
1371out:
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 Torvalds1da177e2005-04-16 15:20:36 -07001378/*
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 Myklebust5ceb9d72018-09-28 09:04:05 -04001389static int
1390nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1391 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Chuck Lever91d5b472006-03-20 13:44:14 -05001396 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
David Howells2b0143b2015-03-17 22:25:59 +00001397 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001399 if (!inode)
1400 return nfs_lookup_revalidate_negative(dir, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001403 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1404 __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 goto out_bad;
1406 }
1407
Trond Myklebustefeda802020-02-05 09:01:54 -05001408 if (nfs_verifier_is_delegated(dentry))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001409 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust15860ab2008-12-23 15:21:54 -05001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* Force a full look up iff the parent directory has changed */
Trond Myklebust73dd6842018-05-10 10:13:09 -04001412 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
NeilBrown912a1082014-07-14 11:28:20 +10001413 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
NeilBrowncc896842017-07-05 12:22:20 +10001414 error = nfs_lookup_verify_inode(inode, flags);
1415 if (error) {
NeilBrowncc896842017-07-05 12:22:20 +10001416 if (error == -ESTALE)
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001417 nfs_zap_caches(dir);
1418 goto out_bad;
NeilBrown1fa1e382014-07-14 11:28:20 +10001419 }
Trond Myklebust63519fb2016-11-19 11:21:54 -05001420 nfs_advise_use_readdirplus(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto out_valid;
1422 }
1423
NeilBrown912a1082014-07-14 11:28:20 +10001424 if (flags & LOOKUP_RCU)
1425 return -ECHILD;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (NFS_STALE(inode))
1428 goto out_bad;
1429
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001430 trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001431 error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001432 trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001433 return error;
1434out_valid:
1435 return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1436out_bad:
1437 if (flags & LOOKUP_RCU)
1438 return -ECHILD;
1439 return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001442static int
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001443__nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1444 int (*reval)(struct inode *, struct dentry *, unsigned int))
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001445{
1446 struct dentry *parent;
1447 struct inode *dir;
1448 int ret;
David Quigleyaa9c2662013-05-22 12:50:44 -04001449
NeilBrownd51ac1a2014-07-14 11:28:20 +10001450 if (flags & LOOKUP_RCU) {
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001451 parent = READ_ONCE(dentry->d_parent);
1452 dir = d_inode_rcu(parent);
1453 if (!dir)
1454 return -ECHILD;
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001455 ret = reval(dir, dentry, flags);
Mark Rutland6aa7de02017-10-23 14:07:29 -07001456 if (parent != READ_ONCE(dentry->d_parent))
NeilBrownd51ac1a2014-07-14 11:28:20 +10001457 return -ECHILD;
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001458 } else {
1459 parent = dget_parent(dentry);
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001460 ret = reval(d_inode(parent), dentry, flags);
NeilBrownd51ac1a2014-07-14 11:28:20 +10001461 dput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Trond Myklebust5ceb9d72018-09-28 09:04:05 -04001463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001466static 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 Torvalds1da177e2005-04-16 15:20:36 -07001471/*
David Howells2b0143b2015-03-17 22:25:59 +00001472 * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001473 * 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 */
1480static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1481{
David Howells2b0143b2015-03-17 22:25:59 +00001482 struct inode *inode = d_inode(dentry);
Trond Myklebust9cdd1d32016-12-16 18:04:47 -05001483 int error = 0;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001484
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 Viro6de14722013-09-16 10:53:17 -04001491 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1492 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001493 return 1;
1494 }
1495
1496 if (is_bad_inode(inode)) {
Al Viro6de14722013-09-16 10:53:17 -04001497 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1498 __func__, dentry);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001499 return 0;
1500 }
1501
NeilBrownb6887412017-08-25 17:34:41 +10001502 error = nfs_lookup_verify_inode(inode, flags);
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001503 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1504 __func__, inode->i_ino, error ? "invalid" : "valid");
1505 return !error;
1506}
1507
1508/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * This is called from dput() when d_count is going to 0.
1510 */
Nick Pigginfe15ce42011-01-07 17:49:23 +11001511static int nfs_dentry_delete(const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Al Viro6de14722013-09-16 10:53:17 -04001513 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1514 dentry, dentry->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Trond Myklebust77f11192008-01-28 19:43:19 -05001516 /* Unhash any dentry with a stale inode */
David Howells2b0143b2015-03-17 22:25:59 +00001517 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
Trond Myklebust77f11192008-01-28 19:43:19 -05001518 return 1;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1521 /* Unhash it, so that ->d_iput() would be called */
1522 return 1;
1523 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001524 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* 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 Myklebust1f018452012-12-14 16:38:46 -05001533/* Ensure that we revalidate inode->i_nlink */
Trond Myklebust1b83d702008-06-11 15:44:04 -04001534static void nfs_drop_nlink(struct inode *inode)
1535{
1536 spin_lock(&inode->i_lock);
Trond Myklebust1f018452012-12-14 16:38:46 -05001537 /* drop the inode if we're reasonably sure this is the last link */
Trond Myklebust59a707b2018-04-08 18:11:18 -04001538 if (inode->i_nlink > 0)
1539 drop_nlink(inode);
1540 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust16e14372018-03-20 16:53:31 -04001541 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
1542 | NFS_INO_INVALID_CTIME
Trond Myklebust59a707b2018-04-08 18:11:18 -04001543 | NFS_INO_INVALID_OTHER
1544 | NFS_INO_REVAL_FORCED;
Trond Myklebust1b83d702008-06-11 15:44:04 -04001545 spin_unlock(&inode->i_lock);
1546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/*
1549 * Called when the dentry loses inode.
1550 * We use it to clean up silly-renamed files.
1551 */
1552static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1553{
Neil Brown83672d32007-02-26 12:48:25 +11001554 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 Torvalds1da177e2005-04-16 15:20:36 -07001558 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04001559 nfs_complete_unlink(dentry, inode);
Trond Myklebust1f018452012-12-14 16:38:46 -05001560 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 iput(inode);
1563}
1564
Al Virob1942c52011-03-16 05:44:14 -04001565static 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 Virof786aa92009-02-20 05:51:22 +00001576const struct dentry_operations nfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 .d_revalidate = nfs_lookup_revalidate,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001578 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 .d_delete = nfs_dentry_delete,
1580 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001581 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001582 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583};
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001584EXPORT_SYMBOL_GPL(nfs_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Bryan Schumaker597d9282012-07-16 16:39:10 -04001586struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct dentry *res;
1589 struct inode *inode = NULL;
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001590 struct nfs_fh *fhandle = NULL;
1591 struct nfs_fattr *fattr = NULL;
David Quigley1775fd32013-05-22 12:50:42 -04001592 struct nfs4_label *label = NULL;
Trond Myklebusta1147b82020-02-05 09:01:52 -05001593 unsigned long dir_verifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Al Viro6de14722013-09-16 10:53:17 -04001596 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
Chuck Lever91d5b472006-03-20 13:44:14 -05001597 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Al Viro130f9ab2016-03-07 22:40:43 -05001599 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1600 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Trond Myklebustfd684072006-09-05 12:27:44 -04001602 /*
1603 * If we're doing an exclusive create, optimize away the lookup
1604 * but don't hash the dentry.
1605 */
Trond Myklebust9f6d44d2018-05-10 10:34:21 -04001606 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
Al Viro130f9ab2016-03-07 22:40:43 -05001607 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001609 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 Quigley14c43f72013-05-22 12:50:43 -04001615 label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
1616 if (IS_ERR(label))
1617 goto out;
1618
Trond Myklebusta1147b82020-02-05 09:01:52 -05001619 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001620 trace_nfs_lookup_enter(dir, dentry, flags);
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001621 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (error == -ENOENT)
1623 goto no_entry;
1624 if (error < 0) {
1625 res = ERR_PTR(error);
Al Virobf130912016-05-29 15:14:14 -04001626 goto out_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
David Quigley1775fd32013-05-22 12:50:42 -04001628 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
Namhyung Kimbf0c84f2010-12-28 17:02:46 +00001629 res = ERR_CAST(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001630 if (IS_ERR(res))
Al Virobf130912016-05-29 15:14:14 -04001631 goto out_label;
David Howells54ceac42006-08-22 20:06:13 -04001632
Trond Myklebust63519fb2016-11-19 11:21:54 -05001633 /* Notify readdir to use READDIRPLUS */
1634 nfs_force_use_readdirplus(dir);
Trond Myklebustd69ee9b2012-05-01 17:37:59 -04001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636no_entry:
Al Viro41d28bc2014-10-12 22:24:21 -04001637 res = d_splice_alias(inode, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001638 if (res != NULL) {
1639 if (IS_ERR(res))
Al Virobf130912016-05-29 15:14:14 -04001640 goto out_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 dentry = res;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001642 }
Trond Myklebusta1147b82020-02-05 09:01:52 -05001643 nfs_set_verifier(dentry, dir_verifier);
Al Virobf130912016-05-29 15:14:14 -04001644out_label:
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001645 trace_nfs_lookup_exit(dir, dentry, flags, error);
David Quigley14c43f72013-05-22 12:50:43 -04001646 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647out:
Trond Myklebuste1fb4d02010-04-16 16:22:47 -04001648 nfs_free_fattr(fattr);
1649 nfs_free_fhandle(fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return res;
1651}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001652EXPORT_SYMBOL_GPL(nfs_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001654#if IS_ENABLED(CONFIG_NFS_V4)
Al Viro0b728e12012-06-10 16:03:43 -04001655static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Al Virof786aa92009-02-20 05:51:22 +00001657const struct dentry_operations nfs4_dentry_operations = {
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001658 .d_revalidate = nfs4_lookup_revalidate,
NeilBrownb6887412017-08-25 17:34:41 +10001659 .d_weak_revalidate = nfs_weak_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 .d_delete = nfs_dentry_delete,
1661 .d_iput = nfs_dentry_iput,
David Howells36d43a42011-01-14 18:45:42 +00001662 .d_automount = nfs_d_automount,
Al Virob1942c52011-03-16 05:44:14 -04001663 .d_release = nfs_d_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664};
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001665EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Al Viro8a5e9292011-06-25 19:15:54 -04001667static 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
NeilBrown532d4de2016-10-13 15:26:47 +11001677static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001678{
NeilBrown532d4de2016-10-13 15:26:47 +11001679 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001680}
1681
1682static int do_open(struct inode *inode, struct file *filp)
1683{
David Howellsf1fe29b2013-09-27 11:20:03 +01001684 nfs_fscache_open_file(inode, filp);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001685 return 0;
1686}
1687
Al Virod9585272012-06-22 12:39:14 +04001688static int nfs_finish_open(struct nfs_open_context *ctx,
1689 struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -04001690 struct file *file, unsigned open_flags)
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001691{
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001692 int err;
1693
Al Virobe12af32018-06-08 11:44:56 -04001694 err = finish_open(file, dentry, do_open);
Al Viro30d90492012-06-22 12:40:19 +04001695 if (err)
Al Virod9585272012-06-22 12:39:14 +04001696 goto out;
NeilBrowneaa2b822017-07-03 15:27:26 +10001697 if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
1698 nfs_file_set_open_context(file, ctx);
1699 else
Trond Myklebust98214212019-08-09 12:15:07 -04001700 err = -EOPENSTALE;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001701out:
Al Virod9585272012-06-22 12:39:14 +04001702 return err;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001703}
1704
Bryan Schumaker73a79702012-07-16 16:39:12 -04001705int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1706 struct file *file, unsigned open_flags,
Al Viro44907d72018-06-08 13:32:02 -04001707 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Al Viroc94c0952016-07-05 09:49:21 -04001709 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001710 struct nfs_open_context *ctx;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001711 struct dentry *res;
1712 struct iattr attr = { .ia_valid = ATTR_OPEN };
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001713 struct inode *inode;
Trond Myklebust1472b832013-08-20 11:59:41 -04001714 unsigned int lookup_flags = 0;
Al Viroc94c0952016-07-05 09:49:21 -04001715 bool switched = false;
Al Viro73a09dd2018-06-08 13:22:02 -04001716 int created = 0;
Trond Myklebust898f6352010-10-23 11:24:25 -04001717 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001719 /* Expect a negative dentry */
David Howells2b0143b2015-03-17 22:25:59 +00001720 BUG_ON(d_inode(dentry));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001721
Niels de Vos1e8968c2013-12-17 18:20:16 +01001722 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001723 dir->i_sb->s_id, dir->i_ino, dentry);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05001724
Jeff Layton9597c132013-08-02 11:39:32 -04001725 err = nfs_check_flags(open_flags);
1726 if (err)
1727 return err;
1728
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001729 /* NFS only supports OPEN on regular files */
1730 if ((open_flags & O_DIRECTORY)) {
Al Viro00699ad2016-07-05 09:44:53 -04001731 if (!d_in_lookup(dentry)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001732 /*
1733 * Hashed negative dentry with O_DIRECTORY: dentry was
1734 * revalidated and is fine, no need to perform lookup
1735 * again
1736 */
Al Virod9585272012-06-22 12:39:14 +04001737 return -ENOENT;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001738 }
Trond Myklebust1472b832013-08-20 11:59:41 -04001739 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 goto no_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001743 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
Al Virod9585272012-06-22 12:39:14 +04001744 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001746 if (open_flags & O_CREAT) {
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001747 struct nfs_server *server = NFS_SERVER(dir);
1748
1749 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1750 mode &= ~current_umask();
1751
Trond Myklebust536e43d2012-01-17 22:04:26 -05001752 attr.ia_valid |= ATTR_MODE;
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001753 attr.ia_mode = mode;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001754 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001755 if (open_flags & O_TRUNC) {
1756 attr.ia_valid |= ATTR_SIZE;
1757 attr.ia_size = 0;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001758 }
1759
Al Viroc94c0952016-07-05 09:49:21 -04001760 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
NeilBrown532d4de2016-10-13 15:26:47 +11001771 ctx = create_nfs_open_context(dentry, open_flags, file);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001772 err = PTR_ERR(ctx);
1773 if (IS_ERR(ctx))
Al Virod9585272012-06-22 12:39:14 +04001774 goto out;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001775
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001776 trace_nfs_atomic_open_enter(dir, ctx, open_flags);
Al Viro73a09dd2018-06-08 13:22:02 -04001777 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
1778 if (created)
1779 file->f_mode |= FMODE_CREATED;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04001780 if (IS_ERR(inode)) {
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001781 err = PTR_ERR(inode);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001782 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001783 put_nfs_open_context(ctx);
Al Virod20cb712016-06-20 13:14:36 -04001784 d_drop(dentry);
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001785 switch (err) {
1786 case -ENOENT:
Peng Tao774d9512017-06-29 06:34:50 -07001787 d_splice_alias(NULL, dentry);
Trond Myklebust809fd142014-10-23 19:33:14 +03001788 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001789 break;
1790 case -EISDIR:
1791 case -ENOTDIR:
1792 goto no_open;
1793 case -ELOOP:
1794 if (!(open_flags & O_NOFOLLOW))
Trond Myklebust6f926b52005-10-18 14:20:18 -07001795 goto no_open;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /* case -EINVAL: */
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001798 default:
1799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Al Virod9585272012-06-22 12:39:14 +04001801 goto out;
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001802 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001803
Al Virob452a452018-06-08 13:06:28 -04001804 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
Trond Myklebust6e0d0be2013-08-20 11:26:17 -04001805 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
Trond Myklebust2d9db752013-08-30 09:17:33 -04001806 put_nfs_open_context(ctx);
Al Virod9585272012-06-22 12:39:14 +04001807out:
Al Viroc94c0952016-07-05 09:49:21 -04001808 if (unlikely(switched)) {
1809 d_lookup_done(dentry);
1810 dput(dentry);
1811 }
Al Virod9585272012-06-22 12:39:14 +04001812 return err;
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814no_open:
Trond Myklebust1472b832013-08-20 11:59:41 -04001815 res = nfs_lookup(dir, dentry, lookup_flags);
Al Viroc94c0952016-07-05 09:49:21 -04001816 if (switched) {
1817 d_lookup_done(dentry);
1818 if (!res)
1819 res = dentry;
1820 else
1821 dput(dentry);
1822 }
Miklos Szeredi0dd2b472012-06-05 15:10:18 +02001823 if (IS_ERR(res))
Al Viroc94c0952016-07-05 09:49:21 -04001824 return PTR_ERR(res);
Al Viroe45198a2012-06-10 06:48:09 -04001825 return finish_no_open(file, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001827EXPORT_SYMBOL_GPL(nfs_atomic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001829static int
1830nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1831 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Nick Piggin657e94b2011-01-14 02:48:39 +00001833 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Al Virofa3c56b2012-06-10 15:36:40 -04001835 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001836 goto full_reval;
Miklos Szeredieda72af2012-06-05 15:10:21 +02001837 if (d_mountpoint(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001838 goto full_reval;
Trond Myklebust2b484292010-09-17 10:56:51 -04001839
David Howells2b0143b2015-03-17 22:25:59 +00001840 inode = d_inode(dentry);
Trond Myklebust2b484292010-09-17 10:56:51 -04001841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* We can't create new files in nfs_open_revalidate(), so we
1843 * optimize away revalidation of negative dentries.
1844 */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001845 if (inode == NULL)
1846 goto full_reval;
NeilBrown49317a72014-07-14 11:28:20 +10001847
Trond Myklebustefeda802020-02-05 09:01:54 -05001848 if (nfs_verifier_is_delegated(dentry))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001849 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
Trond Myklebust216d5d062007-10-01 20:10:12 -04001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* NFS only supports OPEN on regular files */
1852 if (!S_ISREG(inode->i_mode))
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001853 goto full_reval;
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /* We cannot do exclusive creation on a positive dentry */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001856 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 Torvalds1da177e2005-04-16 15:20:36 -07001862
Miklos Szeredi0ef97dc2012-05-21 17:30:20 +02001863 /* Let f_op->open() actually open (and revalidate) the file */
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001864 return 1;
1865reval_dentry:
1866 if (flags & LOOKUP_RCU)
1867 return -ECHILD;
zhangliguang42f72cf2019-02-12 09:38:33 +08001868 return nfs_lookup_revalidate_dentry(dir, dentry, inode);
Trond Myklebust536e43d2012-01-17 22:04:26 -05001869
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001870full_reval:
1871 return nfs_do_lookup_revalidate(dir, dentry, flags);
1872}
Trond Myklebust535918f2010-09-17 10:56:51 -04001873
Trond Myklebustc7944eb2018-09-28 12:42:51 -04001874static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1875{
1876 return __nfs_lookup_revalidate(dentry, flags,
1877 nfs4_do_lookup_revalidate);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04001878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880#endif /* CONFIG_NFSV4 */
1881
Benjamin Coddington406cd912019-09-13 08:29:02 -04001882struct dentry *
1883nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04001884 struct nfs_fattr *fattr,
1885 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Trond Myklebustfab728e2007-09-29 17:41:33 -04001887 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001888 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct inode *inode;
Al Virob0c6108e2018-05-16 10:55:01 -04001890 struct dentry *d;
Benjamin Coddington406cd912019-09-13 08:29:02 -04001891 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Trond Myklebustfab728e2007-09-29 17:41:33 -04001893 d_drop(dentry);
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (fhandle->size == 0) {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05001896 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (error)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001898 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Trond Myklebust5724ab32007-10-01 21:51:38 -04001900 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1902 struct nfs_server *server = NFS_SB(dentry->d_sb);
Trond Myklebusta841b542018-04-07 13:50:59 -04001903 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1904 fattr, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (error < 0)
Trond Myklebustfab728e2007-09-29 17:41:33 -04001906 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
David Quigley1775fd32013-05-22 12:50:42 -04001908 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
Al Virob0c6108e2018-05-16 10:55:01 -04001909 d = d_splice_alias(inode, dentry);
Trond Myklebustfab728e2007-09-29 17:41:33 -04001910out:
1911 dput(parent);
Benjamin Coddington406cd912019-09-13 08:29:02 -04001912 return d;
Trond Myklebustfab728e2007-09-29 17:41:33 -04001913out_error:
1914 nfs_mark_for_revalidate(dir);
Benjamin Coddington406cd912019-09-13 08:29:02 -04001915 d = ERR_PTR(error);
1916 goto out;
1917}
1918EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1919
1920/*
1921 * Code common to create, mkdir, and mknod.
1922 */
1923int 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 Torvalds1da177e2005-04-16 15:20:36 -07001936}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001937EXPORT_SYMBOL_GPL(nfs_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
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 Schumaker597d9282012-07-16 16:39:10 -04001945int nfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04001946 umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct iattr attr;
Al Viroebfc3b42012-06-10 18:05:36 -04001949 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Niels de Vos1e8968c2013-12-17 18:20:16 +01001952 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001953 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 attr.ia_mode = mode;
1956 attr.ia_valid = ATTR_MODE;
1957
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04001958 trace_nfs_create_enter(dir, dentry, open_flags);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02001959 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
Trond Myklebust8b0ad3d2013-08-21 10:53:09 -04001960 trace_nfs_create_exit(dir, dentry, open_flags, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (error != 0)
1962 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return 0;
1964out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 d_drop(dentry);
1966 return error;
1967}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001968EXPORT_SYMBOL_GPL(nfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970/*
1971 * See comments for nfs_proc_create regarding failed operations.
1972 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04001973int
Al Viro1a67aaf2011-07-26 01:52:52 -04001974nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 struct iattr attr;
1977 int status;
1978
Niels de Vos1e8968c2013-12-17 18:20:16 +01001979 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04001980 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 attr.ia_mode = mode;
1983 attr.ia_valid = ATTR_MODE;
1984
Trond Myklebust1ca42382013-08-21 12:36:04 -04001985 trace_nfs_mknod_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
Trond Myklebust1ca42382013-08-21 12:36:04 -04001987 trace_nfs_mknod_exit(dir, dentry, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (status != 0)
1989 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return 0;
1991out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 d_drop(dentry);
1993 return status;
1994}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001995EXPORT_SYMBOL_GPL(nfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997/*
1998 * See comments for nfs_proc_create regarding failed operations.
1999 */
Bryan Schumaker597d9282012-07-16 16:39:10 -04002000int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct iattr attr;
2003 int error;
2004
Niels de Vos1e8968c2013-12-17 18:20:16 +01002005 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002006 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 attr.ia_valid = ATTR_MODE;
2009 attr.ia_mode = mode | S_IFDIR;
2010
Trond Myklebust1ca42382013-08-21 12:36:04 -04002011 trace_nfs_mkdir_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002013 trace_nfs_mkdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (error != 0)
2015 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return 0;
2017out_err:
2018 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return error;
2020}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002021EXPORT_SYMBOL_GPL(nfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002023static void nfs_dentry_handle_enoent(struct dentry *dentry)
2024{
Al Virodc3f4192015-05-18 10:10:34 -04002025 if (simple_positive(dentry))
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002026 d_delete(dentry);
2027}
2028
Bryan Schumaker597d9282012-07-16 16:39:10 -04002029int nfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 int error;
2032
Niels de Vos1e8968c2013-12-17 18:20:16 +01002033 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
Al Viro6de14722013-09-16 10:53:17 -04002034 dir->i_sb->s_id, dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Trond Myklebust1ca42382013-08-21 12:36:04 -04002036 trace_nfs_rmdir_enter(dir, dentry);
David Howells2b0143b2015-03-17 22:25:59 +00002037 if (d_really_is_positive(dentry)) {
Al Viro884be172016-04-28 23:56:31 -04002038 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002039 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2040 /* Ensure the VFS deletes this inode */
2041 switch (error) {
2042 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00002043 clear_nlink(d_inode(dentry));
Trond Myklebustba6c0592013-08-30 12:24:25 -04002044 break;
2045 case -ENOENT:
2046 nfs_dentry_handle_enoent(dentry);
2047 }
Al Viro884be172016-04-28 23:56:31 -04002048 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
Trond Myklebustba6c0592013-08-30 12:24:25 -04002049 } else
2050 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002051 trace_nfs_rmdir_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 return error;
2054}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002055EXPORT_SYMBOL_GPL(nfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057/*
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 */
2064static int nfs_safe_remove(struct dentry *dentry)
2065{
David Howells2b0143b2015-03-17 22:25:59 +00002066 struct inode *dir = d_inode(dentry->d_parent);
2067 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int error = -EBUSY;
2069
Al Viro6de14722013-09-16 10:53:17 -04002070 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
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 Myklebust1ca42382013-08-21 12:36:04 -04002078 trace_nfs_remove_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (inode != NULL) {
Trond Myklebust912678d2018-03-20 16:43:15 -04002080 error = NFS_PROTO(dir)->remove(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (error == 0)
Trond Myklebust1b83d702008-06-11 15:44:04 -04002082 nfs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 } else
Trond Myklebust912678d2018-03-20 16:43:15 -04002084 error = NFS_PROTO(dir)->remove(dir, dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002085 if (error == -ENOENT)
2086 nfs_dentry_handle_enoent(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002087 trace_nfs_remove_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088out:
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 Schumaker597d9282012-07-16 16:39:10 -04002097int nfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 int error;
2100 int need_rehash = 0;
2101
Niels de Vos1e8968c2013-12-17 18:20:16 +01002102 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002103 dir->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Trond Myklebust1ca42382013-08-21 12:36:04 -04002105 trace_nfs_unlink_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 spin_lock(&dentry->d_lock);
Al Viro84d08fa2013-07-05 18:59:33 +04002107 if (d_count(dentry) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 spin_unlock(&dentry->d_lock);
Trond Myklebustccfeb502007-01-13 02:28:12 -05002109 /* Start asynchronous writeout of the inode */
David Howells2b0143b2015-03-17 22:25:59 +00002110 write_inode_now(d_inode(dentry), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 error = nfs_sillyrename(dir, dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 if (!d_unhashed(dentry)) {
2115 __d_drop(dentry);
2116 need_rehash = 1;
2117 }
2118 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 error = nfs_safe_remove(dentry);
Trond Myklebustd45b9d82008-01-28 19:43:18 -05002120 if (!error || error == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2122 } else if (need_rehash)
2123 d_rehash(dentry);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002124out:
2125 trace_nfs_unlink_exit(dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return error;
2127}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002128EXPORT_SYMBOL_GPL(nfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Chuck Lever873101b2006-08-22 20:06:23 -04002130/*
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 Schumaker597d9282012-07-16 16:39:10 -04002145int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Chuck Lever873101b2006-08-22 20:06:23 -04002147 struct page *page;
2148 char *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct iattr attr;
Chuck Lever873101b2006-08-22 20:06:23 -04002150 unsigned int pathlen = strlen(symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 int error;
2152
Niels de Vos1e8968c2013-12-17 18:20:16 +01002153 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
Al Viro6de14722013-09-16 10:53:17 -04002154 dir->i_ino, dentry, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Chuck Lever873101b2006-08-22 20:06:23 -04002156 if (pathlen > PAGE_SIZE)
2157 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Chuck Lever873101b2006-08-22 20:06:23 -04002159 attr.ia_mode = S_IFLNK | S_IRWXUGO;
2160 attr.ia_valid = ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Al Viroe8ecde22016-01-14 17:52:59 -05002162 page = alloc_page(GFP_USER);
Trond Myklebust76566992008-06-11 15:44:22 -04002163 if (!page)
Chuck Lever873101b2006-08-22 20:06:23 -04002164 return -ENOMEM;
Chuck Lever873101b2006-08-22 20:06:23 -04002165
Al Viroe8ecde22016-01-14 17:52:59 -05002166 kaddr = page_address(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002167 memcpy(kaddr, symname, pathlen);
2168 if (pathlen < PAGE_SIZE)
2169 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
Chuck Lever873101b2006-08-22 20:06:23 -04002170
Trond Myklebust1ca42382013-08-21 12:36:04 -04002171 trace_nfs_symlink_enter(dir, dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -04002172 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
Trond Myklebust1ca42382013-08-21 12:36:04 -04002173 trace_nfs_symlink_exit(dir, dentry, error);
Chuck Lever873101b2006-08-22 20:06:23 -04002174 if (error != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01002175 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
Chuck Lever873101b2006-08-22 20:06:23 -04002176 dir->i_sb->s_id, dir->i_ino,
Al Viro6de14722013-09-16 10:53:17 -04002177 dentry, symname, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 d_drop(dentry);
Chuck Lever873101b2006-08-22 20:06:23 -04002179 __free_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002180 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 Howells2b0143b2015-03-17 22:25:59 +00002187 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
Chuck Lever873101b2006-08-22 20:06:23 -04002188 GFP_KERNEL)) {
Chuck Lever873101b2006-08-22 20:06:23 -04002189 SetPageUptodate(page);
2190 unlock_page(page);
Rafael Aquinia0b54add2014-02-10 14:25:48 -08002191 /*
2192 * add_to_page_cache_lru() grabs an extra page refcount.
2193 * Drop it here to avoid leaking this page later.
2194 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 put_page(page);
Chuck Lever873101b2006-08-22 20:06:23 -04002196 } else
2197 __free_page(page);
2198
Chuck Lever873101b2006-08-22 20:06:23 -04002199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002201EXPORT_SYMBOL_GPL(nfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Bryan Schumaker597d9282012-07-16 16:39:10 -04002203int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2205{
David Howells2b0143b2015-03-17 22:25:59 +00002206 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 int error;
2208
Al Viro6de14722013-09-16 10:53:17 -04002209 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2210 old_dentry, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Trond Myklebust1fd10852013-08-21 13:54:44 -04002212 trace_nfs_link_enter(inode, dir, dentry);
Trond Myklebust9697d232007-10-02 21:58:05 -04002213 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
Trond Myklebustcf809552005-10-27 22:12:42 -04002215 if (error == 0) {
Al Viro7de9c6ee2010-10-23 11:11:40 -04002216 ihold(inode);
Trond Myklebust9697d232007-10-02 21:58:05 -04002217 d_add(dentry, inode);
Trond Myklebustcf809552005-10-27 22:12:42 -04002218 }
Trond Myklebust1fd10852013-08-21 13:54:44 -04002219 trace_nfs_link_exit(inode, dir, dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return error;
2221}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002222EXPORT_SYMBOL_GPL(nfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
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 Schumaker597d9282012-07-16 16:39:10 -04002248int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002249 struct inode *new_dir, struct dentry *new_dentry,
2250 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
David Howells2b0143b2015-03-17 22:25:59 +00002252 struct inode *old_inode = d_inode(old_dentry);
2253 struct inode *new_inode = d_inode(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002254 struct dentry *dentry = NULL, *rehash = NULL;
Jeff Layton80a491f2014-03-17 07:06:56 -04002255 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int error = -EBUSY;
2257
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02002258 if (flags)
2259 return -EINVAL;
2260
Al Viro6de14722013-09-16 10:53:17 -04002261 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2262 old_dentry, new_dentry,
Al Viro84d08fa2013-07-05 18:59:33 +04002263 d_count(new_dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Trond Myklebust70ded202013-08-21 12:08:45 -04002265 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
Miklos Szeredi28f79a12009-12-03 15:58:56 -05002267 * 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 Torvalds1da177e2005-04-16 15:20:36 -07002271 */
Miklos Szeredi27226102009-12-03 15:58:56 -05002272 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 Coddingtond9f29502017-06-16 11:12:59 -04002277 if (!d_unhashed(new_dentry)) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002278 d_drop(new_dentry);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002279 rehash = new_dentry;
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Al Viro84d08fa2013-07-05 18:59:33 +04002282 if (d_count(new_dentry) > 2) {
Miklos Szeredi27226102009-12-03 15:58:56 -05002283 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 Szeredi24e93022009-12-03 15:58:56 -05002293 if (err)
Miklos Szeredi27226102009-12-03 15:58:56 -05002294 goto out;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002295
2296 new_dentry = dentry;
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002297 rehash = NULL;
Miklos Szeredi24e93022009-12-03 15:58:56 -05002298 new_inode = NULL;
Miklos Szeredi27226102009-12-03 15:58:56 -05002299 }
Trond Myklebustb1e4adf2009-03-19 15:35:49 -04002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002302 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
Jeff Layton80a491f2014-03-17 07:06:56 -04002303 if (IS_ERR(task)) {
2304 error = PTR_ERR(task);
2305 goto out;
2306 }
2307
2308 error = rpc_wait_for_completion_task(task);
Benjamin Coddington818a8db2017-06-16 11:13:00 -04002309 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 Layton80a491f2014-03-17 07:06:56 -04002314 error = task->tk_status;
2315 rpc_put_task(task);
Trond Myklebust59a707b2018-04-08 18:11:18 -04002316 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07002325out:
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002326 if (rehash)
2327 d_rehash(rehash);
Trond Myklebust70ded202013-08-21 12:08:45 -04002328 trace_nfs_rename_exit(old_dir, old_dentry,
2329 new_dir, new_dentry, error);
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002330 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 Myklebustd8032242017-11-06 15:28:04 -05002340 nfs_set_verifier(old_dentry,
Benjamin Coddingtond9f29502017-06-16 11:12:59 -04002341 nfs_save_change_attribute(new_dir));
2342 } else if (error == -ENOENT)
2343 nfs_dentry_handle_enoent(old_dentry);
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* new dentry created? */
2346 if (dentry)
2347 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return error;
2349}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002350EXPORT_SYMBOL_GPL(nfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002352static DEFINE_SPINLOCK(nfs_access_lru_lock);
2353static LIST_HEAD(nfs_access_lru_list);
2354static atomic_long_t nfs_access_nr_entries;
2355
Trond Myklebusta8b373e2020-02-08 09:14:11 -05002356static unsigned long nfs_access_max_cachesize = 4*1024*1024;
Trond Myklebust3a505842014-07-21 13:53:48 -04002357module_param(nfs_access_max_cachesize, ulong, 0644);
2358MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2359
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002360static void nfs_access_free_entry(struct nfs_access_entry *entry)
2361{
NeilBrownb68572e2018-12-03 11:30:30 +11002362 put_cred(entry->cred);
NeilBrownf682a392014-07-14 11:28:20 +10002363 kfree_rcu(entry, rcu_head);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002364 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002365 atomic_long_dec(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002366 smp_mb__after_atomic();
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002367}
2368
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002369static 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 Myklebust3a505842014-07-21 13:53:48 -04002380static unsigned long
2381nfs_do_access_cache_scan(unsigned int nr_to_scan)
Trond Myklebust979df722006-07-25 11:28:19 -04002382{
2383 LIST_HEAD(head);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002384 struct nfs_inode *nfsi, *next;
Trond Myklebust979df722006-07-25 11:28:19 -04002385 struct nfs_access_entry *cache;
Dave Chinner1ab6c492013-08-28 10:18:09 +10002386 long freed = 0;
Trond Myklebust979df722006-07-25 11:28:19 -04002387
Trond Myklebusta50f7952007-06-05 19:23:43 -04002388 spin_lock(&nfs_access_lru_lock);
Trond Myklebustaa510da2010-09-29 15:11:56 -04002389 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
Trond Myklebust979df722006-07-25 11:28:19 -04002390 struct inode *inode;
2391
2392 if (nr_to_scan-- == 0)
2393 break;
Trond Myklebust9c7e7e22010-05-13 12:51:06 -04002394 inode = &nfsi->vfs_inode;
Trond Myklebust979df722006-07-25 11:28:19 -04002395 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 Chinner1ab6c492013-08-28 10:18:09 +10002402 freed++;
Trond Myklebust979df722006-07-25 11:28:19 -04002403 if (!list_empty(&nfsi->access_cache_entry_lru))
2404 list_move_tail(&nfsi->access_cache_inode_lru,
2405 &nfs_access_lru_list);
2406 else {
2407remove_lru_entry:
2408 list_del_init(&nfsi->access_cache_inode_lru);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002409 smp_mb__before_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002410 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002411 smp_mb__after_atomic();
Trond Myklebust979df722006-07-25 11:28:19 -04002412 }
Trond Myklebust59844a92010-05-26 08:42:24 -04002413 spin_unlock(&inode->i_lock);
Trond Myklebust979df722006-07-25 11:28:19 -04002414 }
2415 spin_unlock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002416 nfs_access_free_list(&head);
Dave Chinner1ab6c492013-08-28 10:18:09 +10002417 return freed;
2418}
2419
2420unsigned long
Trond Myklebust3a505842014-07-21 13:53:48 -04002421nfs_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
2432unsigned long
Dave Chinner1ab6c492013-08-28 10:18:09 +10002433nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2434{
Glauber Costa55f841c2013-08-28 10:17:53 +10002435 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
Trond Myklebust979df722006-07-25 11:28:19 -04002436}
2437
Trond Myklebust3a505842014-07-21 13:53:48 -04002438static void
2439nfs_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 Myklebust1a81bb82010-05-13 12:51:06 -04002454static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002455{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002456 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002457 struct rb_node *n;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002458 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 Myklebust1a81bb82010-05-13 12:51:06 -04002464 list_move(&entry->lru, head);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002465 }
2466 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002467}
2468
2469void nfs_access_zap_cache(struct inode *inode)
2470{
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002471 LIST_HEAD(head);
2472
2473 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2474 return;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002475 /* Remove from global LRU init */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002476 spin_lock(&nfs_access_lru_lock);
2477 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002478 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002479
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002480 spin_lock(&inode->i_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002481 __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 Myklebust1c3c07e2006-07-25 11:28:18 -04002485}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04002486EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002487
NeilBrownb68572e2018-12-03 11:30:30 +11002488static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002489{
2490 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002491
2492 while (n != NULL) {
NeilBrownb68572e2018-12-03 11:30:30 +11002493 struct nfs_access_entry *entry =
2494 rb_entry(n, struct nfs_access_entry, rb_node);
2495 int cmp = cred_fscmp(cred, entry->cred);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002496
NeilBrownb68572e2018-12-03 11:30:30 +11002497 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002498 n = n->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002499 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002500 n = n->rb_right;
2501 else
2502 return entry;
2503 }
2504 return NULL;
2505}
2506
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002507static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Chuck Lever55296802005-08-18 11:24:09 -07002509 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002510 struct nfs_access_entry *cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002511 bool retry = true;
2512 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002514 spin_lock(&inode->i_lock);
Trond Myklebust57b69182016-06-03 17:07:19 -04002515 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 Myklebust21c3ba72016-12-16 18:40:03 -05002523 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
Trond Myklebust57b69182016-06-03 17:07:19 -04002524 break;
Trond Myklebust5c965db2020-01-06 15:39:36 -05002525 if (!retry)
2526 break;
Trond Myklebust57b69182016-06-03 17:07:19 -04002527 err = -ECHILD;
2528 if (!may_block)
2529 goto out;
Trond Myklebust57b69182016-06-03 17:07:19 -04002530 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 Myklebust1c3c07e2006-07-25 11:28:18 -04002537 res->cred = cache->cred;
2538 res->mask = cache->mask;
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002539 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002540 err = 0;
2541out:
2542 spin_unlock(&inode->i_lock);
2543 return err;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002544out_zap:
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002545 spin_unlock(&inode->i_lock);
2546 nfs_access_zap_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002547 return -ENOENT;
2548}
2549
NeilBrownb68572e2018-12-03 11:30:30 +11002550static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
NeilBrownf682a392014-07-14 11:28:20 +10002551{
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 Bhowmik9f01eb52019-12-10 11:16:39 +05302563 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
NeilBrownf682a392014-07-14 11:28:20 +10002564 cache = list_entry(lh, struct nfs_access_entry, lru);
2565 if (lh == &nfsi->access_cache_entry_lru ||
Trond Myklebust9a206de2020-01-26 17:31:14 -05002566 cred_fscmp(cred, cache->cred) != 0)
NeilBrownf682a392014-07-14 11:28:20 +10002567 cache = NULL;
2568 if (cache == NULL)
2569 goto out;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002570 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
NeilBrownf682a392014-07-14 11:28:20 +10002571 goto out;
NeilBrownf682a392014-07-14 11:28:20 +10002572 res->cred = cache->cred;
2573 res->mask = cache->mask;
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002574 err = 0;
NeilBrownf682a392014-07-14 11:28:20 +10002575out:
2576 rcu_read_unlock();
2577 return err;
2578}
2579
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002580int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2581nfs_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}
2592EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2593
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002594static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2595{
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002596 struct nfs_inode *nfsi = NFS_I(inode);
2597 struct rb_root *root_node = &nfsi->access_cache;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002598 struct rb_node **p = &root_node->rb_node;
2599 struct rb_node *parent = NULL;
2600 struct nfs_access_entry *entry;
NeilBrownb68572e2018-12-03 11:30:30 +11002601 int cmp;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002602
2603 spin_lock(&inode->i_lock);
2604 while (*p != NULL) {
2605 parent = *p;
2606 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
NeilBrownb68572e2018-12-03 11:30:30 +11002607 cmp = cred_fscmp(set->cred, entry->cred);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002608
NeilBrownb68572e2018-12-03 11:30:30 +11002609 if (cmp < 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002610 p = &parent->rb_left;
NeilBrownb68572e2018-12-03 11:30:30 +11002611 else if (cmp > 0)
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002612 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 Myklebustcfcea3e2006-07-25 11:28:18 -04002618 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002619 spin_unlock(&inode->i_lock);
2620 return;
2621found:
2622 rb_replace_node(parent, &set->rb_node, root_node);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002623 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2624 list_del(&entry->lru);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002625 spin_unlock(&inode->i_lock);
2626 nfs_access_free_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002629void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002631 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2632 if (cache == NULL)
2633 return;
2634 RB_CLEAR_NODE(&cache->rb_node);
NeilBrownb68572e2018-12-03 11:30:30 +11002635 cache->cred = get_cred(set->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 cache->mask = set->mask;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -04002637
NeilBrownf682a392014-07-14 11:28:20 +10002638 /* 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 Myklebust1c3c07e2006-07-25 11:28:18 -04002643 nfs_access_add_rbtree(inode, cache);
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002644
2645 /* Update accounting */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002646 smp_mb__before_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002647 atomic_long_inc(&nfs_access_nr_entries);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002648 smp_mb__after_atomic();
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002649
2650 /* Add inode to global LRU list */
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002651 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
Trond Myklebustcfcea3e2006-07-25 11:28:18 -04002652 spin_lock(&nfs_access_lru_lock);
Trond Myklebust1a81bb82010-05-13 12:51:06 -04002653 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 Myklebustcfcea3e2006-07-25 11:28:18 -04002656 spin_unlock(&nfs_access_lru_lock);
2657 }
Trond Myklebust3a505842014-07-21 13:53:48 -04002658 nfs_access_cache_enforce_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002660EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2661
Anna Schumaker3c181822017-07-26 10:14:55 -04002662#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 Myklebustecbb9032017-07-11 17:54:35 -04002668#define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
Anna Schumaker3c181822017-07-26 10:14:55 -04002669#define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
2670#define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002671static int
Trond Myklebustecbb9032017-07-11 17:54:35 -04002672nfs_access_calc_mask(u32 access_result, umode_t umode)
Trond Myklebust15d4b732017-07-11 17:54:32 -04002673{
2674 int mask = 0;
2675
2676 if (access_result & NFS_MAY_READ)
2677 mask |= MAY_READ;
Trond Myklebustecbb9032017-07-11 17:54:35 -04002678 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 Myklebust15d4b732017-07-11 17:54:32 -04002690 return mask;
2691}
2692
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002693void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2694{
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002695 entry->mask = access_result;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002696}
2697EXPORT_SYMBOL_GPL(nfs_access_set_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
NeilBrownb68572e2018-12-03 11:30:30 +11002699static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701 struct nfs_access_entry cache;
Trond Myklebust57b69182016-06-03 17:07:19 -04002702 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
Trond Myklebuste8194b72020-01-06 15:25:12 -05002703 int cache_mask = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 int status;
2705
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002706 trace_nfs_access_enter(inode);
2707
Frank van der Lindend2ae4f82020-06-23 22:38:57 +00002708 status = nfs_access_get_cached(inode, cred, &cache, may_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (status == 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002710 goto out_cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
NeilBrownf3324a22014-07-14 11:28:20 +10002712 status = -ECHILD;
Trond Myklebust57b69182016-06-03 17:07:19 -04002713 if (!may_block)
NeilBrownf3324a22014-07-14 11:28:20 +10002714 goto out;
2715
Anna Schumaker1750d922017-07-26 12:00:21 -04002716 /*
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 Linden72832a22020-06-23 22:38:58 +00002720 if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
2721 cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
2722 NFS_ACCESS_XALIST;
2723 }
Anna Schumaker1750d922017-07-26 12:00:21 -04002724 if (S_ISDIR(inode->i_mode))
2725 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
2726 else
2727 cache.mask |= NFS_ACCESS_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 cache.cred = cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 status = NFS_PROTO(inode)->access(inode, &cache);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002730 if (status != 0) {
2731 if (status == -ESTALE) {
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002732 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002733 nfs_set_inode_stale(inode);
2734 else
2735 nfs_zap_caches(inode);
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002736 }
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002737 goto out;
Suresh Jayaramana71ee332009-03-10 20:33:21 -04002738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 nfs_access_add_cache(inode, &cache);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002740out_cached:
Trond Myklebustecbb9032017-07-11 17:54:35 -04002741 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
Trond Myklebustbd8b2442017-07-11 17:54:34 -04002742 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002743 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744out:
Trond Myklebuste8194b72020-01-06 15:25:12 -05002745 trace_nfs_access_exit(inode, mask, cache_mask, status);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04002746 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Trond Myklebustaf22f942007-08-10 17:45:10 -04002749static int nfs_open_permission_mask(int openflags)
2750{
2751 int mask = 0;
2752
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002753 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 Myklebustaf22f942007-08-10 17:45:10 -04002763 return mask;
2764}
2765
NeilBrownb68572e2018-12-03 11:30:30 +11002766int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
Trond Myklebustaf22f942007-08-10 17:45:10 -04002767{
2768 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2769}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002770EXPORT_SYMBOL_GPL(nfs_may_open);
Trond Myklebustaf22f942007-08-10 17:45:10 -04002771
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002772static int nfs_execute_ok(struct inode *inode, int mask)
2773{
2774 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002775 int ret = 0;
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002776
Trond Myklebust38258272018-07-24 14:27:11 -04002777 if (S_ISDIR(inode->i_mode))
2778 return 0;
Trond Myklebustcf834022018-07-20 16:19:35 -04002779 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
Trond Myklebust21c3ba72016-12-16 18:40:03 -05002780 if (mask & MAY_NOT_BLOCK)
2781 return -ECHILD;
2782 ret = __nfs_revalidate_inode(server, inode);
2783 }
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002784 if (ret == 0 && !execute_ok(inode))
2785 ret = -EACCES;
2786 return ret;
2787}
2788
Al Viro10556cb22011-06-20 19:28:19 -04002789int nfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
NeilBrownb68572e2018-12-03 11:30:30 +11002791 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int res = 0;
2793
Chuck Lever91d5b472006-03-20 13:44:14 -05002794 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2795
Al Viroe6305c42008-07-15 21:03:57 -04002796 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 goto out;
2798 /* Is this sys_access() ? */
Eric Paris9cfcac82010-07-23 11:43:51 -04002799 if (mask & (MAY_ACCESS | MAY_CHDIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 goto force_lookup;
2801
2802 switch (inode->i_mode & S_IFMT) {
2803 case S_IFLNK:
2804 goto out;
2805 case S_IFREG:
Trond Myklebust762674f2015-12-26 21:54:58 -05002806 if ((mask & MAY_OPEN) &&
2807 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 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
2819force_lookup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (!NFS_PROTO(inode)->access)
2821 goto out_notsup;
2822
Zhouyi Zhoueb095c12020-03-06 03:45:26 +00002823 res = nfs_do_access(inode, cred, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824out:
Trond Myklebust5c5fc092015-12-28 19:30:05 -05002825 if (!res && (mask & MAY_EXEC))
2826 res = nfs_execute_ok(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +02002827
Niels de Vos1e8968c2013-12-17 18:20:16 +01002828 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002829 inode->i_sb->s_id, inode->i_ino, mask, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return res;
2831out_notsup:
NeilBrownd51ac1a2014-07-14 11:28:20 +10002832 if (mask & MAY_NOT_BLOCK)
2833 return -ECHILD;
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2836 if (res == 0)
Al Viro2830ba72011-06-20 19:16:29 -04002837 res = generic_permission(inode, mask);
Chuck Lever1e7cb3d2006-03-20 13:44:24 -05002838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002840EXPORT_SYMBOL_GPL(nfs_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842/*
2843 * Local variables:
2844 * version-control: t
2845 * kept-new-versions: 5
2846 * End:
2847 */