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