xref: /freebsd/sys/fs/ext2fs/ext2_vnops.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
1 /*-
2  *  modified for EXT2FS support in Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * Copyright (c) 1982, 1986, 1989, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  * (c) UNIX System Laboratories, Inc.
11  * All or some portions of this file are derived from material licensed
12  * to the University of California by American Telephone and Telegraph
13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14  * the permission of UNIX System Laboratories, Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)ufs_vnops.c	8.7 (Berkeley) 2/3/94
41  *	@(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
42  * $FreeBSD$
43  */
44 
45 #include "opt_suiddir.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/stat.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/endian.h>
55 #include <sys/priv.h>
56 #include <sys/mount.h>
57 #include <sys/unistd.h>
58 #include <sys/time.h>
59 #include <sys/vnode.h>
60 #include <sys/namei.h>
61 #include <sys/lockf.h>
62 #include <sys/event.h>
63 #include <sys/conf.h>
64 #include <sys/file.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vnode_pager.h>
71 
72 #include "opt_directio.h"
73 
74 #include <ufs/ufs/dir.h>
75 
76 #include <fs/ext2fs/fs.h>
77 #include <fs/ext2fs/inode.h>
78 #include <fs/ext2fs/ext2_extern.h>
79 #include <fs/ext2fs/ext2fs.h>
80 #include <fs/ext2fs/ext2_dinode.h>
81 #include <fs/ext2fs/ext2_dir.h>
82 #include <fs/ext2fs/ext2_mount.h>
83 
84 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
85 static void ext2_itimes_locked(struct vnode *);
86 
87 static vop_access_t	ext2_access;
88 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
89 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
90     struct thread *);
91 static vop_close_t	ext2_close;
92 static vop_create_t	ext2_create;
93 static vop_fsync_t	ext2_fsync;
94 static vop_getattr_t	ext2_getattr;
95 static vop_link_t	ext2_link;
96 static vop_mkdir_t	ext2_mkdir;
97 static vop_mknod_t	ext2_mknod;
98 static vop_open_t	ext2_open;
99 static vop_pathconf_t	ext2_pathconf;
100 static vop_print_t	ext2_print;
101 static vop_read_t	ext2_read;
102 static vop_readlink_t	ext2_readlink;
103 static vop_remove_t	ext2_remove;
104 static vop_rename_t	ext2_rename;
105 static vop_rmdir_t	ext2_rmdir;
106 static vop_setattr_t	ext2_setattr;
107 static vop_strategy_t	ext2_strategy;
108 static vop_symlink_t	ext2_symlink;
109 static vop_write_t	ext2_write;
110 static vop_vptofh_t	ext2_vptofh;
111 static vop_close_t	ext2fifo_close;
112 static vop_kqfilter_t	ext2fifo_kqfilter;
113 
114 /* Global vfs data structures for ext2. */
115 struct vop_vector ext2_vnodeops = {
116 	.vop_default =		&default_vnodeops,
117 	.vop_access =		ext2_access,
118 	.vop_bmap =		ext2_bmap,
119 	.vop_cachedlookup =	ext2_lookup,
120 	.vop_close =		ext2_close,
121 	.vop_create =		ext2_create,
122 	.vop_fsync =		ext2_fsync,
123 	.vop_getattr =		ext2_getattr,
124 	.vop_inactive =		ext2_inactive,
125 	.vop_link =		ext2_link,
126 	.vop_lookup =		vfs_cache_lookup,
127 	.vop_mkdir =		ext2_mkdir,
128 	.vop_mknod =		ext2_mknod,
129 	.vop_open =		ext2_open,
130 	.vop_pathconf =		ext2_pathconf,
131 	.vop_poll =		vop_stdpoll,
132 	.vop_print =		ext2_print,
133 	.vop_read =		ext2_read,
134 	.vop_readdir =		ext2_readdir,
135 	.vop_readlink =		ext2_readlink,
136 	.vop_reallocblks =	ext2_reallocblks,
137 	.vop_reclaim =		ext2_reclaim,
138 	.vop_remove =		ext2_remove,
139 	.vop_rename =		ext2_rename,
140 	.vop_rmdir =		ext2_rmdir,
141 	.vop_setattr =		ext2_setattr,
142 	.vop_strategy =		ext2_strategy,
143 	.vop_symlink =		ext2_symlink,
144 	.vop_write =		ext2_write,
145 	.vop_vptofh =		ext2_vptofh,
146 };
147 
148 struct vop_vector ext2_fifoops = {
149 	.vop_default =		&fifo_specops,
150 	.vop_access =		ext2_access,
151 	.vop_close =		ext2fifo_close,
152 	.vop_fsync =		ext2_fsync,
153 	.vop_getattr =		ext2_getattr,
154 	.vop_inactive =		ext2_inactive,
155 	.vop_kqfilter =		ext2fifo_kqfilter,
156 	.vop_print =		ext2_print,
157 	.vop_read =		VOP_PANIC,
158 	.vop_reclaim =		ext2_reclaim,
159 	.vop_setattr =		ext2_setattr,
160 	.vop_write =		VOP_PANIC,
161 	.vop_vptofh =		ext2_vptofh,
162 };
163 
164 /*
165  * A virgin directory (no blushing please).
166  * Note that the type and namlen fields are reversed relative to ext2.
167  * Also, we don't use `struct odirtemplate', since it would just cause
168  * endianness problems.
169  */
170 static struct dirtemplate mastertemplate = {
171 	0, 12, 1, EXT2_FT_DIR, ".",
172 	0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
173 };
174 static struct dirtemplate omastertemplate = {
175 	0, 12, 1, EXT2_FT_UNKNOWN, ".",
176 	0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
177 };
178 
179 static void
180 ext2_itimes_locked(struct vnode *vp)
181 {
182 	struct inode *ip;
183 	struct timespec ts;
184 
185 	ASSERT_VI_LOCKED(vp, __func__);
186 
187 	ip = VTOI(vp);
188 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
189 		return;
190 	if ((vp->v_type == VBLK || vp->v_type == VCHR))
191 		ip->i_flag |= IN_LAZYMOD;
192 	else
193 		ip->i_flag |= IN_MODIFIED;
194 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
195 		vfs_timestamp(&ts);
196 		if (ip->i_flag & IN_ACCESS) {
197 			ip->i_atime = ts.tv_sec;
198 			ip->i_atimensec = ts.tv_nsec;
199 		}
200 		if (ip->i_flag & IN_UPDATE) {
201 			ip->i_mtime = ts.tv_sec;
202 			ip->i_mtimensec = ts.tv_nsec;
203 			ip->i_modrev++;
204 		}
205 		if (ip->i_flag & IN_CHANGE) {
206 			ip->i_ctime = ts.tv_sec;
207 			ip->i_ctimensec = ts.tv_nsec;
208 		}
209 	}
210 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
211 }
212 
213 void
214 ext2_itimes(struct vnode *vp)
215 {
216 
217 	VI_LOCK(vp);
218 	ext2_itimes_locked(vp);
219 	VI_UNLOCK(vp);
220 }
221 
222 /*
223  * Create a regular file
224  */
225 static int
226 ext2_create(ap)
227 	struct vop_create_args /* {
228 		struct vnode *a_dvp;
229 		struct vnode **a_vpp;
230 		struct componentname *a_cnp;
231 		struct vattr *a_vap;
232 	} */ *ap;
233 {
234 	int error;
235 
236 	error =
237 	    ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
238 	    ap->a_dvp, ap->a_vpp, ap->a_cnp);
239 	if (error)
240 		return (error);
241 	return (0);
242 }
243 
244 static int
245 ext2_open(ap)
246 	struct vop_open_args /* {
247 		struct vnode *a_vp;
248 		int  a_mode;
249 		struct ucred *a_cred;
250 		struct thread *a_td;
251 	} */ *ap;
252 {
253 
254 	if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
255 		return (EOPNOTSUPP);
256 
257 	/*
258 	 * Files marked append-only must be opened for appending.
259 	 */
260 	if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
261 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
262 		return (EPERM);
263 
264 	vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
265 
266 	return (0);
267 }
268 
269 /*
270  * Close called.
271  *
272  * Update the times on the inode.
273  */
274 static int
275 ext2_close(ap)
276 	struct vop_close_args /* {
277 		struct vnode *a_vp;
278 		int  a_fflag;
279 		struct ucred *a_cred;
280 		struct thread *a_td;
281 	} */ *ap;
282 {
283 	struct vnode *vp = ap->a_vp;
284 
285 	VI_LOCK(vp);
286 	if (vp->v_usecount > 1)
287 		ext2_itimes_locked(vp);
288 	VI_UNLOCK(vp);
289 	return (0);
290 }
291 
292 static int
293 ext2_access(ap)
294 	struct vop_access_args /* {
295 		struct vnode *a_vp;
296 		accmode_t a_accmode;
297 		struct ucred *a_cred;
298 		struct thread *a_td;
299 	} */ *ap;
300 {
301 	struct vnode *vp = ap->a_vp;
302 	struct inode *ip = VTOI(vp);
303 	accmode_t accmode = ap->a_accmode;
304 	int error;
305 
306 	if (vp->v_type == VBLK || vp->v_type == VCHR)
307 		return (EOPNOTSUPP);
308 
309 	/*
310 	 * Disallow write attempts on read-only file systems;
311 	 * unless the file is a socket, fifo, or a block or
312 	 * character device resident on the file system.
313 	 */
314 	if (accmode & VWRITE) {
315 		switch (vp->v_type) {
316 		case VDIR:
317 		case VLNK:
318 		case VREG:
319 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
320 				return (EROFS);
321 			break;
322 		default:
323 			break;
324 		}
325 	}
326 
327 	/* If immutable bit set, nobody gets to write it. */
328 	if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
329 		return (EPERM);
330 
331 	error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
332 	    ap->a_accmode, ap->a_cred, NULL);
333 	return (error);
334 }
335 
336 static int
337 ext2_getattr(ap)
338 	struct vop_getattr_args /* {
339 		struct vnode *a_vp;
340 		struct vattr *a_vap;
341 		struct ucred *a_cred;
342 	} */ *ap;
343 {
344 	struct vnode *vp = ap->a_vp;
345 	struct inode *ip = VTOI(vp);
346 	struct vattr *vap = ap->a_vap;
347 
348 	ext2_itimes(vp);
349 	/*
350 	 * Copy from inode table
351 	 */
352 	vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
353 	vap->va_fileid = ip->i_number;
354 	vap->va_mode = ip->i_mode & ~IFMT;
355 	vap->va_nlink = ip->i_nlink;
356 	vap->va_uid = ip->i_uid;
357 	vap->va_gid = ip->i_gid;
358 	vap->va_rdev = ip->i_rdev;
359 	vap->va_size = ip->i_size;
360 	vap->va_atime.tv_sec = ip->i_atime;
361 	vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
362 	vap->va_mtime.tv_sec = ip->i_mtime;
363 	vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
364 	vap->va_ctime.tv_sec = ip->i_ctime;
365 	vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
366 	if E2DI_HAS_XTIME(ip) {
367 		vap->va_birthtime.tv_sec = ip->i_birthtime;
368 		vap->va_birthtime.tv_nsec = ip->i_birthnsec;
369 	}
370 	vap->va_flags = ip->i_flags;
371 	vap->va_gen = ip->i_gen;
372 	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
373 	vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
374 	vap->va_type = IFTOVT(ip->i_mode);
375 	vap->va_filerev = ip->i_modrev;
376 	return (0);
377 }
378 
379 /*
380  * Set attribute vnode op. called from several syscalls
381  */
382 static int
383 ext2_setattr(ap)
384 	struct vop_setattr_args /* {
385 		struct vnode *a_vp;
386 		struct vattr *a_vap;
387 		struct ucred *a_cred;
388 	} */ *ap;
389 {
390 	struct vattr *vap = ap->a_vap;
391 	struct vnode *vp = ap->a_vp;
392 	struct inode *ip = VTOI(vp);
393 	struct ucred *cred = ap->a_cred;
394 	struct thread *td = curthread;
395 	int error;
396 
397 	/*
398 	 * Check for unsettable attributes.
399 	 */
400 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
401 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
402 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
403 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
404 		return (EINVAL);
405 	}
406 	if (vap->va_flags != VNOVAL) {
407 		/* Disallow flags not supported by ext2fs. */
408 		if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
409 			return (EOPNOTSUPP);
410 
411 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
412 			return (EROFS);
413 		/*
414 		 * Callers may only modify the file flags on objects they
415 		 * have VADMIN rights for.
416 		 */
417 		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
418 			return (error);
419 		/*
420 		 * Unprivileged processes and privileged processes in
421 		 * jail() are not permitted to unset system flags, or
422 		 * modify flags if any system flags are set.
423 		 * Privileged non-jail processes may not modify system flags
424 		 * if securelevel > 0 and any existing system flags are set.
425 		 */
426 		if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
427 			if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
428 				error = securelevel_gt(cred, 0);
429 				if (error)
430 					return (error);
431 			}
432 		} else {
433 			if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
434 			    ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
435 				return (EPERM);
436 		}
437 		ip->i_flags = vap->va_flags;
438 		ip->i_flag |= IN_CHANGE;
439 		if (ip->i_flags & (IMMUTABLE | APPEND))
440 			return (0);
441 	}
442 	if (ip->i_flags & (IMMUTABLE | APPEND))
443 		return (EPERM);
444 	/*
445 	 * Go through the fields and update iff not VNOVAL.
446 	 */
447 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
448 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
449 			return (EROFS);
450 		if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
451 		    td)) != 0)
452 			return (error);
453 	}
454 	if (vap->va_size != VNOVAL) {
455 		/*
456 		 * Disallow write attempts on read-only file systems;
457 		 * unless the file is a socket, fifo, or a block or
458 		 * character device resident on the file system.
459 		 */
460 		switch (vp->v_type) {
461 		case VDIR:
462 			return (EISDIR);
463 		case VLNK:
464 		case VREG:
465 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
466 				return (EROFS);
467 			break;
468 		default:
469 			break;
470 		}
471 		if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
472 			return (error);
473 	}
474 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
475 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
476 			return (EROFS);
477 		/*
478 		 * From utimes(2):
479 		 * If times is NULL, ... The caller must be the owner of
480 		 * the file, have permission to write the file, or be the
481 		 * super-user.
482 		 * If times is non-NULL, ... The caller must be the owner of
483 		 * the file or be the super-user.
484 		 */
485 		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
486 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
487 		    (error = VOP_ACCESS(vp, VWRITE, cred, td))))
488 			return (error);
489 		if (vap->va_atime.tv_sec != VNOVAL)
490 			ip->i_flag |= IN_ACCESS;
491 		if (vap->va_mtime.tv_sec != VNOVAL)
492 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
493 		ext2_itimes(vp);
494 		if (vap->va_atime.tv_sec != VNOVAL) {
495 			ip->i_atime = vap->va_atime.tv_sec;
496 			ip->i_atimensec = vap->va_atime.tv_nsec;
497 		}
498 		if (vap->va_mtime.tv_sec != VNOVAL) {
499 			ip->i_mtime = vap->va_mtime.tv_sec;
500 			ip->i_mtimensec = vap->va_mtime.tv_nsec;
501 		}
502 		ip->i_birthtime = vap->va_birthtime.tv_sec;
503 		ip->i_birthnsec = vap->va_birthtime.tv_nsec;
504 		error = ext2_update(vp, 0);
505 		if (error)
506 			return (error);
507 	}
508 	error = 0;
509 	if (vap->va_mode != (mode_t)VNOVAL) {
510 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
511 			return (EROFS);
512 		error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
513 	}
514 	return (error);
515 }
516 
517 /*
518  * Change the mode on a file.
519  * Inode must be locked before calling.
520  */
521 static int
522 ext2_chmod(vp, mode, cred, td)
523 	struct vnode *vp;
524 	int mode;
525 	struct ucred *cred;
526 	struct thread *td;
527 {
528 	struct inode *ip = VTOI(vp);
529 	int error;
530 
531 	/*
532 	 * To modify the permissions on a file, must possess VADMIN
533 	 * for that file.
534 	 */
535 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
536 		return (error);
537 	/*
538 	 * Privileged processes may set the sticky bit on non-directories,
539 	 * as well as set the setgid bit on a file with a group that the
540 	 * process is not a member of.
541 	 */
542 	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
543 		error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
544 		if (error)
545 			return (EFTYPE);
546 	}
547 	if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
548 		error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
549 		if (error)
550 			return (error);
551 	}
552 	ip->i_mode &= ~ALLPERMS;
553 	ip->i_mode |= (mode & ALLPERMS);
554 	ip->i_flag |= IN_CHANGE;
555 	return (0);
556 }
557 
558 /*
559  * Perform chown operation on inode ip;
560  * inode must be locked prior to call.
561  */
562 static int
563 ext2_chown(vp, uid, gid, cred, td)
564 	struct vnode *vp;
565 	uid_t uid;
566 	gid_t gid;
567 	struct ucred *cred;
568 	struct thread *td;
569 {
570 	struct inode *ip = VTOI(vp);
571 	uid_t ouid;
572 	gid_t ogid;
573 	int error = 0;
574 
575 	if (uid == (uid_t)VNOVAL)
576 		uid = ip->i_uid;
577 	if (gid == (gid_t)VNOVAL)
578 		gid = ip->i_gid;
579 	/*
580 	 * To modify the ownership of a file, must possess VADMIN
581 	 * for that file.
582 	 */
583 	if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
584 		return (error);
585 	/*
586 	 * To change the owner of a file, or change the group of a file
587 	 * to a group of which we are not a member, the caller must
588 	 * have privilege.
589 	 */
590 	if (uid != ip->i_uid || (gid != ip->i_gid &&
591 	    !groupmember(gid, cred))) {
592 		error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
593 		if (error)
594 			return (error);
595 	}
596 	ogid = ip->i_gid;
597 	ouid = ip->i_uid;
598 	ip->i_gid = gid;
599 	ip->i_uid = uid;
600 	ip->i_flag |= IN_CHANGE;
601 	if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
602 		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
603 			ip->i_mode &= ~(ISUID | ISGID);
604 	}
605 	return (0);
606 }
607 
608 /*
609  * Synch an open file.
610  */
611 /* ARGSUSED */
612 static int
613 ext2_fsync(ap)
614 	struct vop_fsync_args /* {
615 		struct vnode *a_vp;
616 		struct ucred *a_cred;
617 		int a_waitfor;
618 		struct thread *a_td;
619 	} */ *ap;
620 {
621 	/*
622 	 * Flush all dirty buffers associated with a vnode.
623 	 */
624 
625 	vop_stdfsync(ap);
626 
627 	return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
628 }
629 
630 /*
631  * Mknod vnode call
632  */
633 /* ARGSUSED */
634 static int
635 ext2_mknod(ap)
636 	struct vop_mknod_args /* {
637 		struct vnode *a_dvp;
638 		struct vnode **a_vpp;
639 		struct componentname *a_cnp;
640 		struct vattr *a_vap;
641 	} */ *ap;
642 {
643 	struct vattr *vap = ap->a_vap;
644 	struct vnode **vpp = ap->a_vpp;
645 	struct inode *ip;
646 	ino_t ino;
647 	int error;
648 
649 	error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
650 	    ap->a_dvp, vpp, ap->a_cnp);
651 	if (error)
652 		return (error);
653 	ip = VTOI(*vpp);
654 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
655 	if (vap->va_rdev != VNOVAL) {
656 		/*
657 		 * Want to be able to use this to make badblock
658 		 * inodes, so don't truncate the dev number.
659 		 */
660 		ip->i_rdev = vap->va_rdev;
661 	}
662 	/*
663 	 * Remove inode, then reload it through VFS_VGET so it is
664 	 * checked to see if it is an alias of an existing entry in
665 	 * the inode cache.	 XXX I don't believe this is necessary now.
666 	 */
667 	(*vpp)->v_type = VNON;
668 	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
669 	vgone(*vpp);
670 	vput(*vpp);
671 	error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
672 	if (error) {
673 		*vpp = NULL;
674 		return (error);
675 	}
676 	return (0);
677 }
678 
679 static int
680 ext2_remove(ap)
681 	struct vop_remove_args /* {
682 		struct vnode *a_dvp;
683 		struct vnode *a_vp;
684 		struct componentname *a_cnp;
685 	} */ *ap;
686 {
687 	struct inode *ip;
688 	struct vnode *vp = ap->a_vp;
689 	struct vnode *dvp = ap->a_dvp;
690 	int error;
691 
692 	ip = VTOI(vp);
693 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
694 	    (VTOI(dvp)->i_flags & APPEND)) {
695 		error = EPERM;
696 		goto out;
697 	}
698 	error = ext2_dirremove(dvp, ap->a_cnp);
699 	if (error == 0) {
700 		ip->i_nlink--;
701 		ip->i_flag |= IN_CHANGE;
702 	}
703 out:
704 	return (error);
705 }
706 
707 /*
708  * link vnode call
709  */
710 static int
711 ext2_link(ap)
712 	struct vop_link_args /* {
713 		struct vnode *a_tdvp;
714 		struct vnode *a_vp;
715 		struct componentname *a_cnp;
716 	} */ *ap;
717 {
718 	struct vnode *vp = ap->a_vp;
719 	struct vnode *tdvp = ap->a_tdvp;
720 	struct componentname *cnp = ap->a_cnp;
721 	struct inode *ip;
722 	int error;
723 
724 #ifdef DIAGNOSTIC
725 	if ((cnp->cn_flags & HASBUF) == 0)
726 		panic("ext2_link: no name");
727 #endif
728 	if (tdvp->v_mount != vp->v_mount) {
729 		error = EXDEV;
730 		goto out;
731 	}
732 	ip = VTOI(vp);
733 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
734 		error = EMLINK;
735 		goto out;
736 	}
737 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
738 		error = EPERM;
739 		goto out;
740 	}
741 	ip->i_nlink++;
742 	ip->i_flag |= IN_CHANGE;
743 	error = ext2_update(vp, !DOINGASYNC(vp));
744 	if (!error)
745 		error = ext2_direnter(ip, tdvp, cnp);
746 	if (error) {
747 		ip->i_nlink--;
748 		ip->i_flag |= IN_CHANGE;
749 	}
750 out:
751 	return (error);
752 }
753 
754 /*
755  * Rename system call.
756  * 	rename("foo", "bar");
757  * is essentially
758  *	unlink("bar");
759  *	link("foo", "bar");
760  *	unlink("foo");
761  * but ``atomically''.  Can't do full commit without saving state in the
762  * inode on disk which isn't feasible at this time.  Best we can do is
763  * always guarantee the target exists.
764  *
765  * Basic algorithm is:
766  *
767  * 1) Bump link count on source while we're linking it to the
768  *    target.  This also ensure the inode won't be deleted out
769  *    from underneath us while we work (it may be truncated by
770  *    a concurrent `trunc' or `open' for creation).
771  * 2) Link source to destination.  If destination already exists,
772  *    delete it first.
773  * 3) Unlink source reference to inode if still around. If a
774  *    directory was moved and the parent of the destination
775  *    is different from the source, patch the ".." entry in the
776  *    directory.
777  */
778 static int
779 ext2_rename(ap)
780 	struct vop_rename_args  /* {
781 		struct vnode *a_fdvp;
782 		struct vnode *a_fvp;
783 		struct componentname *a_fcnp;
784 		struct vnode *a_tdvp;
785 		struct vnode *a_tvp;
786 		struct componentname *a_tcnp;
787 	} */ *ap;
788 {
789 	struct vnode *tvp = ap->a_tvp;
790 	struct vnode *tdvp = ap->a_tdvp;
791 	struct vnode *fvp = ap->a_fvp;
792 	struct vnode *fdvp = ap->a_fdvp;
793 	struct componentname *tcnp = ap->a_tcnp;
794 	struct componentname *fcnp = ap->a_fcnp;
795 	struct inode *ip, *xp, *dp;
796 	struct dirtemplate dirbuf;
797 	int doingdirectory = 0, oldparent = 0, newparent = 0;
798 	int error = 0;
799 	u_char namlen;
800 
801 #ifdef DIAGNOSTIC
802 	if ((tcnp->cn_flags & HASBUF) == 0 ||
803 	    (fcnp->cn_flags & HASBUF) == 0)
804 		panic("ext2_rename: no name");
805 #endif
806 	/*
807 	 * Check for cross-device rename.
808 	 */
809 	if ((fvp->v_mount != tdvp->v_mount) ||
810 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
811 		error = EXDEV;
812 abortit:
813 		if (tdvp == tvp)
814 			vrele(tdvp);
815 		else
816 			vput(tdvp);
817 		if (tvp)
818 			vput(tvp);
819 		vrele(fdvp);
820 		vrele(fvp);
821 		return (error);
822 	}
823 
824 	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
825 	    (VTOI(tdvp)->i_flags & APPEND))) {
826 		error = EPERM;
827 		goto abortit;
828 	}
829 
830 	/*
831 	 * Renaming a file to itself has no effect.  The upper layers should
832 	 * not call us in that case.  Temporarily just warn if they do.
833 	 */
834 	if (fvp == tvp) {
835 		printf("ext2_rename: fvp == tvp (can't happen)\n");
836 		error = 0;
837 		goto abortit;
838 	}
839 
840 	if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
841 		goto abortit;
842 	dp = VTOI(fdvp);
843 	ip = VTOI(fvp);
844  	if (ip->i_nlink >= LINK_MAX) {
845  		VOP_UNLOCK(fvp, 0);
846  		error = EMLINK;
847  		goto abortit;
848  	}
849 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
850 	    || (dp->i_flags & APPEND)) {
851 		VOP_UNLOCK(fvp, 0);
852 		error = EPERM;
853 		goto abortit;
854 	}
855 	if ((ip->i_mode & IFMT) == IFDIR) {
856 		/*
857 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
858 		 */
859 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
860 		    dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
861 		    (ip->i_flag & IN_RENAME)) {
862 			VOP_UNLOCK(fvp, 0);
863 			error = EINVAL;
864 			goto abortit;
865 		}
866 		ip->i_flag |= IN_RENAME;
867 		oldparent = dp->i_number;
868 		doingdirectory++;
869 	}
870 	vrele(fdvp);
871 
872 	/*
873 	 * When the target exists, both the directory
874 	 * and target vnodes are returned locked.
875 	 */
876 	dp = VTOI(tdvp);
877 	xp = NULL;
878 	if (tvp)
879 		xp = VTOI(tvp);
880 
881 	/*
882 	 * 1) Bump link count while we're moving stuff
883 	 *    around.  If we crash somewhere before
884 	 *    completing our work, the link count
885 	 *    may be wrong, but correctable.
886 	 */
887 	ip->i_nlink++;
888 	ip->i_flag |= IN_CHANGE;
889 	if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
890 		VOP_UNLOCK(fvp, 0);
891 		goto bad;
892 	}
893 
894 	/*
895 	 * If ".." must be changed (ie the directory gets a new
896 	 * parent) then the source directory must not be in the
897 	 * directory hierarchy above the target, as this would
898 	 * orphan everything below the source directory. Also
899 	 * the user must have write permission in the source so
900 	 * as to be able to change "..". We must repeat the call
901 	 * to namei, as the parent directory is unlocked by the
902 	 * call to checkpath().
903 	 */
904 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
905 	VOP_UNLOCK(fvp, 0);
906 	if (oldparent != dp->i_number)
907 		newparent = dp->i_number;
908 	if (doingdirectory && newparent) {
909 		if (error)	/* write access check above */
910 			goto bad;
911 		if (xp != NULL)
912 			vput(tvp);
913 		error = ext2_checkpath(ip, dp, tcnp->cn_cred);
914 		if (error)
915 			goto out;
916 		VREF(tdvp);
917 		error = relookup(tdvp, &tvp, tcnp);
918 		if (error)
919 			goto out;
920 		vrele(tdvp);
921 		dp = VTOI(tdvp);
922 		xp = NULL;
923 		if (tvp)
924 			xp = VTOI(tvp);
925 	}
926 	/*
927 	 * 2) If target doesn't exist, link the target
928 	 *    to the source and unlink the source.
929 	 *    Otherwise, rewrite the target directory
930 	 *    entry to reference the source inode and
931 	 *    expunge the original entry's existence.
932 	 */
933 	if (xp == NULL) {
934 		if (dp->i_devvp != ip->i_devvp)
935 			panic("ext2_rename: EXDEV");
936 		/*
937 		 * Account for ".." in new directory.
938 		 * When source and destination have the same
939 		 * parent we don't fool with the link count.
940 		 */
941 		if (doingdirectory && newparent) {
942 			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
943 				error = EMLINK;
944 				goto bad;
945 			}
946 			dp->i_nlink++;
947 			dp->i_flag |= IN_CHANGE;
948 			error = ext2_update(tdvp, !DOINGASYNC(tdvp));
949 			if (error)
950 				goto bad;
951 		}
952 		error = ext2_direnter(ip, tdvp, tcnp);
953 		if (error) {
954 			if (doingdirectory && newparent) {
955 				dp->i_nlink--;
956 				dp->i_flag |= IN_CHANGE;
957 				(void)ext2_update(tdvp, 1);
958 			}
959 			goto bad;
960 		}
961 		vput(tdvp);
962 	} else {
963 		if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
964 		       panic("ext2_rename: EXDEV");
965 		/*
966 		 * Short circuit rename(foo, foo).
967 		 */
968 		if (xp->i_number == ip->i_number)
969 			panic("ext2_rename: same file");
970 		/*
971 		 * If the parent directory is "sticky", then the user must
972 		 * own the parent directory, or the destination of the rename,
973 		 * otherwise the destination may not be changed (except by
974 		 * root). This implements append-only directories.
975 		 */
976 		if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
977 		    tcnp->cn_cred->cr_uid != dp->i_uid &&
978 		    xp->i_uid != tcnp->cn_cred->cr_uid) {
979 			error = EPERM;
980 			goto bad;
981 		}
982 		/*
983 		 * Target must be empty if a directory and have no links
984 		 * to it. Also, ensure source and target are compatible
985 		 * (both directories, or both not directories).
986 		 */
987 		if ((xp->i_mode&IFMT) == IFDIR) {
988 			if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
989 			    xp->i_nlink > 2) {
990 				error = ENOTEMPTY;
991 				goto bad;
992 			}
993 			if (!doingdirectory) {
994 				error = ENOTDIR;
995 				goto bad;
996 			}
997 			cache_purge(tdvp);
998 		} else if (doingdirectory) {
999 			error = EISDIR;
1000 			goto bad;
1001 		}
1002 		error = ext2_dirrewrite(dp, ip, tcnp);
1003 		if (error)
1004 			goto bad;
1005 		/*
1006 		 * If the target directory is in the same
1007 		 * directory as the source directory,
1008 		 * decrement the link count on the parent
1009 		 * of the target directory.
1010 		 */
1011 		if (doingdirectory && !newparent) {
1012 		       dp->i_nlink--;
1013 		       dp->i_flag |= IN_CHANGE;
1014 		}
1015 		vput(tdvp);
1016 		/*
1017 		 * Adjust the link count of the target to
1018 		 * reflect the dirrewrite above.  If this is
1019 		 * a directory it is empty and there are
1020 		 * no links to it, so we can squash the inode and
1021 		 * any space associated with it.  We disallowed
1022 		 * renaming over top of a directory with links to
1023 		 * it above, as the remaining link would point to
1024 		 * a directory without "." or ".." entries.
1025 		 */
1026 		xp->i_nlink--;
1027 		if (doingdirectory) {
1028 			if (--xp->i_nlink != 0)
1029 				panic("ext2_rename: linked directory");
1030 			error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1031 			    tcnp->cn_cred, tcnp->cn_thread);
1032 		}
1033 		xp->i_flag |= IN_CHANGE;
1034 		vput(tvp);
1035 		xp = NULL;
1036 	}
1037 
1038 	/*
1039 	 * 3) Unlink the source.
1040 	 */
1041 	fcnp->cn_flags &= ~MODMASK;
1042 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1043 	VREF(fdvp);
1044 	error = relookup(fdvp, &fvp, fcnp);
1045 	if (error == 0)
1046 		vrele(fdvp);
1047 	if (fvp != NULL) {
1048 		xp = VTOI(fvp);
1049 		dp = VTOI(fdvp);
1050 	} else {
1051 		/*
1052 		 * From name has disappeared.
1053 		 */
1054 		if (doingdirectory)
1055 			panic("ext2_rename: lost dir entry");
1056 		vrele(ap->a_fvp);
1057 		return (0);
1058 	}
1059 	/*
1060 	 * Ensure that the directory entry still exists and has not
1061 	 * changed while the new name has been entered. If the source is
1062 	 * a file then the entry may have been unlinked or renamed. In
1063 	 * either case there is no further work to be done. If the source
1064 	 * is a directory then it cannot have been rmdir'ed; its link
1065 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
1066 	 * The IN_RENAME flag ensures that it cannot be moved by another
1067 	 * rename.
1068 	 */
1069 	if (xp != ip) {
1070 		if (doingdirectory)
1071 			panic("ext2_rename: lost dir entry");
1072 	} else {
1073 		/*
1074 		 * If the source is a directory with a
1075 		 * new parent, the link count of the old
1076 		 * parent directory must be decremented
1077 		 * and ".." set to point to the new parent.
1078 		 */
1079 		if (doingdirectory && newparent) {
1080 			dp->i_nlink--;
1081 			dp->i_flag |= IN_CHANGE;
1082 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1083 				sizeof(struct dirtemplate), (off_t)0,
1084 				UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1085 				tcnp->cn_cred, NOCRED, NULL, NULL);
1086 			if (error == 0) {
1087 				/* Like ufs little-endian: */
1088 				namlen = dirbuf.dotdot_type;
1089 				if (namlen != 2 ||
1090 				    dirbuf.dotdot_name[0] != '.' ||
1091 				    dirbuf.dotdot_name[1] != '.') {
1092 					ext2_dirbad(xp, (doff_t)12,
1093 					    "rename: mangled dir");
1094 				} else {
1095 					dirbuf.dotdot_ino = newparent;
1096 					(void) vn_rdwr(UIO_WRITE, fvp,
1097 					    (caddr_t)&dirbuf,
1098 					    sizeof(struct dirtemplate),
1099 					    (off_t)0, UIO_SYSSPACE,
1100 					    IO_NODELOCKED | IO_SYNC |
1101 					    IO_NOMACCHECK, tcnp->cn_cred,
1102 					    NOCRED, NULL, NULL);
1103 					cache_purge(fdvp);
1104 				}
1105 			}
1106 		}
1107 		error = ext2_dirremove(fdvp, fcnp);
1108 		if (!error) {
1109 			xp->i_nlink--;
1110 			xp->i_flag |= IN_CHANGE;
1111 		}
1112 		xp->i_flag &= ~IN_RENAME;
1113 	}
1114 	if (dp)
1115 		vput(fdvp);
1116 	if (xp)
1117 		vput(fvp);
1118 	vrele(ap->a_fvp);
1119 	return (error);
1120 
1121 bad:
1122 	if (xp)
1123 		vput(ITOV(xp));
1124 	vput(ITOV(dp));
1125 out:
1126 	if (doingdirectory)
1127 		ip->i_flag &= ~IN_RENAME;
1128 	if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1129 		ip->i_nlink--;
1130 		ip->i_flag |= IN_CHANGE;
1131 		ip->i_flag &= ~IN_RENAME;
1132 		vput(fvp);
1133 	} else
1134 		vrele(fvp);
1135 	return (error);
1136 }
1137 
1138 /*
1139  * Mkdir system call
1140  */
1141 static int
1142 ext2_mkdir(ap)
1143 	struct vop_mkdir_args /* {
1144 		struct vnode *a_dvp;
1145 		struct vnode **a_vpp;
1146 		struct componentname *a_cnp;
1147 		struct vattr *a_vap;
1148 	} */ *ap;
1149 {
1150 	struct vnode *dvp = ap->a_dvp;
1151 	struct vattr *vap = ap->a_vap;
1152 	struct componentname *cnp = ap->a_cnp;
1153 	struct inode *ip, *dp;
1154 	struct vnode *tvp;
1155 	struct dirtemplate dirtemplate, *dtp;
1156 	int error, dmode;
1157 
1158 #ifdef DIAGNOSTIC
1159 	if ((cnp->cn_flags & HASBUF) == 0)
1160 		panic("ext2_mkdir: no name");
1161 #endif
1162 	dp = VTOI(dvp);
1163 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1164 		error = EMLINK;
1165 		goto out;
1166 	}
1167 	dmode = vap->va_mode & 0777;
1168 	dmode |= IFDIR;
1169 	/*
1170 	 * Must simulate part of ext2_makeinode here to acquire the inode,
1171 	 * but not have it entered in the parent directory. The entry is
1172 	 * made later after writing "." and ".." entries.
1173 	 */
1174 	error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1175 	if (error)
1176 		goto out;
1177 	ip = VTOI(tvp);
1178 	ip->i_gid = dp->i_gid;
1179 #ifdef SUIDDIR
1180 	{
1181 		/*
1182 		 * if we are hacking owners here, (only do this where told to)
1183 		 * and we are not giving it TOO root, (would subvert quotas)
1184 		 * then go ahead and give it to the other user.
1185 		 * The new directory also inherits the SUID bit.
1186 		 * If user's UID and dir UID are the same,
1187 		 * 'give it away' so that the SUID is still forced on.
1188 		 */
1189 		if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1190 		   (dp->i_mode & ISUID) && dp->i_uid) {
1191 			dmode |= ISUID;
1192 			ip->i_uid = dp->i_uid;
1193 		} else {
1194 			ip->i_uid = cnp->cn_cred->cr_uid;
1195 		}
1196 	}
1197 #else
1198 	ip->i_uid = cnp->cn_cred->cr_uid;
1199 #endif
1200 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1201 	ip->i_mode = dmode;
1202 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1203 	ip->i_nlink = 2;
1204 	if (cnp->cn_flags & ISWHITEOUT)
1205 		ip->i_flags |= UF_OPAQUE;
1206 	error = ext2_update(tvp, 1);
1207 
1208 	/*
1209 	 * Bump link count in parent directory
1210 	 * to reflect work done below.  Should
1211 	 * be done before reference is created
1212 	 * so reparation is possible if we crash.
1213 	 */
1214 	dp->i_nlink++;
1215 	dp->i_flag |= IN_CHANGE;
1216 	error = ext2_update(dvp, !DOINGASYNC(dvp));
1217 	if (error)
1218 		goto bad;
1219 
1220 	/* Initialize directory with "." and ".." from static template. */
1221 	if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1222 	    EXT2F_INCOMPAT_FTYPE))
1223 		dtp = &mastertemplate;
1224 	else
1225 		dtp = &omastertemplate;
1226 	dirtemplate = *dtp;
1227 	dirtemplate.dot_ino = ip->i_number;
1228 	dirtemplate.dotdot_ino = dp->i_number;
1229 	/* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
1230 	 * so let's just redefine it - for this function only
1231 	 */
1232 #undef  DIRBLKSIZ
1233 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
1234 	dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1235 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1236 	    sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE,
1237 	    IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1238 	    NULL, NULL);
1239 	if (error) {
1240 		dp->i_nlink--;
1241 		dp->i_flag |= IN_CHANGE;
1242 		goto bad;
1243 	}
1244 	if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1245 		/* XXX should grow with balloc() */
1246 		panic("ext2_mkdir: blksize");
1247 	else {
1248 		ip->i_size = DIRBLKSIZ;
1249 		ip->i_flag |= IN_CHANGE;
1250 	}
1251 
1252 	/* Directory set up, now install its entry in the parent directory. */
1253 	error = ext2_direnter(ip, dvp, cnp);
1254 	if (error) {
1255 		dp->i_nlink--;
1256 		dp->i_flag |= IN_CHANGE;
1257 	}
1258 bad:
1259 	/*
1260 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1261 	 * for us because we set the link count to 0.
1262 	 */
1263 	if (error) {
1264 		ip->i_nlink = 0;
1265 		ip->i_flag |= IN_CHANGE;
1266 		vput(tvp);
1267 	} else
1268 		*ap->a_vpp = tvp;
1269 out:
1270 	return (error);
1271 #undef  DIRBLKSIZ
1272 #define DIRBLKSIZ  DEV_BSIZE
1273 }
1274 
1275 /*
1276  * Rmdir system call.
1277  */
1278 static int
1279 ext2_rmdir(ap)
1280 	struct vop_rmdir_args /* {
1281 		struct vnode *a_dvp;
1282 		struct vnode *a_vp;
1283 		struct componentname *a_cnp;
1284 	} */ *ap;
1285 {
1286 	struct vnode *vp = ap->a_vp;
1287 	struct vnode *dvp = ap->a_dvp;
1288 	struct componentname *cnp = ap->a_cnp;
1289 	struct inode *ip, *dp;
1290 	int error;
1291 
1292 	ip = VTOI(vp);
1293 	dp = VTOI(dvp);
1294 
1295 	/*
1296 	 * Verify the directory is empty (and valid).
1297 	 * (Rmdir ".." won't be valid since
1298 	 *  ".." will contain a reference to
1299 	 *  the current directory and thus be
1300 	 *  non-empty.)
1301 	 */
1302 	error = 0;
1303 	if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1304 		error = ENOTEMPTY;
1305 		goto out;
1306 	}
1307 	if ((dp->i_flags & APPEND)
1308 	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1309 		error = EPERM;
1310 		goto out;
1311 	}
1312 	/*
1313 	 * Delete reference to directory before purging
1314 	 * inode.  If we crash in between, the directory
1315 	 * will be reattached to lost+found,
1316 	 */
1317 	error = ext2_dirremove(dvp, cnp);
1318 	if (error)
1319 		goto out;
1320 	dp->i_nlink--;
1321 	dp->i_flag |= IN_CHANGE;
1322 	cache_purge(dvp);
1323 	VOP_UNLOCK(dvp, 0);
1324 	/*
1325 	 * Truncate inode.  The only stuff left
1326 	 * in the directory is "." and "..".  The
1327 	 * "." reference is inconsequential since
1328 	 * we're quashing it.  The ".." reference
1329 	 * has already been adjusted above.  We've
1330 	 * removed the "." reference and the reference
1331 	 * in the parent directory, but there may be
1332 	 * other hard links so decrement by 2 and
1333 	 * worry about them later.
1334 	 */
1335 	ip->i_nlink -= 2;
1336 	error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1337 	    cnp->cn_thread);
1338 	cache_purge(ITOV(ip));
1339 	if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1340 		VOP_UNLOCK(vp, 0);
1341 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1342 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1343 	}
1344 out:
1345 	return (error);
1346 }
1347 
1348 /*
1349  * symlink -- make a symbolic link
1350  */
1351 static int
1352 ext2_symlink(ap)
1353 	struct vop_symlink_args /* {
1354 		struct vnode *a_dvp;
1355 		struct vnode **a_vpp;
1356 		struct componentname *a_cnp;
1357 		struct vattr *a_vap;
1358 		char *a_target;
1359 	} */ *ap;
1360 {
1361 	struct vnode *vp, **vpp = ap->a_vpp;
1362 	struct inode *ip;
1363 	int len, error;
1364 
1365 	error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1366 	    vpp, ap->a_cnp);
1367 	if (error)
1368 		return (error);
1369 	vp = *vpp;
1370 	len = strlen(ap->a_target);
1371 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1372 		ip = VTOI(vp);
1373 		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1374 		ip->i_size = len;
1375 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1376 	} else
1377 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1378 		    UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1379 		    ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1380 	if (error)
1381 		vput(vp);
1382 	return (error);
1383 }
1384 
1385 /*
1386  * Return target name of a symbolic link
1387  */
1388 static int
1389 ext2_readlink(ap)
1390 	struct vop_readlink_args /* {
1391 		struct vnode *a_vp;
1392 		struct uio *a_uio;
1393 		struct ucred *a_cred;
1394 	} */ *ap;
1395 {
1396 	struct vnode *vp = ap->a_vp;
1397 	struct inode *ip = VTOI(vp);
1398 	int isize;
1399 
1400 	isize = ip->i_size;
1401 	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1402 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1403 		return (0);
1404 	}
1405 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1406 }
1407 
1408 /*
1409  * Calculate the logical to physical mapping if not done already,
1410  * then call the device strategy routine.
1411  *
1412  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1413  * deadlock on memory.  See ext2_bmap() for details.
1414  */
1415 static int
1416 ext2_strategy(ap)
1417 	struct vop_strategy_args /* {
1418 		struct vnode *a_vp;
1419 		struct buf *a_bp;
1420 	} */ *ap;
1421 {
1422 	struct buf *bp = ap->a_bp;
1423 	struct vnode *vp = ap->a_vp;
1424 	struct inode *ip;
1425 	struct bufobj *bo;
1426 	int32_t blkno;
1427 	int error;
1428 
1429 	ip = VTOI(vp);
1430 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1431 		panic("ext2_strategy: spec");
1432 	if (bp->b_blkno == bp->b_lblkno) {
1433 		error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1434 		bp->b_blkno = blkno;
1435 		if (error) {
1436 			bp->b_error = error;
1437 			bp->b_ioflags |= BIO_ERROR;
1438 			bufdone(bp);
1439 			return (0);
1440 		}
1441 		if ((long)bp->b_blkno == -1)
1442 			vfs_bio_clrbuf(bp);
1443 	}
1444 	if ((long)bp->b_blkno == -1) {
1445 		bufdone(bp);
1446 		return (0);
1447 	}
1448 	bp->b_iooffset = dbtob(bp->b_blkno);
1449 	bo = VFSTOEXT2(vp->v_mount)->um_bo;
1450 	BO_STRATEGY(bo, bp);
1451 	return (0);
1452 }
1453 
1454 /*
1455  * Print out the contents of an inode.
1456  */
1457 static int
1458 ext2_print(ap)
1459 	struct vop_print_args /* {
1460 		struct vnode *a_vp;
1461 	} */ *ap;
1462 {
1463 	struct vnode *vp = ap->a_vp;
1464 	struct inode *ip = VTOI(vp);
1465 
1466 	vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1467 	if (vp->v_type == VFIFO)
1468 		fifo_printinfo(vp);
1469 	printf("\n");
1470 	return (0);
1471 }
1472 
1473 /*
1474  * Close wrapper for fifos.
1475  *
1476  * Update the times on the inode then do device close.
1477  */
1478 static int
1479 ext2fifo_close(ap)
1480 	struct vop_close_args /* {
1481 		struct vnode *a_vp;
1482 		int  a_fflag;
1483 		struct ucred *a_cred;
1484 		struct thread *a_td;
1485 	} */ *ap;
1486 {
1487 	struct vnode *vp = ap->a_vp;
1488 
1489 	VI_LOCK(vp);
1490 	if (vp->v_usecount > 1)
1491 		ext2_itimes_locked(vp);
1492 	VI_UNLOCK(vp);
1493 	return (fifo_specops.vop_close(ap));
1494 }
1495 
1496 /*
1497  * Kqfilter wrapper for fifos.
1498  *
1499  * Fall through to ext2 kqfilter routines if needed
1500  */
1501 static int
1502 ext2fifo_kqfilter(ap)
1503 	struct vop_kqfilter_args *ap;
1504 {
1505 	int error;
1506 
1507 	error = fifo_specops.vop_kqfilter(ap);
1508 	if (error)
1509 		error = vfs_kqfilter(ap);
1510 	return (error);
1511 }
1512 
1513 /*
1514  * Return POSIX pathconf information applicable to ext2 filesystems.
1515  */
1516 static int
1517 ext2_pathconf(ap)
1518 	struct vop_pathconf_args /* {
1519 		struct vnode *a_vp;
1520 		int a_name;
1521 		int *a_retval;
1522 	} */ *ap;
1523 {
1524 
1525 	switch (ap->a_name) {
1526 	case _PC_LINK_MAX:
1527 		*ap->a_retval = LINK_MAX;
1528 		return (0);
1529 	case _PC_NAME_MAX:
1530 		*ap->a_retval = NAME_MAX;
1531 		return (0);
1532 	case _PC_PATH_MAX:
1533 		*ap->a_retval = PATH_MAX;
1534 		return (0);
1535 	case _PC_PIPE_BUF:
1536 		*ap->a_retval = PIPE_BUF;
1537 		return (0);
1538 	case _PC_CHOWN_RESTRICTED:
1539 		*ap->a_retval = 1;
1540 		return (0);
1541 	case _PC_NO_TRUNC:
1542 		*ap->a_retval = 1;
1543 		return (0);
1544 	default:
1545 		return (EINVAL);
1546 	}
1547 	/* NOTREACHED */
1548 }
1549 
1550 /*
1551  * Vnode pointer to File handle
1552  */
1553 /* ARGSUSED */
1554 static int
1555 ext2_vptofh(ap)
1556 	struct vop_vptofh_args /* {
1557 		struct vnode *a_vp;
1558 		struct fid *a_fhp;
1559 	} */ *ap;
1560 {
1561 	struct inode *ip;
1562 	struct ufid *ufhp;
1563 
1564 	ip = VTOI(ap->a_vp);
1565 	ufhp = (struct ufid *)ap->a_fhp;
1566 	ufhp->ufid_len = sizeof(struct ufid);
1567 	ufhp->ufid_ino = ip->i_number;
1568 	ufhp->ufid_gen = ip->i_gen;
1569 	return (0);
1570 }
1571 
1572 /*
1573  * Initialize the vnode associated with a new inode, handle aliased
1574  * vnodes.
1575  */
1576 int
1577 ext2_vinit(mntp, fifoops, vpp)
1578 	struct mount *mntp;
1579 	struct vop_vector *fifoops;
1580 	struct vnode **vpp;
1581 {
1582 	struct inode *ip;
1583 	struct vnode *vp;
1584 
1585 	vp = *vpp;
1586 	ip = VTOI(vp);
1587 	vp->v_type = IFTOVT(ip->i_mode);
1588 	if (vp->v_type == VFIFO)
1589 		vp->v_op = fifoops;
1590 
1591 	if (ip->i_number == EXT2_ROOTINO)
1592 		vp->v_vflag |= VV_ROOT;
1593 	ip->i_modrev = init_va_filerev();
1594 	*vpp = vp;
1595 	return (0);
1596 }
1597 
1598 /*
1599  * Allocate a new inode.
1600  */
1601 static int
1602 ext2_makeinode(mode, dvp, vpp, cnp)
1603 	int mode;
1604 	struct vnode *dvp;
1605 	struct vnode **vpp;
1606 	struct componentname *cnp;
1607 {
1608 	struct inode *ip, *pdir;
1609 	struct vnode *tvp;
1610 	int error;
1611 
1612 	pdir = VTOI(dvp);
1613 #ifdef DIAGNOSTIC
1614 	if ((cnp->cn_flags & HASBUF) == 0)
1615 		panic("ext2_makeinode: no name");
1616 #endif
1617 	*vpp = NULL;
1618 	if ((mode & IFMT) == 0)
1619 		mode |= IFREG;
1620 
1621 	error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1622 	if (error) {
1623 		return (error);
1624 	}
1625 	ip = VTOI(tvp);
1626 	ip->i_gid = pdir->i_gid;
1627 #ifdef SUIDDIR
1628 	{
1629 		/*
1630 		 * if we are
1631 		 * not the owner of the directory,
1632 		 * and we are hacking owners here, (only do this where told to)
1633 		 * and we are not giving it TOO root, (would subvert quotas)
1634 		 * then go ahead and give it to the other user.
1635 		 * Note that this drops off the execute bits for security.
1636 		 */
1637 		if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1638 		     (pdir->i_mode & ISUID) &&
1639 		     (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1640 			ip->i_uid = pdir->i_uid;
1641 			mode &= ~07111;
1642 		} else {
1643 			ip->i_uid = cnp->cn_cred->cr_uid;
1644 		}
1645 	}
1646 #else
1647 	ip->i_uid = cnp->cn_cred->cr_uid;
1648 #endif
1649 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1650 	ip->i_mode = mode;
1651 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
1652 	ip->i_nlink = 1;
1653 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1654 		if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1655 			ip->i_mode &= ~ISGID;
1656 	}
1657 
1658 	if (cnp->cn_flags & ISWHITEOUT)
1659 		ip->i_flags |= UF_OPAQUE;
1660 
1661 	/*
1662 	 * Make sure inode goes to disk before directory entry.
1663 	 */
1664 	error = ext2_update(tvp, !DOINGASYNC(tvp));
1665 	if (error)
1666 		goto bad;
1667 	error = ext2_direnter(ip, dvp, cnp);
1668 	if (error)
1669 		goto bad;
1670 
1671 	*vpp = tvp;
1672 	return (0);
1673 
1674 bad:
1675 	/*
1676 	 * Write error occurred trying to update the inode
1677 	 * or the directory so must deallocate the inode.
1678 	 */
1679 	ip->i_nlink = 0;
1680 	ip->i_flag |= IN_CHANGE;
1681 	vput(tvp);
1682 	return (error);
1683 }
1684 
1685 /*
1686  * Vnode op for reading.
1687  */
1688 static int
1689 ext2_read(ap)
1690 	struct vop_read_args /* {
1691 		struct vnode *a_vp;
1692 		struct uio *a_uio;
1693 		int a_ioflag;
1694 		struct ucred *a_cred;
1695 	} */ *ap;
1696 {
1697 	struct vnode *vp;
1698 	struct inode *ip;
1699 	struct uio *uio;
1700 	struct m_ext2fs *fs;
1701 	struct buf *bp;
1702 	daddr_t lbn, nextlbn;
1703 	off_t bytesinfile;
1704 	long size, xfersize, blkoffset;
1705 	int error, orig_resid, seqcount;
1706 	int ioflag;
1707 
1708 	vp = ap->a_vp;
1709 	uio = ap->a_uio;
1710 	ioflag = ap->a_ioflag;
1711 
1712 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
1713 	ip = VTOI(vp);
1714 
1715 #ifdef INVARIANTS
1716 	if (uio->uio_rw != UIO_READ)
1717 		panic("%s: mode", "ext2_read");
1718 
1719 	if (vp->v_type == VLNK) {
1720 		if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
1721 			panic("%s: short symlink", "ext2_read");
1722 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
1723 		panic("%s: type %d", "ext2_read", vp->v_type);
1724 #endif
1725 	orig_resid = uio->uio_resid;
1726 	KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
1727 	if (orig_resid == 0)
1728 		return (0);
1729 	KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
1730 	fs = ip->i_e2fs;
1731 	if (uio->uio_offset < ip->i_size &&
1732 	    uio->uio_offset >= fs->e2fs_maxfilesize)
1733 	    	return (EOVERFLOW);
1734 
1735 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1736 		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
1737 			break;
1738 		lbn = lblkno(fs, uio->uio_offset);
1739 		nextlbn = lbn + 1;
1740 		size = blksize(fs, ip, lbn);
1741 		blkoffset = blkoff(fs, uio->uio_offset);
1742 
1743 		xfersize = fs->e2fs_fsize - blkoffset;
1744 		if (uio->uio_resid < xfersize)
1745 			xfersize = uio->uio_resid;
1746 		if (bytesinfile < xfersize)
1747 			xfersize = bytesinfile;
1748 
1749 		if (lblktosize(fs, nextlbn) >= ip->i_size)
1750 			error = bread(vp, lbn, size, NOCRED, &bp);
1751 		else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0)
1752 			error = cluster_read(vp, ip->i_size, lbn, size,
1753 			    NOCRED, blkoffset + uio->uio_resid, seqcount, &bp);
1754 		else if (seqcount > 1) {
1755 			int nextsize = blksize(fs, ip, nextlbn);
1756 			error = breadn(vp, lbn,
1757 			    size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1758 		} else
1759 			error = bread(vp, lbn, size, NOCRED, &bp);
1760 		if (error) {
1761 			brelse(bp);
1762 			bp = NULL;
1763 			break;
1764 		}
1765 
1766 		/*
1767 		 * If IO_DIRECT then set B_DIRECT for the buffer.  This
1768 		 * will cause us to attempt to release the buffer later on
1769 		 * and will cause the buffer cache to attempt to free the
1770 		 * underlying pages.
1771 		 */
1772 		if (ioflag & IO_DIRECT)
1773 			bp->b_flags |= B_DIRECT;
1774 
1775 		/*
1776 		 * We should only get non-zero b_resid when an I/O error
1777 		 * has occurred, which should cause us to break above.
1778 		 * However, if the short read did not cause an error,
1779 		 * then we want to ensure that we do not uiomove bad
1780 		 * or uninitialized data.
1781 		 */
1782 		size -= bp->b_resid;
1783 		if (size < xfersize) {
1784 			if (size == 0)
1785 				break;
1786 			xfersize = size;
1787 		}
1788 		error = uiomove((char *)bp->b_data + blkoffset,
1789   			(int)xfersize, uio);
1790 		if (error)
1791 			break;
1792 
1793 		if (ioflag & (IO_VMIO|IO_DIRECT)) {
1794 			/*
1795 			 * If it's VMIO or direct I/O, then we don't
1796 			 * need the buf, mark it available for
1797 			 * freeing. If it's non-direct VMIO, the VM has
1798 			 * the data.
1799 			 */
1800 			bp->b_flags |= B_RELBUF;
1801 			brelse(bp);
1802 		} else {
1803 			/*
1804 			 * Otherwise let whoever
1805 			 * made the request take care of
1806 			 * freeing it. We just queue
1807 			 * it onto another list.
1808 			 */
1809 			bqrelse(bp);
1810 		}
1811 	}
1812 
1813 	/*
1814 	 * This can only happen in the case of an error
1815 	 * because the loop above resets bp to NULL on each iteration
1816 	 * and on normal completion has not set a new value into it.
1817 	 * so it must have come from a 'break' statement
1818 	 */
1819 	if (bp != NULL) {
1820 		if (ioflag & (IO_VMIO|IO_DIRECT)) {
1821 			bp->b_flags |= B_RELBUF;
1822 			brelse(bp);
1823 		} else {
1824 			bqrelse(bp);
1825 		}
1826 	}
1827 
1828 	if ((error == 0 || uio->uio_resid != orig_resid) &&
1829 	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1830 		ip->i_flag |= IN_ACCESS;
1831 	return (error);
1832 }
1833 
1834 /*
1835  * Vnode op for writing.
1836  */
1837 static int
1838 ext2_write(ap)
1839 	struct vop_write_args /* {
1840 		struct vnode *a_vp;
1841 		struct uio *a_uio;
1842 		int a_ioflag;
1843 		struct ucred *a_cred;
1844 	} */ *ap;
1845 {
1846 	struct vnode *vp;
1847 	struct uio *uio;
1848 	struct inode *ip;
1849 	struct m_ext2fs *fs;
1850 	struct buf *bp;
1851 	daddr_t lbn;
1852 	off_t osize;
1853 	int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
1854 
1855 	ioflag = ap->a_ioflag;
1856 	uio = ap->a_uio;
1857 	vp = ap->a_vp;
1858 
1859 	seqcount = ioflag >> IO_SEQSHIFT;
1860 	ip = VTOI(vp);
1861 
1862 #ifdef INVARIANTS
1863 	if (uio->uio_rw != UIO_WRITE)
1864 		panic("%s: mode", "ext2_write");
1865 #endif
1866 
1867 	switch (vp->v_type) {
1868 	case VREG:
1869 		if (ioflag & IO_APPEND)
1870 			uio->uio_offset = ip->i_size;
1871 		if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
1872 			return (EPERM);
1873 		/* FALLTHROUGH */
1874 	case VLNK:
1875 		break;
1876 	case VDIR:
1877 		/* XXX differs from ffs -- this is called from ext2_mkdir(). */
1878 		if ((ioflag & IO_SYNC) == 0)
1879 		panic("ext2_write: nonsync dir write");
1880 		break;
1881 	default:
1882 		panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
1883 		    vp->v_type, (intmax_t)uio->uio_offset,
1884 		    (intmax_t)uio->uio_resid);
1885 	}
1886 
1887 	KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
1888 	KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
1889 	fs = ip->i_e2fs;
1890 	if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
1891 		return (EFBIG);
1892 	/*
1893 	 * Maybe this should be above the vnode op call, but so long as
1894 	 * file servers have no limits, I don't think it matters.
1895 	 */
1896 	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
1897 		return (EFBIG);
1898 
1899 	resid = uio->uio_resid;
1900 	osize = ip->i_size;
1901 	if (seqcount > BA_SEQMAX)
1902 		flags = BA_SEQMAX << BA_SEQSHIFT;
1903 	else
1904 		flags = seqcount << BA_SEQSHIFT;
1905 	if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1906 		flags |= IO_SYNC;
1907 
1908 	for (error = 0; uio->uio_resid > 0;) {
1909 		lbn = lblkno(fs, uio->uio_offset);
1910 		blkoffset = blkoff(fs, uio->uio_offset);
1911 		xfersize = fs->e2fs_fsize - blkoffset;
1912 		if (uio->uio_resid < xfersize)
1913 			xfersize = uio->uio_resid;
1914 		if (uio->uio_offset + xfersize > ip->i_size)
1915 			vnode_pager_setsize(vp, uio->uio_offset + xfersize);
1916 
1917                 /*
1918 		 * We must perform a read-before-write if the transfer size
1919 		 * does not cover the entire buffer.
1920                  */
1921 		if (fs->e2fs_bsize > xfersize)
1922 			flags |= BA_CLRBUF;
1923 		else
1924 			flags &= ~BA_CLRBUF;
1925 		error = ext2_balloc(ip, lbn, blkoffset + xfersize,
1926 		    ap->a_cred, &bp, flags);
1927 		if (error != 0)
1928 			break;
1929 
1930 		/*
1931 		 * If the buffer is not valid and we did not clear garbage
1932 		 * out above, we have to do so here even though the write
1933 		 * covers the entire buffer in order to avoid a mmap()/write
1934 		 * race where another process may see the garbage prior to
1935 		 * the uiomove() for a write replacing it.
1936 		 */
1937 		if ((bp->b_flags & B_CACHE) == 0 && fs->e2fs_bsize <= xfersize)
1938 			vfs_bio_clrbuf(bp);
1939 		if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
1940 			bp->b_flags |= B_NOCACHE;
1941 		if (uio->uio_offset + xfersize > ip->i_size)
1942 			ip->i_size = uio->uio_offset + xfersize;
1943 		size = blksize(fs, ip, lbn) - bp->b_resid;
1944 		if (size < xfersize)
1945 			xfersize = size;
1946 
1947 		error =
1948 		    uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1949 		if (ioflag & (IO_VMIO|IO_DIRECT)) {
1950 			bp->b_flags |= B_RELBUF;
1951 		}
1952 
1953 		/*
1954 		 * If IO_SYNC each buffer is written synchronously.  Otherwise
1955 		 * if we have a severe page deficiency write the buffer
1956 		 * asynchronously.  Otherwise try to cluster, and if that
1957 		 * doesn't do it then either do an async write (if O_DIRECT),
1958 		 * or a delayed write (if not).
1959 		 */
1960 		if (ioflag & IO_SYNC) {
1961 			(void)bwrite(bp);
1962 		} else if (vm_page_count_severe() ||
1963 		    buf_dirty_count_severe() ||
1964 		    (ioflag & IO_ASYNC)) {
1965 			bp->b_flags |= B_CLUSTEROK;
1966 			bawrite(bp);
1967 		} else if (xfersize + blkoffset == fs->e2fs_fsize) {
1968 			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
1969 				bp->b_flags |= B_CLUSTEROK;
1970 				cluster_write(vp, bp, ip->i_size, seqcount);
1971 			} else {
1972 				bawrite(bp);
1973 			}
1974 		} else if (ioflag & IO_DIRECT) {
1975 			bp->b_flags |= B_CLUSTEROK;
1976 			bawrite(bp);
1977 		} else {
1978 			bp->b_flags |= B_CLUSTEROK;
1979 			bdwrite(bp);
1980 		}
1981 		if (error || xfersize == 0)
1982 			break;
1983 	}
1984 	/*
1985 	 * If we successfully wrote any data, and we are not the superuser
1986 	 * we clear the setuid and setgid bits as a precaution against
1987 	 * tampering.
1988 	 */
1989 	if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
1990 	    ap->a_cred) {
1991 		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
1992 			ip->i_mode &= ~(ISUID | ISGID);
1993 	}
1994 	if (error) {
1995 		if (ioflag & IO_UNIT) {
1996 			(void)ext2_truncate(vp, osize,
1997 			    ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
1998 			uio->uio_offset -= resid - uio->uio_resid;
1999 			uio->uio_resid = resid;
2000 		}
2001 	}
2002 	if (uio->uio_resid != resid) {
2003                ip->i_flag |= IN_CHANGE | IN_UPDATE;
2004                if (ioflag & IO_SYNC)
2005                        error = ext2_update(vp, 1);
2006        }
2007 	return (error);
2008 }
2009