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 /* Filehandle matches? */
537 if (memcmp(NFS_FH(inode
), fhandle
.data
, sizeof(struct nfs_fh
)))
540 /* Ok, remember that we successfully checked it.. */
541 nfs_refresh_inode(inode
, &fattr
);
544 nfs_renew_times(dentry
);
549 shrink_dcache_parent(dentry
);
550 /* If we have submounts, don't unhash ! */
551 if (have_submounts(dentry
))
554 /* Purge readdir caches. */
556 if (inode
&& S_ISDIR(inode
->i_mode
))
557 nfs_zap_caches(inode
);
563 * This is called from dput() when d_count is going to 0.
565 static int nfs_dentry_delete(struct dentry
*dentry
)
567 dfprintk(VFS
, "NFS: dentry_delete(%s/%s, %x)\n",
568 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
571 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
572 /* Unhash it, so that ->d_iput() would be called */
580 * Called when the dentry loses inode.
581 * We use it to clean up silly-renamed files.
583 static void nfs_dentry_iput(struct dentry
*dentry
, struct inode
*inode
)
585 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
587 nfs_complete_unlink(dentry
);
593 struct dentry_operations nfs_dentry_operations
= {
594 d_revalidate
: nfs_lookup_revalidate
,
595 d_delete
: nfs_dentry_delete
,
596 d_iput
: nfs_dentry_iput
,
599 static struct dentry
*nfs_lookup(struct inode
*dir
, struct dentry
* dentry
)
603 struct nfs_fh fhandle
;
604 struct nfs_fattr fattr
;
606 dfprintk(VFS
, "NFS: lookup(%s/%s)\n",
607 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
609 error
= -ENAMETOOLONG
;
610 if (dentry
->d_name
.len
> NFS_SERVER(dir
)->namelen
)
614 dentry
->d_op
= &nfs_dentry_operations
;
616 error
= NFS_PROTO(dir
)->lookup(dir
, &dentry
->d_name
, &fhandle
, &fattr
);
618 if (error
== -ENOENT
)
622 inode
= nfs_fhget(dentry
, &fhandle
, &fattr
);
625 d_add(dentry
, inode
);
626 nfs_renew_times(dentry
);
631 return ERR_PTR(error
);
635 * Code common to create, mkdir, and mknod.
637 static int nfs_instantiate(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
638 struct nfs_fattr
*fattr
)
643 inode
= nfs_fhget(dentry
, fhandle
, fattr
);
645 d_instantiate(dentry
, inode
);
646 nfs_renew_times(dentry
);
653 * Following a failed create operation, we drop the dentry rather
654 * than retain a negative dentry. This avoids a problem in the event
655 * that the operation succeeded on the server, but an error in the
656 * reply path made it appear to have failed.
658 static int nfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
661 struct nfs_fattr fattr
;
662 struct nfs_fh fhandle
;
665 dfprintk(VFS
, "NFS: create(%x/%ld, %s\n",
666 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
669 attr
.ia_valid
= ATTR_MODE
;
672 * The 0 argument passed into the create function should one day
673 * contain the O_EXCL flag if requested. This allows NFSv3 to
674 * select the appropriate create strategy. Currently open_namei
675 * does not pass the create flags.
678 error
= NFS_PROTO(dir
)->create(dir
, &dentry
->d_name
,
679 &attr
, 0, &fhandle
, &fattr
);
680 if (!error
&& fhandle
.size
!= 0)
681 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
682 if (error
|| fhandle
.size
== 0)
688 * See comments for nfs_proc_create regarding failed operations.
690 static int nfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, int rdev
)
693 struct nfs_fattr fattr
;
694 struct nfs_fh fhandle
;
697 dfprintk(VFS
, "NFS: mknod(%x/%ld, %s\n",
698 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
701 attr
.ia_valid
= ATTR_MODE
;
704 error
= NFS_PROTO(dir
)->mknod(dir
, &dentry
->d_name
, &attr
, rdev
,
706 if (!error
&& fhandle
.size
!= 0)
707 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
708 if (error
|| fhandle
.size
== 0)
714 * See comments for nfs_proc_create regarding failed operations.
716 static int nfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
719 struct nfs_fattr fattr
;
720 struct nfs_fh fhandle
;
723 dfprintk(VFS
, "NFS: mkdir(%x/%ld, %s\n",
724 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
726 attr
.ia_valid
= ATTR_MODE
;
727 attr
.ia_mode
= mode
| S_IFDIR
;
731 * Always drop the dentry, we can't always depend on
732 * the fattr returned by the server (AIX seems to be
733 * broken). We're better off doing another lookup than
734 * depending on potentially bogus information.
739 error
= NFS_PROTO(dir
)->mkdir(dir
, &dentry
->d_name
, &attr
, &fhandle
,
741 if (!error
&& fhandle
.size
!= 0)
742 error
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
743 if (error
|| fhandle
.size
== 0)
748 static int nfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
752 dfprintk(VFS
, "NFS: rmdir(%x/%ld, %s\n",
753 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
756 error
= NFS_PROTO(dir
)->rmdir(dir
, &dentry
->d_name
);
761 static int nfs_sillyrename(struct inode
*dir
, struct dentry
*dentry
)
763 static unsigned int sillycounter
;
764 const int i_inosize
= sizeof(dir
->i_ino
)*2;
765 const int countersize
= sizeof(sillycounter
)*2;
766 const int slen
= strlen(".nfs") + i_inosize
+ countersize
;
769 struct dentry
*sdentry
;
772 dfprintk(VFS
, "NFS: silly-rename(%s/%s, ct=%d)\n",
773 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
774 atomic_read(&dentry
->d_count
));
776 if (atomic_read(&dentry
->d_count
) == 1)
777 goto out
; /* No need to silly rename. */
781 if (!dentry
->d_inode
)
782 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
783 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
786 * We don't allow a dentry to be silly-renamed twice.
789 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
792 sprintf(silly
, ".nfs%*.*lx",
793 i_inosize
, i_inosize
, dentry
->d_inode
->i_ino
);
797 char *suffix
= silly
+ slen
- countersize
;
801 sprintf(suffix
, "%*.*x", countersize
, countersize
, sillycounter
);
803 dfprintk(VFS
, "trying to rename %s to %s\n",
804 dentry
->d_name
.name
, silly
);
806 sdentry
= lookup_one(silly
, dentry
->d_parent
);
808 * N.B. Better to return EBUSY here ... it could be
809 * dangerous to delete the file while it's in use.
813 } while(sdentry
->d_inode
!= NULL
); /* need negative lookup */
817 qsilly
.len
= strlen(silly
);
818 error
= NFS_PROTO(dir
)->rename(dir
, &dentry
->d_name
, dir
, &qsilly
);
820 nfs_renew_times(dentry
);
821 d_move(dentry
, sdentry
);
822 error
= nfs_async_unlink(dentry
);
823 /* If we return 0 we don't unlink */
831 * Remove a file after making sure there are no pending writes,
832 * and after checking that the file has only one user.
834 * We invalidate the attribute cache and free the inode prior to the operation
835 * to avoid possible races if the server reuses the inode.
837 static int nfs_safe_remove(struct dentry
*dentry
)
839 struct inode
*dir
= dentry
->d_parent
->d_inode
;
840 struct inode
*inode
= dentry
->d_inode
;
841 int error
= -EBUSY
, rehash
= 0;
843 dfprintk(VFS
, "NFS: safe_remove(%s/%s)\n",
844 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
847 * Unhash the dentry while we remove the file ...
849 if (!d_unhashed(dentry
)) {
853 if (atomic_read(&dentry
->d_count
) > 1) {
855 printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
856 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
857 atomic_read(&dentry
->d_count
));
862 /* If the dentry was sillyrenamed, we simply call d_delete() */
863 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
871 error
= NFS_PROTO(dir
)->remove(dir
, &dentry
->d_name
);
886 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
887 * belongs to an active ".nfs..." file and we return -EBUSY.
889 * If sillyrename() returns 0, we do nothing, otherwise we unlink.
891 static int nfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
895 dfprintk(VFS
, "NFS: unlink(%x/%ld, %s)\n",
896 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
);
898 error
= nfs_sillyrename(dir
, dentry
);
899 if (error
&& error
!= -EBUSY
) {
900 error
= nfs_safe_remove(dentry
);
902 nfs_renew_times(dentry
);
909 nfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
912 struct nfs_fattr sym_attr
;
913 struct nfs_fh sym_fh
;
914 struct qstr qsymname
;
918 dfprintk(VFS
, "NFS: symlink(%x/%ld, %s, %s)\n",
919 dir
->i_dev
, dir
->i_ino
, dentry
->d_name
.name
, symname
);
921 error
= -ENAMETOOLONG
;
922 maxlen
= (NFS_PROTO(dir
)->version
==2) ? NFS2_MAXPATHLEN
: NFS3_MAXPATHLEN
;
923 if (strlen(symname
) > maxlen
)
928 printk("nfs_proc_symlink: %s/%s not negative!\n",
929 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
932 * Fill in the sattr for the call.
933 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
935 attr
.ia_valid
= ATTR_MODE
;
936 attr
.ia_mode
= S_IFLNK
| S_IRWXUGO
;
938 qsymname
.name
= symname
;
939 qsymname
.len
= strlen(symname
);
942 error
= NFS_PROTO(dir
)->symlink(dir
, &dentry
->d_name
, &qsymname
,
943 &attr
, &sym_fh
, &sym_attr
);
944 if (!error
&& sym_fh
.size
!= 0 && (sym_attr
.valid
& NFS_ATTR_FATTR
)) {
945 error
= nfs_instantiate(dentry
, &sym_fh
, &sym_attr
);
947 if (error
== -EEXIST
)
948 printk("nfs_proc_symlink: %s/%s already exists??\n",
949 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
958 nfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
960 struct inode
*inode
= old_dentry
->d_inode
;
963 dfprintk(VFS
, "NFS: link(%s/%s -> %s/%s)\n",
964 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
965 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
968 * Drop the dentry in advance to force a new lookup.
969 * Since nfs_proc_link doesn't return a file handle,
970 * we can't use the existing dentry.
975 error
= NFS_PROTO(dir
)->link(inode
, dir
, &dentry
->d_name
);
981 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
982 * different file handle for the same inode after a rename (e.g. when
983 * moving to a different directory). A fail-safe method to do so would
984 * be to look up old_dir/old_name, create a link to new_dir/new_name and
985 * rename the old file using the sillyrename stuff. This way, the original
986 * file in old_dir will go away when the last process iput()s the inode.
990 * It actually works quite well. One needs to have the possibility for
991 * at least one ".nfs..." file in each directory the file ever gets
992 * moved or linked to which happens automagically with the new
993 * implementation that only depends on the dcache stuff instead of
994 * using the inode layer
996 * Unfortunately, things are a little more complicated than indicated
997 * above. For a cross-directory move, we want to make sure we can get
998 * rid of the old inode after the operation. This means there must be
999 * no pending writes (if it's a file), and the use count must be 1.
1000 * If these conditions are met, we can drop the dentries before doing
1003 static int nfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1004 struct inode
*new_dir
, struct dentry
*new_dentry
)
1006 struct inode
*old_inode
= old_dentry
->d_inode
;
1007 struct inode
*new_inode
= new_dentry
->d_inode
;
1008 struct dentry
*dentry
= NULL
, *rehash
= NULL
;
1012 * To prevent any new references to the target during the rename,
1013 * we unhash the dentry and free the inode in advance.
1015 if (!d_unhashed(new_dentry
)) {
1017 rehash
= new_dentry
;
1020 dfprintk(VFS
, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1021 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1022 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
,
1023 atomic_read(&new_dentry
->d_count
));
1026 * First check whether the target is busy ... we can't
1027 * safely do _any_ rename if the target is in use.
1029 * For files, make a copy of the dentry and then do a
1030 * silly-rename. If the silly-rename succeeds, the
1031 * copied dentry is hashed and becomes the new target.
1035 if (S_ISDIR(new_inode
->i_mode
))
1037 else if (atomic_read(&new_dentry
->d_count
) > 1) {
1039 /* copy the target dentry's name */
1040 dentry
= d_alloc(new_dentry
->d_parent
,
1041 &new_dentry
->d_name
);
1045 /* silly-rename the existing target ... */
1046 err
= nfs_sillyrename(new_dir
, new_dentry
);
1048 new_dentry
= rehash
= dentry
;
1050 /* instantiate the replacement target */
1051 d_instantiate(new_dentry
, NULL
);
1054 /* dentry still busy? */
1055 if (atomic_read(&new_dentry
->d_count
) > 1) {
1057 printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1058 new_dentry
->d_parent
->d_name
.name
,
1059 new_dentry
->d_name
.name
,
1060 atomic_read(&new_dentry
->d_count
));
1068 * ... prune child dentries and writebacks if needed.
1070 if (atomic_read(&old_dentry
->d_count
) > 1) {
1071 nfs_wb_all(old_inode
);
1072 shrink_dcache_parent(old_dentry
);
1076 d_delete(new_dentry
);
1078 nfs_zap_caches(new_dir
);
1079 nfs_zap_caches(old_dir
);
1080 error
= NFS_PROTO(old_dir
)->rename(old_dir
, &old_dentry
->d_name
,
1081 new_dir
, &new_dentry
->d_name
);
1085 if (!error
&& !S_ISDIR(old_inode
->i_mode
))
1086 d_move(old_dentry
, new_dentry
);
1088 /* new dentry created? */
1095 nfs_permission(struct inode
*inode
, int mask
)
1097 int error
= vfs_permission(inode
, mask
);
1099 if (!NFS_PROTO(inode
)->access
)
1102 * Trust UNIX mode bits except:
1104 * 1) When override capabilities may have been invoked
1105 * 2) When root squashing may be involved
1106 * 3) When ACLs may overturn a negative answer */
1107 if (!capable(CAP_DAC_OVERRIDE
) && !capable(CAP_DAC_READ_SEARCH
)
1108 && (current
->fsuid
!= 0) && (current
->fsgid
!= 0)
1109 && error
!= -EACCES
)
1112 error
= NFS_PROTO(inode
)->access(inode
, mask
, 0);
1114 if (error
== -EACCES
&& NFS_CLIENT(inode
)->cl_droppriv
&&
1115 current
->uid
!= 0 && current
->gid
!= 0 &&
1116 (current
->fsuid
!= current
->uid
|| current
->fsgid
!= current
->gid
))
1117 error
= NFS_PROTO(inode
)->access(inode
, mask
, 1);
1125 * version-control: t
1126 * kept-new-versions: 5