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