xref: /netbsd-src/sys/fs/v7fs/v7fs_vnops.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: v7fs_vnops.c,v 1.12 2013/03/18 19:35:42 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: v7fs_vnops.c,v 1.12 2013/03/18 19:35:42 plunky Exp $");
34 #if defined _KERNEL_OPT
35 #include "opt_v7fs.h"
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/resource.h>
41 #include <sys/vnode.h>
42 #include <sys/namei.h>
43 #include <sys/dirent.h>
44 #include <sys/malloc.h>
45 #include <sys/lockf.h>
46 #include <sys/unistd.h>
47 #include <sys/fcntl.h>
48 #include <sys/kauth.h>
49 #include <sys/buf.h>
50 #include <sys/stat.h>	/*APPEND */
51 #include <miscfs/genfs/genfs.h>
52 
53 #include <fs/v7fs/v7fs.h>
54 #include <fs/v7fs/v7fs_impl.h>
55 #include <fs/v7fs/v7fs_inode.h>
56 #include <fs/v7fs/v7fs_dirent.h>
57 #include <fs/v7fs/v7fs_file.h>
58 #include <fs/v7fs/v7fs_datablock.h>
59 #include <fs/v7fs/v7fs_extern.h>
60 
61 #ifdef V7FS_VNOPS_DEBUG
62 #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
63 #else
64 #define	DPRINTF(arg...)		((void)0)
65 #endif
66 
67 MALLOC_JUSTDEFINE(M_V7FS_VNODE, "v7fs vnode", "v7fs vnode structures");
68 MALLOC_DECLARE(M_V7FS);
69 
70 int v7fs_vnode_reload(struct mount *, struct vnode *);
71 
72 static v7fs_mode_t vtype_to_v7fs_mode(enum vtype);
73 static uint8_t v7fs_mode_to_d_type(v7fs_mode_t);
74 
75 static v7fs_mode_t
76 vtype_to_v7fs_mode(enum vtype type)
77 {
78 	/* Convert Vnode types to V7FS types (sys/vnode.h)*/
79 	v7fs_mode_t table[] = { 0, V7FS_IFREG, V7FS_IFDIR, V7FS_IFBLK,
80 				V7FS_IFCHR, V7FSBSD_IFLNK, V7FSBSD_IFSOCK,
81 				V7FSBSD_IFFIFO };
82 	return table[type];
83 }
84 
85 static uint8_t
86 v7fs_mode_to_d_type(v7fs_mode_t mode)
87 {
88 	/* Convert V7FS types to dirent d_type (sys/dirent.h)*/
89 
90 	return (mode & V7FS_IFMT) >> 12;
91 }
92 
93 int
94 v7fs_lookup(void *v)
95 {
96 	struct vop_lookup_args /* {
97 				  struct vnode *a_dvp;
98 				  struct vnode **a_vpp;
99 				  struct componentname *a_cnp;
100 				  } */ *a = v;
101 	struct vnode *dvp = a->a_dvp;
102 	struct v7fs_node *parent_node = dvp->v_data;
103 	struct v7fs_inode *parent = &parent_node->inode;
104 	struct v7fs_self *fs = parent_node->v7fsmount->core;/* my filesystem */
105 	struct vnode *vpp;
106 	struct componentname *cnp = a->a_cnp;
107 	int nameiop = cnp->cn_nameiop;
108 	const char *name = cnp->cn_nameptr;
109 	int namelen = cnp->cn_namelen;
110 	int flags = cnp->cn_flags;
111 	bool isdotdot = flags & ISDOTDOT;
112 	bool islastcn = flags & ISLASTCN;
113 	v7fs_ino_t ino;
114 	int error;
115 #ifdef V7FS_VNOPS_DEBUG
116 	const char *opname[] = { "LOOKUP", "CREATE", "DELETE", "RENAME" };
117 #endif
118 	DPRINTF("'%s' op=%s flags=%d parent=%d %o %dbyte\n", name,
119 	    opname[nameiop], cnp->cn_flags, parent->inode_number, parent->mode,
120 	    parent->filesize);
121 
122 	*a->a_vpp = 0;
123 
124 	/* Check directory permission for search */
125 	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred))) {
126 		DPRINTF("***perm.\n");
127 		return error;
128 	}
129 
130 	/* Deny last component write operation on a read-only mount */
131 	if (islastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
132 	    (nameiop == DELETE || nameiop == RENAME)) {
133 		DPRINTF("***ROFS.\n");
134 		return EROFS;
135 	}
136 
137 	/* "." */
138 	if (namelen == 1 && name[0] == '.') {
139 		if ((nameiop == RENAME) && islastcn) {
140 			return EISDIR; /* t_vnops rename_dir(3) */
141 		}
142 		vref(dvp); /* v_usecount++ */
143 		*a->a_vpp = dvp;
144 		DPRINTF("done.(.)\n");
145 		return 0;
146 	}
147 
148 	/* ".." and reguler file. */
149 	if ((error = v7fs_file_lookup_by_name(fs, parent, name, &ino))) {
150 		/* Not found. Tell this entry be able to allocate. */
151 		if (((nameiop == CREATE) || (nameiop == RENAME)) && islastcn) {
152 			/* Check directory permission to allocate. */
153 			if ((error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred))) {
154 				DPRINTF("access denied. (%s)\n", name);
155 				return error;
156 			}
157 			DPRINTF("EJUSTRETURN op=%d (%s)\n", nameiop, name);
158 			return EJUSTRETURN;
159 		}
160 		DPRINTF("lastcn=%d\n", flags & ISLASTCN);
161 		return error;
162 	}
163 
164 	if ((nameiop == DELETE) && islastcn) {
165 		if ((error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred))) {
166 			DPRINTF("access denied. (%s)\n", name);
167 			return error;
168 		}
169 	}
170 
171 	/* Entry found. Allocate v-node */
172 	// Check permissions?
173 	vpp = 0;
174 	if (isdotdot) {
175 		VOP_UNLOCK(dvp); /* preserve reference count. (not vput) */
176 	}
177 	DPRINTF("enter vget\n");
178 	if ((error = v7fs_vget(dvp->v_mount, ino, &vpp))) {
179 		DPRINTF("***can't get vnode.\n");
180 		return error;
181 	}
182 	DPRINTF("exit vget\n");
183 	if (isdotdot) {
184 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
185 	}
186 	*a->a_vpp = vpp;
187 	DPRINTF("done.(%s)\n", name);
188 
189 	return 0;
190 }
191 
192 int
193 v7fs_create(void *v)
194 {
195 	struct vop_create_args /* {
196 				  struct vnode *a_dvp;
197 				  struct vnode **a_vpp;
198 				  struct componentname *a_cnp;
199 				  struct vattr *a_vap;
200 				  } */ *a = v;
201 	struct v7fs_node *parent_node = a->a_dvp->v_data;
202 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
203 	struct v7fs_self *fs = v7fsmount->core;
204 	struct mount *mp = v7fsmount->mountp;
205 	struct v7fs_fileattr attr;
206 	struct vattr *va = a->a_vap;
207 	kauth_cred_t cr = a->a_cnp->cn_cred;
208 	v7fs_ino_t ino;
209 	int error = 0;
210 
211 	DPRINTF("%s parent#%d\n", a->a_cnp->cn_nameptr,
212 	    parent_node->inode.inode_number);
213 	KDASSERT((va->va_type == VREG) || (va->va_type == VSOCK));
214 
215 	memset(&attr, 0, sizeof(attr));
216 	attr.uid = kauth_cred_geteuid(cr);
217 	attr.gid = kauth_cred_getegid(cr);
218 	attr.mode = va->va_mode | vtype_to_v7fs_mode (va->va_type);
219 	attr.device = 0;
220 
221 	/* Allocate disk entry. and register its entry to parent directory. */
222 	if ((error = v7fs_file_allocate(fs, &parent_node->inode,
223 		    a->a_cnp->cn_nameptr, &attr, &ino))) {
224 		DPRINTF("v7fs_file_allocate failed.\n");
225 		goto unlock_exit;
226 	}
227 	/* Sync dirent size change. */
228 	uvm_vnp_setsize(a->a_dvp, v7fs_inode_filesize(&parent_node->inode));
229 
230 	/* Get myself vnode. */
231 	*a->a_vpp = 0;
232 	if ((error = v7fs_vget(mp, ino, a->a_vpp))) {
233 		DPRINTF("v7fs_vget failed.\n");
234 		goto unlock_exit;
235 	}
236 
237 	/* Scheduling update time. real update by v7fs_update */
238 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
239 	newnode->update_ctime = true;
240 	newnode->update_mtime = true;
241 	newnode->update_atime = true;
242 	DPRINTF("allocated %s->#%d\n", a->a_cnp->cn_nameptr, ino);
243 
244 unlock_exit:
245 	/* unlock parent directory */
246 	vput(a->a_dvp);	/* locked at v7fs_lookup(); */
247 
248 	return error;
249 }
250 
251 int
252 v7fs_mknod(void *v)
253 {
254 	struct vop_mknod_args /* {
255 				 struct vnode		*a_dvp;
256 				 struct vnode		**a_vpp;
257 				 struct componentname	*a_cnp;
258 				 struct vattr		*a_vap;
259 				 } */ *a = v;
260 	struct componentname *cnp = a->a_cnp;
261 	kauth_cred_t cr = cnp->cn_cred;
262 	struct vnode *dvp = a->a_dvp;
263 	struct vattr *va = a->a_vap;
264 	struct v7fs_node *parent_node = dvp->v_data;
265 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
266 	struct v7fs_self *fs = v7fsmount->core;
267 	struct mount *mp = v7fsmount->mountp;
268 	struct v7fs_fileattr attr;
269 
270 	v7fs_ino_t ino;
271 	int error = 0;
272 
273 	DPRINTF("%s %06o %lx %d\n", cnp->cn_nameptr, va->va_mode,
274 	    (long)va->va_rdev, va->va_type);
275 	memset(&attr, 0, sizeof(attr));
276 	attr.uid = kauth_cred_geteuid(cr);
277 	attr.gid = kauth_cred_getegid(cr);
278 	attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type);
279 	attr.device = va->va_rdev;
280 
281 	if ((error = v7fs_file_allocate(fs, &parent_node->inode,
282 	    cnp->cn_nameptr, &attr, &ino)))
283 		goto unlock_exit;
284 	/* Sync dirent size change. */
285 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
286 
287 	if ((error = v7fs_vget(mp, ino, a->a_vpp))) {
288 		DPRINTF("can't get vnode.\n");
289 		goto unlock_exit;
290 	}
291 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
292 	newnode->update_ctime = true;
293 	newnode->update_mtime = true;
294 	newnode->update_atime = true;
295 
296 unlock_exit:
297 	vput(dvp);
298 
299 	return error;
300 }
301 
302 int
303 v7fs_open(void *v)
304 {
305 	struct vop_open_args /* {
306 				struct vnode *a_vp;
307 				int  a_mode;
308 				kauth_cred_t a_cred;
309 				} */ *a = v;
310 
311 	struct vnode *vp = a->a_vp;
312 	struct v7fs_node *v7node = vp->v_data;
313 	struct v7fs_inode *inode = &v7node->inode;
314 
315 	DPRINTF("inode %d\n", inode->inode_number);
316 	/* Append mode file pointer is managed by kernel. */
317 	if (inode->append_mode &&
318 	    ((a->a_mode & (FWRITE | O_APPEND)) == FWRITE)) {
319 		DPRINTF("file is already opened by append mode.\n");
320 		return EPERM;
321 	}
322 
323 	return 0;
324 }
325 
326 int
327 v7fs_close(void *v)
328 {
329 	struct vop_close_args /* {
330 				 struct vnodeop_desc *a_desc;
331 				 struct vnode *a_vp;
332 				 int  a_fflag;
333 				 kauth_cred_t a_cred;
334 				 } */ *a = v;
335 	struct vnode *vp = a->a_vp;
336 #ifdef V7FS_VNOPS_DEBUG
337 	struct v7fs_node *v7node = vp->v_data;
338 	struct v7fs_inode *inode = &v7node->inode;
339 #endif
340 	DPRINTF("#%d (i)%dbyte (v)%zubyte\n", inode->inode_number,
341 	    v7fs_inode_filesize(inode), vp->v_size);
342 
343 	/* Update timestamp */
344 	v7fs_update(vp, 0, 0, UPDATE_WAIT);
345 
346 	return 0;
347 }
348 
349 static int
350 v7fs_check_possible(struct vnode *vp, struct v7fs_node *v7node,
351     mode_t mode)
352 {
353 
354 	if (!(mode & VWRITE))
355 	  return 0;
356 
357 	switch (vp->v_type) {
358 	default:
359 		/*  special file is always writable. */
360 		return 0;
361 	case VDIR:
362 	case VLNK:
363 	case VREG:
364 		break;
365 	}
366 
367 	return vp->v_mount->mnt_flag & MNT_RDONLY ? EROFS : 0;
368 }
369 
370 static int
371 v7fs_check_permitted(struct vnode *vp, struct v7fs_node *v7node,
372     mode_t mode, kauth_cred_t cred)
373 {
374 
375 	struct v7fs_inode *inode = &v7node->inode;
376 
377 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
378 	    vp->v_type, inode->mode), vp, NULL, genfs_can_access(vp->v_type,
379 	    inode->mode, inode->uid, inode->gid, mode, cred));
380 }
381 
382 int
383 v7fs_access(void *v)
384 {
385 	struct vop_access_args /* {
386 				  struct vnode	*a_vp;
387 				  int		a_mode;
388 				  kauth_cred_t	a_cred;
389 				  } */ *ap = v;
390 	struct vnode *vp = ap->a_vp;
391 	struct v7fs_node *v7node = vp->v_data;
392 	int error;
393 
394 	error = v7fs_check_possible(vp, v7node, ap->a_mode);
395 	if (error)
396 		return error;
397 
398 	error = v7fs_check_permitted(vp, v7node, ap->a_mode, ap->a_cred);
399 
400 	return error;
401 }
402 
403 int
404 v7fs_getattr(void *v)
405 {
406 	struct vop_getattr_args /* {
407 				   struct vnode *a_vp;
408 				   struct vattr *a_vap;
409 				   kauth_cred_t a_cred;
410 				   } */ *ap = v;
411 	struct vnode *vp = ap->a_vp;
412 	struct v7fs_node *v7node = vp->v_data;
413 	struct v7fs_inode *inode = &v7node->inode;
414 	struct v7fs_mount *v7fsmount = v7node->v7fsmount;
415 	struct vattr *vap = ap->a_vap;
416 
417 	DPRINTF("\n");
418 	vap->va_type = vp->v_type;
419 	vap->va_mode = inode->mode;
420 	vap->va_nlink = inode->nlink;
421 	vap->va_uid = inode->uid;
422 	vap->va_gid = inode->gid;
423 	vap->va_fsid = v7fsmount->devvp->v_rdev;
424 	vap->va_fileid = inode->inode_number;
425 	vap->va_size = vp->v_size;
426 	vap->va_atime.tv_sec = inode->atime;
427 	vap->va_mtime.tv_sec = inode->mtime;
428 	vap->va_ctime.tv_sec = inode->ctime;
429 	vap->va_birthtime.tv_sec = 0;
430 	vap->va_gen = 1;
431 	vap->va_flags = inode->append_mode ? SF_APPEND : 0;
432 	vap->va_rdev = inode->device;
433 	vap->va_bytes = vap->va_size; /* No sparse support. */
434 	vap->va_filerev = 0;
435 	vap->va_vaflags = 0;
436 	/* PAGE_SIZE is larger than sizeof(struct dirent). OK.
437 	   getcwd_scandir()@vfs_getcwd.c */
438 	vap->va_blocksize = PAGE_SIZE;
439 
440 	return 0;
441 }
442 
443 int
444 v7fs_setattr(void *v)
445 {
446 	struct vop_setattr_args /* {
447 				   struct vnode *a_vp;
448 				   struct vattr *a_vap;
449 				   kauth_cred_t a_cred;
450 				   struct proc *p;
451 				   } */ *ap = v;
452 	struct vnode *vp = ap->a_vp;
453 	struct vattr *vap = ap->a_vap;
454 	struct v7fs_node *v7node = vp->v_data;
455 	struct v7fs_self *fs = v7node->v7fsmount->core;
456 	struct v7fs_inode *inode = &v7node->inode;
457 	kauth_cred_t cred = ap->a_cred;
458 	struct timespec *acc, *mod;
459 	int error = 0;
460 	acc = mod = NULL;
461 
462 	DPRINTF("\n");
463 
464 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
465 		switch (vp->v_type) {
466 		default:
467 			/*  special file is always writable. */
468 			break;
469 		case VDIR:
470 		case VLNK:
471 		case VREG:
472 			DPRINTF("read-only mount\n");
473 			return EROFS;
474 		}
475 	}
476 
477 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
478 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
479 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
480 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
481 		DPRINTF("invalid request\n");
482 		return EINVAL;
483 	}
484 	/* File pointer mode. */
485 	if (vap->va_flags != VNOVAL) {
486 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS,
487 		    vp, NULL, genfs_can_chflags(cred, vp->v_type, inode->uid,
488 		    false));
489 		if (error)
490 			return error;
491 		inode->append_mode = vap->va_flags & SF_APPEND;
492 	}
493 
494 	/* File size change. */
495 	if ((vap->va_size != VNOVAL) && (vp->v_type == VREG)) {
496 		error = v7fs_datablock_size_change(fs, vap->va_size, inode);
497 		if (error == 0)
498 			uvm_vnp_setsize(vp, vap->va_size);
499 	}
500 	uid_t uid = inode->uid;
501 	gid_t gid = inode->gid;
502 
503 	if (vap->va_uid != (uid_t)VNOVAL) {
504 		uid = vap->va_uid;
505 		error = kauth_authorize_vnode(cred,
506 		    KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL,
507 		    genfs_can_chown(cred, inode->uid, inode->gid, uid,
508 		    gid));
509 		if (error)
510 			return error;
511 		inode->uid = uid;
512 	}
513 	if (vap->va_gid != (uid_t)VNOVAL) {
514 		gid = vap->va_gid;
515 		error = kauth_authorize_vnode(cred,
516 		    KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL,
517 		    genfs_can_chown(cred, inode->uid, inode->gid, uid,
518 		    gid));
519 		if (error)
520 			return error;
521 		inode->gid = gid;
522 	}
523 	if (vap->va_mode != (mode_t)VNOVAL) {
524 		mode_t mode = vap->va_mode;
525 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY,
526 		    vp, NULL, genfs_can_chmod(vp->v_type, cred, inode->uid, inode->gid,
527 		    mode));
528 		if (error) {
529 			return error;
530 		}
531 		v7fs_inode_chmod(inode, mode);
532 	}
533 	if ((vap->va_atime.tv_sec != VNOVAL) ||
534 	    (vap->va_mtime.tv_sec != VNOVAL) ||
535 	    (vap->va_ctime.tv_sec != VNOVAL)) {
536 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
537 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, inode->uid,
538 		    cred));
539 		if (error)
540 			return error;
541 
542 		if (vap->va_atime.tv_sec != VNOVAL) {
543 			acc = &vap->va_atime;
544 		}
545 		if (vap->va_mtime.tv_sec != VNOVAL) {
546 			mod = &vap->va_mtime;
547 			v7node->update_mtime = true;
548 		}
549 		if (vap->va_ctime.tv_sec != VNOVAL) {
550 			v7node->update_ctime = true;
551 		}
552 	}
553 
554 	v7node->update_atime = true;
555 	v7fs_update(vp, acc, mod, 0);
556 
557 	return error;
558 }
559 
560 int
561 v7fs_read(void *v)
562 {
563 	struct vop_read_args /* {
564 				struct vnode *a_vp;
565 				struct uio *a_uio;
566 				int a_ioflag;
567 				kauth_cred_t a_cred;
568 				} */ *a = v;
569 	struct vnode *vp = a->a_vp;
570 	struct uio *uio = a->a_uio;
571 	struct v7fs_node *v7node = vp->v_data;
572 	struct v7fs_inode *inode = &v7node->inode;
573 	vsize_t sz, filesz = v7fs_inode_filesize(inode);
574 	const int advice = IO_ADV_DECODE(a->a_ioflag);
575 	int error = 0;
576 
577 	DPRINTF("type=%d inode=%d\n", vp->v_type, v7node->inode.inode_number);
578 
579 	while (uio->uio_resid > 0) {
580 		if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0)
581 			break;
582 
583 		error = ubc_uiomove(&vp->v_uobj, uio, sz, advice, UBC_READ |
584 		    UBC_PARTIALOK | UBC_UNMAP_FLAG(v));
585 		if (error) {
586 			break;
587 		}
588 		DPRINTF("read %zubyte\n", sz);
589 	}
590 	v7node->update_atime = true;
591 
592 	return  error;
593 }
594 
595 int
596 v7fs_write(void *v)
597 {
598 	struct vop_write_args /* {
599 				 struct vnode *a_vp;
600 				 struct uio *a_uio;
601 				 int  a_ioflag;
602 				 kauth_cred_t a_cred;
603 				 } */ *a = v;
604 	struct vnode *vp = a->a_vp;
605 	struct uio *uio = a->a_uio;
606 	int advice = IO_ADV_DECODE(a->a_ioflag);
607 	struct v7fs_node *v7node = vp->v_data;
608 	struct v7fs_inode *inode = &v7node->inode;
609 	struct v7fs_self *fs = v7node->v7fsmount->core;
610 	vsize_t sz;
611 	int error = 0;
612 
613 	if (uio->uio_resid == 0)
614 		return 0;
615 
616 	sz = v7fs_inode_filesize(inode);
617 	DPRINTF("(i)%ld (v)%zu ofs=%zu + res=%zu = %zu\n", sz, vp->v_size,
618 	    uio->uio_offset, uio->uio_resid, uio->uio_offset + uio->uio_resid);
619 
620 	/* Append mode file offset is managed by kernel. */
621 	if (a->a_ioflag & IO_APPEND)
622 		uio->uio_offset = sz;
623 
624 	/* If write region is over filesize, expand. */
625 	size_t newsize= uio->uio_offset + uio->uio_resid;
626 	ssize_t expand = newsize - sz;
627  	if (expand > 0) {
628 		if ((error = v7fs_datablock_expand(fs, inode, expand)))
629 			return error;
630 		uvm_vnp_setsize(vp, newsize);
631 	}
632 
633 	while (uio->uio_resid > 0) {
634 		sz = uio->uio_resid;
635 		if ((error = ubc_uiomove(&vp->v_uobj, uio, sz, advice,
636 			    UBC_WRITE | UBC_UNMAP_FLAG(v))))
637 			break;
638 		DPRINTF("write %zubyte\n", sz);
639 	}
640 	v7node->update_mtime = true;
641 
642 	return error;
643 }
644 
645 int
646 v7fs_fsync(void *v)
647 {
648 	struct vop_fsync_args /* {
649 				 struct vnode *a_vp;
650 				 kauth_cred_t a_cred;
651 				 int a_flags;
652 				 off_t offlo;
653 				 off_t offhi;
654 				 } */ *a = v;
655 	struct vnode *vp = a->a_vp;
656 	int error, wait;
657 
658 	DPRINTF("%p\n", a->a_vp);
659 	if (a->a_flags & FSYNC_CACHE) {
660 		return EOPNOTSUPP;
661 	}
662 
663 	wait = (a->a_flags & FSYNC_WAIT);
664 	error = vflushbuf(vp, a->a_flags);
665 
666 	if (error == 0 && (a->a_flags & FSYNC_DATAONLY) == 0)
667 		error = v7fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
668 
669 	return error;
670 }
671 
672 int
673 v7fs_remove(void *v)
674 {
675 	struct vop_remove_args /* {
676 				  struct vnodeop_desc *a_desc;
677 				  struct vnode * a_dvp;
678 				  struct vnode * a_vp;
679 				  struct componentname * a_cnp;
680 				  } */ *a = v;
681 	struct v7fs_node *parent_node = a->a_dvp->v_data;
682 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
683 	struct vnode *vp = a->a_vp;
684 	struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode;
685 	struct vnode *dvp = a->a_dvp;
686 	struct v7fs_self *fs = v7fsmount->core;
687 	bool remove;
688 	int error = 0;
689 
690 	DPRINTF("delete %s\n", a->a_cnp->cn_nameptr);
691 
692 	if (vp->v_type == VDIR) {
693 		error = EPERM;
694 		goto out;
695 	}
696 
697 	remove = v7fs_inode_nlink(inode) == 1;
698 	if (remove)
699 		uvm_vnp_setsize(vp, 0);
700 
701 	if ((error = v7fs_file_deallocate(fs, &parent_node->inode,
702 		    a->a_cnp->cn_nameptr))) {
703 		DPRINTF("v7fs_file_delete failed.\n");
704 		goto out;
705 	}
706 	/* Sync dirent size change. */
707 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
708 	/* This inode is no longer used. -> v7fs_inactive */
709 	if (remove)
710 		memset(inode, 0, sizeof(*inode));
711 
712 out:
713 	if (dvp == vp)
714 		vrele(vp); /* v_usecount-- of unlocked vp */
715 	else
716 		vput(vp); /* unlock vp and then v_usecount-- */
717 	vput(dvp);
718 
719 	return error;
720 }
721 
722 int
723 v7fs_link(void *v)
724 {
725 	struct vop_link_args /* {
726 				struct vnode *a_dvp;
727 				struct vnode *a_vp;
728 				struct componentname *a_cnp;
729 				} */ *a = v;
730 	struct vnode *dvp = a->a_dvp;
731 	struct vnode *vp = a->a_vp;
732 	struct v7fs_node *parent_node = dvp->v_data;
733 	struct v7fs_node *node = vp->v_data;
734 	struct v7fs_inode *parent = &parent_node->inode;
735 	struct v7fs_inode *p = &node->inode;
736 	struct v7fs_self *fs = node->v7fsmount->core;
737 	struct componentname *cnp = a->a_cnp;
738 	int error = 0;
739 
740 	DPRINTF("%p\n", vp);
741 	/* Lock soruce file */
742 	if ((error = vn_lock(vp, LK_EXCLUSIVE))) {
743 		DPRINTF("lock failed. %p\n", vp);
744 		VOP_ABORTOP(dvp, cnp);
745 		goto unlock;
746 	}
747 	error = v7fs_file_link(fs, parent, p, cnp->cn_nameptr);
748 	/* Sync dirent size change. */
749 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
750 
751 	VOP_UNLOCK(vp);
752 unlock:
753 	vput(dvp);
754 
755 	return error;
756 }
757 
758 int
759 v7fs_rename(void *v)
760 {
761 	struct vop_rename_args /* {
762 				  struct vnode *a_fdvp;	from parent-directory
763 				  struct vnode *a_fvp;	from file
764 				  struct componentname *a_fcnp;
765 				  struct vnode *a_tdvp;	to parent-directory
766 				  struct vnode *a_tvp;	to file
767 				  struct componentname *a_tcnp;
768 				  } */ *a = v;
769 	struct vnode *fvp = a->a_fvp;
770 	struct vnode *tvp = a->a_tvp;
771 	struct vnode *fdvp = a->a_fdvp;
772 	struct vnode *tdvp = a->a_tdvp;
773 	struct v7fs_node *parent_from = fdvp->v_data;
774 	struct v7fs_node *parent_to = tdvp->v_data;
775 	struct v7fs_node *v7node = fvp->v_data;
776 	struct v7fs_self *fs = v7node->v7fsmount->core;
777 	const char *from_name = a->a_fcnp->cn_nameptr;
778 	const char *to_name = a->a_tcnp->cn_nameptr;
779 	int error;
780 
781 	DPRINTF("%s->%s %p %p\n", from_name, to_name, fvp, tvp);
782 
783 	if ((fvp->v_mount != tdvp->v_mount) ||
784 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
785 		error = EXDEV;
786 		DPRINTF("cross-device link\n");
787 		goto out;
788 	}
789 	// XXXsource file lock?
790 	error = v7fs_file_rename(fs, &parent_from->inode, from_name,
791 	    &parent_to->inode, to_name);
792 	/* 'to file' inode may be changed. (hard-linked and it is cached.)
793 	   t_vnops rename_reg_nodir */
794 	if (tvp) {
795 		v7fs_vnode_reload(parent_from->v7fsmount->mountp, tvp);
796 	}
797 	/* Sync dirent size change. */
798 	uvm_vnp_setsize(tdvp, v7fs_inode_filesize(&parent_to->inode));
799 	uvm_vnp_setsize(fdvp, v7fs_inode_filesize(&parent_from->inode));
800 out:
801 	if (tvp)
802 		vput(tvp);  /* locked on entry */
803 	if (tdvp == tvp)
804 		vrele(tdvp);
805 	else
806 		vput(tdvp);
807 	vrele(fdvp);
808 	vrele(fvp);
809 
810 	return error;
811 }
812 
813 int
814 v7fs_mkdir(void *v)
815 {
816 	struct vop_mkdir_args /* {
817 				 struct vnode		*a_dvp;
818 				 struct vnode		**a_vpp;
819 				 struct componentname	*a_cnp;
820 				 struct vattr		*a_vap;
821 				 } */ *a = v;
822 	struct componentname *cnp = a->a_cnp;
823 	kauth_cred_t cr = cnp->cn_cred;
824 	struct vnode *dvp = a->a_dvp;
825 	struct vattr *va = a->a_vap;
826 	struct v7fs_node *parent_node = dvp->v_data;
827 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
828 	struct v7fs_self *fs = v7fsmount->core;
829 	struct v7fs_fileattr attr;
830 	struct mount *mp = v7fsmount->mountp;
831 	v7fs_ino_t ino;
832 	int error = 0;
833 
834 	DPRINTF("\n");
835 	memset(&attr, 0, sizeof(attr));
836 	attr.uid = kauth_cred_geteuid(cr);
837 	attr.gid = kauth_cred_getegid(cr);
838 	attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type);
839 
840 	if ((error = v7fs_file_allocate(fs, &parent_node->inode,
841 	    cnp->cn_nameptr, &attr, &ino)))
842 		goto unlock_exit;
843 	/* Sync dirent size change. */
844 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
845 
846 	if ((error = v7fs_vget(mp, ino, a->a_vpp))) {
847 		DPRINTF("can't get vnode.\n");
848 	}
849 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
850 	newnode->update_ctime = true;
851 	newnode->update_mtime = true;
852 	newnode->update_atime = true;
853 
854 unlock_exit:
855 	vput(dvp);
856 
857 	return error;
858 }
859 
860 int
861 v7fs_rmdir(void *v)
862 {
863 	struct vop_rmdir_args /* {
864 				 struct vnode		*a_dvp;
865 				 struct vnode		*a_vp;
866 				 struct componentname	*a_cnp;
867 				 } */ *a = v;
868 	struct vnode *vp = a->a_vp;
869 	struct vnode *dvp = a->a_dvp;
870 	struct v7fs_node *parent_node = dvp->v_data;
871 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
872 	struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode;
873 	struct v7fs_self *fs = v7fsmount->core;
874 	int error = 0;
875 
876 	DPRINTF("delete %s\n", a->a_cnp->cn_nameptr);
877 
878 	KDASSERT(vp->v_type == VDIR);
879 
880 	if ((error = v7fs_file_deallocate(fs, &parent_node->inode,
881 	    a->a_cnp->cn_nameptr))) {
882 		DPRINTF("v7fs_directory_deallocate failed.\n");
883 		goto out;
884 	}
885 	uvm_vnp_setsize(vp, 0);
886 	/* Sync dirent size change. */
887 	uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode));
888 	/* This inode is no longer used. -> v7fs_inactive */
889 	memset(inode, 0, sizeof(*inode));
890 out:
891 	vput(vp);
892 	vput(dvp);
893 
894 	return error;
895 }
896 
897 struct v7fs_readdir_arg {
898 	struct dirent *dp;
899 	struct uio *uio;
900 	int start;
901 	int end;
902 	int cnt;
903 };
904 static int readdir_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
905 
906 int
907 readdir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
908 {
909 	struct v7fs_readdir_arg *p = (struct v7fs_readdir_arg *)ctx;
910 	struct v7fs_dirent *dir;
911 	struct dirent *dp = p->dp;
912 	struct v7fs_inode inode;
913 	char filename[V7FS_NAME_MAX + 1];
914 	int i, n;
915 	int error = 0;
916 	void *buf;
917 
918 	if (!(buf = scratch_read(fs, blk)))
919 		return EIO;
920 	dir = (struct v7fs_dirent *)buf;
921 
922 	n = sz / sizeof(*dir);
923 
924 	for (i = 0; (i < n) && (p->cnt < p->end); i++, dir++, p->cnt++) {
925 		if (p->cnt < p->start)
926 			continue;
927 
928 		if ((error = v7fs_inode_load(fs, &inode, dir->inode_number)))
929 			break;
930 
931 		v7fs_dirent_filename(filename, dir->name);
932 
933 		DPRINTF("inode=%d name=%s %s\n", dir->inode_number, filename,
934 		    v7fs_inode_isdir(&inode) ? "DIR" : "FILE");
935 		memset(dp, 0, sizeof(*dp));
936 		dp->d_fileno = dir->inode_number;
937 		dp->d_type = v7fs_mode_to_d_type(inode.mode);
938 		dp->d_namlen = strlen(filename);
939 		strcpy(dp->d_name, filename);
940 		dp->d_reclen = sizeof(*dp);
941 		if ((error = uiomove(dp, dp->d_reclen, p->uio))) {
942 			DPRINTF("uiomove failed.\n");
943 			break;
944 		}
945 	}
946 	scratch_free(fs, buf);
947 
948 	if (p->cnt == p->end)
949 		return V7FS_ITERATOR_BREAK;
950 
951 	return error;
952 }
953 
954 int
955 v7fs_readdir(void *v)
956 {
957 	struct vop_readdir_args /* {
958 				   struct vnode *a_vp;
959 				   struct uio *a_uio;
960 				   kauth_cred_t a_cred;
961 				   int *a_eofflag;
962 				   off_t **a_cookies;
963 				   int *a_ncookies;
964 				   } */ *a = v;
965 	struct uio *uio = a->a_uio;
966 	struct vnode *vp = a->a_vp;
967 	struct v7fs_node *v7node = vp->v_data;
968 	struct v7fs_inode *inode = &v7node->inode;
969 	struct v7fs_self *fs = v7node->v7fsmount->core;
970 	struct dirent *dp;
971 	int error;
972 
973 	DPRINTF("offset=%zu residue=%zu\n", uio->uio_offset, uio->uio_resid);
974 
975 	KDASSERT(vp->v_type == VDIR);
976 	KDASSERT(uio->uio_offset >= 0);
977 	KDASSERT(v7fs_inode_isdir(inode));
978 
979 	struct v7fs_readdir_arg arg;
980 	arg.start = uio->uio_offset / sizeof(*dp);
981 	arg.end = arg.start +  uio->uio_resid / sizeof(*dp);
982 	if (arg.start == arg.end) {/* user buffer has not enuf space. */
983 		DPRINTF("uio buffer too small\n");
984 		return ENOMEM;
985 	}
986 	dp = malloc(sizeof(*dp), M_V7FS, M_WAITOK | M_ZERO);
987 	arg.cnt = 0;
988 	arg.dp = dp;
989 	arg.uio = uio;
990 
991 	*a->a_eofflag = false;
992 	error = v7fs_datablock_foreach(fs, inode, readdir_subr, &arg);
993 	if (error == V7FS_ITERATOR_END) {
994 		*a->a_eofflag = true;
995 	}
996 	if (error < 0)
997 		error = 0;
998 
999 	free(dp, M_V7FS);
1000 
1001 	return error;
1002 }
1003 
1004 int
1005 v7fs_inactive(void *v)
1006 {
1007 	struct vop_inactive_args /* {
1008 				    struct vnode *a_vp;
1009 				    bool *a_recycle;
1010 				    } */ *a = v;
1011 	struct vnode *vp = a->a_vp;
1012 	struct v7fs_node *v7node = vp->v_data;
1013 	struct v7fs_inode *inode = &v7node->inode;
1014 
1015 	DPRINTF("%p #%d\n", vp, inode->inode_number);
1016 	if (v7fs_inode_allocated(inode)) {
1017 		v7fs_update(vp, 0, 0, UPDATE_WAIT);
1018 		*a->a_recycle = false;
1019 	} else {
1020 		*a->a_recycle = true;
1021 	}
1022 
1023 	VOP_UNLOCK(vp);
1024 
1025 	return 0;
1026 }
1027 
1028 int
1029 v7fs_reclaim(void *v)
1030 {
1031 	/*This vnode is no longer referenced by kernel. */
1032 	extern struct pool v7fs_node_pool;
1033 	struct vop_reclaim_args /* {
1034 				   struct vnode *a_vp;
1035 				   } */ *a = v;
1036 	struct vnode *vp = a->a_vp;
1037 	struct v7fs_node *v7node = vp->v_data;
1038 
1039 	DPRINTF("%p #%d\n", vp, v7node->inode.inode_number);
1040 	mutex_enter(&mntvnode_lock);
1041 	LIST_REMOVE(v7node, link);
1042 	mutex_exit(&mntvnode_lock);
1043 	genfs_node_destroy(vp);
1044 	pool_put(&v7fs_node_pool, v7node);
1045 	vp->v_data = NULL;
1046 
1047 	return 0;
1048 }
1049 
1050 int
1051 v7fs_bmap(void *v)
1052 {
1053 	struct vop_bmap_args /* {
1054 				struct vnode *a_vp;
1055 				daddr_t  a_bn;
1056 				struct vnode **a_vpp;
1057 				daddr_t *a_bnp;
1058 				int *a_runp;
1059 				} */ *a = v;
1060 	struct vnode *vp = a->a_vp;
1061 	struct v7fs_node *v7node = vp->v_data;
1062 	struct v7fs_mount *v7fsmount = v7node->v7fsmount;
1063 	struct v7fs_self *fs = v7node->v7fsmount->core;
1064 	struct v7fs_inode *inode = &v7node->inode;
1065 	int error = 0;
1066 
1067 	DPRINTF("inode=%d offset=%zu %p\n", inode->inode_number, a->a_bn, vp);
1068 	DPRINTF("filesize: %d\n", inode->filesize);
1069 	if (!a->a_bnp)
1070 		return 0;
1071 
1072 	v7fs_daddr_t blk;
1073 	if (!(blk = v7fs_datablock_last(fs, inode,
1074 	    (a->a_bn + 1) << V7FS_BSHIFT))) {
1075 		/* +1 converts block # to file offset. */
1076 		return ENOSPC;
1077 	}
1078 
1079 	*a->a_bnp = blk;
1080 
1081 	if (a->a_vpp)
1082 		*a->a_vpp = v7fsmount->devvp;
1083 	if (a->a_runp)
1084 		*a->a_runp = 0; /*XXX TODO */
1085 
1086 	DPRINTF("%d  %zu->%zu status=%d\n", inode->inode_number, a->a_bn,
1087 	    *a->a_bnp, error);
1088 
1089 	return error;
1090 }
1091 
1092 int
1093 v7fs_strategy(void *v)
1094 {
1095 	struct vop_strategy_args /* {
1096 				    struct vnode *a_vp;
1097 				    struct buf *a_bp;
1098 				    } */ *a = v;
1099 	struct buf *b = a->a_bp;
1100 	struct vnode *vp = a->a_vp;
1101 	struct v7fs_node *v7node = vp->v_data;
1102 	struct v7fs_mount *v7fsmount = v7node->v7fsmount;
1103 	int error;
1104 
1105 	DPRINTF("%p\n", vp);
1106 	KDASSERT(vp->v_type == VREG);
1107 	if (b->b_blkno == b->b_lblkno) {
1108 		error = VOP_BMAP(vp, b->b_lblkno, NULL, &b->b_blkno, NULL);
1109 		if (error) {
1110 			b->b_error = error;
1111 			biodone(b);
1112 			return error;
1113 		}
1114 		if ((long)b->b_blkno == -1)
1115 			clrbuf(b);
1116 	}
1117 	if ((long)b->b_blkno == -1) {
1118 		biodone(b);
1119 		return 0;
1120 	}
1121 
1122 	return VOP_STRATEGY(v7fsmount->devvp, b);
1123 }
1124 
1125 int
1126 v7fs_print(void *v)
1127 {
1128 	struct vop_print_args /* {
1129 				 struct vnode *a_vp;
1130 				 } */ *a = v;
1131 	struct v7fs_node *v7node = a->a_vp->v_data;
1132 
1133 	v7fs_inode_dump(&v7node->inode);
1134 
1135 	return 0;
1136 }
1137 
1138 int
1139 v7fs_advlock(void *v)
1140 {
1141 	struct vop_advlock_args /* {
1142 				   struct vnode *a_vp;
1143 				   void *a_id;
1144 				   int a_op;
1145 				   struct flock *a_fl;
1146 				   int a_flags;
1147 				   } */ *a = v;
1148 	struct v7fs_node *v7node = a->a_vp->v_data;
1149 
1150 	DPRINTF("op=%d\n", a->a_op);
1151 
1152 	return lf_advlock(a, &v7node->lockf,
1153 	    v7fs_inode_filesize(&v7node->inode));
1154 }
1155 
1156 int
1157 v7fs_pathconf(void *v)
1158 {
1159 	struct vop_pathconf_args /* {
1160 				    struct vnode *a_vp;
1161 				    int a_name;
1162 				    register_t *a_retval;
1163 				    } */ *a = v;
1164 	int err = 0;
1165 
1166 	DPRINTF("%p\n", a->a_vp);
1167 
1168 	switch (a->a_name) {
1169 	case _PC_LINK_MAX:
1170 		*a->a_retval = V7FS_LINK_MAX;
1171 		break;
1172 	case _PC_NAME_MAX:
1173 		*a->a_retval = V7FS_NAME_MAX;
1174 		break;
1175 	case _PC_PATH_MAX:
1176 		*a->a_retval = V7FS_PATH_MAX;
1177 		break;
1178 	case _PC_CHOWN_RESTRICTED:
1179 		*a->a_retval = 1;
1180 		break;
1181 	case _PC_NO_TRUNC:
1182 		*a->a_retval = 0;
1183 		break;
1184 	case _PC_SYNC_IO:
1185 		*a->a_retval = 1;
1186 		break;
1187 	case _PC_FILESIZEBITS:
1188 		*a->a_retval = 30; /* ~1G */
1189 		break;
1190 	case _PC_SYMLINK_MAX:
1191 		*a->a_retval = V7FSBSD_MAXSYMLINKLEN;
1192 		break;
1193 	case _PC_2_SYMLINKS:
1194 		*a->a_retval = 1;
1195 		break;
1196 	default:
1197 		err = EINVAL;
1198 		break;
1199 	}
1200 
1201 	return err;
1202 }
1203 
1204 int
1205 v7fs_update(struct vnode *vp, const struct timespec *acc,
1206     const struct timespec *mod, int flags)
1207 {
1208 	struct v7fs_node *v7node = vp->v_data;
1209 	struct v7fs_inode *inode = &v7node->inode;
1210 	struct v7fs_self *fs = v7node->v7fsmount->core;
1211 	bool update = false;
1212 
1213 	DPRINTF("%p %zu %d\n", vp, vp->v_size, v7fs_inode_filesize(inode));
1214 	KDASSERT(vp->v_size == v7fs_inode_filesize(inode));
1215 
1216 	if (v7node->update_atime) {
1217 		inode->atime = acc ? acc->tv_sec : time_second;
1218 		v7node->update_atime = false;
1219 		update = true;
1220 	}
1221 	if (v7node->update_ctime) {
1222 		inode->ctime = time_second;
1223 		v7node->update_ctime = false;
1224 		update = true;
1225 	}
1226 	if (v7node->update_mtime) {
1227 		inode->mtime = mod ? mod->tv_sec : time_second;
1228 		v7node->update_mtime = false;
1229 		update = true;
1230 	}
1231 
1232 	if (update)
1233 		v7fs_inode_writeback(fs, inode);
1234 
1235 	return 0;
1236 }
1237 
1238 int
1239 v7fs_symlink(void *v)
1240 {
1241 	struct vop_symlink_args /* {
1242 				   struct vnode		*a_dvp;
1243 				   struct vnode		**a_vpp;
1244 				   struct componentname	*a_cnp;
1245 				   struct vattr		*a_vap;
1246 				   char			*a_target;
1247 				   } */ *a = v;
1248 	struct v7fs_node *parent_node = a->a_dvp->v_data;
1249 	struct v7fs_mount *v7fsmount = parent_node->v7fsmount;
1250 	struct v7fs_self *fs = v7fsmount->core;
1251 	struct vattr *va = a->a_vap;
1252 	kauth_cred_t cr = a->a_cnp->cn_cred;
1253 	struct componentname *cnp = a->a_cnp;
1254 	struct v7fs_fileattr attr;
1255 	v7fs_ino_t ino;
1256 	const char *from = a->a_target;
1257 	const char *to = cnp->cn_nameptr;
1258 	size_t len = strlen(from) + 1;
1259 	int error = 0;
1260 
1261 	if (len > V7FS_BSIZE) { /* limited to 512byte pathname */
1262 		DPRINTF("too long pathname.");
1263 		return ENAMETOOLONG;
1264 	}
1265 
1266 	memset(&attr, 0, sizeof(attr));
1267 	attr.uid = kauth_cred_geteuid(cr);
1268 	attr.gid = kauth_cred_getegid(cr);
1269 	attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type);
1270 
1271 	if ((error = v7fs_file_allocate
1272 		(fs, &parent_node->inode, to, &attr, &ino))) {
1273 		goto unlock_exit;
1274 	}
1275 	/* Sync dirent size change. */
1276 	uvm_vnp_setsize(a->a_dvp, v7fs_inode_filesize(&parent_node->inode));
1277 
1278 	/* Get myself vnode. */
1279 	if ((error = v7fs_vget(v7fsmount->mountp, ino, a->a_vpp))) {
1280 		DPRINTF("can't get vnode.\n");
1281 	}
1282 
1283 	struct v7fs_node *newnode = (*a->a_vpp)->v_data;
1284 	struct v7fs_inode *p = &newnode->inode;
1285 	v7fs_file_symlink(fs, p, from);
1286 	uvm_vnp_setsize(*a->a_vpp, v7fs_inode_filesize(p));
1287 
1288 	newnode->update_ctime = true;
1289 	newnode->update_mtime = true;
1290 	newnode->update_atime = true;
1291 unlock_exit:
1292 	/* unlock parent directory */
1293 	vput(a->a_dvp);
1294 
1295 	return error;
1296 }
1297 
1298 int
1299 v7fs_readlink(void *v)
1300 {
1301 	struct vop_readlink_args /* {
1302 				    struct vnode	*a_vp;
1303 				    struct uio		*a_uio;
1304 				    kauth_cred_t	a_cred;
1305 				    } */ *a = v;
1306 	struct uio *uio = a->a_uio;
1307 	struct vnode *vp = a->a_vp;
1308 	struct v7fs_node *v7node = vp->v_data;
1309 	struct v7fs_inode *inode = &v7node->inode;
1310 	struct v7fs_self *fs = v7node->v7fsmount->core;
1311 	int error = 0;
1312 
1313 	KDASSERT(vp->v_type == VLNK);
1314 	KDASSERT(uio->uio_offset >= 0);
1315 	KDASSERT(v7fs_inode_islnk(inode));
1316 
1317 	v7fs_daddr_t blk = inode->addr[0];
1318 	void *buf;
1319 	if (!(buf = scratch_read(fs, blk))) {
1320 		error = EIO;
1321 		goto error_exit;
1322 	}
1323 
1324 	if ((error = uiomove(buf, strlen(buf), uio))) {
1325 		DPRINTF("uiomove failed.\n");
1326 	}
1327 	scratch_free(fs, buf);
1328 
1329 error_exit:
1330 	return error;
1331 }
1332