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