]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/nfs/dir.c
nfs: implement i_op->atomic_open()
[mirror_ubuntu-eoan-kernel.git] / fs / nfs / dir.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/dir.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs directory handling functions
7 *
8 * 10 Apr 1996 Added silly rename for unlink --okir
9 * 28 Sep 1996 Improved directory cache --okir
10 * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
11 * Re-implemented silly rename for unlink, newly implemented
12 * silly rename for nfs_rename() following the suggestions
13 * of Olaf Kirch (okir) found in this file.
14 * Following Linus comments on my original hack, this version
15 * depends only on the dcache stuff and doesn't touch the inode
16 * layer (iput() and friends).
17 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
18 */
19
20#include <linux/time.h>
21#include <linux/errno.h>
22#include <linux/stat.h>
23#include <linux/fcntl.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/sunrpc/clnt.h>
29#include <linux/nfs_fs.h>
30#include <linux/nfs_mount.h>
31#include <linux/pagemap.h>
873101b3 32#include <linux/pagevec.h>
1da177e4 33#include <linux/namei.h>
54ceac45 34#include <linux/mount.h>
e8edc6e0 35#include <linux/sched.h>
04e4bd1c 36#include <linux/kmemleak.h>
64c2ce8b 37#include <linux/xattr.h>
1da177e4
LT
38
39#include "delegation.h"
91d5b470 40#include "iostat.h"
4c30d56e 41#include "internal.h"
cd9a1c0e 42#include "fscache.h"
1da177e4 43
1da177e4
LT
44/* #define NFS_DEBUG_VERBOSE 1 */
45
46static int nfs_opendir(struct inode *, struct file *);
480c2006 47static int nfs_closedir(struct inode *, struct file *);
1da177e4
LT
48static int nfs_readdir(struct file *, void *, filldir_t);
49static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
4acdaf27 50static int nfs_create(struct inode *, struct dentry *, umode_t, struct nameidata *);
18bb1db3 51static int nfs_mkdir(struct inode *, struct dentry *, umode_t);
1da177e4
LT
52static int nfs_rmdir(struct inode *, struct dentry *);
53static int nfs_unlink(struct inode *, struct dentry *);
54static int nfs_symlink(struct inode *, struct dentry *, const char *);
55static int nfs_link(struct dentry *, struct inode *, struct dentry *);
1a67aafb 56static int nfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
1da177e4
LT
57static int nfs_rename(struct inode *, struct dentry *,
58 struct inode *, struct dentry *);
02c24a82 59static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
f0dd2136 60static loff_t nfs_llseek_dir(struct file *, loff_t, int);
11de3b11 61static void nfs_readdir_clear_array(struct page*);
1da177e4 62
4b6f5d20 63const struct file_operations nfs_dir_operations = {
f0dd2136 64 .llseek = nfs_llseek_dir,
1da177e4
LT
65 .read = generic_read_dir,
66 .readdir = nfs_readdir,
67 .open = nfs_opendir,
480c2006 68 .release = nfs_closedir,
1da177e4
LT
69 .fsync = nfs_fsync_dir,
70};
71
92e1d5be 72const struct inode_operations nfs_dir_inode_operations = {
1da177e4
LT
73 .create = nfs_create,
74 .lookup = nfs_lookup,
75 .link = nfs_link,
76 .unlink = nfs_unlink,
77 .symlink = nfs_symlink,
78 .mkdir = nfs_mkdir,
79 .rmdir = nfs_rmdir,
80 .mknod = nfs_mknod,
81 .rename = nfs_rename,
82 .permission = nfs_permission,
83 .getattr = nfs_getattr,
84 .setattr = nfs_setattr,
85};
86
11de3b11
TM
87const struct address_space_operations nfs_dir_aops = {
88 .freepage = nfs_readdir_clear_array,
d1bacf9e
BS
89};
90
b7fa0554 91#ifdef CONFIG_NFS_V3
92e1d5be 92const struct inode_operations nfs3_dir_inode_operations = {
b7fa0554
AG
93 .create = nfs_create,
94 .lookup = nfs_lookup,
95 .link = nfs_link,
96 .unlink = nfs_unlink,
97 .symlink = nfs_symlink,
98 .mkdir = nfs_mkdir,
99 .rmdir = nfs_rmdir,
100 .mknod = nfs_mknod,
101 .rename = nfs_rename,
102 .permission = nfs_permission,
103 .getattr = nfs_getattr,
104 .setattr = nfs_setattr,
105 .listxattr = nfs3_listxattr,
106 .getxattr = nfs3_getxattr,
107 .setxattr = nfs3_setxattr,
108 .removexattr = nfs3_removexattr,
109};
110#endif /* CONFIG_NFS_V3 */
111
1da177e4
LT
112#ifdef CONFIG_NFS_V4
113
0dd2b474
MS
114static struct file *nfs_atomic_open(struct inode *, struct dentry *,
115 struct opendata *, unsigned, umode_t,
116 bool *);
117static int nfs4_create(struct inode *dir, struct dentry *dentry,
118 umode_t mode, struct nameidata *nd);
92e1d5be 119const struct inode_operations nfs4_dir_inode_operations = {
0dd2b474
MS
120 .create = nfs4_create,
121 .lookup = nfs_lookup,
122 .atomic_open = nfs_atomic_open,
1da177e4
LT
123 .link = nfs_link,
124 .unlink = nfs_unlink,
125 .symlink = nfs_symlink,
126 .mkdir = nfs_mkdir,
127 .rmdir = nfs_rmdir,
128 .mknod = nfs_mknod,
129 .rename = nfs_rename,
130 .permission = nfs_permission,
131 .getattr = nfs_getattr,
132 .setattr = nfs_setattr,
64c2ce8b
AK
133 .getxattr = generic_getxattr,
134 .setxattr = generic_setxattr,
135 .listxattr = generic_listxattr,
136 .removexattr = generic_removexattr,
1da177e4
LT
137};
138
139#endif /* CONFIG_NFS_V4 */
140
0c030806 141static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
480c2006
BS
142{
143 struct nfs_open_dir_context *ctx;
144 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
145 if (ctx != NULL) {
8ef2ce3e 146 ctx->duped = 0;
0c030806 147 ctx->attr_gencount = NFS_I(dir)->attr_gencount;
480c2006 148 ctx->dir_cookie = 0;
8ef2ce3e 149 ctx->dup_cookie = 0;
480c2006 150 ctx->cred = get_rpccred(cred);
0c030806
TM
151 return ctx;
152 }
153 return ERR_PTR(-ENOMEM);
480c2006
BS
154}
155
156static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
157{
158 put_rpccred(ctx->cred);
159 kfree(ctx);
160}
161
1da177e4
LT
162/*
163 * Open file
164 */
165static int
166nfs_opendir(struct inode *inode, struct file *filp)
167{
480c2006
BS
168 int res = 0;
169 struct nfs_open_dir_context *ctx;
170 struct rpc_cred *cred;
1da177e4 171
6da24bc9 172 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
cc0dd2d1
CL
173 filp->f_path.dentry->d_parent->d_name.name,
174 filp->f_path.dentry->d_name.name);
175
176 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1e7cb3dc 177
480c2006
BS
178 cred = rpc_lookup_cred();
179 if (IS_ERR(cred))
180 return PTR_ERR(cred);
0c030806 181 ctx = alloc_nfs_open_dir_context(inode, cred);
480c2006
BS
182 if (IS_ERR(ctx)) {
183 res = PTR_ERR(ctx);
184 goto out;
185 }
186 filp->private_data = ctx;
f5a73672
NB
187 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
188 /* This is a mountpoint, so d_revalidate will never
189 * have been called, so we need to refresh the
190 * inode (for close-open consistency) ourselves.
191 */
192 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
193 }
480c2006
BS
194out:
195 put_rpccred(cred);
1da177e4
LT
196 return res;
197}
198
480c2006
BS
199static int
200nfs_closedir(struct inode *inode, struct file *filp)
201{
202 put_nfs_open_dir_context(filp->private_data);
203 return 0;
204}
205
d1bacf9e
BS
206struct nfs_cache_array_entry {
207 u64 cookie;
208 u64 ino;
209 struct qstr string;
0b26a0bf 210 unsigned char d_type;
d1bacf9e
BS
211};
212
213struct nfs_cache_array {
88b8e133 214 int size;
d1bacf9e
BS
215 int eof_index;
216 u64 last_cookie;
217 struct nfs_cache_array_entry array[0];
218};
219
573c4e1e 220typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
1da177e4
LT
221typedef struct {
222 struct file *file;
223 struct page *page;
224 unsigned long page_index;
f0dd2136 225 u64 *dir_cookie;
0aded708 226 u64 last_cookie;
f0dd2136 227 loff_t current_index;
1da177e4 228 decode_dirent_t decode;
d1bacf9e 229
1f4eab7e 230 unsigned long timestamp;
4704f0e2 231 unsigned long gencount;
d1bacf9e
BS
232 unsigned int cache_entry_index;
233 unsigned int plus:1;
234 unsigned int eof:1;
1da177e4
LT
235} nfs_readdir_descriptor_t;
236
d1bacf9e
BS
237/*
238 * The caller is responsible for calling nfs_readdir_release_array(page)
1da177e4
LT
239 */
240static
d1bacf9e
BS
241struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
242{
8cd51a0c 243 void *ptr;
d1bacf9e
BS
244 if (page == NULL)
245 return ERR_PTR(-EIO);
8cd51a0c
TM
246 ptr = kmap(page);
247 if (ptr == NULL)
248 return ERR_PTR(-ENOMEM);
249 return ptr;
d1bacf9e
BS
250}
251
252static
253void nfs_readdir_release_array(struct page *page)
254{
255 kunmap(page);
256}
257
258/*
259 * we are freeing strings created by nfs_add_to_readdir_array()
260 */
261static
11de3b11 262void nfs_readdir_clear_array(struct page *page)
d1bacf9e 263{
11de3b11 264 struct nfs_cache_array *array;
d1bacf9e 265 int i;
8cd51a0c 266
2b86ce2d 267 array = kmap_atomic(page);
d1bacf9e
BS
268 for (i = 0; i < array->size; i++)
269 kfree(array->array[i].string.name);
2b86ce2d 270 kunmap_atomic(array);
d1bacf9e
BS
271}
272
273/*
274 * the caller is responsible for freeing qstr.name
275 * when called by nfs_readdir_add_to_array, the strings will be freed in
276 * nfs_clear_readdir_array()
277 */
278static
4a201d6e 279int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
d1bacf9e
BS
280{
281 string->len = len;
282 string->name = kmemdup(name, len, GFP_KERNEL);
4a201d6e
TM
283 if (string->name == NULL)
284 return -ENOMEM;
04e4bd1c
CM
285 /*
286 * Avoid a kmemleak false positive. The pointer to the name is stored
287 * in a page cache page which kmemleak does not scan.
288 */
289 kmemleak_not_leak(string->name);
4a201d6e
TM
290 string->hash = full_name_hash(name, len);
291 return 0;
d1bacf9e
BS
292}
293
294static
295int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
296{
297 struct nfs_cache_array *array = nfs_readdir_get_array(page);
4a201d6e
TM
298 struct nfs_cache_array_entry *cache_entry;
299 int ret;
300
d1bacf9e
BS
301 if (IS_ERR(array))
302 return PTR_ERR(array);
3020093f
TM
303
304 cache_entry = &array->array[array->size];
305
306 /* Check that this entry lies within the page bounds */
8cd51a0c 307 ret = -ENOSPC;
3020093f 308 if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
4a201d6e 309 goto out;
d1bacf9e 310
4a201d6e
TM
311 cache_entry->cookie = entry->prev_cookie;
312 cache_entry->ino = entry->ino;
0b26a0bf 313 cache_entry->d_type = entry->d_type;
4a201d6e
TM
314 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
315 if (ret)
316 goto out;
d1bacf9e 317 array->last_cookie = entry->cookie;
8cd51a0c 318 array->size++;
47c716cb 319 if (entry->eof != 0)
d1bacf9e 320 array->eof_index = array->size;
4a201d6e 321out:
d1bacf9e 322 nfs_readdir_release_array(page);
4a201d6e 323 return ret;
d1bacf9e
BS
324}
325
326static
327int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
328{
329 loff_t diff = desc->file->f_pos - desc->current_index;
330 unsigned int index;
331
332 if (diff < 0)
333 goto out_eof;
334 if (diff >= array->size) {
8cd51a0c 335 if (array->eof_index >= 0)
d1bacf9e 336 goto out_eof;
d1bacf9e
BS
337 return -EAGAIN;
338 }
339
340 index = (unsigned int)diff;
341 *desc->dir_cookie = array->array[index].cookie;
342 desc->cache_entry_index = index;
d1bacf9e
BS
343 return 0;
344out_eof:
345 desc->eof = 1;
346 return -EBADCOOKIE;
347}
348
349static
350int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
351{
352 int i;
8ef2ce3e 353 loff_t new_pos;
d1bacf9e
BS
354 int status = -EAGAIN;
355
356 for (i = 0; i < array->size; i++) {
d1bacf9e 357 if (array->array[i].cookie == *desc->dir_cookie) {
0c030806
TM
358 struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode);
359 struct nfs_open_dir_context *ctx = desc->file->private_data;
360
8ef2ce3e 361 new_pos = desc->current_index + i;
0c030806
TM
362 if (ctx->attr_gencount != nfsi->attr_gencount
363 || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
364 ctx->duped = 0;
365 ctx->attr_gencount = nfsi->attr_gencount;
366 } else if (new_pos < desc->file->f_pos) {
367 if (ctx->duped > 0
368 && ctx->dup_cookie == *desc->dir_cookie) {
369 if (printk_ratelimit()) {
370 pr_notice("NFS: directory %s/%s contains a readdir loop."
371 "Please contact your server vendor. "
374e4e3e 372 "The file: %s has duplicate cookie %llu\n",
0c030806
TM
373 desc->file->f_dentry->d_parent->d_name.name,
374 desc->file->f_dentry->d_name.name,
374e4e3e 375 array->array[i].string.name,
0c030806
TM
376 *desc->dir_cookie);
377 }
378 status = -ELOOP;
379 goto out;
380 }
8ef2ce3e 381 ctx->dup_cookie = *desc->dir_cookie;
0c030806 382 ctx->duped = -1;
8ef2ce3e
BS
383 }
384 desc->file->f_pos = new_pos;
d1bacf9e 385 desc->cache_entry_index = i;
47c716cb 386 return 0;
d1bacf9e
BS
387 }
388 }
47c716cb 389 if (array->eof_index >= 0) {
8cd51a0c 390 status = -EBADCOOKIE;
18fb5fe4
TM
391 if (*desc->dir_cookie == array->last_cookie)
392 desc->eof = 1;
8cd51a0c 393 }
0c030806 394out:
d1bacf9e
BS
395 return status;
396}
397
398static
399int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
400{
401 struct nfs_cache_array *array;
47c716cb 402 int status;
d1bacf9e
BS
403
404 array = nfs_readdir_get_array(desc->page);
405 if (IS_ERR(array)) {
406 status = PTR_ERR(array);
407 goto out;
408 }
409
410 if (*desc->dir_cookie == 0)
411 status = nfs_readdir_search_for_pos(array, desc);
412 else
413 status = nfs_readdir_search_for_cookie(array, desc);
414
47c716cb 415 if (status == -EAGAIN) {
0aded708 416 desc->last_cookie = array->last_cookie;
e47c085a 417 desc->current_index += array->size;
47c716cb
TM
418 desc->page_index++;
419 }
d1bacf9e
BS
420 nfs_readdir_release_array(desc->page);
421out:
422 return status;
423}
424
425/* Fill a page with xdr information before transferring to the cache page */
426static
56e4ebf8 427int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
d1bacf9e 428 struct nfs_entry *entry, struct file *file, struct inode *inode)
1da177e4 429{
480c2006
BS
430 struct nfs_open_dir_context *ctx = file->private_data;
431 struct rpc_cred *cred = ctx->cred;
4704f0e2 432 unsigned long timestamp, gencount;
1da177e4
LT
433 int error;
434
1da177e4
LT
435 again:
436 timestamp = jiffies;
4704f0e2 437 gencount = nfs_inc_attr_generation_counter();
56e4ebf8 438 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
1da177e4
LT
439 NFS_SERVER(inode)->dtsize, desc->plus);
440 if (error < 0) {
441 /* We requested READDIRPLUS, but the server doesn't grok it */
442 if (error == -ENOTSUPP && desc->plus) {
443 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3a10c30a 444 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
1da177e4
LT
445 desc->plus = 0;
446 goto again;
447 }
448 goto error;
449 }
1f4eab7e 450 desc->timestamp = timestamp;
4704f0e2 451 desc->gencount = gencount;
d1bacf9e
BS
452error:
453 return error;
1da177e4
LT
454}
455
573c4e1e
CL
456static int xdr_decode(nfs_readdir_descriptor_t *desc,
457 struct nfs_entry *entry, struct xdr_stream *xdr)
1da177e4 458{
573c4e1e 459 int error;
1da177e4 460
573c4e1e
CL
461 error = desc->decode(xdr, entry, desc->plus);
462 if (error)
463 return error;
d1bacf9e
BS
464 entry->fattr->time_start = desc->timestamp;
465 entry->fattr->gencount = desc->gencount;
466 return 0;
1da177e4
LT
467}
468
d39ab9de
BS
469static
470int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
471{
d39ab9de
BS
472 if (dentry->d_inode == NULL)
473 goto different;
37a09f07 474 if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
d39ab9de
BS
475 goto different;
476 return 1;
477different:
478 return 0;
479}
480
d69ee9b8
TM
481static
482bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
483{
484 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
485 return false;
486 if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
487 return true;
488 if (filp->f_pos == 0)
489 return true;
490 return false;
491}
492
493/*
494 * This function is called by the lookup code to request the use of
495 * readdirplus to accelerate any future lookups in the same
496 * directory.
497 */
498static
499void nfs_advise_use_readdirplus(struct inode *dir)
500{
501 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
502}
503
d39ab9de
BS
504static
505void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
506{
26fe5750 507 struct qstr filename = QSTR_INIT(entry->name, entry->len);
4a201d6e
TM
508 struct dentry *dentry;
509 struct dentry *alias;
d39ab9de
BS
510 struct inode *dir = parent->d_inode;
511 struct inode *inode;
512
4a201d6e
TM
513 if (filename.name[0] == '.') {
514 if (filename.len == 1)
515 return;
516 if (filename.len == 2 && filename.name[1] == '.')
517 return;
518 }
519 filename.hash = full_name_hash(filename.name, filename.len);
d39ab9de 520
4a201d6e 521 dentry = d_lookup(parent, &filename);
d39ab9de
BS
522 if (dentry != NULL) {
523 if (nfs_same_file(dentry, entry)) {
524 nfs_refresh_inode(dentry->d_inode, entry->fattr);
525 goto out;
526 } else {
527 d_drop(dentry);
528 dput(dentry);
529 }
530 }
531
532 dentry = d_alloc(parent, &filename);
4a201d6e
TM
533 if (dentry == NULL)
534 return;
535
d39ab9de
BS
536 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
537 if (IS_ERR(inode))
538 goto out;
539
540 alias = d_materialise_unique(dentry, inode);
541 if (IS_ERR(alias))
542 goto out;
543 else if (alias) {
544 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
545 dput(alias);
546 } else
547 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
548
549out:
550 dput(dentry);
d39ab9de
BS
551}
552
d1bacf9e
BS
553/* Perform conversion from xdr to cache array */
554static
8cd51a0c 555int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
6650239a 556 struct page **xdr_pages, struct page *page, unsigned int buflen)
1da177e4 557{
babddc72 558 struct xdr_stream stream;
f7da7a12 559 struct xdr_buf buf;
6650239a 560 struct page *scratch;
99424380 561 struct nfs_cache_array *array;
5c346854
TM
562 unsigned int count = 0;
563 int status;
babddc72 564
6650239a
TM
565 scratch = alloc_page(GFP_KERNEL);
566 if (scratch == NULL)
567 return -ENOMEM;
babddc72 568
f7da7a12 569 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
6650239a 570 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
99424380
BS
571
572 do {
573 status = xdr_decode(desc, entry, &stream);
8cd51a0c
TM
574 if (status != 0) {
575 if (status == -EAGAIN)
576 status = 0;
99424380 577 break;
8cd51a0c 578 }
99424380 579
5c346854
TM
580 count++;
581
47c716cb 582 if (desc->plus != 0)
d39ab9de 583 nfs_prime_dcache(desc->file->f_path.dentry, entry);
8cd51a0c
TM
584
585 status = nfs_readdir_add_to_array(entry, page);
586 if (status != 0)
587 break;
99424380
BS
588 } while (!entry->eof);
589
47c716cb 590 if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
99424380 591 array = nfs_readdir_get_array(page);
8cd51a0c
TM
592 if (!IS_ERR(array)) {
593 array->eof_index = array->size;
594 status = 0;
595 nfs_readdir_release_array(page);
5c346854
TM
596 } else
597 status = PTR_ERR(array);
1da177e4 598 }
6650239a
TM
599
600 put_page(scratch);
8cd51a0c 601 return status;
56e4ebf8
BS
602}
603
604static
605void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
606{
607 unsigned int i;
608 for (i = 0; i < npages; i++)
609 put_page(pages[i]);
610}
611
612static
613void nfs_readdir_free_large_page(void *ptr, struct page **pages,
614 unsigned int npages)
615{
56e4ebf8
BS
616 nfs_readdir_free_pagearray(pages, npages);
617}
618
619/*
620 * nfs_readdir_large_page will allocate pages that must be freed with a call
621 * to nfs_readdir_free_large_page
622 */
623static
6650239a 624int nfs_readdir_large_page(struct page **pages, unsigned int npages)
56e4ebf8 625{
56e4ebf8
BS
626 unsigned int i;
627
628 for (i = 0; i < npages; i++) {
629 struct page *page = alloc_page(GFP_KERNEL);
630 if (page == NULL)
631 goto out_freepages;
632 pages[i] = page;
633 }
6650239a 634 return 0;
56e4ebf8 635
56e4ebf8
BS
636out_freepages:
637 nfs_readdir_free_pagearray(pages, i);
6650239a 638 return -ENOMEM;
1da177e4
LT
639}
640
d1bacf9e
BS
641static
642int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
00a92642 643{
56e4ebf8
BS
644 struct page *pages[NFS_MAX_READDIR_PAGES];
645 void *pages_ptr = NULL;
d1bacf9e
BS
646 struct nfs_entry entry;
647 struct file *file = desc->file;
648 struct nfs_cache_array *array;
8cd51a0c 649 int status = -ENOMEM;
56e4ebf8 650 unsigned int array_size = ARRAY_SIZE(pages);
d1bacf9e
BS
651
652 entry.prev_cookie = 0;
0aded708 653 entry.cookie = desc->last_cookie;
d1bacf9e
BS
654 entry.eof = 0;
655 entry.fh = nfs_alloc_fhandle();
656 entry.fattr = nfs_alloc_fattr();
573c4e1e 657 entry.server = NFS_SERVER(inode);
d1bacf9e
BS
658 if (entry.fh == NULL || entry.fattr == NULL)
659 goto out;
00a92642 660
d1bacf9e 661 array = nfs_readdir_get_array(page);
8cd51a0c
TM
662 if (IS_ERR(array)) {
663 status = PTR_ERR(array);
664 goto out;
665 }
d1bacf9e
BS
666 memset(array, 0, sizeof(struct nfs_cache_array));
667 array->eof_index = -1;
00a92642 668
6650239a
TM
669 status = nfs_readdir_large_page(pages, array_size);
670 if (status < 0)
d1bacf9e
BS
671 goto out_release_array;
672 do {
ac396128 673 unsigned int pglen;
56e4ebf8 674 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
babddc72 675
d1bacf9e 676 if (status < 0)
00a92642 677 break;
ac396128 678 pglen = status;
6650239a 679 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
8cd51a0c
TM
680 if (status < 0) {
681 if (status == -ENOSPC)
682 status = 0;
683 break;
684 }
685 } while (array->eof_index < 0);
d1bacf9e 686
56e4ebf8 687 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
d1bacf9e
BS
688out_release_array:
689 nfs_readdir_release_array(page);
690out:
691 nfs_free_fattr(entry.fattr);
692 nfs_free_fhandle(entry.fh);
00a92642
OG
693 return status;
694}
695
696/*
d1bacf9e
BS
697 * Now we cache directories properly, by converting xdr information
698 * to an array that can be used for lookups later. This results in
699 * fewer cache pages, since we can store more information on each page.
700 * We only need to convert from xdr once so future lookups are much simpler
1da177e4 701 */
d1bacf9e
BS
702static
703int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
1da177e4 704{
01cce933 705 struct inode *inode = desc->file->f_path.dentry->d_inode;
8cd51a0c 706 int ret;
1da177e4 707
8cd51a0c
TM
708 ret = nfs_readdir_xdr_to_array(desc, page, inode);
709 if (ret < 0)
d1bacf9e
BS
710 goto error;
711 SetPageUptodate(page);
1da177e4 712
d1bacf9e
BS
713 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
714 /* Should never happen */
715 nfs_zap_mapping(inode, inode->i_mapping);
1da177e4 716 }
d1bacf9e
BS
717 unlock_page(page);
718 return 0;
719 error:
720 unlock_page(page);
8cd51a0c 721 return ret;
d1bacf9e 722}
1da177e4 723
d1bacf9e
BS
724static
725void cache_page_release(nfs_readdir_descriptor_t *desc)
726{
11de3b11
TM
727 if (!desc->page->mapping)
728 nfs_readdir_clear_array(desc->page);
d1bacf9e
BS
729 page_cache_release(desc->page);
730 desc->page = NULL;
731}
732
733static
734struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
735{
8cd51a0c 736 return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
d1bacf9e 737 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
1da177e4
LT
738}
739
740/*
d1bacf9e 741 * Returns 0 if desc->dir_cookie was found on page desc->page_index
1da177e4 742 */
d1bacf9e
BS
743static
744int find_cache_page(nfs_readdir_descriptor_t *desc)
745{
746 int res;
747
748 desc->page = get_cache_page(desc);
749 if (IS_ERR(desc->page))
750 return PTR_ERR(desc->page);
751
752 res = nfs_readdir_search_array(desc);
47c716cb
TM
753 if (res != 0)
754 cache_page_release(desc);
d1bacf9e
BS
755 return res;
756}
757
758/* Search for desc->dir_cookie from the beginning of the page cache */
1da177e4
LT
759static inline
760int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
761{
8cd51a0c 762 int res;
d1bacf9e 763
0aded708 764 if (desc->page_index == 0) {
8cd51a0c 765 desc->current_index = 0;
0aded708
TM
766 desc->last_cookie = 0;
767 }
47c716cb 768 do {
d1bacf9e 769 res = find_cache_page(desc);
47c716cb 770 } while (res == -EAGAIN);
1da177e4
LT
771 return res;
772}
773
1da177e4
LT
774/*
775 * Once we've found the start of the dirent within a page: fill 'er up...
776 */
777static
778int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
779 filldir_t filldir)
780{
781 struct file *file = desc->file;
d1bacf9e
BS
782 int i = 0;
783 int res = 0;
784 struct nfs_cache_array *array = NULL;
8ef2ce3e
BS
785 struct nfs_open_dir_context *ctx = file->private_data;
786
d1bacf9e 787 array = nfs_readdir_get_array(desc->page);
e7c58e97
TM
788 if (IS_ERR(array)) {
789 res = PTR_ERR(array);
790 goto out;
791 }
d1bacf9e
BS
792
793 for (i = desc->cache_entry_index; i < array->size; i++) {
ece0b423 794 struct nfs_cache_array_entry *ent;
1da177e4 795
ece0b423
TM
796 ent = &array->array[i];
797 if (filldir(dirent, ent->string.name, ent->string.len,
0b26a0bf
TM
798 file->f_pos, nfs_compat_user_ino64(ent->ino),
799 ent->d_type) < 0) {
ece0b423 800 desc->eof = 1;
1da177e4 801 break;
ece0b423 802 }
00a92642 803 file->f_pos++;
d1bacf9e
BS
804 if (i < (array->size-1))
805 *desc->dir_cookie = array->array[i+1].cookie;
806 else
807 *desc->dir_cookie = array->last_cookie;
0c030806
TM
808 if (ctx->duped != 0)
809 ctx->duped = 1;
1da177e4 810 }
47c716cb 811 if (array->eof_index >= 0)
8cd51a0c 812 desc->eof = 1;
d1bacf9e
BS
813
814 nfs_readdir_release_array(desc->page);
e7c58e97 815out:
d1bacf9e 816 cache_page_release(desc);
1e7cb3dc
CL
817 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
818 (unsigned long long)*desc->dir_cookie, res);
1da177e4
LT
819 return res;
820}
821
822/*
823 * If we cannot find a cookie in our cache, we suspect that this is
824 * because it points to a deleted file, so we ask the server to return
825 * whatever it thinks is the next entry. We then feed this to filldir.
826 * If all goes well, we should then be able to find our way round the
827 * cache on the next call to readdir_search_pagecache();
828 *
829 * NOTE: we cannot add the anonymous page to the pagecache because
830 * the data it contains might not be page aligned. Besides,
831 * we should already have a complete representation of the
832 * directory in the page cache by the time we get here.
833 */
834static inline
835int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
836 filldir_t filldir)
837{
1da177e4
LT
838 struct page *page = NULL;
839 int status;
d1bacf9e 840 struct inode *inode = desc->file->f_path.dentry->d_inode;
0c030806 841 struct nfs_open_dir_context *ctx = desc->file->private_data;
1da177e4 842
1e7cb3dc
CL
843 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
844 (unsigned long long)*desc->dir_cookie);
1da177e4
LT
845
846 page = alloc_page(GFP_HIGHUSER);
847 if (!page) {
848 status = -ENOMEM;
849 goto out;
850 }
d1bacf9e 851
7a8e1dc3 852 desc->page_index = 0;
0aded708 853 desc->last_cookie = *desc->dir_cookie;
7a8e1dc3 854 desc->page = page;
0c030806 855 ctx->duped = 0;
7a8e1dc3 856
85f8607e
TM
857 status = nfs_readdir_xdr_to_array(desc, page, inode);
858 if (status < 0)
1da177e4
LT
859 goto out_release;
860
861 status = nfs_do_filldir(desc, dirent, filldir);
862
1da177e4 863 out:
1e7cb3dc 864 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
3110ff80 865 __func__, status);
1da177e4
LT
866 return status;
867 out_release:
d1bacf9e 868 cache_page_release(desc);
1da177e4
LT
869 goto out;
870}
871
00a92642
OG
872/* The file offset position represents the dirent entry number. A
873 last cookie cache takes care of the common case of reading the
874 whole directory.
1da177e4
LT
875 */
876static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
877{
01cce933 878 struct dentry *dentry = filp->f_path.dentry;
1da177e4
LT
879 struct inode *inode = dentry->d_inode;
880 nfs_readdir_descriptor_t my_desc,
881 *desc = &my_desc;
480c2006 882 struct nfs_open_dir_context *dir_ctx = filp->private_data;
47c716cb 883 int res;
1da177e4 884
6da24bc9 885 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
1e7cb3dc
CL
886 dentry->d_parent->d_name.name, dentry->d_name.name,
887 (long long)filp->f_pos);
91d5b470
CL
888 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
889
1da177e4 890 /*
00a92642 891 * filp->f_pos points to the dirent entry number.
f0dd2136 892 * *desc->dir_cookie has the cookie for the next entry. We have
00a92642
OG
893 * to either find the entry with the appropriate number or
894 * revalidate the cookie.
1da177e4
LT
895 */
896 memset(desc, 0, sizeof(*desc));
897
898 desc->file = filp;
480c2006 899 desc->dir_cookie = &dir_ctx->dir_cookie;
1da177e4 900 desc->decode = NFS_PROTO(inode)->decode_dirent;
d69ee9b8 901 desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
1da177e4 902
565277f6 903 nfs_block_sillyrename(dentry);
1cda707d 904 res = nfs_revalidate_mapping(inode, filp->f_mapping);
fccca7fc
TM
905 if (res < 0)
906 goto out;
907
47c716cb 908 do {
1da177e4 909 res = readdir_search_pagecache(desc);
00a92642 910
1da177e4 911 if (res == -EBADCOOKIE) {
ece0b423 912 res = 0;
1da177e4 913 /* This means either end of directory */
d1bacf9e 914 if (*desc->dir_cookie && desc->eof == 0) {
1da177e4
LT
915 /* Or that the server has 'lost' a cookie */
916 res = uncached_readdir(desc, dirent, filldir);
ece0b423 917 if (res == 0)
1da177e4
LT
918 continue;
919 }
1da177e4
LT
920 break;
921 }
922 if (res == -ETOOSMALL && desc->plus) {
3a10c30a 923 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
1da177e4 924 nfs_zap_caches(inode);
baf57a09 925 desc->page_index = 0;
1da177e4 926 desc->plus = 0;
d1bacf9e 927 desc->eof = 0;
1da177e4
LT
928 continue;
929 }
930 if (res < 0)
931 break;
932
933 res = nfs_do_filldir(desc, dirent, filldir);
ece0b423 934 if (res < 0)
1da177e4 935 break;
47c716cb 936 } while (!desc->eof);
fccca7fc 937out:
565277f6 938 nfs_unblock_sillyrename(dentry);
1e7cb3dc
CL
939 if (res > 0)
940 res = 0;
aa49b4cf 941 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
1e7cb3dc
CL
942 dentry->d_parent->d_name.name, dentry->d_name.name,
943 res);
944 return res;
1da177e4
LT
945}
946
10afec90 947static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
f0dd2136 948{
b84e06c5
CL
949 struct dentry *dentry = filp->f_path.dentry;
950 struct inode *inode = dentry->d_inode;
480c2006 951 struct nfs_open_dir_context *dir_ctx = filp->private_data;
b84e06c5 952
6da24bc9 953 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
b84e06c5
CL
954 dentry->d_parent->d_name.name,
955 dentry->d_name.name,
956 offset, origin);
957
958 mutex_lock(&inode->i_mutex);
f0dd2136
TM
959 switch (origin) {
960 case 1:
961 offset += filp->f_pos;
962 case 0:
963 if (offset >= 0)
964 break;
965 default:
966 offset = -EINVAL;
967 goto out;
968 }
969 if (offset != filp->f_pos) {
970 filp->f_pos = offset;
480c2006 971 dir_ctx->dir_cookie = 0;
8ef2ce3e 972 dir_ctx->duped = 0;
f0dd2136
TM
973 }
974out:
b84e06c5 975 mutex_unlock(&inode->i_mutex);
f0dd2136
TM
976 return offset;
977}
978
1da177e4
LT
979/*
980 * All directory operations under NFS are synchronous, so fsync()
981 * is a dummy operation.
982 */
02c24a82
JB
983static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
984 int datasync)
1da177e4 985{
7ea80859 986 struct dentry *dentry = filp->f_path.dentry;
02c24a82 987 struct inode *inode = dentry->d_inode;
7ea80859 988
6da24bc9 989 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
1e7cb3dc
CL
990 dentry->d_parent->d_name.name, dentry->d_name.name,
991 datasync);
992
02c24a82 993 mutex_lock(&inode->i_mutex);
54917786 994 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
02c24a82 995 mutex_unlock(&inode->i_mutex);
1da177e4
LT
996 return 0;
997}
998
bfc69a45
TM
999/**
1000 * nfs_force_lookup_revalidate - Mark the directory as having changed
1001 * @dir - pointer to directory inode
1002 *
1003 * This forces the revalidation code in nfs_lookup_revalidate() to do a
1004 * full lookup on all child dentries of 'dir' whenever a change occurs
1005 * on the server that might have invalidated our dcache.
1006 *
1007 * The caller should be holding dir->i_lock
1008 */
1009void nfs_force_lookup_revalidate(struct inode *dir)
1010{
011935a0 1011 NFS_I(dir)->cache_change_attribute++;
bfc69a45
TM
1012}
1013
1da177e4
LT
1014/*
1015 * A check for whether or not the parent directory has changed.
1016 * In the case it has, we assume that the dentries are untrustworthy
1017 * and may need to be looked up again.
1018 */
c79ba787 1019static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
1da177e4
LT
1020{
1021 if (IS_ROOT(dentry))
1022 return 1;
4eec952e
TM
1023 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1024 return 0;
f2c77f4e
TM
1025 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1026 return 0;
1027 /* Revalidate nfsi->cache_change_attribute before we declare a match */
1028 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1029 return 0;
1030 if (!nfs_verify_change_attribute(dir, dentry->d_time))
1031 return 0;
1032 return 1;
1da177e4
LT
1033}
1034
1d6757fb
TM
1035/*
1036 * Return the intent data that applies to this particular path component
1037 *
1038 * Note that the current set of intents only apply to the very last
8aeb376c
AV
1039 * component of the path and none of them is set before that last
1040 * component.
1d6757fb 1041 */
34286d66
NP
1042static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
1043 unsigned int mask)
1d6757fb 1044{
1d6757fb
TM
1045 return nd->flags & mask;
1046}
1047
a12802ca
TM
1048/*
1049 * Use intent information to check whether or not we're going to do
1050 * an O_EXCL create using this path component.
1051 */
1052static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
1053{
1054 if (NFS_PROTO(dir)->version == 2)
1055 return 0;
3516586a 1056 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
a12802ca
TM
1057}
1058
1d6757fb
TM
1059/*
1060 * Inode and filehandle revalidation for lookups.
1061 *
1062 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1063 * or if the intent information indicates that we're about to open this
1064 * particular file and the "nocto" mount flag is not set.
1065 *
1066 */
1da177e4
LT
1067static inline
1068int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
1069{
1070 struct nfs_server *server = NFS_SERVER(inode);
1071
36d43a43 1072 if (IS_AUTOMOUNT(inode))
4e99a1ff 1073 return 0;
1da177e4 1074 if (nd != NULL) {
1da177e4 1075 /* VFS wants an on-the-wire revalidation */
1d6757fb 1076 if (nd->flags & LOOKUP_REVAL)
1da177e4
LT
1077 goto out_force;
1078 /* This is an open(2) */
1d6757fb 1079 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
4e0641a7
TM
1080 !(server->flags & NFS_MOUNT_NOCTO) &&
1081 (S_ISREG(inode->i_mode) ||
1082 S_ISDIR(inode->i_mode)))
1da177e4 1083 goto out_force;
4f48af45 1084 return 0;
1da177e4
LT
1085 }
1086 return nfs_revalidate_inode(server, inode);
1087out_force:
1088 return __nfs_revalidate_inode(server, inode);
1089}
1090
1091/*
1092 * We judge how long we want to trust negative
1093 * dentries by looking at the parent inode mtime.
1094 *
1095 * If parent mtime has changed, we revalidate, else we wait for a
1096 * period corresponding to the parent's attribute cache timeout value.
1097 */
1098static inline
1099int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1100 struct nameidata *nd)
1101{
1da177e4 1102 /* Don't revalidate a negative dentry if we're creating a new file */
1d6757fb 1103 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
1da177e4 1104 return 0;
4eec952e
TM
1105 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1106 return 1;
1da177e4
LT
1107 return !nfs_check_verifier(dir, dentry);
1108}
1109
1110/*
1111 * This is called every time the dcache has a lookup hit,
1112 * and we should check whether we can really trust that
1113 * lookup.
1114 *
1115 * NOTE! The hit can be a negative hit too, don't assume
1116 * we have an inode!
1117 *
1118 * If the parent directory is seen to have changed, we throw out the
1119 * cached dentry and do a new lookup.
1120 */
34286d66 1121static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1122{
1123 struct inode *dir;
1124 struct inode *inode;
1125 struct dentry *parent;
e1fb4d05
TM
1126 struct nfs_fh *fhandle = NULL;
1127 struct nfs_fattr *fattr = NULL;
1da177e4 1128 int error;
1da177e4 1129
34286d66
NP
1130 if (nd->flags & LOOKUP_RCU)
1131 return -ECHILD;
1132
1da177e4 1133 parent = dget_parent(dentry);
1da177e4 1134 dir = parent->d_inode;
91d5b470 1135 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1da177e4
LT
1136 inode = dentry->d_inode;
1137
1138 if (!inode) {
1139 if (nfs_neg_need_reval(dir, dentry, nd))
1140 goto out_bad;
d69ee9b8 1141 goto out_valid_noent;
1da177e4
LT
1142 }
1143
1144 if (is_bad_inode(inode)) {
1e7cb3dc 1145 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
3110ff80 1146 __func__, dentry->d_parent->d_name.name,
1e7cb3dc 1147 dentry->d_name.name);
1da177e4
LT
1148 goto out_bad;
1149 }
1150
15860ab1
TM
1151 if (nfs_have_delegation(inode, FMODE_READ))
1152 goto out_set_verifier;
1153
1da177e4 1154 /* Force a full look up iff the parent directory has changed */
a12802ca 1155 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
1da177e4
LT
1156 if (nfs_lookup_verify_inode(inode, nd))
1157 goto out_zap_parent;
1158 goto out_valid;
1159 }
1160
1161 if (NFS_STALE(inode))
1162 goto out_bad;
1163
e1fb4d05
TM
1164 error = -ENOMEM;
1165 fhandle = nfs_alloc_fhandle();
1166 fattr = nfs_alloc_fattr();
1167 if (fhandle == NULL || fattr == NULL)
1168 goto out_error;
1169
80a16b21 1170 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1da177e4
LT
1171 if (error)
1172 goto out_bad;
e1fb4d05 1173 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1da177e4 1174 goto out_bad;
e1fb4d05 1175 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
1da177e4
LT
1176 goto out_bad;
1177
e1fb4d05
TM
1178 nfs_free_fattr(fattr);
1179 nfs_free_fhandle(fhandle);
15860ab1 1180out_set_verifier:
cf8ba45e 1181 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4 1182 out_valid:
d69ee9b8
TM
1183 /* Success: notify readdir to use READDIRPLUS */
1184 nfs_advise_use_readdirplus(dir);
1185 out_valid_noent:
1da177e4 1186 dput(parent);
1e7cb3dc 1187 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
3110ff80 1188 __func__, dentry->d_parent->d_name.name,
1e7cb3dc 1189 dentry->d_name.name);
1da177e4
LT
1190 return 1;
1191out_zap_parent:
1192 nfs_zap_caches(dir);
1193 out_bad:
a1643a92 1194 nfs_mark_for_revalidate(dir);
1da177e4
LT
1195 if (inode && S_ISDIR(inode->i_mode)) {
1196 /* Purge readdir caches. */
1197 nfs_zap_caches(inode);
1198 /* If we have submounts, don't unhash ! */
1199 if (have_submounts(dentry))
1200 goto out_valid;
d9e80b7d
AV
1201 if (dentry->d_flags & DCACHE_DISCONNECTED)
1202 goto out_valid;
1da177e4
LT
1203 shrink_dcache_parent(dentry);
1204 }
1205 d_drop(dentry);
e1fb4d05
TM
1206 nfs_free_fattr(fattr);
1207 nfs_free_fhandle(fhandle);
1da177e4 1208 dput(parent);
1e7cb3dc 1209 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
3110ff80 1210 __func__, dentry->d_parent->d_name.name,
1e7cb3dc 1211 dentry->d_name.name);
1da177e4 1212 return 0;
e1fb4d05
TM
1213out_error:
1214 nfs_free_fattr(fattr);
1215 nfs_free_fhandle(fhandle);
1216 dput(parent);
1217 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1218 __func__, dentry->d_parent->d_name.name,
1219 dentry->d_name.name, error);
1220 return error;
1da177e4
LT
1221}
1222
1223/*
1224 * This is called from dput() when d_count is going to 0.
1225 */
fe15ce44 1226static int nfs_dentry_delete(const struct dentry *dentry)
1da177e4
LT
1227{
1228 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1229 dentry->d_parent->d_name.name, dentry->d_name.name,
1230 dentry->d_flags);
1231
77f11192
TM
1232 /* Unhash any dentry with a stale inode */
1233 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1234 return 1;
1235
1da177e4
LT
1236 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1237 /* Unhash it, so that ->d_iput() would be called */
1238 return 1;
1239 }
1240 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1241 /* Unhash it, so that ancestors of killed async unlink
1242 * files will be cleaned up during umount */
1243 return 1;
1244 }
1245 return 0;
1246
1247}
1248
1b83d707
TM
1249static void nfs_drop_nlink(struct inode *inode)
1250{
1251 spin_lock(&inode->i_lock);
1252 if (inode->i_nlink > 0)
1253 drop_nlink(inode);
1254 spin_unlock(&inode->i_lock);
1255}
1256
1da177e4
LT
1257/*
1258 * Called when the dentry loses inode.
1259 * We use it to clean up silly-renamed files.
1260 */
1261static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1262{
83672d39
NB
1263 if (S_ISDIR(inode->i_mode))
1264 /* drop any readdir cache as it could easily be old */
1265 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1266
1da177e4 1267 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
9a53c3a7 1268 drop_nlink(inode);
e4eff1a6 1269 nfs_complete_unlink(dentry, inode);
1da177e4 1270 }
1da177e4
LT
1271 iput(inode);
1272}
1273
b1942c5f
AV
1274static void nfs_d_release(struct dentry *dentry)
1275{
1276 /* free cached devname value, if it survived that far */
1277 if (unlikely(dentry->d_fsdata)) {
1278 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1279 WARN_ON(1);
1280 else
1281 kfree(dentry->d_fsdata);
1282 }
1283}
1284
f786aa90 1285const struct dentry_operations nfs_dentry_operations = {
1da177e4
LT
1286 .d_revalidate = nfs_lookup_revalidate,
1287 .d_delete = nfs_dentry_delete,
1288 .d_iput = nfs_dentry_iput,
36d43a43 1289 .d_automount = nfs_d_automount,
b1942c5f 1290 .d_release = nfs_d_release,
1da177e4
LT
1291};
1292
1da177e4
LT
1293static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1294{
1295 struct dentry *res;
565277f6 1296 struct dentry *parent;
1da177e4 1297 struct inode *inode = NULL;
e1fb4d05
TM
1298 struct nfs_fh *fhandle = NULL;
1299 struct nfs_fattr *fattr = NULL;
1da177e4 1300 int error;
1da177e4
LT
1301
1302 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1303 dentry->d_parent->d_name.name, dentry->d_name.name);
91d5b470 1304 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1da177e4
LT
1305
1306 res = ERR_PTR(-ENAMETOOLONG);
1307 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1308 goto out;
1309
fd684071
TM
1310 /*
1311 * If we're doing an exclusive create, optimize away the lookup
1312 * but don't hash the dentry.
1313 */
1314 if (nfs_is_exclusive_create(dir, nd)) {
1315 d_instantiate(dentry, NULL);
1316 res = NULL;
fc0f684c 1317 goto out;
fd684071 1318 }
1da177e4 1319
e1fb4d05
TM
1320 res = ERR_PTR(-ENOMEM);
1321 fhandle = nfs_alloc_fhandle();
1322 fattr = nfs_alloc_fattr();
1323 if (fhandle == NULL || fattr == NULL)
1324 goto out;
1325
565277f6
TM
1326 parent = dentry->d_parent;
1327 /* Protect against concurrent sillydeletes */
1328 nfs_block_sillyrename(parent);
80a16b21 1329 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1da177e4
LT
1330 if (error == -ENOENT)
1331 goto no_entry;
1332 if (error < 0) {
1333 res = ERR_PTR(error);
565277f6 1334 goto out_unblock_sillyrename;
1da177e4 1335 }
e1fb4d05 1336 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
bf0c84f1 1337 res = ERR_CAST(inode);
03f28e3a 1338 if (IS_ERR(res))
565277f6 1339 goto out_unblock_sillyrename;
54ceac45 1340
d69ee9b8
TM
1341 /* Success: notify readdir to use READDIRPLUS */
1342 nfs_advise_use_readdirplus(dir);
1343
1da177e4 1344no_entry:
54ceac45 1345 res = d_materialise_unique(dentry, inode);
9eaef27b
TM
1346 if (res != NULL) {
1347 if (IS_ERR(res))
565277f6 1348 goto out_unblock_sillyrename;
1da177e4 1349 dentry = res;
9eaef27b 1350 }
1da177e4 1351 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
565277f6
TM
1352out_unblock_sillyrename:
1353 nfs_unblock_sillyrename(parent);
1da177e4 1354out:
e1fb4d05
TM
1355 nfs_free_fattr(fattr);
1356 nfs_free_fhandle(fhandle);
1da177e4
LT
1357 return res;
1358}
1359
1360#ifdef CONFIG_NFS_V4
0ef97dcf 1361static int nfs4_lookup_revalidate(struct dentry *, struct nameidata *);
1da177e4 1362
f786aa90 1363const struct dentry_operations nfs4_dentry_operations = {
0ef97dcf 1364 .d_revalidate = nfs4_lookup_revalidate,
1da177e4
LT
1365 .d_delete = nfs_dentry_delete,
1366 .d_iput = nfs_dentry_iput,
36d43a43 1367 .d_automount = nfs_d_automount,
b1942c5f 1368 .d_release = nfs_d_release,
1da177e4
LT
1369};
1370
1d6757fb
TM
1371/*
1372 * Use intent information to determine whether we need to substitute
1373 * the NFSv4-style stateful OPEN for the LOOKUP call
1374 */
5584c306 1375static int is_atomic_open(struct nameidata *nd)
1da177e4 1376{
1d6757fb 1377 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
1da177e4
LT
1378 return 0;
1379 /* NFS does not (yet) have a stateful open for directories */
1380 if (nd->flags & LOOKUP_DIRECTORY)
1381 return 0;
1382 /* Are we trying to write to a read only partition? */
2c463e95 1383 if (__mnt_is_readonly(nd->path.mnt) &&
8a5e929d 1384 (nd->intent.open.flags & (O_CREAT|O_TRUNC|O_ACCMODE)))
1da177e4
LT
1385 return 0;
1386 return 1;
1387}
1388
8a5e929d
AV
1389static fmode_t flags_to_mode(int flags)
1390{
1391 fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1392 if ((flags & O_ACCMODE) != O_WRONLY)
1393 res |= FMODE_READ;
1394 if ((flags & O_ACCMODE) != O_RDONLY)
1395 res |= FMODE_WRITE;
1396 return res;
1397}
1398
51141598 1399static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
cd9a1c0e 1400{
5ede7b1c 1401 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
cd9a1c0e
TM
1402}
1403
1404static int do_open(struct inode *inode, struct file *filp)
1405{
1406 nfs_fscache_set_inode_cookie(inode, filp);
1407 return 0;
1408}
1409
0dd2b474
MS
1410static struct file *nfs_finish_open(struct nfs_open_context *ctx,
1411 struct dentry *dentry,
1412 struct opendata *od, unsigned open_flags)
cd9a1c0e
TM
1413{
1414 struct file *filp;
0dd2b474
MS
1415 int err;
1416
1417 if (ctx->dentry != dentry) {
1418 dput(ctx->dentry);
1419 ctx->dentry = dget(dentry);
1420 }
cd9a1c0e
TM
1421
1422 /* If the open_intent is for execute, we have an extra check to make */
1423 if (ctx->mode & FMODE_EXEC) {
0dd2b474
MS
1424 err = nfs_may_open(dentry->d_inode, ctx->cred, open_flags);
1425 if (err < 0) {
1426 filp = ERR_PTR(err);
cd9a1c0e 1427 goto out;
0dd2b474 1428 }
cd9a1c0e 1429 }
0dd2b474
MS
1430
1431 filp = finish_open(od, dentry, do_open);
1432 if (!IS_ERR(filp))
cd9a1c0e 1433 nfs_file_set_open_context(filp, ctx);
0dd2b474 1434
cd9a1c0e
TM
1435out:
1436 put_nfs_open_context(ctx);
0dd2b474 1437 return filp;
cd9a1c0e
TM
1438}
1439
0dd2b474
MS
1440static struct file *nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1441 struct opendata *od, unsigned open_flags,
1442 umode_t mode, bool *created)
1da177e4 1443{
cd9a1c0e 1444 struct nfs_open_context *ctx;
0dd2b474
MS
1445 struct dentry *res;
1446 struct iattr attr = { .ia_valid = ATTR_OPEN };
f46e0bd3 1447 struct inode *inode;
0dd2b474 1448 struct file *filp;
898f635c 1449 int err;
1da177e4 1450
0dd2b474
MS
1451 /* Expect a negative dentry */
1452 BUG_ON(dentry->d_inode);
1453
1454 dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
1e7cb3dc
CL
1455 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1456
0dd2b474
MS
1457 /* NFS only supports OPEN on regular files */
1458 if ((open_flags & O_DIRECTORY)) {
1459 err = -ENOENT;
1460 if (!d_unhashed(dentry)) {
1461 /*
1462 * Hashed negative dentry with O_DIRECTORY: dentry was
1463 * revalidated and is fine, no need to perform lookup
1464 * again
1465 */
1466 goto out_err;
1467 }
1da177e4 1468 goto no_open;
02a913a7 1469 }
1da177e4 1470
0dd2b474
MS
1471 err = -ENAMETOOLONG;
1472 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1473 goto out_err;
cd9a1c0e 1474
0dd2b474 1475 if (open_flags & O_CREAT) {
536e43d1 1476 attr.ia_valid |= ATTR_MODE;
0dd2b474
MS
1477 attr.ia_mode = mode & ~current_umask();
1478 }
536e43d1
TM
1479 if (open_flags & O_TRUNC) {
1480 attr.ia_valid |= ATTR_SIZE;
1481 attr.ia_size = 0;
cd9a1c0e
TM
1482 }
1483
0dd2b474
MS
1484 ctx = create_nfs_open_context(dentry, open_flags);
1485 err = PTR_ERR(ctx);
1486 if (IS_ERR(ctx))
1487 goto out_err;
1488
f46e0bd3 1489 nfs_block_sillyrename(dentry->d_parent);
2b484297 1490 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
0dd2b474 1491 d_drop(dentry);
f46e0bd3
TM
1492 if (IS_ERR(inode)) {
1493 nfs_unblock_sillyrename(dentry->d_parent);
cd9a1c0e 1494 put_nfs_open_context(ctx);
0dd2b474
MS
1495 err = PTR_ERR(inode);
1496 switch (err) {
1497 case -ENOENT:
1498 d_add(dentry, NULL);
1499 break;
1500 case -EISDIR:
1501 case -ENOTDIR:
1502 goto no_open;
1503 case -ELOOP:
1504 if (!(open_flags & O_NOFOLLOW))
6f926b5b 1505 goto no_open;
0dd2b474 1506 break;
1da177e4 1507 /* case -EINVAL: */
0dd2b474
MS
1508 default:
1509 break;
1da177e4 1510 }
0dd2b474 1511 goto out_err;
cd9a1c0e 1512 }
f46e0bd3 1513 res = d_add_unique(dentry, inode);
0dd2b474 1514 if (res != NULL)
1da177e4 1515 dentry = res;
0dd2b474
MS
1516
1517 nfs_unblock_sillyrename(dentry->d_parent);
f46e0bd3 1518 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
0dd2b474
MS
1519
1520 filp = nfs_finish_open(ctx, dentry, od, open_flags);
1521
1522 dput(res);
1523 return filp;
1524
1525out_err:
1526 return ERR_PTR(err);
1527
1da177e4 1528no_open:
0dd2b474
MS
1529 res = nfs_lookup(dir, dentry, NULL);
1530 err = PTR_ERR(res);
1531 if (IS_ERR(res))
1532 goto out_err;
1533
1534 finish_no_open(od, res);
1535 return NULL;
1da177e4
LT
1536}
1537
0ef97dcf 1538static int nfs4_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1539{
1540 struct dentry *parent = NULL;
657e94b6 1541 struct inode *inode;
1da177e4 1542 struct inode *dir;
1da177e4
LT
1543 int openflags, ret = 0;
1544
657e94b6
NP
1545 if (nd->flags & LOOKUP_RCU)
1546 return -ECHILD;
1547
1548 inode = dentry->d_inode;
1f063d2c 1549 if (!is_atomic_open(nd) || d_mountpoint(dentry))
5584c306 1550 goto no_open;
2b484297 1551
1da177e4
LT
1552 parent = dget_parent(dentry);
1553 dir = parent->d_inode;
2b484297 1554
1da177e4
LT
1555 /* We can't create new files in nfs_open_revalidate(), so we
1556 * optimize away revalidation of negative dentries.
1557 */
216d5d06
TM
1558 if (inode == NULL) {
1559 if (!nfs_neg_need_reval(dir, dentry, nd))
1560 ret = 1;
1da177e4 1561 goto out;
216d5d06
TM
1562 }
1563
1da177e4
LT
1564 /* NFS only supports OPEN on regular files */
1565 if (!S_ISREG(inode->i_mode))
5584c306 1566 goto no_open_dput;
1da177e4
LT
1567 openflags = nd->intent.open.flags;
1568 /* We cannot do exclusive creation on a positive dentry */
1569 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
5584c306 1570 goto no_open_dput;
1da177e4 1571
0ef97dcf
MS
1572 /* Let f_op->open() actually open (and revalidate) the file */
1573 ret = 1;
536e43d1 1574
1da177e4
LT
1575out:
1576 dput(parent);
1da177e4 1577 return ret;
535918f1 1578
5584c306 1579no_open_dput:
1da177e4 1580 dput(parent);
5584c306 1581no_open:
1da177e4
LT
1582 return nfs_lookup_revalidate(dentry, nd);
1583}
c0204fd2 1584
0dd2b474
MS
1585static int nfs4_create(struct inode *dir, struct dentry *dentry,
1586 umode_t mode, struct nameidata *nd)
c0204fd2
TM
1587{
1588 struct nfs_open_context *ctx = NULL;
1589 struct iattr attr;
1590 int error;
8a5e929d 1591 int open_flags = O_CREAT|O_EXCL;
c0204fd2
TM
1592
1593 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1594 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1595
1596 attr.ia_mode = mode;
1597 attr.ia_valid = ATTR_MODE;
1598
dd7dd556 1599 if (nd)
c0204fd2
TM
1600 open_flags = nd->intent.open.flags;
1601
f7c85868
AV
1602 ctx = create_nfs_open_context(dentry, open_flags);
1603 error = PTR_ERR(ctx);
1604 if (IS_ERR(ctx))
1605 goto out_err_drop;
c0204fd2
TM
1606
1607 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1608 if (error != 0)
1609 goto out_put_ctx;
0dd2b474
MS
1610
1611 put_nfs_open_context(ctx);
1612
c0204fd2
TM
1613 return 0;
1614out_put_ctx:
f7c85868 1615 put_nfs_open_context(ctx);
898f635c 1616out_err_drop:
c0204fd2
TM
1617 d_drop(dentry);
1618 return error;
1619}
1620
1da177e4
LT
1621#endif /* CONFIG_NFSV4 */
1622
1da177e4
LT
1623/*
1624 * Code common to create, mkdir, and mknod.
1625 */
1626int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1627 struct nfs_fattr *fattr)
1628{
fab728e1
TM
1629 struct dentry *parent = dget_parent(dentry);
1630 struct inode *dir = parent->d_inode;
1da177e4
LT
1631 struct inode *inode;
1632 int error = -EACCES;
1633
fab728e1
TM
1634 d_drop(dentry);
1635
1da177e4
LT
1636 /* We may have been initialized further down */
1637 if (dentry->d_inode)
fab728e1 1638 goto out;
1da177e4 1639 if (fhandle->size == 0) {
80a16b21 1640 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1da177e4 1641 if (error)
fab728e1 1642 goto out_error;
1da177e4 1643 }
5724ab37 1644 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4
LT
1645 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1646 struct nfs_server *server = NFS_SB(dentry->d_sb);
8fa5c000 1647 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
1da177e4 1648 if (error < 0)
fab728e1 1649 goto out_error;
1da177e4 1650 }
1da177e4 1651 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
03f28e3a
TM
1652 error = PTR_ERR(inode);
1653 if (IS_ERR(inode))
fab728e1
TM
1654 goto out_error;
1655 d_add(dentry, inode);
1656out:
1657 dput(parent);
1da177e4 1658 return 0;
fab728e1
TM
1659out_error:
1660 nfs_mark_for_revalidate(dir);
1661 dput(parent);
1662 return error;
1da177e4
LT
1663}
1664
1665/*
1666 * Following a failed create operation, we drop the dentry rather
1667 * than retain a negative dentry. This avoids a problem in the event
1668 * that the operation succeeded on the server, but an error in the
1669 * reply path made it appear to have failed.
1670 */
4acdaf27
AV
1671static int nfs_create(struct inode *dir, struct dentry *dentry,
1672 umode_t mode, struct nameidata *nd)
1da177e4
LT
1673{
1674 struct iattr attr;
1675 int error;
8a5e929d 1676 int open_flags = O_CREAT|O_EXCL;
1da177e4 1677
1e7cb3dc
CL
1678 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1679 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1da177e4
LT
1680
1681 attr.ia_mode = mode;
1682 attr.ia_valid = ATTR_MODE;
1683
dd7dd556 1684 if (nd)
8a0eebf6
TM
1685 open_flags = nd->intent.open.flags;
1686
1687 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
1da177e4
LT
1688 if (error != 0)
1689 goto out_err;
1da177e4
LT
1690 return 0;
1691out_err:
1da177e4
LT
1692 d_drop(dentry);
1693 return error;
1694}
1695
1696/*
1697 * See comments for nfs_proc_create regarding failed operations.
1698 */
1699static int
1a67aafb 1700nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1da177e4
LT
1701{
1702 struct iattr attr;
1703 int status;
1704
1e7cb3dc
CL
1705 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1706 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1da177e4
LT
1707
1708 if (!new_valid_dev(rdev))
1709 return -EINVAL;
1710
1711 attr.ia_mode = mode;
1712 attr.ia_valid = ATTR_MODE;
1713
1da177e4 1714 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1da177e4
LT
1715 if (status != 0)
1716 goto out_err;
1da177e4
LT
1717 return 0;
1718out_err:
1da177e4
LT
1719 d_drop(dentry);
1720 return status;
1721}
1722
1723/*
1724 * See comments for nfs_proc_create regarding failed operations.
1725 */
18bb1db3 1726static int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4
LT
1727{
1728 struct iattr attr;
1729 int error;
1730
1e7cb3dc
CL
1731 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1732 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1da177e4
LT
1733
1734 attr.ia_valid = ATTR_MODE;
1735 attr.ia_mode = mode | S_IFDIR;
1736
1da177e4 1737 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1da177e4
LT
1738 if (error != 0)
1739 goto out_err;
1da177e4
LT
1740 return 0;
1741out_err:
1742 d_drop(dentry);
1da177e4
LT
1743 return error;
1744}
1745
d45b9d8b
TM
1746static void nfs_dentry_handle_enoent(struct dentry *dentry)
1747{
1748 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1749 d_delete(dentry);
1750}
1751
1da177e4
LT
1752static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1753{
1754 int error;
1755
1e7cb3dc
CL
1756 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1757 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1da177e4 1758
1da177e4
LT
1759 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1760 /* Ensure the VFS deletes this inode */
1761 if (error == 0 && dentry->d_inode != NULL)
ce71ec36 1762 clear_nlink(dentry->d_inode);
d45b9d8b
TM
1763 else if (error == -ENOENT)
1764 nfs_dentry_handle_enoent(dentry);
1da177e4
LT
1765
1766 return error;
1767}
1768
1da177e4
LT
1769/*
1770 * Remove a file after making sure there are no pending writes,
1771 * and after checking that the file has only one user.
1772 *
1773 * We invalidate the attribute cache and free the inode prior to the operation
1774 * to avoid possible races if the server reuses the inode.
1775 */
1776static int nfs_safe_remove(struct dentry *dentry)
1777{
1778 struct inode *dir = dentry->d_parent->d_inode;
1779 struct inode *inode = dentry->d_inode;
1780 int error = -EBUSY;
1781
1782 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1783 dentry->d_parent->d_name.name, dentry->d_name.name);
1784
1785 /* If the dentry was sillyrenamed, we simply call d_delete() */
1786 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1787 error = 0;
1788 goto out;
1789 }
1790
1da177e4 1791 if (inode != NULL) {
cae7a073 1792 nfs_inode_return_delegation(inode);
1da177e4
LT
1793 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1794 /* The VFS may want to delete this inode */
1795 if (error == 0)
1b83d707 1796 nfs_drop_nlink(inode);
5ba7cc48 1797 nfs_mark_for_revalidate(inode);
1da177e4
LT
1798 } else
1799 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
d45b9d8b
TM
1800 if (error == -ENOENT)
1801 nfs_dentry_handle_enoent(dentry);
1da177e4
LT
1802out:
1803 return error;
1804}
1805
1806/* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1807 * belongs to an active ".nfs..." file and we return -EBUSY.
1808 *
1809 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1810 */
1811static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1812{
1813 int error;
1814 int need_rehash = 0;
1815
1816 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1817 dir->i_ino, dentry->d_name.name);
1818
1da177e4 1819 spin_lock(&dentry->d_lock);
b7ab39f6 1820 if (dentry->d_count > 1) {
1da177e4 1821 spin_unlock(&dentry->d_lock);
ccfeb506
TM
1822 /* Start asynchronous writeout of the inode */
1823 write_inode_now(dentry->d_inode, 0);
1da177e4 1824 error = nfs_sillyrename(dir, dentry);
1da177e4
LT
1825 return error;
1826 }
1827 if (!d_unhashed(dentry)) {
1828 __d_drop(dentry);
1829 need_rehash = 1;
1830 }
1831 spin_unlock(&dentry->d_lock);
1da177e4 1832 error = nfs_safe_remove(dentry);
d45b9d8b 1833 if (!error || error == -ENOENT) {
1da177e4
LT
1834 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1835 } else if (need_rehash)
1836 d_rehash(dentry);
1da177e4
LT
1837 return error;
1838}
1839
873101b3
CL
1840/*
1841 * To create a symbolic link, most file systems instantiate a new inode,
1842 * add a page to it containing the path, then write it out to the disk
1843 * using prepare_write/commit_write.
1844 *
1845 * Unfortunately the NFS client can't create the in-core inode first
1846 * because it needs a file handle to create an in-core inode (see
1847 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1848 * symlink request has completed on the server.
1849 *
1850 * So instead we allocate a raw page, copy the symname into it, then do
1851 * the SYMLINK request with the page as the buffer. If it succeeds, we
1852 * now have a new file handle and can instantiate an in-core NFS inode
1853 * and move the raw page into its mapping.
1854 */
1855static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1da177e4 1856{
873101b3
CL
1857 struct pagevec lru_pvec;
1858 struct page *page;
1859 char *kaddr;
1da177e4 1860 struct iattr attr;
873101b3 1861 unsigned int pathlen = strlen(symname);
1da177e4
LT
1862 int error;
1863
1864 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1865 dir->i_ino, dentry->d_name.name, symname);
1866
873101b3
CL
1867 if (pathlen > PAGE_SIZE)
1868 return -ENAMETOOLONG;
1da177e4 1869
873101b3
CL
1870 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1871 attr.ia_valid = ATTR_MODE;
1da177e4 1872
83d93f22 1873 page = alloc_page(GFP_HIGHUSER);
76566991 1874 if (!page)
873101b3 1875 return -ENOMEM;
873101b3 1876
2b86ce2d 1877 kaddr = kmap_atomic(page);
873101b3
CL
1878 memcpy(kaddr, symname, pathlen);
1879 if (pathlen < PAGE_SIZE)
1880 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2b86ce2d 1881 kunmap_atomic(kaddr);
873101b3 1882
94a6d753 1883 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
873101b3
CL
1884 if (error != 0) {
1885 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1886 dir->i_sb->s_id, dir->i_ino,
1887 dentry->d_name.name, symname, error);
1da177e4 1888 d_drop(dentry);
873101b3 1889 __free_page(page);
873101b3
CL
1890 return error;
1891 }
1892
1893 /*
1894 * No big deal if we can't add this page to the page cache here.
1895 * READLINK will get the missing page from the server if needed.
1896 */
1897 pagevec_init(&lru_pvec, 0);
1898 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1899 GFP_KERNEL)) {
39cf8a13 1900 pagevec_add(&lru_pvec, page);
4f98a2fe 1901 pagevec_lru_add_file(&lru_pvec);
873101b3
CL
1902 SetPageUptodate(page);
1903 unlock_page(page);
1904 } else
1905 __free_page(page);
1906
873101b3 1907 return 0;
1da177e4
LT
1908}
1909
1910static int
1911nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1912{
1913 struct inode *inode = old_dentry->d_inode;
1914 int error;
1915
1916 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1917 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1918 dentry->d_parent->d_name.name, dentry->d_name.name);
1919
9a3936aa
TM
1920 nfs_inode_return_delegation(inode);
1921
9697d234 1922 d_drop(dentry);
1da177e4 1923 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
cf809556 1924 if (error == 0) {
7de9c6ee 1925 ihold(inode);
9697d234 1926 d_add(dentry, inode);
cf809556 1927 }
1da177e4
LT
1928 return error;
1929}
1930
1931/*
1932 * RENAME
1933 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1934 * different file handle for the same inode after a rename (e.g. when
1935 * moving to a different directory). A fail-safe method to do so would
1936 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1937 * rename the old file using the sillyrename stuff. This way, the original
1938 * file in old_dir will go away when the last process iput()s the inode.
1939 *
1940 * FIXED.
1941 *
1942 * It actually works quite well. One needs to have the possibility for
1943 * at least one ".nfs..." file in each directory the file ever gets
1944 * moved or linked to which happens automagically with the new
1945 * implementation that only depends on the dcache stuff instead of
1946 * using the inode layer
1947 *
1948 * Unfortunately, things are a little more complicated than indicated
1949 * above. For a cross-directory move, we want to make sure we can get
1950 * rid of the old inode after the operation. This means there must be
1951 * no pending writes (if it's a file), and the use count must be 1.
1952 * If these conditions are met, we can drop the dentries before doing
1953 * the rename.
1954 */
1955static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1956 struct inode *new_dir, struct dentry *new_dentry)
1957{
1958 struct inode *old_inode = old_dentry->d_inode;
1959 struct inode *new_inode = new_dentry->d_inode;
1960 struct dentry *dentry = NULL, *rehash = NULL;
1961 int error = -EBUSY;
1962
1da177e4
LT
1963 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1964 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1965 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
b7ab39f6 1966 new_dentry->d_count);
1da177e4
LT
1967
1968 /*
28f79a1a
MS
1969 * For non-directories, check whether the target is busy and if so,
1970 * make a copy of the dentry and then do a silly-rename. If the
1971 * silly-rename succeeds, the copied dentry is hashed and becomes
1972 * the new target.
1da177e4 1973 */
27226104
MS
1974 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1975 /*
1976 * To prevent any new references to the target during the
1977 * rename, we unhash the dentry in advance.
1978 */
1979 if (!d_unhashed(new_dentry)) {
1980 d_drop(new_dentry);
1981 rehash = new_dentry;
1982 }
1da177e4 1983
b7ab39f6 1984 if (new_dentry->d_count > 2) {
27226104
MS
1985 int err;
1986
1987 /* copy the target dentry's name */
1988 dentry = d_alloc(new_dentry->d_parent,
1989 &new_dentry->d_name);
1990 if (!dentry)
1991 goto out;
1992
1993 /* silly-rename the existing target ... */
1994 err = nfs_sillyrename(new_dir, new_dentry);
24e93025 1995 if (err)
27226104 1996 goto out;
24e93025
MS
1997
1998 new_dentry = dentry;
56335936 1999 rehash = NULL;
24e93025 2000 new_inode = NULL;
27226104 2001 }
b1e4adf4 2002 }
1da177e4 2003
cae7a073 2004 nfs_inode_return_delegation(old_inode);
b1e4adf4 2005 if (new_inode != NULL)
24174119 2006 nfs_inode_return_delegation(new_inode);
1da177e4 2007
1da177e4
LT
2008 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
2009 new_dir, &new_dentry->d_name);
5ba7cc48 2010 nfs_mark_for_revalidate(old_inode);
1da177e4
LT
2011out:
2012 if (rehash)
2013 d_rehash(rehash);
2014 if (!error) {
b1e4adf4
TM
2015 if (new_inode != NULL)
2016 nfs_drop_nlink(new_inode);
349457cc 2017 d_move(old_dentry, new_dentry);
8fb559f8
CL
2018 nfs_set_verifier(new_dentry,
2019 nfs_save_change_attribute(new_dir));
d45b9d8b
TM
2020 } else if (error == -ENOENT)
2021 nfs_dentry_handle_enoent(old_dentry);
1da177e4
LT
2022
2023 /* new dentry created? */
2024 if (dentry)
2025 dput(dentry);
1da177e4
LT
2026 return error;
2027}
2028
cfcea3e8
TM
2029static DEFINE_SPINLOCK(nfs_access_lru_lock);
2030static LIST_HEAD(nfs_access_lru_list);
2031static atomic_long_t nfs_access_nr_entries;
2032
1c3c07e9
TM
2033static void nfs_access_free_entry(struct nfs_access_entry *entry)
2034{
2035 put_rpccred(entry->cred);
2036 kfree(entry);
cfcea3e8
TM
2037 smp_mb__before_atomic_dec();
2038 atomic_long_dec(&nfs_access_nr_entries);
2039 smp_mb__after_atomic_dec();
1c3c07e9
TM
2040}
2041
1a81bb8a
TM
2042static void nfs_access_free_list(struct list_head *head)
2043{
2044 struct nfs_access_entry *cache;
2045
2046 while (!list_empty(head)) {
2047 cache = list_entry(head->next, struct nfs_access_entry, lru);
2048 list_del(&cache->lru);
2049 nfs_access_free_entry(cache);
2050 }
2051}
2052
1495f230
YH
2053int nfs_access_cache_shrinker(struct shrinker *shrink,
2054 struct shrink_control *sc)
979df72e
TM
2055{
2056 LIST_HEAD(head);
aa510da5 2057 struct nfs_inode *nfsi, *next;
979df72e 2058 struct nfs_access_entry *cache;
1495f230
YH
2059 int nr_to_scan = sc->nr_to_scan;
2060 gfp_t gfp_mask = sc->gfp_mask;
979df72e 2061
61d5eb29
TM
2062 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2063 return (nr_to_scan == 0) ? 0 : -1;
9c7e7e23 2064
a50f7951 2065 spin_lock(&nfs_access_lru_lock);
aa510da5 2066 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
979df72e
TM
2067 struct inode *inode;
2068
2069 if (nr_to_scan-- == 0)
2070 break;
9c7e7e23 2071 inode = &nfsi->vfs_inode;
979df72e
TM
2072 spin_lock(&inode->i_lock);
2073 if (list_empty(&nfsi->access_cache_entry_lru))
2074 goto remove_lru_entry;
2075 cache = list_entry(nfsi->access_cache_entry_lru.next,
2076 struct nfs_access_entry, lru);
2077 list_move(&cache->lru, &head);
2078 rb_erase(&cache->rb_node, &nfsi->access_cache);
2079 if (!list_empty(&nfsi->access_cache_entry_lru))
2080 list_move_tail(&nfsi->access_cache_inode_lru,
2081 &nfs_access_lru_list);
2082 else {
2083remove_lru_entry:
2084 list_del_init(&nfsi->access_cache_inode_lru);
9c7e7e23 2085 smp_mb__before_clear_bit();
979df72e 2086 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
9c7e7e23 2087 smp_mb__after_clear_bit();
979df72e 2088 }
59844a9b 2089 spin_unlock(&inode->i_lock);
979df72e
TM
2090 }
2091 spin_unlock(&nfs_access_lru_lock);
1a81bb8a 2092 nfs_access_free_list(&head);
979df72e
TM
2093 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
2094}
2095
1a81bb8a 2096static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
1da177e4 2097{
1c3c07e9 2098 struct rb_root *root_node = &nfsi->access_cache;
1a81bb8a 2099 struct rb_node *n;
1c3c07e9
TM
2100 struct nfs_access_entry *entry;
2101
2102 /* Unhook entries from the cache */
2103 while ((n = rb_first(root_node)) != NULL) {
2104 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2105 rb_erase(n, root_node);
1a81bb8a 2106 list_move(&entry->lru, head);
1c3c07e9
TM
2107 }
2108 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
1da177e4
LT
2109}
2110
1c3c07e9 2111void nfs_access_zap_cache(struct inode *inode)
1da177e4 2112{
1a81bb8a
TM
2113 LIST_HEAD(head);
2114
2115 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2116 return;
cfcea3e8 2117 /* Remove from global LRU init */
1a81bb8a
TM
2118 spin_lock(&nfs_access_lru_lock);
2119 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
cfcea3e8 2120 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
cfcea3e8 2121
1c3c07e9 2122 spin_lock(&inode->i_lock);
1a81bb8a
TM
2123 __nfs_access_zap_cache(NFS_I(inode), &head);
2124 spin_unlock(&inode->i_lock);
2125 spin_unlock(&nfs_access_lru_lock);
2126 nfs_access_free_list(&head);
1c3c07e9 2127}
1da177e4 2128
1c3c07e9
TM
2129static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2130{
2131 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2132 struct nfs_access_entry *entry;
2133
2134 while (n != NULL) {
2135 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2136
2137 if (cred < entry->cred)
2138 n = n->rb_left;
2139 else if (cred > entry->cred)
2140 n = n->rb_right;
2141 else
2142 return entry;
1da177e4 2143 }
1c3c07e9
TM
2144 return NULL;
2145}
2146
af22f94a 2147static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
1c3c07e9
TM
2148{
2149 struct nfs_inode *nfsi = NFS_I(inode);
2150 struct nfs_access_entry *cache;
2151 int err = -ENOENT;
2152
dc59250c 2153 spin_lock(&inode->i_lock);
1c3c07e9
TM
2154 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2155 goto out_zap;
2156 cache = nfs_access_search_rbtree(inode, cred);
2157 if (cache == NULL)
2158 goto out;
b4d2314b 2159 if (!nfs_have_delegated_attributes(inode) &&
64672d55 2160 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
1c3c07e9
TM
2161 goto out_stale;
2162 res->jiffies = cache->jiffies;
2163 res->cred = cache->cred;
2164 res->mask = cache->mask;
cfcea3e8 2165 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
1c3c07e9
TM
2166 err = 0;
2167out:
2168 spin_unlock(&inode->i_lock);
2169 return err;
2170out_stale:
2171 rb_erase(&cache->rb_node, &nfsi->access_cache);
cfcea3e8 2172 list_del(&cache->lru);
1c3c07e9
TM
2173 spin_unlock(&inode->i_lock);
2174 nfs_access_free_entry(cache);
2175 return -ENOENT;
2176out_zap:
1a81bb8a
TM
2177 spin_unlock(&inode->i_lock);
2178 nfs_access_zap_cache(inode);
1c3c07e9
TM
2179 return -ENOENT;
2180}
2181
2182static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2183{
cfcea3e8
TM
2184 struct nfs_inode *nfsi = NFS_I(inode);
2185 struct rb_root *root_node = &nfsi->access_cache;
1c3c07e9
TM
2186 struct rb_node **p = &root_node->rb_node;
2187 struct rb_node *parent = NULL;
2188 struct nfs_access_entry *entry;
2189
2190 spin_lock(&inode->i_lock);
2191 while (*p != NULL) {
2192 parent = *p;
2193 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2194
2195 if (set->cred < entry->cred)
2196 p = &parent->rb_left;
2197 else if (set->cred > entry->cred)
2198 p = &parent->rb_right;
2199 else
2200 goto found;
2201 }
2202 rb_link_node(&set->rb_node, parent, p);
2203 rb_insert_color(&set->rb_node, root_node);
cfcea3e8 2204 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
dc59250c 2205 spin_unlock(&inode->i_lock);
1c3c07e9
TM
2206 return;
2207found:
2208 rb_replace_node(parent, &set->rb_node, root_node);
cfcea3e8
TM
2209 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2210 list_del(&entry->lru);
1c3c07e9
TM
2211 spin_unlock(&inode->i_lock);
2212 nfs_access_free_entry(entry);
2213}
2214
af22f94a 2215static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1c3c07e9
TM
2216{
2217 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2218 if (cache == NULL)
2219 return;
2220 RB_CLEAR_NODE(&cache->rb_node);
1da177e4 2221 cache->jiffies = set->jiffies;
1c3c07e9 2222 cache->cred = get_rpccred(set->cred);
1da177e4 2223 cache->mask = set->mask;
1c3c07e9
TM
2224
2225 nfs_access_add_rbtree(inode, cache);
cfcea3e8
TM
2226
2227 /* Update accounting */
2228 smp_mb__before_atomic_inc();
2229 atomic_long_inc(&nfs_access_nr_entries);
2230 smp_mb__after_atomic_inc();
2231
2232 /* Add inode to global LRU list */
1a81bb8a 2233 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
cfcea3e8 2234 spin_lock(&nfs_access_lru_lock);
1a81bb8a
TM
2235 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2236 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2237 &nfs_access_lru_list);
cfcea3e8
TM
2238 spin_unlock(&nfs_access_lru_lock);
2239 }
1da177e4
LT
2240}
2241
2242static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2243{
2244 struct nfs_access_entry cache;
2245 int status;
2246
2247 status = nfs_access_get_cached(inode, cred, &cache);
2248 if (status == 0)
2249 goto out;
2250
2251 /* Be clever: ask server to check for all possible rights */
2252 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2253 cache.cred = cred;
2254 cache.jiffies = jiffies;
2255 status = NFS_PROTO(inode)->access(inode, &cache);
a71ee337
SJ
2256 if (status != 0) {
2257 if (status == -ESTALE) {
2258 nfs_zap_caches(inode);
2259 if (!S_ISDIR(inode->i_mode))
2260 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2261 }
1da177e4 2262 return status;
a71ee337 2263 }
1da177e4
LT
2264 nfs_access_add_cache(inode, &cache);
2265out:
e6305c43 2266 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4
LT
2267 return 0;
2268 return -EACCES;
2269}
2270
af22f94a
TM
2271static int nfs_open_permission_mask(int openflags)
2272{
2273 int mask = 0;
2274
8a5e929d 2275 if ((openflags & O_ACCMODE) != O_WRONLY)
af22f94a 2276 mask |= MAY_READ;
8a5e929d 2277 if ((openflags & O_ACCMODE) != O_RDONLY)
af22f94a 2278 mask |= MAY_WRITE;
8a5e929d 2279 if (openflags & __FMODE_EXEC)
af22f94a
TM
2280 mask |= MAY_EXEC;
2281 return mask;
2282}
2283
2284int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2285{
2286 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2287}
2288
10556cb2 2289int nfs_permission(struct inode *inode, int mask)
1da177e4
LT
2290{
2291 struct rpc_cred *cred;
2292 int res = 0;
2293
10556cb2 2294 if (mask & MAY_NOT_BLOCK)
b74c79e9
NP
2295 return -ECHILD;
2296
91d5b470
CL
2297 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2298
e6305c43 2299 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4
LT
2300 goto out;
2301 /* Is this sys_access() ? */
9cfcac81 2302 if (mask & (MAY_ACCESS | MAY_CHDIR))
1da177e4
LT
2303 goto force_lookup;
2304
2305 switch (inode->i_mode & S_IFMT) {
2306 case S_IFLNK:
2307 goto out;
2308 case S_IFREG:
2309 /* NFSv4 has atomic_open... */
2310 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
7ee2cb7f
FF
2311 && (mask & MAY_OPEN)
2312 && !(mask & MAY_EXEC))
1da177e4
LT
2313 goto out;
2314 break;
2315 case S_IFDIR:
2316 /*
2317 * Optimize away all write operations, since the server
2318 * will check permissions when we perform the op.
2319 */
2320 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2321 goto out;
2322 }
2323
2324force_lookup:
1da177e4
LT
2325 if (!NFS_PROTO(inode)->access)
2326 goto out_notsup;
2327
98a8e323 2328 cred = rpc_lookup_cred();
1da177e4
LT
2329 if (!IS_ERR(cred)) {
2330 res = nfs_do_access(inode, cred, mask);
2331 put_rpccred(cred);
2332 } else
2333 res = PTR_ERR(cred);
1da177e4 2334out:
f696a365
MS
2335 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2336 res = -EACCES;
2337
1e7cb3dc
CL
2338 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2339 inode->i_sb->s_id, inode->i_ino, mask, res);
1da177e4
LT
2340 return res;
2341out_notsup:
2342 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2343 if (res == 0)
2830ba7f 2344 res = generic_permission(inode, mask);
1e7cb3dc 2345 goto out;
1da177e4
LT
2346}
2347
2348/*
2349 * Local variables:
2350 * version-control: t
2351 * kept-new-versions: 5
2352 * End:
2353 */