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