1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * tmpfs vnode interface.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/extattr.h>
45 #include <sys/fcntl.h>
47 #include <sys/filio.h>
48 #include <sys/limits.h>
49 #include <sys/lockf.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
55 #include <sys/rwlock.h>
56 #include <sys/sched.h>
59 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/vnode.h>
62 #include <security/audit/audit.h>
63 #include <security/mac/mac_framework.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_pager.h>
70 #include <vm/swap_pager.h>
72 #include <fs/tmpfs/tmpfs_vnops.h>
73 #include <fs/tmpfs/tmpfs.h>
75 SYSCTL_DECL(_vfs_tmpfs);
78 static volatile int tmpfs_rename_restarts;
79 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
80 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
81 "Times rename had to restart due to lock contention");
83 MALLOC_DEFINE(M_TMPFSEA, "tmpfs extattr", "tmpfs extattr structure");
86 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
90 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
94 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
96 struct tmpfs_dirent *de;
97 struct tmpfs_node *dnode, *pnode;
98 struct tmpfs_mount *tm;
101 /* Caller assumes responsibility for ensuring access (VEXEC). */
102 dnode = VP_TO_TMPFS_DIR(dvp);
105 /* We cannot be requesting the parent directory of the root node. */
106 MPASS(IMPLIES(dnode->tn_type == VDIR &&
107 dnode->tn_dir.tn_parent == dnode,
108 !(cnp->cn_flags & ISDOTDOT)));
110 TMPFS_ASSERT_LOCKED(dnode);
111 if (dnode->tn_dir.tn_parent == NULL) {
115 if (cnp->cn_flags & ISDOTDOT) {
116 tm = VFS_TO_TMPFS(dvp->v_mount);
117 pnode = dnode->tn_dir.tn_parent;
118 tmpfs_ref_node(pnode);
119 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
120 pnode, cnp->cn_lkflags, vpp);
121 tmpfs_free_node(tm, pnode);
124 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
129 de = tmpfs_dir_lookup(dnode, NULL, cnp);
130 if (de != NULL && de->td_node == NULL)
131 cnp->cn_flags |= ISWHITEOUT;
132 if (de == NULL || de->td_node == NULL) {
134 * The entry was not found in the directory.
135 * This is OK if we are creating or renaming an
136 * entry and are working on the last component of
139 if ((cnp->cn_flags & ISLASTCN) &&
140 (cnp->cn_nameiop == CREATE || \
141 cnp->cn_nameiop == RENAME ||
142 (cnp->cn_nameiop == DELETE &&
143 cnp->cn_flags & DOWHITEOUT &&
144 cnp->cn_flags & ISWHITEOUT))) {
145 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
154 struct tmpfs_node *tnode;
157 * The entry was found, so get its associated
163 * If we are not at the last path component and
164 * found a non-directory or non-link entry (which
165 * may itself be pointing to a directory), raise
168 if ((tnode->tn_type != VDIR &&
169 tnode->tn_type != VLNK) &&
170 !(cnp->cn_flags & ISLASTCN)) {
176 * If we are deleting or renaming the entry, keep
177 * track of its tmpfs_dirent so that it can be
178 * easily deleted later.
180 if ((cnp->cn_flags & ISLASTCN) &&
181 (cnp->cn_nameiop == DELETE ||
182 cnp->cn_nameiop == RENAME)) {
183 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
188 /* Allocate a new vnode on the matching entry. */
189 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
190 cnp->cn_lkflags, vpp);
194 if ((dnode->tn_mode & S_ISTXT) &&
195 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
196 curthread) && VOP_ACCESS(*vpp, VADMIN,
197 cnp->cn_cred, curthread)) {
204 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
205 cnp->cn_lkflags, vpp);
213 * Store the result of this lookup in the cache. Avoid this if the
214 * request was for creation, as it does not improve timings on
217 if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
218 cache_enter(dvp, *vpp, cnp);
223 * If there were no errors, *vpp cannot be null and it must be
227 MPASS(*vpp != NULLVP);
228 ASSERT_VOP_LOCKED(*vpp, __func__);
238 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
241 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
245 tmpfs_lookup(struct vop_lookup_args *v)
247 struct vnode *dvp = v->a_dvp;
248 struct vnode **vpp = v->a_vpp;
249 struct componentname *cnp = v->a_cnp;
252 /* Check accessibility of requested node as a first step. */
253 error = vn_dir_check_exec(dvp, cnp);
257 return (tmpfs_lookup1(dvp, vpp, cnp));
261 tmpfs_create(struct vop_create_args *v)
263 struct vnode *dvp = v->a_dvp;
264 struct vnode **vpp = v->a_vpp;
265 struct componentname *cnp = v->a_cnp;
266 struct vattr *vap = v->a_vap;
269 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
271 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
272 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
273 cache_enter(dvp, *vpp, cnp);
278 tmpfs_mknod(struct vop_mknod_args *v)
280 struct vnode *dvp = v->a_dvp;
281 struct vnode **vpp = v->a_vpp;
282 struct componentname *cnp = v->a_cnp;
283 struct vattr *vap = v->a_vap;
285 if (vap->va_type != VBLK && vap->va_type != VCHR &&
286 vap->va_type != VFIFO)
289 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
292 struct fileops tmpfs_fnops;
295 tmpfs_open(struct vop_open_args *v)
298 struct tmpfs_node *node;
304 node = VP_TO_TMPFS_NODE(vp);
307 * The file is still active but all its names have been removed
308 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
309 * it is about to die.
311 if (node->tn_links < 1)
314 /* If the file is marked append-only, deny write requests. */
315 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
319 /* For regular files, the call below is nop. */
320 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
321 OBJ_DEAD) == 0, ("dead object"));
322 vnode_create_vobject(vp, node->tn_size, v->a_td);
326 MPASS(fp == NULL || fp->f_data == NULL);
327 if (error == 0 && fp != NULL && vp->v_type == VREG) {
328 tmpfs_ref_node(node);
329 finit_vnode(fp, mode, node, &tmpfs_fnops);
336 tmpfs_close(struct vop_close_args *v)
338 struct vnode *vp = v->a_vp;
340 /* Update node times. */
347 tmpfs_fo_close(struct file *fp, struct thread *td)
349 struct tmpfs_node *node;
353 MPASS(node->tn_type == VREG);
354 tmpfs_free_node(node->tn_reg.tn_tmp, node);
356 return (vnops.fo_close(fp, td));
360 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
361 * the comment above cache_fplookup for details.
364 tmpfs_fplookup_vexec(struct vop_fplookup_vexec_args *v)
367 struct tmpfs_node *node;
372 node = VP_TO_TMPFS_NODE_SMR(vp);
373 if (__predict_false(node == NULL))
376 all_x = S_IXUSR | S_IXGRP | S_IXOTH;
377 mode = atomic_load_short(&node->tn_mode);
378 if (__predict_true((mode & all_x) == all_x))
382 return (vaccess_vexec_smr(mode, node->tn_uid, node->tn_gid, cred));
386 tmpfs_access_locked(struct vnode *vp, struct tmpfs_node *node,
387 accmode_t accmode, struct ucred *cred)
389 #ifdef DEBUG_VFS_LOCKS
390 if (!mtx_owned(TMPFS_NODE_MTX(node))) {
391 ASSERT_VOP_LOCKED(vp,
392 "tmpfs_access_locked needs locked vnode or node");
396 if ((accmode & VWRITE) != 0 && (node->tn_flags & IMMUTABLE) != 0)
398 return (vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
403 tmpfs_access(struct vop_access_args *v)
405 struct vnode *vp = v->a_vp;
406 struct ucred *cred = v->a_cred;
407 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
408 mode_t all_x = S_IXUSR | S_IXGRP | S_IXOTH;
409 accmode_t accmode = v->a_accmode;
412 * Common case path lookup.
414 if (__predict_true(accmode == VEXEC &&
415 (node->tn_mode & all_x) == all_x))
418 switch (vp->v_type) {
424 if ((accmode & VWRITE) != 0 &&
425 (vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
442 return (tmpfs_access_locked(vp, node, accmode, cred));
446 tmpfs_stat(struct vop_stat_args *v)
448 struct vnode *vp = v->a_vp;
449 struct stat *sb = v->a_sb;
450 struct tmpfs_node *node;
453 node = VP_TO_TMPFS_NODE(vp);
455 tmpfs_update_getattr(vp);
457 error = vop_stat_helper_pre(v);
458 if (__predict_false(error))
461 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
462 sb->st_ino = node->tn_id;
463 sb->st_mode = node->tn_mode | VTTOIF(vp->v_type);
464 sb->st_nlink = node->tn_links;
465 sb->st_uid = node->tn_uid;
466 sb->st_gid = node->tn_gid;
467 sb->st_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
468 node->tn_rdev : NODEV;
469 sb->st_size = node->tn_size;
470 sb->st_atim.tv_sec = node->tn_atime.tv_sec;
471 sb->st_atim.tv_nsec = node->tn_atime.tv_nsec;
472 sb->st_mtim.tv_sec = node->tn_mtime.tv_sec;
473 sb->st_mtim.tv_nsec = node->tn_mtime.tv_nsec;
474 sb->st_ctim.tv_sec = node->tn_ctime.tv_sec;
475 sb->st_ctim.tv_nsec = node->tn_ctime.tv_nsec;
476 sb->st_birthtim.tv_sec = node->tn_birthtime.tv_sec;
477 sb->st_birthtim.tv_nsec = node->tn_birthtime.tv_nsec;
478 sb->st_blksize = PAGE_SIZE;
479 sb->st_flags = node->tn_flags;
480 sb->st_gen = node->tn_gen;
481 if (vp->v_type == VREG) {
483 vm_object_t obj = node->tn_reg.tn_aobj;
485 /* Handle torn read */
486 VM_OBJECT_RLOCK(obj);
488 sb->st_blocks = ptoa(node->tn_reg.tn_pages);
490 VM_OBJECT_RUNLOCK(obj);
493 sb->st_blocks = node->tn_size;
495 sb->st_blocks /= S_BLKSIZE;
496 return (vop_stat_helper_post(v, error));
500 tmpfs_getattr(struct vop_getattr_args *v)
502 struct vnode *vp = v->a_vp;
503 struct vattr *vap = v->a_vap;
504 struct tmpfs_node *node;
506 node = VP_TO_TMPFS_NODE(vp);
508 tmpfs_update_getattr(vp);
510 vap->va_type = vp->v_type;
511 vap->va_mode = node->tn_mode;
512 vap->va_nlink = node->tn_links;
513 vap->va_uid = node->tn_uid;
514 vap->va_gid = node->tn_gid;
515 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
516 vap->va_fileid = node->tn_id;
517 vap->va_size = node->tn_size;
518 vap->va_blocksize = PAGE_SIZE;
519 vap->va_atime = node->tn_atime;
520 vap->va_mtime = node->tn_mtime;
521 vap->va_ctime = node->tn_ctime;
522 vap->va_birthtime = node->tn_birthtime;
523 vap->va_gen = node->tn_gen;
524 vap->va_flags = node->tn_flags;
525 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
526 node->tn_rdev : NODEV;
527 if (vp->v_type == VREG) {
529 vm_object_t obj = node->tn_reg.tn_aobj;
531 VM_OBJECT_RLOCK(obj);
533 vap->va_bytes = ptoa(node->tn_reg.tn_pages);
535 VM_OBJECT_RUNLOCK(obj);
538 vap->va_bytes = node->tn_size;
546 tmpfs_setattr(struct vop_setattr_args *v)
548 struct vnode *vp = v->a_vp;
549 struct vattr *vap = v->a_vap;
550 struct ucred *cred = v->a_cred;
551 struct thread *td = curthread;
555 ASSERT_VOP_IN_SEQC(vp);
559 /* Abort if any unsettable attribute is given. */
560 if (vap->va_type != VNON ||
561 vap->va_nlink != VNOVAL ||
562 vap->va_fsid != VNOVAL ||
563 vap->va_fileid != VNOVAL ||
564 vap->va_blocksize != VNOVAL ||
565 vap->va_gen != VNOVAL ||
566 vap->va_rdev != VNOVAL ||
567 vap->va_bytes != VNOVAL)
570 if (error == 0 && (vap->va_flags != VNOVAL))
571 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
573 if (error == 0 && (vap->va_size != VNOVAL))
574 error = tmpfs_chsize(vp, vap->va_size, cred, td);
576 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
577 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
579 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
580 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
582 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
583 vap->va_atime.tv_nsec != VNOVAL) ||
584 (vap->va_mtime.tv_sec != VNOVAL &&
585 vap->va_mtime.tv_nsec != VNOVAL) ||
586 (vap->va_birthtime.tv_sec != VNOVAL &&
587 vap->va_birthtime.tv_nsec != VNOVAL)))
588 error = tmpfs_chtimes(vp, vap, cred, td);
591 * Update the node times. We give preference to the error codes
592 * generated by this function rather than the ones that may arise
601 tmpfs_read(struct vop_read_args *v)
605 struct tmpfs_node *node;
608 if (vp->v_type != VREG)
611 if (uio->uio_offset < 0)
613 node = VP_TO_TMPFS_NODE(vp);
614 tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
615 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
619 tmpfs_read_pgcache(struct vop_read_pgcache_args *v)
622 struct tmpfs_node *node;
628 VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
630 if (v->a_uio->uio_offset < 0)
636 node = VP_TO_TMPFS_NODE_SMR(vp);
639 MPASS(node->tn_type == VREG);
640 MPASS(node->tn_refcount >= 1);
641 object = node->tn_reg.tn_aobj;
645 MPASS(object->type == tmpfs_pager_type);
646 MPASS((object->flags & (OBJ_ANON | OBJ_DEAD | OBJ_SWAP)) ==
648 if (!VN_IS_DOOMED(vp)) {
649 /* size cannot become shorter due to rangelock. */
650 size = node->tn_size;
651 tmpfs_set_accessed(node->tn_reg.tn_tmp, node);
653 error = uiomove_object(object, size, v->a_uio);
662 tmpfs_write(struct vop_write_args *v)
666 struct tmpfs_node *node;
674 ioflag = v->a_ioflag;
676 node = VP_TO_TMPFS_NODE(vp);
677 oldsize = node->tn_size;
679 if (uio->uio_offset < 0 || vp->v_type != VREG)
681 if (uio->uio_resid == 0)
683 if (ioflag & IO_APPEND)
684 uio->uio_offset = node->tn_size;
685 error = vn_rlimit_fsizex(vp, uio, VFS_TO_TMPFS(vp->v_mount)->
686 tm_maxfilesize, &r, uio->uio_td);
688 vn_rlimit_fsizex_res(uio, r);
692 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
693 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
699 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
700 node->tn_status |= TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED;
701 node->tn_accessed = true;
702 if (node->tn_mode & (S_ISUID | S_ISGID)) {
703 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID)) {
704 newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
705 vn_seqc_write_begin(vp);
706 atomic_store_short(&node->tn_mode, newmode);
707 vn_seqc_write_end(vp);
711 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
714 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
715 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
717 vn_rlimit_fsizex_res(uio, r);
722 tmpfs_deallocate(struct vop_deallocate_args *v)
724 return (tmpfs_reg_punch_hole(v->a_vp, v->a_offset, v->a_len));
728 tmpfs_fsync(struct vop_fsync_args *v)
730 struct vnode *vp = v->a_vp;
732 tmpfs_check_mtime(vp);
739 tmpfs_remove(struct vop_remove_args *v)
741 struct vnode *dvp = v->a_dvp;
742 struct vnode *vp = v->a_vp;
745 struct tmpfs_dirent *de;
746 struct tmpfs_mount *tmp;
747 struct tmpfs_node *dnode;
748 struct tmpfs_node *node;
750 if (vp->v_type == VDIR) {
755 dnode = VP_TO_TMPFS_DIR(dvp);
756 node = VP_TO_TMPFS_NODE(vp);
757 tmp = VFS_TO_TMPFS(vp->v_mount);
758 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
761 /* Files marked as immutable or append-only cannot be deleted. */
762 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
763 (dnode->tn_flags & APPEND)) {
768 /* Remove the entry from the directory; as it is a file, we do not
769 * have to change the number of hard links of the directory. */
770 tmpfs_dir_detach(dvp, de);
771 if (v->a_cnp->cn_flags & DOWHITEOUT)
772 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
774 /* Free the directory entry we just deleted. Note that the node
775 * referred by it will not be removed until the vnode is really
777 tmpfs_free_dirent(tmp, de);
779 node->tn_status |= TMPFS_NODE_CHANGED;
780 node->tn_accessed = true;
788 tmpfs_link(struct vop_link_args *v)
790 struct vnode *dvp = v->a_tdvp;
791 struct vnode *vp = v->a_vp;
792 struct componentname *cnp = v->a_cnp;
795 struct tmpfs_dirent *de;
796 struct tmpfs_node *node;
798 MPASS(dvp != vp); /* XXX When can this be false? */
799 node = VP_TO_TMPFS_NODE(vp);
801 /* Ensure that we do not overflow the maximum number of links imposed
803 MPASS(node->tn_links <= TMPFS_LINK_MAX);
804 if (node->tn_links == TMPFS_LINK_MAX) {
809 /* We cannot create links of files marked immutable or append-only. */
810 if (node->tn_flags & (IMMUTABLE | APPEND)) {
815 /* Allocate a new directory entry to represent the node. */
816 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
817 cnp->cn_nameptr, cnp->cn_namelen, &de);
821 /* Insert the new directory entry into the appropriate directory. */
822 if (cnp->cn_flags & ISWHITEOUT)
823 tmpfs_dir_whiteout_remove(dvp, cnp);
824 tmpfs_dir_attach(dvp, de);
826 /* vp link count has changed, so update node times. */
827 node->tn_status |= TMPFS_NODE_CHANGED;
837 * We acquire all but fdvp locks using non-blocking acquisitions. If we
838 * fail to acquire any lock in the path we will drop all held locks,
839 * acquire the new lock in a blocking fashion, and then release it and
840 * restart the rename. This acquire/release step ensures that we do not
841 * spin on a lock waiting for release. On error release all vnode locks
842 * and decrement references the way tmpfs_rename() would do.
845 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
846 struct vnode *tdvp, struct vnode **tvpp,
847 struct componentname *fcnp, struct componentname *tcnp)
851 struct tmpfs_dirent *de;
852 int error, restarts = 0;
855 if (*tvpp != NULL && *tvpp != tdvp)
861 error = vn_lock(fdvp, LK_EXCLUSIVE);
864 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
866 error = vn_lock(tdvp, LK_EXCLUSIVE);
873 * Re-resolve fvp to be certain it still exists and fetch the
876 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
880 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
881 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
887 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
893 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
898 * Concurrent rename race.
913 * Re-resolve tvp and acquire the vnode lock if present.
915 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
917 * If tvp disappeared we just carry on.
919 if (de == NULL && *tvpp != NULL) {
924 * Get the tvp ino if the lookup succeeded. We may have to restart
925 * if the non-blocking acquire fails.
929 error = tmpfs_alloc_vp(mp, de->td_node,
930 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
939 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
945 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
954 tmpfs_rename_restarts += restarts;
964 tmpfs_rename_restarts += restarts;
970 tmpfs_rename(struct vop_rename_args *v)
972 struct vnode *fdvp = v->a_fdvp;
973 struct vnode *fvp = v->a_fvp;
974 struct componentname *fcnp = v->a_fcnp;
975 struct vnode *tdvp = v->a_tdvp;
976 struct vnode *tvp = v->a_tvp;
977 struct componentname *tcnp = v->a_tcnp;
979 struct tmpfs_dirent *de;
980 struct tmpfs_mount *tmp;
981 struct tmpfs_node *fdnode;
982 struct tmpfs_node *fnode;
983 struct tmpfs_node *tnode;
984 struct tmpfs_node *tdnode;
988 want_seqc_end = false;
991 * Disallow cross-device renames.
992 * XXX Why isn't this done by the caller?
994 if (fvp->v_mount != tdvp->v_mount ||
995 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1000 /* If source and target are the same file, there is nothing to do. */
1007 * If we need to move the directory between entries, lock the
1008 * source so that we can safely operate on it.
1010 if (fdvp != tdvp && fdvp != tvp) {
1011 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1012 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1016 ASSERT_VOP_ELOCKED(fdvp,
1017 "tmpfs_rename: fdvp not locked");
1018 ASSERT_VOP_ELOCKED(tdvp,
1019 "tmpfs_rename: tdvp not locked");
1021 ASSERT_VOP_ELOCKED(tvp,
1022 "tmpfs_rename: tvp not locked");
1031 * Avoid manipulating '.' and '..' entries.
1033 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1034 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) {
1040 vn_seqc_write_begin(tvp);
1041 vn_seqc_write_begin(tdvp);
1042 vn_seqc_write_begin(fvp);
1043 vn_seqc_write_begin(fdvp);
1044 want_seqc_end = true;
1046 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1047 tdnode = VP_TO_TMPFS_DIR(tdvp);
1048 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1049 fdnode = VP_TO_TMPFS_DIR(fdvp);
1050 fnode = VP_TO_TMPFS_NODE(fvp);
1051 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1054 * Entry can disappear before we lock fdvp.
1057 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1058 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1064 MPASS(de->td_node == fnode);
1067 * If re-naming a directory to another preexisting directory
1068 * ensure that the target directory is empty so that its
1069 * removal causes no side effects.
1070 * Kern_rename guarantees the destination to be a directory
1071 * if the source is one.
1074 MPASS(tnode != NULL);
1076 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1077 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1082 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1083 if (tnode->tn_size > 0) {
1087 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1090 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1094 MPASS(fnode->tn_type != VDIR &&
1095 tnode->tn_type != VDIR);
1099 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1100 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1106 * Ensure that we have enough memory to hold the new name, if it
1107 * has to be changed.
1109 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1110 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1111 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1116 * If the node is being moved to another directory, we have to do
1119 if (fdnode != tdnode) {
1121 * In case we are moving a directory, we have to adjust its
1122 * parent to point to the new parent.
1124 if (de->td_node->tn_type == VDIR) {
1125 struct tmpfs_node *n;
1127 TMPFS_NODE_LOCK(fnode);
1128 error = tmpfs_access_locked(fvp, fnode, VWRITE,
1130 TMPFS_NODE_UNLOCK(fnode);
1132 if (newname != NULL)
1133 free(newname, M_TMPFSNAME);
1138 * Ensure the target directory is not a child of the
1139 * directory being moved. Otherwise, we'd end up
1144 * TMPFS_LOCK guaranties that no nodes are freed while
1145 * traversing the list. Nodes can only be marked as
1146 * removed: tn_parent == NULL.
1150 while (n != n->tn_dir.tn_parent) {
1151 struct tmpfs_node *parent;
1154 TMPFS_NODE_UNLOCK(n);
1157 if (newname != NULL)
1158 free(newname, M_TMPFSNAME);
1161 parent = n->tn_dir.tn_parent;
1162 TMPFS_NODE_UNLOCK(n);
1163 if (parent == NULL) {
1167 TMPFS_NODE_LOCK(parent);
1168 if (parent->tn_dir.tn_parent == NULL) {
1169 TMPFS_NODE_UNLOCK(parent);
1178 if (newname != NULL)
1179 free(newname, M_TMPFSNAME);
1182 TMPFS_NODE_UNLOCK(n);
1184 /* Adjust the parent pointer. */
1185 TMPFS_VALIDATE_DIR(fnode);
1186 TMPFS_NODE_LOCK(de->td_node);
1187 de->td_node->tn_dir.tn_parent = tdnode;
1188 TMPFS_NODE_UNLOCK(de->td_node);
1191 * As a result of changing the target of the '..'
1192 * entry, the link count of the source and target
1193 * directories has to be adjusted.
1195 TMPFS_NODE_LOCK(tdnode);
1196 TMPFS_ASSERT_LOCKED(tdnode);
1198 TMPFS_NODE_UNLOCK(tdnode);
1200 TMPFS_NODE_LOCK(fdnode);
1201 TMPFS_ASSERT_LOCKED(fdnode);
1203 TMPFS_NODE_UNLOCK(fdnode);
1208 * Do the move: just remove the entry from the source directory
1209 * and insert it into the target one.
1211 tmpfs_dir_detach(fdvp, de);
1213 if (fcnp->cn_flags & DOWHITEOUT)
1214 tmpfs_dir_whiteout_add(fdvp, fcnp);
1215 if (tcnp->cn_flags & ISWHITEOUT)
1216 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1219 * If the name has changed, we need to make it effective by changing
1220 * it in the directory entry.
1222 if (newname != NULL) {
1223 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1225 free(de->ud.td_name, M_TMPFSNAME);
1226 de->ud.td_name = newname;
1227 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1229 fnode->tn_status |= TMPFS_NODE_CHANGED;
1230 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1234 * If we are overwriting an entry, we have to remove the old one
1235 * from the target directory.
1238 struct tmpfs_dirent *tde;
1240 /* Remove the old entry from the target directory. */
1241 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1242 tmpfs_dir_detach(tdvp, tde);
1244 /* Update node's ctime because of possible hardlinks. */
1245 tnode->tn_status |= TMPFS_NODE_CHANGED;
1249 * Free the directory entry we just deleted. Note that the
1250 * node referred by it will not be removed until the vnode is
1253 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1256 tmpfs_dir_attach(tdvp, de);
1258 if (tmpfs_use_nc(fvp)) {
1259 cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1265 if (fdvp != tdvp && fdvp != tvp)
1269 if (want_seqc_end) {
1271 vn_seqc_write_end(tvp);
1272 vn_seqc_write_end(tdvp);
1273 vn_seqc_write_end(fvp);
1274 vn_seqc_write_end(fdvp);
1278 * Release target nodes.
1279 * XXX: I don't understand when tdvp can be the same as tvp, but
1280 * other code takes care of this...
1289 /* Release source nodes. */
1297 tmpfs_mkdir(struct vop_mkdir_args *v)
1299 struct vnode *dvp = v->a_dvp;
1300 struct vnode **vpp = v->a_vpp;
1301 struct componentname *cnp = v->a_cnp;
1302 struct vattr *vap = v->a_vap;
1304 MPASS(vap->va_type == VDIR);
1306 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
1310 tmpfs_rmdir(struct vop_rmdir_args *v)
1312 struct vnode *dvp = v->a_dvp;
1313 struct vnode *vp = v->a_vp;
1316 struct tmpfs_dirent *de;
1317 struct tmpfs_mount *tmp;
1318 struct tmpfs_node *dnode;
1319 struct tmpfs_node *node;
1321 tmp = VFS_TO_TMPFS(dvp->v_mount);
1322 dnode = VP_TO_TMPFS_DIR(dvp);
1323 node = VP_TO_TMPFS_DIR(vp);
1325 /* Directories with more than two entries ('.' and '..') cannot be
1327 if (node->tn_size > 0) {
1332 if ((dnode->tn_flags & APPEND)
1333 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1338 /* This invariant holds only if we are not trying to remove "..".
1339 * We checked for that above so this is safe now. */
1340 MPASS(node->tn_dir.tn_parent == dnode);
1342 /* Get the directory entry associated with node (vp). This was
1343 * filled by tmpfs_lookup while looking up the entry. */
1344 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1345 MPASS(TMPFS_DIRENT_MATCHES(de,
1346 v->a_cnp->cn_nameptr,
1347 v->a_cnp->cn_namelen));
1349 /* Check flags to see if we are allowed to remove the directory. */
1350 if ((dnode->tn_flags & APPEND) != 0 ||
1351 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1356 /* Detach the directory entry from the directory (dnode). */
1357 tmpfs_dir_detach(dvp, de);
1358 if (v->a_cnp->cn_flags & DOWHITEOUT)
1359 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1361 /* No vnode should be allocated for this entry from this point */
1362 TMPFS_NODE_LOCK(node);
1364 node->tn_dir.tn_parent = NULL;
1365 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1366 node->tn_accessed = true;
1368 TMPFS_NODE_UNLOCK(node);
1370 TMPFS_NODE_LOCK(dnode);
1372 dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1373 dnode->tn_accessed = true;
1374 TMPFS_NODE_UNLOCK(dnode);
1376 if (tmpfs_use_nc(dvp)) {
1377 cache_vop_rmdir(dvp, vp);
1380 /* Free the directory entry we just deleted. Note that the node
1381 * referred by it will not be removed until the vnode is really
1383 tmpfs_free_dirent(tmp, de);
1385 /* Release the deleted vnode (will destroy the node, notify
1386 * interested parties and clean it from the cache). */
1388 dnode->tn_status |= TMPFS_NODE_CHANGED;
1398 tmpfs_symlink(struct vop_symlink_args *v)
1400 struct vnode *dvp = v->a_dvp;
1401 struct vnode **vpp = v->a_vpp;
1402 struct componentname *cnp = v->a_cnp;
1403 struct vattr *vap = v->a_vap;
1404 const char *target = v->a_target;
1406 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1407 MPASS(vap->va_type == VLNK);
1409 vap->va_type = VLNK;
1412 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, target));
1416 tmpfs_readdir(struct vop_readdir_args *va)
1420 struct tmpfs_mount *tm;
1421 struct tmpfs_node *node;
1423 int *eofflag, *ncookies;
1425 int error, maxcookies;
1429 eofflag = va->a_eofflag;
1430 cookies = va->a_cookies;
1431 ncookies = va->a_ncookies;
1433 /* This operation only makes sense on directory nodes. */
1434 if (vp->v_type != VDIR)
1438 node = VP_TO_TMPFS_DIR(vp);
1439 tm = VFS_TO_TMPFS(vp->v_mount);
1441 startresid = uio->uio_resid;
1443 /* Allocate cookies for NFS and compat modules. */
1444 if (cookies != NULL && ncookies != NULL) {
1445 maxcookies = howmany(node->tn_size,
1446 sizeof(struct tmpfs_dirent)) + 2;
1447 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1452 if (cookies == NULL)
1453 error = tmpfs_dir_getdents(tm, node, uio, 0, NULL, NULL);
1455 error = tmpfs_dir_getdents(tm, node, uio, maxcookies, *cookies,
1458 /* Buffer was filled without hitting EOF. */
1459 if (error == EJUSTRETURN)
1460 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1462 if (error != 0 && cookies != NULL && ncookies != NULL) {
1463 free(*cookies, M_TEMP);
1468 if (eofflag != NULL)
1470 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1476 tmpfs_readlink(struct vop_readlink_args *v)
1478 struct vnode *vp = v->a_vp;
1479 struct uio *uio = v->a_uio;
1482 struct tmpfs_node *node;
1484 MPASS(uio->uio_offset == 0);
1485 MPASS(vp->v_type == VLNK);
1487 node = VP_TO_TMPFS_NODE(vp);
1489 error = uiomove(node->tn_link_target, MIN(node->tn_size, uio->uio_resid),
1491 tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
1497 * VOP_FPLOOKUP_SYMLINK routines are subject to special circumstances, see
1498 * the comment above cache_fplookup for details.
1500 * Check tmpfs_alloc_node for tmpfs-specific synchronisation notes.
1503 tmpfs_fplookup_symlink(struct vop_fplookup_symlink_args *v)
1506 struct tmpfs_node *node;
1510 node = VP_TO_TMPFS_NODE_SMR(vp);
1511 if (__predict_false(node == NULL))
1513 if (!atomic_load_char(&node->tn_link_smr))
1515 symlink = atomic_load_ptr(&node->tn_link_target);
1516 if (symlink == NULL)
1519 return (cache_symlink_resolve(v->a_fpl, symlink, node->tn_size));
1523 tmpfs_inactive(struct vop_inactive_args *v)
1526 struct tmpfs_node *node;
1529 node = VP_TO_TMPFS_NODE(vp);
1530 if (node->tn_links == 0)
1533 tmpfs_check_mtime(vp);
1538 tmpfs_need_inactive(struct vop_need_inactive_args *ap)
1541 struct tmpfs_node *node;
1542 struct vm_object *obj;
1545 node = VP_TO_TMPFS_NODE(vp);
1546 if (node->tn_links == 0)
1548 if (vp->v_type == VREG) {
1550 if (obj->generation != obj->cleangeneration)
1559 tmpfs_reclaim(struct vop_reclaim_args *v)
1562 struct tmpfs_mount *tmp;
1563 struct tmpfs_node *node;
1567 node = VP_TO_TMPFS_NODE(vp);
1568 tmp = VFS_TO_TMPFS(vp->v_mount);
1570 if (vp->v_type == VREG)
1571 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1572 vp->v_object = NULL;
1575 TMPFS_NODE_LOCK(node);
1579 * If the node referenced by this vnode was deleted by the user,
1580 * we must free its associated data structures (now that the vnode
1581 * is being reclaimed).
1584 if (node->tn_links == 0 &&
1585 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1586 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1587 unlock = !tmpfs_free_node_locked(tmp, node, true);
1591 TMPFS_NODE_UNLOCK(node);
1595 MPASS(vp->v_data == NULL);
1600 tmpfs_print(struct vop_print_args *v)
1602 struct vnode *vp = v->a_vp;
1604 struct tmpfs_node *node;
1606 node = VP_TO_TMPFS_NODE(vp);
1608 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
1609 node, node->tn_flags, (uintmax_t)node->tn_links);
1610 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1611 node->tn_mode, node->tn_uid, node->tn_gid,
1612 (intmax_t)node->tn_size, node->tn_status);
1614 if (vp->v_type == VFIFO)
1623 tmpfs_pathconf(struct vop_pathconf_args *v)
1625 struct vnode *vp = v->a_vp;
1626 int name = v->a_name;
1627 long *retval = v->a_retval;
1635 *retval = TMPFS_LINK_MAX;
1638 case _PC_SYMLINK_MAX:
1639 *retval = MAXPATHLEN;
1647 if (vp->v_type == VDIR || vp->v_type == VFIFO)
1653 case _PC_CHOWN_RESTRICTED:
1665 case _PC_FILESIZEBITS:
1669 case _PC_MIN_HOLE_SIZE:
1670 *retval = PAGE_SIZE;
1674 error = vop_stdpathconf(v);
1681 tmpfs_vptofh(struct vop_vptofh_args *ap)
1684 IN struct vnode *a_vp;
1685 IN struct fid *a_fhp;
1689 struct tmpfs_fid_data tfd;
1690 struct tmpfs_node *node;
1693 node = VP_TO_TMPFS_NODE(ap->a_vp);
1695 fhp->fid_len = sizeof(tfd);
1698 * Copy into fid_data from the stack to avoid unaligned pointer use.
1699 * See the comment in sys/mount.h on struct fid for details.
1701 tfd.tfd_id = node->tn_id;
1702 tfd.tfd_gen = node->tn_gen;
1703 memcpy(fhp->fid_data, &tfd, fhp->fid_len);
1709 tmpfs_whiteout(struct vop_whiteout_args *ap)
1711 struct vnode *dvp = ap->a_dvp;
1712 struct componentname *cnp = ap->a_cnp;
1713 struct tmpfs_dirent *de;
1715 switch (ap->a_flags) {
1719 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1721 return (de->td_node == NULL ? 0 : EEXIST);
1722 return (tmpfs_dir_whiteout_add(dvp, cnp));
1724 tmpfs_dir_whiteout_remove(dvp, cnp);
1727 panic("tmpfs_whiteout: unknown op");
1732 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1733 struct tmpfs_dirent **pde)
1735 struct tmpfs_dir_cursor dc;
1736 struct tmpfs_dirent *de;
1738 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1739 de = tmpfs_dir_next(tnp, &dc)) {
1740 if (de->td_node == tn) {
1749 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1750 struct tmpfs_node *tnp, char *buf, size_t *buflen, struct vnode **dvp)
1752 struct tmpfs_dirent *de;
1755 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1759 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1762 i -= de->td_namelen;
1766 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1783 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1785 struct vnode *vp, **dvp;
1786 struct tmpfs_node *tn, *tnp, *tnp1;
1787 struct tmpfs_dirent *de;
1788 struct tmpfs_mount *tm;
1796 buflen = ap->a_buflen;
1798 tm = VFS_TO_TMPFS(vp->v_mount);
1799 tn = VP_TO_TMPFS_NODE(vp);
1800 if (tn->tn_type == VDIR) {
1801 tnp = tn->tn_dir.tn_parent;
1804 tmpfs_ref_node(tnp);
1805 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1807 tmpfs_free_node(tm, tnp);
1813 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1814 if (tnp->tn_type != VDIR)
1816 TMPFS_NODE_LOCK(tnp);
1817 tmpfs_ref_node(tnp);
1820 * tn_vnode cannot be instantiated while we hold the
1821 * node lock, so the directory cannot be changed while
1822 * we iterate over it. Do this to avoid instantiating
1823 * vnode for directories which cannot point to our
1826 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1830 TMPFS_NODE_UNLOCK(tnp);
1832 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1835 tmpfs_free_node(tm, tnp);
1838 if (VN_IS_DOOMED(vp)) {
1839 tmpfs_free_node(tm, tnp);
1843 TMPFS_NODE_LOCK(tnp);
1845 if (tmpfs_free_node_locked(tm, tnp, false)) {
1848 KASSERT(tnp->tn_refcount > 0,
1849 ("node %p refcount zero", tnp));
1850 if (tnp->tn_attached) {
1851 tnp1 = LIST_NEXT(tnp, tn_entries);
1852 TMPFS_NODE_UNLOCK(tnp);
1854 TMPFS_NODE_UNLOCK(tnp);
1855 goto restart_locked;
1864 tmpfs_extattr_free(struct tmpfs_extattr *ea)
1866 free(ea->ea_name, M_TMPFSEA);
1867 free(ea->ea_value, M_TMPFSEA);
1868 free(ea, M_TMPFSEA);
1872 tmpfs_extattr_update_mem(struct tmpfs_mount *tmp, ssize_t size)
1876 !tmpfs_pages_check_avail(tmp, howmany(size, PAGE_SIZE))) {
1880 if (tmp->tm_ea_memory_inuse + size > tmp->tm_ea_memory_max) {
1884 tmp->tm_ea_memory_inuse += size;
1890 tmpfs_deleteextattr(struct vop_deleteextattr_args *ap)
1892 struct vnode *vp = ap->a_vp;
1893 struct tmpfs_mount *tmp;
1894 struct tmpfs_node *node;
1895 struct tmpfs_extattr *ea;
1900 node = VP_TO_TMPFS_NODE(vp);
1901 tmp = VFS_TO_TMPFS(vp->v_mount);
1902 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1903 return (EOPNOTSUPP);
1904 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1905 ap->a_cred, ap->a_td, VWRITE);
1908 if (ap->a_name == NULL || ap->a_name[0] == '\0')
1910 namelen = strlen(ap->a_name);
1911 if (namelen > EXTATTR_MAXNAMELEN)
1914 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
1915 if (ea->ea_namespace == ap->a_attrnamespace &&
1916 namelen == ea->ea_namelen &&
1917 memcmp(ap->a_name, ea->ea_name, namelen) == 0)
1923 LIST_REMOVE(ea, ea_extattrs);
1924 diff = -(sizeof(struct tmpfs_extattr) + namelen + ea->ea_size);
1925 tmpfs_extattr_update_mem(tmp, diff);
1926 tmpfs_extattr_free(ea);
1931 tmpfs_getextattr(struct vop_getextattr_args *ap)
1933 struct vnode *vp = ap->a_vp;
1934 struct tmpfs_node *node;
1935 struct tmpfs_extattr *ea;
1939 node = VP_TO_TMPFS_NODE(vp);
1940 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1941 return (EOPNOTSUPP);
1942 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1943 ap->a_cred, ap->a_td, VREAD);
1946 if (ap->a_name == NULL || ap->a_name[0] == '\0')
1948 namelen = strlen(ap->a_name);
1949 if (namelen > EXTATTR_MAXNAMELEN)
1952 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
1953 if (ea->ea_namespace == ap->a_attrnamespace &&
1954 namelen == ea->ea_namelen &&
1955 memcmp(ap->a_name, ea->ea_name, namelen) == 0)
1961 if (ap->a_size != NULL)
1962 *ap->a_size = ea->ea_size;
1963 if (ap->a_uio != NULL && ea->ea_size != 0)
1964 error = uiomove(ea->ea_value, ea->ea_size, ap->a_uio);
1969 tmpfs_listextattr(struct vop_listextattr_args *ap)
1971 struct vnode *vp = ap->a_vp;
1972 struct tmpfs_node *node;
1973 struct tmpfs_extattr *ea;
1976 node = VP_TO_TMPFS_NODE(vp);
1977 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1978 return (EOPNOTSUPP);
1979 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1980 ap->a_cred, ap->a_td, VREAD);
1983 if (ap->a_size != NULL)
1986 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
1987 if (ea->ea_namespace != ap->a_attrnamespace)
1989 if (ap->a_size != NULL)
1990 *ap->a_size += ea->ea_namelen + 1;
1991 if (ap->a_uio != NULL) {
1992 error = uiomove(&ea->ea_namelen, 1, ap->a_uio);
1995 error = uiomove(ea->ea_name, ea->ea_namelen, ap->a_uio);
2005 tmpfs_setextattr(struct vop_setextattr_args *ap)
2007 struct vnode *vp = ap->a_vp;
2008 struct tmpfs_mount *tmp;
2009 struct tmpfs_node *node;
2010 struct tmpfs_extattr *ea;
2011 struct tmpfs_extattr *new_ea;
2017 node = VP_TO_TMPFS_NODE(vp);
2018 tmp = VFS_TO_TMPFS(vp->v_mount);
2019 attr_size = ap->a_uio->uio_resid;
2021 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
2022 return (EOPNOTSUPP);
2023 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
2024 ap->a_cred, ap->a_td, VWRITE);
2027 if (ap->a_name == NULL || ap->a_name[0] == '\0')
2029 namelen = strlen(ap->a_name);
2030 if (namelen > EXTATTR_MAXNAMELEN)
2033 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
2034 if (ea->ea_namespace == ap->a_attrnamespace &&
2035 namelen == ea->ea_namelen &&
2036 memcmp(ap->a_name, ea->ea_name, namelen) == 0) {
2037 diff -= sizeof(struct tmpfs_extattr) + ea->ea_namelen +
2043 diff += sizeof(struct tmpfs_extattr) + namelen + attr_size;
2044 if (!tmpfs_extattr_update_mem(tmp, diff))
2046 new_ea = malloc(sizeof(struct tmpfs_extattr), M_TMPFSEA, M_WAITOK);
2047 new_ea->ea_namespace = ap->a_attrnamespace;
2048 new_ea->ea_name = malloc(namelen, M_TMPFSEA, M_WAITOK);
2049 new_ea->ea_namelen = namelen;
2050 memcpy(new_ea->ea_name, ap->a_name, namelen);
2051 if (attr_size != 0) {
2052 new_ea->ea_value = malloc(attr_size, M_TMPFSEA, M_WAITOK);
2053 new_ea->ea_size = attr_size;
2054 error = uiomove(new_ea->ea_value, attr_size, ap->a_uio);
2056 new_ea->ea_value = NULL;
2057 new_ea->ea_size = 0;
2060 tmpfs_extattr_update_mem(tmp, -diff);
2061 tmpfs_extattr_free(new_ea);
2065 LIST_REMOVE(ea, ea_extattrs);
2066 tmpfs_extattr_free(ea);
2068 LIST_INSERT_HEAD(&node->tn_extattrs, new_ea, ea_extattrs);
2073 tmpfs_seek_data_locked(vm_object_t obj, off_t noff)
2076 vm_pindex_t p, p_m, p_swp;
2078 p = OFF_TO_IDX(noff);
2079 m = vm_page_find_least(obj, p);
2082 * Microoptimize the most common case for SEEK_DATA, where
2083 * there is no hole and the page is resident.
2085 if (m != NULL && vm_page_any_valid(m) && m->pindex == p)
2088 p_swp = swap_pager_find_least(obj, p);
2092 p_m = m == NULL ? obj->size : m->pindex;
2093 return (IDX_TO_OFF(MIN(p_m, p_swp)));
2097 tmpfs_seek_next(off_t noff)
2099 return (noff + PAGE_SIZE - (noff & PAGE_MASK));
2103 tmpfs_seek_clamp(struct tmpfs_node *tn, off_t *noff, bool seekdata)
2105 if (*noff < tn->tn_size)
2109 *noff = tn->tn_size;
2114 tmpfs_seek_hole_locked(vm_object_t obj, off_t noff)
2117 vm_pindex_t p, p_swp;
2119 for (;; noff = tmpfs_seek_next(noff)) {
2121 * Walk over the largest sequential run of the valid pages.
2123 for (m = vm_page_lookup(obj, OFF_TO_IDX(noff));
2124 m != NULL && vm_page_any_valid(m);
2125 m = vm_page_next(m), noff = tmpfs_seek_next(noff))
2129 * Found a hole in the object's page queue. Check if
2130 * there is a hole in the swap at the same place.
2132 p = OFF_TO_IDX(noff);
2133 p_swp = swap_pager_find_least(obj, p);
2135 noff = IDX_TO_OFF(p);
2143 tmpfs_seek_datahole(struct vnode *vp, off_t *off, bool seekdata)
2145 struct tmpfs_node *tn;
2150 if (vp->v_type != VREG)
2152 tn = VP_TO_TMPFS_NODE(vp);
2156 error = tmpfs_seek_clamp(tn, &noff, seekdata);
2159 obj = tn->tn_reg.tn_aobj;
2161 VM_OBJECT_RLOCK(obj);
2162 noff = seekdata ? tmpfs_seek_data_locked(obj, noff) :
2163 tmpfs_seek_hole_locked(obj, noff);
2164 VM_OBJECT_RUNLOCK(obj);
2166 error = tmpfs_seek_clamp(tn, &noff, seekdata);
2173 tmpfs_ioctl(struct vop_ioctl_args *ap)
2175 struct vnode *vp = ap->a_vp;
2178 switch (ap->a_command) {
2181 error = vn_lock(vp, LK_SHARED);
2186 error = tmpfs_seek_datahole(vp, (off_t *)ap->a_data,
2187 ap->a_command == FIOSEEKDATA);
2198 * Vnode operations vector used for files stored in a tmpfs file system.
2200 struct vop_vector tmpfs_vnodeop_entries = {
2201 .vop_default = &default_vnodeops,
2202 .vop_lookup = vfs_cache_lookup,
2203 .vop_cachedlookup = tmpfs_cached_lookup,
2204 .vop_create = tmpfs_create,
2205 .vop_mknod = tmpfs_mknod,
2206 .vop_open = tmpfs_open,
2207 .vop_close = tmpfs_close,
2208 .vop_fplookup_vexec = tmpfs_fplookup_vexec,
2209 .vop_fplookup_symlink = tmpfs_fplookup_symlink,
2210 .vop_access = tmpfs_access,
2211 .vop_stat = tmpfs_stat,
2212 .vop_getattr = tmpfs_getattr,
2213 .vop_setattr = tmpfs_setattr,
2214 .vop_read = tmpfs_read,
2215 .vop_read_pgcache = tmpfs_read_pgcache,
2216 .vop_write = tmpfs_write,
2217 .vop_deallocate = tmpfs_deallocate,
2218 .vop_fsync = tmpfs_fsync,
2219 .vop_remove = tmpfs_remove,
2220 .vop_link = tmpfs_link,
2221 .vop_rename = tmpfs_rename,
2222 .vop_mkdir = tmpfs_mkdir,
2223 .vop_rmdir = tmpfs_rmdir,
2224 .vop_symlink = tmpfs_symlink,
2225 .vop_readdir = tmpfs_readdir,
2226 .vop_readlink = tmpfs_readlink,
2227 .vop_inactive = tmpfs_inactive,
2228 .vop_need_inactive = tmpfs_need_inactive,
2229 .vop_reclaim = tmpfs_reclaim,
2230 .vop_print = tmpfs_print,
2231 .vop_pathconf = tmpfs_pathconf,
2232 .vop_vptofh = tmpfs_vptofh,
2233 .vop_whiteout = tmpfs_whiteout,
2234 .vop_bmap = VOP_EOPNOTSUPP,
2235 .vop_vptocnp = tmpfs_vptocnp,
2236 .vop_lock1 = vop_lock,
2237 .vop_unlock = vop_unlock,
2238 .vop_islocked = vop_islocked,
2239 .vop_deleteextattr = tmpfs_deleteextattr,
2240 .vop_getextattr = tmpfs_getextattr,
2241 .vop_listextattr = tmpfs_listextattr,
2242 .vop_setextattr = tmpfs_setextattr,
2243 .vop_add_writecount = vop_stdadd_writecount_nomsync,
2244 .vop_ioctl = tmpfs_ioctl,
2246 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);
2249 * Same vector for mounts which do not use namecache.
2251 struct vop_vector tmpfs_vnodeop_nonc_entries = {
2252 .vop_default = &tmpfs_vnodeop_entries,
2253 .vop_lookup = tmpfs_lookup,
2255 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_nonc_entries);