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