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