- Stephen Rothwell: APM updates
[davej-history.git] / fs / nfs / dir.c
blob564a47b321ebfdc3b295d91cbab147e99033a116
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 /* Ok, remember that we successfully checked it.. */
537 nfs_refresh_inode(inode, &fattr);
539 if (nfs_inode_is_stale(inode, &fhandle, &fattr))
540 goto out_bad;
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);
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;
579 * Called when the dentry loses inode.
580 * We use it to clean up silly-renamed files.
582 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
584 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
585 lock_kernel();
586 nfs_complete_unlink(dentry);
587 unlock_kernel();
589 iput(inode);
592 struct dentry_operations nfs_dentry_operations = {
593 d_revalidate: nfs_lookup_revalidate,
594 d_delete: nfs_dentry_delete,
595 d_iput: nfs_dentry_iput,
598 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry)
600 struct inode *inode;
601 int error;
602 struct nfs_fh fhandle;
603 struct nfs_fattr fattr;
605 dfprintk(VFS, "NFS: lookup(%s/%s)\n",
606 dentry->d_parent->d_name.name, dentry->d_name.name);
608 error = -ENAMETOOLONG;
609 if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
610 goto out;
612 error = -ENOMEM;
613 dentry->d_op = &nfs_dentry_operations;
615 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
616 inode = NULL;
617 if (error == -ENOENT)
618 goto no_entry;
619 if (!error) {
620 error = -EACCES;
621 inode = nfs_fhget(dentry, &fhandle, &fattr);
622 if (inode) {
623 no_entry:
624 d_add(dentry, inode);
625 nfs_renew_times(dentry);
626 error = 0;
629 out:
630 return ERR_PTR(error);
634 * Code common to create, mkdir, and mknod.
636 static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
637 struct nfs_fattr *fattr)
639 struct inode *inode;
640 int error = -EACCES;
642 inode = nfs_fhget(dentry, fhandle, fattr);
643 if (inode) {
644 d_instantiate(dentry, inode);
645 nfs_renew_times(dentry);
646 error = 0;
648 return error;
652 * Following a failed create operation, we drop the dentry rather
653 * than retain a negative dentry. This avoids a problem in the event
654 * that the operation succeeded on the server, but an error in the
655 * reply path made it appear to have failed.
657 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode)
659 struct iattr attr;
660 struct nfs_fattr fattr;
661 struct nfs_fh fhandle;
662 int error;
664 dfprintk(VFS, "NFS: create(%x/%ld, %s\n",
665 dir->i_dev, dir->i_ino, dentry->d_name.name);
667 attr.ia_mode = mode;
668 attr.ia_valid = ATTR_MODE;
671 * The 0 argument passed into the create function should one day
672 * contain the O_EXCL flag if requested. This allows NFSv3 to
673 * select the appropriate create strategy. Currently open_namei
674 * does not pass the create flags.
676 nfs_zap_caches(dir);
677 error = NFS_PROTO(dir)->create(dir, &dentry->d_name,
678 &attr, 0, &fhandle, &fattr);
679 if (!error && fhandle.size != 0)
680 error = nfs_instantiate(dentry, &fhandle, &fattr);
681 if (error || fhandle.size == 0)
682 d_drop(dentry);
683 return error;
687 * See comments for nfs_proc_create regarding failed operations.
689 static int nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
691 struct iattr attr;
692 struct nfs_fattr fattr;
693 struct nfs_fh fhandle;
694 int error;
696 dfprintk(VFS, "NFS: mknod(%x/%ld, %s\n",
697 dir->i_dev, dir->i_ino, dentry->d_name.name);
699 attr.ia_mode = mode;
700 attr.ia_valid = ATTR_MODE;
702 nfs_zap_caches(dir);
703 error = NFS_PROTO(dir)->mknod(dir, &dentry->d_name, &attr, rdev,
704 &fhandle, &fattr);
705 if (!error && fhandle.size != 0)
706 error = nfs_instantiate(dentry, &fhandle, &fattr);
707 if (error || fhandle.size == 0)
708 d_drop(dentry);
709 return error;
713 * See comments for nfs_proc_create regarding failed operations.
715 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
717 struct iattr attr;
718 struct nfs_fattr fattr;
719 struct nfs_fh fhandle;
720 int error;
722 dfprintk(VFS, "NFS: mkdir(%x/%ld, %s\n",
723 dir->i_dev, dir->i_ino, dentry->d_name.name);
725 attr.ia_valid = ATTR_MODE;
726 attr.ia_mode = mode | S_IFDIR;
728 #if 0
730 * Always drop the dentry, we can't always depend on
731 * the fattr returned by the server (AIX seems to be
732 * broken). We're better off doing another lookup than
733 * depending on potentially bogus information.
735 d_drop(dentry);
736 #endif
737 nfs_zap_caches(dir);
738 error = NFS_PROTO(dir)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
739 &fattr);
740 if (!error && fhandle.size != 0)
741 error = nfs_instantiate(dentry, &fhandle, &fattr);
742 if (error || fhandle.size == 0)
743 d_drop(dentry);
744 return error;
747 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
749 int error;
751 dfprintk(VFS, "NFS: rmdir(%x/%ld, %s\n",
752 dir->i_dev, dir->i_ino, dentry->d_name.name);
754 nfs_zap_caches(dir);
755 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
757 return error;
760 static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
762 static unsigned int sillycounter;
763 const int i_inosize = sizeof(dir->i_ino)*2;
764 const int countersize = sizeof(sillycounter)*2;
765 const int slen = strlen(".nfs") + i_inosize + countersize;
766 char silly[slen+1];
767 struct qstr qsilly;
768 struct dentry *sdentry;
769 int error = -EIO;
771 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
772 dentry->d_parent->d_name.name, dentry->d_name.name,
773 atomic_read(&dentry->d_count));
775 if (atomic_read(&dentry->d_count) == 1)
776 goto out; /* No need to silly rename. */
779 #ifdef NFS_PARANOIA
780 if (!dentry->d_inode)
781 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
782 dentry->d_parent->d_name.name, dentry->d_name.name);
783 #endif
785 * We don't allow a dentry to be silly-renamed twice.
787 error = -EBUSY;
788 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
789 goto out;
791 sprintf(silly, ".nfs%*.*lx",
792 i_inosize, i_inosize, dentry->d_inode->i_ino);
794 sdentry = NULL;
795 do {
796 char *suffix = silly + slen - countersize;
798 dput(sdentry);
799 sillycounter++;
800 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
802 dfprintk(VFS, "trying to rename %s to %s\n",
803 dentry->d_name.name, silly);
805 sdentry = lookup_one(silly, dentry->d_parent);
807 * N.B. Better to return EBUSY here ... it could be
808 * dangerous to delete the file while it's in use.
810 if (IS_ERR(sdentry))
811 goto out;
812 } while(sdentry->d_inode != NULL); /* need negative lookup */
814 nfs_zap_caches(dir);
815 qsilly.name = silly;
816 qsilly.len = strlen(silly);
817 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, dir, &qsilly);
818 if (!error) {
819 nfs_renew_times(dentry);
820 d_move(dentry, sdentry);
821 error = nfs_async_unlink(dentry);
822 /* If we return 0 we don't unlink */
824 dput(sdentry);
825 out:
826 return error;
830 * Remove a file after making sure there are no pending writes,
831 * and after checking that the file has only one user.
833 * We invalidate the attribute cache and free the inode prior to the operation
834 * to avoid possible races if the server reuses the inode.
836 static int nfs_safe_remove(struct dentry *dentry)
838 struct inode *dir = dentry->d_parent->d_inode;
839 struct inode *inode = dentry->d_inode;
840 int error = -EBUSY, rehash = 0;
842 dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
843 dentry->d_parent->d_name.name, dentry->d_name.name);
846 * Unhash the dentry while we remove the file ...
848 if (!d_unhashed(dentry)) {
849 d_drop(dentry);
850 rehash = 1;
852 if (atomic_read(&dentry->d_count) > 1) {
853 #ifdef NFS_PARANOIA
854 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
855 dentry->d_parent->d_name.name, dentry->d_name.name,
856 atomic_read(&dentry->d_count));
857 #endif
858 goto out;
861 /* If the dentry was sillyrenamed, we simply call d_delete() */
862 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
863 error = 0;
864 goto out_delete;
867 nfs_zap_caches(dir);
868 if (inode)
869 NFS_CACHEINV(inode);
870 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
871 if (error < 0)
872 goto out;
874 out_delete:
876 * Free the inode
878 d_delete(dentry);
879 out:
880 if (rehash)
881 d_rehash(dentry);
882 return error;
885 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
886 * belongs to an active ".nfs..." file and we return -EBUSY.
888 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
890 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
892 int error;
894 dfprintk(VFS, "NFS: unlink(%x/%ld, %s)\n",
895 dir->i_dev, dir->i_ino, dentry->d_name.name);
897 error = nfs_sillyrename(dir, dentry);
898 if (error && error != -EBUSY) {
899 error = nfs_safe_remove(dentry);
900 if (!error) {
901 nfs_renew_times(dentry);
904 return error;
907 static int
908 nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
910 struct iattr attr;
911 struct nfs_fattr sym_attr;
912 struct nfs_fh sym_fh;
913 struct qstr qsymname;
914 unsigned int maxlen;
915 int error;
917 dfprintk(VFS, "NFS: symlink(%x/%ld, %s, %s)\n",
918 dir->i_dev, dir->i_ino, dentry->d_name.name, symname);
920 error = -ENAMETOOLONG;
921 maxlen = (NFS_PROTO(dir)->version==2) ? NFS2_MAXPATHLEN : NFS3_MAXPATHLEN;
922 if (strlen(symname) > maxlen)
923 goto out;
925 #ifdef NFS_PARANOIA
926 if (dentry->d_inode)
927 printk("nfs_proc_symlink: %s/%s not negative!\n",
928 dentry->d_parent->d_name.name, dentry->d_name.name);
929 #endif
931 * Fill in the sattr for the call.
932 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
934 attr.ia_valid = ATTR_MODE;
935 attr.ia_mode = S_IFLNK | S_IRWXUGO;
937 qsymname.name = symname;
938 qsymname.len = strlen(symname);
940 nfs_zap_caches(dir);
941 error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
942 &attr, &sym_fh, &sym_attr);
943 if (!error && sym_fh.size != 0 && (sym_attr.valid & NFS_ATTR_FATTR)) {
944 error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
945 } else {
946 if (error == -EEXIST)
947 printk("nfs_proc_symlink: %s/%s already exists??\n",
948 dentry->d_parent->d_name.name, dentry->d_name.name);
949 d_drop(dentry);
952 out:
953 return error;
956 static int
957 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
959 struct inode *inode = old_dentry->d_inode;
960 int error;
962 dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
963 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
964 dentry->d_parent->d_name.name, dentry->d_name.name);
967 * Drop the dentry in advance to force a new lookup.
968 * Since nfs_proc_link doesn't return a file handle,
969 * we can't use the existing dentry.
971 d_drop(dentry);
972 nfs_zap_caches(dir);
973 NFS_CACHEINV(inode);
974 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
975 return error;
979 * RENAME
980 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
981 * different file handle for the same inode after a rename (e.g. when
982 * moving to a different directory). A fail-safe method to do so would
983 * be to look up old_dir/old_name, create a link to new_dir/new_name and
984 * rename the old file using the sillyrename stuff. This way, the original
985 * file in old_dir will go away when the last process iput()s the inode.
987 * FIXED.
989 * It actually works quite well. One needs to have the possibility for
990 * at least one ".nfs..." file in each directory the file ever gets
991 * moved or linked to which happens automagically with the new
992 * implementation that only depends on the dcache stuff instead of
993 * using the inode layer
995 * Unfortunately, things are a little more complicated than indicated
996 * above. For a cross-directory move, we want to make sure we can get
997 * rid of the old inode after the operation. This means there must be
998 * no pending writes (if it's a file), and the use count must be 1.
999 * If these conditions are met, we can drop the dentries before doing
1000 * the rename.
1002 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1003 struct inode *new_dir, struct dentry *new_dentry)
1005 struct inode *old_inode = old_dentry->d_inode;
1006 struct inode *new_inode = new_dentry->d_inode;
1007 struct dentry *dentry = NULL, *rehash = NULL;
1008 int error = -EBUSY;
1011 * To prevent any new references to the target during the rename,
1012 * we unhash the dentry and free the inode in advance.
1014 if (!d_unhashed(new_dentry)) {
1015 d_drop(new_dentry);
1016 rehash = new_dentry;
1019 dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1020 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1021 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1022 atomic_read(&new_dentry->d_count));
1025 * First check whether the target is busy ... we can't
1026 * safely do _any_ rename if the target is in use.
1028 * For files, make a copy of the dentry and then do a
1029 * silly-rename. If the silly-rename succeeds, the
1030 * copied dentry is hashed and becomes the new target.
1032 if (!new_inode)
1033 goto go_ahead;
1034 if (S_ISDIR(new_inode->i_mode))
1035 goto out;
1036 else if (atomic_read(&new_dentry->d_count) > 1) {
1037 int err;
1038 /* copy the target dentry's name */
1039 dentry = d_alloc(new_dentry->d_parent,
1040 &new_dentry->d_name);
1041 if (!dentry)
1042 goto out;
1044 /* silly-rename the existing target ... */
1045 err = nfs_sillyrename(new_dir, new_dentry);
1046 if (!err) {
1047 new_dentry = rehash = dentry;
1048 new_inode = NULL;
1049 /* instantiate the replacement target */
1050 d_instantiate(new_dentry, NULL);
1053 /* dentry still busy? */
1054 if (atomic_read(&new_dentry->d_count) > 1) {
1055 #ifdef NFS_PARANOIA
1056 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1057 new_dentry->d_parent->d_name.name,
1058 new_dentry->d_name.name,
1059 atomic_read(&new_dentry->d_count));
1060 #endif
1061 goto out;
1065 go_ahead:
1067 * ... prune child dentries and writebacks if needed.
1069 if (atomic_read(&old_dentry->d_count) > 1) {
1070 nfs_wb_all(old_inode);
1071 shrink_dcache_parent(old_dentry);
1074 if (new_inode)
1075 d_delete(new_dentry);
1077 nfs_zap_caches(new_dir);
1078 nfs_zap_caches(old_dir);
1079 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1080 new_dir, &new_dentry->d_name);
1081 out:
1082 if (rehash)
1083 d_rehash(rehash);
1084 if (!error && !S_ISDIR(old_inode->i_mode))
1085 d_move(old_dentry, new_dentry);
1087 /* new dentry created? */
1088 if (dentry)
1089 dput(dentry);
1090 return error;
1094 nfs_permission(struct inode *inode, int mask)
1096 int error = vfs_permission(inode, mask);
1098 if (!NFS_PROTO(inode)->access)
1099 goto out;
1101 * Trust UNIX mode bits except:
1103 * 1) When override capabilities may have been invoked
1104 * 2) When root squashing may be involved
1105 * 3) When ACLs may overturn a negative answer */
1106 if (!capable(CAP_DAC_OVERRIDE) && !capable(CAP_DAC_READ_SEARCH)
1107 && (current->fsuid != 0) && (current->fsgid != 0)
1108 && error != -EACCES)
1109 goto out;
1111 error = NFS_PROTO(inode)->access(inode, mask, 0);
1113 if (error == -EACCES && NFS_CLIENT(inode)->cl_droppriv &&
1114 current->uid != 0 && current->gid != 0 &&
1115 (current->fsuid != current->uid || current->fsgid != current->gid))
1116 error = NFS_PROTO(inode)->access(inode, mask, 1);
1118 out:
1119 return error;
1123 * Local variables:
1124 * version-control: t
1125 * kept-new-versions: 5
1126 * End: