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