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