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