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