4 * Copyright (C) 1992 Rick Sladkey
6 * nfs directory handling functions
8 * 10 Apr 1996 Added silly rename for unlink --okir
9 * 28 Sep 1996 Improved directory cache --okir
10 * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
11 * Re-implemented silly rename for unlink, newly implemented
12 * silly rename for nfs_rename() following the suggestions
13 * of Olaf Kirch (okir) found in this file.
14 * Following Linus comments on my original hack, this version
15 * depends only on the dcache stuff and doesn't touch the inode
16 * layer (iput() and friends).
17 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/malloc.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/pagemap.h>
32 #include <linux/smp_lock.h>
34 #define NFS_PARANOIA 1
35 /* #define NFS_DEBUG_VERBOSE 1 */
37 static int nfs_readdir(struct file
*, void *, filldir_t
);
38 static struct dentry
*nfs_lookup(struct inode
*, struct dentry
*);
39 static int nfs_create(struct inode
*, struct dentry
*, int);
40 static int nfs_mkdir(struct inode
*, struct dentry
*, int);
41 static int nfs_rmdir(struct inode
*, struct dentry
*);
42 static int nfs_unlink(struct inode
*, struct dentry
*);
43 static int nfs_symlink(struct inode
*, struct dentry
*, const char *);
44 static int nfs_link(struct dentry
*, struct inode
*, struct dentry
*);
45 static int nfs_mknod(struct inode
*, struct dentry
*, int, int);
46 static int nfs_rename(struct inode
*, struct dentry
*,
47 struct inode
*, struct dentry
*);
49 struct file_operations nfs_dir_operations
= {
50 read
: generic_read_dir
,
56 struct inode_operations nfs_dir_inode_operations
= {
66 permission
: nfs_permission
,
67 revalidate
: nfs_revalidate
,
68 setattr
: nfs_notify_change
,
71 typedef u32
* (*decode_dirent_t
)(u32
*, struct nfs_entry
*, int);
75 unsigned long page_index
;
78 struct nfs_entry
*entry
;
79 decode_dirent_t decode
;
82 } nfs_readdir_descriptor_t
;
84 /* Now we cache directories properly, by stuffing the dirent
85 * data directly in the page cache.
87 * Inode invalidation due to refresh etc. takes care of
88 * _everything_, no sloppy entry flushing logic, no extraneous
89 * copying, network direct to page cache, the way it was meant
92 * NOTE: Dirent information verification is done always by the
93 * page-in of the RPC reply, nowhere else, this simplies
94 * things substantially.
97 int nfs_readdir_filler(nfs_readdir_descriptor_t
*desc
, struct page
*page
)
99 struct file
*file
= desc
->file
;
100 struct inode
*inode
= file
->f_dentry
->d_inode
;
101 struct rpc_cred
*cred
= nfs_file_cred(file
);
102 void *buffer
= kmap(page
);
103 int plus
= NFS_USE_READDIRPLUS(inode
);
106 dfprintk(VFS
, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc
->entry
->cookie
, page
->index
);
109 error
= NFS_PROTO(inode
)->readdir(inode
, cred
, desc
->entry
->cookie
, buffer
,
110 NFS_SERVER(inode
)->dtsize
, plus
);
111 /* We requested READDIRPLUS, but the server doesn't grok it */
112 if (desc
->plus
&& error
== -ENOTSUPP
) {
113 NFS_FLAGS(inode
) &= ~NFS_INO_ADVISE_RDPLUS
;
119 SetPageUptodate(page
);
121 /* Ensure consistent page alignment of the data.
122 * Note: assumes we have exclusive access to this mapping either
123 * throught inode->i_sem or some other mechanism.
125 if (page
->index
== 0)
126 invalidate_inode_pages(inode
);
133 invalidate_inode_pages(inode
);
139 * Given a pointer to a buffer that has already been filled by a call
140 * to readdir, find the next entry.
142 * If the end of the buffer has been reached, return -EAGAIN, if not,
143 * return the offset within the buffer of the next entry to be
147 int find_dirent(nfs_readdir_descriptor_t
*desc
, struct page
*page
)
149 struct nfs_entry
*entry
= desc
->entry
;
150 char *start
= kmap(page
),
156 p
= (char *)desc
->decode((u32
*)p
, entry
, desc
->plus
);
161 desc
->page_offset
= p
- start
;
162 dfprintk(VFS
, "NFS: found cookie %Lu\n", (long long)entry
->cookie
);
163 if (entry
->prev_cookie
== desc
->target
)
165 if (loop_count
++ > 200) {
171 dfprintk(VFS
, "NFS: find_dirent() returns %d\n", status
);
176 * Find the given page, and call find_dirent() in order to try to
177 * return the next entry.
180 int find_dirent_page(nfs_readdir_descriptor_t
*desc
)
182 struct inode
*inode
= desc
->file
->f_dentry
->d_inode
;
184 unsigned long index
= desc
->page_index
;
187 dfprintk(VFS
, "NFS: find_dirent_page() searching directory page %ld\n", desc
->page_index
);
190 page_cache_release(desc
->page
);
194 page
= read_cache_page(&inode
->i_data
, index
,
195 (filler_t
*)nfs_readdir_filler
, desc
);
197 status
= PTR_ERR(page
);
200 if (!Page_Uptodate(page
))
203 /* NOTE: Someone else may have changed the READDIRPLUS flag */
204 desc
->plus
= NFS_USE_READDIRPLUS(inode
);
205 status
= find_dirent(desc
, page
);
209 page_cache_release(page
);
211 dfprintk(VFS
, "NFS: find_dirent_page() returns %d\n", status
);
214 page_cache_release(page
);
219 * Recurse through the page cache pages, and return a
220 * filled nfs_entry structure of the next directory entry if possible.
222 * The target for the search is 'desc->target'.
225 int readdir_search_pagecache(nfs_readdir_descriptor_t
*desc
)
230 dfprintk(VFS
, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc
->target
);
232 res
= find_dirent_page(desc
);
235 /* Align to beginning of next page */
236 desc
->page_offset
= 0;
238 if (loop_count
++ > 200) {
243 dfprintk(VFS
, "NFS: readdir_search_pagecache() returned %d\n", res
);
248 * Once we've found the start of the dirent within a page: fill 'er up...
251 int nfs_do_filldir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
254 struct file
*file
= desc
->file
;
255 struct nfs_entry
*entry
= desc
->entry
;
256 char *start
= kmap(desc
->page
),
257 *p
= start
+ desc
->page_offset
;
258 unsigned long fileid
;
262 dfprintk(VFS
, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc
->target
);
265 /* Note: entry->prev_cookie contains the cookie for
266 * retrieving the current dirent on the server */
267 fileid
= nfs_fileid_to_ino_t(entry
->ino
);
268 res
= filldir(dirent
, entry
->name
, entry
->len
,
269 entry
->prev_cookie
, fileid
, DT_UNKNOWN
);
272 file
->f_pos
= desc
->target
= entry
->cookie
;
273 p
= (char *)desc
->decode((u32
*)p
, entry
, desc
->plus
);
275 if (PTR_ERR(p
) == -EAGAIN
) {
276 desc
->page_offset
= 0;
281 desc
->page_offset
= p
- start
;
282 if (loop_count
++ > 200) {
288 page_cache_release(desc
->page
);
291 dfprintk(VFS
, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc
->target
, res
);
296 * If we cannot find a cookie in our cache, we suspect that this is
297 * because it points to a deleted file, so we ask the server to return
298 * whatever it thinks is the next entry. We then feed this to filldir.
299 * If all goes well, we should then be able to find our way round the
300 * cache on the next call to readdir_search_pagecache();
302 * NOTE: we cannot add the anonymous page to the pagecache because
303 * the data it contains might not be page aligned. Besides,
304 * we should already have a complete representation of the
305 * directory in the page cache by the time we get here.
308 int uncached_readdir(nfs_readdir_descriptor_t
*desc
, void *dirent
,
311 struct file
*file
= desc
->file
;
312 struct inode
*inode
= file
->f_dentry
->d_inode
;
313 struct rpc_cred
*cred
= nfs_file_cred(file
);
314 struct page
*page
= NULL
;
318 dfprintk(VFS
, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc
->target
);
320 page_cache_release(desc
->page
);
324 page
= page_cache_alloc();
330 status
= NFS_PROTO(inode
)->readdir(inode
, cred
, desc
->target
, p
,
331 NFS_SERVER(inode
)->dtsize
, 0);
333 p
= desc
->decode(p
, desc
->entry
, 0);
337 desc
->entry
->prev_cookie
= desc
->target
;
343 desc
->page_index
= 0;
344 desc
->page_offset
= 0;
346 status
= nfs_do_filldir(desc
, dirent
, filldir
);
348 /* Reset read descriptor so it searches the page cache from
349 * the start upon the next call to readdir_search_pagecache() */
350 desc
->page_index
= 0;
351 desc
->page_offset
= 0;
352 memset(desc
->entry
, 0, sizeof(*desc
->entry
));
354 dfprintk(VFS
, "NFS: uncached_readdir() returns %d\n", status
);
357 page_cache_release(page
);
361 /* The file offset position is now represented as a true offset into the
362 * page cache as is the case in most of the other filesystems.
364 static int nfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
366 struct dentry
*dentry
= filp
->f_dentry
;
367 struct inode
*inode
= dentry
->d_inode
;
368 nfs_readdir_descriptor_t my_desc
,
370 struct nfs_entry my_entry
;
373 res
= nfs_revalidate(dentry
);
378 * filp->f_pos points to the file offset in the page cache.
379 * but if the cache has meanwhile been zapped, we need to
380 * read from the last dirent to revalidate f_pos
383 memset(desc
, 0, sizeof(*desc
));
384 memset(&my_entry
, 0, sizeof(my_entry
));
387 desc
->target
= filp
->f_pos
;
388 desc
->entry
= &my_entry
;
389 desc
->decode
= NFS_PROTO(inode
)->decode_dirent
;
391 while(!desc
->entry
->eof
) {
392 res
= readdir_search_pagecache(desc
);
393 if (res
== -EBADCOOKIE
) {
394 /* This means either end of directory */
395 if (desc
->entry
->cookie
== desc
->target
) {
399 /* Or that the server has 'lost' a cookie */
400 res
= uncached_readdir(desc
, dirent
, filldir
);
407 res
= nfs_do_filldir(desc
, dirent
, filldir
);
414 page_cache_release(desc
->page
);
423 * Whenever an NFS operation succeeds, we know that the dentry
424 * is valid, so we update the revalidation timestamp.
426 static inline void nfs_renew_times(struct dentry
* dentry
)
428 dentry
->d_time
= jiffies
;
431 static inline int nfs_dentry_force_reval(struct dentry
*dentry
, int flags
)
433 struct inode
*inode
= dentry
->d_inode
;
434 unsigned long timeout
= NFS_ATTRTIMEO(inode
);
437 * If it's the last lookup in a series, we use a stricter
438 * cache consistency check by looking at the parent mtime.
440 * If it's been modified in the last hour, be really strict.
441 * (This still means that we can avoid doing unnecessary
442 * work on directories like /usr/share/bin etc which basically
445 if (!(flags
& LOOKUP_CONTINUE
)) {
446 long diff
= CURRENT_TIME
- dentry
->d_parent
->d_inode
->i_mtime
;
452 return time_after(jiffies
,dentry
->d_time
+ timeout
);
456 * We judge how long we want to trust negative
457 * dentries by looking at the parent inode mtime.
459 * If mtime is close to present time, we revalidate
462 #define NFS_REVALIDATE_NEGATIVE (1 * HZ)
463 static inline int nfs_neg_need_reval(struct dentry
*dentry
)
465 struct inode
*dir
= dentry
->d_parent
->d_inode
;
466 unsigned long timeout
= NFS_ATTRTIMEO(dir
);
467 long diff
= CURRENT_TIME
- dir
->i_mtime
;
469 if (diff
< 5*60 && timeout
> NFS_REVALIDATE_NEGATIVE
)
470 timeout
= NFS_REVALIDATE_NEGATIVE
;
472 return time_after(jiffies
, dentry
->d_time
+ timeout
);
476 * This is called every time the dcache has a lookup hit,
477 * and we should check whether we can really trust that
480 * NOTE! The hit can be a negative hit too, don't assume
483 * If the dentry is older than the revalidation interval,
484 * we do a new lookup and verify that the dentry is still
487 static int nfs_lookup_revalidate(struct dentry
* dentry
, int flags
)
492 struct nfs_fh fhandle
;
493 struct nfs_fattr fattr
;
496 dir
= dentry
->d_parent
->d_inode
;
497 inode
= dentry
->d_inode
;
499 * If we don't have an inode, let's look at the parent
500 * directory mtime to get a hint about how often we
501 * should validate things..
504 if (nfs_neg_need_reval(dentry
))
509 if (is_bad_inode(inode
)) {
510 dfprintk(VFS
, "nfs_lookup_validate: %s/%s has dud inode\n",
511 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
515 if (!nfs_dentry_force_reval(dentry
, flags
))
518 if (IS_ROOT(dentry
)) {
519 __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
520 goto out_valid_renew
;
524 * Do a new lookup and check the dentry attributes.
526 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, &fhandle
, &fattr
);
530 /* Inode number matches? */
531 if (!(fattr
.valid
& NFS_ATTR_FATTR
) ||
532 NFS_FSID(inode
) != fattr
.fsid
||
533 NFS_FILEID(inode
) != fattr
.fileid
)
536 /* Ok, remember that we successfully checked it.. */
537 nfs_refresh_inode(inode
, &fattr
);
539 if (nfs_inode_is_stale(inode
, &fhandle
, &fattr
))
543 nfs_renew_times(dentry
);
548 shrink_dcache_parent(dentry
);
549 /* If we have submounts, don't unhash ! */
550 if (have_submounts(dentry
))
553 /* Purge readdir caches. */
555 if (inode
&& S_ISDIR(inode
->i_mode
))
556 nfs_zap_caches(inode
);
562 * This is called from dput() when d_count is going to 0.
564 static int nfs_dentry_delete(struct dentry
*dentry
)
566 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
567 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
570 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
571 /* Unhash it, so that ->d_iput() would be called */
579 * Called when the dentry loses inode.
580 * We use it to clean up silly-renamed files.
582 static void nfs_dentry_iput(struct dentry
*dentry
, struct inode
*inode
)
584 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
586 nfs_complete_unlink(dentry
);
592 struct dentry_operations nfs_dentry_operations
= {
593 d_revalidate
: nfs_lookup_revalidate
,
594 d_delete
: nfs_dentry_delete
,
595 d_iput
: nfs_dentry_iput
,
598 static struct dentry
*nfs_lookup(struct inode
*dir
, struct dentry
* dentry
)
602 struct nfs_fh fhandle
;
603 struct nfs_fattr fattr
;
605 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
606 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
608 error
= -ENAMETOOLONG
;
609 if (dentry
->d_name
.len
> NFS_SERVER(dir
)->namelen
)
613 dentry
->d_op
= &nfs_dentry_operations
;
615 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, &fhandle
, &fattr
);
617 if (error
== -ENOENT
)
621 inode
= nfs_fhget(dentry
, &fhandle
, &fattr
);
624 d_add(dentry
, inode
);
625 nfs_renew_times(dentry
);
630 return ERR_PTR(error
);
634 * Code common to create, mkdir, and mknod.
636 static int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
637 struct nfs_fattr
*fattr
)
642 inode
= nfs_fhget(dentry
, fhandle
, fattr
);
644 d_instantiate(dentry
, inode
);
645 nfs_renew_times(dentry
);
652 * Following a failed create operation, we drop the dentry rather
653 * than retain a negative dentry. This avoids a problem in the event
654 * that the operation succeeded on the server, but an error in the
655 * reply path made it appear to have failed.
657 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
660 struct nfs_fattr fattr
;
661 struct nfs_fh fhandle
;
664 dfprintk(VFS
, "NFS: create(%x/%ld, %s\n",
665 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
668 attr
.ia_valid
= ATTR_MODE
;
671 * The 0 argument passed into the create function should one day
672 * contain the O_EXCL flag if requested. This allows NFSv3 to
673 * select the appropriate create strategy. Currently open_namei
674 * does not pass the create flags.
677 error
= NFS_PROTO(dir
)->create(dir
, &dentry
->d_name
,
678 &attr
, 0, &fhandle
, &fattr
);
679 if (!error
&& fhandle
.size
!= 0)
680 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
681 if (error
|| fhandle
.size
== 0)
687 * See comments for nfs_proc_create regarding failed operations.
689 static int nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, int rdev
)
692 struct nfs_fattr fattr
;
693 struct nfs_fh fhandle
;
696 dfprintk(VFS
, "NFS: mknod(%x/%ld, %s\n",
697 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
700 attr
.ia_valid
= ATTR_MODE
;
703 error
= NFS_PROTO(dir
)->mknod(dir
, &dentry
->d_name
, &attr
, rdev
,
705 if (!error
&& fhandle
.size
!= 0)
706 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
707 if (error
|| fhandle
.size
== 0)
713 * See comments for nfs_proc_create regarding failed operations.
715 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
718 struct nfs_fattr fattr
;
719 struct nfs_fh fhandle
;
722 dfprintk(VFS
, "NFS: mkdir(%x/%ld, %s\n",
723 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
725 attr
.ia_valid
= ATTR_MODE
;
726 attr
.ia_mode
= mode
| S_IFDIR
;
730 * Always drop the dentry, we can't always depend on
731 * the fattr returned by the server (AIX seems to be
732 * broken). We're better off doing another lookup than
733 * depending on potentially bogus information.
738 error
= NFS_PROTO(dir
)->mkdir(dir
, &dentry
->d_name
, &attr
, &fhandle
,
740 if (!error
&& fhandle
.size
!= 0)
741 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
742 if (error
|| fhandle
.size
== 0)
747 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
751 dfprintk(VFS
, "NFS: rmdir(%x/%ld, %s\n",
752 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
755 error
= NFS_PROTO(dir
)->rmdir(dir
, &dentry
->d_name
);
760 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
762 static unsigned int sillycounter
;
763 const int i_inosize
= sizeof(dir
->i_ino
)*2;
764 const int countersize
= sizeof(sillycounter
)*2;
765 const int slen
= strlen(".nfs") + i_inosize
+ countersize
;
768 struct dentry
*sdentry
;
771 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
772 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
773 atomic_read(&dentry
->d_count
));
775 if (atomic_read(&dentry
->d_count
) == 1)
776 goto out
; /* No need to silly rename. */
780 if (!dentry
->d_inode
)
781 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
782 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
785 * We don't allow a dentry to be silly-renamed twice.
788 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
791 sprintf(silly
, ".nfs%*.*lx",
792 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
796 char *suffix
= silly
+ slen
- countersize
;
800 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
802 dfprintk(VFS
, "trying to rename %s to %s\n",
803 dentry
->d_name
.name
, silly
);
805 sdentry
= lookup_one(silly
, dentry
->d_parent
);
807 * N.B. Better to return EBUSY here ... it could be
808 * dangerous to delete the file while it's in use.
812 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
816 qsilly
.len
= strlen(silly
);
817 error
= NFS_PROTO(dir
)->rename(dir
, &dentry
->d_name
, dir
, &qsilly
);
819 nfs_renew_times(dentry
);
820 d_move(dentry
, sdentry
);
821 error
= nfs_async_unlink(dentry
);
822 /* If we return 0 we don't unlink */
830 * Remove a file after making sure there are no pending writes,
831 * and after checking that the file has only one user.
833 * We invalidate the attribute cache and free the inode prior to the operation
834 * to avoid possible races if the server reuses the inode.
836 static int nfs_safe_remove(struct dentry
*dentry
)
838 struct inode
*dir
= dentry
->d_parent
->d_inode
;
839 struct inode
*inode
= dentry
->d_inode
;
840 int error
= -EBUSY
, rehash
= 0;
842 dfprintk(VFS
, "NFS: safe_remove(%s/%s)\n",
843 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
846 * Unhash the dentry while we remove the file ...
848 if (!d_unhashed(dentry
)) {
852 if (atomic_read(&dentry
->d_count
) > 1) {
854 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
855 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
856 atomic_read(&dentry
->d_count
));
861 /* If the dentry was sillyrenamed, we simply call d_delete() */
862 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
870 error
= NFS_PROTO(dir
)->remove(dir
, &dentry
->d_name
);
885 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
886 * belongs to an active ".nfs..." file and we return -EBUSY.
888 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
890 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
894 dfprintk(VFS
, "NFS: unlink(%x/%ld, %s)\n",
895 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
897 error
= nfs_sillyrename(dir
, dentry
);
898 if (error
&& error
!= -EBUSY
) {
899 error
= nfs_safe_remove(dentry
);
901 nfs_renew_times(dentry
);
908 nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
911 struct nfs_fattr sym_attr
;
912 struct nfs_fh sym_fh
;
913 struct qstr qsymname
;
917 dfprintk(VFS
, "NFS: symlink(%x/%ld, %s, %s)\n",
918 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
, symname
);
920 error
= -ENAMETOOLONG
;
921 maxlen
= (NFS_PROTO(dir
)->version
==2) ? NFS2_MAXPATHLEN
: NFS3_MAXPATHLEN
;
922 if (strlen(symname
) > maxlen
)
927 printk("nfs_proc_symlink: %s/%s not negative!\n",
928 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
931 * Fill in the sattr for the call.
932 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
934 attr
.ia_valid
= ATTR_MODE
;
935 attr
.ia_mode
= S_IFLNK
| S_IRWXUGO
;
937 qsymname
.name
= symname
;
938 qsymname
.len
= strlen(symname
);
941 error
= NFS_PROTO(dir
)->symlink(dir
, &dentry
->d_name
, &qsymname
,
942 &attr
, &sym_fh
, &sym_attr
);
943 if (!error
&& sym_fh
.size
!= 0 && (sym_attr
.valid
& NFS_ATTR_FATTR
)) {
944 error
= nfs_instantiate(dentry
, &sym_fh
, &sym_attr
);
946 if (error
== -EEXIST
)
947 printk("nfs_proc_symlink: %s/%s already exists??\n",
948 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
957 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
959 struct inode
*inode
= old_dentry
->d_inode
;
962 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
963 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
964 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
967 * Drop the dentry in advance to force a new lookup.
968 * Since nfs_proc_link doesn't return a file handle,
969 * we can't use the existing dentry.
974 error
= NFS_PROTO(dir
)->link(inode
, dir
, &dentry
->d_name
);
980 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
981 * different file handle for the same inode after a rename (e.g. when
982 * moving to a different directory). A fail-safe method to do so would
983 * be to look up old_dir/old_name, create a link to new_dir/new_name and
984 * rename the old file using the sillyrename stuff. This way, the original
985 * file in old_dir will go away when the last process iput()s the inode.
989 * It actually works quite well. One needs to have the possibility for
990 * at least one ".nfs..." file in each directory the file ever gets
991 * moved or linked to which happens automagically with the new
992 * implementation that only depends on the dcache stuff instead of
993 * using the inode layer
995 * Unfortunately, things are a little more complicated than indicated
996 * above. For a cross-directory move, we want to make sure we can get
997 * rid of the old inode after the operation. This means there must be
998 * no pending writes (if it's a file), and the use count must be 1.
999 * If these conditions are met, we can drop the dentries before doing
1002 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1003 struct inode
*new_dir
, struct dentry
*new_dentry
)
1005 struct inode
*old_inode
= old_dentry
->d_inode
;
1006 struct inode
*new_inode
= new_dentry
->d_inode
;
1007 struct dentry
*dentry
= NULL
, *rehash
= NULL
;
1011 * To prevent any new references to the target during the rename,
1012 * we unhash the dentry and free the inode in advance.
1014 if (!d_unhashed(new_dentry
)) {
1016 rehash
= new_dentry
;
1019 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1020 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1021 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1022 atomic_read(&new_dentry
->d_count
));
1025 * First check whether the target is busy ... we can't
1026 * safely do _any_ rename if the target is in use.
1028 * For files, make a copy of the dentry and then do a
1029 * silly-rename. If the silly-rename succeeds, the
1030 * copied dentry is hashed and becomes the new target.
1034 if (S_ISDIR(new_inode
->i_mode
))
1036 else if (atomic_read(&new_dentry
->d_count
) > 1) {
1038 /* copy the target dentry's name */
1039 dentry
= d_alloc(new_dentry
->d_parent
,
1040 &new_dentry
->d_name
);
1044 /* silly-rename the existing target ... */
1045 err
= nfs_sillyrename(new_dir
, new_dentry
);
1047 new_dentry
= rehash
= dentry
;
1049 /* instantiate the replacement target */
1050 d_instantiate(new_dentry
, NULL
);
1053 /* dentry still busy? */
1054 if (atomic_read(&new_dentry
->d_count
) > 1) {
1056 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1057 new_dentry
->d_parent
->d_name
.name
,
1058 new_dentry
->d_name
.name
,
1059 atomic_read(&new_dentry
->d_count
));
1067 * ... prune child dentries and writebacks if needed.
1069 if (atomic_read(&old_dentry
->d_count
) > 1) {
1070 nfs_wb_all(old_inode
);
1071 shrink_dcache_parent(old_dentry
);
1075 d_delete(new_dentry
);
1077 nfs_zap_caches(new_dir
);
1078 nfs_zap_caches(old_dir
);
1079 error
= NFS_PROTO(old_dir
)->rename(old_dir
, &old_dentry
->d_name
,
1080 new_dir
, &new_dentry
->d_name
);
1084 if (!error
&& !S_ISDIR(old_inode
->i_mode
))
1085 d_move(old_dentry
, new_dentry
);
1087 /* new dentry created? */
1094 nfs_permission(struct inode
*inode
, int mask
)
1096 int error
= vfs_permission(inode
, mask
);
1098 if (!NFS_PROTO(inode
)->access
)
1101 * Trust UNIX mode bits except:
1103 * 1) When override capabilities may have been invoked
1104 * 2) When root squashing may be involved
1105 * 3) When ACLs may overturn a negative answer */
1106 if (!capable(CAP_DAC_OVERRIDE
) && !capable(CAP_DAC_READ_SEARCH
)
1107 && (current
->fsuid
!= 0) && (current
->fsgid
!= 0)
1108 && error
!= -EACCES
)
1111 error
= NFS_PROTO(inode
)->access(inode
, mask
, 0);
1113 if (error
== -EACCES
&& NFS_CLIENT(inode
)->cl_droppriv
&&
1114 current
->uid
!= 0 && current
->gid
!= 0 &&
1115 (current
->fsuid
!= current
->uid
|| current
->fsgid
!= current
->gid
))
1116 error
= NFS_PROTO(inode
)->access(inode
, mask
, 1);
1124 * version-control: t
1125 * kept-new-versions: 5