- pre5:
[davej-history.git] / fs / nfs / dir.c
blobe370587be412c0f8b25b41710a4f013ef86edd53
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).
17 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/malloc.h>
27 #include <linux/mm.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/pagemap.h>
32 #include <linux/smp_lock.h>
34 #define NFS_PARANOIA 1
35 /* #define NFS_DEBUG_VERBOSE 1 */
37 static int nfs_readdir(struct file *, void *, filldir_t);
38 static struct dentry *nfs_lookup(struct inode *, struct dentry *);
39 static int nfs_create(struct inode *, struct dentry *, int);
40 static int nfs_mkdir(struct inode *, struct dentry *, int);
41 static int nfs_rmdir(struct inode *, struct dentry *);
42 static int nfs_unlink(struct inode *, struct dentry *);
43 static int nfs_symlink(struct inode *, struct dentry *, const char *);
44 static int nfs_link(struct dentry *, struct inode *, struct dentry *);
45 static int nfs_mknod(struct inode *, struct dentry *, int, int);
46 static int nfs_rename(struct inode *, struct dentry *,
47 struct inode *, struct dentry *);
49 struct file_operations nfs_dir_operations = {
50 read: generic_read_dir,
51 readdir: nfs_readdir,
52 open: nfs_open,
53 release: nfs_release,
56 struct inode_operations nfs_dir_inode_operations = {
57 create: nfs_create,
58 lookup: nfs_lookup,
59 link: nfs_link,
60 unlink: nfs_unlink,
61 symlink: nfs_symlink,
62 mkdir: nfs_mkdir,
63 rmdir: nfs_rmdir,
64 mknod: nfs_mknod,
65 rename: nfs_rename,
66 revalidate: nfs_revalidate,
67 setattr: nfs_notify_change,
70 typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
71 typedef struct {
72 struct file *file;
73 struct page *page;
74 unsigned long page_index;
75 unsigned page_offset;
76 u64 target;
77 struct nfs_entry *entry;
78 decode_dirent_t decode;
79 int plus;
80 int error;
81 } nfs_readdir_descriptor_t;
83 /* Now we cache directories properly, by stuffing the dirent
84 * data directly in the page cache.
86 * Inode invalidation due to refresh etc. takes care of
87 * _everything_, no sloppy entry flushing logic, no extraneous
88 * copying, network direct to page cache, the way it was meant
89 * to be.
91 * NOTE: Dirent information verification is done always by the
92 * page-in of the RPC reply, nowhere else, this simplies
93 * things substantially.
95 static
96 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
98 struct file *file = desc->file;
99 struct inode *inode = file->f_dentry->d_inode;
100 void *buffer = (void *)kmap(page);
101 int plus = NFS_USE_READDIRPLUS(inode);
102 int error;
104 dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
106 again:
107 error = NFS_PROTO(inode)->readdir(file, desc->entry->cookie, buffer,
108 NFS_SERVER(inode)->dtsize, plus);
109 /* We requested READDIRPLUS, but the server doesn't grok it */
110 if (desc->plus && error == -ENOTSUPP) {
111 NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
112 plus = 0;
113 goto again;
115 if (error < 0)
116 goto error;
117 SetPageUptodate(page);
118 kunmap(page);
119 /* Ensure consistent page alignment of the data.
120 * Note: assumes we have exclusive access to this mapping either
121 * throught inode->i_sem or some other mechanism.
123 if (page->index == 0)
124 invalidate_inode_pages(inode);
125 UnlockPage(page);
126 return 0;
127 error:
128 SetPageError(page);
129 kunmap(page);
130 UnlockPage(page);
131 invalidate_inode_pages(inode);
132 desc->error = error;
133 return -EIO;
137 * Given a pointer to a buffer that has already been filled by a call
138 * to readdir, find the next entry.
140 * If the end of the buffer has been reached, return -EAGAIN, if not,
141 * return the offset within the buffer of the next entry to be
142 * read.
144 static inline
145 int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
147 struct nfs_entry *entry = desc->entry;
148 char *start = (char *)kmap(page),
149 *p = start;
150 int loop_count = 0,
151 status = 0;
153 for(;;) {
154 p = (char *)desc->decode((u32*)p, entry, desc->plus);
155 if (IS_ERR(p)) {
156 status = PTR_ERR(p);
157 break;
159 desc->page_offset = p - start;
160 dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
161 if (entry->prev_cookie == desc->target)
162 break;
163 if (loop_count++ > 200) {
164 loop_count = 0;
165 schedule();
168 kunmap(page);
169 dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
170 return status;
174 * Find the given page, and call find_dirent() in order to try to
175 * return the next entry.
177 static inline
178 int find_dirent_page(nfs_readdir_descriptor_t *desc)
180 struct inode *inode = desc->file->f_dentry->d_inode;
181 struct page *page;
182 unsigned long index = desc->page_index;
183 int status;
185 dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
187 if (desc->page) {
188 page_cache_release(desc->page);
189 desc->page = NULL;
192 page = read_cache_page(&inode->i_data, index,
193 (filler_t *)nfs_readdir_filler, desc);
194 if (IS_ERR(page)) {
195 status = PTR_ERR(page);
196 goto out;
198 if (!Page_Uptodate(page))
199 goto read_error;
201 /* NOTE: Someone else may have changed the READDIRPLUS flag */
202 desc->plus = NFS_USE_READDIRPLUS(inode);
203 status = find_dirent(desc, page);
204 if (status >= 0)
205 desc->page = page;
206 else
207 page_cache_release(page);
208 out:
209 dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
210 return status;
211 read_error:
212 page_cache_release(page);
213 return -EIO;
217 * Recurse through the page cache pages, and return a
218 * filled nfs_entry structure of the next directory entry if possible.
220 * The target for the search is 'desc->target'.
222 static inline
223 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
225 int res = 0;
226 int loop_count = 0;
228 dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
229 for (;;) {
230 res = find_dirent_page(desc);
231 if (res != -EAGAIN)
232 break;
233 /* Align to beginning of next page */
234 desc->page_offset = 0;
235 desc->page_index ++;
236 if (loop_count++ > 200) {
237 loop_count = 0;
238 schedule();
241 dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
242 return res;
246 * Once we've found the start of the dirent within a page: fill 'er up...
248 static
249 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
250 filldir_t filldir)
252 struct file *file = desc->file;
253 struct nfs_entry *entry = desc->entry;
254 char *start = (char *)kmap(desc->page),
255 *p = start + desc->page_offset;
256 unsigned long fileid;
257 int loop_count = 0,
258 res = 0;
260 dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
262 for(;;) {
263 /* Note: entry->prev_cookie contains the cookie for
264 * retrieving the current dirent on the server */
265 fileid = nfs_fileid_to_ino_t(entry->ino);
266 res = filldir(dirent, entry->name, entry->len,
267 entry->prev_cookie, fileid, DT_UNKNOWN);
268 if (res < 0)
269 break;
270 file->f_pos = desc->target = entry->cookie;
271 p = (char *)desc->decode((u32 *)p, entry, desc->plus);
272 if (IS_ERR(p)) {
273 if (PTR_ERR(p) == -EAGAIN) {
274 desc->page_offset = 0;
275 desc->page_index ++;
277 break;
279 desc->page_offset = p - start;
280 if (loop_count++ > 200) {
281 loop_count = 0;
282 schedule();
285 kunmap(desc->page);
286 page_cache_release(desc->page);
287 desc->page = NULL;
289 dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
290 return res;
294 * If we cannot find a cookie in our cache, we suspect that this is
295 * because it points to a deleted file, so we ask the server to return
296 * whatever it thinks is the next entry. We then feed this to filldir.
297 * If all goes well, we should then be able to find our way round the
298 * cache on the next call to readdir_search_pagecache();
300 * NOTE: we cannot add the anonymous page to the pagecache because
301 * the data it contains might not be page aligned. Besides,
302 * we should already have a complete representation of the
303 * directory in the page cache by the time we get here.
305 static inline
306 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
307 filldir_t filldir)
309 struct file *file = desc->file;
310 struct inode *inode = file->f_dentry->d_inode;
311 struct page *page = NULL;
312 u32 *p;
313 int status = -EIO;
315 dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
316 if (desc->page) {
317 page_cache_release(desc->page);
318 desc->page = NULL;
321 page = page_cache_alloc();
322 if (!page) {
323 status = -ENOMEM;
324 goto out;
326 p = (u32 *)kmap(page);
327 status = NFS_PROTO(inode)->readdir(file, desc->target, p,
328 NFS_SERVER(inode)->dtsize, 0);
329 if (status >= 0) {
330 p = desc->decode(p, desc->entry, 0);
331 if (IS_ERR(p))
332 status = PTR_ERR(p);
333 else
334 desc->entry->prev_cookie = desc->target;
336 kunmap(page);
337 if (status < 0)
338 goto out_release;
340 desc->page_index = 0;
341 desc->page_offset = 0;
342 desc->page = page;
343 status = nfs_do_filldir(desc, dirent, filldir);
345 /* Reset read descriptor so it searches the page cache from
346 * the start upon the next call to readdir_search_pagecache() */
347 desc->page_index = 0;
348 desc->page_offset = 0;
349 memset(desc->entry, 0, sizeof(*desc->entry));
350 out:
351 dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
352 return status;
353 out_release:
354 page_cache_release(page);
355 goto out;
358 /* The file offset position is now represented as a true offset into the
359 * page cache as is the case in most of the other filesystems.
361 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
363 struct dentry *dentry = filp->f_dentry;
364 struct inode *inode = dentry->d_inode;
365 nfs_readdir_descriptor_t my_desc,
366 *desc = &my_desc;
367 struct nfs_entry my_entry;
368 long res;
370 res = nfs_revalidate(dentry);
371 if (res < 0)
372 return res;
375 * filp->f_pos points to the file offset in the page cache.
376 * but if the cache has meanwhile been zapped, we need to
377 * read from the last dirent to revalidate f_pos
378 * itself.
380 memset(desc, 0, sizeof(*desc));
381 memset(&my_entry, 0, sizeof(my_entry));
383 desc->file = filp;
384 desc->target = filp->f_pos;
385 desc->entry = &my_entry;
386 desc->decode = NFS_PROTO(inode)->decode_dirent;
388 while(!desc->entry->eof) {
389 res = readdir_search_pagecache(desc);
390 if (res == -EBADCOOKIE) {
391 /* This means either end of directory */
392 if (desc->entry->cookie == desc->target) {
393 res = 0;
394 break;
396 /* Or that the server has 'lost' a cookie */
397 res = uncached_readdir(desc, dirent, filldir);
398 if (res >= 0)
399 continue;
401 if (res < 0)
402 break;
404 res = nfs_do_filldir(desc, dirent, filldir);
405 if (res < 0) {
406 res = 0;
407 break;
410 if (desc->page)
411 page_cache_release(desc->page);
412 if (desc->error < 0)
413 return desc->error;
414 if (res < 0)
415 return res;
416 return 0;
420 * Whenever an NFS operation succeeds, we know that the dentry
421 * is valid, so we update the revalidation timestamp.
423 static inline void nfs_renew_times(struct dentry * dentry)
425 dentry->d_time = jiffies;
428 static inline int nfs_dentry_force_reval(struct dentry *dentry, int flags)
430 struct inode *inode = dentry->d_inode;
431 unsigned long timeout = NFS_ATTRTIMEO(inode);
434 * If it's the last lookup in a series, we use a stricter
435 * cache consistency check by looking at the parent mtime.
437 * If it's been modified in the last hour, be really strict.
438 * (This still means that we can avoid doing unnecessary
439 * work on directories like /usr/share/bin etc which basically
440 * never change).
442 if (!(flags & LOOKUP_CONTINUE)) {
443 long diff = CURRENT_TIME - dentry->d_parent->d_inode->i_mtime;
445 if (diff < 15*60)
446 timeout = 0;
449 return time_after(jiffies,dentry->d_time + timeout);
453 * We judge how long we want to trust negative
454 * dentries by looking at the parent inode mtime.
456 * If mtime is close to present time, we revalidate
457 * more often.
459 #define NFS_REVALIDATE_NEGATIVE (1 * HZ)
460 static inline int nfs_neg_need_reval(struct dentry *dentry)
462 struct inode *dir = dentry->d_parent->d_inode;
463 unsigned long timeout = NFS_ATTRTIMEO(dir);
464 long diff = CURRENT_TIME - dir->i_mtime;
466 if (diff < 5*60 && timeout > NFS_REVALIDATE_NEGATIVE)
467 timeout = NFS_REVALIDATE_NEGATIVE;
469 return time_after(jiffies, dentry->d_time + timeout);
473 * This is called every time the dcache has a lookup hit,
474 * and we should check whether we can really trust that
475 * lookup.
477 * NOTE! The hit can be a negative hit too, don't assume
478 * we have an inode!
480 * If the dentry is older than the revalidation interval,
481 * we do a new lookup and verify that the dentry is still
482 * correct.
484 static int nfs_lookup_revalidate(struct dentry * dentry, int flags)
486 struct dentry *dir = dentry->d_parent;
487 struct inode *dir_i = dir->d_inode;
488 struct inode * inode = dentry->d_inode;
489 int error;
490 struct nfs_fh fhandle;
491 struct nfs_fattr fattr;
493 lock_kernel();
494 dir = dentry->d_parent;
495 dir_i = dir->d_inode;
497 * If we don't have an inode, let's look at the parent
498 * directory mtime to get a hint about how often we
499 * should validate things..
501 if (!inode) {
502 if (nfs_neg_need_reval(dentry))
503 goto out_bad;
504 goto out_valid;
507 if (is_bad_inode(inode)) {
508 dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
509 dir->d_name.name, dentry->d_name.name);
510 goto out_bad;
513 if (!nfs_dentry_force_reval(dentry, flags))
514 goto out_valid;
516 if (IS_ROOT(dentry)) {
517 __nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
518 goto out_valid_renew;
522 * Do a new lookup and check the dentry attributes.
524 error = NFS_PROTO(dir_i)->lookup(dir, &dentry->d_name, &fhandle,
525 &fattr);
526 if (error)
527 goto out_bad;
529 /* Inode number matches? */
530 if (!(fattr.valid & NFS_ATTR_FATTR) ||
531 NFS_FSID(inode) != fattr.fsid ||
532 NFS_FILEID(inode) != fattr.fileid)
533 goto out_bad;
535 /* Filehandle matches? */
536 if (memcmp(dentry->d_fsdata, &fhandle, sizeof(struct nfs_fh)))
537 goto out_bad;
539 /* Ok, remeber that we successfully checked it.. */
540 nfs_refresh_inode(inode, &fattr);
542 out_valid_renew:
543 nfs_renew_times(dentry);
544 out_valid:
545 unlock_kernel();
546 return 1;
547 out_bad:
548 shrink_dcache_parent(dentry);
549 /* If we have submounts, don't unhash ! */
550 if (have_submounts(dentry))
551 goto out_valid;
552 d_drop(dentry);
553 /* Purge readdir caches. */
554 nfs_zap_caches(dir_i);
555 if (inode && S_ISDIR(inode->i_mode))
556 nfs_zap_caches(inode);
557 unlock_kernel();
558 return 0;
562 * This is called from dput() when d_count is going to 0.
564 static int nfs_dentry_delete(struct dentry *dentry)
566 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
567 dentry->d_parent->d_name.name, dentry->d_name.name,
568 dentry->d_flags);
570 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
571 /* Unhash it, so that ->d_iput() would be called */
572 return 1;
574 return 0;
578 static kmem_cache_t *nfs_fh_cachep;
580 __inline__ struct nfs_fh *nfs_fh_alloc(void)
582 return kmem_cache_alloc(nfs_fh_cachep, SLAB_KERNEL);
585 __inline__ void nfs_fh_free(struct nfs_fh *p)
587 kmem_cache_free(nfs_fh_cachep, p);
591 * Called when the dentry is being freed to release private memory.
593 static void nfs_dentry_release(struct dentry *dentry)
595 if (dentry->d_fsdata) {
596 lock_kernel();
597 nfs_fh_free(dentry->d_fsdata);
598 unlock_kernel();
603 * Called when the dentry loses inode.
604 * We use it to clean up silly-renamed files.
606 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
608 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
609 lock_kernel();
610 nfs_complete_unlink(dentry);
611 unlock_kernel();
613 iput(inode);
616 struct dentry_operations nfs_dentry_operations = {
617 d_revalidate: nfs_lookup_revalidate,
618 d_delete: nfs_dentry_delete,
619 d_release: nfs_dentry_release,
620 d_iput: nfs_dentry_iput,
623 static struct dentry *nfs_lookup(struct inode *dir_i, struct dentry * dentry)
625 struct dentry *dir = dentry->d_parent;
626 struct inode *inode;
627 int error;
628 struct nfs_fh fhandle;
629 struct nfs_fattr fattr;
631 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
632 dir->d_name.name, dentry->d_name.name);
634 error = -ENAMETOOLONG;
635 if (dentry->d_name.len > NFS_SERVER(dir_i)->namelen)
636 goto out;
638 error = -ENOMEM;
639 if (!dentry->d_fsdata) {
640 dentry->d_fsdata = nfs_fh_alloc();
641 if (!dentry->d_fsdata)
642 goto out;
644 dentry->d_op = &nfs_dentry_operations;
646 error = NFS_PROTO(dir_i)->lookup(dir, &dentry->d_name, &fhandle,
647 &fattr);
648 inode = NULL;
649 if (error == -ENOENT)
650 goto no_entry;
651 if (!error) {
652 error = -EACCES;
653 inode = nfs_fhget(dentry, &fhandle, &fattr);
654 if (inode) {
655 no_entry:
656 d_add(dentry, inode);
657 nfs_renew_times(dentry);
658 error = 0;
661 out:
662 return ERR_PTR(error);
666 * Code common to create, mkdir, and mknod.
668 static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
669 struct nfs_fattr *fattr)
671 struct inode *inode;
672 int error = -EACCES;
674 inode = nfs_fhget(dentry, fhandle, fattr);
675 if (inode) {
676 d_instantiate(dentry, inode);
677 nfs_renew_times(dentry);
678 error = 0;
680 return error;
684 * Following a failed create operation, we drop the dentry rather
685 * than retain a negative dentry. This avoids a problem in the event
686 * that the operation succeeded on the server, but an error in the
687 * reply path made it appear to have failed.
689 static int nfs_create(struct inode *dir_i, struct dentry *dentry, int mode)
691 struct dentry *dir = dentry->d_parent;
692 struct iattr attr;
693 struct nfs_fattr fattr;
694 struct nfs_fh fhandle;
695 int error;
697 dfprintk(VFS, "NFS: create(%x/%ld, %s\n",
698 dir_i->i_dev, dir_i->i_ino, dentry->d_name.name);
700 attr.ia_mode = mode;
701 attr.ia_valid = ATTR_MODE;
704 * The 0 argument passed into the create function should one day
705 * contain the O_EXCL flag if requested. This allows NFSv3 to
706 * select the appropriate create strategy. Currently open_namei
707 * does not pass the create flags.
709 nfs_zap_caches(dir_i);
710 error = NFS_PROTO(dir_i)->create(dir, &dentry->d_name,
711 &attr, 0, &fhandle, &fattr);
712 if (!error && fhandle.size != 0)
713 error = nfs_instantiate(dentry, &fhandle, &fattr);
714 if (error || fhandle.size == 0)
715 d_drop(dentry);
716 return error;
720 * See comments for nfs_proc_create regarding failed operations.
722 static int nfs_mknod(struct inode *dir_i, struct dentry *dentry, int mode, int rdev)
724 struct dentry *dir = dentry->d_parent;
725 struct iattr attr;
726 struct nfs_fattr fattr;
727 struct nfs_fh fhandle;
728 int error;
730 dfprintk(VFS, "NFS: mknod(%x/%ld, %s\n",
731 dir_i->i_dev, dir_i->i_ino, dentry->d_name.name);
733 attr.ia_mode = mode;
734 attr.ia_valid = ATTR_MODE;
736 nfs_zap_caches(dir_i);
737 error = NFS_PROTO(dir_i)->mknod(dir, &dentry->d_name, &attr, rdev,
738 &fhandle, &fattr);
739 if (!error && fhandle.size != 0)
740 error = nfs_instantiate(dentry, &fhandle, &fattr);
741 if (error || fhandle.size == 0)
742 d_drop(dentry);
743 return error;
747 * See comments for nfs_proc_create regarding failed operations.
749 static int nfs_mkdir(struct inode *dir_i, struct dentry *dentry, int mode)
751 struct dentry *dir = dentry->d_parent;
752 struct iattr attr;
753 struct nfs_fattr fattr;
754 struct nfs_fh fhandle;
755 int error;
757 dfprintk(VFS, "NFS: mkdir(%x/%ld, %s\n",
758 dir_i->i_dev, dir_i->i_ino, dentry->d_name.name);
760 attr.ia_valid = ATTR_MODE;
761 attr.ia_mode = mode | S_IFDIR;
763 #if 0
765 * Always drop the dentry, we can't always depend on
766 * the fattr returned by the server (AIX seems to be
767 * broken). We're better off doing another lookup than
768 * depending on potentially bogus information.
770 d_drop(dentry);
771 #endif
772 nfs_zap_caches(dir_i);
773 error = NFS_PROTO(dir_i)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
774 &fattr);
775 if (!error && fhandle.size != 0)
776 error = nfs_instantiate(dentry, &fhandle, &fattr);
777 if (error || fhandle.size == 0)
778 d_drop(dentry);
779 return error;
782 static int nfs_rmdir(struct inode *dir_i, struct dentry *dentry)
784 struct dentry *dir = dentry->d_parent;
785 int error;
787 dfprintk(VFS, "NFS: rmdir(%x/%ld, %s\n",
788 dir_i->i_dev, dir_i->i_ino, dentry->d_name.name);
790 nfs_zap_caches(dir_i);
791 error = NFS_PROTO(dir_i)->rmdir(dir, &dentry->d_name);
793 return error;
796 static int nfs_sillyrename(struct inode *dir_i, struct dentry *dentry)
798 struct dentry *dir = dentry->d_parent;
799 static unsigned int sillycounter = 0;
800 const int i_inosize = sizeof(dir_i->i_ino)*2;
801 const int countersize = sizeof(sillycounter)*2;
802 const int slen = strlen(".nfs") + i_inosize + countersize;
803 char silly[slen+1];
804 struct qstr qsilly;
805 struct dentry *sdentry;
806 int error = -EIO;
808 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
809 dentry->d_parent->d_name.name, dentry->d_name.name,
810 atomic_read(&dentry->d_count));
813 * Note that a silly-renamed file can be deleted once it's
814 * no longer in use -- it's just an ordinary file now.
816 if (atomic_read(&dentry->d_count) == 1) {
817 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
818 goto out; /* No need to silly rename. */
821 #ifdef NFS_PARANOIA
822 if (!dentry->d_inode)
823 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
824 dentry->d_parent->d_name.name, dentry->d_name.name);
825 #endif
827 * We don't allow a dentry to be silly-renamed twice.
829 error = -EBUSY;
830 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
831 goto out;
833 sprintf(silly, ".nfs%*.*lx",
834 i_inosize, i_inosize, dentry->d_inode->i_ino);
836 sdentry = NULL;
837 do {
838 char *suffix = silly + slen - countersize;
840 dput(sdentry);
841 sillycounter++;
842 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
844 dfprintk(VFS, "trying to rename %s to %s\n",
845 dentry->d_name.name, silly);
847 sdentry = lookup_one(silly, dentry->d_parent);
849 * N.B. Better to return EBUSY here ... it could be
850 * dangerous to delete the file while it's in use.
852 if (IS_ERR(sdentry))
853 goto out;
854 } while(sdentry->d_inode != NULL); /* need negative lookup */
856 nfs_zap_caches(dir_i);
857 qsilly.name = silly;
858 qsilly.len = strlen(silly);
859 error = NFS_PROTO(dir_i)->rename(dir, &dentry->d_name, dir, &qsilly);
860 if (!error) {
861 nfs_renew_times(dentry);
862 d_move(dentry, sdentry);
863 error = nfs_async_unlink(dentry);
864 /* If we return 0 we don't unlink */
866 dput(sdentry);
867 out:
868 return error;
872 * Remove a file after making sure there are no pending writes,
873 * and after checking that the file has only one user.
875 * We invalidate the attribute cache and free the inode prior to the operation
876 * to avoid possible races if the server reuses the inode.
878 static int nfs_safe_remove(struct dentry *dentry)
880 struct dentry *dir = dentry->d_parent;
881 struct inode *dir_i = dir->d_inode;
882 struct inode *inode = dentry->d_inode;
883 int error = -EBUSY, rehash = 0;
885 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
886 dentry->d_parent->d_name.name, dentry->d_name.name);
889 * Unhash the dentry while we remove the file ...
891 if (!d_unhashed(dentry)) {
892 d_drop(dentry);
893 rehash = 1;
895 if (atomic_read(&dentry->d_count) > 1) {
896 #ifdef NFS_PARANOIA
897 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
898 dentry->d_parent->d_name.name, dentry->d_name.name,
899 atomic_read(&dentry->d_count));
900 #endif
901 goto out;
903 nfs_zap_caches(dir_i);
904 if (inode)
905 NFS_CACHEINV(inode);
906 error = NFS_PROTO(dir_i)->remove(dir, &dentry->d_name);
907 if (error < 0)
908 goto out;
910 * Free the inode
912 d_delete(dentry);
913 out:
914 if (rehash)
915 d_rehash(dentry);
916 return error;
919 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
920 * belongs to an active ".nfs..." file and we return -EBUSY.
922 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
924 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
926 int error;
928 dfprintk(VFS, "NFS: unlink(%x/%ld, %s)\n",
929 dir->i_dev, dir->i_ino, dentry->d_name.name);
931 error = nfs_sillyrename(dir, dentry);
932 if (error && error != -EBUSY) {
933 error = nfs_safe_remove(dentry);
934 if (!error) {
935 nfs_renew_times(dentry);
938 return error;
941 static int
942 nfs_symlink(struct inode *dir_i, struct dentry *dentry, const char *symname)
944 struct dentry *dir = dentry->d_parent;
945 struct iattr attr;
946 struct nfs_fattr sym_attr;
947 struct nfs_fh sym_fh;
948 struct qstr qsymname;
949 unsigned int maxlen;
950 int error;
952 dfprintk(VFS, "NFS: symlink(%x/%ld, %s, %s)\n",
953 dir_i->i_dev, dir_i->i_ino, dentry->d_name.name, symname);
955 error = -ENAMETOOLONG;
956 maxlen = (NFS_PROTO(dir_i)->version==2) ? NFS2_MAXPATHLEN : NFS3_MAXPATHLEN;
957 if (strlen(symname) > maxlen)
958 goto out;
960 #ifdef NFS_PARANOIA
961 if (dentry->d_inode)
962 printk("nfs_proc_symlink: %s/%s not negative!\n",
963 dentry->d_parent->d_name.name, dentry->d_name.name);
964 #endif
966 * Fill in the sattr for the call.
967 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
969 attr.ia_valid = ATTR_MODE;
970 attr.ia_mode = S_IFLNK | S_IRWXUGO;
972 qsymname.name = symname;
973 qsymname.len = strlen(symname);
975 nfs_zap_caches(dir_i);
976 error = NFS_PROTO(dir_i)->symlink(dir, &dentry->d_name, &qsymname,
977 &attr, &sym_fh, &sym_attr);
978 if (!error && sym_fh.size != 0 && (sym_attr.valid & NFS_ATTR_FATTR)) {
979 error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
980 } else {
981 if (error == -EEXIST)
982 printk("nfs_proc_symlink: %s/%s already exists??\n",
983 dir->d_name.name, dentry->d_name.name);
984 d_drop(dentry);
987 out:
988 return error;
991 static int
992 nfs_link(struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
994 struct dentry *dir = dentry->d_parent;
995 struct inode *inode = old_dentry->d_inode;
996 int error;
998 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
999 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1000 dentry->d_parent->d_name.name, dentry->d_name.name);
1003 * Drop the dentry in advance to force a new lookup.
1004 * Since nfs_proc_link doesn't return a file handle,
1005 * we can't use the existing dentry.
1007 d_drop(dentry);
1008 nfs_zap_caches(dir_i);
1009 NFS_CACHEINV(inode);
1010 error = NFS_PROTO(dir_i)->link(old_dentry, dir, &dentry->d_name);
1011 return error;
1015 * RENAME
1016 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1017 * different file handle for the same inode after a rename (e.g. when
1018 * moving to a different directory). A fail-safe method to do so would
1019 * be to look up old_dir/old_name, create a link to new_dir/new_name and
1020 * rename the old file using the sillyrename stuff. This way, the original
1021 * file in old_dir will go away when the last process iput()s the inode.
1023 * FIXED.
1025 * It actually works quite well. One needs to have the possibility for
1026 * at least one ".nfs..." file in each directory the file ever gets
1027 * moved or linked to which happens automagically with the new
1028 * implementation that only depends on the dcache stuff instead of
1029 * using the inode layer
1031 * Unfortunately, things are a little more complicated than indicated
1032 * above. For a cross-directory move, we want to make sure we can get
1033 * rid of the old inode after the operation. This means there must be
1034 * no pending writes (if it's a file), and the use count must be 1.
1035 * If these conditions are met, we can drop the dentries before doing
1036 * the rename.
1038 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1039 struct inode *new_dir, struct dentry *new_dentry)
1041 struct inode *old_inode = old_dentry->d_inode;
1042 struct inode *new_inode = new_dentry->d_inode;
1043 struct dentry *dentry = NULL, *rehash = NULL;
1044 int error = -EBUSY;
1047 * To prevent any new references to the target during the rename,
1048 * we unhash the dentry and free the inode in advance.
1050 if (!d_unhashed(new_dentry)) {
1051 d_drop(new_dentry);
1052 rehash = new_dentry;
1055 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1056 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1057 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1058 atomic_read(&new_dentry->d_count));
1061 * First check whether the target is busy ... we can't
1062 * safely do _any_ rename if the target is in use.
1064 * For files, make a copy of the dentry and then do a
1065 * silly-rename. If the silly-rename succeeds, the
1066 * copied dentry is hashed and becomes the new target.
1068 if (!new_inode)
1069 goto go_ahead;
1070 if (S_ISDIR(new_inode->i_mode))
1071 goto out;
1072 else if (atomic_read(&new_dentry->d_count) > 1) {
1073 int err;
1074 /* copy the target dentry's name */
1075 dentry = d_alloc(new_dentry->d_parent,
1076 &new_dentry->d_name);
1077 if (!dentry)
1078 goto out;
1080 /* silly-rename the existing target ... */
1081 err = nfs_sillyrename(new_dir, new_dentry);
1082 if (!err) {
1083 new_dentry = rehash = dentry;
1084 new_inode = NULL;
1085 /* instantiate the replacement target */
1086 d_instantiate(new_dentry, NULL);
1089 /* dentry still busy? */
1090 if (atomic_read(&new_dentry->d_count) > 1) {
1091 #ifdef NFS_PARANOIA
1092 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1093 new_dentry->d_parent->d_name.name,
1094 new_dentry->d_name.name,
1095 atomic_read(&new_dentry->d_count));
1096 #endif
1097 goto out;
1101 go_ahead:
1103 * ... prune child dentries and writebacks if needed.
1105 if (atomic_read(&old_dentry->d_count) > 1) {
1106 nfs_wb_all(old_inode);
1107 shrink_dcache_parent(old_dentry);
1110 if (new_inode)
1111 d_delete(new_dentry);
1113 nfs_zap_caches(new_dir);
1114 nfs_zap_caches(old_dir);
1115 error = NFS_PROTO(old_dir)->rename(old_dentry->d_parent,
1116 &old_dentry->d_name,
1117 new_dentry->d_parent,
1118 &new_dentry->d_name);
1119 out:
1120 if (rehash)
1121 d_rehash(rehash);
1122 if (!error && !S_ISDIR(old_inode->i_mode))
1123 d_move(old_dentry, new_dentry);
1125 /* new dentry created? */
1126 if (dentry)
1127 dput(dentry);
1128 return error;
1131 int nfs_init_fhcache(void)
1133 nfs_fh_cachep = kmem_cache_create("nfs_fh",
1134 sizeof(struct nfs_fh),
1135 0, SLAB_HWCACHE_ALIGN,
1136 NULL, NULL);
1137 if (nfs_fh_cachep == NULL)
1138 return -ENOMEM;
1140 return 0;
1143 void nfs_destroy_fhcache(void)
1145 if (kmem_cache_destroy(nfs_fh_cachep))
1146 printk(KERN_INFO "nfs_fh: not all structures were freed\n");
1150 * Local variables:
1151 * version-control: t
1152 * kept-new-versions: 5
1153 * End: