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