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/malloc.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
*);
49 struct file_operations nfs_dir_operations
= {
50 read
: generic_read_dir
,
56 struct inode_operations nfs_dir_inode_operations
= {
66 revalidate
: nfs_revalidate
,
67 setattr
: nfs_notify_change
,
70 typedef u32
* (*decode_dirent_t
)(u32
*, struct nfs_entry
*, int);
74 unsigned long page_index
;
77 struct nfs_entry
*entry
;
78 decode_dirent_t decode
;
81 } nfs_readdir_descriptor_t
;
83 /* Now we cache directories properly, by stuffing the dirent
84 * data directly in the page cache.
86 * Inode invalidation due to refresh etc. takes care of
87 * _everything_, no sloppy entry flushing logic, no extraneous
88 * copying, network direct to page cache, the way it was meant
91 * NOTE: Dirent information verification is done always by the
92 * page-in of the RPC reply, nowhere else, this simplies
93 * things substantially.
96 int nfs_readdir_filler(nfs_readdir_descriptor_t
*desc
, struct page
*page
)
98 struct file
*file
= desc
->file
;
99 struct inode
*inode
= file
->f_dentry
->d_inode
;
100 void *buffer
= (void *)kmap(page
);
101 int plus
= NFS_USE_READDIRPLUS(inode
);
104 dfprintk(VFS
, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc
->entry
->cookie
, page
->index
);
107 error
= NFS_PROTO(inode
)->readdir(file
, desc
->entry
->cookie
, buffer
,
108 NFS_SERVER(inode
)->dtsize
, plus
);
109 /* We requested READDIRPLUS, but the server doesn't grok it */
110 if (desc
->plus
&& error
== -ENOTSUPP
) {
111 NFS_FLAGS(inode
) &= ~NFS_INO_ADVISE_RDPLUS
;
117 SetPageUptodate(page
);
119 /* Ensure consistent page alignment of the data.
120 * Note: assumes we have exclusive access to this mapping either
121 * throught inode->i_sem or some other mechanism.
123 if (page
->index
== 0)
124 invalidate_inode_pages(inode
);
131 invalidate_inode_pages(inode
);
137 * Given a pointer to a buffer that has already been filled by a call
138 * to readdir, find the next entry.
140 * If the end of the buffer has been reached, return -EAGAIN, if not,
141 * return the offset within the buffer of the next entry to be
145 int find_dirent(nfs_readdir_descriptor_t
*desc
, struct page
*page
)
147 struct nfs_entry
*entry
= desc
->entry
;
148 char *start
= (char *)kmap(page
),
154 p
= (char *)desc
->decode((u32
*)p
, entry
, desc
->plus
);
159 desc
->page_offset
= p
- start
;
160 dfprintk(VFS
, "NFS: found cookie %Lu\n", (long long)entry
->cookie
);
161 if (entry
->prev_cookie
== desc
->target
)
163 if (loop_count
++ > 200) {
169 dfprintk(VFS
, "NFS: find_dirent() returns %d\n", status
);
174 * Find the given page, and call find_dirent() in order to try to
175 * return the next entry.
178 int find_dirent_page(nfs_readdir_descriptor_t
*desc
)
180 struct inode
*inode
= desc
->file
->f_dentry
->d_inode
;
182 unsigned long index
= desc
->page_index
;
185 dfprintk(VFS
, "NFS: find_dirent_page() searching directory page %ld\n", desc
->page_index
);
188 page_cache_release(desc
->page
);
192 page
= read_cache_page(&inode
->i_data
, index
,
193 (filler_t
*)nfs_readdir_filler
, desc
);
195 status
= PTR_ERR(page
);
198 if (!Page_Uptodate(page
))
201 /* NOTE: Someone else may have changed the READDIRPLUS flag */
202 desc
->plus
= NFS_USE_READDIRPLUS(inode
);
203 status
= find_dirent(desc
, page
);
207 page_cache_release(page
);
209 dfprintk(VFS
, "NFS: find_dirent_page() returns %d\n", status
);
212 page_cache_release(page
);
217 * Recurse through the page cache pages, and return a
218 * filled nfs_entry structure of the next directory entry if possible.
220 * The target for the search is 'desc->target'.
223 int readdir_search_pagecache(nfs_readdir_descriptor_t
*desc
)
228 dfprintk(VFS
, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc
->target
);
230 res
= find_dirent_page(desc
);
233 /* Align to beginning of next page */
234 desc
->page_offset
= 0;
236 if (loop_count
++ > 200) {
241 dfprintk(VFS
, "NFS: readdir_search_pagecache() returned %d\n", res
);
246 * Once we've found the start of the dirent within a page: fill 'er up...
249 int nfs_do_filldir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
252 struct file
*file
= desc
->file
;
253 struct nfs_entry
*entry
= desc
->entry
;
254 char *start
= (char *)kmap(desc
->page
),
255 *p
= start
+ desc
->page_offset
;
256 unsigned long fileid
;
260 dfprintk(VFS
, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc
->target
);
263 /* Note: entry->prev_cookie contains the cookie for
264 * retrieving the current dirent on the server */
265 fileid
= nfs_fileid_to_ino_t(entry
->ino
);
266 res
= filldir(dirent
, entry
->name
, entry
->len
,
267 entry
->prev_cookie
, fileid
, DT_UNKNOWN
);
270 file
->f_pos
= desc
->target
= entry
->cookie
;
271 p
= (char *)desc
->decode((u32
*)p
, entry
, desc
->plus
);
273 if (PTR_ERR(p
) == -EAGAIN
) {
274 desc
->page_offset
= 0;
279 desc
->page_offset
= p
- start
;
280 if (loop_count
++ > 200) {
286 page_cache_release(desc
->page
);
289 dfprintk(VFS
, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc
->target
, res
);
294 * If we cannot find a cookie in our cache, we suspect that this is
295 * because it points to a deleted file, so we ask the server to return
296 * whatever it thinks is the next entry. We then feed this to filldir.
297 * If all goes well, we should then be able to find our way round the
298 * cache on the next call to readdir_search_pagecache();
300 * NOTE: we cannot add the anonymous page to the pagecache because
301 * the data it contains might not be page aligned. Besides,
302 * we should already have a complete representation of the
303 * directory in the page cache by the time we get here.
306 int uncached_readdir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
309 struct file
*file
= desc
->file
;
310 struct inode
*inode
= file
->f_dentry
->d_inode
;
311 struct page
*page
= NULL
;
315 dfprintk(VFS
, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc
->target
);
317 page_cache_release(desc
->page
);
321 page
= page_cache_alloc();
326 p
= (u32
*)kmap(page
);
327 status
= NFS_PROTO(inode
)->readdir(file
, desc
->target
, p
,
328 NFS_SERVER(inode
)->dtsize
, 0);
330 p
= desc
->decode(p
, desc
->entry
, 0);
334 desc
->entry
->prev_cookie
= desc
->target
;
340 desc
->page_index
= 0;
341 desc
->page_offset
= 0;
343 status
= nfs_do_filldir(desc
, dirent
, filldir
);
345 /* Reset read descriptor so it searches the page cache from
346 * the start upon the next call to readdir_search_pagecache() */
347 desc
->page_index
= 0;
348 desc
->page_offset
= 0;
349 memset(desc
->entry
, 0, sizeof(*desc
->entry
));
351 dfprintk(VFS
, "NFS: uncached_readdir() returns %d\n", status
);
354 page_cache_release(page
);
358 /* The file offset position is now represented as a true offset into the
359 * page cache as is the case in most of the other filesystems.
361 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
363 struct dentry
*dentry
= filp
->f_dentry
;
364 struct inode
*inode
= dentry
->d_inode
;
365 nfs_readdir_descriptor_t my_desc
,
367 struct nfs_entry my_entry
;
370 res
= nfs_revalidate(dentry
);
375 * filp->f_pos points to the file offset in the page cache.
376 * but if the cache has meanwhile been zapped, we need to
377 * read from the last dirent to revalidate f_pos
380 memset(desc
, 0, sizeof(*desc
));
381 memset(&my_entry
, 0, sizeof(my_entry
));
384 desc
->target
= filp
->f_pos
;
385 desc
->entry
= &my_entry
;
386 desc
->decode
= NFS_PROTO(inode
)->decode_dirent
;
388 while(!desc
->entry
->eof
) {
389 res
= readdir_search_pagecache(desc
);
390 if (res
== -EBADCOOKIE
) {
391 /* This means either end of directory */
392 if (desc
->entry
->cookie
== desc
->target
) {
396 /* Or that the server has 'lost' a cookie */
397 res
= uncached_readdir(desc
, dirent
, filldir
);
404 res
= nfs_do_filldir(desc
, dirent
, filldir
);
411 page_cache_release(desc
->page
);
420 * Whenever an NFS operation succeeds, we know that the dentry
421 * is valid, so we update the revalidation timestamp.
423 static inline void nfs_renew_times(struct dentry
* dentry
)
425 dentry
->d_time
= jiffies
;
428 static inline int nfs_dentry_force_reval(struct dentry
*dentry
, int flags
)
430 struct inode
*inode
= dentry
->d_inode
;
431 unsigned long timeout
= NFS_ATTRTIMEO(inode
);
434 * If it's the last lookup in a series, we use a stricter
435 * cache consistency check by looking at the parent mtime.
437 * If it's been modified in the last hour, be really strict.
438 * (This still means that we can avoid doing unnecessary
439 * work on directories like /usr/share/bin etc which basically
442 if (!(flags
& LOOKUP_CONTINUE
)) {
443 long diff
= CURRENT_TIME
- dentry
->d_parent
->d_inode
->i_mtime
;
449 return time_after(jiffies
,dentry
->d_time
+ timeout
);
453 * We judge how long we want to trust negative
454 * dentries by looking at the parent inode mtime.
456 * If mtime is close to present time, we revalidate
459 #define NFS_REVALIDATE_NEGATIVE (1 * HZ)
460 static inline int nfs_neg_need_reval(struct dentry
*dentry
)
462 struct inode
*dir
= dentry
->d_parent
->d_inode
;
463 unsigned long timeout
= NFS_ATTRTIMEO(dir
);
464 long diff
= CURRENT_TIME
- dir
->i_mtime
;
466 if (diff
< 5*60 && timeout
> NFS_REVALIDATE_NEGATIVE
)
467 timeout
= NFS_REVALIDATE_NEGATIVE
;
469 return time_after(jiffies
, dentry
->d_time
+ timeout
);
473 * This is called every time the dcache has a lookup hit,
474 * and we should check whether we can really trust that
477 * NOTE! The hit can be a negative hit too, don't assume
480 * If the dentry is older than the revalidation interval,
481 * we do a new lookup and verify that the dentry is still
484 static int nfs_lookup_revalidate(struct dentry
* dentry
, int flags
)
486 struct dentry
*dir
= dentry
->d_parent
;
487 struct inode
*dir_i
= dir
->d_inode
;
488 struct inode
* inode
= dentry
->d_inode
;
490 struct nfs_fh fhandle
;
491 struct nfs_fattr fattr
;
494 dir
= dentry
->d_parent
;
495 dir_i
= dir
->d_inode
;
497 * If we don't have an inode, let's look at the parent
498 * directory mtime to get a hint about how often we
499 * should validate things..
502 if (nfs_neg_need_reval(dentry
))
507 if (is_bad_inode(inode
)) {
508 dfprintk(VFS
, "nfs_lookup_validate: %s/%s has dud inode\n",
509 dir
->d_name
.name
, dentry
->d_name
.name
);
513 if (!nfs_dentry_force_reval(dentry
, flags
))
516 if (IS_ROOT(dentry
)) {
517 __nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
518 goto out_valid_renew
;
522 * Do a new lookup and check the dentry attributes.
524 error
= NFS_PROTO(dir_i
)->lookup(dir
, &dentry
->d_name
, &fhandle
,
529 /* Inode number matches? */
530 if (!(fattr
.valid
& NFS_ATTR_FATTR
) ||
531 NFS_FSID(inode
) != fattr
.fsid
||
532 NFS_FILEID(inode
) != fattr
.fileid
)
535 /* Filehandle matches? */
536 if (memcmp(dentry
->d_fsdata
, &fhandle
, sizeof(struct nfs_fh
)))
539 /* Ok, remeber that we successfully checked it.. */
540 nfs_refresh_inode(inode
, &fattr
);
543 nfs_renew_times(dentry
);
548 shrink_dcache_parent(dentry
);
549 /* If we have submounts, don't unhash ! */
550 if (have_submounts(dentry
))
553 /* Purge readdir caches. */
554 nfs_zap_caches(dir_i
);
555 if (inode
&& S_ISDIR(inode
->i_mode
))
556 nfs_zap_caches(inode
);
562 * This is called from dput() when d_count is going to 0.
564 static int nfs_dentry_delete(struct dentry
*dentry
)
566 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
567 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
570 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
571 /* Unhash it, so that ->d_iput() would be called */
578 static kmem_cache_t
*nfs_fh_cachep
;
580 __inline__
struct nfs_fh
*nfs_fh_alloc(void)
582 return kmem_cache_alloc(nfs_fh_cachep
, SLAB_KERNEL
);
585 __inline__
void nfs_fh_free(struct nfs_fh
*p
)
587 kmem_cache_free(nfs_fh_cachep
, p
);
591 * Called when the dentry is being freed to release private memory.
593 static void nfs_dentry_release(struct dentry
*dentry
)
595 if (dentry
->d_fsdata
) {
597 nfs_fh_free(dentry
->d_fsdata
);
603 * Called when the dentry loses inode.
604 * We use it to clean up silly-renamed files.
606 static void nfs_dentry_iput(struct dentry
*dentry
, struct inode
*inode
)
608 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
610 nfs_complete_unlink(dentry
);
616 struct dentry_operations nfs_dentry_operations
= {
617 d_revalidate
: nfs_lookup_revalidate
,
618 d_delete
: nfs_dentry_delete
,
619 d_release
: nfs_dentry_release
,
620 d_iput
: nfs_dentry_iput
,
623 static struct dentry
*nfs_lookup(struct inode
*dir_i
, struct dentry
* dentry
)
625 struct dentry
*dir
= dentry
->d_parent
;
628 struct nfs_fh fhandle
;
629 struct nfs_fattr fattr
;
631 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
632 dir
->d_name
.name
, dentry
->d_name
.name
);
634 error
= -ENAMETOOLONG
;
635 if (dentry
->d_name
.len
> NFS_SERVER(dir_i
)->namelen
)
639 if (!dentry
->d_fsdata
) {
640 dentry
->d_fsdata
= nfs_fh_alloc();
641 if (!dentry
->d_fsdata
)
644 dentry
->d_op
= &nfs_dentry_operations
;
646 error
= NFS_PROTO(dir_i
)->lookup(dir
, &dentry
->d_name
, &fhandle
,
649 if (error
== -ENOENT
)
653 inode
= nfs_fhget(dentry
, &fhandle
, &fattr
);
656 d_add(dentry
, inode
);
657 nfs_renew_times(dentry
);
662 return ERR_PTR(error
);
666 * Code common to create, mkdir, and mknod.
668 static int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
669 struct nfs_fattr
*fattr
)
674 inode
= nfs_fhget(dentry
, fhandle
, fattr
);
676 d_instantiate(dentry
, inode
);
677 nfs_renew_times(dentry
);
684 * Following a failed create operation, we drop the dentry rather
685 * than retain a negative dentry. This avoids a problem in the event
686 * that the operation succeeded on the server, but an error in the
687 * reply path made it appear to have failed.
689 static int nfs_create(struct inode
*dir_i
, struct dentry
*dentry
, int mode
)
691 struct dentry
*dir
= dentry
->d_parent
;
693 struct nfs_fattr fattr
;
694 struct nfs_fh fhandle
;
697 dfprintk(VFS
, "NFS: create(%x/%ld, %s\n",
698 dir_i
->i_dev
, dir_i
->i_ino
, dentry
->d_name
.name
);
701 attr
.ia_valid
= ATTR_MODE
;
704 * The 0 argument passed into the create function should one day
705 * contain the O_EXCL flag if requested. This allows NFSv3 to
706 * select the appropriate create strategy. Currently open_namei
707 * does not pass the create flags.
709 nfs_zap_caches(dir_i
);
710 error
= NFS_PROTO(dir_i
)->create(dir
, &dentry
->d_name
,
711 &attr
, 0, &fhandle
, &fattr
);
712 if (!error
&& fhandle
.size
!= 0)
713 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
714 if (error
|| fhandle
.size
== 0)
720 * See comments for nfs_proc_create regarding failed operations.
722 static int nfs_mknod(struct inode
*dir_i
, struct dentry
*dentry
, int mode
, int rdev
)
724 struct dentry
*dir
= dentry
->d_parent
;
726 struct nfs_fattr fattr
;
727 struct nfs_fh fhandle
;
730 dfprintk(VFS
, "NFS: mknod(%x/%ld, %s\n",
731 dir_i
->i_dev
, dir_i
->i_ino
, dentry
->d_name
.name
);
734 attr
.ia_valid
= ATTR_MODE
;
736 nfs_zap_caches(dir_i
);
737 error
= NFS_PROTO(dir_i
)->mknod(dir
, &dentry
->d_name
, &attr
, rdev
,
739 if (!error
&& fhandle
.size
!= 0)
740 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
741 if (error
|| fhandle
.size
== 0)
747 * See comments for nfs_proc_create regarding failed operations.
749 static int nfs_mkdir(struct inode
*dir_i
, struct dentry
*dentry
, int mode
)
751 struct dentry
*dir
= dentry
->d_parent
;
753 struct nfs_fattr fattr
;
754 struct nfs_fh fhandle
;
757 dfprintk(VFS
, "NFS: mkdir(%x/%ld, %s\n",
758 dir_i
->i_dev
, dir_i
->i_ino
, dentry
->d_name
.name
);
760 attr
.ia_valid
= ATTR_MODE
;
761 attr
.ia_mode
= mode
| S_IFDIR
;
765 * Always drop the dentry, we can't always depend on
766 * the fattr returned by the server (AIX seems to be
767 * broken). We're better off doing another lookup than
768 * depending on potentially bogus information.
772 nfs_zap_caches(dir_i
);
773 error
= NFS_PROTO(dir_i
)->mkdir(dir
, &dentry
->d_name
, &attr
, &fhandle
,
775 if (!error
&& fhandle
.size
!= 0)
776 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
777 if (error
|| fhandle
.size
== 0)
782 static int nfs_rmdir(struct inode
*dir_i
, struct dentry
*dentry
)
784 struct dentry
*dir
= dentry
->d_parent
;
787 dfprintk(VFS
, "NFS: rmdir(%x/%ld, %s\n",
788 dir_i
->i_dev
, dir_i
->i_ino
, dentry
->d_name
.name
);
790 nfs_zap_caches(dir_i
);
791 error
= NFS_PROTO(dir_i
)->rmdir(dir
, &dentry
->d_name
);
796 static int nfs_sillyrename(struct inode
*dir_i
, struct dentry
*dentry
)
798 struct dentry
*dir
= dentry
->d_parent
;
799 static unsigned int sillycounter
= 0;
800 const int i_inosize
= sizeof(dir_i
->i_ino
)*2;
801 const int countersize
= sizeof(sillycounter
)*2;
802 const int slen
= strlen(".nfs") + i_inosize
+ countersize
;
805 struct dentry
*sdentry
;
808 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
809 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
810 atomic_read(&dentry
->d_count
));
813 * Note that a silly-renamed file can be deleted once it's
814 * no longer in use -- it's just an ordinary file now.
816 if (atomic_read(&dentry
->d_count
) == 1) {
817 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
818 goto out
; /* No need to silly rename. */
822 if (!dentry
->d_inode
)
823 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
824 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
827 * We don't allow a dentry to be silly-renamed twice.
830 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
833 sprintf(silly
, ".nfs%*.*lx",
834 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
838 char *suffix
= silly
+ slen
- countersize
;
842 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
844 dfprintk(VFS
, "trying to rename %s to %s\n",
845 dentry
->d_name
.name
, silly
);
847 sdentry
= lookup_one(silly
, dentry
->d_parent
);
849 * N.B. Better to return EBUSY here ... it could be
850 * dangerous to delete the file while it's in use.
854 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
856 nfs_zap_caches(dir_i
);
858 qsilly
.len
= strlen(silly
);
859 error
= NFS_PROTO(dir_i
)->rename(dir
, &dentry
->d_name
, dir
, &qsilly
);
861 nfs_renew_times(dentry
);
862 d_move(dentry
, sdentry
);
863 error
= nfs_async_unlink(dentry
);
864 /* If we return 0 we don't unlink */
872 * Remove a file after making sure there are no pending writes,
873 * and after checking that the file has only one user.
875 * We invalidate the attribute cache and free the inode prior to the operation
876 * to avoid possible races if the server reuses the inode.
878 static int nfs_safe_remove(struct dentry
*dentry
)
880 struct dentry
*dir
= dentry
->d_parent
;
881 struct inode
*dir_i
= dir
->d_inode
;
882 struct inode
*inode
= dentry
->d_inode
;
883 int error
= -EBUSY
, rehash
= 0;
885 dfprintk(VFS
, "NFS: safe_remove(%s/%s)\n",
886 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
889 * Unhash the dentry while we remove the file ...
891 if (!d_unhashed(dentry
)) {
895 if (atomic_read(&dentry
->d_count
) > 1) {
897 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
898 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
899 atomic_read(&dentry
->d_count
));
903 nfs_zap_caches(dir_i
);
906 error
= NFS_PROTO(dir_i
)->remove(dir
, &dentry
->d_name
);
919 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
920 * belongs to an active ".nfs..." file and we return -EBUSY.
922 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
924 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
928 dfprintk(VFS
, "NFS: unlink(%x/%ld, %s)\n",
929 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
931 error
= nfs_sillyrename(dir
, dentry
);
932 if (error
&& error
!= -EBUSY
) {
933 error
= nfs_safe_remove(dentry
);
935 nfs_renew_times(dentry
);
942 nfs_symlink(struct inode
*dir_i
, struct dentry
*dentry
, const char *symname
)
944 struct dentry
*dir
= dentry
->d_parent
;
946 struct nfs_fattr sym_attr
;
947 struct nfs_fh sym_fh
;
948 struct qstr qsymname
;
952 dfprintk(VFS
, "NFS: symlink(%x/%ld, %s, %s)\n",
953 dir_i
->i_dev
, dir_i
->i_ino
, dentry
->d_name
.name
, symname
);
955 error
= -ENAMETOOLONG
;
956 maxlen
= (NFS_PROTO(dir_i
)->version
==2) ? NFS2_MAXPATHLEN
: NFS3_MAXPATHLEN
;
957 if (strlen(symname
) > maxlen
)
962 printk("nfs_proc_symlink: %s/%s not negative!\n",
963 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
966 * Fill in the sattr for the call.
967 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
969 attr
.ia_valid
= ATTR_MODE
;
970 attr
.ia_mode
= S_IFLNK
| S_IRWXUGO
;
972 qsymname
.name
= symname
;
973 qsymname
.len
= strlen(symname
);
975 nfs_zap_caches(dir_i
);
976 error
= NFS_PROTO(dir_i
)->symlink(dir
, &dentry
->d_name
, &qsymname
,
977 &attr
, &sym_fh
, &sym_attr
);
978 if (!error
&& sym_fh
.size
!= 0 && (sym_attr
.valid
& NFS_ATTR_FATTR
)) {
979 error
= nfs_instantiate(dentry
, &sym_fh
, &sym_attr
);
981 if (error
== -EEXIST
)
982 printk("nfs_proc_symlink: %s/%s already exists??\n",
983 dir
->d_name
.name
, dentry
->d_name
.name
);
992 nfs_link(struct dentry
*old_dentry
, struct inode
*dir_i
, struct dentry
*dentry
)
994 struct dentry
*dir
= dentry
->d_parent
;
995 struct inode
*inode
= old_dentry
->d_inode
;
998 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
999 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1000 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1003 * Drop the dentry in advance to force a new lookup.
1004 * Since nfs_proc_link doesn't return a file handle,
1005 * we can't use the existing dentry.
1008 nfs_zap_caches(dir_i
);
1009 NFS_CACHEINV(inode
);
1010 error
= NFS_PROTO(dir_i
)->link(old_dentry
, dir
, &dentry
->d_name
);
1016 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1017 * different file handle for the same inode after a rename (e.g. when
1018 * moving to a different directory). A fail-safe method to do so would
1019 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1020 * rename the old file using the sillyrename stuff. This way, the original
1021 * file in old_dir will go away when the last process iput()s the inode.
1025 * It actually works quite well. One needs to have the possibility for
1026 * at least one ".nfs..." file in each directory the file ever gets
1027 * moved or linked to which happens automagically with the new
1028 * implementation that only depends on the dcache stuff instead of
1029 * using the inode layer
1031 * Unfortunately, things are a little more complicated than indicated
1032 * above. For a cross-directory move, we want to make sure we can get
1033 * rid of the old inode after the operation. This means there must be
1034 * no pending writes (if it's a file), and the use count must be 1.
1035 * If these conditions are met, we can drop the dentries before doing
1038 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1039 struct inode
*new_dir
, struct dentry
*new_dentry
)
1041 struct inode
*old_inode
= old_dentry
->d_inode
;
1042 struct inode
*new_inode
= new_dentry
->d_inode
;
1043 struct dentry
*dentry
= NULL
, *rehash
= NULL
;
1047 * To prevent any new references to the target during the rename,
1048 * we unhash the dentry and free the inode in advance.
1050 if (!d_unhashed(new_dentry
)) {
1052 rehash
= new_dentry
;
1055 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1056 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1057 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1058 atomic_read(&new_dentry
->d_count
));
1061 * First check whether the target is busy ... we can't
1062 * safely do _any_ rename if the target is in use.
1064 * For files, make a copy of the dentry and then do a
1065 * silly-rename. If the silly-rename succeeds, the
1066 * copied dentry is hashed and becomes the new target.
1070 if (S_ISDIR(new_inode
->i_mode
))
1072 else if (atomic_read(&new_dentry
->d_count
) > 1) {
1074 /* copy the target dentry's name */
1075 dentry
= d_alloc(new_dentry
->d_parent
,
1076 &new_dentry
->d_name
);
1080 /* silly-rename the existing target ... */
1081 err
= nfs_sillyrename(new_dir
, new_dentry
);
1083 new_dentry
= rehash
= dentry
;
1085 /* instantiate the replacement target */
1086 d_instantiate(new_dentry
, NULL
);
1089 /* dentry still busy? */
1090 if (atomic_read(&new_dentry
->d_count
) > 1) {
1092 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1093 new_dentry
->d_parent
->d_name
.name
,
1094 new_dentry
->d_name
.name
,
1095 atomic_read(&new_dentry
->d_count
));
1103 * ... prune child dentries and writebacks if needed.
1105 if (atomic_read(&old_dentry
->d_count
) > 1) {
1106 nfs_wb_all(old_inode
);
1107 shrink_dcache_parent(old_dentry
);
1111 d_delete(new_dentry
);
1113 nfs_zap_caches(new_dir
);
1114 nfs_zap_caches(old_dir
);
1115 error
= NFS_PROTO(old_dir
)->rename(old_dentry
->d_parent
,
1116 &old_dentry
->d_name
,
1117 new_dentry
->d_parent
,
1118 &new_dentry
->d_name
);
1122 if (!error
&& !S_ISDIR(old_inode
->i_mode
))
1123 d_move(old_dentry
, new_dentry
);
1125 /* new dentry created? */
1131 int nfs_init_fhcache(void)
1133 nfs_fh_cachep
= kmem_cache_create("nfs_fh",
1134 sizeof(struct nfs_fh
),
1135 0, SLAB_HWCACHE_ALIGN
,
1137 if (nfs_fh_cachep
== NULL
)
1143 void nfs_destroy_fhcache(void)
1145 if (kmem_cache_destroy(nfs_fh_cachep
))
1146 printk(KERN_INFO
"nfs_fh: not all structures were freed\n");
1151 * version-control: t
1152 * kept-new-versions: 5