Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.4
      1 /*	$NetBSD: tmpfs_subr.c,v 1.4 2005/09/13 14:29:18 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005 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.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Efficient memory file system supporting functions.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.4 2005/09/13 14:29:18 yamt Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/dirent.h>
     48 #include <sys/event.h>
     49 #include <sys/malloc.h>
     50 #include <sys/mount.h>
     51 #include <sys/namei.h>
     52 #include <sys/time.h>
     53 #include <sys/stat.h>
     54 #include <sys/systm.h>
     55 #include <sys/swap.h>
     56 #include <sys/vnode.h>
     57 
     58 #include <uvm/uvm.h>
     59 
     60 #include <miscfs/specfs/specdev.h>
     61 #include <fs/tmpfs/tmpfs.h>
     62 #include <fs/tmpfs/tmpfs_fifoops.h>
     63 #include <fs/tmpfs/tmpfs_specops.h>
     64 #include <fs/tmpfs/tmpfs_vnops.h>
     65 
     66 /* --------------------------------------------------------------------- */
     67 
     68 int
     69 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
     70     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
     71     char *target, dev_t rdev, struct proc *p, struct tmpfs_node **node)
     72 {
     73 	struct tmpfs_node *nnode;
     74 
     75 	/* If the root directory of the 'tmp' file system is not yet
     76 	 * allocated, this must be the request to do it. */
     77 	KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
     78 
     79 	KASSERT(IFF(type == VLNK, target != NULL));
     80 	KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
     81 
     82 	KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
     83 
     84 	nnode = NULL;
     85 	if (LIST_EMPTY(&tmp->tm_nodes_avail)) {
     86 		KASSERT(tmp->tm_nodes_last <= tmp->tm_nodes_max);
     87 		if (tmp->tm_nodes_last == tmp->tm_nodes_max)
     88 			return ENOSPC;
     89 
     90 		nnode =
     91 		    (struct tmpfs_node *)TMPFS_POOL_GET(&tmp->tm_node_pool, 0);
     92 		if (nnode == NULL)
     93 			return ENOSPC;
     94 		nnode->tn_id = tmp->tm_nodes_last++;
     95 		nnode->tn_gen = 0;
     96 	} else {
     97 		nnode = LIST_FIRST(&tmp->tm_nodes_avail);
     98 		LIST_REMOVE(nnode, tn_entries);
     99 		nnode->tn_gen++;
    100 	}
    101 	KASSERT(nnode != NULL);
    102 	LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
    103 
    104 	/* Generic initialization. */
    105 	nnode->tn_type = type;
    106 	nnode->tn_size = 0;
    107 	nnode->tn_status = 0;
    108 	nnode->tn_flags = 0;
    109 	nnode->tn_links = 0;
    110 	(void)nanotime(&nnode->tn_atime);
    111 	nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
    112 	    nnode->tn_atime;
    113 	nnode->tn_uid = uid;
    114 	nnode->tn_gid = gid;
    115 	nnode->tn_mode = mode;
    116 	nnode->tn_vnode = NULL;
    117 
    118 	/* Type-specific initialization. */
    119 	switch (nnode->tn_type) {
    120 	case VBLK:
    121 	case VCHR:
    122 		nnode->tn_rdev = rdev;
    123 		break;
    124 
    125 	case VDIR:
    126 		TAILQ_INIT(&nnode->tn_dir);
    127 		nnode->tn_parent = (parent == NULL) ? nnode : parent;
    128 		nnode->tn_readdir_lastn = 0;
    129 		nnode->tn_readdir_lastp = NULL;
    130 		nnode->tn_links++;
    131 		nnode->tn_parent->tn_links++;
    132 		break;
    133 
    134 	case VFIFO:
    135 		/* FALLTHROUGH */
    136 	case VSOCK:
    137 		break;
    138 
    139 	case VLNK:
    140 		KASSERT(strlen(target) < MAXPATHLEN);
    141 		nnode->tn_link = tmpfs_str_pool_get(&tmp->tm_str_pool,
    142 		    strlen(target), 0);
    143 		if (nnode->tn_link == NULL) {
    144 			nnode->tn_type = VNON;
    145 			tmpfs_free_node(tmp, nnode);
    146 			return ENOSPC;
    147 		}
    148 		strcpy(nnode->tn_link, target);
    149 		nnode->tn_size = strlen(target);
    150 		break;
    151 
    152 	case VREG:
    153 		nnode->tn_aobj = uao_create(INT32_MAX - PAGE_SIZE, 0);
    154 		nnode->tn_aobj_pages = 0;
    155 		break;
    156 
    157 	default:
    158 		KASSERT(0);
    159 	}
    160 
    161 	*node = nnode;
    162 	return 0;
    163 }
    164 
    165 /* --------------------------------------------------------------------- */
    166 
    167 void
    168 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
    169 {
    170 	ino_t id;
    171 	unsigned long gen;
    172 	size_t pages;
    173 
    174 	switch (node->tn_type) {
    175 	case VNON:
    176 		/* Do not do anything.  VNON is provided to let the
    177 		 * allocation routine clean itself easily by avoiding
    178 		 * duplicating code in it. */
    179 		/* FALLTHROUGH */
    180 	case VBLK:
    181 		/* FALLTHROUGH */
    182 	case VCHR:
    183 		/* FALLTHROUGH */
    184 	case VDIR:
    185 		/* FALLTHROUGH */
    186 	case VFIFO:
    187 		/* FALLTHROUGH */
    188 	case VSOCK:
    189 		pages = 0;
    190 		break;
    191 
    192 	case VLNK:
    193 		tmpfs_str_pool_put(&tmp->tm_str_pool, node->tn_link,
    194 		    strlen(node->tn_link));
    195 		pages = 0;
    196 		break;
    197 
    198 	case VREG:
    199 		if (node->tn_aobj != NULL)
    200 			uao_detach(node->tn_aobj);
    201 		pages = node->tn_aobj_pages;
    202 		break;
    203 
    204 	default:
    205 		KASSERT(0);
    206 		pages = 0; /* Shut up gcc when !DIAGNOSTIC. */
    207 		break;
    208 	}
    209 
    210 	tmp->tm_pages_used -= pages;
    211 
    212 	LIST_REMOVE(node, tn_entries);
    213 	id = node->tn_id;
    214 	gen = node->tn_gen;
    215 	memset(node, 0, sizeof(struct tmpfs_node));
    216 	node->tn_id = id;
    217 	node->tn_type = VNON;
    218 	node->tn_gen = gen;
    219 	LIST_INSERT_HEAD(&tmp->tm_nodes_avail, node, tn_entries);
    220 }
    221 
    222 /* --------------------------------------------------------------------- */
    223 
    224 int
    225 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
    226     const char *name, uint16_t len, struct tmpfs_dirent **de)
    227 {
    228 	struct tmpfs_dirent *nde;
    229 
    230 	nde = (struct tmpfs_dirent *)TMPFS_POOL_GET(&tmp->tm_dirent_pool, 0);
    231 	if (nde == NULL)
    232 		return ENOSPC;
    233 
    234 	nde->td_name = tmpfs_str_pool_get(&tmp->tm_str_pool, len, 0);
    235 	if (nde->td_name == NULL) {
    236 		TMPFS_POOL_PUT(&tmp->tm_dirent_pool, nde);
    237 		return ENOSPC;
    238 	}
    239 	nde->td_namelen = len;
    240 	memcpy(nde->td_name, name, len);
    241 	nde->td_node = node;
    242 
    243 	node->tn_links++;
    244 	*de = nde;
    245 
    246 	return 0;
    247 }
    248 
    249 /* --------------------------------------------------------------------- */
    250 
    251 void
    252 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
    253     boolean_t node_exists)
    254 {
    255 	if (node_exists) {
    256 		struct tmpfs_node *node;
    257 
    258 		node = de->td_node;
    259 
    260 		KASSERT(node->tn_links > 0);
    261 		node->tn_links--;
    262 	}
    263 
    264 	tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name, de->td_namelen);
    265 	TMPFS_POOL_PUT(&tmp->tm_dirent_pool, de);
    266 }
    267 
    268 /* --------------------------------------------------------------------- */
    269 
    270 int
    271 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
    272 {
    273 	int error;
    274 	struct vnode *nvp;
    275 	struct vnode *vp;
    276 
    277 	vp = NULL;
    278 
    279 	if (node->tn_vnode != NULL) {
    280 		vp = node->tn_vnode;
    281 		vget(vp, LK_EXCLUSIVE | LK_RETRY);
    282 		error = 0;
    283 		goto out;
    284 	}
    285 
    286 	/* Get a new vnode and associate it with our node. */
    287 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp);
    288 	if (error != 0)
    289 		goto out;
    290 	KASSERT(vp != NULL);
    291 
    292 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    293 	if (error != 0) {
    294 		vp->v_data = NULL;
    295 		ungetnewvnode(vp);
    296 		vp = NULL;
    297 		goto out;
    298 	}
    299 
    300 	vp->v_data = node;
    301 	vp->v_type = node->tn_type;
    302 
    303 	/* Type-specific initialization. */
    304 	switch (node->tn_type) {
    305 	case VBLK:
    306 		/* FALLTHROUGH */
    307 	case VCHR:
    308 		vp->v_op = tmpfs_specop_p;
    309 		nvp = checkalias(vp, node->tn_rdev, mp);
    310 		if (nvp != NULL) {
    311 			/* Discard unneeded vnode, but save its inode. */
    312 			nvp->v_data = vp->v_data;
    313 			vp->v_data = NULL;
    314 
    315 			/* XXX spec_vnodeops has no locking, so we have to
    316 			 * do it explicitly. */
    317 			VOP_UNLOCK(vp, 0);
    318 			vp->v_op = spec_vnodeop_p;
    319 			vp->v_flag &= ~VLOCKSWORK;
    320 			vrele(vp);
    321 			vgone(vp);
    322 
    323 			/* Reinitialize aliased node. */
    324 			vp = nvp;
    325 			error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    326 			if (error != 0) {
    327 				vp->v_data = NULL;
    328 				vp = NULL;
    329 				goto out;
    330 			}
    331 		}
    332 		break;
    333 
    334 	case VDIR:
    335 		vp->v_flag = node->tn_parent == node ? VROOT : 0;
    336 		break;
    337 
    338 	case VFIFO:
    339 		vp->v_op = tmpfs_fifoop_p;
    340 		break;
    341 
    342 	case VLNK:
    343 		/* FALLTHROUGH */
    344 	case VREG:
    345 		/* FALLTHROUGH */
    346 	case VSOCK:
    347 		break;
    348 
    349 	default:
    350 		KASSERT(0);
    351 	}
    352 
    353 	uvm_vnp_setsize(vp, node->tn_size);
    354 
    355 	error = 0;
    356 
    357 out:
    358 	*vpp = node->tn_vnode = vp;
    359 
    360 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
    361 	KASSERT(*vpp == node->tn_vnode);
    362 
    363 	return error;
    364 }
    365 
    366 /* --------------------------------------------------------------------- */
    367 
    368 void
    369 tmpfs_free_vp(struct vnode *vp)
    370 {
    371 	struct tmpfs_node *node;
    372 
    373 	node = VP_TO_TMPFS_NODE(vp);
    374 
    375 	node->tn_vnode = NULL;
    376 	vp->v_data = NULL;
    377 }
    378 
    379 /* --------------------------------------------------------------------- */
    380 
    381 /* Allocates a new file of type 'type' and adds it to the parent directory
    382  * 'dvp'; this addition is done using the component name given in 'cnp'.
    383  * The ownership of the new file is automatically assigned based on the
    384  * credentials of the caller (through 'cnp'), the group is set based on
    385  * the parent directory and the mode is determined from the 'vap' argument.
    386  * If successful, *vpp holds a vnode to the newly created file and zero
    387  * is returned.  Otherwise *vpp is NULL and the function returns an
    388  * appropriate error code .*/
    389 int
    390 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
    391     struct componentname *cnp, char *target)
    392 {
    393 	int error;
    394 	struct tmpfs_dirent *de;
    395 	struct tmpfs_mount *tmp;
    396 	struct tmpfs_node *dnode;
    397 	struct tmpfs_node *node;
    398 	struct tmpfs_node *parent;
    399 
    400 	KASSERT(VOP_ISLOCKED(dvp));
    401 	KASSERT(cnp->cn_flags & HASBUF);
    402 
    403 	tmp = VFS_TO_TMPFS(dvp->v_mount);
    404 	dnode = VP_TO_TMPFS_DIR(dvp);
    405 	*vpp = NULL;
    406 
    407 	/* If the entry we are creating is a directory, we cannot overflow
    408 	 * the number of links of its parent, because it will get a new
    409 	 * link. */
    410 	if (vap->va_type == VDIR) {
    411 		/* Ensure that we do not overflow the maximum number of links
    412 		 * imposed by the system. */
    413 		KASSERT(dnode->tn_links <= LINK_MAX);
    414 		if (dnode->tn_links == LINK_MAX) {
    415 			error = EMLINK;
    416 			goto out;
    417 		}
    418 
    419 		parent = dnode;
    420 	} else
    421 		parent = NULL;
    422 
    423 	/* Allocate a node that represents the new file. */
    424 	error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
    425 	    dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev,
    426 	    cnp->cn_proc, &node);
    427 	if (error != 0)
    428 		goto out;
    429 
    430 	/* Allocate a directory entry that points to the new file. */
    431 	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
    432 	    &de);
    433 	if (error != 0) {
    434 		tmpfs_free_node(tmp, node);
    435 		goto out;
    436 	}
    437 
    438 	/* Allocate a vnode for the new file. */
    439 	error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
    440 	if (error != 0) {
    441 		tmpfs_free_dirent(tmp, de, TRUE);
    442 		tmpfs_free_node(tmp, node);
    443 		goto out;
    444 	}
    445 
    446 	/* Now that all required items are allocated, we can proceed to
    447 	 * insert the new node into the directory, an operation that
    448 	 * cannot fail. */
    449 	tmpfs_dir_attach(dvp, de);
    450 	VN_KNOTE(dvp, NOTE_WRITE);
    451 
    452 out:
    453 	if (error != 0 || !(cnp->cn_flags & SAVESTART))
    454 		PNBUF_PUT(cnp->cn_pnbuf);
    455 	vput(dvp);
    456 
    457 	KASSERT(!VOP_ISLOCKED(dvp));
    458 	KASSERT(IFF(error == 0, *vpp != NULL));
    459 
    460 	return error;
    461 }
    462 
    463 /* --------------------------------------------------------------------- */
    464 
    465 void
    466 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
    467 {
    468 	struct tmpfs_node *dnode;
    469 
    470 	dnode = VP_TO_TMPFS_DIR(vp);
    471 
    472 	TAILQ_INSERT_TAIL(&dnode->tn_dir, de, td_entries);
    473 	dnode->tn_size += sizeof(struct tmpfs_dirent);
    474 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
    475 	    TMPFS_NODE_MODIFIED;
    476 	uvm_vnp_setsize(vp, dnode->tn_size);
    477 }
    478 
    479 /* --------------------------------------------------------------------- */
    480 
    481 void
    482 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
    483 {
    484 	struct tmpfs_node *dnode;
    485 
    486 	dnode = VP_TO_TMPFS_DIR(vp);
    487 
    488 	TAILQ_REMOVE(&dnode->tn_dir, de, td_entries);
    489 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
    490 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
    491 	    TMPFS_NODE_MODIFIED;
    492 	uvm_vnp_setsize(vp, dnode->tn_size);
    493 }
    494 
    495 /* --------------------------------------------------------------------- */
    496 
    497 struct tmpfs_dirent *
    498 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp)
    499 {
    500 	boolean_t found;
    501 	struct tmpfs_dirent *de;
    502 
    503 	KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
    504 	KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
    505 	    cnp->cn_nameptr[1] == '.')));
    506 	TMPFS_VALIDATE_DIR(node);
    507 
    508 	node->tn_status |= TMPFS_NODE_ACCESSED;
    509 
    510 	found = 0;
    511 	TAILQ_FOREACH(de, &node->tn_dir, td_entries) {
    512 		KASSERT(cnp->cn_namelen < 0xffff);
    513 		if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
    514 		    memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
    515 			found = 1;
    516 			break;
    517 		}
    518 	}
    519 
    520 	return found ? de : NULL;
    521 }
    522 
    523 /* --------------------------------------------------------------------- */
    524 
    525 /* Helper function for tmpfs_readdir.  Creates a '.' entry for the given
    526  * directory and returns it in the uio space.  The function returns 0
    527  * on success, -1 if there was not enough space in the uio structure to
    528  * hold the directory entry or an appropriate error code if another
    529  * error happens. */
    530 int
    531 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
    532 {
    533 	int error;
    534 	struct dirent dent;
    535 
    536 	TMPFS_VALIDATE_DIR(node);
    537 	KASSERT(uio->uio_offset == 0);
    538 
    539 	dent.d_fileno = node->tn_id;
    540 	dent.d_type = DT_DIR;
    541 	dent.d_namlen = 1;
    542 	dent.d_name[0] = '.';
    543 	dent.d_name[1] = '\0';
    544 	dent.d_reclen = _DIRENT_SIZE(&dent);
    545 
    546 	if (dent.d_reclen > uio->uio_resid)
    547 		error = -1;
    548 	else {
    549 		error = uiomove(&dent, dent.d_reclen, uio);
    550 		if (error == 0)
    551 			uio->uio_offset += sizeof(struct tmpfs_dirent) - \
    552 			    dent.d_reclen;
    553 	}
    554 
    555 	node->tn_status |= TMPFS_NODE_ACCESSED;
    556 
    557 	return error;
    558 }
    559 
    560 /* --------------------------------------------------------------------- */
    561 
    562 /* Helper function for tmpfs_readdir.  Creates a '..' entry for the given
    563  * directory and returns it in the uio space.  The function returns 0
    564  * on success, -1 if there was not enough space in the uio structure to
    565  * hold the directory entry or an appropriate error code if another
    566  * error happens. */
    567 int
    568 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
    569 {
    570 	int error;
    571 	struct dirent dent;
    572 
    573 	TMPFS_VALIDATE_DIR(node);
    574 	KASSERT(uio->uio_offset == sizeof(struct tmpfs_dirent));
    575 
    576 	dent.d_fileno = node->tn_id;
    577 	dent.d_type = DT_DIR;
    578 	dent.d_namlen = 2;
    579 	dent.d_name[0] = '.';
    580 	dent.d_name[1] = '.';
    581 	dent.d_name[2] = '\0';
    582 	dent.d_reclen = _DIRENT_SIZE(&dent);
    583 
    584 	if (dent.d_reclen > uio->uio_resid)
    585 		error = -1;
    586 	else {
    587 		error = uiomove(&dent, dent.d_reclen, uio);
    588 		if (error == 0)
    589 			uio->uio_offset += sizeof(struct tmpfs_dirent) - \
    590 			    dent.d_reclen;
    591 	}
    592 
    593 	node->tn_status |= TMPFS_NODE_ACCESSED;
    594 
    595 	return error;
    596 }
    597 
    598 /* --------------------------------------------------------------------- */
    599 
    600 /* Helper function for tmpfs_readdir.  Returns as much directory entries
    601  * as can fit in the uio space.  The read starts at uio->uio_offset.
    602  * The function returns 0 on success, -1 if there was not enough space
    603  * in the uio structure to hold the directory entry or an appropriate
    604  * error code if another error happens. */
    605 int
    606 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio)
    607 {
    608 	int error;
    609 	long cnt, startcnt;
    610 	struct tmpfs_dirent *de;
    611 
    612 	TMPFS_VALIDATE_DIR(node);
    613 	KASSERT(uio->uio_offset % sizeof(struct tmpfs_dirent) == 0);
    614 	KASSERT(uio->uio_offset >= sizeof(struct tmpfs_dirent) * 2);
    615 	KASSERT(uio->uio_offset < node->tn_size +
    616 	    sizeof(struct tmpfs_dirent) * 2);
    617 
    618 	/* Locate the first directory entry we have to return.  We have cached
    619 	 * the last readdir in the node, so use those values if appropriate.
    620 	 * Otherwise do a linear scan to find the requested entry. */
    621 	de = NULL;
    622 	startcnt = uio->uio_offset / sizeof(struct tmpfs_dirent) - 2;
    623 	if (startcnt == node->tn_readdir_lastn && \
    624 	    node->tn_readdir_lastp != NULL) {
    625 		cnt = node->tn_readdir_lastn;
    626 		de = node->tn_readdir_lastp;
    627 	} else {
    628 		cnt = 0;
    629 		de = TAILQ_FIRST(&node->tn_dir);
    630 		while (cnt < startcnt) {
    631 			cnt++;
    632 			de = TAILQ_NEXT(de, td_entries);
    633 
    634 			/* Ensure that if we have not found the desired item,
    635 			 * there are more entries in the directory to continue
    636 			 * the search. */
    637 			KASSERT(IMPLIES(de == TAILQ_LAST(&node->tn_dir,
    638 			    tmpfs_dir), cnt == startcnt));
    639 		}
    640 	}
    641 	KASSERT(cnt == startcnt);
    642 	KASSERT(de != NULL);
    643 
    644 	/* Read as much entries as possible; i.e., until we reach the end of
    645 	 * the directory or we exhaust uio space. */
    646 	do {
    647 		struct dirent d;
    648 
    649 		/* Create a dirent structure representing the current
    650 		 * tmpfs_node and fill it. */
    651 		d.d_fileno = de->td_node->tn_id;
    652 		switch (de->td_node->tn_type) {
    653 		case VBLK:
    654 			d.d_type = DT_BLK;
    655 			break;
    656 
    657 		case VCHR:
    658 			d.d_type = DT_CHR;
    659 			break;
    660 
    661 		case VDIR:
    662 			d.d_type = DT_DIR;
    663 			break;
    664 
    665 		case VFIFO:
    666 			d.d_type = DT_FIFO;
    667 			break;
    668 
    669 		case VLNK:
    670 			d.d_type = DT_LNK;
    671 			break;
    672 
    673 		case VREG:
    674 			d.d_type = DT_REG;
    675 			break;
    676 
    677 		case VSOCK:
    678 			d.d_type = DT_SOCK;
    679 			break;
    680 
    681 		default:
    682 			KASSERT(0);
    683 		}
    684 		d.d_namlen = de->td_namelen;
    685 		KASSERT(de->td_namelen < sizeof(d.d_name));
    686 		(void)memcpy(d.d_name, de->td_name, de->td_namelen);
    687 		d.d_name[de->td_namelen] = '\0';
    688 		d.d_reclen = _DIRENT_SIZE(&d);
    689 
    690 		/* Stop reading if the directory entry we are treating is
    691 		 * bigger than the amount of data that can be returned. */
    692 		if (d.d_reclen > uio->uio_resid) {
    693 			error = -1;
    694 			break;
    695 		}
    696 
    697 		/* Copy the new dirent structure into the output buffer and
    698 		 * advance pointers. */
    699 		error = uiomove(&d, d.d_reclen, uio);
    700 
    701 		cnt++;
    702 		de = TAILQ_NEXT(de, td_entries);
    703 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
    704 
    705 	/* Update the offset in the uio structure to be correctly aligned
    706 	 * with tmpfs_dirent structures.  Otherwise, the offset is the
    707 	 * size of the returned dirent structures, which is useless for us. */
    708 	uio->uio_offset = (cnt + 2) * sizeof(struct tmpfs_dirent);
    709 
    710 	/* Cache the current status. */
    711 	if (de == NULL) {
    712 		KASSERT(cnt == node->tn_size / sizeof(struct tmpfs_dirent));
    713 		node->tn_readdir_lastn = 0;
    714 		node->tn_readdir_lastp = NULL;
    715 	} else {
    716 		node->tn_readdir_lastn = cnt;
    717 		node->tn_readdir_lastp = de;
    718 	}
    719 
    720 	node->tn_status |= TMPFS_NODE_ACCESSED;
    721 
    722 	return error;
    723 }
    724 
    725 /* --------------------------------------------------------------------- */
    726 
    727 int
    728 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    729 {
    730 	int error;
    731 	size_t newpages, oldpages;
    732 	struct tmpfs_mount *tmp;
    733 	struct tmpfs_node *node;
    734 
    735 	KASSERT(vp->v_type == VREG);
    736 	KASSERT(newsize >= 0);
    737 
    738 	node = VP_TO_TMPFS_NODE(vp);
    739 	tmp = VFS_TO_TMPFS(vp->v_mount);
    740 
    741 	/* Convert the old and new sizes to the number of pages needed to
    742 	 * store them.  It may happen that we do not need to do anything
    743 	 * because the last allocated page can accommodate the change on
    744 	 * its own. */
    745 	oldpages = round_page(node->tn_size) / PAGE_SIZE;
    746 	KASSERT(oldpages == node->tn_aobj_pages);
    747 	newpages = round_page(newsize) / PAGE_SIZE;
    748 
    749 	if (newpages > oldpages &&
    750 	    newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) {
    751 		error = ENOSPC;
    752 		goto out;
    753 	}
    754 
    755 	node->tn_aobj_pages = newpages;
    756 
    757 	tmp->tm_pages_used += (newpages - oldpages);
    758 	node->tn_size = newsize;
    759 	uvm_vnp_setsize(vp, newsize);
    760 
    761 	error = 0;
    762 
    763 out:
    764 	return error;
    765 }
    766 
    767 /* --------------------------------------------------------------------- */
    768 
    769 /* Returns information about the number of available memory pages,
    770  * including physical and virtual ones.
    771  *
    772  * If 'total' is TRUE, the value returned is the total amount of memory
    773  * pages configured for the system (either in use or free).
    774  * If it is FALSE, the value returned is the amount of free memory pages.
    775  *
    776  * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
    777  * excessive memory usage.
    778  *
    779  * XXX: This function is used every time TMPFS_PAGES_MAX is called to gather
    780  * the amount of free memory, something that happens during _each_
    781  * object allocation.  The time it takes to run this function so many
    782  * times is not negligible, so this value should be stored as an
    783  * aggregate somewhere, possibly within UVM (we cannot do it ourselves
    784  * because we can't get notifications on memory usage changes). */
    785 size_t
    786 tmpfs_mem_info(boolean_t total)
    787 {
    788 	int i, sec;
    789 	register_t retval;
    790 	size_t size;
    791 	struct swapent *sep;
    792 
    793 	sec = uvmexp.nswapdev;
    794 	sep = (struct swapent *)malloc(sizeof(struct swapent) * sec, M_TEMP,
    795 	    M_WAITOK);
    796 	KASSERT(sep != NULL);
    797 	uvm_swap_stats(SWAP_STATS, sep, sec, &retval);
    798 	KASSERT(retval == sec);
    799 
    800 	size = 0;
    801 	if (total) {
    802 		for (i = 0; i < sec; i++)
    803 			size += dbtob(sep[i].se_nblks) / PAGE_SIZE;
    804 	} else {
    805 		for (i = 0; i < sec; i++)
    806 			size += dbtob(sep[i].se_nblks - sep[i].se_inuse) /
    807 			    PAGE_SIZE;
    808 	}
    809 	size += uvmexp.free;
    810 
    811 	free(sep, M_TEMP);
    812 
    813 	return size;
    814 }
    815 
    816 /* --------------------------------------------------------------------- */
    817 
    818 /* Change flags of the given vnode.
    819  * Caller should execute VOP_UPDATE on vp after a successful execution.
    820  * The vnode must be locked on entry and remain locked on exit. */
    821 int
    822 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct proc *p)
    823 {
    824 	int error;
    825 	struct tmpfs_node *node;
    826 
    827 	KASSERT(VOP_ISLOCKED(vp));
    828 
    829 	node = VP_TO_TMPFS_NODE(vp);
    830 
    831 	/* Disallow this operation if the file system is mounted read-only. */
    832 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    833 		return EROFS;
    834 
    835 	/* XXX: The following comes from UFS code, and can be found in
    836 	 * several other file systems.  Shouldn't this be centralized
    837 	 * somewhere? */
    838 	if (cred->cr_uid != node->tn_uid &&
    839 	    (error = suser(cred, &p->p_acflag)))
    840 		return error;
    841 	if (cred->cr_uid == 0) {
    842 		/* The super-user is only allowed to change flags if the file
    843 		 * wasn't protected before and the securelevel is zero. */
    844 		if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) &&
    845 		    securelevel > 0)
    846 			return EPERM;
    847 		node->tn_flags = flags;
    848 	} else {
    849 		/* Regular users can change flags provided they only want to
    850 		 * change user-specific ones, not those reserved for the
    851 		 * super-user. */
    852 		if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) ||
    853 		    (flags & UF_SETTABLE) != flags)
    854 			return EPERM;
    855 		if ((node->tn_flags & SF_SETTABLE) != (flags & SF_SETTABLE))
    856 			return EPERM;
    857 		node->tn_flags &= SF_SETTABLE;
    858 		node->tn_flags |= (flags & UF_SETTABLE);
    859 	}
    860 
    861 	node->tn_status |= TMPFS_NODE_CHANGED;
    862 	VN_KNOTE(vp, NOTE_ATTRIB);
    863 
    864 	KASSERT(VOP_ISLOCKED(vp));
    865 
    866 	return 0;
    867 }
    868 
    869 /* --------------------------------------------------------------------- */
    870 
    871 /* Change access mode on the given vnode.
    872  * Caller should execute VOP_UPDATE on vp after a successful execution.
    873  * The vnode must be locked on entry and remain locked on exit. */
    874 int
    875 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct proc *p)
    876 {
    877 	int error;
    878 	struct tmpfs_node *node;
    879 
    880 	KASSERT(VOP_ISLOCKED(vp));
    881 
    882 	node = VP_TO_TMPFS_NODE(vp);
    883 
    884 	/* Disallow this operation if the file system is mounted read-only. */
    885 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    886 		return EROFS;
    887 
    888 	/* Immutable or append-only files cannot be modified, either. */
    889 	if (node->tn_flags & (IMMUTABLE | APPEND))
    890 		return EPERM;
    891 
    892 	/* XXX: The following comes from UFS code, and can be found in
    893 	 * several other file systems.  Shouldn't this be centralized
    894 	 * somewhere? */
    895 	if (cred->cr_uid != node->tn_uid &&
    896 	    (error = suser(cred, &p->p_acflag)))
    897 		return error;
    898 	if (cred->cr_uid != 0) {
    899 		if (vp->v_type != VDIR && (mode & S_ISTXT))
    900 			return EFTYPE;
    901 
    902 		if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID))
    903 			return EPERM;
    904 	}
    905 
    906 	node->tn_mode = (mode & ALLPERMS);
    907 
    908 	node->tn_status |= TMPFS_NODE_CHANGED;
    909 	VN_KNOTE(vp, NOTE_ATTRIB);
    910 
    911 	KASSERT(VOP_ISLOCKED(vp));
    912 
    913 	return 0;
    914 }
    915 
    916 /* --------------------------------------------------------------------- */
    917 
    918 /* Change ownership of the given vnode.  At least one of uid or gid must
    919  * be different than VNOVAL.  If one is set to that value, the attribute
    920  * is unchanged.
    921  * Caller should execute VOP_UPDATE on vp after a successful execution.
    922  * The vnode must be locked on entry and remain locked on exit. */
    923 int
    924 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
    925     struct proc *p)
    926 {
    927 	int error;
    928 	struct tmpfs_node *node;
    929 
    930 	KASSERT(VOP_ISLOCKED(vp));
    931 
    932 	node = VP_TO_TMPFS_NODE(vp);
    933 
    934 	/* Assign default values if they are unknown. */
    935 	KASSERT(uid != VNOVAL || gid != VNOVAL);
    936 	if (uid == VNOVAL)
    937 		uid = node->tn_uid;
    938 	if (gid == VNOVAL)
    939 		gid = node->tn_gid;
    940 	KASSERT(uid != VNOVAL && gid != VNOVAL);
    941 
    942 	/* Disallow this operation if the file system is mounted read-only. */
    943 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    944 		return EROFS;
    945 
    946 	/* Immutable or append-only files cannot be modified, either. */
    947 	if (node->tn_flags & (IMMUTABLE | APPEND))
    948 		return EPERM;
    949 
    950 	/* XXX: The following comes from UFS code, and can be found in
    951 	 * several other file systems.  Shouldn't this be centralized
    952 	 * somewhere? */
    953 	if ((cred->cr_uid != node->tn_uid || uid != node->tn_uid ||
    954 	    (gid != node->tn_gid && !(cred->cr_gid == node->tn_gid ||
    955 	     groupmember(gid, cred)))) &&
    956 	    ((error = suser(cred, &p->p_acflag)) != 0))
    957 		return error;
    958 
    959 	node->tn_uid = uid;
    960 	node->tn_gid = gid;
    961 
    962 	node->tn_status |= TMPFS_NODE_CHANGED;
    963 	VN_KNOTE(vp, NOTE_ATTRIB);
    964 
    965 	KASSERT(VOP_ISLOCKED(vp));
    966 
    967 	return 0;
    968 }
    969 
    970 /* --------------------------------------------------------------------- */
    971 
    972 /* Change size of the given vnode.
    973  * Caller should execute VOP_UPDATE on vp after a successful execution.
    974  * The vnode must be locked on entry and remain locked on exit. */
    975 int
    976 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
    977     struct proc *p)
    978 {
    979 	int error;
    980 	struct tmpfs_node *node;
    981 
    982 	KASSERT(VOP_ISLOCKED(vp));
    983 
    984 	node = VP_TO_TMPFS_NODE(vp);
    985 
    986 	/* Decide whether this is a valid operation based on the file type. */
    987 	error = 0;
    988 	switch (vp->v_type) {
    989 	case VDIR:
    990 		return EISDIR;
    991 
    992 	case VLNK:
    993 		/* FALLTHROUGH */
    994 	case VREG:
    995 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    996 			return EROFS;
    997 		break;
    998 
    999 	case VBLK:
   1000 		/* FALLTHROUGH */
   1001 	case VCHR:
   1002 		/* FALLTHROUGH */
   1003 	case VSOCK:
   1004 		/* FALLTHROUGH */
   1005 	case VFIFO:
   1006 		/* Allow modifications of special files even if in the file
   1007 		 * system is mounted read-only (we are not modifying the
   1008 		 * files themselves, but the objects they represent). */
   1009 		break;
   1010 
   1011 	default:
   1012 		/* Anything else is unsupported. */
   1013 		return EINVAL;
   1014 	}
   1015 
   1016 	/* Immutable or append-only files cannot be modified, either. */
   1017 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1018 		return EPERM;
   1019 
   1020 	error = VOP_TRUNCATE(vp, size, 0, cred, p);
   1021 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
   1022 	 * for us, as will update tn_status; no need to do that here. */
   1023 
   1024 	KASSERT(VOP_ISLOCKED(vp));
   1025 
   1026 	return error;
   1027 }
   1028 
   1029 /* --------------------------------------------------------------------- */
   1030 
   1031 /* Change access and modification times of the given vnode.
   1032  * Caller should execute VOP_UPDATE on vp after a successful execution.
   1033  * The vnode must be locked on entry and remain locked on exit. */
   1034 int
   1035 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
   1036     int vaflags, struct ucred *cred, struct proc *p)
   1037 {
   1038 	int error;
   1039 	struct tmpfs_node *node;
   1040 
   1041 	KASSERT(VOP_ISLOCKED(vp));
   1042 
   1043 	node = VP_TO_TMPFS_NODE(vp);
   1044 
   1045 	/* Disallow this operation if the file system is mounted read-only. */
   1046 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1047 		return EROFS;
   1048 
   1049 	/* Immutable or append-only files cannot be modified, either. */
   1050 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1051 		return EPERM;
   1052 
   1053 	/* XXX: The following comes from UFS code, and can be found in
   1054 	 * several other file systems.  Shouldn't this be centralized
   1055 	 * somewhere? */
   1056 	if (cred->cr_uid != node->tn_uid &&
   1057 	    (error = suser(cred, &p->p_acflag)) &&
   1058 	    ((vaflags & VA_UTIMES_NULL) == 0 ||
   1059 	    (error = VOP_ACCESS(vp, VWRITE, cred, p))))
   1060 		return error;
   1061 
   1062 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
   1063 		node->tn_status |= TMPFS_NODE_ACCESSED;
   1064 
   1065 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
   1066 		node->tn_status |= TMPFS_NODE_MODIFIED;
   1067 
   1068 	error = VOP_UPDATE(vp, atime, mtime, 0);
   1069 
   1070 	KASSERT(VOP_ISLOCKED(vp));
   1071 
   1072 	return error;
   1073 }
   1074