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