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