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