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