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>
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/smp_lock.h>
33 #include <linux/namei.h>
34 #include <linux/mount.h>
37 #include "delegation.h"
40 #define NFS_PARANOIA 1
41 /* #define NFS_DEBUG_VERBOSE 1 */
43 static int nfs_opendir(struct inode
*, struct file
*);
44 static int nfs_readdir(struct file
*, void *, filldir_t
);
45 static struct dentry
*nfs_lookup(struct inode
*, struct dentry
*, struct nameidata
*);
46 static int nfs_create(struct inode
*, struct dentry
*, int, struct nameidata
*);
47 static int nfs_mkdir(struct inode
*, struct dentry
*, int);
48 static int nfs_rmdir(struct inode
*, struct dentry
*);
49 static int nfs_unlink(struct inode
*, struct dentry
*);
50 static int nfs_symlink(struct inode
*, struct dentry
*, const char *);
51 static int nfs_link(struct dentry
*, struct inode
*, struct dentry
*);
52 static int nfs_mknod(struct inode
*, struct dentry
*, int, dev_t
);
53 static int nfs_rename(struct inode
*, struct dentry
*,
54 struct inode
*, struct dentry
*);
55 static int nfs_fsync_dir(struct file
*, struct dentry
*, int);
56 static loff_t
nfs_llseek_dir(struct file
*, loff_t
, int);
58 const struct file_operations nfs_dir_operations
= {
59 .llseek
= nfs_llseek_dir
,
60 .read
= generic_read_dir
,
61 .readdir
= nfs_readdir
,
63 .release
= nfs_release
,
64 .fsync
= nfs_fsync_dir
,
67 struct inode_operations nfs_dir_inode_operations
= {
72 .symlink
= nfs_symlink
,
77 .permission
= nfs_permission
,
78 .getattr
= nfs_getattr
,
79 .setattr
= nfs_setattr
,
83 struct inode_operations nfs3_dir_inode_operations
= {
88 .symlink
= nfs_symlink
,
93 .permission
= nfs_permission
,
94 .getattr
= nfs_getattr
,
95 .setattr
= nfs_setattr
,
96 .listxattr
= nfs3_listxattr
,
97 .getxattr
= nfs3_getxattr
,
98 .setxattr
= nfs3_setxattr
,
99 .removexattr
= nfs3_removexattr
,
101 #endif /* CONFIG_NFS_V3 */
105 static struct dentry
*nfs_atomic_lookup(struct inode
*, struct dentry
*, struct nameidata
*);
106 struct inode_operations nfs4_dir_inode_operations
= {
107 .create
= nfs_create
,
108 .lookup
= nfs_atomic_lookup
,
110 .unlink
= nfs_unlink
,
111 .symlink
= nfs_symlink
,
115 .rename
= nfs_rename
,
116 .permission
= nfs_permission
,
117 .getattr
= nfs_getattr
,
118 .setattr
= nfs_setattr
,
119 .getxattr
= nfs4_getxattr
,
120 .setxattr
= nfs4_setxattr
,
121 .listxattr
= nfs4_listxattr
,
124 #endif /* CONFIG_NFS_V4 */
130 nfs_opendir(struct inode
*inode
, struct file
*filp
)
134 dfprintk(VFS
, "NFS: opendir(%s/%ld)\n",
135 inode
->i_sb
->s_id
, inode
->i_ino
);
138 /* Call generic open code in order to cache credentials */
139 res
= nfs_open(inode
, filp
);
144 typedef u32
* (*decode_dirent_t
)(u32
*, struct nfs_entry
*, int);
148 unsigned long page_index
;
151 loff_t current_index
;
152 struct nfs_entry
*entry
;
153 decode_dirent_t decode
;
156 } nfs_readdir_descriptor_t
;
158 /* Now we cache directories properly, by stuffing the dirent
159 * data directly in the page cache.
161 * Inode invalidation due to refresh etc. takes care of
162 * _everything_, no sloppy entry flushing logic, no extraneous
163 * copying, network direct to page cache, the way it was meant
166 * NOTE: Dirent information verification is done always by the
167 * page-in of the RPC reply, nowhere else, this simplies
168 * things substantially.
171 int nfs_readdir_filler(nfs_readdir_descriptor_t
*desc
, struct page
*page
)
173 struct file
*file
= desc
->file
;
174 struct inode
*inode
= file
->f_dentry
->d_inode
;
175 struct rpc_cred
*cred
= nfs_file_cred(file
);
176 unsigned long timestamp
;
179 dfprintk(DIRCACHE
, "NFS: %s: reading cookie %Lu into page %lu\n",
180 __FUNCTION__
, (long long)desc
->entry
->cookie
,
185 error
= NFS_PROTO(inode
)->readdir(file
->f_dentry
, cred
, desc
->entry
->cookie
, page
,
186 NFS_SERVER(inode
)->dtsize
, desc
->plus
);
188 /* We requested READDIRPLUS, but the server doesn't grok it */
189 if (error
== -ENOTSUPP
&& desc
->plus
) {
190 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_READDIRPLUS
;
191 clear_bit(NFS_INO_ADVISE_RDPLUS
, &NFS_FLAGS(inode
));
197 SetPageUptodate(page
);
198 spin_lock(&inode
->i_lock
);
199 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
200 spin_unlock(&inode
->i_lock
);
201 /* Ensure consistent page alignment of the data.
202 * Note: assumes we have exclusive access to this mapping either
203 * through inode->i_mutex or some other mechanism.
205 if (page
->index
== 0)
206 invalidate_inode_pages2_range(inode
->i_mapping
, PAGE_CACHE_SIZE
, -1);
212 nfs_zap_caches(inode
);
218 int dir_decode(nfs_readdir_descriptor_t
*desc
)
221 p
= desc
->decode(p
, desc
->entry
, desc
->plus
);
229 void dir_page_release(nfs_readdir_descriptor_t
*desc
)
232 page_cache_release(desc
->page
);
238 * Given a pointer to a buffer that has already been filled by a call
239 * to readdir, find the next entry with cookie '*desc->dir_cookie'.
241 * If the end of the buffer has been reached, return -EAGAIN, if not,
242 * return the offset within the buffer of the next entry to be
246 int find_dirent(nfs_readdir_descriptor_t
*desc
)
248 struct nfs_entry
*entry
= desc
->entry
;
252 while((status
= dir_decode(desc
)) == 0) {
253 dfprintk(DIRCACHE
, "NFS: %s: examining cookie %Lu\n",
254 __FUNCTION__
, (unsigned long long)entry
->cookie
);
255 if (entry
->prev_cookie
== *desc
->dir_cookie
)
257 if (loop_count
++ > 200) {
266 * Given a pointer to a buffer that has already been filled by a call
267 * to readdir, find the entry at offset 'desc->file->f_pos'.
269 * If the end of the buffer has been reached, return -EAGAIN, if not,
270 * return the offset within the buffer of the next entry to be
274 int find_dirent_index(nfs_readdir_descriptor_t
*desc
)
276 struct nfs_entry
*entry
= desc
->entry
;
281 status
= dir_decode(desc
);
285 dfprintk(DIRCACHE
, "NFS: found cookie %Lu at index %Ld\n",
286 (unsigned long long)entry
->cookie
, desc
->current_index
);
288 if (desc
->file
->f_pos
== desc
->current_index
) {
289 *desc
->dir_cookie
= entry
->cookie
;
292 desc
->current_index
++;
293 if (loop_count
++ > 200) {
302 * Find the given page, and call find_dirent() or find_dirent_index in
303 * order to try to return the next entry.
306 int find_dirent_page(nfs_readdir_descriptor_t
*desc
)
308 struct inode
*inode
= desc
->file
->f_dentry
->d_inode
;
312 dfprintk(DIRCACHE
, "NFS: %s: searching page %ld for target %Lu\n",
313 __FUNCTION__
, desc
->page_index
,
314 (long long) *desc
->dir_cookie
);
316 page
= read_cache_page(inode
->i_mapping
, desc
->page_index
,
317 (filler_t
*)nfs_readdir_filler
, desc
);
319 status
= PTR_ERR(page
);
322 if (!PageUptodate(page
))
325 /* NOTE: Someone else may have changed the READDIRPLUS flag */
327 desc
->ptr
= kmap(page
); /* matching kunmap in nfs_do_filldir */
328 if (*desc
->dir_cookie
!= 0)
329 status
= find_dirent(desc
);
331 status
= find_dirent_index(desc
);
333 dir_page_release(desc
);
335 dfprintk(DIRCACHE
, "NFS: %s: returns %d\n", __FUNCTION__
, status
);
338 page_cache_release(page
);
343 * Recurse through the page cache pages, and return a
344 * filled nfs_entry structure of the next directory entry if possible.
346 * The target for the search is '*desc->dir_cookie' if non-0,
347 * 'desc->file->f_pos' otherwise
350 int readdir_search_pagecache(nfs_readdir_descriptor_t
*desc
)
355 /* Always search-by-index from the beginning of the cache */
356 if (*desc
->dir_cookie
== 0) {
357 dfprintk(DIRCACHE
, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
358 (long long)desc
->file
->f_pos
);
359 desc
->page_index
= 0;
360 desc
->entry
->cookie
= desc
->entry
->prev_cookie
= 0;
361 desc
->entry
->eof
= 0;
362 desc
->current_index
= 0;
364 dfprintk(DIRCACHE
, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
365 (unsigned long long)*desc
->dir_cookie
);
368 res
= find_dirent_page(desc
);
371 /* Align to beginning of next page */
373 if (loop_count
++ > 200) {
379 dfprintk(DIRCACHE
, "NFS: %s: returns %d\n", __FUNCTION__
, res
);
383 static inline unsigned int dt_type(struct inode
*inode
)
385 return (inode
->i_mode
>> 12) & 15;
388 static struct dentry
*nfs_readdir_lookup(nfs_readdir_descriptor_t
*desc
);
391 * Once we've found the start of the dirent within a page: fill 'er up...
394 int nfs_do_filldir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
397 struct file
*file
= desc
->file
;
398 struct nfs_entry
*entry
= desc
->entry
;
399 struct dentry
*dentry
= NULL
;
400 unsigned long fileid
;
404 dfprintk(DIRCACHE
, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
405 (unsigned long long)entry
->cookie
);
408 unsigned d_type
= DT_UNKNOWN
;
409 /* Note: entry->prev_cookie contains the cookie for
410 * retrieving the current dirent on the server */
411 fileid
= nfs_fileid_to_ino_t(entry
->ino
);
413 /* Get a dentry if we have one */
416 dentry
= nfs_readdir_lookup(desc
);
418 /* Use readdirplus info */
419 if (dentry
!= NULL
&& dentry
->d_inode
!= NULL
) {
420 d_type
= dt_type(dentry
->d_inode
);
421 fileid
= dentry
->d_inode
->i_ino
;
424 res
= filldir(dirent
, entry
->name
, entry
->len
,
425 file
->f_pos
, fileid
, d_type
);
429 *desc
->dir_cookie
= entry
->cookie
;
430 if (dir_decode(desc
) != 0) {
434 if (loop_count
++ > 200) {
439 dir_page_release(desc
);
442 dfprintk(DIRCACHE
, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
443 (unsigned long long)*desc
->dir_cookie
, res
);
448 * If we cannot find a cookie in our cache, we suspect that this is
449 * because it points to a deleted file, so we ask the server to return
450 * whatever it thinks is the next entry. We then feed this to filldir.
451 * If all goes well, we should then be able to find our way round the
452 * cache on the next call to readdir_search_pagecache();
454 * NOTE: we cannot add the anonymous page to the pagecache because
455 * the data it contains might not be page aligned. Besides,
456 * we should already have a complete representation of the
457 * directory in the page cache by the time we get here.
460 int uncached_readdir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
463 struct file
*file
= desc
->file
;
464 struct inode
*inode
= file
->f_dentry
->d_inode
;
465 struct rpc_cred
*cred
= nfs_file_cred(file
);
466 struct page
*page
= NULL
;
469 dfprintk(DIRCACHE
, "NFS: uncached_readdir() searching for cookie %Lu\n",
470 (unsigned long long)*desc
->dir_cookie
);
472 page
= alloc_page(GFP_HIGHUSER
);
477 desc
->error
= NFS_PROTO(inode
)->readdir(file
->f_dentry
, cred
, *desc
->dir_cookie
,
479 NFS_SERVER(inode
)->dtsize
,
481 spin_lock(&inode
->i_lock
);
482 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
483 spin_unlock(&inode
->i_lock
);
485 desc
->ptr
= kmap(page
); /* matching kunmap in nfs_do_filldir */
486 if (desc
->error
>= 0) {
487 if ((status
= dir_decode(desc
)) == 0)
488 desc
->entry
->prev_cookie
= *desc
->dir_cookie
;
494 status
= nfs_do_filldir(desc
, dirent
, filldir
);
496 /* Reset read descriptor so it searches the page cache from
497 * the start upon the next call to readdir_search_pagecache() */
498 desc
->page_index
= 0;
499 desc
->entry
->cookie
= desc
->entry
->prev_cookie
= 0;
500 desc
->entry
->eof
= 0;
502 dfprintk(DIRCACHE
, "NFS: %s: returns %d\n",
503 __FUNCTION__
, status
);
506 dir_page_release(desc
);
510 /* The file offset position represents the dirent entry number. A
511 last cookie cache takes care of the common case of reading the
514 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
516 struct dentry
*dentry
= filp
->f_dentry
;
517 struct inode
*inode
= dentry
->d_inode
;
518 nfs_readdir_descriptor_t my_desc
,
520 struct nfs_entry my_entry
;
522 struct nfs_fattr fattr
;
525 dfprintk(VFS
, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
526 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
527 (long long)filp
->f_pos
);
528 nfs_inc_stats(inode
, NFSIOS_VFSGETDENTS
);
532 res
= nfs_revalidate_mapping(inode
, filp
->f_mapping
);
539 * filp->f_pos points to the dirent entry number.
540 * *desc->dir_cookie has the cookie for the next entry. We have
541 * to either find the entry with the appropriate number or
542 * revalidate the cookie.
544 memset(desc
, 0, sizeof(*desc
));
547 desc
->dir_cookie
= &((struct nfs_open_context
*)filp
->private_data
)->dir_cookie
;
548 desc
->decode
= NFS_PROTO(inode
)->decode_dirent
;
549 desc
->plus
= NFS_USE_READDIRPLUS(inode
);
551 my_entry
.cookie
= my_entry
.prev_cookie
= 0;
554 my_entry
.fattr
= &fattr
;
555 nfs_fattr_init(&fattr
);
556 desc
->entry
= &my_entry
;
558 while(!desc
->entry
->eof
) {
559 res
= readdir_search_pagecache(desc
);
561 if (res
== -EBADCOOKIE
) {
562 /* This means either end of directory */
563 if (*desc
->dir_cookie
&& desc
->entry
->cookie
!= *desc
->dir_cookie
) {
564 /* Or that the server has 'lost' a cookie */
565 res
= uncached_readdir(desc
, dirent
, filldir
);
572 if (res
== -ETOOSMALL
&& desc
->plus
) {
573 clear_bit(NFS_INO_ADVISE_RDPLUS
, &NFS_FLAGS(inode
));
574 nfs_zap_caches(inode
);
576 desc
->entry
->eof
= 0;
582 res
= nfs_do_filldir(desc
, dirent
, filldir
);
591 dfprintk(VFS
, "NFS: readdir(%s/%s) returns %ld\n",
592 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
597 loff_t
nfs_llseek_dir(struct file
*filp
, loff_t offset
, int origin
)
599 mutex_lock(&filp
->f_dentry
->d_inode
->i_mutex
);
602 offset
+= filp
->f_pos
;
610 if (offset
!= filp
->f_pos
) {
611 filp
->f_pos
= offset
;
612 ((struct nfs_open_context
*)filp
->private_data
)->dir_cookie
= 0;
615 mutex_unlock(&filp
->f_dentry
->d_inode
->i_mutex
);
620 * All directory operations under NFS are synchronous, so fsync()
621 * is a dummy operation.
623 int nfs_fsync_dir(struct file
*filp
, struct dentry
*dentry
, int datasync
)
625 dfprintk(VFS
, "NFS: fsync_dir(%s/%s) datasync %d\n",
626 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
633 * A check for whether or not the parent directory has changed.
634 * In the case it has, we assume that the dentries are untrustworthy
635 * and may need to be looked up again.
637 static inline int nfs_check_verifier(struct inode
*dir
, struct dentry
*dentry
)
641 if ((NFS_I(dir
)->cache_validity
& NFS_INO_INVALID_ATTR
) != 0
642 || nfs_attribute_timeout(dir
))
644 return nfs_verify_change_attribute(dir
, (unsigned long)dentry
->d_fsdata
);
647 static inline void nfs_set_verifier(struct dentry
* dentry
, unsigned long verf
)
649 dentry
->d_fsdata
= (void *)verf
;
653 * Whenever an NFS operation succeeds, we know that the dentry
654 * is valid, so we update the revalidation timestamp.
656 static inline void nfs_renew_times(struct dentry
* dentry
)
658 dentry
->d_time
= jiffies
;
662 * Return the intent data that applies to this particular path component
664 * Note that the current set of intents only apply to the very last
665 * component of the path.
666 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
668 static inline unsigned int nfs_lookup_check_intent(struct nameidata
*nd
, unsigned int mask
)
670 if (nd
->flags
& (LOOKUP_CONTINUE
|LOOKUP_PARENT
))
672 return nd
->flags
& mask
;
676 * Inode and filehandle revalidation for lookups.
678 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
679 * or if the intent information indicates that we're about to open this
680 * particular file and the "nocto" mount flag is not set.
684 int nfs_lookup_verify_inode(struct inode
*inode
, struct nameidata
*nd
)
686 struct nfs_server
*server
= NFS_SERVER(inode
);
689 /* VFS wants an on-the-wire revalidation */
690 if (nd
->flags
& LOOKUP_REVAL
)
692 /* This is an open(2) */
693 if (nfs_lookup_check_intent(nd
, LOOKUP_OPEN
) != 0 &&
694 !(server
->flags
& NFS_MOUNT_NOCTO
) &&
695 (S_ISREG(inode
->i_mode
) ||
696 S_ISDIR(inode
->i_mode
)))
699 return nfs_revalidate_inode(server
, inode
);
701 return __nfs_revalidate_inode(server
, inode
);
705 * We judge how long we want to trust negative
706 * dentries by looking at the parent inode mtime.
708 * If parent mtime has changed, we revalidate, else we wait for a
709 * period corresponding to the parent's attribute cache timeout value.
712 int nfs_neg_need_reval(struct inode
*dir
, struct dentry
*dentry
,
713 struct nameidata
*nd
)
715 /* Don't revalidate a negative dentry if we're creating a new file */
716 if (nd
!= NULL
&& nfs_lookup_check_intent(nd
, LOOKUP_CREATE
) != 0)
718 return !nfs_check_verifier(dir
, dentry
);
722 * This is called every time the dcache has a lookup hit,
723 * and we should check whether we can really trust that
726 * NOTE! The hit can be a negative hit too, don't assume
729 * If the parent directory is seen to have changed, we throw out the
730 * cached dentry and do a new lookup.
732 static int nfs_lookup_revalidate(struct dentry
* dentry
, struct nameidata
*nd
)
736 struct dentry
*parent
;
738 struct nfs_fh fhandle
;
739 struct nfs_fattr fattr
;
740 unsigned long verifier
;
742 parent
= dget_parent(dentry
);
744 dir
= parent
->d_inode
;
745 nfs_inc_stats(dir
, NFSIOS_DENTRYREVALIDATE
);
746 inode
= dentry
->d_inode
;
749 if (nfs_neg_need_reval(dir
, dentry
, nd
))
754 if (is_bad_inode(inode
)) {
755 dfprintk(LOOKUPCACHE
, "%s: %s/%s has dud inode\n",
756 __FUNCTION__
, dentry
->d_parent
->d_name
.name
,
757 dentry
->d_name
.name
);
761 /* Revalidate parent directory attribute cache */
762 if (nfs_revalidate_inode(NFS_SERVER(dir
), dir
) < 0)
765 /* Force a full look up iff the parent directory has changed */
766 if (nfs_check_verifier(dir
, dentry
)) {
767 if (nfs_lookup_verify_inode(inode
, nd
))
772 if (NFS_STALE(inode
))
775 verifier
= nfs_save_change_attribute(dir
);
776 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, &fhandle
, &fattr
);
779 if (nfs_compare_fh(NFS_FH(inode
), &fhandle
))
781 if ((error
= nfs_refresh_inode(inode
, &fattr
)) != 0)
784 nfs_renew_times(dentry
);
785 nfs_set_verifier(dentry
, verifier
);
789 dfprintk(LOOKUPCACHE
, "NFS: %s(%s/%s) is valid\n",
790 __FUNCTION__
, dentry
->d_parent
->d_name
.name
,
791 dentry
->d_name
.name
);
797 if (inode
&& S_ISDIR(inode
->i_mode
)) {
798 /* Purge readdir caches. */
799 nfs_zap_caches(inode
);
800 /* If we have submounts, don't unhash ! */
801 if (have_submounts(dentry
))
803 shrink_dcache_parent(dentry
);
808 dfprintk(LOOKUPCACHE
, "NFS: %s(%s/%s) is invalid\n",
809 __FUNCTION__
, dentry
->d_parent
->d_name
.name
,
810 dentry
->d_name
.name
);
815 * This is called from dput() when d_count is going to 0.
817 static int nfs_dentry_delete(struct dentry
*dentry
)
819 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
820 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
823 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
824 /* Unhash it, so that ->d_iput() would be called */
827 if (!(dentry
->d_sb
->s_flags
& MS_ACTIVE
)) {
828 /* Unhash it, so that ancestors of killed async unlink
829 * files will be cleaned up during umount */
837 * Called when the dentry loses inode.
838 * We use it to clean up silly-renamed files.
840 static void nfs_dentry_iput(struct dentry
*dentry
, struct inode
*inode
)
842 nfs_inode_return_delegation(inode
);
843 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
846 nfs_complete_unlink(dentry
);
849 /* When creating a negative dentry, we want to renew d_time */
850 nfs_renew_times(dentry
);
854 struct dentry_operations nfs_dentry_operations
= {
855 .d_revalidate
= nfs_lookup_revalidate
,
856 .d_delete
= nfs_dentry_delete
,
857 .d_iput
= nfs_dentry_iput
,
861 * Use intent information to check whether or not we're going to do
862 * an O_EXCL create using this path component.
865 int nfs_is_exclusive_create(struct inode
*dir
, struct nameidata
*nd
)
867 if (NFS_PROTO(dir
)->version
== 2)
869 if (nd
== NULL
|| nfs_lookup_check_intent(nd
, LOOKUP_CREATE
) == 0)
871 return (nd
->intent
.open
.flags
& O_EXCL
) != 0;
874 static inline int nfs_reval_fsid(struct vfsmount
*mnt
, struct inode
*dir
,
875 struct nfs_fh
*fh
, struct nfs_fattr
*fattr
)
877 struct nfs_server
*server
= NFS_SERVER(dir
);
879 if (!nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
880 /* Revalidate fsid on root dir */
881 return __nfs_revalidate_inode(server
, mnt
->mnt_root
->d_inode
);
885 static struct dentry
*nfs_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
888 struct inode
*inode
= NULL
;
890 struct nfs_fh fhandle
;
891 struct nfs_fattr fattr
;
893 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
894 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
895 nfs_inc_stats(dir
, NFSIOS_VFSLOOKUP
);
897 res
= ERR_PTR(-ENAMETOOLONG
);
898 if (dentry
->d_name
.len
> NFS_SERVER(dir
)->namelen
)
901 res
= ERR_PTR(-ENOMEM
);
902 dentry
->d_op
= NFS_PROTO(dir
)->dentry_ops
;
906 /* If we're doing an exclusive create, optimize away the lookup */
907 if (nfs_is_exclusive_create(dir
, nd
))
910 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, &fhandle
, &fattr
);
911 if (error
== -ENOENT
)
914 res
= ERR_PTR(error
);
917 error
= nfs_reval_fsid(nd
->mnt
, dir
, &fhandle
, &fattr
);
919 res
= ERR_PTR(error
);
922 inode
= nfs_fhget(dentry
->d_sb
, &fhandle
, &fattr
);
923 res
= (struct dentry
*)inode
;
928 res
= d_materialise_unique(dentry
, inode
);
931 nfs_renew_times(dentry
);
932 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
940 static int nfs_open_revalidate(struct dentry
*, struct nameidata
*);
942 struct dentry_operations nfs4_dentry_operations
= {
943 .d_revalidate
= nfs_open_revalidate
,
944 .d_delete
= nfs_dentry_delete
,
945 .d_iput
= nfs_dentry_iput
,
949 * Use intent information to determine whether we need to substitute
950 * the NFSv4-style stateful OPEN for the LOOKUP call
952 static int is_atomic_open(struct inode
*dir
, struct nameidata
*nd
)
954 if (nd
== NULL
|| nfs_lookup_check_intent(nd
, LOOKUP_OPEN
) == 0)
956 /* NFS does not (yet) have a stateful open for directories */
957 if (nd
->flags
& LOOKUP_DIRECTORY
)
959 /* Are we trying to write to a read only partition? */
960 if (IS_RDONLY(dir
) && (nd
->intent
.open
.flags
& (O_CREAT
|O_TRUNC
|FMODE_WRITE
)))
965 static struct dentry
*nfs_atomic_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
967 struct dentry
*res
= NULL
;
970 dfprintk(VFS
, "NFS: atomic_lookup(%s/%ld), %s\n",
971 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
973 /* Check that we are indeed trying to open this file */
974 if (!is_atomic_open(dir
, nd
))
977 if (dentry
->d_name
.len
> NFS_SERVER(dir
)->namelen
) {
978 res
= ERR_PTR(-ENAMETOOLONG
);
981 dentry
->d_op
= NFS_PROTO(dir
)->dentry_ops
;
983 /* Let vfs_create() deal with O_EXCL */
984 if (nd
->intent
.open
.flags
& O_EXCL
) {
989 /* Open the file on the server */
991 /* Revalidate parent directory attribute cache */
992 error
= nfs_revalidate_inode(NFS_SERVER(dir
), dir
);
994 res
= ERR_PTR(error
);
999 if (nd
->intent
.open
.flags
& O_CREAT
) {
1000 nfs_begin_data_update(dir
);
1001 res
= nfs4_atomic_open(dir
, dentry
, nd
);
1002 nfs_end_data_update(dir
);
1004 res
= nfs4_atomic_open(dir
, dentry
, nd
);
1007 error
= PTR_ERR(res
);
1009 /* Make a negative dentry */
1013 /* This turned out not to be a regular file */
1018 if (!(nd
->intent
.open
.flags
& O_NOFOLLOW
))
1024 } else if (res
!= NULL
)
1026 nfs_renew_times(dentry
);
1027 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1031 return nfs_lookup(dir
, dentry
, nd
);
1034 static int nfs_open_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1036 struct dentry
*parent
= NULL
;
1037 struct inode
*inode
= dentry
->d_inode
;
1039 unsigned long verifier
;
1040 int openflags
, ret
= 0;
1042 parent
= dget_parent(dentry
);
1043 dir
= parent
->d_inode
;
1044 if (!is_atomic_open(dir
, nd
))
1046 /* We can't create new files in nfs_open_revalidate(), so we
1047 * optimize away revalidation of negative dentries.
1051 /* NFS only supports OPEN on regular files */
1052 if (!S_ISREG(inode
->i_mode
))
1054 openflags
= nd
->intent
.open
.flags
;
1055 /* We cannot do exclusive creation on a positive dentry */
1056 if ((openflags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1058 /* We can't create new files, or truncate existing ones here */
1059 openflags
&= ~(O_CREAT
|O_TRUNC
);
1062 * Note: we're not holding inode->i_mutex and so may be racing with
1063 * operations that change the directory. We therefore save the
1064 * change attribute *before* we do the RPC call.
1067 verifier
= nfs_save_change_attribute(dir
);
1068 ret
= nfs4_open_revalidate(dir
, dentry
, openflags
, nd
);
1070 nfs_set_verifier(dentry
, verifier
);
1079 if (inode
!= NULL
&& nfs_have_delegation(inode
, FMODE_READ
))
1081 return nfs_lookup_revalidate(dentry
, nd
);
1083 #endif /* CONFIG_NFSV4 */
1085 static struct dentry
*nfs_readdir_lookup(nfs_readdir_descriptor_t
*desc
)
1087 struct dentry
*parent
= desc
->file
->f_dentry
;
1088 struct inode
*dir
= parent
->d_inode
;
1089 struct nfs_entry
*entry
= desc
->entry
;
1090 struct dentry
*dentry
, *alias
;
1091 struct qstr name
= {
1092 .name
= entry
->name
,
1095 struct inode
*inode
;
1099 if (name
.name
[0] == '.' && name
.name
[1] == '.')
1100 return dget_parent(parent
);
1103 if (name
.name
[0] == '.')
1104 return dget(parent
);
1106 name
.hash
= full_name_hash(name
.name
, name
.len
);
1107 dentry
= d_lookup(parent
, &name
);
1110 if (!desc
->plus
|| !(entry
->fattr
->valid
& NFS_ATTR_FATTR
))
1112 /* Note: caller is already holding the dir->i_mutex! */
1113 dentry
= d_alloc(parent
, &name
);
1116 dentry
->d_op
= NFS_PROTO(dir
)->dentry_ops
;
1117 inode
= nfs_fhget(dentry
->d_sb
, entry
->fh
, entry
->fattr
);
1118 if (IS_ERR(inode
)) {
1123 alias
= d_materialise_unique(dentry
, inode
);
1124 if (alias
!= NULL
) {
1129 nfs_renew_times(dentry
);
1130 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1135 * Code common to create, mkdir, and mknod.
1137 int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
1138 struct nfs_fattr
*fattr
)
1140 struct inode
*inode
;
1141 int error
= -EACCES
;
1143 /* We may have been initialized further down */
1144 if (dentry
->d_inode
)
1146 if (fhandle
->size
== 0) {
1147 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1148 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, fhandle
, fattr
);
1152 if (!(fattr
->valid
& NFS_ATTR_FATTR
)) {
1153 struct nfs_server
*server
= NFS_SB(dentry
->d_sb
);
1154 error
= server
->nfs_client
->rpc_ops
->getattr(server
, fhandle
, fattr
);
1158 inode
= nfs_fhget(dentry
->d_sb
, fhandle
, fattr
);
1159 error
= PTR_ERR(inode
);
1162 d_instantiate(dentry
, inode
);
1170 * Following a failed create operation, we drop the dentry rather
1171 * than retain a negative dentry. This avoids a problem in the event
1172 * that the operation succeeded on the server, but an error in the
1173 * reply path made it appear to have failed.
1175 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1176 struct nameidata
*nd
)
1182 dfprintk(VFS
, "NFS: create(%s/%ld), %s\n",
1183 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1185 attr
.ia_mode
= mode
;
1186 attr
.ia_valid
= ATTR_MODE
;
1188 if (nd
&& (nd
->flags
& LOOKUP_CREATE
))
1189 open_flags
= nd
->intent
.open
.flags
;
1192 nfs_begin_data_update(dir
);
1193 error
= NFS_PROTO(dir
)->create(dir
, dentry
, &attr
, open_flags
, nd
);
1194 nfs_end_data_update(dir
);
1197 nfs_renew_times(dentry
);
1198 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1208 * See comments for nfs_proc_create regarding failed operations.
1211 nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
1216 dfprintk(VFS
, "NFS: mknod(%s/%ld), %s\n",
1217 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1219 if (!new_valid_dev(rdev
))
1222 attr
.ia_mode
= mode
;
1223 attr
.ia_valid
= ATTR_MODE
;
1226 nfs_begin_data_update(dir
);
1227 status
= NFS_PROTO(dir
)->mknod(dir
, dentry
, &attr
, rdev
);
1228 nfs_end_data_update(dir
);
1231 nfs_renew_times(dentry
);
1232 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1242 * See comments for nfs_proc_create regarding failed operations.
1244 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1249 dfprintk(VFS
, "NFS: mkdir(%s/%ld), %s\n",
1250 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1252 attr
.ia_valid
= ATTR_MODE
;
1253 attr
.ia_mode
= mode
| S_IFDIR
;
1256 nfs_begin_data_update(dir
);
1257 error
= NFS_PROTO(dir
)->mkdir(dir
, dentry
, &attr
);
1258 nfs_end_data_update(dir
);
1261 nfs_renew_times(dentry
);
1262 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1271 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1275 dfprintk(VFS
, "NFS: rmdir(%s/%ld), %s\n",
1276 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1279 nfs_begin_data_update(dir
);
1280 error
= NFS_PROTO(dir
)->rmdir(dir
, &dentry
->d_name
);
1281 /* Ensure the VFS deletes this inode */
1282 if (error
== 0 && dentry
->d_inode
!= NULL
)
1283 dentry
->d_inode
->i_nlink
= 0;
1284 nfs_end_data_update(dir
);
1290 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
1292 static unsigned int sillycounter
;
1293 const int i_inosize
= sizeof(dir
->i_ino
)*2;
1294 const int countersize
= sizeof(sillycounter
)*2;
1295 const int slen
= sizeof(".nfs") + i_inosize
+ countersize
- 1;
1298 struct dentry
*sdentry
;
1301 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
1302 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
1303 atomic_read(&dentry
->d_count
));
1304 nfs_inc_stats(dir
, NFSIOS_SILLYRENAME
);
1307 if (!dentry
->d_inode
)
1308 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
1309 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1312 * We don't allow a dentry to be silly-renamed twice.
1315 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1318 sprintf(silly
, ".nfs%*.*lx",
1319 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
1321 /* Return delegation in anticipation of the rename */
1322 nfs_inode_return_delegation(dentry
->d_inode
);
1326 char *suffix
= silly
+ slen
- countersize
;
1330 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
1332 dfprintk(VFS
, "NFS: trying to rename %s to %s\n",
1333 dentry
->d_name
.name
, silly
);
1335 sdentry
= lookup_one_len(silly
, dentry
->d_parent
, slen
);
1337 * N.B. Better to return EBUSY here ... it could be
1338 * dangerous to delete the file while it's in use.
1340 if (IS_ERR(sdentry
))
1342 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
1344 qsilly
.name
= silly
;
1345 qsilly
.len
= strlen(silly
);
1346 nfs_begin_data_update(dir
);
1347 if (dentry
->d_inode
) {
1348 nfs_begin_data_update(dentry
->d_inode
);
1349 error
= NFS_PROTO(dir
)->rename(dir
, &dentry
->d_name
,
1351 nfs_mark_for_revalidate(dentry
->d_inode
);
1352 nfs_end_data_update(dentry
->d_inode
);
1354 error
= NFS_PROTO(dir
)->rename(dir
, &dentry
->d_name
,
1356 nfs_end_data_update(dir
);
1358 nfs_renew_times(dentry
);
1359 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1360 d_move(dentry
, sdentry
);
1361 error
= nfs_async_unlink(dentry
);
1362 /* If we return 0 we don't unlink */
1370 * Remove a file after making sure there are no pending writes,
1371 * and after checking that the file has only one user.
1373 * We invalidate the attribute cache and free the inode prior to the operation
1374 * to avoid possible races if the server reuses the inode.
1376 static int nfs_safe_remove(struct dentry
*dentry
)
1378 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1379 struct inode
*inode
= dentry
->d_inode
;
1382 dfprintk(VFS
, "NFS: safe_remove(%s/%s)\n",
1383 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1385 /* If the dentry was sillyrenamed, we simply call d_delete() */
1386 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
1391 nfs_begin_data_update(dir
);
1392 if (inode
!= NULL
) {
1393 nfs_inode_return_delegation(inode
);
1394 nfs_begin_data_update(inode
);
1395 error
= NFS_PROTO(dir
)->remove(dir
, &dentry
->d_name
);
1396 /* The VFS may want to delete this inode */
1399 nfs_mark_for_revalidate(inode
);
1400 nfs_end_data_update(inode
);
1402 error
= NFS_PROTO(dir
)->remove(dir
, &dentry
->d_name
);
1403 nfs_end_data_update(dir
);
1408 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1409 * belongs to an active ".nfs..." file and we return -EBUSY.
1411 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1413 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1416 int need_rehash
= 0;
1418 dfprintk(VFS
, "NFS: unlink(%s/%ld, %s)\n", dir
->i_sb
->s_id
,
1419 dir
->i_ino
, dentry
->d_name
.name
);
1422 spin_lock(&dcache_lock
);
1423 spin_lock(&dentry
->d_lock
);
1424 if (atomic_read(&dentry
->d_count
) > 1) {
1425 spin_unlock(&dentry
->d_lock
);
1426 spin_unlock(&dcache_lock
);
1427 error
= nfs_sillyrename(dir
, dentry
);
1431 if (!d_unhashed(dentry
)) {
1435 spin_unlock(&dentry
->d_lock
);
1436 spin_unlock(&dcache_lock
);
1437 error
= nfs_safe_remove(dentry
);
1439 nfs_renew_times(dentry
);
1440 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1441 } else if (need_rehash
)
1448 nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1451 struct nfs_fattr sym_attr
;
1452 struct nfs_fh sym_fh
;
1453 struct qstr qsymname
;
1456 dfprintk(VFS
, "NFS: symlink(%s/%ld, %s, %s)\n", dir
->i_sb
->s_id
,
1457 dir
->i_ino
, dentry
->d_name
.name
, symname
);
1460 if (dentry
->d_inode
)
1461 printk("nfs_proc_symlink: %s/%s not negative!\n",
1462 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1465 * Fill in the sattr for the call.
1466 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1468 attr
.ia_valid
= ATTR_MODE
;
1469 attr
.ia_mode
= S_IFLNK
| S_IRWXUGO
;
1471 qsymname
.name
= symname
;
1472 qsymname
.len
= strlen(symname
);
1475 nfs_begin_data_update(dir
);
1476 error
= NFS_PROTO(dir
)->symlink(dir
, &dentry
->d_name
, &qsymname
,
1477 &attr
, &sym_fh
, &sym_attr
);
1478 nfs_end_data_update(dir
);
1480 error
= nfs_instantiate(dentry
, &sym_fh
, &sym_attr
);
1488 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
1490 struct inode
*inode
= old_dentry
->d_inode
;
1493 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
1494 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1495 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1498 nfs_begin_data_update(dir
);
1499 nfs_begin_data_update(inode
);
1500 error
= NFS_PROTO(dir
)->link(inode
, dir
, &dentry
->d_name
);
1502 atomic_inc(&inode
->i_count
);
1503 d_instantiate(dentry
, inode
);
1505 nfs_end_data_update(inode
);
1506 nfs_end_data_update(dir
);
1513 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1514 * different file handle for the same inode after a rename (e.g. when
1515 * moving to a different directory). A fail-safe method to do so would
1516 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1517 * rename the old file using the sillyrename stuff. This way, the original
1518 * file in old_dir will go away when the last process iput()s the inode.
1522 * It actually works quite well. One needs to have the possibility for
1523 * at least one ".nfs..." file in each directory the file ever gets
1524 * moved or linked to which happens automagically with the new
1525 * implementation that only depends on the dcache stuff instead of
1526 * using the inode layer
1528 * Unfortunately, things are a little more complicated than indicated
1529 * above. For a cross-directory move, we want to make sure we can get
1530 * rid of the old inode after the operation. This means there must be
1531 * no pending writes (if it's a file), and the use count must be 1.
1532 * If these conditions are met, we can drop the dentries before doing
1535 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1536 struct inode
*new_dir
, struct dentry
*new_dentry
)
1538 struct inode
*old_inode
= old_dentry
->d_inode
;
1539 struct inode
*new_inode
= new_dentry
->d_inode
;
1540 struct dentry
*dentry
= NULL
, *rehash
= NULL
;
1544 * To prevent any new references to the target during the rename,
1545 * we unhash the dentry and free the inode in advance.
1548 if (!d_unhashed(new_dentry
)) {
1550 rehash
= new_dentry
;
1553 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1554 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1555 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1556 atomic_read(&new_dentry
->d_count
));
1559 * First check whether the target is busy ... we can't
1560 * safely do _any_ rename if the target is in use.
1562 * For files, make a copy of the dentry and then do a
1563 * silly-rename. If the silly-rename succeeds, the
1564 * copied dentry is hashed and becomes the new target.
1568 if (S_ISDIR(new_inode
->i_mode
)) {
1570 if (!S_ISDIR(old_inode
->i_mode
))
1572 } else if (atomic_read(&new_dentry
->d_count
) > 2) {
1574 /* copy the target dentry's name */
1575 dentry
= d_alloc(new_dentry
->d_parent
,
1576 &new_dentry
->d_name
);
1580 /* silly-rename the existing target ... */
1581 err
= nfs_sillyrename(new_dir
, new_dentry
);
1583 new_dentry
= rehash
= dentry
;
1585 /* instantiate the replacement target */
1586 d_instantiate(new_dentry
, NULL
);
1587 } else if (atomic_read(&new_dentry
->d_count
) > 1) {
1588 /* dentry still busy? */
1590 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1591 new_dentry
->d_parent
->d_name
.name
,
1592 new_dentry
->d_name
.name
,
1593 atomic_read(&new_dentry
->d_count
));
1598 new_inode
->i_nlink
--;
1602 * ... prune child dentries and writebacks if needed.
1604 if (atomic_read(&old_dentry
->d_count
) > 1) {
1605 nfs_wb_all(old_inode
);
1606 shrink_dcache_parent(old_dentry
);
1608 nfs_inode_return_delegation(old_inode
);
1610 if (new_inode
!= NULL
) {
1611 nfs_inode_return_delegation(new_inode
);
1612 d_delete(new_dentry
);
1615 nfs_begin_data_update(old_dir
);
1616 nfs_begin_data_update(new_dir
);
1617 nfs_begin_data_update(old_inode
);
1618 error
= NFS_PROTO(old_dir
)->rename(old_dir
, &old_dentry
->d_name
,
1619 new_dir
, &new_dentry
->d_name
);
1620 nfs_mark_for_revalidate(old_inode
);
1621 nfs_end_data_update(old_inode
);
1622 nfs_end_data_update(new_dir
);
1623 nfs_end_data_update(old_dir
);
1628 if (!S_ISDIR(old_inode
->i_mode
))
1629 d_move(old_dentry
, new_dentry
);
1630 nfs_renew_times(new_dentry
);
1631 nfs_set_verifier(new_dentry
, nfs_save_change_attribute(new_dir
));
1634 /* new dentry created? */
1641 static DEFINE_SPINLOCK(nfs_access_lru_lock
);
1642 static LIST_HEAD(nfs_access_lru_list
);
1643 static atomic_long_t nfs_access_nr_entries
;
1645 static void nfs_access_free_entry(struct nfs_access_entry
*entry
)
1647 put_rpccred(entry
->cred
);
1649 smp_mb__before_atomic_dec();
1650 atomic_long_dec(&nfs_access_nr_entries
);
1651 smp_mb__after_atomic_dec();
1654 int nfs_access_cache_shrinker(int nr_to_scan
, gfp_t gfp_mask
)
1657 struct nfs_inode
*nfsi
;
1658 struct nfs_access_entry
*cache
;
1660 spin_lock(&nfs_access_lru_lock
);
1662 list_for_each_entry(nfsi
, &nfs_access_lru_list
, access_cache_inode_lru
) {
1663 struct inode
*inode
;
1665 if (nr_to_scan
-- == 0)
1667 inode
= igrab(&nfsi
->vfs_inode
);
1670 spin_lock(&inode
->i_lock
);
1671 if (list_empty(&nfsi
->access_cache_entry_lru
))
1672 goto remove_lru_entry
;
1673 cache
= list_entry(nfsi
->access_cache_entry_lru
.next
,
1674 struct nfs_access_entry
, lru
);
1675 list_move(&cache
->lru
, &head
);
1676 rb_erase(&cache
->rb_node
, &nfsi
->access_cache
);
1677 if (!list_empty(&nfsi
->access_cache_entry_lru
))
1678 list_move_tail(&nfsi
->access_cache_inode_lru
,
1679 &nfs_access_lru_list
);
1682 list_del_init(&nfsi
->access_cache_inode_lru
);
1683 clear_bit(NFS_INO_ACL_LRU_SET
, &nfsi
->flags
);
1685 spin_unlock(&inode
->i_lock
);
1689 spin_unlock(&nfs_access_lru_lock
);
1690 while (!list_empty(&head
)) {
1691 cache
= list_entry(head
.next
, struct nfs_access_entry
, lru
);
1692 list_del(&cache
->lru
);
1693 nfs_access_free_entry(cache
);
1695 return (atomic_long_read(&nfs_access_nr_entries
) / 100) * sysctl_vfs_cache_pressure
;
1698 static void __nfs_access_zap_cache(struct inode
*inode
)
1700 struct nfs_inode
*nfsi
= NFS_I(inode
);
1701 struct rb_root
*root_node
= &nfsi
->access_cache
;
1702 struct rb_node
*n
, *dispose
= NULL
;
1703 struct nfs_access_entry
*entry
;
1705 /* Unhook entries from the cache */
1706 while ((n
= rb_first(root_node
)) != NULL
) {
1707 entry
= rb_entry(n
, struct nfs_access_entry
, rb_node
);
1708 rb_erase(n
, root_node
);
1709 list_del(&entry
->lru
);
1710 n
->rb_left
= dispose
;
1713 nfsi
->cache_validity
&= ~NFS_INO_INVALID_ACCESS
;
1714 spin_unlock(&inode
->i_lock
);
1716 /* Now kill them all! */
1717 while (dispose
!= NULL
) {
1719 dispose
= n
->rb_left
;
1720 nfs_access_free_entry(rb_entry(n
, struct nfs_access_entry
, rb_node
));
1724 void nfs_access_zap_cache(struct inode
*inode
)
1726 /* Remove from global LRU init */
1727 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET
, &NFS_FLAGS(inode
))) {
1728 spin_lock(&nfs_access_lru_lock
);
1729 list_del_init(&NFS_I(inode
)->access_cache_inode_lru
);
1730 spin_unlock(&nfs_access_lru_lock
);
1733 spin_lock(&inode
->i_lock
);
1734 /* This will release the spinlock */
1735 __nfs_access_zap_cache(inode
);
1738 static struct nfs_access_entry
*nfs_access_search_rbtree(struct inode
*inode
, struct rpc_cred
*cred
)
1740 struct rb_node
*n
= NFS_I(inode
)->access_cache
.rb_node
;
1741 struct nfs_access_entry
*entry
;
1744 entry
= rb_entry(n
, struct nfs_access_entry
, rb_node
);
1746 if (cred
< entry
->cred
)
1748 else if (cred
> entry
->cred
)
1756 int nfs_access_get_cached(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_access_entry
*res
)
1758 struct nfs_inode
*nfsi
= NFS_I(inode
);
1759 struct nfs_access_entry
*cache
;
1762 spin_lock(&inode
->i_lock
);
1763 if (nfsi
->cache_validity
& NFS_INO_INVALID_ACCESS
)
1765 cache
= nfs_access_search_rbtree(inode
, cred
);
1768 if (time_after(jiffies
, cache
->jiffies
+ NFS_ATTRTIMEO(inode
)))
1770 res
->jiffies
= cache
->jiffies
;
1771 res
->cred
= cache
->cred
;
1772 res
->mask
= cache
->mask
;
1773 list_move_tail(&cache
->lru
, &nfsi
->access_cache_entry_lru
);
1776 spin_unlock(&inode
->i_lock
);
1779 rb_erase(&cache
->rb_node
, &nfsi
->access_cache
);
1780 list_del(&cache
->lru
);
1781 spin_unlock(&inode
->i_lock
);
1782 nfs_access_free_entry(cache
);
1785 /* This will release the spinlock */
1786 __nfs_access_zap_cache(inode
);
1790 static void nfs_access_add_rbtree(struct inode
*inode
, struct nfs_access_entry
*set
)
1792 struct nfs_inode
*nfsi
= NFS_I(inode
);
1793 struct rb_root
*root_node
= &nfsi
->access_cache
;
1794 struct rb_node
**p
= &root_node
->rb_node
;
1795 struct rb_node
*parent
= NULL
;
1796 struct nfs_access_entry
*entry
;
1798 spin_lock(&inode
->i_lock
);
1799 while (*p
!= NULL
) {
1801 entry
= rb_entry(parent
, struct nfs_access_entry
, rb_node
);
1803 if (set
->cred
< entry
->cred
)
1804 p
= &parent
->rb_left
;
1805 else if (set
->cred
> entry
->cred
)
1806 p
= &parent
->rb_right
;
1810 rb_link_node(&set
->rb_node
, parent
, p
);
1811 rb_insert_color(&set
->rb_node
, root_node
);
1812 list_add_tail(&set
->lru
, &nfsi
->access_cache_entry_lru
);
1813 spin_unlock(&inode
->i_lock
);
1816 rb_replace_node(parent
, &set
->rb_node
, root_node
);
1817 list_add_tail(&set
->lru
, &nfsi
->access_cache_entry_lru
);
1818 list_del(&entry
->lru
);
1819 spin_unlock(&inode
->i_lock
);
1820 nfs_access_free_entry(entry
);
1823 void nfs_access_add_cache(struct inode
*inode
, struct nfs_access_entry
*set
)
1825 struct nfs_access_entry
*cache
= kmalloc(sizeof(*cache
), GFP_KERNEL
);
1828 RB_CLEAR_NODE(&cache
->rb_node
);
1829 cache
->jiffies
= set
->jiffies
;
1830 cache
->cred
= get_rpccred(set
->cred
);
1831 cache
->mask
= set
->mask
;
1833 nfs_access_add_rbtree(inode
, cache
);
1835 /* Update accounting */
1836 smp_mb__before_atomic_inc();
1837 atomic_long_inc(&nfs_access_nr_entries
);
1838 smp_mb__after_atomic_inc();
1840 /* Add inode to global LRU list */
1841 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET
, &NFS_FLAGS(inode
))) {
1842 spin_lock(&nfs_access_lru_lock
);
1843 list_add_tail(&NFS_I(inode
)->access_cache_inode_lru
, &nfs_access_lru_list
);
1844 spin_unlock(&nfs_access_lru_lock
);
1848 static int nfs_do_access(struct inode
*inode
, struct rpc_cred
*cred
, int mask
)
1850 struct nfs_access_entry cache
;
1853 status
= nfs_access_get_cached(inode
, cred
, &cache
);
1857 /* Be clever: ask server to check for all possible rights */
1858 cache
.mask
= MAY_EXEC
| MAY_WRITE
| MAY_READ
;
1860 cache
.jiffies
= jiffies
;
1861 status
= NFS_PROTO(inode
)->access(inode
, &cache
);
1864 nfs_access_add_cache(inode
, &cache
);
1866 if ((cache
.mask
& mask
) == mask
)
1871 int nfs_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
1873 struct rpc_cred
*cred
;
1876 nfs_inc_stats(inode
, NFSIOS_VFSACCESS
);
1880 /* Is this sys_access() ? */
1881 if (nd
!= NULL
&& (nd
->flags
& LOOKUP_ACCESS
))
1884 switch (inode
->i_mode
& S_IFMT
) {
1888 /* NFSv4 has atomic_open... */
1889 if (nfs_server_capable(inode
, NFS_CAP_ATOMIC_OPEN
)
1891 && (nd
->flags
& LOOKUP_OPEN
))
1896 * Optimize away all write operations, since the server
1897 * will check permissions when we perform the op.
1899 if ((mask
& MAY_WRITE
) && !(mask
& MAY_READ
))
1906 if (!NFS_PROTO(inode
)->access
)
1909 cred
= rpcauth_lookupcred(NFS_CLIENT(inode
)->cl_auth
, 0);
1910 if (!IS_ERR(cred
)) {
1911 res
= nfs_do_access(inode
, cred
, mask
);
1914 res
= PTR_ERR(cred
);
1917 dfprintk(VFS
, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
1918 inode
->i_sb
->s_id
, inode
->i_ino
, mask
, res
);
1921 res
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
1923 res
= generic_permission(inode
, mask
, NULL
);
1930 * version-control: t
1931 * kept-new-versions: 5