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