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).
19 #include <linux/sched.h>
20 #include <linux/errno.h>
21 #include <linux/stat.h>
22 #include <linux/fcntl.h>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
25 #include <linux/malloc.h>
27 #include <linux/sunrpc/types.h>
28 #include <linux/nfs_fs.h>
30 #include <asm/segment.h> /* for fs functions */
32 #define NFS_MAX_AGE 10*HZ /* max age for dentry validation */
34 /* needed by smbfs as well ... move to dcache? */
35 extern void nfs_renew_times(struct dentry
*);
37 #define NFS_PARANOIA 1
38 /* #define NFS_DEBUG_VERBOSE 1 */
41 * Head for a dircache entry. Currently still very simple; when
42 * the cache grows larger, we will need a LRU list.
45 dev_t dev
; /* device number */
46 ino_t ino
; /* inode number */
47 u32 cookie
; /* cookie of first entry */
48 unsigned short valid
: 1, /* data is valid */
49 locked
: 1; /* entry locked */
50 unsigned int size
; /* # of entries */
51 unsigned long age
; /* last used */
52 unsigned long mtime
; /* last attr stamp */
53 struct wait_queue
* wait
;
54 __u32
* entry
; /* three __u32's per entry */
57 static int nfs_safe_remove(struct dentry
*);
59 static int nfs_dir_open(struct inode
*, struct file
*);
60 static ssize_t
nfs_dir_read(struct file
*, char *, size_t, loff_t
*);
61 static int nfs_readdir(struct file
*, void *, filldir_t
);
62 static int nfs_lookup(struct inode
*, struct dentry
*);
63 static int nfs_create(struct inode
*, struct dentry
*, int);
64 static int nfs_mkdir(struct inode
*, struct dentry
*, int);
65 static int nfs_rmdir(struct inode
*, struct dentry
*);
66 static int nfs_unlink(struct inode
*, struct dentry
*);
67 static int nfs_symlink(struct inode
*, struct dentry
*, const char *);
68 static int nfs_link(struct dentry
*, struct inode
*, struct dentry
*);
69 static int nfs_mknod(struct inode
*, struct dentry
*, int, int);
70 static int nfs_rename(struct inode
*, struct dentry
*,
71 struct inode
*, struct dentry
*);
73 static struct file_operations nfs_dir_operations
= {
74 NULL
, /* lseek - default */
75 nfs_dir_read
, /* read - bad */
76 NULL
, /* write - bad */
77 nfs_readdir
, /* readdir */
78 NULL
, /* select - default */
79 NULL
, /* ioctl - default */
81 nfs_dir_open
, /* open - revalidate */
83 NULL
, /* no special release code */
87 struct inode_operations nfs_dir_inode_operations
= {
88 &nfs_dir_operations
, /* default directory file-ops */
89 nfs_create
, /* create */
90 nfs_lookup
, /* lookup */
92 nfs_unlink
, /* unlink */
93 nfs_symlink
, /* symlink */
94 nfs_mkdir
, /* mkdir */
95 nfs_rmdir
, /* rmdir */
96 nfs_mknod
, /* mknod */
97 nfs_rename
, /* rename */
99 NULL
, /* follow_link */
101 NULL
, /* writepage */
104 NULL
, /* permission */
106 NULL
, /* updatepage */
107 nfs_revalidate
, /* revalidate */
111 nfs_dir_open(struct inode
*dir
, struct file
*file
)
113 struct dentry
*dentry
= file
->f_dentry
;
115 dfprintk(VFS
, "NFS: nfs_dir_open(%s/%s)\n",
116 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
117 return nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
121 nfs_dir_read(struct file
*filp
, char *buf
, size_t count
, loff_t
*ppos
)
126 static struct nfs_dirent dircache
[NFS_MAX_DIRCACHE
];
129 * We need to do caching of directory entries to prevent an
130 * incredible amount of RPC traffic. Only the most recent open
131 * directory is cached. This seems sufficient for most purposes.
132 * Technically, we ought to flush the cache on close but this is
133 * not a problem in practice.
135 * XXX: Do proper directory caching by stuffing data into the
136 * page cache (may require some fiddling for rsize < PAGE_SIZE).
139 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
141 struct dentry
*dentry
= filp
->f_dentry
;
142 struct inode
*inode
= dentry
->d_inode
;
143 static struct wait_queue
*readdir_wait
= NULL
;
144 struct wait_queue
**waitp
= NULL
;
145 struct nfs_dirent
*cache
, *free
;
146 unsigned long age
, dead
;
153 dfprintk(VFS
, "NFS: nfs_readdir(%s/%s)\n",
154 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
156 if (!inode
|| !S_ISDIR(inode
->i_mode
)) {
157 printk("nfs_readdir: inode is NULL or not a directory\n");
161 result
= nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
166 * Try to find the entry in the cache
170 interruptible_sleep_on(waitp
);
171 if (signal_pending(current
))
176 cookie
= filp
->f_pos
;
179 age
= ~(unsigned long) 0;
180 dead
= jiffies
- NFS_ATTRTIMEO(inode
);
182 for (i
= 0, cache
= dircache
; i
< NFS_MAX_DIRCACHE
; i
++, cache
++) {
184 dprintk("NFS: dircache[%d] valid %d locked %d\n",
185 i, cache->valid, cache->locked);
187 ismydir
= (cache
->dev
== inode
->i_dev
188 && cache
->ino
== inode
->i_ino
);
190 if (!ismydir
|| cache
->cookie
!= cookie
)
192 dfprintk(DIRCACHE
, "NFS: waiting on dircache entry\n");
193 waitp
= &cache
->wait
;
197 if (ismydir
&& cache
->mtime
!= inode
->i_mtime
)
200 if (!cache
->valid
|| cache
->age
< dead
) {
203 } else if (cache
->age
< age
) {
208 if (!ismydir
|| !cache
->valid
)
211 if (cache
->cookie
== cookie
&& cache
->size
> 0) {
212 entry
= cache
->entry
+ (index
= 0);
216 for (j
= 0; j
< cache
->size
; j
++) {
217 __u32
*this_ent
= cache
->entry
+ j
*3;
219 if (*(this_ent
+1) != cookie
)
221 if (j
< cache
->size
- 1) {
223 entry
= this_ent
+ 3;
224 } else if (*(this_ent
+2) & (1 << 15)) {
231 dfprintk(DIRCACHE
, "NFS: found dircache entry %d\n",
232 (int)(cache
- dircache
));
239 * Okay, entry not present in cache, or locked and inaccessible.
240 * Set up the cache entry and attempt a READDIR call.
243 if ((cache
= free
) == NULL
) {
244 dfprintk(DIRCACHE
, "NFS: dircache contention\n");
245 waitp
= &readdir_wait
;
248 dfprintk(DIRCACHE
, "NFS: using free dircache entry %d\n",
249 (int)(free
- dircache
));
250 cache
->cookie
= cookie
;
253 cache
->dev
= inode
->i_dev
;
254 cache
->ino
= inode
->i_ino
;
257 cache
->entry
= (__u32
*) get_free_page(GFP_KERNEL
);
262 result
= nfs_proc_readdir(NFS_SERVER(inode
), NFS_FH(dentry
),
263 cookie
, PAGE_SIZE
, cache
->entry
);
266 cache
->size
= result
;
268 entry
= cache
->entry
+ (index
= 0);
270 cache
->mtime
= inode
->i_mtime
;
271 cache
->age
= jiffies
;
274 * Yowza! We have a cache entry...
276 start
= (char *) cache
->entry
;
277 while (index
< cache
->size
) {
278 __u32 fileid
= *entry
++;
279 __u32 nextpos
= *entry
++; /* cookie */
280 __u32 length
= *entry
++;
283 * Unpack the eof flag, offset, and length
285 result
= length
& (1 << 15); /* eof flag */
286 name
= start
+ ((length
>> 16) & 0xFFFF);
289 dprintk("NFS: filldir(%p, %.*s, %d, %d, %x, eof %x)\n", entry,
290 (int) length, name, length,
291 (unsigned int) filp->f_pos,
295 if (filldir(dirent
, name
, length
, cookie
, fileid
) < 0)
300 filp
->f_pos
= cookie
;
303 /* XXX: May want to kick async readdir-ahead here. Not too hard
307 dfprintk(DIRCACHE
, "NFS: nfs_readdir complete\n");
309 wake_up(&cache
->wait
);
310 wake_up(&readdir_wait
);
317 * Invalidate dircache entries for an inode.
320 nfs_invalidate_dircache(struct inode
*inode
)
322 struct nfs_dirent
*cache
= dircache
;
323 dev_t dev
= inode
->i_dev
;
324 ino_t ino
= inode
->i_ino
;
327 dfprintk(DIRCACHE
, "NFS: invalidate dircache for %x/%ld\n", dev
, (long)ino
);
328 for (i
= NFS_MAX_DIRCACHE
; i
--; cache
++) {
329 if (cache
->ino
!= ino
)
331 if (cache
->dev
!= dev
)
334 printk("NFS: cache locked for %s/%ld\n",
335 kdevname(dev
), (long) ino
);
338 cache
->valid
= 0; /* brute force */
343 * Invalidate the dircache for a super block (or all caches),
344 * and release the cache memory.
347 nfs_invalidate_dircache_sb(struct super_block
*sb
)
349 struct nfs_dirent
*cache
= dircache
;
352 for (i
= NFS_MAX_DIRCACHE
; i
--; cache
++) {
353 if (sb
&& sb
->s_dev
!= cache
->dev
)
356 printk("NFS: cache locked at umount %s\n",
357 (cache
->entry
? "(lost a page!)" : ""));
360 cache
->valid
= 0; /* brute force */
362 free_page((unsigned long) cache
->entry
);
369 * Free directory cache memory
370 * Called from cleanup_module
373 nfs_free_dircache(void)
375 dfprintk(DIRCACHE
, "NFS: freeing dircache\n");
376 nfs_invalidate_dircache_sb(NULL
);
380 * This is called every time the dcache has a lookup hit,
381 * and we should check whether we can really trust that
384 * NOTE! The hit can be a negative hit too, don't assume
387 * The decision to drop the dentry should probably be
388 * smarter than this. Right now we believe in directories
389 * for 10 seconds, and in normal files for five..
391 static int nfs_lookup_revalidate(struct dentry
* dentry
)
393 unsigned long time
= jiffies
- dentry
->d_time
;
394 unsigned long max
= 5*HZ
;
396 if (dentry
->d_inode
) {
397 if (is_bad_inode(dentry
->d_inode
)) {
399 printk("nfs_lookup_validate: %s/%s has dud inode\n",
400 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
404 if (S_ISDIR(dentry
->d_inode
->i_mode
))
408 return (time
< max
) || IS_ROOT(dentry
);
414 * This is called from dput() when d_count is going to 0.
415 * We use it to clean up silly-renamed files, and to check
416 * for dentries that have already expired.
418 static void nfs_dentry_delete(struct dentry
*dentry
)
420 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
423 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
424 #ifdef NFS_DEBUG_VERBOSE
425 printk("nfs_dentry_delete: unlinking %s/%s\n",
426 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
428 /* Unhash it first */
430 error
= nfs_safe_remove(dentry
);
432 printk("NFS: can't silly-delete %s/%s, error=%d\n",
433 dentry
->d_parent
->d_name
.name
,
434 dentry
->d_name
.name
, error
);
437 * Check whether to expire the dentry ...
440 unsigned long age
= jiffies
- dentry
->d_time
;
441 if (age
> NFS_MAX_AGE
)
447 * Sanity check: if the dentry has been unhashed and the
448 * inode still has users, we could have problems ...
450 if (list_empty(&dentry
->d_hash
) && dentry
->d_inode
) {
451 struct inode
*inode
= dentry
->d_inode
;
452 int max_count
= (S_ISDIR(inode
->i_mode
) ? 1 : inode
->i_nlink
);
453 if (inode
->i_count
> max_count
) {
454 printk("nfs_dentry_delete: %s/%s: ino=%ld, count=%d, nlink=%d\n",
455 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
456 inode
->i_ino
, inode
->i_count
, inode
->i_nlink
);
463 * Called to free the inode from the dentry. We must flush
464 * any pending writes for this dentry before freeing the inode.
466 static void nfs_dentry_iput(struct dentry
*dentry
, struct inode
*inode
)
468 if (NFS_WRITEBACK(inode
)) {
470 printk("nfs_dentry_iput: pending writes for %s/%s, i_count=%d\n",
471 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, inode
->i_count
);
473 while (nfs_find_dentry_request(inode
, dentry
)) {
475 printk("nfs_dentry_iput: flushing %s/%s\n",
476 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
478 nfs_flush_dirty_pages(inode
, 0, 0, 0);
485 * Called when the dentry is being freed to release private memory.
487 static void nfs_dentry_release(struct dentry
*dentry
)
489 if (dentry
->d_fsdata
)
490 kfree(dentry
->d_fsdata
);
493 struct dentry_operations nfs_dentry_operations
= {
494 nfs_lookup_revalidate
, /* d_validate(struct dentry *) */
496 NULL
, /* d_compare */
497 nfs_dentry_delete
, /* d_delete(struct dentry *) */
498 nfs_dentry_release
, /* d_release(struct dentry *) */
499 nfs_dentry_iput
/* d_iput(struct dentry *, struct inode *) */
504 * Display all dentries holding the specified inode.
506 static void show_dentry(struct list_head
* dlist
)
508 struct list_head
*tmp
= dlist
;
510 while ((tmp
= tmp
->next
) != dlist
) {
511 struct dentry
* dentry
= list_entry(tmp
, struct dentry
, d_alias
);
512 const char * unhashed
= "";
514 if (list_empty(&dentry
->d_hash
))
515 unhashed
= "(unhashed)";
517 printk("show_dentry: %s/%s, d_count=%d%s\n",
518 dentry
->d_parent
->d_name
.name
,
519 dentry
->d_name
.name
, dentry
->d_count
,
526 * Whenever a lookup succeeds, we know the parent directories
527 * are all valid, so we want to update the dentry timestamps.
529 void nfs_renew_times(struct dentry
* dentry
)
532 dentry
->d_time
= jiffies
;
533 if (dentry
== dentry
->d_parent
)
535 dentry
= dentry
->d_parent
;
539 static int nfs_lookup(struct inode
*dir
, struct dentry
* dentry
)
543 struct nfs_fh fhandle
;
544 struct nfs_fattr fattr
;
546 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
547 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
549 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
550 printk("nfs_lookup: inode is NULL or not a directory\n");
554 error
= -ENAMETOOLONG
;
555 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
559 if (!dentry
->d_fsdata
) {
560 dentry
->d_fsdata
= kmalloc(sizeof(struct nfs_fh
), GFP_KERNEL
);
561 if (!dentry
->d_fsdata
)
564 dentry
->d_op
= &nfs_dentry_operations
;
566 error
= nfs_proc_lookup(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
567 dentry
->d_name
.name
, &fhandle
, &fattr
);
569 if (error
== -ENOENT
)
573 inode
= nfs_fhget(dentry
, &fhandle
, &fattr
);
576 if (inode
->i_count
> (S_ISDIR(inode
->i_mode
) ? 1 : inode
->i_nlink
)) {
577 printk("nfs_lookup: %s/%s ino=%ld in use, count=%d, nlink=%d\n",
578 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
579 inode
->i_ino
, inode
->i_count
, inode
->i_nlink
);
580 show_dentry(&inode
->i_dentry
);
584 d_add(dentry
, inode
);
585 nfs_renew_times(dentry
);
591 printk("nfs_lookup: %s/%s failed, error=%d\n",
592 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, error
);
599 * Code common to create, mkdir, and mknod.
601 static int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
602 struct nfs_fattr
*fattr
)
607 inode
= nfs_fhget(dentry
, fhandle
, fattr
);
610 if (inode
->i_count
> (S_ISDIR(inode
->i_mode
) ? 1 : inode
->i_nlink
)) {
611 printk("nfs_instantiate: %s/%s ino=%ld in use, count=%d, nlink=%d\n",
612 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
613 inode
->i_ino
, inode
->i_count
, inode
->i_nlink
);
614 show_dentry(&inode
->i_dentry
);
617 d_instantiate(dentry
, inode
);
618 nfs_renew_times(dentry
);
625 * Following a failed create operation, we drop the dentry rather
626 * than retain a negative dentry. This avoids a problem in the event
627 * that the operation succeeded on the server, but an error in the
628 * reply path made it appear to have failed.
630 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
633 struct nfs_sattr sattr
;
634 struct nfs_fattr fattr
;
635 struct nfs_fh fhandle
;
637 dfprintk(VFS
, "NFS: create(%x/%ld, %s\n",
638 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
640 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
641 printk("nfs_create: inode is NULL or not a directory\n");
645 error
= -ENAMETOOLONG
;
646 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
650 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
651 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
654 * Invalidate the dir cache before the operation to avoid a race.
656 nfs_invalidate_dircache(dir
);
657 error
= nfs_proc_create(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
658 dentry
->d_name
.name
, &sattr
, &fhandle
, &fattr
);
660 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
668 * See comments for nfs_proc_create regarding failed operations.
670 static int nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, int rdev
)
673 struct nfs_sattr sattr
;
674 struct nfs_fattr fattr
;
675 struct nfs_fh fhandle
;
677 dfprintk(VFS
, "NFS: mknod(%x/%ld, %s\n",
678 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
680 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
681 printk("nfs_mknod: inode is NULL or not a directory\n");
685 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
686 return -ENAMETOOLONG
;
689 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
690 if (S_ISCHR(mode
) || S_ISBLK(mode
))
691 sattr
.size
= rdev
; /* get out your barf bag */
692 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
694 nfs_invalidate_dircache(dir
);
695 error
= nfs_proc_create(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
696 dentry
->d_name
.name
, &sattr
, &fhandle
, &fattr
);
698 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
705 * See comments for nfs_proc_create regarding failed operations.
707 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
710 struct nfs_sattr sattr
;
711 struct nfs_fattr fattr
;
712 struct nfs_fh fhandle
;
714 dfprintk(VFS
, "NFS: mkdir(%x/%ld, %s\n",
715 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
717 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
718 printk("nfs_mkdir: inode is NULL or not a directory\n");
722 error
= -ENAMETOOLONG
;
723 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
726 sattr
.mode
= mode
| S_IFDIR
;
727 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
728 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
731 * Always drop the dentry, we can't always depend on
732 * the fattr returned by the server (AIX seems to be
733 * broken). We're better off doing another lookup than
734 * depending on potentially bogus information.
737 nfs_invalidate_dircache(dir
);
738 error
= nfs_proc_mkdir(NFS_DSERVER(dentry
), NFS_FH(dentry
->d_parent
),
739 dentry
->d_name
.name
, &sattr
, &fhandle
, &fattr
);
745 * To avoid retaining a stale inode reference, we check the dentry
746 * use count prior to the operation, and return EBUSY if it has
749 * We update inode->i_nlink and free the inode prior to the operation
750 * to avoid possible races if the server reuses the inode.
752 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
754 int error
, rehash
= 0;
756 dfprintk(VFS
, "NFS: rmdir(%x/%ld, %s\n",
757 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
759 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
760 printk("nfs_rmdir: inode is NULL or not a directory\n");
764 error
= -ENAMETOOLONG
;
765 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
769 if (dentry
->d_count
> 1) {
770 /* Attempt to shrink child dentries ... */
771 shrink_dcache_parent(dentry
);
772 if (dentry
->d_count
> 1)
776 if (dentry
->d_inode
->i_count
> 1)
777 printk("nfs_rmdir: %s/%s inode busy?? i_count=%d, i_nlink=%d\n",
778 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
779 dentry
->d_inode
->i_count
, dentry
->d_inode
->i_nlink
);
782 * Unhash the dentry while we remove the directory.
784 if (!list_empty(&dentry
->d_hash
)) {
789 * Update i_nlink and free the inode before unlinking.
791 if (dentry
->d_inode
->i_nlink
)
792 dentry
->d_inode
->i_nlink
--;
794 nfs_invalidate_dircache(dir
);
795 error
= nfs_proc_rmdir(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
796 dentry
->d_name
.name
);
800 nfs_renew_times(dentry
);
807 /* Note: we copy the code from lookup_dentry() here, only: we have to
808 * omit the directory lock. We are already the owner of the lock when
809 * we reach here. And "down(&dir->i_sem)" would make us sleep forever
810 * ('cause WE have the lock)
812 * VERY IMPORTANT: calculate the hash for this dentry!!!!!!!!
813 * Otherwise the cached lookup DEFINITELY WILL fail. And a new dentry
814 * is created. Without the DCACHE_NFSFS_RENAMED flag. And with d_count
817 * Concerning my choice of the temp name: it is just nice to have
818 * i_ino part of the temp name, as this offers another check whether
819 * somebody attempts to remove the "silly renamed" dentry itself.
820 * Which is something that I consider evil. Your opinion may vary.
822 * Now that I compute the hash value right, it should be possible to simply
823 * check for the DCACHE_NFSFS_RENAMED flag in dentry->d_flag instead of
824 * doing the string compare.
826 * This offers the opportunity to shorten the temp name. Currently, I use
827 * the hex representation of i_ino + an event counter. This sums up to
828 * as much as 36 characters for a 64 bit machine, and needs 20 chars on
831 * The use of i_ino is simply cosmetic. All we need is a unique temp
832 * file name for the .nfs files. The event counter seemed to be adequate.
833 * And as we retry in case such a file already exists, we are guaranteed
838 struct dentry
*nfs_silly_lookup(struct dentry
*parent
, char *silly
, int slen
)
841 struct dentry
*sdentry
;
846 sqstr
.hash
= full_name_hash(silly
, slen
);
847 sdentry
= d_lookup(parent
, &sqstr
);
849 sdentry
= d_alloc(parent
, &sqstr
);
851 return ERR_PTR(-ENOMEM
);
852 error
= nfs_lookup(parent
->d_inode
, sdentry
);
855 return ERR_PTR(error
);
861 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
863 static unsigned int sillycounter
= 0;
864 const int i_inosize
= sizeof(dir
->i_ino
)*2;
865 const int countersize
= sizeof(sillycounter
)*2;
866 const int slen
= strlen(".nfs") + i_inosize
+ countersize
;
868 struct dentry
*sdentry
;
872 * Note that a silly-renamed file can be deleted once it's
873 * no longer in use -- it's just an ordinary file now.
875 if (dentry
->d_count
== 1) {
876 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
877 goto out
; /* No need to silly rename. */
881 if (!dentry
->d_inode
)
882 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
883 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
886 * We don't allow a dentry to be silly-renamed twice.
889 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
892 sprintf(silly
, ".nfs%*.*lx",
893 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
897 char *suffix
= silly
+ slen
- countersize
;
901 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
903 dfprintk(VFS
, "trying to rename %s to %s\n",
904 dentry
->d_name
.name
, silly
);
906 sdentry
= nfs_silly_lookup(dentry
->d_parent
, silly
, slen
);
908 * N.B. Better to return EBUSY here ... it could be
909 * dangerous to delete the file while it's in use.
913 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
915 nfs_invalidate_dircache(dir
);
916 error
= nfs_proc_rename(NFS_SERVER(dir
),
917 NFS_FH(dentry
->d_parent
), dentry
->d_name
.name
,
918 NFS_FH(dentry
->d_parent
), silly
);
920 nfs_renew_times(dentry
);
921 d_move(dentry
, sdentry
);
922 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
923 /* If we return 0 we don't unlink */
931 * Remove a file after making sure there are no pending writes,
932 * and after checking that the file has only one user.
934 * We update inode->i_nlink and free the inode prior to the operation
935 * to avoid possible races if the server reuses the inode.
937 static int nfs_safe_remove(struct dentry
*dentry
)
939 struct inode
*dir
= dentry
->d_parent
->d_inode
;
940 struct inode
*inode
= dentry
->d_inode
;
941 int error
, rehash
= 0;
943 /* N.B. not needed now that d_delete is done in advance? */
946 if (NFS_WRITEBACK(inode
)) {
947 nfs_flush_dirty_pages(inode
, 0, 0, 0);
948 if (NFS_WRITEBACK(inode
)) {
950 printk("nfs_safe_remove: %s/%s writes pending, d_count=%d\n",
951 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, dentry
->d_count
);
958 printk("nfs_safe_remove: %s/%s already negative??\n",
959 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
963 if (dentry
->d_count
> 1) {
965 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
966 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, dentry
->d_count
);
971 if (inode
&& inode
->i_count
> inode
->i_nlink
)
972 printk("nfs_safe_remove: %s/%s inode busy?? i_count=%d, i_nlink=%d\n",
973 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
974 inode
->i_count
, inode
->i_nlink
);
977 * Unhash the dentry while we remove the file ...
979 if (!list_empty(&dentry
->d_hash
)) {
984 * Update i_nlink and free the inode before unlinking.
991 nfs_invalidate_dircache(dir
);
992 error
= nfs_proc_remove(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
993 dentry
->d_name
.name
);
995 * Rehash the negative dentry if the operation succeeded.
997 if (!error
&& rehash
)
1003 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
1004 * belongs to an active ".nfs..." file and we return -EBUSY.
1006 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
1008 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1012 dfprintk(VFS
, "NFS: unlink(%x/%ld, %s)\n",
1013 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
1015 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
1016 printk("nfs_unlink: inode is NULL or not a directory\n");
1020 error
= -ENAMETOOLONG
;
1021 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
1024 error
= nfs_sillyrename(dir
, dentry
);
1025 if (error
&& error
!= -EBUSY
) {
1026 error
= nfs_safe_remove(dentry
);
1028 nfs_renew_times(dentry
);
1036 nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1038 struct nfs_sattr sattr
;
1041 dfprintk(VFS
, "NFS: symlink(%x/%ld, %s, %s)\n",
1042 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
, symname
);
1044 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
1045 printk("nfs_symlink: inode is NULL or not a directory\n");
1049 error
= -ENAMETOOLONG
;
1050 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
1053 if (strlen(symname
) > NFS_MAXPATHLEN
)
1057 if (dentry
->d_inode
)
1058 printk("nfs_proc_symlink: %s/%s not negative!\n",
1059 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1062 * Fill in the sattr for the call.
1063 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1065 sattr
.mode
= S_IFLNK
| S_IRWXUGO
;
1066 sattr
.uid
= sattr
.gid
= sattr
.size
= (unsigned) -1;
1067 sattr
.atime
.seconds
= sattr
.mtime
.seconds
= (unsigned) -1;
1070 * Drop the dentry in advance to force a new lookup.
1071 * Since nfs_proc_symlink doesn't return a fattr, we
1072 * can't instantiate the new inode.
1075 nfs_invalidate_dircache(dir
);
1076 error
= nfs_proc_symlink(NFS_SERVER(dir
), NFS_FH(dentry
->d_parent
),
1077 dentry
->d_name
.name
, symname
, &sattr
);
1079 nfs_renew_times(dentry
->d_parent
);
1080 } else if (error
== -EEXIST
) {
1081 printk("nfs_proc_symlink: %s/%s already exists??\n",
1082 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1090 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
1092 struct inode
*inode
= old_dentry
->d_inode
;
1095 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
1096 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1097 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1099 if (!dir
|| !S_ISDIR(dir
->i_mode
)) {
1100 printk("nfs_link: dir is NULL or not a directory\n");
1104 error
= -ENAMETOOLONG
;
1105 if (dentry
->d_name
.len
> NFS_MAXNAMLEN
)
1109 * Drop the dentry in advance to force a new lookup.
1110 * Since nfs_proc_link doesn't return a file handle,
1111 * we can't use the existing dentry.
1114 nfs_invalidate_dircache(dir
);
1115 error
= nfs_proc_link(NFS_DSERVER(old_dentry
), NFS_FH(old_dentry
),
1116 NFS_FH(dentry
->d_parent
), dentry
->d_name
.name
);
1119 * Update the link count immediately, as some apps
1120 * (e.g. pine) test this after making a link.
1130 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1131 * different file handle for the same inode after a rename (e.g. when
1132 * moving to a different directory). A fail-safe method to do so would
1133 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1134 * rename the old file using the sillyrename stuff. This way, the original
1135 * file in old_dir will go away when the last process iput()s the inode.
1139 * It actually works quite well. One needs to have the possibility for
1140 * at least one ".nfs..." file in each directory the file ever gets
1141 * moved or linked to which happens automagically with the new
1142 * implementation that only depends on the dcache stuff instead of
1143 * using the inode layer
1145 * Unfortunately, things are a little more complicated than indicated
1146 * above. For a cross-directory move, we want to make sure we can get
1147 * rid of the old inode after the operation. This means there must be
1148 * no pending writes (if it's a file), and the use count must be 1.
1149 * If these conditions are met, we can drop the dentries before doing
1152 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1153 struct inode
*new_dir
, struct dentry
*new_dentry
)
1155 struct inode
*old_inode
= old_dentry
->d_inode
;
1156 struct inode
*new_inode
= new_dentry
->d_inode
;
1157 int error
, rehash
= 0, update
= 1;
1159 #ifdef NFS_DEBUG_VERBOSE
1160 printk("nfs_rename: old %s/%s, count=%d, new %s/%s, count=%d\n",
1161 old_dentry
->d_parent
->d_name
.name
,old_dentry
->d_name
.name
,old_dentry
->d_count
,
1162 new_dentry
->d_parent
->d_name
.name
,new_dentry
->d_name
.name
,new_dentry
->d_count
);
1164 if (!old_dir
|| !S_ISDIR(old_dir
->i_mode
)) {
1165 printk("nfs_rename: old inode is NULL or not a directory\n");
1169 if (!new_dir
|| !S_ISDIR(new_dir
->i_mode
)) {
1170 printk("nfs_rename: new inode is NULL or not a directory\n");
1174 error
= -ENAMETOOLONG
;
1175 if (old_dentry
->d_name
.len
> NFS_MAXNAMLEN
||
1176 new_dentry
->d_name
.len
> NFS_MAXNAMLEN
)
1180 * First check whether the target is busy ... we can't
1181 * safely do _any_ rename if the target is in use.
1183 if (new_dentry
->d_count
> 1 && !list_empty(&new_dentry
->d_subdirs
))
1184 shrink_dcache_parent(new_dentry
);
1186 if (new_dentry
->d_count
> 1) {
1188 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1189 new_dentry
->d_parent
->d_name
.name
,new_dentry
->d_name
.name
,new_dentry
->d_count
);
1195 * Check for within-directory rename ... no complications.
1197 if (new_dir
== old_dir
)
1200 * Cross-directory move ... check whether it's a file.
1202 if (S_ISREG(old_inode
->i_mode
)) {
1203 if (NFS_WRITEBACK(old_inode
)) {
1205 printk("nfs_rename: %s/%s has pending writes\n",
1206 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
);
1208 nfs_flush_dirty_pages(old_inode
, 0, 0, 0);
1209 if (NFS_WRITEBACK(old_inode
)) {
1211 printk("nfs_rename: %s/%s has pending writes after flush\n",
1212 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
);
1219 * Moving a directory ... prune child dentries if needed.
1221 else if (old_dentry
->d_count
> 1)
1222 shrink_dcache_parent(old_dentry
);
1225 * Now check the use counts ... we can't safely do the
1226 * rename unless we can drop the dentries first.
1228 if (old_dentry
->d_count
> 1) {
1230 printk("nfs_rename: old dentry %s/%s busy, d_count=%d\n",
1231 old_dentry
->d_parent
->d_name
.name
,old_dentry
->d_name
.name
,old_dentry
->d_count
);
1235 if (new_dentry
->d_count
> 1) {
1237 printk("nfs_rename: new dentry %s/%s busy, d_count=%d\n",
1238 new_dentry
->d_parent
->d_name
.name
,new_dentry
->d_name
.name
,new_dentry
->d_count
);
1248 * To prevent any new references to the target during the rename,
1249 * we unhash the dentry and free the inode in advance.
1253 new_inode
->i_count
> (S_ISDIR(new_inode
->i_mode
) ? 1 : new_inode
->i_nlink
))
1254 printk("nfs_rename: %s/%s inode busy?? i_count=%d, i_nlink=%d\n",
1255 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1256 new_inode
->i_count
, new_inode
->i_nlink
);
1258 if (!list_empty(&new_dentry
->d_hash
)) {
1263 d_delete(new_dentry
);
1266 nfs_invalidate_dircache(new_dir
);
1267 nfs_invalidate_dircache(old_dir
);
1268 error
= nfs_proc_rename(NFS_DSERVER(old_dentry
),
1269 NFS_FH(old_dentry
->d_parent
), old_dentry
->d_name
.name
,
1270 NFS_FH(new_dentry
->d_parent
), new_dentry
->d_name
.name
);
1272 /* Update the dcache if needed */
1274 d_add(new_dentry
, NULL
);
1276 d_move(old_dentry
, new_dentry
);
1284 * version-control: t
1285 * kept-new-versions: 5