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 #define NFS_NEED_XDR_TYPES
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/stat.h>
24 #include <linux/fcntl.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/malloc.h>
29 #include <linux/sunrpc/clnt.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs.h>
32 #include <linux/pagemap.h>
34 #include <asm/segment.h> /* for fs functions */
36 #define NFS_PARANOIA 1
37 /* #define NFS_DEBUG_VERBOSE 1 */
39 static int nfs_safe_remove(struct dentry
*);
41 static int nfs_readdir(struct file
*, void *, filldir_t
);
42 static struct dentry
*nfs_lookup(struct inode
*, struct dentry
*);
43 static int nfs_create(struct inode
*, struct dentry
*, int);
44 static int nfs_mkdir(struct inode
*, struct dentry
*, int);
45 static int nfs_rmdir(struct inode
*, struct dentry
*);
46 static int nfs_unlink(struct inode
*, struct dentry
*);
47 static int nfs_symlink(struct inode
*, struct dentry
*, const char *);
48 static int nfs_link(struct dentry
*, struct inode
*, struct dentry
*);
49 static int nfs_mknod(struct inode
*, struct dentry
*, int, int);
50 static int nfs_rename(struct inode
*, struct dentry
*,
51 struct inode
*, struct dentry
*);
53 struct file_operations nfs_dir_operations
= {
54 read
: generic_read_dir
,
60 struct inode_operations nfs_dir_inode_operations
= {
70 revalidate
: nfs_revalidate
,
71 setattr
: nfs_notify_change
,
74 /* Each readdir response is composed of entries which look
75 * like the following, as per the NFSv2 RFC:
77 * __u32 not_end zero if end of response
78 * __u32 file ID opaque ino_t
79 * __u32 namelen size of name string
80 * VAR name string the string, padded to modulo 4 bytes
81 * __u32 cookie opaque ID of next entry
83 * When you hit not_end being zero, the next __u32 is non-zero if
84 * this is the end of the complete set of readdir entires for this
85 * directory. This can be used, for example, to initiate pre-fetch.
87 * In order to know what to ask the server for, we only need to know
88 * the final cookie of the previous page, and offset zero has cookie
89 * zero, so we cache cookie to page offset translations in chunks.
91 #define COOKIES_PER_CHUNK (8 - ((sizeof(void *) / sizeof(__u32))))
92 struct nfs_cookie_table
{
93 struct nfs_cookie_table
*next
;
94 __u32 cookies
[COOKIES_PER_CHUNK
];
96 static kmem_cache_t
*nfs_cookie_cachep
;
98 /* This whole scheme relies on the fact that dirent cookies
99 * are monotonically increasing.
101 * Another invariant is that once we have a valid non-zero
102 * EOF marker cached, we also have the complete set of cookie
105 * We return the page offset assosciated with the page where
106 * cookie must be if it exists at all, however if we can not
107 * figure that out conclusively, we return < 0.
109 static long __nfs_readdir_offset(struct inode
*inode
, __u32 cookie
)
111 struct nfs_cookie_table
*p
;
112 unsigned long ret
= 0;
114 for(p
= NFS_COOKIES(inode
); p
!= NULL
; p
= p
->next
) {
117 for (i
= 0; i
< COOKIES_PER_CHUNK
; i
++) {
118 __u32 this_cookie
= p
->cookies
[i
];
120 /* End of known cookies, EOF is our only hope. */
124 /* Next cookie is larger, must be in previous page. */
125 if (this_cookie
> cookie
)
130 /* Exact cookie match, it must be in this page :-) */
131 if (this_cookie
== cookie
)
136 if (NFS_DIREOF(inode
) != 0)
142 static __inline__
long nfs_readdir_offset(struct inode
*inode
, __u32 cookie
)
144 /* Cookie zero is always at page offset zero. Optimize the
145 * other common case since most directories fit entirely
148 if (!cookie
|| (!NFS_COOKIES(inode
) && NFS_DIREOF(inode
)))
150 return __nfs_readdir_offset(inode
, cookie
);
153 /* Since a cookie of zero is declared special by the NFS
154 * protocol, we easily can tell if a cookie in an existing
155 * table chunk is valid or not.
157 * NOTE: The cookies are indexed off-by-one because zero
160 static __inline__ __u32
*find_cookie(struct inode
*inode
, unsigned long off
)
162 static __u32 cookie_zero
= 0;
163 struct nfs_cookie_table
*p
;
169 p
= NFS_COOKIES(inode
);
170 while(off
>= COOKIES_PER_CHUNK
&& p
) {
171 off
-= COOKIES_PER_CHUNK
;
176 ret
= &p
->cookies
[off
];
183 #define NFS_NAMELEN_ALIGN(__len) ((((__len)+3)>>2)<<2)
184 static int create_cookie(__u32 cookie
, unsigned long off
, struct inode
*inode
)
186 struct nfs_cookie_table
**cpp
;
188 cpp
= (struct nfs_cookie_table
**) &NFS_COOKIES(inode
);
189 while (off
>= COOKIES_PER_CHUNK
&& *cpp
) {
190 off
-= COOKIES_PER_CHUNK
;
194 (*cpp
)->cookies
[off
] = cookie
;
196 struct nfs_cookie_table
*new;
199 new = kmem_cache_alloc(nfs_cookie_cachep
, SLAB_ATOMIC
);
204 for(i
= 0; i
< COOKIES_PER_CHUNK
; i
++) {
206 new->cookies
[i
] = cookie
;
215 static struct page
*try_to_get_dirent_page(struct file
*, __u32
, int);
217 /* Recover from a revalidation flush. The case here is that
218 * the inode for the directory got invalidated somehow, and
219 * all of our cached information is lost. In order to get
220 * a correct cookie for the current readdir request from the
221 * user, we must (re-)fetch older readdir page cache entries.
223 * Returns < 0 if some error occurrs, else it is the page offset
226 static long refetch_to_readdir_cookie(struct file
*file
, struct inode
*inode
)
229 u32 goal_cookie
= file
->f_pos
;
230 long cur_off
, ret
= -1L;
235 page
= find_get_page(&inode
->i_data
, cur_off
);
237 if (!Page_Uptodate(page
))
240 __u32
*cp
= find_cookie(inode
, cur_off
);
245 page
= try_to_get_dirent_page(file
, *cp
, 0);
250 /* Someone touched the dir on us. */
254 page_cache_release(page
);
256 if ((ret
= nfs_readdir_offset(inode
, goal_cookie
)) >= 0)
266 page_cache_release(page
);
270 /* Now we cache directories properly, by stuffing the dirent
271 * data directly in the page cache.
273 * Inode invalidation due to refresh etc. takes care of
274 * _everything_, no sloppy entry flushing logic, no extraneous
275 * copying, network direct to page cache, the way it was meant
278 * NOTE: Dirent information verification is done always by the
279 * page-in of the RPC reply, nowhere else, this simplies
280 * things substantially.
283 static int nfs_dir_filler(struct dentry
*dentry
, struct page
*page
)
285 struct nfs_readdirargs rd_args
;
286 struct nfs_readdirres rd_res
;
287 struct inode
*inode
= dentry
->d_inode
;
288 long offset
= page
->index
;
295 cookiep
= find_cookie(inode
, offset
);
299 rd_args
.fh
= NFS_FH(dentry
);
300 rd_res
.buffer
= (char *)page_address(page
);
301 rd_res
.bufsiz
= PAGE_CACHE_SIZE
;
302 rd_res
.cookie
= *cookiep
;
304 rd_args
.buffer
= rd_res
.buffer
;
305 rd_args
.bufsiz
= rd_res
.bufsiz
;
306 rd_args
.cookie
= rd_res
.cookie
;
307 err
= rpc_call(NFS_CLIENT(inode
),
308 NFSPROC_READDIR
, &rd_args
, &rd_res
, 0);
311 } while(rd_res
.bufsiz
> 0);
314 if (rd_res
.bufsiz
< 0)
315 NFS_DIREOF(inode
) = rd_res
.cookie
;
316 else if (create_cookie(rd_res
.cookie
, offset
, inode
))
319 SetPageUptodate(page
);
330 static struct page
*try_to_get_dirent_page(struct file
*file
, __u32 cookie
, int refetch_ok
)
332 struct dentry
*dentry
= file
->f_dentry
;
333 struct inode
*inode
= dentry
->d_inode
;
337 if ((offset
= nfs_readdir_offset(inode
, cookie
)) < 0) {
339 (offset
= refetch_to_readdir_cookie(file
, inode
)) < 0) {
344 page
= read_cache_page(&inode
->i_data
, offset
,
345 (filler_t
*)nfs_dir_filler
, dentry
);
348 if (!Page_Uptodate(page
))
353 page_cache_release(page
);
358 /* Seek up to dirent assosciated with the passed in cookie,
359 * then fill in dirents found. Return the last cookie
360 * actually given to the user, to update the file position.
362 static __inline__ u32
nfs_do_filldir(__u32
*p
, u32 cookie
,
363 void *dirent
, filldir_t filldir
)
367 while((end
= *p
++) != 0) {
368 __u32 fileid
, len
, skip
, this_cookie
;
374 skip
= NFS_NAMELEN_ALIGN(len
);
378 if (this_cookie
< cookie
)
381 cookie
= this_cookie
;
382 if (filldir(dirent
, name
, len
, cookie
, fileid
) < 0)
389 /* The file offset position is represented in pure bytes, to
390 * make the page cache interface straight forward.
392 * However, some way is needed to make the connection between the
393 * opaque NFS directory entry cookies and our offsets, so a per-inode
394 * cookie cache table is used.
396 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
398 struct dentry
*dentry
= filp
->f_dentry
;
399 struct inode
*inode
= dentry
->d_inode
;
404 res
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
408 if (NFS_DIREOF(inode
) && filp
->f_pos
>= NFS_DIREOF(inode
))
411 if ((offset
= nfs_readdir_offset(inode
, filp
->f_pos
)) < 0)
414 page
= find_get_page(&inode
->i_data
, offset
);
417 if (!Page_Uptodate(page
))
418 goto dirent_read_error
;
421 filp
->f_pos
= nfs_do_filldir((__u32
*) page_address(page
),
422 filp
->f_pos
, dirent
, filldir
);
424 page_cache_release(page
);
428 page
= try_to_get_dirent_page(filp
, filp
->f_pos
, 1);
432 if (Page_Uptodate(page
))
435 page_cache_release(page
);
440 /* Flush directory cookie and EOF caches for an inode.
441 * So we don't thrash allocating/freeing cookie tables,
442 * we keep the cookies around until the inode is
445 __inline__
void nfs_flush_dircache(struct inode
*inode
)
447 struct nfs_cookie_table
*p
= NFS_COOKIES(inode
);
452 for(i
= 0; i
< COOKIES_PER_CHUNK
; i
++)
457 NFS_DIREOF(inode
) = 0;
460 /* Free up directory cache state, this happens when
461 * nfs_delete_inode is called on an NFS directory.
463 void nfs_free_dircache(struct inode
*inode
)
465 struct nfs_cookie_table
*p
= NFS_COOKIES(inode
);
468 struct nfs_cookie_table
*next
= p
->next
;
469 kmem_cache_free(nfs_cookie_cachep
, p
);
472 NFS_COOKIES(inode
) = NULL
;
473 NFS_DIREOF(inode
) = 0;
477 * Whenever an NFS operation succeeds, we know that the dentry
478 * is valid, so we update the revalidation timestamp.
480 static inline void nfs_renew_times(struct dentry
* dentry
)
482 dentry
->d_time
= jiffies
;
485 static inline int nfs_dentry_force_reval(struct dentry
*dentry
, int flags
)
487 struct inode
*inode
= dentry
->d_inode
;
488 unsigned long timeout
= NFS_ATTRTIMEO(inode
);
491 * If it's the last lookup in a series, we use a stricter
492 * cache consistency check by looking at the parent mtime.
494 * If it's been modified in the last hour, be really strict.
495 * (This still means that we can avoid doing unnecessary
496 * work on directories like /usr/share/bin etc which basically
499 if (!(flags
& LOOKUP_CONTINUE
)) {
500 long diff
= CURRENT_TIME
- dentry
->d_parent
->d_inode
->i_mtime
;
506 return time_after(jiffies
,dentry
->d_time
+ timeout
);
510 * We judge how long we want to trust negative
511 * dentries by looking at the parent inode mtime.
513 * If mtime is close to present time, we revalidate
516 #define NFS_REVALIDATE_NEGATIVE (1 * HZ)
517 static inline int nfs_neg_need_reval(struct dentry
*dentry
)
519 struct inode
*dir
= dentry
->d_parent
->d_inode
;
520 unsigned long timeout
= NFS_ATTRTIMEO(dir
);
521 long diff
= CURRENT_TIME
- dir
->i_mtime
;
523 if (diff
< 5*60 && timeout
> NFS_REVALIDATE_NEGATIVE
)
524 timeout
= NFS_REVALIDATE_NEGATIVE
;
526 return time_after(jiffies
, dentry
->d_time
+ timeout
);
530 * This is called every time the dcache has a lookup hit,
531 * and we should check whether we can really trust that
534 * NOTE! The hit can be a negative hit too, don't assume
537 * If the dentry is older than the revalidation interval,
538 * we do a new lookup and verify that the dentry is still
541 static int nfs_lookup_revalidate(struct dentry
* dentry
, int flags
)
543 struct dentry
* parent
= dentry
->d_parent
;
544 struct inode
* inode
= dentry
->d_inode
;
546 struct nfs_fh fhandle
;
547 struct nfs_fattr fattr
;
550 * If we don't have an inode, let's look at the parent
551 * directory mtime to get a hint about how often we
552 * should validate things..
555 if (nfs_neg_need_reval(dentry
))
560 if (is_bad_inode(inode
)) {
561 dfprintk(VFS
, "nfs_lookup_validate: %s/%s has dud inode\n",
562 parent
->d_name
.name
, dentry
->d_name
.name
);
566 if (!nfs_dentry_force_reval(dentry
, flags
))
569 if (IS_ROOT(dentry
)) {
570 __nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
571 goto out_valid_renew
;
575 * Do a new lookup and check the dentry attributes.
577 error
= nfs_proc_lookup(NFS_DSERVER(parent
), NFS_FH(parent
),
578 dentry
->d_name
.name
, &fhandle
, &fattr
);
582 /* Inode number matches? */
583 if (NFS_FSID(inode
) != fattr
.fsid
||
584 NFS_FILEID(inode
) != fattr
.fileid
)
587 /* Filehandle matches? */
588 if (memcmp(dentry
->d_fsdata
, &fhandle
, sizeof(struct nfs_fh
)))
591 /* Ok, remeber that we successfully checked it.. */
592 nfs_refresh_inode(inode
, &fattr
);
595 nfs_renew_times(dentry
);
600 if (!list_empty(&dentry
->d_subdirs
))
601 shrink_dcache_parent(dentry
);
602 /* Purge readdir caches. */
603 if (dentry
->d_parent
->d_inode
) {
604 nfs_zap_caches(dentry
->d_parent
->d_inode
);
605 NFS_CACHEINV(dentry
->d_parent
->d_inode
);
611 * This is called from dput() when d_count is going to 0.
612 * We use it to clean up silly-renamed files.
614 static void nfs_dentry_delete(struct dentry
*dentry
)
616 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
617 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
620 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
623 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
624 /* Unhash it first */
626 error
= nfs_safe_remove(dentry
);
628 printk("NFS: can't silly-delete %s/%s, error=%d\n",
629 dentry
->d_parent
->d_name
.name
,
630 dentry
->d_name
.name
, error
);
635 static kmem_cache_t
*nfs_fh_cachep
;
637 __inline__
struct nfs_fh
*nfs_fh_alloc(void)
639 return kmem_cache_alloc(nfs_fh_cachep
, SLAB_KERNEL
);
642 __inline__
void nfs_fh_free(struct nfs_fh
*p
)
644 kmem_cache_free(nfs_fh_cachep
, p
);
648 * Called when the dentry is being freed to release private memory.
650 static void nfs_dentry_release(struct dentry
*dentry
)
652 if (dentry
->d_fsdata
)
653 nfs_fh_free(dentry
->d_fsdata
);
656 struct dentry_operations nfs_dentry_operations
= {
657 d_revalidate
: nfs_lookup_revalidate
,
658 d_delete
: nfs_dentry_delete
,
659 d_release
: nfs_dentry_release
,
662 #if 0 /* dead code */
665 * Display all dentries holding the specified inode.
667 static void show_dentry(struct list_head
* dlist
)
669 struct list_head
*tmp
= dlist
;
671 while ((tmp
= tmp
->next
) != dlist
) {
672 struct dentry
* dentry
= list_entry(tmp
, struct dentry
, d_alias
);
673 const char * unhashed
= "";
675 if (list_empty(&dentry
->d_hash
))
676 unhashed
= "(unhashed)";
678 printk("show_dentry: %s/%s, d_count=%d%s\n",
679 dentry
->d_parent
->d_name
.name
,
680 dentry
->d_name
.name
, dentry
->d_count
,
684 #endif /* NFS_PARANOIA */
687 static struct dentry
*nfs_lookup(struct inode
*dir
, struct dentry
* dentry
)
691 struct nfs_fh fhandle
;
692 struct nfs_fattr fattr
;
694 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
695 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
697 error
= -ENAMETOOLONG
;
698 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
702 if (!dentry
->d_fsdata
) {
703 dentry
->d_fsdata
= nfs_fh_alloc();
704 if (!dentry
->d_fsdata
)
707 dentry
->d_op
= &nfs_dentry_operations
;
709 error
= nfs_proc_lookup(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
710 dentry
->d_name
.name
, &fhandle
, &fattr
);
712 if (error
== -ENOENT
)
716 inode
= nfs_fhget(dentry
, &fhandle
, &fattr
);
719 d_add(dentry
, inode
);
720 nfs_renew_times(dentry
);
725 return ERR_PTR(error
);
729 * Code common to create, mkdir, and mknod.
731 static int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
732 struct nfs_fattr
*fattr
)
737 inode
= nfs_fhget(dentry
, fhandle
, fattr
);
739 d_instantiate(dentry
, inode
);
740 nfs_renew_times(dentry
);
747 * Following a failed create operation, we drop the dentry rather
748 * than retain a negative dentry. This avoids a problem in the event
749 * that the operation succeeded on the server, but an error in the
750 * reply path made it appear to have failed.
752 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
756 struct nfs_fattr fattr
;
757 struct nfs_fh fhandle
;
759 dfprintk(VFS
, "NFS: create(%x/%ld, %s\n",
760 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
763 attr
.ia_valid
= ATTR_MODE
;
766 * Invalidate the dir cache before the operation to avoid a race.
768 invalidate_inode_pages(dir
);
769 nfs_flush_dircache(dir
);
770 error
= nfs_proc_create(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
771 dentry
->d_name
.name
, &attr
, &fhandle
, &fattr
);
773 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
780 * See comments for nfs_proc_create regarding failed operations.
782 static int nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, int rdev
)
786 struct nfs_fattr fattr
;
787 struct nfs_fh fhandle
;
789 dfprintk(VFS
, "NFS: mknod(%x/%ld, %s\n",
790 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
793 attr
.ia_valid
= ATTR_MODE
;
794 /* FIXME: move this to a special nfs_proc_mknod() */
795 if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
796 attr
.ia_size
= rdev
; /* get out your barf bag */
797 attr
.ia_valid
|= ATTR_SIZE
;
800 invalidate_inode_pages(dir
);
801 nfs_flush_dircache(dir
);
802 error
= nfs_proc_create(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
803 dentry
->d_name
.name
, &attr
, &fhandle
, &fattr
);
805 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
812 * See comments for nfs_proc_create regarding failed operations.
814 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
818 struct nfs_fattr fattr
;
819 struct nfs_fh fhandle
;
821 dfprintk(VFS
, "NFS: mkdir(%x/%ld, %s\n",
822 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
824 attr
.ia_valid
= ATTR_MODE
;
825 attr
.ia_mode
= mode
| S_IFDIR
;
828 * Always drop the dentry, we can't always depend on
829 * the fattr returned by the server (AIX seems to be
830 * broken). We're better off doing another lookup than
831 * depending on potentially bogus information.
834 invalidate_inode_pages(dir
);
835 nfs_flush_dircache(dir
);
836 error
= nfs_proc_mkdir(NFS_DSERVER(dentry
), NFS_FH(dentry
->d_parent
),
837 dentry
->d_name
.name
, &attr
, &fhandle
, &fattr
);
843 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
847 dfprintk(VFS
, "NFS: rmdir(%x/%ld, %s\n",
848 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
850 invalidate_inode_pages(dir
);
851 nfs_flush_dircache(dir
);
852 error
= nfs_proc_rmdir(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
853 dentry
->d_name
.name
);
855 /* Update i_nlink and invalidate dentry. */
866 /* Note: we copy the code from lookup_dentry() here, only: we have to
867 * omit the directory lock. We are already the owner of the lock when
868 * we reach here. And "down(&dir->i_sem)" would make us sleep forever
869 * ('cause WE have the lock)
871 * VERY IMPORTANT: calculate the hash for this dentry!!!!!!!!
872 * Otherwise the cached lookup DEFINITELY WILL fail. And a new dentry
873 * is created. Without the DCACHE_NFSFS_RENAMED flag. And with d_count
876 * Concerning my choice of the temp name: it is just nice to have
877 * i_ino part of the temp name, as this offers another check whether
878 * somebody attempts to remove the "silly renamed" dentry itself.
879 * Which is something that I consider evil. Your opinion may vary.
881 * Now that I compute the hash value right, it should be possible to simply
882 * check for the DCACHE_NFSFS_RENAMED flag in dentry->d_flag instead of
883 * doing the string compare.
885 * This offers the opportunity to shorten the temp name. Currently, I use
886 * the hex representation of i_ino + an event counter. This sums up to
887 * as much as 36 characters for a 64 bit machine, and needs 20 chars on
890 * The use of i_ino is simply cosmetic. All we need is a unique temp
891 * file name for the .nfs files. The event counter seemed to be adequate.
892 * And as we retry in case such a file already exists, we are guaranteed
897 struct dentry
*nfs_silly_lookup(struct dentry
*parent
, char *silly
, int slen
)
900 struct dentry
*sdentry
;
905 sqstr
.hash
= full_name_hash(silly
, slen
);
906 sdentry
= d_lookup(parent
, &sqstr
);
908 sdentry
= d_alloc(parent
, &sqstr
);
910 return ERR_PTR(-ENOMEM
);
911 res
= nfs_lookup(parent
->d_inode
, sdentry
);
920 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
922 static unsigned int sillycounter
= 0;
923 const int i_inosize
= sizeof(dir
->i_ino
)*2;
924 const int countersize
= sizeof(sillycounter
)*2;
925 const int slen
= strlen(".nfs") + i_inosize
+ countersize
;
927 struct dentry
*sdentry
;
930 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
931 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
935 * Note that a silly-renamed file can be deleted once it's
936 * no longer in use -- it's just an ordinary file now.
938 if (dentry
->d_count
== 1) {
939 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
940 goto out
; /* No need to silly rename. */
944 if (!dentry
->d_inode
)
945 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
946 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
949 * We don't allow a dentry to be silly-renamed twice.
952 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
955 sprintf(silly
, ".nfs%*.*lx",
956 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
960 char *suffix
= silly
+ slen
- countersize
;
964 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
966 dfprintk(VFS
, "trying to rename %s to %s\n",
967 dentry
->d_name
.name
, silly
);
969 sdentry
= nfs_silly_lookup(dentry
->d_parent
, silly
, slen
);
971 * N.B. Better to return EBUSY here ... it could be
972 * dangerous to delete the file while it's in use.
976 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
978 invalidate_inode_pages(dir
);
979 nfs_flush_dircache(dir
);
980 error
= nfs_proc_rename(NFS_SERVER(dir
),
981 NFS_FH(dentry
->d_parent
), dentry
->d_name
.name
,
982 NFS_FH(dentry
->d_parent
), silly
);
984 nfs_renew_times(dentry
);
985 d_move(dentry
, sdentry
);
986 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
987 /* If we return 0 we don't unlink */
995 * Remove a file after making sure there are no pending writes,
996 * and after checking that the file has only one user.
998 * We update inode->i_nlink and free the inode prior to the operation
999 * to avoid possible races if the server reuses the inode.
1001 static int nfs_safe_remove(struct dentry
*dentry
)
1003 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1004 struct inode
*inode
= dentry
->d_inode
;
1005 int error
, rehash
= 0;
1007 dfprintk(VFS
, "NFS: safe_remove(%s/%s, %ld)\n",
1008 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
1011 /* N.B. not needed now that d_delete is done in advance? */
1015 printk("nfs_safe_remove: %s/%s already negative??\n",
1016 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1020 if (dentry
->d_count
> 1) {
1022 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
1023 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, dentry
->d_count
);
1028 * Unhash the dentry while we remove the file ...
1030 if (!list_empty(&dentry
->d_hash
)) {
1035 * Update i_nlink and free the inode before unlinking.
1042 invalidate_inode_pages(dir
);
1043 nfs_flush_dircache(dir
);
1044 error
= nfs_proc_remove(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
1045 dentry
->d_name
.name
);
1047 * Rehash the negative dentry if the operation succeeded.
1049 if (!error
&& rehash
)
1050 d_add(dentry
, NULL
);
1055 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1056 * belongs to an active ".nfs..." file and we return -EBUSY.
1058 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1060 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1064 dfprintk(VFS
, "NFS: unlink(%x/%ld, %s)\n",
1065 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
1067 error
= nfs_sillyrename(dir
, dentry
);
1068 if (error
&& error
!= -EBUSY
) {
1069 error
= nfs_safe_remove(dentry
);
1071 nfs_renew_times(dentry
);
1078 nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1083 dfprintk(VFS
, "NFS: symlink(%x/%ld, %s, %s)\n",
1084 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
, symname
);
1086 error
= -ENAMETOOLONG
;
1087 if (strlen(symname
) > NFS_MAXPATHLEN
)
1091 if (dentry
->d_inode
)
1092 printk("nfs_proc_symlink: %s/%s not negative!\n",
1093 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1096 * Fill in the sattr for the call.
1097 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1099 attr
.ia_valid
= ATTR_MODE
;
1100 attr
.ia_mode
= S_IFLNK
| S_IRWXUGO
;
1103 * Drop the dentry in advance to force a new lookup.
1104 * Since nfs_proc_symlink doesn't return a fattr, we
1105 * can't instantiate the new inode.
1108 invalidate_inode_pages(dir
);
1109 nfs_flush_dircache(dir
);
1110 error
= nfs_proc_symlink(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
1111 dentry
->d_name
.name
, symname
, &attr
);
1113 nfs_renew_times(dentry
->d_parent
);
1114 } else if (error
== -EEXIST
) {
1115 printk("nfs_proc_symlink: %s/%s already exists??\n",
1116 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1124 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
1126 struct inode
*inode
= old_dentry
->d_inode
;
1129 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
1130 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1131 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1134 * Drop the dentry in advance to force a new lookup.
1135 * Since nfs_proc_link doesn't return a file handle,
1136 * we can't use the existing dentry.
1139 invalidate_inode_pages(dir
);
1140 nfs_flush_dircache(dir
);
1141 error
= nfs_proc_link(NFS_DSERVER(old_dentry
), NFS_FH(old_dentry
),
1142 NFS_FH(dentry
->d_parent
), dentry
->d_name
.name
);
1145 * Update the link count immediately, as some apps
1146 * (e.g. pine) test this after making a link.
1155 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1156 * different file handle for the same inode after a rename (e.g. when
1157 * moving to a different directory). A fail-safe method to do so would
1158 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1159 * rename the old file using the sillyrename stuff. This way, the original
1160 * file in old_dir will go away when the last process iput()s the inode.
1164 * It actually works quite well. One needs to have the possibility for
1165 * at least one ".nfs..." file in each directory the file ever gets
1166 * moved or linked to which happens automagically with the new
1167 * implementation that only depends on the dcache stuff instead of
1168 * using the inode layer
1170 * Unfortunately, things are a little more complicated than indicated
1171 * above. For a cross-directory move, we want to make sure we can get
1172 * rid of the old inode after the operation. This means there must be
1173 * no pending writes (if it's a file), and the use count must be 1.
1174 * If these conditions are met, we can drop the dentries before doing
1177 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1178 struct inode
*new_dir
, struct dentry
*new_dentry
)
1180 struct inode
*old_inode
= old_dentry
->d_inode
;
1181 struct inode
*new_inode
= new_dentry
->d_inode
;
1182 struct dentry
*dentry
= NULL
;
1183 int error
, rehash
= 0;
1185 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1186 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1187 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1188 new_dentry
->d_count
);
1191 * First check whether the target is busy ... we can't
1192 * safely do _any_ rename if the target is in use.
1194 * For files, make a copy of the dentry and then do a
1195 * silly-rename. If the silly-rename succeeds, the
1196 * copied dentry is hashed and becomes the new target.
1198 * With directories check is done in VFS.
1201 if (new_dentry
->d_count
> 1 && new_inode
) {
1203 /* copy the target dentry's name */
1204 dentry
= d_alloc(new_dentry
->d_parent
,
1205 &new_dentry
->d_name
);
1209 /* silly-rename the existing target ... */
1210 err
= nfs_sillyrename(new_dir
, new_dentry
);
1212 new_dentry
= dentry
;
1214 /* hash the replacement target */
1215 d_add(new_dentry
, NULL
);
1218 /* dentry still busy? */
1219 if (new_dentry
->d_count
> 1) {
1221 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1222 new_dentry
->d_parent
->d_name
.name
,
1223 new_dentry
->d_name
.name
,
1224 new_dentry
->d_count
);
1231 * ... prune child dentries and writebacks if needed.
1233 if (old_dentry
->d_count
> 1) {
1234 nfs_wb_all(old_inode
);
1235 shrink_dcache_parent(old_dentry
);
1238 if (new_dentry
->d_count
> 1 && new_inode
) {
1240 printk("nfs_rename: new dentry %s/%s busy, d_count=%d\n",
1241 new_dentry
->d_parent
->d_name
.name
,
1242 new_dentry
->d_name
.name
,
1243 new_dentry
->d_count
);
1249 * To prevent any new references to the target during the rename,
1250 * we unhash the dentry and free the inode in advance.
1252 if (!list_empty(&new_dentry
->d_hash
)) {
1257 d_delete(new_dentry
);
1259 invalidate_inode_pages(new_dir
);
1260 nfs_flush_dircache(new_dir
);
1261 invalidate_inode_pages(old_dir
);
1262 nfs_flush_dircache(old_dir
);
1263 error
= nfs_proc_rename(NFS_DSERVER(old_dentry
),
1264 NFS_FH(old_dentry
->d_parent
), old_dentry
->d_name
.name
,
1265 NFS_FH(new_dentry
->d_parent
), new_dentry
->d_name
.name
);
1267 NFS_CACHEINV(old_dir
);
1268 NFS_CACHEINV(new_dir
);
1269 /* Update the dcache if needed */
1271 d_add(new_dentry
, NULL
);
1272 if (!error
&& !S_ISDIR(old_inode
->i_mode
))
1273 d_move(old_dentry
, new_dentry
);
1276 /* new dentry created? */
1282 int nfs_init_fhcache(void)
1284 nfs_fh_cachep
= kmem_cache_create("nfs_fh",
1285 sizeof(struct nfs_fh
),
1286 0, SLAB_HWCACHE_ALIGN
,
1288 if (nfs_fh_cachep
== NULL
)
1291 nfs_cookie_cachep
= kmem_cache_create("nfs_dcookie",
1292 sizeof(struct nfs_cookie_table
),
1293 0, SLAB_HWCACHE_ALIGN
,
1295 if (nfs_cookie_cachep
== NULL
)
1303 * version-control: t
1304 * kept-new-versions: 5