Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_subr.c revision 1.81
      1  1.81     rmind /*	$NetBSD: tmpfs_subr.c,v 1.81 2013/10/31 00:59:17 rmind 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.81     rmind __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.81 2013/10/31 00:59:17 rmind 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.81     rmind 		KASSERT(target != NULL);
    170   1.1      jmmv 
    171   1.7      yamt 		nnode->tn_size = strlen(target);
    172  1.81     rmind 		KASSERT(nnode->tn_size > 0);
    173  1.81     rmind 		KASSERT(nnode->tn_size < MAXPATHLEN);
    174  1.81     rmind 		nnode->tn_size++; /* include the NIL */
    175  1.81     rmind 
    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.80     rmind 		VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE);
    518  1.71     rmind 
    519  1.71     rmind 		/* If directory - decrease the link count of parent. */
    520  1.71     rmind 		if (node->tn_type == VDIR) {
    521  1.71     rmind 			KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
    522  1.71     rmind 			node->tn_spec.tn_dir.tn_parent = NULL;
    523  1.71     rmind 
    524  1.71     rmind 			KASSERT(dnode->tn_links > 0);
    525  1.71     rmind 			dnode->tn_links--;
    526  1.71     rmind 			events |= NOTE_LINK;
    527  1.71     rmind 		}
    528  1.71     rmind 	}
    529   1.1      jmmv 
    530  1.71     rmind 	/* Remove the entry from the directory. */
    531  1.18      jmmv 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
    532  1.18      jmmv 		dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
    533  1.18      jmmv 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    534   1.5      yamt 	}
    535  1.67     rmind 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
    536   1.5      yamt 
    537  1.67     rmind 	dnode->tn_size -= sizeof(tmpfs_dirent_t);
    538  1.67     rmind 	dnode->tn_status |= TMPFS_NODE_STATUSALL;
    539  1.71     rmind 	uvm_vnp_setsize(dvp, dnode->tn_size);
    540  1.71     rmind 	VN_KNOTE(dvp, events);
    541   1.1      jmmv }
    542   1.1      jmmv 
    543   1.8      jmmv /*
    544  1.67     rmind  * tmpfs_dir_lookup: find a directory entry in the specified inode.
    545   1.8      jmmv  *
    546  1.67     rmind  * Note that the . and .. components are not allowed as they do not
    547  1.67     rmind  * physically exist within directories.
    548   1.8      jmmv  */
    549  1.67     rmind tmpfs_dirent_t *
    550  1.67     rmind tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
    551   1.1      jmmv {
    552  1.67     rmind 	const char *name = cnp->cn_nameptr;
    553  1.67     rmind 	const uint16_t nlen = cnp->cn_namelen;
    554  1.67     rmind 	tmpfs_dirent_t *de;
    555   1.1      jmmv 
    556  1.49      yamt 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    557  1.67     rmind 	KASSERT(nlen != 1 || !(name[0] == '.'));
    558  1.67     rmind 	KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
    559  1.71     rmind 	TMPFS_VALIDATE_DIR(node);
    560   1.1      jmmv 
    561  1.18      jmmv 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    562  1.67     rmind 		if (de->td_namelen != nlen)
    563  1.67     rmind 			continue;
    564  1.69     rmind 		if (memcmp(de->td_name, name, nlen) != 0)
    565  1.67     rmind 			continue;
    566  1.67     rmind 		break;
    567   1.1      jmmv 	}
    568  1.67     rmind 	node->tn_status |= TMPFS_NODE_ACCESSED;
    569  1.49      yamt 	return de;
    570   1.1      jmmv }
    571   1.1      jmmv 
    572   1.9      jmmv /*
    573  1.71     rmind  * tmpfs_dir_cached: get a cached directory entry if it is valid.  Used to
    574  1.71     rmind  * avoid unnecessary tmpds_dir_lookup().
    575  1.71     rmind  *
    576  1.71     rmind  * => The vnode must be locked.
    577  1.71     rmind  */
    578  1.71     rmind tmpfs_dirent_t *
    579  1.71     rmind tmpfs_dir_cached(tmpfs_node_t *node)
    580  1.71     rmind {
    581  1.71     rmind 	tmpfs_dirent_t *de = node->tn_dirent_hint;
    582  1.71     rmind 
    583  1.71     rmind 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    584  1.71     rmind 
    585  1.71     rmind 	if (de == NULL) {
    586  1.71     rmind 		return NULL;
    587  1.71     rmind 	}
    588  1.71     rmind 	KASSERT(de->td_node == node);
    589  1.71     rmind 
    590  1.71     rmind 	/*
    591  1.71     rmind 	 * Directories always have a valid hint.  For files, check if there
    592  1.71     rmind 	 * are any hard links.  If there are - hint might be invalid.
    593  1.71     rmind 	 */
    594  1.71     rmind 	return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
    595  1.71     rmind }
    596  1.71     rmind 
    597  1.71     rmind /*
    598  1.67     rmind  * tmpfs_dir_getdotdent: helper function for tmpfs_readdir.  Creates a
    599  1.67     rmind  * '.' entry for the given directory and returns it in the uio space.
    600   1.9      jmmv  */
    601   1.1      jmmv int
    602  1.67     rmind tmpfs_dir_getdotdent(tmpfs_node_t *node, struct uio *uio)
    603   1.1      jmmv {
    604  1.67     rmind 	struct dirent *dentp;
    605   1.1      jmmv 	int error;
    606   1.1      jmmv 
    607   1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    608   1.5      yamt 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
    609   1.1      jmmv 
    610  1.56     rmind 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    611  1.37    rumble 	dentp->d_fileno = node->tn_id;
    612  1.37    rumble 	dentp->d_type = DT_DIR;
    613  1.37    rumble 	dentp->d_namlen = 1;
    614  1.37    rumble 	dentp->d_name[0] = '.';
    615  1.37    rumble 	dentp->d_name[1] = '\0';
    616  1.37    rumble 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    617   1.1      jmmv 
    618  1.37    rumble 	if (dentp->d_reclen > uio->uio_resid)
    619   1.1      jmmv 		error = -1;
    620   1.1      jmmv 	else {
    621  1.37    rumble 		error = uiomove(dentp, dentp->d_reclen, uio);
    622   1.1      jmmv 		if (error == 0)
    623   1.5      yamt 			uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
    624   1.1      jmmv 	}
    625   1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    626  1.43        ad 	kmem_free(dentp, sizeof(struct dirent));
    627   1.1      jmmv 	return error;
    628   1.1      jmmv }
    629   1.1      jmmv 
    630   1.9      jmmv /*
    631  1.67     rmind  * tmpfs_dir_getdotdotdent: helper function for tmpfs_readdir.  Creates a
    632  1.67     rmind  * '..' entry for the given directory and returns it in the uio space.
    633   1.9      jmmv  */
    634   1.1      jmmv int
    635  1.67     rmind tmpfs_dir_getdotdotdent(tmpfs_node_t *node, struct uio *uio)
    636   1.1      jmmv {
    637  1.67     rmind 	struct dirent *dentp;
    638   1.1      jmmv 	int error;
    639   1.1      jmmv 
    640   1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    641   1.5      yamt 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
    642   1.1      jmmv 
    643  1.56     rmind 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    644  1.37    rumble 	dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
    645  1.37    rumble 	dentp->d_type = DT_DIR;
    646  1.37    rumble 	dentp->d_namlen = 2;
    647  1.37    rumble 	dentp->d_name[0] = '.';
    648  1.37    rumble 	dentp->d_name[1] = '.';
    649  1.37    rumble 	dentp->d_name[2] = '\0';
    650  1.37    rumble 	dentp->d_reclen = _DIRENT_SIZE(dentp);
    651   1.1      jmmv 
    652  1.37    rumble 	if (dentp->d_reclen > uio->uio_resid)
    653   1.1      jmmv 		error = -1;
    654   1.1      jmmv 	else {
    655  1.37    rumble 		error = uiomove(dentp, dentp->d_reclen, uio);
    656   1.5      yamt 		if (error == 0) {
    657  1.67     rmind 			tmpfs_dirent_t *de;
    658   1.5      yamt 
    659  1.18      jmmv 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
    660   1.5      yamt 			if (de == NULL)
    661   1.5      yamt 				uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    662   1.5      yamt 			else
    663  1.27      jmmv 				uio->uio_offset = tmpfs_dircookie(de);
    664   1.5      yamt 		}
    665   1.1      jmmv 	}
    666   1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    667  1.43        ad 	kmem_free(dentp, sizeof(struct dirent));
    668   1.1      jmmv 	return error;
    669   1.1      jmmv }
    670   1.1      jmmv 
    671   1.9      jmmv /*
    672  1.67     rmind  * tmpfs_dir_lookupbycookie: lookup a directory entry by associated cookie.
    673   1.9      jmmv  */
    674  1.67     rmind tmpfs_dirent_t *
    675  1.67     rmind tmpfs_dir_lookupbycookie(tmpfs_node_t *node, off_t cookie)
    676   1.5      yamt {
    677  1.67     rmind 	tmpfs_dirent_t *de;
    678   1.5      yamt 
    679  1.49      yamt 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    680  1.49      yamt 
    681  1.18      jmmv 	if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
    682  1.18      jmmv 	    node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
    683  1.18      jmmv 		return node->tn_spec.tn_dir.tn_readdir_lastp;
    684   1.5      yamt 	}
    685  1.18      jmmv 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
    686  1.27      jmmv 		if (tmpfs_dircookie(de) == cookie) {
    687   1.5      yamt 			break;
    688   1.5      yamt 		}
    689   1.5      yamt 	}
    690   1.5      yamt 	return de;
    691   1.5      yamt }
    692   1.5      yamt 
    693   1.9      jmmv /*
    694  1.67     rmind  * tmpfs_dir_getdents: relper function for tmpfs_readdir.
    695  1.67     rmind  *
    696  1.67     rmind  * => Returns as much directory entries as can fit in the uio space.
    697  1.67     rmind  * => The read starts at uio->uio_offset.
    698   1.9      jmmv  */
    699   1.1      jmmv int
    700  1.67     rmind tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp)
    701   1.1      jmmv {
    702  1.67     rmind 	tmpfs_dirent_t *de;
    703  1.67     rmind 	struct dirent *dentp;
    704  1.67     rmind 	off_t startcookie;
    705   1.1      jmmv 	int error;
    706   1.1      jmmv 
    707  1.49      yamt 	KASSERT(VOP_ISLOCKED(node->tn_vnode));
    708   1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    709   1.1      jmmv 
    710  1.67     rmind 	/*
    711  1.67     rmind 	 * Locate the first directory entry we have to return.  We have cached
    712   1.1      jmmv 	 * the last readdir in the node, so use those values if appropriate.
    713  1.67     rmind 	 * Otherwise do a linear scan to find the requested entry.
    714  1.67     rmind 	 */
    715   1.5      yamt 	startcookie = uio->uio_offset;
    716   1.5      yamt 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
    717   1.5      yamt 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
    718   1.5      yamt 	if (startcookie == TMPFS_DIRCOOKIE_EOF) {
    719   1.5      yamt 		return 0;
    720   1.1      jmmv 	} else {
    721   1.5      yamt 		de = tmpfs_dir_lookupbycookie(node, startcookie);
    722   1.5      yamt 	}
    723   1.5      yamt 	if (de == NULL) {
    724   1.5      yamt 		return EINVAL;
    725   1.1      jmmv 	}
    726   1.1      jmmv 
    727  1.67     rmind 	/*
    728  1.67     rmind 	 * Read as much entries as possible; i.e., until we reach the end
    729  1.67     rmind 	 * of the directory or we exhaust uio space.
    730  1.67     rmind 	 */
    731  1.56     rmind 	dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
    732   1.1      jmmv 	do {
    733  1.67     rmind 		/*
    734  1.67     rmind 		 * Create a dirent structure representing the current
    735  1.67     rmind 		 * inode and fill it.
    736  1.67     rmind 		 */
    737  1.62     pooka 		if (de->td_node == TMPFS_NODE_WHITEOUT) {
    738  1.62     pooka 			dentp->d_fileno = 1;
    739  1.62     pooka 			dentp->d_type = DT_WHT;
    740  1.62     pooka 		} else {
    741  1.62     pooka 			dentp->d_fileno = de->td_node->tn_id;
    742  1.62     pooka 			switch (de->td_node->tn_type) {
    743  1.62     pooka 			case VBLK:
    744  1.62     pooka 				dentp->d_type = DT_BLK;
    745  1.67     rmind 				break;
    746  1.62     pooka 			case VCHR:
    747  1.62     pooka 				dentp->d_type = DT_CHR;
    748  1.62     pooka 				break;
    749  1.62     pooka 			case VDIR:
    750  1.62     pooka 				dentp->d_type = DT_DIR;
    751  1.62     pooka 				break;
    752  1.62     pooka 			case VFIFO:
    753  1.62     pooka 				dentp->d_type = DT_FIFO;
    754  1.62     pooka 				break;
    755  1.62     pooka 			case VLNK:
    756  1.62     pooka 				dentp->d_type = DT_LNK;
    757  1.62     pooka 				break;
    758  1.62     pooka 			case VREG:
    759  1.62     pooka 				dentp->d_type = DT_REG;
    760  1.62     pooka 				break;
    761  1.62     pooka 			case VSOCK:
    762  1.62     pooka 				dentp->d_type = DT_SOCK;
    763  1.67     rmind 				break;
    764  1.62     pooka 			default:
    765  1.67     rmind 				KASSERT(false);
    766  1.62     pooka 			}
    767   1.1      jmmv 		}
    768  1.37    rumble 		dentp->d_namlen = de->td_namelen;
    769  1.37    rumble 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
    770  1.67     rmind 		memcpy(dentp->d_name, de->td_name, de->td_namelen);
    771  1.37    rumble 		dentp->d_name[de->td_namelen] = '\0';
    772  1.37    rumble 		dentp->d_reclen = _DIRENT_SIZE(dentp);
    773   1.1      jmmv 
    774   1.1      jmmv 		/* Stop reading if the directory entry we are treating is
    775   1.1      jmmv 		 * bigger than the amount of data that can be returned. */
    776  1.37    rumble 		if (dentp->d_reclen > uio->uio_resid) {
    777   1.1      jmmv 			error = -1;
    778   1.1      jmmv 			break;
    779   1.1      jmmv 		}
    780   1.1      jmmv 
    781  1.67     rmind 		/*
    782  1.67     rmind 		 * Copy the new dirent structure into the output buffer and
    783  1.67     rmind 		 * advance pointers.
    784  1.67     rmind 		 */
    785  1.37    rumble 		error = uiomove(dentp, dentp->d_reclen, uio);
    786   1.1      jmmv 
    787   1.5      yamt 		(*cntp)++;
    788   1.1      jmmv 		de = TAILQ_NEXT(de, td_entries);
    789   1.1      jmmv 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
    790   1.1      jmmv 
    791   1.5      yamt 	/* Update the offset and cache. */
    792   1.1      jmmv 	if (de == NULL) {
    793   1.5      yamt 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
    794  1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastn = 0;
    795  1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
    796   1.1      jmmv 	} else {
    797  1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
    798  1.27      jmmv 		    tmpfs_dircookie(de);
    799  1.18      jmmv 		node->tn_spec.tn_dir.tn_readdir_lastp = de;
    800   1.1      jmmv 	}
    801   1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    802  1.43        ad 	kmem_free(dentp, sizeof(struct dirent));
    803   1.1      jmmv 	return error;
    804   1.1      jmmv }
    805   1.1      jmmv 
    806   1.8      jmmv /*
    807  1.67     rmind  * tmpfs_reg_resize: resize the underlying UVM object associated with the
    808  1.67     rmind  * specified regular file.
    809   1.8      jmmv  */
    810   1.1      jmmv int
    811   1.1      jmmv tmpfs_reg_resize(struct vnode *vp, off_t newsize)
    812   1.1      jmmv {
    813  1.67     rmind 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
    814  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    815  1.74   hannken 	struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
    816  1.57     rmind 	size_t newpages, oldpages;
    817  1.13      yamt 	off_t oldsize;
    818   1.1      jmmv 
    819   1.1      jmmv 	KASSERT(vp->v_type == VREG);
    820   1.1      jmmv 	KASSERT(newsize >= 0);
    821   1.1      jmmv 
    822  1.13      yamt 	oldsize = node->tn_size;
    823  1.57     rmind 	oldpages = round_page(oldsize) >> PAGE_SHIFT;
    824  1.57     rmind 	newpages = round_page(newsize) >> PAGE_SHIFT;
    825  1.18      jmmv 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
    826   1.1      jmmv 
    827  1.57     rmind 	if (newpages > oldpages) {
    828  1.57     rmind 		/* Increase the used-memory counter if getting extra pages. */
    829  1.57     rmind 		if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
    830  1.57     rmind 			return ENOSPC;
    831  1.57     rmind 		}
    832  1.57     rmind 	} else if (newsize < oldsize) {
    833  1.76     enami 		int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
    834  1.13      yamt 
    835  1.74   hannken 		ubc_zerorange(uobj, newsize, zerolen, UBC_UNMAP_FLAG(vp));
    836  1.13      yamt 	}
    837   1.1      jmmv 
    838  1.36     pooka 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
    839  1.36     pooka 	node->tn_size = newsize;
    840  1.36     pooka 	uvm_vnp_setsize(vp, newsize);
    841  1.36     pooka 
    842  1.76     enami 	/*
    843  1.76     enami 	 * Free "backing store".
    844  1.76     enami 	 */
    845  1.43        ad 	if (newpages < oldpages) {
    846  1.76     enami 		KASSERT(uobj->vmobjlock == vp->v_interlock);
    847  1.76     enami 
    848  1.76     enami 		mutex_enter(uobj->vmobjlock);
    849  1.76     enami 		uao_dropswap_range(uobj, newpages, oldpages);
    850  1.76     enami 		mutex_exit(uobj->vmobjlock);
    851  1.76     enami 
    852  1.57     rmind 		/* Decrease the used-memory counter. */
    853  1.57     rmind 		tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
    854  1.43        ad 	}
    855  1.67     rmind 	if (newsize > oldsize) {
    856  1.29      jmmv 		VN_KNOTE(vp, NOTE_EXTEND);
    857  1.67     rmind 	}
    858  1.57     rmind 	return 0;
    859   1.1      jmmv }
    860   1.1      jmmv 
    861   1.9      jmmv /*
    862  1.67     rmind  * tmpfs_chflags: change flags of the given vnode.
    863  1.67     rmind  *
    864  1.67     rmind  * => Caller should perform tmpfs_update().
    865   1.9      jmmv  */
    866   1.1      jmmv int
    867  1.67     rmind tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l)
    868   1.1      jmmv {
    869  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    870  1.55     pooka 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    871  1.79      elad 	int error;
    872  1.79      elad 	bool changing_sysflags = false;
    873   1.1      jmmv 
    874   1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    875   1.1      jmmv 
    876   1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
    877   1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    878   1.1      jmmv 		return EROFS;
    879   1.1      jmmv 
    880  1.54      elad 	/*
    881  1.54      elad 	 * If the new flags have non-user flags that are different than
    882  1.54      elad 	 * those on the node, we need special permission to change them.
    883  1.54      elad 	 */
    884  1.54      elad 	if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
    885  1.54      elad 		action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    886  1.79      elad 		changing_sysflags = true;
    887  1.54      elad 	}
    888  1.54      elad 
    889  1.54      elad 	/*
    890  1.54      elad 	 * Indicate that this node's flags have system attributes in them if
    891  1.54      elad 	 * that's the case.
    892  1.54      elad 	 */
    893  1.54      elad 	if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
    894  1.54      elad 		action |= KAUTH_VNODE_HAS_SYSFLAGS;
    895  1.54      elad 	}
    896  1.54      elad 
    897  1.79      elad 	error = kauth_authorize_vnode(cred, action, vp, NULL,
    898  1.79      elad 	    genfs_can_chflags(cred, vp->v_type, node->tn_uid,
    899  1.79      elad 	    changing_sysflags));
    900  1.54      elad 	if (error)
    901   1.1      jmmv 		return error;
    902  1.54      elad 
    903  1.54      elad 	/*
    904  1.54      elad 	 * Set the flags. If we're not setting non-user flags, be careful not
    905  1.54      elad 	 * to overwrite them.
    906  1.54      elad 	 *
    907  1.54      elad 	 * XXX: Can't we always assign here? if the system flags are different,
    908  1.54      elad 	 *      the code above should catch attempts to change them without
    909  1.54      elad 	 *      proper permissions, and if we're here it means it's okay to
    910  1.54      elad 	 *      change them...
    911  1.54      elad 	 */
    912  1.79      elad 	if (!changing_sysflags) {
    913  1.54      elad 		/* Clear all user-settable flags and re-set them. */
    914   1.1      jmmv 		node->tn_flags &= SF_SETTABLE;
    915   1.1      jmmv 		node->tn_flags |= (flags & UF_SETTABLE);
    916  1.67     rmind 	} else {
    917  1.67     rmind 		node->tn_flags = flags;
    918   1.1      jmmv 	}
    919   1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    920   1.1      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
    921   1.1      jmmv 	return 0;
    922   1.1      jmmv }
    923   1.1      jmmv 
    924   1.9      jmmv /*
    925  1.67     rmind  * tmpfs_chmod: change access mode on the given vnode.
    926  1.67     rmind  *
    927  1.67     rmind  * => Caller should perform tmpfs_update().
    928   1.9      jmmv  */
    929   1.1      jmmv int
    930  1.67     rmind tmpfs_chmod(vnode_t *vp, mode_t mode, kauth_cred_t cred, lwp_t *l)
    931   1.1      jmmv {
    932  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    933  1.51      elad 	int error;
    934   1.1      jmmv 
    935   1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    936   1.1      jmmv 
    937   1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
    938   1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    939   1.1      jmmv 		return EROFS;
    940   1.1      jmmv 
    941   1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
    942   1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND))
    943   1.1      jmmv 		return EPERM;
    944   1.1      jmmv 
    945  1.54      elad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
    946  1.79      elad 	    NULL, genfs_can_chmod(vp->v_type, cred, node->tn_uid, node->tn_gid, mode));
    947  1.67     rmind 	if (error) {
    948  1.67     rmind 		return error;
    949  1.67     rmind 	}
    950   1.1      jmmv 	node->tn_mode = (mode & ALLPERMS);
    951   1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    952   1.1      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
    953   1.1      jmmv 	return 0;
    954   1.1      jmmv }
    955   1.1      jmmv 
    956   1.9      jmmv /*
    957  1.67     rmind  * tmpfs_chown: change ownership of the given vnode.
    958  1.67     rmind  *
    959  1.67     rmind  * => At least one of uid or gid must be different than VNOVAL.
    960  1.67     rmind  * => Attribute is unchanged for VNOVAL case.
    961  1.67     rmind  * => Caller should perform tmpfs_update().
    962   1.9      jmmv  */
    963   1.1      jmmv int
    964  1.67     rmind tmpfs_chown(vnode_t *vp, uid_t uid, gid_t gid, kauth_cred_t cred, lwp_t *l)
    965   1.1      jmmv {
    966  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
    967  1.51      elad 	int error;
    968   1.1      jmmv 
    969   1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    970   1.1      jmmv 
    971   1.1      jmmv 	/* Assign default values if they are unknown. */
    972   1.1      jmmv 	KASSERT(uid != VNOVAL || gid != VNOVAL);
    973  1.67     rmind 	if (uid == VNOVAL) {
    974   1.1      jmmv 		uid = node->tn_uid;
    975  1.67     rmind 	}
    976  1.67     rmind 	if (gid == VNOVAL) {
    977   1.1      jmmv 		gid = node->tn_gid;
    978  1.67     rmind 	}
    979   1.1      jmmv 
    980   1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
    981   1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    982   1.1      jmmv 		return EROFS;
    983   1.1      jmmv 
    984   1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
    985   1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND))
    986   1.1      jmmv 		return EPERM;
    987   1.1      jmmv 
    988  1.54      elad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
    989  1.79      elad 	    NULL, genfs_can_chown(cred, node->tn_uid, node->tn_gid, uid,
    990  1.67     rmind 	    gid));
    991  1.67     rmind 	if (error) {
    992  1.67     rmind 		return error;
    993  1.67     rmind 	}
    994   1.1      jmmv 	node->tn_uid = uid;
    995   1.1      jmmv 	node->tn_gid = gid;
    996   1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    997   1.1      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
    998   1.1      jmmv 	return 0;
    999   1.1      jmmv }
   1000   1.1      jmmv 
   1001   1.9      jmmv /*
   1002  1.67     rmind  * tmpfs_chsize: change size of the given vnode.
   1003   1.9      jmmv  */
   1004   1.1      jmmv int
   1005  1.67     rmind tmpfs_chsize(vnode_t *vp, u_quad_t size, kauth_cred_t cred, lwp_t *l)
   1006   1.1      jmmv {
   1007  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1008   1.1      jmmv 
   1009   1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1010   1.1      jmmv 
   1011   1.1      jmmv 	/* Decide whether this is a valid operation based on the file type. */
   1012   1.1      jmmv 	switch (vp->v_type) {
   1013   1.1      jmmv 	case VDIR:
   1014   1.1      jmmv 		return EISDIR;
   1015   1.1      jmmv 	case VREG:
   1016  1.67     rmind 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
   1017   1.1      jmmv 			return EROFS;
   1018  1.67     rmind 		}
   1019   1.1      jmmv 		break;
   1020   1.1      jmmv 	case VBLK:
   1021   1.1      jmmv 	case VCHR:
   1022   1.1      jmmv 	case VFIFO:
   1023  1.67     rmind 		/*
   1024  1.67     rmind 		 * Allow modifications of special files even if in the file
   1025   1.1      jmmv 		 * system is mounted read-only (we are not modifying the
   1026  1.67     rmind 		 * files themselves, but the objects they represent).
   1027  1.67     rmind 		 */
   1028  1.14      yamt 		return 0;
   1029   1.1      jmmv 	default:
   1030  1.14      yamt 		return EOPNOTSUPP;
   1031   1.1      jmmv 	}
   1032   1.1      jmmv 
   1033   1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
   1034  1.67     rmind 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
   1035   1.1      jmmv 		return EPERM;
   1036  1.67     rmind 	}
   1037   1.1      jmmv 
   1038  1.67     rmind 	/* Note: tmpfs_truncate() will raise NOTE_EXTEND and NOTE_ATTRIB. */
   1039  1.67     rmind 	return tmpfs_truncate(vp, size);
   1040   1.1      jmmv }
   1041   1.1      jmmv 
   1042   1.9      jmmv /*
   1043  1.67     rmind  * tmpfs_chtimes: change access and modification times for vnode.
   1044   1.9      jmmv  */
   1045   1.1      jmmv int
   1046  1.67     rmind tmpfs_chtimes(vnode_t *vp, const struct timespec *atime,
   1047  1.48  christos     const struct timespec *mtime, const struct timespec *btime,
   1048  1.67     rmind     int vaflags, kauth_cred_t cred, lwp_t *l)
   1049   1.1      jmmv {
   1050  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1051   1.1      jmmv 	int error;
   1052   1.1      jmmv 
   1053   1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1054   1.1      jmmv 
   1055   1.1      jmmv 	/* Disallow this operation if the file system is mounted read-only. */
   1056   1.1      jmmv 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1057   1.1      jmmv 		return EROFS;
   1058   1.1      jmmv 
   1059   1.1      jmmv 	/* Immutable or append-only files cannot be modified, either. */
   1060   1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND))
   1061   1.1      jmmv 		return EPERM;
   1062   1.1      jmmv 
   1063  1.54      elad 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
   1064  1.67     rmind 	    genfs_can_chtimes(vp, vaflags, node->tn_uid, cred));
   1065  1.53      elad 	if (error)
   1066  1.67     rmind 		return error;
   1067   1.1      jmmv 
   1068   1.1      jmmv 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
   1069   1.1      jmmv 		node->tn_status |= TMPFS_NODE_ACCESSED;
   1070   1.1      jmmv 
   1071   1.1      jmmv 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
   1072   1.1      jmmv 		node->tn_status |= TMPFS_NODE_MODIFIED;
   1073   1.1      jmmv 
   1074  1.48  christos 	if (btime->tv_sec == VNOVAL && btime->tv_nsec == VNOVAL)
   1075  1.48  christos 		btime = NULL;
   1076  1.48  christos 
   1077  1.48  christos 	tmpfs_update(vp, atime, mtime, btime, 0);
   1078  1.29      jmmv 	VN_KNOTE(vp, NOTE_ATTRIB);
   1079  1.12      yamt 	return 0;
   1080   1.1      jmmv }
   1081  1.10      yamt 
   1082  1.67     rmind /*
   1083  1.67     rmind  * tmpfs_update: update timestamps, et al.
   1084  1.67     rmind  */
   1085  1.10      yamt void
   1086  1.67     rmind tmpfs_update(vnode_t *vp, const struct timespec *acc,
   1087  1.67     rmind     const struct timespec *mod, const struct timespec *birth, int flags)
   1088  1.10      yamt {
   1089  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1090  1.56     rmind 	struct timespec nowtm;
   1091  1.10      yamt 
   1092  1.70     rmind 	/* KASSERT(VOP_ISLOCKED(vp)); */
   1093  1.10      yamt 
   1094  1.67     rmind 	if (flags & UPDATE_CLOSE) {
   1095  1.67     rmind 		/* XXX Need to do anything special? */
   1096  1.67     rmind 	}
   1097  1.67     rmind 	if ((node->tn_status & TMPFS_NODE_STATUSALL) == 0) {
   1098  1.10      yamt 		return;
   1099  1.67     rmind 	}
   1100  1.56     rmind 	if (birth != NULL) {
   1101  1.48  christos 		node->tn_birthtime = *birth;
   1102  1.56     rmind 	}
   1103  1.56     rmind 	vfs_timestamp(&nowtm);
   1104  1.48  christos 
   1105  1.10      yamt 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
   1106  1.56     rmind 		node->tn_atime = acc ? *acc : nowtm;
   1107  1.10      yamt 	}
   1108  1.10      yamt 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
   1109  1.56     rmind 		node->tn_mtime = mod ? *mod : nowtm;
   1110  1.10      yamt 	}
   1111  1.48  christos 	if (node->tn_status & TMPFS_NODE_CHANGED) {
   1112  1.56     rmind 		node->tn_ctime = nowtm;
   1113  1.48  christos 	}
   1114  1.21    kardel 
   1115  1.67     rmind 	node->tn_status &= ~TMPFS_NODE_STATUSALL;
   1116  1.10      yamt }
   1117  1.12      yamt 
   1118  1.12      yamt int
   1119  1.67     rmind tmpfs_truncate(vnode_t *vp, off_t length)
   1120  1.12      yamt {
   1121  1.67     rmind 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
   1122  1.12      yamt 	int error;
   1123  1.12      yamt 
   1124  1.12      yamt 	if (length < 0) {
   1125  1.12      yamt 		error = EINVAL;
   1126  1.12      yamt 		goto out;
   1127  1.12      yamt 	}
   1128  1.12      yamt 	if (node->tn_size == length) {
   1129  1.12      yamt 		error = 0;
   1130  1.12      yamt 		goto out;
   1131  1.12      yamt 	}
   1132  1.12      yamt 	error = tmpfs_reg_resize(vp, length);
   1133  1.67     rmind 	if (error == 0) {
   1134  1.12      yamt 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1135  1.67     rmind 	}
   1136  1.12      yamt out:
   1137  1.48  christos 	tmpfs_update(vp, NULL, NULL, NULL, 0);
   1138  1.12      yamt 	return error;
   1139  1.12      yamt }
   1140