xref: /netbsd-src/sys/ufs/lfs/ulfs_vnops.c (revision 200d779b75dbeafa7bc01fd0f60bc61185f6967b)
1 /*	$NetBSD: ulfs_vnops.c,v 1.28 2015/09/01 06:16:59 dholland Exp $	*/
2 /*  from NetBSD: ufs_vnops.c,v 1.213 2013/06/08 05:47:02 kardel Exp  */
3 
4 /*-
5  * Copyright (c) 2008 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Wasabi Systems, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1989, 1993, 1995
35  *	The Regents of the University of California.  All rights reserved.
36  * (c) UNIX System Laboratories, Inc.
37  * All or some portions of this file are derived from material licensed
38  * to the University of California by American Telephone and Telegraph
39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40  * the permission of UNIX System Laboratories, Inc.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)ufs_vnops.c	8.28 (Berkeley) 7/31/95
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: ulfs_vnops.c,v 1.28 2015/09/01 06:16:59 dholland Exp $");
71 
72 #if defined(_KERNEL_OPT)
73 #include "opt_lfs.h"
74 #include "opt_quota.h"
75 #endif
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/namei.h>
80 #include <sys/resourcevar.h>
81 #include <sys/kernel.h>
82 #include <sys/file.h>
83 #include <sys/stat.h>
84 #include <sys/buf.h>
85 #include <sys/proc.h>
86 #include <sys/mount.h>
87 #include <sys/vnode.h>
88 #include <sys/kmem.h>
89 #include <sys/malloc.h>
90 #include <sys/dirent.h>
91 #include <sys/lockf.h>
92 #include <sys/kauth.h>
93 #include <sys/wapbl.h>
94 #include <sys/fstrans.h>
95 
96 #include <miscfs/specfs/specdev.h>
97 #include <miscfs/fifofs/fifo.h>
98 #include <miscfs/genfs/genfs.h>
99 
100 #include <ufs/lfs/lfs_extern.h>
101 #include <ufs/lfs/lfs.h>
102 #include <ufs/lfs/lfs_accessors.h>
103 
104 #include <ufs/lfs/ulfs_inode.h>
105 #include <ufs/lfs/ulfsmount.h>
106 #include <ufs/lfs/ulfs_bswap.h>
107 #include <ufs/lfs/ulfs_extern.h>
108 #ifdef LFS_DIRHASH
109 #include <ufs/lfs/ulfs_dirhash.h>
110 #endif
111 
112 #include <uvm/uvm.h>
113 
114 static int ulfs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
115 static int ulfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
116     struct lwp *);
117 
118 /*
119  * Open called.
120  *
121  * Nothing to do.
122  */
123 /* ARGSUSED */
124 int
125 ulfs_open(void *v)
126 {
127 	struct vop_open_args /* {
128 		struct vnode	*a_vp;
129 		int		a_mode;
130 		kauth_cred_t	a_cred;
131 	} */ *ap = v;
132 
133 	/*
134 	 * Files marked append-only must be opened for appending.
135 	 */
136 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
137 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
138 		return (EPERM);
139 	return (0);
140 }
141 
142 static int
143 ulfs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode,
144     kauth_cred_t cred)
145 {
146 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
147 	int error;
148 #endif
149 
150 	/*
151 	 * Disallow write attempts on read-only file systems;
152 	 * unless the file is a socket, fifo, or a block or
153 	 * character device resident on the file system.
154 	 */
155 	if (mode & VWRITE) {
156 		switch (vp->v_type) {
157 		case VDIR:
158 		case VLNK:
159 		case VREG:
160 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
161 				return (EROFS);
162 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
163 			fstrans_start(vp->v_mount, FSTRANS_SHARED);
164 			error = lfs_chkdq(ip, 0, cred, 0);
165 			fstrans_done(vp->v_mount);
166 			if (error != 0)
167 				return error;
168 #endif
169 			break;
170 		case VBAD:
171 		case VBLK:
172 		case VCHR:
173 		case VSOCK:
174 		case VFIFO:
175 		case VNON:
176 		default:
177 			break;
178 		}
179 	}
180 
181 	/* If it is a snapshot, nobody gets access to it. */
182 	if ((ip->i_flags & SF_SNAPSHOT))
183 		return (EPERM);
184 	/* If immutable bit set, nobody gets to write it. */
185 	if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
186 		return (EPERM);
187 
188 	return 0;
189 }
190 
191 static int
192 ulfs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
193     kauth_cred_t cred)
194 {
195 
196 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
197 	    ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
198 	    ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
199 }
200 
201 int
202 ulfs_access(void *v)
203 {
204 	struct vop_access_args /* {
205 		struct vnode	*a_vp;
206 		int		a_mode;
207 		kauth_cred_t	a_cred;
208 	} */ *ap = v;
209 	struct vnode	*vp;
210 	struct inode	*ip;
211 	mode_t		mode;
212 	int		error;
213 
214 	vp = ap->a_vp;
215 	ip = VTOI(vp);
216 	mode = ap->a_mode;
217 
218 	error = ulfs_check_possible(vp, ip, mode, ap->a_cred);
219 	if (error)
220 		return error;
221 
222 	error = ulfs_check_permitted(vp, ip, mode, ap->a_cred);
223 
224 	return error;
225 }
226 
227 /*
228  * Set attribute vnode op. called from several syscalls
229  */
230 int
231 ulfs_setattr(void *v)
232 {
233 	struct vop_setattr_args /* {
234 		struct vnode	*a_vp;
235 		struct vattr	*a_vap;
236 		kauth_cred_t	a_cred;
237 	} */ *ap = v;
238 	struct vattr	*vap;
239 	struct vnode	*vp;
240 	struct inode	*ip;
241 	struct lfs	*fs;
242 	kauth_cred_t	cred;
243 	struct lwp	*l;
244 	int		error;
245 	kauth_action_t	action;
246 	bool		changing_sysflags;
247 
248 	vap = ap->a_vap;
249 	vp = ap->a_vp;
250 	ip = VTOI(vp);
251 	fs = ip->i_lfs;
252 	cred = ap->a_cred;
253 	l = curlwp;
254 	action = KAUTH_VNODE_WRITE_FLAGS;
255 	changing_sysflags = false;
256 
257 	/*
258 	 * Check for unsettable attributes.
259 	 */
260 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
261 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
262 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
263 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
264 		return (EINVAL);
265 	}
266 
267 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
268 
269 	if (vap->va_flags != VNOVAL) {
270 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
271 			error = EROFS;
272 			goto out;
273 		}
274 
275 		/* Snapshot flag cannot be set or cleared */
276 		if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
277 		    (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
278 			error = EPERM;
279 			goto out;
280 		}
281 
282 		if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
283 			action |= KAUTH_VNODE_HAS_SYSFLAGS;
284 		}
285 
286 		if ((vap->va_flags & SF_SETTABLE) != (ip->i_flags & SF_SETTABLE)) {
287 			action |= KAUTH_VNODE_WRITE_SYSFLAGS;
288 			changing_sysflags = true;
289 		}
290 
291 		error = kauth_authorize_vnode(cred, action, vp, NULL,
292 		    genfs_can_chflags(cred, vp->v_type, ip->i_uid,
293 		    changing_sysflags));
294 		if (error)
295 			goto out;
296 
297 		if (changing_sysflags) {
298 			ip->i_flags = vap->va_flags;
299 			DIP_ASSIGN(ip, flags, ip->i_flags);
300 		} else {
301 			ip->i_flags &= SF_SETTABLE;
302 			ip->i_flags |= (vap->va_flags & UF_SETTABLE);
303 			DIP_ASSIGN(ip, flags, ip->i_flags);
304 		}
305 		ip->i_flag |= IN_CHANGE;
306 		if (vap->va_flags & (IMMUTABLE | APPEND)) {
307 			error = 0;
308 			goto out;
309 		}
310 	}
311 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
312 		error = EPERM;
313 		goto out;
314 	}
315 	/*
316 	 * Go through the fields and update iff not VNOVAL.
317 	 */
318 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
319 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
320 			error = EROFS;
321 			goto out;
322 		}
323 		error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
324 		if (error)
325 			goto out;
326 	}
327 	if (vap->va_size != VNOVAL) {
328 		/*
329 		 * Disallow write attempts on read-only file systems;
330 		 * unless the file is a socket, fifo, or a block or
331 		 * character device resident on the file system.
332 		 */
333 		switch (vp->v_type) {
334 		case VDIR:
335 			error = EISDIR;
336 			goto out;
337 		case VCHR:
338 		case VBLK:
339 		case VFIFO:
340 			break;
341 		case VREG:
342 			if (vp->v_mount->mnt_flag & MNT_RDONLY) {
343 				error = EROFS;
344 				goto out;
345 			}
346 			if ((ip->i_flags & SF_SNAPSHOT) != 0) {
347 				error = EPERM;
348 				goto out;
349 			}
350 			error = lfs_truncate(vp, vap->va_size, 0, cred);
351 			if (error)
352 				goto out;
353 			break;
354 		default:
355 			error = EOPNOTSUPP;
356 			goto out;
357 		}
358 	}
359 	ip = VTOI(vp);
360 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
361 	    vap->va_birthtime.tv_sec != VNOVAL) {
362 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
363 			error = EROFS;
364 			goto out;
365 		}
366 		if ((ip->i_flags & SF_SNAPSHOT) != 0) {
367 			error = EPERM;
368 			goto out;
369 		}
370 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
371 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred));
372 		if (error)
373 			goto out;
374 		if (vap->va_atime.tv_sec != VNOVAL)
375 			if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
376 				ip->i_flag |= IN_ACCESS;
377 		if (vap->va_mtime.tv_sec != VNOVAL) {
378 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
379 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
380 				ip->i_flag |= IN_ACCESS;
381 		}
382 		if (vap->va_birthtime.tv_sec != VNOVAL) {
383 			lfs_dino_setbirthtime(fs, ip->i_din,
384 					      &vap->va_birthtime);
385 		}
386 		error = lfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
387 		if (error)
388 			goto out;
389 	}
390 	error = 0;
391 	if (vap->va_mode != (mode_t)VNOVAL) {
392 		if (vp->v_mount->mnt_flag & MNT_RDONLY) {
393 			error = EROFS;
394 			goto out;
395 		}
396 		if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
397 		    (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
398 		     S_IXOTH | S_IWOTH))) {
399 			error = EPERM;
400 			goto out;
401 		}
402 		error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
403 	}
404 	VN_KNOTE(vp, NOTE_ATTRIB);
405 out:
406 	fstrans_done(vp->v_mount);
407 	return (error);
408 }
409 
410 /*
411  * Change the mode on a file.
412  * Inode must be locked before calling.
413  */
414 static int
415 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
416 {
417 	struct inode	*ip;
418 	int		error;
419 
420 	ip = VTOI(vp);
421 
422 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
423 	    NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode));
424 	if (error)
425 		return (error);
426 
427 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
428 	ip->i_mode &= ~ALLPERMS;
429 	ip->i_mode |= (mode & ALLPERMS);
430 	ip->i_flag |= IN_CHANGE;
431 	DIP_ASSIGN(ip, mode, ip->i_mode);
432 	fstrans_done(vp->v_mount);
433 	return (0);
434 }
435 
436 /*
437  * Perform chown operation on inode ip;
438  * inode must be locked prior to call.
439  */
440 static int
441 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
442     	struct lwp *l)
443 {
444 	struct inode	*ip;
445 	int		error = 0;
446 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
447 	uid_t		ouid;
448 	gid_t		ogid;
449 	int64_t		change;
450 #endif
451 	ip = VTOI(vp);
452 	error = 0;
453 
454 	if (uid == (uid_t)VNOVAL)
455 		uid = ip->i_uid;
456 	if (gid == (gid_t)VNOVAL)
457 		gid = ip->i_gid;
458 
459 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
460 	    NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
461 	if (error)
462 		return (error);
463 
464 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
465 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
466 	ogid = ip->i_gid;
467 	ouid = ip->i_uid;
468 	change = DIP(ip, blocks);
469 	(void) lfs_chkdq(ip, -change, cred, 0);
470 	(void) lfs_chkiq(ip, -1, cred, 0);
471 #endif
472 	ip->i_gid = gid;
473 	DIP_ASSIGN(ip, gid, gid);
474 	ip->i_uid = uid;
475 	DIP_ASSIGN(ip, uid, uid);
476 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
477 	if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
478 		if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
479 			goto good;
480 		else
481 			(void) lfs_chkdq(ip, -change, cred, FORCE);
482 	}
483 	ip->i_gid = ogid;
484 	DIP_ASSIGN(ip, gid, ogid);
485 	ip->i_uid = ouid;
486 	DIP_ASSIGN(ip, uid, ouid);
487 	(void) lfs_chkdq(ip, change, cred, FORCE);
488 	(void) lfs_chkiq(ip, 1, cred, FORCE);
489 	fstrans_done(vp->v_mount);
490 	return (error);
491  good:
492 #endif /* LFS_QUOTA || LFS_QUOTA2 */
493 	ip->i_flag |= IN_CHANGE;
494 	fstrans_done(vp->v_mount);
495 	return (0);
496 }
497 
498 int
499 ulfs_remove(void *v)
500 {
501 	struct vop_remove_args /* {
502 		struct vnode		*a_dvp;
503 		struct vnode		*a_vp;
504 		struct componentname	*a_cnp;
505 	} */ *ap = v;
506 	struct vnode	*vp, *dvp;
507 	struct inode	*ip;
508 	struct mount	*mp;
509 	int		error;
510 	struct ulfs_lookup_results *ulr;
511 
512 	vp = ap->a_vp;
513 	dvp = ap->a_dvp;
514 	ip = VTOI(vp);
515 	mp = dvp->v_mount;
516 	KASSERT(mp == vp->v_mount); /* XXX Not stable without lock.  */
517 
518 	/* XXX should handle this material another way */
519 	ulr = &VTOI(dvp)->i_crap;
520 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
521 
522 	fstrans_start(mp, FSTRANS_SHARED);
523 	if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
524 	    (VTOI(dvp)->i_flags & APPEND))
525 		error = EPERM;
526 	else {
527 		error = ulfs_dirremove(dvp, ulr,
528 				      ip, ap->a_cnp->cn_flags, 0);
529 	}
530 	VN_KNOTE(vp, NOTE_DELETE);
531 	VN_KNOTE(dvp, NOTE_WRITE);
532 	if (dvp == vp)
533 		vrele(vp);
534 	else
535 		vput(vp);
536 	vput(dvp);
537 	fstrans_done(mp);
538 	return (error);
539 }
540 
541 /*
542  * ulfs_link: create hard link.
543  */
544 int
545 ulfs_link(void *v)
546 {
547 	struct vop_link_v2_args /* {
548 		struct vnode *a_dvp;
549 		struct vnode *a_vp;
550 		struct componentname *a_cnp;
551 	} */ *ap = v;
552 	struct vnode *dvp = ap->a_dvp;
553 	struct vnode *vp = ap->a_vp;
554 	struct componentname *cnp = ap->a_cnp;
555 	struct mount *mp = dvp->v_mount;
556 	struct inode *ip;
557 	struct lfs_direct *newdir;
558 	int error;
559 	struct ulfs_lookup_results *ulr;
560 
561 	KASSERT(dvp != vp);
562 	KASSERT(vp->v_type != VDIR);
563 	KASSERT(mp == vp->v_mount); /* XXX Not stable without lock.  */
564 
565 	/* XXX should handle this material another way */
566 	ulr = &VTOI(dvp)->i_crap;
567 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
568 
569 	fstrans_start(mp, FSTRANS_SHARED);
570 	error = vn_lock(vp, LK_EXCLUSIVE);
571 	if (error) {
572 		VOP_ABORTOP(dvp, cnp);
573 		goto out2;
574 	}
575 	ip = VTOI(vp);
576 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
577 		VOP_ABORTOP(dvp, cnp);
578 		error = EMLINK;
579 		goto out1;
580 	}
581 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
582 		VOP_ABORTOP(dvp, cnp);
583 		error = EPERM;
584 		goto out1;
585 	}
586 	ip->i_nlink++;
587 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
588 	ip->i_flag |= IN_CHANGE;
589 	error = lfs_update(vp, NULL, NULL, UPDATE_DIROP);
590 	if (!error) {
591 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
592 		ulfs_makedirentry(ip, cnp, newdir);
593 		error = ulfs_direnter(dvp, ulr, vp, newdir, cnp, NULL);
594 		pool_cache_put(ulfs_direct_cache, newdir);
595 	}
596 	if (error) {
597 		ip->i_nlink--;
598 		DIP_ASSIGN(ip, nlink, ip->i_nlink);
599 		ip->i_flag |= IN_CHANGE;
600 	}
601  out1:
602 	VOP_UNLOCK(vp);
603  out2:
604 	VN_KNOTE(vp, NOTE_LINK);
605 	VN_KNOTE(dvp, NOTE_WRITE);
606 	fstrans_done(mp);
607 	return (error);
608 }
609 
610 /*
611  * whiteout vnode call
612  */
613 int
614 ulfs_whiteout(void *v)
615 {
616 	struct vop_whiteout_args /* {
617 		struct vnode		*a_dvp;
618 		struct componentname	*a_cnp;
619 		int			a_flags;
620 	} */ *ap = v;
621 	struct vnode		*dvp = ap->a_dvp;
622 	struct componentname	*cnp = ap->a_cnp;
623 	struct lfs_direct		*newdir;
624 	int			error;
625 	struct ulfsmount	*ump = VFSTOULFS(dvp->v_mount);
626 	struct lfs *fs = ump->um_lfs;
627 	struct ulfs_lookup_results *ulr;
628 
629 	/* XXX should handle this material another way */
630 	ulr = &VTOI(dvp)->i_crap;
631 	ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
632 
633 	error = 0;
634 	switch (ap->a_flags) {
635 	case LOOKUP:
636 		/* 4.4 format directories support whiteout operations */
637 		if (fs->um_maxsymlinklen > 0)
638 			return (0);
639 		return (EOPNOTSUPP);
640 
641 	case CREATE:
642 		/* create a new directory whiteout */
643 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
644 #ifdef DIAGNOSTIC
645 		if (fs->um_maxsymlinklen <= 0)
646 			panic("ulfs_whiteout: old format filesystem");
647 #endif
648 
649 		newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
650 		ulfs_makedirentry_bytype(fs, cnp, ULFS_WINO, LFS_DT_WHT,
651 					 newdir);
652 		error = ulfs_direnter(dvp, ulr, NULL, newdir, cnp, NULL);
653 		pool_cache_put(ulfs_direct_cache, newdir);
654 		break;
655 
656 	case DELETE:
657 		/* remove an existing directory whiteout */
658 		fstrans_start(dvp->v_mount, FSTRANS_SHARED);
659 #ifdef DIAGNOSTIC
660 		if (fs->um_maxsymlinklen <= 0)
661 			panic("ulfs_whiteout: old format filesystem");
662 #endif
663 
664 		cnp->cn_flags &= ~DOWHITEOUT;
665 		error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
666 		break;
667 	default:
668 		panic("ulfs_whiteout: unknown op");
669 		/* NOTREACHED */
670 	}
671 	fstrans_done(dvp->v_mount);
672 	return (error);
673 }
674 
675 int
676 ulfs_rmdir(void *v)
677 {
678 	struct vop_rmdir_args /* {
679 		struct vnode		*a_dvp;
680 		struct vnode		*a_vp;
681 		struct componentname	*a_cnp;
682 	} */ *ap = v;
683 	struct vnode		*vp, *dvp;
684 	struct componentname	*cnp;
685 	struct inode		*ip, *dp;
686 	int			error;
687 	struct ulfs_lookup_results *ulr;
688 
689 	vp = ap->a_vp;
690 	dvp = ap->a_dvp;
691 	cnp = ap->a_cnp;
692 	ip = VTOI(vp);
693 	dp = VTOI(dvp);
694 
695 	/* XXX should handle this material another way */
696 	ulr = &dp->i_crap;
697 	ULFS_CHECK_CRAPCOUNTER(dp);
698 
699 	/*
700 	 * No rmdir "." or of mounted directories please.
701 	 */
702 	if (dp == ip || vp->v_mountedhere != NULL) {
703 		if (dp == ip)
704 			vrele(dvp);
705 		else
706 			vput(dvp);
707 		vput(vp);
708 		return (EINVAL);
709 	}
710 
711 	fstrans_start(dvp->v_mount, FSTRANS_SHARED);
712 
713 	/*
714 	 * Do not remove a directory that is in the process of being renamed.
715 	 * Verify that the directory is empty (and valid). (Rmdir ".." won't
716 	 * be valid since ".." will contain a reference to the current
717 	 * directory and thus be non-empty.)
718 	 */
719 	error = 0;
720 	if (ip->i_nlink != 2 ||
721 	    !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
722 		error = ENOTEMPTY;
723 		goto out;
724 	}
725 	if ((dp->i_flags & APPEND) ||
726 		(ip->i_flags & (IMMUTABLE | APPEND))) {
727 		error = EPERM;
728 		goto out;
729 	}
730 	/*
731 	 * Delete reference to directory before purging
732 	 * inode.  If we crash in between, the directory
733 	 * will be reattached to lost+found,
734 	 */
735 	error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
736 	if (error) {
737 		goto out;
738 	}
739 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
740 	cache_purge(dvp);
741 	/*
742 	 * Truncate inode.  The only stuff left in the directory is "." and
743 	 * "..".  The "." reference is inconsequential since we're quashing
744 	 * it.
745 	 */
746 	dp->i_nlink--;
747 	DIP_ASSIGN(dp, nlink, dp->i_nlink);
748 	dp->i_flag |= IN_CHANGE;
749 	ip->i_nlink--;
750 	DIP_ASSIGN(ip, nlink, ip->i_nlink);
751 	ip->i_flag |= IN_CHANGE;
752 	error = lfs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
753 	cache_purge(vp);
754 #ifdef LFS_DIRHASH
755 	if (ip->i_dirhash != NULL)
756 		ulfsdirhash_free(ip);
757 #endif
758  out:
759 	VN_KNOTE(vp, NOTE_DELETE);
760 	vput(vp);
761 	fstrans_done(dvp->v_mount);
762 	vput(dvp);
763 	return (error);
764 }
765 
766 /*
767  * Vnode op for reading directories.
768  *
769  * This routine handles converting from the on-disk directory format
770  * "struct lfs_direct" to the in-memory format "struct dirent" as well as
771  * byte swapping the entries if necessary.
772  */
773 int
774 ulfs_readdir(void *v)
775 {
776 	struct vop_readdir_args /* {
777 		struct vnode	*a_vp;
778 		struct uio	*a_uio;
779 		kauth_cred_t	a_cred;
780 		int		*a_eofflag;
781 		off_t		**a_cookies;
782 		int		*ncookies;
783 	} */ *ap = v;
784 	struct vnode	*vp = ap->a_vp;
785 	struct lfs_direct	*cdp, *ecdp;
786 	struct dirent	*ndp;
787 	char		*cdbuf, *ndbuf, *endp;
788 	struct uio	auio, *uio;
789 	struct iovec	aiov;
790 	int		error;
791 	size_t		count, ccount, rcount, cdbufsz, ndbufsz;
792 	off_t		off, *ccp;
793 	off_t		startoff;
794 	size_t		skipbytes;
795 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
796 	struct lfs *fs = ump->um_lfs;
797 	int nswap = ULFS_MPNEEDSWAP(fs);
798 	uio = ap->a_uio;
799 	count = uio->uio_resid;
800 	rcount = count - ((uio->uio_offset + count) & (fs->um_dirblksiz - 1));
801 
802 	if (rcount < _DIRENT_MINSIZE(cdp) || count < _DIRENT_MINSIZE(ndp))
803 		return EINVAL;
804 
805 	startoff = uio->uio_offset & ~(fs->um_dirblksiz - 1);
806 	skipbytes = uio->uio_offset - startoff;
807 	rcount += skipbytes;
808 
809 	auio.uio_iov = &aiov;
810 	auio.uio_iovcnt = 1;
811 	auio.uio_offset = startoff;
812 	auio.uio_resid = rcount;
813 	UIO_SETUP_SYSSPACE(&auio);
814 	auio.uio_rw = UIO_READ;
815 	cdbufsz = rcount;
816 	cdbuf = kmem_alloc(cdbufsz, KM_SLEEP);
817 	aiov.iov_base = cdbuf;
818 	aiov.iov_len = rcount;
819 	error = VOP_READ(vp, &auio, 0, ap->a_cred);
820 	if (error != 0) {
821 		kmem_free(cdbuf, cdbufsz);
822 		return error;
823 	}
824 
825 	rcount -= auio.uio_resid;
826 
827 	cdp = (struct lfs_direct *)(void *)cdbuf;
828 	ecdp = (struct lfs_direct *)(void *)&cdbuf[rcount];
829 
830 	ndbufsz = count;
831 	ndbuf = kmem_alloc(ndbufsz, KM_SLEEP);
832 	ndp = (struct dirent *)(void *)ndbuf;
833 	endp = &ndbuf[count];
834 
835 	off = uio->uio_offset;
836 	if (ap->a_cookies) {
837 		ccount = rcount / _DIRENT_RECLEN(cdp, 1);
838 		ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp),
839 		    M_TEMP, M_WAITOK);
840 	} else {
841 		/* XXX: GCC */
842 		ccount = 0;
843 		ccp = NULL;
844 	}
845 
846 	while (cdp < ecdp) {
847 		cdp->d_reclen = ulfs_rw16(cdp->d_reclen, nswap);
848 		if (skipbytes > 0) {
849 			if (cdp->d_reclen <= skipbytes) {
850 				skipbytes -= cdp->d_reclen;
851 				cdp = _DIRENT_NEXT(cdp);
852 				continue;
853 			}
854 			/*
855 			 * invalid cookie.
856 			 */
857 			error = EINVAL;
858 			goto out;
859 		}
860 		if (cdp->d_reclen == 0) {
861 			struct dirent *ondp = ndp;
862 			ndp->d_reclen = _DIRENT_MINSIZE(ndp);
863 			ndp = _DIRENT_NEXT(ndp);
864 			ondp->d_reclen = 0;
865 			cdp = ecdp;
866 			break;
867 		}
868 		ndp->d_type = lfs_dir_gettype(fs, cdp);
869 		ndp->d_namlen = lfs_dir_getnamlen(fs, cdp);
870 		ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen);
871 		if ((char *)(void *)ndp + ndp->d_reclen +
872 		    _DIRENT_MINSIZE(ndp) > endp)
873 			break;
874 		ndp->d_fileno = ulfs_rw32(cdp->d_ino, nswap);
875 		(void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen);
876 		memset(&ndp->d_name[ndp->d_namlen], 0,
877 		    ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen);
878 		off += cdp->d_reclen;
879 		if (ap->a_cookies) {
880 			KASSERT(ccp - *(ap->a_cookies) < ccount);
881 			*(ccp++) = off;
882 		}
883 		ndp = _DIRENT_NEXT(ndp);
884 		cdp = _DIRENT_NEXT(cdp);
885 	}
886 
887 	count = ((char *)(void *)ndp - ndbuf);
888 	error = uiomove(ndbuf, count, uio);
889 out:
890 	if (ap->a_cookies) {
891 		if (error) {
892 			free(*(ap->a_cookies), M_TEMP);
893 			*(ap->a_cookies) = NULL;
894 			*(ap->a_ncookies) = 0;
895 		} else {
896 			*ap->a_ncookies = ccp - *(ap->a_cookies);
897 		}
898 	}
899 	uio->uio_offset = off;
900 	kmem_free(ndbuf, ndbufsz);
901 	kmem_free(cdbuf, cdbufsz);
902 	*ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
903 	return error;
904 }
905 
906 /*
907  * Return target name of a symbolic link
908  */
909 int
910 ulfs_readlink(void *v)
911 {
912 	struct vop_readlink_args /* {
913 		struct vnode	*a_vp;
914 		struct uio	*a_uio;
915 		kauth_cred_t	a_cred;
916 	} */ *ap = v;
917 	struct vnode	*vp = ap->a_vp;
918 	struct inode	*ip = VTOI(vp);
919 	struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
920 	struct lfs *fs = ump->um_lfs;
921 	int		isize;
922 
923 	isize = ip->i_size;
924 	if (isize < fs->um_maxsymlinklen ||
925 	    (fs->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
926 		uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
927 		return (0);
928 	}
929 	return (lfs_bufrd(vp, ap->a_uio, 0, ap->a_cred));
930 }
931 
932 /*
933  * Print out the contents of an inode.
934  */
935 int
936 ulfs_print(void *v)
937 {
938 	struct vop_print_args /* {
939 		struct vnode	*a_vp;
940 	} */ *ap = v;
941 	struct vnode	*vp;
942 	struct inode	*ip;
943 
944 	vp = ap->a_vp;
945 	ip = VTOI(vp);
946 	printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
947 	    (unsigned long long)ip->i_number,
948 	    (unsigned long long)major(ip->i_dev),
949 	    (unsigned long long)minor(ip->i_dev));
950 	printf(" flags 0x%x, nlink %d\n",
951 	    ip->i_flag, ip->i_nlink);
952 	printf("\tmode 0%o, owner %d, group %d, size %qd",
953 	    ip->i_mode, ip->i_uid, ip->i_gid,
954 	    (long long)ip->i_size);
955 	if (vp->v_type == VFIFO)
956 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
957 	printf("\n");
958 	return (0);
959 }
960 
961 /*
962  * Read wrapper for special devices.
963  */
964 int
965 ulfsspec_read(void *v)
966 {
967 	struct vop_read_args /* {
968 		struct vnode	*a_vp;
969 		struct uio	*a_uio;
970 		int		a_ioflag;
971 		kauth_cred_t	a_cred;
972 	} */ *ap = v;
973 
974 	/*
975 	 * Set access flag.
976 	 */
977 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
978 		VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
979 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
980 }
981 
982 /*
983  * Write wrapper for special devices.
984  */
985 int
986 ulfsspec_write(void *v)
987 {
988 	struct vop_write_args /* {
989 		struct vnode	*a_vp;
990 		struct uio	*a_uio;
991 		int		a_ioflag;
992 		kauth_cred_t	a_cred;
993 	} */ *ap = v;
994 
995 	/*
996 	 * Set update and change flags.
997 	 */
998 	if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
999 		VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
1000 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1001 }
1002 
1003 /*
1004  * Read wrapper for fifo's
1005  */
1006 int
1007 ulfsfifo_read(void *v)
1008 {
1009 	struct vop_read_args /* {
1010 		struct vnode	*a_vp;
1011 		struct uio	*a_uio;
1012 		int		a_ioflag;
1013 		kauth_cred_t	a_cred;
1014 	} */ *ap = v;
1015 
1016 	/*
1017 	 * Set access flag.
1018 	 */
1019 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1020 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1021 }
1022 
1023 /*
1024  * Write wrapper for fifo's.
1025  */
1026 int
1027 ulfsfifo_write(void *v)
1028 {
1029 	struct vop_write_args /* {
1030 		struct vnode	*a_vp;
1031 		struct uio	*a_uio;
1032 		int		a_ioflag;
1033 		kauth_cred_t	a_cred;
1034 	} */ *ap = v;
1035 
1036 	/*
1037 	 * Set update and change flags.
1038 	 */
1039 	VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
1040 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1041 }
1042 
1043 /*
1044  * Return POSIX pathconf information applicable to ulfs filesystems.
1045  */
1046 int
1047 ulfs_pathconf(void *v)
1048 {
1049 	struct vop_pathconf_args /* {
1050 		struct vnode	*a_vp;
1051 		int		a_name;
1052 		register_t	*a_retval;
1053 	} */ *ap = v;
1054 
1055 	switch (ap->a_name) {
1056 	case _PC_LINK_MAX:
1057 		*ap->a_retval = LINK_MAX;
1058 		return (0);
1059 	case _PC_NAME_MAX:
1060 		*ap->a_retval = LFS_MAXNAMLEN;
1061 		return (0);
1062 	case _PC_PATH_MAX:
1063 		*ap->a_retval = PATH_MAX;
1064 		return (0);
1065 	case _PC_PIPE_BUF:
1066 		*ap->a_retval = PIPE_BUF;
1067 		return (0);
1068 	case _PC_CHOWN_RESTRICTED:
1069 		*ap->a_retval = 1;
1070 		return (0);
1071 	case _PC_NO_TRUNC:
1072 		*ap->a_retval = 1;
1073 		return (0);
1074 	case _PC_SYNC_IO:
1075 		*ap->a_retval = 1;
1076 		return (0);
1077 	case _PC_FILESIZEBITS:
1078 		*ap->a_retval = 42;
1079 		return (0);
1080 	case _PC_SYMLINK_MAX:
1081 		*ap->a_retval = MAXPATHLEN;
1082 		return (0);
1083 	case _PC_2_SYMLINKS:
1084 		*ap->a_retval = 1;
1085 		return (0);
1086 	default:
1087 		return (EINVAL);
1088 	}
1089 	/* NOTREACHED */
1090 }
1091 
1092 /*
1093  * Advisory record locking support
1094  */
1095 int
1096 ulfs_advlock(void *v)
1097 {
1098 	struct vop_advlock_args /* {
1099 		struct vnode	*a_vp;
1100 		void *		a_id;
1101 		int		a_op;
1102 		struct flock	*a_fl;
1103 		int		a_flags;
1104 	} */ *ap = v;
1105 	struct inode *ip;
1106 
1107 	ip = VTOI(ap->a_vp);
1108 	return lf_advlock(ap, &ip->i_lockf, ip->i_size);
1109 }
1110 
1111 /*
1112  * Initialize the vnode associated with a new inode, handle aliased
1113  * vnodes.
1114  */
1115 void
1116 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
1117 	struct vnode **vpp)
1118 {
1119 	struct timeval	tv;
1120 	struct inode	*ip;
1121 	struct vnode	*vp;
1122 	dev_t		rdev;
1123 	struct ulfsmount *ump;
1124 
1125 	vp = *vpp;
1126 	ip = VTOI(vp);
1127 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
1128 	case VCHR:
1129 	case VBLK:
1130 		vp->v_op = specops;
1131 		ump = ip->i_ump;
1132 		// XXX clean this up
1133 		if (ump->um_fstype == ULFS1)
1134 			rdev = (dev_t)ulfs_rw32(ip->i_din->u_32.di_rdev,
1135 			    ULFS_MPNEEDSWAP(ump->um_lfs));
1136 		else
1137 			rdev = (dev_t)ulfs_rw64(ip->i_din->u_64.di_rdev,
1138 			    ULFS_MPNEEDSWAP(ump->um_lfs));
1139 		spec_node_init(vp, rdev);
1140 		break;
1141 	case VFIFO:
1142 		vp->v_op = fifoops;
1143 		break;
1144 	case VNON:
1145 	case VBAD:
1146 	case VSOCK:
1147 	case VLNK:
1148 	case VDIR:
1149 	case VREG:
1150 		break;
1151 	}
1152 	if (ip->i_number == ULFS_ROOTINO)
1153                 vp->v_vflag |= VV_ROOT;
1154 	/*
1155 	 * Initialize modrev times
1156 	 */
1157 	getmicrouptime(&tv);
1158 	ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
1159 			| tv.tv_usec * 4294u;
1160 	*vpp = vp;
1161 }
1162 
1163 /*
1164  * Allocate a new inode.
1165  */
1166 int
1167 ulfs_makeinode(struct vattr *vap, struct vnode *dvp,
1168 	const struct ulfs_lookup_results *ulr,
1169 	struct vnode **vpp, struct componentname *cnp)
1170 {
1171 	struct inode	*ip;
1172 	struct lfs_direct	*newdir;
1173 	struct vnode	*tvp;
1174 	int		error;
1175 
1176 	error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, &tvp);
1177 	if (error)
1178 		return error;
1179 	error = vn_lock(tvp, LK_EXCLUSIVE);
1180 	if (error) {
1181 		vrele(tvp);
1182 		return error;
1183 	}
1184 	lfs_mark_vnode(tvp);
1185 	*vpp = tvp;
1186 	ip = VTOI(tvp);
1187 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1188 	ip->i_nlink = 1;
1189 	DIP_ASSIGN(ip, nlink, 1);
1190 
1191 	/* Authorize setting SGID if needed. */
1192 	if (ip->i_mode & ISGID) {
1193 		error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
1194 		    tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
1195 		    ip->i_gid, MAKEIMODE(vap->va_type, vap->va_mode)));
1196 		if (error) {
1197 			ip->i_mode &= ~ISGID;
1198 			DIP_ASSIGN(ip, mode, ip->i_mode);
1199 		}
1200 	}
1201 
1202 	if (cnp->cn_flags & ISWHITEOUT) {
1203 		ip->i_flags |= UF_OPAQUE;
1204 		DIP_ASSIGN(ip, flags, ip->i_flags);
1205 	}
1206 
1207 	/*
1208 	 * Make sure inode goes to disk before directory entry.
1209 	 */
1210 	if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0)
1211 		goto bad;
1212 	newdir = pool_cache_get(ulfs_direct_cache, PR_WAITOK);
1213 	ulfs_makedirentry(ip, cnp, newdir);
1214 	error = ulfs_direnter(dvp, ulr, tvp, newdir, cnp, NULL);
1215 	pool_cache_put(ulfs_direct_cache, newdir);
1216 	if (error)
1217 		goto bad;
1218 	*vpp = tvp;
1219 	return (0);
1220 
1221  bad:
1222 	/*
1223 	 * Write error occurred trying to update the inode
1224 	 * or the directory so must deallocate the inode.
1225 	 */
1226 	ip->i_nlink = 0;
1227 	DIP_ASSIGN(ip, nlink, 0);
1228 	ip->i_flag |= IN_CHANGE;
1229 	/* If IN_ADIROP, account for it */
1230 	lfs_unmark_vnode(tvp);
1231 	vput(tvp);
1232 	return (error);
1233 }
1234 
1235 /*
1236  * Allocate len bytes at offset off.
1237  */
1238 int
1239 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
1240     kauth_cred_t cred)
1241 {
1242         struct inode *ip = VTOI(vp);
1243         int error, delta, bshift, bsize;
1244         UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
1245 
1246         error = 0;
1247         bshift = vp->v_mount->mnt_fs_bshift;
1248         bsize = 1 << bshift;
1249 
1250         delta = off & (bsize - 1);
1251         off -= delta;
1252         len += delta;
1253 
1254         while (len > 0) {
1255                 bsize = MIN(bsize, len);
1256 
1257                 error = lfs_balloc(vp, off, bsize, cred, flags, NULL);
1258                 if (error) {
1259                         goto out;
1260                 }
1261 
1262                 /*
1263                  * increase file size now, lfs_balloc() requires that
1264                  * EOF be up-to-date before each call.
1265                  */
1266 
1267                 if (ip->i_size < off + bsize) {
1268                         UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
1269                             vp, ip->i_size, off + bsize, 0);
1270                         ip->i_size = off + bsize;
1271 			DIP_ASSIGN(ip, size, ip->i_size);
1272                 }
1273 
1274                 off += bsize;
1275                 len -= bsize;
1276         }
1277 
1278 out:
1279 	return error;
1280 }
1281 
1282 void
1283 ulfs_gop_markupdate(struct vnode *vp, int flags)
1284 {
1285 	u_int32_t mask = 0;
1286 
1287 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1288 		mask = IN_ACCESS;
1289 	}
1290 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1291 		if (vp->v_type == VREG) {
1292 			mask |= IN_CHANGE | IN_UPDATE;
1293 		} else {
1294 			mask |= IN_MODIFY;
1295 		}
1296 	}
1297 	if (mask) {
1298 		struct inode *ip = VTOI(vp);
1299 
1300 		ip->i_flag |= mask;
1301 	}
1302 }
1303 
1304 int
1305 ulfs_bufio(enum uio_rw rw, struct vnode *vp, void *buf, size_t len, off_t off,
1306     int ioflg, kauth_cred_t cred, size_t *aresid, struct lwp *l)
1307 {
1308 	struct iovec iov;
1309 	struct uio uio;
1310 	int error;
1311 
1312 	KASSERT(ISSET(ioflg, IO_NODELOCKED));
1313 	KASSERT(VOP_ISLOCKED(vp));
1314 	KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1315 
1316 	iov.iov_base = buf;
1317 	iov.iov_len = len;
1318 	uio.uio_iov = &iov;
1319 	uio.uio_iovcnt = 1;
1320 	uio.uio_resid = len;
1321 	uio.uio_offset = off;
1322 	uio.uio_rw = rw;
1323 	UIO_SETUP_SYSSPACE(&uio);
1324 
1325 	switch (rw) {
1326 	case UIO_READ:
1327 		error = lfs_bufrd(vp, &uio, ioflg, cred);
1328 		break;
1329 	case UIO_WRITE:
1330 		error = lfs_bufwr(vp, &uio, ioflg, cred);
1331 		break;
1332 	default:
1333 		panic("invalid uio rw: %d", (int)rw);
1334 	}
1335 
1336 	if (aresid)
1337 		*aresid = uio.uio_resid;
1338 	else if (uio.uio_resid && error == 0)
1339 		error = EIO;
1340 
1341 	KASSERT(VOP_ISLOCKED(vp));
1342 	KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1343 	return error;
1344 }
1345