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 static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
109 const struct inode_operations nfs4_dir_inode_operations = {
110 .create = nfs_open_create,
111 .lookup = nfs_atomic_lookup,
113 .unlink = nfs_unlink,
114 .symlink = nfs_symlink,
118 .rename = nfs_rename,
119 .permission = nfs_permission,
120 .getattr = nfs_getattr,
121 .setattr = nfs_setattr,
122 .getxattr = nfs4_getxattr,
123 .setxattr = nfs4_setxattr,
124 .listxattr = nfs4_listxattr,
127 #endif /* CONFIG_NFS_V4 */
133 nfs_opendir(struct inode *inode, struct file *filp)
137 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
138 filp->f_path.dentry->d_parent->d_name.name,
139 filp->f_path.dentry->d_name.name);
141 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
143 /* Call generic open code in order to cache credentials */
144 res = nfs_open(inode, filp);
145 if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
146 /* This is a mountpoint, so d_revalidate will never
147 * have been called, so we need to refresh the
148 * inode (for close-open consistency) ourselves.
150 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
155 typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
159 unsigned long page_index;
162 loff_t current_index;
163 struct nfs_entry *entry;
164 decode_dirent_t decode;
166 unsigned long timestamp;
167 unsigned long gencount;
169 } nfs_readdir_descriptor_t;
171 /* Now we cache directories properly, by stuffing the dirent
172 * data directly in the page cache.
174 * Inode invalidation due to refresh etc. takes care of
175 * _everything_, no sloppy entry flushing logic, no extraneous
176 * copying, network direct to page cache, the way it was meant
179 * NOTE: Dirent information verification is done always by the
180 * page-in of the RPC reply, nowhere else, this simplies
181 * things substantially.
184 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
186 struct file *file = desc->file;
187 struct inode *inode = file->f_path.dentry->d_inode;
188 struct rpc_cred *cred = nfs_file_cred(file);
189 unsigned long timestamp, gencount;
192 dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
193 __func__, (long long)desc->entry->cookie,
198 gencount = nfs_inc_attr_generation_counter();
199 error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
200 NFS_SERVER(inode)->dtsize, desc->plus);
202 /* We requested READDIRPLUS, but the server doesn't grok it */
203 if (error == -ENOTSUPP && desc->plus) {
204 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
205 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
211 desc->timestamp = timestamp;
212 desc->gencount = gencount;
213 desc->timestamp_valid = 1;
214 SetPageUptodate(page);
215 /* Ensure consistent page alignment of the data.
216 * Note: assumes we have exclusive access to this mapping either
217 * through inode->i_mutex or some other mechanism.
219 if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
220 /* Should never happen */
221 nfs_zap_mapping(inode, inode->i_mapping);
231 int dir_decode(nfs_readdir_descriptor_t *desc)
233 __be32 *p = desc->ptr;
234 p = desc->decode(p, desc->entry, desc->plus);
238 if (desc->timestamp_valid) {
239 desc->entry->fattr->time_start = desc->timestamp;
240 desc->entry->fattr->gencount = desc->gencount;
242 desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
247 void dir_page_release(nfs_readdir_descriptor_t *desc)
250 page_cache_release(desc->page);
256 * Given a pointer to a buffer that has already been filled by a call
257 * to readdir, find the next entry with cookie '*desc->dir_cookie'.
259 * If the end of the buffer has been reached, return -EAGAIN, if not,
260 * return the offset within the buffer of the next entry to be
264 int find_dirent(nfs_readdir_descriptor_t *desc)
266 struct nfs_entry *entry = desc->entry;
270 while((status = dir_decode(desc)) == 0) {
271 dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
272 __func__, (unsigned long long)entry->cookie);
273 if (entry->prev_cookie == *desc->dir_cookie)
275 if (loop_count++ > 200) {
284 * Given a pointer to a buffer that has already been filled by a call
285 * to readdir, find the entry at offset 'desc->file->f_pos'.
287 * If the end of the buffer has been reached, return -EAGAIN, if not,
288 * return the offset within the buffer of the next entry to be
292 int find_dirent_index(nfs_readdir_descriptor_t *desc)
294 struct nfs_entry *entry = desc->entry;
299 status = dir_decode(desc);
303 dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
304 (unsigned long long)entry->cookie, desc->current_index);
306 if (desc->file->f_pos == desc->current_index) {
307 *desc->dir_cookie = entry->cookie;
310 desc->current_index++;
311 if (loop_count++ > 200) {
320 * Find the given page, and call find_dirent() or find_dirent_index in
321 * order to try to return the next entry.
324 int find_dirent_page(nfs_readdir_descriptor_t *desc)
326 struct inode *inode = desc->file->f_path.dentry->d_inode;
330 dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
331 __func__, desc->page_index,
332 (long long) *desc->dir_cookie);
334 /* If we find the page in the page_cache, we cannot be sure
335 * how fresh the data is, so we will ignore readdir_plus attributes.
337 desc->timestamp_valid = 0;
338 page = read_cache_page(inode->i_mapping, desc->page_index,
339 (filler_t *)nfs_readdir_filler, desc);
341 status = PTR_ERR(page);
345 /* NOTE: Someone else may have changed the READDIRPLUS flag */
347 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
348 if (*desc->dir_cookie != 0)
349 status = find_dirent(desc);
351 status = find_dirent_index(desc);
353 dir_page_release(desc);
355 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
360 * Recurse through the page cache pages, and return a
361 * filled nfs_entry structure of the next directory entry if possible.
363 * The target for the search is '*desc->dir_cookie' if non-0,
364 * 'desc->file->f_pos' otherwise
367 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
372 /* Always search-by-index from the beginning of the cache */
373 if (*desc->dir_cookie == 0) {
374 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
375 (long long)desc->file->f_pos);
376 desc->page_index = 0;
377 desc->entry->cookie = desc->entry->prev_cookie = 0;
378 desc->entry->eof = 0;
379 desc->current_index = 0;
381 dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
382 (unsigned long long)*desc->dir_cookie);
385 res = find_dirent_page(desc);
388 /* Align to beginning of next page */
390 if (loop_count++ > 200) {
396 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res);
400 static inline unsigned int dt_type(struct inode *inode)
402 return (inode->i_mode >> 12) & 15;
405 static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
408 * Once we've found the start of the dirent within a page: fill 'er up...
411 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
414 struct file *file = desc->file;
415 struct nfs_entry *entry = desc->entry;
416 struct dentry *dentry = NULL;
421 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
422 (unsigned long long)entry->cookie);
425 unsigned d_type = DT_UNKNOWN;
426 /* Note: entry->prev_cookie contains the cookie for
427 * retrieving the current dirent on the server */
430 /* Get a dentry if we have one */
433 dentry = nfs_readdir_lookup(desc);
435 /* Use readdirplus info */
436 if (dentry != NULL && dentry->d_inode != NULL) {
437 d_type = dt_type(dentry->d_inode);
438 fileid = NFS_FILEID(dentry->d_inode);
441 res = filldir(dirent, entry->name, entry->len,
442 file->f_pos, nfs_compat_user_ino64(fileid),
447 *desc->dir_cookie = entry->cookie;
448 if (dir_decode(desc) != 0) {
452 if (loop_count++ > 200) {
457 dir_page_release(desc);
460 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
461 (unsigned long long)*desc->dir_cookie, res);
466 * If we cannot find a cookie in our cache, we suspect that this is
467 * because it points to a deleted file, so we ask the server to return
468 * whatever it thinks is the next entry. We then feed this to filldir.
469 * If all goes well, we should then be able to find our way round the
470 * cache on the next call to readdir_search_pagecache();
472 * NOTE: we cannot add the anonymous page to the pagecache because
473 * the data it contains might not be page aligned. Besides,
474 * we should already have a complete representation of the
475 * directory in the page cache by the time we get here.
478 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
481 struct file *file = desc->file;
482 struct inode *inode = file->f_path.dentry->d_inode;
483 struct rpc_cred *cred = nfs_file_cred(file);
484 struct page *page = NULL;
486 unsigned long timestamp, gencount;
488 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
489 (unsigned long long)*desc->dir_cookie);
491 page = alloc_page(GFP_HIGHUSER);
497 gencount = nfs_inc_attr_generation_counter();
498 status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred,
499 *desc->dir_cookie, page,
500 NFS_SERVER(inode)->dtsize,
503 desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
505 desc->timestamp = timestamp;
506 desc->gencount = gencount;
507 desc->timestamp_valid = 1;
508 if ((status = dir_decode(desc)) == 0)
509 desc->entry->prev_cookie = *desc->dir_cookie;
515 status = nfs_do_filldir(desc, dirent, filldir);
517 /* Reset read descriptor so it searches the page cache from
518 * the start upon the next call to readdir_search_pagecache() */
519 desc->page_index = 0;
520 desc->entry->cookie = desc->entry->prev_cookie = 0;
521 desc->entry->eof = 0;
523 dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
527 dir_page_release(desc);
531 /* The file offset position represents the dirent entry number. A
532 last cookie cache takes care of the common case of reading the
535 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
537 struct dentry *dentry = filp->f_path.dentry;
538 struct inode *inode = dentry->d_inode;
539 nfs_readdir_descriptor_t my_desc,
541 struct nfs_entry my_entry;
544 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
545 dentry->d_parent->d_name.name, dentry->d_name.name,
546 (long long)filp->f_pos);
547 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
550 * filp->f_pos points to the dirent entry number.
551 * *desc->dir_cookie has the cookie for the next entry. We have
552 * to either find the entry with the appropriate number or
553 * revalidate the cookie.
555 memset(desc, 0, sizeof(*desc));
558 desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
559 desc->decode = NFS_PROTO(inode)->decode_dirent;
560 desc->plus = NFS_USE_READDIRPLUS(inode);
562 my_entry.cookie = my_entry.prev_cookie = 0;
564 my_entry.fh = nfs_alloc_fhandle();
565 my_entry.fattr = nfs_alloc_fattr();
566 if (my_entry.fh == NULL || my_entry.fattr == NULL)
567 goto out_alloc_failed;
569 desc->entry = &my_entry;
571 nfs_block_sillyrename(dentry);
572 res = nfs_revalidate_mapping(inode, filp->f_mapping);
576 while(!desc->entry->eof) {
577 res = readdir_search_pagecache(desc);
579 if (res == -EBADCOOKIE) {
580 /* This means either end of directory */
581 if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
582 /* Or that the server has 'lost' a cookie */
583 res = uncached_readdir(desc, dirent, filldir);
590 if (res == -ETOOSMALL && desc->plus) {
591 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
592 nfs_zap_caches(inode);
594 desc->entry->eof = 0;
600 res = nfs_do_filldir(desc, dirent, filldir);
607 nfs_unblock_sillyrename(dentry);
611 nfs_free_fattr(my_entry.fattr);
612 nfs_free_fhandle(my_entry.fh);
613 dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
614 dentry->d_parent->d_name.name, dentry->d_name.name,
619 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
621 struct dentry *dentry = filp->f_path.dentry;
622 struct inode *inode = dentry->d_inode;
624 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
625 dentry->d_parent->d_name.name,
629 mutex_lock(&inode->i_mutex);
632 offset += filp->f_pos;
640 if (offset != filp->f_pos) {
641 filp->f_pos = offset;
642 nfs_file_open_context(filp)->dir_cookie = 0;
645 mutex_unlock(&inode->i_mutex);
650 * All directory operations under NFS are synchronous, so fsync()
651 * is a dummy operation.
653 static int nfs_fsync_dir(struct file *filp, int datasync)
655 struct dentry *dentry = filp->f_path.dentry;
657 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
658 dentry->d_parent->d_name.name, dentry->d_name.name,
661 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
666 * nfs_force_lookup_revalidate - Mark the directory as having changed
667 * @dir - pointer to directory inode
669 * This forces the revalidation code in nfs_lookup_revalidate() to do a
670 * full lookup on all child dentries of 'dir' whenever a change occurs
671 * on the server that might have invalidated our dcache.
673 * The caller should be holding dir->i_lock
675 void nfs_force_lookup_revalidate(struct inode *dir)
677 NFS_I(dir)->cache_change_attribute++;
681 * A check for whether or not the parent directory has changed.
682 * In the case it has, we assume that the dentries are untrustworthy
683 * and may need to be looked up again.
685 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
689 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
691 if (!nfs_verify_change_attribute(dir, dentry->d_time))
693 /* Revalidate nfsi->cache_change_attribute before we declare a match */
694 if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
696 if (!nfs_verify_change_attribute(dir, dentry->d_time))
702 * Return the intent data that applies to this particular path component
704 * Note that the current set of intents only apply to the very last
705 * component of the path.
706 * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
708 static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
710 if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
712 return nd->flags & mask;
716 * Use intent information to check whether or not we're going to do
717 * an O_EXCL create using this path component.
719 static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
721 if (NFS_PROTO(dir)->version == 2)
723 return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
727 * Inode and filehandle revalidation for lookups.
729 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
730 * or if the intent information indicates that we're about to open this
731 * particular file and the "nocto" mount flag is not set.
735 int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
737 struct nfs_server *server = NFS_SERVER(inode);
739 if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
742 /* VFS wants an on-the-wire revalidation */
743 if (nd->flags & LOOKUP_REVAL)
745 /* This is an open(2) */
746 if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
747 !(server->flags & NFS_MOUNT_NOCTO) &&
748 (S_ISREG(inode->i_mode) ||
749 S_ISDIR(inode->i_mode)))
753 return nfs_revalidate_inode(server, inode);
755 return __nfs_revalidate_inode(server, inode);
759 * We judge how long we want to trust negative
760 * dentries by looking at the parent inode mtime.
762 * If parent mtime has changed, we revalidate, else we wait for a
763 * period corresponding to the parent's attribute cache timeout value.
766 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
767 struct nameidata *nd)
769 /* Don't revalidate a negative dentry if we're creating a new file */
770 if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
772 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
774 return !nfs_check_verifier(dir, dentry);
778 * This is called every time the dcache has a lookup hit,
779 * and we should check whether we can really trust that
782 * NOTE! The hit can be a negative hit too, don't assume
785 * If the parent directory is seen to have changed, we throw out the
786 * cached dentry and do a new lookup.
788 static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
792 struct dentry *parent;
793 struct nfs_fh *fhandle = NULL;
794 struct nfs_fattr *fattr = NULL;
797 parent = dget_parent(dentry);
798 dir = parent->d_inode;
799 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
800 inode = dentry->d_inode;
803 if (nfs_neg_need_reval(dir, dentry, nd))
808 if (is_bad_inode(inode)) {
809 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
810 __func__, dentry->d_parent->d_name.name,
811 dentry->d_name.name);
815 if (nfs_have_delegation(inode, FMODE_READ))
816 goto out_set_verifier;
818 /* Force a full look up iff the parent directory has changed */
819 if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
820 if (nfs_lookup_verify_inode(inode, nd))
825 if (NFS_STALE(inode))
829 fhandle = nfs_alloc_fhandle();
830 fattr = nfs_alloc_fattr();
831 if (fhandle == NULL || fattr == NULL)
834 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
837 if (nfs_compare_fh(NFS_FH(inode), fhandle))
839 if ((error = nfs_refresh_inode(inode, fattr)) != 0)
842 nfs_free_fattr(fattr);
843 nfs_free_fhandle(fhandle);
845 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
848 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
849 __func__, dentry->d_parent->d_name.name,
850 dentry->d_name.name);
855 nfs_mark_for_revalidate(dir);
856 if (inode && S_ISDIR(inode->i_mode)) {
857 /* Purge readdir caches. */
858 nfs_zap_caches(inode);
859 /* If we have submounts, don't unhash ! */
860 if (have_submounts(dentry))
862 if (dentry->d_flags & DCACHE_DISCONNECTED)
864 shrink_dcache_parent(dentry);
867 nfs_free_fattr(fattr);
868 nfs_free_fhandle(fhandle);
870 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
871 __func__, dentry->d_parent->d_name.name,
872 dentry->d_name.name);
875 nfs_free_fattr(fattr);
876 nfs_free_fhandle(fhandle);
878 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
879 __func__, dentry->d_parent->d_name.name,
880 dentry->d_name.name, error);
885 * This is called from dput() when d_count is going to 0.
887 static int nfs_dentry_delete(struct dentry *dentry)
889 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
890 dentry->d_parent->d_name.name, dentry->d_name.name,
893 /* Unhash any dentry with a stale inode */
894 if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
897 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
898 /* Unhash it, so that ->d_iput() would be called */
901 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
902 /* Unhash it, so that ancestors of killed async unlink
903 * files will be cleaned up during umount */
910 static void nfs_drop_nlink(struct inode *inode)
912 spin_lock(&inode->i_lock);
913 if (inode->i_nlink > 0)
915 spin_unlock(&inode->i_lock);
919 * Called when the dentry loses inode.
920 * We use it to clean up silly-renamed files.
922 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
924 if (S_ISDIR(inode->i_mode))
925 /* drop any readdir cache as it could easily be old */
926 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
928 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
930 nfs_complete_unlink(dentry, inode);
935 const struct dentry_operations nfs_dentry_operations = {
936 .d_revalidate = nfs_lookup_revalidate,
937 .d_delete = nfs_dentry_delete,
938 .d_iput = nfs_dentry_iput,
941 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
944 struct dentry *parent;
945 struct inode *inode = NULL;
946 struct nfs_fh *fhandle = NULL;
947 struct nfs_fattr *fattr = NULL;
950 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
951 dentry->d_parent->d_name.name, dentry->d_name.name);
952 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
954 res = ERR_PTR(-ENAMETOOLONG);
955 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
958 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
961 * If we're doing an exclusive create, optimize away the lookup
962 * but don't hash the dentry.
964 if (nfs_is_exclusive_create(dir, nd)) {
965 d_instantiate(dentry, NULL);
970 res = ERR_PTR(-ENOMEM);
971 fhandle = nfs_alloc_fhandle();
972 fattr = nfs_alloc_fattr();
973 if (fhandle == NULL || fattr == NULL)
976 parent = dentry->d_parent;
977 /* Protect against concurrent sillydeletes */
978 nfs_block_sillyrename(parent);
979 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
980 if (error == -ENOENT)
983 res = ERR_PTR(error);
984 goto out_unblock_sillyrename;
986 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
987 res = (struct dentry *)inode;
989 goto out_unblock_sillyrename;
992 res = d_materialise_unique(dentry, inode);
995 goto out_unblock_sillyrename;
998 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
999 out_unblock_sillyrename:
1000 nfs_unblock_sillyrename(parent);
1002 nfs_free_fattr(fattr);
1003 nfs_free_fhandle(fhandle);
1007 #ifdef CONFIG_NFS_V4
1008 static int nfs_open_revalidate(struct dentry *, struct nameidata *);
1010 const struct dentry_operations nfs4_dentry_operations = {
1011 .d_revalidate = nfs_open_revalidate,
1012 .d_delete = nfs_dentry_delete,
1013 .d_iput = nfs_dentry_iput,
1017 * Use intent information to determine whether we need to substitute
1018 * the NFSv4-style stateful OPEN for the LOOKUP call
1020 static int is_atomic_open(struct nameidata *nd)
1022 if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
1024 /* NFS does not (yet) have a stateful open for directories */
1025 if (nd->flags & LOOKUP_DIRECTORY)
1027 /* Are we trying to write to a read only partition? */
1028 if (__mnt_is_readonly(nd->path.mnt) &&
1029 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
1034 static struct nfs_open_context *nameidata_to_nfs_open_context(struct dentry *dentry, struct nameidata *nd)
1036 struct path path = {
1037 .mnt = nd->path.mnt,
1040 struct nfs_open_context *ctx;
1041 struct rpc_cred *cred;
1042 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1044 cred = rpc_lookup_cred();
1046 return ERR_CAST(cred);
1047 ctx = alloc_nfs_open_context(&path, cred, fmode);
1050 return ERR_PTR(-ENOMEM);
1054 static int do_open(struct inode *inode, struct file *filp)
1056 nfs_fscache_set_inode_cookie(inode, filp);
1060 static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
1065 /* If the open_intent is for execute, we have an extra check to make */
1066 if (ctx->mode & FMODE_EXEC) {
1067 ret = nfs_may_open(ctx->path.dentry->d_inode,
1069 nd->intent.open.flags);
1073 filp = lookup_instantiate_filp(nd, ctx->path.dentry, do_open);
1075 ret = PTR_ERR(filp);
1077 nfs_file_set_open_context(filp, ctx);
1079 put_nfs_open_context(ctx);
1083 static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1085 struct nfs_open_context *ctx;
1087 struct dentry *res = NULL;
1088 struct inode *inode;
1091 dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
1092 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1094 /* Check that we are indeed trying to open this file */
1095 if (!is_atomic_open(nd))
1098 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
1099 res = ERR_PTR(-ENAMETOOLONG);
1102 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1104 /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
1106 if (nd->flags & LOOKUP_EXCL) {
1107 d_instantiate(dentry, NULL);
1111 ctx = nameidata_to_nfs_open_context(dentry, nd);
1112 res = ERR_CAST(ctx);
1116 open_flags = nd->intent.open.flags;
1117 if (nd->flags & LOOKUP_CREATE) {
1118 attr.ia_mode = nd->intent.open.create_mode;
1119 attr.ia_valid = ATTR_MODE;
1120 if (!IS_POSIXACL(dir))
1121 attr.ia_mode &= ~current_umask();
1123 open_flags &= ~O_EXCL;
1125 BUG_ON(open_flags & O_CREAT);
1128 /* Open the file on the server */
1129 nfs_block_sillyrename(dentry->d_parent);
1130 inode = nfs4_atomic_open(dir, ctx, open_flags, &attr);
1131 if (IS_ERR(inode)) {
1132 nfs_unblock_sillyrename(dentry->d_parent);
1133 put_nfs_open_context(ctx);
1134 switch (PTR_ERR(inode)) {
1135 /* Make a negative dentry */
1137 d_add(dentry, NULL);
1140 /* This turned out not to be a regular file */
1145 if (!(nd->intent.open.flags & O_NOFOLLOW))
1149 res = ERR_CAST(inode);
1153 res = d_add_unique(dentry, inode);
1155 dput(ctx->path.dentry);
1156 ctx->path.dentry = dget(res);
1159 nfs_intent_set_file(nd, ctx);
1160 nfs_unblock_sillyrename(dentry->d_parent);
1162 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1165 return nfs_lookup(dir, dentry, nd);
1168 static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1170 struct dentry *parent = NULL;
1171 struct inode *inode = dentry->d_inode;
1173 struct nfs_open_context *ctx;
1174 int openflags, ret = 0;
1176 if (!is_atomic_open(nd) || d_mountpoint(dentry))
1178 parent = dget_parent(dentry);
1179 dir = parent->d_inode;
1180 /* We can't create new files in nfs_open_revalidate(), so we
1181 * optimize away revalidation of negative dentries.
1183 if (inode == NULL) {
1184 if (!nfs_neg_need_reval(dir, dentry, nd))
1189 /* NFS only supports OPEN on regular files */
1190 if (!S_ISREG(inode->i_mode))
1192 openflags = nd->intent.open.flags;
1193 /* We cannot do exclusive creation on a positive dentry */
1194 if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1196 /* We can't create new files, or truncate existing ones here */
1197 openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
1199 ctx = nameidata_to_nfs_open_context(dentry, nd);
1204 * Note: we're not holding inode->i_mutex and so may be racing with
1205 * operations that change the directory. We therefore save the
1206 * change attribute *before* we do the RPC call.
1208 inode = nfs4_atomic_open(dir, ctx, openflags, NULL);
1209 if (IS_ERR(inode)) {
1210 ret = PTR_ERR(inode);
1223 if (inode == dentry->d_inode) {
1224 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1225 nfs_intent_set_file(nd, ctx);
1235 put_nfs_open_context(ctx);
1241 return nfs_lookup_revalidate(dentry, nd);
1244 static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
1245 struct nameidata *nd)
1247 struct nfs_open_context *ctx = NULL;
1252 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1253 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1255 attr.ia_mode = mode;
1256 attr.ia_valid = ATTR_MODE;
1258 if ((nd->flags & LOOKUP_CREATE) != 0) {
1259 open_flags = nd->intent.open.flags;
1261 ctx = nameidata_to_nfs_open_context(dentry, nd);
1262 error = PTR_ERR(ctx);
1267 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
1271 nfs_intent_set_file(nd, ctx);
1275 put_nfs_open_context(ctx);
1281 #endif /* CONFIG_NFSV4 */
1283 static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
1285 struct dentry *parent = desc->file->f_path.dentry;
1286 struct inode *dir = parent->d_inode;
1287 struct nfs_entry *entry = desc->entry;
1288 struct dentry *dentry, *alias;
1289 struct qstr name = {
1290 .name = entry->name,
1293 struct inode *inode;
1294 unsigned long verf = nfs_save_change_attribute(dir);
1298 if (name.name[0] == '.' && name.name[1] == '.')
1299 return dget_parent(parent);
1302 if (name.name[0] == '.')
1303 return dget(parent);
1306 spin_lock(&dir->i_lock);
1307 if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
1308 spin_unlock(&dir->i_lock);
1311 spin_unlock(&dir->i_lock);
1313 name.hash = full_name_hash(name.name, name.len);
1314 dentry = d_lookup(parent, &name);
1315 if (dentry != NULL) {
1316 /* Is this a positive dentry that matches the readdir info? */
1317 if (dentry->d_inode != NULL &&
1318 (NFS_FILEID(dentry->d_inode) == entry->ino ||
1319 d_mountpoint(dentry))) {
1320 if (!desc->plus || entry->fh->size == 0)
1322 if (nfs_compare_fh(NFS_FH(dentry->d_inode),
1326 /* No, so d_drop to allow one to be created */
1330 if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
1332 if (name.len > NFS_SERVER(dir)->namelen)
1334 /* Note: caller is already holding the dir->i_mutex! */
1335 dentry = d_alloc(parent, &name);
1338 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1339 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
1340 if (IS_ERR(inode)) {
1345 alias = d_materialise_unique(dentry, inode);
1346 if (alias != NULL) {
1354 nfs_set_verifier(dentry, verf);
1359 * Code common to create, mkdir, and mknod.
1361 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1362 struct nfs_fattr *fattr)
1364 struct dentry *parent = dget_parent(dentry);
1365 struct inode *dir = parent->d_inode;
1366 struct inode *inode;
1367 int error = -EACCES;
1371 /* We may have been initialized further down */
1372 if (dentry->d_inode)
1374 if (fhandle->size == 0) {
1375 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1379 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1380 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1381 struct nfs_server *server = NFS_SB(dentry->d_sb);
1382 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
1386 inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1387 error = PTR_ERR(inode);
1390 d_add(dentry, inode);
1395 nfs_mark_for_revalidate(dir);
1401 * Following a failed create operation, we drop the dentry rather
1402 * than retain a negative dentry. This avoids a problem in the event
1403 * that the operation succeeded on the server, but an error in the
1404 * reply path made it appear to have failed.
1406 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1407 struct nameidata *nd)
1412 dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1413 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1415 attr.ia_mode = mode;
1416 attr.ia_valid = ATTR_MODE;
1418 error = NFS_PROTO(dir)->create(dir, dentry, &attr, 0, NULL);
1428 * See comments for nfs_proc_create regarding failed operations.
1431 nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1436 dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1437 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1439 if (!new_valid_dev(rdev))
1442 attr.ia_mode = mode;
1443 attr.ia_valid = ATTR_MODE;
1445 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1455 * See comments for nfs_proc_create regarding failed operations.
1457 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1462 dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1463 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1465 attr.ia_valid = ATTR_MODE;
1466 attr.ia_mode = mode | S_IFDIR;
1468 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1477 static void nfs_dentry_handle_enoent(struct dentry *dentry)
1479 if (dentry->d_inode != NULL && !d_unhashed(dentry))
1483 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1487 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1488 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1490 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1491 /* Ensure the VFS deletes this inode */
1492 if (error == 0 && dentry->d_inode != NULL)
1493 clear_nlink(dentry->d_inode);
1494 else if (error == -ENOENT)
1495 nfs_dentry_handle_enoent(dentry);
1500 static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
1502 static unsigned int sillycounter;
1503 const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
1504 const int countersize = sizeof(sillycounter)*2;
1505 const int slen = sizeof(".nfs")+fileidsize+countersize-1;
1508 struct dentry *sdentry;
1511 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
1512 dentry->d_parent->d_name.name, dentry->d_name.name,
1513 atomic_read(&dentry->d_count));
1514 nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
1517 * We don't allow a dentry to be silly-renamed twice.
1520 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1523 sprintf(silly, ".nfs%*.*Lx",
1524 fileidsize, fileidsize,
1525 (unsigned long long)NFS_FILEID(dentry->d_inode));
1527 /* Return delegation in anticipation of the rename */
1528 nfs_inode_return_delegation(dentry->d_inode);
1532 char *suffix = silly + slen - countersize;
1536 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
1538 dfprintk(VFS, "NFS: trying to rename %s to %s\n",
1539 dentry->d_name.name, silly);
1541 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
1543 * N.B. Better to return EBUSY here ... it could be
1544 * dangerous to delete the file while it's in use.
1546 if (IS_ERR(sdentry))
1548 } while(sdentry->d_inode != NULL); /* need negative lookup */
1550 qsilly.name = silly;
1551 qsilly.len = strlen(silly);
1552 if (dentry->d_inode) {
1553 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1555 nfs_mark_for_revalidate(dentry->d_inode);
1557 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1560 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1561 d_move(dentry, sdentry);
1562 error = nfs_async_unlink(dir, dentry);
1563 /* If we return 0 we don't unlink */
1571 * Remove a file after making sure there are no pending writes,
1572 * and after checking that the file has only one user.
1574 * We invalidate the attribute cache and free the inode prior to the operation
1575 * to avoid possible races if the server reuses the inode.
1577 static int nfs_safe_remove(struct dentry *dentry)
1579 struct inode *dir = dentry->d_parent->d_inode;
1580 struct inode *inode = dentry->d_inode;
1583 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1584 dentry->d_parent->d_name.name, dentry->d_name.name);
1586 /* If the dentry was sillyrenamed, we simply call d_delete() */
1587 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1592 if (inode != NULL) {
1593 nfs_inode_return_delegation(inode);
1594 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1595 /* The VFS may want to delete this inode */
1597 nfs_drop_nlink(inode);
1598 nfs_mark_for_revalidate(inode);
1600 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1601 if (error == -ENOENT)
1602 nfs_dentry_handle_enoent(dentry);
1607 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1608 * belongs to an active ".nfs..." file and we return -EBUSY.
1610 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1612 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1615 int need_rehash = 0;
1617 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1618 dir->i_ino, dentry->d_name.name);
1620 spin_lock(&dcache_lock);
1621 spin_lock(&dentry->d_lock);
1622 if (atomic_read(&dentry->d_count) > 1) {
1623 spin_unlock(&dentry->d_lock);
1624 spin_unlock(&dcache_lock);
1625 /* Start asynchronous writeout of the inode */
1626 write_inode_now(dentry->d_inode, 0);
1627 error = nfs_sillyrename(dir, dentry);
1630 if (!d_unhashed(dentry)) {
1634 spin_unlock(&dentry->d_lock);
1635 spin_unlock(&dcache_lock);
1636 error = nfs_safe_remove(dentry);
1637 if (!error || error == -ENOENT) {
1638 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1639 } else if (need_rehash)
1645 * To create a symbolic link, most file systems instantiate a new inode,
1646 * add a page to it containing the path, then write it out to the disk
1647 * using prepare_write/commit_write.
1649 * Unfortunately the NFS client can't create the in-core inode first
1650 * because it needs a file handle to create an in-core inode (see
1651 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
1652 * symlink request has completed on the server.
1654 * So instead we allocate a raw page, copy the symname into it, then do
1655 * the SYMLINK request with the page as the buffer. If it succeeds, we
1656 * now have a new file handle and can instantiate an in-core NFS inode
1657 * and move the raw page into its mapping.
1659 static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1661 struct pagevec lru_pvec;
1665 unsigned int pathlen = strlen(symname);
1668 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1669 dir->i_ino, dentry->d_name.name, symname);
1671 if (pathlen > PAGE_SIZE)
1672 return -ENAMETOOLONG;
1674 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1675 attr.ia_valid = ATTR_MODE;
1677 page = alloc_page(GFP_HIGHUSER);
1681 kaddr = kmap_atomic(page, KM_USER0);
1682 memcpy(kaddr, symname, pathlen);
1683 if (pathlen < PAGE_SIZE)
1684 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1685 kunmap_atomic(kaddr, KM_USER0);
1687 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1689 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1690 dir->i_sb->s_id, dir->i_ino,
1691 dentry->d_name.name, symname, error);
1698 * No big deal if we can't add this page to the page cache here.
1699 * READLINK will get the missing page from the server if needed.
1701 pagevec_init(&lru_pvec, 0);
1702 if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
1704 pagevec_add(&lru_pvec, page);
1705 pagevec_lru_add_file(&lru_pvec);
1706 SetPageUptodate(page);
1715 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1717 struct inode *inode = old_dentry->d_inode;
1720 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1721 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1722 dentry->d_parent->d_name.name, dentry->d_name.name);
1724 nfs_inode_return_delegation(inode);
1727 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1729 atomic_inc(&inode->i_count);
1730 d_add(dentry, inode);
1737 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1738 * different file handle for the same inode after a rename (e.g. when
1739 * moving to a different directory). A fail-safe method to do so would
1740 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1741 * rename the old file using the sillyrename stuff. This way, the original
1742 * file in old_dir will go away when the last process iput()s the inode.
1746 * It actually works quite well. One needs to have the possibility for
1747 * at least one ".nfs..." file in each directory the file ever gets
1748 * moved or linked to which happens automagically with the new
1749 * implementation that only depends on the dcache stuff instead of
1750 * using the inode layer
1752 * Unfortunately, things are a little more complicated than indicated
1753 * above. For a cross-directory move, we want to make sure we can get
1754 * rid of the old inode after the operation. This means there must be
1755 * no pending writes (if it's a file), and the use count must be 1.
1756 * If these conditions are met, we can drop the dentries before doing
1759 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1760 struct inode *new_dir, struct dentry *new_dentry)
1762 struct inode *old_inode = old_dentry->d_inode;
1763 struct inode *new_inode = new_dentry->d_inode;
1764 struct dentry *dentry = NULL, *rehash = NULL;
1767 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1768 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1769 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1770 atomic_read(&new_dentry->d_count));
1773 * For non-directories, check whether the target is busy and if so,
1774 * make a copy of the dentry and then do a silly-rename. If the
1775 * silly-rename succeeds, the copied dentry is hashed and becomes
1778 if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1780 * To prevent any new references to the target during the
1781 * rename, we unhash the dentry in advance.
1783 if (!d_unhashed(new_dentry)) {
1785 rehash = new_dentry;
1788 if (atomic_read(&new_dentry->d_count) > 2) {
1791 /* copy the target dentry's name */
1792 dentry = d_alloc(new_dentry->d_parent,
1793 &new_dentry->d_name);
1797 /* silly-rename the existing target ... */
1798 err = nfs_sillyrename(new_dir, new_dentry);
1802 new_dentry = dentry;
1808 nfs_inode_return_delegation(old_inode);
1809 if (new_inode != NULL)
1810 nfs_inode_return_delegation(new_inode);
1812 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1813 new_dir, &new_dentry->d_name);
1814 nfs_mark_for_revalidate(old_inode);
1819 if (new_inode != NULL)
1820 nfs_drop_nlink(new_inode);
1821 d_move(old_dentry, new_dentry);
1822 nfs_set_verifier(new_dentry,
1823 nfs_save_change_attribute(new_dir));
1824 } else if (error == -ENOENT)
1825 nfs_dentry_handle_enoent(old_dentry);
1827 /* new dentry created? */
1833 static DEFINE_SPINLOCK(nfs_access_lru_lock);
1834 static LIST_HEAD(nfs_access_lru_list);
1835 static atomic_long_t nfs_access_nr_entries;
1837 static void nfs_access_free_entry(struct nfs_access_entry *entry)
1839 put_rpccred(entry->cred);
1841 smp_mb__before_atomic_dec();
1842 atomic_long_dec(&nfs_access_nr_entries);
1843 smp_mb__after_atomic_dec();
1846 static void nfs_access_free_list(struct list_head *head)
1848 struct nfs_access_entry *cache;
1850 while (!list_empty(head)) {
1851 cache = list_entry(head->next, struct nfs_access_entry, lru);
1852 list_del(&cache->lru);
1853 nfs_access_free_entry(cache);
1857 int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
1860 struct nfs_inode *nfsi;
1861 struct nfs_access_entry *cache;
1863 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
1864 return (nr_to_scan == 0) ? 0 : -1;
1866 spin_lock(&nfs_access_lru_lock);
1867 list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
1868 struct inode *inode;
1870 if (nr_to_scan-- == 0)
1872 inode = &nfsi->vfs_inode;
1873 spin_lock(&inode->i_lock);
1874 if (list_empty(&nfsi->access_cache_entry_lru))
1875 goto remove_lru_entry;
1876 cache = list_entry(nfsi->access_cache_entry_lru.next,
1877 struct nfs_access_entry, lru);
1878 list_move(&cache->lru, &head);
1879 rb_erase(&cache->rb_node, &nfsi->access_cache);
1880 if (!list_empty(&nfsi->access_cache_entry_lru))
1881 list_move_tail(&nfsi->access_cache_inode_lru,
1882 &nfs_access_lru_list);
1885 list_del_init(&nfsi->access_cache_inode_lru);
1886 smp_mb__before_clear_bit();
1887 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
1888 smp_mb__after_clear_bit();
1890 spin_unlock(&inode->i_lock);
1892 spin_unlock(&nfs_access_lru_lock);
1893 nfs_access_free_list(&head);
1894 return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
1897 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
1899 struct rb_root *root_node = &nfsi->access_cache;
1901 struct nfs_access_entry *entry;
1903 /* Unhook entries from the cache */
1904 while ((n = rb_first(root_node)) != NULL) {
1905 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1906 rb_erase(n, root_node);
1907 list_move(&entry->lru, head);
1909 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
1912 void nfs_access_zap_cache(struct inode *inode)
1916 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
1918 /* Remove from global LRU init */
1919 spin_lock(&nfs_access_lru_lock);
1920 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
1921 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1923 spin_lock(&inode->i_lock);
1924 __nfs_access_zap_cache(NFS_I(inode), &head);
1925 spin_unlock(&inode->i_lock);
1926 spin_unlock(&nfs_access_lru_lock);
1927 nfs_access_free_list(&head);
1930 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
1932 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
1933 struct nfs_access_entry *entry;
1936 entry = rb_entry(n, struct nfs_access_entry, rb_node);
1938 if (cred < entry->cred)
1940 else if (cred > entry->cred)
1948 static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
1950 struct nfs_inode *nfsi = NFS_I(inode);
1951 struct nfs_access_entry *cache;
1954 spin_lock(&inode->i_lock);
1955 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
1957 cache = nfs_access_search_rbtree(inode, cred);
1960 if (!nfs_have_delegated_attributes(inode) &&
1961 !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
1963 res->jiffies = cache->jiffies;
1964 res->cred = cache->cred;
1965 res->mask = cache->mask;
1966 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
1969 spin_unlock(&inode->i_lock);
1972 rb_erase(&cache->rb_node, &nfsi->access_cache);
1973 list_del(&cache->lru);
1974 spin_unlock(&inode->i_lock);
1975 nfs_access_free_entry(cache);
1978 spin_unlock(&inode->i_lock);
1979 nfs_access_zap_cache(inode);
1983 static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
1985 struct nfs_inode *nfsi = NFS_I(inode);
1986 struct rb_root *root_node = &nfsi->access_cache;
1987 struct rb_node **p = &root_node->rb_node;
1988 struct rb_node *parent = NULL;
1989 struct nfs_access_entry *entry;
1991 spin_lock(&inode->i_lock);
1992 while (*p != NULL) {
1994 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
1996 if (set->cred < entry->cred)
1997 p = &parent->rb_left;
1998 else if (set->cred > entry->cred)
1999 p = &parent->rb_right;
2003 rb_link_node(&set->rb_node, parent, p);
2004 rb_insert_color(&set->rb_node, root_node);
2005 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2006 spin_unlock(&inode->i_lock);
2009 rb_replace_node(parent, &set->rb_node, root_node);
2010 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2011 list_del(&entry->lru);
2012 spin_unlock(&inode->i_lock);
2013 nfs_access_free_entry(entry);
2016 static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
2018 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2021 RB_CLEAR_NODE(&cache->rb_node);
2022 cache->jiffies = set->jiffies;
2023 cache->cred = get_rpccred(set->cred);
2024 cache->mask = set->mask;
2026 nfs_access_add_rbtree(inode, cache);
2028 /* Update accounting */
2029 smp_mb__before_atomic_inc();
2030 atomic_long_inc(&nfs_access_nr_entries);
2031 smp_mb__after_atomic_inc();
2033 /* Add inode to global LRU list */
2034 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2035 spin_lock(&nfs_access_lru_lock);
2036 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2037 list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2038 &nfs_access_lru_list);
2039 spin_unlock(&nfs_access_lru_lock);
2043 static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2045 struct nfs_access_entry cache;
2048 status = nfs_access_get_cached(inode, cred, &cache);
2052 /* Be clever: ask server to check for all possible rights */
2053 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2055 cache.jiffies = jiffies;
2056 status = NFS_PROTO(inode)->access(inode, &cache);
2058 if (status == -ESTALE) {
2059 nfs_zap_caches(inode);
2060 if (!S_ISDIR(inode->i_mode))
2061 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2065 nfs_access_add_cache(inode, &cache);
2067 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2072 static int nfs_open_permission_mask(int openflags)
2076 if (openflags & FMODE_READ)
2078 if (openflags & FMODE_WRITE)
2080 if (openflags & FMODE_EXEC)
2085 int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2087 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2090 int nfs_permission(struct inode *inode, int mask)
2092 struct rpc_cred *cred;
2095 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2097 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2099 /* Is this sys_access() ? */
2100 if (mask & (MAY_ACCESS | MAY_CHDIR))
2103 switch (inode->i_mode & S_IFMT) {
2107 /* NFSv4 has atomic_open... */
2108 if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
2109 && (mask & MAY_OPEN)
2110 && !(mask & MAY_EXEC))
2115 * Optimize away all write operations, since the server
2116 * will check permissions when we perform the op.
2118 if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2123 if (!NFS_PROTO(inode)->access)
2126 cred = rpc_lookup_cred();
2127 if (!IS_ERR(cred)) {
2128 res = nfs_do_access(inode, cred, mask);
2131 res = PTR_ERR(cred);
2133 if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2136 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2137 inode->i_sb->s_id, inode->i_ino, mask, res);
2140 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2142 res = generic_permission(inode, mask, NULL);
2148 * version-control: t
2149 * kept-new-versions: 5