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