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