1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
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/fcntl.h>
46 #include <sys/filio.h>
47 #include <sys/limits.h>
48 #include <sys/lockf.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
54 #include <sys/rwlock.h>
55 #include <sys/sched.h>
58 #include <sys/sysctl.h>
59 #include <sys/unistd.h>
60 #include <sys/vnode.h>
61 #include <security/audit/audit.h>
62 #include <security/mac/mac_framework.h>
65 #include <vm/vm_param.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_pager.h>
69 #include <vm/swap_pager.h>
71 #include <fs/tmpfs/tmpfs_vnops.h>
72 #include <fs/tmpfs/tmpfs.h>
74 SYSCTL_DECL(_vfs_tmpfs);
77 static volatile int tmpfs_rename_restarts;
78 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
79 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
80 "Times rename had to restart due to lock contention");
83 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
87 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
91 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
93 struct tmpfs_dirent *de;
94 struct tmpfs_node *dnode, *pnode;
95 struct tmpfs_mount *tm;
98 /* Caller assumes responsibility for ensuring access (VEXEC). */
99 dnode = VP_TO_TMPFS_DIR(dvp);
102 /* We cannot be requesting the parent directory of the root node. */
103 MPASS(IMPLIES(dnode->tn_type == VDIR &&
104 dnode->tn_dir.tn_parent == dnode,
105 !(cnp->cn_flags & ISDOTDOT)));
107 TMPFS_ASSERT_LOCKED(dnode);
108 if (dnode->tn_dir.tn_parent == NULL) {
112 if (cnp->cn_flags & ISDOTDOT) {
113 tm = VFS_TO_TMPFS(dvp->v_mount);
114 pnode = dnode->tn_dir.tn_parent;
115 tmpfs_ref_node(pnode);
116 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
117 pnode, cnp->cn_lkflags, vpp);
118 tmpfs_free_node(tm, pnode);
121 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
126 de = tmpfs_dir_lookup(dnode, NULL, cnp);
127 if (de != NULL && de->td_node == NULL)
128 cnp->cn_flags |= ISWHITEOUT;
129 if (de == NULL || de->td_node == NULL) {
131 * The entry was not found in the directory.
132 * This is OK if we are creating or renaming an
133 * entry and are working on the last component of
136 if ((cnp->cn_flags & ISLASTCN) &&
137 (cnp->cn_nameiop == CREATE || \
138 cnp->cn_nameiop == RENAME ||
139 (cnp->cn_nameiop == DELETE &&
140 cnp->cn_flags & DOWHITEOUT &&
141 cnp->cn_flags & ISWHITEOUT))) {
142 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
151 struct tmpfs_node *tnode;
154 * The entry was found, so get its associated
160 * If we are not at the last path component and
161 * found a non-directory or non-link entry (which
162 * may itself be pointing to a directory), raise
165 if ((tnode->tn_type != VDIR &&
166 tnode->tn_type != VLNK) &&
167 !(cnp->cn_flags & ISLASTCN)) {
173 * If we are deleting or renaming the entry, keep
174 * track of its tmpfs_dirent so that it can be
175 * easily deleted later.
177 if ((cnp->cn_flags & ISLASTCN) &&
178 (cnp->cn_nameiop == DELETE ||
179 cnp->cn_nameiop == RENAME)) {
180 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
185 /* Allocate a new vnode on the matching entry. */
186 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
187 cnp->cn_lkflags, vpp);
191 if ((dnode->tn_mode & S_ISTXT) &&
192 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
193 curthread) && VOP_ACCESS(*vpp, VADMIN,
194 cnp->cn_cred, curthread)) {
201 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
202 cnp->cn_lkflags, vpp);
210 * Store the result of this lookup in the cache. Avoid this if the
211 * request was for creation, as it does not improve timings on
214 if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
215 cache_enter(dvp, *vpp, cnp);
219 * If there were no errors, *vpp cannot be null and it must be
222 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
228 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
231 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
235 tmpfs_lookup(struct vop_lookup_args *v)
237 struct vnode *dvp = v->a_dvp;
238 struct vnode **vpp = v->a_vpp;
239 struct componentname *cnp = v->a_cnp;
242 /* Check accessibility of requested node as a first step. */
243 error = vn_dir_check_exec(dvp, cnp);
247 return (tmpfs_lookup1(dvp, vpp, cnp));
251 tmpfs_create(struct vop_create_args *v)
253 struct vnode *dvp = v->a_dvp;
254 struct vnode **vpp = v->a_vpp;
255 struct componentname *cnp = v->a_cnp;
256 struct vattr *vap = v->a_vap;
259 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
261 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
262 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
263 cache_enter(dvp, *vpp, cnp);
268 tmpfs_mknod(struct vop_mknod_args *v)
270 struct vnode *dvp = v->a_dvp;
271 struct vnode **vpp = v->a_vpp;
272 struct componentname *cnp = v->a_cnp;
273 struct vattr *vap = v->a_vap;
275 if (vap->va_type != VBLK && vap->va_type != VCHR &&
276 vap->va_type != VFIFO)
279 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
282 struct fileops tmpfs_fnops;
285 tmpfs_open(struct vop_open_args *v)
288 struct tmpfs_node *node;
294 node = VP_TO_TMPFS_NODE(vp);
297 * The file is still active but all its names have been removed
298 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
299 * it is about to die.
301 if (node->tn_links < 1)
304 /* If the file is marked append-only, deny write requests. */
305 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
309 /* For regular files, the call below is nop. */
310 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
311 OBJ_DEAD) == 0, ("dead object"));
312 vnode_create_vobject(vp, node->tn_size, v->a_td);
316 MPASS(fp == NULL || fp->f_data == NULL);
317 if (error == 0 && fp != NULL && vp->v_type == VREG) {
318 tmpfs_ref_node(node);
319 finit_vnode(fp, mode, node, &tmpfs_fnops);
326 tmpfs_close(struct vop_close_args *v)
328 struct vnode *vp = v->a_vp;
330 /* Update node times. */
337 tmpfs_fo_close(struct file *fp, struct thread *td)
339 struct tmpfs_node *node;
343 MPASS(node->tn_type == VREG);
344 tmpfs_free_node(node->tn_reg.tn_tmp, node);
346 return (vnops.fo_close(fp, td));
350 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
351 * the comment above cache_fplookup for details.
354 tmpfs_fplookup_vexec(struct vop_fplookup_vexec_args *v)
357 struct tmpfs_node *node;
362 node = VP_TO_TMPFS_NODE_SMR(vp);
363 if (__predict_false(node == NULL))
366 all_x = S_IXUSR | S_IXGRP | S_IXOTH;
367 mode = atomic_load_short(&node->tn_mode);
368 if (__predict_true((mode & all_x) == all_x))
372 return (vaccess_vexec_smr(mode, node->tn_uid, node->tn_gid, cred));
376 tmpfs_access(struct vop_access_args *v)
378 struct vnode *vp = v->a_vp;
379 accmode_t accmode = v->a_accmode;
380 struct ucred *cred = v->a_cred;
381 mode_t all_x = S_IXUSR | S_IXGRP | S_IXOTH;
383 struct tmpfs_node *node;
385 MPASS(VOP_ISLOCKED(vp));
387 node = VP_TO_TMPFS_NODE(vp);
390 * Common case path lookup.
392 if (__predict_true(accmode == VEXEC && (node->tn_mode & all_x) == all_x))
395 switch (vp->v_type) {
401 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
421 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
426 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
430 MPASS(VOP_ISLOCKED(vp));
436 tmpfs_stat(struct vop_stat_args *v)
438 struct vnode *vp = v->a_vp;
439 struct stat *sb = v->a_sb;
441 struct tmpfs_node *node;
444 node = VP_TO_TMPFS_NODE(vp);
446 tmpfs_update_getattr(vp);
448 error = vop_stat_helper_pre(v);
449 if (__predict_false(error))
452 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
453 sb->st_ino = node->tn_id;
454 sb->st_mode = node->tn_mode | VTTOIF(vp->v_type);
455 sb->st_nlink = node->tn_links;
456 sb->st_uid = node->tn_uid;
457 sb->st_gid = node->tn_gid;
458 sb->st_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
459 node->tn_rdev : NODEV;
460 sb->st_size = node->tn_size;
461 sb->st_atim.tv_sec = node->tn_atime.tv_sec;
462 sb->st_atim.tv_nsec = node->tn_atime.tv_nsec;
463 sb->st_mtim.tv_sec = node->tn_mtime.tv_sec;
464 sb->st_mtim.tv_nsec = node->tn_mtime.tv_nsec;
465 sb->st_ctim.tv_sec = node->tn_ctime.tv_sec;
466 sb->st_ctim.tv_nsec = node->tn_ctime.tv_nsec;
467 sb->st_birthtim.tv_sec = node->tn_birthtime.tv_sec;
468 sb->st_birthtim.tv_nsec = node->tn_birthtime.tv_nsec;
469 sb->st_blksize = PAGE_SIZE;
470 sb->st_flags = node->tn_flags;
471 sb->st_gen = node->tn_gen;
472 if (vp->v_type == VREG) {
473 obj = node->tn_reg.tn_aobj;
474 sb->st_blocks = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
476 sb->st_blocks = node->tn_size;
477 sb->st_blocks /= S_BLKSIZE;
478 return (vop_stat_helper_post(v, error));
482 tmpfs_getattr(struct vop_getattr_args *v)
484 struct vnode *vp = v->a_vp;
485 struct vattr *vap = v->a_vap;
487 struct tmpfs_node *node;
489 node = VP_TO_TMPFS_NODE(vp);
491 tmpfs_update_getattr(vp);
493 vap->va_type = vp->v_type;
494 vap->va_mode = node->tn_mode;
495 vap->va_nlink = node->tn_links;
496 vap->va_uid = node->tn_uid;
497 vap->va_gid = node->tn_gid;
498 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
499 vap->va_fileid = node->tn_id;
500 vap->va_size = node->tn_size;
501 vap->va_blocksize = PAGE_SIZE;
502 vap->va_atime = node->tn_atime;
503 vap->va_mtime = node->tn_mtime;
504 vap->va_ctime = node->tn_ctime;
505 vap->va_birthtime = node->tn_birthtime;
506 vap->va_gen = node->tn_gen;
507 vap->va_flags = node->tn_flags;
508 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
509 node->tn_rdev : NODEV;
510 if (vp->v_type == VREG) {
511 obj = node->tn_reg.tn_aobj;
512 vap->va_bytes = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
514 vap->va_bytes = node->tn_size;
521 tmpfs_setattr(struct vop_setattr_args *v)
523 struct vnode *vp = v->a_vp;
524 struct vattr *vap = v->a_vap;
525 struct ucred *cred = v->a_cred;
526 struct thread *td = curthread;
530 MPASS(VOP_ISLOCKED(vp));
531 ASSERT_VOP_IN_SEQC(vp);
535 /* Abort if any unsettable attribute is given. */
536 if (vap->va_type != VNON ||
537 vap->va_nlink != VNOVAL ||
538 vap->va_fsid != VNOVAL ||
539 vap->va_fileid != VNOVAL ||
540 vap->va_blocksize != VNOVAL ||
541 vap->va_gen != VNOVAL ||
542 vap->va_rdev != VNOVAL ||
543 vap->va_bytes != VNOVAL)
546 if (error == 0 && (vap->va_flags != VNOVAL))
547 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
549 if (error == 0 && (vap->va_size != VNOVAL))
550 error = tmpfs_chsize(vp, vap->va_size, cred, td);
552 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
553 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
555 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
556 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
558 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
559 vap->va_atime.tv_nsec != VNOVAL) ||
560 (vap->va_mtime.tv_sec != VNOVAL &&
561 vap->va_mtime.tv_nsec != VNOVAL) ||
562 (vap->va_birthtime.tv_sec != VNOVAL &&
563 vap->va_birthtime.tv_nsec != VNOVAL)))
564 error = tmpfs_chtimes(vp, vap, cred, td);
567 * Update the node times. We give preference to the error codes
568 * generated by this function rather than the ones that may arise
573 MPASS(VOP_ISLOCKED(vp));
579 tmpfs_read(struct vop_read_args *v)
583 struct tmpfs_node *node;
586 if (vp->v_type != VREG)
589 if (uio->uio_offset < 0)
591 node = VP_TO_TMPFS_NODE(vp);
592 tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
593 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
597 tmpfs_read_pgcache(struct vop_read_pgcache_args *v)
600 struct tmpfs_node *node;
606 VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
608 if (v->a_uio->uio_offset < 0)
614 node = VP_TO_TMPFS_NODE_SMR(vp);
617 MPASS(node->tn_type == VREG);
618 MPASS(node->tn_refcount >= 1);
619 object = node->tn_reg.tn_aobj;
623 MPASS(object->type == tmpfs_pager_type);
624 MPASS((object->flags & (OBJ_ANON | OBJ_DEAD | OBJ_SWAP)) ==
626 if (!VN_IS_DOOMED(vp)) {
627 /* size cannot become shorter due to rangelock. */
628 size = node->tn_size;
629 tmpfs_set_accessed(node->tn_reg.tn_tmp, node);
631 error = uiomove_object(object, size, v->a_uio);
640 tmpfs_write(struct vop_write_args *v)
644 struct tmpfs_node *node;
652 ioflag = v->a_ioflag;
654 node = VP_TO_TMPFS_NODE(vp);
655 oldsize = node->tn_size;
657 if (uio->uio_offset < 0 || vp->v_type != VREG)
659 if (uio->uio_resid == 0)
661 if (ioflag & IO_APPEND)
662 uio->uio_offset = node->tn_size;
663 error = vn_rlimit_fsizex(vp, uio, VFS_TO_TMPFS(vp->v_mount)->
664 tm_maxfilesize, &r, uio->uio_td);
666 vn_rlimit_fsizex_res(uio, r);
670 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
671 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
677 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
678 node->tn_status |= TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED;
679 node->tn_accessed = true;
680 if (node->tn_mode & (S_ISUID | S_ISGID)) {
681 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID)) {
682 newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
683 vn_seqc_write_begin(vp);
684 atomic_store_short(&node->tn_mode, newmode);
685 vn_seqc_write_end(vp);
689 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
692 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
693 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
695 vn_rlimit_fsizex_res(uio, r);
700 tmpfs_deallocate(struct vop_deallocate_args *v)
702 return (tmpfs_reg_punch_hole(v->a_vp, v->a_offset, v->a_len));
706 tmpfs_fsync(struct vop_fsync_args *v)
708 struct vnode *vp = v->a_vp;
710 MPASS(VOP_ISLOCKED(vp));
712 tmpfs_check_mtime(vp);
719 tmpfs_remove(struct vop_remove_args *v)
721 struct vnode *dvp = v->a_dvp;
722 struct vnode *vp = v->a_vp;
725 struct tmpfs_dirent *de;
726 struct tmpfs_mount *tmp;
727 struct tmpfs_node *dnode;
728 struct tmpfs_node *node;
730 MPASS(VOP_ISLOCKED(dvp));
731 MPASS(VOP_ISLOCKED(vp));
733 if (vp->v_type == VDIR) {
738 dnode = VP_TO_TMPFS_DIR(dvp);
739 node = VP_TO_TMPFS_NODE(vp);
740 tmp = VFS_TO_TMPFS(vp->v_mount);
741 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
744 /* Files marked as immutable or append-only cannot be deleted. */
745 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
746 (dnode->tn_flags & APPEND)) {
751 /* Remove the entry from the directory; as it is a file, we do not
752 * have to change the number of hard links of the directory. */
753 tmpfs_dir_detach(dvp, de);
754 if (v->a_cnp->cn_flags & DOWHITEOUT)
755 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
757 /* Free the directory entry we just deleted. Note that the node
758 * referred by it will not be removed until the vnode is really
760 tmpfs_free_dirent(tmp, de);
762 node->tn_status |= TMPFS_NODE_CHANGED;
763 node->tn_accessed = true;
771 tmpfs_link(struct vop_link_args *v)
773 struct vnode *dvp = v->a_tdvp;
774 struct vnode *vp = v->a_vp;
775 struct componentname *cnp = v->a_cnp;
778 struct tmpfs_dirent *de;
779 struct tmpfs_node *node;
781 MPASS(VOP_ISLOCKED(dvp));
782 MPASS(dvp != vp); /* XXX When can this be false? */
783 node = VP_TO_TMPFS_NODE(vp);
785 /* Ensure that we do not overflow the maximum number of links imposed
787 MPASS(node->tn_links <= TMPFS_LINK_MAX);
788 if (node->tn_links == TMPFS_LINK_MAX) {
793 /* We cannot create links of files marked immutable or append-only. */
794 if (node->tn_flags & (IMMUTABLE | APPEND)) {
799 /* Allocate a new directory entry to represent the node. */
800 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
801 cnp->cn_nameptr, cnp->cn_namelen, &de);
805 /* Insert the new directory entry into the appropriate directory. */
806 if (cnp->cn_flags & ISWHITEOUT)
807 tmpfs_dir_whiteout_remove(dvp, cnp);
808 tmpfs_dir_attach(dvp, de);
810 /* vp link count has changed, so update node times. */
811 node->tn_status |= TMPFS_NODE_CHANGED;
821 * We acquire all but fdvp locks using non-blocking acquisitions. If we
822 * fail to acquire any lock in the path we will drop all held locks,
823 * acquire the new lock in a blocking fashion, and then release it and
824 * restart the rename. This acquire/release step ensures that we do not
825 * spin on a lock waiting for release. On error release all vnode locks
826 * and decrement references the way tmpfs_rename() would do.
829 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
830 struct vnode *tdvp, struct vnode **tvpp,
831 struct componentname *fcnp, struct componentname *tcnp)
835 struct tmpfs_dirent *de;
836 int error, restarts = 0;
839 if (*tvpp != NULL && *tvpp != tdvp)
845 error = vn_lock(fdvp, LK_EXCLUSIVE);
848 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
850 error = vn_lock(tdvp, LK_EXCLUSIVE);
857 * Re-resolve fvp to be certain it still exists and fetch the
860 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
864 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
865 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
871 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
877 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
882 * Concurrent rename race.
897 * Re-resolve tvp and acquire the vnode lock if present.
899 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
901 * If tvp disappeared we just carry on.
903 if (de == NULL && *tvpp != NULL) {
908 * Get the tvp ino if the lookup succeeded. We may have to restart
909 * if the non-blocking acquire fails.
913 error = tmpfs_alloc_vp(mp, de->td_node,
914 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
923 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
929 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
938 tmpfs_rename_restarts += restarts;
948 tmpfs_rename_restarts += restarts;
954 tmpfs_rename(struct vop_rename_args *v)
956 struct vnode *fdvp = v->a_fdvp;
957 struct vnode *fvp = v->a_fvp;
958 struct componentname *fcnp = v->a_fcnp;
959 struct vnode *tdvp = v->a_tdvp;
960 struct vnode *tvp = v->a_tvp;
961 struct componentname *tcnp = v->a_tcnp;
963 struct tmpfs_dirent *de;
964 struct tmpfs_mount *tmp;
965 struct tmpfs_node *fdnode;
966 struct tmpfs_node *fnode;
967 struct tmpfs_node *tnode;
968 struct tmpfs_node *tdnode;
972 MPASS(VOP_ISLOCKED(tdvp));
973 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
975 want_seqc_end = false;
978 * Disallow cross-device renames.
979 * XXX Why isn't this done by the caller?
981 if (fvp->v_mount != tdvp->v_mount ||
982 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
987 /* If source and target are the same file, there is nothing to do. */
994 * If we need to move the directory between entries, lock the
995 * source so that we can safely operate on it.
997 if (fdvp != tdvp && fdvp != tvp) {
998 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
999 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1003 ASSERT_VOP_ELOCKED(fdvp,
1004 "tmpfs_rename: fdvp not locked");
1005 ASSERT_VOP_ELOCKED(tdvp,
1006 "tmpfs_rename: tdvp not locked");
1008 ASSERT_VOP_ELOCKED(tvp,
1009 "tmpfs_rename: tvp not locked");
1018 vn_seqc_write_begin(tvp);
1019 vn_seqc_write_begin(tdvp);
1020 vn_seqc_write_begin(fvp);
1021 vn_seqc_write_begin(fdvp);
1022 want_seqc_end = true;
1024 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1025 tdnode = VP_TO_TMPFS_DIR(tdvp);
1026 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1027 fdnode = VP_TO_TMPFS_DIR(fdvp);
1028 fnode = VP_TO_TMPFS_NODE(fvp);
1029 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1032 * Entry can disappear before we lock fdvp,
1033 * also avoid manipulating '.' and '..' entries.
1036 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1037 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1043 MPASS(de->td_node == fnode);
1046 * If re-naming a directory to another preexisting directory
1047 * ensure that the target directory is empty so that its
1048 * removal causes no side effects.
1049 * Kern_rename guarantees the destination to be a directory
1050 * if the source is one.
1053 MPASS(tnode != NULL);
1055 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1056 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1061 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1062 if (tnode->tn_size > 0) {
1066 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1069 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1073 MPASS(fnode->tn_type != VDIR &&
1074 tnode->tn_type != VDIR);
1078 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1079 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1085 * Ensure that we have enough memory to hold the new name, if it
1086 * has to be changed.
1088 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1089 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1090 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1095 * If the node is being moved to another directory, we have to do
1098 if (fdnode != tdnode) {
1100 * In case we are moving a directory, we have to adjust its
1101 * parent to point to the new parent.
1103 if (de->td_node->tn_type == VDIR) {
1104 struct tmpfs_node *n;
1107 * Ensure the target directory is not a child of the
1108 * directory being moved. Otherwise, we'd end up
1113 * TMPFS_LOCK guaranties that no nodes are freed while
1114 * traversing the list. Nodes can only be marked as
1115 * removed: tn_parent == NULL.
1119 while (n != n->tn_dir.tn_parent) {
1120 struct tmpfs_node *parent;
1123 TMPFS_NODE_UNLOCK(n);
1126 if (newname != NULL)
1127 free(newname, M_TMPFSNAME);
1130 parent = n->tn_dir.tn_parent;
1131 TMPFS_NODE_UNLOCK(n);
1132 if (parent == NULL) {
1136 TMPFS_NODE_LOCK(parent);
1137 if (parent->tn_dir.tn_parent == NULL) {
1138 TMPFS_NODE_UNLOCK(parent);
1147 if (newname != NULL)
1148 free(newname, M_TMPFSNAME);
1151 TMPFS_NODE_UNLOCK(n);
1153 /* Adjust the parent pointer. */
1154 TMPFS_VALIDATE_DIR(fnode);
1155 TMPFS_NODE_LOCK(de->td_node);
1156 de->td_node->tn_dir.tn_parent = tdnode;
1157 TMPFS_NODE_UNLOCK(de->td_node);
1160 * As a result of changing the target of the '..'
1161 * entry, the link count of the source and target
1162 * directories has to be adjusted.
1164 TMPFS_NODE_LOCK(tdnode);
1165 TMPFS_ASSERT_LOCKED(tdnode);
1167 TMPFS_NODE_UNLOCK(tdnode);
1169 TMPFS_NODE_LOCK(fdnode);
1170 TMPFS_ASSERT_LOCKED(fdnode);
1172 TMPFS_NODE_UNLOCK(fdnode);
1177 * Do the move: just remove the entry from the source directory
1178 * and insert it into the target one.
1180 tmpfs_dir_detach(fdvp, de);
1182 if (fcnp->cn_flags & DOWHITEOUT)
1183 tmpfs_dir_whiteout_add(fdvp, fcnp);
1184 if (tcnp->cn_flags & ISWHITEOUT)
1185 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1188 * If the name has changed, we need to make it effective by changing
1189 * it in the directory entry.
1191 if (newname != NULL) {
1192 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1194 free(de->ud.td_name, M_TMPFSNAME);
1195 de->ud.td_name = newname;
1196 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1198 fnode->tn_status |= TMPFS_NODE_CHANGED;
1199 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1203 * If we are overwriting an entry, we have to remove the old one
1204 * from the target directory.
1207 struct tmpfs_dirent *tde;
1209 /* Remove the old entry from the target directory. */
1210 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1211 tmpfs_dir_detach(tdvp, tde);
1214 * Free the directory entry we just deleted. Note that the
1215 * node referred by it will not be removed until the vnode is
1218 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1221 tmpfs_dir_attach(tdvp, de);
1223 if (tmpfs_use_nc(fvp)) {
1224 cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1230 if (fdvp != tdvp && fdvp != tvp)
1234 if (want_seqc_end) {
1236 vn_seqc_write_end(tvp);
1237 vn_seqc_write_end(tdvp);
1238 vn_seqc_write_end(fvp);
1239 vn_seqc_write_end(fdvp);
1243 * Release target nodes.
1244 * XXX: I don't understand when tdvp can be the same as tvp, but
1245 * other code takes care of this...
1254 /* Release source nodes. */
1262 tmpfs_mkdir(struct vop_mkdir_args *v)
1264 struct vnode *dvp = v->a_dvp;
1265 struct vnode **vpp = v->a_vpp;
1266 struct componentname *cnp = v->a_cnp;
1267 struct vattr *vap = v->a_vap;
1269 MPASS(vap->va_type == VDIR);
1271 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
1275 tmpfs_rmdir(struct vop_rmdir_args *v)
1277 struct vnode *dvp = v->a_dvp;
1278 struct vnode *vp = v->a_vp;
1281 struct tmpfs_dirent *de;
1282 struct tmpfs_mount *tmp;
1283 struct tmpfs_node *dnode;
1284 struct tmpfs_node *node;
1286 MPASS(VOP_ISLOCKED(dvp));
1287 MPASS(VOP_ISLOCKED(vp));
1289 tmp = VFS_TO_TMPFS(dvp->v_mount);
1290 dnode = VP_TO_TMPFS_DIR(dvp);
1291 node = VP_TO_TMPFS_DIR(vp);
1293 /* Directories with more than two entries ('.' and '..') cannot be
1295 if (node->tn_size > 0) {
1300 if ((dnode->tn_flags & APPEND)
1301 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1306 /* This invariant holds only if we are not trying to remove "..".
1307 * We checked for that above so this is safe now. */
1308 MPASS(node->tn_dir.tn_parent == dnode);
1310 /* Get the directory entry associated with node (vp). This was
1311 * filled by tmpfs_lookup while looking up the entry. */
1312 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1313 MPASS(TMPFS_DIRENT_MATCHES(de,
1314 v->a_cnp->cn_nameptr,
1315 v->a_cnp->cn_namelen));
1317 /* Check flags to see if we are allowed to remove the directory. */
1318 if ((dnode->tn_flags & APPEND) != 0 ||
1319 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1324 /* Detach the directory entry from the directory (dnode). */
1325 tmpfs_dir_detach(dvp, de);
1326 if (v->a_cnp->cn_flags & DOWHITEOUT)
1327 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1329 /* No vnode should be allocated for this entry from this point */
1330 TMPFS_NODE_LOCK(node);
1332 node->tn_dir.tn_parent = NULL;
1333 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1334 node->tn_accessed = true;
1336 TMPFS_NODE_UNLOCK(node);
1338 TMPFS_NODE_LOCK(dnode);
1340 dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1341 dnode->tn_accessed = true;
1342 TMPFS_NODE_UNLOCK(dnode);
1344 if (tmpfs_use_nc(dvp)) {
1345 cache_vop_rmdir(dvp, vp);
1348 /* Free the directory entry we just deleted. Note that the node
1349 * referred by it will not be removed until the vnode is really
1351 tmpfs_free_dirent(tmp, de);
1353 /* Release the deleted vnode (will destroy the node, notify
1354 * interested parties and clean it from the cache). */
1356 dnode->tn_status |= TMPFS_NODE_CHANGED;
1366 tmpfs_symlink(struct vop_symlink_args *v)
1368 struct vnode *dvp = v->a_dvp;
1369 struct vnode **vpp = v->a_vpp;
1370 struct componentname *cnp = v->a_cnp;
1371 struct vattr *vap = v->a_vap;
1372 const char *target = v->a_target;
1374 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1375 MPASS(vap->va_type == VLNK);
1377 vap->va_type = VLNK;
1380 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, target));
1384 tmpfs_readdir(struct vop_readdir_args *va)
1388 struct tmpfs_mount *tm;
1389 struct tmpfs_node *node;
1391 int *eofflag, *ncookies;
1393 int error, maxcookies;
1397 eofflag = va->a_eofflag;
1398 cookies = va->a_cookies;
1399 ncookies = va->a_ncookies;
1401 /* This operation only makes sense on directory nodes. */
1402 if (vp->v_type != VDIR)
1406 node = VP_TO_TMPFS_DIR(vp);
1407 tm = VFS_TO_TMPFS(vp->v_mount);
1409 startresid = uio->uio_resid;
1411 /* Allocate cookies for NFS and compat modules. */
1412 if (cookies != NULL && ncookies != NULL) {
1413 maxcookies = howmany(node->tn_size,
1414 sizeof(struct tmpfs_dirent)) + 2;
1415 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1420 if (cookies == NULL)
1421 error = tmpfs_dir_getdents(tm, node, uio, 0, NULL, NULL);
1423 error = tmpfs_dir_getdents(tm, node, uio, maxcookies, *cookies,
1426 /* Buffer was filled without hitting EOF. */
1427 if (error == EJUSTRETURN)
1428 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1430 if (error != 0 && cookies != NULL && ncookies != NULL) {
1431 free(*cookies, M_TEMP);
1436 if (eofflag != NULL)
1438 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1444 tmpfs_readlink(struct vop_readlink_args *v)
1446 struct vnode *vp = v->a_vp;
1447 struct uio *uio = v->a_uio;
1450 struct tmpfs_node *node;
1452 MPASS(uio->uio_offset == 0);
1453 MPASS(vp->v_type == VLNK);
1455 node = VP_TO_TMPFS_NODE(vp);
1457 error = uiomove(node->tn_link_target, MIN(node->tn_size, uio->uio_resid),
1459 tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
1465 * VOP_FPLOOKUP_SYMLINK routines are subject to special circumstances, see
1466 * the comment above cache_fplookup for details.
1468 * Check tmpfs_alloc_node for tmpfs-specific synchronisation notes.
1471 tmpfs_fplookup_symlink(struct vop_fplookup_symlink_args *v)
1474 struct tmpfs_node *node;
1478 node = VP_TO_TMPFS_NODE_SMR(vp);
1479 if (__predict_false(node == NULL))
1481 if (!atomic_load_char(&node->tn_link_smr))
1483 symlink = atomic_load_ptr(&node->tn_link_target);
1484 if (symlink == NULL)
1487 return (cache_symlink_resolve(v->a_fpl, symlink, node->tn_size));
1491 tmpfs_inactive(struct vop_inactive_args *v)
1494 struct tmpfs_node *node;
1497 node = VP_TO_TMPFS_NODE(vp);
1498 if (node->tn_links == 0)
1501 tmpfs_check_mtime(vp);
1506 tmpfs_need_inactive(struct vop_need_inactive_args *ap)
1509 struct tmpfs_node *node;
1510 struct vm_object *obj;
1513 node = VP_TO_TMPFS_NODE(vp);
1514 if (node->tn_links == 0)
1516 if (vp->v_type == VREG) {
1518 if (obj->generation != obj->cleangeneration)
1527 tmpfs_reclaim(struct vop_reclaim_args *v)
1530 struct tmpfs_mount *tmp;
1531 struct tmpfs_node *node;
1535 node = VP_TO_TMPFS_NODE(vp);
1536 tmp = VFS_TO_TMPFS(vp->v_mount);
1538 if (vp->v_type == VREG)
1539 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1540 vp->v_object = NULL;
1543 TMPFS_NODE_LOCK(node);
1547 * If the node referenced by this vnode was deleted by the user,
1548 * we must free its associated data structures (now that the vnode
1549 * is being reclaimed).
1552 if (node->tn_links == 0 &&
1553 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1554 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1555 unlock = !tmpfs_free_node_locked(tmp, node, true);
1559 TMPFS_NODE_UNLOCK(node);
1563 MPASS(vp->v_data == NULL);
1568 tmpfs_print(struct vop_print_args *v)
1570 struct vnode *vp = v->a_vp;
1572 struct tmpfs_node *node;
1574 node = VP_TO_TMPFS_NODE(vp);
1576 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
1577 node, node->tn_flags, (uintmax_t)node->tn_links);
1578 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1579 node->tn_mode, node->tn_uid, node->tn_gid,
1580 (intmax_t)node->tn_size, node->tn_status);
1582 if (vp->v_type == VFIFO)
1591 tmpfs_pathconf(struct vop_pathconf_args *v)
1593 struct vnode *vp = v->a_vp;
1594 int name = v->a_name;
1595 long *retval = v->a_retval;
1603 *retval = TMPFS_LINK_MAX;
1606 case _PC_SYMLINK_MAX:
1607 *retval = MAXPATHLEN;
1615 if (vp->v_type == VDIR || vp->v_type == VFIFO)
1621 case _PC_CHOWN_RESTRICTED:
1633 case _PC_FILESIZEBITS:
1637 case _PC_MIN_HOLE_SIZE:
1638 *retval = PAGE_SIZE;
1642 error = vop_stdpathconf(v);
1649 tmpfs_vptofh(struct vop_vptofh_args *ap)
1652 IN struct vnode *a_vp;
1653 IN struct fid *a_fhp;
1657 struct tmpfs_fid_data tfd;
1658 struct tmpfs_node *node;
1661 node = VP_TO_TMPFS_NODE(ap->a_vp);
1663 fhp->fid_len = sizeof(tfd);
1666 * Copy into fid_data from the stack to avoid unaligned pointer use.
1667 * See the comment in sys/mount.h on struct fid for details.
1669 tfd.tfd_id = node->tn_id;
1670 tfd.tfd_gen = node->tn_gen;
1671 memcpy(fhp->fid_data, &tfd, fhp->fid_len);
1677 tmpfs_whiteout(struct vop_whiteout_args *ap)
1679 struct vnode *dvp = ap->a_dvp;
1680 struct componentname *cnp = ap->a_cnp;
1681 struct tmpfs_dirent *de;
1683 switch (ap->a_flags) {
1687 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1689 return (de->td_node == NULL ? 0 : EEXIST);
1690 return (tmpfs_dir_whiteout_add(dvp, cnp));
1692 tmpfs_dir_whiteout_remove(dvp, cnp);
1695 panic("tmpfs_whiteout: unknown op");
1700 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1701 struct tmpfs_dirent **pde)
1703 struct tmpfs_dir_cursor dc;
1704 struct tmpfs_dirent *de;
1706 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1707 de = tmpfs_dir_next(tnp, &dc)) {
1708 if (de->td_node == tn) {
1717 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1718 struct tmpfs_node *tnp, char *buf, size_t *buflen, struct vnode **dvp)
1720 struct tmpfs_dirent *de;
1723 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1727 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1730 i -= de->td_namelen;
1734 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1751 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1753 struct vnode *vp, **dvp;
1754 struct tmpfs_node *tn, *tnp, *tnp1;
1755 struct tmpfs_dirent *de;
1756 struct tmpfs_mount *tm;
1764 buflen = ap->a_buflen;
1766 tm = VFS_TO_TMPFS(vp->v_mount);
1767 tn = VP_TO_TMPFS_NODE(vp);
1768 if (tn->tn_type == VDIR) {
1769 tnp = tn->tn_dir.tn_parent;
1772 tmpfs_ref_node(tnp);
1773 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1775 tmpfs_free_node(tm, tnp);
1781 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1782 if (tnp->tn_type != VDIR)
1784 TMPFS_NODE_LOCK(tnp);
1785 tmpfs_ref_node(tnp);
1788 * tn_vnode cannot be instantiated while we hold the
1789 * node lock, so the directory cannot be changed while
1790 * we iterate over it. Do this to avoid instantiating
1791 * vnode for directories which cannot point to our
1794 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1798 TMPFS_NODE_UNLOCK(tnp);
1800 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1803 tmpfs_free_node(tm, tnp);
1806 if (VN_IS_DOOMED(vp)) {
1807 tmpfs_free_node(tm, tnp);
1811 TMPFS_NODE_LOCK(tnp);
1813 if (tmpfs_free_node_locked(tm, tnp, false)) {
1816 KASSERT(tnp->tn_refcount > 0,
1817 ("node %p refcount zero", tnp));
1818 if (tnp->tn_attached) {
1819 tnp1 = LIST_NEXT(tnp, tn_entries);
1820 TMPFS_NODE_UNLOCK(tnp);
1822 TMPFS_NODE_UNLOCK(tnp);
1823 goto restart_locked;
1832 tmpfs_seek_data_locked(vm_object_t obj, off_t noff)
1835 vm_pindex_t p, p_m, p_swp;
1837 p = OFF_TO_IDX(noff);
1838 m = vm_page_find_least(obj, p);
1841 * Microoptimize the most common case for SEEK_DATA, where
1842 * there is no hole and the page is resident.
1844 if (m != NULL && vm_page_any_valid(m) && m->pindex == p)
1847 p_swp = swap_pager_find_least(obj, p);
1851 p_m = m == NULL ? obj->size : m->pindex;
1852 return (IDX_TO_OFF(MIN(p_m, p_swp)));
1856 tmpfs_seek_next(off_t noff)
1858 return (noff + PAGE_SIZE - (noff & PAGE_MASK));
1862 tmpfs_seek_clamp(struct tmpfs_node *tn, off_t *noff, bool seekdata)
1864 if (*noff < tn->tn_size)
1868 *noff = tn->tn_size;
1873 tmpfs_seek_hole_locked(vm_object_t obj, off_t noff)
1876 vm_pindex_t p, p_swp;
1878 for (;; noff = tmpfs_seek_next(noff)) {
1880 * Walk over the largest sequential run of the valid pages.
1882 for (m = vm_page_lookup(obj, OFF_TO_IDX(noff));
1883 m != NULL && vm_page_any_valid(m);
1884 m = vm_page_next(m), noff = tmpfs_seek_next(noff))
1888 * Found a hole in the object's page queue. Check if
1889 * there is a hole in the swap at the same place.
1891 p = OFF_TO_IDX(noff);
1892 p_swp = swap_pager_find_least(obj, p);
1894 noff = IDX_TO_OFF(p);
1902 tmpfs_seek_datahole(struct vnode *vp, off_t *off, bool seekdata)
1904 struct tmpfs_node *tn;
1909 if (vp->v_type != VREG)
1911 tn = VP_TO_TMPFS_NODE(vp);
1915 error = tmpfs_seek_clamp(tn, &noff, seekdata);
1918 obj = tn->tn_reg.tn_aobj;
1920 VM_OBJECT_RLOCK(obj);
1921 noff = seekdata ? tmpfs_seek_data_locked(obj, noff) :
1922 tmpfs_seek_hole_locked(obj, noff);
1923 VM_OBJECT_RUNLOCK(obj);
1925 error = tmpfs_seek_clamp(tn, &noff, seekdata);
1932 tmpfs_ioctl(struct vop_ioctl_args *ap)
1934 struct vnode *vp = ap->a_vp;
1937 switch (ap->a_command) {
1940 error = vn_lock(vp, LK_SHARED);
1945 error = tmpfs_seek_datahole(vp, (off_t *)ap->a_data,
1946 ap->a_command == FIOSEEKDATA);
1957 * Vnode operations vector used for files stored in a tmpfs file system.
1959 struct vop_vector tmpfs_vnodeop_entries = {
1960 .vop_default = &default_vnodeops,
1961 .vop_lookup = vfs_cache_lookup,
1962 .vop_cachedlookup = tmpfs_cached_lookup,
1963 .vop_create = tmpfs_create,
1964 .vop_mknod = tmpfs_mknod,
1965 .vop_open = tmpfs_open,
1966 .vop_close = tmpfs_close,
1967 .vop_fplookup_vexec = tmpfs_fplookup_vexec,
1968 .vop_fplookup_symlink = tmpfs_fplookup_symlink,
1969 .vop_access = tmpfs_access,
1970 .vop_stat = tmpfs_stat,
1971 .vop_getattr = tmpfs_getattr,
1972 .vop_setattr = tmpfs_setattr,
1973 .vop_read = tmpfs_read,
1974 .vop_read_pgcache = tmpfs_read_pgcache,
1975 .vop_write = tmpfs_write,
1976 .vop_deallocate = tmpfs_deallocate,
1977 .vop_fsync = tmpfs_fsync,
1978 .vop_remove = tmpfs_remove,
1979 .vop_link = tmpfs_link,
1980 .vop_rename = tmpfs_rename,
1981 .vop_mkdir = tmpfs_mkdir,
1982 .vop_rmdir = tmpfs_rmdir,
1983 .vop_symlink = tmpfs_symlink,
1984 .vop_readdir = tmpfs_readdir,
1985 .vop_readlink = tmpfs_readlink,
1986 .vop_inactive = tmpfs_inactive,
1987 .vop_need_inactive = tmpfs_need_inactive,
1988 .vop_reclaim = tmpfs_reclaim,
1989 .vop_print = tmpfs_print,
1990 .vop_pathconf = tmpfs_pathconf,
1991 .vop_vptofh = tmpfs_vptofh,
1992 .vop_whiteout = tmpfs_whiteout,
1993 .vop_bmap = VOP_EOPNOTSUPP,
1994 .vop_vptocnp = tmpfs_vptocnp,
1995 .vop_lock1 = vop_lock,
1996 .vop_unlock = vop_unlock,
1997 .vop_islocked = vop_islocked,
1998 .vop_add_writecount = vop_stdadd_writecount_nomsync,
1999 .vop_ioctl = tmpfs_ioctl,
2001 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);
2004 * Same vector for mounts which do not use namecache.
2006 struct vop_vector tmpfs_vnodeop_nonc_entries = {
2007 .vop_default = &tmpfs_vnodeop_entries,
2008 .vop_lookup = tmpfs_lookup,
2010 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_nonc_entries);