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