xref: /minix3/sys/fs/v7fs/v7fs_vnops.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: v7fs_vnops.c,v 1.21 2015/04/20 23:03:08 riastradh Exp $	*/
29f988b79SJean-Baptiste Boric 
39f988b79SJean-Baptiste Boric /*-
49f988b79SJean-Baptiste Boric  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
59f988b79SJean-Baptiste Boric  * All rights reserved.
69f988b79SJean-Baptiste Boric  *
79f988b79SJean-Baptiste Boric  * This code is derived from software contributed to The NetBSD Foundation
89f988b79SJean-Baptiste Boric  * by UCHIYAMA Yasushi.
99f988b79SJean-Baptiste Boric  *
109f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
119f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
129f988b79SJean-Baptiste Boric  * are met:
139f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
149f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
159f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
169f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
179f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
189f988b79SJean-Baptiste Boric  *
199f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209f988b79SJean-Baptiste Boric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219f988b79SJean-Baptiste Boric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229f988b79SJean-Baptiste Boric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239f988b79SJean-Baptiste Boric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249f988b79SJean-Baptiste Boric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259f988b79SJean-Baptiste Boric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269f988b79SJean-Baptiste Boric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279f988b79SJean-Baptiste Boric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289f988b79SJean-Baptiste Boric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299f988b79SJean-Baptiste Boric  * POSSIBILITY OF SUCH DAMAGE.
309f988b79SJean-Baptiste Boric  */
319f988b79SJean-Baptiste Boric 
329f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: v7fs_vnops.c,v 1.21 2015/04/20 23:03:08 riastradh Exp $");
349f988b79SJean-Baptiste Boric #if defined _KERNEL_OPT
359f988b79SJean-Baptiste Boric #include "opt_v7fs.h"
369f988b79SJean-Baptiste Boric #endif
379f988b79SJean-Baptiste Boric 
389f988b79SJean-Baptiste Boric #include <sys/param.h>
399f988b79SJean-Baptiste Boric #include <sys/kernel.h>
409f988b79SJean-Baptiste Boric #include <sys/resource.h>
419f988b79SJean-Baptiste Boric #include <sys/vnode.h>
429f988b79SJean-Baptiste Boric #include <sys/namei.h>
439f988b79SJean-Baptiste Boric #include <sys/dirent.h>
449f988b79SJean-Baptiste Boric #include <sys/kmem.h>
459f988b79SJean-Baptiste Boric #include <sys/lockf.h>
469f988b79SJean-Baptiste Boric #include <sys/unistd.h>
479f988b79SJean-Baptiste Boric #include <sys/fcntl.h>
489f988b79SJean-Baptiste Boric #include <sys/kauth.h>
499f988b79SJean-Baptiste Boric #include <sys/buf.h>
509f988b79SJean-Baptiste Boric #include <sys/stat.h>	/*APPEND */
519f988b79SJean-Baptiste Boric #include <miscfs/genfs/genfs.h>
529f988b79SJean-Baptiste Boric 
539f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs.h>
549f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs_impl.h>
559f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs_inode.h>
569f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs_dirent.h>
579f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs_file.h>
589f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs_datablock.h>
599f988b79SJean-Baptiste Boric #include <fs/v7fs/v7fs_extern.h>
609f988b79SJean-Baptiste Boric 
619f988b79SJean-Baptiste Boric #ifdef V7FS_VNOPS_DEBUG
629f988b79SJean-Baptiste Boric #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
639f988b79SJean-Baptiste Boric #else
649f988b79SJean-Baptiste Boric #define	DPRINTF(arg...)		((void)0)
659f988b79SJean-Baptiste Boric #endif
669f988b79SJean-Baptiste Boric 
679f988b79SJean-Baptiste Boric static v7fs_mode_t vtype_to_v7fs_mode(enum vtype);
689f988b79SJean-Baptiste Boric static uint8_t v7fs_mode_to_d_type(v7fs_mode_t);
699f988b79SJean-Baptiste Boric 
709f988b79SJean-Baptiste Boric static v7fs_mode_t
vtype_to_v7fs_mode(enum vtype type)719f988b79SJean-Baptiste Boric vtype_to_v7fs_mode(enum vtype type)
729f988b79SJean-Baptiste Boric {
739f988b79SJean-Baptiste Boric 	/* Convert Vnode types to V7FS types (sys/vnode.h)*/
749f988b79SJean-Baptiste Boric 	v7fs_mode_t table[] = { 0, V7FS_IFREG, V7FS_IFDIR, V7FS_IFBLK,
759f988b79SJean-Baptiste Boric 				V7FS_IFCHR, V7FSBSD_IFLNK, V7FSBSD_IFSOCK,
769f988b79SJean-Baptiste Boric 				V7FSBSD_IFFIFO };
779f988b79SJean-Baptiste Boric 	return table[type];
789f988b79SJean-Baptiste Boric }
799f988b79SJean-Baptiste Boric 
809f988b79SJean-Baptiste Boric static uint8_t
v7fs_mode_to_d_type(v7fs_mode_t mode)819f988b79SJean-Baptiste Boric v7fs_mode_to_d_type(v7fs_mode_t mode)
829f988b79SJean-Baptiste Boric {
839f988b79SJean-Baptiste Boric 	/* Convert V7FS types to dirent d_type (sys/dirent.h)*/
849f988b79SJean-Baptiste Boric 
859f988b79SJean-Baptiste Boric 	return (mode & V7FS_IFMT) >> 12;
869f988b79SJean-Baptiste Boric }
879f988b79SJean-Baptiste Boric 
889f988b79SJean-Baptiste Boric int
v7fs_lookup(void * v)899f988b79SJean-Baptiste Boric v7fs_lookup(void *v)
909f988b79SJean-Baptiste Boric {
91*0a6a1f1dSLionel Sambuc 	struct vop_lookup_v2_args /* {
929f988b79SJean-Baptiste Boric 				  struct vnode *a_dvp;
939f988b79SJean-Baptiste Boric 				  struct vnode **a_vpp;
949f988b79SJean-Baptiste Boric 				  struct componentname *a_cnp;
959f988b79SJean-Baptiste Boric 				  } */ *a = v;
969f988b79SJean-Baptiste Boric 	struct vnode *dvp = a->a_dvp;
979f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = dvp->v_data;
989f988b79SJean-Baptiste Boric 	struct v7fs_inode *parent = &parent_node->inode;
999f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = parent_node->v7fsmount->core;/* my filesystem */
1009f988b79SJean-Baptiste Boric 	struct vnode *vpp;
1019f988b79SJean-Baptiste Boric 	struct componentname *cnp = a->a_cnp;
1029f988b79SJean-Baptiste Boric 	int nameiop = cnp->cn_nameiop;
1039f988b79SJean-Baptiste Boric 	const char *name = cnp->cn_nameptr;
1049f988b79SJean-Baptiste Boric 	int namelen = cnp->cn_namelen;
1059f988b79SJean-Baptiste Boric 	int flags = cnp->cn_flags;
1069f988b79SJean-Baptiste Boric 	bool isdotdot = flags & ISDOTDOT;
1079f988b79SJean-Baptiste Boric 	bool islastcn = flags & ISLASTCN;
1089f988b79SJean-Baptiste Boric 	v7fs_ino_t ino;
1099f988b79SJean-Baptiste Boric 	int error;
1109f988b79SJean-Baptiste Boric #ifdef V7FS_VNOPS_DEBUG
1119f988b79SJean-Baptiste Boric 	const char *opname[] = { "LOOKUP", "CREATE", "DELETE", "RENAME" };
1129f988b79SJean-Baptiste Boric #endif
1139f988b79SJean-Baptiste Boric 	DPRINTF("'%s' op=%s flags=%d parent=%d %o %dbyte\n", name,
1149f988b79SJean-Baptiste Boric 	    opname[nameiop], cnp->cn_flags, parent->inode_number, parent->mode,
1159f988b79SJean-Baptiste Boric 	    parent->filesize);
1169f988b79SJean-Baptiste Boric 
1179f988b79SJean-Baptiste Boric 	*a->a_vpp = 0;
1189f988b79SJean-Baptiste Boric 
1199f988b79SJean-Baptiste Boric 	/* Check directory permission for search */
1209f988b79SJean-Baptiste Boric 	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred))) {
1219f988b79SJean-Baptiste Boric 		DPRINTF("***perm.\n");
1229f988b79SJean-Baptiste Boric 		return error;
1239f988b79SJean-Baptiste Boric 	}
1249f988b79SJean-Baptiste Boric 
1259f988b79SJean-Baptiste Boric 	/* Deny last component write operation on a read-only mount */
1269f988b79SJean-Baptiste Boric 	if (islastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1279f988b79SJean-Baptiste Boric 	    (nameiop == DELETE || nameiop == RENAME)) {
1289f988b79SJean-Baptiste Boric 		DPRINTF("***ROFS.\n");
1299f988b79SJean-Baptiste Boric 		return EROFS;
1309f988b79SJean-Baptiste Boric 	}
1319f988b79SJean-Baptiste Boric 
132*0a6a1f1dSLionel Sambuc 	/* No lookup on removed directory */
133*0a6a1f1dSLionel Sambuc 	if (v7fs_inode_nlink(parent) == 0)
134*0a6a1f1dSLionel Sambuc 		return ENOENT;
135*0a6a1f1dSLionel Sambuc 
1369f988b79SJean-Baptiste Boric 	/* "." */
1379f988b79SJean-Baptiste Boric 	if (namelen == 1 && name[0] == '.') {
1389f988b79SJean-Baptiste Boric 		if ((nameiop == RENAME) && islastcn) {
1399f988b79SJean-Baptiste Boric 			return EISDIR; /* t_vnops rename_dir(3) */
1409f988b79SJean-Baptiste Boric 		}
1419f988b79SJean-Baptiste Boric 		vref(dvp); /* v_usecount++ */
1429f988b79SJean-Baptiste Boric 		*a->a_vpp = dvp;
1439f988b79SJean-Baptiste Boric 		DPRINTF("done.(.)\n");
1449f988b79SJean-Baptiste Boric 		return 0;
1459f988b79SJean-Baptiste Boric 	}
1469f988b79SJean-Baptiste Boric 
1479f988b79SJean-Baptiste Boric 	/* ".." and reguler file. */
1489f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_lookup_by_name(fs, parent, name, &ino))) {
1499f988b79SJean-Baptiste Boric 		/* Not found. Tell this entry be able to allocate. */
1509f988b79SJean-Baptiste Boric 		if (((nameiop == CREATE) || (nameiop == RENAME)) && islastcn) {
1519f988b79SJean-Baptiste Boric 			/* Check directory permission to allocate. */
1529f988b79SJean-Baptiste Boric 			if ((error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred))) {
1539f988b79SJean-Baptiste Boric 				DPRINTF("access denied. (%s)\n", name);
1549f988b79SJean-Baptiste Boric 				return error;
1559f988b79SJean-Baptiste Boric 			}
1569f988b79SJean-Baptiste Boric 			DPRINTF("EJUSTRETURN op=%d (%s)\n", nameiop, name);
1579f988b79SJean-Baptiste Boric 			return EJUSTRETURN;
1589f988b79SJean-Baptiste Boric 		}
1599f988b79SJean-Baptiste Boric 		DPRINTF("lastcn=%d\n", flags & ISLASTCN);
1609f988b79SJean-Baptiste Boric 		return error;
1619f988b79SJean-Baptiste Boric 	}
1629f988b79SJean-Baptiste Boric 
1639f988b79SJean-Baptiste Boric 	if ((nameiop == DELETE) && islastcn) {
1649f988b79SJean-Baptiste Boric 		if ((error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred))) {
1659f988b79SJean-Baptiste Boric 			DPRINTF("access denied. (%s)\n", name);
1669f988b79SJean-Baptiste Boric 			return error;
1679f988b79SJean-Baptiste Boric 		}
1689f988b79SJean-Baptiste Boric 	}
1699f988b79SJean-Baptiste Boric 
1709f988b79SJean-Baptiste Boric 	/* Entry found. Allocate v-node */
1719f988b79SJean-Baptiste Boric 	// Check permissions?
1729f988b79SJean-Baptiste Boric 	vpp = 0;
1739f988b79SJean-Baptiste Boric 	if (isdotdot) {
1749f988b79SJean-Baptiste Boric 		VOP_UNLOCK(dvp); /* preserve reference count. (not vput) */
1759f988b79SJean-Baptiste Boric 	}
1769f988b79SJean-Baptiste Boric 	DPRINTF("enter vget\n");
1779f988b79SJean-Baptiste Boric 	if ((error = v7fs_vget(dvp->v_mount, ino, &vpp))) {
1789f988b79SJean-Baptiste Boric 		DPRINTF("***can't get vnode.\n");
1799f988b79SJean-Baptiste Boric 		return error;
1809f988b79SJean-Baptiste Boric 	}
1819f988b79SJean-Baptiste Boric 	DPRINTF("exit vget\n");
1829f988b79SJean-Baptiste Boric 	if (isdotdot) {
1839f988b79SJean-Baptiste Boric 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1849f988b79SJean-Baptiste Boric 	}
185*0a6a1f1dSLionel Sambuc 	if (vpp != dvp)
186*0a6a1f1dSLionel Sambuc 		VOP_UNLOCK(vpp);
1879f988b79SJean-Baptiste Boric 	*a->a_vpp = vpp;
1889f988b79SJean-Baptiste Boric 	DPRINTF("done.(%s)\n", name);
1899f988b79SJean-Baptiste Boric 
1909f988b79SJean-Baptiste Boric 	return 0;
1919f988b79SJean-Baptiste Boric }
1929f988b79SJean-Baptiste Boric 
1939f988b79SJean-Baptiste Boric int
v7fs_create(void * v)1949f988b79SJean-Baptiste Boric v7fs_create(void *v)
1959f988b79SJean-Baptiste Boric {
196*0a6a1f1dSLionel Sambuc 	struct vop_create_v3_args /* {
1979f988b79SJean-Baptiste Boric 				  struct vnode *a_dvp;
1989f988b79SJean-Baptiste Boric 				  struct vnode **a_vpp;
1999f988b79SJean-Baptiste Boric 				  struct componentname *a_cnp;
2009f988b79SJean-Baptiste Boric 				  struct vattr *a_vap;
2019f988b79SJean-Baptiste Boric 				  } */ *a = v;
2029f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = a->a_dvp->v_data;
2039f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
2049f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7fsmount->core;
2059f988b79SJean-Baptiste Boric 	struct mount *mp = v7fsmount->mountp;
2069f988b79SJean-Baptiste Boric 	struct v7fs_fileattr attr;
2079f988b79SJean-Baptiste Boric 	struct vattr *va = a->a_vap;
2089f988b79SJean-Baptiste Boric 	kauth_cred_t cr = a->a_cnp->cn_cred;
2099f988b79SJean-Baptiste Boric 	v7fs_ino_t ino;
2109f988b79SJean-Baptiste Boric 	int error = 0;
2119f988b79SJean-Baptiste Boric 
2129f988b79SJean-Baptiste Boric 	DPRINTF("%s parent#%d\n", a->a_cnp->cn_nameptr,
2139f988b79SJean-Baptiste Boric 	    parent_node->inode.inode_number);
2149f988b79SJean-Baptiste Boric 	KDASSERT((va->va_type == VREG) || (va->va_type == VSOCK));
2159f988b79SJean-Baptiste Boric 
2169f988b79SJean-Baptiste Boric 	memset(&attr, 0, sizeof(attr));
2179f988b79SJean-Baptiste Boric 	attr.uid = kauth_cred_geteuid(cr);
2189f988b79SJean-Baptiste Boric 	attr.gid = kauth_cred_getegid(cr);
2199f988b79SJean-Baptiste Boric 	attr.mode = va->va_mode | vtype_to_v7fs_mode (va->va_type);
2209f988b79SJean-Baptiste Boric 	attr.device = 0;
2219f988b79SJean-Baptiste Boric 
2229f988b79SJean-Baptiste Boric 	/* Allocate disk entry. and register its entry to parent directory. */
2239f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_allocate(fs, &parent_node->inode,
2249f988b79SJean-Baptiste Boric 		    a->a_cnp->cn_nameptr, &attr, &ino))) {
2259f988b79SJean-Baptiste Boric 		DPRINTF("v7fs_file_allocate failed.\n");
226*0a6a1f1dSLionel Sambuc 		return error;
2279f988b79SJean-Baptiste Boric 	}
2289f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
2299f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(a->a_dvp, v7fs_inode_filesize(&parent_node->inode));
2309f988b79SJean-Baptiste Boric 
2319f988b79SJean-Baptiste Boric 	/* Get myself vnode. */
2329f988b79SJean-Baptiste Boric 	*a->a_vpp = 0;
2339f988b79SJean-Baptiste Boric 	if ((error = v7fs_vget(mp, ino, a->a_vpp))) {
2349f988b79SJean-Baptiste Boric 		DPRINTF("v7fs_vget failed.\n");
235*0a6a1f1dSLionel Sambuc 		return error;
2369f988b79SJean-Baptiste Boric 	}
2379f988b79SJean-Baptiste Boric 
2389f988b79SJean-Baptiste Boric 	/* Scheduling update time. real update by v7fs_update */
2399f988b79SJean-Baptiste Boric 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
2409f988b79SJean-Baptiste Boric 	newnode->update_ctime = true;
2419f988b79SJean-Baptiste Boric 	newnode->update_mtime = true;
2429f988b79SJean-Baptiste Boric 	newnode->update_atime = true;
2439f988b79SJean-Baptiste Boric 	DPRINTF("allocated %s->#%d\n", a->a_cnp->cn_nameptr, ino);
2449f988b79SJean-Baptiste Boric 
245*0a6a1f1dSLionel Sambuc 	if (error == 0)
246*0a6a1f1dSLionel Sambuc 		VOP_UNLOCK(*a->a_vpp);
2479f988b79SJean-Baptiste Boric 
2489f988b79SJean-Baptiste Boric 	return error;
2499f988b79SJean-Baptiste Boric }
2509f988b79SJean-Baptiste Boric 
2519f988b79SJean-Baptiste Boric int
v7fs_mknod(void * v)2529f988b79SJean-Baptiste Boric v7fs_mknod(void *v)
2539f988b79SJean-Baptiste Boric {
254*0a6a1f1dSLionel Sambuc 	struct vop_mknod_v3_args /* {
2559f988b79SJean-Baptiste Boric 				 struct vnode		*a_dvp;
2569f988b79SJean-Baptiste Boric 				 struct vnode		**a_vpp;
2579f988b79SJean-Baptiste Boric 				 struct componentname	*a_cnp;
2589f988b79SJean-Baptiste Boric 				 struct vattr		*a_vap;
2599f988b79SJean-Baptiste Boric 				 } */ *a = v;
2609f988b79SJean-Baptiste Boric 	struct componentname *cnp = a->a_cnp;
2619f988b79SJean-Baptiste Boric 	kauth_cred_t cr = cnp->cn_cred;
2629f988b79SJean-Baptiste Boric 	struct vnode *dvp = a->a_dvp;
2639f988b79SJean-Baptiste Boric 	struct vattr *va = a->a_vap;
2649f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = dvp->v_data;
2659f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
2669f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7fsmount->core;
2679f988b79SJean-Baptiste Boric 	struct mount *mp = v7fsmount->mountp;
2689f988b79SJean-Baptiste Boric 	struct v7fs_fileattr attr;
2699f988b79SJean-Baptiste Boric 
2709f988b79SJean-Baptiste Boric 	v7fs_ino_t ino;
2719f988b79SJean-Baptiste Boric 	int error = 0;
2729f988b79SJean-Baptiste Boric 
2739f988b79SJean-Baptiste Boric 	DPRINTF("%s %06o %lx %d\n", cnp->cn_nameptr, va->va_mode,
2749f988b79SJean-Baptiste Boric 	    (long)va->va_rdev, va->va_type);
2759f988b79SJean-Baptiste Boric 	memset(&attr, 0, sizeof(attr));
2769f988b79SJean-Baptiste Boric 	attr.uid = kauth_cred_geteuid(cr);
2779f988b79SJean-Baptiste Boric 	attr.gid = kauth_cred_getegid(cr);
2789f988b79SJean-Baptiste Boric 	attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type);
2799f988b79SJean-Baptiste Boric 	attr.device = va->va_rdev;
2809f988b79SJean-Baptiste Boric 
2819f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_allocate(fs, &parent_node->inode,
2829f988b79SJean-Baptiste Boric 	    cnp->cn_nameptr, &attr, &ino)))
283*0a6a1f1dSLionel Sambuc 		return error;
2849f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
2859f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
2869f988b79SJean-Baptiste Boric 
2879f988b79SJean-Baptiste Boric 	if ((error = v7fs_vget(mp, ino, a->a_vpp))) {
2889f988b79SJean-Baptiste Boric 		DPRINTF("can't get vnode.\n");
289*0a6a1f1dSLionel Sambuc 		return error;
2909f988b79SJean-Baptiste Boric 	}
2919f988b79SJean-Baptiste Boric 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
2929f988b79SJean-Baptiste Boric 	newnode->update_ctime = true;
2939f988b79SJean-Baptiste Boric 	newnode->update_mtime = true;
2949f988b79SJean-Baptiste Boric 	newnode->update_atime = true;
2959f988b79SJean-Baptiste Boric 
296*0a6a1f1dSLionel Sambuc 	if (error == 0)
297*0a6a1f1dSLionel Sambuc 		VOP_UNLOCK(*a->a_vpp);
2989f988b79SJean-Baptiste Boric 
2999f988b79SJean-Baptiste Boric 	return error;
3009f988b79SJean-Baptiste Boric }
3019f988b79SJean-Baptiste Boric 
3029f988b79SJean-Baptiste Boric int
v7fs_open(void * v)3039f988b79SJean-Baptiste Boric v7fs_open(void *v)
3049f988b79SJean-Baptiste Boric {
3059f988b79SJean-Baptiste Boric 	struct vop_open_args /* {
3069f988b79SJean-Baptiste Boric 				struct vnode *a_vp;
3079f988b79SJean-Baptiste Boric 				int  a_mode;
3089f988b79SJean-Baptiste Boric 				kauth_cred_t a_cred;
3099f988b79SJean-Baptiste Boric 				} */ *a = v;
3109f988b79SJean-Baptiste Boric 
3119f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
3129f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
3139f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
3149f988b79SJean-Baptiste Boric 
3159f988b79SJean-Baptiste Boric 	DPRINTF("inode %d\n", inode->inode_number);
3169f988b79SJean-Baptiste Boric 	/* Append mode file pointer is managed by kernel. */
3179f988b79SJean-Baptiste Boric 	if (inode->append_mode &&
3189f988b79SJean-Baptiste Boric 	    ((a->a_mode & (FWRITE | O_APPEND)) == FWRITE)) {
3199f988b79SJean-Baptiste Boric 		DPRINTF("file is already opened by append mode.\n");
3209f988b79SJean-Baptiste Boric 		return EPERM;
3219f988b79SJean-Baptiste Boric 	}
3229f988b79SJean-Baptiste Boric 
3239f988b79SJean-Baptiste Boric 	return 0;
3249f988b79SJean-Baptiste Boric }
3259f988b79SJean-Baptiste Boric 
3269f988b79SJean-Baptiste Boric int
v7fs_close(void * v)3279f988b79SJean-Baptiste Boric v7fs_close(void *v)
3289f988b79SJean-Baptiste Boric {
3299f988b79SJean-Baptiste Boric 	struct vop_close_args /* {
3309f988b79SJean-Baptiste Boric 				 struct vnodeop_desc *a_desc;
3319f988b79SJean-Baptiste Boric 				 struct vnode *a_vp;
3329f988b79SJean-Baptiste Boric 				 int  a_fflag;
3339f988b79SJean-Baptiste Boric 				 kauth_cred_t a_cred;
3349f988b79SJean-Baptiste Boric 				 } */ *a = v;
3359f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
3369f988b79SJean-Baptiste Boric #ifdef V7FS_VNOPS_DEBUG
3379f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
3389f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
3399f988b79SJean-Baptiste Boric #endif
3409f988b79SJean-Baptiste Boric 	DPRINTF("#%d (i)%dbyte (v)%zubyte\n", inode->inode_number,
3419f988b79SJean-Baptiste Boric 	    v7fs_inode_filesize(inode), vp->v_size);
3429f988b79SJean-Baptiste Boric 
3439f988b79SJean-Baptiste Boric 	/* Update timestamp */
3449f988b79SJean-Baptiste Boric 	v7fs_update(vp, 0, 0, UPDATE_WAIT);
3459f988b79SJean-Baptiste Boric 
3469f988b79SJean-Baptiste Boric 	return 0;
3479f988b79SJean-Baptiste Boric }
3489f988b79SJean-Baptiste Boric 
3499f988b79SJean-Baptiste Boric static int
v7fs_check_possible(struct vnode * vp,struct v7fs_node * v7node,mode_t mode)3509f988b79SJean-Baptiste Boric v7fs_check_possible(struct vnode *vp, struct v7fs_node *v7node,
3519f988b79SJean-Baptiste Boric     mode_t mode)
3529f988b79SJean-Baptiste Boric {
3539f988b79SJean-Baptiste Boric 
3549f988b79SJean-Baptiste Boric 	if (!(mode & VWRITE))
3559f988b79SJean-Baptiste Boric 	  return 0;
3569f988b79SJean-Baptiste Boric 
3579f988b79SJean-Baptiste Boric 	switch (vp->v_type) {
3589f988b79SJean-Baptiste Boric 	default:
3599f988b79SJean-Baptiste Boric 		/*  special file is always writable. */
3609f988b79SJean-Baptiste Boric 		return 0;
3619f988b79SJean-Baptiste Boric 	case VDIR:
3629f988b79SJean-Baptiste Boric 	case VLNK:
3639f988b79SJean-Baptiste Boric 	case VREG:
3649f988b79SJean-Baptiste Boric 		break;
3659f988b79SJean-Baptiste Boric 	}
3669f988b79SJean-Baptiste Boric 
3679f988b79SJean-Baptiste Boric 	return vp->v_mount->mnt_flag & MNT_RDONLY ? EROFS : 0;
3689f988b79SJean-Baptiste Boric }
3699f988b79SJean-Baptiste Boric 
3709f988b79SJean-Baptiste Boric static int
v7fs_check_permitted(struct vnode * vp,struct v7fs_node * v7node,mode_t mode,kauth_cred_t cred)3719f988b79SJean-Baptiste Boric v7fs_check_permitted(struct vnode *vp, struct v7fs_node *v7node,
3729f988b79SJean-Baptiste Boric     mode_t mode, kauth_cred_t cred)
3739f988b79SJean-Baptiste Boric {
3749f988b79SJean-Baptiste Boric 
3759f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
3769f988b79SJean-Baptiste Boric 
3779f988b79SJean-Baptiste Boric 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
3789f988b79SJean-Baptiste Boric 	    vp->v_type, inode->mode), vp, NULL, genfs_can_access(vp->v_type,
3799f988b79SJean-Baptiste Boric 	    inode->mode, inode->uid, inode->gid, mode, cred));
3809f988b79SJean-Baptiste Boric }
3819f988b79SJean-Baptiste Boric 
3829f988b79SJean-Baptiste Boric int
v7fs_access(void * v)3839f988b79SJean-Baptiste Boric v7fs_access(void *v)
3849f988b79SJean-Baptiste Boric {
3859f988b79SJean-Baptiste Boric 	struct vop_access_args /* {
3869f988b79SJean-Baptiste Boric 				  struct vnode	*a_vp;
3879f988b79SJean-Baptiste Boric 				  int		a_mode;
3889f988b79SJean-Baptiste Boric 				  kauth_cred_t	a_cred;
3899f988b79SJean-Baptiste Boric 				  } */ *ap = v;
3909f988b79SJean-Baptiste Boric 	struct vnode *vp = ap->a_vp;
3919f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
3929f988b79SJean-Baptiste Boric 	int error;
3939f988b79SJean-Baptiste Boric 
3949f988b79SJean-Baptiste Boric 	error = v7fs_check_possible(vp, v7node, ap->a_mode);
3959f988b79SJean-Baptiste Boric 	if (error)
3969f988b79SJean-Baptiste Boric 		return error;
3979f988b79SJean-Baptiste Boric 
3989f988b79SJean-Baptiste Boric 	error = v7fs_check_permitted(vp, v7node, ap->a_mode, ap->a_cred);
3999f988b79SJean-Baptiste Boric 
4009f988b79SJean-Baptiste Boric 	return error;
4019f988b79SJean-Baptiste Boric }
4029f988b79SJean-Baptiste Boric 
4039f988b79SJean-Baptiste Boric int
v7fs_getattr(void * v)4049f988b79SJean-Baptiste Boric v7fs_getattr(void *v)
4059f988b79SJean-Baptiste Boric {
4069f988b79SJean-Baptiste Boric 	struct vop_getattr_args /* {
4079f988b79SJean-Baptiste Boric 				   struct vnode *a_vp;
4089f988b79SJean-Baptiste Boric 				   struct vattr *a_vap;
4099f988b79SJean-Baptiste Boric 				   kauth_cred_t a_cred;
4109f988b79SJean-Baptiste Boric 				   } */ *ap = v;
4119f988b79SJean-Baptiste Boric 	struct vnode *vp = ap->a_vp;
4129f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
4139f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
4149f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = v7node->v7fsmount;
4159f988b79SJean-Baptiste Boric 	struct vattr *vap = ap->a_vap;
4169f988b79SJean-Baptiste Boric 
4179f988b79SJean-Baptiste Boric 	DPRINTF("\n");
4189f988b79SJean-Baptiste Boric 	vap->va_type = vp->v_type;
4199f988b79SJean-Baptiste Boric 	vap->va_mode = inode->mode;
4209f988b79SJean-Baptiste Boric 	vap->va_nlink = inode->nlink;
4219f988b79SJean-Baptiste Boric 	vap->va_uid = inode->uid;
4229f988b79SJean-Baptiste Boric 	vap->va_gid = inode->gid;
4239f988b79SJean-Baptiste Boric 	vap->va_fsid = v7fsmount->devvp->v_rdev;
4249f988b79SJean-Baptiste Boric 	vap->va_fileid = inode->inode_number;
4259f988b79SJean-Baptiste Boric 	vap->va_size = vp->v_size;
426*0a6a1f1dSLionel Sambuc 	if (vp->v_type == VLNK) {
427*0a6a1f1dSLionel Sambuc 		/* Ajust for trailing NUL. */
428*0a6a1f1dSLionel Sambuc 		KASSERT(vap->va_size > 0);
429*0a6a1f1dSLionel Sambuc 		vap->va_size -= 1;
430*0a6a1f1dSLionel Sambuc 	}
4319f988b79SJean-Baptiste Boric 	vap->va_atime.tv_sec = inode->atime;
4329f988b79SJean-Baptiste Boric 	vap->va_mtime.tv_sec = inode->mtime;
4339f988b79SJean-Baptiste Boric 	vap->va_ctime.tv_sec = inode->ctime;
4349f988b79SJean-Baptiste Boric 	vap->va_birthtime.tv_sec = 0;
4359f988b79SJean-Baptiste Boric 	vap->va_gen = 1;
4369f988b79SJean-Baptiste Boric 	vap->va_flags = inode->append_mode ? SF_APPEND : 0;
4379f988b79SJean-Baptiste Boric 	vap->va_rdev = inode->device;
4389f988b79SJean-Baptiste Boric 	vap->va_bytes = vap->va_size; /* No sparse support. */
4399f988b79SJean-Baptiste Boric 	vap->va_filerev = 0;
4409f988b79SJean-Baptiste Boric 	vap->va_vaflags = 0;
4419f988b79SJean-Baptiste Boric 	/* PAGE_SIZE is larger than sizeof(struct dirent). OK.
4429f988b79SJean-Baptiste Boric 	   getcwd_scandir()@vfs_getcwd.c */
4439f988b79SJean-Baptiste Boric 	vap->va_blocksize = PAGE_SIZE;
4449f988b79SJean-Baptiste Boric 
4459f988b79SJean-Baptiste Boric 	return 0;
4469f988b79SJean-Baptiste Boric }
4479f988b79SJean-Baptiste Boric 
4489f988b79SJean-Baptiste Boric int
v7fs_setattr(void * v)4499f988b79SJean-Baptiste Boric v7fs_setattr(void *v)
4509f988b79SJean-Baptiste Boric {
4519f988b79SJean-Baptiste Boric 	struct vop_setattr_args /* {
4529f988b79SJean-Baptiste Boric 				   struct vnode *a_vp;
4539f988b79SJean-Baptiste Boric 				   struct vattr *a_vap;
4549f988b79SJean-Baptiste Boric 				   kauth_cred_t a_cred;
4559f988b79SJean-Baptiste Boric 				   struct proc *p;
4569f988b79SJean-Baptiste Boric 				   } */ *ap = v;
4579f988b79SJean-Baptiste Boric 	struct vnode *vp = ap->a_vp;
4589f988b79SJean-Baptiste Boric 	struct vattr *vap = ap->a_vap;
4599f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
4609f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
4619f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
4629f988b79SJean-Baptiste Boric 	kauth_cred_t cred = ap->a_cred;
4639f988b79SJean-Baptiste Boric 	struct timespec *acc, *mod;
4649f988b79SJean-Baptiste Boric 	int error = 0;
4659f988b79SJean-Baptiste Boric 	acc = mod = NULL;
4669f988b79SJean-Baptiste Boric 
4679f988b79SJean-Baptiste Boric 	DPRINTF("\n");
4689f988b79SJean-Baptiste Boric 
4699f988b79SJean-Baptiste Boric 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
4709f988b79SJean-Baptiste Boric 		switch (vp->v_type) {
4719f988b79SJean-Baptiste Boric 		default:
4729f988b79SJean-Baptiste Boric 			/*  special file is always writable. */
4739f988b79SJean-Baptiste Boric 			break;
4749f988b79SJean-Baptiste Boric 		case VDIR:
4759f988b79SJean-Baptiste Boric 		case VLNK:
4769f988b79SJean-Baptiste Boric 		case VREG:
4779f988b79SJean-Baptiste Boric 			DPRINTF("read-only mount\n");
4789f988b79SJean-Baptiste Boric 			return EROFS;
4799f988b79SJean-Baptiste Boric 		}
4809f988b79SJean-Baptiste Boric 	}
4819f988b79SJean-Baptiste Boric 
4829f988b79SJean-Baptiste Boric 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
4839f988b79SJean-Baptiste Boric 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
4849f988b79SJean-Baptiste Boric 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
4859f988b79SJean-Baptiste Boric 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
4869f988b79SJean-Baptiste Boric 		DPRINTF("invalid request\n");
4879f988b79SJean-Baptiste Boric 		return EINVAL;
4889f988b79SJean-Baptiste Boric 	}
4899f988b79SJean-Baptiste Boric 	/* File pointer mode. */
4909f988b79SJean-Baptiste Boric 	if (vap->va_flags != VNOVAL) {
4919f988b79SJean-Baptiste Boric 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS,
4929f988b79SJean-Baptiste Boric 		    vp, NULL, genfs_can_chflags(cred, vp->v_type, inode->uid,
4939f988b79SJean-Baptiste Boric 		    false));
4949f988b79SJean-Baptiste Boric 		if (error)
4959f988b79SJean-Baptiste Boric 			return error;
4969f988b79SJean-Baptiste Boric 		inode->append_mode = vap->va_flags & SF_APPEND;
4979f988b79SJean-Baptiste Boric 	}
4989f988b79SJean-Baptiste Boric 
4999f988b79SJean-Baptiste Boric 	/* File size change. */
5009f988b79SJean-Baptiste Boric 	if ((vap->va_size != VNOVAL) && (vp->v_type == VREG)) {
5019f988b79SJean-Baptiste Boric 		error = v7fs_datablock_size_change(fs, vap->va_size, inode);
5029f988b79SJean-Baptiste Boric 		if (error == 0)
5039f988b79SJean-Baptiste Boric 			uvm_vnp_setsize(vp, vap->va_size);
5049f988b79SJean-Baptiste Boric 	}
5059f988b79SJean-Baptiste Boric 	uid_t uid = inode->uid;
5069f988b79SJean-Baptiste Boric 	gid_t gid = inode->gid;
5079f988b79SJean-Baptiste Boric 
5089f988b79SJean-Baptiste Boric 	if (vap->va_uid != (uid_t)VNOVAL) {
5099f988b79SJean-Baptiste Boric 		uid = vap->va_uid;
5109f988b79SJean-Baptiste Boric 		error = kauth_authorize_vnode(cred,
5119f988b79SJean-Baptiste Boric 		    KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL,
5129f988b79SJean-Baptiste Boric 		    genfs_can_chown(cred, inode->uid, inode->gid, uid,
5139f988b79SJean-Baptiste Boric 		    gid));
5149f988b79SJean-Baptiste Boric 		if (error)
5159f988b79SJean-Baptiste Boric 			return error;
5169f988b79SJean-Baptiste Boric 		inode->uid = uid;
5179f988b79SJean-Baptiste Boric 	}
5189f988b79SJean-Baptiste Boric 	if (vap->va_gid != (uid_t)VNOVAL) {
5199f988b79SJean-Baptiste Boric 		gid = vap->va_gid;
5209f988b79SJean-Baptiste Boric 		error = kauth_authorize_vnode(cred,
5219f988b79SJean-Baptiste Boric 		    KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL,
5229f988b79SJean-Baptiste Boric 		    genfs_can_chown(cred, inode->uid, inode->gid, uid,
5239f988b79SJean-Baptiste Boric 		    gid));
5249f988b79SJean-Baptiste Boric 		if (error)
5259f988b79SJean-Baptiste Boric 			return error;
5269f988b79SJean-Baptiste Boric 		inode->gid = gid;
5279f988b79SJean-Baptiste Boric 	}
5289f988b79SJean-Baptiste Boric 	if (vap->va_mode != (mode_t)VNOVAL) {
5299f988b79SJean-Baptiste Boric 		mode_t mode = vap->va_mode;
5309f988b79SJean-Baptiste Boric 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY,
5319f988b79SJean-Baptiste Boric 		    vp, NULL, genfs_can_chmod(vp->v_type, cred, inode->uid, inode->gid,
5329f988b79SJean-Baptiste Boric 		    mode));
5339f988b79SJean-Baptiste Boric 		if (error) {
5349f988b79SJean-Baptiste Boric 			return error;
5359f988b79SJean-Baptiste Boric 		}
5369f988b79SJean-Baptiste Boric 		v7fs_inode_chmod(inode, mode);
5379f988b79SJean-Baptiste Boric 	}
5389f988b79SJean-Baptiste Boric 	if ((vap->va_atime.tv_sec != VNOVAL) ||
5399f988b79SJean-Baptiste Boric 	    (vap->va_mtime.tv_sec != VNOVAL) ||
5409f988b79SJean-Baptiste Boric 	    (vap->va_ctime.tv_sec != VNOVAL)) {
5419f988b79SJean-Baptiste Boric 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
5429f988b79SJean-Baptiste Boric 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, inode->uid,
5439f988b79SJean-Baptiste Boric 		    cred));
5449f988b79SJean-Baptiste Boric 		if (error)
5459f988b79SJean-Baptiste Boric 			return error;
5469f988b79SJean-Baptiste Boric 
5479f988b79SJean-Baptiste Boric 		if (vap->va_atime.tv_sec != VNOVAL) {
5489f988b79SJean-Baptiste Boric 			acc = &vap->va_atime;
5499f988b79SJean-Baptiste Boric 		}
5509f988b79SJean-Baptiste Boric 		if (vap->va_mtime.tv_sec != VNOVAL) {
5519f988b79SJean-Baptiste Boric 			mod = &vap->va_mtime;
5529f988b79SJean-Baptiste Boric 			v7node->update_mtime = true;
5539f988b79SJean-Baptiste Boric 		}
5549f988b79SJean-Baptiste Boric 		if (vap->va_ctime.tv_sec != VNOVAL) {
5559f988b79SJean-Baptiste Boric 			v7node->update_ctime = true;
5569f988b79SJean-Baptiste Boric 		}
5579f988b79SJean-Baptiste Boric 	}
5589f988b79SJean-Baptiste Boric 
5599f988b79SJean-Baptiste Boric 	v7node->update_atime = true;
5609f988b79SJean-Baptiste Boric 	v7fs_update(vp, acc, mod, 0);
5619f988b79SJean-Baptiste Boric 
5629f988b79SJean-Baptiste Boric 	return error;
5639f988b79SJean-Baptiste Boric }
5649f988b79SJean-Baptiste Boric 
5659f988b79SJean-Baptiste Boric int
v7fs_read(void * v)5669f988b79SJean-Baptiste Boric v7fs_read(void *v)
5679f988b79SJean-Baptiste Boric {
5689f988b79SJean-Baptiste Boric 	struct vop_read_args /* {
5699f988b79SJean-Baptiste Boric 				struct vnode *a_vp;
5709f988b79SJean-Baptiste Boric 				struct uio *a_uio;
5719f988b79SJean-Baptiste Boric 				int a_ioflag;
5729f988b79SJean-Baptiste Boric 				kauth_cred_t a_cred;
5739f988b79SJean-Baptiste Boric 				} */ *a = v;
5749f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
5759f988b79SJean-Baptiste Boric 	struct uio *uio = a->a_uio;
5769f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
5779f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
5789f988b79SJean-Baptiste Boric 	vsize_t sz, filesz = v7fs_inode_filesize(inode);
5799f988b79SJean-Baptiste Boric 	const int advice = IO_ADV_DECODE(a->a_ioflag);
5809f988b79SJean-Baptiste Boric 	int error = 0;
5819f988b79SJean-Baptiste Boric 
5829f988b79SJean-Baptiste Boric 	DPRINTF("type=%d inode=%d\n", vp->v_type, v7node->inode.inode_number);
5839f988b79SJean-Baptiste Boric 
5849f988b79SJean-Baptiste Boric 	while (uio->uio_resid > 0) {
5859f988b79SJean-Baptiste Boric 		if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0)
5869f988b79SJean-Baptiste Boric 			break;
5879f988b79SJean-Baptiste Boric 
5889f988b79SJean-Baptiste Boric 		error = ubc_uiomove(&vp->v_uobj, uio, sz, advice, UBC_READ |
5899f988b79SJean-Baptiste Boric 		    UBC_PARTIALOK | UBC_UNMAP_FLAG(v));
5909f988b79SJean-Baptiste Boric 		if (error) {
5919f988b79SJean-Baptiste Boric 			break;
5929f988b79SJean-Baptiste Boric 		}
5939f988b79SJean-Baptiste Boric 		DPRINTF("read %zubyte\n", sz);
5949f988b79SJean-Baptiste Boric 	}
5959f988b79SJean-Baptiste Boric 	v7node->update_atime = true;
5969f988b79SJean-Baptiste Boric 
5979f988b79SJean-Baptiste Boric 	return error;
5989f988b79SJean-Baptiste Boric }
5999f988b79SJean-Baptiste Boric 
6009f988b79SJean-Baptiste Boric int
v7fs_write(void * v)6019f988b79SJean-Baptiste Boric v7fs_write(void *v)
6029f988b79SJean-Baptiste Boric {
6039f988b79SJean-Baptiste Boric 	struct vop_write_args /* {
6049f988b79SJean-Baptiste Boric 				 struct vnode *a_vp;
6059f988b79SJean-Baptiste Boric 				 struct uio *a_uio;
6069f988b79SJean-Baptiste Boric 				 int  a_ioflag;
6079f988b79SJean-Baptiste Boric 				 kauth_cred_t a_cred;
6089f988b79SJean-Baptiste Boric 				 } */ *a = v;
6099f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
6109f988b79SJean-Baptiste Boric 	struct uio *uio = a->a_uio;
6119f988b79SJean-Baptiste Boric 	int advice = IO_ADV_DECODE(a->a_ioflag);
6129f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
6139f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
6149f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
6159f988b79SJean-Baptiste Boric 	vsize_t sz;
6169f988b79SJean-Baptiste Boric 	int error = 0;
6179f988b79SJean-Baptiste Boric 
6189f988b79SJean-Baptiste Boric 	if (uio->uio_resid == 0)
6199f988b79SJean-Baptiste Boric 		return 0;
6209f988b79SJean-Baptiste Boric 
6219f988b79SJean-Baptiste Boric 	sz = v7fs_inode_filesize(inode);
6229f988b79SJean-Baptiste Boric 	DPRINTF("(i)%ld (v)%zu ofs=%zu + res=%zu = %zu\n", sz, vp->v_size,
6239f988b79SJean-Baptiste Boric 	    uio->uio_offset, uio->uio_resid, uio->uio_offset + uio->uio_resid);
6249f988b79SJean-Baptiste Boric 
6259f988b79SJean-Baptiste Boric 	/* Append mode file offset is managed by kernel. */
6269f988b79SJean-Baptiste Boric 	if (a->a_ioflag & IO_APPEND)
6279f988b79SJean-Baptiste Boric 		uio->uio_offset = sz;
6289f988b79SJean-Baptiste Boric 
6299f988b79SJean-Baptiste Boric 	/* If write region is over filesize, expand. */
6309f988b79SJean-Baptiste Boric 	size_t newsize= uio->uio_offset + uio->uio_resid;
6319f988b79SJean-Baptiste Boric 	ssize_t expand = newsize - sz;
6329f988b79SJean-Baptiste Boric  	if (expand > 0) {
6339f988b79SJean-Baptiste Boric 		if ((error = v7fs_datablock_expand(fs, inode, expand)))
6349f988b79SJean-Baptiste Boric 			return error;
6359f988b79SJean-Baptiste Boric 		uvm_vnp_setsize(vp, newsize);
6369f988b79SJean-Baptiste Boric 	}
6379f988b79SJean-Baptiste Boric 
6389f988b79SJean-Baptiste Boric 	while (uio->uio_resid > 0) {
6399f988b79SJean-Baptiste Boric 		sz = uio->uio_resid;
6409f988b79SJean-Baptiste Boric 		if ((error = ubc_uiomove(&vp->v_uobj, uio, sz, advice,
6419f988b79SJean-Baptiste Boric 			    UBC_WRITE | UBC_UNMAP_FLAG(v))))
6429f988b79SJean-Baptiste Boric 			break;
6439f988b79SJean-Baptiste Boric 		DPRINTF("write %zubyte\n", sz);
6449f988b79SJean-Baptiste Boric 	}
6459f988b79SJean-Baptiste Boric 	v7node->update_mtime = true;
6469f988b79SJean-Baptiste Boric 
6479f988b79SJean-Baptiste Boric 	return error;
6489f988b79SJean-Baptiste Boric }
6499f988b79SJean-Baptiste Boric 
6509f988b79SJean-Baptiste Boric int
v7fs_fsync(void * v)6519f988b79SJean-Baptiste Boric v7fs_fsync(void *v)
6529f988b79SJean-Baptiste Boric {
6539f988b79SJean-Baptiste Boric 	struct vop_fsync_args /* {
6549f988b79SJean-Baptiste Boric 				 struct vnode *a_vp;
6559f988b79SJean-Baptiste Boric 				 kauth_cred_t a_cred;
6569f988b79SJean-Baptiste Boric 				 int a_flags;
6579f988b79SJean-Baptiste Boric 				 off_t offlo;
6589f988b79SJean-Baptiste Boric 				 off_t offhi;
6599f988b79SJean-Baptiste Boric 				 } */ *a = v;
6609f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
6619f988b79SJean-Baptiste Boric 	int error, wait;
6629f988b79SJean-Baptiste Boric 
6639f988b79SJean-Baptiste Boric 	DPRINTF("%p\n", a->a_vp);
6649f988b79SJean-Baptiste Boric 	if (a->a_flags & FSYNC_CACHE) {
6659f988b79SJean-Baptiste Boric 		return EOPNOTSUPP;
6669f988b79SJean-Baptiste Boric 	}
6679f988b79SJean-Baptiste Boric 
6689f988b79SJean-Baptiste Boric 	wait = (a->a_flags & FSYNC_WAIT);
6699f988b79SJean-Baptiste Boric 	error = vflushbuf(vp, a->a_flags);
6709f988b79SJean-Baptiste Boric 
6719f988b79SJean-Baptiste Boric 	if (error == 0 && (a->a_flags & FSYNC_DATAONLY) == 0)
6729f988b79SJean-Baptiste Boric 		error = v7fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
6739f988b79SJean-Baptiste Boric 
6749f988b79SJean-Baptiste Boric 	return error;
6759f988b79SJean-Baptiste Boric }
6769f988b79SJean-Baptiste Boric 
6779f988b79SJean-Baptiste Boric int
v7fs_remove(void * v)6789f988b79SJean-Baptiste Boric v7fs_remove(void *v)
6799f988b79SJean-Baptiste Boric {
6809f988b79SJean-Baptiste Boric 	struct vop_remove_args /* {
6819f988b79SJean-Baptiste Boric 				  struct vnodeop_desc *a_desc;
6829f988b79SJean-Baptiste Boric 				  struct vnode * a_dvp;
6839f988b79SJean-Baptiste Boric 				  struct vnode * a_vp;
6849f988b79SJean-Baptiste Boric 				  struct componentname * a_cnp;
6859f988b79SJean-Baptiste Boric 				  } */ *a = v;
6869f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = a->a_dvp->v_data;
6879f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
6889f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
6899f988b79SJean-Baptiste Boric 	struct vnode *dvp = a->a_dvp;
690*0a6a1f1dSLionel Sambuc 	struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode;
6919f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7fsmount->core;
6929f988b79SJean-Baptiste Boric 	int error = 0;
6939f988b79SJean-Baptiste Boric 
6949f988b79SJean-Baptiste Boric 	DPRINTF("delete %s\n", a->a_cnp->cn_nameptr);
6959f988b79SJean-Baptiste Boric 
6969f988b79SJean-Baptiste Boric 	if (vp->v_type == VDIR) {
6979f988b79SJean-Baptiste Boric 		error = EPERM;
6989f988b79SJean-Baptiste Boric 		goto out;
6999f988b79SJean-Baptiste Boric 	}
7009f988b79SJean-Baptiste Boric 
7019f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_deallocate(fs, &parent_node->inode,
7029f988b79SJean-Baptiste Boric 		    a->a_cnp->cn_nameptr))) {
7039f988b79SJean-Baptiste Boric 		DPRINTF("v7fs_file_delete failed.\n");
7049f988b79SJean-Baptiste Boric 		goto out;
7059f988b79SJean-Baptiste Boric 	}
706*0a6a1f1dSLionel Sambuc 	error = v7fs_inode_load(fs, inode, inode->inode_number);
707*0a6a1f1dSLionel Sambuc 	if (error)
708*0a6a1f1dSLionel Sambuc 		goto out;
7099f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
7109f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
7119f988b79SJean-Baptiste Boric 
7129f988b79SJean-Baptiste Boric out:
7139f988b79SJean-Baptiste Boric 	if (dvp == vp)
7149f988b79SJean-Baptiste Boric 		vrele(vp); /* v_usecount-- of unlocked vp */
7159f988b79SJean-Baptiste Boric 	else
7169f988b79SJean-Baptiste Boric 		vput(vp); /* unlock vp and then v_usecount-- */
7179f988b79SJean-Baptiste Boric 	vput(dvp);
7189f988b79SJean-Baptiste Boric 
7199f988b79SJean-Baptiste Boric 	return error;
7209f988b79SJean-Baptiste Boric }
7219f988b79SJean-Baptiste Boric 
7229f988b79SJean-Baptiste Boric int
v7fs_link(void * v)7239f988b79SJean-Baptiste Boric v7fs_link(void *v)
7249f988b79SJean-Baptiste Boric {
725*0a6a1f1dSLionel Sambuc 	struct vop_link_v2_args /* {
7269f988b79SJean-Baptiste Boric 				struct vnode *a_dvp;
7279f988b79SJean-Baptiste Boric 				struct vnode *a_vp;
7289f988b79SJean-Baptiste Boric 				struct componentname *a_cnp;
7299f988b79SJean-Baptiste Boric 				} */ *a = v;
7309f988b79SJean-Baptiste Boric 	struct vnode *dvp = a->a_dvp;
7319f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
7329f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = dvp->v_data;
7339f988b79SJean-Baptiste Boric 	struct v7fs_node *node = vp->v_data;
7349f988b79SJean-Baptiste Boric 	struct v7fs_inode *parent = &parent_node->inode;
7359f988b79SJean-Baptiste Boric 	struct v7fs_inode *p = &node->inode;
7369f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = node->v7fsmount->core;
7379f988b79SJean-Baptiste Boric 	struct componentname *cnp = a->a_cnp;
7389f988b79SJean-Baptiste Boric 	int error = 0;
7399f988b79SJean-Baptiste Boric 
7409f988b79SJean-Baptiste Boric 	DPRINTF("%p\n", vp);
7419f988b79SJean-Baptiste Boric 	/* Lock soruce file */
7429f988b79SJean-Baptiste Boric 	if ((error = vn_lock(vp, LK_EXCLUSIVE))) {
7439f988b79SJean-Baptiste Boric 		DPRINTF("lock failed. %p\n", vp);
7449f988b79SJean-Baptiste Boric 		VOP_ABORTOP(dvp, cnp);
7459f988b79SJean-Baptiste Boric 		goto unlock;
7469f988b79SJean-Baptiste Boric 	}
7479f988b79SJean-Baptiste Boric 	error = v7fs_file_link(fs, parent, p, cnp->cn_nameptr);
7489f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
7499f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
7509f988b79SJean-Baptiste Boric 
7519f988b79SJean-Baptiste Boric 	VOP_UNLOCK(vp);
7529f988b79SJean-Baptiste Boric unlock:
7539f988b79SJean-Baptiste Boric 	return error;
7549f988b79SJean-Baptiste Boric }
7559f988b79SJean-Baptiste Boric 
7569f988b79SJean-Baptiste Boric int
v7fs_rename(void * v)7579f988b79SJean-Baptiste Boric v7fs_rename(void *v)
7589f988b79SJean-Baptiste Boric {
7599f988b79SJean-Baptiste Boric 	struct vop_rename_args /* {
7609f988b79SJean-Baptiste Boric 				  struct vnode *a_fdvp;	from parent-directory
7619f988b79SJean-Baptiste Boric 				  struct vnode *a_fvp;	from file
7629f988b79SJean-Baptiste Boric 				  struct componentname *a_fcnp;
7639f988b79SJean-Baptiste Boric 				  struct vnode *a_tdvp;	to parent-directory
7649f988b79SJean-Baptiste Boric 				  struct vnode *a_tvp;	to file
7659f988b79SJean-Baptiste Boric 				  struct componentname *a_tcnp;
7669f988b79SJean-Baptiste Boric 				  } */ *a = v;
7679f988b79SJean-Baptiste Boric 	struct vnode *fvp = a->a_fvp;
7689f988b79SJean-Baptiste Boric 	struct vnode *tvp = a->a_tvp;
7699f988b79SJean-Baptiste Boric 	struct vnode *fdvp = a->a_fdvp;
7709f988b79SJean-Baptiste Boric 	struct vnode *tdvp = a->a_tdvp;
7719f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_from = fdvp->v_data;
7729f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_to = tdvp->v_data;
7739f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = fvp->v_data;
7749f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
7759f988b79SJean-Baptiste Boric 	const char *from_name = a->a_fcnp->cn_nameptr;
7769f988b79SJean-Baptiste Boric 	const char *to_name = a->a_tcnp->cn_nameptr;
7779f988b79SJean-Baptiste Boric 	int error;
7789f988b79SJean-Baptiste Boric 
7799f988b79SJean-Baptiste Boric 	DPRINTF("%s->%s %p %p\n", from_name, to_name, fvp, tvp);
7809f988b79SJean-Baptiste Boric 
7819f988b79SJean-Baptiste Boric 	if ((fvp->v_mount != tdvp->v_mount) ||
7829f988b79SJean-Baptiste Boric 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
7839f988b79SJean-Baptiste Boric 		error = EXDEV;
7849f988b79SJean-Baptiste Boric 		DPRINTF("cross-device link\n");
7859f988b79SJean-Baptiste Boric 		goto out;
7869f988b79SJean-Baptiste Boric 	}
7879f988b79SJean-Baptiste Boric 	// XXXsource file lock?
7889f988b79SJean-Baptiste Boric 	error = v7fs_file_rename(fs, &parent_from->inode, from_name,
7899f988b79SJean-Baptiste Boric 	    &parent_to->inode, to_name);
7909f988b79SJean-Baptiste Boric 	/* 'to file' inode may be changed. (hard-linked and it is cached.)
7919f988b79SJean-Baptiste Boric 	   t_vnops rename_reg_nodir */
792*0a6a1f1dSLionel Sambuc 	if (error == 0 && tvp) {
793*0a6a1f1dSLionel Sambuc 		struct v7fs_inode *inode =
794*0a6a1f1dSLionel Sambuc 		    &((struct v7fs_node *)tvp->v_data)->inode;
795*0a6a1f1dSLionel Sambuc 
796*0a6a1f1dSLionel Sambuc 		error = v7fs_inode_load(fs, inode, inode->inode_number);
797*0a6a1f1dSLionel Sambuc 		uvm_vnp_setsize(tvp, v7fs_inode_filesize(inode));
7989f988b79SJean-Baptiste Boric 	}
7999f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
8009f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(tdvp, v7fs_inode_filesize(&parent_to->inode));
8019f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(fdvp, v7fs_inode_filesize(&parent_from->inode));
8029f988b79SJean-Baptiste Boric out:
8039f988b79SJean-Baptiste Boric 	if (tvp)
8049f988b79SJean-Baptiste Boric 		vput(tvp);  /* locked on entry */
8059f988b79SJean-Baptiste Boric 	if (tdvp == tvp)
8069f988b79SJean-Baptiste Boric 		vrele(tdvp);
8079f988b79SJean-Baptiste Boric 	else
8089f988b79SJean-Baptiste Boric 		vput(tdvp);
8099f988b79SJean-Baptiste Boric 	vrele(fdvp);
8109f988b79SJean-Baptiste Boric 	vrele(fvp);
8119f988b79SJean-Baptiste Boric 
8129f988b79SJean-Baptiste Boric 	return error;
8139f988b79SJean-Baptiste Boric }
8149f988b79SJean-Baptiste Boric 
8159f988b79SJean-Baptiste Boric int
v7fs_mkdir(void * v)8169f988b79SJean-Baptiste Boric v7fs_mkdir(void *v)
8179f988b79SJean-Baptiste Boric {
818*0a6a1f1dSLionel Sambuc 	struct vop_mkdir_v3_args /* {
8199f988b79SJean-Baptiste Boric 				 struct vnode		*a_dvp;
8209f988b79SJean-Baptiste Boric 				 struct vnode		**a_vpp;
8219f988b79SJean-Baptiste Boric 				 struct componentname	*a_cnp;
8229f988b79SJean-Baptiste Boric 				 struct vattr		*a_vap;
8239f988b79SJean-Baptiste Boric 				 } */ *a = v;
8249f988b79SJean-Baptiste Boric 	struct componentname *cnp = a->a_cnp;
8259f988b79SJean-Baptiste Boric 	kauth_cred_t cr = cnp->cn_cred;
8269f988b79SJean-Baptiste Boric 	struct vnode *dvp = a->a_dvp;
8279f988b79SJean-Baptiste Boric 	struct vattr *va = a->a_vap;
8289f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = dvp->v_data;
8299f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
8309f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7fsmount->core;
8319f988b79SJean-Baptiste Boric 	struct v7fs_fileattr attr;
8329f988b79SJean-Baptiste Boric 	struct mount *mp = v7fsmount->mountp;
8339f988b79SJean-Baptiste Boric 	v7fs_ino_t ino;
8349f988b79SJean-Baptiste Boric 	int error = 0;
8359f988b79SJean-Baptiste Boric 
8369f988b79SJean-Baptiste Boric 	DPRINTF("\n");
8379f988b79SJean-Baptiste Boric 	memset(&attr, 0, sizeof(attr));
8389f988b79SJean-Baptiste Boric 	attr.uid = kauth_cred_geteuid(cr);
8399f988b79SJean-Baptiste Boric 	attr.gid = kauth_cred_getegid(cr);
8409f988b79SJean-Baptiste Boric 	attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type);
8419f988b79SJean-Baptiste Boric 
8429f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_allocate(fs, &parent_node->inode,
8439f988b79SJean-Baptiste Boric 	    cnp->cn_nameptr, &attr, &ino)))
844*0a6a1f1dSLionel Sambuc 		return error;
8459f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
8469f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
8479f988b79SJean-Baptiste Boric 
8489f988b79SJean-Baptiste Boric 	if ((error = v7fs_vget(mp, ino, a->a_vpp))) {
8499f988b79SJean-Baptiste Boric 		DPRINTF("can't get vnode.\n");
8509f988b79SJean-Baptiste Boric 	}
8519f988b79SJean-Baptiste Boric 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
8529f988b79SJean-Baptiste Boric 	newnode->update_ctime = true;
8539f988b79SJean-Baptiste Boric 	newnode->update_mtime = true;
8549f988b79SJean-Baptiste Boric 	newnode->update_atime = true;
8559f988b79SJean-Baptiste Boric 
856*0a6a1f1dSLionel Sambuc 	if (error == 0)
857*0a6a1f1dSLionel Sambuc 		VOP_UNLOCK(*a->a_vpp);
8589f988b79SJean-Baptiste Boric 
8599f988b79SJean-Baptiste Boric 	return error;
8609f988b79SJean-Baptiste Boric }
8619f988b79SJean-Baptiste Boric 
8629f988b79SJean-Baptiste Boric int
v7fs_rmdir(void * v)8639f988b79SJean-Baptiste Boric v7fs_rmdir(void *v)
8649f988b79SJean-Baptiste Boric {
8659f988b79SJean-Baptiste Boric 	struct vop_rmdir_args /* {
8669f988b79SJean-Baptiste Boric 				 struct vnode		*a_dvp;
8679f988b79SJean-Baptiste Boric 				 struct vnode		*a_vp;
8689f988b79SJean-Baptiste Boric 				 struct componentname	*a_cnp;
8699f988b79SJean-Baptiste Boric 				 } */ *a = v;
8709f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
8719f988b79SJean-Baptiste Boric 	struct vnode *dvp = a->a_dvp;
8729f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = dvp->v_data;
8739f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
8749f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode;
8759f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7fsmount->core;
8769f988b79SJean-Baptiste Boric 	int error = 0;
8779f988b79SJean-Baptiste Boric 
8789f988b79SJean-Baptiste Boric 	DPRINTF("delete %s\n", a->a_cnp->cn_nameptr);
8799f988b79SJean-Baptiste Boric 
8809f988b79SJean-Baptiste Boric 	KDASSERT(vp->v_type == VDIR);
8819f988b79SJean-Baptiste Boric 
8829f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_deallocate(fs, &parent_node->inode,
8839f988b79SJean-Baptiste Boric 	    a->a_cnp->cn_nameptr))) {
8849f988b79SJean-Baptiste Boric 		DPRINTF("v7fs_directory_deallocate failed.\n");
8859f988b79SJean-Baptiste Boric 		goto out;
8869f988b79SJean-Baptiste Boric 	}
887*0a6a1f1dSLionel Sambuc 	error = v7fs_inode_load(fs, inode, inode->inode_number);
888*0a6a1f1dSLionel Sambuc 	if (error)
889*0a6a1f1dSLionel Sambuc 		goto out;
890*0a6a1f1dSLionel Sambuc 	uvm_vnp_setsize(vp, v7fs_inode_filesize(inode));
8919f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
8929f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
8939f988b79SJean-Baptiste Boric out:
8949f988b79SJean-Baptiste Boric 	vput(vp);
8959f988b79SJean-Baptiste Boric 	vput(dvp);
8969f988b79SJean-Baptiste Boric 
8979f988b79SJean-Baptiste Boric 	return error;
8989f988b79SJean-Baptiste Boric }
8999f988b79SJean-Baptiste Boric 
9009f988b79SJean-Baptiste Boric struct v7fs_readdir_arg {
9019f988b79SJean-Baptiste Boric 	struct dirent *dp;
9029f988b79SJean-Baptiste Boric 	struct uio *uio;
9039f988b79SJean-Baptiste Boric 	int start;
9049f988b79SJean-Baptiste Boric 	int end;
9059f988b79SJean-Baptiste Boric 	int cnt;
9069f988b79SJean-Baptiste Boric };
9079f988b79SJean-Baptiste Boric static int readdir_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
9089f988b79SJean-Baptiste Boric 
9099f988b79SJean-Baptiste Boric int
readdir_subr(struct v7fs_self * fs,void * ctx,v7fs_daddr_t blk,size_t sz)9109f988b79SJean-Baptiste Boric readdir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
9119f988b79SJean-Baptiste Boric {
9129f988b79SJean-Baptiste Boric 	struct v7fs_readdir_arg *p = (struct v7fs_readdir_arg *)ctx;
9139f988b79SJean-Baptiste Boric 	struct v7fs_dirent *dir;
9149f988b79SJean-Baptiste Boric 	struct dirent *dp = p->dp;
9159f988b79SJean-Baptiste Boric 	struct v7fs_inode inode;
9169f988b79SJean-Baptiste Boric 	char filename[V7FS_NAME_MAX + 1];
9179f988b79SJean-Baptiste Boric 	int i, n;
9189f988b79SJean-Baptiste Boric 	int error = 0;
9199f988b79SJean-Baptiste Boric 	void *buf;
9209f988b79SJean-Baptiste Boric 
9219f988b79SJean-Baptiste Boric 	if (!(buf = scratch_read(fs, blk)))
9229f988b79SJean-Baptiste Boric 		return EIO;
9239f988b79SJean-Baptiste Boric 	dir = (struct v7fs_dirent *)buf;
9249f988b79SJean-Baptiste Boric 
9259f988b79SJean-Baptiste Boric 	n = sz / sizeof(*dir);
9269f988b79SJean-Baptiste Boric 
9279f988b79SJean-Baptiste Boric 	for (i = 0; (i < n) && (p->cnt < p->end); i++, dir++, p->cnt++) {
9289f988b79SJean-Baptiste Boric 		if (p->cnt < p->start)
9299f988b79SJean-Baptiste Boric 			continue;
9309f988b79SJean-Baptiste Boric 
9319f988b79SJean-Baptiste Boric 		if ((error = v7fs_inode_load(fs, &inode, dir->inode_number)))
9329f988b79SJean-Baptiste Boric 			break;
9339f988b79SJean-Baptiste Boric 
9349f988b79SJean-Baptiste Boric 		v7fs_dirent_filename(filename, dir->name);
9359f988b79SJean-Baptiste Boric 
9369f988b79SJean-Baptiste Boric 		DPRINTF("inode=%d name=%s %s\n", dir->inode_number, filename,
9379f988b79SJean-Baptiste Boric 		    v7fs_inode_isdir(&inode) ? "DIR" : "FILE");
9389f988b79SJean-Baptiste Boric 		memset(dp, 0, sizeof(*dp));
9399f988b79SJean-Baptiste Boric 		dp->d_fileno = dir->inode_number;
9409f988b79SJean-Baptiste Boric 		dp->d_type = v7fs_mode_to_d_type(inode.mode);
9419f988b79SJean-Baptiste Boric 		dp->d_namlen = strlen(filename);
9429f988b79SJean-Baptiste Boric 		strcpy(dp->d_name, filename);
9439f988b79SJean-Baptiste Boric 		dp->d_reclen = sizeof(*dp);
9449f988b79SJean-Baptiste Boric 		if ((error = uiomove(dp, dp->d_reclen, p->uio))) {
9459f988b79SJean-Baptiste Boric 			DPRINTF("uiomove failed.\n");
9469f988b79SJean-Baptiste Boric 			break;
9479f988b79SJean-Baptiste Boric 		}
9489f988b79SJean-Baptiste Boric 	}
9499f988b79SJean-Baptiste Boric 	scratch_free(fs, buf);
9509f988b79SJean-Baptiste Boric 
9519f988b79SJean-Baptiste Boric 	if (p->cnt == p->end)
9529f988b79SJean-Baptiste Boric 		return V7FS_ITERATOR_BREAK;
9539f988b79SJean-Baptiste Boric 
9549f988b79SJean-Baptiste Boric 	return error;
9559f988b79SJean-Baptiste Boric }
9569f988b79SJean-Baptiste Boric 
9579f988b79SJean-Baptiste Boric int
v7fs_readdir(void * v)9589f988b79SJean-Baptiste Boric v7fs_readdir(void *v)
9599f988b79SJean-Baptiste Boric {
9609f988b79SJean-Baptiste Boric 	struct vop_readdir_args /* {
9619f988b79SJean-Baptiste Boric 				   struct vnode *a_vp;
9629f988b79SJean-Baptiste Boric 				   struct uio *a_uio;
9639f988b79SJean-Baptiste Boric 				   kauth_cred_t a_cred;
9649f988b79SJean-Baptiste Boric 				   int *a_eofflag;
9659f988b79SJean-Baptiste Boric 				   off_t **a_cookies;
9669f988b79SJean-Baptiste Boric 				   int *a_ncookies;
9679f988b79SJean-Baptiste Boric 				   } */ *a = v;
9689f988b79SJean-Baptiste Boric 	struct uio *uio = a->a_uio;
9699f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
9709f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
9719f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
9729f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
9739f988b79SJean-Baptiste Boric 	struct dirent *dp;
9749f988b79SJean-Baptiste Boric 	int error;
9759f988b79SJean-Baptiste Boric 
9769f988b79SJean-Baptiste Boric 	DPRINTF("offset=%zu residue=%zu\n", uio->uio_offset, uio->uio_resid);
9779f988b79SJean-Baptiste Boric 
9789f988b79SJean-Baptiste Boric 	KDASSERT(vp->v_type == VDIR);
9799f988b79SJean-Baptiste Boric 	KDASSERT(uio->uio_offset >= 0);
9809f988b79SJean-Baptiste Boric 	KDASSERT(v7fs_inode_isdir(inode));
9819f988b79SJean-Baptiste Boric 
9829f988b79SJean-Baptiste Boric 	struct v7fs_readdir_arg arg;
9839f988b79SJean-Baptiste Boric 	arg.start = uio->uio_offset / sizeof(*dp);
9849f988b79SJean-Baptiste Boric 	arg.end = arg.start +  uio->uio_resid / sizeof(*dp);
9859f988b79SJean-Baptiste Boric 	if (arg.start == arg.end) {/* user buffer has not enuf space. */
9869f988b79SJean-Baptiste Boric 		DPRINTF("uio buffer too small\n");
9879f988b79SJean-Baptiste Boric 		return ENOMEM;
9889f988b79SJean-Baptiste Boric 	}
9899f988b79SJean-Baptiste Boric 	dp = kmem_zalloc(sizeof(*dp), KM_SLEEP);
9909f988b79SJean-Baptiste Boric 	arg.cnt = 0;
9919f988b79SJean-Baptiste Boric 	arg.dp = dp;
9929f988b79SJean-Baptiste Boric 	arg.uio = uio;
9939f988b79SJean-Baptiste Boric 
9949f988b79SJean-Baptiste Boric 	*a->a_eofflag = false;
9959f988b79SJean-Baptiste Boric 	error = v7fs_datablock_foreach(fs, inode, readdir_subr, &arg);
9969f988b79SJean-Baptiste Boric 	if (error == V7FS_ITERATOR_END) {
9979f988b79SJean-Baptiste Boric 		*a->a_eofflag = true;
9989f988b79SJean-Baptiste Boric 	}
9999f988b79SJean-Baptiste Boric 	if (error < 0)
10009f988b79SJean-Baptiste Boric 		error = 0;
10019f988b79SJean-Baptiste Boric 
10029f988b79SJean-Baptiste Boric 	kmem_free(dp, sizeof(*dp));
10039f988b79SJean-Baptiste Boric 
10049f988b79SJean-Baptiste Boric 	return error;
10059f988b79SJean-Baptiste Boric }
10069f988b79SJean-Baptiste Boric 
10079f988b79SJean-Baptiste Boric int
v7fs_inactive(void * v)10089f988b79SJean-Baptiste Boric v7fs_inactive(void *v)
10099f988b79SJean-Baptiste Boric {
10109f988b79SJean-Baptiste Boric 	struct vop_inactive_args /* {
10119f988b79SJean-Baptiste Boric 				    struct vnode *a_vp;
10129f988b79SJean-Baptiste Boric 				    bool *a_recycle;
10139f988b79SJean-Baptiste Boric 				    } */ *a = v;
10149f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
10159f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
10169f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
10179f988b79SJean-Baptiste Boric 
10189f988b79SJean-Baptiste Boric 	DPRINTF("%p #%d\n", vp, inode->inode_number);
1019*0a6a1f1dSLionel Sambuc 	if (v7fs_inode_nlink(inode) > 0) {
10209f988b79SJean-Baptiste Boric 		v7fs_update(vp, 0, 0, UPDATE_WAIT);
10219f988b79SJean-Baptiste Boric 		*a->a_recycle = false;
10229f988b79SJean-Baptiste Boric 	} else {
10239f988b79SJean-Baptiste Boric 		*a->a_recycle = true;
10249f988b79SJean-Baptiste Boric 	}
10259f988b79SJean-Baptiste Boric 
10269f988b79SJean-Baptiste Boric 	VOP_UNLOCK(vp);
10279f988b79SJean-Baptiste Boric 
10289f988b79SJean-Baptiste Boric 	return 0;
10299f988b79SJean-Baptiste Boric }
10309f988b79SJean-Baptiste Boric 
10319f988b79SJean-Baptiste Boric int
v7fs_reclaim(void * v)10329f988b79SJean-Baptiste Boric v7fs_reclaim(void *v)
10339f988b79SJean-Baptiste Boric {
10349f988b79SJean-Baptiste Boric 	/*This vnode is no longer referenced by kernel. */
10359f988b79SJean-Baptiste Boric 	extern struct pool v7fs_node_pool;
10369f988b79SJean-Baptiste Boric 	struct vop_reclaim_args /* {
10379f988b79SJean-Baptiste Boric 				   struct vnode *a_vp;
10389f988b79SJean-Baptiste Boric 				   } */ *a = v;
10399f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
10409f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
1041*0a6a1f1dSLionel Sambuc 	struct v7fs_self *fs = v7node->v7fsmount->core;
1042*0a6a1f1dSLionel Sambuc 	struct v7fs_inode *inode = &v7node->inode;
10439f988b79SJean-Baptiste Boric 
1044*0a6a1f1dSLionel Sambuc 	DPRINTF("%p #%d\n", vp, inode->inode_number);
1045*0a6a1f1dSLionel Sambuc 	if (v7fs_inode_nlink(inode) == 0) {
1046*0a6a1f1dSLionel Sambuc 		v7fs_datablock_size_change(fs, 0, inode);
1047*0a6a1f1dSLionel Sambuc 		DPRINTF("remove datablock\n");
1048*0a6a1f1dSLionel Sambuc 		v7fs_inode_deallocate(fs, inode->inode_number);
1049*0a6a1f1dSLionel Sambuc 		DPRINTF("remove inode\n");
1050*0a6a1f1dSLionel Sambuc 	}
1051*0a6a1f1dSLionel Sambuc 	vcache_remove(vp->v_mount,
1052*0a6a1f1dSLionel Sambuc 	    &inode->inode_number, sizeof(inode->inode_number));
10539f988b79SJean-Baptiste Boric 	genfs_node_destroy(vp);
10549f988b79SJean-Baptiste Boric 	pool_put(&v7fs_node_pool, v7node);
1055*0a6a1f1dSLionel Sambuc 	mutex_enter(vp->v_interlock);
10569f988b79SJean-Baptiste Boric 	vp->v_data = NULL;
1057*0a6a1f1dSLionel Sambuc 	mutex_exit(vp->v_interlock);
10589f988b79SJean-Baptiste Boric 
10599f988b79SJean-Baptiste Boric 	return 0;
10609f988b79SJean-Baptiste Boric }
10619f988b79SJean-Baptiste Boric 
10629f988b79SJean-Baptiste Boric int
v7fs_bmap(void * v)10639f988b79SJean-Baptiste Boric v7fs_bmap(void *v)
10649f988b79SJean-Baptiste Boric {
10659f988b79SJean-Baptiste Boric 	struct vop_bmap_args /* {
10669f988b79SJean-Baptiste Boric 				struct vnode *a_vp;
10679f988b79SJean-Baptiste Boric 				daddr_t  a_bn;
10689f988b79SJean-Baptiste Boric 				struct vnode **a_vpp;
10699f988b79SJean-Baptiste Boric 				daddr_t *a_bnp;
10709f988b79SJean-Baptiste Boric 				int *a_runp;
10719f988b79SJean-Baptiste Boric 				} */ *a = v;
10729f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
10739f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
10749f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = v7node->v7fsmount;
10759f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
10769f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
10779f988b79SJean-Baptiste Boric 	int error = 0;
10789f988b79SJean-Baptiste Boric 
10799f988b79SJean-Baptiste Boric 	DPRINTF("inode=%d offset=%zu %p\n", inode->inode_number, a->a_bn, vp);
10809f988b79SJean-Baptiste Boric 	DPRINTF("filesize: %d\n", inode->filesize);
10819f988b79SJean-Baptiste Boric 	if (!a->a_bnp)
10829f988b79SJean-Baptiste Boric 		return 0;
10839f988b79SJean-Baptiste Boric 
10849f988b79SJean-Baptiste Boric 	v7fs_daddr_t blk;
10859f988b79SJean-Baptiste Boric 	if (!(blk = v7fs_datablock_last(fs, inode,
10869f988b79SJean-Baptiste Boric 	    (a->a_bn + 1) << V7FS_BSHIFT))) {
10879f988b79SJean-Baptiste Boric 		/* +1 converts block # to file offset. */
10889f988b79SJean-Baptiste Boric 		return ENOSPC;
10899f988b79SJean-Baptiste Boric 	}
10909f988b79SJean-Baptiste Boric 
10919f988b79SJean-Baptiste Boric 	*a->a_bnp = blk;
10929f988b79SJean-Baptiste Boric 
10939f988b79SJean-Baptiste Boric 	if (a->a_vpp)
10949f988b79SJean-Baptiste Boric 		*a->a_vpp = v7fsmount->devvp;
10959f988b79SJean-Baptiste Boric 	if (a->a_runp)
10969f988b79SJean-Baptiste Boric 		*a->a_runp = 0; /*XXX TODO */
10979f988b79SJean-Baptiste Boric 
10989f988b79SJean-Baptiste Boric 	DPRINTF("%d  %zu->%zu status=%d\n", inode->inode_number, a->a_bn,
10999f988b79SJean-Baptiste Boric 	    *a->a_bnp, error);
11009f988b79SJean-Baptiste Boric 
11019f988b79SJean-Baptiste Boric 	return error;
11029f988b79SJean-Baptiste Boric }
11039f988b79SJean-Baptiste Boric 
11049f988b79SJean-Baptiste Boric int
v7fs_strategy(void * v)11059f988b79SJean-Baptiste Boric v7fs_strategy(void *v)
11069f988b79SJean-Baptiste Boric {
11079f988b79SJean-Baptiste Boric 	struct vop_strategy_args /* {
11089f988b79SJean-Baptiste Boric 				    struct vnode *a_vp;
11099f988b79SJean-Baptiste Boric 				    struct buf *a_bp;
11109f988b79SJean-Baptiste Boric 				    } */ *a = v;
11119f988b79SJean-Baptiste Boric 	struct buf *b = a->a_bp;
11129f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
11139f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
11149f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = v7node->v7fsmount;
11159f988b79SJean-Baptiste Boric 	int error;
11169f988b79SJean-Baptiste Boric 
11179f988b79SJean-Baptiste Boric 	DPRINTF("%p\n", vp);
11189f988b79SJean-Baptiste Boric 	KDASSERT(vp->v_type == VREG);
11199f988b79SJean-Baptiste Boric 	if (b->b_blkno == b->b_lblkno) {
11209f988b79SJean-Baptiste Boric 		error = VOP_BMAP(vp, b->b_lblkno, NULL, &b->b_blkno, NULL);
11219f988b79SJean-Baptiste Boric 		if (error) {
11229f988b79SJean-Baptiste Boric 			b->b_error = error;
11239f988b79SJean-Baptiste Boric 			biodone(b);
11249f988b79SJean-Baptiste Boric 			return error;
11259f988b79SJean-Baptiste Boric 		}
11269f988b79SJean-Baptiste Boric 		if ((long)b->b_blkno == -1)
11279f988b79SJean-Baptiste Boric 			clrbuf(b);
11289f988b79SJean-Baptiste Boric 	}
11299f988b79SJean-Baptiste Boric 	if ((long)b->b_blkno == -1) {
11309f988b79SJean-Baptiste Boric 		biodone(b);
11319f988b79SJean-Baptiste Boric 		return 0;
11329f988b79SJean-Baptiste Boric 	}
11339f988b79SJean-Baptiste Boric 
11349f988b79SJean-Baptiste Boric 	return VOP_STRATEGY(v7fsmount->devvp, b);
11359f988b79SJean-Baptiste Boric }
11369f988b79SJean-Baptiste Boric 
11379f988b79SJean-Baptiste Boric int
v7fs_print(void * v)11389f988b79SJean-Baptiste Boric v7fs_print(void *v)
11399f988b79SJean-Baptiste Boric {
11409f988b79SJean-Baptiste Boric 	struct vop_print_args /* {
11419f988b79SJean-Baptiste Boric 				 struct vnode *a_vp;
11429f988b79SJean-Baptiste Boric 				 } */ *a = v;
11439f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = a->a_vp->v_data;
11449f988b79SJean-Baptiste Boric 
11459f988b79SJean-Baptiste Boric 	v7fs_inode_dump(&v7node->inode);
11469f988b79SJean-Baptiste Boric 
11479f988b79SJean-Baptiste Boric 	return 0;
11489f988b79SJean-Baptiste Boric }
11499f988b79SJean-Baptiste Boric 
11509f988b79SJean-Baptiste Boric int
v7fs_advlock(void * v)11519f988b79SJean-Baptiste Boric v7fs_advlock(void *v)
11529f988b79SJean-Baptiste Boric {
11539f988b79SJean-Baptiste Boric 	struct vop_advlock_args /* {
11549f988b79SJean-Baptiste Boric 				   struct vnode *a_vp;
11559f988b79SJean-Baptiste Boric 				   void *a_id;
11569f988b79SJean-Baptiste Boric 				   int a_op;
11579f988b79SJean-Baptiste Boric 				   struct flock *a_fl;
11589f988b79SJean-Baptiste Boric 				   int a_flags;
11599f988b79SJean-Baptiste Boric 				   } */ *a = v;
11609f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = a->a_vp->v_data;
11619f988b79SJean-Baptiste Boric 
11629f988b79SJean-Baptiste Boric 	DPRINTF("op=%d\n", a->a_op);
11639f988b79SJean-Baptiste Boric 
11649f988b79SJean-Baptiste Boric 	return lf_advlock(a, &v7node->lockf,
11659f988b79SJean-Baptiste Boric 	    v7fs_inode_filesize(&v7node->inode));
11669f988b79SJean-Baptiste Boric }
11679f988b79SJean-Baptiste Boric 
11689f988b79SJean-Baptiste Boric int
v7fs_pathconf(void * v)11699f988b79SJean-Baptiste Boric v7fs_pathconf(void *v)
11709f988b79SJean-Baptiste Boric {
11719f988b79SJean-Baptiste Boric 	struct vop_pathconf_args /* {
11729f988b79SJean-Baptiste Boric 				    struct vnode *a_vp;
11739f988b79SJean-Baptiste Boric 				    int a_name;
11749f988b79SJean-Baptiste Boric 				    register_t *a_retval;
11759f988b79SJean-Baptiste Boric 				    } */ *a = v;
11769f988b79SJean-Baptiste Boric 	int err = 0;
11779f988b79SJean-Baptiste Boric 
11789f988b79SJean-Baptiste Boric 	DPRINTF("%p\n", a->a_vp);
11799f988b79SJean-Baptiste Boric 
11809f988b79SJean-Baptiste Boric 	switch (a->a_name) {
11819f988b79SJean-Baptiste Boric 	case _PC_LINK_MAX:
11829f988b79SJean-Baptiste Boric 		*a->a_retval = V7FS_LINK_MAX;
11839f988b79SJean-Baptiste Boric 		break;
11849f988b79SJean-Baptiste Boric 	case _PC_NAME_MAX:
11859f988b79SJean-Baptiste Boric 		*a->a_retval = V7FS_NAME_MAX;
11869f988b79SJean-Baptiste Boric 		break;
11879f988b79SJean-Baptiste Boric 	case _PC_PATH_MAX:
11889f988b79SJean-Baptiste Boric 		*a->a_retval = V7FS_PATH_MAX;
11899f988b79SJean-Baptiste Boric 		break;
11909f988b79SJean-Baptiste Boric 	case _PC_CHOWN_RESTRICTED:
11919f988b79SJean-Baptiste Boric 		*a->a_retval = 1;
11929f988b79SJean-Baptiste Boric 		break;
11939f988b79SJean-Baptiste Boric 	case _PC_NO_TRUNC:
11949f988b79SJean-Baptiste Boric 		*a->a_retval = 0;
11959f988b79SJean-Baptiste Boric 		break;
11969f988b79SJean-Baptiste Boric 	case _PC_SYNC_IO:
11979f988b79SJean-Baptiste Boric 		*a->a_retval = 1;
11989f988b79SJean-Baptiste Boric 		break;
11999f988b79SJean-Baptiste Boric 	case _PC_FILESIZEBITS:
12009f988b79SJean-Baptiste Boric 		*a->a_retval = 30; /* ~1G */
12019f988b79SJean-Baptiste Boric 		break;
12029f988b79SJean-Baptiste Boric 	case _PC_SYMLINK_MAX:
12039f988b79SJean-Baptiste Boric 		*a->a_retval = V7FSBSD_MAXSYMLINKLEN;
12049f988b79SJean-Baptiste Boric 		break;
12059f988b79SJean-Baptiste Boric 	case _PC_2_SYMLINKS:
12069f988b79SJean-Baptiste Boric 		*a->a_retval = 1;
12079f988b79SJean-Baptiste Boric 		break;
12089f988b79SJean-Baptiste Boric 	default:
12099f988b79SJean-Baptiste Boric 		err = EINVAL;
12109f988b79SJean-Baptiste Boric 		break;
12119f988b79SJean-Baptiste Boric 	}
12129f988b79SJean-Baptiste Boric 
12139f988b79SJean-Baptiste Boric 	return err;
12149f988b79SJean-Baptiste Boric }
12159f988b79SJean-Baptiste Boric 
12169f988b79SJean-Baptiste Boric int
v7fs_update(struct vnode * vp,const struct timespec * acc,const struct timespec * mod,int flags)12179f988b79SJean-Baptiste Boric v7fs_update(struct vnode *vp, const struct timespec *acc,
12189f988b79SJean-Baptiste Boric     const struct timespec *mod, int flags)
12199f988b79SJean-Baptiste Boric {
12209f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
12219f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
12229f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
12239f988b79SJean-Baptiste Boric 	bool update = false;
12249f988b79SJean-Baptiste Boric 
12259f988b79SJean-Baptiste Boric 	DPRINTF("%p %zu %d\n", vp, vp->v_size, v7fs_inode_filesize(inode));
12269f988b79SJean-Baptiste Boric 	KDASSERT(vp->v_size == v7fs_inode_filesize(inode));
12279f988b79SJean-Baptiste Boric 
12289f988b79SJean-Baptiste Boric 	if (v7node->update_atime) {
12299f988b79SJean-Baptiste Boric 		inode->atime = acc ? acc->tv_sec : time_second;
12309f988b79SJean-Baptiste Boric 		v7node->update_atime = false;
12319f988b79SJean-Baptiste Boric 		update = true;
12329f988b79SJean-Baptiste Boric 	}
12339f988b79SJean-Baptiste Boric 	if (v7node->update_ctime) {
12349f988b79SJean-Baptiste Boric 		inode->ctime = time_second;
12359f988b79SJean-Baptiste Boric 		v7node->update_ctime = false;
12369f988b79SJean-Baptiste Boric 		update = true;
12379f988b79SJean-Baptiste Boric 	}
12389f988b79SJean-Baptiste Boric 	if (v7node->update_mtime) {
12399f988b79SJean-Baptiste Boric 		inode->mtime = mod ? mod->tv_sec : time_second;
12409f988b79SJean-Baptiste Boric 		v7node->update_mtime = false;
12419f988b79SJean-Baptiste Boric 		update = true;
12429f988b79SJean-Baptiste Boric 	}
12439f988b79SJean-Baptiste Boric 
12449f988b79SJean-Baptiste Boric 	if (update)
12459f988b79SJean-Baptiste Boric 		v7fs_inode_writeback(fs, inode);
12469f988b79SJean-Baptiste Boric 
12479f988b79SJean-Baptiste Boric 	return 0;
12489f988b79SJean-Baptiste Boric }
12499f988b79SJean-Baptiste Boric 
12509f988b79SJean-Baptiste Boric int
v7fs_symlink(void * v)12519f988b79SJean-Baptiste Boric v7fs_symlink(void *v)
12529f988b79SJean-Baptiste Boric {
1253*0a6a1f1dSLionel Sambuc 	struct vop_symlink_v3_args /* {
12549f988b79SJean-Baptiste Boric 				   struct vnode		*a_dvp;
12559f988b79SJean-Baptiste Boric 				   struct vnode		**a_vpp;
12569f988b79SJean-Baptiste Boric 				   struct componentname	*a_cnp;
12579f988b79SJean-Baptiste Boric 				   struct vattr		*a_vap;
12589f988b79SJean-Baptiste Boric 				   char			*a_target;
12599f988b79SJean-Baptiste Boric 				   } */ *a = v;
12609f988b79SJean-Baptiste Boric 	struct v7fs_node *parent_node = a->a_dvp->v_data;
12619f988b79SJean-Baptiste Boric 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
12629f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7fsmount->core;
12639f988b79SJean-Baptiste Boric 	struct vattr *va = a->a_vap;
12649f988b79SJean-Baptiste Boric 	kauth_cred_t cr = a->a_cnp->cn_cred;
12659f988b79SJean-Baptiste Boric 	struct componentname *cnp = a->a_cnp;
12669f988b79SJean-Baptiste Boric 	struct v7fs_fileattr attr;
12679f988b79SJean-Baptiste Boric 	v7fs_ino_t ino;
12689f988b79SJean-Baptiste Boric 	const char *from = a->a_target;
12699f988b79SJean-Baptiste Boric 	const char *to = cnp->cn_nameptr;
12709f988b79SJean-Baptiste Boric 	size_t len = strlen(from) + 1;
12719f988b79SJean-Baptiste Boric 	int error = 0;
12729f988b79SJean-Baptiste Boric 
12739f988b79SJean-Baptiste Boric 	if (len > V7FS_BSIZE) { /* limited to 512byte pathname */
12749f988b79SJean-Baptiste Boric 		DPRINTF("too long pathname.");
12759f988b79SJean-Baptiste Boric 		return ENAMETOOLONG;
12769f988b79SJean-Baptiste Boric 	}
12779f988b79SJean-Baptiste Boric 
12789f988b79SJean-Baptiste Boric 	memset(&attr, 0, sizeof(attr));
12799f988b79SJean-Baptiste Boric 	attr.uid = kauth_cred_geteuid(cr);
12809f988b79SJean-Baptiste Boric 	attr.gid = kauth_cred_getegid(cr);
12819f988b79SJean-Baptiste Boric 	attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type);
12829f988b79SJean-Baptiste Boric 
12839f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_allocate
12849f988b79SJean-Baptiste Boric 		(fs, &parent_node->inode, to, &attr, &ino))) {
1285*0a6a1f1dSLionel Sambuc 		return error;
12869f988b79SJean-Baptiste Boric 	}
12879f988b79SJean-Baptiste Boric 	/* Sync dirent size change. */
12889f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(a->a_dvp, v7fs_inode_filesize(&parent_node->inode));
12899f988b79SJean-Baptiste Boric 
12909f988b79SJean-Baptiste Boric 	/* Get myself vnode. */
12919f988b79SJean-Baptiste Boric 	if ((error = v7fs_vget(v7fsmount->mountp, ino, a->a_vpp))) {
12929f988b79SJean-Baptiste Boric 		DPRINTF("can't get vnode.\n");
12939f988b79SJean-Baptiste Boric 	}
12949f988b79SJean-Baptiste Boric 
12959f988b79SJean-Baptiste Boric 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
12969f988b79SJean-Baptiste Boric 	struct v7fs_inode *p = &newnode->inode;
12979f988b79SJean-Baptiste Boric 	v7fs_file_symlink(fs, p, from);
12989f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(*a->a_vpp, v7fs_inode_filesize(p));
12999f988b79SJean-Baptiste Boric 
13009f988b79SJean-Baptiste Boric 	newnode->update_ctime = true;
13019f988b79SJean-Baptiste Boric 	newnode->update_mtime = true;
13029f988b79SJean-Baptiste Boric 	newnode->update_atime = true;
1303*0a6a1f1dSLionel Sambuc 
1304*0a6a1f1dSLionel Sambuc 	if (error == 0)
1305*0a6a1f1dSLionel Sambuc 		VOP_UNLOCK(*a->a_vpp);
13069f988b79SJean-Baptiste Boric 
13079f988b79SJean-Baptiste Boric 	return error;
13089f988b79SJean-Baptiste Boric }
13099f988b79SJean-Baptiste Boric 
13109f988b79SJean-Baptiste Boric int
v7fs_readlink(void * v)13119f988b79SJean-Baptiste Boric v7fs_readlink(void *v)
13129f988b79SJean-Baptiste Boric {
13139f988b79SJean-Baptiste Boric 	struct vop_readlink_args /* {
13149f988b79SJean-Baptiste Boric 				    struct vnode	*a_vp;
13159f988b79SJean-Baptiste Boric 				    struct uio		*a_uio;
13169f988b79SJean-Baptiste Boric 				    kauth_cred_t	a_cred;
13179f988b79SJean-Baptiste Boric 				    } */ *a = v;
13189f988b79SJean-Baptiste Boric 	struct uio *uio = a->a_uio;
13199f988b79SJean-Baptiste Boric 	struct vnode *vp = a->a_vp;
13209f988b79SJean-Baptiste Boric 	struct v7fs_node *v7node = vp->v_data;
13219f988b79SJean-Baptiste Boric 	struct v7fs_inode *inode = &v7node->inode;
13229f988b79SJean-Baptiste Boric 	struct v7fs_self *fs = v7node->v7fsmount->core;
13239f988b79SJean-Baptiste Boric 	int error = 0;
13249f988b79SJean-Baptiste Boric 
13259f988b79SJean-Baptiste Boric 	KDASSERT(vp->v_type == VLNK);
13269f988b79SJean-Baptiste Boric 	KDASSERT(uio->uio_offset >= 0);
13279f988b79SJean-Baptiste Boric 	KDASSERT(v7fs_inode_islnk(inode));
13289f988b79SJean-Baptiste Boric 
13299f988b79SJean-Baptiste Boric 	v7fs_daddr_t blk = inode->addr[0];
13309f988b79SJean-Baptiste Boric 	void *buf;
13319f988b79SJean-Baptiste Boric 	if (!(buf = scratch_read(fs, blk))) {
13329f988b79SJean-Baptiste Boric 		error = EIO;
13339f988b79SJean-Baptiste Boric 		goto error_exit;
13349f988b79SJean-Baptiste Boric 	}
13359f988b79SJean-Baptiste Boric 
13369f988b79SJean-Baptiste Boric 	if ((error = uiomove(buf, strlen(buf), uio))) {
13379f988b79SJean-Baptiste Boric 		DPRINTF("uiomove failed.\n");
13389f988b79SJean-Baptiste Boric 	}
13399f988b79SJean-Baptiste Boric 	scratch_free(fs, buf);
13409f988b79SJean-Baptiste Boric 
13419f988b79SJean-Baptiste Boric error_exit:
13429f988b79SJean-Baptiste Boric 	return error;
13439f988b79SJean-Baptiste Boric }
1344