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