Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.56.4.8
      1 /*	$NetBSD: tmpfs_subr.c,v 1.56.4.8 2011/05/31 01:51:58 rmind Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      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
      9  * 2005 program.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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.
     19  *
     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.
     31  */
     32 
     33 /*
     34  * Efficient memory file system supporting functions.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.56.4.8 2011/05/31 01:51:58 rmind Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/dirent.h>
     42 #include <sys/event.h>
     43 #include <sys/kmem.h>
     44 #include <sys/mount.h>
     45 #include <sys/namei.h>
     46 #include <sys/time.h>
     47 #include <sys/stat.h>
     48 #include <sys/systm.h>
     49 #include <sys/swap.h>
     50 #include <sys/vnode.h>
     51 #include <sys/kauth.h>
     52 #include <sys/proc.h>
     53 #include <sys/atomic.h>
     54 
     55 #include <uvm/uvm.h>
     56 
     57 #include <miscfs/specfs/specdev.h>
     58 #include <miscfs/genfs/genfs.h>
     59 #include <fs/tmpfs/tmpfs.h>
     60 #include <fs/tmpfs/tmpfs_fifoops.h>
     61 #include <fs/tmpfs/tmpfs_specops.h>
     62 #include <fs/tmpfs/tmpfs_vnops.h>
     63 
     64 /* --------------------------------------------------------------------- */
     65 
     66 /*
     67  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
     68  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
     69  * using the credentials of the process 'p'.
     70  *
     71  * If the node type is set to 'VDIR', then the parent parameter must point
     72  * to the parent directory of the node being created.  It may only be NULL
     73  * while allocating the root node.
     74  *
     75  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
     76  * specifies the device the node represents.
     77  *
     78  * If the node type is set to 'VLNK', then the parameter target specifies
     79  * the file name of the target file for the symbolic link that is being
     80  * created.
     81  *
     82  * Note that new nodes are retrieved from the available list if it has
     83  * items or, if it is empty, from the node pool as long as there is enough
     84  * space to create them.
     85  *
     86  * Returns zero on success or an appropriate error code on failure.
     87  */
     88 int
     89 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
     90     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
     91     char *target, dev_t rdev, struct tmpfs_node **node)
     92 {
     93 	struct tmpfs_node *nnode;
     94 
     95 	/* If the root directory of the 'tmp' file system is not yet
     96 	 * allocated, this must be the request to do it. */
     97 	KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
     98 
     99 	KASSERT(IFF(type == VLNK, target != NULL));
    100 	KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
    101 
    102 	KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
    103 
    104 	nnode = NULL;
    105 	if (atomic_inc_uint_nv(&tmp->tm_nodes_cnt) >= tmp->tm_nodes_max) {
    106 		atomic_dec_uint(&tmp->tm_nodes_cnt);
    107 		return ENOSPC;
    108 	}
    109 
    110 	nnode = tmpfs_node_get(tmp);
    111 	if (nnode == NULL) {
    112 		atomic_dec_uint(&tmp->tm_nodes_cnt);
    113 		return ENOSPC;
    114 	}
    115 
    116 	/*
    117 	 * XXX Where the pool is backed by a map larger than (4GB *
    118 	 * sizeof(*nnode)), this may produce duplicate inode numbers
    119 	 * for applications that do not understand 64-bit ino_t.
    120 	 */
    121 	nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
    122 	nnode->tn_gen = arc4random();
    123 
    124 	/* Generic initialization. */
    125 	nnode->tn_type = type;
    126 	nnode->tn_size = 0;
    127 	nnode->tn_status = 0;
    128 	nnode->tn_flags = 0;
    129 	nnode->tn_links = 0;
    130 
    131 	vfs_timestamp(&nnode->tn_atime);
    132 	nnode->tn_birthtime = nnode->tn_atime;
    133 	nnode->tn_ctime = nnode->tn_atime;
    134 	nnode->tn_mtime = nnode->tn_atime;
    135 
    136 	nnode->tn_uid = uid;
    137 	nnode->tn_gid = gid;
    138 	nnode->tn_mode = mode;
    139 	nnode->tn_lockf = NULL;
    140 	nnode->tn_vnode = NULL;
    141 
    142 	/* Type-specific initialization. */
    143 	switch (nnode->tn_type) {
    144 	case VBLK:
    145 	case VCHR:
    146 		nnode->tn_spec.tn_dev.tn_rdev = rdev;
    147 		break;
    148 
    149 	case VDIR:
    150 		TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
    151 		nnode->tn_spec.tn_dir.tn_parent =
    152 		    (parent == NULL) ? nnode : parent;
    153 		nnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    154 		nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    155 		nnode->tn_links++;
    156 		break;
    157 
    158 	case VFIFO:
    159 		/* FALLTHROUGH */
    160 	case VSOCK:
    161 		break;
    162 
    163 	case VLNK:
    164 		KASSERT(strlen(target) < MAXPATHLEN);
    165 		nnode->tn_size = strlen(target);
    166 		if (nnode->tn_size == 0) {
    167 			nnode->tn_spec.tn_lnk.tn_link = NULL;
    168 			break;
    169 		}
    170 		nnode->tn_spec.tn_lnk.tn_link =
    171 		    tmpfs_strname_alloc(tmp, nnode->tn_size);
    172 		if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
    173 			atomic_dec_uint(&tmp->tm_nodes_cnt);
    174 			tmpfs_node_put(tmp, nnode);
    175 			return ENOSPC;
    176 		}
    177 		memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
    178 		break;
    179 
    180 	case VREG:
    181 		nnode->tn_spec.tn_reg.tn_aobj =
    182 		    uao_create(INT32_MAX - PAGE_SIZE, 0);
    183 		nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
    184 		break;
    185 
    186 	default:
    187 		KASSERT(0);
    188 	}
    189 
    190 	mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
    191 
    192 	mutex_enter(&tmp->tm_lock);
    193 	LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
    194 	mutex_exit(&tmp->tm_lock);
    195 
    196 	*node = nnode;
    197 	return 0;
    198 }
    199 
    200 /* --------------------------------------------------------------------- */
    201 
    202 /*
    203  * Destroys the node pointed to by node from the file system 'tmp'.
    204  * If the node does not belong to the given mount point, the results are
    205  * unpredicted.
    206  *
    207  * If the node references a directory; no entries are allowed because
    208  * their removal could need a recursive algorithm, something forbidden in
    209  * kernel space.  Furthermore, there is not need to provide such
    210  * functionality (recursive removal) because the only primitives offered
    211  * to the user are the removal of empty directories and the deletion of
    212  * individual files.
    213  *
    214  * Note that nodes are not really deleted; in fact, when a node has been
    215  * allocated, it cannot be deleted during the whole life of the file
    216  * system.  Instead, they are moved to the available list and remain there
    217  * until reused.
    218  */
    219 void
    220 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
    221 {
    222 	size_t objsz;
    223 
    224 	mutex_enter(&tmp->tm_lock);
    225 	LIST_REMOVE(node, tn_entries);
    226 	mutex_exit(&tmp->tm_lock);
    227 	atomic_dec_uint(&tmp->tm_nodes_cnt);
    228 
    229 	switch (node->tn_type) {
    230 	case VLNK:
    231 		if (node->tn_size > 0)
    232 			tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
    233 			    node->tn_size);
    234 		break;
    235 	case VREG:
    236 		/*
    237 		 * Calculate the size of node data, decrease the used-memory
    238 		 * counter, and destroy the memory object (if any).
    239 		 */
    240 		objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
    241 		if (objsz != 0) {
    242 			tmpfs_mem_decr(tmp, objsz);
    243 		}
    244 		if (node->tn_spec.tn_reg.tn_aobj != NULL) {
    245 			uao_detach(node->tn_spec.tn_reg.tn_aobj);
    246 		}
    247 		break;
    248 	default:
    249 		break;
    250 	}
    251 
    252 	mutex_destroy(&node->tn_vlock);
    253 	tmpfs_node_put(tmp, node);
    254 }
    255 
    256 /* --------------------------------------------------------------------- */
    257 
    258 /*
    259  * Allocates a new directory entry for the node node with a name of name.
    260  * The new directory entry is returned in *de.
    261  *
    262  * The link count of node is increased by one to reflect the new object
    263  * referencing it.  This takes care of notifying kqueue listeners about
    264  * this change.
    265  *
    266  * Returns zero on success or an appropriate error code on failure.
    267  */
    268 int
    269 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
    270     const char *name, uint16_t len, struct tmpfs_dirent **de)
    271 {
    272 	struct tmpfs_dirent *nde;
    273 
    274 	nde = tmpfs_dirent_get(tmp);
    275 	if (nde == NULL)
    276 		return ENOSPC;
    277 
    278 	nde->td_name = tmpfs_strname_alloc(tmp, len);
    279 	if (nde->td_name == NULL) {
    280 		tmpfs_dirent_put(tmp, nde);
    281 		return ENOSPC;
    282 	}
    283 	nde->td_namelen = len;
    284 	memcpy(nde->td_name, name, len);
    285 	nde->td_node = node;
    286 
    287 	if (node != TMPFS_NODE_WHITEOUT) {
    288 		node->tn_links++;
    289 		if (node->tn_links > 1 && node->tn_vnode != NULL)
    290 			VN_KNOTE(node->tn_vnode, NOTE_LINK);
    291 	}
    292 	*de = nde;
    293 
    294 	return 0;
    295 }
    296 
    297 /* --------------------------------------------------------------------- */
    298 
    299 /*
    300  * Frees a directory entry.  It is the caller's responsibility to destroy
    301  * the node referenced by it if needed.
    302  *
    303  * The link count of node is decreased by one to reflect the removal of an
    304  * object that referenced it.  This only happens if 'node_exists' is true;
    305  * otherwise the function will not access the node referred to by the
    306  * directory entry, as it may already have been released from the outside.
    307  *
    308  * Interested parties (kqueue) are notified of the link count change; note
    309  * that this can include both the node pointed to by the directory entry
    310  * as well as its parent.
    311  */
    312 void
    313 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
    314     bool node_exists)
    315 {
    316 	if (node_exists && de->td_node != TMPFS_NODE_WHITEOUT) {
    317 		struct tmpfs_node *node;
    318 
    319 		node = de->td_node;
    320 
    321 		KASSERT(node->tn_links > 0);
    322 		node->tn_links--;
    323 		if (node->tn_vnode != NULL)
    324 			VN_KNOTE(node->tn_vnode, node->tn_links == 0 ?
    325 			    NOTE_DELETE : NOTE_LINK);
    326 		if (node->tn_type == VDIR)
    327 			VN_KNOTE(node->tn_spec.tn_dir.tn_parent->tn_vnode,
    328 			    NOTE_LINK);
    329 	}
    330 
    331 	tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
    332 	tmpfs_dirent_put(tmp, de);
    333 }
    334 
    335 /* --------------------------------------------------------------------- */
    336 
    337 /*
    338  * Allocates a new vnode for the node node or returns a new reference to
    339  * an existing one if the node had already a vnode referencing it.  The
    340  * resulting locked vnode is returned in *vpp.
    341  *
    342  * Returns zero on success or an appropriate error code on failure.
    343  */
    344 int
    345 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
    346 {
    347 	kmutex_t *slock;
    348 	int error;
    349 	struct vnode *vp;
    350 
    351 	/* If there is already a vnode, then lock it. */
    352 	for (;;) {
    353 		mutex_enter(&node->tn_vlock);
    354 		if ((vp = node->tn_vnode) != NULL) {
    355 			mutex_enter(vp->v_interlock);
    356 			mutex_exit(&node->tn_vlock);
    357 			error = vget(vp, LK_EXCLUSIVE);
    358 			if (error == ENOENT) {
    359 				/* vnode was reclaimed. */
    360 				continue;
    361 			}
    362 			*vpp = vp;
    363 			return error;
    364 		}
    365 		break;
    366 	}
    367 
    368 	/*
    369 	 * Get a new vnode and associate it with our inode.  Share the
    370 	 * lock with underlying UVM object, if there is one (VREG case).
    371 	 */
    372 	if (node->tn_type == VREG) {
    373 		struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
    374 		slock = uobj->vmobjlock;
    375 	} else {
    376 		slock = NULL;
    377 	}
    378 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, slock, &vp);
    379 	if (error != 0) {
    380 		mutex_exit(&node->tn_vlock);
    381 		return error;
    382 	}
    383 
    384 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    385 	if (error != 0) {
    386 		mutex_exit(&node->tn_vlock);
    387 		ungetnewvnode(vp);
    388 		return error;
    389 	}
    390 
    391 	vp->v_type = node->tn_type;
    392 
    393 	/* Type-specific initialization. */
    394 	switch (node->tn_type) {
    395 	case VBLK:
    396 		/* FALLTHROUGH */
    397 	case VCHR:
    398 		vp->v_op = tmpfs_specop_p;
    399 		spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
    400 		break;
    401 
    402 	case VDIR:
    403 		vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
    404 		    VV_ROOT : 0;
    405 		break;
    406 
    407 	case VFIFO:
    408 		vp->v_op = tmpfs_fifoop_p;
    409 		break;
    410 
    411 	case VLNK:
    412 		/* FALLTHROUGH */
    413 	case VREG:
    414 		/* FALLTHROUGH */
    415 	case VSOCK:
    416 		break;
    417 
    418 	default:
    419 		KASSERT(0);
    420 	}
    421 
    422 	uvm_vnp_setsize(vp, node->tn_size);
    423 	vp->v_data = node;
    424 	node->tn_vnode = vp;
    425 	mutex_exit(&node->tn_vlock);
    426 	*vpp = vp;
    427 
    428 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
    429 	KASSERT(*vpp == node->tn_vnode);
    430 
    431 	return error;
    432 }
    433 
    434 /* --------------------------------------------------------------------- */
    435 
    436 /*
    437  * Destroys the association between the vnode vp and the node it
    438  * references.
    439  */
    440 void
    441 tmpfs_free_vp(struct vnode *vp)
    442 {
    443 	struct tmpfs_node *node;
    444 
    445 	node = VP_TO_TMPFS_NODE(vp);
    446 
    447 	mutex_enter(&node->tn_vlock);
    448 	node->tn_vnode = NULL;
    449 	mutex_exit(&node->tn_vlock);
    450 	vp->v_data = NULL;
    451 }
    452 
    453 /* --------------------------------------------------------------------- */
    454 
    455 /*
    456  * Allocates a new file of type 'type' and adds it to the parent directory
    457  * 'dvp'; this addition is done using the component name given in 'cnp'.
    458  * The ownership of the new file is automatically assigned based on the
    459  * credentials of the caller (through 'cnp'), the group is set based on
    460  * the parent directory and the mode is determined from the 'vap' argument.
    461  * If successful, *vpp holds a vnode to the newly created file and zero
    462  * is returned.  Otherwise *vpp is NULL and the function returns an
    463  * appropriate error code.
    464  */
    465 int
    466 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
    467     struct componentname *cnp, char *target)
    468 {
    469 	int error;
    470 	struct tmpfs_dirent *de;
    471 	struct tmpfs_mount *tmp;
    472 	struct tmpfs_node *dnode;
    473 	struct tmpfs_node *node;
    474 	struct tmpfs_node *parent;
    475 
    476 	KASSERT(VOP_ISLOCKED(dvp));
    477 
    478 	tmp = VFS_TO_TMPFS(dvp->v_mount);
    479 	dnode = VP_TO_TMPFS_DIR(dvp);
    480 	*vpp = NULL;
    481 
    482 	/* If the entry we are creating is a directory, we cannot overflow
    483 	 * the number of links of its parent, because it will get a new
    484 	 * link. */
    485 	if (vap->va_type == VDIR) {
    486 		/* Ensure that we do not overflow the maximum number of links
    487 		 * imposed by the system. */
    488 		KASSERT(dnode->tn_links <= LINK_MAX);
    489 		if (dnode->tn_links == LINK_MAX) {
    490 			error = EMLINK;
    491 			goto out;
    492 		}
    493 
    494 		parent = dnode;
    495 	} else
    496 		parent = NULL;
    497 
    498 	/* Allocate a node that represents the new file. */
    499 	error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
    500 	    dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
    501 	if (error != 0)
    502 		goto out;
    503 
    504 	/* Allocate a directory entry that points to the new file. */
    505 	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
    506 	    &de);
    507 	if (error != 0) {
    508 		tmpfs_free_node(tmp, node);
    509 		goto out;
    510 	}
    511 
    512 	/* Allocate a vnode for the new file. */
    513 	error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
    514 	if (error != 0) {
    515 		tmpfs_free_dirent(tmp, de, true);
    516 		tmpfs_free_node(tmp, node);
    517 		goto out;
    518 	}
    519 
    520 	/* Now that all required items are allocated, we can proceed to
    521 	 * insert the new node into the directory, an operation that
    522 	 * cannot fail. */
    523 	tmpfs_dir_attach(dvp, de);
    524 	if (vap->va_type == VDIR) {
    525 		VN_KNOTE(dvp, NOTE_LINK);
    526 		dnode->tn_links++;
    527 		KASSERT(dnode->tn_links <= LINK_MAX);
    528 	}
    529 
    530 out:
    531 	vput(dvp);
    532 
    533 	KASSERT(IFF(error == 0, *vpp != NULL));
    534 
    535 	return error;
    536 }
    537 
    538 /* --------------------------------------------------------------------- */
    539 
    540 /*
    541  * Attaches the directory entry de to the directory represented by vp.
    542  * Note that this does not change the link count of the node pointed by
    543  * the directory entry, as this is done by tmpfs_alloc_dirent.
    544  *
    545  * As the "parent" directory changes, interested parties are notified of
    546  * a write to it.
    547  */
    548 void
    549 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
    550 {
    551 	struct tmpfs_node *dnode;
    552 
    553 	KASSERT(VOP_ISLOCKED(vp));
    554 	dnode = VP_TO_TMPFS_DIR(vp);
    555 
    556 	TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    557 	dnode->tn_size += sizeof(struct tmpfs_dirent);
    558 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
    559 	    TMPFS_NODE_MODIFIED;
    560 	uvm_vnp_setsize(vp, dnode->tn_size);
    561 
    562 	VN_KNOTE(vp, NOTE_WRITE);
    563 }
    564 
    565 /* --------------------------------------------------------------------- */
    566 
    567 /*
    568  * Detaches the directory entry de from the directory represented by vp.
    569  * Note that this does not change the link count of the node pointed by
    570  * the directory entry, as this is done by tmpfs_free_dirent.
    571  *
    572  * As the "parent" directory changes, interested parties are notified of
    573  * a write to it.
    574  */
    575 void
    576 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
    577 {
    578 	struct tmpfs_node *dnode;
    579 
    580 	KASSERT(VOP_ISLOCKED(vp));
    581 	dnode = VP_TO_TMPFS_DIR(vp);
    582 
    583 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
    584 		dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    585 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    586 	}
    587 
    588 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    589 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
    590 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
    591 	    TMPFS_NODE_MODIFIED;
    592 	uvm_vnp_setsize(vp, dnode->tn_size);
    593 
    594 	VN_KNOTE(vp, NOTE_WRITE);
    595 }
    596 
    597 /* --------------------------------------------------------------------- */
    598 
    599 /*
    600  * Looks for a directory entry in the directory represented by node.
    601  * 'cnp' describes the name of the entry to look for.  Note that the .
    602  * and .. components are not allowed as they do not physically exist
    603  * within directories.
    604  *
    605  * Returns a pointer to the entry when found, otherwise NULL.
    606  */
    607 struct tmpfs_dirent *
    608 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp)
    609 {
    610 	struct tmpfs_dirent *de;
    611 
    612 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    613 	KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
    614 	KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
    615 	    cnp->cn_nameptr[1] == '.')));
    616 	TMPFS_VALIDATE_DIR(node);
    617 
    618 	node->tn_status |= TMPFS_NODE_ACCESSED;
    619 
    620 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    621 		KASSERT(cnp->cn_namelen < 0xffff);
    622 		if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
    623 		    memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
    624 			break;
    625 		}
    626 	}
    627 
    628 	return de;
    629 }
    630 
    631 /* --------------------------------------------------------------------- */
    632 
    633 /*
    634  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
    635  * directory and returns it in the uio space.  The function returns 0
    636  * on success, -1 if there was not enough space in the uio structure to
    637  * hold the directory entry or an appropriate error code if another
    638  * error happens.
    639  */
    640 int
    641 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
    642 {
    643 	int error;
    644 	struct dirent *dentp;
    645 
    646 	TMPFS_VALIDATE_DIR(node);
    647 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
    648 
    649 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    650 
    651 	dentp->d_fileno = node->tn_id;
    652 	dentp->d_type = DT_DIR;
    653 	dentp->d_namlen = 1;
    654 	dentp->d_name[0] = '.';
    655 	dentp->d_name[1] = '\0';
    656 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    657 
    658 	if (dentp->d_reclen > uio->uio_resid)
    659 		error = -1;
    660 	else {
    661 		error = uiomove(dentp, dentp->d_reclen, uio);
    662 		if (error == 0)
    663 			uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
    664 	}
    665 
    666 	node->tn_status |= TMPFS_NODE_ACCESSED;
    667 
    668 	kmem_free(dentp, sizeof(struct dirent));
    669 	return error;
    670 }
    671 
    672 /* --------------------------------------------------------------------- */
    673 
    674 /*
    675  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
    676  * directory and returns it in the uio space.  The function returns 0
    677  * on success, -1 if there was not enough space in the uio structure to
    678  * hold the directory entry or an appropriate error code if another
    679  * error happens.
    680  */
    681 int
    682 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
    683 {
    684 	int error;
    685 	struct dirent *dentp;
    686 
    687 	TMPFS_VALIDATE_DIR(node);
    688 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
    689 
    690 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    691 
    692 	dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
    693 	dentp->d_type = DT_DIR;
    694 	dentp->d_namlen = 2;
    695 	dentp->d_name[0] = '.';
    696 	dentp->d_name[1] = '.';
    697 	dentp->d_name[2] = '\0';
    698 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    699 
    700 	if (dentp->d_reclen > uio->uio_resid)
    701 		error = -1;
    702 	else {
    703 		error = uiomove(dentp, dentp->d_reclen, uio);
    704 		if (error == 0) {
    705 			struct tmpfs_dirent *de;
    706 
    707 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    708 			if (de == NULL)
    709 				uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    710 			else
    711 				uio->uio_offset = tmpfs_dircookie(de);
    712 		}
    713 	}
    714 
    715 	node->tn_status |= TMPFS_NODE_ACCESSED;
    716 
    717 	kmem_free(dentp, sizeof(struct dirent));
    718 	return error;
    719 }
    720 
    721 /* --------------------------------------------------------------------- */
    722 
    723 /*
    724  * Lookup a directory entry by its associated cookie.
    725  */
    726 struct tmpfs_dirent *
    727 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
    728 {
    729 	struct tmpfs_dirent *de;
    730 
    731 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    732 
    733 	if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
    734 	    node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
    735 		return node->tn_spec.tn_dir.tn_readdir_lastp;
    736 	}
    737 
    738 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    739 		if (tmpfs_dircookie(de) == cookie) {
    740 			break;
    741 		}
    742 	}
    743 
    744 	return de;
    745 }
    746 
    747 /* --------------------------------------------------------------------- */
    748 
    749 /*
    750  * Helper function for tmpfs_readdir.  Returns as much directory entries
    751  * as can fit in the uio space.  The read starts at uio->uio_offset.
    752  * The function returns 0 on success, -1 if there was not enough space
    753  * in the uio structure to hold the directory entry or an appropriate
    754  * error code if another error happens.
    755  */
    756 int
    757 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
    758 {
    759 	int error;
    760 	off_t startcookie;
    761 	struct dirent *dentp;
    762 	struct tmpfs_dirent *de;
    763 
    764 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    765 	TMPFS_VALIDATE_DIR(node);
    766 
    767 	/* Locate the first directory entry we have to return.  We have cached
    768 	 * the last readdir in the node, so use those values if appropriate.
    769 	 * Otherwise do a linear scan to find the requested entry. */
    770 	startcookie = uio->uio_offset;
    771 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
    772 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
    773 	if (startcookie == TMPFS_DIRCOOKIE_EOF) {
    774 		return 0;
    775 	} else {
    776 		de = tmpfs_dir_lookupbycookie(node, startcookie);
    777 	}
    778 	if (de == NULL) {
    779 		return EINVAL;
    780 	}
    781 
    782 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    783 
    784 	/* Read as much entries as possible; i.e., until we reach the end of
    785 	 * the directory or we exhaust uio space. */
    786 	do {
    787 		/* Create a dirent structure representing the current
    788 		 * tmpfs_node and fill it. */
    789 		if (de->td_node == TMPFS_NODE_WHITEOUT) {
    790 			dentp->d_fileno = 1;
    791 			dentp->d_type = DT_WHT;
    792 		} else {
    793 			dentp->d_fileno = de->td_node->tn_id;
    794 			switch (de->td_node->tn_type) {
    795 			case VBLK:
    796 				dentp->d_type = DT_BLK;
    797 			break;
    798 
    799 			case VCHR:
    800 				dentp->d_type = DT_CHR;
    801 				break;
    802 
    803 			case VDIR:
    804 				dentp->d_type = DT_DIR;
    805 				break;
    806 
    807 			case VFIFO:
    808 				dentp->d_type = DT_FIFO;
    809 				break;
    810 
    811 			case VLNK:
    812 				dentp->d_type = DT_LNK;
    813 				break;
    814 
    815 			case VREG:
    816 				dentp->d_type = DT_REG;
    817 				break;
    818 
    819 			case VSOCK:
    820 				dentp->d_type = DT_SOCK;
    821 			break;
    822 
    823 			default:
    824 				KASSERT(0);
    825 			}
    826 		}
    827 		dentp->d_namlen = de->td_namelen;
    828 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
    829 		(void)memcpy(dentp->d_name, de->td_name, de->td_namelen);
    830 		dentp->d_name[de->td_namelen] = '\0';
    831 		dentp->d_reclen = _DIRENT_SIZE(dentp);
    832 
    833 		/* Stop reading if the directory entry we are treating is
    834 		 * bigger than the amount of data that can be returned. */
    835 		if (dentp->d_reclen > uio->uio_resid) {
    836 			error = -1;
    837 			break;
    838 		}
    839 
    840 		/* Copy the new dirent structure into the output buffer and
    841 		 * advance pointers. */
    842 		error = uiomove(dentp, dentp->d_reclen, uio);
    843 
    844 		(*cntp)++;
    845 		de = TAILQ_NEXT(de, td_entries);
    846 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
    847 
    848 	/* Update the offset and cache. */
    849 	if (de == NULL) {
    850 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    851 		node->tn_spec.tn_dir.tn_readdir_lastn = 0;
    852 		node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    853 	} else {
    854 		node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
    855 		    tmpfs_dircookie(de);
    856 		node->tn_spec.tn_dir.tn_readdir_lastp = de;
    857 	}
    858 
    859 	node->tn_status |= TMPFS_NODE_ACCESSED;
    860 
    861 	kmem_free(dentp, sizeof(struct dirent));
    862 	return error;
    863 }
    864 
    865 /* --------------------------------------------------------------------- */
    866 
    867 /*
    868  * Resizes the aobj associated to the regular file pointed to by vp to
    869  * the size newsize.  'vp' must point to a vnode that represents a regular
    870  * file.  'newsize' must be positive.
    871  *
    872  * If the file is extended, the appropriate kevent is raised.  This does
    873  * not rise a write event though because resizing is not the same as
    874  * writing.
    875  *
    876  * Returns zero on success or an appropriate error code on failure.
    877  */
    878 int
    879 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    880 {
    881 	size_t newpages, oldpages;
    882 	struct tmpfs_mount *tmp;
    883 	struct tmpfs_node *node;
    884 	off_t oldsize;
    885 
    886 	KASSERT(vp->v_type == VREG);
    887 	KASSERT(newsize >= 0);
    888 
    889 	node = VP_TO_TMPFS_NODE(vp);
    890 	tmp = VFS_TO_TMPFS(vp->v_mount);
    891 
    892 	oldsize = node->tn_size;
    893 	oldpages = round_page(oldsize) >> PAGE_SHIFT;
    894 	newpages = round_page(newsize) >> PAGE_SHIFT;
    895 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
    896 
    897 	if (newpages > oldpages) {
    898 		/* Increase the used-memory counter if getting extra pages. */
    899 		if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
    900 			return ENOSPC;
    901 		}
    902 	} else if (newsize < oldsize) {
    903 		int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
    904 
    905 		/* Zero out the truncated part of the last page. */
    906 		uvm_vnp_zerorange(vp, newsize, zerolen);
    907 	}
    908 
    909 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
    910 	node->tn_size = newsize;
    911 	uvm_vnp_setsize(vp, newsize);
    912 
    913 	/*
    914 	 * Free "backing store".
    915 	 */
    916 	if (newpages < oldpages) {
    917 		struct uvm_object *uobj;
    918 
    919 		uobj = node->tn_spec.tn_reg.tn_aobj;
    920 		KASSERT(uobj->vmobjlock == vp->v_interlock);
    921 
    922 		mutex_enter(uobj->vmobjlock);
    923 		uao_dropswap_range(uobj, newpages, oldpages);
    924 		mutex_exit(uobj->vmobjlock);
    925 
    926 		/* Decrease the used-memory counter. */
    927 		tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
    928 	}
    929 
    930 	if (newsize > oldsize)
    931 		VN_KNOTE(vp, NOTE_EXTEND);
    932 
    933 	return 0;
    934 }
    935 
    936 /*
    937  * Change flags of the given vnode.
    938  * Caller should execute tmpfs_update on vp after a successful execution.
    939  * The vnode must be locked on entry and remain locked on exit.
    940  */
    941 int
    942 tmpfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
    943 {
    944 	int error;
    945 	struct tmpfs_node *node;
    946 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    947 	int fs_decision = 0;
    948 
    949 	KASSERT(VOP_ISLOCKED(vp));
    950 
    951 	node = VP_TO_TMPFS_NODE(vp);
    952 
    953 	/* Disallow this operation if the file system is mounted read-only. */
    954 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    955 		return EROFS;
    956 
    957 	if (kauth_cred_geteuid(cred) != node->tn_uid)
    958 		fs_decision = EACCES;
    959 
    960 	/*
    961 	 * If the new flags have non-user flags that are different than
    962 	 * those on the node, we need special permission to change them.
    963 	 */
    964 	if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
    965 		action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    966 		if (!fs_decision)
    967 			fs_decision = EPERM;
    968 	}
    969 
    970 	/*
    971 	 * Indicate that this node's flags have system attributes in them if
    972 	 * that's the case.
    973 	 */
    974 	if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
    975 		action |= KAUTH_VNODE_HAS_SYSFLAGS;
    976 	}
    977 
    978 	error = kauth_authorize_vnode(cred, action, vp, NULL, fs_decision);
    979 	if (error)
    980 		return error;
    981 
    982 	/*
    983 	 * Set the flags. If we're not setting non-user flags, be careful not
    984 	 * to overwrite them.
    985 	 *
    986 	 * XXX: Can't we always assign here? if the system flags are different,
    987 	 *      the code above should catch attempts to change them without
    988 	 *      proper permissions, and if we're here it means it's okay to
    989 	 *      change them...
    990 	 */
    991 	if (action & KAUTH_VNODE_WRITE_SYSFLAGS) {
    992 		node->tn_flags = flags;
    993 	} else {
    994 		/* Clear all user-settable flags and re-set them. */
    995 		node->tn_flags &= SF_SETTABLE;
    996 		node->tn_flags |= (flags & UF_SETTABLE);
    997 	}
    998 
    999 	node->tn_status |= TMPFS_NODE_CHANGED;
   1000 	VN_KNOTE(vp, NOTE_ATTRIB);
   1001 
   1002 	KASSERT(VOP_ISLOCKED(vp));
   1003 
   1004 	return 0;
   1005 }
   1006 
   1007 /* --------------------------------------------------------------------- */
   1008 
   1009 /*
   1010  * Change access mode on the given vnode.
   1011  * Caller should execute tmpfs_update on vp after a successful execution.
   1012  * The vnode must be locked on entry and remain locked on exit.
   1013  */
   1014 int
   1015 tmpfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
   1016 {
   1017 	int error;
   1018 	struct tmpfs_node *node;
   1019 
   1020 	KASSERT(VOP_ISLOCKED(vp));
   1021 
   1022 	node = VP_TO_TMPFS_NODE(vp);
   1023 
   1024 	/* Disallow this operation if the file system is mounted read-only. */
   1025 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1026 		return EROFS;
   1027 
   1028 	/* Immutable or append-only files cannot be modified, either. */
   1029 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1030 		return EPERM;
   1031 
   1032 	error = genfs_can_chmod(vp, cred, node->tn_uid, node->tn_gid,
   1033 	    mode);
   1034 
   1035 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
   1036 	    NULL, error);
   1037 	if (error)
   1038 		return (error);
   1039 
   1040 	node->tn_mode = (mode & ALLPERMS);
   1041 
   1042 	node->tn_status |= TMPFS_NODE_CHANGED;
   1043 	VN_KNOTE(vp, NOTE_ATTRIB);
   1044 
   1045 	KASSERT(VOP_ISLOCKED(vp));
   1046 
   1047 	return 0;
   1048 }
   1049 
   1050 /* --------------------------------------------------------------------- */
   1051 
   1052 /*
   1053  * Change ownership of the given vnode.  At least one of uid or gid must
   1054  * be different than VNOVAL.  If one is set to that value, the attribute
   1055  * is unchanged.
   1056  * Caller should execute tmpfs_update on vp after a successful execution.
   1057  * The vnode must be locked on entry and remain locked on exit.
   1058  */
   1059 int
   1060 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
   1061     struct lwp *l)
   1062 {
   1063 	int error;
   1064 	struct tmpfs_node *node;
   1065 
   1066 	KASSERT(VOP_ISLOCKED(vp));
   1067 
   1068 	node = VP_TO_TMPFS_NODE(vp);
   1069 
   1070 	/* Assign default values if they are unknown. */
   1071 	KASSERT(uid != VNOVAL || gid != VNOVAL);
   1072 	if (uid == VNOVAL)
   1073 		uid = node->tn_uid;
   1074 	if (gid == VNOVAL)
   1075 		gid = node->tn_gid;
   1076 	KASSERT(uid != VNOVAL && gid != VNOVAL);
   1077 
   1078 	/* Disallow this operation if the file system is mounted read-only. */
   1079 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1080 		return EROFS;
   1081 
   1082 	/* Immutable or append-only files cannot be modified, either. */
   1083 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1084 		return EPERM;
   1085 
   1086 	error = genfs_can_chown(vp, cred, node->tn_uid, node->tn_gid, uid,
   1087 	    gid);
   1088 
   1089 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
   1090 	    NULL, error);
   1091 	if (error)
   1092 		return (error);
   1093 
   1094 	node->tn_uid = uid;
   1095 	node->tn_gid = gid;
   1096 
   1097 	node->tn_status |= TMPFS_NODE_CHANGED;
   1098 	VN_KNOTE(vp, NOTE_ATTRIB);
   1099 
   1100 	KASSERT(VOP_ISLOCKED(vp));
   1101 
   1102 	return 0;
   1103 }
   1104 
   1105 /* --------------------------------------------------------------------- */
   1106 
   1107 /*
   1108  * Change size of the given vnode.
   1109  * Caller should execute tmpfs_update on vp after a successful execution.
   1110  * The vnode must be locked on entry and remain locked on exit.
   1111  */
   1112 int
   1113 tmpfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred,
   1114     struct lwp *l)
   1115 {
   1116 	int error;
   1117 	struct tmpfs_node *node;
   1118 
   1119 	KASSERT(VOP_ISLOCKED(vp));
   1120 
   1121 	node = VP_TO_TMPFS_NODE(vp);
   1122 
   1123 	/* Decide whether this is a valid operation based on the file type. */
   1124 	error = 0;
   1125 	switch (vp->v_type) {
   1126 	case VDIR:
   1127 		return EISDIR;
   1128 
   1129 	case VREG:
   1130 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1131 			return EROFS;
   1132 		break;
   1133 
   1134 	case VBLK:
   1135 		/* FALLTHROUGH */
   1136 	case VCHR:
   1137 		/* FALLTHROUGH */
   1138 	case VFIFO:
   1139 		/* Allow modifications of special files even if in the file
   1140 		 * system is mounted read-only (we are not modifying the
   1141 		 * files themselves, but the objects they represent). */
   1142 		return 0;
   1143 
   1144 	default:
   1145 		/* Anything else is unsupported. */
   1146 		return EOPNOTSUPP;
   1147 	}
   1148 
   1149 	/* Immutable or append-only files cannot be modified, either. */
   1150 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1151 		return EPERM;
   1152 
   1153 	error = tmpfs_truncate(vp, size);
   1154 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
   1155 	 * for us, as will update tn_status; no need to do that here. */
   1156 
   1157 	KASSERT(VOP_ISLOCKED(vp));
   1158 
   1159 	return error;
   1160 }
   1161 
   1162 /* --------------------------------------------------------------------- */
   1163 
   1164 /*
   1165  * Change access and modification times of the given vnode.
   1166  * Caller should execute tmpfs_update on vp after a successful execution.
   1167  * The vnode must be locked on entry and remain locked on exit.
   1168  */
   1169 int
   1170 tmpfs_chtimes(struct vnode *vp, const struct timespec *atime,
   1171     const struct timespec *mtime, const struct timespec *btime,
   1172     int vaflags, kauth_cred_t cred, struct lwp *l)
   1173 {
   1174 	int error;
   1175 	struct tmpfs_node *node;
   1176 
   1177 	KASSERT(VOP_ISLOCKED(vp));
   1178 
   1179 	node = VP_TO_TMPFS_NODE(vp);
   1180 
   1181 	/* Disallow this operation if the file system is mounted read-only. */
   1182 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1183 		return EROFS;
   1184 
   1185 	/* Immutable or append-only files cannot be modified, either. */
   1186 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1187 		return EPERM;
   1188 
   1189 	error = genfs_can_chtimes(vp, vaflags, node->tn_uid, cred);
   1190 
   1191 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
   1192 	    error);
   1193 	if (error)
   1194 		return (error);
   1195 
   1196 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
   1197 		node->tn_status |= TMPFS_NODE_ACCESSED;
   1198 
   1199 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
   1200 		node->tn_status |= TMPFS_NODE_MODIFIED;
   1201 
   1202 	if (btime->tv_sec == VNOVAL && btime->tv_nsec == VNOVAL)
   1203 		btime = NULL;
   1204 
   1205 	tmpfs_update(vp, atime, mtime, btime, 0);
   1206 	VN_KNOTE(vp, NOTE_ATTRIB);
   1207 
   1208 	KASSERT(VOP_ISLOCKED(vp));
   1209 
   1210 	return 0;
   1211 }
   1212 
   1213 /* --------------------------------------------------------------------- */
   1214 
   1215 /* Sync timestamps */
   1216 void
   1217 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
   1218     const struct timespec *mod, const struct timespec *birth)
   1219 {
   1220 	struct tmpfs_node *node;
   1221 	struct timespec nowtm;
   1222 
   1223 	node = VP_TO_TMPFS_NODE(vp);
   1224 
   1225 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
   1226 	    TMPFS_NODE_CHANGED)) == 0)
   1227 		return;
   1228 
   1229 	if (birth != NULL) {
   1230 		node->tn_birthtime = *birth;
   1231 	}
   1232 	vfs_timestamp(&nowtm);
   1233 
   1234 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
   1235 		node->tn_atime = acc ? *acc : nowtm;
   1236 	}
   1237 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
   1238 		node->tn_mtime = mod ? *mod : nowtm;
   1239 	}
   1240 	if (node->tn_status & TMPFS_NODE_CHANGED) {
   1241 		node->tn_ctime = nowtm;
   1242 	}
   1243 
   1244 	node->tn_status &=
   1245 	    ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
   1246 }
   1247 
   1248 /* --------------------------------------------------------------------- */
   1249 
   1250 void
   1251 tmpfs_update(struct vnode *vp, const struct timespec *acc,
   1252     const struct timespec *mod, const struct timespec *birth, int flags)
   1253 {
   1254 
   1255 	struct tmpfs_node *node;
   1256 
   1257 	KASSERT(VOP_ISLOCKED(vp));
   1258 
   1259 	node = VP_TO_TMPFS_NODE(vp);
   1260 
   1261 #if 0
   1262 	if (flags & UPDATE_CLOSE)
   1263 		; /* XXX Need to do anything special? */
   1264 #endif
   1265 
   1266 	tmpfs_itimes(vp, acc, mod, birth);
   1267 
   1268 	KASSERT(VOP_ISLOCKED(vp));
   1269 }
   1270 
   1271 /* --------------------------------------------------------------------- */
   1272 
   1273 int
   1274 tmpfs_truncate(struct vnode *vp, off_t length)
   1275 {
   1276 	bool extended;
   1277 	int error;
   1278 	struct tmpfs_node *node;
   1279 
   1280 	node = VP_TO_TMPFS_NODE(vp);
   1281 	extended = length > node->tn_size;
   1282 
   1283 	if (length < 0) {
   1284 		error = EINVAL;
   1285 		goto out;
   1286 	}
   1287 
   1288 	if (node->tn_size == length) {
   1289 		error = 0;
   1290 		goto out;
   1291 	}
   1292 
   1293 	error = tmpfs_reg_resize(vp, length);
   1294 	if (error == 0)
   1295 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1296 
   1297 out:
   1298 	tmpfs_update(vp, NULL, NULL, NULL, 0);
   1299 
   1300 	return error;
   1301 }
   1302