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 ssize_t
nfs_dir_read(struct file
*, char *, size_t, loff_t
*);
42 static int nfs_readdir(struct file
*, void *, filldir_t
);
43 static struct dentry
*nfs_lookup(struct inode
*, struct dentry
*);
44 static int nfs_create(struct inode
*, struct dentry
*, int);
45 static int nfs_mkdir(struct inode
*, struct dentry
*, int);
46 static int nfs_rmdir(struct inode
*, struct dentry
*);
47 static int nfs_unlink(struct inode
*, struct dentry
*);
48 static int nfs_symlink(struct inode
*, struct dentry
*, const char *);
49 static int nfs_link(struct dentry
*, struct inode
*, struct dentry
*);
50 static int nfs_mknod(struct inode
*, struct dentry
*, int, int);
51 static int nfs_rename(struct inode
*, struct dentry
*,
52 struct inode
*, struct dentry
*);
54 static struct file_operations nfs_dir_operations
= {
55 NULL
, /* lseek - default */
56 nfs_dir_read
, /* read - bad */
57 NULL
, /* write - bad */
58 nfs_readdir
, /* readdir */
59 NULL
, /* select - default */
60 NULL
, /* ioctl - default */
64 nfs_release
, /* release */
68 struct inode_operations nfs_dir_inode_operations
= {
69 &nfs_dir_operations
, /* default directory file-ops */
70 nfs_create
, /* create */
71 nfs_lookup
, /* lookup */
73 nfs_unlink
, /* unlink */
74 nfs_symlink
, /* symlink */
75 nfs_mkdir
, /* mkdir */
76 nfs_rmdir
, /* rmdir */
77 nfs_mknod
, /* mknod */
78 nfs_rename
, /* rename */
80 NULL
, /* follow_link */
85 NULL
, /* permission */
87 NULL
, /* updatepage */
88 nfs_revalidate
, /* revalidate */
92 nfs_dir_read(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
97 /* Each readdir response is composed of entries which look
98 * like the following, as per the NFSv2 RFC:
100 * __u32 not_end zero if end of response
101 * __u32 file ID opaque ino_t
102 * __u32 namelen size of name string
103 * VAR name string the string, padded to modulo 4 bytes
104 * __u32 cookie opaque ID of next entry
106 * When you hit not_end being zero, the next __u32 is non-zero if
107 * this is the end of the complete set of readdir entires for this
108 * directory. This can be used, for example, to initiate pre-fetch.
110 * In order to know what to ask the server for, we only need to know
111 * the final cookie of the previous page, and offset zero has cookie
112 * zero, so we cache cookie to page offset translations in chunks.
114 #define COOKIES_PER_CHUNK (8 - ((sizeof(void *) / sizeof(__u32))))
115 struct nfs_cookie_table
{
116 struct nfs_cookie_table
*next
;
117 __u32 cookies
[COOKIES_PER_CHUNK
];
119 static kmem_cache_t
*nfs_cookie_cachep
;
121 /* This whole scheme relies on the fact that dirent cookies
122 * are monotonically increasing.
124 * Another invariant is that once we have a valid non-zero
125 * EOF marker cached, we also have the complete set of cookie
128 * We return the page offset assosciated with the page where
129 * cookie must be if it exists at all, however if we can not
130 * figure that out conclusively, we return < 0.
132 static long __nfs_readdir_offset(struct inode
*inode
, __u32 cookie
)
134 struct nfs_cookie_table
*p
;
135 unsigned long ret
= 0;
137 for(p
= NFS_COOKIES(inode
); p
!= NULL
; p
= p
->next
) {
140 for (i
= 0; i
< COOKIES_PER_CHUNK
; i
++) {
141 __u32 this_cookie
= p
->cookies
[i
];
143 /* End of known cookies, EOF is our only hope. */
147 /* Next cookie is larger, must be in previous page. */
148 if (this_cookie
> cookie
)
153 /* Exact cookie match, it must be in this page :-) */
154 if (this_cookie
== cookie
)
159 if (NFS_DIREOF(inode
) != 0)
165 static __inline__
long nfs_readdir_offset(struct inode
*inode
, __u32 cookie
)
167 /* Cookie zero is always at page offset zero. Optimize the
168 * other common case since most directories fit entirely
171 if (!cookie
|| (!NFS_COOKIES(inode
) && NFS_DIREOF(inode
)))
173 return __nfs_readdir_offset(inode
, cookie
);
176 /* Since a cookie of zero is declared special by the NFS
177 * protocol, we easily can tell if a cookie in an existing
178 * table chunk is valid or not.
180 * NOTE: The cookies are indexed off-by-one because zero
183 static __inline__ __u32
*find_cookie(struct inode
*inode
, unsigned long off
)
185 static __u32 cookie_zero
= 0;
186 struct nfs_cookie_table
*p
;
192 p
= NFS_COOKIES(inode
);
193 while(off
>= COOKIES_PER_CHUNK
&& p
) {
194 off
-= COOKIES_PER_CHUNK
;
199 ret
= &p
->cookies
[off
];
206 #define NFS_NAMELEN_ALIGN(__len) ((((__len)+3)>>2)<<2)
207 static int create_cookie(__u32 cookie
, unsigned long off
, struct inode
*inode
)
209 struct nfs_cookie_table
**cpp
;
211 cpp
= (struct nfs_cookie_table
**) &NFS_COOKIES(inode
);
212 while (off
>= COOKIES_PER_CHUNK
&& *cpp
) {
213 off
-= COOKIES_PER_CHUNK
;
217 (*cpp
)->cookies
[off
] = cookie
;
219 struct nfs_cookie_table
*new;
222 new = kmem_cache_alloc(nfs_cookie_cachep
, SLAB_ATOMIC
);
227 for(i
= 0; i
< COOKIES_PER_CHUNK
; i
++) {
229 new->cookies
[i
] = cookie
;
238 static struct page
*try_to_get_dirent_page(struct file
*, __u32
, int);
240 /* Recover from a revalidation flush. The case here is that
241 * the inode for the directory got invalidated somehow, and
242 * all of our cached information is lost. In order to get
243 * a correct cookie for the current readdir request from the
244 * user, we must (re-)fetch older readdir page cache entries.
246 * Returns < 0 if some error occurrs, else it is the page offset
249 static long refetch_to_readdir_cookie(struct file
*file
, struct inode
*inode
)
252 u32 goal_cookie
= file
->f_pos
;
253 long cur_off
, ret
= -1L;
258 page
= find_get_page(inode
, cur_off
);
260 if (!Page_Uptodate(page
))
263 __u32
*cp
= find_cookie(inode
, cur_off
);
268 page
= try_to_get_dirent_page(file
, *cp
, 0);
273 /* Someone touched the dir on us. */
277 page_cache_release(page
);
279 if ((ret
= nfs_readdir_offset(inode
, goal_cookie
)) >= 0)
289 page_cache_release(page
);
293 /* Now we cache directories properly, by stuffing the dirent
294 * data directly in the page cache.
296 * Inode invalidation due to refresh etc. takes care of
297 * _everything_, no sloppy entry flushing logic, no extraneous
298 * copying, network direct to page cache, the way it was meant
301 * NOTE: Dirent information verification is done always by the
302 * page-in of the RPC reply, nowhere else, this simplies
303 * things substantially.
305 static struct page
*try_to_get_dirent_page(struct file
*file
, __u32 cookie
, int refetch_ok
)
307 struct nfs_readdirargs rd_args
;
308 struct nfs_readdirres rd_res
;
309 struct dentry
*dentry
= file
->f_dentry
;
310 struct inode
*inode
= dentry
->d_inode
;
311 struct page
*page
, **hash
;
312 unsigned long page_cache
;
317 page_cache
= page_cache_alloc();
321 if ((offset
= nfs_readdir_offset(inode
, cookie
)) < 0) {
323 (offset
= refetch_to_readdir_cookie(file
, inode
)) < 0) {
324 page_cache_free(page_cache
);
329 cookiep
= find_cookie(inode
, offset
);
331 /* Gross fatal error. */
332 page_cache_free(page_cache
);
336 hash
= page_hash(inode
, offset
);
338 page
= __find_lock_page(inode
, offset
, *hash
);
340 page_cache_free(page_cache
);
344 page
= page_cache_entry(page_cache
);
345 if (add_to_page_cache_unique(page
, inode
, offset
, hash
)) {
346 page_cache_release(page
);
350 rd_args
.fh
= NFS_FH(dentry
);
351 rd_res
.buffer
= (char *)page_cache
;
352 rd_res
.bufsiz
= PAGE_CACHE_SIZE
;
353 rd_res
.cookie
= *cookiep
;
355 rd_args
.buffer
= rd_res
.buffer
;
356 rd_args
.bufsiz
= rd_res
.bufsiz
;
357 rd_args
.cookie
= rd_res
.cookie
;
358 if (rpc_call(NFS_CLIENT(inode
),
359 NFSPROC_READDIR
, &rd_args
, &rd_res
, 0) < 0)
361 } while(rd_res
.bufsiz
> 0);
363 if (rd_res
.bufsiz
< 0)
364 NFS_DIREOF(inode
) = rd_res
.cookie
;
365 else if (create_cookie(rd_res
.cookie
, offset
, inode
))
368 SetPageUptodate(page
);
379 /* Seek up to dirent assosciated with the passed in cookie,
380 * then fill in dirents found. Return the last cookie
381 * actually given to the user, to update the file position.
383 static __inline__ u32
nfs_do_filldir(__u32
*p
, u32 cookie
,
384 void *dirent
, filldir_t filldir
)
388 while((end
= *p
++) != 0) {
389 __u32 fileid
, len
, skip
, this_cookie
;
395 skip
= NFS_NAMELEN_ALIGN(len
);
399 if (this_cookie
< cookie
)
402 cookie
= this_cookie
;
403 if (filldir(dirent
, name
, len
, cookie
, fileid
) < 0)
410 /* The file offset position is represented in pure bytes, to
411 * make the page cache interface straight forward.
413 * However, some way is needed to make the connection between the
414 * opaque NFS directory entry cookies and our offsets, so a per-inode
415 * cookie cache table is used.
417 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
419 struct dentry
*dentry
= filp
->f_dentry
;
420 struct inode
*inode
= dentry
->d_inode
;
421 struct page
*page
, **hash
;
425 res
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
429 if (NFS_DIREOF(inode
) && filp
->f_pos
>= NFS_DIREOF(inode
))
432 if ((offset
= nfs_readdir_offset(inode
, filp
->f_pos
)) < 0)
435 hash
= page_hash(inode
, offset
);
436 page
= __find_get_page(inode
, offset
, *hash
);
439 if (!Page_Uptodate(page
))
440 goto dirent_read_error
;
442 filp
->f_pos
= nfs_do_filldir((__u32
*) page_address(page
),
443 filp
->f_pos
, dirent
, filldir
);
444 page_cache_release(page
);
448 page
= try_to_get_dirent_page(filp
, filp
->f_pos
, 1);
452 if (Page_Uptodate(page
))
455 page_cache_release(page
);
460 /* Flush directory cookie and EOF caches for an inode.
461 * So we don't thrash allocating/freeing cookie tables,
462 * we keep the cookies around until the inode is
465 __inline__
void nfs_flush_dircache(struct inode
*inode
)
467 struct nfs_cookie_table
*p
= NFS_COOKIES(inode
);
472 for(i
= 0; i
< COOKIES_PER_CHUNK
; i
++)
477 NFS_DIREOF(inode
) = 0;
480 /* Free up directory cache state, this happens when
481 * nfs_delete_inode is called on an NFS directory.
483 void nfs_free_dircache(struct inode
*inode
)
485 struct nfs_cookie_table
*p
= NFS_COOKIES(inode
);
488 struct nfs_cookie_table
*next
= p
->next
;
489 kmem_cache_free(nfs_cookie_cachep
, p
);
492 NFS_COOKIES(inode
) = NULL
;
493 NFS_DIREOF(inode
) = 0;
497 * Whenever an NFS operation succeeds, we know that the dentry
498 * is valid, so we update the revalidation timestamp.
500 static inline void nfs_renew_times(struct dentry
* dentry
)
502 dentry
->d_time
= jiffies
;
505 static inline int nfs_dentry_force_reval(struct dentry
*dentry
, int flags
)
507 struct inode
*inode
= dentry
->d_inode
;
508 unsigned long timeout
= NFS_ATTRTIMEO(inode
);
511 * If it's the last lookup in a series, we use a stricter
512 * cache consistency check by looking at the parent mtime.
514 * If it's been modified in the last hour, be really strict.
515 * (This still means that we can avoid doing unnecessary
516 * work on directories like /usr/share/bin etc which basically
519 if (!(flags
& LOOKUP_CONTINUE
)) {
520 long diff
= CURRENT_TIME
- dentry
->d_parent
->d_inode
->i_mtime
;
526 return time_after(jiffies
,dentry
->d_time
+ timeout
);
530 * We judge how long we want to trust negative
531 * dentries by looking at the parent inode mtime.
533 * If mtime is close to present time, we revalidate
536 static inline int nfs_neg_need_reval(struct dentry
*dentry
)
538 unsigned long timeout
= 30 * HZ
;
539 long diff
= CURRENT_TIME
- dentry
->d_parent
->d_inode
->i_mtime
;
544 return time_after(jiffies
, dentry
->d_time
+ timeout
);
548 * This is called every time the dcache has a lookup hit,
549 * and we should check whether we can really trust that
552 * NOTE! The hit can be a negative hit too, don't assume
555 * If the dentry is older than the revalidation interval,
556 * we do a new lookup and verify that the dentry is still
559 static int nfs_lookup_revalidate(struct dentry
* dentry
, int flags
)
561 struct dentry
* parent
= dentry
->d_parent
;
562 struct inode
* inode
= dentry
->d_inode
;
564 struct nfs_fh fhandle
;
565 struct nfs_fattr fattr
;
568 * If we don't have an inode, let's look at the parent
569 * directory mtime to get a hint about how often we
570 * should validate things..
573 if (nfs_neg_need_reval(dentry
))
578 if (is_bad_inode(inode
)) {
579 dfprintk(VFS
, "nfs_lookup_validate: %s/%s has dud inode\n",
580 parent
->d_name
.name
, dentry
->d_name
.name
);
587 if (!nfs_dentry_force_reval(dentry
, flags
))
591 * Do a new lookup and check the dentry attributes.
593 error
= nfs_proc_lookup(NFS_DSERVER(parent
), NFS_FH(parent
),
594 dentry
->d_name
.name
, &fhandle
, &fattr
);
598 /* Inode number matches? */
599 if (fattr
.fileid
!= inode
->i_ino
)
602 /* Filehandle matches? */
603 if (memcmp(dentry
->d_fsdata
, &fhandle
, sizeof(struct nfs_fh
))) {
604 if (dentry
->d_count
< 2)
608 /* Ok, remeber that we successfully checked it.. */
609 nfs_renew_times(dentry
);
610 nfs_refresh_inode(inode
, &fattr
);
615 /* Purge readdir caches. */
616 if (dentry
->d_parent
->d_inode
) {
617 invalidate_inode_pages(dentry
->d_parent
->d_inode
);
618 nfs_flush_dircache(dentry
->d_parent
->d_inode
);
620 if (inode
&& S_ISDIR(inode
->i_mode
)) {
621 invalidate_inode_pages(inode
);
622 nfs_flush_dircache(inode
);
628 * This is called from dput() when d_count is going to 0.
629 * We use it to clean up silly-renamed files.
631 static void nfs_dentry_delete(struct dentry
*dentry
)
633 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
634 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
637 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
640 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
641 /* Unhash it first */
643 error
= nfs_safe_remove(dentry
);
645 printk("NFS: can't silly-delete %s/%s, error=%d\n",
646 dentry
->d_parent
->d_name
.name
,
647 dentry
->d_name
.name
, error
);
652 * Sanity check: if the dentry has been unhashed and the
653 * inode still has users, we could have problems ...
655 if (list_empty(&dentry
->d_hash
) && dentry
->d_inode
) {
656 struct inode
*inode
= dentry
->d_inode
;
657 int max_count
= (S_ISDIR(inode
->i_mode
) ? 1 : inode
->i_nlink
);
658 if (inode
->i_count
> max_count
) {
659 printk("nfs_dentry_delete: %s/%s: ino=%ld, count=%d, nlink=%d\n",
660 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
661 inode
->i_ino
, inode
->i_count
, inode
->i_nlink
);
667 static kmem_cache_t
*nfs_fh_cachep
;
669 __inline__
struct nfs_fh
*nfs_fh_alloc(void)
671 return kmem_cache_alloc(nfs_fh_cachep
, SLAB_KERNEL
);
674 __inline__
void nfs_fh_free(struct nfs_fh
*p
)
676 kmem_cache_free(nfs_fh_cachep
, p
);
680 * Called when the dentry is being freed to release private memory.
682 static void nfs_dentry_release(struct dentry
*dentry
)
684 if (dentry
->d_fsdata
)
685 nfs_fh_free(dentry
->d_fsdata
);
688 struct dentry_operations nfs_dentry_operations
= {
689 nfs_lookup_revalidate
, /* d_revalidate(struct dentry *, int) */
691 NULL
, /* d_compare */
692 nfs_dentry_delete
, /* d_delete(struct dentry *) */
693 nfs_dentry_release
, /* d_release(struct dentry *) */
699 * Display all dentries holding the specified inode.
701 static void show_dentry(struct list_head
* dlist
)
703 struct list_head
*tmp
= dlist
;
705 while ((tmp
= tmp
->next
) != dlist
) {
706 struct dentry
* dentry
= list_entry(tmp
, struct dentry
, d_alias
);
707 const char * unhashed
= "";
709 if (list_empty(&dentry
->d_hash
))
710 unhashed
= "(unhashed)";
712 printk("show_dentry: %s/%s, d_count=%d%s\n",
713 dentry
->d_parent
->d_name
.name
,
714 dentry
->d_name
.name
, dentry
->d_count
,
720 static struct dentry
*nfs_lookup(struct inode
*dir
, struct dentry
* dentry
)
724 struct nfs_fh fhandle
;
725 struct nfs_fattr fattr
;
727 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
728 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
730 error
= -ENAMETOOLONG
;
731 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
735 if (!dentry
->d_fsdata
) {
736 dentry
->d_fsdata
= nfs_fh_alloc();
737 if (!dentry
->d_fsdata
)
740 dentry
->d_op
= &nfs_dentry_operations
;
742 error
= nfs_proc_lookup(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
743 dentry
->d_name
.name
, &fhandle
, &fattr
);
745 if (error
== -ENOENT
)
749 inode
= nfs_fhget(dentry
, &fhandle
, &fattr
);
752 if (inode
->i_count
> (S_ISDIR(inode
->i_mode
) ? 1 : inode
->i_nlink
)) {
753 printk("nfs_lookup: %s/%s ino=%ld in use, count=%d, nlink=%d\n",
754 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
755 inode
->i_ino
, inode
->i_count
, inode
->i_nlink
);
756 show_dentry(&inode
->i_dentry
);
760 d_add(dentry
, inode
);
761 nfs_renew_times(dentry
);
766 return ERR_PTR(error
);
770 * Code common to create, mkdir, and mknod.
772 static int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
773 struct nfs_fattr
*fattr
)
778 inode
= nfs_fhget(dentry
, fhandle
, fattr
);
781 if (inode
->i_count
> (S_ISDIR(inode
->i_mode
) ? 1 : inode
->i_nlink
)) {
782 printk("nfs_instantiate: %s/%s ino=%ld in use, count=%d, nlink=%d\n",
783 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
784 inode
->i_ino
, inode
->i_count
, inode
->i_nlink
);
785 show_dentry(&inode
->i_dentry
);
788 d_instantiate(dentry
, inode
);
789 nfs_renew_times(dentry
);
796 * Following a failed create operation, we drop the dentry rather
797 * than retain a negative dentry. This avoids a problem in the event
798 * that the operation succeeded on the server, but an error in the
799 * reply path made it appear to have failed.
801 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
804 struct nfs_sattr sattr
;
805 struct nfs_fattr fattr
;
806 struct nfs_fh fhandle
;
808 dfprintk(VFS
, "NFS: create(%x/%ld, %s\n",
809 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
812 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
813 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
816 * Invalidate the dir cache before the operation to avoid a race.
818 invalidate_inode_pages(dir
);
819 nfs_flush_dircache(dir
);
820 error
= nfs_proc_create(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
821 dentry
->d_name
.name
, &sattr
, &fhandle
, &fattr
);
823 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
830 * See comments for nfs_proc_create regarding failed operations.
832 static int nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, int rdev
)
835 struct nfs_sattr sattr
;
836 struct nfs_fattr fattr
;
837 struct nfs_fh fhandle
;
839 dfprintk(VFS
, "NFS: mknod(%x/%ld, %s\n",
840 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
843 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
844 if (S_ISCHR(mode
) || S_ISBLK(mode
))
845 sattr
.size
= rdev
; /* get out your barf bag */
846 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
848 invalidate_inode_pages(dir
);
849 nfs_flush_dircache(dir
);
850 error
= nfs_proc_create(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
851 dentry
->d_name
.name
, &sattr
, &fhandle
, &fattr
);
853 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
860 * See comments for nfs_proc_create regarding failed operations.
862 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
865 struct nfs_sattr sattr
;
866 struct nfs_fattr fattr
;
867 struct nfs_fh fhandle
;
869 dfprintk(VFS
, "NFS: mkdir(%x/%ld, %s\n",
870 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
872 sattr
.mode
= mode
| S_IFDIR
;
873 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
874 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
877 * Always drop the dentry, we can't always depend on
878 * the fattr returned by the server (AIX seems to be
879 * broken). We're better off doing another lookup than
880 * depending on potentially bogus information.
883 invalidate_inode_pages(dir
);
884 nfs_flush_dircache(dir
);
885 error
= nfs_proc_mkdir(NFS_DSERVER(dentry
), NFS_FH(dentry
->d_parent
),
886 dentry
->d_name
.name
, &sattr
, &fhandle
, &fattr
);
890 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
894 dfprintk(VFS
, "NFS: rmdir(%x/%ld, %s\n",
895 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
898 if (dentry
->d_inode
->i_count
> 1)
899 printk("nfs_rmdir: %s/%s inode busy?? i_count=%d, i_nlink=%d\n",
900 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
901 dentry
->d_inode
->i_count
, dentry
->d_inode
->i_nlink
);
904 invalidate_inode_pages(dir
);
905 nfs_flush_dircache(dir
);
906 error
= nfs_proc_rmdir(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
907 dentry
->d_name
.name
);
909 /* Update i_nlink and invalidate dentry. */
912 if (dentry
->d_inode
->i_nlink
)
913 dentry
->d_inode
->i_nlink
--;
920 /* Note: we copy the code from lookup_dentry() here, only: we have to
921 * omit the directory lock. We are already the owner of the lock when
922 * we reach here. And "down(&dir->i_sem)" would make us sleep forever
923 * ('cause WE have the lock)
925 * VERY IMPORTANT: calculate the hash for this dentry!!!!!!!!
926 * Otherwise the cached lookup DEFINITELY WILL fail. And a new dentry
927 * is created. Without the DCACHE_NFSFS_RENAMED flag. And with d_count
930 * Concerning my choice of the temp name: it is just nice to have
931 * i_ino part of the temp name, as this offers another check whether
932 * somebody attempts to remove the "silly renamed" dentry itself.
933 * Which is something that I consider evil. Your opinion may vary.
935 * Now that I compute the hash value right, it should be possible to simply
936 * check for the DCACHE_NFSFS_RENAMED flag in dentry->d_flag instead of
937 * doing the string compare.
939 * This offers the opportunity to shorten the temp name. Currently, I use
940 * the hex representation of i_ino + an event counter. This sums up to
941 * as much as 36 characters for a 64 bit machine, and needs 20 chars on
944 * The use of i_ino is simply cosmetic. All we need is a unique temp
945 * file name for the .nfs files. The event counter seemed to be adequate.
946 * And as we retry in case such a file already exists, we are guaranteed
951 struct dentry
*nfs_silly_lookup(struct dentry
*parent
, char *silly
, int slen
)
954 struct dentry
*sdentry
;
959 sqstr
.hash
= full_name_hash(silly
, slen
);
960 sdentry
= d_lookup(parent
, &sqstr
);
962 sdentry
= d_alloc(parent
, &sqstr
);
964 return ERR_PTR(-ENOMEM
);
965 res
= nfs_lookup(parent
->d_inode
, sdentry
);
974 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
976 static unsigned int sillycounter
= 0;
977 const int i_inosize
= sizeof(dir
->i_ino
)*2;
978 const int countersize
= sizeof(sillycounter
)*2;
979 const int slen
= strlen(".nfs") + i_inosize
+ countersize
;
981 struct dentry
*sdentry
;
984 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
985 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
989 * Note that a silly-renamed file can be deleted once it's
990 * no longer in use -- it's just an ordinary file now.
992 if (dentry
->d_count
== 1) {
993 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
994 goto out
; /* No need to silly rename. */
998 if (!dentry
->d_inode
)
999 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
1000 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1003 * We don't allow a dentry to be silly-renamed twice.
1006 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1009 sprintf(silly
, ".nfs%*.*lx",
1010 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
1014 char *suffix
= silly
+ slen
- countersize
;
1018 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
1020 dfprintk(VFS
, "trying to rename %s to %s\n",
1021 dentry
->d_name
.name
, silly
);
1023 sdentry
= nfs_silly_lookup(dentry
->d_parent
, silly
, slen
);
1025 * N.B. Better to return EBUSY here ... it could be
1026 * dangerous to delete the file while it's in use.
1028 if (IS_ERR(sdentry
))
1030 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
1032 invalidate_inode_pages(dir
);
1033 nfs_flush_dircache(dir
);
1034 error
= nfs_proc_rename(NFS_SERVER(dir
),
1035 NFS_FH(dentry
->d_parent
), dentry
->d_name
.name
,
1036 NFS_FH(dentry
->d_parent
), silly
);
1038 nfs_renew_times(dentry
);
1039 d_move(dentry
, sdentry
);
1040 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
1041 /* If we return 0 we don't unlink */
1049 * Remove a file after making sure there are no pending writes,
1050 * and after checking that the file has only one user.
1052 * We update inode->i_nlink and free the inode prior to the operation
1053 * to avoid possible races if the server reuses the inode.
1055 static int nfs_safe_remove(struct dentry
*dentry
)
1057 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1058 struct inode
*inode
= dentry
->d_inode
;
1059 int error
, rehash
= 0;
1061 dfprintk(VFS
, "NFS: safe_remove(%s/%s, %ld)\n",
1062 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
1065 /* N.B. not needed now that d_delete is done in advance? */
1069 printk("nfs_safe_remove: %s/%s already negative??\n",
1070 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1074 if (dentry
->d_count
> 1) {
1076 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
1077 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, dentry
->d_count
);
1082 if (inode
&& inode
->i_count
> inode
->i_nlink
)
1083 printk("nfs_safe_remove: %s/%s inode busy?? i_count=%d, i_nlink=%d\n",
1084 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
1085 inode
->i_count
, inode
->i_nlink
);
1088 * Unhash the dentry while we remove the file ...
1090 if (!list_empty(&dentry
->d_hash
)) {
1095 * Update i_nlink and free the inode before unlinking.
1102 invalidate_inode_pages(dir
);
1103 nfs_flush_dircache(dir
);
1104 error
= nfs_proc_remove(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
1105 dentry
->d_name
.name
);
1107 * Rehash the negative dentry if the operation succeeded.
1109 if (!error
&& rehash
)
1110 d_add(dentry
, NULL
);
1115 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1116 * belongs to an active ".nfs..." file and we return -EBUSY.
1118 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1120 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1124 dfprintk(VFS
, "NFS: unlink(%x/%ld, %s)\n",
1125 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
1127 error
= nfs_sillyrename(dir
, dentry
);
1128 if (error
&& error
!= -EBUSY
) {
1129 error
= nfs_safe_remove(dentry
);
1131 nfs_renew_times(dentry
);
1138 nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1140 struct nfs_sattr sattr
;
1143 dfprintk(VFS
, "NFS: symlink(%x/%ld, %s, %s)\n",
1144 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
, symname
);
1146 error
= -ENAMETOOLONG
;
1147 if (strlen(symname
) > NFS_MAXPATHLEN
)
1151 if (dentry
->d_inode
)
1152 printk("nfs_proc_symlink: %s/%s not negative!\n",
1153 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1156 * Fill in the sattr for the call.
1157 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1159 sattr
.mode
= S_IFLNK
| S_IRWXUGO
;
1160 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
1161 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
1164 * Drop the dentry in advance to force a new lookup.
1165 * Since nfs_proc_symlink doesn't return a fattr, we
1166 * can't instantiate the new inode.
1169 invalidate_inode_pages(dir
);
1170 nfs_flush_dircache(dir
);
1171 error
= nfs_proc_symlink(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
1172 dentry
->d_name
.name
, symname
, &sattr
);
1174 nfs_renew_times(dentry
->d_parent
);
1175 } else if (error
== -EEXIST
) {
1176 printk("nfs_proc_symlink: %s/%s already exists??\n",
1177 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1185 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
1187 struct inode
*inode
= old_dentry
->d_inode
;
1190 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
1191 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1192 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1195 * Drop the dentry in advance to force a new lookup.
1196 * Since nfs_proc_link doesn't return a file handle,
1197 * we can't use the existing dentry.
1200 invalidate_inode_pages(dir
);
1201 nfs_flush_dircache(dir
);
1202 error
= nfs_proc_link(NFS_DSERVER(old_dentry
), NFS_FH(old_dentry
),
1203 NFS_FH(dentry
->d_parent
), dentry
->d_name
.name
);
1206 * Update the link count immediately, as some apps
1207 * (e.g. pine) test this after making a link.
1216 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1217 * different file handle for the same inode after a rename (e.g. when
1218 * moving to a different directory). A fail-safe method to do so would
1219 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1220 * rename the old file using the sillyrename stuff. This way, the original
1221 * file in old_dir will go away when the last process iput()s the inode.
1225 * It actually works quite well. One needs to have the possibility for
1226 * at least one ".nfs..." file in each directory the file ever gets
1227 * moved or linked to which happens automagically with the new
1228 * implementation that only depends on the dcache stuff instead of
1229 * using the inode layer
1231 * Unfortunately, things are a little more complicated than indicated
1232 * above. For a cross-directory move, we want to make sure we can get
1233 * rid of the old inode after the operation. This means there must be
1234 * no pending writes (if it's a file), and the use count must be 1.
1235 * If these conditions are met, we can drop the dentries before doing
1238 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1239 struct inode
*new_dir
, struct dentry
*new_dentry
)
1241 struct inode
*old_inode
= old_dentry
->d_inode
;
1242 struct inode
*new_inode
= new_dentry
->d_inode
;
1243 struct dentry
*dentry
= NULL
;
1244 int error
, rehash
= 0, update
= 1;
1246 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1247 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1248 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1249 new_dentry
->d_count
);
1252 * First check whether the target is busy ... we can't
1253 * safely do _any_ rename if the target is in use.
1255 * For files, make a copy of the dentry and then do a
1256 * silly-rename. If the silly-rename succeeds, the
1257 * copied dentry is hashed and becomes the new target.
1259 * With directories check is done in VFS.
1262 if (new_dentry
->d_count
> 1 && new_inode
) {
1264 /* copy the target dentry's name */
1265 dentry
= d_alloc(new_dentry
->d_parent
,
1266 &new_dentry
->d_name
);
1270 /* silly-rename the existing target ... */
1271 err
= nfs_sillyrename(new_dir
, new_dentry
);
1273 new_dentry
= dentry
;
1275 /* hash the replacement target */
1276 d_add(new_dentry
, NULL
);
1279 /* dentry still busy? */
1280 if (new_dentry
->d_count
> 1) {
1282 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1283 new_dentry
->d_parent
->d_name
.name
,new_dentry
->d_name
.name
,new_dentry
->d_count
);
1290 * Check for within-directory rename ... no complications.
1292 if (new_dir
== old_dir
)
1295 * Cross-directory move ...
1297 * ... prune child dentries and writebacks if needed.
1299 if (old_dentry
->d_count
> 1) {
1300 nfs_wb_all(old_inode
);
1301 shrink_dcache_parent(old_dentry
);
1305 * Now check the use counts ... we can't safely do the
1306 * rename unless we can drop the dentries first.
1308 if (old_dentry
->d_count
> 1) {
1310 printk("nfs_rename: old dentry %s/%s busy, d_count=%d\n",
1311 old_dentry
->d_parent
->d_name
.name
,old_dentry
->d_name
.name
,old_dentry
->d_count
);
1315 if (new_dentry
->d_count
> 1 && new_inode
) {
1317 printk("nfs_rename: new dentry %s/%s busy, d_count=%d\n",
1318 new_dentry
->d_parent
->d_name
.name
,new_dentry
->d_name
.name
,new_dentry
->d_count
);
1328 * To prevent any new references to the target during the rename,
1329 * we unhash the dentry and free the inode in advance.
1333 new_inode
->i_count
> (S_ISDIR(new_inode
->i_mode
) ? 1 : new_inode
->i_nlink
))
1334 printk("nfs_rename: %s/%s inode busy?? i_count=%d, i_nlink=%d\n",
1335 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1336 new_inode
->i_count
, new_inode
->i_nlink
);
1338 if (!list_empty(&new_dentry
->d_hash
)) {
1343 d_delete(new_dentry
);
1346 invalidate_inode_pages(new_dir
);
1347 nfs_flush_dircache(new_dir
);
1348 invalidate_inode_pages(old_dir
);
1349 nfs_flush_dircache(old_dir
);
1350 error
= nfs_proc_rename(NFS_DSERVER(old_dentry
),
1351 NFS_FH(old_dentry
->d_parent
), old_dentry
->d_name
.name
,
1352 NFS_FH(new_dentry
->d_parent
), new_dentry
->d_name
.name
);
1353 if (!error
&& !S_ISDIR(old_inode
->i_mode
)) {
1354 /* Update the dcache if needed */
1356 d_add(new_dentry
, NULL
);
1358 d_move(old_dentry
, new_dentry
);
1362 /* new dentry created? */
1368 int nfs_init_fhcache(void)
1370 nfs_fh_cachep
= kmem_cache_create("nfs_fh",
1371 sizeof(struct nfs_fh
),
1372 0, SLAB_HWCACHE_ALIGN
,
1374 if (nfs_fh_cachep
== NULL
)
1377 nfs_cookie_cachep
= kmem_cache_create("nfs_dcookie",
1378 sizeof(struct nfs_cookie_table
),
1379 0, SLAB_HWCACHE_ALIGN
,
1381 if (nfs_cookie_cachep
== NULL
)
1389 * version-control: t
1390 * kept-new-versions: 5