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>
48 #include <sys/systm.h>
49 #include <sys/unistd.h>
50 #include <sys/vfsops.h>
51 #include <sys/vnode.h>
53 #include <sys/mplock2.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
59 #include <vm/swap_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 tmpfs_node *tnode;
78 struct tmpfs_dirent *de;
79 struct tmpfs_node *dnode;
81 dnode = VP_TO_TMPFS_DIR(dvp);
83 de = tmpfs_dir_lookup(dnode, NULL, ncp);
88 * Allocate a vnode for the node we found.
91 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
92 LK_EXCLUSIVE | LK_RETRY, &vp);
100 * Store the result of this lookup in the cache. Avoid this if the
101 * request was for creation, as it does not improve timings on
106 cache_setvp(v->a_nch, vp);
108 } else if (error == ENOENT) {
109 cache_setvp(v->a_nch, NULL);
115 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
117 struct vnode *dvp = v->a_dvp;
118 struct vnode **vpp = v->a_vpp;
119 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
120 struct ucred *cred = v->a_cred;
124 /* Check accessibility of requested node as a first step. */
125 error = VOP_ACCESS(dvp, VEXEC, cred);
129 if (dnode->tn_dir.tn_parent != NULL) {
130 /* Allocate a new vnode on the matching entry. */
131 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
132 LK_EXCLUSIVE | LK_RETRY, vpp);
138 return (*vpp == NULL) ? ENOENT : 0;
141 /* --------------------------------------------------------------------- */
144 tmpfs_ncreate(struct vop_ncreate_args *v)
146 struct vnode *dvp = v->a_dvp;
147 struct vnode **vpp = v->a_vpp;
148 struct namecache *ncp = v->a_nch->ncp;
149 struct vattr *vap = v->a_vap;
150 struct ucred *cred = v->a_cred;
153 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
155 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
157 cache_setunresolved(v->a_nch);
158 cache_setvp(v->a_nch, *vpp);
163 /* --------------------------------------------------------------------- */
166 tmpfs_nmknod(struct vop_nmknod_args *v)
168 struct vnode *dvp = v->a_dvp;
169 struct vnode **vpp = v->a_vpp;
170 struct namecache *ncp = v->a_nch->ncp;
171 struct vattr *vap = v->a_vap;
172 struct ucred *cred = v->a_cred;
175 if (vap->va_type != VBLK && vap->va_type != VCHR &&
176 vap->va_type != VFIFO)
179 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
181 cache_setunresolved(v->a_nch);
182 cache_setvp(v->a_nch, *vpp);
188 /* --------------------------------------------------------------------- */
191 tmpfs_open(struct vop_open_args *v)
193 struct vnode *vp = v->a_vp;
194 int mode = v->a_mode;
197 struct tmpfs_node *node;
199 node = VP_TO_TMPFS_NODE(vp);
201 /* The file is still active but all its names have been removed
202 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
203 * it is about to die. */
204 if (node->tn_links < 1)
207 /* If the file is marked append-only, deny write requests. */
208 if ((node->tn_flags & APPEND) &&
209 (mode & (FWRITE | O_APPEND)) == FWRITE) {
212 return (vop_stdopen(v));
217 /* --------------------------------------------------------------------- */
220 tmpfs_close(struct vop_close_args *v)
222 struct vnode *vp = v->a_vp;
223 struct tmpfs_node *node;
225 node = VP_TO_TMPFS_NODE(vp);
227 if (node->tn_links > 0) {
228 /* Update node times. No need to do it if the node has
229 * been deleted, because it will vanish after we return. */
233 return vop_stdclose(v);
236 /* --------------------------------------------------------------------- */
239 tmpfs_access(struct vop_access_args *v)
241 struct vnode *vp = v->a_vp;
243 struct tmpfs_node *node;
245 node = VP_TO_TMPFS_NODE(vp);
247 switch (vp->v_type) {
253 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
273 if (VWRITE && node->tn_flags & IMMUTABLE) {
278 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
285 /* --------------------------------------------------------------------- */
288 tmpfs_getattr(struct vop_getattr_args *v)
290 struct vnode *vp = v->a_vp;
291 struct vattr *vap = v->a_vap;
292 struct tmpfs_node *node;
294 node = VP_TO_TMPFS_NODE(vp);
298 vap->va_type = vp->v_type;
299 vap->va_mode = node->tn_mode;
300 vap->va_nlink = node->tn_links;
301 vap->va_uid = node->tn_uid;
302 vap->va_gid = node->tn_gid;
303 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
304 vap->va_fileid = node->tn_id;
305 vap->va_size = node->tn_size;
306 vap->va_blocksize = PAGE_SIZE;
307 vap->va_atime.tv_sec = node->tn_atime;
308 vap->va_atime.tv_nsec = node->tn_atimensec;
309 vap->va_mtime.tv_sec = node->tn_mtime;
310 vap->va_mtime.tv_nsec = node->tn_mtimensec;
311 vap->va_ctime.tv_sec = node->tn_ctime;
312 vap->va_ctime.tv_nsec = node->tn_ctimensec;
313 vap->va_gen = node->tn_gen;
314 vap->va_flags = node->tn_flags;
315 if (vp->v_type == VBLK || vp->v_type == VCHR)
317 vap->va_rmajor = umajor(node->tn_rdev);
318 vap->va_rminor = uminor(node->tn_rdev);
320 vap->va_bytes = round_page(node->tn_size);
326 /* --------------------------------------------------------------------- */
329 tmpfs_setattr(struct vop_setattr_args *v)
331 struct vnode *vp = v->a_vp;
332 struct vattr *vap = v->a_vap;
333 struct ucred *cred = v->a_cred;
336 if (error == 0 && (vap->va_flags != VNOVAL))
337 error = tmpfs_chflags(vp, vap->va_flags, cred);
339 if (error == 0 && (vap->va_size != VNOVAL))
340 error = tmpfs_chsize(vp, vap->va_size, cred);
342 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
343 vap->va_gid != (gid_t)VNOVAL)) {
344 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
347 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
348 error = tmpfs_chmod(vp, vap->va_mode, cred);
350 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
351 vap->va_atime.tv_nsec != VNOVAL) ||
352 (vap->va_mtime.tv_sec != VNOVAL &&
353 vap->va_mtime.tv_nsec != VNOVAL) )) {
354 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
355 vap->va_vaflags, cred);
358 /* Update the node times. We give preference to the error codes
359 * generated by this function rather than the ones that may arise
360 * from tmpfs_update. */
366 /* --------------------------------------------------------------------- */
369 * fsync is usually a NOP, but we must take action when unmounting or
373 tmpfs_fsync(struct vop_fsync_args *v)
375 struct tmpfs_mount *tmp;
376 struct tmpfs_node *node;
377 struct vnode *vp = v->a_vp;
379 tmp = VFS_TO_TMPFS(vp->v_mount);
380 node = VP_TO_TMPFS_NODE(vp);
383 if (vp->v_type == VREG) {
384 if (vp->v_flag & VRECLAIMED) {
385 if (node->tn_links == 0)
386 tmpfs_truncate(vp, 0);
388 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
394 /* --------------------------------------------------------------------- */
397 tmpfs_read (struct vop_read_args *ap)
400 struct vnode *vp = ap->a_vp;
401 struct uio *uio = ap->a_uio;
402 struct tmpfs_node *node;
410 if (uio->uio_resid == 0) {
414 node = VP_TO_TMPFS_NODE(vp);
416 if (uio->uio_offset < 0)
418 if (vp->v_type != VREG)
422 if(curthread->td_mpcount)
430 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
432 * Use buffer cache I/O (via tmpfs_strategy)
434 offset = (size_t)uio->uio_offset & BMASK;
435 base_offset = (off_t)uio->uio_offset - offset;
436 bp = getcacheblk(vp, base_offset);
439 if (got_mplock == 0) {
444 error = bread(vp, base_offset, BSIZE, &bp);
447 kprintf("tmpfs_read bread error %d\n", error);
452 if (got_mplock == 0) {
458 * Figure out how many bytes we can actually copy this loop.
460 len = BSIZE - offset;
461 if (len > uio->uio_resid)
462 len = uio->uio_resid;
463 if (len > node->tn_size - uio->uio_offset)
464 len = (size_t)(node->tn_size - uio->uio_offset);
466 error = uiomove((char *)bp->b_data + offset, len, uio);
469 kprintf("tmpfs_read uiomove error %d\n", error);
477 TMPFS_NODE_LOCK(node);
478 node->tn_status |= TMPFS_NODE_ACCESSED;
479 TMPFS_NODE_UNLOCK(node);
485 tmpfs_write (struct vop_write_args *ap)
488 struct vnode *vp = ap->a_vp;
489 struct uio *uio = ap->a_uio;
490 struct thread *td = uio->uio_td;
491 struct tmpfs_node *node;
503 if (uio->uio_resid == 0) {
507 node = VP_TO_TMPFS_NODE(vp);
509 if (vp->v_type != VREG)
512 oldsize = node->tn_size;
513 if (ap->a_ioflag & IO_APPEND)
514 uio->uio_offset = node->tn_size;
517 * Check for illegal write offsets.
519 if (uio->uio_offset + uio->uio_resid >
520 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
523 if (vp->v_type == VREG && td != NULL) {
524 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
527 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
528 ksignal(td->td_proc, SIGXFSZ);
535 * Extend the file's size if necessary
537 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
540 if (curthread->td_mpcount) {
549 while (uio->uio_resid > 0) {
551 * Use buffer cache I/O (via tmpfs_strategy)
553 offset = (size_t)uio->uio_offset & BMASK;
554 base_offset = (off_t)uio->uio_offset - offset;
555 len = BSIZE - offset;
556 if (len > uio->uio_resid)
557 len = uio->uio_resid;
559 if ((uio->uio_offset + len) > node->tn_size) {
560 trivial = (uio->uio_offset <= node->tn_size);
561 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
567 * Read to fill in any gaps. Theoretically we could
568 * optimize this if the write covers the entire buffer
569 * and is not a UIO_NOCOPY write, however this can lead
570 * to a security violation exposing random kernel memory
571 * (whatever junk was in the backing VM pages before).
573 * So just use bread() to do the right thing.
575 error = bread(vp, base_offset, BSIZE, &bp);
576 error = uiomove((char *)bp->b_data + offset, len, uio);
578 kprintf("tmpfs_write uiomove error %d\n", error);
583 if (uio->uio_offset > node->tn_size)
584 node->tn_size = uio->uio_offset;
587 * The data has been loaded into the buffer, write it out.
589 * We want tmpfs to be able to use all available ram, not
590 * just the buffer cache, so if not explicitly paging we
591 * use buwrite() to leave the buffer clean but mark all the
592 * VM pages valid+dirty.
594 * When the kernel is paging, either via normal pageout
595 * operation or when cleaning the object during a recycle,
596 * the underlying VM pages are going to get thrown away
597 * so we MUST write them to swap.
599 * XXX unfortunately this catches msync() system calls too
602 if (vm_swap_size == 0) {
604 * if swap isn't configured yet, force a buwrite() to
605 * avoid problems further down the line, due to flushing
610 if (ap->a_ioflag & IO_SYNC) {
612 } else if ((ap->a_ioflag & IO_ASYNC) ||
613 (uio->uio_segflg == UIO_NOCOPY)) {
621 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
631 (void)tmpfs_reg_resize(vp, oldsize, trivial);
635 TMPFS_NODE_LOCK(node);
636 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
637 (extended? TMPFS_NODE_CHANGED : 0);
639 if (node->tn_mode & (S_ISUID | S_ISGID)) {
640 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
641 node->tn_mode &= ~(S_ISUID | S_ISGID);
643 TMPFS_NODE_UNLOCK(node);
649 tmpfs_advlock (struct vop_advlock_args *ap)
651 struct tmpfs_node *node;
652 struct vnode *vp = ap->a_vp;
654 node = VP_TO_TMPFS_NODE(vp);
656 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
661 tmpfs_strategy(struct vop_strategy_args *ap)
663 struct bio *bio = ap->a_bio;
664 struct buf *bp = bio->bio_buf;
665 struct vnode *vp = ap->a_vp;
666 struct tmpfs_node *node;
669 if (vp->v_type != VREG) {
670 bp->b_resid = bp->b_bcount;
671 bp->b_flags |= B_ERROR | B_INVAL;
672 bp->b_error = EINVAL;
677 node = VP_TO_TMPFS_NODE(vp);
679 uobj = node->tn_reg.tn_aobj;
682 * Call swap_pager_strategy to read or write between the VM
683 * object and the buffer cache.
685 swap_pager_strategy(uobj, bio);
691 tmpfs_bmap(struct vop_bmap_args *ap)
693 if (ap->a_doffsetp != NULL)
694 *ap->a_doffsetp = ap->a_loffset;
695 if (ap->a_runp != NULL)
697 if (ap->a_runb != NULL)
703 /* --------------------------------------------------------------------- */
706 tmpfs_nremove(struct vop_nremove_args *v)
708 struct vnode *dvp = v->a_dvp;
709 struct namecache *ncp = v->a_nch->ncp;
712 struct tmpfs_dirent *de;
713 struct tmpfs_mount *tmp;
714 struct tmpfs_node *dnode;
715 struct tmpfs_node *node;
718 * We have to acquire the vp from v->a_nch because
719 * we will likely unresolve the namecache entry, and
720 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
721 * sequence to recover space from the file.
723 error = cache_vref(v->a_nch, v->a_cred, &vp);
724 KKASSERT(error == 0);
726 if (vp->v_type == VDIR) {
731 dnode = VP_TO_TMPFS_DIR(dvp);
732 node = VP_TO_TMPFS_NODE(vp);
733 tmp = VFS_TO_TMPFS(vp->v_mount);
734 de = tmpfs_dir_lookup(dnode, node, ncp);
740 /* Files marked as immutable or append-only cannot be deleted. */
741 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
742 (dnode->tn_flags & APPEND)) {
747 /* Remove the entry from the directory; as it is a file, we do not
748 * have to change the number of hard links of the directory. */
749 tmpfs_dir_detach(dnode, de);
751 /* Free the directory entry we just deleted. Note that the node
752 * referred by it will not be removed until the vnode is really
754 tmpfs_free_dirent(tmp, de);
756 if (node->tn_links > 0) {
757 TMPFS_NODE_LOCK(node);
758 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
760 TMPFS_NODE_UNLOCK(node);
763 cache_setunresolved(v->a_nch);
764 cache_setvp(v->a_nch, NULL);
765 /*cache_inval_vp(vp, CINV_DESTROY);*/
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 cache_setunresolved(v->a_nch);
837 cache_setvp(v->a_nch, vp);
844 /* --------------------------------------------------------------------- */
847 tmpfs_nrename(struct vop_nrename_args *v)
849 struct vnode *fdvp = v->a_fdvp;
850 struct namecache *fncp = v->a_fnch->ncp;
851 struct vnode *fvp = fncp->nc_vp;
852 struct vnode *tdvp = v->a_tdvp;
853 struct namecache *tncp = v->a_tnch->ncp;
854 struct vnode *tvp = tncp->nc_vp;
855 struct tmpfs_dirent *de;
856 struct tmpfs_mount *tmp;
857 struct tmpfs_node *fdnode;
858 struct tmpfs_node *fnode;
859 struct tmpfs_node *tnode;
860 struct tmpfs_node *tdnode;
865 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
867 /* Disallow cross-device renames.
868 * XXX Why isn't this done by the caller? */
869 if (fvp->v_mount != tdvp->v_mount ||
870 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
875 tmp = VFS_TO_TMPFS(tdvp->v_mount);
876 tdnode = VP_TO_TMPFS_DIR(tdvp);
878 /* If source and target are the same file, there is nothing to do. */
884 fdnode = VP_TO_TMPFS_DIR(fdvp);
885 fnode = VP_TO_TMPFS_NODE(fvp);
886 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
888 /* Avoid manipulating '.' and '..' entries. */
893 KKASSERT(de->td_node == fnode);
896 * If replacing an entry in the target directory and that entry
897 * is a directory, it must be empty.
899 * Kern_rename gurantees the destination to be a directory
900 * if the source is one (it does?).
903 KKASSERT(tnode != NULL);
905 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
906 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
911 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
912 if (tnode->tn_size > 0) {
916 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
919 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
923 KKASSERT(fnode->tn_type != VDIR &&
924 tnode->tn_type != VDIR);
928 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
929 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
935 * Ensure that we have enough memory to hold the new name, if it
938 if (fncp->nc_nlen != tncp->nc_nlen ||
939 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
940 newname = kmalloc(tncp->nc_nlen + 1, M_TMPFSNAME, M_WAITOK);
941 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
942 newname[tncp->nc_nlen] = '\0';
948 * Unlink entry from source directory. Note that the kernel has
949 * already checked for illegal recursion cases (renaming a directory
950 * into a subdirectory of itself).
952 if (fdnode != tdnode)
953 tmpfs_dir_detach(fdnode, de);
956 * Handle any name change. Swap with newname, we will
957 * deallocate it at the end.
959 if (newname != NULL) {
961 TMPFS_NODE_LOCK(fnode);
962 fnode->tn_status |= TMPFS_NODE_CHANGED;
963 TMPFS_NODE_UNLOCK(fnode);
965 oldname = de->td_name;
966 de->td_name = newname;
967 de->td_namelen = (uint16_t)tncp->nc_nlen;
972 * Link entry to target directory. If the entry
973 * represents a directory move the parent linkage
976 if (fdnode != tdnode) {
977 if (de->td_node->tn_type == VDIR) {
978 TMPFS_VALIDATE_DIR(fnode);
980 TMPFS_NODE_LOCK(tdnode);
982 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
983 TMPFS_NODE_UNLOCK(tdnode);
985 TMPFS_NODE_LOCK(fnode);
986 fnode->tn_dir.tn_parent = tdnode;
987 fnode->tn_status |= TMPFS_NODE_CHANGED;
988 TMPFS_NODE_UNLOCK(fnode);
990 TMPFS_NODE_LOCK(fdnode);
992 fdnode->tn_status |= TMPFS_NODE_MODIFIED;
993 TMPFS_NODE_UNLOCK(fdnode);
995 tmpfs_dir_attach(tdnode, de);
997 TMPFS_NODE_LOCK(tdnode);
998 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
999 TMPFS_NODE_UNLOCK(tdnode);
1003 * If we are overwriting an entry, we have to remove the old one
1004 * from the target directory.
1007 /* Remove the old entry from the target directory. */
1008 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1009 tmpfs_dir_detach(tdnode, de);
1012 * Free the directory entry we just deleted. Note that the
1013 * node referred by it will not be removed until the vnode is
1016 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1017 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1024 kfree(newname, M_TMPFSNAME);
1027 cache_rename(v->a_fnch, v->a_tnch);
1034 /* Release target nodes. */
1035 /* XXX: I don't understand when tdvp can be the same as tvp, but
1036 * other code takes care of this... */
1043 /* --------------------------------------------------------------------- */
1046 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1048 struct vnode *dvp = v->a_dvp;
1049 struct vnode **vpp = v->a_vpp;
1050 struct namecache *ncp = v->a_nch->ncp;
1051 struct vattr *vap = v->a_vap;
1052 struct ucred *cred = v->a_cred;
1055 KKASSERT(vap->va_type == VDIR);
1057 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1059 cache_setunresolved(v->a_nch);
1060 cache_setvp(v->a_nch, *vpp);
1066 /* --------------------------------------------------------------------- */
1069 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1071 struct vnode *dvp = v->a_dvp;
1072 struct namecache *ncp = v->a_nch->ncp;
1074 struct tmpfs_dirent *de;
1075 struct tmpfs_mount *tmp;
1076 struct tmpfs_node *dnode;
1077 struct tmpfs_node *node;
1081 * We have to acquire the vp from v->a_nch because
1082 * we will likely unresolve the namecache entry, and
1083 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1086 error = cache_vref(v->a_nch, v->a_cred, &vp);
1087 KKASSERT(error == 0);
1090 * Prevalidate so we don't hit an assertion later
1092 if (vp->v_type != VDIR) {
1097 tmp = VFS_TO_TMPFS(dvp->v_mount);
1098 dnode = VP_TO_TMPFS_DIR(dvp);
1099 node = VP_TO_TMPFS_DIR(vp);
1101 /* Directories with more than two entries ('.' and '..') cannot be
1103 if (node->tn_size > 0) {
1108 if ((dnode->tn_flags & APPEND)
1109 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1114 /* This invariant holds only if we are not trying to remove "..".
1115 * We checked for that above so this is safe now. */
1116 KKASSERT(node->tn_dir.tn_parent == dnode);
1118 /* Get the directory entry associated with node (vp). This was
1119 * filled by tmpfs_lookup while looking up the entry. */
1120 de = tmpfs_dir_lookup(dnode, node, ncp);
1121 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1125 /* Check flags to see if we are allowed to remove the directory. */
1126 if ((dnode->tn_flags & APPEND) ||
1127 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1133 /* Detach the directory entry from the directory (dnode). */
1134 tmpfs_dir_detach(dnode, de);
1136 /* No vnode should be allocated for this entry from this point */
1137 TMPFS_NODE_LOCK(node);
1138 TMPFS_ASSERT_ELOCKED(node);
1139 TMPFS_NODE_LOCK(dnode);
1140 TMPFS_ASSERT_ELOCKED(dnode);
1143 /* handled by tmpfs_free_node */
1144 KKASSERT(node->tn_links > 0);
1146 node->tn_dir.tn_parent = NULL;
1148 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1149 TMPFS_NODE_MODIFIED;
1152 /* handled by tmpfs_free_node */
1153 KKASSERT(dnode->tn_links > 0);
1156 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1157 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1159 TMPFS_NODE_UNLOCK(dnode);
1160 TMPFS_NODE_UNLOCK(node);
1162 /* Free the directory entry we just deleted. Note that the node
1163 * referred by it will not be removed until the vnode is really
1165 tmpfs_free_dirent(tmp, de);
1167 /* Release the deleted vnode (will destroy the node, notify
1168 * interested parties and clean it from the cache). */
1170 TMPFS_NODE_LOCK(dnode);
1171 dnode->tn_status |= TMPFS_NODE_CHANGED;
1172 TMPFS_NODE_UNLOCK(dnode);
1175 cache_setunresolved(v->a_nch);
1176 cache_setvp(v->a_nch, NULL);
1177 /*cache_inval_vp(vp, CINV_DESTROY);*/
1186 /* --------------------------------------------------------------------- */
1189 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1191 struct vnode *dvp = v->a_dvp;
1192 struct vnode **vpp = v->a_vpp;
1193 struct namecache *ncp = v->a_nch->ncp;
1194 struct vattr *vap = v->a_vap;
1195 struct ucred *cred = v->a_cred;
1196 char *target = v->a_target;
1199 vap->va_type = VLNK;
1200 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1202 cache_setunresolved(v->a_nch);
1203 cache_setvp(v->a_nch, *vpp);
1209 /* --------------------------------------------------------------------- */
1212 tmpfs_readdir(struct vop_readdir_args *v)
1214 struct vnode *vp = v->a_vp;
1215 struct uio *uio = v->a_uio;
1216 int *eofflag = v->a_eofflag;
1217 off_t **cookies = v->a_cookies;
1218 int *ncookies = v->a_ncookies;
1219 struct tmpfs_mount *tmp;
1223 struct tmpfs_node *node;
1225 /* This operation only makes sense on directory nodes. */
1226 if (vp->v_type != VDIR)
1229 tmp = VFS_TO_TMPFS(vp->v_mount);
1230 node = VP_TO_TMPFS_DIR(vp);
1231 startoff = uio->uio_offset;
1233 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1234 error = tmpfs_dir_getdotdent(node, uio);
1240 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1241 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1247 error = tmpfs_dir_getdents(node, uio, &cnt);
1250 KKASSERT(error >= -1);
1255 if (eofflag != NULL)
1257 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1259 /* Update NFS-related variables. */
1260 if (error == 0 && cookies != NULL && ncookies != NULL) {
1262 off_t off = startoff;
1263 struct tmpfs_dirent *de = NULL;
1266 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1268 for (i = 0; i < cnt; i++) {
1269 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1270 if (off == TMPFS_DIRCOOKIE_DOT) {
1271 off = TMPFS_DIRCOOKIE_DOTDOT;
1273 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1274 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1275 } else if (de != NULL) {
1276 de = TAILQ_NEXT(de, td_entries);
1278 de = tmpfs_dir_lookupbycookie(node,
1280 KKASSERT(de != NULL);
1281 de = TAILQ_NEXT(de, td_entries);
1284 off = TMPFS_DIRCOOKIE_EOF;
1286 off = tmpfs_dircookie(de);
1289 (*cookies)[i] = off;
1291 KKASSERT(uio->uio_offset == off);
1297 /* --------------------------------------------------------------------- */
1300 tmpfs_readlink(struct vop_readlink_args *v)
1302 struct vnode *vp = v->a_vp;
1303 struct uio *uio = v->a_uio;
1306 struct tmpfs_node *node;
1308 KKASSERT(uio->uio_offset == 0);
1309 KKASSERT(vp->v_type == VLNK);
1311 node = VP_TO_TMPFS_NODE(vp);
1313 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1315 TMPFS_NODE_LOCK(node);
1316 node->tn_status |= TMPFS_NODE_ACCESSED;
1317 TMPFS_NODE_UNLOCK(node);
1322 /* --------------------------------------------------------------------- */
1325 tmpfs_inactive(struct vop_inactive_args *v)
1327 struct vnode *vp = v->a_vp;
1329 struct tmpfs_node *node;
1331 node = VP_TO_TMPFS_NODE(vp);
1334 * Get rid of unreferenced deleted vnodes sooner rather than
1335 * later so the data memory can be recovered immediately.
1337 * We must truncate the vnode to prevent the normal reclamation
1338 * path from flushing the data for the removed file to disk.
1340 TMPFS_NODE_LOCK(node);
1341 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1342 (node->tn_links == 0 ||
1343 (node->tn_links == 1 && node->tn_type == VDIR &&
1344 node->tn_dir.tn_parent)))
1346 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1347 TMPFS_NODE_UNLOCK(node);
1348 if (node->tn_type == VREG)
1349 tmpfs_truncate(vp, 0);
1352 TMPFS_NODE_UNLOCK(node);
1358 /* --------------------------------------------------------------------- */
1361 tmpfs_reclaim(struct vop_reclaim_args *v)
1363 struct vnode *vp = v->a_vp;
1364 struct tmpfs_mount *tmp;
1365 struct tmpfs_node *node;
1367 node = VP_TO_TMPFS_NODE(vp);
1368 tmp = VFS_TO_TMPFS(vp->v_mount);
1373 * If the node referenced by this vnode was deleted by the
1374 * user, we must free its associated data structures now that
1375 * the vnode is being reclaimed.
1377 * Directories have an extra link ref.
1379 TMPFS_NODE_LOCK(node);
1380 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1381 (node->tn_links == 0 ||
1382 (node->tn_links == 1 && node->tn_type == VDIR &&
1383 node->tn_dir.tn_parent)))
1385 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1386 tmpfs_free_node(tmp, node);
1389 TMPFS_NODE_UNLOCK(node);
1392 KKASSERT(vp->v_data == NULL);
1396 /* --------------------------------------------------------------------- */
1399 tmpfs_print(struct vop_print_args *v)
1401 struct vnode *vp = v->a_vp;
1403 struct tmpfs_node *node;
1405 node = VP_TO_TMPFS_NODE(vp);
1407 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1408 node, node->tn_flags, node->tn_links);
1409 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1410 node->tn_mode, node->tn_uid, node->tn_gid,
1411 (uintmax_t)node->tn_size, node->tn_status);
1413 if (vp->v_type == VFIFO)
1421 /* --------------------------------------------------------------------- */
1424 tmpfs_pathconf(struct vop_pathconf_args *v)
1426 int name = v->a_name;
1427 register_t *retval = v->a_retval;
1450 case _PC_CHOWN_RESTRICTED:
1462 case _PC_FILESIZEBITS:
1463 *retval = 0; /* XXX Don't know which value should I return. */
1473 /* --------------------------------------------------------------------- */
1476 * vnode operations vector used for files stored in a tmpfs file system.
1478 struct vop_ops tmpfs_vnode_vops = {
1479 .vop_default = vop_defaultop,
1480 .vop_getpages = vop_stdgetpages,
1481 .vop_putpages = vop_stdputpages,
1482 .vop_ncreate = tmpfs_ncreate,
1483 .vop_nresolve = tmpfs_nresolve,
1484 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1485 .vop_nmknod = tmpfs_nmknod,
1486 .vop_open = tmpfs_open,
1487 .vop_close = tmpfs_close,
1488 .vop_access = tmpfs_access,
1489 .vop_getattr = tmpfs_getattr,
1490 .vop_setattr = tmpfs_setattr,
1491 .vop_read = tmpfs_read,
1492 .vop_write = tmpfs_write,
1493 .vop_fsync = tmpfs_fsync,
1494 .vop_nremove = tmpfs_nremove,
1495 .vop_nlink = tmpfs_nlink,
1496 .vop_nrename = tmpfs_nrename,
1497 .vop_nmkdir = tmpfs_nmkdir,
1498 .vop_nrmdir = tmpfs_nrmdir,
1499 .vop_nsymlink = tmpfs_nsymlink,
1500 .vop_readdir = tmpfs_readdir,
1501 .vop_readlink = tmpfs_readlink,
1502 .vop_inactive = tmpfs_inactive,
1503 .vop_reclaim = tmpfs_reclaim,
1504 .vop_print = tmpfs_print,
1505 .vop_pathconf = tmpfs_pathconf,
1506 .vop_bmap = tmpfs_bmap,
1507 .vop_strategy = tmpfs_strategy,
1508 .vop_advlock = tmpfs_advlock,