This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / nfs / dir.c
1 /*
2  *  linux/fs/nfs/dir.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs directory handling functions
7  *
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
18  */
19
20 #include <linux/time.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/slab.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>
33 #include <linux/namei.h>
34
35 #include "delegation.h"
36
37 #define NFS_PARANOIA 1
38 /* #define NFS_DEBUG_VERBOSE 1 */
39
40 static int nfs_opendir(struct inode *, struct file *);
41 static int nfs_readdir(struct file *, void *, filldir_t);
42 static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
43 static int nfs_cached_lookup(struct inode *, struct dentry *,
44                                 struct nfs_fh *, struct nfs_fattr *);
45 static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
46 static int nfs_mkdir(struct inode *, struct dentry *, int);
47 static int nfs_rmdir(struct inode *, struct dentry *);
48 static int nfs_unlink(struct inode *, struct dentry *);
49 static int nfs_symlink(struct inode *, struct dentry *, const char *);
50 static int nfs_link(struct dentry *, struct inode *, struct dentry *);
51 static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
52 static int nfs_rename(struct inode *, struct dentry *,
53                       struct inode *, struct dentry *);
54 static int nfs_fsync_dir(struct file *, struct dentry *, int);
55
56 struct file_operations nfs_dir_operations = {
57         .read           = generic_read_dir,
58         .readdir        = nfs_readdir,
59         .open           = nfs_opendir,
60         .release        = nfs_release,
61         .fsync          = nfs_fsync_dir,
62 };
63
64 struct inode_operations nfs_dir_inode_operations = {
65         .create         = nfs_create,
66         .lookup         = nfs_lookup,
67         .link           = nfs_link,
68         .unlink         = nfs_unlink,
69         .symlink        = nfs_symlink,
70         .mkdir          = nfs_mkdir,
71         .rmdir          = nfs_rmdir,
72         .mknod          = nfs_mknod,
73         .rename         = nfs_rename,
74         .permission     = nfs_permission,
75         .getattr        = nfs_getattr,
76         .setattr        = nfs_setattr,
77 };
78
79 #ifdef CONFIG_NFS_V4
80
81 static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
82 struct inode_operations nfs4_dir_inode_operations = {
83         .create         = nfs_create,
84         .lookup         = nfs_atomic_lookup,
85         .link           = nfs_link,
86         .unlink         = nfs_unlink,
87         .symlink        = nfs_symlink,
88         .mkdir          = nfs_mkdir,
89         .rmdir          = nfs_rmdir,
90         .mknod          = nfs_mknod,
91         .rename         = nfs_rename,
92         .permission     = nfs_permission,
93         .getattr        = nfs_getattr,
94         .setattr        = nfs_setattr,
95 };
96
97 #endif /* CONFIG_NFS_V4 */
98
99 /*
100  * Open file
101  */
102 static int
103 nfs_opendir(struct inode *inode, struct file *filp)
104 {
105         int res = 0;
106
107         lock_kernel();
108         /* Call generic open code in order to cache credentials */
109         if (!res)
110                 res = nfs_open(inode, filp);
111         unlock_kernel();
112         return res;
113 }
114
115 typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
116 typedef struct {
117         struct file     *file;
118         struct page     *page;
119         unsigned long   page_index;
120         u32             *ptr;
121         u64             target;
122         struct nfs_entry *entry;
123         decode_dirent_t decode;
124         int             plus;
125         int             error;
126 } nfs_readdir_descriptor_t;
127
128 /* Now we cache directories properly, by stuffing the dirent
129  * data directly in the page cache.
130  *
131  * Inode invalidation due to refresh etc. takes care of
132  * _everything_, no sloppy entry flushing logic, no extraneous
133  * copying, network direct to page cache, the way it was meant
134  * to be.
135  *
136  * NOTE: Dirent information verification is done always by the
137  *       page-in of the RPC reply, nowhere else, this simplies
138  *       things substantially.
139  */
140 static
141 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
142 {
143         struct file     *file = desc->file;
144         struct inode    *inode = file->f_dentry->d_inode;
145         struct rpc_cred *cred = nfs_file_cred(file);
146         unsigned long   timestamp;
147         int             error;
148
149         dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
150
151  again:
152         timestamp = jiffies;
153         error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
154                                           NFS_SERVER(inode)->dtsize, desc->plus);
155         if (error < 0) {
156                 /* We requested READDIRPLUS, but the server doesn't grok it */
157                 if (error == -ENOTSUPP && desc->plus) {
158                         NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
159                         NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
160                         desc->plus = 0;
161                         goto again;
162                 }
163                 goto error;
164         }
165         SetPageUptodate(page);
166         NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
167         /* Ensure consistent page alignment of the data.
168          * Note: assumes we have exclusive access to this mapping either
169          *       throught inode->i_sem or some other mechanism.
170          */
171         if (page->index == 0) {
172                 invalidate_inode_pages(inode->i_mapping);
173                 NFS_I(inode)->readdir_timestamp = timestamp;
174         }
175         unlock_page(page);
176         return 0;
177  error:
178         SetPageError(page);
179         unlock_page(page);
180         nfs_zap_caches(inode);
181         desc->error = error;
182         return -EIO;
183 }
184
185 static inline
186 int dir_decode(nfs_readdir_descriptor_t *desc)
187 {
188         u32     *p = desc->ptr;
189         p = desc->decode(p, desc->entry, desc->plus);
190         if (IS_ERR(p))
191                 return PTR_ERR(p);
192         desc->ptr = p;
193         return 0;
194 }
195
196 static inline
197 void dir_page_release(nfs_readdir_descriptor_t *desc)
198 {
199         kunmap(desc->page);
200         page_cache_release(desc->page);
201         desc->page = NULL;
202         desc->ptr = NULL;
203 }
204
205 /*
206  * Given a pointer to a buffer that has already been filled by a call
207  * to readdir, find the next entry.
208  *
209  * If the end of the buffer has been reached, return -EAGAIN, if not,
210  * return the offset within the buffer of the next entry to be
211  * read.
212  */
213 static inline
214 int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
215 {
216         struct nfs_entry *entry = desc->entry;
217         int             loop_count = 0,
218                         status;
219
220         while((status = dir_decode(desc)) == 0) {
221                 dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
222                 if (entry->prev_cookie == desc->target)
223                         break;
224                 if (loop_count++ > 200) {
225                         loop_count = 0;
226                         schedule();
227                 }
228         }
229         dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
230         return status;
231 }
232
233 /*
234  * Find the given page, and call find_dirent() in order to try to
235  * return the next entry.
236  */
237 static inline
238 int find_dirent_page(nfs_readdir_descriptor_t *desc)
239 {
240         struct inode    *inode = desc->file->f_dentry->d_inode;
241         struct page     *page;
242         int             status;
243
244         dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
245
246         page = read_cache_page(inode->i_mapping, desc->page_index,
247                                (filler_t *)nfs_readdir_filler, desc);
248         if (IS_ERR(page)) {
249                 status = PTR_ERR(page);
250                 goto out;
251         }
252         if (!PageUptodate(page))
253                 goto read_error;
254
255         /* NOTE: Someone else may have changed the READDIRPLUS flag */
256         desc->page = page;
257         desc->ptr = kmap(page);         /* matching kunmap in nfs_do_filldir */
258         status = find_dirent(desc, page);
259         if (status < 0)
260                 dir_page_release(desc);
261  out:
262         dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
263         return status;
264  read_error:
265         page_cache_release(page);
266         return -EIO;
267 }
268
269 /*
270  * Recurse through the page cache pages, and return a
271  * filled nfs_entry structure of the next directory entry if possible.
272  *
273  * The target for the search is 'desc->target'.
274  */
275 static inline
276 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
277 {
278         int             loop_count = 0;
279         int             res;
280
281         dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
282         for (;;) {
283                 res = find_dirent_page(desc);
284                 if (res != -EAGAIN)
285                         break;
286                 /* Align to beginning of next page */
287                 desc->page_index ++;
288                 if (loop_count++ > 200) {
289                         loop_count = 0;
290                         schedule();
291                 }
292         }
293         dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
294         return res;
295 }
296
297 static unsigned int nfs_type2dtype[] = {
298         DT_UNKNOWN,
299         DT_REG,
300         DT_DIR,
301         DT_BLK,
302         DT_CHR,
303         DT_LNK,
304         DT_SOCK,
305         DT_UNKNOWN,
306         DT_FIFO
307 };
308
309 static inline
310 unsigned int nfs_type_to_d_type(enum nfs_ftype type)
311 {
312         return nfs_type2dtype[type];
313 }
314
315 /*
316  * Once we've found the start of the dirent within a page: fill 'er up...
317  */
318 static 
319 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
320                    filldir_t filldir)
321 {
322         struct file     *file = desc->file;
323         struct nfs_entry *entry = desc->entry;
324         unsigned long   fileid;
325         int             loop_count = 0,
326                         res;
327
328         dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
329
330         for(;;) {
331                 unsigned d_type = DT_UNKNOWN;
332                 /* Note: entry->prev_cookie contains the cookie for
333                  *       retrieving the current dirent on the server */
334                 fileid = nfs_fileid_to_ino_t(entry->ino);
335
336                 /* Use readdirplus info */
337                 if (desc->plus && (entry->fattr->valid & NFS_ATTR_FATTR))
338                         d_type = nfs_type_to_d_type(entry->fattr->type);
339
340                 res = filldir(dirent, entry->name, entry->len, 
341                               entry->prev_cookie, fileid, d_type);
342                 if (res < 0)
343                         break;
344                 file->f_pos = desc->target = entry->cookie;
345                 if (dir_decode(desc) != 0) {
346                         desc->page_index ++;
347                         break;
348                 }
349                 if (loop_count++ > 200) {
350                         loop_count = 0;
351                         schedule();
352                 }
353         }
354         dir_page_release(desc);
355
356         dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
357         return res;
358 }
359
360 /*
361  * If we cannot find a cookie in our cache, we suspect that this is
362  * because it points to a deleted file, so we ask the server to return
363  * whatever it thinks is the next entry. We then feed this to filldir.
364  * If all goes well, we should then be able to find our way round the
365  * cache on the next call to readdir_search_pagecache();
366  *
367  * NOTE: we cannot add the anonymous page to the pagecache because
368  *       the data it contains might not be page aligned. Besides,
369  *       we should already have a complete representation of the
370  *       directory in the page cache by the time we get here.
371  */
372 static inline
373 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
374                      filldir_t filldir)
375 {
376         struct file     *file = desc->file;
377         struct inode    *inode = file->f_dentry->d_inode;
378         struct rpc_cred *cred = nfs_file_cred(file);
379         struct page     *page = NULL;
380         int             status;
381
382         dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
383
384         page = alloc_page(GFP_HIGHUSER);
385         if (!page) {
386                 status = -ENOMEM;
387                 goto out;
388         }
389         desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target,
390                                                 page,
391                                                 NFS_SERVER(inode)->dtsize,
392                                                 desc->plus);
393         NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
394         desc->page = page;
395         desc->ptr = kmap(page);         /* matching kunmap in nfs_do_filldir */
396         if (desc->error >= 0) {
397                 if ((status = dir_decode(desc)) == 0)
398                         desc->entry->prev_cookie = desc->target;
399         } else
400                 status = -EIO;
401         if (status < 0)
402                 goto out_release;
403
404         status = nfs_do_filldir(desc, dirent, filldir);
405
406         /* Reset read descriptor so it searches the page cache from
407          * the start upon the next call to readdir_search_pagecache() */
408         desc->page_index = 0;
409         desc->entry->cookie = desc->entry->prev_cookie = 0;
410         desc->entry->eof = 0;
411  out:
412         dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
413         return status;
414  out_release:
415         dir_page_release(desc);
416         goto out;
417 }
418
419 /* The file offset position is now represented as a true offset into the
420  * page cache as is the case in most of the other filesystems.
421  */
422 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
423 {
424         struct dentry   *dentry = filp->f_dentry;
425         struct inode    *inode = dentry->d_inode;
426         nfs_readdir_descriptor_t my_desc,
427                         *desc = &my_desc;
428         struct nfs_entry my_entry;
429         struct nfs_fh    fh;
430         struct nfs_fattr fattr;
431         long            res;
432
433         lock_kernel();
434
435         res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
436         if (res < 0) {
437                 unlock_kernel();
438                 return res;
439         }
440
441         /*
442          * filp->f_pos points to the file offset in the page cache.
443          * but if the cache has meanwhile been zapped, we need to
444          * read from the last dirent to revalidate f_pos
445          * itself.
446          */
447         memset(desc, 0, sizeof(*desc));
448
449         desc->file = filp;
450         desc->target = filp->f_pos;
451         desc->decode = NFS_PROTO(inode)->decode_dirent;
452         desc->plus = NFS_USE_READDIRPLUS(inode);
453
454         my_entry.cookie = my_entry.prev_cookie = 0;
455         my_entry.eof = 0;
456         my_entry.fh = &fh;
457         my_entry.fattr = &fattr;
458         desc->entry = &my_entry;
459
460         while(!desc->entry->eof) {
461                 res = readdir_search_pagecache(desc);
462                 if (res == -EBADCOOKIE) {
463                         /* This means either end of directory */
464                         if (desc->entry->cookie != desc->target) {
465                                 /* Or that the server has 'lost' a cookie */
466                                 res = uncached_readdir(desc, dirent, filldir);
467                                 if (res >= 0)
468                                         continue;
469                         }
470                         res = 0;
471                         break;
472                 }
473                 if (res == -ETOOSMALL && desc->plus) {
474                         NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
475                         nfs_zap_caches(inode);
476                         desc->plus = 0;
477                         desc->entry->eof = 0;
478                         continue;
479                 }
480                 if (res < 0)
481                         break;
482
483                 res = nfs_do_filldir(desc, dirent, filldir);
484                 if (res < 0) {
485                         res = 0;
486                         break;
487                 }
488         }
489         unlock_kernel();
490         if (desc->error < 0)
491                 return desc->error;
492         if (res < 0)
493                 return res;
494         return 0;
495 }
496
497 /*
498  * All directory operations under NFS are synchronous, so fsync()
499  * is a dummy operation.
500  */
501 int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
502 {
503         return 0;
504 }
505
506 /*
507  * A check for whether or not the parent directory has changed.
508  * In the case it has, we assume that the dentries are untrustworthy
509  * and may need to be looked up again.
510  */
511 static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
512 {
513         if (IS_ROOT(dentry))
514                 return 1;
515         if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0
516                         || nfs_attribute_timeout(dir))
517                 return 0;
518         return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
519 }
520
521 static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
522 {
523         dentry->d_fsdata = (void *)verf;
524 }
525
526 /*
527  * Whenever an NFS operation succeeds, we know that the dentry
528  * is valid, so we update the revalidation timestamp.
529  */
530 static inline void nfs_renew_times(struct dentry * dentry)
531 {
532         dentry->d_time = jiffies;
533 }
534
535 static inline
536 int nfs_lookup_verify_inode(struct inode *inode, int isopen)
537 {
538         struct nfs_server *server = NFS_SERVER(inode);
539
540         if (isopen && !(server->flags & NFS_MOUNT_NOCTO))
541                 return __nfs_revalidate_inode(server, inode);
542         return nfs_revalidate_inode(server, inode);
543 }
544
545 /*
546  * We judge how long we want to trust negative
547  * dentries by looking at the parent inode mtime.
548  *
549  * If parent mtime has changed, we revalidate, else we wait for a
550  * period corresponding to the parent's attribute cache timeout value.
551  */
552 static inline
553 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
554                        struct nameidata *nd)
555 {
556         int ndflags = 0;
557
558         if (nd)
559                 ndflags = nd->flags;
560         /* Don't revalidate a negative dentry if we're creating a new file */
561         if ((ndflags & LOOKUP_CREATE) && !(ndflags & LOOKUP_CONTINUE))
562                 return 0;
563         return !nfs_check_verifier(dir, dentry);
564 }
565
566 /*
567  * This is called every time the dcache has a lookup hit,
568  * and we should check whether we can really trust that
569  * lookup.
570  *
571  * NOTE! The hit can be a negative hit too, don't assume
572  * we have an inode!
573  *
574  * If the parent directory is seen to have changed, we throw out the
575  * cached dentry and do a new lookup.
576  */
577 static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
578 {
579         struct inode *dir;
580         struct inode *inode;
581         struct dentry *parent;
582         int error;
583         struct nfs_fh fhandle;
584         struct nfs_fattr fattr;
585         unsigned long verifier;
586         int isopen = 0;
587
588         parent = dget_parent(dentry);
589         lock_kernel();
590         dir = parent->d_inode;
591         inode = dentry->d_inode;
592
593         if (nd && !(nd->flags & LOOKUP_CONTINUE) && (nd->flags & LOOKUP_OPEN))
594                 isopen = 1;
595
596         if (!inode) {
597                 if (nfs_neg_need_reval(dir, dentry, nd))
598                         goto out_bad;
599                 goto out_valid;
600         }
601
602         if (is_bad_inode(inode)) {
603                 dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
604                         dentry->d_parent->d_name.name, dentry->d_name.name);
605                 goto out_bad;
606         }
607
608         /* Revalidate parent directory attribute cache */
609         nfs_revalidate_inode(NFS_SERVER(dir), dir);
610
611         /* Force a full look up iff the parent directory has changed */
612         if (nfs_check_verifier(dir, dentry)) {
613                 if (nfs_lookup_verify_inode(inode, isopen))
614                         goto out_zap_parent;
615                 goto out_valid;
616         }
617
618         /*
619          * Note: we're not holding inode->i_sem and so may be racing with
620          * operations that change the directory. We therefore save the
621          * change attribute *before* we do the RPC call.
622          */
623         verifier = nfs_save_change_attribute(dir);
624         error = nfs_cached_lookup(dir, dentry, &fhandle, &fattr);
625         if (!error) {
626                 if (nfs_compare_fh(NFS_FH(inode), &fhandle))
627                         goto out_bad;
628                 if (nfs_lookup_verify_inode(inode, isopen))
629                         goto out_zap_parent;
630                 goto out_valid_renew;
631         }
632
633         if (NFS_STALE(inode))
634                 goto out_bad;
635
636         error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
637         if (error)
638                 goto out_bad;
639         if (nfs_compare_fh(NFS_FH(inode), &fhandle))
640                 goto out_bad;
641         if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
642                 goto out_bad;
643
644  out_valid_renew:
645         nfs_renew_times(dentry);
646         nfs_set_verifier(dentry, verifier);
647  out_valid:
648         unlock_kernel();
649         dput(parent);
650         return 1;
651 out_zap_parent:
652         nfs_zap_caches(dir);
653  out_bad:
654         NFS_CACHEINV(dir);
655         if (inode && S_ISDIR(inode->i_mode)) {
656                 /* Purge readdir caches. */
657                 nfs_zap_caches(inode);
658                 /* If we have submounts, don't unhash ! */
659                 if (have_submounts(dentry))
660                         goto out_valid;
661                 shrink_dcache_parent(dentry);
662         }
663         d_drop(dentry);
664         unlock_kernel();
665         dput(parent);
666         return 0;
667 }
668
669 /*
670  * This is called from dput() when d_count is going to 0.
671  */
672 static int nfs_dentry_delete(struct dentry *dentry)
673 {
674         dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
675                 dentry->d_parent->d_name.name, dentry->d_name.name,
676                 dentry->d_flags);
677
678         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
679                 /* Unhash it, so that ->d_iput() would be called */
680                 return 1;
681         }
682         if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
683                 /* Unhash it, so that ancestors of killed async unlink
684                  * files will be cleaned up during umount */
685                 return 1;
686         }
687         return 0;
688
689 }
690
691 /*
692  * Called when the dentry loses inode.
693  * We use it to clean up silly-renamed files.
694  */
695 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
696 {
697         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
698                 lock_kernel();
699                 inode->i_nlink--;
700                 nfs_complete_unlink(dentry);
701                 unlock_kernel();
702         }
703         /* When creating a negative dentry, we want to renew d_time */
704         nfs_renew_times(dentry);
705         iput(inode);
706 }
707
708 struct dentry_operations nfs_dentry_operations = {
709         .d_revalidate   = nfs_lookup_revalidate,
710         .d_delete       = nfs_dentry_delete,
711         .d_iput         = nfs_dentry_iput,
712 };
713
714 static inline
715 int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
716 {
717         if (NFS_PROTO(dir)->version == 2)
718                 return 0;
719         if (!nd || (nd->flags & LOOKUP_CONTINUE) || !(nd->flags & LOOKUP_CREATE))
720                 return 0;
721         return (nd->intent.open.flags & O_EXCL) != 0;
722 }
723
724 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
725 {
726         struct inode *inode = NULL;
727         int error;
728         struct nfs_fh fhandle;
729         struct nfs_fattr fattr;
730
731         dfprintk(VFS, "NFS: lookup(%s/%s)\n",
732                 dentry->d_parent->d_name.name, dentry->d_name.name);
733
734         error = -ENAMETOOLONG;
735         if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
736                 goto out;
737
738         error = -ENOMEM;
739         dentry->d_op = NFS_PROTO(dir)->dentry_ops;
740
741         lock_kernel();
742         /* Revalidate parent directory attribute cache */
743         nfs_revalidate_inode(NFS_SERVER(dir), dir);
744
745         /* If we're doing an exclusive create, optimize away the lookup */
746         if (nfs_is_exclusive_create(dir, nd))
747                 goto no_entry;
748
749         error = nfs_cached_lookup(dir, dentry, &fhandle, &fattr);
750         if (error != 0) {
751                 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name,
752                                 &fhandle, &fattr);
753                 if (error == -ENOENT)
754                         goto no_entry;
755                 if (error != 0)
756                         goto out_unlock;
757         }
758         error = -EACCES;
759         inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
760         if (!inode)
761                 goto out_unlock;
762 no_entry:
763         error = 0;
764         d_add(dentry, inode);
765         nfs_renew_times(dentry);
766         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
767 out_unlock:
768         unlock_kernel();
769 out:
770         BUG_ON(error > 0);
771         return ERR_PTR(error);
772 }
773
774 #ifdef CONFIG_NFS_V4
775 static int nfs_open_revalidate(struct dentry *, struct nameidata *);
776
777 struct dentry_operations nfs4_dentry_operations = {
778         .d_revalidate   = nfs_open_revalidate,
779         .d_delete       = nfs_dentry_delete,
780         .d_iput         = nfs_dentry_iput,
781 };
782
783 static int is_atomic_open(struct inode *dir, struct nameidata *nd)
784 {
785         if (!nd)
786                 return 0;
787         /* Check that we are indeed trying to open this file */
788         if ((nd->flags & LOOKUP_CONTINUE) || !(nd->flags & LOOKUP_OPEN))
789                 return 0;
790         /* NFS does not (yet) have a stateful open for directories */
791         if (nd->flags & LOOKUP_DIRECTORY)
792                 return 0;
793         /* Are we trying to write to a read only partition? */
794         if ((IS_RDONLY(dir) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
795                 (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
796                 return 0;
797         return 1;
798 }
799
800 static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
801 {
802         struct inode *inode = NULL;
803         int error = 0;
804
805         /* Check that we are indeed trying to open this file */
806         if (!is_atomic_open(dir, nd))
807                 goto no_open;
808
809         if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
810                 error = -ENAMETOOLONG;
811                 goto out;
812         }
813         dentry->d_op = NFS_PROTO(dir)->dentry_ops;
814
815         /* Let vfs_create() deal with O_EXCL */
816         if (nd->intent.open.flags & O_EXCL)
817                 goto no_entry;
818
819         /* Open the file on the server */
820         lock_kernel();
821         /* Revalidate parent directory attribute cache */
822         nfs_revalidate_inode(NFS_SERVER(dir), dir);
823
824         if (nd->intent.open.flags & O_CREAT) {
825                 nfs_begin_data_update(dir);
826                 inode = nfs4_atomic_open(dir, dentry, nd);
827                 nfs_end_data_update(dir);
828         } else
829                 inode = nfs4_atomic_open(dir, dentry, nd);
830         unlock_kernel();
831         if (IS_ERR(inode)) {
832                 error = PTR_ERR(inode);
833                 switch (error) {
834                         /* Make a negative dentry */
835                         case -ENOENT:
836                                 inode = NULL;
837                                 break;
838                         /* This turned out not to be a regular file */
839                         case -ELOOP:
840                                 if (!(nd->intent.open.flags & O_NOFOLLOW))
841                                         goto no_open;
842                         /* case -EISDIR: */
843                         /* case -EINVAL: */
844                         default:
845                                 goto out;
846                 }
847         }
848 no_entry:
849         d_add(dentry, inode);
850         nfs_renew_times(dentry);
851         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
852 out:
853         BUG_ON(error > 0);
854         return ERR_PTR(error);
855 no_open:
856         return nfs_lookup(dir, dentry, nd);
857 }
858
859 static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
860 {
861         struct dentry *parent = NULL;
862         struct inode *inode = dentry->d_inode;
863         struct inode *dir;
864         unsigned long verifier;
865         int openflags, ret = 0;
866
867         parent = dget_parent(dentry);
868         dir = parent->d_inode;
869         if (!is_atomic_open(dir, nd))
870                 goto no_open;
871         /* We can't create new files in nfs_open_revalidate(), so we
872          * optimize away revalidation of negative dentries.
873          */
874         if (inode == NULL)
875                 goto out;
876         /* NFS only supports OPEN on regular files */
877         if (!S_ISREG(inode->i_mode))
878                 goto no_open;
879         openflags = nd->intent.open.flags;
880         /* We cannot do exclusive creation on a positive dentry */
881         if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
882                 goto no_open;
883         /* We can't create new files, or truncate existing ones here */
884         openflags &= ~(O_CREAT|O_TRUNC);
885
886         /*
887          * Note: we're not holding inode->i_sem and so may be racing with
888          * operations that change the directory. We therefore save the
889          * change attribute *before* we do the RPC call.
890          */
891         lock_kernel();
892         verifier = nfs_save_change_attribute(dir);
893         ret = nfs4_open_revalidate(dir, dentry, openflags);
894         if (!ret)
895                 nfs_set_verifier(dentry, verifier);
896         unlock_kernel();
897 out:
898         dput(parent);
899         if (!ret)
900                 d_drop(dentry);
901         return ret;
902 no_open:
903         dput(parent);
904         if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
905                 return 1;
906         return nfs_lookup_revalidate(dentry, nd);
907 }
908 #endif /* CONFIG_NFSV4 */
909
910 static inline
911 int find_dirent_name(nfs_readdir_descriptor_t *desc, struct page *page, struct dentry *dentry)
912 {
913         struct nfs_entry *entry = desc->entry;
914         int              status;
915
916         while((status = dir_decode(desc)) == 0) {
917                 if (entry->len != dentry->d_name.len)
918                         continue;
919                 if (memcmp(entry->name, dentry->d_name.name, entry->len))
920                         continue;
921                 if (!(entry->fattr->valid & NFS_ATTR_FATTR))
922                         continue;
923                 break;
924         }
925         return status;
926 }
927
928 /*
929  * Use the cached Readdirplus results in order to avoid a LOOKUP call
930  * whenever we believe that the parent directory has not changed.
931  *
932  * We assume that any file creation/rename changes the directory mtime.
933  * As this results in a page cache invalidation whenever it occurs,
934  * we don't require any other tests for cache coherency.
935  */
936 static
937 int nfs_cached_lookup(struct inode *dir, struct dentry *dentry,
938                         struct nfs_fh *fh, struct nfs_fattr *fattr)
939 {
940         nfs_readdir_descriptor_t desc;
941         struct nfs_server *server;
942         struct nfs_entry entry;
943         struct page *page;
944         unsigned long timestamp;
945         int res;
946
947         if (!NFS_USE_READDIRPLUS(dir))
948                 return -ENOENT;
949         server = NFS_SERVER(dir);
950         /* Don't use readdirplus unless the cache is stable */
951         if ((server->flags & NFS_MOUNT_NOAC) != 0
952                         || nfs_caches_unstable(dir)
953                         || nfs_attribute_timeout(dir))
954                 return -ENOENT;
955         if ((NFS_FLAGS(dir) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) != 0)
956                 return -ENOENT;
957         timestamp = NFS_I(dir)->readdir_timestamp;
958
959         entry.fh = fh;
960         entry.fattr = fattr;
961
962         desc.decode = NFS_PROTO(dir)->decode_dirent;
963         desc.entry = &entry;
964         desc.page_index = 0;
965         desc.plus = 1;
966
967         for(;(page = find_get_page(dir->i_mapping, desc.page_index)); desc.page_index++) {
968
969                 res = -EIO;
970                 if (PageUptodate(page)) {
971                         void * kaddr = kmap_atomic(page, KM_USER0);
972                         desc.ptr = kaddr;
973                         res = find_dirent_name(&desc, page, dentry);
974                         kunmap_atomic(kaddr, KM_USER0);
975                 }
976                 page_cache_release(page);
977
978                 if (res == 0)
979                         goto out_found;
980                 if (res != -EAGAIN)
981                         break;
982         }
983         return -ENOENT;
984  out_found:
985         fattr->timestamp = timestamp;
986         return 0;
987 }
988
989 /*
990  * Code common to create, mkdir, and mknod.
991  */
992 static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
993                                 struct nfs_fattr *fattr)
994 {
995         struct inode *inode;
996         int error = -EACCES;
997
998         /* We may have been initialized further down */
999         if (dentry->d_inode)
1000                 return 0;
1001         if (fhandle->size == 0) {
1002                 struct inode *dir = dentry->d_parent->d_inode;
1003                 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1004                 if (error)
1005                         goto out_err;
1006         }
1007         if (!(fattr->valid & NFS_ATTR_FATTR)) {
1008                 struct nfs_server *server = NFS_SB(dentry->d_sb);
1009                 error = server->rpc_ops->getattr(server, fhandle, fattr);
1010                 if (error < 0)
1011                         goto out_err;
1012         }
1013         inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1014         if (inode) {
1015                 d_instantiate(dentry, inode);
1016                 nfs_renew_times(dentry);
1017                 nfs_set_verifier(dentry, nfs_save_change_attribute(dentry->d_parent->d_inode));
1018                 return 0;
1019         }
1020         error = -ENOMEM;
1021 out_err:
1022         d_drop(dentry);
1023         return error;
1024 }
1025
1026 /*
1027  * Following a failed create operation, we drop the dentry rather
1028  * than retain a negative dentry. This avoids a problem in the event
1029  * that the operation succeeded on the server, but an error in the
1030  * reply path made it appear to have failed.
1031  */
1032 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1033                 struct nameidata *nd)
1034 {
1035         struct iattr attr;
1036         struct inode *inode;
1037         int error;
1038         int open_flags = 0;
1039
1040         dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id, 
1041                 dir->i_ino, dentry->d_name.name);
1042
1043         attr.ia_mode = mode;
1044         attr.ia_valid = ATTR_MODE;
1045
1046         if (nd && (nd->flags & LOOKUP_CREATE))
1047                 open_flags = nd->intent.open.flags;
1048
1049         /*
1050          * The 0 argument passed into the create function should one day
1051          * contain the O_EXCL flag if requested. This allows NFSv3 to
1052          * select the appropriate create strategy. Currently open_namei
1053          * does not pass the create flags.
1054          */
1055         lock_kernel();
1056         nfs_begin_data_update(dir);
1057         inode = NFS_PROTO(dir)->create(dir, &dentry->d_name, &attr, open_flags);
1058         nfs_end_data_update(dir);
1059         if (!IS_ERR(inode)) {
1060                 d_instantiate(dentry, inode);
1061                 nfs_renew_times(dentry);
1062                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1063                 error = 0;
1064         } else {
1065                 error = PTR_ERR(inode);
1066                 d_drop(dentry);
1067         }
1068         unlock_kernel();
1069         return error;
1070 }
1071
1072 /*
1073  * See comments for nfs_proc_create regarding failed operations.
1074  */
1075 static int
1076 nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1077 {
1078         struct iattr attr;
1079         struct nfs_fattr fattr;
1080         struct nfs_fh fhandle;
1081         int error;
1082
1083         dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id,
1084                 dir->i_ino, dentry->d_name.name);
1085
1086         if (!new_valid_dev(rdev))
1087                 return -EINVAL;
1088
1089         attr.ia_mode = mode;
1090         attr.ia_valid = ATTR_MODE;
1091
1092         lock_kernel();
1093         nfs_begin_data_update(dir);
1094         error = NFS_PROTO(dir)->mknod(dir, &dentry->d_name, &attr, rdev,
1095                                         &fhandle, &fattr);
1096         nfs_end_data_update(dir);
1097         if (!error)
1098                 error = nfs_instantiate(dentry, &fhandle, &fattr);
1099         else
1100                 d_drop(dentry);
1101         unlock_kernel();
1102         return error;
1103 }
1104
1105 /*
1106  * See comments for nfs_proc_create regarding failed operations.
1107  */
1108 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1109 {
1110         struct iattr attr;
1111         struct nfs_fattr fattr;
1112         struct nfs_fh fhandle;
1113         int error;
1114
1115         dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id,
1116                 dir->i_ino, dentry->d_name.name);
1117
1118         attr.ia_valid = ATTR_MODE;
1119         attr.ia_mode = mode | S_IFDIR;
1120
1121         lock_kernel();
1122 #if 0
1123         /*
1124          * Always drop the dentry, we can't always depend on
1125          * the fattr returned by the server (AIX seems to be
1126          * broken). We're better off doing another lookup than
1127          * depending on potentially bogus information.
1128          */
1129         d_drop(dentry);
1130 #endif
1131         nfs_begin_data_update(dir);
1132         error = NFS_PROTO(dir)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
1133                                         &fattr);
1134         nfs_end_data_update(dir);
1135         if (!error)
1136                 error = nfs_instantiate(dentry, &fhandle, &fattr);
1137         else
1138                 d_drop(dentry);
1139         unlock_kernel();
1140         return error;
1141 }
1142
1143 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1144 {
1145         int error;
1146
1147         dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id,
1148                 dir->i_ino, dentry->d_name.name);
1149
1150         lock_kernel();
1151         nfs_begin_data_update(dir);
1152         error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1153         /* Ensure the VFS deletes this inode */
1154         if (error == 0 && dentry->d_inode != NULL)
1155                 dentry->d_inode->i_nlink = 0;
1156         nfs_end_data_update(dir);
1157         unlock_kernel();
1158
1159         return error;
1160 }
1161
1162 static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
1163 {
1164         static unsigned int sillycounter;
1165         const int      i_inosize  = sizeof(dir->i_ino)*2;
1166         const int      countersize = sizeof(sillycounter)*2;
1167         const int      slen       = strlen(".nfs") + i_inosize + countersize;
1168         char           silly[slen+1];
1169         struct qstr    qsilly;
1170         struct dentry *sdentry;
1171         int            error = -EIO;
1172
1173         dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
1174                 dentry->d_parent->d_name.name, dentry->d_name.name, 
1175                 atomic_read(&dentry->d_count));
1176
1177 #ifdef NFS_PARANOIA
1178 if (!dentry->d_inode)
1179 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
1180 dentry->d_parent->d_name.name, dentry->d_name.name);
1181 #endif
1182         /*
1183          * We don't allow a dentry to be silly-renamed twice.
1184          */
1185         error = -EBUSY;
1186         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1187                 goto out;
1188
1189         sprintf(silly, ".nfs%*.*lx",
1190                 i_inosize, i_inosize, dentry->d_inode->i_ino);
1191
1192         sdentry = NULL;
1193         do {
1194                 char *suffix = silly + slen - countersize;
1195
1196                 dput(sdentry);
1197                 sillycounter++;
1198                 sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
1199
1200                 dfprintk(VFS, "trying to rename %s to %s\n",
1201                          dentry->d_name.name, silly);
1202                 
1203                 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
1204                 /*
1205                  * N.B. Better to return EBUSY here ... it could be
1206                  * dangerous to delete the file while it's in use.
1207                  */
1208                 if (IS_ERR(sdentry))
1209                         goto out;
1210         } while(sdentry->d_inode != NULL); /* need negative lookup */
1211
1212         qsilly.name = silly;
1213         qsilly.len  = strlen(silly);
1214         nfs_begin_data_update(dir);
1215         if (dentry->d_inode) {
1216                 nfs_begin_data_update(dentry->d_inode);
1217                 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1218                                 dir, &qsilly);
1219                 nfs_end_data_update(dentry->d_inode);
1220         } else
1221                 error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1222                                 dir, &qsilly);
1223         nfs_end_data_update(dir);
1224         if (!error) {
1225                 nfs_renew_times(dentry);
1226                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1227                 d_move(dentry, sdentry);
1228                 error = nfs_async_unlink(dentry);
1229                 /* If we return 0 we don't unlink */
1230         }
1231         dput(sdentry);
1232 out:
1233         return error;
1234 }
1235
1236 /*
1237  * Remove a file after making sure there are no pending writes,
1238  * and after checking that the file has only one user. 
1239  *
1240  * We invalidate the attribute cache and free the inode prior to the operation
1241  * to avoid possible races if the server reuses the inode.
1242  */
1243 static int nfs_safe_remove(struct dentry *dentry)
1244 {
1245         struct inode *dir = dentry->d_parent->d_inode;
1246         struct inode *inode = dentry->d_inode;
1247         int error = -EBUSY;
1248                 
1249         dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1250                 dentry->d_parent->d_name.name, dentry->d_name.name);
1251
1252         /* If the dentry was sillyrenamed, we simply call d_delete() */
1253         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1254                 error = 0;
1255                 goto out;
1256         }
1257
1258         nfs_begin_data_update(dir);
1259         if (inode != NULL) {
1260                 nfs_begin_data_update(inode);
1261                 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1262                 /* The VFS may want to delete this inode */
1263                 if (error == 0)
1264                         inode->i_nlink--;
1265                 nfs_end_data_update(inode);
1266         } else
1267                 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1268         nfs_end_data_update(dir);
1269 out:
1270         return error;
1271 }
1272
1273 /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
1274  *  belongs to an active ".nfs..." file and we return -EBUSY.
1275  *
1276  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
1277  */
1278 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1279 {
1280         int error;
1281         int need_rehash = 0;
1282
1283         dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1284                 dir->i_ino, dentry->d_name.name);
1285
1286         lock_kernel();
1287         spin_lock(&dcache_lock);
1288         spin_lock(&dentry->d_lock);
1289         if (atomic_read(&dentry->d_count) > 1) {
1290                 spin_unlock(&dentry->d_lock);
1291                 spin_unlock(&dcache_lock);
1292                 error = nfs_sillyrename(dir, dentry);
1293                 unlock_kernel();
1294                 return error;
1295         }
1296         if (!d_unhashed(dentry)) {
1297                 __d_drop(dentry);
1298                 need_rehash = 1;
1299         }
1300         spin_unlock(&dentry->d_lock);
1301         spin_unlock(&dcache_lock);
1302         error = nfs_safe_remove(dentry);
1303         if (!error) {
1304                 nfs_renew_times(dentry);
1305                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1306         } else if (need_rehash)
1307                 d_rehash(dentry);
1308         unlock_kernel();
1309         return error;
1310 }
1311
1312 static int
1313 nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1314 {
1315         struct iattr attr;
1316         struct nfs_fattr sym_attr;
1317         struct nfs_fh sym_fh;
1318         struct qstr qsymname;
1319         int error;
1320
1321         dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1322                 dir->i_ino, dentry->d_name.name, symname);
1323
1324 #ifdef NFS_PARANOIA
1325 if (dentry->d_inode)
1326 printk("nfs_proc_symlink: %s/%s not negative!\n",
1327 dentry->d_parent->d_name.name, dentry->d_name.name);
1328 #endif
1329         /*
1330          * Fill in the sattr for the call.
1331          * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1332          */
1333         attr.ia_valid = ATTR_MODE;
1334         attr.ia_mode = S_IFLNK | S_IRWXUGO;
1335
1336         qsymname.name = symname;
1337         qsymname.len  = strlen(symname);
1338
1339         lock_kernel();
1340         nfs_begin_data_update(dir);
1341         error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
1342                                           &attr, &sym_fh, &sym_attr);
1343         nfs_end_data_update(dir);
1344         if (!error) {
1345                 error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
1346         } else {
1347                 if (error == -EEXIST)
1348                         printk("nfs_proc_symlink: %s/%s already exists??\n",
1349                                dentry->d_parent->d_name.name, dentry->d_name.name);
1350                 d_drop(dentry);
1351         }
1352         unlock_kernel();
1353         return error;
1354 }
1355
1356 static int 
1357 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1358 {
1359         struct inode *inode = old_dentry->d_inode;
1360         int error;
1361
1362         dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1363                 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1364                 dentry->d_parent->d_name.name, dentry->d_name.name);
1365
1366         /*
1367          * Drop the dentry in advance to force a new lookup.
1368          * Since nfs_proc_link doesn't return a file handle,
1369          * we can't use the existing dentry.
1370          */
1371         lock_kernel();
1372         d_drop(dentry);
1373
1374         nfs_begin_data_update(dir);
1375         nfs_begin_data_update(inode);
1376         error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1377         nfs_end_data_update(inode);
1378         nfs_end_data_update(dir);
1379         unlock_kernel();
1380         return error;
1381 }
1382
1383 /*
1384  * RENAME
1385  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1386  * different file handle for the same inode after a rename (e.g. when
1387  * moving to a different directory). A fail-safe method to do so would
1388  * be to look up old_dir/old_name, create a link to new_dir/new_name and
1389  * rename the old file using the sillyrename stuff. This way, the original
1390  * file in old_dir will go away when the last process iput()s the inode.
1391  *
1392  * FIXED.
1393  * 
1394  * It actually works quite well. One needs to have the possibility for
1395  * at least one ".nfs..." file in each directory the file ever gets
1396  * moved or linked to which happens automagically with the new
1397  * implementation that only depends on the dcache stuff instead of
1398  * using the inode layer
1399  *
1400  * Unfortunately, things are a little more complicated than indicated
1401  * above. For a cross-directory move, we want to make sure we can get
1402  * rid of the old inode after the operation.  This means there must be
1403  * no pending writes (if it's a file), and the use count must be 1.
1404  * If these conditions are met, we can drop the dentries before doing
1405  * the rename.
1406  */
1407 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1408                       struct inode *new_dir, struct dentry *new_dentry)
1409 {
1410         struct inode *old_inode = old_dentry->d_inode;
1411         struct inode *new_inode = new_dentry->d_inode;
1412         struct dentry *dentry = NULL, *rehash = NULL;
1413         int error = -EBUSY;
1414
1415         /*
1416          * To prevent any new references to the target during the rename,
1417          * we unhash the dentry and free the inode in advance.
1418          */
1419         lock_kernel();
1420         if (!d_unhashed(new_dentry)) {
1421                 d_drop(new_dentry);
1422                 rehash = new_dentry;
1423         }
1424
1425         dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1426                  old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1427                  new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1428                  atomic_read(&new_dentry->d_count));
1429
1430         /*
1431          * First check whether the target is busy ... we can't
1432          * safely do _any_ rename if the target is in use.
1433          *
1434          * For files, make a copy of the dentry and then do a 
1435          * silly-rename. If the silly-rename succeeds, the
1436          * copied dentry is hashed and becomes the new target.
1437          */
1438         if (!new_inode)
1439                 goto go_ahead;
1440         if (S_ISDIR(new_inode->i_mode))
1441                 goto out;
1442         else if (atomic_read(&new_dentry->d_count) > 2) {
1443                 int err;
1444                 /* copy the target dentry's name */
1445                 dentry = d_alloc(new_dentry->d_parent,
1446                                  &new_dentry->d_name);
1447                 if (!dentry)
1448                         goto out;
1449
1450                 /* silly-rename the existing target ... */
1451                 err = nfs_sillyrename(new_dir, new_dentry);
1452                 if (!err) {
1453                         new_dentry = rehash = dentry;
1454                         new_inode = NULL;
1455                         /* instantiate the replacement target */
1456                         d_instantiate(new_dentry, NULL);
1457                 } else if (atomic_read(&new_dentry->d_count) > 1) {
1458                 /* dentry still busy? */
1459 #ifdef NFS_PARANOIA
1460                         printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1461                                new_dentry->d_parent->d_name.name,
1462                                new_dentry->d_name.name,
1463                                atomic_read(&new_dentry->d_count));
1464 #endif
1465                         goto out;
1466                 }
1467         }
1468
1469 go_ahead:
1470         /*
1471          * ... prune child dentries and writebacks if needed.
1472          */
1473         if (atomic_read(&old_dentry->d_count) > 1) {
1474                 nfs_wb_all(old_inode);
1475                 shrink_dcache_parent(old_dentry);
1476         }
1477
1478         if (new_inode)
1479                 d_delete(new_dentry);
1480
1481         nfs_begin_data_update(old_dir);
1482         nfs_begin_data_update(new_dir);
1483         nfs_begin_data_update(old_inode);
1484         error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1485                                            new_dir, &new_dentry->d_name);
1486         nfs_end_data_update(old_inode);
1487         nfs_end_data_update(new_dir);
1488         nfs_end_data_update(old_dir);
1489 out:
1490         if (rehash)
1491                 d_rehash(rehash);
1492         if (!error) {
1493                 if (!S_ISDIR(old_inode->i_mode))
1494                         d_move(old_dentry, new_dentry);
1495                 nfs_renew_times(new_dentry);
1496                 nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
1497         }
1498
1499         /* new dentry created? */
1500         if (dentry)
1501                 dput(dentry);
1502         unlock_kernel();
1503         return error;
1504 }
1505
1506 int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
1507 {
1508         struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
1509
1510         if (cache->cred != cred
1511                         || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
1512                         || (NFS_FLAGS(inode) & NFS_INO_INVALID_ATTR))
1513                 return -ENOENT;
1514         memcpy(res, cache, sizeof(*res));
1515         return 0;
1516 }
1517
1518 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1519 {
1520         struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
1521
1522         if (cache->cred != set->cred) {
1523                 if (cache->cred)
1524                         put_rpccred(cache->cred);
1525                 cache->cred = get_rpccred(set->cred);
1526         }
1527         cache->jiffies = set->jiffies;
1528         cache->mask = set->mask;
1529 }
1530
1531 static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
1532 {
1533         struct nfs_access_entry cache;
1534         int status;
1535
1536         status = nfs_access_get_cached(inode, cred, &cache);
1537         if (status == 0)
1538                 goto out;
1539
1540         /* Be clever: ask server to check for all possible rights */
1541         cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
1542         cache.cred = cred;
1543         cache.jiffies = jiffies;
1544         status = NFS_PROTO(inode)->access(inode, &cache);
1545         if (status != 0)
1546                 return status;
1547         nfs_access_add_cache(inode, &cache);
1548 out:
1549         if ((cache.mask & mask) == mask)
1550                 return 0;
1551         return -EACCES;
1552 }
1553
1554 int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1555 {
1556         struct rpc_cred *cred;
1557         int mode = inode->i_mode;
1558         int res;
1559
1560         if (mask == 0)
1561                 return 0;
1562         if (mask & MAY_WRITE) {
1563                 /*
1564                  *
1565                  * Nobody gets write access to a read-only fs.
1566                  *
1567                  */
1568                 if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
1569                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1570                         return -EROFS;
1571
1572                 /*
1573                  *
1574                  * Nobody gets write access to an immutable file.
1575                  *
1576                  */
1577                 if (IS_IMMUTABLE(inode))
1578                         return -EACCES;
1579         }
1580         /* Are we checking permissions on anything other than lookup/execute? */
1581         if ((mask & MAY_EXEC) == 0) {
1582                 /* We only need to check permissions on file open() and access() */
1583                 if (!nd || !(nd->flags & (LOOKUP_OPEN|LOOKUP_ACCESS)))
1584                         return 0;
1585                 /* NFSv4 has atomic_open... */
1586                 if (NFS_PROTO(inode)->version > 3 && (nd->flags & LOOKUP_OPEN))
1587                         return 0;
1588         }
1589
1590         lock_kernel();
1591
1592         if (!NFS_PROTO(inode)->access)
1593                 goto out_notsup;
1594
1595         cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1596         res = nfs_do_access(inode, cred, mask);
1597         put_rpccred(cred);
1598         unlock_kernel();
1599         return res;
1600 out_notsup:
1601         nfs_revalidate_inode(NFS_SERVER(inode), inode);
1602         res = generic_permission(inode, mask, NULL);
1603         unlock_kernel();
1604         return res;
1605 }
1606
1607 /*
1608  * Local variables:
1609  *  version-control: t
1610  *  kept-new-versions: 5
1611  * End:
1612  */