2 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $
34 * tmpfs vnode interface.
37 #include <sys/kernel.h>
38 #include <sys/kern_syscall.h>
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
44 #include <sys/resourcevar.h>
45 #include <sys/sched.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vfsops.h>
50 #include <sys/vnode.h>
52 #include <sys/mplock2.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
58 #include <vm/swap_pager.h>
60 #include <vfs/fifofs/fifo.h>
61 #include <vfs/tmpfs/tmpfs_vnops.h>
62 #include <vfs/tmpfs/tmpfs.h>
64 MALLOC_DECLARE(M_TMPFS);
68 tmpfs_knote(struct vnode *vp, int flags)
71 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
75 /* --------------------------------------------------------------------- */
78 tmpfs_nresolve(struct vop_nresolve_args *v)
80 struct vnode *dvp = v->a_dvp;
81 struct vnode *vp = NULL;
82 struct namecache *ncp = v->a_nch->ncp;
83 struct tmpfs_node *tnode;
86 struct tmpfs_dirent *de;
87 struct tmpfs_node *dnode;
89 dnode = VP_TO_TMPFS_DIR(dvp);
91 de = tmpfs_dir_lookup(dnode, NULL, ncp);
96 * Allocate a vnode for the node we found.
99 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
100 LK_EXCLUSIVE | LK_RETRY, &vp);
108 * Store the result of this lookup in the cache. Avoid this if the
109 * request was for creation, as it does not improve timings on
114 cache_setvp(v->a_nch, vp);
116 } else if (error == ENOENT) {
117 cache_setvp(v->a_nch, NULL);
123 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
125 struct vnode *dvp = v->a_dvp;
126 struct vnode **vpp = v->a_vpp;
127 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
128 struct ucred *cred = v->a_cred;
132 /* Check accessibility of requested node as a first step. */
133 error = VOP_ACCESS(dvp, VEXEC, cred);
137 if (dnode->tn_dir.tn_parent != NULL) {
138 /* Allocate a new vnode on the matching entry. */
139 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
140 LK_EXCLUSIVE | LK_RETRY, vpp);
146 return (*vpp == NULL) ? ENOENT : 0;
149 /* --------------------------------------------------------------------- */
152 tmpfs_ncreate(struct vop_ncreate_args *v)
154 struct vnode *dvp = v->a_dvp;
155 struct vnode **vpp = v->a_vpp;
156 struct namecache *ncp = v->a_nch->ncp;
157 struct vattr *vap = v->a_vap;
158 struct ucred *cred = v->a_cred;
161 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
163 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
165 cache_setunresolved(v->a_nch);
166 cache_setvp(v->a_nch, *vpp);
167 tmpfs_knote(dvp, NOTE_WRITE);
172 /* --------------------------------------------------------------------- */
175 tmpfs_nmknod(struct vop_nmknod_args *v)
177 struct vnode *dvp = v->a_dvp;
178 struct vnode **vpp = v->a_vpp;
179 struct namecache *ncp = v->a_nch->ncp;
180 struct vattr *vap = v->a_vap;
181 struct ucred *cred = v->a_cred;
184 if (vap->va_type != VBLK && vap->va_type != VCHR &&
185 vap->va_type != VFIFO)
188 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
190 cache_setunresolved(v->a_nch);
191 cache_setvp(v->a_nch, *vpp);
192 tmpfs_knote(dvp, NOTE_WRITE);
198 /* --------------------------------------------------------------------- */
201 tmpfs_open(struct vop_open_args *v)
203 struct vnode *vp = v->a_vp;
204 int mode = v->a_mode;
207 struct tmpfs_node *node;
209 node = VP_TO_TMPFS_NODE(vp);
211 /* The file is still active but all its names have been removed
212 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
213 * it is about to die. */
214 if (node->tn_links < 1)
217 /* If the file is marked append-only, deny write requests. */
218 if ((node->tn_flags & APPEND) &&
219 (mode & (FWRITE | O_APPEND)) == FWRITE) {
222 return (vop_stdopen(v));
227 /* --------------------------------------------------------------------- */
230 tmpfs_close(struct vop_close_args *v)
232 struct vnode *vp = v->a_vp;
233 struct tmpfs_node *node;
235 node = VP_TO_TMPFS_NODE(vp);
237 if (node->tn_links > 0) {
238 /* Update node times. No need to do it if the node has
239 * been deleted, because it will vanish after we return. */
243 return vop_stdclose(v);
246 /* --------------------------------------------------------------------- */
249 tmpfs_access(struct vop_access_args *v)
251 struct vnode *vp = v->a_vp;
253 struct tmpfs_node *node;
255 node = VP_TO_TMPFS_NODE(vp);
257 switch (vp->v_type) {
263 if ((v->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
283 if ((v->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
288 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
295 /* --------------------------------------------------------------------- */
298 tmpfs_getattr(struct vop_getattr_args *v)
300 struct vnode *vp = v->a_vp;
301 struct vattr *vap = v->a_vap;
302 struct tmpfs_node *node;
304 node = VP_TO_TMPFS_NODE(vp);
308 vap->va_type = vp->v_type;
309 vap->va_mode = node->tn_mode;
310 vap->va_nlink = node->tn_links;
311 vap->va_uid = node->tn_uid;
312 vap->va_gid = node->tn_gid;
313 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
314 vap->va_fileid = node->tn_id;
315 vap->va_size = node->tn_size;
316 vap->va_blocksize = PAGE_SIZE;
317 vap->va_atime.tv_sec = node->tn_atime;
318 vap->va_atime.tv_nsec = node->tn_atimensec;
319 vap->va_mtime.tv_sec = node->tn_mtime;
320 vap->va_mtime.tv_nsec = node->tn_mtimensec;
321 vap->va_ctime.tv_sec = node->tn_ctime;
322 vap->va_ctime.tv_nsec = node->tn_ctimensec;
323 vap->va_gen = node->tn_gen;
324 vap->va_flags = node->tn_flags;
325 if (vp->v_type == VBLK || vp->v_type == VCHR)
327 vap->va_rmajor = umajor(node->tn_rdev);
328 vap->va_rminor = uminor(node->tn_rdev);
330 vap->va_bytes = round_page(node->tn_size);
336 /* --------------------------------------------------------------------- */
339 tmpfs_setattr(struct vop_setattr_args *v)
341 struct vnode *vp = v->a_vp;
342 struct vattr *vap = v->a_vap;
343 struct ucred *cred = v->a_cred;
344 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
348 if (error == 0 && (vap->va_flags != VNOVAL)) {
349 error = tmpfs_chflags(vp, vap->va_flags, cred);
350 kflags |= NOTE_ATTRIB;
353 if (error == 0 && (vap->va_size != VNOVAL)) {
354 if (vap->va_size > node->tn_size)
355 kflags |= NOTE_WRITE | NOTE_EXTEND;
357 kflags |= NOTE_WRITE;
358 error = tmpfs_chsize(vp, vap->va_size, cred);
361 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
362 vap->va_gid != (gid_t)VNOVAL)) {
363 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
364 kflags |= NOTE_ATTRIB;
367 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
368 error = tmpfs_chmod(vp, vap->va_mode, cred);
369 kflags |= NOTE_ATTRIB;
372 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
373 vap->va_atime.tv_nsec != VNOVAL) ||
374 (vap->va_mtime.tv_sec != VNOVAL &&
375 vap->va_mtime.tv_nsec != VNOVAL) )) {
376 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
377 vap->va_vaflags, cred);
378 kflags |= NOTE_ATTRIB;
381 /* Update the node times. We give preference to the error codes
382 * generated by this function rather than the ones that may arise
383 * from tmpfs_update. */
385 tmpfs_knote(vp, kflags);
390 /* --------------------------------------------------------------------- */
393 * fsync is usually a NOP, but we must take action when unmounting or
397 tmpfs_fsync(struct vop_fsync_args *v)
399 struct tmpfs_mount *tmp;
400 struct tmpfs_node *node;
401 struct vnode *vp = v->a_vp;
403 tmp = VFS_TO_TMPFS(vp->v_mount);
404 node = VP_TO_TMPFS_NODE(vp);
407 if (vp->v_type == VREG) {
408 if (vp->v_flag & VRECLAIMED) {
409 if (node->tn_links == 0)
410 tmpfs_truncate(vp, 0);
412 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
418 /* --------------------------------------------------------------------- */
421 tmpfs_read (struct vop_read_args *ap)
424 struct vnode *vp = ap->a_vp;
425 struct uio *uio = ap->a_uio;
426 struct tmpfs_node *node;
433 if (uio->uio_resid == 0) {
437 node = VP_TO_TMPFS_NODE(vp);
439 if (uio->uio_offset < 0)
441 if (vp->v_type != VREG)
444 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
446 * Use buffer cache I/O (via tmpfs_strategy)
448 offset = (size_t)uio->uio_offset & BMASK;
449 base_offset = (off_t)uio->uio_offset - offset;
450 bp = getcacheblk(vp, base_offset);
453 error = bread(vp, base_offset, BSIZE, &bp);
456 kprintf("tmpfs_read bread error %d\n", error);
462 * Figure out how many bytes we can actually copy this loop.
464 len = BSIZE - offset;
465 if (len > uio->uio_resid)
466 len = uio->uio_resid;
467 if (len > node->tn_size - uio->uio_offset)
468 len = (size_t)(node->tn_size - uio->uio_offset);
470 error = uiomove((char *)bp->b_data + offset, len, uio);
473 kprintf("tmpfs_read uiomove error %d\n", error);
478 TMPFS_NODE_LOCK(node);
479 node->tn_status |= TMPFS_NODE_ACCESSED;
480 TMPFS_NODE_UNLOCK(node);
486 tmpfs_write (struct vop_write_args *ap)
489 struct vnode *vp = ap->a_vp;
490 struct uio *uio = ap->a_uio;
491 struct thread *td = uio->uio_td;
492 struct tmpfs_node *node;
504 if (uio->uio_resid == 0) {
508 node = VP_TO_TMPFS_NODE(vp);
510 if (vp->v_type != VREG)
513 oldsize = node->tn_size;
514 if (ap->a_ioflag & IO_APPEND)
515 uio->uio_offset = node->tn_size;
518 * Check for illegal write offsets.
520 if (uio->uio_offset + uio->uio_resid >
521 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
524 if (vp->v_type == VREG && td != NULL) {
525 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
528 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
529 ksignal(td->td_proc, SIGXFSZ);
536 * Extend the file's size if necessary
538 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
542 while (uio->uio_resid > 0) {
544 * Use buffer cache I/O (via tmpfs_strategy)
546 offset = (size_t)uio->uio_offset & BMASK;
547 base_offset = (off_t)uio->uio_offset - offset;
548 len = BSIZE - offset;
549 if (len > uio->uio_resid)
550 len = uio->uio_resid;
552 if ((uio->uio_offset + len) > node->tn_size) {
553 trivial = (uio->uio_offset <= node->tn_size);
554 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
560 * Read to fill in any gaps. Theoretically we could
561 * optimize this if the write covers the entire buffer
562 * and is not a UIO_NOCOPY write, however this can lead
563 * to a security violation exposing random kernel memory
564 * (whatever junk was in the backing VM pages before).
566 * So just use bread() to do the right thing.
568 error = bread(vp, base_offset, BSIZE, &bp);
569 error = uiomove((char *)bp->b_data + offset, len, uio);
571 kprintf("tmpfs_write uiomove error %d\n", error);
576 if (uio->uio_offset > node->tn_size) {
577 node->tn_size = uio->uio_offset;
578 kflags |= NOTE_EXTEND;
580 kflags |= NOTE_WRITE;
583 * The data has been loaded into the buffer, write it out.
585 * We want tmpfs to be able to use all available ram, not
586 * just the buffer cache, so if not explicitly paging we
587 * use buwrite() to leave the buffer clean but mark all the
588 * VM pages valid+dirty.
590 * When the kernel is paging, either via normal pageout
591 * operation or when cleaning the object during a recycle,
592 * the underlying VM pages are going to get thrown away
593 * so we MUST write them to swap.
595 * XXX unfortunately this catches msync() system calls too
598 if (vm_swap_size == 0) {
600 * if swap isn't configured yet, force a buwrite() to
601 * avoid problems further down the line, due to flushing
606 if (ap->a_ioflag & IO_SYNC) {
608 } else if ((ap->a_ioflag & IO_ASYNC) ||
609 (uio->uio_segflg == UIO_NOCOPY)) {
617 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
626 (void)tmpfs_reg_resize(vp, oldsize, trivial);
627 kflags &= ~NOTE_EXTEND;
632 TMPFS_NODE_LOCK(node);
633 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
634 (extended? TMPFS_NODE_CHANGED : 0);
636 if (node->tn_mode & (S_ISUID | S_ISGID)) {
637 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
638 node->tn_mode &= ~(S_ISUID | S_ISGID);
640 TMPFS_NODE_UNLOCK(node);
643 tmpfs_knote(vp, kflags);
648 tmpfs_advlock (struct vop_advlock_args *ap)
650 struct tmpfs_node *node;
651 struct vnode *vp = ap->a_vp;
653 node = VP_TO_TMPFS_NODE(vp);
655 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
659 tmpfs_strategy(struct vop_strategy_args *ap)
661 struct bio *bio = ap->a_bio;
662 struct buf *bp = bio->bio_buf;
663 struct vnode *vp = ap->a_vp;
664 struct tmpfs_node *node;
667 if (vp->v_type != VREG) {
668 bp->b_resid = bp->b_bcount;
669 bp->b_flags |= B_ERROR | B_INVAL;
670 bp->b_error = EINVAL;
675 node = VP_TO_TMPFS_NODE(vp);
677 uobj = node->tn_reg.tn_aobj;
680 * Call swap_pager_strategy to read or write between the VM
681 * object and the buffer cache.
683 swap_pager_strategy(uobj, bio);
689 tmpfs_bmap(struct vop_bmap_args *ap)
691 if (ap->a_doffsetp != NULL)
692 *ap->a_doffsetp = ap->a_loffset;
693 if (ap->a_runp != NULL)
695 if (ap->a_runb != NULL)
701 /* --------------------------------------------------------------------- */
704 tmpfs_nremove(struct vop_nremove_args *v)
706 struct vnode *dvp = v->a_dvp;
707 struct namecache *ncp = v->a_nch->ncp;
710 struct tmpfs_dirent *de;
711 struct tmpfs_mount *tmp;
712 struct tmpfs_node *dnode;
713 struct tmpfs_node *node;
716 * We have to acquire the vp from v->a_nch because
717 * we will likely unresolve the namecache entry, and
718 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
719 * sequence to recover space from the file.
721 error = cache_vref(v->a_nch, v->a_cred, &vp);
722 KKASSERT(error == 0);
724 if (vp->v_type == VDIR) {
729 dnode = VP_TO_TMPFS_DIR(dvp);
730 node = VP_TO_TMPFS_NODE(vp);
731 tmp = VFS_TO_TMPFS(vp->v_mount);
732 de = tmpfs_dir_lookup(dnode, node, ncp);
738 /* Files marked as immutable or append-only cannot be deleted. */
739 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
740 (dnode->tn_flags & APPEND)) {
745 /* Remove the entry from the directory; as it is a file, we do not
746 * have to change the number of hard links of the directory. */
747 tmpfs_dir_detach(dnode, de);
749 /* Free the directory entry we just deleted. Note that the node
750 * referred by it will not be removed until the vnode is really
752 tmpfs_free_dirent(tmp, de);
754 if (node->tn_links > 0) {
755 TMPFS_NODE_LOCK(node);
756 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
758 TMPFS_NODE_UNLOCK(node);
761 cache_setunresolved(v->a_nch);
762 cache_setvp(v->a_nch, NULL);
763 tmpfs_knote(vp, NOTE_DELETE);
764 /*cache_inval_vp(vp, CINV_DESTROY);*/
765 tmpfs_knote(dvp, NOTE_WRITE);
774 /* --------------------------------------------------------------------- */
777 tmpfs_nlink(struct vop_nlink_args *v)
779 struct vnode *dvp = v->a_dvp;
780 struct vnode *vp = v->a_vp;
781 struct namecache *ncp = v->a_nch->ncp;
782 struct tmpfs_dirent *de;
783 struct tmpfs_node *node;
784 struct tmpfs_node *dnode;
787 KKASSERT(dvp != vp); /* XXX When can this be false? */
789 node = VP_TO_TMPFS_NODE(vp);
790 dnode = VP_TO_TMPFS_NODE(dvp);
792 /* XXX: Why aren't the following two tests done by the caller? */
794 /* Hard links of directories are forbidden. */
795 if (vp->v_type == VDIR) {
800 /* Cannot create cross-device links. */
801 if (dvp->v_mount != vp->v_mount) {
806 /* Ensure that we do not overflow the maximum number of links imposed
808 KKASSERT(node->tn_links <= LINK_MAX);
809 if (node->tn_links == LINK_MAX) {
814 /* We cannot create links of files marked immutable or append-only. */
815 if (node->tn_flags & (IMMUTABLE | APPEND)) {
820 /* Allocate a new directory entry to represent the node. */
821 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
822 ncp->nc_name, ncp->nc_nlen, &de);
826 /* Insert the new directory entry into the appropriate directory. */
827 tmpfs_dir_attach(dnode, de);
829 /* vp link count has changed, so update node times. */
831 TMPFS_NODE_LOCK(node);
832 node->tn_status |= TMPFS_NODE_CHANGED;
833 TMPFS_NODE_UNLOCK(node);
836 tmpfs_knote(vp, NOTE_LINK);
837 cache_setunresolved(v->a_nch);
838 cache_setvp(v->a_nch, vp);
839 tmpfs_knote(dvp, NOTE_WRITE);
846 /* --------------------------------------------------------------------- */
849 tmpfs_nrename(struct vop_nrename_args *v)
851 struct vnode *fdvp = v->a_fdvp;
852 struct namecache *fncp = v->a_fnch->ncp;
853 struct vnode *fvp = fncp->nc_vp;
854 struct vnode *tdvp = v->a_tdvp;
855 struct namecache *tncp = v->a_tnch->ncp;
856 struct vnode *tvp = tncp->nc_vp;
857 struct tmpfs_dirent *de;
858 struct tmpfs_mount *tmp;
859 struct tmpfs_node *fdnode;
860 struct tmpfs_node *fnode;
861 struct tmpfs_node *tnode;
862 struct tmpfs_node *tdnode;
867 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
869 /* Disallow cross-device renames.
870 * XXX Why isn't this done by the caller? */
871 if (fvp->v_mount != tdvp->v_mount ||
872 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
877 tmp = VFS_TO_TMPFS(tdvp->v_mount);
878 tdnode = VP_TO_TMPFS_DIR(tdvp);
880 /* If source and target are the same file, there is nothing to do. */
886 fdnode = VP_TO_TMPFS_DIR(fdvp);
887 fnode = VP_TO_TMPFS_NODE(fvp);
888 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
890 /* Avoid manipulating '.' and '..' entries. */
895 KKASSERT(de->td_node == fnode);
898 * If replacing an entry in the target directory and that entry
899 * is a directory, it must be empty.
901 * Kern_rename gurantees the destination to be a directory
902 * if the source is one (it does?).
905 KKASSERT(tnode != NULL);
907 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
908 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
913 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
914 if (tnode->tn_size > 0) {
918 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
921 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
925 KKASSERT(fnode->tn_type != VDIR &&
926 tnode->tn_type != VDIR);
930 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
931 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
937 * Ensure that we have enough memory to hold the new name, if it
940 if (fncp->nc_nlen != tncp->nc_nlen ||
941 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
942 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
943 M_WAITOK | M_NULLOK);
944 if (newname == NULL) {
948 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
949 newname[tncp->nc_nlen] = '\0';
955 * Unlink entry from source directory. Note that the kernel has
956 * already checked for illegal recursion cases (renaming a directory
957 * into a subdirectory of itself).
959 if (fdnode != tdnode)
960 tmpfs_dir_detach(fdnode, de);
963 * Handle any name change. Swap with newname, we will
964 * deallocate it at the end.
966 if (newname != NULL) {
968 TMPFS_NODE_LOCK(fnode);
969 fnode->tn_status |= TMPFS_NODE_CHANGED;
970 TMPFS_NODE_UNLOCK(fnode);
972 oldname = de->td_name;
973 de->td_name = newname;
974 de->td_namelen = (uint16_t)tncp->nc_nlen;
979 * Link entry to target directory. If the entry
980 * represents a directory move the parent linkage
983 if (fdnode != tdnode) {
984 if (de->td_node->tn_type == VDIR) {
985 TMPFS_VALIDATE_DIR(fnode);
987 TMPFS_NODE_LOCK(tdnode);
989 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
990 TMPFS_NODE_UNLOCK(tdnode);
992 TMPFS_NODE_LOCK(fnode);
993 fnode->tn_dir.tn_parent = tdnode;
994 fnode->tn_status |= TMPFS_NODE_CHANGED;
995 TMPFS_NODE_UNLOCK(fnode);
997 TMPFS_NODE_LOCK(fdnode);
999 fdnode->tn_status |= TMPFS_NODE_MODIFIED;
1000 TMPFS_NODE_UNLOCK(fdnode);
1002 tmpfs_dir_attach(tdnode, de);
1004 TMPFS_NODE_LOCK(tdnode);
1005 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1006 TMPFS_NODE_UNLOCK(tdnode);
1010 * If we are overwriting an entry, we have to remove the old one
1011 * from the target directory.
1014 /* Remove the old entry from the target directory. */
1015 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1016 tmpfs_dir_detach(tdnode, de);
1017 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1020 * Free the directory entry we just deleted. Note that the
1021 * node referred by it will not be removed until the vnode is
1024 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1025 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1032 kfree(newname, tmp->tm_name_zone);
1035 cache_rename(v->a_fnch, v->a_tnch);
1036 tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1037 tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1038 if (fnode->tn_vnode)
1039 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1046 /* Release target nodes. */
1047 /* XXX: I don't understand when tdvp can be the same as tvp, but
1048 * other code takes care of this... */
1055 /* --------------------------------------------------------------------- */
1058 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1060 struct vnode *dvp = v->a_dvp;
1061 struct vnode **vpp = v->a_vpp;
1062 struct namecache *ncp = v->a_nch->ncp;
1063 struct vattr *vap = v->a_vap;
1064 struct ucred *cred = v->a_cred;
1067 KKASSERT(vap->va_type == VDIR);
1069 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1071 cache_setunresolved(v->a_nch);
1072 cache_setvp(v->a_nch, *vpp);
1073 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1079 /* --------------------------------------------------------------------- */
1082 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1084 struct vnode *dvp = v->a_dvp;
1085 struct namecache *ncp = v->a_nch->ncp;
1087 struct tmpfs_dirent *de;
1088 struct tmpfs_mount *tmp;
1089 struct tmpfs_node *dnode;
1090 struct tmpfs_node *node;
1094 * We have to acquire the vp from v->a_nch because
1095 * we will likely unresolve the namecache entry, and
1096 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1099 error = cache_vref(v->a_nch, v->a_cred, &vp);
1100 KKASSERT(error == 0);
1103 * Prevalidate so we don't hit an assertion later
1105 if (vp->v_type != VDIR) {
1110 tmp = VFS_TO_TMPFS(dvp->v_mount);
1111 dnode = VP_TO_TMPFS_DIR(dvp);
1112 node = VP_TO_TMPFS_DIR(vp);
1114 /* Directories with more than two entries ('.' and '..') cannot be
1116 if (node->tn_size > 0) {
1121 if ((dnode->tn_flags & APPEND)
1122 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1127 /* This invariant holds only if we are not trying to remove "..".
1128 * We checked for that above so this is safe now. */
1129 KKASSERT(node->tn_dir.tn_parent == dnode);
1131 /* Get the directory entry associated with node (vp). This was
1132 * filled by tmpfs_lookup while looking up the entry. */
1133 de = tmpfs_dir_lookup(dnode, node, ncp);
1134 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1138 /* Check flags to see if we are allowed to remove the directory. */
1139 if ((dnode->tn_flags & APPEND) ||
1140 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1146 /* Detach the directory entry from the directory (dnode). */
1147 tmpfs_dir_detach(dnode, de);
1149 /* No vnode should be allocated for this entry from this point */
1150 TMPFS_NODE_LOCK(node);
1151 TMPFS_ASSERT_ELOCKED(node);
1152 TMPFS_NODE_LOCK(dnode);
1153 TMPFS_ASSERT_ELOCKED(dnode);
1156 /* handled by tmpfs_free_node */
1157 KKASSERT(node->tn_links > 0);
1159 node->tn_dir.tn_parent = NULL;
1161 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1162 TMPFS_NODE_MODIFIED;
1165 /* handled by tmpfs_free_node */
1166 KKASSERT(dnode->tn_links > 0);
1169 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1170 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1172 TMPFS_NODE_UNLOCK(dnode);
1173 TMPFS_NODE_UNLOCK(node);
1175 /* Free the directory entry we just deleted. Note that the node
1176 * referred by it will not be removed until the vnode is really
1178 tmpfs_free_dirent(tmp, de);
1180 /* Release the deleted vnode (will destroy the node, notify
1181 * interested parties and clean it from the cache). */
1183 TMPFS_NODE_LOCK(dnode);
1184 dnode->tn_status |= TMPFS_NODE_CHANGED;
1185 TMPFS_NODE_UNLOCK(dnode);
1188 cache_setunresolved(v->a_nch);
1189 cache_setvp(v->a_nch, NULL);
1190 /*cache_inval_vp(vp, CINV_DESTROY);*/
1191 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1200 /* --------------------------------------------------------------------- */
1203 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1205 struct vnode *dvp = v->a_dvp;
1206 struct vnode **vpp = v->a_vpp;
1207 struct namecache *ncp = v->a_nch->ncp;
1208 struct vattr *vap = v->a_vap;
1209 struct ucred *cred = v->a_cred;
1210 char *target = v->a_target;
1213 vap->va_type = VLNK;
1214 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1216 tmpfs_knote(*vpp, NOTE_WRITE);
1217 cache_setunresolved(v->a_nch);
1218 cache_setvp(v->a_nch, *vpp);
1224 /* --------------------------------------------------------------------- */
1227 tmpfs_readdir(struct vop_readdir_args *v)
1229 struct vnode *vp = v->a_vp;
1230 struct uio *uio = v->a_uio;
1231 int *eofflag = v->a_eofflag;
1232 off_t **cookies = v->a_cookies;
1233 int *ncookies = v->a_ncookies;
1234 struct tmpfs_mount *tmp;
1238 struct tmpfs_node *node;
1240 /* This operation only makes sense on directory nodes. */
1241 if (vp->v_type != VDIR)
1244 tmp = VFS_TO_TMPFS(vp->v_mount);
1245 node = VP_TO_TMPFS_DIR(vp);
1246 startoff = uio->uio_offset;
1248 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1249 error = tmpfs_dir_getdotdent(node, uio);
1255 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1256 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1262 error = tmpfs_dir_getdents(node, uio, &cnt);
1265 KKASSERT(error >= -1);
1270 if (eofflag != NULL)
1272 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1274 /* Update NFS-related variables. */
1275 if (error == 0 && cookies != NULL && ncookies != NULL) {
1277 off_t off = startoff;
1278 struct tmpfs_dirent *de = NULL;
1281 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1283 for (i = 0; i < cnt; i++) {
1284 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1285 if (off == TMPFS_DIRCOOKIE_DOT) {
1286 off = TMPFS_DIRCOOKIE_DOTDOT;
1288 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1289 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1290 } else if (de != NULL) {
1291 de = TAILQ_NEXT(de, td_entries);
1293 de = tmpfs_dir_lookupbycookie(node,
1295 KKASSERT(de != NULL);
1296 de = TAILQ_NEXT(de, td_entries);
1299 off = TMPFS_DIRCOOKIE_EOF;
1301 off = tmpfs_dircookie(de);
1304 (*cookies)[i] = off;
1306 KKASSERT(uio->uio_offset == off);
1312 /* --------------------------------------------------------------------- */
1315 tmpfs_readlink(struct vop_readlink_args *v)
1317 struct vnode *vp = v->a_vp;
1318 struct uio *uio = v->a_uio;
1321 struct tmpfs_node *node;
1323 KKASSERT(uio->uio_offset == 0);
1324 KKASSERT(vp->v_type == VLNK);
1326 node = VP_TO_TMPFS_NODE(vp);
1328 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1330 TMPFS_NODE_LOCK(node);
1331 node->tn_status |= TMPFS_NODE_ACCESSED;
1332 TMPFS_NODE_UNLOCK(node);
1337 /* --------------------------------------------------------------------- */
1340 tmpfs_inactive(struct vop_inactive_args *v)
1342 struct vnode *vp = v->a_vp;
1344 struct tmpfs_node *node;
1346 node = VP_TO_TMPFS_NODE(vp);
1349 * Get rid of unreferenced deleted vnodes sooner rather than
1350 * later so the data memory can be recovered immediately.
1352 * We must truncate the vnode to prevent the normal reclamation
1353 * path from flushing the data for the removed file to disk.
1355 TMPFS_NODE_LOCK(node);
1356 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1357 (node->tn_links == 0 ||
1358 (node->tn_links == 1 && node->tn_type == VDIR &&
1359 node->tn_dir.tn_parent)))
1361 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1362 TMPFS_NODE_UNLOCK(node);
1363 if (node->tn_type == VREG)
1364 tmpfs_truncate(vp, 0);
1367 TMPFS_NODE_UNLOCK(node);
1373 /* --------------------------------------------------------------------- */
1376 tmpfs_reclaim(struct vop_reclaim_args *v)
1378 struct vnode *vp = v->a_vp;
1379 struct tmpfs_mount *tmp;
1380 struct tmpfs_node *node;
1382 node = VP_TO_TMPFS_NODE(vp);
1383 tmp = VFS_TO_TMPFS(vp->v_mount);
1388 * If the node referenced by this vnode was deleted by the
1389 * user, we must free its associated data structures now that
1390 * the vnode is being reclaimed.
1392 * Directories have an extra link ref.
1394 TMPFS_NODE_LOCK(node);
1395 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1396 (node->tn_links == 0 ||
1397 (node->tn_links == 1 && node->tn_type == VDIR &&
1398 node->tn_dir.tn_parent)))
1400 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1401 tmpfs_free_node(tmp, node);
1404 TMPFS_NODE_UNLOCK(node);
1407 KKASSERT(vp->v_data == NULL);
1411 /* --------------------------------------------------------------------- */
1414 tmpfs_print(struct vop_print_args *v)
1416 struct vnode *vp = v->a_vp;
1418 struct tmpfs_node *node;
1420 node = VP_TO_TMPFS_NODE(vp);
1422 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1423 node, node->tn_flags, node->tn_links);
1424 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1425 node->tn_mode, node->tn_uid, node->tn_gid,
1426 (uintmax_t)node->tn_size, node->tn_status);
1428 if (vp->v_type == VFIFO)
1436 /* --------------------------------------------------------------------- */
1439 tmpfs_pathconf(struct vop_pathconf_args *v)
1441 int name = v->a_name;
1442 register_t *retval = v->a_retval;
1465 case _PC_CHOWN_RESTRICTED:
1477 case _PC_FILESIZEBITS:
1478 *retval = 0; /* XXX Don't know which value should I return. */
1488 /************************************************************************
1490 ************************************************************************/
1492 static void filt_tmpfsdetach(struct knote *kn);
1493 static int filt_tmpfsread(struct knote *kn, long hint);
1494 static int filt_tmpfswrite(struct knote *kn, long hint);
1495 static int filt_tmpfsvnode(struct knote *kn, long hint);
1497 static struct filterops tmpfsread_filtops =
1498 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsread };
1499 static struct filterops tmpfswrite_filtops =
1500 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfswrite };
1501 static struct filterops tmpfsvnode_filtops =
1502 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1505 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1507 struct vnode *vp = ap->a_vp;
1508 struct knote *kn = ap->a_kn;
1510 switch (kn->kn_filter) {
1512 kn->kn_fop = &tmpfsread_filtops;
1515 kn->kn_fop = &tmpfswrite_filtops;
1518 kn->kn_fop = &tmpfsvnode_filtops;
1521 return (EOPNOTSUPP);
1524 kn->kn_hook = (caddr_t)vp;
1526 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1532 filt_tmpfsdetach(struct knote *kn)
1534 struct vnode *vp = (void *)kn->kn_hook;
1536 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1540 filt_tmpfsread(struct knote *kn, long hint)
1542 struct vnode *vp = (void *)kn->kn_hook;
1543 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1546 if (hint == NOTE_REVOKE) {
1547 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1550 off = node->tn_size - kn->kn_fp->f_offset;
1551 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1552 if (kn->kn_sfflags & NOTE_OLDAPI)
1556 * Handle possible MP race interlock on filter check/write
1558 if (kn->kn_data == 0) {
1560 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1563 return (kn->kn_data != 0);
1567 filt_tmpfswrite(struct knote *kn, long hint)
1569 if (hint == NOTE_REVOKE)
1570 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1576 filt_tmpfsvnode(struct knote *kn, long hint)
1578 if (kn->kn_sfflags & hint)
1579 kn->kn_fflags |= hint;
1580 if (hint == NOTE_REVOKE) {
1581 kn->kn_flags |= EV_EOF;
1584 return (kn->kn_fflags != 0);
1588 /* --------------------------------------------------------------------- */
1591 * vnode operations vector used for files stored in a tmpfs file system.
1593 struct vop_ops tmpfs_vnode_vops = {
1594 .vop_default = vop_defaultop,
1595 .vop_getpages = vop_stdgetpages,
1596 .vop_putpages = vop_stdputpages,
1597 .vop_ncreate = tmpfs_ncreate,
1598 .vop_nresolve = tmpfs_nresolve,
1599 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1600 .vop_nmknod = tmpfs_nmknod,
1601 .vop_open = tmpfs_open,
1602 .vop_close = tmpfs_close,
1603 .vop_access = tmpfs_access,
1604 .vop_getattr = tmpfs_getattr,
1605 .vop_setattr = tmpfs_setattr,
1606 .vop_read = tmpfs_read,
1607 .vop_write = tmpfs_write,
1608 .vop_fsync = tmpfs_fsync,
1609 .vop_nremove = tmpfs_nremove,
1610 .vop_nlink = tmpfs_nlink,
1611 .vop_nrename = tmpfs_nrename,
1612 .vop_nmkdir = tmpfs_nmkdir,
1613 .vop_nrmdir = tmpfs_nrmdir,
1614 .vop_nsymlink = tmpfs_nsymlink,
1615 .vop_readdir = tmpfs_readdir,
1616 .vop_readlink = tmpfs_readlink,
1617 .vop_inactive = tmpfs_inactive,
1618 .vop_reclaim = tmpfs_reclaim,
1619 .vop_print = tmpfs_print,
1620 .vop_pathconf = tmpfs_pathconf,
1621 .vop_bmap = tmpfs_bmap,
1622 .vop_strategy = tmpfs_strategy,
1623 .vop_advlock = tmpfs_advlock,
1624 .vop_kqfilter = tmpfs_kqfilter