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/pagevec.h>
33 #include <linux/namei.h>
34 #include <linux/mount.h>
35 #include <linux/sched.h>
38 #include "delegation.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);
60 const struct file_operations nfs_dir_operations
= {
61 .llseek
= nfs_llseek_dir
,
62 .read
= generic_read_dir
,
63 .readdir
= nfs_readdir
,
65 .release
= nfs_release
,
66 .fsync
= nfs_fsync_dir
,
69 const struct inode_operations nfs_dir_inode_operations
= {
74 .symlink
= nfs_symlink
,
79 .permission
= nfs_permission
,
80 .getattr
= nfs_getattr
,
81 .setattr
= nfs_setattr
,
85 const struct inode_operations nfs3_dir_inode_operations
= {
90 .symlink
= nfs_symlink
,
95 .permission
= nfs_permission
,
96 .getattr
= nfs_getattr
,
97 .setattr
= nfs_setattr
,
98 .listxattr
= nfs3_listxattr
,
99 .getxattr
= nfs3_getxattr
,
100 .setxattr
= nfs3_setxattr
,
101 .removexattr
= nfs3_removexattr
,
103 #endif /* CONFIG_NFS_V3 */
107 static struct dentry
*nfs_atomic_lookup(struct inode
*, struct dentry
*, struct nameidata
*);
108 const struct inode_operations nfs4_dir_inode_operations
= {
109 .create
= nfs_create
,
110 .lookup
= nfs_atomic_lookup
,
112 .unlink
= nfs_unlink
,
113 .symlink
= nfs_symlink
,
117 .rename
= nfs_rename
,
118 .permission
= nfs_permission
,
119 .getattr
= nfs_getattr
,
120 .setattr
= nfs_setattr
,
121 .getxattr
= nfs4_getxattr
,
122 .setxattr
= nfs4_setxattr
,
123 .listxattr
= nfs4_listxattr
,
126 #endif /* CONFIG_NFS_V4 */
132 nfs_opendir(struct inode
*inode
, struct file
*filp
)
136 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
137 filp
->f_path
.dentry
->d_parent
->d_name
.name
,
138 filp
->f_path
.dentry
->d_name
.name
);
140 nfs_inc_stats(inode
, NFSIOS_VFSOPEN
);
142 /* Call generic open code in order to cache credentials */
143 res
= nfs_open(inode
, filp
);
144 if (filp
->f_path
.dentry
== filp
->f_path
.mnt
->mnt_root
) {
145 /* This is a mountpoint, so d_revalidate will never
146 * have been called, so we need to refresh the
147 * inode (for close-open consistency) ourselves.
149 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
154 typedef __be32
* (*decode_dirent_t
)(__be32
*, struct nfs_entry
*, int);
158 unsigned long page_index
;
161 loff_t current_index
;
162 struct nfs_entry
*entry
;
163 decode_dirent_t decode
;
165 unsigned long timestamp
;
166 unsigned long gencount
;
168 } nfs_readdir_descriptor_t
;
170 /* Now we cache directories properly, by stuffing the dirent
171 * data directly in the page cache.
173 * Inode invalidation due to refresh etc. takes care of
174 * _everything_, no sloppy entry flushing logic, no extraneous
175 * copying, network direct to page cache, the way it was meant
178 * NOTE: Dirent information verification is done always by the
179 * page-in of the RPC reply, nowhere else, this simplies
180 * things substantially.
183 int nfs_readdir_filler(nfs_readdir_descriptor_t
*desc
, struct page
*page
)
185 struct file
*file
= desc
->file
;
186 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
187 struct rpc_cred
*cred
= nfs_file_cred(file
);
188 unsigned long timestamp
, gencount
;
191 dfprintk(DIRCACHE
, "NFS: %s: reading cookie %Lu into page %lu\n",
192 __func__
, (long long)desc
->entry
->cookie
,
197 gencount
= nfs_inc_attr_generation_counter();
198 error
= NFS_PROTO(inode
)->readdir(file
->f_path
.dentry
, cred
, desc
->entry
->cookie
, page
,
199 NFS_SERVER(inode
)->dtsize
, desc
->plus
);
201 /* We requested READDIRPLUS, but the server doesn't grok it */
202 if (error
== -ENOTSUPP
&& desc
->plus
) {
203 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_READDIRPLUS
;
204 clear_bit(NFS_INO_ADVISE_RDPLUS
, &NFS_I(inode
)->flags
);
210 desc
->timestamp
= timestamp
;
211 desc
->gencount
= gencount
;
212 desc
->timestamp_valid
= 1;
213 SetPageUptodate(page
);
214 /* Ensure consistent page alignment of the data.
215 * Note: assumes we have exclusive access to this mapping either
216 * through inode->i_mutex or some other mechanism.
218 if (invalidate_inode_pages2_range(inode
->i_mapping
, page
->index
+ 1, -1) < 0) {
219 /* Should never happen */
220 nfs_zap_mapping(inode
, inode
->i_mapping
);
230 int dir_decode(nfs_readdir_descriptor_t
*desc
)
232 __be32
*p
= desc
->ptr
;
233 p
= desc
->decode(p
, desc
->entry
, desc
->plus
);
237 if (desc
->timestamp_valid
) {
238 desc
->entry
->fattr
->time_start
= desc
->timestamp
;
239 desc
->entry
->fattr
->gencount
= desc
->gencount
;
241 desc
->entry
->fattr
->valid
&= ~NFS_ATTR_FATTR
;
246 void dir_page_release(nfs_readdir_descriptor_t
*desc
)
249 page_cache_release(desc
->page
);
255 * Given a pointer to a buffer that has already been filled by a call
256 * to readdir, find the next entry with cookie '*desc->dir_cookie'.
258 * If the end of the buffer has been reached, return -EAGAIN, if not,
259 * return the offset within the buffer of the next entry to be
263 int find_dirent(nfs_readdir_descriptor_t
*desc
)
265 struct nfs_entry
*entry
= desc
->entry
;
269 while((status
= dir_decode(desc
)) == 0) {
270 dfprintk(DIRCACHE
, "NFS: %s: examining cookie %Lu\n",
271 __func__
, (unsigned long long)entry
->cookie
);
272 if (entry
->prev_cookie
== *desc
->dir_cookie
)
274 if (loop_count
++ > 200) {
283 * Given a pointer to a buffer that has already been filled by a call
284 * to readdir, find the entry at offset 'desc->file->f_pos'.
286 * If the end of the buffer has been reached, return -EAGAIN, if not,
287 * return the offset within the buffer of the next entry to be
291 int find_dirent_index(nfs_readdir_descriptor_t
*desc
)
293 struct nfs_entry
*entry
= desc
->entry
;
298 status
= dir_decode(desc
);
302 dfprintk(DIRCACHE
, "NFS: found cookie %Lu at index %Ld\n",
303 (unsigned long long)entry
->cookie
, desc
->current_index
);
305 if (desc
->file
->f_pos
== desc
->current_index
) {
306 *desc
->dir_cookie
= entry
->cookie
;
309 desc
->current_index
++;
310 if (loop_count
++ > 200) {
319 * Find the given page, and call find_dirent() or find_dirent_index in
320 * order to try to return the next entry.
323 int find_dirent_page(nfs_readdir_descriptor_t
*desc
)
325 struct inode
*inode
= desc
->file
->f_path
.dentry
->d_inode
;
329 dfprintk(DIRCACHE
, "NFS: %s: searching page %ld for target %Lu\n",
330 __func__
, desc
->page_index
,
331 (long long) *desc
->dir_cookie
);
333 /* If we find the page in the page_cache, we cannot be sure
334 * how fresh the data is, so we will ignore readdir_plus attributes.
336 desc
->timestamp_valid
= 0;
337 page
= read_cache_page(inode
->i_mapping
, desc
->page_index
,
338 (filler_t
*)nfs_readdir_filler
, desc
);
340 status
= PTR_ERR(page
);
344 /* NOTE: Someone else may have changed the READDIRPLUS flag */
346 desc
->ptr
= kmap(page
); /* matching kunmap in nfs_do_filldir */
347 if (*desc
->dir_cookie
!= 0)
348 status
= find_dirent(desc
);
350 status
= find_dirent_index(desc
);
352 dir_page_release(desc
);
354 dfprintk(DIRCACHE
, "NFS: %s: returns %d\n", __func__
, status
);
359 * Recurse through the page cache pages, and return a
360 * filled nfs_entry structure of the next directory entry if possible.
362 * The target for the search is '*desc->dir_cookie' if non-0,
363 * 'desc->file->f_pos' otherwise
366 int readdir_search_pagecache(nfs_readdir_descriptor_t
*desc
)
371 /* Always search-by-index from the beginning of the cache */
372 if (*desc
->dir_cookie
== 0) {
373 dfprintk(DIRCACHE
, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
374 (long long)desc
->file
->f_pos
);
375 desc
->page_index
= 0;
376 desc
->entry
->cookie
= desc
->entry
->prev_cookie
= 0;
377 desc
->entry
->eof
= 0;
378 desc
->current_index
= 0;
380 dfprintk(DIRCACHE
, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
381 (unsigned long long)*desc
->dir_cookie
);
384 res
= find_dirent_page(desc
);
387 /* Align to beginning of next page */
389 if (loop_count
++ > 200) {
395 dfprintk(DIRCACHE
, "NFS: %s: returns %d\n", __func__
, res
);
399 static inline unsigned int dt_type(struct inode
*inode
)
401 return (inode
->i_mode
>> 12) & 15;
404 static struct dentry
*nfs_readdir_lookup(nfs_readdir_descriptor_t
*desc
);
407 * Once we've found the start of the dirent within a page: fill 'er up...
410 int nfs_do_filldir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
413 struct file
*file
= desc
->file
;
414 struct nfs_entry
*entry
= desc
->entry
;
415 struct dentry
*dentry
= NULL
;
420 dfprintk(DIRCACHE
, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
421 (unsigned long long)entry
->cookie
);
424 unsigned d_type
= DT_UNKNOWN
;
425 /* Note: entry->prev_cookie contains the cookie for
426 * retrieving the current dirent on the server */
429 /* Get a dentry if we have one */
432 dentry
= nfs_readdir_lookup(desc
);
434 /* Use readdirplus info */
435 if (dentry
!= NULL
&& dentry
->d_inode
!= NULL
) {
436 d_type
= dt_type(dentry
->d_inode
);
437 fileid
= NFS_FILEID(dentry
->d_inode
);
440 res
= filldir(dirent
, entry
->name
, entry
->len
,
441 file
->f_pos
, nfs_compat_user_ino64(fileid
),
446 *desc
->dir_cookie
= entry
->cookie
;
447 if (dir_decode(desc
) != 0) {
451 if (loop_count
++ > 200) {
456 dir_page_release(desc
);
459 dfprintk(DIRCACHE
, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
460 (unsigned long long)*desc
->dir_cookie
, res
);
465 * If we cannot find a cookie in our cache, we suspect that this is
466 * because it points to a deleted file, so we ask the server to return
467 * whatever it thinks is the next entry. We then feed this to filldir.
468 * If all goes well, we should then be able to find our way round the
469 * cache on the next call to readdir_search_pagecache();
471 * NOTE: we cannot add the anonymous page to the pagecache because
472 * the data it contains might not be page aligned. Besides,
473 * we should already have a complete representation of the
474 * directory in the page cache by the time we get here.
477 int uncached_readdir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
480 struct file
*file
= desc
->file
;
481 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
482 struct rpc_cred
*cred
= nfs_file_cred(file
);
483 struct page
*page
= NULL
;
485 unsigned long timestamp
, gencount
;
487 dfprintk(DIRCACHE
, "NFS: uncached_readdir() searching for cookie %Lu\n",
488 (unsigned long long)*desc
->dir_cookie
);
490 page
= alloc_page(GFP_HIGHUSER
);
496 gencount
= nfs_inc_attr_generation_counter();
497 status
= NFS_PROTO(inode
)->readdir(file
->f_path
.dentry
, cred
,
498 *desc
->dir_cookie
, page
,
499 NFS_SERVER(inode
)->dtsize
,
502 desc
->ptr
= kmap(page
); /* matching kunmap in nfs_do_filldir */
504 desc
->timestamp
= timestamp
;
505 desc
->gencount
= gencount
;
506 desc
->timestamp_valid
= 1;
507 if ((status
= dir_decode(desc
)) == 0)
508 desc
->entry
->prev_cookie
= *desc
->dir_cookie
;
514 status
= nfs_do_filldir(desc
, dirent
, filldir
);
516 /* Reset read descriptor so it searches the page cache from
517 * the start upon the next call to readdir_search_pagecache() */
518 desc
->page_index
= 0;
519 desc
->entry
->cookie
= desc
->entry
->prev_cookie
= 0;
520 desc
->entry
->eof
= 0;
522 dfprintk(DIRCACHE
, "NFS: %s: returns %d\n",
526 dir_page_release(desc
);
530 /* The file offset position represents the dirent entry number. A
531 last cookie cache takes care of the common case of reading the
534 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
536 struct dentry
*dentry
= filp
->f_path
.dentry
;
537 struct inode
*inode
= dentry
->d_inode
;
538 nfs_readdir_descriptor_t my_desc
,
540 struct nfs_entry my_entry
;
543 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
544 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
545 (long long)filp
->f_pos
);
546 nfs_inc_stats(inode
, NFSIOS_VFSGETDENTS
);
549 * filp->f_pos points to the dirent entry number.
550 * *desc->dir_cookie has the cookie for the next entry. We have
551 * to either find the entry with the appropriate number or
552 * revalidate the cookie.
554 memset(desc
, 0, sizeof(*desc
));
557 desc
->dir_cookie
= &nfs_file_open_context(filp
)->dir_cookie
;
558 desc
->decode
= NFS_PROTO(inode
)->decode_dirent
;
559 desc
->plus
= NFS_USE_READDIRPLUS(inode
);
561 my_entry
.cookie
= my_entry
.prev_cookie
= 0;
563 my_entry
.fh
= nfs_alloc_fhandle();
564 my_entry
.fattr
= nfs_alloc_fattr();
565 if (my_entry
.fh
== NULL
|| my_entry
.fattr
== NULL
)
566 goto out_alloc_failed
;
568 desc
->entry
= &my_entry
;
570 nfs_block_sillyrename(dentry
);
571 res
= nfs_revalidate_mapping(inode
, filp
->f_mapping
);
575 while(!desc
->entry
->eof
) {
576 res
= readdir_search_pagecache(desc
);
578 if (res
== -EBADCOOKIE
) {
579 /* This means either end of directory */
580 if (*desc
->dir_cookie
&& desc
->entry
->cookie
!= *desc
->dir_cookie
) {
581 /* Or that the server has 'lost' a cookie */
582 res
= uncached_readdir(desc
, dirent
, filldir
);
589 if (res
== -ETOOSMALL
&& desc
->plus
) {
590 clear_bit(NFS_INO_ADVISE_RDPLUS
, &NFS_I(inode
)->flags
);
591 nfs_zap_caches(inode
);
593 desc
->entry
->eof
= 0;
599 res
= nfs_do_filldir(desc
, dirent
, filldir
);
606 nfs_unblock_sillyrename(dentry
);
610 nfs_free_fattr(my_entry
.fattr
);
611 nfs_free_fhandle(my_entry
.fh
);
612 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
613 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
618 static loff_t
nfs_llseek_dir(struct file
*filp
, loff_t offset
, int origin
)
620 struct dentry
*dentry
= filp
->f_path
.dentry
;
621 struct inode
*inode
= dentry
->d_inode
;
623 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
624 dentry
->d_parent
->d_name
.name
,
628 mutex_lock(&inode
->i_mutex
);
631 offset
+= filp
->f_pos
;
639 if (offset
!= filp
->f_pos
) {
640 filp
->f_pos
= offset
;
641 nfs_file_open_context(filp
)->dir_cookie
= 0;
644 mutex_unlock(&inode
->i_mutex
);
649 * All directory operations under NFS are synchronous, so fsync()
650 * is a dummy operation.
652 static int nfs_fsync_dir(struct file
*filp
, int datasync
)
654 struct dentry
*dentry
= filp
->f_path
.dentry
;
656 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
657 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
660 nfs_inc_stats(dentry
->d_inode
, NFSIOS_VFSFSYNC
);
665 * nfs_force_lookup_revalidate - Mark the directory as having changed
666 * @dir - pointer to directory inode
668 * This forces the revalidation code in nfs_lookup_revalidate() to do a
669 * full lookup on all child dentries of 'dir' whenever a change occurs
670 * on the server that might have invalidated our dcache.
672 * The caller should be holding dir->i_lock
674 void nfs_force_lookup_revalidate(struct inode
*dir
)
676 NFS_I(dir
)->cache_change_attribute
++;
680 * A check for whether or not the parent directory has changed.
681 * In the case it has, we assume that the dentries are untrustworthy
682 * and may need to be looked up again.
684 static int nfs_check_verifier(struct inode
*dir
, struct dentry
*dentry
)
688 if (NFS_SERVER(dir
)->flags
& NFS_MOUNT_LOOKUP_CACHE_NONE
)
690 if (!nfs_verify_change_attribute(dir
, dentry
->d_time
))
692 /* Revalidate nfsi->cache_change_attribute before we declare a match */
693 if (nfs_revalidate_inode(NFS_SERVER(dir
), dir
) < 0)
695 if (!nfs_verify_change_attribute(dir
, dentry
->d_time
))
701 * Return the intent data that applies to this particular path component
703 * Note that the current set of intents only apply to the very last
704 * component of the path.
705 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
707 static inline unsigned int nfs_lookup_check_intent(struct nameidata
*nd
, unsigned int mask
)
709 if (nd
->flags
& (LOOKUP_CONTINUE
|LOOKUP_PARENT
))
711 return nd
->flags
& mask
;
715 * Use intent information to check whether or not we're going to do
716 * an O_EXCL create using this path component.
718 static int nfs_is_exclusive_create(struct inode
*dir
, struct nameidata
*nd
)
720 if (NFS_PROTO(dir
)->version
== 2)
722 return nd
&& nfs_lookup_check_intent(nd
, LOOKUP_EXCL
);
726 * Inode and filehandle revalidation for lookups.
728 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
729 * or if the intent information indicates that we're about to open this
730 * particular file and the "nocto" mount flag is not set.
734 int nfs_lookup_verify_inode(struct inode
*inode
, struct nameidata
*nd
)
736 struct nfs_server
*server
= NFS_SERVER(inode
);
738 if (test_bit(NFS_INO_MOUNTPOINT
, &NFS_I(inode
)->flags
))
741 /* VFS wants an on-the-wire revalidation */
742 if (nd
->flags
& LOOKUP_REVAL
)
744 /* This is an open(2) */
745 if (nfs_lookup_check_intent(nd
, LOOKUP_OPEN
) != 0 &&
746 !(server
->flags
& NFS_MOUNT_NOCTO
) &&
747 (S_ISREG(inode
->i_mode
) ||
748 S_ISDIR(inode
->i_mode
)))
752 return nfs_revalidate_inode(server
, inode
);
754 return __nfs_revalidate_inode(server
, inode
);
758 * We judge how long we want to trust negative
759 * dentries by looking at the parent inode mtime.
761 * If parent mtime has changed, we revalidate, else we wait for a
762 * period corresponding to the parent's attribute cache timeout value.
765 int nfs_neg_need_reval(struct inode
*dir
, struct dentry
*dentry
,
766 struct nameidata
*nd
)
768 /* Don't revalidate a negative dentry if we're creating a new file */
769 if (nd
!= NULL
&& nfs_lookup_check_intent(nd
, LOOKUP_CREATE
) != 0)
771 if (NFS_SERVER(dir
)->flags
& NFS_MOUNT_LOOKUP_CACHE_NONEG
)
773 return !nfs_check_verifier(dir
, dentry
);
777 * This is called every time the dcache has a lookup hit,
778 * and we should check whether we can really trust that
781 * NOTE! The hit can be a negative hit too, don't assume
784 * If the parent directory is seen to have changed, we throw out the
785 * cached dentry and do a new lookup.
787 static int nfs_lookup_revalidate(struct dentry
* dentry
, struct nameidata
*nd
)
791 struct dentry
*parent
;
792 struct nfs_fh
*fhandle
= NULL
;
793 struct nfs_fattr
*fattr
= NULL
;
796 parent
= dget_parent(dentry
);
797 dir
= parent
->d_inode
;
798 nfs_inc_stats(dir
, NFSIOS_DENTRYREVALIDATE
);
799 inode
= dentry
->d_inode
;
802 if (nfs_neg_need_reval(dir
, dentry
, nd
))
807 if (is_bad_inode(inode
)) {
808 dfprintk(LOOKUPCACHE
, "%s: %s/%s has dud inode\n",
809 __func__
, dentry
->d_parent
->d_name
.name
,
810 dentry
->d_name
.name
);
814 if (nfs_have_delegation(inode
, FMODE_READ
))
815 goto out_set_verifier
;
817 /* Force a full look up iff the parent directory has changed */
818 if (!nfs_is_exclusive_create(dir
, nd
) && nfs_check_verifier(dir
, dentry
)) {
819 if (nfs_lookup_verify_inode(inode
, nd
))
824 if (NFS_STALE(inode
))
828 fhandle
= nfs_alloc_fhandle();
829 fattr
= nfs_alloc_fattr();
830 if (fhandle
== NULL
|| fattr
== NULL
)
833 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, fhandle
, fattr
);
836 if (nfs_compare_fh(NFS_FH(inode
), fhandle
))
838 if ((error
= nfs_refresh_inode(inode
, fattr
)) != 0)
841 nfs_free_fattr(fattr
);
842 nfs_free_fhandle(fhandle
);
844 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
847 dfprintk(LOOKUPCACHE
, "NFS: %s(%s/%s) is valid\n",
848 __func__
, dentry
->d_parent
->d_name
.name
,
849 dentry
->d_name
.name
);
854 nfs_mark_for_revalidate(dir
);
855 if (inode
&& S_ISDIR(inode
->i_mode
)) {
856 /* Purge readdir caches. */
857 nfs_zap_caches(inode
);
858 /* If we have submounts, don't unhash ! */
859 if (have_submounts(dentry
))
861 if (dentry
->d_flags
& DCACHE_DISCONNECTED
)
863 shrink_dcache_parent(dentry
);
866 nfs_free_fattr(fattr
);
867 nfs_free_fhandle(fhandle
);
869 dfprintk(LOOKUPCACHE
, "NFS: %s(%s/%s) is invalid\n",
870 __func__
, dentry
->d_parent
->d_name
.name
,
871 dentry
->d_name
.name
);
874 nfs_free_fattr(fattr
);
875 nfs_free_fhandle(fhandle
);
877 dfprintk(LOOKUPCACHE
, "NFS: %s(%s/%s) lookup returned error %d\n",
878 __func__
, dentry
->d_parent
->d_name
.name
,
879 dentry
->d_name
.name
, error
);
884 * This is called from dput() when d_count is going to 0.
886 static int nfs_dentry_delete(struct dentry
*dentry
)
888 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
889 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
892 /* Unhash any dentry with a stale inode */
893 if (dentry
->d_inode
!= NULL
&& NFS_STALE(dentry
->d_inode
))
896 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
897 /* Unhash it, so that ->d_iput() would be called */
900 if (!(dentry
->d_sb
->s_flags
& MS_ACTIVE
)) {
901 /* Unhash it, so that ancestors of killed async unlink
902 * files will be cleaned up during umount */
909 static void nfs_drop_nlink(struct inode
*inode
)
911 spin_lock(&inode
->i_lock
);
912 if (inode
->i_nlink
> 0)
914 spin_unlock(&inode
->i_lock
);
918 * Called when the dentry loses inode.
919 * We use it to clean up silly-renamed files.
921 static void nfs_dentry_iput(struct dentry
*dentry
, struct inode
*inode
)
923 if (S_ISDIR(inode
->i_mode
))
924 /* drop any readdir cache as it could easily be old */
925 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_DATA
;
927 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
929 nfs_complete_unlink(dentry
, inode
);
934 const struct dentry_operations nfs_dentry_operations
= {
935 .d_revalidate
= nfs_lookup_revalidate
,
936 .d_delete
= nfs_dentry_delete
,
937 .d_iput
= nfs_dentry_iput
,
940 static struct dentry
*nfs_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
943 struct dentry
*parent
;
944 struct inode
*inode
= NULL
;
945 struct nfs_fh
*fhandle
= NULL
;
946 struct nfs_fattr
*fattr
= NULL
;
949 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
950 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
951 nfs_inc_stats(dir
, NFSIOS_VFSLOOKUP
);
953 res
= ERR_PTR(-ENAMETOOLONG
);
954 if (dentry
->d_name
.len
> NFS_SERVER(dir
)->namelen
)
957 dentry
->d_op
= NFS_PROTO(dir
)->dentry_ops
;
960 * If we're doing an exclusive create, optimize away the lookup
961 * but don't hash the dentry.
963 if (nfs_is_exclusive_create(dir
, nd
)) {
964 d_instantiate(dentry
, NULL
);
969 res
= ERR_PTR(-ENOMEM
);
970 fhandle
= nfs_alloc_fhandle();
971 fattr
= nfs_alloc_fattr();
972 if (fhandle
== NULL
|| fattr
== NULL
)
975 parent
= dentry
->d_parent
;
976 /* Protect against concurrent sillydeletes */
977 nfs_block_sillyrename(parent
);
978 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, fhandle
, fattr
);
979 if (error
== -ENOENT
)
982 res
= ERR_PTR(error
);
983 goto out_unblock_sillyrename
;
985 inode
= nfs_fhget(dentry
->d_sb
, fhandle
, fattr
);
986 res
= (struct dentry
*)inode
;
988 goto out_unblock_sillyrename
;
991 res
= d_materialise_unique(dentry
, inode
);
994 goto out_unblock_sillyrename
;
997 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
998 out_unblock_sillyrename
:
999 nfs_unblock_sillyrename(parent
);
1001 nfs_free_fattr(fattr
);
1002 nfs_free_fhandle(fhandle
);
1006 #ifdef CONFIG_NFS_V4
1007 static int nfs_open_revalidate(struct dentry
*, struct nameidata
*);
1009 const struct dentry_operations nfs4_dentry_operations
= {
1010 .d_revalidate
= nfs_open_revalidate
,
1011 .d_delete
= nfs_dentry_delete
,
1012 .d_iput
= nfs_dentry_iput
,
1016 * Use intent information to determine whether we need to substitute
1017 * the NFSv4-style stateful OPEN for the LOOKUP call
1019 static int is_atomic_open(struct nameidata
*nd
)
1021 if (nd
== NULL
|| nfs_lookup_check_intent(nd
, LOOKUP_OPEN
) == 0)
1023 /* NFS does not (yet) have a stateful open for directories */
1024 if (nd
->flags
& LOOKUP_DIRECTORY
)
1026 /* Are we trying to write to a read only partition? */
1027 if (__mnt_is_readonly(nd
->path
.mnt
) &&
1028 (nd
->intent
.open
.flags
& (O_CREAT
|O_TRUNC
|FMODE_WRITE
)))
1033 static struct nfs_open_context
*nameidata_to_nfs_open_context(struct dentry
*dentry
, struct nameidata
*nd
)
1035 struct path path
= {
1036 .mnt
= nd
->path
.mnt
,
1039 struct nfs_open_context
*ctx
;
1040 struct rpc_cred
*cred
;
1041 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
1043 cred
= rpc_lookup_cred();
1045 return ERR_CAST(cred
);
1046 ctx
= alloc_nfs_open_context(&path
, cred
, fmode
);
1049 return ERR_PTR(-ENOMEM
);
1053 static int do_open(struct inode
*inode
, struct file
*filp
)
1055 nfs_fscache_set_inode_cookie(inode
, filp
);
1059 static int nfs_intent_set_file(struct nameidata
*nd
, struct nfs_open_context
*ctx
)
1064 /* If the open_intent is for execute, we have an extra check to make */
1065 if (ctx
->mode
& FMODE_EXEC
) {
1066 ret
= nfs_may_open(ctx
->path
.dentry
->d_inode
,
1068 nd
->intent
.open
.flags
);
1072 filp
= lookup_instantiate_filp(nd
, ctx
->path
.dentry
, do_open
);
1074 ret
= PTR_ERR(filp
);
1076 nfs_file_set_open_context(filp
, ctx
);
1078 put_nfs_open_context(ctx
);
1082 static struct dentry
*nfs_atomic_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1084 struct nfs_open_context
*ctx
;
1086 struct dentry
*res
= NULL
;
1090 dfprintk(VFS
, "NFS: atomic_lookup(%s/%ld), %s\n",
1091 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1093 /* Check that we are indeed trying to open this file */
1094 if (!is_atomic_open(nd
))
1097 if (dentry
->d_name
.len
> NFS_SERVER(dir
)->namelen
) {
1098 res
= ERR_PTR(-ENAMETOOLONG
);
1101 dentry
->d_op
= NFS_PROTO(dir
)->dentry_ops
;
1103 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1105 if (nd
->flags
& LOOKUP_EXCL
) {
1106 d_instantiate(dentry
, NULL
);
1110 ctx
= nameidata_to_nfs_open_context(dentry
, nd
);
1111 res
= ERR_CAST(ctx
);
1115 open_flags
= nd
->intent
.open
.flags
;
1116 if (nd
->flags
& LOOKUP_CREATE
) {
1117 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
1118 attr
.ia_valid
= ATTR_MODE
;
1119 if (!IS_POSIXACL(dir
))
1120 attr
.ia_mode
&= ~current_umask();
1122 open_flags
&= ~O_EXCL
;
1124 BUG_ON(open_flags
& O_CREAT
);
1127 /* Open the file on the server */
1128 res
= nfs4_atomic_open(dir
, ctx
, open_flags
, &attr
);
1130 put_nfs_open_context(ctx
);
1131 error
= PTR_ERR(res
);
1133 /* Make a negative dentry */
1137 /* This turned out not to be a regular file */
1142 if (!(nd
->intent
.open
.flags
& O_NOFOLLOW
))
1151 nfs_intent_set_file(nd
, ctx
);
1155 return nfs_lookup(dir
, dentry
, nd
);
1158 static int nfs_open_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1160 struct dentry
*parent
= NULL
;
1161 struct inode
*inode
= dentry
->d_inode
;
1163 int openflags
, ret
= 0;
1165 if (!is_atomic_open(nd
) || d_mountpoint(dentry
))
1167 parent
= dget_parent(dentry
);
1168 dir
= parent
->d_inode
;
1169 /* We can't create new files in nfs_open_revalidate(), so we
1170 * optimize away revalidation of negative dentries.
1172 if (inode
== NULL
) {
1173 if (!nfs_neg_need_reval(dir
, dentry
, nd
))
1178 /* NFS only supports OPEN on regular files */
1179 if (!S_ISREG(inode
->i_mode
))
1181 openflags
= nd
->intent
.open
.flags
;
1182 /* We cannot do exclusive creation on a positive dentry */
1183 if ((openflags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1185 /* We can't create new files, or truncate existing ones here */
1186 openflags
&= ~(O_CREAT
|O_EXCL
|O_TRUNC
);
1189 * Note: we're not holding inode->i_mutex and so may be racing with
1190 * operations that change the directory. We therefore save the
1191 * change attribute *before* we do the RPC call.
1193 ret
= nfs4_open_revalidate(dir
, dentry
, openflags
, nd
);
1202 return nfs_lookup_revalidate(dentry
, nd
);
1204 #endif /* CONFIG_NFSV4 */
1206 static struct dentry
*nfs_readdir_lookup(nfs_readdir_descriptor_t
*desc
)
1208 struct dentry
*parent
= desc
->file
->f_path
.dentry
;
1209 struct inode
*dir
= parent
->d_inode
;
1210 struct nfs_entry
*entry
= desc
->entry
;
1211 struct dentry
*dentry
, *alias
;
1212 struct qstr name
= {
1213 .name
= entry
->name
,
1216 struct inode
*inode
;
1217 unsigned long verf
= nfs_save_change_attribute(dir
);
1221 if (name
.name
[0] == '.' && name
.name
[1] == '.')
1222 return dget_parent(parent
);
1225 if (name
.name
[0] == '.')
1226 return dget(parent
);
1229 spin_lock(&dir
->i_lock
);
1230 if (NFS_I(dir
)->cache_validity
& NFS_INO_INVALID_DATA
) {
1231 spin_unlock(&dir
->i_lock
);
1234 spin_unlock(&dir
->i_lock
);
1236 name
.hash
= full_name_hash(name
.name
, name
.len
);
1237 dentry
= d_lookup(parent
, &name
);
1238 if (dentry
!= NULL
) {
1239 /* Is this a positive dentry that matches the readdir info? */
1240 if (dentry
->d_inode
!= NULL
&&
1241 (NFS_FILEID(dentry
->d_inode
) == entry
->ino
||
1242 d_mountpoint(dentry
))) {
1243 if (!desc
->plus
|| entry
->fh
->size
== 0)
1245 if (nfs_compare_fh(NFS_FH(dentry
->d_inode
),
1249 /* No, so d_drop to allow one to be created */
1253 if (!desc
->plus
|| !(entry
->fattr
->valid
& NFS_ATTR_FATTR
))
1255 if (name
.len
> NFS_SERVER(dir
)->namelen
)
1257 /* Note: caller is already holding the dir->i_mutex! */
1258 dentry
= d_alloc(parent
, &name
);
1261 dentry
->d_op
= NFS_PROTO(dir
)->dentry_ops
;
1262 inode
= nfs_fhget(dentry
->d_sb
, entry
->fh
, entry
->fattr
);
1263 if (IS_ERR(inode
)) {
1268 alias
= d_materialise_unique(dentry
, inode
);
1269 if (alias
!= NULL
) {
1277 nfs_set_verifier(dentry
, verf
);
1282 * Code common to create, mkdir, and mknod.
1284 int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
1285 struct nfs_fattr
*fattr
)
1287 struct dentry
*parent
= dget_parent(dentry
);
1288 struct inode
*dir
= parent
->d_inode
;
1289 struct inode
*inode
;
1290 int error
= -EACCES
;
1294 /* We may have been initialized further down */
1295 if (dentry
->d_inode
)
1297 if (fhandle
->size
== 0) {
1298 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, fhandle
, fattr
);
1302 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1303 if (!(fattr
->valid
& NFS_ATTR_FATTR
)) {
1304 struct nfs_server
*server
= NFS_SB(dentry
->d_sb
);
1305 error
= server
->nfs_client
->rpc_ops
->getattr(server
, fhandle
, fattr
);
1309 inode
= nfs_fhget(dentry
->d_sb
, fhandle
, fattr
);
1310 error
= PTR_ERR(inode
);
1313 d_add(dentry
, inode
);
1318 nfs_mark_for_revalidate(dir
);
1324 * Following a failed create operation, we drop the dentry rather
1325 * than retain a negative dentry. This avoids a problem in the event
1326 * that the operation succeeded on the server, but an error in the
1327 * reply path made it appear to have failed.
1329 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1330 struct nameidata
*nd
)
1336 dfprintk(VFS
, "NFS: create(%s/%ld), %s\n",
1337 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1339 attr
.ia_mode
= mode
;
1340 attr
.ia_valid
= ATTR_MODE
;
1342 if ((nd
->flags
& LOOKUP_CREATE
) != 0)
1343 open_flags
= nd
->intent
.open
.flags
;
1345 error
= NFS_PROTO(dir
)->create(dir
, dentry
, &attr
, open_flags
, nd
);
1355 * See comments for nfs_proc_create regarding failed operations.
1358 nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
1363 dfprintk(VFS
, "NFS: mknod(%s/%ld), %s\n",
1364 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1366 if (!new_valid_dev(rdev
))
1369 attr
.ia_mode
= mode
;
1370 attr
.ia_valid
= ATTR_MODE
;
1372 status
= NFS_PROTO(dir
)->mknod(dir
, dentry
, &attr
, rdev
);
1382 * See comments for nfs_proc_create regarding failed operations.
1384 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1389 dfprintk(VFS
, "NFS: mkdir(%s/%ld), %s\n",
1390 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1392 attr
.ia_valid
= ATTR_MODE
;
1393 attr
.ia_mode
= mode
| S_IFDIR
;
1395 error
= NFS_PROTO(dir
)->mkdir(dir
, dentry
, &attr
);
1404 static void nfs_dentry_handle_enoent(struct dentry
*dentry
)
1406 if (dentry
->d_inode
!= NULL
&& !d_unhashed(dentry
))
1410 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1414 dfprintk(VFS
, "NFS: rmdir(%s/%ld), %s\n",
1415 dir
->i_sb
->s_id
, dir
->i_ino
, dentry
->d_name
.name
);
1417 error
= NFS_PROTO(dir
)->rmdir(dir
, &dentry
->d_name
);
1418 /* Ensure the VFS deletes this inode */
1419 if (error
== 0 && dentry
->d_inode
!= NULL
)
1420 clear_nlink(dentry
->d_inode
);
1421 else if (error
== -ENOENT
)
1422 nfs_dentry_handle_enoent(dentry
);
1427 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
1429 static unsigned int sillycounter
;
1430 const int fileidsize
= sizeof(NFS_FILEID(dentry
->d_inode
))*2;
1431 const int countersize
= sizeof(sillycounter
)*2;
1432 const int slen
= sizeof(".nfs")+fileidsize
+countersize
-1;
1435 struct dentry
*sdentry
;
1438 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
1439 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
1440 atomic_read(&dentry
->d_count
));
1441 nfs_inc_stats(dir
, NFSIOS_SILLYRENAME
);
1444 * We don't allow a dentry to be silly-renamed twice.
1447 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1450 sprintf(silly
, ".nfs%*.*Lx",
1451 fileidsize
, fileidsize
,
1452 (unsigned long long)NFS_FILEID(dentry
->d_inode
));
1454 /* Return delegation in anticipation of the rename */
1455 nfs_inode_return_delegation(dentry
->d_inode
);
1459 char *suffix
= silly
+ slen
- countersize
;
1463 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
1465 dfprintk(VFS
, "NFS: trying to rename %s to %s\n",
1466 dentry
->d_name
.name
, silly
);
1468 sdentry
= lookup_one_len(silly
, dentry
->d_parent
, slen
);
1470 * N.B. Better to return EBUSY here ... it could be
1471 * dangerous to delete the file while it's in use.
1473 if (IS_ERR(sdentry
))
1475 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
1477 qsilly
.name
= silly
;
1478 qsilly
.len
= strlen(silly
);
1479 if (dentry
->d_inode
) {
1480 error
= NFS_PROTO(dir
)->rename(dir
, &dentry
->d_name
,
1482 nfs_mark_for_revalidate(dentry
->d_inode
);
1484 error
= NFS_PROTO(dir
)->rename(dir
, &dentry
->d_name
,
1487 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1488 d_move(dentry
, sdentry
);
1489 error
= nfs_async_unlink(dir
, dentry
);
1490 /* If we return 0 we don't unlink */
1498 * Remove a file after making sure there are no pending writes,
1499 * and after checking that the file has only one user.
1501 * We invalidate the attribute cache and free the inode prior to the operation
1502 * to avoid possible races if the server reuses the inode.
1504 static int nfs_safe_remove(struct dentry
*dentry
)
1506 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1507 struct inode
*inode
= dentry
->d_inode
;
1510 dfprintk(VFS
, "NFS: safe_remove(%s/%s)\n",
1511 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1513 /* If the dentry was sillyrenamed, we simply call d_delete() */
1514 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
1519 if (inode
!= NULL
) {
1520 nfs_inode_return_delegation(inode
);
1521 error
= NFS_PROTO(dir
)->remove(dir
, &dentry
->d_name
);
1522 /* The VFS may want to delete this inode */
1524 nfs_drop_nlink(inode
);
1525 nfs_mark_for_revalidate(inode
);
1527 error
= NFS_PROTO(dir
)->remove(dir
, &dentry
->d_name
);
1528 if (error
== -ENOENT
)
1529 nfs_dentry_handle_enoent(dentry
);
1534 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1535 * belongs to an active ".nfs..." file and we return -EBUSY.
1537 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1539 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1542 int need_rehash
= 0;
1544 dfprintk(VFS
, "NFS: unlink(%s/%ld, %s)\n", dir
->i_sb
->s_id
,
1545 dir
->i_ino
, dentry
->d_name
.name
);
1547 spin_lock(&dcache_lock
);
1548 spin_lock(&dentry
->d_lock
);
1549 if (atomic_read(&dentry
->d_count
) > 1) {
1550 spin_unlock(&dentry
->d_lock
);
1551 spin_unlock(&dcache_lock
);
1552 /* Start asynchronous writeout of the inode */
1553 write_inode_now(dentry
->d_inode
, 0);
1554 error
= nfs_sillyrename(dir
, dentry
);
1557 if (!d_unhashed(dentry
)) {
1561 spin_unlock(&dentry
->d_lock
);
1562 spin_unlock(&dcache_lock
);
1563 error
= nfs_safe_remove(dentry
);
1564 if (!error
|| error
== -ENOENT
) {
1565 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1566 } else if (need_rehash
)
1572 * To create a symbolic link, most file systems instantiate a new inode,
1573 * add a page to it containing the path, then write it out to the disk
1574 * using prepare_write/commit_write.
1576 * Unfortunately the NFS client can't create the in-core inode first
1577 * because it needs a file handle to create an in-core inode (see
1578 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1579 * symlink request has completed on the server.
1581 * So instead we allocate a raw page, copy the symname into it, then do
1582 * the SYMLINK request with the page as the buffer. If it succeeds, we
1583 * now have a new file handle and can instantiate an in-core NFS inode
1584 * and move the raw page into its mapping.
1586 static int nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1588 struct pagevec lru_pvec
;
1592 unsigned int pathlen
= strlen(symname
);
1595 dfprintk(VFS
, "NFS: symlink(%s/%ld, %s, %s)\n", dir
->i_sb
->s_id
,
1596 dir
->i_ino
, dentry
->d_name
.name
, symname
);
1598 if (pathlen
> PAGE_SIZE
)
1599 return -ENAMETOOLONG
;
1601 attr
.ia_mode
= S_IFLNK
| S_IRWXUGO
;
1602 attr
.ia_valid
= ATTR_MODE
;
1604 page
= alloc_page(GFP_HIGHUSER
);
1608 kaddr
= kmap_atomic(page
, KM_USER0
);
1609 memcpy(kaddr
, symname
, pathlen
);
1610 if (pathlen
< PAGE_SIZE
)
1611 memset(kaddr
+ pathlen
, 0, PAGE_SIZE
- pathlen
);
1612 kunmap_atomic(kaddr
, KM_USER0
);
1614 error
= NFS_PROTO(dir
)->symlink(dir
, dentry
, page
, pathlen
, &attr
);
1616 dfprintk(VFS
, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1617 dir
->i_sb
->s_id
, dir
->i_ino
,
1618 dentry
->d_name
.name
, symname
, error
);
1625 * No big deal if we can't add this page to the page cache here.
1626 * READLINK will get the missing page from the server if needed.
1628 pagevec_init(&lru_pvec
, 0);
1629 if (!add_to_page_cache(page
, dentry
->d_inode
->i_mapping
, 0,
1631 pagevec_add(&lru_pvec
, page
);
1632 pagevec_lru_add_file(&lru_pvec
);
1633 SetPageUptodate(page
);
1642 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
1644 struct inode
*inode
= old_dentry
->d_inode
;
1647 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
1648 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1649 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1651 nfs_inode_return_delegation(inode
);
1654 error
= NFS_PROTO(dir
)->link(inode
, dir
, &dentry
->d_name
);
1656 atomic_inc(&inode
->i_count
);
1657 d_add(dentry
, inode
);
1664 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1665 * different file handle for the same inode after a rename (e.g. when
1666 * moving to a different directory). A fail-safe method to do so would
1667 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1668 * rename the old file using the sillyrename stuff. This way, the original
1669 * file in old_dir will go away when the last process iput()s the inode.
1673 * It actually works quite well. One needs to have the possibility for
1674 * at least one ".nfs..." file in each directory the file ever gets
1675 * moved or linked to which happens automagically with the new
1676 * implementation that only depends on the dcache stuff instead of
1677 * using the inode layer
1679 * Unfortunately, things are a little more complicated than indicated
1680 * above. For a cross-directory move, we want to make sure we can get
1681 * rid of the old inode after the operation. This means there must be
1682 * no pending writes (if it's a file), and the use count must be 1.
1683 * If these conditions are met, we can drop the dentries before doing
1686 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1687 struct inode
*new_dir
, struct dentry
*new_dentry
)
1689 struct inode
*old_inode
= old_dentry
->d_inode
;
1690 struct inode
*new_inode
= new_dentry
->d_inode
;
1691 struct dentry
*dentry
= NULL
, *rehash
= NULL
;
1694 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1695 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1696 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1697 atomic_read(&new_dentry
->d_count
));
1700 * For non-directories, check whether the target is busy and if so,
1701 * make a copy of the dentry and then do a silly-rename. If the
1702 * silly-rename succeeds, the copied dentry is hashed and becomes
1705 if (new_inode
&& !S_ISDIR(new_inode
->i_mode
)) {
1707 * To prevent any new references to the target during the
1708 * rename, we unhash the dentry in advance.
1710 if (!d_unhashed(new_dentry
)) {
1712 rehash
= new_dentry
;
1715 if (atomic_read(&new_dentry
->d_count
) > 2) {
1718 /* copy the target dentry's name */
1719 dentry
= d_alloc(new_dentry
->d_parent
,
1720 &new_dentry
->d_name
);
1724 /* silly-rename the existing target ... */
1725 err
= nfs_sillyrename(new_dir
, new_dentry
);
1729 new_dentry
= dentry
;
1735 nfs_inode_return_delegation(old_inode
);
1736 if (new_inode
!= NULL
)
1737 nfs_inode_return_delegation(new_inode
);
1739 error
= NFS_PROTO(old_dir
)->rename(old_dir
, &old_dentry
->d_name
,
1740 new_dir
, &new_dentry
->d_name
);
1741 nfs_mark_for_revalidate(old_inode
);
1746 if (new_inode
!= NULL
)
1747 nfs_drop_nlink(new_inode
);
1748 d_move(old_dentry
, new_dentry
);
1749 nfs_set_verifier(new_dentry
,
1750 nfs_save_change_attribute(new_dir
));
1751 } else if (error
== -ENOENT
)
1752 nfs_dentry_handle_enoent(old_dentry
);
1754 /* new dentry created? */
1760 static DEFINE_SPINLOCK(nfs_access_lru_lock
);
1761 static LIST_HEAD(nfs_access_lru_list
);
1762 static atomic_long_t nfs_access_nr_entries
;
1764 static void nfs_access_free_entry(struct nfs_access_entry
*entry
)
1766 put_rpccred(entry
->cred
);
1768 smp_mb__before_atomic_dec();
1769 atomic_long_dec(&nfs_access_nr_entries
);
1770 smp_mb__after_atomic_dec();
1773 static void nfs_access_free_list(struct list_head
*head
)
1775 struct nfs_access_entry
*cache
;
1777 while (!list_empty(head
)) {
1778 cache
= list_entry(head
->next
, struct nfs_access_entry
, lru
);
1779 list_del(&cache
->lru
);
1780 nfs_access_free_entry(cache
);
1784 int nfs_access_cache_shrinker(struct shrinker
*shrink
, int nr_to_scan
, gfp_t gfp_mask
)
1787 struct nfs_inode
*nfsi
;
1788 struct nfs_access_entry
*cache
;
1790 if ((gfp_mask
& GFP_KERNEL
) != GFP_KERNEL
)
1791 return (nr_to_scan
== 0) ? 0 : -1;
1793 spin_lock(&nfs_access_lru_lock
);
1794 list_for_each_entry(nfsi
, &nfs_access_lru_list
, access_cache_inode_lru
) {
1795 struct inode
*inode
;
1797 if (nr_to_scan
-- == 0)
1799 inode
= &nfsi
->vfs_inode
;
1800 spin_lock(&inode
->i_lock
);
1801 if (list_empty(&nfsi
->access_cache_entry_lru
))
1802 goto remove_lru_entry
;
1803 cache
= list_entry(nfsi
->access_cache_entry_lru
.next
,
1804 struct nfs_access_entry
, lru
);
1805 list_move(&cache
->lru
, &head
);
1806 rb_erase(&cache
->rb_node
, &nfsi
->access_cache
);
1807 if (!list_empty(&nfsi
->access_cache_entry_lru
))
1808 list_move_tail(&nfsi
->access_cache_inode_lru
,
1809 &nfs_access_lru_list
);
1812 list_del_init(&nfsi
->access_cache_inode_lru
);
1813 smp_mb__before_clear_bit();
1814 clear_bit(NFS_INO_ACL_LRU_SET
, &nfsi
->flags
);
1815 smp_mb__after_clear_bit();
1817 spin_unlock(&inode
->i_lock
);
1819 spin_unlock(&nfs_access_lru_lock
);
1820 nfs_access_free_list(&head
);
1821 return (atomic_long_read(&nfs_access_nr_entries
) / 100) * sysctl_vfs_cache_pressure
;
1824 static void __nfs_access_zap_cache(struct nfs_inode
*nfsi
, struct list_head
*head
)
1826 struct rb_root
*root_node
= &nfsi
->access_cache
;
1828 struct nfs_access_entry
*entry
;
1830 /* Unhook entries from the cache */
1831 while ((n
= rb_first(root_node
)) != NULL
) {
1832 entry
= rb_entry(n
, struct nfs_access_entry
, rb_node
);
1833 rb_erase(n
, root_node
);
1834 list_move(&entry
->lru
, head
);
1836 nfsi
->cache_validity
&= ~NFS_INO_INVALID_ACCESS
;
1839 void nfs_access_zap_cache(struct inode
*inode
)
1843 if (test_bit(NFS_INO_ACL_LRU_SET
, &NFS_I(inode
)->flags
) == 0)
1845 /* Remove from global LRU init */
1846 spin_lock(&nfs_access_lru_lock
);
1847 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET
, &NFS_I(inode
)->flags
))
1848 list_del_init(&NFS_I(inode
)->access_cache_inode_lru
);
1850 spin_lock(&inode
->i_lock
);
1851 __nfs_access_zap_cache(NFS_I(inode
), &head
);
1852 spin_unlock(&inode
->i_lock
);
1853 spin_unlock(&nfs_access_lru_lock
);
1854 nfs_access_free_list(&head
);
1857 static struct nfs_access_entry
*nfs_access_search_rbtree(struct inode
*inode
, struct rpc_cred
*cred
)
1859 struct rb_node
*n
= NFS_I(inode
)->access_cache
.rb_node
;
1860 struct nfs_access_entry
*entry
;
1863 entry
= rb_entry(n
, struct nfs_access_entry
, rb_node
);
1865 if (cred
< entry
->cred
)
1867 else if (cred
> entry
->cred
)
1875 static int nfs_access_get_cached(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_access_entry
*res
)
1877 struct nfs_inode
*nfsi
= NFS_I(inode
);
1878 struct nfs_access_entry
*cache
;
1881 spin_lock(&inode
->i_lock
);
1882 if (nfsi
->cache_validity
& NFS_INO_INVALID_ACCESS
)
1884 cache
= nfs_access_search_rbtree(inode
, cred
);
1887 if (!nfs_have_delegated_attributes(inode
) &&
1888 !time_in_range_open(jiffies
, cache
->jiffies
, cache
->jiffies
+ nfsi
->attrtimeo
))
1890 res
->jiffies
= cache
->jiffies
;
1891 res
->cred
= cache
->cred
;
1892 res
->mask
= cache
->mask
;
1893 list_move_tail(&cache
->lru
, &nfsi
->access_cache_entry_lru
);
1896 spin_unlock(&inode
->i_lock
);
1899 rb_erase(&cache
->rb_node
, &nfsi
->access_cache
);
1900 list_del(&cache
->lru
);
1901 spin_unlock(&inode
->i_lock
);
1902 nfs_access_free_entry(cache
);
1905 spin_unlock(&inode
->i_lock
);
1906 nfs_access_zap_cache(inode
);
1910 static void nfs_access_add_rbtree(struct inode
*inode
, struct nfs_access_entry
*set
)
1912 struct nfs_inode
*nfsi
= NFS_I(inode
);
1913 struct rb_root
*root_node
= &nfsi
->access_cache
;
1914 struct rb_node
**p
= &root_node
->rb_node
;
1915 struct rb_node
*parent
= NULL
;
1916 struct nfs_access_entry
*entry
;
1918 spin_lock(&inode
->i_lock
);
1919 while (*p
!= NULL
) {
1921 entry
= rb_entry(parent
, struct nfs_access_entry
, rb_node
);
1923 if (set
->cred
< entry
->cred
)
1924 p
= &parent
->rb_left
;
1925 else if (set
->cred
> entry
->cred
)
1926 p
= &parent
->rb_right
;
1930 rb_link_node(&set
->rb_node
, parent
, p
);
1931 rb_insert_color(&set
->rb_node
, root_node
);
1932 list_add_tail(&set
->lru
, &nfsi
->access_cache_entry_lru
);
1933 spin_unlock(&inode
->i_lock
);
1936 rb_replace_node(parent
, &set
->rb_node
, root_node
);
1937 list_add_tail(&set
->lru
, &nfsi
->access_cache_entry_lru
);
1938 list_del(&entry
->lru
);
1939 spin_unlock(&inode
->i_lock
);
1940 nfs_access_free_entry(entry
);
1943 static void nfs_access_add_cache(struct inode
*inode
, struct nfs_access_entry
*set
)
1945 struct nfs_access_entry
*cache
= kmalloc(sizeof(*cache
), GFP_KERNEL
);
1948 RB_CLEAR_NODE(&cache
->rb_node
);
1949 cache
->jiffies
= set
->jiffies
;
1950 cache
->cred
= get_rpccred(set
->cred
);
1951 cache
->mask
= set
->mask
;
1953 nfs_access_add_rbtree(inode
, cache
);
1955 /* Update accounting */
1956 smp_mb__before_atomic_inc();
1957 atomic_long_inc(&nfs_access_nr_entries
);
1958 smp_mb__after_atomic_inc();
1960 /* Add inode to global LRU list */
1961 if (!test_bit(NFS_INO_ACL_LRU_SET
, &NFS_I(inode
)->flags
)) {
1962 spin_lock(&nfs_access_lru_lock
);
1963 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET
, &NFS_I(inode
)->flags
))
1964 list_add_tail(&NFS_I(inode
)->access_cache_inode_lru
,
1965 &nfs_access_lru_list
);
1966 spin_unlock(&nfs_access_lru_lock
);
1970 static int nfs_do_access(struct inode
*inode
, struct rpc_cred
*cred
, int mask
)
1972 struct nfs_access_entry cache
;
1975 status
= nfs_access_get_cached(inode
, cred
, &cache
);
1979 /* Be clever: ask server to check for all possible rights */
1980 cache
.mask
= MAY_EXEC
| MAY_WRITE
| MAY_READ
;
1982 cache
.jiffies
= jiffies
;
1983 status
= NFS_PROTO(inode
)->access(inode
, &cache
);
1985 if (status
== -ESTALE
) {
1986 nfs_zap_caches(inode
);
1987 if (!S_ISDIR(inode
->i_mode
))
1988 set_bit(NFS_INO_STALE
, &NFS_I(inode
)->flags
);
1992 nfs_access_add_cache(inode
, &cache
);
1994 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
1999 static int nfs_open_permission_mask(int openflags
)
2003 if (openflags
& FMODE_READ
)
2005 if (openflags
& FMODE_WRITE
)
2007 if (openflags
& FMODE_EXEC
)
2012 int nfs_may_open(struct inode
*inode
, struct rpc_cred
*cred
, int openflags
)
2014 return nfs_do_access(inode
, cred
, nfs_open_permission_mask(openflags
));
2017 int nfs_permission(struct inode
*inode
, int mask
)
2019 struct rpc_cred
*cred
;
2022 nfs_inc_stats(inode
, NFSIOS_VFSACCESS
);
2024 if ((mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
2026 /* Is this sys_access() ? */
2027 if (mask
& (MAY_ACCESS
| MAY_CHDIR
))
2030 switch (inode
->i_mode
& S_IFMT
) {
2034 /* NFSv4 has atomic_open... */
2035 if (nfs_server_capable(inode
, NFS_CAP_ATOMIC_OPEN
)
2036 && (mask
& MAY_OPEN
)
2037 && !(mask
& MAY_EXEC
))
2042 * Optimize away all write operations, since the server
2043 * will check permissions when we perform the op.
2045 if ((mask
& MAY_WRITE
) && !(mask
& MAY_READ
))
2050 if (!NFS_PROTO(inode
)->access
)
2053 cred
= rpc_lookup_cred();
2054 if (!IS_ERR(cred
)) {
2055 res
= nfs_do_access(inode
, cred
, mask
);
2058 res
= PTR_ERR(cred
);
2060 if (!res
&& (mask
& MAY_EXEC
) && !execute_ok(inode
))
2063 dfprintk(VFS
, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2064 inode
->i_sb
->s_id
, inode
->i_ino
, mask
, res
);
2067 res
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
2069 res
= generic_permission(inode
, mask
, NULL
);
2075 * version-control: t
2076 * kept-new-versions: 5