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