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