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