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