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/sched.h>
21 #include <linux/errno.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/pagemap.h>
32 #include <linux/smp_lock.h>
34 #define NFS_PARANOIA 1
35 /* #define NFS_DEBUG_VERBOSE 1 */
37 static int nfs_readdir(struct file *, void *, filldir_t);
38 static struct dentry *nfs_lookup(struct inode *, struct dentry *);
39 static int nfs_create(struct inode *, struct dentry *, int);
40 static int nfs_mkdir(struct inode *, struct dentry *, int);
41 static int nfs_rmdir(struct inode *, struct dentry *);
42 static int nfs_unlink(struct inode *, struct dentry *);
43 static int nfs_symlink(struct inode *, struct dentry *, const char *);
44 static int nfs_link(struct dentry *, struct inode *, struct dentry *);
45 static int nfs_mknod(struct inode *, struct dentry *, int, int);
46 static int nfs_rename(struct inode *, struct dentry *,
47 struct inode *, struct dentry *);
48 static int nfs_fsync_dir(struct file *, struct dentry *, int);
50 struct file_operations nfs_dir_operations = {
51 read: generic_read_dir,
58 struct inode_operations nfs_dir_inode_operations = {
68 permission: nfs_permission,
69 revalidate: nfs_revalidate,
70 setattr: nfs_notify_change,
73 typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
77 unsigned long page_index;
80 struct nfs_entry *entry;
81 decode_dirent_t decode;
84 } nfs_readdir_descriptor_t;
86 /* Now we cache directories properly, by stuffing the dirent
87 * data directly in the page cache.
89 * Inode invalidation due to refresh etc. takes care of
90 * _everything_, no sloppy entry flushing logic, no extraneous
91 * copying, network direct to page cache, the way it was meant
94 * NOTE: Dirent information verification is done always by the
95 * page-in of the RPC reply, nowhere else, this simplies
96 * things substantially.
99 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
101 struct file *file = desc->file;
102 struct inode *inode = file->f_dentry->d_inode;
103 struct rpc_cred *cred = nfs_file_cred(file);
106 dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
109 error = NFS_PROTO(inode)->readdir(inode, cred, desc->entry->cookie, page,
110 NFS_SERVER(inode)->dtsize, desc->plus);
111 /* We requested READDIRPLUS, but the server doesn't grok it */
112 if (desc->plus && error == -ENOTSUPP) {
113 NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
119 SetPageUptodate(page);
120 /* Ensure consistent page alignment of the data.
121 * Note: assumes we have exclusive access to this mapping either
122 * throught inode->i_sem or some other mechanism.
124 if (page->index == 0)
125 invalidate_inode_pages(inode);
131 invalidate_inode_pages(inode);
137 int dir_decode(nfs_readdir_descriptor_t *desc)
140 p = desc->decode(p, desc->entry, desc->plus);
148 void dir_page_release(nfs_readdir_descriptor_t *desc)
151 page_cache_release(desc->page);
157 * Given a pointer to a buffer that has already been filled by a call
158 * to readdir, find the next entry.
160 * If the end of the buffer has been reached, return -EAGAIN, if not,
161 * return the offset within the buffer of the next entry to be
165 int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
167 struct nfs_entry *entry = desc->entry;
171 while((status = dir_decode(desc)) == 0) {
172 dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
173 if (entry->prev_cookie == desc->target)
175 if (loop_count++ > 200) {
180 dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
185 * Find the given page, and call find_dirent() in order to try to
186 * return the next entry.
189 int find_dirent_page(nfs_readdir_descriptor_t *desc)
191 struct inode *inode = desc->file->f_dentry->d_inode;
195 dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
197 desc->plus = NFS_USE_READDIRPLUS(inode);
198 page = read_cache_page(&inode->i_data, desc->page_index,
199 (filler_t *)nfs_readdir_filler, desc);
201 status = PTR_ERR(page);
204 if (!Page_Uptodate(page))
207 /* NOTE: Someone else may have changed the READDIRPLUS flag */
209 desc->ptr = kmap(page);
210 status = find_dirent(desc, page);
212 dir_page_release(desc);
214 dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
217 page_cache_release(page);
222 * Recurse through the page cache pages, and return a
223 * filled nfs_entry structure of the next directory entry if possible.
225 * The target for the search is 'desc->target'.
228 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
233 dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
235 res = find_dirent_page(desc);
238 /* Align to beginning of next page */
240 if (loop_count++ > 200) {
245 dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
250 * Once we've found the start of the dirent within a page: fill 'er up...
253 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
256 struct file *file = desc->file;
257 struct nfs_entry *entry = desc->entry;
258 unsigned long fileid;
262 dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
265 /* Note: entry->prev_cookie contains the cookie for
266 * retrieving the current dirent on the server */
267 fileid = nfs_fileid_to_ino_t(entry->ino);
268 res = filldir(dirent, entry->name, entry->len,
269 entry->prev_cookie, fileid, DT_UNKNOWN);
272 file->f_pos = desc->target = entry->cookie;
273 if (dir_decode(desc) != 0) {
277 if (loop_count++ > 200) {
282 dir_page_release(desc);
284 dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
289 * If we cannot find a cookie in our cache, we suspect that this is
290 * because it points to a deleted file, so we ask the server to return
291 * whatever it thinks is the next entry. We then feed this to filldir.
292 * If all goes well, we should then be able to find our way round the
293 * cache on the next call to readdir_search_pagecache();
295 * NOTE: we cannot add the anonymous page to the pagecache because
296 * the data it contains might not be page aligned. Besides,
297 * we should already have a complete representation of the
298 * directory in the page cache by the time we get here.
301 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
304 struct file *file = desc->file;
305 struct inode *inode = file->f_dentry->d_inode;
306 struct rpc_cred *cred = nfs_file_cred(file);
307 struct page *page = NULL;
310 dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
312 page = alloc_page(GFP_HIGHUSER);
317 desc->error = NFS_PROTO(inode)->readdir(inode, cred, desc->target,
319 NFS_SERVER(inode)->dtsize,
322 desc->ptr = kmap(page);
323 if (desc->error >= 0) {
324 if ((status = dir_decode(desc)) == 0)
325 desc->entry->prev_cookie = desc->target;
331 status = nfs_do_filldir(desc, dirent, filldir);
333 /* Reset read descriptor so it searches the page cache from
334 * the start upon the next call to readdir_search_pagecache() */
335 desc->page_index = 0;
336 memset(desc->entry, 0, sizeof(*desc->entry));
338 dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
341 dir_page_release(desc);
345 /* The file offset position is now represented as a true offset into the
346 * page cache as is the case in most of the other filesystems.
348 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
350 struct dentry *dentry = filp->f_dentry;
351 struct inode *inode = dentry->d_inode;
352 nfs_readdir_descriptor_t my_desc,
354 struct nfs_entry my_entry;
357 res = nfs_revalidate(dentry);
362 * filp->f_pos points to the file offset in the page cache.
363 * but if the cache has meanwhile been zapped, we need to
364 * read from the last dirent to revalidate f_pos
367 memset(desc, 0, sizeof(*desc));
368 memset(&my_entry, 0, sizeof(my_entry));
371 desc->target = filp->f_pos;
372 desc->entry = &my_entry;
373 desc->decode = NFS_PROTO(inode)->decode_dirent;
375 while(!desc->entry->eof) {
376 res = readdir_search_pagecache(desc);
377 if (res == -EBADCOOKIE) {
378 /* This means either end of directory */
379 if (desc->entry->cookie != desc->target) {
380 /* Or that the server has 'lost' a cookie */
381 res = uncached_readdir(desc, dirent, filldir);
390 res = nfs_do_filldir(desc, dirent, filldir);
404 * All directory operations under NFS are synchronous, so fsync()
405 * is a dummy operation.
407 int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
413 * A check for whether or not the parent directory has changed.
414 * In the case it has, we assume that the dentries are untrustworthy
415 * and may need to be looked up again.
418 int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
422 if (nfs_revalidate_inode(NFS_SERVER(dir), dir))
424 return time_after(dentry->d_time, NFS_MTIME_UPDATE(dir));
428 * Whenever an NFS operation succeeds, we know that the dentry
429 * is valid, so we update the revalidation timestamp.
431 static inline void nfs_renew_times(struct dentry * dentry)
433 dentry->d_time = jiffies;
437 int nfs_lookup_verify_inode(struct inode *inode, int flags)
439 struct nfs_server *server = NFS_SERVER(inode);
441 * If we're interested in close-to-open cache consistency,
442 * then we revalidate the inode upon lookup.
444 if (!(server->flags & NFS_MOUNT_NOCTO) && !(flags & LOOKUP_CONTINUE))
446 return nfs_revalidate_inode(server, inode);
450 * We judge how long we want to trust negative
451 * dentries by looking at the parent inode mtime.
453 * If parent mtime has changed, we revalidate, else we wait for a
454 * period corresponding to the parent's attribute cache timeout value.
456 static inline int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry)
458 if (!nfs_check_verifier(dir, dentry))
460 return time_after(jiffies, dentry->d_time + NFS_ATTRTIMEO(dir));
464 * This is called every time the dcache has a lookup hit,
465 * and we should check whether we can really trust that
468 * NOTE! The hit can be a negative hit too, don't assume
471 * If the parent directory is seen to have changed, we throw out the
472 * cached dentry and do a new lookup.
474 static int nfs_lookup_revalidate(struct dentry * dentry, int flags)
479 struct nfs_fh fhandle;
480 struct nfs_fattr fattr;
483 dir = dentry->d_parent->d_inode;
484 inode = dentry->d_inode;
487 if (nfs_neg_need_reval(dir, dentry))
492 if (is_bad_inode(inode)) {
493 dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
494 dentry->d_parent->d_name.name, dentry->d_name.name);
498 /* Force a full look up iff the parent directory has changed */
499 if (nfs_check_verifier(dir, dentry)) {
500 if (nfs_lookup_verify_inode(inode, flags))
505 if (NFS_STALE(inode))
508 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
511 if (memcmp(NFS_FH(inode), &fhandle, sizeof(struct nfs_fh))!= 0)
513 if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
516 nfs_renew_times(dentry);
522 if (inode && S_ISDIR(inode->i_mode)) {
523 /* Purge readdir caches. */
524 nfs_zap_caches(inode);
525 /* If we have submounts, don't unhash ! */
526 if (have_submounts(dentry))
528 shrink_dcache_parent(dentry);
536 * This is called from dput() when d_count is going to 0.
538 static int nfs_dentry_delete(struct dentry *dentry)
540 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
541 dentry->d_parent->d_name.name, dentry->d_name.name,
544 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
545 /* Unhash it, so that ->d_iput() would be called */
548 if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
549 /* Unhash it, so that ancestors of killed async unlink
550 * files will be cleaned up during umount */
558 * Called when the dentry loses inode.
559 * We use it to clean up silly-renamed files.
561 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
563 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
565 nfs_complete_unlink(dentry);
568 if (is_bad_inode(inode))
573 struct dentry_operations nfs_dentry_operations = {
574 d_revalidate: nfs_lookup_revalidate,
575 d_delete: nfs_dentry_delete,
576 d_iput: nfs_dentry_iput,
579 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry)
583 struct nfs_fh fhandle;
584 struct nfs_fattr fattr;
586 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
587 dentry->d_parent->d_name.name, dentry->d_name.name);
589 error = -ENAMETOOLONG;
590 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
594 dentry->d_op = &nfs_dentry_operations;
596 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
598 if (error == -ENOENT)
602 inode = nfs_fhget(dentry, &fhandle, &fattr);
605 d_add(dentry, inode);
608 nfs_renew_times(dentry);
611 return ERR_PTR(error);
615 * Code common to create, mkdir, and mknod.
617 static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
618 struct nfs_fattr *fattr)
623 if (fhandle->size == 0 || !(fattr->valid & NFS_ATTR_FATTR)) {
624 struct inode *dir = dentry->d_parent->d_inode;
625 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
629 inode = nfs_fhget(dentry, fhandle, fattr);
631 d_instantiate(dentry, inode);
632 nfs_renew_times(dentry);
642 * Following a failed create operation, we drop the dentry rather
643 * than retain a negative dentry. This avoids a problem in the event
644 * that the operation succeeded on the server, but an error in the
645 * reply path made it appear to have failed.
647 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode)
650 struct nfs_fattr fattr;
651 struct nfs_fh fhandle;
654 dfprintk(VFS, "NFS: create(%x/%ld, %s\n",
655 dir->i_dev, dir->i_ino, dentry->d_name.name);
658 attr.ia_valid = ATTR_MODE;
661 * The 0 argument passed into the create function should one day
662 * contain the O_EXCL flag if requested. This allows NFSv3 to
663 * select the appropriate create strategy. Currently open_namei
664 * does not pass the create flags.
667 error = NFS_PROTO(dir)->create(dir, &dentry->d_name,
668 &attr, 0, &fhandle, &fattr);
670 error = nfs_instantiate(dentry, &fhandle, &fattr);
677 * See comments for nfs_proc_create regarding failed operations.
679 static int nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
682 struct nfs_fattr fattr;
683 struct nfs_fh fhandle;
686 dfprintk(VFS, "NFS: mknod(%x/%ld, %s\n",
687 dir->i_dev, dir->i_ino, dentry->d_name.name);
690 attr.ia_valid = ATTR_MODE;
693 error = NFS_PROTO(dir)->mknod(dir, &dentry->d_name, &attr, rdev,
696 error = nfs_instantiate(dentry, &fhandle, &fattr);
703 * See comments for nfs_proc_create regarding failed operations.
705 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
708 struct nfs_fattr fattr;
709 struct nfs_fh fhandle;
712 dfprintk(VFS, "NFS: mkdir(%x/%ld, %s\n",
713 dir->i_dev, dir->i_ino, dentry->d_name.name);
715 attr.ia_valid = ATTR_MODE;
716 attr.ia_mode = mode | S_IFDIR;
720 * Always drop the dentry, we can't always depend on
721 * the fattr returned by the server (AIX seems to be
722 * broken). We're better off doing another lookup than
723 * depending on potentially bogus information.
728 error = NFS_PROTO(dir)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
731 error = nfs_instantiate(dentry, &fhandle, &fattr);
737 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
741 dfprintk(VFS, "NFS: rmdir(%x/%ld, %s\n",
742 dir->i_dev, dir->i_ino, dentry->d_name.name);
745 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
747 dentry->d_inode->i_nlink = 0;
752 static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
754 static unsigned int sillycounter;
755 const int i_inosize = sizeof(dir->i_ino)*2;
756 const int countersize = sizeof(sillycounter)*2;
757 const int slen = strlen(".nfs") + i_inosize + countersize;
760 struct dentry *sdentry;
763 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
764 dentry->d_parent->d_name.name, dentry->d_name.name,
765 atomic_read(&dentry->d_count));
767 if (atomic_read(&dentry->d_count) == 1)
768 goto out; /* No need to silly rename. */
772 if (!dentry->d_inode)
773 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
774 dentry->d_parent->d_name.name, dentry->d_name.name);
777 * We don't allow a dentry to be silly-renamed twice.
780 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
783 sprintf(silly, ".nfs%*.*lx",
784 i_inosize, i_inosize, dentry->d_inode->i_ino);
788 char *suffix = silly + slen - countersize;
792 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
794 dfprintk(VFS, "trying to rename %s to %s\n",
795 dentry->d_name.name, silly);
797 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
799 * N.B. Better to return EBUSY here ... it could be
800 * dangerous to delete the file while it's in use.
804 } while(sdentry->d_inode != NULL); /* need negative lookup */
808 qsilly.len = strlen(silly);
809 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, dir, &qsilly);
811 nfs_renew_times(dentry);
812 d_move(dentry, sdentry);
813 error = nfs_async_unlink(dentry);
814 /* If we return 0 we don't unlink */
822 * Remove a file after making sure there are no pending writes,
823 * and after checking that the file has only one user.
825 * We invalidate the attribute cache and free the inode prior to the operation
826 * to avoid possible races if the server reuses the inode.
828 static int nfs_safe_remove(struct dentry *dentry)
830 struct inode *dir = dentry->d_parent->d_inode;
831 struct inode *inode = dentry->d_inode;
832 int error = -EBUSY, rehash = 0;
834 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
835 dentry->d_parent->d_name.name, dentry->d_name.name);
838 * Unhash the dentry while we remove the file ...
840 if (!d_unhashed(dentry)) {
844 if (atomic_read(&dentry->d_count) > 1) {
846 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
847 dentry->d_parent->d_name.name, dentry->d_name.name,
848 atomic_read(&dentry->d_count));
853 /* If the dentry was sillyrenamed, we simply call d_delete() */
854 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
862 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
864 /* if server returned ENOENT, assume that the dentry is already gone
865 * and update the cache accordingly */
866 if (error < 0 && (error != -ENOENT))
882 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
883 * belongs to an active ".nfs..." file and we return -EBUSY.
885 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
887 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
891 dfprintk(VFS, "NFS: unlink(%x/%ld, %s)\n",
892 dir->i_dev, dir->i_ino, dentry->d_name.name);
894 error = nfs_sillyrename(dir, dentry);
895 if (error && error != -EBUSY) {
896 error = nfs_safe_remove(dentry);
898 nfs_renew_times(dentry);
905 nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
908 struct nfs_fattr sym_attr;
909 struct nfs_fh sym_fh;
910 struct qstr qsymname;
914 dfprintk(VFS, "NFS: symlink(%x/%ld, %s, %s)\n",
915 dir->i_dev, dir->i_ino, dentry->d_name.name, symname);
917 error = -ENAMETOOLONG;
918 maxlen = (NFS_PROTO(dir)->version==2) ? NFS2_MAXPATHLEN : NFS3_MAXPATHLEN;
919 if (strlen(symname) > maxlen)
924 printk("nfs_proc_symlink: %s/%s not negative!\n",
925 dentry->d_parent->d_name.name, dentry->d_name.name);
928 * Fill in the sattr for the call.
929 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
931 attr.ia_valid = ATTR_MODE;
932 attr.ia_mode = S_IFLNK | S_IRWXUGO;
934 qsymname.name = symname;
935 qsymname.len = strlen(symname);
938 error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
939 &attr, &sym_fh, &sym_attr);
941 error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
943 if (error == -EEXIST)
944 printk("nfs_proc_symlink: %s/%s already exists??\n",
945 dentry->d_parent->d_name.name, dentry->d_name.name);
954 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
956 struct inode *inode = old_dentry->d_inode;
959 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
960 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
961 dentry->d_parent->d_name.name, dentry->d_name.name);
964 * Drop the dentry in advance to force a new lookup.
965 * Since nfs_proc_link doesn't return a file handle,
966 * we can't use the existing dentry.
971 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
977 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
978 * different file handle for the same inode after a rename (e.g. when
979 * moving to a different directory). A fail-safe method to do so would
980 * be to look up old_dir/old_name, create a link to new_dir/new_name and
981 * rename the old file using the sillyrename stuff. This way, the original
982 * file in old_dir will go away when the last process iput()s the inode.
986 * It actually works quite well. One needs to have the possibility for
987 * at least one ".nfs..." file in each directory the file ever gets
988 * moved or linked to which happens automagically with the new
989 * implementation that only depends on the dcache stuff instead of
990 * using the inode layer
992 * Unfortunately, things are a little more complicated than indicated
993 * above. For a cross-directory move, we want to make sure we can get
994 * rid of the old inode after the operation. This means there must be
995 * no pending writes (if it's a file), and the use count must be 1.
996 * If these conditions are met, we can drop the dentries before doing
999 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1000 struct inode *new_dir, struct dentry *new_dentry)
1002 struct inode *old_inode = old_dentry->d_inode;
1003 struct inode *new_inode = new_dentry->d_inode;
1004 struct dentry *dentry = NULL, *rehash = NULL;
1008 * To prevent any new references to the target during the rename,
1009 * we unhash the dentry and free the inode in advance.
1011 if (!d_unhashed(new_dentry)) {
1013 rehash = new_dentry;
1016 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1017 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1018 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1019 atomic_read(&new_dentry->d_count));
1022 * First check whether the target is busy ... we can't
1023 * safely do _any_ rename if the target is in use.
1025 * For files, make a copy of the dentry and then do a
1026 * silly-rename. If the silly-rename succeeds, the
1027 * copied dentry is hashed and becomes the new target.
1031 if (S_ISDIR(new_inode->i_mode))
1033 else if (atomic_read(&new_dentry->d_count) > 1) {
1035 /* copy the target dentry's name */
1036 dentry = d_alloc(new_dentry->d_parent,
1037 &new_dentry->d_name);
1041 /* silly-rename the existing target ... */
1042 err = nfs_sillyrename(new_dir, new_dentry);
1044 new_dentry = rehash = dentry;
1046 /* instantiate the replacement target */
1047 d_instantiate(new_dentry, NULL);
1050 /* dentry still busy? */
1051 if (atomic_read(&new_dentry->d_count) > 1) {
1053 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1054 new_dentry->d_parent->d_name.name,
1055 new_dentry->d_name.name,
1056 atomic_read(&new_dentry->d_count));
1064 * ... prune child dentries and writebacks if needed.
1066 if (atomic_read(&old_dentry->d_count) > 1) {
1067 nfs_wb_all(old_inode);
1068 shrink_dcache_parent(old_dentry);
1072 d_delete(new_dentry);
1074 nfs_zap_caches(new_dir);
1075 nfs_zap_caches(old_dir);
1076 NFS_CACHEINV(old_inode);
1077 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1078 new_dir, &new_dentry->d_name);
1082 if (!error && !S_ISDIR(old_inode->i_mode))
1083 d_move(old_dentry, new_dentry);
1085 /* new dentry created? */
1092 nfs_permission(struct inode *inode, int mask)
1094 int error = vfs_permission(inode, mask);
1096 if (!NFS_PROTO(inode)->access)
1099 if (error == -EROFS)
1103 * Trust UNIX mode bits except:
1105 * 1) When override capabilities may have been invoked
1106 * 2) When root squashing may be involved
1107 * 3) When ACLs may overturn a negative answer */
1108 if (!capable(CAP_DAC_OVERRIDE) && !capable(CAP_DAC_READ_SEARCH)
1109 && (current->fsuid != 0) && (current->fsgid != 0)
1110 && error != -EACCES)
1113 error = NFS_PROTO(inode)->access(inode, mask, 0);
1115 if (error == -EACCES && NFS_CLIENT(inode)->cl_droppriv &&
1116 current->uid != 0 && current->gid != 0 &&
1117 (current->fsuid != current->uid || current->fsgid != current->gid))
1118 error = NFS_PROTO(inode)->access(inode, mask, 1);
1126 * version-control: t
1127 * kept-new-versions: 5