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