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