NFS: Fix a couple of regressions in readdir.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / dir.c
blobc6ce8af266ed891df0e41502285dec34b9e87db3
1 /*
2 * linux/fs/nfs/dir.c
4 * Copyright (C) 1992 Rick Sladkey
6 * nfs directory handling functions
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
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>
32 #include <linux/pagevec.h>
33 #include <linux/namei.h>
34 #include <linux/mount.h>
35 #include <linux/sched.h>
36 #include <linux/vmalloc.h>
38 #include "delegation.h"
39 #include "iostat.h"
40 #include "internal.h"
41 #include "fscache.h"
43 /* #define NFS_DEBUG_VERBOSE 1 */
45 static int nfs_opendir(struct inode *, struct file *);
46 static int nfs_readdir(struct file *, void *, filldir_t);
47 static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
48 static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
49 static int nfs_mkdir(struct inode *, struct dentry *, int);
50 static int nfs_rmdir(struct inode *, struct dentry *);
51 static int nfs_unlink(struct inode *, struct dentry *);
52 static int nfs_symlink(struct inode *, struct dentry *, const char *);
53 static int nfs_link(struct dentry *, struct inode *, struct dentry *);
54 static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
55 static int nfs_rename(struct inode *, struct dentry *,
56 struct inode *, struct dentry *);
57 static int nfs_fsync_dir(struct file *, int);
58 static loff_t nfs_llseek_dir(struct file *, loff_t, int);
59 static int nfs_readdir_clear_array(struct page*, gfp_t);
61 const struct file_operations nfs_dir_operations = {
62 .llseek = nfs_llseek_dir,
63 .read = generic_read_dir,
64 .readdir = nfs_readdir,
65 .open = nfs_opendir,
66 .release = nfs_release,
67 .fsync = nfs_fsync_dir,
70 const struct inode_operations nfs_dir_inode_operations = {
71 .create = nfs_create,
72 .lookup = nfs_lookup,
73 .link = nfs_link,
74 .unlink = nfs_unlink,
75 .symlink = nfs_symlink,
76 .mkdir = nfs_mkdir,
77 .rmdir = nfs_rmdir,
78 .mknod = nfs_mknod,
79 .rename = nfs_rename,
80 .permission = nfs_permission,
81 .getattr = nfs_getattr,
82 .setattr = nfs_setattr,
85 const struct address_space_operations nfs_dir_addr_space_ops = {
86 .releasepage = nfs_readdir_clear_array,
89 #ifdef CONFIG_NFS_V3
90 const struct inode_operations nfs3_dir_inode_operations = {
91 .create = nfs_create,
92 .lookup = nfs_lookup,
93 .link = nfs_link,
94 .unlink = nfs_unlink,
95 .symlink = nfs_symlink,
96 .mkdir = nfs_mkdir,
97 .rmdir = nfs_rmdir,
98 .mknod = nfs_mknod,
99 .rename = nfs_rename,
100 .permission = nfs_permission,
101 .getattr = nfs_getattr,
102 .setattr = nfs_setattr,
103 .listxattr = nfs3_listxattr,
104 .getxattr = nfs3_getxattr,
105 .setxattr = nfs3_setxattr,
106 .removexattr = nfs3_removexattr,
108 #endif /* CONFIG_NFS_V3 */
110 #ifdef CONFIG_NFS_V4
112 static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
113 static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
114 const struct inode_operations nfs4_dir_inode_operations = {
115 .create = nfs_open_create,
116 .lookup = nfs_atomic_lookup,
117 .link = nfs_link,
118 .unlink = nfs_unlink,
119 .symlink = nfs_symlink,
120 .mkdir = nfs_mkdir,
121 .rmdir = nfs_rmdir,
122 .mknod = nfs_mknod,
123 .rename = nfs_rename,
124 .permission = nfs_permission,
125 .getattr = nfs_getattr,
126 .setattr = nfs_setattr,
127 .getxattr = nfs4_getxattr,
128 .setxattr = nfs4_setxattr,
129 .listxattr = nfs4_listxattr,
132 #endif /* CONFIG_NFS_V4 */
135 * Open file
137 static int
138 nfs_opendir(struct inode *inode, struct file *filp)
140 int res;
142 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
143 filp->f_path.dentry->d_parent->d_name.name,
144 filp->f_path.dentry->d_name.name);
146 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
148 /* Call generic open code in order to cache credentials */
149 res = nfs_open(inode, filp);
150 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
151 /* This is a mountpoint, so d_revalidate will never
152 * have been called, so we need to refresh the
153 * inode (for close-open consistency) ourselves.
155 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
157 return res;
160 struct nfs_cache_array_entry {
161 u64 cookie;
162 u64 ino;
163 struct qstr string;
166 struct nfs_cache_array {
167 unsigned int size;
168 int eof_index;
169 u64 last_cookie;
170 struct nfs_cache_array_entry array[0];
173 #define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
175 typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, struct nfs_server *, int);
176 typedef struct {
177 struct file *file;
178 struct page *page;
179 unsigned long page_index;
180 u64 *dir_cookie;
181 loff_t current_index;
182 decode_dirent_t decode;
184 unsigned long timestamp;
185 unsigned long gencount;
186 unsigned int cache_entry_index;
187 unsigned int plus:1;
188 unsigned int eof:1;
189 } nfs_readdir_descriptor_t;
192 * The caller is responsible for calling nfs_readdir_release_array(page)
194 static
195 struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
197 void *ptr;
198 if (page == NULL)
199 return ERR_PTR(-EIO);
200 ptr = kmap(page);
201 if (ptr == NULL)
202 return ERR_PTR(-ENOMEM);
203 return ptr;
206 static
207 void nfs_readdir_release_array(struct page *page)
209 kunmap(page);
213 * we are freeing strings created by nfs_add_to_readdir_array()
215 static
216 int nfs_readdir_clear_array(struct page *page, gfp_t mask)
218 struct nfs_cache_array *array = nfs_readdir_get_array(page);
219 int i;
221 if (IS_ERR(array))
222 return PTR_ERR(array);
223 for (i = 0; i < array->size; i++)
224 kfree(array->array[i].string.name);
225 nfs_readdir_release_array(page);
226 return 0;
230 * the caller is responsible for freeing qstr.name
231 * when called by nfs_readdir_add_to_array, the strings will be freed in
232 * nfs_clear_readdir_array()
234 static
235 int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
237 string->len = len;
238 string->name = kmemdup(name, len, GFP_KERNEL);
239 if (string->name == NULL)
240 return -ENOMEM;
241 string->hash = full_name_hash(name, len);
242 return 0;
245 static
246 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
248 struct nfs_cache_array *array = nfs_readdir_get_array(page);
249 struct nfs_cache_array_entry *cache_entry;
250 int ret;
252 if (IS_ERR(array))
253 return PTR_ERR(array);
254 ret = -ENOSPC;
255 if (array->size >= MAX_READDIR_ARRAY)
256 goto out;
258 cache_entry = &array->array[array->size];
259 cache_entry->cookie = entry->prev_cookie;
260 cache_entry->ino = entry->ino;
261 ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
262 if (ret)
263 goto out;
264 array->last_cookie = entry->cookie;
265 array->size++;
266 if (entry->eof == 1)
267 array->eof_index = array->size;
268 out:
269 nfs_readdir_release_array(page);
270 return ret;
273 static
274 int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
276 loff_t diff = desc->file->f_pos - desc->current_index;
277 unsigned int index;
279 if (diff < 0)
280 goto out_eof;
281 if (diff >= array->size) {
282 if (array->eof_index >= 0)
283 goto out_eof;
284 desc->current_index += array->size;
285 return -EAGAIN;
288 index = (unsigned int)diff;
289 *desc->dir_cookie = array->array[index].cookie;
290 desc->cache_entry_index = index;
291 return 0;
292 out_eof:
293 desc->eof = 1;
294 return -EBADCOOKIE;
297 static
298 int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
300 int i;
301 int status = -EAGAIN;
303 for (i = 0; i < array->size; i++) {
304 if (array->array[i].cookie == *desc->dir_cookie) {
305 desc->cache_entry_index = i;
306 status = 0;
307 goto out;
310 if (i == array->eof_index) {
311 desc->eof = 1;
312 status = -EBADCOOKIE;
314 out:
315 return status;
318 static
319 int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
321 struct nfs_cache_array *array;
322 int status = -EBADCOOKIE;
324 if (desc->dir_cookie == NULL)
325 goto out;
327 array = nfs_readdir_get_array(desc->page);
328 if (IS_ERR(array)) {
329 status = PTR_ERR(array);
330 goto out;
333 if (*desc->dir_cookie == 0)
334 status = nfs_readdir_search_for_pos(array, desc);
335 else
336 status = nfs_readdir_search_for_cookie(array, desc);
338 nfs_readdir_release_array(desc->page);
339 out:
340 return status;
343 /* Fill a page with xdr information before transferring to the cache page */
344 static
345 int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
346 struct nfs_entry *entry, struct file *file, struct inode *inode)
348 struct rpc_cred *cred = nfs_file_cred(file);
349 unsigned long timestamp, gencount;
350 int error;
352 again:
353 timestamp = jiffies;
354 gencount = nfs_inc_attr_generation_counter();
355 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
356 NFS_SERVER(inode)->dtsize, desc->plus);
357 if (error < 0) {
358 /* We requested READDIRPLUS, but the server doesn't grok it */
359 if (error == -ENOTSUPP && desc->plus) {
360 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
361 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
362 desc->plus = 0;
363 goto again;
365 goto error;
367 desc->timestamp = timestamp;
368 desc->gencount = gencount;
369 error:
370 return error;
373 /* Fill in an entry based on the xdr code stored in desc->page */
374 static
375 int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct xdr_stream *stream)
377 __be32 *p = desc->decode(stream, entry, NFS_SERVER(desc->file->f_path.dentry->d_inode), desc->plus);
378 if (IS_ERR(p))
379 return PTR_ERR(p);
381 entry->fattr->time_start = desc->timestamp;
382 entry->fattr->gencount = desc->gencount;
383 return 0;
386 static
387 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
389 struct nfs_inode *node;
390 if (dentry->d_inode == NULL)
391 goto different;
392 node = NFS_I(dentry->d_inode);
393 if (node->fh.size != entry->fh->size)
394 goto different;
395 if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
396 goto different;
397 return 1;
398 different:
399 return 0;
402 static
403 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
405 struct qstr filename = {
406 .len = entry->len,
407 .name = entry->name,
409 struct dentry *dentry;
410 struct dentry *alias;
411 struct inode *dir = parent->d_inode;
412 struct inode *inode;
414 if (filename.name[0] == '.') {
415 if (filename.len == 1)
416 return;
417 if (filename.len == 2 && filename.name[1] == '.')
418 return;
420 filename.hash = full_name_hash(filename.name, filename.len);
422 dentry = d_lookup(parent, &filename);
423 if (dentry != NULL) {
424 if (nfs_same_file(dentry, entry)) {
425 nfs_refresh_inode(dentry->d_inode, entry->fattr);
426 goto out;
427 } else {
428 d_drop(dentry);
429 dput(dentry);
433 dentry = d_alloc(parent, &filename);
434 if (dentry == NULL)
435 return;
437 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
438 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
439 if (IS_ERR(inode))
440 goto out;
442 alias = d_materialise_unique(dentry, inode);
443 if (IS_ERR(alias))
444 goto out;
445 else if (alias) {
446 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
447 dput(alias);
448 } else
449 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
451 out:
452 dput(dentry);
455 /* Perform conversion from xdr to cache array */
456 static
457 int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
458 void *xdr_page, struct page *page, unsigned int buflen)
460 struct xdr_stream stream;
461 struct xdr_buf buf;
462 __be32 *ptr = xdr_page;
463 int status;
464 struct nfs_cache_array *array;
466 buf.head->iov_base = xdr_page;
467 buf.head->iov_len = buflen;
468 buf.tail->iov_len = 0;
469 buf.page_base = 0;
470 buf.page_len = 0;
471 buf.buflen = buf.head->iov_len;
472 buf.len = buf.head->iov_len;
474 xdr_init_decode(&stream, &buf, ptr);
477 do {
478 status = xdr_decode(desc, entry, &stream);
479 if (status != 0) {
480 if (status == -EAGAIN)
481 status = 0;
482 break;
485 if (desc->plus == 1)
486 nfs_prime_dcache(desc->file->f_path.dentry, entry);
488 status = nfs_readdir_add_to_array(entry, page);
489 if (status != 0)
490 break;
491 } while (!entry->eof);
493 if (status == -EBADCOOKIE && entry->eof) {
494 array = nfs_readdir_get_array(page);
495 if (!IS_ERR(array)) {
496 array->eof_index = array->size;
497 status = 0;
498 nfs_readdir_release_array(page);
501 return status;
504 static
505 void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
507 unsigned int i;
508 for (i = 0; i < npages; i++)
509 put_page(pages[i]);
512 static
513 void nfs_readdir_free_large_page(void *ptr, struct page **pages,
514 unsigned int npages)
516 vm_unmap_ram(ptr, npages);
517 nfs_readdir_free_pagearray(pages, npages);
521 * nfs_readdir_large_page will allocate pages that must be freed with a call
522 * to nfs_readdir_free_large_page
524 static
525 void *nfs_readdir_large_page(struct page **pages, unsigned int npages)
527 void *ptr;
528 unsigned int i;
530 for (i = 0; i < npages; i++) {
531 struct page *page = alloc_page(GFP_KERNEL);
532 if (page == NULL)
533 goto out_freepages;
534 pages[i] = page;
537 ptr = vm_map_ram(pages, npages, 0, PAGE_KERNEL);
538 if (!IS_ERR_OR_NULL(ptr))
539 return ptr;
540 out_freepages:
541 nfs_readdir_free_pagearray(pages, i);
542 return NULL;
545 static
546 int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
548 struct page *pages[NFS_MAX_READDIR_PAGES];
549 void *pages_ptr = NULL;
550 struct nfs_entry entry;
551 struct file *file = desc->file;
552 struct nfs_cache_array *array;
553 int status = -ENOMEM;
554 unsigned int array_size = ARRAY_SIZE(pages);
556 entry.prev_cookie = 0;
557 entry.cookie = *desc->dir_cookie;
558 entry.eof = 0;
559 entry.fh = nfs_alloc_fhandle();
560 entry.fattr = nfs_alloc_fattr();
561 if (entry.fh == NULL || entry.fattr == NULL)
562 goto out;
564 array = nfs_readdir_get_array(page);
565 if (IS_ERR(array)) {
566 status = PTR_ERR(array);
567 goto out;
569 memset(array, 0, sizeof(struct nfs_cache_array));
570 array->eof_index = -1;
572 pages_ptr = nfs_readdir_large_page(pages, array_size);
573 if (!pages_ptr)
574 goto out_release_array;
575 do {
576 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
578 if (status < 0)
579 break;
580 status = nfs_readdir_page_filler(desc, &entry, pages_ptr, page, array_size * PAGE_SIZE);
581 if (status < 0) {
582 if (status == -ENOSPC)
583 status = 0;
584 break;
586 } while (array->eof_index < 0);
588 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
589 out_release_array:
590 nfs_readdir_release_array(page);
591 out:
592 nfs_free_fattr(entry.fattr);
593 nfs_free_fhandle(entry.fh);
594 return status;
598 * Now we cache directories properly, by converting xdr information
599 * to an array that can be used for lookups later. This results in
600 * fewer cache pages, since we can store more information on each page.
601 * We only need to convert from xdr once so future lookups are much simpler
603 static
604 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
606 struct inode *inode = desc->file->f_path.dentry->d_inode;
607 int ret;
609 ret = nfs_readdir_xdr_to_array(desc, page, inode);
610 if (ret < 0)
611 goto error;
612 SetPageUptodate(page);
614 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
615 /* Should never happen */
616 nfs_zap_mapping(inode, inode->i_mapping);
618 unlock_page(page);
619 return 0;
620 error:
621 unlock_page(page);
622 return ret;
625 static
626 void cache_page_release(nfs_readdir_descriptor_t *desc)
628 page_cache_release(desc->page);
629 desc->page = NULL;
632 static
633 struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
635 return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
636 desc->page_index, (filler_t *)nfs_readdir_filler, desc);
640 * Returns 0 if desc->dir_cookie was found on page desc->page_index
642 static
643 int find_cache_page(nfs_readdir_descriptor_t *desc)
645 int res;
647 desc->page = get_cache_page(desc);
648 if (IS_ERR(desc->page))
649 return PTR_ERR(desc->page);
651 res = nfs_readdir_search_array(desc);
652 if (res == 0)
653 return 0;
654 cache_page_release(desc);
655 return res;
658 /* Search for desc->dir_cookie from the beginning of the page cache */
659 static inline
660 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
662 int res;
664 if (desc->page_index == 0)
665 desc->current_index = 0;
666 while (1) {
667 res = find_cache_page(desc);
668 if (res != -EAGAIN)
669 break;
670 desc->page_index++;
672 return res;
675 static inline unsigned int dt_type(struct inode *inode)
677 return (inode->i_mode >> 12) & 15;
681 * Once we've found the start of the dirent within a page: fill 'er up...
683 static
684 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
685 filldir_t filldir)
687 struct file *file = desc->file;
688 int i = 0;
689 int res = 0;
690 struct nfs_cache_array *array = NULL;
691 unsigned int d_type = DT_UNKNOWN;
692 struct dentry *dentry = NULL;
694 array = nfs_readdir_get_array(desc->page);
695 if (IS_ERR(array))
696 return PTR_ERR(array);
698 for (i = desc->cache_entry_index; i < array->size; i++) {
699 d_type = DT_UNKNOWN;
701 res = filldir(dirent, array->array[i].string.name,
702 array->array[i].string.len, file->f_pos,
703 nfs_compat_user_ino64(array->array[i].ino), d_type);
704 if (res < 0)
705 break;
706 file->f_pos++;
707 desc->cache_entry_index = i;
708 if (i < (array->size-1))
709 *desc->dir_cookie = array->array[i+1].cookie;
710 else
711 *desc->dir_cookie = array->last_cookie;
713 if (i == array->eof_index)
714 desc->eof = 1;
716 nfs_readdir_release_array(desc->page);
717 cache_page_release(desc);
718 if (dentry != NULL)
719 dput(dentry);
720 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
721 (unsigned long long)*desc->dir_cookie, res);
722 return res;
726 * If we cannot find a cookie in our cache, we suspect that this is
727 * because it points to a deleted file, so we ask the server to return
728 * whatever it thinks is the next entry. We then feed this to filldir.
729 * If all goes well, we should then be able to find our way round the
730 * cache on the next call to readdir_search_pagecache();
732 * NOTE: we cannot add the anonymous page to the pagecache because
733 * the data it contains might not be page aligned. Besides,
734 * we should already have a complete representation of the
735 * directory in the page cache by the time we get here.
737 static inline
738 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
739 filldir_t filldir)
741 struct page *page = NULL;
742 int status;
743 struct inode *inode = desc->file->f_path.dentry->d_inode;
745 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
746 (unsigned long long)*desc->dir_cookie);
748 page = alloc_page(GFP_HIGHUSER);
749 if (!page) {
750 status = -ENOMEM;
751 goto out;
754 if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
755 status = -EIO;
756 goto out_release;
759 desc->page_index = 0;
760 desc->page = page;
761 status = nfs_do_filldir(desc, dirent, filldir);
763 out:
764 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
765 __func__, status);
766 return status;
767 out_release:
768 cache_page_release(desc);
769 goto out;
772 /* The file offset position represents the dirent entry number. A
773 last cookie cache takes care of the common case of reading the
774 whole directory.
776 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
778 struct dentry *dentry = filp->f_path.dentry;
779 struct inode *inode = dentry->d_inode;
780 nfs_readdir_descriptor_t my_desc,
781 *desc = &my_desc;
782 int res = -ENOMEM;
784 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
785 dentry->d_parent->d_name.name, dentry->d_name.name,
786 (long long)filp->f_pos);
787 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
790 * filp->f_pos points to the dirent entry number.
791 * *desc->dir_cookie has the cookie for the next entry. We have
792 * to either find the entry with the appropriate number or
793 * revalidate the cookie.
795 memset(desc, 0, sizeof(*desc));
797 desc->file = filp;
798 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
799 desc->decode = NFS_PROTO(inode)->decode_dirent;
800 desc->plus = NFS_USE_READDIRPLUS(inode);
802 nfs_block_sillyrename(dentry);
803 res = nfs_revalidate_mapping(inode, filp->f_mapping);
804 if (res < 0)
805 goto out;
807 while (desc->eof != 1) {
808 res = readdir_search_pagecache(desc);
810 if (res == -EBADCOOKIE) {
811 /* This means either end of directory */
812 if (*desc->dir_cookie && desc->eof == 0) {
813 /* Or that the server has 'lost' a cookie */
814 res = uncached_readdir(desc, dirent, filldir);
815 if (res >= 0)
816 continue;
818 res = 0;
819 break;
821 if (res == -ETOOSMALL && desc->plus) {
822 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
823 nfs_zap_caches(inode);
824 desc->page_index = 0;
825 desc->plus = 0;
826 desc->eof = 0;
827 continue;
829 if (res < 0)
830 break;
832 res = nfs_do_filldir(desc, dirent, filldir);
833 if (res < 0) {
834 res = 0;
835 break;
838 out:
839 nfs_unblock_sillyrename(dentry);
840 if (res > 0)
841 res = 0;
842 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
843 dentry->d_parent->d_name.name, dentry->d_name.name,
844 res);
845 return res;
848 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
850 struct dentry *dentry = filp->f_path.dentry;
851 struct inode *inode = dentry->d_inode;
853 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
854 dentry->d_parent->d_name.name,
855 dentry->d_name.name,
856 offset, origin);
858 mutex_lock(&inode->i_mutex);
859 switch (origin) {
860 case 1:
861 offset += filp->f_pos;
862 case 0:
863 if (offset >= 0)
864 break;
865 default:
866 offset = -EINVAL;
867 goto out;
869 if (offset != filp->f_pos) {
870 filp->f_pos = offset;
871 nfs_file_open_context(filp)->dir_cookie = 0;
873 out:
874 mutex_unlock(&inode->i_mutex);
875 return offset;
879 * All directory operations under NFS are synchronous, so fsync()
880 * is a dummy operation.
882 static int nfs_fsync_dir(struct file *filp, int datasync)
884 struct dentry *dentry = filp->f_path.dentry;
886 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
887 dentry->d_parent->d_name.name, dentry->d_name.name,
888 datasync);
890 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
891 return 0;
895 * nfs_force_lookup_revalidate - Mark the directory as having changed
896 * @dir - pointer to directory inode
898 * This forces the revalidation code in nfs_lookup_revalidate() to do a
899 * full lookup on all child dentries of 'dir' whenever a change occurs
900 * on the server that might have invalidated our dcache.
902 * The caller should be holding dir->i_lock
904 void nfs_force_lookup_revalidate(struct inode *dir)
906 NFS_I(dir)->cache_change_attribute++;
910 * A check for whether or not the parent directory has changed.
911 * In the case it has, we assume that the dentries are untrustworthy
912 * and may need to be looked up again.
914 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
916 if (IS_ROOT(dentry))
917 return 1;
918 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
919 return 0;
920 if (!nfs_verify_change_attribute(dir, dentry->d_time))
921 return 0;
922 /* Revalidate nfsi->cache_change_attribute before we declare a match */
923 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
924 return 0;
925 if (!nfs_verify_change_attribute(dir, dentry->d_time))
926 return 0;
927 return 1;
931 * Return the intent data that applies to this particular path component
933 * Note that the current set of intents only apply to the very last
934 * component of the path.
935 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
937 static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
939 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
940 return 0;
941 return nd->flags & mask;
945 * Use intent information to check whether or not we're going to do
946 * an O_EXCL create using this path component.
948 static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
950 if (NFS_PROTO(dir)->version == 2)
951 return 0;
952 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
956 * Inode and filehandle revalidation for lookups.
958 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
959 * or if the intent information indicates that we're about to open this
960 * particular file and the "nocto" mount flag is not set.
963 static inline
964 int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
966 struct nfs_server *server = NFS_SERVER(inode);
968 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
969 return 0;
970 if (nd != NULL) {
971 /* VFS wants an on-the-wire revalidation */
972 if (nd->flags & LOOKUP_REVAL)
973 goto out_force;
974 /* This is an open(2) */
975 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
976 !(server->flags & NFS_MOUNT_NOCTO) &&
977 (S_ISREG(inode->i_mode) ||
978 S_ISDIR(inode->i_mode)))
979 goto out_force;
980 return 0;
982 return nfs_revalidate_inode(server, inode);
983 out_force:
984 return __nfs_revalidate_inode(server, inode);
988 * We judge how long we want to trust negative
989 * dentries by looking at the parent inode mtime.
991 * If parent mtime has changed, we revalidate, else we wait for a
992 * period corresponding to the parent's attribute cache timeout value.
994 static inline
995 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
996 struct nameidata *nd)
998 /* Don't revalidate a negative dentry if we're creating a new file */
999 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
1000 return 0;
1001 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1002 return 1;
1003 return !nfs_check_verifier(dir, dentry);
1007 * This is called every time the dcache has a lookup hit,
1008 * and we should check whether we can really trust that
1009 * lookup.
1011 * NOTE! The hit can be a negative hit too, don't assume
1012 * we have an inode!
1014 * If the parent directory is seen to have changed, we throw out the
1015 * cached dentry and do a new lookup.
1017 static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
1019 struct inode *dir;
1020 struct inode *inode;
1021 struct dentry *parent;
1022 struct nfs_fh *fhandle = NULL;
1023 struct nfs_fattr *fattr = NULL;
1024 int error;
1026 parent = dget_parent(dentry);
1027 dir = parent->d_inode;
1028 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1029 inode = dentry->d_inode;
1031 if (!inode) {
1032 if (nfs_neg_need_reval(dir, dentry, nd))
1033 goto out_bad;
1034 goto out_valid;
1037 if (is_bad_inode(inode)) {
1038 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
1039 __func__, dentry->d_parent->d_name.name,
1040 dentry->d_name.name);
1041 goto out_bad;
1044 if (nfs_have_delegation(inode, FMODE_READ))
1045 goto out_set_verifier;
1047 /* Force a full look up iff the parent directory has changed */
1048 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
1049 if (nfs_lookup_verify_inode(inode, nd))
1050 goto out_zap_parent;
1051 goto out_valid;
1054 if (NFS_STALE(inode))
1055 goto out_bad;
1057 error = -ENOMEM;
1058 fhandle = nfs_alloc_fhandle();
1059 fattr = nfs_alloc_fattr();
1060 if (fhandle == NULL || fattr == NULL)
1061 goto out_error;
1063 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1064 if (error)
1065 goto out_bad;
1066 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1067 goto out_bad;
1068 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
1069 goto out_bad;
1071 nfs_free_fattr(fattr);
1072 nfs_free_fhandle(fhandle);
1073 out_set_verifier:
1074 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1075 out_valid:
1076 dput(parent);
1077 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
1078 __func__, dentry->d_parent->d_name.name,
1079 dentry->d_name.name);
1080 return 1;
1081 out_zap_parent:
1082 nfs_zap_caches(dir);
1083 out_bad:
1084 nfs_mark_for_revalidate(dir);
1085 if (inode && S_ISDIR(inode->i_mode)) {
1086 /* Purge readdir caches. */
1087 nfs_zap_caches(inode);
1088 /* If we have submounts, don't unhash ! */
1089 if (have_submounts(dentry))
1090 goto out_valid;
1091 if (dentry->d_flags & DCACHE_DISCONNECTED)
1092 goto out_valid;
1093 shrink_dcache_parent(dentry);
1095 d_drop(dentry);
1096 nfs_free_fattr(fattr);
1097 nfs_free_fhandle(fhandle);
1098 dput(parent);
1099 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
1100 __func__, dentry->d_parent->d_name.name,
1101 dentry->d_name.name);
1102 return 0;
1103 out_error:
1104 nfs_free_fattr(fattr);
1105 nfs_free_fhandle(fhandle);
1106 dput(parent);
1107 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1108 __func__, dentry->d_parent->d_name.name,
1109 dentry->d_name.name, error);
1110 return error;
1114 * This is called from dput() when d_count is going to 0.
1116 static int nfs_dentry_delete(struct dentry *dentry)
1118 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1119 dentry->d_parent->d_name.name, dentry->d_name.name,
1120 dentry->d_flags);
1122 /* Unhash any dentry with a stale inode */
1123 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1124 return 1;
1126 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1127 /* Unhash it, so that ->d_iput() would be called */
1128 return 1;
1130 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1131 /* Unhash it, so that ancestors of killed async unlink
1132 * files will be cleaned up during umount */
1133 return 1;
1135 return 0;
1139 static void nfs_drop_nlink(struct inode *inode)
1141 spin_lock(&inode->i_lock);
1142 if (inode->i_nlink > 0)
1143 drop_nlink(inode);
1144 spin_unlock(&inode->i_lock);
1148 * Called when the dentry loses inode.
1149 * We use it to clean up silly-renamed files.
1151 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1153 if (S_ISDIR(inode->i_mode))
1154 /* drop any readdir cache as it could easily be old */
1155 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1157 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1158 drop_nlink(inode);
1159 nfs_complete_unlink(dentry, inode);
1161 iput(inode);
1164 const struct dentry_operations nfs_dentry_operations = {
1165 .d_revalidate = nfs_lookup_revalidate,
1166 .d_delete = nfs_dentry_delete,
1167 .d_iput = nfs_dentry_iput,
1170 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1172 struct dentry *res;
1173 struct dentry *parent;
1174 struct inode *inode = NULL;
1175 struct nfs_fh *fhandle = NULL;
1176 struct nfs_fattr *fattr = NULL;
1177 int error;
1179 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1180 dentry->d_parent->d_name.name, dentry->d_name.name);
1181 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1183 res = ERR_PTR(-ENAMETOOLONG);
1184 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1185 goto out;
1187 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1190 * If we're doing an exclusive create, optimize away the lookup
1191 * but don't hash the dentry.
1193 if (nfs_is_exclusive_create(dir, nd)) {
1194 d_instantiate(dentry, NULL);
1195 res = NULL;
1196 goto out;
1199 res = ERR_PTR(-ENOMEM);
1200 fhandle = nfs_alloc_fhandle();
1201 fattr = nfs_alloc_fattr();
1202 if (fhandle == NULL || fattr == NULL)
1203 goto out;
1205 parent = dentry->d_parent;
1206 /* Protect against concurrent sillydeletes */
1207 nfs_block_sillyrename(parent);
1208 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1209 if (error == -ENOENT)
1210 goto no_entry;
1211 if (error < 0) {
1212 res = ERR_PTR(error);
1213 goto out_unblock_sillyrename;
1215 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1216 res = (struct dentry *)inode;
1217 if (IS_ERR(res))
1218 goto out_unblock_sillyrename;
1220 no_entry:
1221 res = d_materialise_unique(dentry, inode);
1222 if (res != NULL) {
1223 if (IS_ERR(res))
1224 goto out_unblock_sillyrename;
1225 dentry = res;
1227 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1228 out_unblock_sillyrename:
1229 nfs_unblock_sillyrename(parent);
1230 out:
1231 nfs_free_fattr(fattr);
1232 nfs_free_fhandle(fhandle);
1233 return res;
1236 #ifdef CONFIG_NFS_V4
1237 static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1239 const struct dentry_operations nfs4_dentry_operations = {
1240 .d_revalidate = nfs_open_revalidate,
1241 .d_delete = nfs_dentry_delete,
1242 .d_iput = nfs_dentry_iput,
1246 * Use intent information to determine whether we need to substitute
1247 * the NFSv4-style stateful OPEN for the LOOKUP call
1249 static int is_atomic_open(struct nameidata *nd)
1251 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
1252 return 0;
1253 /* NFS does not (yet) have a stateful open for directories */
1254 if (nd->flags & LOOKUP_DIRECTORY)
1255 return 0;
1256 /* Are we trying to write to a read only partition? */
1257 if (__mnt_is_readonly(nd->path.mnt) &&
1258 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
1259 return 0;
1260 return 1;
1263 static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1265 struct path path = {
1266 .mnt = nd->path.mnt,
1267 .dentry = dentry,
1269 struct nfs_open_context *ctx;
1270 struct rpc_cred *cred;
1271 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1273 cred = rpc_lookup_cred();
1274 if (IS_ERR(cred))
1275 return ERR_CAST(cred);
1276 ctx = alloc_nfs_open_context(&path, cred, fmode);
1277 put_rpccred(cred);
1278 if (ctx == NULL)
1279 return ERR_PTR(-ENOMEM);
1280 return ctx;
1283 static int do_open(struct inode *inode, struct file *filp)
1285 nfs_fscache_set_inode_cookie(inode, filp);
1286 return 0;
1289 static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1291 struct file *filp;
1292 int ret = 0;
1294 /* If the open_intent is for execute, we have an extra check to make */
1295 if (ctx->mode & FMODE_EXEC) {
1296 ret = nfs_may_open(ctx->path.dentry->d_inode,
1297 ctx->cred,
1298 nd->intent.open.flags);
1299 if (ret < 0)
1300 goto out;
1302 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1303 if (IS_ERR(filp))
1304 ret = PTR_ERR(filp);
1305 else
1306 nfs_file_set_open_context(filp, ctx);
1307 out:
1308 put_nfs_open_context(ctx);
1309 return ret;
1312 static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1314 struct nfs_open_context *ctx;
1315 struct iattr attr;
1316 struct dentry *res = NULL;
1317 struct inode *inode;
1318 int open_flags;
1319 int err;
1321 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1322 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1324 /* Check that we are indeed trying to open this file */
1325 if (!is_atomic_open(nd))
1326 goto no_open;
1328 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1329 res = ERR_PTR(-ENAMETOOLONG);
1330 goto out;
1332 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1334 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1335 * the dentry. */
1336 if (nd->flags & LOOKUP_EXCL) {
1337 d_instantiate(dentry, NULL);
1338 goto out;
1341 ctx = nameidata_to_nfs_open_context(dentry, nd);
1342 res = ERR_CAST(ctx);
1343 if (IS_ERR(ctx))
1344 goto out;
1346 open_flags = nd->intent.open.flags;
1347 if (nd->flags & LOOKUP_CREATE) {
1348 attr.ia_mode = nd->intent.open.create_mode;
1349 attr.ia_valid = ATTR_MODE;
1350 if (!IS_POSIXACL(dir))
1351 attr.ia_mode &= ~current_umask();
1352 } else {
1353 open_flags &= ~(O_EXCL | O_CREAT);
1354 attr.ia_valid = 0;
1357 /* Open the file on the server */
1358 nfs_block_sillyrename(dentry->d_parent);
1359 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1360 if (IS_ERR(inode)) {
1361 nfs_unblock_sillyrename(dentry->d_parent);
1362 put_nfs_open_context(ctx);
1363 switch (PTR_ERR(inode)) {
1364 /* Make a negative dentry */
1365 case -ENOENT:
1366 d_add(dentry, NULL);
1367 res = NULL;
1368 goto out;
1369 /* This turned out not to be a regular file */
1370 case -ENOTDIR:
1371 goto no_open;
1372 case -ELOOP:
1373 if (!(nd->intent.open.flags & O_NOFOLLOW))
1374 goto no_open;
1375 /* case -EISDIR: */
1376 /* case -EINVAL: */
1377 default:
1378 res = ERR_CAST(inode);
1379 goto out;
1382 res = d_add_unique(dentry, inode);
1383 nfs_unblock_sillyrename(dentry->d_parent);
1384 if (res != NULL) {
1385 dput(ctx->path.dentry);
1386 ctx->path.dentry = dget(res);
1387 dentry = res;
1389 err = nfs_intent_set_file(nd, ctx);
1390 if (err < 0) {
1391 if (res != NULL)
1392 dput(res);
1393 return ERR_PTR(err);
1395 out:
1396 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1397 return res;
1398 no_open:
1399 return nfs_lookup(dir, dentry, nd);
1402 static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1404 struct dentry *parent = NULL;
1405 struct inode *inode = dentry->d_inode;
1406 struct inode *dir;
1407 struct nfs_open_context *ctx;
1408 int openflags, ret = 0;
1410 if (!is_atomic_open(nd) || d_mountpoint(dentry))
1411 goto no_open;
1413 parent = dget_parent(dentry);
1414 dir = parent->d_inode;
1416 /* We can't create new files in nfs_open_revalidate(), so we
1417 * optimize away revalidation of negative dentries.
1419 if (inode == NULL) {
1420 if (!nfs_neg_need_reval(dir, dentry, nd))
1421 ret = 1;
1422 goto out;
1425 /* NFS only supports OPEN on regular files */
1426 if (!S_ISREG(inode->i_mode))
1427 goto no_open_dput;
1428 openflags = nd->intent.open.flags;
1429 /* We cannot do exclusive creation on a positive dentry */
1430 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1431 goto no_open_dput;
1432 /* We can't create new files, or truncate existing ones here */
1433 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
1435 ctx = nameidata_to_nfs_open_context(dentry, nd);
1436 ret = PTR_ERR(ctx);
1437 if (IS_ERR(ctx))
1438 goto out;
1440 * Note: we're not holding inode->i_mutex and so may be racing with
1441 * operations that change the directory. We therefore save the
1442 * change attribute *before* we do the RPC call.
1444 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
1445 if (IS_ERR(inode)) {
1446 ret = PTR_ERR(inode);
1447 switch (ret) {
1448 case -EPERM:
1449 case -EACCES:
1450 case -EDQUOT:
1451 case -ENOSPC:
1452 case -EROFS:
1453 goto out_put_ctx;
1454 default:
1455 goto out_drop;
1458 iput(inode);
1459 if (inode != dentry->d_inode)
1460 goto out_drop;
1462 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1463 ret = nfs_intent_set_file(nd, ctx);
1464 if (ret >= 0)
1465 ret = 1;
1466 out:
1467 dput(parent);
1468 return ret;
1469 out_drop:
1470 d_drop(dentry);
1471 ret = 0;
1472 out_put_ctx:
1473 put_nfs_open_context(ctx);
1474 goto out;
1476 no_open_dput:
1477 dput(parent);
1478 no_open:
1479 return nfs_lookup_revalidate(dentry, nd);
1482 static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1483 struct nameidata *nd)
1485 struct nfs_open_context *ctx = NULL;
1486 struct iattr attr;
1487 int error;
1488 int open_flags = 0;
1490 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1491 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1493 attr.ia_mode = mode;
1494 attr.ia_valid = ATTR_MODE;
1496 if ((nd->flags & LOOKUP_CREATE) != 0) {
1497 open_flags = nd->intent.open.flags;
1499 ctx = nameidata_to_nfs_open_context(dentry, nd);
1500 error = PTR_ERR(ctx);
1501 if (IS_ERR(ctx))
1502 goto out_err_drop;
1505 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1506 if (error != 0)
1507 goto out_put_ctx;
1508 if (ctx != NULL) {
1509 error = nfs_intent_set_file(nd, ctx);
1510 if (error < 0)
1511 goto out_err;
1513 return 0;
1514 out_put_ctx:
1515 if (ctx != NULL)
1516 put_nfs_open_context(ctx);
1517 out_err_drop:
1518 d_drop(dentry);
1519 out_err:
1520 return error;
1523 #endif /* CONFIG_NFSV4 */
1526 * Code common to create, mkdir, and mknod.
1528 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1529 struct nfs_fattr *fattr)
1531 struct dentry *parent = dget_parent(dentry);
1532 struct inode *dir = parent->d_inode;
1533 struct inode *inode;
1534 int error = -EACCES;
1536 d_drop(dentry);
1538 /* We may have been initialized further down */
1539 if (dentry->d_inode)
1540 goto out;
1541 if (fhandle->size == 0) {
1542 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1543 if (error)
1544 goto out_error;
1546 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1547 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1548 struct nfs_server *server = NFS_SB(dentry->d_sb);
1549 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
1550 if (error < 0)
1551 goto out_error;
1553 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1554 error = PTR_ERR(inode);
1555 if (IS_ERR(inode))
1556 goto out_error;
1557 d_add(dentry, inode);
1558 out:
1559 dput(parent);
1560 return 0;
1561 out_error:
1562 nfs_mark_for_revalidate(dir);
1563 dput(parent);
1564 return error;
1568 * Following a failed create operation, we drop the dentry rather
1569 * than retain a negative dentry. This avoids a problem in the event
1570 * that the operation succeeded on the server, but an error in the
1571 * reply path made it appear to have failed.
1573 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1574 struct nameidata *nd)
1576 struct iattr attr;
1577 int error;
1579 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1580 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1582 attr.ia_mode = mode;
1583 attr.ia_valid = ATTR_MODE;
1585 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
1586 if (error != 0)
1587 goto out_err;
1588 return 0;
1589 out_err:
1590 d_drop(dentry);
1591 return error;
1595 * See comments for nfs_proc_create regarding failed operations.
1597 static int
1598 nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1600 struct iattr attr;
1601 int status;
1603 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1604 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1606 if (!new_valid_dev(rdev))
1607 return -EINVAL;
1609 attr.ia_mode = mode;
1610 attr.ia_valid = ATTR_MODE;
1612 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1613 if (status != 0)
1614 goto out_err;
1615 return 0;
1616 out_err:
1617 d_drop(dentry);
1618 return status;
1622 * See comments for nfs_proc_create regarding failed operations.
1624 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1626 struct iattr attr;
1627 int error;
1629 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1630 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1632 attr.ia_valid = ATTR_MODE;
1633 attr.ia_mode = mode | S_IFDIR;
1635 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1636 if (error != 0)
1637 goto out_err;
1638 return 0;
1639 out_err:
1640 d_drop(dentry);
1641 return error;
1644 static void nfs_dentry_handle_enoent(struct dentry *dentry)
1646 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1647 d_delete(dentry);
1650 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1652 int error;
1654 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1655 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1657 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1658 /* Ensure the VFS deletes this inode */
1659 if (error == 0 && dentry->d_inode != NULL)
1660 clear_nlink(dentry->d_inode);
1661 else if (error == -ENOENT)
1662 nfs_dentry_handle_enoent(dentry);
1664 return error;
1668 * Remove a file after making sure there are no pending writes,
1669 * and after checking that the file has only one user.
1671 * We invalidate the attribute cache and free the inode prior to the operation
1672 * to avoid possible races if the server reuses the inode.
1674 static int nfs_safe_remove(struct dentry *dentry)
1676 struct inode *dir = dentry->d_parent->d_inode;
1677 struct inode *inode = dentry->d_inode;
1678 int error = -EBUSY;
1680 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1681 dentry->d_parent->d_name.name, dentry->d_name.name);
1683 /* If the dentry was sillyrenamed, we simply call d_delete() */
1684 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1685 error = 0;
1686 goto out;
1689 if (inode != NULL) {
1690 nfs_inode_return_delegation(inode);
1691 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1692 /* The VFS may want to delete this inode */
1693 if (error == 0)
1694 nfs_drop_nlink(inode);
1695 nfs_mark_for_revalidate(inode);
1696 } else
1697 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1698 if (error == -ENOENT)
1699 nfs_dentry_handle_enoent(dentry);
1700 out:
1701 return error;
1704 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1705 * belongs to an active ".nfs..." file and we return -EBUSY.
1707 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1709 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1711 int error;
1712 int need_rehash = 0;
1714 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1715 dir->i_ino, dentry->d_name.name);
1717 spin_lock(&dcache_lock);
1718 spin_lock(&dentry->d_lock);
1719 if (atomic_read(&dentry->d_count) > 1) {
1720 spin_unlock(&dentry->d_lock);
1721 spin_unlock(&dcache_lock);
1722 /* Start asynchronous writeout of the inode */
1723 write_inode_now(dentry->d_inode, 0);
1724 error = nfs_sillyrename(dir, dentry);
1725 return error;
1727 if (!d_unhashed(dentry)) {
1728 __d_drop(dentry);
1729 need_rehash = 1;
1731 spin_unlock(&dentry->d_lock);
1732 spin_unlock(&dcache_lock);
1733 error = nfs_safe_remove(dentry);
1734 if (!error || error == -ENOENT) {
1735 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1736 } else if (need_rehash)
1737 d_rehash(dentry);
1738 return error;
1742 * To create a symbolic link, most file systems instantiate a new inode,
1743 * add a page to it containing the path, then write it out to the disk
1744 * using prepare_write/commit_write.
1746 * Unfortunately the NFS client can't create the in-core inode first
1747 * because it needs a file handle to create an in-core inode (see
1748 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1749 * symlink request has completed on the server.
1751 * So instead we allocate a raw page, copy the symname into it, then do
1752 * the SYMLINK request with the page as the buffer. If it succeeds, we
1753 * now have a new file handle and can instantiate an in-core NFS inode
1754 * and move the raw page into its mapping.
1756 static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1758 struct pagevec lru_pvec;
1759 struct page *page;
1760 char *kaddr;
1761 struct iattr attr;
1762 unsigned int pathlen = strlen(symname);
1763 int error;
1765 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1766 dir->i_ino, dentry->d_name.name, symname);
1768 if (pathlen > PAGE_SIZE)
1769 return -ENAMETOOLONG;
1771 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1772 attr.ia_valid = ATTR_MODE;
1774 page = alloc_page(GFP_HIGHUSER);
1775 if (!page)
1776 return -ENOMEM;
1778 kaddr = kmap_atomic(page, KM_USER0);
1779 memcpy(kaddr, symname, pathlen);
1780 if (pathlen < PAGE_SIZE)
1781 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1782 kunmap_atomic(kaddr, KM_USER0);
1784 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1785 if (error != 0) {
1786 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1787 dir->i_sb->s_id, dir->i_ino,
1788 dentry->d_name.name, symname, error);
1789 d_drop(dentry);
1790 __free_page(page);
1791 return error;
1795 * No big deal if we can't add this page to the page cache here.
1796 * READLINK will get the missing page from the server if needed.
1798 pagevec_init(&lru_pvec, 0);
1799 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1800 GFP_KERNEL)) {
1801 pagevec_add(&lru_pvec, page);
1802 pagevec_lru_add_file(&lru_pvec);
1803 SetPageUptodate(page);
1804 unlock_page(page);
1805 } else
1806 __free_page(page);
1808 return 0;
1811 static int
1812 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1814 struct inode *inode = old_dentry->d_inode;
1815 int error;
1817 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1818 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1819 dentry->d_parent->d_name.name, dentry->d_name.name);
1821 nfs_inode_return_delegation(inode);
1823 d_drop(dentry);
1824 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1825 if (error == 0) {
1826 ihold(inode);
1827 d_add(dentry, inode);
1829 return error;
1833 * RENAME
1834 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1835 * different file handle for the same inode after a rename (e.g. when
1836 * moving to a different directory). A fail-safe method to do so would
1837 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1838 * rename the old file using the sillyrename stuff. This way, the original
1839 * file in old_dir will go away when the last process iput()s the inode.
1841 * FIXED.
1843 * It actually works quite well. One needs to have the possibility for
1844 * at least one ".nfs..." file in each directory the file ever gets
1845 * moved or linked to which happens automagically with the new
1846 * implementation that only depends on the dcache stuff instead of
1847 * using the inode layer
1849 * Unfortunately, things are a little more complicated than indicated
1850 * above. For a cross-directory move, we want to make sure we can get
1851 * rid of the old inode after the operation. This means there must be
1852 * no pending writes (if it's a file), and the use count must be 1.
1853 * If these conditions are met, we can drop the dentries before doing
1854 * the rename.
1856 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1857 struct inode *new_dir, struct dentry *new_dentry)
1859 struct inode *old_inode = old_dentry->d_inode;
1860 struct inode *new_inode = new_dentry->d_inode;
1861 struct dentry *dentry = NULL, *rehash = NULL;
1862 int error = -EBUSY;
1864 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1865 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1866 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1867 atomic_read(&new_dentry->d_count));
1870 * For non-directories, check whether the target is busy and if so,
1871 * make a copy of the dentry and then do a silly-rename. If the
1872 * silly-rename succeeds, the copied dentry is hashed and becomes
1873 * the new target.
1875 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1877 * To prevent any new references to the target during the
1878 * rename, we unhash the dentry in advance.
1880 if (!d_unhashed(new_dentry)) {
1881 d_drop(new_dentry);
1882 rehash = new_dentry;
1885 if (atomic_read(&new_dentry->d_count) > 2) {
1886 int err;
1888 /* copy the target dentry's name */
1889 dentry = d_alloc(new_dentry->d_parent,
1890 &new_dentry->d_name);
1891 if (!dentry)
1892 goto out;
1894 /* silly-rename the existing target ... */
1895 err = nfs_sillyrename(new_dir, new_dentry);
1896 if (err)
1897 goto out;
1899 new_dentry = dentry;
1900 rehash = NULL;
1901 new_inode = NULL;
1905 nfs_inode_return_delegation(old_inode);
1906 if (new_inode != NULL)
1907 nfs_inode_return_delegation(new_inode);
1909 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1910 new_dir, &new_dentry->d_name);
1911 nfs_mark_for_revalidate(old_inode);
1912 out:
1913 if (rehash)
1914 d_rehash(rehash);
1915 if (!error) {
1916 if (new_inode != NULL)
1917 nfs_drop_nlink(new_inode);
1918 d_move(old_dentry, new_dentry);
1919 nfs_set_verifier(new_dentry,
1920 nfs_save_change_attribute(new_dir));
1921 } else if (error == -ENOENT)
1922 nfs_dentry_handle_enoent(old_dentry);
1924 /* new dentry created? */
1925 if (dentry)
1926 dput(dentry);
1927 return error;
1930 static DEFINE_SPINLOCK(nfs_access_lru_lock);
1931 static LIST_HEAD(nfs_access_lru_list);
1932 static atomic_long_t nfs_access_nr_entries;
1934 static void nfs_access_free_entry(struct nfs_access_entry *entry)
1936 put_rpccred(entry->cred);
1937 kfree(entry);
1938 smp_mb__before_atomic_dec();
1939 atomic_long_dec(&nfs_access_nr_entries);
1940 smp_mb__after_atomic_dec();
1943 static void nfs_access_free_list(struct list_head *head)
1945 struct nfs_access_entry *cache;
1947 while (!list_empty(head)) {
1948 cache = list_entry(head->next, struct nfs_access_entry, lru);
1949 list_del(&cache->lru);
1950 nfs_access_free_entry(cache);
1954 int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
1956 LIST_HEAD(head);
1957 struct nfs_inode *nfsi, *next;
1958 struct nfs_access_entry *cache;
1960 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1961 return (nr_to_scan == 0) ? 0 : -1;
1963 spin_lock(&nfs_access_lru_lock);
1964 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
1965 struct inode *inode;
1967 if (nr_to_scan-- == 0)
1968 break;
1969 inode = &nfsi->vfs_inode;
1970 spin_lock(&inode->i_lock);
1971 if (list_empty(&nfsi->access_cache_entry_lru))
1972 goto remove_lru_entry;
1973 cache = list_entry(nfsi->access_cache_entry_lru.next,
1974 struct nfs_access_entry, lru);
1975 list_move(&cache->lru, &head);
1976 rb_erase(&cache->rb_node, &nfsi->access_cache);
1977 if (!list_empty(&nfsi->access_cache_entry_lru))
1978 list_move_tail(&nfsi->access_cache_inode_lru,
1979 &nfs_access_lru_list);
1980 else {
1981 remove_lru_entry:
1982 list_del_init(&nfsi->access_cache_inode_lru);
1983 smp_mb__before_clear_bit();
1984 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
1985 smp_mb__after_clear_bit();
1987 spin_unlock(&inode->i_lock);
1989 spin_unlock(&nfs_access_lru_lock);
1990 nfs_access_free_list(&head);
1991 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1994 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
1996 struct rb_root *root_node = &nfsi->access_cache;
1997 struct rb_node *n;
1998 struct nfs_access_entry *entry;
2000 /* Unhook entries from the cache */
2001 while ((n = rb_first(root_node)) != NULL) {
2002 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2003 rb_erase(n, root_node);
2004 list_move(&entry->lru, head);
2006 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
2009 void nfs_access_zap_cache(struct inode *inode)
2011 LIST_HEAD(head);
2013 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2014 return;
2015 /* Remove from global LRU init */
2016 spin_lock(&nfs_access_lru_lock);
2017 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2018 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2020 spin_lock(&inode->i_lock);
2021 __nfs_access_zap_cache(NFS_I(inode), &head);
2022 spin_unlock(&inode->i_lock);
2023 spin_unlock(&nfs_access_lru_lock);
2024 nfs_access_free_list(&head);
2027 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2029 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2030 struct nfs_access_entry *entry;
2032 while (n != NULL) {
2033 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2035 if (cred < entry->cred)
2036 n = n->rb_left;
2037 else if (cred > entry->cred)
2038 n = n->rb_right;
2039 else
2040 return entry;
2042 return NULL;
2045 static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2047 struct nfs_inode *nfsi = NFS_I(inode);
2048 struct nfs_access_entry *cache;
2049 int err = -ENOENT;
2051 spin_lock(&inode->i_lock);
2052 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2053 goto out_zap;
2054 cache = nfs_access_search_rbtree(inode, cred);
2055 if (cache == NULL)
2056 goto out;
2057 if (!nfs_have_delegated_attributes(inode) &&
2058 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
2059 goto out_stale;
2060 res->jiffies = cache->jiffies;
2061 res->cred = cache->cred;
2062 res->mask = cache->mask;
2063 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
2064 err = 0;
2065 out:
2066 spin_unlock(&inode->i_lock);
2067 return err;
2068 out_stale:
2069 rb_erase(&cache->rb_node, &nfsi->access_cache);
2070 list_del(&cache->lru);
2071 spin_unlock(&inode->i_lock);
2072 nfs_access_free_entry(cache);
2073 return -ENOENT;
2074 out_zap:
2075 spin_unlock(&inode->i_lock);
2076 nfs_access_zap_cache(inode);
2077 return -ENOENT;
2080 static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2082 struct nfs_inode *nfsi = NFS_I(inode);
2083 struct rb_root *root_node = &nfsi->access_cache;
2084 struct rb_node **p = &root_node->rb_node;
2085 struct rb_node *parent = NULL;
2086 struct nfs_access_entry *entry;
2088 spin_lock(&inode->i_lock);
2089 while (*p != NULL) {
2090 parent = *p;
2091 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2093 if (set->cred < entry->cred)
2094 p = &parent->rb_left;
2095 else if (set->cred > entry->cred)
2096 p = &parent->rb_right;
2097 else
2098 goto found;
2100 rb_link_node(&set->rb_node, parent, p);
2101 rb_insert_color(&set->rb_node, root_node);
2102 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2103 spin_unlock(&inode->i_lock);
2104 return;
2105 found:
2106 rb_replace_node(parent, &set->rb_node, root_node);
2107 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2108 list_del(&entry->lru);
2109 spin_unlock(&inode->i_lock);
2110 nfs_access_free_entry(entry);
2113 static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
2115 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2116 if (cache == NULL)
2117 return;
2118 RB_CLEAR_NODE(&cache->rb_node);
2119 cache->jiffies = set->jiffies;
2120 cache->cred = get_rpccred(set->cred);
2121 cache->mask = set->mask;
2123 nfs_access_add_rbtree(inode, cache);
2125 /* Update accounting */
2126 smp_mb__before_atomic_inc();
2127 atomic_long_inc(&nfs_access_nr_entries);
2128 smp_mb__after_atomic_inc();
2130 /* Add inode to global LRU list */
2131 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2132 spin_lock(&nfs_access_lru_lock);
2133 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2134 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2135 &nfs_access_lru_list);
2136 spin_unlock(&nfs_access_lru_lock);
2140 static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2142 struct nfs_access_entry cache;
2143 int status;
2145 status = nfs_access_get_cached(inode, cred, &cache);
2146 if (status == 0)
2147 goto out;
2149 /* Be clever: ask server to check for all possible rights */
2150 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2151 cache.cred = cred;
2152 cache.jiffies = jiffies;
2153 status = NFS_PROTO(inode)->access(inode, &cache);
2154 if (status != 0) {
2155 if (status == -ESTALE) {
2156 nfs_zap_caches(inode);
2157 if (!S_ISDIR(inode->i_mode))
2158 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2160 return status;
2162 nfs_access_add_cache(inode, &cache);
2163 out:
2164 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2165 return 0;
2166 return -EACCES;
2169 static int nfs_open_permission_mask(int openflags)
2171 int mask = 0;
2173 if (openflags & FMODE_READ)
2174 mask |= MAY_READ;
2175 if (openflags & FMODE_WRITE)
2176 mask |= MAY_WRITE;
2177 if (openflags & FMODE_EXEC)
2178 mask |= MAY_EXEC;
2179 return mask;
2182 int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2184 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2187 int nfs_permission(struct inode *inode, int mask)
2189 struct rpc_cred *cred;
2190 int res = 0;
2192 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2194 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2195 goto out;
2196 /* Is this sys_access() ? */
2197 if (mask & (MAY_ACCESS | MAY_CHDIR))
2198 goto force_lookup;
2200 switch (inode->i_mode & S_IFMT) {
2201 case S_IFLNK:
2202 goto out;
2203 case S_IFREG:
2204 /* NFSv4 has atomic_open... */
2205 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
2206 && (mask & MAY_OPEN)
2207 && !(mask & MAY_EXEC))
2208 goto out;
2209 break;
2210 case S_IFDIR:
2212 * Optimize away all write operations, since the server
2213 * will check permissions when we perform the op.
2215 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2216 goto out;
2219 force_lookup:
2220 if (!NFS_PROTO(inode)->access)
2221 goto out_notsup;
2223 cred = rpc_lookup_cred();
2224 if (!IS_ERR(cred)) {
2225 res = nfs_do_access(inode, cred, mask);
2226 put_rpccred(cred);
2227 } else
2228 res = PTR_ERR(cred);
2229 out:
2230 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2231 res = -EACCES;
2233 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2234 inode->i_sb->s_id, inode->i_ino, mask, res);
2235 return res;
2236 out_notsup:
2237 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2238 if (res == 0)
2239 res = generic_permission(inode, mask, NULL);
2240 goto out;
2244 * Local variables:
2245 * version-control: t
2246 * kept-new-versions: 5
2247 * End: