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