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