1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * tmpfs vnode interface.
36 #include <sys/cdefs.h>
38 #include <sys/kernel.h>
39 #include <sys/kern_syscall.h>
40 #include <sys/param.h>
41 #include <sys/fcntl.h>
42 #include <sys/lockf.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/sfbuf.h>
49 #include <sys/systm.h>
50 #include <sys/unistd.h>
51 #include <sys/vfsops.h>
52 #include <sys/vnode.h>
54 #include <sys/mplock2.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
61 #include <vfs/fifofs/fifo.h>
62 #include <vfs/tmpfs/tmpfs_vnops.h>
63 #include <vfs/tmpfs/tmpfs.h>
65 MALLOC_DECLARE(M_TMPFS
);
67 /* --------------------------------------------------------------------- */
70 tmpfs_nresolve(struct vop_nresolve_args
*v
)
72 struct vnode
*dvp
= v
->a_dvp
;
73 struct vnode
*vp
= NULL
;
74 struct namecache
*ncp
= v
->a_nch
->ncp
;
75 struct ucred
*cred
= v
->a_cred
;
78 struct tmpfs_dirent
*de
;
79 struct tmpfs_node
*dnode
;
81 dnode
= VP_TO_TMPFS_DIR(dvp
);
83 if (!vn_islocked(dvp
));
84 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
86 /* Check accessibility of requested node as a first step. */
87 error
= VOP_ACCESS(dvp
, VEXEC
, cred
);
91 if (dnode
->tn_dir
.tn_parent
== NULL
) {
96 de
= tmpfs_dir_lookup(dnode
, NULL
, ncp
);
98 /* The entry was not found in the directory.
99 * This is OK if we are creating or renaming an
100 * entry and are working on the last component of
102 error
= VOP_ACCESS(dvp
, VWRITE
, cred
);
110 struct tmpfs_node
*tnode
;
112 /* The entry was found, so get its associated
116 /* If we are not at the last path component and
117 * found a non-directory or non-link entry (which
118 * may itself be pointing to a directory), raise
120 if (tnode
->tn_links
> 1 &&
121 tnode
->tn_type
!= VDIR
&& tnode
->tn_type
!= VLNK
) {
126 error
= VOP_ACCESS(dvp
, VWRITE
, cred
);
130 /* Allocate a new vnode on the matching entry. */
131 error
= tmpfs_alloc_vp(dvp
->v_mount
, tnode
,
132 LK_EXCLUSIVE
| LK_RETRY
, &vp
);
136 if ((dnode
->tn_mode
& S_ISTXT
) &&
137 VOP_ACCESS(vp
, VWRITE
, cred
)) {
148 /* Store the result of this lookup in the cache. Avoid this if the
149 * request was for creation, as it does not improve timings on
153 cache_setvp(v
->a_nch
, vp
);
156 if (error
== ENOENT
) {
157 cache_setvp(v
->a_nch
, NULL
);
163 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args
*v
)
165 struct vnode
*dvp
= v
->a_dvp
;
166 struct vnode
**vpp
= v
->a_vpp
;
167 struct tmpfs_node
*dnode
= VP_TO_TMPFS_NODE(dvp
);
168 struct ucred
*cred
= v
->a_cred
;
172 /* Check accessibility of requested node as a first step. */
173 error
= VOP_ACCESS(dvp
, VEXEC
, cred
);
177 if (dnode
->tn_dir
.tn_parent
!= NULL
) {
178 /* Allocate a new vnode on the matching entry. */
179 error
= tmpfs_alloc_vp(dvp
->v_mount
, dnode
->tn_dir
.tn_parent
,
180 LK_EXCLUSIVE
| LK_RETRY
, vpp
);
186 return (*vpp
== NULL
) ? ENOENT
: 0;
189 /* --------------------------------------------------------------------- */
192 tmpfs_ncreate(struct vop_ncreate_args
*v
)
194 struct vnode
*dvp
= v
->a_dvp
;
195 struct vnode
**vpp
= v
->a_vpp
;
196 struct namecache
*ncp
= v
->a_nch
->ncp
;
197 struct vattr
*vap
= v
->a_vap
;
198 struct ucred
*cred
= v
->a_cred
;
201 KKASSERT(vap
->va_type
== VREG
|| vap
->va_type
== VSOCK
);
203 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
204 error
= tmpfs_alloc_file(dvp
, vpp
, vap
, ncp
, cred
, NULL
);
206 cache_setunresolved(v
->a_nch
);
207 cache_setvp(v
->a_nch
, *vpp
);
213 /* --------------------------------------------------------------------- */
216 tmpfs_nmknod(struct vop_nmknod_args
*v
)
218 struct vnode
*dvp
= v
->a_dvp
;
219 struct vnode
**vpp
= v
->a_vpp
;
220 struct namecache
*ncp
= v
->a_nch
->ncp
;
221 struct vattr
*vap
= v
->a_vap
;
222 struct ucred
*cred
= v
->a_cred
;
225 if (vap
->va_type
!= VBLK
&& vap
->va_type
!= VCHR
&&
226 vap
->va_type
!= VFIFO
)
229 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
230 error
= tmpfs_alloc_file(dvp
, vpp
, vap
, ncp
, cred
, NULL
);
232 cache_setunresolved(v
->a_nch
);
233 cache_setvp(v
->a_nch
, *vpp
);
240 /* --------------------------------------------------------------------- */
243 tmpfs_open(struct vop_open_args
*v
)
245 struct vnode
*vp
= v
->a_vp
;
246 int mode
= v
->a_mode
;
249 struct tmpfs_node
*node
;
251 KKASSERT(vn_islocked(vp
));
253 node
= VP_TO_TMPFS_NODE(vp
);
255 /* The file is still active but all its names have been removed
256 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
257 * it is about to die. */
258 if (node
->tn_links
< 1)
261 /* If the file is marked append-only, deny write requests. */
262 if (node
->tn_flags
& APPEND
&& (mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
265 return (vop_stdopen(v
));
268 KKASSERT(vn_islocked(vp
));
272 /* --------------------------------------------------------------------- */
275 tmpfs_close(struct vop_close_args
*v
)
277 struct vnode
*vp
= v
->a_vp
;
278 struct tmpfs_node
*node
;
280 node
= VP_TO_TMPFS_NODE(vp
);
282 if (node
->tn_links
> 0) {
283 /* Update node times. No need to do it if the node has
284 * been deleted, because it will vanish after we return. */
288 return vop_stdclose(v
);
291 /* --------------------------------------------------------------------- */
294 tmpfs_access(struct vop_access_args
*v
)
296 struct vnode
*vp
= v
->a_vp
;
298 struct tmpfs_node
*node
;
300 KKASSERT(vn_islocked(vp
));
302 node
= VP_TO_TMPFS_NODE(vp
);
304 switch (vp
->v_type
) {
310 if (VWRITE
&& vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
330 if (VWRITE
&& node
->tn_flags
& IMMUTABLE
) {
335 error
= vop_helper_access(v
, node
->tn_uid
, node
->tn_gid
, node
->tn_mode
, 0);
342 /* --------------------------------------------------------------------- */
345 tmpfs_getattr(struct vop_getattr_args
*v
)
347 struct vnode
*vp
= v
->a_vp
;
348 struct vattr
*vap
= v
->a_vap
;
350 struct tmpfs_node
*node
;
353 if(!vn_islocked(vp
)) {
355 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
358 node
= VP_TO_TMPFS_NODE(vp
);
362 vap
->va_type
= vp
->v_type
;
363 vap
->va_mode
= node
->tn_mode
;
364 vap
->va_nlink
= node
->tn_links
;
365 vap
->va_uid
= node
->tn_uid
;
366 vap
->va_gid
= node
->tn_gid
;
367 vap
->va_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
.val
[0];
368 vap
->va_fileid
= node
->tn_id
;
369 vap
->va_size
= node
->tn_size
;
370 vap
->va_blocksize
= PAGE_SIZE
;
371 vap
->va_atime
.tv_sec
= node
->tn_atime
;
372 vap
->va_atime
.tv_nsec
= node
->tn_atimensec
;
373 vap
->va_mtime
.tv_sec
= node
->tn_mtime
;
374 vap
->va_mtime
.tv_nsec
= node
->tn_mtimensec
;
375 vap
->va_ctime
.tv_sec
= node
->tn_ctime
;
376 vap
->va_ctime
.tv_nsec
= node
->tn_ctimensec
;
377 vap
->va_gen
= node
->tn_gen
;
378 vap
->va_flags
= node
->tn_flags
;
379 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
381 vap
->va_rmajor
= umajor(node
->tn_rdev
);
382 vap
->va_rminor
= uminor(node
->tn_rdev
);
384 vap
->va_bytes
= round_page(node
->tn_size
);
393 /* --------------------------------------------------------------------- */
396 tmpfs_setattr(struct vop_setattr_args
*v
)
398 struct vnode
*vp
= v
->a_vp
;
399 struct vattr
*vap
= v
->a_vap
;
400 struct ucred
*cred
= v
->a_cred
;
405 if(!vn_islocked(vp
)) {
407 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
410 /* Abort if any unsettable attribute is given. */
411 if (vap
->va_type
!= VNON
||
412 vap
->va_nlink
!= VNOVAL
||
413 vap
->va_fsid
!= VNOVAL
||
414 vap
->va_fileid
!= VNOVAL
||
415 vap
->va_blocksize
!= VNOVAL
||
416 vap
->va_gen
!= VNOVAL
||
417 vap
->va_rmajor
!= VNOVAL
||
418 vap
->va_bytes
!= VNOVAL
)
421 if (error
== 0 && (vap
->va_flags
!= VNOVAL
))
422 error
= tmpfs_chflags(vp
, vap
->va_flags
, cred
);
424 if (error
== 0 && (vap
->va_size
!= VNOVAL
))
425 error
= tmpfs_chsize(vp
, vap
->va_size
, cred
);
427 if (error
== 0 && (vap
->va_uid
!= VNOVAL
|| vap
->va_gid
!= VNOVAL
))
428 error
= tmpfs_chown(vp
, vap
->va_uid
, vap
->va_gid
, cred
);
430 if (error
== 0 && (vap
->va_mode
!= (mode_t
)VNOVAL
))
431 error
= tmpfs_chmod(vp
, vap
->va_mode
, cred
);
433 if (error
== 0 && ((vap
->va_atime
.tv_sec
!= VNOVAL
&&
434 vap
->va_atime
.tv_nsec
!= VNOVAL
) ||
435 (vap
->va_mtime
.tv_sec
!= VNOVAL
&&
436 vap
->va_mtime
.tv_nsec
!= VNOVAL
) ))
437 error
= tmpfs_chtimes(vp
, &vap
->va_atime
, &vap
->va_mtime
,
438 vap
->va_vaflags
, cred
);
440 /* Update the node times. We give preference to the error codes
441 * generated by this function rather than the ones that may arise
442 * from tmpfs_update. */
451 /* --------------------------------------------------------------------- */
454 tmpfs_fsync(struct vop_fsync_args
*v
)
456 struct vnode
*vp
= v
->a_vp
;
464 /* --------------------------------------------------------------------- */
467 tmpfs_read (struct vop_read_args
*ap
)
470 struct vnode
*vp
= ap
->a_vp
;
471 struct uio
*uio
= ap
->a_uio
;
472 struct tmpfs_node
*node
;
480 if (uio
->uio_resid
== 0) {
484 node
= VP_TO_TMPFS_NODE(vp
);
486 if (uio
->uio_offset
< 0)
488 if (vp
->v_type
!= VREG
)
491 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
494 if(curthread
->td_mpcount
)
502 while (uio
->uio_resid
> 0 && uio
->uio_offset
< node
->tn_size
) {
504 * Use buffer cache I/O (via tmpfs_strategy)
506 offset
= (off_t
)uio
->uio_offset
& BMASK
;
507 base_offset
= (off_t
)uio
->uio_offset
- offset
;
508 bp
= getcacheblk(vp
, base_offset
);
511 if (got_mplock
== 0) {
516 error
= bread(vp
, base_offset
, BSIZE
, &bp
);
519 kprintf("tmpfs_read bread error %d\n", error
);
524 if (got_mplock
== 0) {
530 * Figure out how many bytes we can actually copy this loop.
532 len
= BSIZE
- offset
;
533 if (len
> uio
->uio_resid
)
534 len
= uio
->uio_resid
;
535 if (len
> node
->tn_size
- uio
->uio_offset
)
536 len
= (size_t)(node
->tn_size
- uio
->uio_offset
);
538 error
= uiomove((char *)bp
->b_data
+ offset
, len
, uio
);
541 kprintf("tmpfs_read uiomove error %d\n", error
);
549 TMPFS_NODE_LOCK(node
);
550 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
551 TMPFS_NODE_UNLOCK(node
);
559 tmpfs_write (struct vop_write_args
*ap
)
562 struct vnode
*vp
= ap
->a_vp
;
563 struct uio
*uio
= ap
->a_uio
;
564 struct thread
*td
= uio
->uio_td
;
565 struct tmpfs_node
*node
;
577 if (uio
->uio_resid
== 0) {
581 node
= VP_TO_TMPFS_NODE(vp
);
583 if (vp
->v_type
!= VREG
)
586 oldsize
= node
->tn_size
;
587 if (ap
->a_ioflag
& IO_APPEND
)
588 uio
->uio_offset
= node
->tn_size
;
591 * Check for illegal write offsets.
593 if (uio
->uio_offset
+ uio
->uio_resid
>
594 VFS_TO_TMPFS(vp
->v_mount
)->tm_maxfilesize
)
597 if (vp
->v_type
== VREG
&& td
!= NULL
) {
598 error
= kern_getrlimit(RLIMIT_FSIZE
, &limit
);
601 if (uio
->uio_offset
+ uio
->uio_resid
> limit
.rlim_cur
) {
602 ksignal(td
->td_proc
, SIGXFSZ
);
609 * Extend the file's size if necessary
611 extended
= (uio
->uio_offset
+ uio
->uio_resid
) > node
->tn_size
;
613 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
615 if(curthread
->td_mpcount
)
625 while (uio
->uio_resid
> 0) {
627 * Use buffer cache I/O (via tmpfs_strategy)
629 offset
= (off_t
)uio
->uio_offset
& BMASK
;
630 base_offset
= (off_t
)uio
->uio_offset
- offset
;
631 len
= BSIZE
- offset
;
632 if (len
> uio
->uio_resid
)
633 len
= uio
->uio_resid
;
635 if ((uio
->uio_offset
+ len
) > node
->tn_size
) {
636 trivial
= uio
->uio_offset
<= node
->tn_size
;
637 error
= tmpfs_reg_resize(vp
, uio
->uio_offset
+ len
, trivial
);
642 bp
= getblk(vp
, base_offset
, BSIZE
, GETBLK_BHEAVY
, 0);
645 error
= uiomove((char *)bp
->b_data
+ offset
, len
, uio
);
647 kprintf("tmpfs_write uiomove error %d\n", error
);
652 if (uio
->uio_offset
> node
->tn_size
)
653 node
->tn_size
= uio
->uio_offset
;
656 * The data has been loaded into the buffer, write it out. (via tmpfs_strategy)
658 * call bdwrite() because we don't care about storage io flag (ap->a_ioflag) for a swap I/O
659 * maybe bawrite() for IO_DIRECT, bwrite() for IO_SYNC
661 * XXX: need to implement tmpfs_bmap() for a dirty bit handling of bdwrite()
665 kprintf("tmpfs_write bwrite error %d\n", error
);
676 (void)tmpfs_reg_resize(vp
, oldsize
, trivial
);
680 TMPFS_NODE_LOCK(node
);
681 node
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_MODIFIED
|
682 (extended
? TMPFS_NODE_CHANGED
: 0);
684 if (node
->tn_mode
& (S_ISUID
| S_ISGID
)) {
685 if (priv_check_cred(ap
->a_cred
, PRIV_VFS_RETAINSUGID
, 0))
686 node
->tn_mode
&= ~(S_ISUID
| S_ISGID
);
688 TMPFS_NODE_UNLOCK(node
);
696 tmpfs_advlock (struct vop_advlock_args
*ap
)
698 struct tmpfs_node
*node
;
699 struct vnode
*vp
= ap
->a_vp
;
701 node
= VP_TO_TMPFS_NODE(vp
);
703 return (lf_advlock(ap
, &node
->tn_advlock
, node
->tn_size
));
708 tmpfs_strategy(struct vop_strategy_args
*ap
)
710 struct bio
*bio
= ap
->a_bio
;
711 struct vnode
*vp
= ap
->a_vp
;
712 struct tmpfs_node
*node
;
715 if (vp
->v_type
!= VREG
)
718 node
= VP_TO_TMPFS_NODE(vp
);
720 uobj
= node
->tn_reg
.tn_aobj
;
722 * call swap_pager_strategy to store vm object into swap device
724 swap_pager_strategy(uobj
, bio
);
730 tmpfs_bmap(struct vop_bmap_args
*ap
)
732 if (ap
->a_doffsetp
!= NULL
)
733 *ap
->a_doffsetp
= ap
->a_loffset
;
734 if (ap
->a_runp
!= NULL
)
736 if (ap
->a_runb
!= NULL
)
741 /* --------------------------------------------------------------------- */
744 tmpfs_nremove(struct vop_nremove_args
*v
)
746 struct vnode
*dvp
= v
->a_dvp
;
747 struct namecache
*ncp
= v
->a_nch
->ncp
;
748 struct vnode
*vp
= ncp
->nc_vp
;
750 struct tmpfs_dirent
*de
;
751 struct tmpfs_mount
*tmp
;
752 struct tmpfs_node
*dnode
;
753 struct tmpfs_node
*node
;
755 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
756 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
758 if (vp
->v_type
== VDIR
) {
763 dnode
= VP_TO_TMPFS_DIR(dvp
);
764 node
= VP_TO_TMPFS_NODE(vp
);
765 tmp
= VFS_TO_TMPFS(vp
->v_mount
);
766 de
= tmpfs_dir_lookup(dnode
, node
, ncp
);
772 /* Files marked as immutable or append-only cannot be deleted. */
773 if ((node
->tn_flags
& (IMMUTABLE
| APPEND
| NOUNLINK
)) ||
774 (dnode
->tn_flags
& APPEND
)) {
779 /* Remove the entry from the directory; as it is a file, we do not
780 * have to change the number of hard links of the directory. */
781 tmpfs_dir_detach(dvp
, de
);
783 /* Free the directory entry we just deleted. Note that the node
784 * referred by it will not be removed until the vnode is really
786 tmpfs_free_dirent(tmp
, de
, TRUE
);
788 if (node
->tn_links
> 0) {
789 TMPFS_NODE_LOCK(node
);
790 node
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_CHANGED
| \
792 TMPFS_NODE_UNLOCK(node
);
795 cache_setunresolved(v
->a_nch
);
796 cache_setvp(v
->a_nch
, NULL
);
797 cache_inval_vp(vp
, CINV_DESTROY
);
808 /* --------------------------------------------------------------------- */
811 tmpfs_nlink(struct vop_nlink_args
*v
)
813 struct vnode
*dvp
= v
->a_dvp
;
814 struct vnode
*vp
= v
->a_vp
;
815 struct namecache
*ncp
= v
->a_nch
->ncp
;
818 struct tmpfs_dirent
*de
;
819 struct tmpfs_node
*node
;
821 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
822 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
825 KKASSERT(dvp
!= vp
); /* XXX When can this be false? */
827 node
= VP_TO_TMPFS_NODE(vp
);
829 /* XXX: Why aren't the following two tests done by the caller? */
831 /* Hard links of directories are forbidden. */
832 if (vp
->v_type
== VDIR
) {
837 /* Cannot create cross-device links. */
838 if (dvp
->v_mount
!= vp
->v_mount
) {
843 /* Ensure that we do not overflow the maximum number of links imposed
845 KKASSERT(node
->tn_links
<= LINK_MAX
);
846 if (node
->tn_links
== LINK_MAX
) {
851 /* We cannot create links of files marked immutable or append-only. */
852 if (node
->tn_flags
& (IMMUTABLE
| APPEND
)) {
857 /* Allocate a new directory entry to represent the node. */
858 error
= tmpfs_alloc_dirent(VFS_TO_TMPFS(vp
->v_mount
), node
,
859 ncp
->nc_name
, ncp
->nc_nlen
, &de
);
863 /* Insert the new directory entry into the appropriate directory. */
864 tmpfs_dir_attach(dvp
, de
);
866 /* vp link count has changed, so update node times. */
868 TMPFS_NODE_LOCK(node
);
869 node
->tn_status
|= TMPFS_NODE_CHANGED
;
870 TMPFS_NODE_UNLOCK(node
);
873 cache_setunresolved(v
->a_nch
);
874 cache_setvp(v
->a_nch
, vp
);
884 /* --------------------------------------------------------------------- */
887 tmpfs_nrename(struct vop_nrename_args
*v
)
889 struct vnode
*fdvp
= v
->a_fdvp
;
890 struct namecache
*fncp
= v
->a_fnch
->ncp
;
891 struct vnode
*fvp
= fncp
->nc_vp
;
892 struct vnode
*tdvp
= v
->a_tdvp
;
893 struct namecache
*tncp
= v
->a_tnch
->ncp
;
894 struct vnode
*tvp
= tncp
->nc_vp
;
898 struct tmpfs_dirent
*de
;
899 struct tmpfs_mount
*tmp
;
900 struct tmpfs_node
*fdnode
;
901 struct tmpfs_node
*fnode
;
902 struct tmpfs_node
*tnode
;
903 struct tmpfs_node
*tdnode
;
905 vn_lock(tdvp
, LK_EXCLUSIVE
| LK_RETRY
);
906 if(tvp
!= NULL
&& tdvp
!= tvp
)
907 vn_lock(tvp
, LK_EXCLUSIVE
| LK_RETRY
);
909 tnode
= (tvp
== NULL
) ? NULL
: VP_TO_TMPFS_NODE(tvp
);
911 /* Disallow cross-device renames.
912 * XXX Why isn't this done by the caller? */
913 if (fvp
->v_mount
!= tdvp
->v_mount
||
914 (tvp
!= NULL
&& fvp
->v_mount
!= tvp
->v_mount
)) {
919 tmp
= VFS_TO_TMPFS(tdvp
->v_mount
);
920 tdnode
= VP_TO_TMPFS_DIR(tdvp
);
922 /* If source and target are the same file, there is nothing to do. */
928 /* If we need to move the directory between entries, lock the
929 * source so that we can safely operate on it. */
931 error
= vn_lock(fdvp
, LK_EXCLUSIVE
| LK_RETRY
);
935 fdnode
= VP_TO_TMPFS_DIR(fdvp
);
936 fnode
= VP_TO_TMPFS_NODE(fvp
);
937 de
= tmpfs_dir_lookup(fdnode
, fnode
, fncp
);
939 /* Avoid manipulating '.' and '..' entries. */
944 KKASSERT(de
->td_node
== fnode
);
946 /* If re-naming a directory to another preexisting directory
947 * ensure that the target directory is empty so that its
948 * removal causes no side effects.
949 * Kern_rename gurantees the destination to be a directory
950 * if the source is one. */
952 KKASSERT(tnode
!= NULL
);
954 if ((tnode
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
)) ||
955 (tdnode
->tn_flags
& (APPEND
| IMMUTABLE
))) {
960 if (fnode
->tn_type
== VDIR
&& tnode
->tn_type
== VDIR
) {
961 if (tnode
->tn_size
> 0) {
965 } else if (fnode
->tn_type
== VDIR
&& tnode
->tn_type
!= VDIR
) {
968 } else if (fnode
->tn_type
!= VDIR
&& tnode
->tn_type
== VDIR
) {
972 KKASSERT(fnode
->tn_type
!= VDIR
&&
973 tnode
->tn_type
!= VDIR
);
977 if ((fnode
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
))
978 || (fdnode
->tn_flags
& (APPEND
| IMMUTABLE
))) {
983 /* Ensure that we have enough memory to hold the new name, if it
984 * has to be changed. */
985 if (fncp
->nc_nlen
!= tncp
->nc_nlen
||
986 bcmp(fncp
->nc_name
, tncp
->nc_name
, fncp
->nc_nlen
) != 0) {
987 newname
= kmalloc(tncp
->nc_nlen
, M_TMPFSNAME
, M_WAITOK
);
991 /* If the node is being moved to another directory, we have to do
993 if (fdnode
!= tdnode
) {
994 /* In case we are moving a directory, we have to adjust its
995 * parent to point to the new parent. */
996 if (de
->td_node
->tn_type
== VDIR
) {
997 struct tmpfs_node
*n
;
999 /* Ensure the target directory is not a child of the
1000 * directory being moved. Otherwise, we'd end up
1001 * with stale nodes. */
1003 /* TMPFS_LOCK garanties that no nodes are freed while
1004 * traversing the list. Nodes can only be marked as
1005 * removed: tn_parent == NULL. */
1008 while (n
!= n
->tn_dir
.tn_parent
) {
1009 struct tmpfs_node
*parent
;
1012 TMPFS_NODE_UNLOCK(n
);
1015 if (newname
!= NULL
)
1016 kfree(newname
, M_TMPFSNAME
);
1019 parent
= n
->tn_dir
.tn_parent
;
1020 if (parent
== NULL
) {
1024 TMPFS_NODE_LOCK(parent
);
1025 if (parent
->tn_dir
.tn_parent
== NULL
) {
1026 TMPFS_NODE_UNLOCK(parent
);
1032 TMPFS_NODE_UNLOCK(n
);
1036 if (newname
!= NULL
)
1037 kfree(newname
, M_TMPFSNAME
);
1041 /* Adjust the parent pointer. */
1042 TMPFS_VALIDATE_DIR(fnode
);
1043 TMPFS_NODE_LOCK(de
->td_node
);
1044 de
->td_node
->tn_dir
.tn_parent
= tdnode
;
1046 /* As a result of changing the target of the '..'
1047 * entry, the link count of the source and target
1048 * directories has to be adjusted. */
1049 TMPFS_NODE_LOCK(tdnode
);
1050 TMPFS_ASSERT_LOCKED(tdnode
);
1051 TMPFS_NODE_LOCK(fdnode
);
1052 TMPFS_ASSERT_LOCKED(fdnode
);
1057 TMPFS_NODE_UNLOCK(fdnode
);
1058 TMPFS_NODE_UNLOCK(tdnode
);
1059 TMPFS_NODE_UNLOCK(de
->td_node
);
1062 /* Do the move: just remove the entry from the source directory
1063 * and insert it into the target one. */
1064 tmpfs_dir_detach(fdvp
, de
);
1065 tmpfs_dir_attach(tdvp
, de
);
1068 /* If the name has changed, we need to make it effective by changing
1069 * it in the directory entry. */
1070 if (newname
!= NULL
) {
1072 kfree(de
->td_name
, M_TMPFSNAME
);
1073 de
->td_namelen
= (uint16_t)tncp
->nc_nlen
;
1074 bcopy(tncp
->nc_name
, newname
, tncp
->nc_nlen
);
1075 newname
[tncp
->nc_nlen
] = '\0';
1076 de
->td_name
= newname
;
1078 TMPFS_NODE_LOCK(tdnode
);
1079 TMPFS_NODE_LOCK(fdnode
);
1081 fnode
->tn_status
|= TMPFS_NODE_CHANGED
;
1082 tdnode
->tn_status
|= TMPFS_NODE_MODIFIED
;
1084 TMPFS_NODE_UNLOCK(fdnode
);
1085 TMPFS_NODE_UNLOCK(tdnode
);
1088 /* If we are overwriting an entry, we have to remove the old one
1089 * from the target directory. */
1091 /* Remove the old entry from the target directory. */
1092 de
= tmpfs_dir_lookup(tdnode
, tnode
, tncp
);
1093 tmpfs_dir_detach(tdvp
, de
);
1095 /* Free the directory entry we just deleted. Note that the
1096 * node referred by it will not be removed until the vnode is
1097 * really reclaimed. */
1098 tmpfs_free_dirent(VFS_TO_TMPFS(tvp
->v_mount
), de
, TRUE
);
1100 cache_inval_vp(tvp
, CINV_DESTROY
);
1103 cache_rename(v
->a_fnch
, v
->a_tnch
);
1107 if (fdnode
!= tdnode
)
1111 /* Release target nodes. */
1112 /* XXX: I don't understand when tdvp can be the same as tvp, but
1113 * other code takes care of this... */
1125 /* --------------------------------------------------------------------- */
1128 tmpfs_nmkdir(struct vop_nmkdir_args
*v
)
1130 struct vnode
*dvp
= v
->a_dvp
;
1131 struct vnode
**vpp
= v
->a_vpp
;
1132 struct namecache
*ncp
= v
->a_nch
->ncp
;
1133 struct vattr
*vap
= v
->a_vap
;
1134 struct ucred
*cred
= v
->a_cred
;
1137 KKASSERT(vap
->va_type
== VDIR
);
1139 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1140 error
= tmpfs_alloc_file(dvp
, vpp
, vap
, ncp
, cred
, NULL
);
1142 cache_setunresolved(v
->a_nch
);
1143 cache_setvp(v
->a_nch
, *vpp
);
1150 /* --------------------------------------------------------------------- */
1153 tmpfs_nrmdir(struct vop_nrmdir_args
*v
)
1155 struct vnode
*dvp
= v
->a_dvp
;
1156 struct namecache
*ncp
= v
->a_nch
->ncp
;
1157 struct vnode
*vp
= ncp
->nc_vp
;
1160 struct tmpfs_dirent
*de
;
1161 struct tmpfs_mount
*tmp
;
1162 struct tmpfs_node
*dnode
;
1163 struct tmpfs_node
*node
;
1165 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1166 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1168 tmp
= VFS_TO_TMPFS(dvp
->v_mount
);
1169 dnode
= VP_TO_TMPFS_DIR(dvp
);
1170 node
= VP_TO_TMPFS_DIR(vp
);
1172 /* Directories with more than two entries ('.' and '..') cannot be
1174 if (node
->tn_size
> 0) {
1179 if ((dnode
->tn_flags
& APPEND
)
1180 || (node
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
))) {
1185 /* This invariant holds only if we are not trying to remove "..".
1186 * We checked for that above so this is safe now. */
1187 KKASSERT(node
->tn_dir
.tn_parent
== dnode
);
1189 /* Get the directory entry associated with node (vp). This was
1190 * filled by tmpfs_lookup while looking up the entry. */
1191 de
= tmpfs_dir_lookup(dnode
, node
, ncp
);
1192 KKASSERT(TMPFS_DIRENT_MATCHES(de
,
1196 /* Check flags to see if we are allowed to remove the directory. */
1197 if (dnode
->tn_flags
& APPEND
1198 || node
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
)) {
1204 /* Detach the directory entry from the directory (dnode). */
1205 tmpfs_dir_detach(dvp
, de
);
1207 /* No vnode should be allocated for this entry from this point */
1208 TMPFS_NODE_LOCK(node
);
1209 TMPFS_ASSERT_ELOCKED(node
);
1210 TMPFS_NODE_LOCK(dnode
);
1211 TMPFS_ASSERT_ELOCKED(dnode
);
1214 node
->tn_dir
.tn_parent
= NULL
;
1215 node
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_CHANGED
| \
1216 TMPFS_NODE_MODIFIED
;
1219 dnode
->tn_status
|= TMPFS_NODE_ACCESSED
| \
1220 TMPFS_NODE_CHANGED
| TMPFS_NODE_MODIFIED
;
1222 TMPFS_NODE_UNLOCK(dnode
);
1223 TMPFS_NODE_UNLOCK(node
);
1225 /* Free the directory entry we just deleted. Note that the node
1226 * referred by it will not be removed until the vnode is really
1228 tmpfs_free_dirent(tmp
, de
, TRUE
);
1230 /* Release the deleted vnode (will destroy the node, notify
1231 * interested parties and clean it from the cache). */
1233 TMPFS_NODE_LOCK(dnode
);
1234 dnode
->tn_status
|= TMPFS_NODE_CHANGED
;
1235 TMPFS_NODE_UNLOCK(dnode
);
1238 cache_setunresolved(v
->a_nch
);
1239 cache_setvp(v
->a_nch
, NULL
);
1240 cache_inval_vp(vp
, CINV_DESTROY
);
1250 /* --------------------------------------------------------------------- */
1253 tmpfs_nsymlink(struct vop_nsymlink_args
*v
)
1255 struct vnode
*dvp
= v
->a_dvp
;
1256 struct vnode
**vpp
= v
->a_vpp
;
1257 struct namecache
*ncp
= v
->a_nch
->ncp
;
1258 struct vattr
*vap
= v
->a_vap
;
1259 struct ucred
*cred
= v
->a_cred
;
1260 char *target
= v
->a_target
;
1263 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1264 vap
->va_type
= VLNK
;
1265 error
= tmpfs_alloc_file(dvp
, vpp
, vap
, ncp
, cred
, target
);
1267 cache_setunresolved(v
->a_nch
);
1268 cache_setvp(v
->a_nch
, *vpp
);
1275 /* --------------------------------------------------------------------- */
1278 tmpfs_readdir(struct vop_readdir_args
*v
)
1280 struct vnode
*vp
= v
->a_vp
;
1281 struct uio
*uio
= v
->a_uio
;
1282 int *eofflag
= v
->a_eofflag
;
1283 off_t
**cookies
= v
->a_cookies
;
1284 int *ncookies
= v
->a_ncookies
;
1289 struct tmpfs_node
*node
;
1291 /* This operation only makes sense on directory nodes. */
1292 if (vp
->v_type
!= VDIR
)
1295 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1297 node
= VP_TO_TMPFS_DIR(vp
);
1298 startoff
= uio
->uio_offset
;
1300 if (uio
->uio_offset
== TMPFS_DIRCOOKIE_DOT
) {
1301 error
= tmpfs_dir_getdotdent(node
, uio
);
1307 if (uio
->uio_offset
== TMPFS_DIRCOOKIE_DOTDOT
) {
1308 error
= tmpfs_dir_getdotdotdent(node
, uio
);
1314 error
= tmpfs_dir_getdents(node
, uio
, &cnt
);
1317 KKASSERT(error
>= -1);
1322 if (eofflag
!= NULL
)
1324 (error
== 0 && uio
->uio_offset
== TMPFS_DIRCOOKIE_EOF
);
1326 /* Update NFS-related variables. */
1327 if (error
== 0 && cookies
!= NULL
&& ncookies
!= NULL
) {
1329 off_t off
= startoff
;
1330 struct tmpfs_dirent
*de
= NULL
;
1333 *cookies
= kmalloc(cnt
* sizeof(off_t
), M_TEMP
, M_WAITOK
);
1335 for (i
= 0; i
< cnt
; i
++) {
1336 KKASSERT(off
!= TMPFS_DIRCOOKIE_EOF
);
1337 if (off
== TMPFS_DIRCOOKIE_DOT
) {
1338 off
= TMPFS_DIRCOOKIE_DOTDOT
;
1340 if (off
== TMPFS_DIRCOOKIE_DOTDOT
) {
1341 de
= TAILQ_FIRST(&node
->tn_dir
.tn_dirhead
);
1342 } else if (de
!= NULL
) {
1343 de
= TAILQ_NEXT(de
, td_entries
);
1345 de
= tmpfs_dir_lookupbycookie(node
,
1347 KKASSERT(de
!= NULL
);
1348 de
= TAILQ_NEXT(de
, td_entries
);
1351 off
= TMPFS_DIRCOOKIE_EOF
;
1353 off
= tmpfs_dircookie(de
);
1356 (*cookies
)[i
] = off
;
1358 KKASSERT(uio
->uio_offset
== off
);
1365 /* --------------------------------------------------------------------- */
1368 tmpfs_readlink(struct vop_readlink_args
*v
)
1370 struct vnode
*vp
= v
->a_vp
;
1371 struct uio
*uio
= v
->a_uio
;
1374 struct tmpfs_node
*node
;
1376 KKASSERT(uio
->uio_offset
== 0);
1377 KKASSERT(vp
->v_type
== VLNK
);
1379 node
= VP_TO_TMPFS_NODE(vp
);
1381 error
= uiomove(node
->tn_link
, MIN(node
->tn_size
, uio
->uio_resid
),
1383 TMPFS_NODE_LOCK(node
);
1384 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
1385 TMPFS_NODE_UNLOCK(node
);
1390 /* --------------------------------------------------------------------- */
1393 tmpfs_inactive(struct vop_inactive_args
*v
)
1395 struct vnode
*vp
= v
->a_vp
;
1397 struct tmpfs_node
*node
;
1399 KKASSERT(vn_islocked(vp
));
1401 node
= VP_TO_TMPFS_NODE(vp
);
1403 TMPFS_NODE_LOCK(node
);
1404 if (node
->tn_links
== 0 &&
1405 (node
->tn_vpstate
& TMPFS_VNODE_DOOMED
)) {
1406 TMPFS_NODE_UNLOCK(node
);
1410 TMPFS_NODE_UNLOCK(node
);
1415 /* --------------------------------------------------------------------- */
1418 tmpfs_reclaim(struct vop_reclaim_args
*v
)
1420 struct vnode
*vp
= v
->a_vp
;
1422 struct tmpfs_mount
*tmp
;
1423 struct tmpfs_node
*node
;
1425 node
= VP_TO_TMPFS_NODE(vp
);
1426 tmp
= VFS_TO_TMPFS(vp
->v_mount
);
1428 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1431 /* If the node referenced by this vnode was deleted by the user,
1432 * we must free its associated data structures (now that the vnode
1433 * is being reclaimed). */
1434 TMPFS_NODE_LOCK(node
);
1435 if (node
->tn_links
== 0 &&
1436 (node
->tn_vpstate
& TMPFS_VNODE_ALLOCATING
) == 0) {
1437 node
->tn_vpstate
= TMPFS_VNODE_DOOMED
;
1438 TMPFS_NODE_UNLOCK(node
);
1439 tmpfs_free_node(tmp
, node
);
1442 TMPFS_NODE_UNLOCK(node
);
1446 KKASSERT(vp
->v_data
== NULL
);
1450 /* --------------------------------------------------------------------- */
1453 tmpfs_print(struct vop_print_args
*v
)
1455 struct vnode
*vp
= v
->a_vp
;
1457 struct tmpfs_node
*node
;
1459 node
= VP_TO_TMPFS_NODE(vp
);
1461 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1462 node
, node
->tn_flags
, node
->tn_links
);
1463 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1464 node
->tn_mode
, node
->tn_uid
, node
->tn_gid
,
1465 (uintmax_t)node
->tn_size
, node
->tn_status
);
1467 if (vp
->v_type
== VFIFO
)
1475 /* --------------------------------------------------------------------- */
1478 tmpfs_pathconf(struct vop_pathconf_args
*v
)
1480 int name
= v
->a_name
;
1481 register_t
*retval
= v
->a_retval
;
1504 case _PC_CHOWN_RESTRICTED
:
1516 case _PC_FILESIZEBITS
:
1517 *retval
= 0; /* XXX Don't know which value should I return. */
1527 /* --------------------------------------------------------------------- */
1530 * vnode operations vector used for files stored in a tmpfs file system.
1532 struct vop_ops tmpfs_vnode_vops
= {
1533 .vop_default
= vop_defaultop
,
1534 .vop_getpages
= vop_stdgetpages
,
1535 .vop_putpages
= vop_stdputpages
,
1536 .vop_ncreate
= tmpfs_ncreate
,
1537 .vop_nresolve
= tmpfs_nresolve
,
1538 .vop_nlookupdotdot
= tmpfs_nlookupdotdot
,
1539 .vop_nmknod
= tmpfs_nmknod
,
1540 .vop_open
= tmpfs_open
,
1541 .vop_close
= tmpfs_close
,
1542 .vop_access
= tmpfs_access
,
1543 .vop_getattr
= tmpfs_getattr
,
1544 .vop_setattr
= tmpfs_setattr
,
1545 .vop_read
= tmpfs_read
,
1546 .vop_write
= tmpfs_write
,
1547 .vop_fsync
= tmpfs_fsync
,
1548 .vop_nremove
= tmpfs_nremove
,
1549 .vop_nlink
= tmpfs_nlink
,
1550 .vop_nrename
= tmpfs_nrename
,
1551 .vop_nmkdir
= tmpfs_nmkdir
,
1552 .vop_nrmdir
= tmpfs_nrmdir
,
1553 .vop_nsymlink
= tmpfs_nsymlink
,
1554 .vop_readdir
= tmpfs_readdir
,
1555 .vop_readlink
= tmpfs_readlink
,
1556 .vop_inactive
= tmpfs_inactive
,
1557 .vop_reclaim
= tmpfs_reclaim
,
1558 .vop_print
= tmpfs_print
,
1559 .vop_pathconf
= tmpfs_pathconf
,
1560 // .vop_bmap = tmpfs_bmap,
1561 .vop_bmap
= (void *)vop_eopnotsupp
,
1562 .vop_strategy
= tmpfs_strategy
,
1563 .vop_advlock
= tmpfs_advlock
,