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