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