xref: /freebsd-13-stable/sys/ufs/ufs/ufs_vnops.c (revision b525eaaa4aafeee857d1abef27d7ddf1af76846f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)ufs_vnops.c	8.27 (Berkeley) 5/27/95
37  */
38 
39 #include <sys/cdefs.h>
40 #include "opt_quota.h"
41 #include "opt_suiddir.h"
42 #include "opt_ufs.h"
43 #include "opt_ffs.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/namei.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/filio.h>
52 #include <sys/stat.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/mount.h>
56 #include <sys/priv.h>
57 #include <sys/refcount.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/lockf.h>
62 #include <sys/conf.h>
63 #include <sys/acl.h>
64 #include <sys/smr.h>
65 
66 #include <security/audit/audit.h>
67 #include <security/mac/mac_framework.h>
68 
69 #include <sys/file.h>		/* XXX */
70 
71 #include <vm/vm.h>
72 #include <vm/vm_extern.h>
73 
74 #include <ufs/ufs/acl.h>
75 #include <ufs/ufs/extattr.h>
76 #include <ufs/ufs/quota.h>
77 #include <ufs/ufs/inode.h>
78 #include <ufs/ufs/dir.h>
79 #include <ufs/ufs/ufsmount.h>
80 #include <ufs/ufs/ufs_extern.h>
81 #ifdef UFS_DIRHASH
82 #include <ufs/ufs/dirhash.h>
83 #endif
84 #ifdef UFS_GJOURNAL
85 #include <ufs/ufs/gjournal.h>
86 FEATURE(ufs_gjournal, "Journaling support through GEOM for UFS");
87 #endif
88 
89 #ifdef QUOTA
90 FEATURE(ufs_quota, "UFS disk quotas support");
91 FEATURE(ufs_quota64, "64bit UFS disk quotas support");
92 #endif
93 
94 #ifdef SUIDDIR
95 FEATURE(suiddir,
96     "Give all new files in directory the same ownership as the directory");
97 #endif
98 
99 VFS_SMR_DECLARE;
100 
101 #include <ufs/ffs/ffs_extern.h>
102 
103 static vop_accessx_t	ufs_accessx;
104 static vop_fplookup_vexec_t ufs_fplookup_vexec;
105 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
106 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
107     struct thread *);
108 static vop_close_t	ufs_close;
109 static vop_create_t	ufs_create;
110 static vop_stat_t	ufs_stat;
111 static vop_getattr_t	ufs_getattr;
112 static vop_ioctl_t	ufs_ioctl;
113 static vop_link_t	ufs_link;
114 static int ufs_makeinode(int mode, struct vnode *, struct vnode **,
115     struct componentname *, const char *);
116 static vop_mmapped_t	ufs_mmapped;
117 static vop_mkdir_t	ufs_mkdir;
118 static vop_mknod_t	ufs_mknod;
119 static vop_open_t	ufs_open;
120 static vop_pathconf_t	ufs_pathconf;
121 static vop_print_t	ufs_print;
122 static vop_readlink_t	ufs_readlink;
123 static vop_remove_t	ufs_remove;
124 static vop_rename_t	ufs_rename;
125 static vop_rmdir_t	ufs_rmdir;
126 static vop_setattr_t	ufs_setattr;
127 static vop_strategy_t	ufs_strategy;
128 static vop_symlink_t	ufs_symlink;
129 static vop_whiteout_t	ufs_whiteout;
130 static vop_close_t	ufsfifo_close;
131 
132 SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
133     "UFS filesystem");
134 
135 /*
136  * A virgin directory (no blushing please).
137  */
138 static struct dirtemplate mastertemplate = {
139 	0, 12, DT_DIR, 1, ".",
140 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
141 };
142 static struct odirtemplate omastertemplate = {
143 	0, 12, 1, ".",
144 	0, DIRBLKSIZ - 12, 2, ".."
145 };
146 
147 static void
ufs_itimes_locked(struct vnode * vp)148 ufs_itimes_locked(struct vnode *vp)
149 {
150 	struct inode *ip;
151 	struct timespec ts;
152 
153 	ASSERT_VI_LOCKED(vp, __func__);
154 
155 	ip = VTOI(vp);
156 	if (UFS_RDONLY(ip))
157 		goto out;
158 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
159 		return;
160 
161 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
162 		UFS_INODE_SET_FLAG(ip, IN_LAZYMOD);
163 	else if (((vp->v_mount->mnt_kern_flag &
164 		    (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
165 		    (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
166 		UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
167 	else if (ip->i_flag & IN_ACCESS)
168 		UFS_INODE_SET_FLAG(ip, IN_LAZYACCESS);
169 	vfs_timestamp(&ts);
170 	if (ip->i_flag & IN_ACCESS) {
171 		DIP_SET(ip, i_atime, ts.tv_sec);
172 		DIP_SET(ip, i_atimensec, ts.tv_nsec);
173 	}
174 	if (ip->i_flag & IN_UPDATE) {
175 		DIP_SET(ip, i_mtime, ts.tv_sec);
176 		DIP_SET(ip, i_mtimensec, ts.tv_nsec);
177 	}
178 	if (ip->i_flag & IN_CHANGE) {
179 		DIP_SET(ip, i_ctime, ts.tv_sec);
180 		DIP_SET(ip, i_ctimensec, ts.tv_nsec);
181 		DIP_SET(ip, i_modrev, DIP(ip, i_modrev) + 1);
182 	}
183 
184  out:
185 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
186 }
187 
188 void
ufs_itimes(struct vnode * vp)189 ufs_itimes(struct vnode *vp)
190 {
191 	struct inode *ip;
192 
193 	ip = VTOI(vp);
194 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
195 		return;
196 
197 	VI_LOCK(vp);
198 	ufs_itimes_locked(vp);
199 	VI_UNLOCK(vp);
200 }
201 
202 static int
ufs_sync_nlink1(struct mount * mp)203 ufs_sync_nlink1(struct mount *mp)
204 {
205 	int error;
206 
207 	error = vfs_busy(mp, 0);
208 	if (error == 0) {
209 		VFS_SYNC(mp, MNT_WAIT);
210 		vfs_unbusy(mp);
211 		error = ERELOOKUP;
212 	}
213 	vfs_rel(mp);
214 	return (error);
215 }
216 
217 static int
ufs_sync_nlink(struct vnode * vp,struct vnode * vp1)218 ufs_sync_nlink(struct vnode *vp, struct vnode *vp1)
219 {
220 	struct inode *ip;
221 	struct mount *mp;
222 	int error;
223 
224 	ip = VTOI(vp);
225 	if (ip->i_nlink < UFS_LINK_MAX)
226 		return (0);
227 	if (!DOINGSOFTDEP(vp) || ip->i_effnlink >= UFS_LINK_MAX)
228 		return (EMLINK);
229 
230 	mp = vp->v_mount;
231 	vfs_ref(mp);
232 	VOP_UNLOCK(vp);
233 	if (vp1 != NULL)
234 		VOP_UNLOCK(vp1);
235 	error = ufs_sync_nlink1(mp);
236 	vn_lock_pair(vp, false, LK_EXCLUSIVE, vp1, false, LK_EXCLUSIVE);
237 	return (error);
238 }
239 
240 /*
241  * Create a regular file
242  */
243 static int
ufs_create(struct vop_create_args * ap)244 ufs_create(
245 	struct vop_create_args /* {
246 		struct vnode *a_dvp;
247 		struct vnode **a_vpp;
248 		struct componentname *a_cnp;
249 		struct vattr *a_vap;
250 	} */ *ap)
251 {
252 	int error;
253 
254 	error =
255 	    ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
256 	    ap->a_dvp, ap->a_vpp, ap->a_cnp, "ufs_create");
257 	if (error != 0)
258 		return (error);
259 	if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
260 		cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
261 	return (0);
262 }
263 
264 /*
265  * Mknod vnode call
266  */
267 /* ARGSUSED */
268 static int
ufs_mknod(struct vop_mknod_args * ap)269 ufs_mknod(
270 	struct vop_mknod_args /* {
271 		struct vnode *a_dvp;
272 		struct vnode **a_vpp;
273 		struct componentname *a_cnp;
274 		struct vattr *a_vap;
275 	} */ *ap)
276 {
277 	struct vattr *vap = ap->a_vap;
278 	struct vnode **vpp = ap->a_vpp;
279 	struct inode *ip;
280 	ino_t ino;
281 	int error;
282 
283 	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
284 	    ap->a_dvp, vpp, ap->a_cnp, "ufs_mknod");
285 	if (error)
286 		return (error);
287 	ip = VTOI(*vpp);
288 	UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
289 	if (vap->va_rdev != VNOVAL) {
290 		/*
291 		 * Want to be able to use this to make badblock
292 		 * inodes, so don't truncate the dev number.
293 		 */
294 		DIP_SET(ip, i_rdev, vap->va_rdev);
295 	}
296 	/*
297 	 * Remove inode, then reload it through VFS_VGET().  This is
298 	 * needed to do further inode initialization, for instance
299 	 * fifo, which was too early for VFS_VGET() done as part of
300 	 * UFS_VALLOC().
301 	 */
302 	(*vpp)->v_type = VNON;
303 	ino = ip->i_number;	/* Save this before vgone() invalidates ip. */
304 	vgone(*vpp);
305 	vput(*vpp);
306 	error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
307 	if (error) {
308 		*vpp = NULL;
309 		return (error);
310 	}
311 	return (0);
312 }
313 
314 /*
315  * Open called.
316  */
317 /* ARGSUSED */
318 static int
ufs_open(struct vop_open_args * ap)319 ufs_open(struct vop_open_args *ap)
320 {
321 	struct vnode *vp = ap->a_vp;
322 	struct inode *ip;
323 
324 	if (vp->v_type == VCHR || vp->v_type == VBLK)
325 		return (EOPNOTSUPP);
326 
327 	ip = VTOI(vp);
328 	vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
329 	if (vp->v_type == VREG && (vn_irflag_read(vp) & VIRF_PGREAD) == 0) {
330 		vn_irflag_set_cond(vp, VIRF_PGREAD);
331 	}
332 
333 	/*
334 	 * Files marked append-only must be opened for appending.
335 	 */
336 	if ((ip->i_flags & APPEND) &&
337 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
338 		return (EPERM);
339 
340 	return (0);
341 }
342 
343 /*
344  * Close called.
345  *
346  * Update the times on the inode.
347  */
348 /* ARGSUSED */
349 static int
ufs_close(struct vop_close_args * ap)350 ufs_close(
351 	struct vop_close_args /* {
352 		struct vnode *a_vp;
353 		int  a_fflag;
354 		struct ucred *a_cred;
355 		struct thread *a_td;
356 	} */ *ap)
357 {
358 	struct vnode *vp = ap->a_vp;
359 
360 	ufs_itimes(vp);
361 	return (0);
362 }
363 
364 static int
ufs_accessx(struct vop_accessx_args * ap)365 ufs_accessx(
366 	struct vop_accessx_args /* {
367 		struct vnode *a_vp;
368 		accmode_t a_accmode;
369 		struct ucred *a_cred;
370 		struct thread *a_td;
371 	} */ *ap)
372 {
373 	struct vnode *vp = ap->a_vp;
374 	struct inode *ip = VTOI(vp);
375 	accmode_t accmode = ap->a_accmode;
376 	int error;
377 #ifdef UFS_ACL
378 	struct acl *acl;
379 	acl_type_t type;
380 #endif
381 
382 	/*
383 	 * Disallow write attempts on read-only filesystems;
384 	 * unless the file is a socket, fifo, or a block or
385 	 * character device resident on the filesystem.
386 	 */
387 	if (accmode & VMODIFY_PERMS) {
388 		switch (vp->v_type) {
389 		case VDIR:
390 		case VLNK:
391 		case VREG:
392 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
393 				return (EROFS);
394 #ifdef QUOTA
395 			/*
396 			 * Inode is accounted in the quotas only if struct
397 			 * dquot is attached to it. VOP_ACCESS() is called
398 			 * from vn_open_cred() and provides a convenient
399 			 * point to call getinoquota().  The lock mode is
400 			 * exclusive when the file is opening for write.
401 			 */
402 			if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
403 				error = getinoquota(ip);
404 				if (error != 0)
405 					return (error);
406 			}
407 #endif
408 			break;
409 		default:
410 			break;
411 		}
412 	}
413 
414 	/*
415 	 * If immutable bit set, nobody gets to write it.  "& ~VADMIN_PERMS"
416 	 * permits the owner of the file to remove the IMMUTABLE flag.
417 	 */
418 	if ((accmode & (VMODIFY_PERMS & ~VADMIN_PERMS)) &&
419 	    (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
420 		return (EPERM);
421 
422 #ifdef UFS_ACL
423 	if ((vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) != 0) {
424 		if (vp->v_mount->mnt_flag & MNT_NFS4ACLS)
425 			type = ACL_TYPE_NFS4;
426 		else
427 			type = ACL_TYPE_ACCESS;
428 
429 		acl = acl_alloc(M_WAITOK);
430 		if (type == ACL_TYPE_NFS4)
431 			error = ufs_getacl_nfs4_internal(vp, acl, ap->a_td);
432 		else
433 			error = VOP_GETACL(vp, type, acl, ap->a_cred, ap->a_td);
434 		switch (error) {
435 		case 0:
436 			if (type == ACL_TYPE_NFS4) {
437 				error = vaccess_acl_nfs4(vp->v_type, ip->i_uid,
438 				    ip->i_gid, acl, accmode, ap->a_cred);
439 			} else {
440 				error = vfs_unixify_accmode(&accmode);
441 				if (error == 0)
442 					error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
443 					    ip->i_gid, acl, accmode, ap->a_cred);
444 			}
445 			break;
446 		default:
447 			if (error != EOPNOTSUPP)
448 				printf(
449 "ufs_accessx(): Error retrieving ACL on object (%d).\n",
450 				    error);
451 			/*
452 			 * XXX: Fall back until debugged.  Should
453 			 * eventually possibly log an error, and return
454 			 * EPERM for safety.
455 			 */
456 			error = vfs_unixify_accmode(&accmode);
457 			if (error == 0)
458 				error = vaccess(vp->v_type, ip->i_mode,
459 				    ip->i_uid, ip->i_gid, accmode, ap->a_cred);
460 		}
461 		acl_free(acl);
462 
463 		return (error);
464 	}
465 #endif /* !UFS_ACL */
466 	error = vfs_unixify_accmode(&accmode);
467 	if (error == 0)
468 		error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
469 		    accmode, ap->a_cred);
470 	return (error);
471 }
472 
473 /*
474  * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
475  * the comment above cache_fplookup for details.
476  */
477 static int
ufs_fplookup_vexec(struct vop_fplookup_vexec_args * ap)478 ufs_fplookup_vexec(
479 	struct vop_fplookup_vexec_args /* {
480 		struct vnode *a_vp;
481 		struct ucred *a_cred;
482 		struct thread *a_td;
483 	} */ *ap)
484 {
485 	struct vnode *vp;
486 	struct inode *ip;
487 	struct ucred *cred;
488 	mode_t all_x, mode;
489 
490 	vp = ap->a_vp;
491 	ip = VTOI_SMR(vp);
492 	if (__predict_false(ip == NULL))
493 		return (EAGAIN);
494 
495 	/*
496 	 * XXX ACL race
497 	 *
498 	 * ACLs are not supported and UFS clears/sets this flag on mount and
499 	 * remount. However, we may still be racing with seeing them and there
500 	 * is no provision to make sure they were accounted for. This matches
501 	 * the behavior of the locked case, since the lookup there is also
502 	 * racy: mount takes no measures to block anyone from progressing.
503 	 */
504 	all_x = S_IXUSR | S_IXGRP | S_IXOTH;
505 	mode = atomic_load_short(&ip->i_mode);
506 	if (__predict_true((mode & all_x) == all_x))
507 		return (0);
508 
509 	cred = ap->a_cred;
510 	return (vaccess_vexec_smr(mode, ip->i_uid, ip->i_gid, cred));
511 }
512 
513 /* ARGSUSED */
514 static int
ufs_stat(struct vop_stat_args * ap)515 ufs_stat(struct vop_stat_args *ap)
516 {
517 	struct vnode *vp = ap->a_vp;
518 	struct inode *ip = VTOI(vp);
519 	struct stat *sb = ap->a_sb;
520 	int error;
521 
522 	error = vop_stat_helper_pre(ap);
523 	if (__predict_false(error))
524 		return (error);
525 
526 	VI_LOCK(vp);
527 	ufs_itimes_locked(vp);
528 	if (I_IS_UFS1(ip)) {
529 		sb->st_atim.tv_sec = ip->i_din1->di_atime;
530 		sb->st_atim.tv_nsec = ip->i_din1->di_atimensec;
531 	} else {
532 		sb->st_atim.tv_sec = ip->i_din2->di_atime;
533 		sb->st_atim.tv_nsec = ip->i_din2->di_atimensec;
534 	}
535 	VI_UNLOCK(vp);
536 
537 	sb->st_dev = dev2udev(ITOUMP(ip)->um_dev);
538 	sb->st_ino = ip->i_number;
539 	sb->st_mode = (ip->i_mode & ~IFMT) | VTTOIF(vp->v_type);
540 	sb->st_nlink = ip->i_effnlink;
541 	sb->st_uid = ip->i_uid;
542 	sb->st_gid = ip->i_gid;
543 	if (I_IS_UFS1(ip)) {
544 		sb->st_rdev = ip->i_din1->di_rdev;
545 		sb->st_size = ip->i_din1->di_size;
546 		sb->st_mtim.tv_sec = ip->i_din1->di_mtime;
547 		sb->st_mtim.tv_nsec = ip->i_din1->di_mtimensec;
548 		sb->st_ctim.tv_sec = ip->i_din1->di_ctime;
549 		sb->st_ctim.tv_nsec = ip->i_din1->di_ctimensec;
550 		sb->st_birthtim.tv_sec = -1;
551 		sb->st_birthtim.tv_nsec = 0;
552 		sb->st_blocks = dbtob((uint64_t)ip->i_din1->di_blocks) / S_BLKSIZE;
553 	} else {
554 		sb->st_rdev = ip->i_din2->di_rdev;
555 		sb->st_size = ip->i_din2->di_size;
556 		sb->st_mtim.tv_sec = ip->i_din2->di_mtime;
557 		sb->st_mtim.tv_nsec = ip->i_din2->di_mtimensec;
558 		sb->st_ctim.tv_sec = ip->i_din2->di_ctime;
559 		sb->st_ctim.tv_nsec = ip->i_din2->di_ctimensec;
560 		sb->st_birthtim.tv_sec = ip->i_din2->di_birthtime;
561 		sb->st_birthtim.tv_nsec = ip->i_din2->di_birthnsec;
562 		sb->st_blocks = dbtob((uint64_t)ip->i_din2->di_blocks) / S_BLKSIZE;
563 	}
564 
565 	sb->st_blksize = max(PAGE_SIZE, vp->v_mount->mnt_stat.f_iosize);
566 	sb->st_flags = ip->i_flags;
567 	sb->st_gen = ip->i_gen;
568 
569 	return (vop_stat_helper_post(ap, error));
570 }
571 
572 /* ARGSUSED */
573 static int
ufs_getattr(struct vop_getattr_args * ap)574 ufs_getattr(
575 	struct vop_getattr_args /* {
576 		struct vnode *a_vp;
577 		struct vattr *a_vap;
578 		struct ucred *a_cred;
579 	} */ *ap)
580 {
581 	struct vnode *vp = ap->a_vp;
582 	struct inode *ip = VTOI(vp);
583 	struct vattr *vap = ap->a_vap;
584 
585 	VI_LOCK(vp);
586 	ufs_itimes_locked(vp);
587 	if (I_IS_UFS1(ip)) {
588 		vap->va_atime.tv_sec = ip->i_din1->di_atime;
589 		vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
590 	} else {
591 		vap->va_atime.tv_sec = ip->i_din2->di_atime;
592 		vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
593 	}
594 	VI_UNLOCK(vp);
595 	/*
596 	 * Copy from inode table
597 	 */
598 	vap->va_fsid = dev2udev(ITOUMP(ip)->um_dev);
599 	vap->va_fileid = ip->i_number;
600 	vap->va_mode = ip->i_mode & ~IFMT;
601 	vap->va_nlink = ip->i_effnlink;
602 	vap->va_uid = ip->i_uid;
603 	vap->va_gid = ip->i_gid;
604 	if (I_IS_UFS1(ip)) {
605 		vap->va_rdev = ip->i_din1->di_rdev;
606 		vap->va_size = ip->i_din1->di_size;
607 		vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
608 		vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
609 		vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
610 		vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
611 		vap->va_bytes = dbtob((uint64_t)ip->i_din1->di_blocks);
612 		vap->va_filerev = ip->i_din1->di_modrev;
613 	} else {
614 		vap->va_rdev = ip->i_din2->di_rdev;
615 		vap->va_size = ip->i_din2->di_size;
616 		vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
617 		vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
618 		vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
619 		vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
620 		vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
621 		vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
622 		vap->va_bytes = dbtob((uint64_t)ip->i_din2->di_blocks);
623 		vap->va_filerev = ip->i_din2->di_modrev;
624 	}
625 	vap->va_flags = ip->i_flags;
626 	vap->va_gen = ip->i_gen;
627 	vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
628 	vap->va_type = IFTOVT(ip->i_mode);
629 	return (0);
630 }
631 
632 /*
633  * Set attribute vnode op. called from several syscalls
634  */
635 static int
ufs_setattr(struct vop_setattr_args * ap)636 ufs_setattr(
637 	struct vop_setattr_args /* {
638 		struct vnode *a_vp;
639 		struct vattr *a_vap;
640 		struct ucred *a_cred;
641 	} */ *ap)
642 {
643 	struct vattr *vap = ap->a_vap;
644 	struct vnode *vp = ap->a_vp;
645 	struct inode *ip = VTOI(vp);
646 	struct ucred *cred = ap->a_cred;
647 	struct thread *td = curthread;
648 	int error;
649 
650 	/*
651 	 * Check for unsettable attributes.
652 	 */
653 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
654 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
655 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
656 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
657 		return (EINVAL);
658 	}
659 	if (vap->va_flags != VNOVAL) {
660 		if ((vap->va_flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE |
661 		    SF_NOUNLINK | SF_SNAPSHOT | UF_APPEND | UF_ARCHIVE |
662 		    UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP | UF_NOUNLINK |
663 		    UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
664 		    UF_SPARSE | UF_SYSTEM)) != 0)
665 			return (EOPNOTSUPP);
666 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
667 			return (EROFS);
668 		/*
669 		 * Callers may only modify the file flags on objects they
670 		 * have VADMIN rights for.
671 		 */
672 		if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
673 			return (error);
674 		/*
675 		 * Unprivileged processes are not permitted to unset system
676 		 * flags, or modify flags if any system flags are set.
677 		 * Privileged non-jail processes may not modify system flags
678 		 * if securelevel > 0 and any existing system flags are set.
679 		 * Privileged jail processes behave like privileged non-jail
680 		 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
681 		 * otherwise, they behave like unprivileged processes.
682 		 */
683 		if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
684 			if (ip->i_flags &
685 			    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
686 				error = securelevel_gt(cred, 0);
687 				if (error)
688 					return (error);
689 			}
690 			/* The snapshot flag cannot be toggled. */
691 			if ((vap->va_flags ^ ip->i_flags) & SF_SNAPSHOT)
692 				return (EPERM);
693 		} else {
694 			if (ip->i_flags &
695 			    (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
696 			    ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
697 				return (EPERM);
698 		}
699 		ip->i_flags = vap->va_flags;
700 		DIP_SET(ip, i_flags, vap->va_flags);
701 		UFS_INODE_SET_FLAG(ip, IN_CHANGE);
702 		error = UFS_UPDATE(vp, 0);
703 		if (ip->i_flags & (IMMUTABLE | APPEND))
704 			return (error);
705 	}
706 	/*
707 	 * If immutable or append, no one can change any of its attributes
708 	 * except the ones already handled (in some cases, file flags
709 	 * including the immutability flags themselves for the superuser).
710 	 */
711 	if (ip->i_flags & (IMMUTABLE | APPEND))
712 		return (EPERM);
713 	/*
714 	 * Go through the fields and update iff not VNOVAL.
715 	 */
716 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
717 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
718 			return (EROFS);
719 		if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
720 		    td)) != 0)
721 			return (error);
722 	}
723 	if (vap->va_size != VNOVAL) {
724 		/*
725 		 * XXX most of the following special cases should be in
726 		 * callers instead of in N filesystems.  The VDIR check
727 		 * mostly already is.
728 		 */
729 		switch (vp->v_type) {
730 		case VDIR:
731 			return (EISDIR);
732 		case VLNK:
733 		case VREG:
734 			/*
735 			 * Truncation should have an effect in these cases.
736 			 * Disallow it if the filesystem is read-only or
737 			 * the file is being snapshotted.
738 			 */
739 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
740 				return (EROFS);
741 			if (IS_SNAPSHOT(ip))
742 				return (EPERM);
743 			break;
744 		default:
745 			/*
746 			 * According to POSIX, the result is unspecified
747 			 * for file types other than regular files,
748 			 * directories and shared memory objects.  We
749 			 * don't support shared memory objects in the file
750 			 * system, and have dubious support for truncating
751 			 * symlinks.  Just ignore the request in other cases.
752 			 */
753 			return (0);
754 		}
755 		error = vn_rlimit_trunc(vap->va_size, td);
756 		if (error != 0)
757 			return (error);
758 		if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL |
759 		    ((vap->va_vaflags & VA_SYNC) != 0 ? IO_SYNC : 0),
760 		    cred)) != 0)
761 			return (error);
762 	}
763 	if (vap->va_atime.tv_sec != VNOVAL ||
764 	    vap->va_mtime.tv_sec != VNOVAL ||
765 	    vap->va_birthtime.tv_sec != VNOVAL) {
766 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
767 			return (EROFS);
768 		if (IS_SNAPSHOT(ip))
769 			return (EPERM);
770 		error = vn_utimes_perm(vp, vap, cred, td);
771 		if (error != 0)
772 			return (error);
773 		UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_MODIFIED);
774 		if (vap->va_atime.tv_sec != VNOVAL) {
775 			ip->i_flag &= ~IN_ACCESS;
776 			DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
777 			DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
778 		}
779 		if (vap->va_mtime.tv_sec != VNOVAL) {
780 			ip->i_flag &= ~IN_UPDATE;
781 			DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
782 			DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
783 		}
784 		if (vap->va_birthtime.tv_sec != VNOVAL && I_IS_UFS2(ip)) {
785 			ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
786 			ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
787 		}
788 		error = UFS_UPDATE(vp, 0);
789 		if (error)
790 			return (error);
791 	}
792 	error = 0;
793 	if (vap->va_mode != (mode_t)VNOVAL) {
794 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
795 			return (EROFS);
796 		if (IS_SNAPSHOT(ip) && (vap->va_mode & (S_IXUSR | S_IWUSR |
797 		    S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)) != 0)
798 			return (EPERM);
799 		error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
800 	}
801 	return (error);
802 }
803 
804 #ifdef UFS_ACL
805 static int
ufs_update_nfs4_acl_after_mode_change(struct vnode * vp,int mode,int file_owner_id,struct ucred * cred,struct thread * td)806 ufs_update_nfs4_acl_after_mode_change(struct vnode *vp, int mode,
807     int file_owner_id, struct ucred *cred, struct thread *td)
808 {
809 	int error;
810 	struct acl *aclp;
811 
812 	aclp = acl_alloc(M_WAITOK);
813 	error = ufs_getacl_nfs4_internal(vp, aclp, td);
814 	/*
815 	 * We don't have to handle EOPNOTSUPP here, as the filesystem claims
816 	 * it supports ACLs.
817 	 */
818 	if (error)
819 		goto out;
820 
821 	acl_nfs4_sync_acl_from_mode(aclp, mode, file_owner_id);
822 	error = ufs_setacl_nfs4_internal(vp, aclp, td);
823 
824 out:
825 	acl_free(aclp);
826 	return (error);
827 }
828 #endif /* UFS_ACL */
829 
830 static int
ufs_mmapped(struct vop_mmapped_args * ap)831 ufs_mmapped(
832 	struct vop_mmapped_args /* {
833 		struct vnode *a_vp;
834 	} */ *ap)
835 {
836 	struct vnode *vp;
837 	struct inode *ip;
838 	struct mount *mp;
839 
840 	vp = ap->a_vp;
841 	ip = VTOI(vp);
842 	mp = vp->v_mount;
843 
844 	if ((mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
845 		UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
846 	/*
847 	 * XXXKIB No UFS_UPDATE(ap->a_vp, 0) there.
848 	 */
849 	return (0);
850 }
851 
852 /*
853  * Change the mode on a file.
854  * Inode must be locked before calling.
855  */
856 static int
ufs_chmod(struct vnode * vp,int mode,struct ucred * cred,struct thread * td)857 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
858 {
859 	struct inode *ip = VTOI(vp);
860 	int newmode, error;
861 
862 	/*
863 	 * To modify the permissions on a file, must possess VADMIN
864 	 * for that file.
865 	 */
866 	if ((error = VOP_ACCESSX(vp, VWRITE_ACL, cred, td)))
867 		return (error);
868 	/*
869 	 * Privileged processes may set the sticky bit on non-directories,
870 	 * as well as set the setgid bit on a file with a group that the
871 	 * process is not a member of.  Both of these are allowed in
872 	 * jail(8).
873 	 */
874 	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
875 		if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
876 			return (EFTYPE);
877 	}
878 	if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
879 		error = priv_check_cred(cred, PRIV_VFS_SETGID);
880 		if (error)
881 			return (error);
882 	}
883 
884 	/*
885 	 * Deny setting setuid if we are not the file owner.
886 	 */
887 	if ((mode & ISUID) && ip->i_uid != cred->cr_uid) {
888 		error = priv_check_cred(cred, PRIV_VFS_ADMIN);
889 		if (error)
890 			return (error);
891 	}
892 
893 	newmode = ip->i_mode & ~ALLPERMS;
894 	newmode |= (mode & ALLPERMS);
895 	UFS_INODE_SET_MODE(ip, newmode);
896 	DIP_SET(ip, i_mode, ip->i_mode);
897 	UFS_INODE_SET_FLAG(ip, IN_CHANGE);
898 #ifdef UFS_ACL
899 	if ((vp->v_mount->mnt_flag & MNT_NFS4ACLS) != 0)
900 		error = ufs_update_nfs4_acl_after_mode_change(vp, mode, ip->i_uid, cred, td);
901 #endif
902 	if (error == 0 && (ip->i_flag & IN_CHANGE) != 0)
903 		error = UFS_UPDATE(vp, 0);
904 
905 	return (error);
906 }
907 
908 /*
909  * Perform chown operation on inode ip;
910  * inode must be locked prior to call.
911  */
912 static int
ufs_chown(struct vnode * vp,uid_t uid,gid_t gid,struct ucred * cred,struct thread * td)913 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
914     struct thread *td)
915 {
916 	struct inode *ip = VTOI(vp);
917 	uid_t ouid;
918 	gid_t ogid;
919 	int error = 0;
920 #ifdef QUOTA
921 	int i;
922 	ufs2_daddr_t change;
923 #endif
924 
925 	if (uid == (uid_t)VNOVAL)
926 		uid = ip->i_uid;
927 	if (gid == (gid_t)VNOVAL)
928 		gid = ip->i_gid;
929 	/*
930 	 * To modify the ownership of a file, must possess VADMIN for that
931 	 * file.
932 	 */
933 	if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred, td)))
934 		return (error);
935 	/*
936 	 * To change the owner of a file, or change the group of a file to a
937 	 * group of which we are not a member, the caller must have
938 	 * privilege.
939 	 */
940 	if (((uid != ip->i_uid && uid != cred->cr_uid) ||
941 	    (gid != ip->i_gid && !groupmember(gid, cred))) &&
942 	    (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
943 		return (error);
944 	ogid = ip->i_gid;
945 	ouid = ip->i_uid;
946 #ifdef QUOTA
947 	if ((error = getinoquota(ip)) != 0)
948 		return (error);
949 	if (ouid == uid) {
950 		dqrele(vp, ip->i_dquot[USRQUOTA]);
951 		ip->i_dquot[USRQUOTA] = NODQUOT;
952 	}
953 	if (ogid == gid) {
954 		dqrele(vp, ip->i_dquot[GRPQUOTA]);
955 		ip->i_dquot[GRPQUOTA] = NODQUOT;
956 	}
957 	change = DIP(ip, i_blocks);
958 	(void) chkdq(ip, -change, cred, CHOWN|FORCE);
959 	(void) chkiq(ip, -1, cred, CHOWN|FORCE);
960 	for (i = 0; i < MAXQUOTAS; i++) {
961 		dqrele(vp, ip->i_dquot[i]);
962 		ip->i_dquot[i] = NODQUOT;
963 	}
964 #endif
965 	ip->i_gid = gid;
966 	DIP_SET(ip, i_gid, gid);
967 	ip->i_uid = uid;
968 	DIP_SET(ip, i_uid, uid);
969 #ifdef QUOTA
970 	if ((error = getinoquota(ip)) == 0) {
971 		if (ouid == uid) {
972 			dqrele(vp, ip->i_dquot[USRQUOTA]);
973 			ip->i_dquot[USRQUOTA] = NODQUOT;
974 		}
975 		if (ogid == gid) {
976 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
977 			ip->i_dquot[GRPQUOTA] = NODQUOT;
978 		}
979 		if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
980 			if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
981 				goto good;
982 			else
983 				(void) chkdq(ip, -change, cred, CHOWN|FORCE);
984 		}
985 		for (i = 0; i < MAXQUOTAS; i++) {
986 			dqrele(vp, ip->i_dquot[i]);
987 			ip->i_dquot[i] = NODQUOT;
988 		}
989 	}
990 	ip->i_gid = ogid;
991 	DIP_SET(ip, i_gid, ogid);
992 	ip->i_uid = ouid;
993 	DIP_SET(ip, i_uid, ouid);
994 	if (getinoquota(ip) == 0) {
995 		if (ouid == uid) {
996 			dqrele(vp, ip->i_dquot[USRQUOTA]);
997 			ip->i_dquot[USRQUOTA] = NODQUOT;
998 		}
999 		if (ogid == gid) {
1000 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
1001 			ip->i_dquot[GRPQUOTA] = NODQUOT;
1002 		}
1003 		(void) chkdq(ip, change, cred, FORCE|CHOWN);
1004 		(void) chkiq(ip, 1, cred, FORCE|CHOWN);
1005 		(void) getinoquota(ip);
1006 	}
1007 	return (error);
1008 good:
1009 	if (getinoquota(ip))
1010 		panic("ufs_chown: lost quota");
1011 #endif /* QUOTA */
1012 	UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1013 	if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
1014 		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
1015 			UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1016 			DIP_SET(ip, i_mode, ip->i_mode);
1017 		}
1018 	}
1019 	error = UFS_UPDATE(vp, 0);
1020 	return (error);
1021 }
1022 
1023 static int
ufs_remove(struct vop_remove_args * ap)1024 ufs_remove(
1025 	struct vop_remove_args /* {
1026 		struct vnode *a_dvp;
1027 		struct vnode *a_vp;
1028 		struct componentname *a_cnp;
1029 	} */ *ap)
1030 {
1031 	struct inode *ip;
1032 	struct vnode *vp = ap->a_vp;
1033 	struct vnode *dvp = ap->a_dvp;
1034 	int error;
1035 	struct thread *td;
1036 
1037 	td = curthread;
1038 	ip = VTOI(vp);
1039 	if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1040 	    (VTOI(dvp)->i_flags & APPEND))
1041 		return (EPERM);
1042 	if (DOINGSUJ(dvp)) {
1043 		error = softdep_prelink(dvp, vp, ap->a_cnp);
1044 		if (error != 0) {
1045 			MPASS(error == ERELOOKUP);
1046 			return (error);
1047 		}
1048 	}
1049 
1050 #ifdef UFS_GJOURNAL
1051 	ufs_gjournal_orphan(vp);
1052 #endif
1053 	error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
1054 	if (ip->i_nlink <= 0)
1055 		vp->v_vflag |= VV_NOSYNC;
1056 	if (IS_SNAPSHOT(ip)) {
1057 		/*
1058 		 * Avoid deadlock where another thread is trying to
1059 		 * update the inodeblock for dvp and is waiting on
1060 		 * snaplk.  Temporary unlock the vnode lock for the
1061 		 * unlinked file and sync the directory.  This should
1062 		 * allow vput() of the directory to not block later on
1063 		 * while holding the snapshot vnode locked, assuming
1064 		 * that the directory hasn't been unlinked too.
1065 		 */
1066 		VOP_UNLOCK(vp);
1067 		(void) VOP_FSYNC(dvp, MNT_WAIT, td);
1068 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1069 	}
1070 	return (error);
1071 }
1072 
1073 static void
print_bad_link_count(const char * funcname,struct vnode * dvp)1074 print_bad_link_count(const char *funcname, struct vnode *dvp)
1075 {
1076 	struct inode *dip;
1077 
1078 	dip = VTOI(dvp);
1079 	uprintf("%s: Bad link count %d on parent inode %jd in file system %s\n",
1080 	    funcname, dip->i_effnlink, (intmax_t)dip->i_number,
1081 	    dvp->v_mount->mnt_stat.f_mntonname);
1082 }
1083 
1084 /*
1085  * link vnode call
1086  */
1087 static int
ufs_link(struct vop_link_args * ap)1088 ufs_link(
1089 	struct vop_link_args /* {
1090 		struct vnode *a_tdvp;
1091 		struct vnode *a_vp;
1092 		struct componentname *a_cnp;
1093 	} */ *ap)
1094 {
1095 	struct vnode *vp = ap->a_vp;
1096 	struct vnode *tdvp = ap->a_tdvp;
1097 	struct componentname *cnp = ap->a_cnp;
1098 	struct inode *ip;
1099 	struct direct newdir;
1100 	int error;
1101 
1102 #ifdef INVARIANTS
1103 	if ((cnp->cn_flags & HASBUF) == 0)
1104 		panic("ufs_link: no name");
1105 #endif
1106 
1107 	if (DOINGSUJ(tdvp)) {
1108 		error = softdep_prelink(tdvp, vp, cnp);
1109 		if (error != 0) {
1110 			MPASS(error == ERELOOKUP);
1111 			return (error);
1112 		}
1113 	}
1114 
1115 	if (VTOI(tdvp)->i_effnlink < 2) {
1116 		print_bad_link_count("ufs_link", tdvp);
1117 		error = EINVAL;
1118 		goto out;
1119 	}
1120 	error = ufs_sync_nlink(vp, tdvp);
1121 	if (error != 0)
1122 		goto out;
1123 	ip = VTOI(vp);
1124 
1125 	/*
1126 	 * The file may have been removed after namei dropped the original
1127 	 * lock.
1128 	 */
1129 	if (ip->i_effnlink == 0) {
1130 		error = ENOENT;
1131 		goto out;
1132 	}
1133 	if (ip->i_flags & (IMMUTABLE | APPEND)) {
1134 		error = EPERM;
1135 		goto out;
1136 	}
1137 
1138 	ip->i_effnlink++;
1139 	ip->i_nlink++;
1140 	DIP_SET_NLINK(ip, ip->i_nlink);
1141 	UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1142 	if (DOINGSOFTDEP(vp))
1143 		softdep_setup_link(VTOI(tdvp), ip);
1144 	error = UFS_UPDATE(vp, !DOINGSOFTDEP(vp) && !DOINGASYNC(vp));
1145 	if (!error) {
1146 		ufs_makedirentry(ip, cnp, &newdir);
1147 		error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
1148 	}
1149 
1150 	if (error) {
1151 		ip->i_effnlink--;
1152 		ip->i_nlink--;
1153 		DIP_SET_NLINK(ip, ip->i_nlink);
1154 		UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1155 		if (DOINGSOFTDEP(vp))
1156 			softdep_revert_link(VTOI(tdvp), ip);
1157 	}
1158 out:
1159 	return (error);
1160 }
1161 
1162 /*
1163  * whiteout vnode call
1164  */
1165 static int
ufs_whiteout(struct vop_whiteout_args * ap)1166 ufs_whiteout(
1167 	struct vop_whiteout_args /* {
1168 		struct vnode *a_dvp;
1169 		struct componentname *a_cnp;
1170 		int a_flags;
1171 	} */ *ap)
1172 {
1173 	struct vnode *dvp = ap->a_dvp;
1174 	struct componentname *cnp = ap->a_cnp;
1175 	struct direct newdir;
1176 	int error = 0;
1177 
1178 	if (DOINGSUJ(dvp) && (ap->a_flags == CREATE ||
1179 	    ap->a_flags == DELETE)) {
1180 		error = softdep_prelink(dvp, NULL, cnp);
1181 		if (error != 0) {
1182 			MPASS(error == ERELOOKUP);
1183 			return (error);
1184 		}
1185 	}
1186 
1187 	switch (ap->a_flags) {
1188 	case LOOKUP:
1189 		/* 4.4 format directories support whiteout operations */
1190 		if (!OFSFMT(dvp))
1191 			return (0);
1192 		return (EOPNOTSUPP);
1193 
1194 	case CREATE:
1195 		/* create a new directory whiteout */
1196 #ifdef INVARIANTS
1197 		if ((cnp->cn_flags & SAVENAME) == 0)
1198 			panic("ufs_whiteout: missing name");
1199 		if (OFSFMT(dvp))
1200 			panic("ufs_whiteout: old format filesystem");
1201 #endif
1202 
1203 		newdir.d_ino = UFS_WINO;
1204 		newdir.d_namlen = cnp->cn_namelen;
1205 		bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
1206 		newdir.d_type = DT_WHT;
1207 		error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
1208 		break;
1209 
1210 	case DELETE:
1211 		/* remove an existing directory whiteout */
1212 #ifdef INVARIANTS
1213 		if (OFSFMT(dvp))
1214 			panic("ufs_whiteout: old format filesystem");
1215 #endif
1216 
1217 		cnp->cn_flags &= ~DOWHITEOUT;
1218 		error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
1219 		break;
1220 	default:
1221 		panic("ufs_whiteout: unknown op");
1222 	}
1223 	return (error);
1224 }
1225 
1226 static volatile int rename_restarts;
1227 SYSCTL_INT(_vfs_ufs, OID_AUTO, rename_restarts, CTLFLAG_RD,
1228     __DEVOLATILE(int *, &rename_restarts), 0,
1229     "Times rename had to restart due to lock contention");
1230 
1231 /*
1232  * Rename system call.
1233  * 	rename("foo", "bar");
1234  * is essentially
1235  *	unlink("bar");
1236  *	link("foo", "bar");
1237  *	unlink("foo");
1238  * but ``atomically''.  Can't do full commit without saving state in the
1239  * inode on disk which isn't feasible at this time.  Best we can do is
1240  * always guarantee the target exists.
1241  *
1242  * Basic algorithm is:
1243  *
1244  * 1) Bump link count on source while we're linking it to the
1245  *    target.  This also ensure the inode won't be deleted out
1246  *    from underneath us while we work (it may be truncated by
1247  *    a concurrent `trunc' or `open' for creation).
1248  * 2) Link source to destination.  If destination already exists,
1249  *    delete it first.
1250  * 3) Unlink source reference to inode if still around. If a
1251  *    directory was moved and the parent of the destination
1252  *    is different from the source, patch the ".." entry in the
1253  *    directory.
1254  */
1255 static int
ufs_rename(struct vop_rename_args * ap)1256 ufs_rename(
1257 	struct vop_rename_args  /* {
1258 		struct vnode *a_fdvp;
1259 		struct vnode *a_fvp;
1260 		struct componentname *a_fcnp;
1261 		struct vnode *a_tdvp;
1262 		struct vnode *a_tvp;
1263 		struct componentname *a_tcnp;
1264 	} */ *ap)
1265 {
1266 	struct vnode *tvp = ap->a_tvp;
1267 	struct vnode *tdvp = ap->a_tdvp;
1268 	struct vnode *fvp = ap->a_fvp;
1269 	struct vnode *fdvp = ap->a_fdvp;
1270 	struct vnode *nvp;
1271 	struct componentname *tcnp = ap->a_tcnp;
1272 	struct componentname *fcnp = ap->a_fcnp;
1273 	struct thread *td = fcnp->cn_thread;
1274 	struct inode *fip, *tip, *tdp, *fdp;
1275 	struct direct newdir;
1276 	off_t endoff;
1277 	int doingdirectory, newparent;
1278 	int error = 0;
1279 	struct mount *mp;
1280 	ino_t ino;
1281 	seqc_t fdvp_s, fvp_s, tdvp_s, tvp_s;
1282 	bool checkpath_locked, want_seqc_end;
1283 
1284 	checkpath_locked = want_seqc_end = false;
1285 
1286 #ifdef INVARIANTS
1287 	if ((tcnp->cn_flags & HASBUF) == 0 ||
1288 	    (fcnp->cn_flags & HASBUF) == 0)
1289 		panic("ufs_rename: no name");
1290 #endif
1291 	endoff = 0;
1292 	mp = tdvp->v_mount;
1293 	VOP_UNLOCK(tdvp);
1294 	if (tvp && tvp != tdvp)
1295 		VOP_UNLOCK(tvp);
1296 	/*
1297 	 * Check for cross-device rename.
1298 	 */
1299 	if ((fvp->v_mount != tdvp->v_mount) ||
1300 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1301 		error = EXDEV;
1302 		mp = NULL;
1303 		goto releout;
1304 	}
1305 
1306 	fdvp_s = fvp_s = tdvp_s = tvp_s = SEQC_MOD;
1307 relock:
1308 	/*
1309 	 * We need to acquire 2 to 4 locks depending on whether tvp is NULL
1310 	 * and fdvp and tdvp are the same directory.  Subsequently we need
1311 	 * to double-check all paths and in the directory rename case we
1312 	 * need to verify that we are not creating a directory loop.  To
1313 	 * handle this we acquire all but fdvp using non-blocking
1314 	 * acquisitions.  If we fail to acquire any lock in the path we will
1315 	 * drop all held locks, acquire the new lock in a blocking fashion,
1316 	 * and then release it and restart the rename.  This acquire/release
1317 	 * step ensures that we do not spin on a lock waiting for release.
1318 	 */
1319 	error = vn_lock(fdvp, LK_EXCLUSIVE);
1320 	if (error)
1321 		goto releout;
1322 	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1323 		VOP_UNLOCK(fdvp);
1324 		error = vn_lock(tdvp, LK_EXCLUSIVE);
1325 		if (error)
1326 			goto releout;
1327 		VOP_UNLOCK(tdvp);
1328 		atomic_add_int(&rename_restarts, 1);
1329 		goto relock;
1330 	}
1331 	/*
1332 	 * Re-resolve fvp to be certain it still exists and fetch the
1333 	 * correct vnode.
1334 	 */
1335 	error = ufs_lookup_ino(fdvp, NULL, fcnp, &ino);
1336 	if (error) {
1337 		VOP_UNLOCK(fdvp);
1338 		VOP_UNLOCK(tdvp);
1339 		goto releout;
1340 	}
1341 	error = VFS_VGET(mp, ino, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1342 	if (error) {
1343 		VOP_UNLOCK(fdvp);
1344 		VOP_UNLOCK(tdvp);
1345 		if (error != EBUSY)
1346 			goto releout;
1347 		error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &nvp);
1348 		if (error != 0)
1349 			goto releout;
1350 		VOP_UNLOCK(nvp);
1351 		vrele(fvp);
1352 		fvp = nvp;
1353 		atomic_add_int(&rename_restarts, 1);
1354 		goto relock;
1355 	}
1356 	vrele(fvp);
1357 	fvp = nvp;
1358 	/*
1359 	 * Re-resolve tvp and acquire the vnode lock if present.
1360 	 */
1361 	error = ufs_lookup_ino(tdvp, NULL, tcnp, &ino);
1362 	if (error != 0 && error != EJUSTRETURN) {
1363 		VOP_UNLOCK(fdvp);
1364 		VOP_UNLOCK(tdvp);
1365 		VOP_UNLOCK(fvp);
1366 		goto releout;
1367 	}
1368 	/*
1369 	 * If tvp disappeared we just carry on.
1370 	 */
1371 	if (error == EJUSTRETURN && tvp != NULL) {
1372 		vrele(tvp);
1373 		tvp = NULL;
1374 	}
1375 	/*
1376 	 * Get the tvp ino if the lookup succeeded.  We may have to restart
1377 	 * if the non-blocking acquire fails.
1378 	 */
1379 	if (error == 0) {
1380 		nvp = NULL;
1381 		error = VFS_VGET(mp, ino, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1382 		if (tvp)
1383 			vrele(tvp);
1384 		tvp = nvp;
1385 		if (error) {
1386 			VOP_UNLOCK(fdvp);
1387 			VOP_UNLOCK(tdvp);
1388 			VOP_UNLOCK(fvp);
1389 			if (error != EBUSY)
1390 				goto releout;
1391 			error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &nvp);
1392 			if (error != 0)
1393 				goto releout;
1394 			vput(nvp);
1395 			atomic_add_int(&rename_restarts, 1);
1396 			goto relock;
1397 		}
1398 	}
1399 
1400 	if (DOINGSUJ(fdvp) &&
1401 	    (seqc_in_modify(fdvp_s) || !vn_seqc_consistent(fdvp, fdvp_s) ||
1402 	     seqc_in_modify(fvp_s) || !vn_seqc_consistent(fvp, fvp_s) ||
1403 	     seqc_in_modify(tdvp_s) || !vn_seqc_consistent(tdvp, tdvp_s) ||
1404 	     (tvp != NULL && (seqc_in_modify(tvp_s) ||
1405 	     !vn_seqc_consistent(tvp, tvp_s))))) {
1406 		error = softdep_prerename(fdvp, fvp, tdvp, tvp);
1407 		if (error != 0)
1408 			goto releout;
1409 	}
1410 
1411 	fdp = VTOI(fdvp);
1412 	fip = VTOI(fvp);
1413 	tdp = VTOI(tdvp);
1414 	tip = NULL;
1415 	if (tvp)
1416 		tip = VTOI(tvp);
1417 	if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1418 	    (VTOI(tdvp)->i_flags & APPEND))) {
1419 		error = EPERM;
1420 		goto unlockout;
1421 	}
1422 	/*
1423 	 * Renaming a file to itself has no effect.  The upper layers should
1424 	 * not call us in that case.  However, things could change after
1425 	 * we drop the locks above.
1426 	 */
1427 	if (fvp == tvp) {
1428 		error = 0;
1429 		goto unlockout;
1430 	}
1431 	doingdirectory = 0;
1432 	newparent = 0;
1433 	ino = fip->i_number;
1434 	if (fip->i_nlink >= UFS_LINK_MAX) {
1435 		if (!DOINGSOFTDEP(fvp) || fip->i_effnlink >= UFS_LINK_MAX) {
1436 			error = EMLINK;
1437 			goto unlockout;
1438 		}
1439 		vfs_ref(mp);
1440 		MPASS(!want_seqc_end);
1441 		if (checkpath_locked) {
1442 			sx_xunlock(&VFSTOUFS(mp)->um_checkpath_lock);
1443 			checkpath_locked = false;
1444 		}
1445 		VOP_UNLOCK(fdvp);
1446 		VOP_UNLOCK(fvp);
1447 		vref(tdvp);
1448 		if (tvp != NULL)
1449 			vref(tvp);
1450 		VOP_VPUT_PAIR(tdvp, &tvp, true);
1451 		error = ufs_sync_nlink1(mp);
1452 		vrele(fdvp);
1453 		vrele(fvp);
1454 		vrele(tdvp);
1455 		if (tvp != NULL)
1456 			vrele(tvp);
1457 		return (error);
1458 	}
1459 	if ((fip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1460 	    || (fdp->i_flags & APPEND)) {
1461 		error = EPERM;
1462 		goto unlockout;
1463 	}
1464 	if ((fip->i_mode & IFMT) == IFDIR) {
1465 		/*
1466 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1467 		 */
1468 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1469 		    fdp == fip ||
1470 		    (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
1471 			error = EINVAL;
1472 			goto unlockout;
1473 		}
1474 		if (fdp->i_number != tdp->i_number)
1475 			newparent = tdp->i_number;
1476 		doingdirectory = 1;
1477 	}
1478 	if ((fvp->v_type == VDIR && fvp->v_mountedhere != NULL) ||
1479 	    (tvp != NULL && tvp->v_type == VDIR &&
1480 	    tvp->v_mountedhere != NULL)) {
1481 		error = EXDEV;
1482 		goto unlockout;
1483 	}
1484 
1485 	/*
1486 	 * If ".." must be changed (ie the directory gets a new
1487 	 * parent) then the source directory must not be in the
1488 	 * directory hierarchy above the target, as this would
1489 	 * orphan everything below the source directory. Also
1490 	 * the user must have write permission in the source so
1491 	 * as to be able to change "..".
1492 	 */
1493 	if (doingdirectory && newparent) {
1494 		error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1495 		if (error)
1496 			goto unlockout;
1497 
1498 		sx_xlock(&VFSTOUFS(mp)->um_checkpath_lock);
1499 		checkpath_locked = true;
1500 		error = ufs_checkpath(ino, fdp->i_number, tdp, tcnp->cn_cred,
1501 		    &ino);
1502 		/*
1503 		 * We encountered a lock that we have to wait for.  Unlock
1504 		 * everything else and VGET before restarting.
1505 		 */
1506 		if (ino) {
1507 			sx_xunlock(&VFSTOUFS(mp)->um_checkpath_lock);
1508 			checkpath_locked = false;
1509 			VOP_UNLOCK(fdvp);
1510 			VOP_UNLOCK(fvp);
1511 			VOP_UNLOCK(tdvp);
1512 			if (tvp)
1513 				VOP_UNLOCK(tvp);
1514 			error = VFS_VGET(mp, ino, LK_SHARED, &nvp);
1515 			if (error == 0)
1516 				vput(nvp);
1517 			atomic_add_int(&rename_restarts, 1);
1518 			goto relock;
1519 		}
1520 		if (error)
1521 			goto unlockout;
1522 		if ((tcnp->cn_flags & SAVESTART) == 0)
1523 			panic("ufs_rename: lost to startdir");
1524 	}
1525 	if (fip->i_effnlink == 0 || fdp->i_effnlink == 0 ||
1526 	    tdp->i_effnlink == 0)
1527 		panic("Bad effnlink fip %p, fdp %p, tdp %p", fip, fdp, tdp);
1528 
1529 	if (tvp != NULL)
1530 		vn_seqc_write_begin(tvp);
1531 	vn_seqc_write_begin(tdvp);
1532 	vn_seqc_write_begin(fvp);
1533 	vn_seqc_write_begin(fdvp);
1534 	want_seqc_end = true;
1535 
1536 	/*
1537 	 * 1) Bump link count while we're moving stuff
1538 	 *    around.  If we crash somewhere before
1539 	 *    completing our work, the link count
1540 	 *    may be wrong, but correctable.
1541 	 */
1542 	fip->i_effnlink++;
1543 	fip->i_nlink++;
1544 	DIP_SET_NLINK(fip, fip->i_nlink);
1545 	UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1546 	if (DOINGSOFTDEP(fvp))
1547 		softdep_setup_link(tdp, fip);
1548 	error = UFS_UPDATE(fvp, !DOINGSOFTDEP(fvp) && !DOINGASYNC(fvp));
1549 	if (error)
1550 		goto bad;
1551 
1552 	/*
1553 	 * 2) If target doesn't exist, link the target
1554 	 *    to the source and unlink the source.
1555 	 *    Otherwise, rewrite the target directory
1556 	 *    entry to reference the source inode and
1557 	 *    expunge the original entry's existence.
1558 	 */
1559 	if (tip == NULL) {
1560 		if (ITODEV(tdp) != ITODEV(fip))
1561 			panic("ufs_rename: EXDEV");
1562 		if (doingdirectory && newparent) {
1563 			/*
1564 			 * Account for ".." in new directory.
1565 			 * When source and destination have the same
1566 			 * parent we don't adjust the link count.  The
1567 			 * actual link modification is completed when
1568 			 * .. is rewritten below.
1569 			 */
1570 			if (tdp->i_nlink >= UFS_LINK_MAX) {
1571 				fip->i_effnlink--;
1572 				fip->i_nlink--;
1573 				DIP_SET_NLINK(fip, fip->i_nlink);
1574 				UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1575 				if (DOINGSOFTDEP(fvp))
1576 					softdep_revert_link(tdp, fip);
1577 				if (!DOINGSOFTDEP(tdvp) ||
1578 				    tdp->i_effnlink >= UFS_LINK_MAX) {
1579 					error = EMLINK;
1580 					goto unlockout;
1581 				}
1582 				MPASS(want_seqc_end);
1583 				if (tvp != NULL)
1584 					vn_seqc_write_end(tvp);
1585 				vn_seqc_write_end(tdvp);
1586 				vn_seqc_write_end(fvp);
1587 				vn_seqc_write_end(fdvp);
1588 				want_seqc_end = false;
1589 				vfs_ref(mp);
1590 				MPASS(checkpath_locked);
1591 				sx_xunlock(&VFSTOUFS(mp)->um_checkpath_lock);
1592 				checkpath_locked = false;
1593 				VOP_UNLOCK(fdvp);
1594 				VOP_UNLOCK(fvp);
1595 				vref(tdvp);
1596 				if (tvp != NULL)
1597 					vref(tvp);
1598 				VOP_VPUT_PAIR(tdvp, &tvp, true);
1599 				error = ufs_sync_nlink1(mp);
1600 				vrele(fdvp);
1601 				vrele(fvp);
1602 				vrele(tdvp);
1603 				if (tvp != NULL)
1604 					vrele(tvp);
1605 				return (error);
1606 			}
1607 		}
1608 		ufs_makedirentry(fip, tcnp, &newdir);
1609 		error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1610 		if (error)
1611 			goto bad;
1612 		/* Setup tdvp for directory compaction if needed. */
1613 		if (I_COUNT(tdp) != 0 && I_ENDOFF(tdp) != 0 &&
1614 		    I_ENDOFF(tdp) < tdp->i_size)
1615 			endoff = I_ENDOFF(tdp);
1616 	} else {
1617 		if (ITODEV(tip) != ITODEV(tdp) || ITODEV(tip) != ITODEV(fip))
1618 			panic("ufs_rename: EXDEV");
1619 		/*
1620 		 * Short circuit rename(foo, foo).
1621 		 */
1622 		if (tip->i_number == fip->i_number)
1623 			panic("ufs_rename: same file");
1624 		/*
1625 		 * If the parent directory is "sticky", then the caller
1626 		 * must possess VADMIN for the parent directory, or the
1627 		 * destination of the rename.  This implements append-only
1628 		 * directories.
1629 		 */
1630 		if ((tdp->i_mode & S_ISTXT) &&
1631 		    VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1632 		    VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1633 			error = EPERM;
1634 			goto bad;
1635 		}
1636 		/*
1637 		 * Target must be empty if a directory and have no links
1638 		 * to it. Also, ensure source and target are compatible
1639 		 * (both directories, or both not directories).
1640 		 */
1641 		if ((tip->i_mode & IFMT) == IFDIR) {
1642 			if ((tip->i_effnlink > 2) ||
1643 			    !ufs_dirempty(tip, tdp->i_number, tcnp->cn_cred)) {
1644 				error = ENOTEMPTY;
1645 				goto bad;
1646 			}
1647 			if (!doingdirectory) {
1648 				error = ENOTDIR;
1649 				goto bad;
1650 			}
1651 			cache_purge(tdvp);
1652 		} else if (doingdirectory) {
1653 			error = EISDIR;
1654 			goto bad;
1655 		}
1656 		if (doingdirectory) {
1657 			if (!newparent) {
1658 				tdp->i_effnlink--;
1659 				if (DOINGSOFTDEP(tdvp))
1660 					softdep_change_linkcnt(tdp);
1661 			}
1662 			tip->i_effnlink--;
1663 			if (DOINGSOFTDEP(tvp))
1664 				softdep_change_linkcnt(tip);
1665 		}
1666 		error = ufs_dirrewrite(tdp, tip, fip->i_number,
1667 		    IFTODT(fip->i_mode),
1668 		    (doingdirectory && newparent) ? newparent : doingdirectory);
1669 		if (error) {
1670 			if (doingdirectory) {
1671 				if (!newparent) {
1672 					tdp->i_effnlink++;
1673 					if (DOINGSOFTDEP(tdvp))
1674 						softdep_change_linkcnt(tdp);
1675 				}
1676 				tip->i_effnlink++;
1677 				if (DOINGSOFTDEP(tvp))
1678 					softdep_change_linkcnt(tip);
1679 			}
1680 			goto bad;
1681 		}
1682 		if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1683 			/*
1684 			 * The only stuff left in the directory is "."
1685 			 * and "..". The "." reference is inconsequential
1686 			 * since we are quashing it. We have removed the "."
1687 			 * reference and the reference in the parent directory,
1688 			 * but there may be other hard links. The soft
1689 			 * dependency code will arrange to do these operations
1690 			 * after the parent directory entry has been deleted on
1691 			 * disk, so when running with that code we avoid doing
1692 			 * them now.
1693 			 */
1694 			if (!newparent) {
1695 				tdp->i_nlink--;
1696 				DIP_SET_NLINK(tdp, tdp->i_nlink);
1697 				UFS_INODE_SET_FLAG(tdp, IN_CHANGE);
1698 			}
1699 			tip->i_nlink--;
1700 			DIP_SET_NLINK(tip, tip->i_nlink);
1701 			UFS_INODE_SET_FLAG(tip, IN_CHANGE);
1702 		}
1703 	}
1704 
1705 	/*
1706 	 * 3) Unlink the source.  We have to resolve the path again to
1707 	 * fixup the directory offset and count for ufs_dirremove.
1708 	 */
1709 	if (fdvp == tdvp) {
1710 		error = ufs_lookup_ino(fdvp, NULL, fcnp, &ino);
1711 		if (error)
1712 			panic("ufs_rename: from entry went away!");
1713 		if (ino != fip->i_number)
1714 			panic("ufs_rename: ino mismatch %ju != %ju\n",
1715 			    (uintmax_t)ino, (uintmax_t)fip->i_number);
1716 	}
1717 	/*
1718 	 * If the source is a directory with a
1719 	 * new parent, the link count of the old
1720 	 * parent directory must be decremented
1721 	 * and ".." set to point to the new parent.
1722 	 */
1723 	if (doingdirectory && newparent) {
1724 		/*
1725 		 * Set the directory depth based on its new parent.
1726 		 */
1727 		DIP_SET(fip, i_dirdepth, DIP(tdp, i_dirdepth) + 1);
1728 		/*
1729 		 * If tip exists we simply use its link, otherwise we must
1730 		 * add a new one.
1731 		 */
1732 		if (tip == NULL) {
1733 			tdp->i_effnlink++;
1734 			tdp->i_nlink++;
1735 			DIP_SET_NLINK(tdp, tdp->i_nlink);
1736 			UFS_INODE_SET_FLAG(tdp, IN_CHANGE);
1737 			if (DOINGSOFTDEP(tdvp))
1738 				softdep_setup_dotdot_link(tdp, fip);
1739 			error = UFS_UPDATE(tdvp, !DOINGSOFTDEP(tdvp) &&
1740 			    !DOINGASYNC(tdvp));
1741 			/* Don't go to bad here as the new link exists. */
1742 			if (error)
1743 				goto unlockout;
1744 		} else if (DOINGSUJ(tdvp))
1745 			/* Journal must account for each new link. */
1746 			softdep_setup_dotdot_link(tdp, fip);
1747 		SET_I_OFFSET(fip, mastertemplate.dot_reclen);
1748 		ufs_dirrewrite(fip, fdp, newparent, DT_DIR, 0);
1749 		cache_purge(fdvp);
1750 	}
1751 	error = ufs_dirremove(fdvp, fip, fcnp->cn_flags, 0);
1752 	/*
1753 	 * The kern_renameat() looks up the fvp using the DELETE flag, which
1754 	 * causes the removal of the name cache entry for fvp.
1755 	 * As the relookup of the fvp is done in two steps:
1756 	 * ufs_lookup_ino() and then VFS_VGET(), another thread might do a
1757 	 * normal lookup of the from name just before the VFS_VGET() call,
1758 	 * causing the cache entry to be re-instantiated.
1759 	 *
1760 	 * The same issue also applies to tvp if it exists as
1761 	 * otherwise we may have a stale name cache entry for the new
1762 	 * name that references the old i-node if it has other links
1763 	 * or open file descriptors.
1764 	 */
1765 	cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1766 
1767 unlockout:
1768 	if (want_seqc_end) {
1769 		if (tvp != NULL)
1770 			vn_seqc_write_end(tvp);
1771 		vn_seqc_write_end(tdvp);
1772 		vn_seqc_write_end(fvp);
1773 		vn_seqc_write_end(fdvp);
1774 	}
1775 
1776 	if (checkpath_locked)
1777 		sx_xunlock(&VFSTOUFS(mp)->um_checkpath_lock);
1778 
1779 	vput(fdvp);
1780 	vput(fvp);
1781 
1782 	/*
1783 	 * If compaction or fsync was requested do it in
1784 	 * ffs_vput_pair() now that other locks are no longer needed.
1785 	 */
1786 	if (error == 0 && endoff != 0) {
1787 		UFS_INODE_SET_FLAG(tdp, IN_ENDOFF);
1788 		SET_I_ENDOFF(tdp, endoff);
1789 	}
1790 	VOP_VPUT_PAIR(tdvp, &tvp, true);
1791 	return (error);
1792 
1793 bad:
1794 	fip->i_effnlink--;
1795 	fip->i_nlink--;
1796 	DIP_SET_NLINK(fip, fip->i_nlink);
1797 	UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1798 	if (DOINGSOFTDEP(fvp))
1799 		softdep_revert_link(tdp, fip);
1800 	goto unlockout;
1801 
1802 releout:
1803 	if (want_seqc_end) {
1804 		if (tvp != NULL)
1805 			vn_seqc_write_end(tvp);
1806 		vn_seqc_write_end(tdvp);
1807 		vn_seqc_write_end(fvp);
1808 		vn_seqc_write_end(fdvp);
1809 	}
1810 
1811 	vrele(fdvp);
1812 	vrele(fvp);
1813 	vrele(tdvp);
1814 	if (tvp)
1815 		vrele(tvp);
1816 
1817 	return (error);
1818 }
1819 
1820 #ifdef UFS_ACL
1821 static int
ufs_do_posix1e_acl_inheritance_dir(struct vnode * dvp,struct vnode * tvp,mode_t dmode,struct ucred * cred,struct thread * td)1822 ufs_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1823     mode_t dmode, struct ucred *cred, struct thread *td)
1824 {
1825 	int error;
1826 	struct inode *ip = VTOI(tvp);
1827 	struct acl *dacl, *acl;
1828 
1829 	acl = acl_alloc(M_WAITOK);
1830 	dacl = acl_alloc(M_WAITOK);
1831 
1832 	/*
1833 	 * Retrieve default ACL from parent, if any.
1834 	 */
1835 	error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1836 	switch (error) {
1837 	case 0:
1838 		/*
1839 		 * Retrieved a default ACL, so merge mode and ACL if
1840 		 * necessary.  If the ACL is empty, fall through to
1841 		 * the "not defined or available" case.
1842 		 */
1843 		if (acl->acl_cnt != 0) {
1844 			dmode = acl_posix1e_newfilemode(dmode, acl);
1845 			UFS_INODE_SET_MODE(ip, dmode);
1846 			DIP_SET(ip, i_mode, dmode);
1847 			*dacl = *acl;
1848 			ufs_sync_acl_from_inode(ip, acl);
1849 			break;
1850 		}
1851 		/* FALLTHROUGH */
1852 
1853 	case EOPNOTSUPP:
1854 		/*
1855 		 * Just use the mode as-is.
1856 		 */
1857 		UFS_INODE_SET_MODE(ip, dmode);
1858 		DIP_SET(ip, i_mode, dmode);
1859 		error = 0;
1860 		goto out;
1861 
1862 	default:
1863 		goto out;
1864 	}
1865 
1866 	/*
1867 	 * XXX: If we abort now, will Soft Updates notify the extattr
1868 	 * code that the EAs for the file need to be released?
1869 	 */
1870 	error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1871 	if (error == 0)
1872 		error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1873 	switch (error) {
1874 	case 0:
1875 		break;
1876 
1877 	case EOPNOTSUPP:
1878 		/*
1879 		 * XXX: This should not happen, as EOPNOTSUPP above
1880 		 * was supposed to free acl.
1881 		 */
1882 		printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1883 		/*
1884 		panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1885 		 */
1886 		break;
1887 
1888 	default:
1889 		goto out;
1890 	}
1891 
1892 out:
1893 	acl_free(acl);
1894 	acl_free(dacl);
1895 
1896 	return (error);
1897 }
1898 
1899 static int
ufs_do_posix1e_acl_inheritance_file(struct vnode * dvp,struct vnode * tvp,mode_t mode,struct ucred * cred,struct thread * td)1900 ufs_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1901     mode_t mode, struct ucred *cred, struct thread *td)
1902 {
1903 	int error;
1904 	struct inode *ip = VTOI(tvp);
1905 	struct acl *acl;
1906 
1907 	acl = acl_alloc(M_WAITOK);
1908 
1909 	/*
1910 	 * Retrieve default ACL for parent, if any.
1911 	 */
1912 	error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1913 	switch (error) {
1914 	case 0:
1915 		/*
1916 		 * Retrieved a default ACL, so merge mode and ACL if
1917 		 * necessary.
1918 		 */
1919 		if (acl->acl_cnt != 0) {
1920 			/*
1921 			 * Two possible ways for default ACL to not
1922 			 * be present.  First, the EA can be
1923 			 * undefined, or second, the default ACL can
1924 			 * be blank.  If it's blank, fall through to
1925 			 * the it's not defined case.
1926 			 */
1927 			mode = acl_posix1e_newfilemode(mode, acl);
1928 			UFS_INODE_SET_MODE(ip, mode);
1929 			DIP_SET(ip, i_mode, mode);
1930 			ufs_sync_acl_from_inode(ip, acl);
1931 			break;
1932 		}
1933 		/* FALLTHROUGH */
1934 
1935 	case EOPNOTSUPP:
1936 		/*
1937 		 * Just use the mode as-is.
1938 		 */
1939 		UFS_INODE_SET_MODE(ip, mode);
1940 		DIP_SET(ip, i_mode, mode);
1941 		error = 0;
1942 		goto out;
1943 
1944 	default:
1945 		goto out;
1946 	}
1947 
1948 	/*
1949 	 * XXX: If we abort now, will Soft Updates notify the extattr
1950 	 * code that the EAs for the file need to be released?
1951 	 */
1952 	error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1953 	switch (error) {
1954 	case 0:
1955 		break;
1956 
1957 	case EOPNOTSUPP:
1958 		/*
1959 		 * XXX: This should not happen, as EOPNOTSUPP above was
1960 		 * supposed to free acl.
1961 		 */
1962 		printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1963 		    "but no VOP_SETACL()\n");
1964 		/* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1965 		    "but no VOP_SETACL()"); */
1966 		break;
1967 
1968 	default:
1969 		goto out;
1970 	}
1971 
1972 out:
1973 	acl_free(acl);
1974 
1975 	return (error);
1976 }
1977 
1978 static int
ufs_do_nfs4_acl_inheritance(struct vnode * dvp,struct vnode * tvp,mode_t child_mode,struct ucred * cred,struct thread * td)1979 ufs_do_nfs4_acl_inheritance(struct vnode *dvp, struct vnode *tvp,
1980     mode_t child_mode, struct ucred *cred, struct thread *td)
1981 {
1982 	int error;
1983 	struct acl *parent_aclp, *child_aclp;
1984 
1985 	parent_aclp = acl_alloc(M_WAITOK);
1986 	child_aclp = acl_alloc(M_WAITOK | M_ZERO);
1987 
1988 	error = ufs_getacl_nfs4_internal(dvp, parent_aclp, td);
1989 	if (error)
1990 		goto out;
1991 	acl_nfs4_compute_inherited_acl(parent_aclp, child_aclp,
1992 	    child_mode, VTOI(tvp)->i_uid, tvp->v_type == VDIR);
1993 	error = ufs_setacl_nfs4_internal(tvp, child_aclp, td);
1994 	if (error)
1995 		goto out;
1996 out:
1997 	acl_free(parent_aclp);
1998 	acl_free(child_aclp);
1999 
2000 	return (error);
2001 }
2002 #endif
2003 
2004 /*
2005  * Mkdir system call
2006  */
2007 static int
ufs_mkdir(struct vop_mkdir_args * ap)2008 ufs_mkdir(
2009 	struct vop_mkdir_args /* {
2010 		struct vnode *a_dvp;
2011 		struct vnode **a_vpp;
2012 		struct componentname *a_cnp;
2013 		struct vattr *a_vap;
2014 	} */ *ap)
2015 {
2016 	struct vnode *dvp = ap->a_dvp;
2017 	struct vattr *vap = ap->a_vap;
2018 	struct componentname *cnp = ap->a_cnp;
2019 	struct inode *ip, *dp;
2020 	struct vnode *tvp;
2021 	struct buf *bp;
2022 	struct dirtemplate dirtemplate, *dtp;
2023 	struct direct newdir;
2024 	int error, dmode;
2025 	long blkoff;
2026 
2027 #ifdef INVARIANTS
2028 	if ((cnp->cn_flags & HASBUF) == 0)
2029 		panic("ufs_mkdir: no name");
2030 #endif
2031 	dp = VTOI(dvp);
2032 	error = ufs_sync_nlink(dvp, NULL);
2033 	if (error != 0)
2034 		goto out;
2035 	dmode = vap->va_mode & 0777;
2036 	dmode |= IFDIR;
2037 
2038 	/*
2039 	 * Must simulate part of ufs_makeinode here to acquire the inode,
2040 	 * but not have it entered in the parent directory. The entry is
2041 	 * made later after writing "." and ".." entries.
2042 	 */
2043 	if (dp->i_effnlink < 2) {
2044 		print_bad_link_count("ufs_mkdir", dvp);
2045 		error = EINVAL;
2046 		goto out;
2047 	}
2048 
2049 	if (DOINGSUJ(dvp)) {
2050 		error = softdep_prelink(dvp, NULL, cnp);
2051 		if (error != 0) {
2052 			MPASS(error == ERELOOKUP);
2053 			return (error);
2054 		}
2055 	}
2056 
2057 	error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
2058 	if (error)
2059 		goto out;
2060 	vn_seqc_write_begin(tvp);
2061 	ip = VTOI(tvp);
2062 	ip->i_gid = dp->i_gid;
2063 	DIP_SET(ip, i_gid, dp->i_gid);
2064 #ifdef SUIDDIR
2065 	{
2066 #ifdef QUOTA
2067 		struct ucred ucred, *ucp;
2068 		gid_t ucred_group;
2069 		ucp = cnp->cn_cred;
2070 #endif
2071 		/*
2072 		 * If we are hacking owners here, (only do this where told to)
2073 		 * and we are not giving it TO root, (would subvert quotas)
2074 		 * then go ahead and give it to the other user.
2075 		 * The new directory also inherits the SUID bit.
2076 		 * If user's UID and dir UID are the same,
2077 		 * 'give it away' so that the SUID is still forced on.
2078 		 */
2079 		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2080 		    (dp->i_mode & ISUID) && dp->i_uid) {
2081 			dmode |= ISUID;
2082 			ip->i_uid = dp->i_uid;
2083 			DIP_SET(ip, i_uid, dp->i_uid);
2084 #ifdef QUOTA
2085 			if (dp->i_uid != cnp->cn_cred->cr_uid) {
2086 				/*
2087 				 * Make sure the correct user gets charged
2088 				 * for the space.
2089 				 * Make a dummy credential for the victim.
2090 				 * XXX This seems to never be accessed out of
2091 				 * our context so a stack variable is ok.
2092 				 */
2093 				refcount_init(&ucred.cr_ref, 1);
2094 				ucred.cr_uid = ip->i_uid;
2095 				ucred.cr_ngroups = 1;
2096 				ucred.cr_groups = &ucred_group;
2097 				ucred.cr_groups[0] = dp->i_gid;
2098 				ucp = &ucred;
2099 			}
2100 #endif
2101 		} else {
2102 			ip->i_uid = cnp->cn_cred->cr_uid;
2103 			DIP_SET(ip, i_uid, ip->i_uid);
2104 		}
2105 #ifdef QUOTA
2106 		if ((error = getinoquota(ip)) ||
2107 	    	    (error = chkiq(ip, 1, ucp, 0))) {
2108 			if (DOINGSOFTDEP(tvp))
2109 				softdep_revert_link(dp, ip);
2110 			UFS_VFREE(tvp, ip->i_number, dmode);
2111 			vn_seqc_write_end(tvp);
2112 			vgone(tvp);
2113 			vput(tvp);
2114 			return (error);
2115 		}
2116 #endif
2117 	}
2118 #else	/* !SUIDDIR */
2119 	ip->i_uid = cnp->cn_cred->cr_uid;
2120 	DIP_SET(ip, i_uid, ip->i_uid);
2121 #ifdef QUOTA
2122 	if ((error = getinoquota(ip)) ||
2123 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2124 		if (DOINGSOFTDEP(tvp))
2125 			softdep_revert_link(dp, ip);
2126 		UFS_VFREE(tvp, ip->i_number, dmode);
2127 		vn_seqc_write_end(tvp);
2128 		vgone(tvp);
2129 		vput(tvp);
2130 		return (error);
2131 	}
2132 #endif
2133 #endif	/* !SUIDDIR */
2134 	UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
2135 	UFS_INODE_SET_MODE(ip, dmode);
2136 	DIP_SET(ip, i_mode, dmode);
2137 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
2138 	ip->i_effnlink = 2;
2139 	ip->i_nlink = 2;
2140 	DIP_SET_NLINK(ip, 2);
2141 	DIP_SET(ip, i_dirdepth, DIP(dp,i_dirdepth) + 1);
2142 
2143 	if (cnp->cn_flags & ISWHITEOUT) {
2144 		ip->i_flags |= UF_OPAQUE;
2145 		DIP_SET(ip, i_flags, ip->i_flags);
2146 	}
2147 
2148 	/*
2149 	 * Bump link count in parent directory to reflect work done below.
2150 	 * Should be done before reference is created so cleanup is
2151 	 * possible if we crash.
2152 	 */
2153 	dp->i_effnlink++;
2154 	dp->i_nlink++;
2155 	DIP_SET_NLINK(dp, dp->i_nlink);
2156 	UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2157 	if (DOINGSOFTDEP(dvp))
2158 		softdep_setup_mkdir(dp, ip);
2159 	error = UFS_UPDATE(dvp, !DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp));
2160 	if (error)
2161 		goto bad;
2162 #ifdef MAC
2163 	if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2164 		error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2165 		    dvp, tvp, cnp);
2166 		if (error)
2167 			goto bad;
2168 	}
2169 #endif
2170 #ifdef UFS_ACL
2171 	if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2172 		error = ufs_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
2173 		    cnp->cn_cred, cnp->cn_thread);
2174 		if (error)
2175 			goto bad;
2176 	} else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2177 		error = ufs_do_nfs4_acl_inheritance(dvp, tvp, dmode,
2178 		    cnp->cn_cred, cnp->cn_thread);
2179 		if (error)
2180 			goto bad;
2181 	}
2182 #endif /* !UFS_ACL */
2183 
2184 	/*
2185 	 * Initialize directory with "." and ".." from static template.
2186 	 */
2187 	if (!OFSFMT(dvp))
2188 		dtp = &mastertemplate;
2189 	else
2190 		dtp = (struct dirtemplate *)&omastertemplate;
2191 	dirtemplate = *dtp;
2192 	dirtemplate.dot_ino = ip->i_number;
2193 	dirtemplate.dotdot_ino = dp->i_number;
2194 	vnode_pager_setsize(tvp, DIRBLKSIZ);
2195 	if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
2196 	    BA_CLRBUF, &bp)) != 0)
2197 		goto bad;
2198 	ip->i_size = DIRBLKSIZ;
2199 	DIP_SET(ip, i_size, DIRBLKSIZ);
2200 	UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
2201 	bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
2202 	if (DOINGSOFTDEP(tvp)) {
2203 		/*
2204 		 * Ensure that the entire newly allocated block is a
2205 		 * valid directory so that future growth within the
2206 		 * block does not have to ensure that the block is
2207 		 * written before the inode.
2208 		 */
2209 		blkoff = DIRBLKSIZ;
2210 		while (blkoff < bp->b_bcount) {
2211 			((struct direct *)
2212 			   (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
2213 			blkoff += DIRBLKSIZ;
2214 		}
2215 	}
2216 	if ((error = UFS_UPDATE(tvp, !DOINGSOFTDEP(tvp) &&
2217 	    !DOINGASYNC(tvp))) != 0) {
2218 		(void)bwrite(bp);
2219 		goto bad;
2220 	}
2221 	/*
2222 	 * Directory set up, now install its entry in the parent directory.
2223 	 *
2224 	 * If we are not doing soft dependencies, then we must write out the
2225 	 * buffer containing the new directory body before entering the new
2226 	 * name in the parent. If we are doing soft dependencies, then the
2227 	 * buffer containing the new directory body will be passed to and
2228 	 * released in the soft dependency code after the code has attached
2229 	 * an appropriate ordering dependency to the buffer which ensures that
2230 	 * the buffer is written before the new name is written in the parent.
2231 	 */
2232 	if (DOINGASYNC(dvp))
2233 		bdwrite(bp);
2234 	else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
2235 		goto bad;
2236 	ufs_makedirentry(ip, cnp, &newdir);
2237 	error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
2238 
2239 bad:
2240 	if (error == 0) {
2241 		*ap->a_vpp = tvp;
2242 		vn_seqc_write_end(tvp);
2243 	} else {
2244 		dp->i_effnlink--;
2245 		dp->i_nlink--;
2246 		DIP_SET_NLINK(dp, dp->i_nlink);
2247 		UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2248 		/*
2249 		 * No need to do an explicit VOP_TRUNCATE here, vrele will
2250 		 * do this for us because we set the link count to 0.
2251 		 */
2252 		ip->i_effnlink = 0;
2253 		ip->i_nlink = 0;
2254 		DIP_SET_NLINK(ip, 0);
2255 		UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2256 		if (DOINGSOFTDEP(tvp))
2257 			softdep_revert_mkdir(dp, ip);
2258 		vn_seqc_write_end(tvp);
2259 		vgone(tvp);
2260 		vput(tvp);
2261 	}
2262 out:
2263 	return (error);
2264 }
2265 
2266 /*
2267  * Rmdir system call.
2268  */
2269 static int
ufs_rmdir(struct vop_rmdir_args * ap)2270 ufs_rmdir(
2271 	struct vop_rmdir_args /* {
2272 		struct vnode *a_dvp;
2273 		struct vnode *a_vp;
2274 		struct componentname *a_cnp;
2275 	} */ *ap)
2276 {
2277 	struct vnode *vp = ap->a_vp;
2278 	struct vnode *dvp = ap->a_dvp;
2279 	struct componentname *cnp = ap->a_cnp;
2280 	struct inode *ip, *dp;
2281 	int error;
2282 
2283 	ip = VTOI(vp);
2284 	dp = VTOI(dvp);
2285 
2286 	/*
2287 	 * Do not remove a directory that is in the process of being renamed.
2288 	 * Verify the directory is empty (and valid). Rmdir ".." will not be
2289 	 * valid since ".." will contain a reference to the current directory
2290 	 * and thus be non-empty. Do not allow the removal of mounted on
2291 	 * directories (this can happen when an NFS exported filesystem
2292 	 * tries to remove a locally mounted on directory).
2293 	 */
2294 	error = 0;
2295 	if (dp->i_effnlink <= 2) {
2296 		if (dp->i_effnlink == 2)
2297 			print_bad_link_count("ufs_rmdir", dvp);
2298 		error = EINVAL;
2299 		goto out;
2300 	}
2301 	if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
2302 		error = ENOTEMPTY;
2303 		goto out;
2304 	}
2305 	if ((dp->i_flags & APPEND)
2306 	    || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
2307 		error = EPERM;
2308 		goto out;
2309 	}
2310 	if (vp->v_mountedhere != 0) {
2311 		error = EINVAL;
2312 		goto out;
2313 	}
2314 	if (DOINGSUJ(dvp)) {
2315 		error = softdep_prelink(dvp, vp, cnp);
2316 		if (error != 0) {
2317 			MPASS(error == ERELOOKUP);
2318 			return (error);
2319 		}
2320 	}
2321 
2322 #ifdef UFS_GJOURNAL
2323 	ufs_gjournal_orphan(vp);
2324 #endif
2325 	/*
2326 	 * Delete reference to directory before purging
2327 	 * inode.  If we crash in between, the directory
2328 	 * will be reattached to lost+found,
2329 	 */
2330 	dp->i_effnlink--;
2331 	ip->i_effnlink--;
2332 	if (DOINGSOFTDEP(vp))
2333 		softdep_setup_rmdir(dp, ip);
2334 	error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
2335 	if (error) {
2336 		dp->i_effnlink++;
2337 		ip->i_effnlink++;
2338 		if (DOINGSOFTDEP(vp))
2339 			softdep_revert_rmdir(dp, ip);
2340 		goto out;
2341 	}
2342 	/*
2343 	 * The only stuff left in the directory is "." and "..". The "."
2344 	 * reference is inconsequential since we are quashing it. The soft
2345 	 * dependency code will arrange to do these operations after
2346 	 * the parent directory entry has been deleted on disk, so
2347 	 * when running with that code we avoid doing them now.
2348 	 */
2349 	if (!DOINGSOFTDEP(vp)) {
2350 		dp->i_nlink--;
2351 		DIP_SET_NLINK(dp, dp->i_nlink);
2352 		UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2353 		error = UFS_UPDATE(dvp, 0);
2354 		ip->i_nlink--;
2355 		DIP_SET_NLINK(ip, ip->i_nlink);
2356 		UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2357 	}
2358 	cache_vop_rmdir(dvp, vp);
2359 #ifdef UFS_DIRHASH
2360 	/* Kill any active hash; i_effnlink == 0, so it will not come back. */
2361 	if (ip->i_dirhash != NULL)
2362 		ufsdirhash_free(ip);
2363 #endif
2364 out:
2365 	return (error);
2366 }
2367 
2368 /*
2369  * symlink -- make a symbolic link
2370  */
2371 static int
ufs_symlink(struct vop_symlink_args * ap)2372 ufs_symlink(
2373 	struct vop_symlink_args /* {
2374 		struct vnode *a_dvp;
2375 		struct vnode **a_vpp;
2376 		struct componentname *a_cnp;
2377 		struct vattr *a_vap;
2378 		const char *a_target;
2379 	} */ *ap)
2380 {
2381 	struct vnode *vp, **vpp = ap->a_vpp;
2382 	struct inode *ip;
2383 	int len, error;
2384 
2385 	error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
2386 	    vpp, ap->a_cnp, "ufs_symlink");
2387 	if (error)
2388 		return (error);
2389 	vp = *vpp;
2390 	len = strlen(ap->a_target);
2391 	if (len < VFSTOUFS(vp->v_mount)->um_maxsymlinklen) {
2392 		ip = VTOI(vp);
2393 		bcopy(ap->a_target, DIP(ip, i_shortlink), len);
2394 		ip->i_size = len;
2395 		DIP_SET(ip, i_size, len);
2396 		UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
2397 		error = UFS_UPDATE(vp, 0);
2398 	} else
2399 		error = vn_rdwr(UIO_WRITE, vp, __DECONST(void *, ap->a_target),
2400 		    len, (off_t)0, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
2401 		    ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
2402 	if (error)
2403 		vput(vp);
2404 	return (error);
2405 }
2406 
2407 /*
2408  * Vnode op for reading directories.
2409  */
2410 int
ufs_readdir(struct vop_readdir_args * ap)2411 ufs_readdir(
2412 	struct vop_readdir_args /* {
2413 		struct vnode *a_vp;
2414 		struct uio *a_uio;
2415 		struct ucred *a_cred;
2416 		int *a_eofflag;
2417 		int *a_ncookies;
2418 		u_long **a_cookies;
2419 	} */ *ap)
2420 {
2421 	struct vnode *vp = ap->a_vp;
2422 	struct uio *uio = ap->a_uio;
2423 	struct buf *bp;
2424 	struct inode *ip;
2425 	struct direct *dp, *edp;
2426 	u_long *cookies;
2427 	struct dirent dstdp;
2428 	off_t offset, startoffset;
2429 	size_t readcnt, skipcnt;
2430 	ssize_t startresid;
2431 	uint64_t ncookies;
2432 	int error;
2433 
2434 	if (uio->uio_offset < 0)
2435 		return (EINVAL);
2436 	ip = VTOI(vp);
2437 	if (ip->i_effnlink == 0) {
2438 		*ap->a_eofflag = 1;
2439 		return (0);
2440 	}
2441 	if (ap->a_ncookies != NULL) {
2442 		if (uio->uio_resid < 0)
2443 			ncookies = 0;
2444 		else
2445 			ncookies = uio->uio_resid;
2446 		if (uio->uio_offset >= ip->i_size)
2447 			ncookies = 0;
2448 		else if (ip->i_size - uio->uio_offset < ncookies)
2449 			ncookies = ip->i_size - uio->uio_offset;
2450 		ncookies = ncookies / (offsetof(struct direct, d_name) + 4) + 1;
2451 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
2452 		*ap->a_ncookies = ncookies;
2453 		*ap->a_cookies = cookies;
2454 	} else {
2455 		ncookies = 0;
2456 		cookies = NULL;
2457 	}
2458 	offset = startoffset = uio->uio_offset;
2459 	startresid = uio->uio_resid;
2460 	error = 0;
2461 	while (error == 0 && uio->uio_resid > 0 &&
2462 	    uio->uio_offset < ip->i_size) {
2463 		error = UFS_BLKATOFF(vp, uio->uio_offset, NULL, &bp);
2464 		if (error)
2465 			break;
2466 		if (bp->b_offset + bp->b_bcount > ip->i_size)
2467 			readcnt = ip->i_size - bp->b_offset;
2468 		else
2469 			readcnt = bp->b_bcount;
2470 		skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
2471 		    ~(size_t)(DIRBLKSIZ - 1);
2472 		offset = bp->b_offset + skipcnt;
2473 		dp = (struct direct *)&bp->b_data[skipcnt];
2474 		edp = (struct direct *)&bp->b_data[readcnt];
2475 		while (error == 0 && uio->uio_resid > 0 && dp < edp) {
2476 			if (dp->d_reclen <= offsetof(struct direct, d_name) ||
2477 			    (caddr_t)dp + dp->d_reclen > (caddr_t)edp) {
2478 				error = EIO;
2479 				break;
2480 			}
2481 #if BYTE_ORDER == LITTLE_ENDIAN
2482 			/* Old filesystem format. */
2483 			if (OFSFMT(vp)) {
2484 				dstdp.d_namlen = dp->d_type;
2485 				dstdp.d_type = dp->d_namlen;
2486 			} else
2487 #endif
2488 			{
2489 				dstdp.d_namlen = dp->d_namlen;
2490 				dstdp.d_type = dp->d_type;
2491 			}
2492 			if (offsetof(struct direct, d_name) + dstdp.d_namlen >
2493 			    dp->d_reclen) {
2494 				error = EIO;
2495 				break;
2496 			}
2497 			if (offset < startoffset || dp->d_ino == 0)
2498 				goto nextentry;
2499 			dstdp.d_fileno = dp->d_ino;
2500 			dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
2501 			bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
2502 			/* NOTE: d_off is the offset of the *next* entry. */
2503 			dstdp.d_off = offset + dp->d_reclen;
2504 			dirent_terminate(&dstdp);
2505 			if (dstdp.d_reclen > uio->uio_resid) {
2506 				if (uio->uio_resid == startresid)
2507 					error = EINVAL;
2508 				else
2509 					error = EJUSTRETURN;
2510 				break;
2511 			}
2512 			/* Advance dp. */
2513 			error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
2514 			if (error)
2515 				break;
2516 			if (cookies != NULL) {
2517 				KASSERT(ncookies > 0,
2518 				    ("ufs_readdir: cookies buffer too small"));
2519 				*cookies = offset + dp->d_reclen;
2520 				cookies++;
2521 				ncookies--;
2522 			}
2523 nextentry:
2524 			offset += dp->d_reclen;
2525 			dp = (struct direct *)((caddr_t)dp + dp->d_reclen);
2526 		}
2527 		bqrelse(bp);
2528 		uio->uio_offset = offset;
2529 	}
2530 	/* We need to correct uio_offset. */
2531 	uio->uio_offset = offset;
2532 	if (error == EJUSTRETURN)
2533 		error = 0;
2534 	if (ap->a_ncookies != NULL) {
2535 		if (error == 0) {
2536 			*ap->a_ncookies -= ncookies;
2537 		} else {
2538 			free(*ap->a_cookies, M_TEMP);
2539 			*ap->a_ncookies = 0;
2540 			*ap->a_cookies = NULL;
2541 		}
2542 	}
2543 	if (error == 0 && ap->a_eofflag)
2544 		*ap->a_eofflag = ip->i_size <= uio->uio_offset;
2545 	return (error);
2546 }
2547 
2548 /*
2549  * Return target name of a symbolic link
2550  */
2551 static int
ufs_readlink(struct vop_readlink_args * ap)2552 ufs_readlink(
2553 	struct vop_readlink_args /* {
2554 		struct vnode *a_vp;
2555 		struct uio *a_uio;
2556 		struct ucred *a_cred;
2557 	} */ *ap)
2558 {
2559 	struct vnode *vp = ap->a_vp;
2560 	struct inode *ip = VTOI(vp);
2561 	doff_t isize;
2562 
2563 	isize = ip->i_size;
2564 	if (isize < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
2565 		return (uiomove(DIP(ip, i_shortlink), isize, ap->a_uio));
2566 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
2567 }
2568 
2569 /*
2570  * Calculate the logical to physical mapping if not done already,
2571  * then call the device strategy routine.
2572  *
2573  * In order to be able to swap to a file, the ufs_bmaparray() operation may not
2574  * deadlock on memory.  See ufs_bmap() for details.
2575  */
2576 static int
ufs_strategy(struct vop_strategy_args * ap)2577 ufs_strategy(
2578 	struct vop_strategy_args /* {
2579 		struct vnode *a_vp;
2580 		struct buf *a_bp;
2581 	} */ *ap)
2582 {
2583 	struct buf *bp = ap->a_bp;
2584 	struct vnode *vp = ap->a_vp;
2585 	ufs2_daddr_t blkno;
2586 	int error;
2587 
2588 	if (bp->b_blkno == bp->b_lblkno) {
2589 		error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2590 		bp->b_blkno = blkno;
2591 		if (error) {
2592 			bp->b_error = error;
2593 			bp->b_ioflags |= BIO_ERROR;
2594 			bufdone(bp);
2595 			return (0);
2596 		}
2597 		if ((long)bp->b_blkno == -1)
2598 			vfs_bio_clrbuf(bp);
2599 	}
2600 	if ((long)bp->b_blkno == -1) {
2601 		bufdone(bp);
2602 		return (0);
2603 	}
2604 	bp->b_iooffset = dbtob(bp->b_blkno);
2605 	BO_STRATEGY(VFSTOUFS(vp->v_mount)->um_bo, bp);
2606 	return (0);
2607 }
2608 
2609 /*
2610  * Print out the contents of an inode.
2611  */
2612 static int
ufs_print(struct vop_print_args * ap)2613 ufs_print(
2614 	struct vop_print_args /* {
2615 		struct vnode *a_vp;
2616 	} */ *ap)
2617 {
2618 	struct vnode *vp = ap->a_vp;
2619 	struct inode *ip = VTOI(vp);
2620 
2621 	printf("\tnlink=%d, effnlink=%d, size=%jd", ip->i_nlink,
2622 	    ip->i_effnlink, (intmax_t)ip->i_size);
2623 	if (I_IS_UFS2(ip))
2624 		printf(", extsize %d", ip->i_din2->di_extsize);
2625 	printf("\n\tgeneration=%jx, uid=%d, gid=%d, flags=0x%b\n",
2626 	    (uintmax_t)ip->i_gen, ip->i_uid, ip->i_gid,
2627 	    (uint32_t)ip->i_flags, PRINT_INODE_FLAGS);
2628 	printf("\tino %ju, on dev %s", (intmax_t)ip->i_number,
2629 	    devtoname(ITODEV(ip)));
2630 	if (vp->v_type == VFIFO)
2631 		fifo_printinfo(vp);
2632 	printf("\n");
2633 	return (0);
2634 }
2635 
2636 /*
2637  * Close wrapper for fifos.
2638  *
2639  * Update the times on the inode then do device close.
2640  */
2641 static int
ufsfifo_close(struct vop_close_args * ap)2642 ufsfifo_close(
2643 	struct vop_close_args /* {
2644 		struct vnode *a_vp;
2645 		int  a_fflag;
2646 		struct ucred *a_cred;
2647 		struct thread *a_td;
2648 	} */ *ap)
2649 {
2650 
2651 	ufs_close(ap);
2652 	return (fifo_specops.vop_close(ap));
2653 }
2654 
2655 /*
2656  * Return POSIX pathconf information applicable to ufs filesystems.
2657  */
2658 static int
ufs_pathconf(struct vop_pathconf_args * ap)2659 ufs_pathconf(
2660 	struct vop_pathconf_args /* {
2661 		struct vnode *a_vp;
2662 		int a_name;
2663 		int *a_retval;
2664 	} */ *ap)
2665 {
2666 	int error;
2667 
2668 	error = 0;
2669 	switch (ap->a_name) {
2670 	case _PC_LINK_MAX:
2671 		*ap->a_retval = UFS_LINK_MAX;
2672 		break;
2673 	case _PC_NAME_MAX:
2674 		*ap->a_retval = UFS_MAXNAMLEN;
2675 		break;
2676 	case _PC_PIPE_BUF:
2677 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
2678 			*ap->a_retval = PIPE_BUF;
2679 		else
2680 			error = EINVAL;
2681 		break;
2682 	case _PC_CHOWN_RESTRICTED:
2683 		*ap->a_retval = 1;
2684 		break;
2685 	case _PC_NO_TRUNC:
2686 		*ap->a_retval = 1;
2687 		break;
2688 #ifdef UFS_ACL
2689 	case _PC_ACL_EXTENDED:
2690 		if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2691 			*ap->a_retval = 1;
2692 		else
2693 			*ap->a_retval = 0;
2694 		break;
2695 	case _PC_ACL_NFS4:
2696 		if (ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS)
2697 			*ap->a_retval = 1;
2698 		else
2699 			*ap->a_retval = 0;
2700 		break;
2701 #endif
2702 	case _PC_ACL_PATH_MAX:
2703 #ifdef UFS_ACL
2704 		if (ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS))
2705 			*ap->a_retval = ACL_MAX_ENTRIES;
2706 		else
2707 			*ap->a_retval = 3;
2708 #else
2709 		*ap->a_retval = 3;
2710 #endif
2711 		break;
2712 #ifdef MAC
2713 	case _PC_MAC_PRESENT:
2714 		if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2715 			*ap->a_retval = 1;
2716 		else
2717 			*ap->a_retval = 0;
2718 		break;
2719 #endif
2720 	case _PC_MIN_HOLE_SIZE:
2721 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2722 		break;
2723 	case _PC_PRIO_IO:
2724 		*ap->a_retval = 0;
2725 		break;
2726 	case _PC_SYNC_IO:
2727 		*ap->a_retval = 0;
2728 		break;
2729 	case _PC_ALLOC_SIZE_MIN:
2730 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2731 		break;
2732 	case _PC_FILESIZEBITS:
2733 		*ap->a_retval = 64;
2734 		break;
2735 	case _PC_REC_INCR_XFER_SIZE:
2736 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2737 		break;
2738 	case _PC_REC_MAX_XFER_SIZE:
2739 		*ap->a_retval = -1; /* means ``unlimited'' */
2740 		break;
2741 	case _PC_REC_MIN_XFER_SIZE:
2742 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2743 		break;
2744 	case _PC_REC_XFER_ALIGN:
2745 		*ap->a_retval = PAGE_SIZE;
2746 		break;
2747 	case _PC_SYMLINK_MAX:
2748 		*ap->a_retval = MAXPATHLEN;
2749 		break;
2750 
2751 	default:
2752 		error = vop_stdpathconf(ap);
2753 		break;
2754 	}
2755 	return (error);
2756 }
2757 
2758 /*
2759  * Initialize the vnode associated with a new inode, handle aliased
2760  * vnodes.
2761  */
2762 int
ufs_vinit(struct mount * mntp,struct vop_vector * fifoops,struct vnode ** vpp)2763 ufs_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
2764 {
2765 	struct inode *ip;
2766 	struct vnode *vp;
2767 
2768 	vp = *vpp;
2769 	ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2770 	ip = VTOI(vp);
2771 	vp->v_type = IFTOVT(ip->i_mode);
2772 	/*
2773 	 * Only unallocated inodes should be of type VNON.
2774 	 */
2775 	if (ip->i_mode != 0 && vp->v_type == VNON)
2776 		return (EINVAL);
2777 	if (vp->v_type == VFIFO)
2778 		vp->v_op = fifoops;
2779 	if (ip->i_number == UFS_ROOTINO)
2780 		vp->v_vflag |= VV_ROOT;
2781 	*vpp = vp;
2782 	return (0);
2783 }
2784 
2785 /*
2786  * Allocate a new inode.
2787  * Vnode dvp must be locked.
2788  */
2789 static int
ufs_makeinode(int mode,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,const char * callfunc)2790 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2791     struct componentname *cnp, const char *callfunc)
2792 {
2793 	struct inode *ip, *pdir;
2794 	struct direct newdir;
2795 	struct vnode *tvp;
2796 	int error;
2797 
2798 	pdir = VTOI(dvp);
2799 #ifdef INVARIANTS
2800 	if ((cnp->cn_flags & HASBUF) == 0)
2801 		panic("%s: no name", callfunc);
2802 #endif
2803 	*vpp = NULL;
2804 	if ((mode & IFMT) == 0)
2805 		mode |= IFREG;
2806 
2807 	if (pdir->i_effnlink < 2) {
2808 		print_bad_link_count(callfunc, dvp);
2809 		return (EINVAL);
2810 	}
2811 	if (DOINGSUJ(dvp)) {
2812 		error = softdep_prelink(dvp, NULL, cnp);
2813 		if (error != 0) {
2814 			MPASS(error == ERELOOKUP);
2815 			return (error);
2816 		}
2817 	}
2818 	error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2819 	if (error)
2820 		return (error);
2821 	ip = VTOI(tvp);
2822 	ip->i_gid = pdir->i_gid;
2823 	DIP_SET(ip, i_gid, pdir->i_gid);
2824 #ifdef SUIDDIR
2825 	{
2826 #ifdef QUOTA
2827 		struct ucred ucred, *ucp;
2828 		gid_t ucred_group;
2829 		ucp = cnp->cn_cred;
2830 #endif
2831 		/*
2832 		 * If we are not the owner of the directory,
2833 		 * and we are hacking owners here, (only do this where told to)
2834 		 * and we are not giving it TO root, (would subvert quotas)
2835 		 * then go ahead and give it to the other user.
2836 		 * Note that this drops off the execute bits for security.
2837 		 */
2838 		if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2839 		    (pdir->i_mode & ISUID) &&
2840 		    (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2841 			ip->i_uid = pdir->i_uid;
2842 			DIP_SET(ip, i_uid, ip->i_uid);
2843 			mode &= ~07111;
2844 #ifdef QUOTA
2845 			/*
2846 			 * Make sure the correct user gets charged
2847 			 * for the space.
2848 			 * Quickly knock up a dummy credential for the victim.
2849 			 * XXX This seems to never be accessed out of our
2850 			 * context so a stack variable is ok.
2851 			 */
2852 			refcount_init(&ucred.cr_ref, 1);
2853 			ucred.cr_uid = ip->i_uid;
2854 			ucred.cr_ngroups = 1;
2855 			ucred.cr_groups = &ucred_group;
2856 			ucred.cr_groups[0] = pdir->i_gid;
2857 			ucp = &ucred;
2858 #endif
2859 		} else {
2860 			ip->i_uid = cnp->cn_cred->cr_uid;
2861 			DIP_SET(ip, i_uid, ip->i_uid);
2862 		}
2863 
2864 #ifdef QUOTA
2865 		if ((error = getinoquota(ip)) ||
2866 	    	    (error = chkiq(ip, 1, ucp, 0))) {
2867 			if (DOINGSOFTDEP(tvp))
2868 				softdep_revert_link(pdir, ip);
2869 			UFS_VFREE(tvp, ip->i_number, mode);
2870 			vgone(tvp);
2871 			vput(tvp);
2872 			return (error);
2873 		}
2874 #endif
2875 	}
2876 #else	/* !SUIDDIR */
2877 	ip->i_uid = cnp->cn_cred->cr_uid;
2878 	DIP_SET(ip, i_uid, ip->i_uid);
2879 #ifdef QUOTA
2880 	if ((error = getinoquota(ip)) ||
2881 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2882 		if (DOINGSOFTDEP(tvp))
2883 			softdep_revert_link(pdir, ip);
2884 		UFS_VFREE(tvp, ip->i_number, mode);
2885 		vgone(tvp);
2886 		vput(tvp);
2887 		return (error);
2888 	}
2889 #endif
2890 #endif	/* !SUIDDIR */
2891 	vn_seqc_write_begin(tvp); /* Mostly to cover asserts */
2892 	UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
2893 	UFS_INODE_SET_MODE(ip, mode);
2894 	DIP_SET(ip, i_mode, mode);
2895 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
2896 	ip->i_effnlink = 1;
2897 	ip->i_nlink = 1;
2898 	DIP_SET_NLINK(ip, 1);
2899 	if (DOINGSOFTDEP(tvp))
2900 		softdep_setup_create(VTOI(dvp), ip);
2901 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2902 	    priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID)) {
2903 		UFS_INODE_SET_MODE(ip, ip->i_mode & ~ISGID);
2904 		DIP_SET(ip, i_mode, ip->i_mode);
2905 	}
2906 
2907 	if (cnp->cn_flags & ISWHITEOUT) {
2908 		ip->i_flags |= UF_OPAQUE;
2909 		DIP_SET(ip, i_flags, ip->i_flags);
2910 	}
2911 
2912 	/*
2913 	 * Make sure inode goes to disk before directory entry.
2914 	 */
2915 	error = UFS_UPDATE(tvp, !DOINGSOFTDEP(tvp) && !DOINGASYNC(tvp));
2916 	if (error)
2917 		goto bad;
2918 #ifdef MAC
2919 	if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2920 		error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2921 		    dvp, tvp, cnp);
2922 		if (error)
2923 			goto bad;
2924 	}
2925 #endif
2926 #ifdef UFS_ACL
2927 	if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2928 		error = ufs_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
2929 		    cnp->cn_cred, cnp->cn_thread);
2930 		if (error)
2931 			goto bad;
2932 	} else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2933 		error = ufs_do_nfs4_acl_inheritance(dvp, tvp, mode,
2934 		    cnp->cn_cred, cnp->cn_thread);
2935 		if (error)
2936 			goto bad;
2937 	}
2938 #endif /* !UFS_ACL */
2939 	ufs_makedirentry(ip, cnp, &newdir);
2940 	error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2941 	if (error)
2942 		goto bad;
2943 	vn_seqc_write_end(tvp);
2944 	*vpp = tvp;
2945 	return (0);
2946 
2947 bad:
2948 	/*
2949 	 * Write error occurred trying to update the inode
2950 	 * or the directory so must deallocate the inode.
2951 	 */
2952 	ip->i_effnlink = 0;
2953 	ip->i_nlink = 0;
2954 	DIP_SET_NLINK(ip, 0);
2955 	UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2956 	if (DOINGSOFTDEP(tvp))
2957 		softdep_revert_create(VTOI(dvp), ip);
2958 	vn_seqc_write_end(tvp);
2959 	vgone(tvp);
2960 	vput(tvp);
2961 	return (error);
2962 }
2963 
2964 static int
ufs_ioctl(struct vop_ioctl_args * ap)2965 ufs_ioctl(struct vop_ioctl_args *ap)
2966 {
2967 	struct vnode *vp;
2968 	int error;
2969 
2970 	vp = ap->a_vp;
2971 	switch (ap->a_command) {
2972 	case FIOSEEKDATA:
2973 		error = vn_lock(vp, LK_EXCLUSIVE);
2974 		if (error == 0) {
2975 			error = ufs_bmap_seekdata(vp, (off_t *)ap->a_data);
2976 			VOP_UNLOCK(vp);
2977 		} else
2978 			error = EBADF;
2979 		return (error);
2980 	case FIOSEEKHOLE:
2981 		return (vn_bmap_seekhole(vp, ap->a_command, (off_t *)ap->a_data,
2982 		    ap->a_cred));
2983 	default:
2984 		return (ENOTTY);
2985 	}
2986 }
2987 
2988 static int
ufs_read_pgcache(struct vop_read_pgcache_args * ap)2989 ufs_read_pgcache(struct vop_read_pgcache_args *ap)
2990 {
2991 	struct uio *uio;
2992 	struct vnode *vp;
2993 
2994 	uio = ap->a_uio;
2995 	vp = ap->a_vp;
2996 	VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
2997 
2998 	if (uio->uio_resid > ptoa(io_hold_cnt) || uio->uio_offset < 0 ||
2999 	    (ap->a_ioflag & IO_DIRECT) != 0)
3000 		return (EJUSTRETURN);
3001 	return (vn_read_from_obj(vp, uio));
3002 }
3003 
3004 /* Global vfs data structures for ufs. */
3005 struct vop_vector ufs_vnodeops = {
3006 	.vop_default =		&default_vnodeops,
3007 	.vop_fsync =		VOP_PANIC,
3008 	.vop_read =		VOP_PANIC,
3009 	.vop_reallocblks =	VOP_PANIC,
3010 	.vop_write =		VOP_PANIC,
3011 	.vop_accessx =		ufs_accessx,
3012 	.vop_bmap =		ufs_bmap,
3013 	.vop_fplookup_vexec =	ufs_fplookup_vexec,
3014 	.vop_fplookup_symlink =	VOP_EAGAIN,
3015 	.vop_cachedlookup =	ufs_lookup,
3016 	.vop_close =		ufs_close,
3017 	.vop_create =		ufs_create,
3018 	.vop_stat =		ufs_stat,
3019 	.vop_getattr =		ufs_getattr,
3020 	.vop_inactive =		ufs_inactive,
3021 	.vop_ioctl =		ufs_ioctl,
3022 	.vop_link =		ufs_link,
3023 	.vop_lookup =		vfs_cache_lookup,
3024 	.vop_mmapped =		ufs_mmapped,
3025 	.vop_mkdir =		ufs_mkdir,
3026 	.vop_mknod =		ufs_mknod,
3027 	.vop_need_inactive =	ufs_need_inactive,
3028 	.vop_open =		ufs_open,
3029 	.vop_pathconf =		ufs_pathconf,
3030 	.vop_poll =		vop_stdpoll,
3031 	.vop_print =		ufs_print,
3032 	.vop_read_pgcache =	ufs_read_pgcache,
3033 	.vop_readdir =		ufs_readdir,
3034 	.vop_readlink =		ufs_readlink,
3035 	.vop_reclaim =		ufs_reclaim,
3036 	.vop_remove =		ufs_remove,
3037 	.vop_rename =		ufs_rename,
3038 	.vop_rmdir =		ufs_rmdir,
3039 	.vop_setattr =		ufs_setattr,
3040 #ifdef MAC
3041 	.vop_setlabel =		vop_stdsetlabel_ea,
3042 #endif
3043 	.vop_strategy =		ufs_strategy,
3044 	.vop_symlink =		ufs_symlink,
3045 	.vop_whiteout =		ufs_whiteout,
3046 #ifdef UFS_EXTATTR
3047 	.vop_getextattr =	ufs_getextattr,
3048 	.vop_deleteextattr =	ufs_deleteextattr,
3049 	.vop_setextattr =	ufs_setextattr,
3050 #endif
3051 #ifdef UFS_ACL
3052 	.vop_getacl =		ufs_getacl,
3053 	.vop_setacl =		ufs_setacl,
3054 	.vop_aclcheck =		ufs_aclcheck,
3055 #endif
3056 };
3057 VFS_VOP_VECTOR_REGISTER(ufs_vnodeops);
3058 
3059 struct vop_vector ufs_fifoops = {
3060 	.vop_default =		&fifo_specops,
3061 	.vop_fsync =		VOP_PANIC,
3062 	.vop_accessx =		ufs_accessx,
3063 	.vop_close =		ufsfifo_close,
3064 	.vop_getattr =		ufs_getattr,
3065 	.vop_inactive =		ufs_inactive,
3066 	.vop_pathconf = 	ufs_pathconf,
3067 	.vop_print =		ufs_print,
3068 	.vop_read =		VOP_PANIC,
3069 	.vop_reclaim =		ufs_reclaim,
3070 	.vop_setattr =		ufs_setattr,
3071 #ifdef MAC
3072 	.vop_setlabel =		vop_stdsetlabel_ea,
3073 #endif
3074 	.vop_write =		VOP_PANIC,
3075 #ifdef UFS_EXTATTR
3076 	.vop_getextattr =	ufs_getextattr,
3077 	.vop_deleteextattr =	ufs_deleteextattr,
3078 	.vop_setextattr =	ufs_setextattr,
3079 #endif
3080 #ifdef UFS_ACL
3081 	.vop_getacl =		ufs_getacl,
3082 	.vop_setacl =		ufs_setacl,
3083 	.vop_aclcheck =		ufs_aclcheck,
3084 #endif
3085 };
3086 VFS_VOP_VECTOR_REGISTER(ufs_fifoops);
3087