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