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