- Andries Brouwer: final isofs pieces.
[davej-history.git] / fs / nfs / dir.c
blob4821de8d346aa03285ca6b64a7ac548621bda4be
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 permission: nfs_permission,
67 revalidate: nfs_revalidate,
68 setattr: nfs_notify_change,
71 typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
72 typedef struct {
73 struct file *file;
74 struct page *page;
75 unsigned long page_index;
76 unsigned page_offset;
77 u64 target;
78 struct nfs_entry *entry;
79 decode_dirent_t decode;
80 int plus;
81 int error;
82 } nfs_readdir_descriptor_t;
84 /* Now we cache directories properly, by stuffing the dirent
85 * data directly in the page cache.
87 * Inode invalidation due to refresh etc. takes care of
88 * _everything_, no sloppy entry flushing logic, no extraneous
89 * copying, network direct to page cache, the way it was meant
90 * to be.
92 * NOTE: Dirent information verification is done always by the
93 * page-in of the RPC reply, nowhere else, this simplies
94 * things substantially.
96 static
97 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
99 struct file *file = desc->file;
100 struct inode *inode = file->f_dentry->d_inode;
101 struct rpc_cred *cred = nfs_file_cred(file);
102 void *buffer = kmap(page);
103 int plus = NFS_USE_READDIRPLUS(inode);
104 int error;
106 dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
108 again:
109 error = NFS_PROTO(inode)->readdir(inode, cred, desc->entry->cookie, buffer,
110 NFS_SERVER(inode)->dtsize, plus);
111 /* We requested READDIRPLUS, but the server doesn't grok it */
112 if (desc->plus && error == -ENOTSUPP) {
113 NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
114 plus = 0;
115 goto again;
117 if (error < 0)
118 goto error;
119 SetPageUptodate(page);
120 kunmap(page);
121 /* Ensure consistent page alignment of the data.
122 * Note: assumes we have exclusive access to this mapping either
123 * throught inode->i_sem or some other mechanism.
125 if (page->index == 0)
126 invalidate_inode_pages(inode);
127 UnlockPage(page);
128 return 0;
129 error:
130 SetPageError(page);
131 kunmap(page);
132 UnlockPage(page);
133 invalidate_inode_pages(inode);
134 desc->error = error;
135 return -EIO;
139 * Given a pointer to a buffer that has already been filled by a call
140 * to readdir, find the next entry.
142 * If the end of the buffer has been reached, return -EAGAIN, if not,
143 * return the offset within the buffer of the next entry to be
144 * read.
146 static inline
147 int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
149 struct nfs_entry *entry = desc->entry;
150 char *start = kmap(page),
151 *p = start;
152 int loop_count = 0,
153 status = 0;
155 for(;;) {
156 p = (char *)desc->decode((u32*)p, entry, desc->plus);
157 if (IS_ERR(p)) {
158 status = PTR_ERR(p);
159 break;
161 desc->page_offset = p - start;
162 dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
163 if (entry->prev_cookie == desc->target)
164 break;
165 if (loop_count++ > 200) {
166 loop_count = 0;
167 schedule();
170 kunmap(page);
171 dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
172 return status;
176 * Find the given page, and call find_dirent() in order to try to
177 * return the next entry.
179 static inline
180 int find_dirent_page(nfs_readdir_descriptor_t *desc)
182 struct inode *inode = desc->file->f_dentry->d_inode;
183 struct page *page;
184 unsigned long index = desc->page_index;
185 int status;
187 dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
189 if (desc->page) {
190 page_cache_release(desc->page);
191 desc->page = NULL;
194 page = read_cache_page(&inode->i_data, index,
195 (filler_t *)nfs_readdir_filler, desc);
196 if (IS_ERR(page)) {
197 status = PTR_ERR(page);
198 goto out;
200 if (!Page_Uptodate(page))
201 goto read_error;
203 /* NOTE: Someone else may have changed the READDIRPLUS flag */
204 desc->plus = NFS_USE_READDIRPLUS(inode);
205 status = find_dirent(desc, page);
206 if (status >= 0)
207 desc->page = page;
208 else
209 page_cache_release(page);
210 out:
211 dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
212 return status;
213 read_error:
214 page_cache_release(page);
215 return -EIO;
219 * Recurse through the page cache pages, and return a
220 * filled nfs_entry structure of the next directory entry if possible.
222 * The target for the search is 'desc->target'.
224 static inline
225 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
227 int res = 0;
228 int loop_count = 0;
230 dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
231 for (;;) {
232 res = find_dirent_page(desc);
233 if (res != -EAGAIN)
234 break;
235 /* Align to beginning of next page */
236 desc->page_offset = 0;
237 desc->page_index ++;
238 if (loop_count++ > 200) {
239 loop_count = 0;
240 schedule();
243 dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
244 return res;
248 * Once we've found the start of the dirent within a page: fill 'er up...
250 static
251 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
252 filldir_t filldir)
254 struct file *file = desc->file;
255 struct nfs_entry *entry = desc->entry;
256 char *start = kmap(desc->page),
257 *p = start + desc->page_offset;
258 unsigned long fileid;
259 int loop_count = 0,
260 res = 0;
262 dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
264 for(;;) {
265 /* Note: entry->prev_cookie contains the cookie for
266 * retrieving the current dirent on the server */
267 fileid = nfs_fileid_to_ino_t(entry->ino);
268 res = filldir(dirent, entry->name, entry->len,
269 entry->prev_cookie, fileid, DT_UNKNOWN);
270 if (res < 0)
271 break;
272 file->f_pos = desc->target = entry->cookie;
273 p = (char *)desc->decode((u32 *)p, entry, desc->plus);
274 if (IS_ERR(p)) {
275 if (PTR_ERR(p) == -EAGAIN) {
276 desc->page_offset = 0;
277 desc->page_index ++;
279 break;
281 desc->page_offset = p - start;
282 if (loop_count++ > 200) {
283 loop_count = 0;
284 schedule();
287 kunmap(desc->page);
288 page_cache_release(desc->page);
289 desc->page = NULL;
291 dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
292 return res;
296 * If we cannot find a cookie in our cache, we suspect that this is
297 * because it points to a deleted file, so we ask the server to return
298 * whatever it thinks is the next entry. We then feed this to filldir.
299 * If all goes well, we should then be able to find our way round the
300 * cache on the next call to readdir_search_pagecache();
302 * NOTE: we cannot add the anonymous page to the pagecache because
303 * the data it contains might not be page aligned. Besides,
304 * we should already have a complete representation of the
305 * directory in the page cache by the time we get here.
307 static inline
308 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
309 filldir_t filldir)
311 struct file *file = desc->file;
312 struct inode *inode = file->f_dentry->d_inode;
313 struct rpc_cred *cred = nfs_file_cred(file);
314 struct page *page = NULL;
315 u32 *p;
316 int status = -EIO;
318 dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
319 if (desc->page) {
320 page_cache_release(desc->page);
321 desc->page = NULL;
324 page = page_cache_alloc();
325 if (!page) {
326 status = -ENOMEM;
327 goto out;
329 p = kmap(page);
330 status = NFS_PROTO(inode)->readdir(inode, cred, desc->target, p,
331 NFS_SERVER(inode)->dtsize, 0);
332 if (status >= 0) {
333 p = desc->decode(p, desc->entry, 0);
334 if (IS_ERR(p))
335 status = PTR_ERR(p);
336 else
337 desc->entry->prev_cookie = desc->target;
339 kunmap(page);
340 if (status < 0)
341 goto out_release;
343 desc->page_index = 0;
344 desc->page_offset = 0;
345 desc->page = page;
346 status = nfs_do_filldir(desc, dirent, filldir);
348 /* Reset read descriptor so it searches the page cache from
349 * the start upon the next call to readdir_search_pagecache() */
350 desc->page_index = 0;
351 desc->page_offset = 0;
352 memset(desc->entry, 0, sizeof(*desc->entry));
353 out:
354 dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
355 return status;
356 out_release:
357 page_cache_release(page);
358 goto out;
361 /* The file offset position is now represented as a true offset into the
362 * page cache as is the case in most of the other filesystems.
364 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
366 struct dentry *dentry = filp->f_dentry;
367 struct inode *inode = dentry->d_inode;
368 nfs_readdir_descriptor_t my_desc,
369 *desc = &my_desc;
370 struct nfs_entry my_entry;
371 long res;
373 res = nfs_revalidate(dentry);
374 if (res < 0)
375 return res;
378 * filp->f_pos points to the file offset in the page cache.
379 * but if the cache has meanwhile been zapped, we need to
380 * read from the last dirent to revalidate f_pos
381 * itself.
383 memset(desc, 0, sizeof(*desc));
384 memset(&my_entry, 0, sizeof(my_entry));
386 desc->file = filp;
387 desc->target = filp->f_pos;
388 desc->entry = &my_entry;
389 desc->decode = NFS_PROTO(inode)->decode_dirent;
391 while(!desc->entry->eof) {
392 res = readdir_search_pagecache(desc);
393 if (res == -EBADCOOKIE) {
394 /* This means either end of directory */
395 if (desc->entry->cookie == desc->target) {
396 res = 0;
397 break;
399 /* Or that the server has 'lost' a cookie */
400 res = uncached_readdir(desc, dirent, filldir);
401 if (res >= 0)
402 continue;
404 if (res < 0)
405 break;
407 res = nfs_do_filldir(desc, dirent, filldir);
408 if (res < 0) {
409 res = 0;
410 break;
413 if (desc->page)
414 page_cache_release(desc->page);
415 if (desc->error < 0)
416 return desc->error;
417 if (res < 0)
418 return res;
419 return 0;
423 * Whenever an NFS operation succeeds, we know that the dentry
424 * is valid, so we update the revalidation timestamp.
426 static inline void nfs_renew_times(struct dentry * dentry)
428 dentry->d_time = jiffies;
431 static inline int nfs_dentry_force_reval(struct dentry *dentry, int flags)
433 struct inode *inode = dentry->d_inode;
434 unsigned long timeout = NFS_ATTRTIMEO(inode);
437 * If it's the last lookup in a series, we use a stricter
438 * cache consistency check by looking at the parent mtime.
440 * If it's been modified in the last hour, be really strict.
441 * (This still means that we can avoid doing unnecessary
442 * work on directories like /usr/share/bin etc which basically
443 * never change).
445 if (!(flags & LOOKUP_CONTINUE)) {
446 long diff = CURRENT_TIME - dentry->d_parent->d_inode->i_mtime;
448 if (diff < 15*60)
449 timeout = 0;
452 return time_after(jiffies,dentry->d_time + timeout);
456 * We judge how long we want to trust negative
457 * dentries by looking at the parent inode mtime.
459 * If mtime is close to present time, we revalidate
460 * more often.
462 #define NFS_REVALIDATE_NEGATIVE (1 * HZ)
463 static inline int nfs_neg_need_reval(struct dentry *dentry)
465 struct inode *dir = dentry->d_parent->d_inode;
466 unsigned long timeout = NFS_ATTRTIMEO(dir);
467 long diff = CURRENT_TIME - dir->i_mtime;
469 if (diff < 5*60 && timeout > NFS_REVALIDATE_NEGATIVE)
470 timeout = NFS_REVALIDATE_NEGATIVE;
472 return time_after(jiffies, dentry->d_time + timeout);
476 * This is called every time the dcache has a lookup hit,
477 * and we should check whether we can really trust that
478 * lookup.
480 * NOTE! The hit can be a negative hit too, don't assume
481 * we have an inode!
483 * If the dentry is older than the revalidation interval,
484 * we do a new lookup and verify that the dentry is still
485 * correct.
487 static int nfs_lookup_revalidate(struct dentry * dentry, int flags)
489 struct inode *dir;
490 struct inode *inode;
491 int error;
492 struct nfs_fh fhandle;
493 struct nfs_fattr fattr;
495 lock_kernel();
496 dir = dentry->d_parent->d_inode;
497 inode = dentry->d_inode;
499 * If we don't have an inode, let's look at the parent
500 * directory mtime to get a hint about how often we
501 * should validate things..
503 if (!inode) {
504 if (nfs_neg_need_reval(dentry))
505 goto out_bad;
506 goto out_valid;
509 if (is_bad_inode(inode)) {
510 dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
511 dentry->d_parent->d_name.name, dentry->d_name.name);
512 goto out_bad;
515 if (!nfs_dentry_force_reval(dentry, flags))
516 goto out_valid;
518 if (IS_ROOT(dentry)) {
519 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
520 goto out_valid_renew;
524 * Do a new lookup and check the dentry attributes.
526 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
527 if (error)
528 goto out_bad;
530 /* Inode number matches? */
531 if (!(fattr.valid & NFS_ATTR_FATTR) ||
532 NFS_FSID(inode) != fattr.fsid ||
533 NFS_FILEID(inode) != fattr.fileid)
534 goto out_bad;
536 /* Filehandle matches? */
537 if (memcmp(NFS_FH(inode), fhandle.data, sizeof(struct nfs_fh)))
538 goto out_bad;
540 /* Ok, remember that we successfully checked it.. */
541 nfs_refresh_inode(inode, &fattr);
543 out_valid_renew:
544 nfs_renew_times(dentry);
545 out_valid:
546 unlock_kernel();
547 return 1;
548 out_bad:
549 shrink_dcache_parent(dentry);
550 /* If we have submounts, don't unhash ! */
551 if (have_submounts(dentry))
552 goto out_valid;
553 d_drop(dentry);
554 /* Purge readdir caches. */
555 nfs_zap_caches(dir);
556 if (inode && S_ISDIR(inode->i_mode))
557 nfs_zap_caches(inode);
558 unlock_kernel();
559 return 0;
563 * This is called from dput() when d_count is going to 0.
565 static int nfs_dentry_delete(struct dentry *dentry)
567 dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
568 dentry->d_parent->d_name.name, dentry->d_name.name,
569 dentry->d_flags);
571 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
572 /* Unhash it, so that ->d_iput() would be called */
573 return 1;
575 return 0;
580 * Called when the dentry loses inode.
581 * We use it to clean up silly-renamed files.
583 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
585 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
586 lock_kernel();
587 nfs_complete_unlink(dentry);
588 unlock_kernel();
590 iput(inode);
593 struct dentry_operations nfs_dentry_operations = {
594 d_revalidate: nfs_lookup_revalidate,
595 d_delete: nfs_dentry_delete,
596 d_iput: nfs_dentry_iput,
599 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry)
601 struct inode *inode;
602 int error;
603 struct nfs_fh fhandle;
604 struct nfs_fattr fattr;
606 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
607 dentry->d_parent->d_name.name, dentry->d_name.name);
609 error = -ENAMETOOLONG;
610 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
611 goto out;
613 error = -ENOMEM;
614 dentry->d_op = &nfs_dentry_operations;
616 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
617 inode = NULL;
618 if (error == -ENOENT)
619 goto no_entry;
620 if (!error) {
621 error = -EACCES;
622 inode = nfs_fhget(dentry, &fhandle, &fattr);
623 if (inode) {
624 no_entry:
625 d_add(dentry, inode);
626 nfs_renew_times(dentry);
627 error = 0;
630 out:
631 return ERR_PTR(error);
635 * Code common to create, mkdir, and mknod.
637 static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
638 struct nfs_fattr *fattr)
640 struct inode *inode;
641 int error = -EACCES;
643 inode = nfs_fhget(dentry, fhandle, fattr);
644 if (inode) {
645 d_instantiate(dentry, inode);
646 nfs_renew_times(dentry);
647 error = 0;
649 return error;
653 * Following a failed create operation, we drop the dentry rather
654 * than retain a negative dentry. This avoids a problem in the event
655 * that the operation succeeded on the server, but an error in the
656 * reply path made it appear to have failed.
658 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode)
660 struct iattr attr;
661 struct nfs_fattr fattr;
662 struct nfs_fh fhandle;
663 int error;
665 dfprintk(VFS, "NFS: create(%x/%ld, %s\n",
666 dir->i_dev, dir->i_ino, dentry->d_name.name);
668 attr.ia_mode = mode;
669 attr.ia_valid = ATTR_MODE;
672 * The 0 argument passed into the create function should one day
673 * contain the O_EXCL flag if requested. This allows NFSv3 to
674 * select the appropriate create strategy. Currently open_namei
675 * does not pass the create flags.
677 nfs_zap_caches(dir);
678 error = NFS_PROTO(dir)->create(dir, &dentry->d_name,
679 &attr, 0, &fhandle, &fattr);
680 if (!error && fhandle.size != 0)
681 error = nfs_instantiate(dentry, &fhandle, &fattr);
682 if (error || fhandle.size == 0)
683 d_drop(dentry);
684 return error;
688 * See comments for nfs_proc_create regarding failed operations.
690 static int nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
692 struct iattr attr;
693 struct nfs_fattr fattr;
694 struct nfs_fh fhandle;
695 int error;
697 dfprintk(VFS, "NFS: mknod(%x/%ld, %s\n",
698 dir->i_dev, dir->i_ino, dentry->d_name.name);
700 attr.ia_mode = mode;
701 attr.ia_valid = ATTR_MODE;
703 nfs_zap_caches(dir);
704 error = NFS_PROTO(dir)->mknod(dir, &dentry->d_name, &attr, rdev,
705 &fhandle, &fattr);
706 if (!error && fhandle.size != 0)
707 error = nfs_instantiate(dentry, &fhandle, &fattr);
708 if (error || fhandle.size == 0)
709 d_drop(dentry);
710 return error;
714 * See comments for nfs_proc_create regarding failed operations.
716 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
718 struct iattr attr;
719 struct nfs_fattr fattr;
720 struct nfs_fh fhandle;
721 int error;
723 dfprintk(VFS, "NFS: mkdir(%x/%ld, %s\n",
724 dir->i_dev, dir->i_ino, dentry->d_name.name);
726 attr.ia_valid = ATTR_MODE;
727 attr.ia_mode = mode | S_IFDIR;
729 #if 0
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 #endif
738 nfs_zap_caches(dir);
739 error = NFS_PROTO(dir)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
740 &fattr);
741 if (!error && fhandle.size != 0)
742 error = nfs_instantiate(dentry, &fhandle, &fattr);
743 if (error || fhandle.size == 0)
744 d_drop(dentry);
745 return error;
748 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
750 int error;
752 dfprintk(VFS, "NFS: rmdir(%x/%ld, %s\n",
753 dir->i_dev, dir->i_ino, dentry->d_name.name);
755 nfs_zap_caches(dir);
756 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
758 return error;
761 static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
763 static unsigned int sillycounter;
764 const int i_inosize = sizeof(dir->i_ino)*2;
765 const int countersize = sizeof(sillycounter)*2;
766 const int slen = strlen(".nfs") + i_inosize + countersize;
767 char silly[slen+1];
768 struct qstr qsilly;
769 struct dentry *sdentry;
770 int error = -EIO;
772 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
773 dentry->d_parent->d_name.name, dentry->d_name.name,
774 atomic_read(&dentry->d_count));
776 if (atomic_read(&dentry->d_count) == 1)
777 goto out; /* No need to silly rename. */
780 #ifdef NFS_PARANOIA
781 if (!dentry->d_inode)
782 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
783 dentry->d_parent->d_name.name, dentry->d_name.name);
784 #endif
786 * We don't allow a dentry to be silly-renamed twice.
788 error = -EBUSY;
789 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
790 goto out;
792 sprintf(silly, ".nfs%*.*lx",
793 i_inosize, i_inosize, dentry->d_inode->i_ino);
795 sdentry = NULL;
796 do {
797 char *suffix = silly + slen - countersize;
799 dput(sdentry);
800 sillycounter++;
801 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
803 dfprintk(VFS, "trying to rename %s to %s\n",
804 dentry->d_name.name, silly);
806 sdentry = lookup_one(silly, dentry->d_parent);
808 * N.B. Better to return EBUSY here ... it could be
809 * dangerous to delete the file while it's in use.
811 if (IS_ERR(sdentry))
812 goto out;
813 } while(sdentry->d_inode != NULL); /* need negative lookup */
815 nfs_zap_caches(dir);
816 qsilly.name = silly;
817 qsilly.len = strlen(silly);
818 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, dir, &qsilly);
819 if (!error) {
820 nfs_renew_times(dentry);
821 d_move(dentry, sdentry);
822 error = nfs_async_unlink(dentry);
823 /* If we return 0 we don't unlink */
825 dput(sdentry);
826 out:
827 return error;
831 * Remove a file after making sure there are no pending writes,
832 * and after checking that the file has only one user.
834 * We invalidate the attribute cache and free the inode prior to the operation
835 * to avoid possible races if the server reuses the inode.
837 static int nfs_safe_remove(struct dentry *dentry)
839 struct inode *dir = dentry->d_parent->d_inode;
840 struct inode *inode = dentry->d_inode;
841 int error = -EBUSY, rehash = 0;
843 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
844 dentry->d_parent->d_name.name, dentry->d_name.name);
847 * Unhash the dentry while we remove the file ...
849 if (!d_unhashed(dentry)) {
850 d_drop(dentry);
851 rehash = 1;
853 if (atomic_read(&dentry->d_count) > 1) {
854 #ifdef NFS_PARANOIA
855 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
856 dentry->d_parent->d_name.name, dentry->d_name.name,
857 atomic_read(&dentry->d_count));
858 #endif
859 goto out;
862 /* If the dentry was sillyrenamed, we simply call d_delete() */
863 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
864 error = 0;
865 goto out_delete;
868 nfs_zap_caches(dir);
869 if (inode)
870 NFS_CACHEINV(inode);
871 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
872 if (error < 0)
873 goto out;
875 out_delete:
877 * Free the inode
879 d_delete(dentry);
880 out:
881 if (rehash)
882 d_rehash(dentry);
883 return error;
886 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
887 * belongs to an active ".nfs..." file and we return -EBUSY.
889 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
891 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
893 int error;
895 dfprintk(VFS, "NFS: unlink(%x/%ld, %s)\n",
896 dir->i_dev, dir->i_ino, dentry->d_name.name);
898 error = nfs_sillyrename(dir, dentry);
899 if (error && error != -EBUSY) {
900 error = nfs_safe_remove(dentry);
901 if (!error) {
902 nfs_renew_times(dentry);
905 return error;
908 static int
909 nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
911 struct iattr attr;
912 struct nfs_fattr sym_attr;
913 struct nfs_fh sym_fh;
914 struct qstr qsymname;
915 unsigned int maxlen;
916 int error;
918 dfprintk(VFS, "NFS: symlink(%x/%ld, %s, %s)\n",
919 dir->i_dev, dir->i_ino, dentry->d_name.name, symname);
921 error = -ENAMETOOLONG;
922 maxlen = (NFS_PROTO(dir)->version==2) ? NFS2_MAXPATHLEN : NFS3_MAXPATHLEN;
923 if (strlen(symname) > maxlen)
924 goto out;
926 #ifdef NFS_PARANOIA
927 if (dentry->d_inode)
928 printk("nfs_proc_symlink: %s/%s not negative!\n",
929 dentry->d_parent->d_name.name, dentry->d_name.name);
930 #endif
932 * Fill in the sattr for the call.
933 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
935 attr.ia_valid = ATTR_MODE;
936 attr.ia_mode = S_IFLNK | S_IRWXUGO;
938 qsymname.name = symname;
939 qsymname.len = strlen(symname);
941 nfs_zap_caches(dir);
942 error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
943 &attr, &sym_fh, &sym_attr);
944 if (!error && sym_fh.size != 0 && (sym_attr.valid & NFS_ATTR_FATTR)) {
945 error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
946 } else {
947 if (error == -EEXIST)
948 printk("nfs_proc_symlink: %s/%s already exists??\n",
949 dentry->d_parent->d_name.name, dentry->d_name.name);
950 d_drop(dentry);
953 out:
954 return error;
957 static int
958 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
960 struct inode *inode = old_dentry->d_inode;
961 int error;
963 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
964 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
965 dentry->d_parent->d_name.name, dentry->d_name.name);
968 * Drop the dentry in advance to force a new lookup.
969 * Since nfs_proc_link doesn't return a file handle,
970 * we can't use the existing dentry.
972 d_drop(dentry);
973 nfs_zap_caches(dir);
974 NFS_CACHEINV(inode);
975 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
976 return error;
980 * RENAME
981 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
982 * different file handle for the same inode after a rename (e.g. when
983 * moving to a different directory). A fail-safe method to do so would
984 * be to look up old_dir/old_name, create a link to new_dir/new_name and
985 * rename the old file using the sillyrename stuff. This way, the original
986 * file in old_dir will go away when the last process iput()s the inode.
988 * FIXED.
990 * It actually works quite well. One needs to have the possibility for
991 * at least one ".nfs..." file in each directory the file ever gets
992 * moved or linked to which happens automagically with the new
993 * implementation that only depends on the dcache stuff instead of
994 * using the inode layer
996 * Unfortunately, things are a little more complicated than indicated
997 * above. For a cross-directory move, we want to make sure we can get
998 * rid of the old inode after the operation. This means there must be
999 * no pending writes (if it's a file), and the use count must be 1.
1000 * If these conditions are met, we can drop the dentries before doing
1001 * the rename.
1003 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1004 struct inode *new_dir, struct dentry *new_dentry)
1006 struct inode *old_inode = old_dentry->d_inode;
1007 struct inode *new_inode = new_dentry->d_inode;
1008 struct dentry *dentry = NULL, *rehash = NULL;
1009 int error = -EBUSY;
1012 * To prevent any new references to the target during the rename,
1013 * we unhash the dentry and free the inode in advance.
1015 if (!d_unhashed(new_dentry)) {
1016 d_drop(new_dentry);
1017 rehash = new_dentry;
1020 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1021 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1022 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1023 atomic_read(&new_dentry->d_count));
1026 * First check whether the target is busy ... we can't
1027 * safely do _any_ rename if the target is in use.
1029 * For files, make a copy of the dentry and then do a
1030 * silly-rename. If the silly-rename succeeds, the
1031 * copied dentry is hashed and becomes the new target.
1033 if (!new_inode)
1034 goto go_ahead;
1035 if (S_ISDIR(new_inode->i_mode))
1036 goto out;
1037 else if (atomic_read(&new_dentry->d_count) > 1) {
1038 int err;
1039 /* copy the target dentry's name */
1040 dentry = d_alloc(new_dentry->d_parent,
1041 &new_dentry->d_name);
1042 if (!dentry)
1043 goto out;
1045 /* silly-rename the existing target ... */
1046 err = nfs_sillyrename(new_dir, new_dentry);
1047 if (!err) {
1048 new_dentry = rehash = dentry;
1049 new_inode = NULL;
1050 /* instantiate the replacement target */
1051 d_instantiate(new_dentry, NULL);
1054 /* dentry still busy? */
1055 if (atomic_read(&new_dentry->d_count) > 1) {
1056 #ifdef NFS_PARANOIA
1057 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1058 new_dentry->d_parent->d_name.name,
1059 new_dentry->d_name.name,
1060 atomic_read(&new_dentry->d_count));
1061 #endif
1062 goto out;
1066 go_ahead:
1068 * ... prune child dentries and writebacks if needed.
1070 if (atomic_read(&old_dentry->d_count) > 1) {
1071 nfs_wb_all(old_inode);
1072 shrink_dcache_parent(old_dentry);
1075 if (new_inode)
1076 d_delete(new_dentry);
1078 nfs_zap_caches(new_dir);
1079 nfs_zap_caches(old_dir);
1080 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1081 new_dir, &new_dentry->d_name);
1082 out:
1083 if (rehash)
1084 d_rehash(rehash);
1085 if (!error && !S_ISDIR(old_inode->i_mode))
1086 d_move(old_dentry, new_dentry);
1088 /* new dentry created? */
1089 if (dentry)
1090 dput(dentry);
1091 return error;
1095 nfs_permission(struct inode *inode, int mask)
1097 int error = vfs_permission(inode, mask);
1099 if (!NFS_PROTO(inode)->access)
1100 goto out;
1102 * Trust UNIX mode bits except:
1104 * 1) When override capabilities may have been invoked
1105 * 2) When root squashing may be involved
1106 * 3) When ACLs may overturn a negative answer */
1107 if (!capable(CAP_DAC_OVERRIDE) && !capable(CAP_DAC_READ_SEARCH)
1108 && (current->fsuid != 0) && (current->fsgid != 0)
1109 && error != -EACCES)
1110 goto out;
1112 error = NFS_PROTO(inode)->access(inode, mask, 0);
1114 if (error == -EACCES && NFS_CLIENT(inode)->cl_droppriv &&
1115 current->uid != 0 && current->gid != 0 &&
1116 (current->fsuid != current->uid || current->fsgid != current->gid))
1117 error = NFS_PROTO(inode)->access(inode, mask, 1);
1119 out:
1120 return error;
1124 * Local variables:
1125 * version-control: t
1126 * kept-new-versions: 5
1127 * End: