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