Import 2.1.118
[davej-history.git] / fs / nfs / dir.c
blob3e5e59affdf946019b9d254a44d47fbf197557cc
1 /*
2 * linux/fs/nfs/dir.c
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>
26 #include <linux/mm.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.
44 struct nfs_dirent {
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 */
80 NULL, /* mmap */
81 nfs_dir_open, /* open - revalidate */
82 NULL, /* flush */
83 NULL, /* no special release code */
84 NULL /* fsync */
87 struct inode_operations nfs_dir_inode_operations = {
88 &nfs_dir_operations, /* default directory file-ops */
89 nfs_create, /* create */
90 nfs_lookup, /* lookup */
91 nfs_link, /* link */
92 nfs_unlink, /* unlink */
93 nfs_symlink, /* symlink */
94 nfs_mkdir, /* mkdir */
95 nfs_rmdir, /* rmdir */
96 nfs_mknod, /* mknod */
97 nfs_rename, /* rename */
98 NULL, /* readlink */
99 NULL, /* follow_link */
100 NULL, /* readpage */
101 NULL, /* writepage */
102 NULL, /* bmap */
103 NULL, /* truncate */
104 NULL, /* permission */
105 NULL, /* smap */
106 NULL, /* updatepage */
107 nfs_revalidate, /* revalidate */
110 static int
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);
120 static ssize_t
121 nfs_dir_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
123 return -EISDIR;
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;
147 u32 cookie;
148 int ismydir, result;
149 int i, j, index = 0;
150 __u32 *entry;
151 char *name, *start;
153 dfprintk(VFS, "NFS: nfs_readdir(%s/%s)\n",
154 dentry->d_parent->d_name.name, dentry->d_name.name);
155 result = -EBADF;
156 if (!inode || !S_ISDIR(inode->i_mode)) {
157 printk("nfs_readdir: inode is NULL or not a directory\n");
158 goto out;
161 result = nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
162 if (result < 0)
163 goto out;
166 * Try to find the entry in the cache
168 again:
169 if (waitp) {
170 interruptible_sleep_on(waitp);
171 if (signal_pending(current))
172 return -ERESTARTSYS;
173 waitp = NULL;
176 cookie = filp->f_pos;
177 entry = NULL;
178 free = NULL;
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);
189 if (cache->locked) {
190 if (!ismydir || cache->cookie != cookie)
191 continue;
192 dfprintk(DIRCACHE, "NFS: waiting on dircache entry\n");
193 waitp = &cache->wait;
194 goto again;
197 if (ismydir && cache->mtime != inode->i_mtime)
198 cache->valid = 0;
200 if (!cache->valid || cache->age < dead) {
201 free = cache;
202 age = 0;
203 } else if (cache->age < age) {
204 free = cache;
205 age = cache->age;
208 if (!ismydir || !cache->valid)
209 continue;
211 if (cache->cookie == cookie && cache->size > 0) {
212 entry = cache->entry + (index = 0);
213 cache->locked = 1;
214 break;
216 for (j = 0; j < cache->size; j++) {
217 __u32 *this_ent = cache->entry + j*3;
219 if (*(this_ent+1) != cookie)
220 continue;
221 if (j < cache->size - 1) {
222 index = j + 1;
223 entry = this_ent + 3;
224 } else if (*(this_ent+2) & (1 << 15)) {
225 /* eof */
226 return 0;
228 break;
230 if (entry) {
231 dfprintk(DIRCACHE, "NFS: found dircache entry %d\n",
232 (int)(cache - dircache));
233 cache->locked = 1;
234 break;
239 * Okay, entry not present in cache, or locked and inaccessible.
240 * Set up the cache entry and attempt a READDIR call.
242 if (entry == NULL) {
243 if ((cache = free) == NULL) {
244 dfprintk(DIRCACHE, "NFS: dircache contention\n");
245 waitp = &readdir_wait;
246 goto again;
248 dfprintk(DIRCACHE, "NFS: using free dircache entry %d\n",
249 (int)(free - dircache));
250 cache->cookie = cookie;
251 cache->locked = 1;
252 cache->valid = 0;
253 cache->dev = inode->i_dev;
254 cache->ino = inode->i_ino;
255 if (!cache->entry) {
256 result = -ENOMEM;
257 cache->entry = (__u32 *) get_free_page(GFP_KERNEL);
258 if (!cache->entry)
259 goto done;
262 result = nfs_proc_readdir(NFS_SERVER(inode), NFS_FH(dentry),
263 cookie, PAGE_SIZE, cache->entry);
264 if (result <= 0)
265 goto done;
266 cache->size = result;
267 cache->valid = 1;
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);
287 length &= 0x7FFF;
289 dprintk("NFS: filldir(%p, %.*s, %d, %d, %x, eof %x)\n", entry,
290 (int) length, name, length,
291 (unsigned int) filp->f_pos,
292 fileid, result);
295 if (filldir(dirent, name, length, cookie, fileid) < 0)
296 break;
297 cookie = nextpos;
298 index++;
300 filp->f_pos = cookie;
301 result = 0;
303 /* XXX: May want to kick async readdir-ahead here. Not too hard
304 * to do. */
306 done:
307 dfprintk(DIRCACHE, "NFS: nfs_readdir complete\n");
308 cache->locked = 0;
309 wake_up(&cache->wait);
310 wake_up(&readdir_wait);
312 out:
313 return result;
317 * Invalidate dircache entries for an inode.
319 void
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;
325 int i;
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)
330 continue;
331 if (cache->dev != dev)
332 continue;
333 if (cache->locked) {
334 printk("NFS: cache locked for %s/%ld\n",
335 kdevname(dev), (long) ino);
336 continue;
338 cache->valid = 0; /* brute force */
343 * Invalidate the dircache for a super block (or all caches),
344 * and release the cache memory.
346 void
347 nfs_invalidate_dircache_sb(struct super_block *sb)
349 struct nfs_dirent *cache = dircache;
350 int i;
352 for (i = NFS_MAX_DIRCACHE; i--; cache++) {
353 if (sb && sb->s_dev != cache->dev)
354 continue;
355 if (cache->locked) {
356 printk("NFS: cache locked at umount %s\n",
357 (cache->entry ? "(lost a page!)" : ""));
358 continue;
360 cache->valid = 0; /* brute force */
361 if (cache->entry) {
362 free_page((unsigned long) cache->entry);
363 cache->entry = NULL;
369 * Free directory cache memory
370 * Called from cleanup_module
372 void
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
382 * lookup.
384 * NOTE! The hit can be a negative hit too, don't assume
385 * we have an inode!
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)) {
398 #ifdef NFS_PARANOIA
399 printk("nfs_lookup_validate: %s/%s has dud inode\n",
400 dentry->d_parent->d_name.name, dentry->d_name.name);
401 #endif
402 goto bad;
404 if (S_ISDIR(dentry->d_inode->i_mode))
405 max = NFS_MAX_AGE;
408 return (time < max) || IS_ROOT(dentry);
409 bad:
410 return 0;
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) {
421 int error;
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);
427 #endif
428 /* Unhash it first */
429 d_drop(dentry);
430 error = nfs_safe_remove(dentry);
431 if (error)
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 ...
439 else {
440 unsigned long age = jiffies - dentry->d_time;
441 if (age > NFS_MAX_AGE)
442 d_drop(dentry);
445 #ifdef NFS_PARANOIA
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);
459 #endif
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)) {
469 #ifdef NFS_PARANOIA
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);
472 #endif
473 while (nfs_find_dentry_request(inode, dentry)) {
474 #ifdef NFS_PARANOIA
475 printk("nfs_dentry_iput: flushing %s/%s\n",
476 dentry->d_parent->d_name.name, dentry->d_name.name);
477 #endif
478 nfs_flush_dirty_pages(inode, 0, 0, 0);
481 iput(inode);
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 *) */
495 NULL, /* d_hash */
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 *) */
502 #ifdef NFS_PARANOIA
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,
520 unhashed);
523 #endif
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)
531 for (;;) {
532 dentry->d_time = jiffies;
533 if (dentry == dentry->d_parent)
534 break;
535 dentry = dentry->d_parent;
539 static int nfs_lookup(struct inode *dir, struct dentry * dentry)
541 struct inode *inode;
542 int error;
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");
551 return -ENOENT;
554 error = -ENAMETOOLONG;
555 if (dentry->d_name.len > NFS_MAXNAMLEN)
556 goto out;
558 error = -ENOMEM;
559 if (!dentry->d_fsdata) {
560 dentry->d_fsdata = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
561 if (!dentry->d_fsdata)
562 goto out;
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);
568 inode = NULL;
569 if (error == -ENOENT)
570 goto no_entry;
571 if (!error) {
572 error = -EACCES;
573 inode = nfs_fhget(dentry, &fhandle, &fattr);
574 if (inode) {
575 #ifdef NFS_PARANOIA
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);
582 #endif
583 no_entry:
584 d_add(dentry, inode);
585 nfs_renew_times(dentry);
586 error = 0;
589 #ifdef NFS_PARANOIA
590 if (error)
591 printk("nfs_lookup: %s/%s failed, error=%d\n",
592 dentry->d_parent->d_name.name, dentry->d_name.name, error);
593 #endif
594 out:
595 return 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)
604 struct inode *inode;
605 int error = -EACCES;
607 inode = nfs_fhget(dentry, fhandle, fattr);
608 if (inode) {
609 #ifdef NFS_PARANOIA
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);
616 #endif
617 d_instantiate(dentry, inode);
618 nfs_renew_times(dentry);
619 error = 0;
621 return error;
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)
632 int error;
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");
642 return -ENOENT;
645 error = -ENAMETOOLONG;
646 if (dentry->d_name.len > NFS_MAXNAMLEN)
647 goto out;
649 sattr.mode = mode;
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);
659 if (!error)
660 error = nfs_instantiate(dentry, &fhandle, &fattr);
661 if (error)
662 d_drop(dentry);
663 out:
664 return error;
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)
672 int error;
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");
682 return -ENOENT;
685 if (dentry->d_name.len > NFS_MAXNAMLEN)
686 return -ENAMETOOLONG;
688 sattr.mode = mode;
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);
697 if (!error)
698 error = nfs_instantiate(dentry, &fhandle, &fattr);
699 if (error)
700 d_drop(dentry);
701 return error;
705 * See comments for nfs_proc_create regarding failed operations.
707 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
709 int error;
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");
719 return -ENOENT;
722 error = -ENAMETOOLONG;
723 if (dentry->d_name.len > NFS_MAXNAMLEN)
724 goto out;
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.
736 d_drop(dentry);
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);
740 out:
741 return error;
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
747 * multiple users.
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");
761 return -ENOENT;
764 error = -ENAMETOOLONG;
765 if (dentry->d_name.len > NFS_MAXNAMLEN)
766 goto out;
768 error = -EBUSY;
769 if (dentry->d_count > 1) {
770 /* Attempt to shrink child dentries ... */
771 shrink_dcache_parent(dentry);
772 if (dentry->d_count > 1)
773 goto out;
775 #ifdef NFS_PARANOIA
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);
780 #endif
782 * Unhash the dentry while we remove the directory.
784 if (!list_empty(&dentry->d_hash)) {
785 d_drop(dentry);
786 rehash = 1;
789 * Update i_nlink and free the inode before unlinking.
791 if (dentry->d_inode->i_nlink)
792 dentry->d_inode->i_nlink --;
793 d_delete(dentry);
794 nfs_invalidate_dircache(dir);
795 error = nfs_proc_rmdir(NFS_SERVER(dir), NFS_FH(dentry->d_parent),
796 dentry->d_name.name);
797 if (!error) {
798 if (rehash)
799 d_add(dentry, NULL);
800 nfs_renew_times(dentry);
802 out:
803 return error;
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
815 * == 1. And trouble.
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.
821 * BUT:
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.
825 * WHICH MEANS:
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
829 * a 32 bit machine.
830 * QUINTESSENCE
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
834 * to succeed.
837 static
838 struct dentry *nfs_silly_lookup(struct dentry *parent, char *silly, int slen)
840 struct qstr sqstr;
841 struct dentry *sdentry;
842 int error;
844 sqstr.name = silly;
845 sqstr.len = slen;
846 sqstr.hash = full_name_hash(silly, slen);
847 sdentry = d_lookup(parent, &sqstr);
848 if (!sdentry) {
849 sdentry = d_alloc(parent, &sqstr);
850 if (sdentry == NULL)
851 return ERR_PTR(-ENOMEM);
852 error = nfs_lookup(parent->d_inode, sdentry);
853 if (error) {
854 dput(sdentry);
855 return ERR_PTR(error);
858 return sdentry;
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;
867 char silly[slen+1];
868 struct dentry *sdentry;
869 int error = -EIO;
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. */
880 #ifdef NFS_PARANOIA
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);
884 #endif
886 * We don't allow a dentry to be silly-renamed twice.
888 error = -EBUSY;
889 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
890 goto out;
892 sprintf(silly, ".nfs%*.*lx",
893 i_inosize, i_inosize, dentry->d_inode->i_ino);
895 sdentry = NULL;
896 do {
897 char *suffix = silly + slen - countersize;
899 dput(sdentry);
900 sillycounter++;
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.
911 if (IS_ERR(sdentry))
912 goto out;
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);
919 if (!error) {
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 */
925 dput(sdentry);
926 out:
927 return error;
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? */
944 error = -EBUSY;
945 if (inode) {
946 if (NFS_WRITEBACK(inode)) {
947 nfs_flush_dirty_pages(inode, 0, 0, 0);
948 if (NFS_WRITEBACK(inode)) {
949 #ifdef NFS_PARANOIA
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);
952 #endif
953 goto out;
956 } else {
957 #ifdef NFS_PARANOIA
958 printk("nfs_safe_remove: %s/%s already negative??\n",
959 dentry->d_parent->d_name.name, dentry->d_name.name);
960 #endif
963 if (dentry->d_count > 1) {
964 #ifdef NFS_PARANOIA
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);
967 #endif
968 goto out;
970 #ifdef NFS_PARANOIA
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);
975 #endif
977 * Unhash the dentry while we remove the file ...
979 if (!list_empty(&dentry->d_hash)) {
980 d_drop(dentry);
981 rehash = 1;
984 * Update i_nlink and free the inode before unlinking.
986 if (inode) {
987 if (inode->i_nlink)
988 inode->i_nlink --;
989 d_delete(dentry);
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)
998 d_add(dentry, NULL);
999 out:
1000 return error;
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)
1010 int error;
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");
1017 return -ENOENT;
1020 error = -ENAMETOOLONG;
1021 if (dentry->d_name.len > NFS_MAXNAMLEN)
1022 goto out;
1024 error = nfs_sillyrename(dir, dentry);
1025 if (error && error != -EBUSY) {
1026 error = nfs_safe_remove(dentry);
1027 if (!error) {
1028 nfs_renew_times(dentry);
1031 out:
1032 return error;
1035 static int
1036 nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1038 struct nfs_sattr sattr;
1039 int error;
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");
1046 return -ENOENT;
1049 error = -ENAMETOOLONG;
1050 if (dentry->d_name.len > NFS_MAXNAMLEN)
1051 goto out;
1053 if (strlen(symname) > NFS_MAXPATHLEN)
1054 goto out;
1056 #ifdef NFS_PARANOIA
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);
1060 #endif
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.
1074 d_drop(dentry);
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);
1078 if (!error) {
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);
1085 out:
1086 return error;
1089 static int
1090 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1092 struct inode *inode = old_dentry->d_inode;
1093 int error;
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");
1101 return -ENOENT;
1104 error = -ENAMETOOLONG;
1105 if (dentry->d_name.len > NFS_MAXNAMLEN)
1106 goto out;
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.
1113 d_drop(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);
1117 if (!error) {
1119 * Update the link count immediately, as some apps
1120 * (e.g. pine) test this after making a link.
1122 inode->i_nlink++;
1124 out:
1125 return error;
1129 * RENAME
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.
1137 * FIXED.
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
1150 * the rename.
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);
1163 #endif
1164 if (!old_dir || !S_ISDIR(old_dir->i_mode)) {
1165 printk("nfs_rename: old inode is NULL or not a directory\n");
1166 return -ENOENT;
1169 if (!new_dir || !S_ISDIR(new_dir->i_mode)) {
1170 printk("nfs_rename: new inode is NULL or not a directory\n");
1171 return -ENOENT;
1174 error = -ENAMETOOLONG;
1175 if (old_dentry->d_name.len > NFS_MAXNAMLEN ||
1176 new_dentry->d_name.len > NFS_MAXNAMLEN)
1177 goto out;
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);
1185 error = -EBUSY;
1186 if (new_dentry->d_count > 1) {
1187 #ifdef NFS_PARANOIA
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);
1190 #endif
1191 goto out;
1195 * Check for within-directory rename ... no complications.
1197 if (new_dir == old_dir)
1198 goto do_rename;
1200 * Cross-directory move ... check whether it's a file.
1202 if (S_ISREG(old_inode->i_mode)) {
1203 if (NFS_WRITEBACK(old_inode)) {
1204 #ifdef NFS_PARANOIA
1205 printk("nfs_rename: %s/%s has pending writes\n",
1206 old_dentry->d_parent->d_name.name, old_dentry->d_name.name);
1207 #endif
1208 nfs_flush_dirty_pages(old_inode, 0, 0, 0);
1209 if (NFS_WRITEBACK(old_inode)) {
1210 #ifdef NFS_PARANOIA
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);
1213 #endif
1214 goto out;
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) {
1229 #ifdef NFS_PARANOIA
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);
1232 #endif
1233 goto out;
1235 if (new_dentry->d_count > 1) {
1236 #ifdef NFS_PARANOIA
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);
1239 #endif
1240 goto out;
1243 d_drop(old_dentry);
1244 update = 0;
1246 do_rename:
1248 * To prevent any new references to the target during the rename,
1249 * we unhash the dentry and free the inode in advance.
1251 #ifdef NFS_PARANOIA
1252 if (new_inode &&
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);
1257 #endif
1258 if (!list_empty(&new_dentry->d_hash)) {
1259 d_drop(new_dentry);
1260 rehash = update;
1262 if (new_inode) {
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);
1271 if (!error) {
1272 /* Update the dcache if needed */
1273 if (rehash)
1274 d_add(new_dentry, NULL);
1275 if (update)
1276 d_move(old_dentry, new_dentry);
1278 out:
1279 return error;
1283 * Local variables:
1284 * version-control: t
1285 * kept-new-versions: 5
1286 * End: