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