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