Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_vnops.c revision 1.49.10.6
      1  1.49.10.6      yamt /*	$NetBSD: tmpfs_vnops.c,v 1.49.10.6 2010/08/11 22:54:36 yamt Exp $	*/
      2        1.1      jmmv 
      3        1.1      jmmv /*
      4       1.45        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.12      jmmv  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9       1.12      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.1      jmmv  * tmpfs vnode interface.
     35        1.1      jmmv  */
     36        1.1      jmmv 
     37        1.1      jmmv #include <sys/cdefs.h>
     38  1.49.10.6      yamt __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.49.10.6 2010/08/11 22:54:36 yamt 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/fcntl.h>
     43        1.1      jmmv #include <sys/event.h>
     44        1.1      jmmv #include <sys/malloc.h>
     45        1.1      jmmv #include <sys/namei.h>
     46        1.1      jmmv #include <sys/proc.h>
     47        1.1      jmmv #include <sys/stat.h>
     48        1.1      jmmv #include <sys/uio.h>
     49        1.1      jmmv #include <sys/unistd.h>
     50        1.1      jmmv #include <sys/vnode.h>
     51       1.15      jmmv #include <sys/lockf.h>
     52       1.24  christos #include <sys/kauth.h>
     53        1.1      jmmv 
     54        1.1      jmmv #include <uvm/uvm.h>
     55        1.1      jmmv 
     56        1.1      jmmv #include <miscfs/fifofs/fifo.h>
     57  1.49.10.3      yamt #include <miscfs/genfs/genfs.h>
     58        1.1      jmmv #include <fs/tmpfs/tmpfs_vnops.h>
     59        1.1      jmmv #include <fs/tmpfs/tmpfs.h>
     60        1.1      jmmv 
     61        1.1      jmmv /* --------------------------------------------------------------------- */
     62        1.1      jmmv 
     63        1.1      jmmv /*
     64        1.2      jmmv  * vnode operations vector used for files stored in a tmpfs file system.
     65        1.1      jmmv  */
     66        1.1      jmmv int (**tmpfs_vnodeop_p)(void *);
     67        1.1      jmmv const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
     68        1.1      jmmv 	{ &vop_default_desc,		vn_default_error },
     69        1.1      jmmv 	{ &vop_lookup_desc,		tmpfs_lookup },
     70        1.1      jmmv 	{ &vop_create_desc,		tmpfs_create },
     71        1.1      jmmv 	{ &vop_mknod_desc,		tmpfs_mknod },
     72        1.1      jmmv 	{ &vop_open_desc,		tmpfs_open },
     73        1.1      jmmv 	{ &vop_close_desc,		tmpfs_close },
     74        1.1      jmmv 	{ &vop_access_desc,		tmpfs_access },
     75        1.1      jmmv 	{ &vop_getattr_desc,		tmpfs_getattr },
     76        1.1      jmmv 	{ &vop_setattr_desc,		tmpfs_setattr },
     77        1.1      jmmv 	{ &vop_read_desc,		tmpfs_read },
     78        1.1      jmmv 	{ &vop_write_desc,		tmpfs_write },
     79        1.1      jmmv 	{ &vop_ioctl_desc,		tmpfs_ioctl },
     80        1.1      jmmv 	{ &vop_fcntl_desc,		tmpfs_fcntl },
     81        1.1      jmmv 	{ &vop_poll_desc,		tmpfs_poll },
     82        1.1      jmmv 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
     83        1.1      jmmv 	{ &vop_revoke_desc,		tmpfs_revoke },
     84        1.1      jmmv 	{ &vop_mmap_desc,		tmpfs_mmap },
     85        1.1      jmmv 	{ &vop_fsync_desc,		tmpfs_fsync },
     86        1.1      jmmv 	{ &vop_seek_desc,		tmpfs_seek },
     87        1.1      jmmv 	{ &vop_remove_desc,		tmpfs_remove },
     88        1.1      jmmv 	{ &vop_link_desc,		tmpfs_link },
     89        1.1      jmmv 	{ &vop_rename_desc,		tmpfs_rename },
     90        1.1      jmmv 	{ &vop_mkdir_desc,		tmpfs_mkdir },
     91        1.1      jmmv 	{ &vop_rmdir_desc,		tmpfs_rmdir },
     92        1.1      jmmv 	{ &vop_symlink_desc,		tmpfs_symlink },
     93        1.1      jmmv 	{ &vop_readdir_desc,		tmpfs_readdir },
     94        1.1      jmmv 	{ &vop_readlink_desc,		tmpfs_readlink },
     95        1.1      jmmv 	{ &vop_abortop_desc,		tmpfs_abortop },
     96        1.1      jmmv 	{ &vop_inactive_desc,		tmpfs_inactive },
     97        1.1      jmmv 	{ &vop_reclaim_desc,		tmpfs_reclaim },
     98        1.1      jmmv 	{ &vop_lock_desc,		tmpfs_lock },
     99        1.1      jmmv 	{ &vop_unlock_desc,		tmpfs_unlock },
    100        1.1      jmmv 	{ &vop_bmap_desc,		tmpfs_bmap },
    101        1.1      jmmv 	{ &vop_strategy_desc,		tmpfs_strategy },
    102        1.1      jmmv 	{ &vop_print_desc,		tmpfs_print },
    103        1.1      jmmv 	{ &vop_pathconf_desc,		tmpfs_pathconf },
    104        1.1      jmmv 	{ &vop_islocked_desc,		tmpfs_islocked },
    105        1.1      jmmv 	{ &vop_advlock_desc,		tmpfs_advlock },
    106        1.1      jmmv 	{ &vop_bwrite_desc,		tmpfs_bwrite },
    107        1.1      jmmv 	{ &vop_getpages_desc,		tmpfs_getpages },
    108        1.1      jmmv 	{ &vop_putpages_desc,		tmpfs_putpages },
    109        1.1      jmmv 	{ NULL, NULL }
    110        1.1      jmmv };
    111        1.1      jmmv const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
    112        1.1      jmmv 	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
    113        1.1      jmmv 
    114  1.49.10.6      yamt /*
    115  1.49.10.6      yamt  * tmpfs_lookup: lookup routine.
    116  1.49.10.6      yamt  *
    117  1.49.10.6      yamt  * Arguments: dvp (directory being searched), vpp (result),
    118  1.49.10.6      yamt  * cnp (component name - path).
    119  1.49.10.6      yamt  *
    120  1.49.10.6      yamt  * => Caller holds a reference and lock on dvp.
    121  1.49.10.6      yamt  * => We return looked-up vnode (vpp) locked, with a reference held.
    122  1.49.10.6      yamt  */
    123        1.1      jmmv int
    124        1.1      jmmv tmpfs_lookup(void *v)
    125        1.1      jmmv {
    126  1.49.10.6      yamt 	struct vop_lookup_args /* {
    127  1.49.10.6      yamt 		struct vnode *a_dvp;
    128  1.49.10.6      yamt 		struct vnode **a_vpp;
    129  1.49.10.6      yamt 		struct componentname *a_cnp;
    130  1.49.10.6      yamt 	} */ *ap = v;
    131  1.49.10.6      yamt 	struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
    132  1.49.10.6      yamt 	struct componentname *cnp = ap->a_cnp;
    133        1.1      jmmv 	struct tmpfs_dirent *de;
    134        1.1      jmmv 	struct tmpfs_node *dnode;
    135  1.49.10.6      yamt 	int error;
    136        1.1      jmmv 
    137        1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    138        1.1      jmmv 
    139        1.1      jmmv 	dnode = VP_TO_TMPFS_DIR(dvp);
    140        1.1      jmmv 	*vpp = NULL;
    141        1.1      jmmv 
    142        1.1      jmmv 	/* Check accessibility of requested node as a first step. */
    143       1.44     pooka 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    144        1.1      jmmv 	if (error != 0)
    145        1.1      jmmv 		goto out;
    146        1.1      jmmv 
    147  1.49.10.6      yamt 	/*
    148  1.49.10.6      yamt 	 * If requesting the last path component on a read-only file system
    149  1.49.10.6      yamt 	 * with a write operation, deny it.
    150  1.49.10.6      yamt 	 */
    151        1.1      jmmv 	if ((cnp->cn_flags & ISLASTCN) &&
    152        1.1      jmmv 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    153        1.1      jmmv 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    154        1.1      jmmv 		error = EROFS;
    155        1.1      jmmv 		goto out;
    156        1.1      jmmv 	}
    157        1.1      jmmv 
    158  1.49.10.6      yamt 	/*
    159  1.49.10.6      yamt 	 * Avoid doing a linear scan of the directory if the requested
    160  1.49.10.6      yamt 	 * directory/name couple is already in the cache.
    161  1.49.10.6      yamt 	 */
    162        1.1      jmmv 	error = cache_lookup(dvp, vpp, cnp);
    163        1.1      jmmv 	if (error >= 0)
    164        1.1      jmmv 		goto out;
    165        1.1      jmmv 
    166        1.1      jmmv 	/* We cannot be requesting the parent directory of the root node. */
    167        1.1      jmmv 	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
    168       1.21      jmmv 	    dnode->tn_spec.tn_dir.tn_parent == dnode,
    169       1.21      jmmv 	    !(cnp->cn_flags & ISDOTDOT)));
    170        1.1      jmmv 
    171        1.1      jmmv 	if (cnp->cn_flags & ISDOTDOT) {
    172  1.49.10.6      yamt 		VOP_UNLOCK(dvp);
    173        1.1      jmmv 
    174        1.1      jmmv 		/* Allocate a new vnode on the matching entry. */
    175       1.21      jmmv 		error = tmpfs_alloc_vp(dvp->v_mount,
    176       1.21      jmmv 		    dnode->tn_spec.tn_dir.tn_parent, vpp);
    177        1.1      jmmv 
    178       1.33       chs 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    179  1.49.10.6      yamt 		goto done;
    180  1.49.10.6      yamt 
    181        1.1      jmmv 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    182  1.49.10.6      yamt 		if ((cnp->cn_flags & ISLASTCN) &&
    183  1.49.10.6      yamt 		    (cnp->cn_nameiop == RENAME)) {
    184  1.49.10.6      yamt 			error = EISDIR;
    185  1.49.10.6      yamt 			goto out;
    186  1.49.10.6      yamt 		}
    187  1.49.10.5      yamt 		vref(dvp);
    188        1.1      jmmv 		*vpp = dvp;
    189        1.1      jmmv 		error = 0;
    190  1.49.10.6      yamt 		goto done;
    191  1.49.10.6      yamt 	}
    192        1.1      jmmv 
    193  1.49.10.6      yamt 	de = tmpfs_dir_lookup(dnode, cnp);
    194  1.49.10.6      yamt 	if (de == NULL) {
    195  1.49.10.6      yamt 		/*
    196  1.49.10.6      yamt 		 * The entry was not found in the directory.  This is valid
    197  1.49.10.6      yamt 		 * if we are creating or renaming an entry and are working
    198  1.49.10.6      yamt 		 * on the last component of the path name.
    199  1.49.10.6      yamt 		 */
    200  1.49.10.6      yamt 		if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == CREATE ||
    201  1.49.10.6      yamt 		    cnp->cn_nameiop == RENAME)) {
    202  1.49.10.6      yamt 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    203  1.49.10.6      yamt 			if (error) {
    204        1.1      jmmv 				goto out;
    205        1.1      jmmv 			}
    206  1.49.10.6      yamt 			/* Keep the component name for future uses. */
    207  1.49.10.6      yamt 			cnp->cn_flags |= SAVENAME;
    208  1.49.10.6      yamt 			error = EJUSTRETURN;
    209  1.49.10.6      yamt 		} else {
    210  1.49.10.6      yamt 			error = ENOENT;
    211  1.49.10.6      yamt 		}
    212  1.49.10.6      yamt 	} else {
    213  1.49.10.6      yamt 		struct tmpfs_node *tnode = de->td_node;
    214        1.1      jmmv 
    215  1.49.10.6      yamt 		/*
    216  1.49.10.6      yamt 		 * If we are not at the last path component and found a
    217  1.49.10.6      yamt 		 * non-directory or non-link entry (which may itself be
    218  1.49.10.6      yamt 		 * pointing to a directory), raise an error.
    219  1.49.10.6      yamt 		 */
    220  1.49.10.6      yamt 		if ((tnode->tn_type != VDIR && tnode->tn_type != VLNK) &&
    221  1.49.10.6      yamt 		    (cnp->cn_flags & ISLASTCN) == 0) {
    222  1.49.10.6      yamt 			error = ENOTDIR;
    223  1.49.10.6      yamt 			goto out;
    224  1.49.10.6      yamt 		}
    225  1.49.10.4      yamt 
    226  1.49.10.6      yamt 		/* Check permissions. */
    227  1.49.10.6      yamt 		if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == DELETE ||
    228  1.49.10.6      yamt 		    cnp->cn_nameiop == RENAME)) {
    229  1.49.10.6      yamt 			kauth_action_t action = 0;
    230  1.49.10.6      yamt 
    231  1.49.10.6      yamt 			/* This is the file-system's decision. */
    232  1.49.10.6      yamt 			if ((dnode->tn_mode & S_ISTXT) != 0 &&
    233  1.49.10.6      yamt 			    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
    234  1.49.10.6      yamt 			    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid)
    235  1.49.10.6      yamt 				error = EPERM;
    236  1.49.10.6      yamt 			else
    237  1.49.10.6      yamt 				error = 0;
    238  1.49.10.4      yamt 
    239  1.49.10.6      yamt 			/* Only bother if we are not already failing it. */
    240  1.49.10.6      yamt 			if (!error) {
    241  1.49.10.6      yamt 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    242  1.49.10.6      yamt 			}
    243        1.1      jmmv 
    244  1.49.10.6      yamt 			if (cnp->cn_nameiop == DELETE) {
    245  1.49.10.6      yamt 				action |= KAUTH_VNODE_DELETE;
    246  1.49.10.6      yamt 			} else {
    247  1.49.10.6      yamt 				KASSERT(cnp->cn_nameiop == RENAME);
    248  1.49.10.6      yamt 				action |= KAUTH_VNODE_RENAME;
    249  1.49.10.6      yamt 			}
    250  1.49.10.6      yamt 			error = kauth_authorize_vnode(cnp->cn_cred,
    251  1.49.10.6      yamt 			    action, *vpp, dvp, error);
    252  1.49.10.6      yamt 			if (error) {
    253  1.49.10.6      yamt 				goto out;
    254  1.49.10.6      yamt 			}
    255  1.49.10.6      yamt 			cnp->cn_flags |= SAVENAME;
    256        1.1      jmmv 		}
    257  1.49.10.6      yamt 		/* Allocate a new vnode on the matching entry. */
    258  1.49.10.6      yamt 		error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
    259        1.1      jmmv 	}
    260  1.49.10.6      yamt done:
    261  1.49.10.6      yamt 	/*
    262  1.49.10.6      yamt 	 * Store the result of this lookup in the cache.  Avoid this if the
    263        1.1      jmmv 	 * request was for creation, as it does not improve timings on
    264  1.49.10.6      yamt 	 * emprical tests.
    265  1.49.10.6      yamt 	 */
    266       1.45        ad 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
    267       1.45        ad 	    (cnp->cn_flags & ISDOTDOT) == 0)
    268        1.1      jmmv 		cache_enter(dvp, *vpp, cnp);
    269        1.1      jmmv 
    270        1.1      jmmv out:
    271        1.1      jmmv 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
    272       1.33       chs 	KASSERT(VOP_ISLOCKED(dvp));
    273        1.1      jmmv 	return error;
    274        1.1      jmmv }
    275        1.1      jmmv 
    276        1.1      jmmv int
    277        1.1      jmmv tmpfs_create(void *v)
    278        1.1      jmmv {
    279        1.1      jmmv 	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
    280        1.1      jmmv 	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
    281        1.1      jmmv 	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
    282        1.1      jmmv 	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
    283        1.1      jmmv 
    284        1.1      jmmv 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
    285        1.1      jmmv 
    286        1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    287        1.1      jmmv }
    288        1.1      jmmv /* --------------------------------------------------------------------- */
    289        1.1      jmmv 
    290        1.1      jmmv int
    291        1.1      jmmv tmpfs_mknod(void *v)
    292        1.1      jmmv {
    293        1.1      jmmv 	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
    294        1.1      jmmv 	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
    295        1.1      jmmv 	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
    296        1.1      jmmv 	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
    297        1.1      jmmv 
    298        1.1      jmmv 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
    299  1.49.10.2      yamt 	    vap->va_type != VFIFO) {
    300  1.49.10.2      yamt 		vput(dvp);
    301        1.1      jmmv 		return EINVAL;
    302  1.49.10.2      yamt 	}
    303        1.1      jmmv 
    304        1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
    305        1.1      jmmv }
    306        1.1      jmmv 
    307        1.1      jmmv /* --------------------------------------------------------------------- */
    308        1.1      jmmv 
    309        1.1      jmmv int
    310        1.1      jmmv tmpfs_open(void *v)
    311        1.1      jmmv {
    312        1.1      jmmv 	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
    313        1.1      jmmv 	int mode = ((struct vop_open_args *)v)->a_mode;
    314        1.1      jmmv 
    315        1.1      jmmv 	int error;
    316        1.1      jmmv 	struct tmpfs_node *node;
    317        1.1      jmmv 
    318        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    319        1.1      jmmv 
    320        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    321        1.1      jmmv 
    322       1.32      jmmv 	/* The file is still active but all its names have been removed
    323       1.32      jmmv 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
    324       1.32      jmmv 	 * it is about to die. */
    325       1.32      jmmv 	if (node->tn_links < 1) {
    326       1.32      jmmv 		error = ENOENT;
    327       1.32      jmmv 		goto out;
    328       1.32      jmmv 	}
    329       1.32      jmmv 
    330        1.1      jmmv 	/* If the file is marked append-only, deny write requests. */
    331        1.1      jmmv 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
    332        1.1      jmmv 		error = EPERM;
    333        1.1      jmmv 	else
    334        1.1      jmmv 		error = 0;
    335        1.1      jmmv 
    336       1.32      jmmv out:
    337        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    338        1.1      jmmv 
    339        1.1      jmmv 	return error;
    340        1.1      jmmv }
    341        1.1      jmmv 
    342        1.1      jmmv /* --------------------------------------------------------------------- */
    343        1.1      jmmv 
    344        1.1      jmmv int
    345        1.1      jmmv tmpfs_close(void *v)
    346        1.1      jmmv {
    347        1.1      jmmv 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
    348        1.1      jmmv 
    349        1.1      jmmv 	struct tmpfs_node *node;
    350        1.1      jmmv 
    351        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    352        1.1      jmmv 
    353        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    354        1.1      jmmv 
    355        1.1      jmmv 	if (node->tn_links > 0) {
    356        1.1      jmmv 		/* Update node times.  No need to do it if the node has
    357        1.1      jmmv 		 * been deleted, because it will vanish after we return. */
    358  1.49.10.2      yamt 		tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    359       1.17      yamt 	}
    360        1.1      jmmv 
    361       1.17      yamt 	return 0;
    362        1.1      jmmv }
    363        1.1      jmmv 
    364        1.1      jmmv /* --------------------------------------------------------------------- */
    365        1.1      jmmv 
    366  1.49.10.3      yamt static int
    367  1.49.10.3      yamt tmpfs_check_possible(struct vnode *vp, struct tmpfs_node *node, mode_t mode)
    368        1.1      jmmv {
    369  1.49.10.3      yamt 	int error = 0;
    370        1.1      jmmv 
    371        1.1      jmmv 	switch (vp->v_type) {
    372        1.1      jmmv 	case VDIR:
    373        1.1      jmmv 		/* FALLTHROUGH */
    374        1.1      jmmv 	case VLNK:
    375        1.1      jmmv 		/* FALLTHROUGH */
    376        1.1      jmmv 	case VREG:
    377        1.1      jmmv 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
    378        1.1      jmmv 			error = EROFS;
    379        1.1      jmmv 			goto out;
    380        1.1      jmmv 		}
    381        1.1      jmmv 		break;
    382        1.1      jmmv 
    383        1.1      jmmv 	case VBLK:
    384        1.1      jmmv 		/* FALLTHROUGH */
    385        1.1      jmmv 	case VCHR:
    386        1.1      jmmv 		/* FALLTHROUGH */
    387        1.1      jmmv 	case VSOCK:
    388        1.1      jmmv 		/* FALLTHROUGH */
    389        1.1      jmmv 	case VFIFO:
    390        1.1      jmmv 		break;
    391        1.1      jmmv 
    392        1.1      jmmv 	default:
    393        1.1      jmmv 		error = EINVAL;
    394        1.1      jmmv 		goto out;
    395        1.1      jmmv 	}
    396        1.1      jmmv 
    397        1.1      jmmv 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
    398        1.1      jmmv 		error = EPERM;
    399        1.1      jmmv 		goto out;
    400        1.1      jmmv 	}
    401        1.1      jmmv 
    402  1.49.10.3      yamt  out:
    403  1.49.10.3      yamt 	return error;
    404  1.49.10.3      yamt }
    405  1.49.10.3      yamt 
    406  1.49.10.3      yamt static int
    407  1.49.10.3      yamt tmpfs_check_permitted(struct vnode *vp, struct tmpfs_node *node, mode_t mode,
    408  1.49.10.3      yamt     kauth_cred_t cred)
    409  1.49.10.3      yamt {
    410  1.49.10.3      yamt 
    411  1.49.10.3      yamt 	return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
    412        1.1      jmmv 	    node->tn_gid, mode, cred);
    413  1.49.10.3      yamt }
    414  1.49.10.3      yamt 
    415  1.49.10.3      yamt int
    416  1.49.10.3      yamt tmpfs_access(void *v)
    417  1.49.10.3      yamt {
    418  1.49.10.3      yamt 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
    419  1.49.10.3      yamt 	int mode = ((struct vop_access_args *)v)->a_mode;
    420  1.49.10.3      yamt 	kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
    421  1.49.10.3      yamt 
    422  1.49.10.3      yamt 	int error;
    423  1.49.10.3      yamt 	struct tmpfs_node *node;
    424  1.49.10.3      yamt 
    425  1.49.10.3      yamt 	KASSERT(VOP_ISLOCKED(vp));
    426  1.49.10.3      yamt 
    427  1.49.10.3      yamt 	node = VP_TO_TMPFS_NODE(vp);
    428  1.49.10.3      yamt 
    429  1.49.10.3      yamt 	error = tmpfs_check_possible(vp, node, mode);
    430  1.49.10.3      yamt 	if (error)
    431  1.49.10.3      yamt 		goto out;
    432  1.49.10.3      yamt 
    433  1.49.10.3      yamt 	error = tmpfs_check_permitted(vp, node, mode, cred);
    434        1.1      jmmv 
    435  1.49.10.4      yamt 	error = kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
    436  1.49.10.4      yamt 	    NULL, error);
    437  1.49.10.4      yamt 
    438        1.1      jmmv out:
    439        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    440        1.1      jmmv 
    441        1.1      jmmv 	return error;
    442        1.1      jmmv }
    443        1.1      jmmv 
    444        1.1      jmmv /* --------------------------------------------------------------------- */
    445        1.1      jmmv 
    446        1.1      jmmv int
    447        1.1      jmmv tmpfs_getattr(void *v)
    448        1.1      jmmv {
    449        1.1      jmmv 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
    450        1.1      jmmv 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
    451        1.1      jmmv 
    452        1.1      jmmv 	struct tmpfs_node *node;
    453        1.1      jmmv 
    454        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    455        1.1      jmmv 
    456  1.49.10.5      yamt 	vattr_null(vap);
    457        1.1      jmmv 
    458  1.49.10.2      yamt 	tmpfs_itimes(vp, NULL, NULL, NULL);
    459       1.14      yamt 
    460        1.1      jmmv 	vap->va_type = vp->v_type;
    461        1.1      jmmv 	vap->va_mode = node->tn_mode;
    462        1.1      jmmv 	vap->va_nlink = node->tn_links;
    463        1.1      jmmv 	vap->va_uid = node->tn_uid;
    464        1.1      jmmv 	vap->va_gid = node->tn_gid;
    465        1.1      jmmv 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    466        1.1      jmmv 	vap->va_fileid = node->tn_id;
    467        1.1      jmmv 	vap->va_size = node->tn_size;
    468        1.1      jmmv 	vap->va_blocksize = PAGE_SIZE;
    469        1.1      jmmv 	vap->va_atime = node->tn_atime;
    470        1.1      jmmv 	vap->va_mtime = node->tn_mtime;
    471        1.1      jmmv 	vap->va_ctime = node->tn_ctime;
    472        1.1      jmmv 	vap->va_birthtime = node->tn_birthtime;
    473        1.1      jmmv 	vap->va_gen = node->tn_gen;
    474        1.1      jmmv 	vap->va_flags = node->tn_flags;
    475        1.1      jmmv 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
    476       1.21      jmmv 		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
    477        1.1      jmmv 	vap->va_bytes = round_page(node->tn_size);
    478        1.1      jmmv 	vap->va_filerev = VNOVAL;
    479        1.1      jmmv 	vap->va_vaflags = 0;
    480        1.1      jmmv 	vap->va_spare = VNOVAL; /* XXX */
    481        1.1      jmmv 
    482        1.1      jmmv 	return 0;
    483        1.1      jmmv }
    484        1.1      jmmv 
    485        1.1      jmmv /* --------------------------------------------------------------------- */
    486        1.1      jmmv 
    487  1.49.10.2      yamt #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
    488        1.1      jmmv /* XXX Should this operation be atomic?  I think it should, but code in
    489        1.1      jmmv  * XXX other places (e.g., ufs) doesn't seem to be... */
    490        1.1      jmmv int
    491        1.1      jmmv tmpfs_setattr(void *v)
    492        1.1      jmmv {
    493        1.1      jmmv 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
    494        1.1      jmmv 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
    495       1.23      elad 	kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
    496       1.44     pooka 	struct lwp *l = curlwp;
    497        1.1      jmmv 
    498       1.17      yamt 	int error;
    499        1.1      jmmv 
    500        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    501        1.1      jmmv 
    502        1.1      jmmv 	error = 0;
    503        1.1      jmmv 
    504        1.1      jmmv 	/* Abort if any unsettable attribute is given. */
    505        1.1      jmmv 	if (vap->va_type != VNON ||
    506        1.1      jmmv 	    vap->va_nlink != VNOVAL ||
    507        1.1      jmmv 	    vap->va_fsid != VNOVAL ||
    508        1.1      jmmv 	    vap->va_fileid != VNOVAL ||
    509        1.1      jmmv 	    vap->va_blocksize != VNOVAL ||
    510  1.49.10.2      yamt 	    GOODTIME(&vap->va_ctime) ||
    511        1.1      jmmv 	    vap->va_gen != VNOVAL ||
    512        1.1      jmmv 	    vap->va_rdev != VNOVAL ||
    513        1.1      jmmv 	    vap->va_bytes != VNOVAL)
    514        1.1      jmmv 		error = EINVAL;
    515        1.1      jmmv 
    516        1.1      jmmv 	if (error == 0 && (vap->va_flags != VNOVAL))
    517       1.25        ad 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
    518        1.1      jmmv 
    519        1.1      jmmv 	if (error == 0 && (vap->va_size != VNOVAL))
    520       1.25        ad 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
    521        1.1      jmmv 
    522        1.1      jmmv 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
    523       1.25        ad 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
    524        1.1      jmmv 
    525        1.1      jmmv 	if (error == 0 && (vap->va_mode != VNOVAL))
    526       1.25        ad 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
    527        1.1      jmmv 
    528  1.49.10.2      yamt 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
    529  1.49.10.2      yamt 	    || GOODTIME(&vap->va_birthtime)))
    530  1.49.10.2      yamt 		if ((error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
    531  1.49.10.2      yamt 		    &vap->va_birthtime, vap->va_vaflags, cred, l)) == 0)
    532  1.49.10.2      yamt 			return 0;
    533        1.1      jmmv 
    534        1.1      jmmv 	/* Update the node times.  We give preference to the error codes
    535        1.1      jmmv 	 * generated by this function rather than the ones that may arise
    536        1.1      jmmv 	 * from tmpfs_update. */
    537  1.49.10.2      yamt 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    538        1.1      jmmv 
    539        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    540        1.1      jmmv 
    541        1.1      jmmv 	return error;
    542        1.1      jmmv }
    543        1.1      jmmv 
    544        1.1      jmmv /* --------------------------------------------------------------------- */
    545        1.1      jmmv 
    546        1.1      jmmv int
    547        1.1      jmmv tmpfs_read(void *v)
    548        1.1      jmmv {
    549        1.1      jmmv 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
    550        1.1      jmmv 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
    551  1.49.10.2      yamt 	int ioflag = ((struct vop_read_args *)v)->a_ioflag;
    552        1.1      jmmv 
    553        1.7      jmmv 	int error;
    554        1.6      yamt 	struct tmpfs_node *node;
    555        1.6      yamt 	struct uvm_object *uobj;
    556        1.1      jmmv 
    557        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    558        1.1      jmmv 
    559        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    560        1.1      jmmv 
    561        1.5      yamt 	if (vp->v_type != VREG) {
    562        1.5      yamt 		error = EISDIR;
    563        1.5      yamt 		goto out;
    564        1.5      yamt 	}
    565        1.5      yamt 
    566        1.5      yamt 	if (uio->uio_offset < 0) {
    567        1.1      jmmv 		error = EINVAL;
    568        1.1      jmmv 		goto out;
    569        1.1      jmmv 	}
    570        1.1      jmmv 
    571        1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
    572        1.1      jmmv 
    573       1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
    574        1.6      yamt 	error = 0;
    575        1.7      jmmv 	while (error == 0 && uio->uio_resid > 0) {
    576        1.6      yamt 		vsize_t len;
    577        1.6      yamt 
    578        1.8      yamt 		if (node->tn_size <= uio->uio_offset)
    579        1.8      yamt 			break;
    580        1.8      yamt 
    581        1.6      yamt 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    582        1.7      jmmv 		if (len == 0)
    583        1.6      yamt 			break;
    584        1.7      jmmv 
    585  1.49.10.2      yamt 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    586  1.49.10.2      yamt 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    587        1.1      jmmv 	}
    588        1.1      jmmv 
    589        1.1      jmmv out:
    590        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    591        1.1      jmmv 
    592        1.1      jmmv 	return error;
    593        1.1      jmmv }
    594        1.1      jmmv 
    595        1.1      jmmv /* --------------------------------------------------------------------- */
    596        1.1      jmmv 
    597        1.1      jmmv int
    598        1.1      jmmv tmpfs_write(void *v)
    599        1.1      jmmv {
    600        1.1      jmmv 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
    601        1.1      jmmv 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
    602        1.1      jmmv 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
    603        1.1      jmmv 
    604       1.36   thorpej 	bool extended;
    605        1.1      jmmv 	int error;
    606        1.1      jmmv 	off_t oldsize;
    607        1.1      jmmv 	struct tmpfs_node *node;
    608        1.6      yamt 	struct uvm_object *uobj;
    609        1.1      jmmv 
    610        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    611        1.1      jmmv 
    612        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    613        1.1      jmmv 	oldsize = node->tn_size;
    614        1.1      jmmv 
    615        1.1      jmmv 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
    616        1.1      jmmv 		error = EINVAL;
    617        1.1      jmmv 		goto out;
    618        1.1      jmmv 	}
    619        1.1      jmmv 
    620        1.1      jmmv 	if (uio->uio_resid == 0) {
    621        1.1      jmmv 		error = 0;
    622        1.1      jmmv 		goto out;
    623        1.1      jmmv 	}
    624        1.1      jmmv 
    625        1.1      jmmv 	if (ioflag & IO_APPEND)
    626        1.1      jmmv 		uio->uio_offset = node->tn_size;
    627        1.1      jmmv 
    628        1.1      jmmv 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
    629        1.1      jmmv 	if (extended) {
    630        1.1      jmmv 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
    631        1.1      jmmv 		if (error != 0)
    632        1.1      jmmv 			goto out;
    633        1.1      jmmv 	}
    634        1.1      jmmv 
    635       1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
    636        1.6      yamt 	error = 0;
    637        1.7      jmmv 	while (error == 0 && uio->uio_resid > 0) {
    638        1.6      yamt 		vsize_t len;
    639        1.6      yamt 
    640        1.6      yamt 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
    641        1.7      jmmv 		if (len == 0)
    642        1.6      yamt 			break;
    643        1.7      jmmv 
    644  1.49.10.2      yamt 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
    645  1.49.10.2      yamt 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    646        1.1      jmmv 	}
    647        1.6      yamt 
    648        1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
    649        1.1      jmmv 	    (extended ? TMPFS_NODE_CHANGED : 0);
    650        1.1      jmmv 
    651        1.7      jmmv 	if (error != 0)
    652        1.6      yamt 		(void)tmpfs_reg_resize(vp, oldsize);
    653        1.6      yamt 
    654       1.31      jmmv 	VN_KNOTE(vp, NOTE_WRITE);
    655       1.31      jmmv 
    656        1.1      jmmv out:
    657        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    658        1.1      jmmv 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
    659        1.1      jmmv 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
    660        1.1      jmmv 
    661        1.1      jmmv 	return error;
    662        1.1      jmmv }
    663        1.1      jmmv 
    664        1.1      jmmv /* --------------------------------------------------------------------- */
    665        1.1      jmmv 
    666        1.1      jmmv int
    667        1.1      jmmv tmpfs_fsync(void *v)
    668        1.1      jmmv {
    669        1.1      jmmv 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
    670        1.1      jmmv 
    671        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    672        1.1      jmmv 
    673  1.49.10.2      yamt 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    674       1.17      yamt 
    675       1.17      yamt 	return 0;
    676        1.1      jmmv }
    677        1.1      jmmv 
    678        1.1      jmmv /* --------------------------------------------------------------------- */
    679        1.1      jmmv 
    680        1.1      jmmv int
    681        1.1      jmmv tmpfs_remove(void *v)
    682        1.1      jmmv {
    683        1.1      jmmv 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
    684        1.1      jmmv 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
    685       1.45        ad 	struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
    686        1.1      jmmv 
    687        1.1      jmmv 	int error;
    688        1.1      jmmv 	struct tmpfs_dirent *de;
    689        1.1      jmmv 	struct tmpfs_mount *tmp;
    690        1.1      jmmv 	struct tmpfs_node *dnode;
    691        1.1      jmmv 	struct tmpfs_node *node;
    692        1.1      jmmv 
    693        1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    694        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
    695        1.1      jmmv 
    696       1.34     pooka 	if (vp->v_type == VDIR) {
    697       1.34     pooka 		error = EPERM;
    698       1.34     pooka 		goto out;
    699       1.34     pooka 	}
    700       1.34     pooka 
    701        1.1      jmmv 	dnode = VP_TO_TMPFS_DIR(dvp);
    702        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    703        1.1      jmmv 	tmp = VFS_TO_TMPFS(vp->v_mount);
    704       1.45        ad 	de = tmpfs_dir_lookup(dnode, cnp);
    705  1.49.10.2      yamt 	KASSERT(de);
    706       1.45        ad 	KASSERT(de->td_node == node);
    707        1.1      jmmv 
    708        1.1      jmmv 	/* Files marked as immutable or append-only cannot be deleted. */
    709        1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    710        1.1      jmmv 		error = EPERM;
    711        1.1      jmmv 		goto out;
    712        1.1      jmmv 	}
    713        1.1      jmmv 
    714        1.1      jmmv 	/* Remove the entry from the directory; as it is a file, we do not
    715        1.1      jmmv 	 * have to change the number of hard links of the directory. */
    716        1.1      jmmv 	tmpfs_dir_detach(dvp, de);
    717        1.1      jmmv 
    718        1.1      jmmv 	/* Free the directory entry we just deleted.  Note that the node
    719        1.1      jmmv 	 * referred by it will not be removed until the vnode is really
    720        1.1      jmmv 	 * reclaimed. */
    721       1.37   thorpej 	tmpfs_free_dirent(tmp, de, true);
    722        1.1      jmmv 
    723        1.1      jmmv 	error = 0;
    724        1.1      jmmv 
    725        1.1      jmmv out:
    726        1.1      jmmv 	vput(vp);
    727       1.34     pooka 	if (dvp == vp)
    728       1.34     pooka 		vrele(dvp);
    729       1.34     pooka 	else
    730       1.34     pooka 		vput(dvp);
    731  1.49.10.5      yamt 	if (cnp->cn_flags & HASBUF) {
    732  1.49.10.5      yamt 		PNBUF_PUT(cnp->cn_pnbuf);
    733  1.49.10.5      yamt 		cnp->cn_flags &= ~HASBUF;
    734  1.49.10.5      yamt 	}
    735        1.1      jmmv 
    736        1.1      jmmv 	return error;
    737        1.1      jmmv }
    738        1.1      jmmv 
    739        1.1      jmmv /* --------------------------------------------------------------------- */
    740        1.1      jmmv 
    741        1.1      jmmv int
    742        1.1      jmmv tmpfs_link(void *v)
    743        1.1      jmmv {
    744        1.1      jmmv 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
    745        1.1      jmmv 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
    746        1.1      jmmv 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
    747        1.1      jmmv 
    748        1.1      jmmv 	int error;
    749        1.1      jmmv 	struct tmpfs_dirent *de;
    750        1.1      jmmv 	struct tmpfs_node *dnode;
    751        1.1      jmmv 	struct tmpfs_node *node;
    752        1.1      jmmv 
    753        1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
    754        1.1      jmmv 	KASSERT(cnp->cn_flags & HASBUF);
    755        1.1      jmmv 	KASSERT(dvp != vp); /* XXX When can this be false? */
    756        1.1      jmmv 
    757        1.1      jmmv 	dnode = VP_TO_TMPFS_DIR(dvp);
    758        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    759        1.1      jmmv 
    760       1.17      yamt 	/* Lock vp because we will need to run tmpfs_update over it, which
    761        1.1      jmmv 	 * needs the vnode to be locked. */
    762  1.49.10.5      yamt 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    763        1.1      jmmv 
    764        1.1      jmmv 	/* XXX: Why aren't the following two tests done by the caller? */
    765        1.1      jmmv 
    766        1.1      jmmv 	/* Hard links of directories are forbidden. */
    767        1.1      jmmv 	if (vp->v_type == VDIR) {
    768        1.1      jmmv 		error = EPERM;
    769        1.1      jmmv 		goto out;
    770        1.1      jmmv 	}
    771        1.1      jmmv 
    772        1.1      jmmv 	/* Cannot create cross-device links. */
    773        1.1      jmmv 	if (dvp->v_mount != vp->v_mount) {
    774        1.1      jmmv 		error = EXDEV;
    775        1.1      jmmv 		goto out;
    776        1.1      jmmv 	}
    777        1.1      jmmv 
    778        1.1      jmmv 	/* Ensure that we do not overflow the maximum number of links imposed
    779        1.1      jmmv 	 * by the system. */
    780        1.1      jmmv 	KASSERT(node->tn_links <= LINK_MAX);
    781        1.1      jmmv 	if (node->tn_links == LINK_MAX) {
    782        1.1      jmmv 		error = EMLINK;
    783        1.1      jmmv 		goto out;
    784        1.1      jmmv 	}
    785        1.1      jmmv 
    786        1.1      jmmv 	/* We cannot create links of files marked immutable or append-only. */
    787        1.1      jmmv 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
    788        1.1      jmmv 		error = EPERM;
    789        1.1      jmmv 		goto out;
    790        1.1      jmmv 	}
    791        1.1      jmmv 
    792        1.1      jmmv 	/* Allocate a new directory entry to represent the node. */
    793        1.1      jmmv 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
    794        1.1      jmmv 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
    795        1.1      jmmv 	if (error != 0)
    796        1.1      jmmv 		goto out;
    797        1.1      jmmv 
    798        1.1      jmmv 	/* Insert the new directory entry into the appropriate directory. */
    799        1.1      jmmv 	tmpfs_dir_attach(dvp, de);
    800        1.1      jmmv 
    801        1.1      jmmv 	/* vp link count has changed, so update node times. */
    802        1.1      jmmv 	node->tn_status |= TMPFS_NODE_CHANGED;
    803  1.49.10.2      yamt 	tmpfs_update(vp, NULL, NULL, NULL, 0);
    804        1.1      jmmv 
    805        1.1      jmmv 	error = 0;
    806        1.1      jmmv 
    807        1.1      jmmv out:
    808  1.49.10.6      yamt 	VOP_UNLOCK(vp);
    809        1.4      yamt 	PNBUF_PUT(cnp->cn_pnbuf);
    810        1.1      jmmv 	vput(dvp);
    811        1.1      jmmv 
    812        1.1      jmmv 	return error;
    813        1.1      jmmv }
    814        1.1      jmmv 
    815  1.49.10.5      yamt /*
    816  1.49.10.5      yamt  * tmpfs_rename: rename routine.
    817  1.49.10.5      yamt  *
    818  1.49.10.5      yamt  * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
    819  1.49.10.5      yamt  * and tvp (to-leaf), if exists (NULL if not).
    820  1.49.10.5      yamt  *
    821  1.49.10.5      yamt  * => Caller holds a reference on fdvp and fvp, they are unlocked.
    822  1.49.10.5      yamt  *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
    823  1.49.10.5      yamt  *
    824  1.49.10.5      yamt  * => Both tdvp and tvp are referenced and locked.  It is our responsibility
    825  1.49.10.5      yamt  *    to release the references and unlock them (or destroy).
    826  1.49.10.5      yamt  */
    827        1.1      jmmv int
    828        1.1      jmmv tmpfs_rename(void *v)
    829        1.1      jmmv {
    830        1.1      jmmv 	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
    831        1.1      jmmv 	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
    832        1.1      jmmv 	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
    833        1.1      jmmv 	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
    834        1.1      jmmv 	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
    835        1.1      jmmv 	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
    836        1.1      jmmv 
    837        1.1      jmmv 	char *newname;
    838        1.1      jmmv 	int error;
    839       1.45        ad 	struct tmpfs_dirent *de, *de2;
    840        1.1      jmmv 	struct tmpfs_mount *tmp;
    841        1.1      jmmv 	struct tmpfs_node *fdnode;
    842        1.1      jmmv 	struct tmpfs_node *fnode;
    843       1.39      jmmv 	struct tmpfs_node *tnode;
    844        1.1      jmmv 	struct tmpfs_node *tdnode;
    845       1.45        ad 	size_t namelen;
    846        1.1      jmmv 
    847        1.1      jmmv 	KASSERT(VOP_ISLOCKED(tdvp));
    848       1.45        ad 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
    849        1.1      jmmv 	KASSERT(fcnp->cn_flags & HASBUF);
    850        1.1      jmmv 	KASSERT(tcnp->cn_flags & HASBUF);
    851        1.1      jmmv 
    852       1.45        ad 	newname = NULL;
    853       1.45        ad 	namelen = 0;
    854       1.45        ad 	tmp = NULL;
    855        1.1      jmmv 
    856       1.45        ad 	/* Disallow cross-device renames. */
    857        1.1      jmmv 	if (fvp->v_mount != tdvp->v_mount ||
    858        1.1      jmmv 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
    859        1.1      jmmv 		error = EXDEV;
    860       1.45        ad 		goto out_unlocked;
    861        1.1      jmmv 	}
    862        1.1      jmmv 
    863       1.45        ad 	fnode = VP_TO_TMPFS_NODE(fvp);
    864       1.45        ad 	fdnode = VP_TO_TMPFS_DIR(fdvp);
    865       1.45        ad 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
    866       1.45        ad 	tdnode = VP_TO_TMPFS_DIR(tdvp);
    867        1.1      jmmv 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
    868       1.45        ad 
    869  1.49.10.2      yamt 	if (fdvp == tvp) {
    870  1.49.10.2      yamt 		error = 0;
    871  1.49.10.2      yamt 		goto out_unlocked;
    872  1.49.10.2      yamt 	}
    873  1.49.10.2      yamt 
    874  1.49.10.6      yamt 	/* Allocate memory, if necessary, for a new name. */
    875  1.49.10.6      yamt 	namelen = tcnp->cn_namelen;
    876  1.49.10.6      yamt 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
    877  1.49.10.6      yamt 		newname = tmpfs_strname_alloc(tmp, namelen);
    878  1.49.10.6      yamt 		if (newname == NULL) {
    879  1.49.10.6      yamt 			error = ENOSPC;
    880  1.49.10.6      yamt 			goto out_unlocked;
    881  1.49.10.6      yamt 		}
    882  1.49.10.6      yamt 	}
    883  1.49.10.6      yamt 
    884       1.45        ad 	/* If we need to move the directory between entries, lock the
    885       1.45        ad 	 * source so that we can safely operate on it. */
    886       1.45        ad 
    887       1.45        ad 	/* XXX: this is a potential locking order violation! */
    888       1.45        ad 	if (fdnode != tdnode) {
    889  1.49.10.5      yamt 		vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
    890       1.45        ad 	}
    891       1.45        ad 
    892  1.49.10.2      yamt 	/*
    893  1.49.10.2      yamt 	 * If the node we were renaming has scarpered, just give up.
    894  1.49.10.2      yamt 	 */
    895       1.45        ad 	de = tmpfs_dir_lookup(fdnode, fcnp);
    896  1.49.10.2      yamt 	if (de == NULL || de->td_node != fnode) {
    897       1.45        ad 		error = ENOENT;
    898       1.45        ad 		goto out;
    899       1.45        ad 	}
    900        1.1      jmmv 
    901  1.49.10.5      yamt 	/* If source and target is the same vnode, remove the source link. */
    902        1.1      jmmv 	if (fvp == tvp) {
    903  1.49.10.5      yamt 		/*
    904  1.49.10.5      yamt 		 * Detach and free the directory entry.  Drops the link
    905  1.49.10.5      yamt 		 * count on the node.
    906  1.49.10.5      yamt 		 */
    907  1.49.10.5      yamt 		tmpfs_dir_detach(fdvp, de);
    908  1.49.10.5      yamt 		tmpfs_free_dirent(VFS_TO_TMPFS(fvp->v_mount), de, true);
    909  1.49.10.5      yamt 		VN_KNOTE(fdvp, NOTE_WRITE);
    910  1.49.10.5      yamt 		goto out_ok;
    911        1.1      jmmv 	}
    912        1.1      jmmv 
    913       1.39      jmmv 	/* If replacing an existing entry, ensure we can do the operation. */
    914       1.39      jmmv 	if (tvp != NULL) {
    915       1.39      jmmv 		KASSERT(tnode != NULL);
    916       1.39      jmmv 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
    917       1.39      jmmv 			if (tnode->tn_size > 0) {
    918       1.39      jmmv 				error = ENOTEMPTY;
    919       1.39      jmmv 				goto out;
    920       1.39      jmmv 			}
    921       1.39      jmmv 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
    922       1.39      jmmv 			error = ENOTDIR;
    923       1.39      jmmv 			goto out;
    924       1.39      jmmv 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
    925       1.39      jmmv 			error = EISDIR;
    926       1.39      jmmv 			goto out;
    927       1.39      jmmv 		} else {
    928       1.39      jmmv 			KASSERT(fnode->tn_type != VDIR &&
    929       1.39      jmmv 			        tnode->tn_type != VDIR);
    930       1.39      jmmv 		}
    931       1.39      jmmv 	}
    932       1.39      jmmv 
    933        1.1      jmmv 	/* If the node is being moved to another directory, we have to do
    934        1.1      jmmv 	 * the move. */
    935        1.1      jmmv 	if (fdnode != tdnode) {
    936        1.1      jmmv 		/* In case we are moving a directory, we have to adjust its
    937        1.1      jmmv 		 * parent to point to the new parent. */
    938        1.1      jmmv 		if (de->td_node->tn_type == VDIR) {
    939        1.1      jmmv 			struct tmpfs_node *n;
    940        1.1      jmmv 
    941        1.1      jmmv 			/* Ensure the target directory is not a child of the
    942        1.1      jmmv 			 * directory being moved.  Otherwise, we'd end up
    943        1.1      jmmv 			 * with stale nodes. */
    944        1.1      jmmv 			n = tdnode;
    945       1.21      jmmv 			while (n != n->tn_spec.tn_dir.tn_parent) {
    946        1.1      jmmv 				if (n == fnode) {
    947        1.1      jmmv 					error = EINVAL;
    948       1.45        ad 					goto out;
    949        1.1      jmmv 				}
    950       1.21      jmmv 				n = n->tn_spec.tn_dir.tn_parent;
    951        1.1      jmmv 			}
    952        1.1      jmmv 
    953        1.1      jmmv 			/* Adjust the parent pointer. */
    954        1.1      jmmv 			TMPFS_VALIDATE_DIR(fnode);
    955       1.21      jmmv 			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
    956        1.1      jmmv 
    957        1.1      jmmv 			/* As a result of changing the target of the '..'
    958        1.1      jmmv 			 * entry, the link count of the source and target
    959        1.1      jmmv 			 * directories has to be adjusted. */
    960        1.1      jmmv 			fdnode->tn_links--;
    961        1.1      jmmv 			tdnode->tn_links++;
    962        1.1      jmmv 		}
    963        1.1      jmmv 
    964        1.1      jmmv 		/* Do the move: just remove the entry from the source directory
    965        1.1      jmmv 		 * and insert it into the target one. */
    966        1.1      jmmv 		tmpfs_dir_detach(fdvp, de);
    967        1.1      jmmv 		tmpfs_dir_attach(tdvp, de);
    968        1.1      jmmv 
    969        1.1      jmmv 		/* Notify listeners of fdvp about the change in the directory.
    970        1.1      jmmv 		 * We can do it at this point because we aren't touching fdvp
    971        1.1      jmmv 		 * any more below. */
    972        1.1      jmmv 		VN_KNOTE(fdvp, NOTE_WRITE);
    973        1.1      jmmv 	}
    974        1.1      jmmv 
    975       1.45        ad 	/* If we are overwriting an entry, we have to remove the old one
    976       1.45        ad 	 * from the target directory. */
    977       1.45        ad 	if (tvp != NULL) {
    978       1.45        ad 		KASSERT(tnode != NULL);
    979       1.45        ad 
    980       1.45        ad 		/* Remove the old entry from the target directory.
    981       1.45        ad 		 * Note! This relies on tmpfs_dir_attach() putting the new
    982       1.45        ad 		 * node on the end of the target's node list. */
    983       1.45        ad 		de2 = tmpfs_dir_lookup(tdnode, tcnp);
    984       1.45        ad 		KASSERT(de2 != NULL);
    985       1.45        ad 		KASSERT(de2->td_node == tnode);
    986       1.45        ad 		tmpfs_dir_detach(tdvp, de2);
    987       1.45        ad 
    988       1.45        ad 		/* Free the directory entry we just deleted.  Note that the
    989       1.45        ad 		 * node referred by it will not be removed until the vnode is
    990       1.45        ad 		 * really reclaimed. */
    991       1.45        ad 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
    992       1.45        ad 	}
    993       1.45        ad 
    994        1.1      jmmv 	/* If the name has changed, we need to make it effective by changing
    995        1.1      jmmv 	 * it in the directory entry. */
    996        1.1      jmmv 	if (newname != NULL) {
    997        1.1      jmmv 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
    998        1.1      jmmv 		KASSERT(tcnp->cn_namelen < 0xffff);
    999        1.1      jmmv 
   1000  1.49.10.6      yamt 		tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
   1001       1.45        ad 		de->td_namelen = (uint16_t)namelen;
   1002       1.45        ad 		memcpy(newname, tcnp->cn_nameptr, namelen);
   1003        1.1      jmmv 		de->td_name = newname;
   1004       1.45        ad 		newname = NULL;
   1005        1.1      jmmv 
   1006       1.27      jmmv 		fnode->tn_status |= TMPFS_NODE_CHANGED;
   1007       1.26      jmmv 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
   1008        1.1      jmmv 	}
   1009  1.49.10.5      yamt  out_ok:
   1010        1.1      jmmv 	/* Notify listeners of tdvp about the change in the directory (either
   1011       1.31      jmmv 	 * because a new entry was added or because one was removed) and
   1012       1.31      jmmv 	 * listeners of fvp about the rename. */
   1013        1.1      jmmv 	VN_KNOTE(tdvp, NOTE_WRITE);
   1014       1.31      jmmv 	VN_KNOTE(fvp, NOTE_RENAME);
   1015        1.1      jmmv 
   1016        1.1      jmmv 	error = 0;
   1017        1.1      jmmv 
   1018       1.45        ad  out:
   1019       1.11      jmmv 	if (fdnode != tdnode)
   1020  1.49.10.6      yamt 		VOP_UNLOCK(fdvp);
   1021       1.11      jmmv 
   1022       1.45        ad  out_unlocked:
   1023        1.1      jmmv 	/* Release target nodes. */
   1024        1.1      jmmv 	if (tdvp == tvp)
   1025        1.1      jmmv 		vrele(tdvp);
   1026        1.1      jmmv 	else
   1027        1.1      jmmv 		vput(tdvp);
   1028        1.1      jmmv 	if (tvp != NULL)
   1029        1.1      jmmv 		vput(tvp);
   1030        1.1      jmmv 
   1031        1.1      jmmv 	/* Release source nodes. */
   1032        1.1      jmmv 	vrele(fdvp);
   1033        1.1      jmmv 	vrele(fvp);
   1034        1.1      jmmv 
   1035  1.49.10.6      yamt 	if (newname != NULL) {
   1036  1.49.10.6      yamt 		tmpfs_strname_free(tmp, newname, namelen);
   1037  1.49.10.6      yamt 	}
   1038        1.1      jmmv 	return error;
   1039        1.1      jmmv }
   1040        1.1      jmmv 
   1041        1.1      jmmv /* --------------------------------------------------------------------- */
   1042        1.1      jmmv 
   1043        1.1      jmmv int
   1044        1.1      jmmv tmpfs_mkdir(void *v)
   1045        1.1      jmmv {
   1046        1.1      jmmv 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
   1047        1.1      jmmv 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
   1048        1.1      jmmv 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
   1049        1.1      jmmv 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
   1050        1.1      jmmv 
   1051        1.1      jmmv 	KASSERT(vap->va_type == VDIR);
   1052        1.1      jmmv 
   1053        1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
   1054        1.1      jmmv }
   1055        1.1      jmmv 
   1056        1.1      jmmv /* --------------------------------------------------------------------- */
   1057        1.1      jmmv 
   1058        1.1      jmmv int
   1059        1.1      jmmv tmpfs_rmdir(void *v)
   1060        1.1      jmmv {
   1061        1.1      jmmv 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
   1062        1.1      jmmv 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
   1063       1.45        ad 	struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
   1064        1.1      jmmv 
   1065        1.1      jmmv 	int error;
   1066        1.1      jmmv 	struct tmpfs_dirent *de;
   1067        1.1      jmmv 	struct tmpfs_mount *tmp;
   1068        1.1      jmmv 	struct tmpfs_node *dnode;
   1069        1.1      jmmv 	struct tmpfs_node *node;
   1070        1.1      jmmv 
   1071        1.1      jmmv 	KASSERT(VOP_ISLOCKED(dvp));
   1072        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1073        1.1      jmmv 
   1074        1.1      jmmv 	tmp = VFS_TO_TMPFS(dvp->v_mount);
   1075        1.1      jmmv 	dnode = VP_TO_TMPFS_DIR(dvp);
   1076        1.1      jmmv 	node = VP_TO_TMPFS_DIR(vp);
   1077       1.40    dyoung 	error = 0;
   1078       1.34     pooka 
   1079       1.34     pooka 	/* Directories with more than two entries ('.' and '..') cannot be
   1080       1.34     pooka 	 * removed. */
   1081       1.34     pooka 	if (node->tn_size > 0) {
   1082       1.34     pooka 		error = ENOTEMPTY;
   1083       1.34     pooka 		goto out;
   1084       1.34     pooka 	}
   1085       1.34     pooka 
   1086       1.34     pooka 	/* This invariant holds only if we are not trying to remove "..".
   1087       1.34     pooka 	 * We checked for that above so this is safe now. */
   1088       1.21      jmmv 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
   1089        1.1      jmmv 
   1090       1.45        ad 	/* Get the directory entry associated with node (vp). */
   1091       1.45        ad 	de = tmpfs_dir_lookup(dnode, cnp);
   1092  1.49.10.2      yamt 	KASSERT(de);
   1093       1.45        ad 	KASSERT(de->td_node == node);
   1094        1.1      jmmv 
   1095        1.1      jmmv 	/* Check flags to see if we are allowed to remove the directory. */
   1096        1.1      jmmv 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
   1097        1.1      jmmv 		error = EPERM;
   1098        1.1      jmmv 		goto out;
   1099        1.1      jmmv 	}
   1100        1.1      jmmv 
   1101        1.1      jmmv 	/* Detach the directory entry from the directory (dnode). */
   1102        1.1      jmmv 	tmpfs_dir_detach(dvp, de);
   1103        1.1      jmmv 
   1104        1.1      jmmv 	node->tn_links--;
   1105        1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
   1106        1.1      jmmv 	    TMPFS_NODE_MODIFIED;
   1107       1.21      jmmv 	node->tn_spec.tn_dir.tn_parent->tn_links--;
   1108       1.21      jmmv 	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
   1109        1.1      jmmv 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
   1110        1.1      jmmv 
   1111       1.31      jmmv 	/* Release the parent. */
   1112        1.1      jmmv 	cache_purge(dvp); /* XXX Is this needed? */
   1113        1.1      jmmv 
   1114        1.1      jmmv 	/* Free the directory entry we just deleted.  Note that the node
   1115        1.1      jmmv 	 * referred by it will not be removed until the vnode is really
   1116        1.1      jmmv 	 * reclaimed. */
   1117       1.37   thorpej 	tmpfs_free_dirent(tmp, de, true);
   1118        1.1      jmmv 
   1119       1.45        ad 	KASSERT(node->tn_links == 0);
   1120       1.40    dyoung  out:
   1121       1.40    dyoung 	/* Release the nodes. */
   1122       1.40    dyoung 	vput(dvp);
   1123        1.1      jmmv 	vput(vp);
   1124  1.49.10.2      yamt 	PNBUF_PUT(cnp->cn_pnbuf);
   1125        1.1      jmmv 
   1126        1.1      jmmv 	return error;
   1127        1.1      jmmv }
   1128        1.1      jmmv 
   1129        1.1      jmmv /* --------------------------------------------------------------------- */
   1130        1.1      jmmv 
   1131        1.1      jmmv int
   1132        1.1      jmmv tmpfs_symlink(void *v)
   1133        1.1      jmmv {
   1134        1.1      jmmv 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
   1135        1.1      jmmv 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
   1136        1.1      jmmv 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
   1137        1.1      jmmv 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
   1138        1.1      jmmv 	char *target = ((struct vop_symlink_args *)v)->a_target;
   1139        1.1      jmmv 
   1140        1.1      jmmv 	KASSERT(vap->va_type == VLNK);
   1141        1.1      jmmv 
   1142        1.1      jmmv 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
   1143        1.1      jmmv }
   1144        1.1      jmmv 
   1145        1.1      jmmv /* --------------------------------------------------------------------- */
   1146        1.1      jmmv 
   1147        1.1      jmmv int
   1148        1.1      jmmv tmpfs_readdir(void *v)
   1149        1.1      jmmv {
   1150        1.1      jmmv 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
   1151        1.1      jmmv 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
   1152        1.1      jmmv 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
   1153        1.1      jmmv 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
   1154        1.1      jmmv 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
   1155        1.1      jmmv 
   1156        1.1      jmmv 	int error;
   1157       1.10      yamt 	off_t startoff;
   1158       1.10      yamt 	off_t cnt;
   1159        1.1      jmmv 	struct tmpfs_node *node;
   1160        1.1      jmmv 
   1161        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1162        1.1      jmmv 
   1163        1.1      jmmv 	/* This operation only makes sense on directory nodes. */
   1164        1.1      jmmv 	if (vp->v_type != VDIR) {
   1165        1.1      jmmv 		error = ENOTDIR;
   1166        1.1      jmmv 		goto out;
   1167        1.1      jmmv 	}
   1168        1.1      jmmv 
   1169        1.1      jmmv 	node = VP_TO_TMPFS_DIR(vp);
   1170        1.1      jmmv 
   1171        1.1      jmmv 	startoff = uio->uio_offset;
   1172        1.1      jmmv 
   1173       1.10      yamt 	cnt = 0;
   1174       1.10      yamt 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
   1175        1.1      jmmv 		error = tmpfs_dir_getdotdent(node, uio);
   1176        1.1      jmmv 		if (error == -1) {
   1177        1.1      jmmv 			error = 0;
   1178        1.1      jmmv 			goto outok;
   1179        1.1      jmmv 		} else if (error != 0)
   1180        1.1      jmmv 			goto outok;
   1181       1.10      yamt 		cnt++;
   1182        1.1      jmmv 	}
   1183        1.1      jmmv 
   1184       1.10      yamt 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
   1185        1.1      jmmv 		error = tmpfs_dir_getdotdotdent(node, uio);
   1186        1.1      jmmv 		if (error == -1) {
   1187        1.1      jmmv 			error = 0;
   1188        1.1      jmmv 			goto outok;
   1189        1.1      jmmv 		} else if (error != 0)
   1190        1.1      jmmv 			goto outok;
   1191       1.10      yamt 		cnt++;
   1192        1.1      jmmv 	}
   1193        1.1      jmmv 
   1194       1.10      yamt 	error = tmpfs_dir_getdents(node, uio, &cnt);
   1195        1.1      jmmv 	if (error == -1)
   1196        1.1      jmmv 		error = 0;
   1197        1.1      jmmv 	KASSERT(error >= 0);
   1198        1.1      jmmv 
   1199        1.1      jmmv outok:
   1200       1.10      yamt 	/* This label assumes that startoff has been
   1201        1.1      jmmv 	 * initialized.  If the compiler didn't spit out warnings, we'd
   1202        1.1      jmmv 	 * simply make this one be 'out' and drop 'outok'. */
   1203        1.1      jmmv 
   1204        1.1      jmmv 	if (eofflag != NULL)
   1205        1.1      jmmv 		*eofflag =
   1206       1.10      yamt 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
   1207        1.1      jmmv 
   1208        1.1      jmmv 	/* Update NFS-related variables. */
   1209        1.1      jmmv 	if (error == 0 && cookies != NULL && ncookies != NULL) {
   1210        1.1      jmmv 		off_t i;
   1211       1.10      yamt 		off_t off = startoff;
   1212       1.10      yamt 		struct tmpfs_dirent *de = NULL;
   1213        1.1      jmmv 
   1214       1.10      yamt 		*ncookies = cnt;
   1215       1.10      yamt 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
   1216       1.10      yamt 
   1217       1.10      yamt 		for (i = 0; i < cnt; i++) {
   1218       1.10      yamt 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
   1219       1.10      yamt 			if (off == TMPFS_DIRCOOKIE_DOT) {
   1220       1.10      yamt 				off = TMPFS_DIRCOOKIE_DOTDOT;
   1221       1.10      yamt 			} else {
   1222       1.10      yamt 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
   1223       1.21      jmmv 					de = TAILQ_FIRST(&node->tn_spec.
   1224       1.21      jmmv 					    tn_dir.tn_dir);
   1225       1.10      yamt 				} else if (de != NULL) {
   1226       1.10      yamt 					de = TAILQ_NEXT(de, td_entries);
   1227       1.10      yamt 				} else {
   1228       1.10      yamt 					de = tmpfs_dir_lookupbycookie(node,
   1229       1.10      yamt 					    off);
   1230       1.10      yamt 					KASSERT(de != NULL);
   1231       1.10      yamt 					de = TAILQ_NEXT(de, td_entries);
   1232       1.10      yamt 				}
   1233       1.10      yamt 				if (de == NULL) {
   1234       1.10      yamt 					off = TMPFS_DIRCOOKIE_EOF;
   1235       1.10      yamt 				} else {
   1236       1.29      jmmv 					off = tmpfs_dircookie(de);
   1237       1.10      yamt 				}
   1238       1.10      yamt 			}
   1239       1.10      yamt 
   1240       1.10      yamt 			(*cookies)[i] = off;
   1241       1.10      yamt 		}
   1242       1.10      yamt 		KASSERT(uio->uio_offset == off);
   1243        1.1      jmmv 	}
   1244        1.1      jmmv 
   1245        1.1      jmmv out:
   1246        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1247        1.1      jmmv 
   1248        1.1      jmmv 	return error;
   1249        1.1      jmmv }
   1250        1.1      jmmv 
   1251        1.1      jmmv /* --------------------------------------------------------------------- */
   1252        1.1      jmmv 
   1253        1.1      jmmv int
   1254        1.1      jmmv tmpfs_readlink(void *v)
   1255        1.1      jmmv {
   1256        1.1      jmmv 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
   1257        1.1      jmmv 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
   1258        1.1      jmmv 
   1259        1.1      jmmv 	int error;
   1260        1.1      jmmv 	struct tmpfs_node *node;
   1261        1.1      jmmv 
   1262        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1263        1.1      jmmv 	KASSERT(uio->uio_offset == 0);
   1264        1.1      jmmv 	KASSERT(vp->v_type == VLNK);
   1265        1.1      jmmv 
   1266        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1267        1.1      jmmv 
   1268       1.21      jmmv 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
   1269       1.21      jmmv 	    MIN(node->tn_size, uio->uio_resid), uio);
   1270        1.1      jmmv 	node->tn_status |= TMPFS_NODE_ACCESSED;
   1271        1.1      jmmv 
   1272        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1273        1.1      jmmv 
   1274        1.1      jmmv 	return error;
   1275        1.1      jmmv }
   1276        1.1      jmmv 
   1277        1.1      jmmv /* --------------------------------------------------------------------- */
   1278        1.1      jmmv 
   1279        1.1      jmmv int
   1280        1.1      jmmv tmpfs_inactive(void *v)
   1281        1.1      jmmv {
   1282        1.1      jmmv 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
   1283        1.1      jmmv 
   1284        1.1      jmmv 	struct tmpfs_node *node;
   1285        1.1      jmmv 
   1286        1.1      jmmv 	KASSERT(VOP_ISLOCKED(vp));
   1287        1.1      jmmv 
   1288        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1289       1.45        ad 	*((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
   1290  1.49.10.6      yamt 	VOP_UNLOCK(vp);
   1291        1.1      jmmv 
   1292        1.1      jmmv 	return 0;
   1293        1.1      jmmv }
   1294        1.1      jmmv 
   1295        1.1      jmmv /* --------------------------------------------------------------------- */
   1296        1.1      jmmv 
   1297        1.1      jmmv int
   1298        1.1      jmmv tmpfs_reclaim(void *v)
   1299        1.1      jmmv {
   1300        1.1      jmmv 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
   1301        1.1      jmmv 
   1302        1.1      jmmv 	struct tmpfs_mount *tmp;
   1303        1.1      jmmv 	struct tmpfs_node *node;
   1304        1.1      jmmv 
   1305        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1306        1.1      jmmv 	tmp = VFS_TO_TMPFS(vp->v_mount);
   1307        1.1      jmmv 
   1308        1.1      jmmv 	cache_purge(vp);
   1309        1.1      jmmv 	tmpfs_free_vp(vp);
   1310        1.1      jmmv 
   1311        1.1      jmmv 	/* If the node referenced by this vnode was deleted by the user,
   1312        1.1      jmmv 	 * we must free its associated data structures (now that the vnode
   1313        1.1      jmmv 	 * is being reclaimed). */
   1314        1.1      jmmv 	if (node->tn_links == 0)
   1315        1.1      jmmv 		tmpfs_free_node(tmp, node);
   1316        1.1      jmmv 
   1317        1.1      jmmv 	KASSERT(vp->v_data == NULL);
   1318        1.1      jmmv 
   1319        1.1      jmmv 	return 0;
   1320        1.1      jmmv }
   1321        1.1      jmmv 
   1322        1.1      jmmv /* --------------------------------------------------------------------- */
   1323        1.1      jmmv 
   1324        1.1      jmmv int
   1325        1.1      jmmv tmpfs_print(void *v)
   1326        1.1      jmmv {
   1327        1.1      jmmv 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
   1328        1.1      jmmv 
   1329        1.1      jmmv 	struct tmpfs_node *node;
   1330        1.1      jmmv 
   1331        1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1332        1.1      jmmv 
   1333        1.1      jmmv 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
   1334        1.1      jmmv 	    node, node->tn_flags, node->tn_links);
   1335        1.1      jmmv 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
   1336  1.49.10.6      yamt 	    ", status 0x%x",
   1337        1.1      jmmv 	    node->tn_mode, node->tn_uid, node->tn_gid,
   1338        1.1      jmmv 	    (uintmax_t)node->tn_size, node->tn_status);
   1339        1.1      jmmv 	if (vp->v_type == VFIFO)
   1340  1.49.10.6      yamt 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   1341        1.1      jmmv 	printf("\n");
   1342        1.1      jmmv 
   1343        1.1      jmmv 	return 0;
   1344        1.1      jmmv }
   1345        1.1      jmmv 
   1346        1.1      jmmv /* --------------------------------------------------------------------- */
   1347        1.1      jmmv 
   1348        1.1      jmmv int
   1349        1.1      jmmv tmpfs_pathconf(void *v)
   1350        1.1      jmmv {
   1351        1.1      jmmv 	int name = ((struct vop_pathconf_args *)v)->a_name;
   1352        1.1      jmmv 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
   1353        1.1      jmmv 
   1354        1.1      jmmv 	int error;
   1355        1.1      jmmv 
   1356        1.1      jmmv 	error = 0;
   1357        1.1      jmmv 
   1358        1.1      jmmv 	switch (name) {
   1359        1.1      jmmv 	case _PC_LINK_MAX:
   1360        1.1      jmmv 		*retval = LINK_MAX;
   1361        1.1      jmmv 		break;
   1362        1.1      jmmv 
   1363        1.1      jmmv 	case _PC_NAME_MAX:
   1364        1.1      jmmv 		*retval = NAME_MAX;
   1365        1.1      jmmv 		break;
   1366        1.1      jmmv 
   1367        1.1      jmmv 	case _PC_PATH_MAX:
   1368        1.1      jmmv 		*retval = PATH_MAX;
   1369        1.1      jmmv 		break;
   1370        1.1      jmmv 
   1371        1.1      jmmv 	case _PC_PIPE_BUF:
   1372        1.1      jmmv 		*retval = PIPE_BUF;
   1373        1.1      jmmv 		break;
   1374        1.1      jmmv 
   1375        1.1      jmmv 	case _PC_CHOWN_RESTRICTED:
   1376        1.1      jmmv 		*retval = 1;
   1377        1.1      jmmv 		break;
   1378        1.1      jmmv 
   1379        1.1      jmmv 	case _PC_NO_TRUNC:
   1380        1.1      jmmv 		*retval = 1;
   1381        1.1      jmmv 		break;
   1382        1.1      jmmv 
   1383        1.1      jmmv 	case _PC_SYNC_IO:
   1384        1.1      jmmv 		*retval = 1;
   1385        1.1      jmmv 		break;
   1386        1.1      jmmv 
   1387        1.1      jmmv 	case _PC_FILESIZEBITS:
   1388        1.1      jmmv 		*retval = 0; /* XXX Don't know which value should I return. */
   1389        1.1      jmmv 		break;
   1390        1.1      jmmv 
   1391        1.1      jmmv 	default:
   1392        1.1      jmmv 		error = EINVAL;
   1393        1.1      jmmv 	}
   1394        1.1      jmmv 
   1395        1.1      jmmv 	return error;
   1396        1.1      jmmv }
   1397        1.1      jmmv 
   1398        1.1      jmmv /* --------------------------------------------------------------------- */
   1399        1.1      jmmv 
   1400        1.1      jmmv int
   1401       1.15      jmmv tmpfs_advlock(void *v)
   1402       1.15      jmmv {
   1403       1.15      jmmv 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
   1404       1.15      jmmv 
   1405       1.15      jmmv 	struct tmpfs_node *node;
   1406       1.15      jmmv 
   1407       1.15      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1408       1.15      jmmv 
   1409       1.15      jmmv 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
   1410       1.15      jmmv }
   1411       1.15      jmmv 
   1412       1.15      jmmv /* --------------------------------------------------------------------- */
   1413       1.15      jmmv 
   1414       1.15      jmmv int
   1415        1.1      jmmv tmpfs_getpages(void *v)
   1416        1.1      jmmv {
   1417        1.7      jmmv 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
   1418        1.7      jmmv 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
   1419        1.7      jmmv 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
   1420        1.7      jmmv 	int *count = ((struct vop_getpages_args *)v)->a_count;
   1421        1.7      jmmv 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
   1422        1.7      jmmv 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
   1423        1.7      jmmv 	int advice = ((struct vop_getpages_args *)v)->a_advice;
   1424        1.7      jmmv 	int flags = ((struct vop_getpages_args *)v)->a_flags;
   1425        1.7      jmmv 
   1426        1.7      jmmv 	int error;
   1427       1.28      jmmv 	int i;
   1428        1.7      jmmv 	struct tmpfs_node *node;
   1429        1.6      yamt 	struct uvm_object *uobj;
   1430        1.9      yamt 	int npages = *count;
   1431        1.1      jmmv 
   1432        1.6      yamt 	KASSERT(vp->v_type == VREG);
   1433       1.45        ad 	KASSERT(mutex_owned(&vp->v_interlock));
   1434        1.1      jmmv 
   1435        1.7      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1436       1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1437        1.1      jmmv 
   1438        1.9      yamt 	/* We currently don't rely on PGO_PASTEOF. */
   1439        1.9      yamt 
   1440        1.9      yamt 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
   1441        1.9      yamt 		if ((flags & PGO_LOCKED) == 0)
   1442       1.45        ad 			mutex_exit(&vp->v_interlock);
   1443        1.9      yamt 		return EINVAL;
   1444        1.9      yamt 	}
   1445        1.9      yamt 
   1446        1.9      yamt 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
   1447        1.9      yamt 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
   1448        1.9      yamt 	}
   1449        1.9      yamt 
   1450        1.7      jmmv 	if ((flags & PGO_LOCKED) != 0)
   1451        1.6      yamt 		return EBUSY;
   1452        1.1      jmmv 
   1453        1.6      yamt 	if ((flags & PGO_NOTIMESTAMP) == 0) {
   1454        1.7      jmmv 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   1455        1.6      yamt 			node->tn_status |= TMPFS_NODE_ACCESSED;
   1456        1.7      jmmv 
   1457        1.7      jmmv 		if ((access_type & VM_PROT_WRITE) != 0)
   1458        1.6      yamt 			node->tn_status |= TMPFS_NODE_MODIFIED;
   1459        1.1      jmmv 	}
   1460        1.1      jmmv 
   1461       1.45        ad 	mutex_exit(&vp->v_interlock);
   1462        1.6      yamt 
   1463       1.28      jmmv 	/*
   1464       1.28      jmmv 	 * Make sure that the array on which we will store the
   1465       1.28      jmmv 	 * gotten pages is clean.  Otherwise uao_get (pointed to by
   1466       1.28      jmmv 	 * the pgo_get below) gets confused and does not return the
   1467       1.28      jmmv 	 * appropriate pages.
   1468       1.49      jmmv 	 *
   1469       1.28      jmmv 	 * XXX This shall be revisited when kern/32166 is addressed
   1470       1.28      jmmv 	 * because the loop to clean m[i] will most likely be redundant
   1471       1.28      jmmv 	 * as well as the PGO_ALLPAGES flag.
   1472       1.28      jmmv 	 */
   1473       1.28      jmmv 	if (m != NULL)
   1474       1.28      jmmv 		for (i = 0; i < npages; i++)
   1475       1.28      jmmv 			m[i] = NULL;
   1476       1.45        ad 	mutex_enter(&uobj->vmobjlock);
   1477        1.9      yamt 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
   1478       1.28      jmmv 	    access_type, advice, flags | PGO_ALLPAGES);
   1479       1.28      jmmv #if defined(DEBUG)
   1480       1.28      jmmv 	{
   1481       1.28      jmmv 		/* Make sure that all the pages we return are valid. */
   1482       1.28      jmmv 		int dbgi;
   1483       1.28      jmmv 		if (error == 0 && m != NULL)
   1484       1.28      jmmv 			for (dbgi = 0; dbgi < npages; dbgi++)
   1485       1.28      jmmv 				KASSERT(m[dbgi] != NULL);
   1486       1.28      jmmv 	}
   1487       1.28      jmmv #endif
   1488        1.1      jmmv 
   1489        1.6      yamt 	return error;
   1490        1.6      yamt }
   1491        1.6      yamt 
   1492        1.6      yamt /* --------------------------------------------------------------------- */
   1493        1.6      yamt 
   1494        1.6      yamt int
   1495        1.6      yamt tmpfs_putpages(void *v)
   1496        1.6      yamt {
   1497        1.7      jmmv 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
   1498        1.7      jmmv 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
   1499        1.7      jmmv 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
   1500        1.7      jmmv 	int flags = ((struct vop_putpages_args *)v)->a_flags;
   1501        1.7      jmmv 
   1502        1.7      jmmv 	int error;
   1503        1.7      jmmv 	struct tmpfs_node *node;
   1504        1.6      yamt 	struct uvm_object *uobj;
   1505        1.6      yamt 
   1506       1.45        ad 	KASSERT(mutex_owned(&vp->v_interlock));
   1507        1.6      yamt 
   1508        1.7      jmmv 	node = VP_TO_TMPFS_NODE(vp);
   1509        1.7      jmmv 
   1510        1.6      yamt 	if (vp->v_type != VREG) {
   1511       1.45        ad 		mutex_exit(&vp->v_interlock);
   1512        1.6      yamt 		return 0;
   1513        1.1      jmmv 	}
   1514        1.1      jmmv 
   1515       1.21      jmmv 	uobj = node->tn_spec.tn_reg.tn_aobj;
   1516       1.45        ad 	mutex_exit(&vp->v_interlock);
   1517        1.6      yamt 
   1518       1.45        ad 	mutex_enter(&uobj->vmobjlock);
   1519        1.7      jmmv 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
   1520        1.6      yamt 
   1521        1.6      yamt 	/* XXX mtime */
   1522        1.1      jmmv 
   1523        1.1      jmmv 	return error;
   1524        1.1      jmmv }
   1525