Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.62.2.1
      1  1.62.2.1    jruoho /*	$NetBSD: tmpfs_subr.c,v 1.62.2.1 2011/06/06 09:09:24 jruoho Exp $	*/
      2       1.1      jmmv 
      3       1.1      jmmv /*
      4  1.62.2.1    jruoho  * Copyright (c) 2005-2011 The NetBSD Foundation, Inc.
      5       1.1      jmmv  * All rights reserved.
      6       1.1      jmmv  *
      7       1.1      jmmv  * This code is derived from software contributed to The NetBSD Foundation
      8       1.8      jmmv  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9  1.62.2.1    jruoho  * 2005 program, and by Mindaugas Rasiukevicius.
     10       1.1      jmmv  *
     11       1.1      jmmv  * Redistribution and use in source and binary forms, with or without
     12       1.1      jmmv  * modification, are permitted provided that the following conditions
     13       1.1      jmmv  * are met:
     14       1.1      jmmv  * 1. Redistributions of source code must retain the above copyright
     15       1.1      jmmv  *    notice, this list of conditions and the following disclaimer.
     16       1.1      jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1      jmmv  *    notice, this list of conditions and the following disclaimer in the
     18       1.1      jmmv  *    documentation and/or other materials provided with the distribution.
     19       1.1      jmmv  *
     20       1.1      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1      jmmv  */
     32       1.1      jmmv 
     33       1.1      jmmv /*
     34  1.62.2.1    jruoho  * Efficient memory file system: interfaces for inode and directory entry
     35  1.62.2.1    jruoho  * construction, destruction and manipulation.
     36  1.62.2.1    jruoho  *
     37  1.62.2.1    jruoho  * Reference counting
     38  1.62.2.1    jruoho  *
     39  1.62.2.1    jruoho  *	The link count of inode (tmpfs_node_t::tn_links) is used as a
     40  1.62.2.1    jruoho  *	reference counter.  However, it has slightly different semantics.
     41  1.62.2.1    jruoho  *
     42  1.62.2.1    jruoho  *	For directories - link count represents directory entries, which
     43  1.62.2.1    jruoho  *	refer to the directories.  In other words, it represents the count
     44  1.62.2.1    jruoho  *	of sub-directories.  It also takes into account the virtual '.'
     45  1.62.2.1    jruoho  *	entry (which has no real entry in the list).  For files - link count
     46  1.62.2.1    jruoho  *	represents the hard links.  Since only empty directories can be
     47  1.62.2.1    jruoho  *	removed - link count aligns the reference counting requirements
     48  1.62.2.1    jruoho  *	enough.  Note: to check whether directory is not empty, the inode
     49  1.62.2.1    jruoho  *	size (tmpfs_node_t::tn_size) can be used.
     50  1.62.2.1    jruoho  *
     51  1.62.2.1    jruoho  *	The inode itself, as an object, gathers its first reference when
     52  1.62.2.1    jruoho  *	directory entry is attached via tmpfs_dir_attach(9).  For instance,
     53  1.62.2.1    jruoho  *	after regular tmpfs_create(), a file would have a link count of 1,
     54  1.62.2.1    jruoho  *	while directory after tmpfs_mkdir() would have 2 (due to '.').
     55  1.62.2.1    jruoho  *
     56  1.62.2.1    jruoho  * Reclamation
     57  1.62.2.1    jruoho  *
     58  1.62.2.1    jruoho  *	It should be noted that tmpfs inodes rely on a combination of vnode
     59  1.62.2.1    jruoho  *	reference counting and link counting.  That is, an inode can only be
     60  1.62.2.1    jruoho  *	destroyed if its associated vnode is inactive.  The destruction is
     61  1.62.2.1    jruoho  *	done on vnode reclamation i.e. tmpfs_reclaim().  It should be noted
     62  1.62.2.1    jruoho  *	that tmpfs_node_t::tn_links being 0 is a destruction criterion.
     63  1.62.2.1    jruoho  *
     64  1.62.2.1    jruoho  *	If an inode has references within the file system (tn_links > 0) and
     65  1.62.2.1    jruoho  *	its inactive vnode gets reclaimed/recycled - then the association is
     66  1.62.2.1    jruoho  *	broken in tmpfs_reclaim().  In such case, an inode will always pass
     67  1.62.2.1    jruoho  *	tmpfs_lookup() and thus tmpfs_vnode_get() to associate a new vnode.
     68  1.62.2.1    jruoho  *
     69  1.62.2.1    jruoho  * Lock order
     70  1.62.2.1    jruoho  *
     71  1.62.2.1    jruoho  *	tmpfs_node_t::tn_vlock ->
     72  1.62.2.1    jruoho  *		vnode_t::v_vlock ->
     73  1.62.2.1    jruoho  *			vnode_t::v_interlock
     74       1.1      jmmv  */
     75       1.1      jmmv 
     76       1.1      jmmv #include <sys/cdefs.h>
     77  1.62.2.1    jruoho __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.62.2.1 2011/06/06 09:09:24 jruoho Exp $");
     78       1.1      jmmv 
     79       1.1      jmmv #include <sys/param.h>
     80       1.1      jmmv #include <sys/dirent.h>
     81       1.1      jmmv #include <sys/event.h>
     82      1.43        ad #include <sys/kmem.h>
     83       1.1      jmmv #include <sys/mount.h>
     84       1.1      jmmv #include <sys/namei.h>
     85       1.1      jmmv #include <sys/time.h>
     86       1.1      jmmv #include <sys/stat.h>
     87       1.1      jmmv #include <sys/systm.h>
     88       1.1      jmmv #include <sys/vnode.h>
     89      1.20  christos #include <sys/kauth.h>
     90      1.43        ad #include <sys/atomic.h>
     91       1.1      jmmv 
     92       1.1      jmmv #include <uvm/uvm.h>
     93       1.1      jmmv 
     94       1.1      jmmv #include <miscfs/specfs/specdev.h>
     95      1.53      elad #include <miscfs/genfs/genfs.h>
     96       1.1      jmmv #include <fs/tmpfs/tmpfs.h>
     97       1.1      jmmv #include <fs/tmpfs/tmpfs_fifoops.h>
     98       1.1      jmmv #include <fs/tmpfs/tmpfs_specops.h>
     99       1.1      jmmv #include <fs/tmpfs/tmpfs_vnops.h>
    100       1.1      jmmv 
    101       1.8      jmmv /*
    102  1.62.2.1    jruoho  * tmpfs_alloc_node: allocate a new inode of a specified type and
    103  1.62.2.1    jruoho  * insert it into the list of specified mount point.
    104       1.8      jmmv  */
    105       1.1      jmmv int
    106  1.62.2.1    jruoho tmpfs_alloc_node(tmpfs_mount_t *tmp, enum vtype type, uid_t uid, gid_t gid,
    107  1.62.2.1    jruoho     mode_t mode, char *target, dev_t rdev, tmpfs_node_t **node)
    108  1.62.2.1    jruoho {
    109  1.62.2.1    jruoho 	tmpfs_node_t *nnode;
    110       1.1      jmmv 
    111      1.57     rmind 	nnode = tmpfs_node_get(tmp);
    112      1.43        ad 	if (nnode == NULL) {
    113      1.43        ad 		return ENOSPC;
    114       1.1      jmmv 	}
    115      1.43        ad 
    116  1.62.2.1    jruoho 	/* Initially, no references and no associations. */
    117  1.62.2.1    jruoho 	nnode->tn_links = 0;
    118  1.62.2.1    jruoho 	nnode->tn_vnode = NULL;
    119  1.62.2.1    jruoho 	nnode->tn_dirent_hint = NULL;
    120  1.62.2.1    jruoho 
    121      1.43        ad 	/*
    122      1.43        ad 	 * XXX Where the pool is backed by a map larger than (4GB *
    123      1.43        ad 	 * sizeof(*nnode)), this may produce duplicate inode numbers
    124      1.43        ad 	 * for applications that do not understand 64-bit ino_t.
    125      1.43        ad 	 */
    126      1.43        ad 	nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
    127  1.62.2.1    jruoho 	nnode->tn_gen = TMPFS_NODE_GEN_MASK & arc4random();
    128       1.1      jmmv 
    129       1.1      jmmv 	/* Generic initialization. */
    130       1.1      jmmv 	nnode->tn_type = type;
    131       1.1      jmmv 	nnode->tn_size = 0;
    132       1.1      jmmv 	nnode->tn_status = 0;
    133       1.1      jmmv 	nnode->tn_flags = 0;
    134  1.62.2.1    jruoho 	nnode->tn_lockf = NULL;
    135      1.56     rmind 
    136      1.56     rmind 	vfs_timestamp(&nnode->tn_atime);
    137      1.56     rmind 	nnode->tn_birthtime = nnode->tn_atime;
    138      1.56     rmind 	nnode->tn_ctime = nnode->tn_atime;
    139      1.56     rmind 	nnode->tn_mtime = nnode->tn_atime;
    140      1.56     rmind 
    141  1.62.2.1    jruoho 	KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
    142       1.1      jmmv 	nnode->tn_uid = uid;
    143       1.1      jmmv 	nnode->tn_gid = gid;
    144       1.1      jmmv 	nnode->tn_mode = mode;
    145       1.1      jmmv 
    146       1.1      jmmv 	/* Type-specific initialization. */
    147       1.1      jmmv 	switch (nnode->tn_type) {
    148       1.1      jmmv 	case VBLK:
    149       1.1      jmmv 	case VCHR:
    150  1.62.2.1    jruoho 		/* Character/block special device. */
    151  1.62.2.1    jruoho 		KASSERT(rdev != VNOVAL);
    152      1.18      jmmv 		nnode->tn_spec.tn_dev.tn_rdev = rdev;
    153       1.1      jmmv 		break;
    154       1.1      jmmv 	case VDIR:
    155  1.62.2.1    jruoho 		/* Directory. */
    156      1.18      jmmv 		TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
    157  1.62.2.1    jruoho 		nnode->tn_spec.tn_dir.tn_parent = NULL;
    158      1.18      jmmv 		nnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    159      1.18      jmmv 		nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    160  1.62.2.1    jruoho 
    161  1.62.2.1    jruoho 		/* Extra link count for the virtual '.' entry. */
    162       1.1      jmmv 		nnode->tn_links++;
    163       1.1      jmmv 		break;
    164       1.1      jmmv 	case VFIFO:
    165       1.1      jmmv 	case VSOCK:
    166       1.1      jmmv 		break;
    167       1.1      jmmv 	case VLNK:
    168  1.62.2.1    jruoho 		/* Symbolic link.  Target specifies the file name. */
    169  1.62.2.1    jruoho 		KASSERT(target && strlen(target) < MAXPATHLEN);
    170  1.62.2.1    jruoho 
    171       1.7      yamt 		nnode->tn_size = strlen(target);
    172  1.62.2.1    jruoho 		if (nnode->tn_size == 0) {
    173  1.62.2.1    jruoho 			nnode->tn_spec.tn_lnk.tn_link = NULL;
    174  1.62.2.1    jruoho 			break;
    175  1.62.2.1    jruoho 		}
    176      1.18      jmmv 		nnode->tn_spec.tn_lnk.tn_link =
    177      1.57     rmind 		    tmpfs_strname_alloc(tmp, nnode->tn_size);
    178      1.18      jmmv 		if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
    179      1.57     rmind 			tmpfs_node_put(tmp, nnode);
    180       1.1      jmmv 			return ENOSPC;
    181       1.1      jmmv 		}
    182      1.18      jmmv 		memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
    183       1.1      jmmv 		break;
    184       1.1      jmmv 	case VREG:
    185  1.62.2.1    jruoho 		/* Regular file.  Create an underlying UVM object. */
    186      1.18      jmmv 		nnode->tn_spec.tn_reg.tn_aobj =
    187      1.18      jmmv 		    uao_create(INT32_MAX - PAGE_SIZE, 0);
    188      1.18      jmmv 		nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
    189       1.1      jmmv 		break;
    190       1.1      jmmv 	default:
    191  1.62.2.1    jruoho 		KASSERT(false);
    192       1.1      jmmv 	}
    193       1.1      jmmv 
    194      1.43        ad 	mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
    195      1.43        ad 
    196      1.43        ad 	mutex_enter(&tmp->tm_lock);
    197      1.43        ad 	LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
    198      1.43        ad 	mutex_exit(&tmp->tm_lock);
    199      1.43        ad 
    200       1.1      jmmv 	*node = nnode;
    201       1.1      jmmv 	return 0;
    202       1.1      jmmv }
    203       1.1      jmmv 
    204       1.8      jmmv /*
    205  1.62.2.1    jruoho  * tmpfs_free_node: remove the inode from a list in the mount point and
    206  1.62.2.1    jruoho  * destroy the inode structures.
    207       1.8      jmmv  */
    208       1.1      jmmv void
    209  1.62.2.1    jruoho tmpfs_free_node(tmpfs_mount_t *tmp, tmpfs_node_t *node)
    210       1.1      jmmv {
    211      1.57     rmind 	size_t objsz;
    212      1.43        ad 
    213      1.43        ad 	mutex_enter(&tmp->tm_lock);
    214      1.43        ad 	LIST_REMOVE(node, tn_entries);
    215      1.43        ad 	mutex_exit(&tmp->tm_lock);
    216       1.1      jmmv 
    217      1.40        ad 	switch (node->tn_type) {
    218       1.1      jmmv 	case VLNK:
    219  1.62.2.1    jruoho 		if (node->tn_size > 0) {
    220  1.62.2.1    jruoho 			tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
    221  1.62.2.1    jruoho 			    node->tn_size);
    222  1.62.2.1    jruoho 		}
    223       1.1      jmmv 		break;
    224       1.1      jmmv 	case VREG:
    225      1.57     rmind 		/*
    226  1.62.2.1    jruoho 		 * Calculate the size of inode data, decrease the used-memory
    227  1.62.2.1    jruoho 		 * counter, and destroy the unerlying UVM object (if any).
    228      1.57     rmind 		 */
    229      1.57     rmind 		objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
    230      1.57     rmind 		if (objsz != 0) {
    231      1.57     rmind 			tmpfs_mem_decr(tmp, objsz);
    232      1.57     rmind 		}
    233      1.57     rmind 		if (node->tn_spec.tn_reg.tn_aobj != NULL) {
    234      1.18      jmmv 			uao_detach(node->tn_spec.tn_reg.tn_aobj);
    235      1.57     rmind 		}
    236       1.1      jmmv 		break;
    237  1.62.2.1    jruoho 	case VDIR:
    238  1.62.2.1    jruoho 		/*
    239  1.62.2.1    jruoho 		 * KASSERT(TAILQ_EMPTY(&node->tn_spec.tn_dir.tn_dir));
    240  1.62.2.1    jruoho 		 * KASSERT(node->tn_spec.tn_dir.tn_parent == NULL ||
    241  1.62.2.1    jruoho 		 *     node == tmp->tm_root);
    242  1.62.2.1    jruoho 		 */
    243  1.62.2.1    jruoho 		break;
    244       1.1      jmmv 	default:
    245       1.1      jmmv 		break;
    246       1.1      jmmv 	}
    247       1.1      jmmv 
    248      1.43        ad 	mutex_destroy(&node->tn_vlock);
    249      1.57     rmind 	tmpfs_node_put(tmp, node);
    250       1.1      jmmv }
    251       1.1      jmmv 
    252       1.8      jmmv /*
    253  1.62.2.1    jruoho  * tmpfs_vnode_get: allocate or reclaim a vnode for a specified inode.
    254       1.8      jmmv  *
    255  1.62.2.1    jruoho  * => Must be called with tmpfs_node_t::tn_vlock held.
    256  1.62.2.1    jruoho  * => Returns vnode (*vpp) locked.
    257       1.8      jmmv  */
    258       1.1      jmmv int
    259  1.62.2.1    jruoho tmpfs_vnode_get(struct mount *mp, tmpfs_node_t *node, vnode_t **vpp)
    260       1.1      jmmv {
    261  1.62.2.1    jruoho 	vnode_t *vp;
    262       1.1      jmmv 	int error;
    263  1.62.2.1    jruoho again:
    264  1.62.2.1    jruoho 	/* If there is already a vnode, try to reclaim it. */
    265  1.62.2.1    jruoho 	if ((vp = node->tn_vnode) != NULL) {
    266  1.62.2.1    jruoho 		atomic_or_ulong(&node->tn_gen, TMPFS_RECLAIMING_BIT);
    267  1.62.2.1    jruoho 		mutex_enter(&vp->v_interlock);
    268  1.62.2.1    jruoho 		mutex_exit(&node->tn_vlock);
    269  1.62.2.1    jruoho 		error = vget(vp, LK_EXCLUSIVE);
    270  1.62.2.1    jruoho 		if (error == ENOENT) {
    271  1.62.2.1    jruoho 			mutex_enter(&node->tn_vlock);
    272  1.62.2.1    jruoho 			goto again;
    273      1.43        ad 		}
    274  1.62.2.1    jruoho 		atomic_and_ulong(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
    275  1.62.2.1    jruoho 		*vpp = vp;
    276  1.62.2.1    jruoho 		return error;
    277  1.62.2.1    jruoho 	}
    278  1.62.2.1    jruoho 	if (TMPFS_NODE_RECLAIMING(node)) {
    279  1.62.2.1    jruoho 		atomic_and_ulong(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
    280       1.1      jmmv 	}
    281       1.1      jmmv 
    282       1.1      jmmv 	/* Get a new vnode and associate it with our node. */
    283       1.1      jmmv 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp);
    284  1.62.2.1    jruoho 	if (error) {
    285      1.43        ad 		mutex_exit(&node->tn_vlock);
    286      1.43        ad 		return error;
    287       1.1      jmmv 	}
    288       1.1      jmmv 
    289  1.62.2.1    jruoho 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    290       1.1      jmmv 	vp->v_type = node->tn_type;
    291       1.1      jmmv 
    292       1.1      jmmv 	/* Type-specific initialization. */
    293       1.1      jmmv 	switch (node->tn_type) {
    294       1.1      jmmv 	case VBLK:
    295       1.1      jmmv 	case VCHR:
    296       1.1      jmmv 		vp->v_op = tmpfs_specop_p;
    297      1.44        ad 		spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
    298       1.1      jmmv 		break;
    299       1.1      jmmv 	case VDIR:
    300      1.40        ad 		vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
    301      1.40        ad 		    VV_ROOT : 0;
    302       1.1      jmmv 		break;
    303       1.1      jmmv 	case VFIFO:
    304       1.1      jmmv 		vp->v_op = tmpfs_fifoop_p;
    305       1.1      jmmv 		break;
    306       1.1      jmmv 	case VLNK:
    307       1.1      jmmv 	case VREG:
    308       1.1      jmmv 	case VSOCK:
    309       1.1      jmmv 		break;
    310       1.1      jmmv 	default:
    311  1.62.2.1    jruoho 		KASSERT(false);
    312       1.1      jmmv 	}
    313       1.1      jmmv 
    314       1.1      jmmv 	uvm_vnp_setsize(vp, node->tn_size);
    315      1.43        ad 	vp->v_data = node;
    316      1.43        ad 	node->tn_vnode = vp;
    317      1.43        ad 	mutex_exit(&node->tn_vlock);
    318       1.1      jmmv 
    319  1.62.2.1    jruoho 	KASSERT(VOP_ISLOCKED(vp));
    320  1.62.2.1    jruoho 	*vpp = vp;
    321  1.62.2.1    jruoho 	return 0;
    322       1.1      jmmv }
    323       1.1      jmmv 
    324       1.9      jmmv /*
    325  1.62.2.1    jruoho  * tmpfs_alloc_file: allocate a new file of specified type and adds it
    326  1.62.2.1    jruoho  * into the parent directory.
    327  1.62.2.1    jruoho  *
    328  1.62.2.1    jruoho  * => Credentials of the caller are used.
    329       1.9      jmmv  */
    330       1.1      jmmv int
    331  1.62.2.1    jruoho tmpfs_alloc_file(vnode_t *dvp, vnode_t **vpp, struct vattr *vap,
    332       1.1      jmmv     struct componentname *cnp, char *target)
    333       1.1      jmmv {
    334  1.62.2.1    jruoho 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
    335  1.62.2.1    jruoho 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node;
    336  1.62.2.1    jruoho 	tmpfs_dirent_t *de;
    337       1.1      jmmv 	int error;
    338       1.1      jmmv 
    339       1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    340       1.1      jmmv 	*vpp = NULL;
    341       1.1      jmmv 
    342  1.62.2.1    jruoho 	/* Check for the maximum number of links limit. */
    343       1.1      jmmv 	if (vap->va_type == VDIR) {
    344  1.62.2.1    jruoho 		/* Check for maximum links limit. */
    345       1.1      jmmv 		if (dnode->tn_links == LINK_MAX) {
    346       1.1      jmmv 			error = EMLINK;
    347       1.1      jmmv 			goto out;
    348       1.1      jmmv 		}
    349  1.62.2.1    jruoho 		KASSERT(dnode->tn_links < LINK_MAX);
    350  1.62.2.1    jruoho 	}
    351       1.1      jmmv 
    352       1.1      jmmv 	/* Allocate a node that represents the new file. */
    353      1.19      elad 	error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
    354  1.62.2.1    jruoho 	    dnode->tn_gid, vap->va_mode, target, vap->va_rdev, &node);
    355  1.62.2.1    jruoho 	if (error)
    356       1.1      jmmv 		goto out;
    357       1.1      jmmv 
    358       1.1      jmmv 	/* Allocate a directory entry that points to the new file. */
    359  1.62.2.1    jruoho 	error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de);
    360  1.62.2.1    jruoho 	if (error) {
    361       1.1      jmmv 		tmpfs_free_node(tmp, node);
    362       1.1      jmmv 		goto out;
    363       1.1      jmmv 	}
    364       1.1      jmmv 
    365  1.62.2.1    jruoho 	/* Get a vnode for the new file. */
    366  1.62.2.1    jruoho 	mutex_enter(&node->tn_vlock);
    367  1.62.2.1    jruoho 	error = tmpfs_vnode_get(dvp->v_mount, node, vpp);
    368  1.62.2.1    jruoho 	if (error) {
    369  1.62.2.1    jruoho 		tmpfs_free_dirent(tmp, de);
    370       1.1      jmmv 		tmpfs_free_node(tmp, node);
    371       1.1      jmmv 		goto out;
    372       1.1      jmmv 	}
    373       1.1      jmmv 
    374  1.62.2.1    jruoho 	/* Associate inode and attach the entry into the directory. */
    375  1.62.2.1    jruoho 	tmpfs_dir_attach(dvp, de, node);
    376       1.1      jmmv out:
    377       1.1      jmmv 	vput(dvp);
    378  1.62.2.1    jruoho 	return error;
    379  1.62.2.1    jruoho }
    380       1.1      jmmv 
    381  1.62.2.1    jruoho /*
    382  1.62.2.1    jruoho  * tmpfs_alloc_dirent: allocates a new directory entry for the inode.
    383  1.62.2.1    jruoho  * The directory entry contains a path name component.
    384  1.62.2.1    jruoho  */
    385  1.62.2.1    jruoho int
    386  1.62.2.1    jruoho tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len,
    387  1.62.2.1    jruoho     tmpfs_dirent_t **de)
    388  1.62.2.1    jruoho {
    389  1.62.2.1    jruoho 	tmpfs_dirent_t *nde;
    390       1.1      jmmv 
    391  1.62.2.1    jruoho 	nde = tmpfs_dirent_get(tmp);
    392  1.62.2.1    jruoho 	if (nde == NULL)
    393  1.62.2.1    jruoho 		return ENOSPC;
    394  1.62.2.1    jruoho 
    395  1.62.2.1    jruoho 	nde->td_name = tmpfs_strname_alloc(tmp, len);
    396  1.62.2.1    jruoho 	if (nde->td_name == NULL) {
    397  1.62.2.1    jruoho 		tmpfs_dirent_put(tmp, nde);
    398  1.62.2.1    jruoho 		return ENOSPC;
    399  1.62.2.1    jruoho 	}
    400  1.62.2.1    jruoho 	nde->td_namelen = len;
    401  1.62.2.1    jruoho 	memcpy(nde->td_name, name, len);
    402  1.62.2.1    jruoho 
    403  1.62.2.1    jruoho 	*de = nde;
    404  1.62.2.1    jruoho 	return 0;
    405       1.1      jmmv }
    406       1.1      jmmv 
    407  1.62.2.1    jruoho /*
    408  1.62.2.1    jruoho  * tmpfs_free_dirent: free a directory entry.
    409  1.62.2.1    jruoho  */
    410  1.62.2.1    jruoho void
    411  1.62.2.1    jruoho tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de)
    412  1.62.2.1    jruoho {
    413  1.62.2.1    jruoho 
    414  1.62.2.1    jruoho 	/* KASSERT(de->td_node == NULL); */
    415  1.62.2.1    jruoho 	tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
    416  1.62.2.1    jruoho 	tmpfs_dirent_put(tmp, de);
    417  1.62.2.1    jruoho }
    418       1.1      jmmv 
    419       1.8      jmmv /*
    420  1.62.2.1    jruoho  * tmpfs_dir_attach: associate directory entry with a specified inode,
    421  1.62.2.1    jruoho  * and attach the entry into the directory, specified by vnode.
    422      1.29      jmmv  *
    423  1.62.2.1    jruoho  * => Increases link count on the associated node.
    424  1.62.2.1    jruoho  * => Increases link count on directory node, if our node is VDIR.
    425  1.62.2.1    jruoho  *    It is caller's responsibility to check for the LINK_MAX limit.
    426  1.62.2.1    jruoho  * => Triggers kqueue events here.
    427       1.8      jmmv  */
    428       1.1      jmmv void
    429  1.62.2.1    jruoho tmpfs_dir_attach(vnode_t *dvp, tmpfs_dirent_t *de, tmpfs_node_t *node)
    430       1.1      jmmv {
    431  1.62.2.1    jruoho 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    432  1.62.2.1    jruoho 	int events = NOTE_WRITE;
    433       1.1      jmmv 
    434  1.62.2.1    jruoho 	KASSERT(VOP_ISLOCKED(dvp));
    435  1.62.2.1    jruoho 
    436  1.62.2.1    jruoho 	/* Associate directory entry and the inode. */
    437  1.62.2.1    jruoho 	if (node != TMPFS_NODE_WHITEOUT) {
    438  1.62.2.1    jruoho 		de->td_node = node;
    439  1.62.2.1    jruoho 		KASSERT(node->tn_links < LINK_MAX);
    440  1.62.2.1    jruoho 		node->tn_links++;
    441  1.62.2.1    jruoho 
    442  1.62.2.1    jruoho 		/* Save the hint (might overwrite). */
    443  1.62.2.1    jruoho 		node->tn_dirent_hint = de;
    444  1.62.2.1    jruoho 	}
    445       1.1      jmmv 
    446  1.62.2.1    jruoho 	/* Insert the entry to the directory (parent of inode). */
    447      1.18      jmmv 	TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    448  1.62.2.1    jruoho 	dnode->tn_size += sizeof(tmpfs_dirent_t);
    449  1.62.2.1    jruoho 	dnode->tn_status |= TMPFS_NODE_STATUSALL;
    450  1.62.2.1    jruoho 	uvm_vnp_setsize(dvp, dnode->tn_size);
    451  1.62.2.1    jruoho 
    452  1.62.2.1    jruoho 	if (node != TMPFS_NODE_WHITEOUT && node->tn_type == VDIR) {
    453  1.62.2.1    jruoho 		/* Set parent. */
    454  1.62.2.1    jruoho 		KASSERT(node->tn_spec.tn_dir.tn_parent == NULL);
    455  1.62.2.1    jruoho 		node->tn_spec.tn_dir.tn_parent = dnode;
    456      1.29      jmmv 
    457  1.62.2.1    jruoho 		/* Increase the link count of parent. */
    458  1.62.2.1    jruoho 		KASSERT(dnode->tn_links < LINK_MAX);
    459  1.62.2.1    jruoho 		dnode->tn_links++;
    460  1.62.2.1    jruoho 		events |= NOTE_LINK;
    461       1.1      jmmv 
    462  1.62.2.1    jruoho 		TMPFS_VALIDATE_DIR(node);
    463  1.62.2.1    jruoho 	}
    464  1.62.2.1    jruoho 	VN_KNOTE(dvp, events);
    465  1.62.2.1    jruoho }
    466       1.1      jmmv 
    467       1.8      jmmv /*
    468  1.62.2.1    jruoho  * tmpfs_dir_detach: disassociate directory entry and its inode,
    469  1.62.2.1    jruoho  * and detach the entry from the directory, specified by vnode.
    470      1.29      jmmv  *
    471  1.62.2.1    jruoho  * => Decreases link count on the associated node.
    472  1.62.2.1    jruoho  * => Decreases the link count on directory node, if our node is VDIR.
    473  1.62.2.1    jruoho  * => Triggers kqueue events here.
    474       1.8      jmmv  */
    475       1.1      jmmv void
    476  1.62.2.1    jruoho tmpfs_dir_detach(vnode_t *dvp, tmpfs_dirent_t *de)
    477       1.1      jmmv {
    478  1.62.2.1    jruoho 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
    479  1.62.2.1    jruoho 	tmpfs_node_t *node = de->td_node;
    480  1.62.2.1    jruoho 	int events = NOTE_WRITE;
    481       1.1      jmmv 
    482  1.62.2.1    jruoho 	KASSERT(VOP_ISLOCKED(dvp));
    483  1.62.2.1    jruoho 
    484  1.62.2.1    jruoho 	if (node != TMPFS_NODE_WHITEOUT) {
    485  1.62.2.1    jruoho 		vnode_t *vp = node->tn_vnode;
    486  1.62.2.1    jruoho 
    487  1.62.2.1    jruoho 		KASSERT(VOP_ISLOCKED(vp));
    488  1.62.2.1    jruoho 
    489  1.62.2.1    jruoho 		/* Deassociate the inode and entry. */
    490  1.62.2.1    jruoho 		de->td_node = NULL;
    491  1.62.2.1    jruoho 		node->tn_dirent_hint = NULL;
    492  1.62.2.1    jruoho 
    493  1.62.2.1    jruoho 		KASSERT(node->tn_links > 0);
    494  1.62.2.1    jruoho 		node->tn_links--;
    495  1.62.2.1    jruoho 		if (vp) {
    496  1.62.2.1    jruoho 			VN_KNOTE(vp, node->tn_links ?
    497  1.62.2.1    jruoho 			    NOTE_LINK : NOTE_DELETE);
    498  1.62.2.1    jruoho 		}
    499  1.62.2.1    jruoho 
    500  1.62.2.1    jruoho 		/* If directory - decrease the link count of parent. */
    501  1.62.2.1    jruoho 		if (node->tn_type == VDIR) {
    502  1.62.2.1    jruoho 			KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    503  1.62.2.1    jruoho 			node->tn_spec.tn_dir.tn_parent = NULL;
    504  1.62.2.1    jruoho 
    505  1.62.2.1    jruoho 			KASSERT(dnode->tn_links > 0);
    506  1.62.2.1    jruoho 			dnode->tn_links--;
    507  1.62.2.1    jruoho 			events |= NOTE_LINK;
    508  1.62.2.1    jruoho 		}
    509  1.62.2.1    jruoho 	}
    510       1.1      jmmv 
    511  1.62.2.1    jruoho 	/* Remove the entry from the directory. */
    512      1.18      jmmv 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
    513      1.18      jmmv 		dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    514      1.18      jmmv 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    515       1.5      yamt 	}
    516      1.18      jmmv 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    517      1.29      jmmv 
    518  1.62.2.1    jruoho 	dnode->tn_size -= sizeof(tmpfs_dirent_t);
    519  1.62.2.1    jruoho 	dnode->tn_status |= TMPFS_NODE_STATUSALL;
    520  1.62.2.1    jruoho 	uvm_vnp_setsize(dvp, dnode->tn_size);
    521  1.62.2.1    jruoho 	VN_KNOTE(dvp, events);
    522       1.1      jmmv }
    523       1.1      jmmv 
    524       1.8      jmmv /*
    525  1.62.2.1    jruoho  * tmpfs_dir_lookup: find a directory entry in the specified inode.
    526       1.8      jmmv  *
    527  1.62.2.1    jruoho  * Note that the . and .. components are not allowed as they do not
    528  1.62.2.1    jruoho  * physically exist within directories.
    529       1.8      jmmv  */
    530  1.62.2.1    jruoho tmpfs_dirent_t *
    531  1.62.2.1    jruoho tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
    532       1.1      jmmv {
    533  1.62.2.1    jruoho 	const char *name = cnp->cn_nameptr;
    534  1.62.2.1    jruoho 	const uint16_t nlen = cnp->cn_namelen;
    535  1.62.2.1    jruoho 	tmpfs_dirent_t *de;
    536       1.1      jmmv 
    537      1.49      yamt 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    538  1.62.2.1    jruoho 	KASSERT(nlen != 1 || !(name[0] == '.'));
    539  1.62.2.1    jruoho 	KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
    540       1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    541       1.1      jmmv 
    542      1.18      jmmv 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    543  1.62.2.1    jruoho 		if (de->td_namelen != nlen)
    544  1.62.2.1    jruoho 			continue;
    545  1.62.2.1    jruoho 		if (memcmp(de->td_name, name, nlen) != 0)
    546  1.62.2.1    jruoho 			continue;
    547  1.62.2.1    jruoho 		break;
    548       1.1      jmmv 	}
    549  1.62.2.1    jruoho 	node->tn_status |= TMPFS_NODE_ACCESSED;
    550      1.49      yamt 	return de;
    551       1.1      jmmv }
    552       1.1      jmmv 
    553  1.62.2.1    jruoho /*
    554  1.62.2.1    jruoho  * tmpfs_dir_cached: get a cached directory entry if it is valid.  Used to
    555  1.62.2.1    jruoho  * avoid unnecessary tmpds_dir_lookup().
    556  1.62.2.1    jruoho  *
    557  1.62.2.1    jruoho  * => The vnode must be locked.
    558  1.62.2.1    jruoho  */
    559  1.62.2.1    jruoho tmpfs_dirent_t *
    560  1.62.2.1    jruoho tmpfs_dir_cached(tmpfs_node_t *node)
    561  1.62.2.1    jruoho {
    562  1.62.2.1    jruoho 	tmpfs_dirent_t *de = node->tn_dirent_hint;
    563  1.62.2.1    jruoho 
    564  1.62.2.1    jruoho 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    565  1.62.2.1    jruoho 
    566  1.62.2.1    jruoho 	if (de == NULL) {
    567  1.62.2.1    jruoho 		return NULL;
    568  1.62.2.1    jruoho 	}
    569  1.62.2.1    jruoho 	KASSERT(de->td_node == node);
    570  1.62.2.1    jruoho 
    571  1.62.2.1    jruoho 	/*
    572  1.62.2.1    jruoho 	 * Directories always have a valid hint.  For files, check if there
    573  1.62.2.1    jruoho 	 * are any hard links.  If there are - hint might be invalid.
    574  1.62.2.1    jruoho 	 */
    575  1.62.2.1    jruoho 	return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
    576  1.62.2.1    jruoho }
    577       1.1      jmmv 
    578       1.9      jmmv /*
    579  1.62.2.1    jruoho  * tmpfs_dir_getdotdent: helper function for tmpfs_readdir.  Creates a
    580  1.62.2.1    jruoho  * '.' entry for the given directory and returns it in the uio space.
    581       1.9      jmmv  */
    582       1.1      jmmv int
    583  1.62.2.1    jruoho tmpfs_dir_getdotdent(tmpfs_node_t *node, struct uio *uio)
    584       1.1      jmmv {
    585      1.37    rumble 	struct dirent *dentp;
    586  1.62.2.1    jruoho 	int error;
    587       1.1      jmmv 
    588       1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    589       1.5      yamt 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
    590       1.1      jmmv 
    591      1.56     rmind 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    592      1.37    rumble 	dentp->d_fileno = node->tn_id;
    593      1.37    rumble 	dentp->d_type = DT_DIR;
    594      1.37    rumble 	dentp->d_namlen = 1;
    595      1.37    rumble 	dentp->d_name[0] = '.';
    596      1.37    rumble 	dentp->d_name[1] = '\0';
    597      1.37    rumble 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    598       1.1      jmmv 
    599      1.37    rumble 	if (dentp->d_reclen > uio->uio_resid)
    600       1.1      jmmv 		error = -1;
    601       1.1      jmmv 	else {
    602      1.37    rumble 		error = uiomove(dentp, dentp->d_reclen, uio);
    603       1.1      jmmv 		if (error == 0)
    604       1.5      yamt 			uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
    605       1.1      jmmv 	}
    606       1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    607      1.43        ad 	kmem_free(dentp, sizeof(struct dirent));
    608       1.1      jmmv 	return error;
    609       1.1      jmmv }
    610       1.1      jmmv 
    611       1.9      jmmv /*
    612  1.62.2.1    jruoho  * tmpfs_dir_getdotdotdent: helper function for tmpfs_readdir.  Creates a
    613  1.62.2.1    jruoho  * '..' entry for the given directory and returns it in the uio space.
    614       1.9      jmmv  */
    615       1.1      jmmv int
    616  1.62.2.1    jruoho tmpfs_dir_getdotdotdent(tmpfs_node_t *node, struct uio *uio)
    617       1.1      jmmv {
    618      1.37    rumble 	struct dirent *dentp;
    619  1.62.2.1    jruoho 	int error;
    620       1.1      jmmv 
    621       1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    622       1.5      yamt 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
    623       1.1      jmmv 
    624      1.56     rmind 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    625      1.37    rumble 	dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
    626      1.37    rumble 	dentp->d_type = DT_DIR;
    627      1.37    rumble 	dentp->d_namlen = 2;
    628      1.37    rumble 	dentp->d_name[0] = '.';
    629      1.37    rumble 	dentp->d_name[1] = '.';
    630      1.37    rumble 	dentp->d_name[2] = '\0';
    631      1.37    rumble 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    632       1.1      jmmv 
    633      1.37    rumble 	if (dentp->d_reclen > uio->uio_resid)
    634       1.1      jmmv 		error = -1;
    635       1.1      jmmv 	else {
    636      1.37    rumble 		error = uiomove(dentp, dentp->d_reclen, uio);
    637       1.5      yamt 		if (error == 0) {
    638  1.62.2.1    jruoho 			tmpfs_dirent_t *de;
    639       1.5      yamt 
    640      1.18      jmmv 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    641       1.5      yamt 			if (de == NULL)
    642       1.5      yamt 				uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    643       1.5      yamt 			else
    644      1.27      jmmv 				uio->uio_offset = tmpfs_dircookie(de);
    645       1.5      yamt 		}
    646       1.1      jmmv 	}
    647       1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    648      1.43        ad 	kmem_free(dentp, sizeof(struct dirent));
    649       1.1      jmmv 	return error;
    650       1.1      jmmv }
    651       1.1      jmmv 
    652       1.9      jmmv /*
    653  1.62.2.1    jruoho  * tmpfs_dir_lookupbycookie: lookup a directory entry by associated cookie.
    654       1.9      jmmv  */
    655  1.62.2.1    jruoho tmpfs_dirent_t *
    656  1.62.2.1    jruoho tmpfs_dir_lookupbycookie(tmpfs_node_t *node, off_t cookie)
    657       1.5      yamt {
    658  1.62.2.1    jruoho 	tmpfs_dirent_t *de;
    659       1.5      yamt 
    660      1.49      yamt 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    661      1.49      yamt 
    662      1.18      jmmv 	if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
    663      1.18      jmmv 	    node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
    664      1.18      jmmv 		return node->tn_spec.tn_dir.tn_readdir_lastp;
    665       1.5      yamt 	}
    666      1.18      jmmv 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    667      1.27      jmmv 		if (tmpfs_dircookie(de) == cookie) {
    668       1.5      yamt 			break;
    669       1.5      yamt 		}
    670       1.5      yamt 	}
    671       1.5      yamt 	return de;
    672       1.5      yamt }
    673       1.5      yamt 
    674       1.9      jmmv /*
    675  1.62.2.1    jruoho  * tmpfs_dir_getdents: relper function for tmpfs_readdir.
    676  1.62.2.1    jruoho  *
    677  1.62.2.1    jruoho  * => Returns as much directory entries as can fit in the uio space.
    678  1.62.2.1    jruoho  * => The read starts at uio->uio_offset.
    679       1.9      jmmv  */
    680       1.1      jmmv int
    681  1.62.2.1    jruoho tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp)
    682       1.1      jmmv {
    683  1.62.2.1    jruoho 	tmpfs_dirent_t *de;
    684      1.37    rumble 	struct dirent *dentp;
    685  1.62.2.1    jruoho 	off_t startcookie;
    686  1.62.2.1    jruoho 	int error;
    687       1.1      jmmv 
    688      1.49      yamt 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    689       1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    690       1.1      jmmv 
    691  1.62.2.1    jruoho 	/*
    692  1.62.2.1    jruoho 	 * Locate the first directory entry we have to return.  We have cached
    693       1.1      jmmv 	 * the last readdir in the node, so use those values if appropriate.
    694  1.62.2.1    jruoho 	 * Otherwise do a linear scan to find the requested entry.
    695  1.62.2.1    jruoho 	 */
    696       1.5      yamt 	startcookie = uio->uio_offset;
    697       1.5      yamt 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
    698       1.5      yamt 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
    699       1.5      yamt 	if (startcookie == TMPFS_DIRCOOKIE_EOF) {
    700       1.5      yamt 		return 0;
    701       1.1      jmmv 	} else {
    702       1.5      yamt 		de = tmpfs_dir_lookupbycookie(node, startcookie);
    703       1.5      yamt 	}
    704       1.5      yamt 	if (de == NULL) {
    705       1.5      yamt 		return EINVAL;
    706       1.1      jmmv 	}
    707       1.1      jmmv 
    708  1.62.2.1    jruoho 	/*
    709  1.62.2.1    jruoho 	 * Read as much entries as possible; i.e., until we reach the end
    710  1.62.2.1    jruoho 	 * of the directory or we exhaust uio space.
    711  1.62.2.1    jruoho 	 */
    712      1.56     rmind 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    713       1.1      jmmv 	do {
    714  1.62.2.1    jruoho 		/*
    715  1.62.2.1    jruoho 		 * Create a dirent structure representing the current
    716  1.62.2.1    jruoho 		 * inode and fill it.
    717  1.62.2.1    jruoho 		 */
    718      1.62     pooka 		if (de->td_node == TMPFS_NODE_WHITEOUT) {
    719      1.62     pooka 			dentp->d_fileno = 1;
    720      1.62     pooka 			dentp->d_type = DT_WHT;
    721      1.62     pooka 		} else {
    722      1.62     pooka 			dentp->d_fileno = de->td_node->tn_id;
    723      1.62     pooka 			switch (de->td_node->tn_type) {
    724      1.62     pooka 			case VBLK:
    725      1.62     pooka 				dentp->d_type = DT_BLK;
    726  1.62.2.1    jruoho 				break;
    727      1.62     pooka 			case VCHR:
    728      1.62     pooka 				dentp->d_type = DT_CHR;
    729      1.62     pooka 				break;
    730      1.62     pooka 			case VDIR:
    731      1.62     pooka 				dentp->d_type = DT_DIR;
    732      1.62     pooka 				break;
    733      1.62     pooka 			case VFIFO:
    734      1.62     pooka 				dentp->d_type = DT_FIFO;
    735      1.62     pooka 				break;
    736      1.62     pooka 			case VLNK:
    737      1.62     pooka 				dentp->d_type = DT_LNK;
    738      1.62     pooka 				break;
    739      1.62     pooka 			case VREG:
    740      1.62     pooka 				dentp->d_type = DT_REG;
    741      1.62     pooka 				break;
    742      1.62     pooka 			case VSOCK:
    743      1.62     pooka 				dentp->d_type = DT_SOCK;
    744  1.62.2.1    jruoho 				break;
    745      1.62     pooka 			default:
    746  1.62.2.1    jruoho 				KASSERT(false);
    747      1.62     pooka 			}
    748       1.1      jmmv 		}
    749      1.37    rumble 		dentp->d_namlen = de->td_namelen;
    750      1.37    rumble 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
    751  1.62.2.1    jruoho 		memcpy(dentp->d_name, de->td_name, de->td_namelen);
    752      1.37    rumble 		dentp->d_name[de->td_namelen] = '\0';
    753      1.37    rumble 		dentp->d_reclen = _DIRENT_SIZE(dentp);
    754       1.1      jmmv 
    755       1.1      jmmv 		/* Stop reading if the directory entry we are treating is
    756       1.1      jmmv 		 * bigger than the amount of data that can be returned. */
    757      1.37    rumble 		if (dentp->d_reclen > uio->uio_resid) {
    758       1.1      jmmv 			error = -1;
    759       1.1      jmmv 			break;
    760       1.1      jmmv 		}
    761       1.1      jmmv 
    762  1.62.2.1    jruoho 		/*
    763  1.62.2.1    jruoho 		 * Copy the new dirent structure into the output buffer and
    764  1.62.2.1    jruoho 		 * advance pointers.
    765  1.62.2.1    jruoho 		 */
    766      1.37    rumble 		error = uiomove(dentp, dentp->d_reclen, uio);
    767       1.1      jmmv 
    768       1.5      yamt 		(*cntp)++;
    769       1.1      jmmv 		de = TAILQ_NEXT(de, td_entries);
    770       1.1      jmmv 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
    771       1.1      jmmv 
    772       1.5      yamt 	/* Update the offset and cache. */
    773       1.1      jmmv 	if (de == NULL) {
    774       1.5      yamt 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    775      1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastn = 0;
    776      1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    777       1.1      jmmv 	} else {
    778      1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
    779      1.27      jmmv 		    tmpfs_dircookie(de);
    780      1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastp = de;
    781       1.1      jmmv 	}
    782       1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    783      1.43        ad 	kmem_free(dentp, sizeof(struct dirent));
    784       1.1      jmmv 	return error;
    785       1.1      jmmv }
    786       1.1      jmmv 
    787       1.8      jmmv /*
    788  1.62.2.1    jruoho  * tmpfs_reg_resize: resize the underlying UVM object associated with the
    789  1.62.2.1    jruoho  * specified regular file.
    790       1.8      jmmv  */
    791       1.1      jmmv int
    792       1.1      jmmv tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    793       1.1      jmmv {
    794  1.62.2.1    jruoho 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
    795  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    796      1.57     rmind 	size_t newpages, oldpages;
    797      1.13      yamt 	off_t oldsize;
    798       1.1      jmmv 
    799       1.1      jmmv 	KASSERT(vp->v_type == VREG);
    800       1.1      jmmv 	KASSERT(newsize >= 0);
    801       1.1      jmmv 
    802      1.13      yamt 	oldsize = node->tn_size;
    803      1.57     rmind 	oldpages = round_page(oldsize) >> PAGE_SHIFT;
    804      1.57     rmind 	newpages = round_page(newsize) >> PAGE_SHIFT;
    805      1.18      jmmv 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
    806       1.1      jmmv 
    807      1.57     rmind 	if (newpages > oldpages) {
    808      1.57     rmind 		/* Increase the used-memory counter if getting extra pages. */
    809      1.57     rmind 		if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
    810      1.57     rmind 			return ENOSPC;
    811      1.57     rmind 		}
    812      1.57     rmind 	} else if (newsize < oldsize) {
    813      1.13      yamt 		int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
    814      1.13      yamt 
    815      1.57     rmind 		/* Zero out the truncated part of the last page. */
    816      1.13      yamt 		uvm_vnp_zerorange(vp, newsize, zerolen);
    817      1.13      yamt 	}
    818       1.1      jmmv 
    819      1.36     pooka 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
    820      1.36     pooka 	node->tn_size = newsize;
    821      1.36     pooka 	uvm_vnp_setsize(vp, newsize);
    822      1.36     pooka 
    823      1.43        ad 	/*
    824      1.57     rmind 	 * Free "backing store".
    825      1.43        ad 	 */
    826      1.43        ad 	if (newpages < oldpages) {
    827      1.43        ad 		struct uvm_object *uobj;
    828      1.43        ad 
    829      1.43        ad 		uobj = node->tn_spec.tn_reg.tn_aobj;
    830      1.43        ad 
    831      1.43        ad 		mutex_enter(&uobj->vmobjlock);
    832      1.43        ad 		uao_dropswap_range(uobj, newpages, oldpages);
    833      1.43        ad 		mutex_exit(&uobj->vmobjlock);
    834      1.57     rmind 
    835      1.57     rmind 		/* Decrease the used-memory counter. */
    836      1.57     rmind 		tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
    837      1.43        ad 	}
    838  1.62.2.1    jruoho 	if (newsize > oldsize) {
    839      1.29      jmmv 		VN_KNOTE(vp, NOTE_EXTEND);
    840  1.62.2.1    jruoho 	}
    841      1.57     rmind 	return 0;
    842       1.1      jmmv }
    843       1.1      jmmv 
    844       1.9      jmmv /*
    845  1.62.2.1    jruoho  * tmpfs_chflags: change flags of the given vnode.
    846  1.62.2.1    jruoho  *
    847  1.62.2.1    jruoho  * => Caller should perform tmpfs_update().
    848       1.9      jmmv  */
    849       1.1      jmmv int
    850  1.62.2.1    jruoho tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l)
    851       1.1      jmmv {
    852  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    853      1.55     pooka 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    854  1.62.2.1    jruoho 	int error, fs_decision = 0;
    855       1.1      jmmv 
    856       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    857       1.1      jmmv 
    858       1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
    859       1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    860       1.1      jmmv 		return EROFS;
    861       1.1      jmmv 
    862  1.62.2.1    jruoho 	if (kauth_cred_geteuid(cred) != node->tn_uid) {
    863      1.54      elad 		fs_decision = EACCES;
    864  1.62.2.1    jruoho 	}
    865      1.54      elad 
    866      1.54      elad 	/*
    867      1.54      elad 	 * If the new flags have non-user flags that are different than
    868      1.54      elad 	 * those on the node, we need special permission to change them.
    869      1.54      elad 	 */
    870      1.54      elad 	if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
    871      1.54      elad 		action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    872  1.62.2.1    jruoho 		if (!fs_decision) {
    873      1.54      elad 			fs_decision = EPERM;
    874  1.62.2.1    jruoho 		}
    875      1.54      elad 	}
    876      1.54      elad 
    877      1.54      elad 	/*
    878      1.54      elad 	 * Indicate that this node's flags have system attributes in them if
    879      1.54      elad 	 * that's the case.
    880      1.54      elad 	 */
    881      1.54      elad 	if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
    882      1.54      elad 		action |= KAUTH_VNODE_HAS_SYSFLAGS;
    883      1.54      elad 	}
    884      1.54      elad 
    885      1.54      elad 	error = kauth_authorize_vnode(cred, action, vp, NULL, fs_decision);
    886      1.54      elad 	if (error)
    887       1.1      jmmv 		return error;
    888      1.54      elad 
    889      1.54      elad 	/*
    890      1.54      elad 	 * Set the flags. If we're not setting non-user flags, be careful not
    891      1.54      elad 	 * to overwrite them.
    892      1.54      elad 	 *
    893      1.54      elad 	 * XXX: Can't we always assign here? if the system flags are different,
    894      1.54      elad 	 *      the code above should catch attempts to change them without
    895      1.54      elad 	 *      proper permissions, and if we're here it means it's okay to
    896      1.54      elad 	 *      change them...
    897      1.54      elad 	 */
    898  1.62.2.1    jruoho 	if ((action & KAUTH_VNODE_WRITE_SYSFLAGS) == 0) {
    899      1.54      elad 		/* Clear all user-settable flags and re-set them. */
    900       1.1      jmmv 		node->tn_flags &= SF_SETTABLE;
    901       1.1      jmmv 		node->tn_flags |= (flags & UF_SETTABLE);
    902  1.62.2.1    jruoho 	} else {
    903  1.62.2.1    jruoho 		node->tn_flags = flags;
    904       1.1      jmmv 	}
    905       1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    906       1.1      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
    907       1.1      jmmv 	return 0;
    908       1.1      jmmv }
    909       1.1      jmmv 
    910       1.9      jmmv /*
    911  1.62.2.1    jruoho  * tmpfs_chmod: change access mode on the given vnode.
    912  1.62.2.1    jruoho  *
    913  1.62.2.1    jruoho  * => Caller should perform tmpfs_update().
    914       1.9      jmmv  */
    915       1.1      jmmv int
    916  1.62.2.1    jruoho tmpfs_chmod(vnode_t *vp, mode_t mode, kauth_cred_t cred, lwp_t *l)
    917       1.1      jmmv {
    918  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    919      1.51      elad 	int error;
    920       1.1      jmmv 
    921       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    922       1.1      jmmv 
    923       1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
    924       1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    925       1.1      jmmv 		return EROFS;
    926       1.1      jmmv 
    927       1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
    928       1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND))
    929       1.1      jmmv 		return EPERM;
    930       1.1      jmmv 
    931      1.54      elad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    932  1.62.2.1    jruoho 	    NULL, genfs_can_chmod(vp, cred, node->tn_uid, node->tn_gid, mode));
    933  1.62.2.1    jruoho 	if (error) {
    934  1.62.2.1    jruoho 		return error;
    935  1.62.2.1    jruoho 	}
    936       1.1      jmmv 	node->tn_mode = (mode & ALLPERMS);
    937       1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    938       1.1      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
    939       1.1      jmmv 	return 0;
    940       1.1      jmmv }
    941       1.1      jmmv 
    942       1.9      jmmv /*
    943  1.62.2.1    jruoho  * tmpfs_chown: change ownership of the given vnode.
    944  1.62.2.1    jruoho  *
    945  1.62.2.1    jruoho  * => At least one of uid or gid must be different than VNOVAL.
    946  1.62.2.1    jruoho  * => Attribute is unchanged for VNOVAL case.
    947  1.62.2.1    jruoho  * => Caller should perform tmpfs_update().
    948       1.9      jmmv  */
    949       1.1      jmmv int
    950  1.62.2.1    jruoho tmpfs_chown(vnode_t *vp, uid_t uid, gid_t gid, kauth_cred_t cred, lwp_t *l)
    951       1.1      jmmv {
    952  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    953      1.51      elad 	int error;
    954       1.1      jmmv 
    955       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    956       1.1      jmmv 
    957       1.1      jmmv 	/* Assign default values if they are unknown. */
    958       1.1      jmmv 	KASSERT(uid != VNOVAL || gid != VNOVAL);
    959  1.62.2.1    jruoho 	if (uid == VNOVAL) {
    960       1.1      jmmv 		uid = node->tn_uid;
    961  1.62.2.1    jruoho 	}
    962  1.62.2.1    jruoho 	if (gid == VNOVAL) {
    963       1.1      jmmv 		gid = node->tn_gid;
    964  1.62.2.1    jruoho 	}
    965       1.1      jmmv 
    966       1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
    967       1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    968       1.1      jmmv 		return EROFS;
    969       1.1      jmmv 
    970       1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
    971       1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND))
    972       1.1      jmmv 		return EPERM;
    973       1.1      jmmv 
    974      1.54      elad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    975  1.62.2.1    jruoho 	    NULL, genfs_can_chown(vp, cred, node->tn_uid, node->tn_gid, uid,
    976  1.62.2.1    jruoho 	    gid));
    977  1.62.2.1    jruoho 	if (error) {
    978  1.62.2.1    jruoho 		return error;
    979  1.62.2.1    jruoho 	}
    980       1.1      jmmv 	node->tn_uid = uid;
    981       1.1      jmmv 	node->tn_gid = gid;
    982       1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    983       1.1      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
    984       1.1      jmmv 	return 0;
    985       1.1      jmmv }
    986       1.1      jmmv 
    987       1.9      jmmv /*
    988  1.62.2.1    jruoho  * tmpfs_chsize: change size of the given vnode.
    989       1.9      jmmv  */
    990       1.1      jmmv int
    991  1.62.2.1    jruoho tmpfs_chsize(vnode_t *vp, u_quad_t size, kauth_cred_t cred, lwp_t *l)
    992       1.1      jmmv {
    993  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    994       1.1      jmmv 
    995       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    996       1.1      jmmv 
    997       1.1      jmmv 	/* Decide whether this is a valid operation based on the file type. */
    998       1.1      jmmv 	switch (vp->v_type) {
    999       1.1      jmmv 	case VDIR:
   1000       1.1      jmmv 		return EISDIR;
   1001       1.1      jmmv 	case VREG:
   1002  1.62.2.1    jruoho 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
   1003       1.1      jmmv 			return EROFS;
   1004  1.62.2.1    jruoho 		}
   1005       1.1      jmmv 		break;
   1006       1.1      jmmv 	case VBLK:
   1007       1.1      jmmv 	case VCHR:
   1008       1.1      jmmv 	case VFIFO:
   1009  1.62.2.1    jruoho 		/*
   1010  1.62.2.1    jruoho 		 * Allow modifications of special files even if in the file
   1011       1.1      jmmv 		 * system is mounted read-only (we are not modifying the
   1012  1.62.2.1    jruoho 		 * files themselves, but the objects they represent).
   1013  1.62.2.1    jruoho 		 */
   1014      1.14      yamt 		return 0;
   1015       1.1      jmmv 	default:
   1016      1.14      yamt 		return EOPNOTSUPP;
   1017       1.1      jmmv 	}
   1018       1.1      jmmv 
   1019       1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
   1020  1.62.2.1    jruoho 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
   1021       1.1      jmmv 		return EPERM;
   1022  1.62.2.1    jruoho 	}
   1023       1.1      jmmv 
   1024  1.62.2.1    jruoho 	/* Note: tmpfs_truncate() will raise NOTE_EXTEND and NOTE_ATTRIB. */
   1025  1.62.2.1    jruoho 	return tmpfs_truncate(vp, size);
   1026       1.1      jmmv }
   1027       1.1      jmmv 
   1028       1.9      jmmv /*
   1029  1.62.2.1    jruoho  * tmpfs_chtimes: change access and modification times for vnode.
   1030       1.9      jmmv  */
   1031       1.1      jmmv int
   1032  1.62.2.1    jruoho tmpfs_chtimes(vnode_t *vp, const struct timespec *atime,
   1033      1.48  christos     const struct timespec *mtime, const struct timespec *btime,
   1034  1.62.2.1    jruoho     int vaflags, kauth_cred_t cred, lwp_t *l)
   1035       1.1      jmmv {
   1036  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1037       1.1      jmmv 	int error;
   1038       1.1      jmmv 
   1039       1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1040       1.1      jmmv 
   1041       1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
   1042       1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1043       1.1      jmmv 		return EROFS;
   1044       1.1      jmmv 
   1045       1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
   1046       1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1047       1.1      jmmv 		return EPERM;
   1048       1.1      jmmv 
   1049      1.54      elad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
   1050  1.62.2.1    jruoho 	    genfs_can_chtimes(vp, vaflags, node->tn_uid, cred));
   1051      1.53      elad 	if (error)
   1052  1.62.2.1    jruoho 		return error;
   1053       1.1      jmmv 
   1054       1.1      jmmv 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
   1055       1.1      jmmv 		node->tn_status |= TMPFS_NODE_ACCESSED;
   1056       1.1      jmmv 
   1057       1.1      jmmv 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
   1058       1.1      jmmv 		node->tn_status |= TMPFS_NODE_MODIFIED;
   1059       1.1      jmmv 
   1060      1.48  christos 	if (btime->tv_sec == VNOVAL && btime->tv_nsec == VNOVAL)
   1061      1.48  christos 		btime = NULL;
   1062      1.48  christos 
   1063      1.48  christos 	tmpfs_update(vp, atime, mtime, btime, 0);
   1064      1.29      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
   1065      1.12      yamt 	return 0;
   1066       1.1      jmmv }
   1067      1.10      yamt 
   1068  1.62.2.1    jruoho /*
   1069  1.62.2.1    jruoho  * tmpfs_update: update timestamps, et al.
   1070  1.62.2.1    jruoho  */
   1071      1.10      yamt void
   1072  1.62.2.1    jruoho tmpfs_update(vnode_t *vp, const struct timespec *acc,
   1073  1.62.2.1    jruoho     const struct timespec *mod, const struct timespec *birth, int flags)
   1074      1.10      yamt {
   1075  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1076      1.56     rmind 	struct timespec nowtm;
   1077      1.10      yamt 
   1078  1.62.2.1    jruoho 	/* KASSERT(VOP_ISLOCKED(vp)); */
   1079      1.10      yamt 
   1080  1.62.2.1    jruoho 	if (flags & UPDATE_CLOSE) {
   1081  1.62.2.1    jruoho 		/* XXX Need to do anything special? */
   1082  1.62.2.1    jruoho 	}
   1083  1.62.2.1    jruoho 	if ((node->tn_status & TMPFS_NODE_STATUSALL) == 0) {
   1084      1.10      yamt 		return;
   1085  1.62.2.1    jruoho 	}
   1086      1.56     rmind 	if (birth != NULL) {
   1087      1.48  christos 		node->tn_birthtime = *birth;
   1088      1.56     rmind 	}
   1089      1.56     rmind 	vfs_timestamp(&nowtm);
   1090      1.48  christos 
   1091      1.10      yamt 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
   1092      1.56     rmind 		node->tn_atime = acc ? *acc : nowtm;
   1093      1.10      yamt 	}
   1094      1.10      yamt 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
   1095      1.56     rmind 		node->tn_mtime = mod ? *mod : nowtm;
   1096      1.10      yamt 	}
   1097      1.48  christos 	if (node->tn_status & TMPFS_NODE_CHANGED) {
   1098      1.56     rmind 		node->tn_ctime = nowtm;
   1099      1.48  christos 	}
   1100      1.21    kardel 
   1101  1.62.2.1    jruoho 	node->tn_status &= ~TMPFS_NODE_STATUSALL;
   1102      1.10      yamt }
   1103      1.12      yamt 
   1104      1.12      yamt int
   1105  1.62.2.1    jruoho tmpfs_truncate(vnode_t *vp, off_t length)
   1106      1.12      yamt {
   1107  1.62.2.1    jruoho 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1108      1.12      yamt 	int error;
   1109      1.12      yamt 
   1110      1.12      yamt 	if (length < 0) {
   1111      1.12      yamt 		error = EINVAL;
   1112      1.12      yamt 		goto out;
   1113      1.12      yamt 	}
   1114      1.12      yamt 	if (node->tn_size == length) {
   1115      1.12      yamt 		error = 0;
   1116      1.12      yamt 		goto out;
   1117      1.12      yamt 	}
   1118      1.12      yamt 	error = tmpfs_reg_resize(vp, length);
   1119  1.62.2.1    jruoho 	if (error == 0) {
   1120      1.12      yamt 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1121  1.62.2.1    jruoho 	}
   1122      1.12      yamt out:
   1123      1.48  christos 	tmpfs_update(vp, NULL, NULL, NULL, 0);
   1124      1.12      yamt 	return error;
   1125      1.12      yamt }
   1126