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