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