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