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 * fsync is basically a NOP. However, when unmounting we have
455 * to clean out the buffer cache to prevent a dirty buffers kernel
456 * assertion. We can do this simply by truncating the file to 0.
459 tmpfs_fsync(struct vop_fsync_args
*v
)
461 struct tmpfs_mount
*tmp
;
462 struct vnode
*vp
= v
->a_vp
;
464 tmp
= VFS_TO_TMPFS(vp
->v_mount
);
465 if (tmp
->tm_flags
& TMPFS_FLAG_UNMOUNTING
) {
466 if (vp
->v_type
== VREG
)
467 tmpfs_truncate(vp
, 0);
474 /* --------------------------------------------------------------------- */
477 tmpfs_read (struct vop_read_args
*ap
)
480 struct vnode
*vp
= ap
->a_vp
;
481 struct uio
*uio
= ap
->a_uio
;
482 struct tmpfs_node
*node
;
490 if (uio
->uio_resid
== 0) {
494 node
= VP_TO_TMPFS_NODE(vp
);
496 if (uio
->uio_offset
< 0)
498 if (vp
->v_type
!= VREG
)
501 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
504 if(curthread
->td_mpcount
)
512 while (uio
->uio_resid
> 0 && uio
->uio_offset
< node
->tn_size
) {
514 * Use buffer cache I/O (via tmpfs_strategy)
516 offset
= (size_t)uio
->uio_offset
& BMASK
;
517 base_offset
= (off_t
)uio
->uio_offset
- offset
;
518 bp
= getcacheblk(vp
, base_offset
);
521 if (got_mplock
== 0) {
526 error
= bread(vp
, base_offset
, BSIZE
, &bp
);
529 kprintf("tmpfs_read bread error %d\n", error
);
534 if (got_mplock
== 0) {
540 * Figure out how many bytes we can actually copy this loop.
542 len
= BSIZE
- offset
;
543 if (len
> uio
->uio_resid
)
544 len
= uio
->uio_resid
;
545 if (len
> node
->tn_size
- uio
->uio_offset
)
546 len
= (size_t)(node
->tn_size
- uio
->uio_offset
);
548 error
= uiomove((char *)bp
->b_data
+ offset
, len
, uio
);
551 kprintf("tmpfs_read uiomove error %d\n", error
);
559 TMPFS_NODE_LOCK(node
);
560 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
561 TMPFS_NODE_UNLOCK(node
);
569 tmpfs_write (struct vop_write_args
*ap
)
572 struct vnode
*vp
= ap
->a_vp
;
573 struct uio
*uio
= ap
->a_uio
;
574 struct thread
*td
= uio
->uio_td
;
575 struct tmpfs_node
*node
;
587 if (uio
->uio_resid
== 0) {
591 node
= VP_TO_TMPFS_NODE(vp
);
593 if (vp
->v_type
!= VREG
)
596 oldsize
= node
->tn_size
;
597 if (ap
->a_ioflag
& IO_APPEND
)
598 uio
->uio_offset
= node
->tn_size
;
601 * Check for illegal write offsets.
603 if (uio
->uio_offset
+ uio
->uio_resid
>
604 VFS_TO_TMPFS(vp
->v_mount
)->tm_maxfilesize
)
607 if (vp
->v_type
== VREG
&& td
!= NULL
) {
608 error
= kern_getrlimit(RLIMIT_FSIZE
, &limit
);
611 if (uio
->uio_offset
+ uio
->uio_resid
> limit
.rlim_cur
) {
612 ksignal(td
->td_proc
, SIGXFSZ
);
619 * Extend the file's size if necessary
621 extended
= ((uio
->uio_offset
+ uio
->uio_resid
) > node
->tn_size
);
623 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
625 if(curthread
->td_mpcount
)
634 while (uio
->uio_resid
> 0) {
636 * Use buffer cache I/O (via tmpfs_strategy)
638 offset
= (size_t)uio
->uio_offset
& BMASK
;
639 base_offset
= (off_t
)uio
->uio_offset
- offset
;
640 len
= BSIZE
- offset
;
641 if (len
> uio
->uio_resid
)
642 len
= uio
->uio_resid
;
644 if ((uio
->uio_offset
+ len
) > node
->tn_size
) {
645 trivial
= (uio
->uio_offset
<= node
->tn_size
);
646 error
= tmpfs_reg_resize(vp
, uio
->uio_offset
+ len
, trivial
);
652 * Read to fill in any gaps. Theoretically we could
653 * optimize this if the write covers the entire buffer
654 * and is not a UIO_NOCOPY write, however this can lead
655 * to a security violation exposing random kernel memory
656 * (whatever junk was in the backing VM pages before).
658 * So just use bread() to do the right thing.
660 error
= bread(vp
, base_offset
, BSIZE
, &bp
);
661 error
= uiomove((char *)bp
->b_data
+ offset
, len
, uio
);
663 kprintf("tmpfs_write uiomove error %d\n", error
);
668 if (uio
->uio_offset
> node
->tn_size
)
669 node
->tn_size
= uio
->uio_offset
;
672 * The data has been loaded into the buffer, write it out.
674 * We want tmpfs to be able to use all available ram, not
675 * just the buffer cache, so if not explicitly paging we
676 * use buwrite() to leave the buffer clean but mark all the
677 * VM pages valid+dirty.
679 * Be careful, the kernel uses IO_SYNC/IO_ASYNC when it
680 * really really wants to flush the buffer, typically as
681 * part of the paging system, and we'll loop forever if
684 if (ap
->a_ioflag
& IO_SYNC
)
686 else if (ap
->a_ioflag
& IO_ASYNC
)
692 kprintf("tmpfs_write bwrite error %d\n", error
);
702 (void)tmpfs_reg_resize(vp
, oldsize
, trivial
);
706 TMPFS_NODE_LOCK(node
);
707 node
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_MODIFIED
|
708 (extended
? TMPFS_NODE_CHANGED
: 0);
710 if (node
->tn_mode
& (S_ISUID
| S_ISGID
)) {
711 if (priv_check_cred(ap
->a_cred
, PRIV_VFS_RETAINSUGID
, 0))
712 node
->tn_mode
&= ~(S_ISUID
| S_ISGID
);
714 TMPFS_NODE_UNLOCK(node
);
722 tmpfs_advlock (struct vop_advlock_args
*ap
)
724 struct tmpfs_node
*node
;
725 struct vnode
*vp
= ap
->a_vp
;
727 node
= VP_TO_TMPFS_NODE(vp
);
729 return (lf_advlock(ap
, &node
->tn_advlock
, node
->tn_size
));
734 tmpfs_strategy(struct vop_strategy_args
*ap
)
736 struct bio
*bio
= ap
->a_bio
;
737 struct buf
*bp
= bio
->bio_buf
;
738 struct vnode
*vp
= ap
->a_vp
;
739 struct tmpfs_node
*node
;
742 if (vp
->v_type
!= VREG
) {
743 bp
->b_resid
= bp
->b_bcount
;
744 bp
->b_flags
|= B_ERROR
| B_INVAL
;
745 bp
->b_error
= EINVAL
;
750 node
= VP_TO_TMPFS_NODE(vp
);
752 uobj
= node
->tn_reg
.tn_aobj
;
755 * Call swap_pager_strategy to read or write between the VM
756 * object and the buffer cache.
758 swap_pager_strategy(uobj
, bio
);
764 tmpfs_bmap(struct vop_bmap_args
*ap
)
766 if (ap
->a_doffsetp
!= NULL
)
767 *ap
->a_doffsetp
= ap
->a_loffset
;
768 if (ap
->a_runp
!= NULL
)
770 if (ap
->a_runb
!= NULL
)
776 /* --------------------------------------------------------------------- */
779 tmpfs_nremove(struct vop_nremove_args
*v
)
781 struct vnode
*dvp
= v
->a_dvp
;
782 struct namecache
*ncp
= v
->a_nch
->ncp
;
785 struct tmpfs_dirent
*de
;
786 struct tmpfs_mount
*tmp
;
787 struct tmpfs_node
*dnode
;
788 struct tmpfs_node
*node
;
791 * We have to acquire the vp from v->a_nch because
792 * we will likely unresolve the namecache entry, and
793 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
794 * sequence to recover space from the file.
796 error
= cache_vref(v
->a_nch
, v
->a_cred
, &vp
);
797 KKASSERT(error
== 0);
798 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
800 if (vp
->v_type
== VDIR
) {
805 dnode
= VP_TO_TMPFS_DIR(dvp
);
806 node
= VP_TO_TMPFS_NODE(vp
);
807 tmp
= VFS_TO_TMPFS(vp
->v_mount
);
808 de
= tmpfs_dir_lookup(dnode
, node
, ncp
);
814 /* Files marked as immutable or append-only cannot be deleted. */
815 if ((node
->tn_flags
& (IMMUTABLE
| APPEND
| NOUNLINK
)) ||
816 (dnode
->tn_flags
& APPEND
)) {
821 /* Remove the entry from the directory; as it is a file, we do not
822 * have to change the number of hard links of the directory. */
823 tmpfs_dir_detach(dvp
, de
);
825 /* Free the directory entry we just deleted. Note that the node
826 * referred by it will not be removed until the vnode is really
828 tmpfs_free_dirent(tmp
, de
);
830 if (node
->tn_links
> 0) {
831 TMPFS_NODE_LOCK(node
);
832 node
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_CHANGED
| \
834 TMPFS_NODE_UNLOCK(node
);
837 cache_setunresolved(v
->a_nch
);
838 cache_setvp(v
->a_nch
, NULL
);
839 /*cache_inval_vp(vp, CINV_DESTROY);*/
849 /* --------------------------------------------------------------------- */
852 tmpfs_nlink(struct vop_nlink_args
*v
)
854 struct vnode
*dvp
= v
->a_dvp
;
855 struct vnode
*vp
= v
->a_vp
;
856 struct namecache
*ncp
= v
->a_nch
->ncp
;
859 struct tmpfs_dirent
*de
;
860 struct tmpfs_node
*node
;
862 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
863 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
866 KKASSERT(dvp
!= vp
); /* XXX When can this be false? */
868 node
= VP_TO_TMPFS_NODE(vp
);
870 /* XXX: Why aren't the following two tests done by the caller? */
872 /* Hard links of directories are forbidden. */
873 if (vp
->v_type
== VDIR
) {
878 /* Cannot create cross-device links. */
879 if (dvp
->v_mount
!= vp
->v_mount
) {
884 /* Ensure that we do not overflow the maximum number of links imposed
886 KKASSERT(node
->tn_links
<= LINK_MAX
);
887 if (node
->tn_links
== LINK_MAX
) {
892 /* We cannot create links of files marked immutable or append-only. */
893 if (node
->tn_flags
& (IMMUTABLE
| APPEND
)) {
898 /* Allocate a new directory entry to represent the node. */
899 error
= tmpfs_alloc_dirent(VFS_TO_TMPFS(vp
->v_mount
), node
,
900 ncp
->nc_name
, ncp
->nc_nlen
, &de
);
904 /* Insert the new directory entry into the appropriate directory. */
905 tmpfs_dir_attach(dvp
, de
);
907 /* vp link count has changed, so update node times. */
909 TMPFS_NODE_LOCK(node
);
910 node
->tn_status
|= TMPFS_NODE_CHANGED
;
911 TMPFS_NODE_UNLOCK(node
);
914 cache_setunresolved(v
->a_nch
);
915 cache_setvp(v
->a_nch
, vp
);
925 /* --------------------------------------------------------------------- */
928 tmpfs_nrename(struct vop_nrename_args
*v
)
930 struct vnode
*fdvp
= v
->a_fdvp
;
931 struct namecache
*fncp
= v
->a_fnch
->ncp
;
932 struct vnode
*fvp
= fncp
->nc_vp
;
933 struct vnode
*tdvp
= v
->a_tdvp
;
934 struct namecache
*tncp
= v
->a_tnch
->ncp
;
935 struct vnode
*tvp
= tncp
->nc_vp
;
939 struct tmpfs_dirent
*de
;
940 struct tmpfs_mount
*tmp
;
941 struct tmpfs_node
*fdnode
;
942 struct tmpfs_node
*fnode
;
943 struct tmpfs_node
*tnode
;
944 struct tmpfs_node
*tdnode
;
946 vn_lock(tdvp
, LK_EXCLUSIVE
| LK_RETRY
);
947 if(tvp
!= NULL
&& tdvp
!= tvp
)
948 vn_lock(tvp
, LK_EXCLUSIVE
| LK_RETRY
);
950 tnode
= (tvp
== NULL
) ? NULL
: VP_TO_TMPFS_NODE(tvp
);
952 /* Disallow cross-device renames.
953 * XXX Why isn't this done by the caller? */
954 if (fvp
->v_mount
!= tdvp
->v_mount
||
955 (tvp
!= NULL
&& fvp
->v_mount
!= tvp
->v_mount
)) {
960 tmp
= VFS_TO_TMPFS(tdvp
->v_mount
);
961 tdnode
= VP_TO_TMPFS_DIR(tdvp
);
963 /* If source and target are the same file, there is nothing to do. */
969 /* If we need to move the directory between entries, lock the
970 * source so that we can safely operate on it. */
972 error
= vn_lock(fdvp
, LK_EXCLUSIVE
| LK_RETRY
);
976 fdnode
= VP_TO_TMPFS_DIR(fdvp
);
977 fnode
= VP_TO_TMPFS_NODE(fvp
);
978 de
= tmpfs_dir_lookup(fdnode
, fnode
, fncp
);
980 /* Avoid manipulating '.' and '..' entries. */
985 KKASSERT(de
->td_node
== fnode
);
987 /* If re-naming a directory to another preexisting directory
988 * ensure that the target directory is empty so that its
989 * removal causes no side effects.
990 * Kern_rename gurantees the destination to be a directory
991 * if the source is one. */
993 KKASSERT(tnode
!= NULL
);
995 if ((tnode
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
)) ||
996 (tdnode
->tn_flags
& (APPEND
| IMMUTABLE
))) {
1001 if (fnode
->tn_type
== VDIR
&& tnode
->tn_type
== VDIR
) {
1002 if (tnode
->tn_size
> 0) {
1006 } else if (fnode
->tn_type
== VDIR
&& tnode
->tn_type
!= VDIR
) {
1009 } else if (fnode
->tn_type
!= VDIR
&& tnode
->tn_type
== VDIR
) {
1013 KKASSERT(fnode
->tn_type
!= VDIR
&&
1014 tnode
->tn_type
!= VDIR
);
1018 if ((fnode
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
))
1019 || (fdnode
->tn_flags
& (APPEND
| IMMUTABLE
))) {
1024 /* Ensure that we have enough memory to hold the new name, if it
1025 * has to be changed. */
1026 if (fncp
->nc_nlen
!= tncp
->nc_nlen
||
1027 bcmp(fncp
->nc_name
, tncp
->nc_name
, fncp
->nc_nlen
) != 0) {
1028 newname
= kmalloc(tncp
->nc_nlen
+ 1, M_TMPFSNAME
, M_WAITOK
);
1032 /* If the node is being moved to another directory, we have to do
1034 if (fdnode
!= tdnode
) {
1035 /* In case we are moving a directory, we have to adjust its
1036 * parent to point to the new parent. */
1037 if (de
->td_node
->tn_type
== VDIR
) {
1038 struct tmpfs_node
*n
;
1040 /* Ensure the target directory is not a child of the
1041 * directory being moved. Otherwise, we'd end up
1042 * with stale nodes. */
1044 /* TMPFS_LOCK garanties that no nodes are freed while
1045 * traversing the list. Nodes can only be marked as
1046 * removed: tn_parent == NULL. */
1049 while (n
!= n
->tn_dir
.tn_parent
) {
1050 struct tmpfs_node
*parent
;
1053 TMPFS_NODE_UNLOCK(n
);
1056 if (newname
!= NULL
)
1057 kfree(newname
, M_TMPFSNAME
);
1060 parent
= n
->tn_dir
.tn_parent
;
1061 if (parent
== NULL
) {
1065 TMPFS_NODE_LOCK(parent
);
1066 if (parent
->tn_dir
.tn_parent
== NULL
) {
1067 TMPFS_NODE_UNLOCK(parent
);
1073 TMPFS_NODE_UNLOCK(n
);
1077 if (newname
!= NULL
)
1078 kfree(newname
, M_TMPFSNAME
);
1082 /* Adjust the parent pointer. */
1083 TMPFS_VALIDATE_DIR(fnode
);
1084 TMPFS_NODE_LOCK(de
->td_node
);
1085 de
->td_node
->tn_dir
.tn_parent
= tdnode
;
1087 /* As a result of changing the target of the '..'
1088 * entry, the link count of the source and target
1089 * directories has to be adjusted. */
1090 TMPFS_NODE_LOCK(tdnode
);
1091 TMPFS_ASSERT_LOCKED(tdnode
);
1092 TMPFS_NODE_LOCK(fdnode
);
1093 TMPFS_ASSERT_LOCKED(fdnode
);
1098 TMPFS_NODE_UNLOCK(fdnode
);
1099 TMPFS_NODE_UNLOCK(tdnode
);
1100 TMPFS_NODE_UNLOCK(de
->td_node
);
1103 /* Do the move: just remove the entry from the source directory
1104 * and insert it into the target one. */
1105 tmpfs_dir_detach(fdvp
, de
);
1106 tmpfs_dir_attach(tdvp
, de
);
1109 /* If the name has changed, we need to make it effective by changing
1110 * it in the directory entry. */
1111 if (newname
!= NULL
) {
1113 kfree(de
->td_name
, M_TMPFSNAME
);
1114 de
->td_namelen
= (uint16_t)tncp
->nc_nlen
;
1115 bcopy(tncp
->nc_name
, newname
, tncp
->nc_nlen
);
1116 newname
[tncp
->nc_nlen
] = '\0';
1117 de
->td_name
= newname
;
1119 TMPFS_NODE_LOCK(tdnode
);
1120 TMPFS_NODE_LOCK(fdnode
);
1122 fnode
->tn_status
|= TMPFS_NODE_CHANGED
;
1123 tdnode
->tn_status
|= TMPFS_NODE_MODIFIED
;
1125 TMPFS_NODE_UNLOCK(fdnode
);
1126 TMPFS_NODE_UNLOCK(tdnode
);
1129 /* If we are overwriting an entry, we have to remove the old one
1130 * from the target directory. */
1132 /* Remove the old entry from the target directory. */
1133 de
= tmpfs_dir_lookup(tdnode
, tnode
, tncp
);
1134 tmpfs_dir_detach(tdvp
, de
);
1136 /* Free the directory entry we just deleted. Note that the
1137 * node referred by it will not be removed until the vnode is
1138 * really reclaimed. */
1139 tmpfs_free_dirent(VFS_TO_TMPFS(tvp
->v_mount
), de
);
1140 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1143 cache_rename(v
->a_fnch
, v
->a_tnch
);
1147 if (fdnode
!= tdnode
)
1151 /* Release target nodes. */
1152 /* XXX: I don't understand when tdvp can be the same as tvp, but
1153 * other code takes care of this... */
1165 /* --------------------------------------------------------------------- */
1168 tmpfs_nmkdir(struct vop_nmkdir_args
*v
)
1170 struct vnode
*dvp
= v
->a_dvp
;
1171 struct vnode
**vpp
= v
->a_vpp
;
1172 struct namecache
*ncp
= v
->a_nch
->ncp
;
1173 struct vattr
*vap
= v
->a_vap
;
1174 struct ucred
*cred
= v
->a_cred
;
1177 KKASSERT(vap
->va_type
== VDIR
);
1179 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1180 error
= tmpfs_alloc_file(dvp
, vpp
, vap
, ncp
, cred
, NULL
);
1182 cache_setunresolved(v
->a_nch
);
1183 cache_setvp(v
->a_nch
, *vpp
);
1190 /* --------------------------------------------------------------------- */
1193 tmpfs_nrmdir(struct vop_nrmdir_args
*v
)
1195 struct vnode
*dvp
= v
->a_dvp
;
1196 struct namecache
*ncp
= v
->a_nch
->ncp
;
1200 struct tmpfs_dirent
*de
;
1201 struct tmpfs_mount
*tmp
;
1202 struct tmpfs_node
*dnode
;
1203 struct tmpfs_node
*node
;
1206 * We have to acquire the vp from v->a_nch because
1207 * we will likely unresolve the namecache entry, and
1208 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1211 error
= cache_vref(v
->a_nch
, v
->a_cred
, &vp
);
1212 KKASSERT(error
== 0);
1213 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1215 tmp
= VFS_TO_TMPFS(dvp
->v_mount
);
1216 dnode
= VP_TO_TMPFS_DIR(dvp
);
1217 node
= VP_TO_TMPFS_DIR(vp
);
1219 /* Directories with more than two entries ('.' and '..') cannot be
1221 if (node
->tn_size
> 0) {
1226 if ((dnode
->tn_flags
& APPEND
)
1227 || (node
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
))) {
1232 /* This invariant holds only if we are not trying to remove "..".
1233 * We checked for that above so this is safe now. */
1234 KKASSERT(node
->tn_dir
.tn_parent
== dnode
);
1236 /* Get the directory entry associated with node (vp). This was
1237 * filled by tmpfs_lookup while looking up the entry. */
1238 de
= tmpfs_dir_lookup(dnode
, node
, ncp
);
1239 KKASSERT(TMPFS_DIRENT_MATCHES(de
,
1243 /* Check flags to see if we are allowed to remove the directory. */
1244 if (dnode
->tn_flags
& APPEND
1245 || node
->tn_flags
& (NOUNLINK
| IMMUTABLE
| APPEND
)) {
1251 /* Detach the directory entry from the directory (dnode). */
1252 tmpfs_dir_detach(dvp
, de
);
1254 /* No vnode should be allocated for this entry from this point */
1255 TMPFS_NODE_LOCK(node
);
1256 TMPFS_ASSERT_ELOCKED(node
);
1257 TMPFS_NODE_LOCK(dnode
);
1258 TMPFS_ASSERT_ELOCKED(dnode
);
1261 /* handled by tmpfs_free_node */
1262 KKASSERT(node
->tn_links
> 0);
1264 node
->tn_dir
.tn_parent
= NULL
;
1266 node
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_CHANGED
| \
1267 TMPFS_NODE_MODIFIED
;
1270 /* handled by tmpfs_free_node */
1271 KKASSERT(dnode
->tn_links
> 0);
1274 dnode
->tn_status
|= TMPFS_NODE_ACCESSED
| \
1275 TMPFS_NODE_CHANGED
| TMPFS_NODE_MODIFIED
;
1277 TMPFS_NODE_UNLOCK(dnode
);
1278 TMPFS_NODE_UNLOCK(node
);
1280 /* Free the directory entry we just deleted. Note that the node
1281 * referred by it will not be removed until the vnode is really
1283 tmpfs_free_dirent(tmp
, de
);
1285 /* Release the deleted vnode (will destroy the node, notify
1286 * interested parties and clean it from the cache). */
1288 TMPFS_NODE_LOCK(dnode
);
1289 dnode
->tn_status
|= TMPFS_NODE_CHANGED
;
1290 TMPFS_NODE_UNLOCK(dnode
);
1293 cache_setunresolved(v
->a_nch
);
1294 cache_setvp(v
->a_nch
, NULL
);
1295 /*cache_inval_vp(vp, CINV_DESTROY);*/
1305 /* --------------------------------------------------------------------- */
1308 tmpfs_nsymlink(struct vop_nsymlink_args
*v
)
1310 struct vnode
*dvp
= v
->a_dvp
;
1311 struct vnode
**vpp
= v
->a_vpp
;
1312 struct namecache
*ncp
= v
->a_nch
->ncp
;
1313 struct vattr
*vap
= v
->a_vap
;
1314 struct ucred
*cred
= v
->a_cred
;
1315 char *target
= v
->a_target
;
1318 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
);
1319 vap
->va_type
= VLNK
;
1320 error
= tmpfs_alloc_file(dvp
, vpp
, vap
, ncp
, cred
, target
);
1322 cache_setunresolved(v
->a_nch
);
1323 cache_setvp(v
->a_nch
, *vpp
);
1330 /* --------------------------------------------------------------------- */
1333 tmpfs_readdir(struct vop_readdir_args
*v
)
1335 struct vnode
*vp
= v
->a_vp
;
1336 struct uio
*uio
= v
->a_uio
;
1337 int *eofflag
= v
->a_eofflag
;
1338 off_t
**cookies
= v
->a_cookies
;
1339 int *ncookies
= v
->a_ncookies
;
1344 struct tmpfs_node
*node
;
1346 /* This operation only makes sense on directory nodes. */
1347 if (vp
->v_type
!= VDIR
)
1350 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1352 node
= VP_TO_TMPFS_DIR(vp
);
1353 startoff
= uio
->uio_offset
;
1355 if (uio
->uio_offset
== TMPFS_DIRCOOKIE_DOT
) {
1356 error
= tmpfs_dir_getdotdent(node
, uio
);
1362 if (uio
->uio_offset
== TMPFS_DIRCOOKIE_DOTDOT
) {
1363 error
= tmpfs_dir_getdotdotdent(node
, uio
);
1369 error
= tmpfs_dir_getdents(node
, uio
, &cnt
);
1372 KKASSERT(error
>= -1);
1377 if (eofflag
!= NULL
)
1379 (error
== 0 && uio
->uio_offset
== TMPFS_DIRCOOKIE_EOF
);
1381 /* Update NFS-related variables. */
1382 if (error
== 0 && cookies
!= NULL
&& ncookies
!= NULL
) {
1384 off_t off
= startoff
;
1385 struct tmpfs_dirent
*de
= NULL
;
1388 *cookies
= kmalloc(cnt
* sizeof(off_t
), M_TEMP
, M_WAITOK
);
1390 for (i
= 0; i
< cnt
; i
++) {
1391 KKASSERT(off
!= TMPFS_DIRCOOKIE_EOF
);
1392 if (off
== TMPFS_DIRCOOKIE_DOT
) {
1393 off
= TMPFS_DIRCOOKIE_DOTDOT
;
1395 if (off
== TMPFS_DIRCOOKIE_DOTDOT
) {
1396 de
= TAILQ_FIRST(&node
->tn_dir
.tn_dirhead
);
1397 } else if (de
!= NULL
) {
1398 de
= TAILQ_NEXT(de
, td_entries
);
1400 de
= tmpfs_dir_lookupbycookie(node
,
1402 KKASSERT(de
!= NULL
);
1403 de
= TAILQ_NEXT(de
, td_entries
);
1406 off
= TMPFS_DIRCOOKIE_EOF
;
1408 off
= tmpfs_dircookie(de
);
1411 (*cookies
)[i
] = off
;
1413 KKASSERT(uio
->uio_offset
== off
);
1420 /* --------------------------------------------------------------------- */
1423 tmpfs_readlink(struct vop_readlink_args
*v
)
1425 struct vnode
*vp
= v
->a_vp
;
1426 struct uio
*uio
= v
->a_uio
;
1429 struct tmpfs_node
*node
;
1431 KKASSERT(uio
->uio_offset
== 0);
1432 KKASSERT(vp
->v_type
== VLNK
);
1434 node
= VP_TO_TMPFS_NODE(vp
);
1436 error
= uiomove(node
->tn_link
, MIN(node
->tn_size
, uio
->uio_resid
),
1438 TMPFS_NODE_LOCK(node
);
1439 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
1440 TMPFS_NODE_UNLOCK(node
);
1445 /* --------------------------------------------------------------------- */
1448 tmpfs_inactive(struct vop_inactive_args
*v
)
1450 struct vnode
*vp
= v
->a_vp
;
1452 struct tmpfs_node
*node
;
1454 KKASSERT(vn_islocked(vp
));
1456 node
= VP_TO_TMPFS_NODE(vp
);
1459 * Get rid of unreferenced deleted vnodes sooner rather than
1460 * later so the data memory can be recovered immediately.
1462 TMPFS_NODE_LOCK(node
);
1463 if (node
->tn_links
== 0 &&
1464 (node
->tn_vpstate
& TMPFS_VNODE_ALLOCATING
) == 0) {
1465 node
->tn_vpstate
= TMPFS_VNODE_DOOMED
;
1466 TMPFS_NODE_UNLOCK(node
);
1469 TMPFS_NODE_UNLOCK(node
);
1475 /* --------------------------------------------------------------------- */
1478 tmpfs_reclaim(struct vop_reclaim_args
*v
)
1480 struct vnode
*vp
= v
->a_vp
;
1481 struct tmpfs_mount
*tmp
;
1482 struct tmpfs_node
*node
;
1484 node
= VP_TO_TMPFS_NODE(vp
);
1485 tmp
= VFS_TO_TMPFS(vp
->v_mount
);
1489 /* If the node referenced by this vnode was deleted by the user,
1490 * we must free its associated data structures (now that the vnode
1491 * is being reclaimed). */
1492 TMPFS_NODE_LOCK(node
);
1493 if (node
->tn_links
== 0 &&
1494 (node
->tn_vpstate
& TMPFS_VNODE_ALLOCATING
) == 0) {
1495 node
->tn_vpstate
= TMPFS_VNODE_DOOMED
;
1496 tmpfs_free_node(tmp
, node
);
1499 TMPFS_NODE_UNLOCK(node
);
1502 KKASSERT(vp
->v_data
== NULL
);
1506 /* --------------------------------------------------------------------- */
1509 tmpfs_print(struct vop_print_args
*v
)
1511 struct vnode
*vp
= v
->a_vp
;
1513 struct tmpfs_node
*node
;
1515 node
= VP_TO_TMPFS_NODE(vp
);
1517 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1518 node
, node
->tn_flags
, node
->tn_links
);
1519 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1520 node
->tn_mode
, node
->tn_uid
, node
->tn_gid
,
1521 (uintmax_t)node
->tn_size
, node
->tn_status
);
1523 if (vp
->v_type
== VFIFO
)
1531 /* --------------------------------------------------------------------- */
1534 tmpfs_pathconf(struct vop_pathconf_args
*v
)
1536 int name
= v
->a_name
;
1537 register_t
*retval
= v
->a_retval
;
1560 case _PC_CHOWN_RESTRICTED
:
1572 case _PC_FILESIZEBITS
:
1573 *retval
= 0; /* XXX Don't know which value should I return. */
1583 /* --------------------------------------------------------------------- */
1586 * vnode operations vector used for files stored in a tmpfs file system.
1588 struct vop_ops tmpfs_vnode_vops
= {
1589 .vop_default
= vop_defaultop
,
1590 .vop_getpages
= vop_stdgetpages
,
1591 .vop_putpages
= vop_stdputpages
,
1592 .vop_ncreate
= tmpfs_ncreate
,
1593 .vop_nresolve
= tmpfs_nresolve
,
1594 .vop_nlookupdotdot
= tmpfs_nlookupdotdot
,
1595 .vop_nmknod
= tmpfs_nmknod
,
1596 .vop_open
= tmpfs_open
,
1597 .vop_close
= tmpfs_close
,
1598 .vop_access
= tmpfs_access
,
1599 .vop_getattr
= tmpfs_getattr
,
1600 .vop_setattr
= tmpfs_setattr
,
1601 .vop_read
= tmpfs_read
,
1602 .vop_write
= tmpfs_write
,
1603 .vop_fsync
= tmpfs_fsync
,
1604 .vop_nremove
= tmpfs_nremove
,
1605 .vop_nlink
= tmpfs_nlink
,
1606 .vop_nrename
= tmpfs_nrename
,
1607 .vop_nmkdir
= tmpfs_nmkdir
,
1608 .vop_nrmdir
= tmpfs_nrmdir
,
1609 .vop_nsymlink
= tmpfs_nsymlink
,
1610 .vop_readdir
= tmpfs_readdir
,
1611 .vop_readlink
= tmpfs_readlink
,
1612 .vop_inactive
= tmpfs_inactive
,
1613 .vop_reclaim
= tmpfs_reclaim
,
1614 .vop_print
= tmpfs_print
,
1615 .vop_pathconf
= tmpfs_pathconf
,
1616 .vop_bmap
= tmpfs_bmap
,
1617 .vop_bmap
= (void *)vop_eopnotsupp
,
1618 .vop_strategy
= tmpfs_strategy
,
1619 .vop_advlock
= tmpfs_advlock
,