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