1 /* $NetBSD: ufs_vnops.c,v 1.189 2011/04/30 14:24:27 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Wasabi Systems, 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1989, 1993, 1995 34 * The Regents of the University of California. All rights reserved. 35 * (c) UNIX System Laboratories, Inc. 36 * All or some portions of this file are derived from material licensed 37 * to the University of California by American Telephone and Telegraph 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 39 * the permission of UNIX System Laboratories, Inc. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)ufs_vnops.c 8.28 (Berkeley) 7/31/95 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: ufs_vnops.c,v 1.189 2011/04/30 14:24:27 hannken Exp $"); 70 71 #if defined(_KERNEL_OPT) 72 #include "opt_ffs.h" 73 #include "opt_quota.h" 74 #endif 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/namei.h> 79 #include <sys/resourcevar.h> 80 #include <sys/kernel.h> 81 #include <sys/file.h> 82 #include <sys/stat.h> 83 #include <sys/buf.h> 84 #include <sys/proc.h> 85 #include <sys/mount.h> 86 #include <sys/vnode.h> 87 #include <sys/malloc.h> 88 #include <sys/dirent.h> 89 #include <sys/lockf.h> 90 #include <sys/kauth.h> 91 #include <sys/wapbl.h> 92 #include <sys/fstrans.h> 93 94 #include <miscfs/specfs/specdev.h> 95 #include <miscfs/fifofs/fifo.h> 96 #include <miscfs/genfs/genfs.h> 97 98 #include <ufs/ufs/inode.h> 99 #include <ufs/ufs/dir.h> 100 #include <ufs/ufs/ufsmount.h> 101 #include <ufs/ufs/ufs_bswap.h> 102 #include <ufs/ufs/ufs_extern.h> 103 #include <ufs/ufs/ufs_wapbl.h> 104 #ifdef UFS_DIRHASH 105 #include <ufs/ufs/dirhash.h> 106 #endif 107 #include <ufs/ext2fs/ext2fs_extern.h> 108 #include <ufs/ffs/ffs_extern.h> 109 #include <ufs/lfs/lfs_extern.h> 110 111 #include <uvm/uvm.h> 112 113 static int ufs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *); 114 static int ufs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, 115 struct lwp *); 116 117 /* 118 * A virgin directory (no blushing please). 119 */ 120 static const struct dirtemplate mastertemplate = { 121 0, 12, DT_DIR, 1, ".", 122 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." 123 }; 124 125 /* 126 * Create a regular file 127 */ 128 int 129 ufs_create(void *v) 130 { 131 struct vop_create_args /* { 132 struct vnode *a_dvp; 133 struct vnode **a_vpp; 134 struct componentname *a_cnp; 135 struct vattr *a_vap; 136 } */ *ap = v; 137 int error; 138 139 /* 140 * UFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful 141 * ufs_makeinode 142 */ 143 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED); 144 error = 145 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), 146 ap->a_dvp, ap->a_vpp, ap->a_cnp); 147 if (error) { 148 fstrans_done(ap->a_dvp->v_mount); 149 return (error); 150 } 151 UFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp); 152 fstrans_done(ap->a_dvp->v_mount); 153 VN_KNOTE(ap->a_dvp, NOTE_WRITE); 154 return (0); 155 } 156 157 /* 158 * Mknod vnode call 159 */ 160 /* ARGSUSED */ 161 int 162 ufs_mknod(void *v) 163 { 164 struct vop_mknod_args /* { 165 struct vnode *a_dvp; 166 struct vnode **a_vpp; 167 struct componentname *a_cnp; 168 struct vattr *a_vap; 169 } */ *ap = v; 170 struct vattr *vap; 171 struct vnode **vpp; 172 struct inode *ip; 173 int error; 174 struct mount *mp; 175 ino_t ino; 176 177 vap = ap->a_vap; 178 vpp = ap->a_vpp; 179 180 /* 181 * UFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful 182 * ufs_makeinode 183 */ 184 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED); 185 if ((error = 186 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), 187 ap->a_dvp, vpp, ap->a_cnp)) != 0) 188 goto out; 189 VN_KNOTE(ap->a_dvp, NOTE_WRITE); 190 ip = VTOI(*vpp); 191 mp = (*vpp)->v_mount; 192 ino = ip->i_number; 193 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 194 if (vap->va_rdev != VNOVAL) { 195 struct ufsmount *ump = ip->i_ump; 196 /* 197 * Want to be able to use this to make badblock 198 * inodes, so don't truncate the dev number. 199 */ 200 if (ump->um_fstype == UFS1) 201 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev, 202 UFS_MPNEEDSWAP(ump)); 203 else 204 ip->i_ffs2_rdev = ufs_rw64(vap->va_rdev, 205 UFS_MPNEEDSWAP(ump)); 206 } 207 UFS_WAPBL_UPDATE(*vpp, NULL, NULL, 0); 208 UFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp); 209 /* 210 * Remove inode so that it will be reloaded by VFS_VGET and 211 * checked to see if it is an alias of an existing entry in 212 * the inode cache. 213 */ 214 (*vpp)->v_type = VNON; 215 VOP_UNLOCK(*vpp); 216 vgone(*vpp); 217 error = VFS_VGET(mp, ino, vpp); 218 out: 219 fstrans_done(ap->a_dvp->v_mount); 220 if (error != 0) { 221 *vpp = NULL; 222 return (error); 223 } 224 return (0); 225 } 226 227 /* 228 * Open called. 229 * 230 * Nothing to do. 231 */ 232 /* ARGSUSED */ 233 int 234 ufs_open(void *v) 235 { 236 struct vop_open_args /* { 237 struct vnode *a_vp; 238 int a_mode; 239 kauth_cred_t a_cred; 240 } */ *ap = v; 241 242 /* 243 * Files marked append-only must be opened for appending. 244 */ 245 if ((VTOI(ap->a_vp)->i_flags & APPEND) && 246 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 247 return (EPERM); 248 return (0); 249 } 250 251 /* 252 * Close called. 253 * 254 * Update the times on the inode. 255 */ 256 /* ARGSUSED */ 257 int 258 ufs_close(void *v) 259 { 260 struct vop_close_args /* { 261 struct vnode *a_vp; 262 int a_fflag; 263 kauth_cred_t a_cred; 264 } */ *ap = v; 265 struct vnode *vp; 266 struct inode *ip; 267 268 vp = ap->a_vp; 269 ip = VTOI(vp); 270 fstrans_start(vp->v_mount, FSTRANS_SHARED); 271 if (vp->v_usecount > 1) 272 UFS_ITIMES(vp, NULL, NULL, NULL); 273 fstrans_done(vp->v_mount); 274 return (0); 275 } 276 277 static int 278 ufs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode, 279 kauth_cred_t cred) 280 { 281 #if defined(QUOTA) || defined(QUOTA2) 282 int error; 283 #endif 284 285 /* 286 * Disallow write attempts on read-only file systems; 287 * unless the file is a socket, fifo, or a block or 288 * character device resident on the file system. 289 */ 290 if (mode & VWRITE) { 291 switch (vp->v_type) { 292 case VDIR: 293 case VLNK: 294 case VREG: 295 if (vp->v_mount->mnt_flag & MNT_RDONLY) 296 return (EROFS); 297 #if defined(QUOTA) || defined(QUOTA2) 298 fstrans_start(vp->v_mount, FSTRANS_SHARED); 299 error = chkdq(ip, 0, cred, 0); 300 fstrans_done(vp->v_mount); 301 if (error != 0) 302 return error; 303 #endif 304 break; 305 case VBAD: 306 case VBLK: 307 case VCHR: 308 case VSOCK: 309 case VFIFO: 310 case VNON: 311 default: 312 break; 313 } 314 } 315 316 /* If it is a snapshot, nobody gets access to it. */ 317 if ((ip->i_flags & SF_SNAPSHOT)) 318 return (EPERM); 319 /* If immutable bit set, nobody gets to write it. */ 320 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE)) 321 return (EPERM); 322 323 return 0; 324 } 325 326 static int 327 ufs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode, 328 kauth_cred_t cred) 329 { 330 331 return genfs_can_access(vp->v_type, ip->i_mode & ALLPERMS, ip->i_uid, 332 ip->i_gid, mode, cred); 333 } 334 335 int 336 ufs_access(void *v) 337 { 338 struct vop_access_args /* { 339 struct vnode *a_vp; 340 int a_mode; 341 kauth_cred_t a_cred; 342 } */ *ap = v; 343 struct vnode *vp; 344 struct inode *ip; 345 mode_t mode; 346 int error; 347 348 vp = ap->a_vp; 349 ip = VTOI(vp); 350 mode = ap->a_mode; 351 352 error = ufs_check_possible(vp, ip, mode, ap->a_cred); 353 if (error) 354 return error; 355 356 error = ufs_check_permitted(vp, ip, mode, ap->a_cred); 357 358 return error; 359 } 360 361 /* ARGSUSED */ 362 int 363 ufs_getattr(void *v) 364 { 365 struct vop_getattr_args /* { 366 struct vnode *a_vp; 367 struct vattr *a_vap; 368 kauth_cred_t a_cred; 369 } */ *ap = v; 370 struct vnode *vp; 371 struct inode *ip; 372 struct vattr *vap; 373 374 vp = ap->a_vp; 375 ip = VTOI(vp); 376 vap = ap->a_vap; 377 fstrans_start(vp->v_mount, FSTRANS_SHARED); 378 UFS_ITIMES(vp, NULL, NULL, NULL); 379 380 /* 381 * Copy from inode table 382 */ 383 vap->va_fsid = ip->i_dev; 384 vap->va_fileid = ip->i_number; 385 vap->va_mode = ip->i_mode & ALLPERMS; 386 vap->va_nlink = ip->i_nlink; 387 vap->va_uid = ip->i_uid; 388 vap->va_gid = ip->i_gid; 389 vap->va_size = vp->v_size; 390 if (ip->i_ump->um_fstype == UFS1) { 391 vap->va_rdev = (dev_t)ufs_rw32(ip->i_ffs1_rdev, 392 UFS_MPNEEDSWAP(ip->i_ump)); 393 vap->va_atime.tv_sec = ip->i_ffs1_atime; 394 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec; 395 vap->va_mtime.tv_sec = ip->i_ffs1_mtime; 396 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec; 397 vap->va_ctime.tv_sec = ip->i_ffs1_ctime; 398 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec; 399 vap->va_birthtime.tv_sec = 0; 400 vap->va_birthtime.tv_nsec = 0; 401 vap->va_bytes = dbtob((u_quad_t)ip->i_ffs1_blocks); 402 } else { 403 vap->va_rdev = (dev_t)ufs_rw64(ip->i_ffs2_rdev, 404 UFS_MPNEEDSWAP(ip->i_ump)); 405 vap->va_atime.tv_sec = ip->i_ffs2_atime; 406 vap->va_atime.tv_nsec = ip->i_ffs2_atimensec; 407 vap->va_mtime.tv_sec = ip->i_ffs2_mtime; 408 vap->va_mtime.tv_nsec = ip->i_ffs2_mtimensec; 409 vap->va_ctime.tv_sec = ip->i_ffs2_ctime; 410 vap->va_ctime.tv_nsec = ip->i_ffs2_ctimensec; 411 vap->va_birthtime.tv_sec = ip->i_ffs2_birthtime; 412 vap->va_birthtime.tv_nsec = ip->i_ffs2_birthnsec; 413 vap->va_bytes = dbtob(ip->i_ffs2_blocks); 414 } 415 vap->va_gen = ip->i_gen; 416 vap->va_flags = ip->i_flags; 417 418 /* this doesn't belong here */ 419 if (vp->v_type == VBLK) 420 vap->va_blocksize = BLKDEV_IOSIZE; 421 else if (vp->v_type == VCHR) 422 vap->va_blocksize = MAXBSIZE; 423 else 424 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; 425 vap->va_type = vp->v_type; 426 vap->va_filerev = ip->i_modrev; 427 fstrans_done(vp->v_mount); 428 return (0); 429 } 430 431 /* 432 * Set attribute vnode op. called from several syscalls 433 */ 434 int 435 ufs_setattr(void *v) 436 { 437 struct vop_setattr_args /* { 438 struct vnode *a_vp; 439 struct vattr *a_vap; 440 kauth_cred_t a_cred; 441 } */ *ap = v; 442 struct vattr *vap; 443 struct vnode *vp; 444 struct inode *ip; 445 kauth_cred_t cred; 446 struct lwp *l; 447 int error; 448 449 vap = ap->a_vap; 450 vp = ap->a_vp; 451 ip = VTOI(vp); 452 cred = ap->a_cred; 453 l = curlwp; 454 455 /* 456 * Check for unsettable attributes. 457 */ 458 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 459 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 460 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 461 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 462 return (EINVAL); 463 } 464 465 fstrans_start(vp->v_mount, FSTRANS_SHARED); 466 467 if (vap->va_flags != VNOVAL) { 468 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 469 error = EROFS; 470 goto out; 471 } 472 if (kauth_cred_geteuid(cred) != ip->i_uid && 473 (error = kauth_authorize_generic(cred, 474 KAUTH_GENERIC_ISSUSER, NULL))) 475 goto out; 476 if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, 477 NULL) == 0) { 478 if ((ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) && 479 kauth_authorize_system(l->l_cred, 480 KAUTH_SYSTEM_CHSYSFLAGS, 0, NULL, NULL, NULL)) { 481 error = EPERM; 482 goto out; 483 } 484 /* Snapshot flag cannot be set or cleared */ 485 if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) != 486 (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) { 487 error = EPERM; 488 goto out; 489 } 490 error = UFS_WAPBL_BEGIN(vp->v_mount); 491 if (error) 492 goto out; 493 ip->i_flags = vap->va_flags; 494 DIP_ASSIGN(ip, flags, ip->i_flags); 495 } else { 496 if ((ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) || 497 (vap->va_flags & UF_SETTABLE) != vap->va_flags) { 498 error = EPERM; 499 goto out; 500 } 501 if ((ip->i_flags & SF_SETTABLE) != 502 (vap->va_flags & SF_SETTABLE)) { 503 error = EPERM; 504 goto out; 505 } 506 error = UFS_WAPBL_BEGIN(vp->v_mount); 507 if (error) 508 goto out; 509 ip->i_flags &= SF_SETTABLE; 510 ip->i_flags |= (vap->va_flags & UF_SETTABLE); 511 DIP_ASSIGN(ip, flags, ip->i_flags); 512 } 513 ip->i_flag |= IN_CHANGE; 514 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 515 UFS_WAPBL_END(vp->v_mount); 516 if (vap->va_flags & (IMMUTABLE | APPEND)) { 517 error = 0; 518 goto out; 519 } 520 } 521 if (ip->i_flags & (IMMUTABLE | APPEND)) { 522 error = EPERM; 523 goto out; 524 } 525 /* 526 * Go through the fields and update iff not VNOVAL. 527 */ 528 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 529 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 530 error = EROFS; 531 goto out; 532 } 533 error = UFS_WAPBL_BEGIN(vp->v_mount); 534 if (error) 535 goto out; 536 error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, l); 537 UFS_WAPBL_END(vp->v_mount); 538 if (error) 539 goto out; 540 } 541 if (vap->va_size != VNOVAL) { 542 /* 543 * Disallow write attempts on read-only file systems; 544 * unless the file is a socket, fifo, or a block or 545 * character device resident on the file system. 546 */ 547 switch (vp->v_type) { 548 case VDIR: 549 error = EISDIR; 550 goto out; 551 case VCHR: 552 case VBLK: 553 case VFIFO: 554 break; 555 case VREG: 556 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 557 error = EROFS; 558 goto out; 559 } 560 if ((ip->i_flags & SF_SNAPSHOT) != 0) { 561 error = EPERM; 562 goto out; 563 } 564 error = UFS_WAPBL_BEGIN(vp->v_mount); 565 if (error) 566 goto out; 567 /* 568 * When journaling, only truncate one indirect block 569 * at a time. 570 */ 571 if (vp->v_mount->mnt_wapbl) { 572 uint64_t incr = MNINDIR(ip->i_ump) << 573 vp->v_mount->mnt_fs_bshift; /* Power of 2 */ 574 uint64_t base = NDADDR << 575 vp->v_mount->mnt_fs_bshift; 576 while (!error && ip->i_size > base + incr && 577 ip->i_size > vap->va_size + incr) { 578 /* 579 * round down to next full indirect 580 * block boundary. 581 */ 582 uint64_t nsize = base + 583 ((ip->i_size - base - 1) & 584 ~(incr - 1)); 585 error = UFS_TRUNCATE(vp, nsize, 0, 586 cred); 587 if (error == 0) { 588 UFS_WAPBL_END(vp->v_mount); 589 error = 590 UFS_WAPBL_BEGIN(vp->v_mount); 591 } 592 } 593 } 594 if (!error) 595 error = UFS_TRUNCATE(vp, vap->va_size, 0, cred); 596 UFS_WAPBL_END(vp->v_mount); 597 if (error) 598 goto out; 599 break; 600 default: 601 error = EOPNOTSUPP; 602 goto out; 603 } 604 } 605 ip = VTOI(vp); 606 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL || 607 vap->va_birthtime.tv_sec != VNOVAL) { 608 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 609 error = EROFS; 610 goto out; 611 } 612 if ((ip->i_flags & SF_SNAPSHOT) != 0) { 613 error = EPERM; 614 goto out; 615 } 616 error = genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred); 617 if (error) 618 goto out; 619 error = UFS_WAPBL_BEGIN(vp->v_mount); 620 if (error) 621 goto out; 622 if (vap->va_atime.tv_sec != VNOVAL) 623 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) 624 ip->i_flag |= IN_ACCESS; 625 if (vap->va_mtime.tv_sec != VNOVAL) 626 ip->i_flag |= IN_CHANGE | IN_UPDATE; 627 if (vap->va_birthtime.tv_sec != VNOVAL && 628 ip->i_ump->um_fstype == UFS2) { 629 ip->i_ffs2_birthtime = vap->va_birthtime.tv_sec; 630 ip->i_ffs2_birthnsec = vap->va_birthtime.tv_nsec; 631 } 632 error = UFS_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 0); 633 UFS_WAPBL_END(vp->v_mount); 634 if (error) 635 goto out; 636 } 637 error = 0; 638 if (vap->va_mode != (mode_t)VNOVAL) { 639 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 640 error = EROFS; 641 goto out; 642 } 643 if ((ip->i_flags & SF_SNAPSHOT) != 0 && 644 (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | 645 S_IXOTH | S_IWOTH))) { 646 error = EPERM; 647 goto out; 648 } 649 error = UFS_WAPBL_BEGIN(vp->v_mount); 650 if (error) 651 goto out; 652 error = ufs_chmod(vp, (int)vap->va_mode, cred, l); 653 UFS_WAPBL_END(vp->v_mount); 654 } 655 VN_KNOTE(vp, NOTE_ATTRIB); 656 out: 657 fstrans_done(vp->v_mount); 658 return (error); 659 } 660 661 /* 662 * Change the mode on a file. 663 * Inode must be locked before calling. 664 */ 665 static int 666 ufs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l) 667 { 668 struct inode *ip; 669 int error; 670 671 UFS_WAPBL_JLOCK_ASSERT(vp->v_mount); 672 673 ip = VTOI(vp); 674 675 error = genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid, mode); 676 if (error) 677 return (error); 678 679 fstrans_start(vp->v_mount, FSTRANS_SHARED); 680 ip->i_mode &= ~ALLPERMS; 681 ip->i_mode |= (mode & ALLPERMS); 682 ip->i_flag |= IN_CHANGE; 683 DIP_ASSIGN(ip, mode, ip->i_mode); 684 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 685 fstrans_done(vp->v_mount); 686 return (0); 687 } 688 689 /* 690 * Perform chown operation on inode ip; 691 * inode must be locked prior to call. 692 */ 693 static int 694 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred, 695 struct lwp *l) 696 { 697 struct inode *ip; 698 int error = 0; 699 #if defined(QUOTA) || defined(QUOTA2) 700 uid_t ouid; 701 gid_t ogid; 702 int64_t change; 703 #endif 704 ip = VTOI(vp); 705 error = 0; 706 707 if (uid == (uid_t)VNOVAL) 708 uid = ip->i_uid; 709 if (gid == (gid_t)VNOVAL) 710 gid = ip->i_gid; 711 712 error = genfs_can_chown(vp, cred, ip->i_uid, ip->i_gid, uid, gid); 713 if (error) 714 return (error); 715 716 fstrans_start(vp->v_mount, FSTRANS_SHARED); 717 #if defined(QUOTA) || defined(QUOTA2) 718 ogid = ip->i_gid; 719 ouid = ip->i_uid; 720 change = DIP(ip, blocks); 721 (void) chkdq(ip, -change, cred, 0); 722 (void) chkiq(ip, -1, cred, 0); 723 #endif 724 ip->i_gid = gid; 725 DIP_ASSIGN(ip, gid, gid); 726 ip->i_uid = uid; 727 DIP_ASSIGN(ip, uid, uid); 728 #if defined(QUOTA) || defined(QUOTA2) 729 if ((error = chkdq(ip, change, cred, 0)) == 0) { 730 if ((error = chkiq(ip, 1, cred, 0)) == 0) 731 goto good; 732 else 733 (void) chkdq(ip, -change, cred, FORCE); 734 } 735 ip->i_gid = ogid; 736 DIP_ASSIGN(ip, gid, ogid); 737 ip->i_uid = ouid; 738 DIP_ASSIGN(ip, uid, ouid); 739 (void) chkdq(ip, change, cred, FORCE); 740 (void) chkiq(ip, 1, cred, FORCE); 741 fstrans_done(vp->v_mount); 742 return (error); 743 good: 744 #endif /* QUOTA || QUOTA2 */ 745 ip->i_flag |= IN_CHANGE; 746 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 747 fstrans_done(vp->v_mount); 748 return (0); 749 } 750 751 int 752 ufs_remove(void *v) 753 { 754 struct vop_remove_args /* { 755 struct vnode *a_dvp; 756 struct vnode *a_vp; 757 struct componentname *a_cnp; 758 } */ *ap = v; 759 struct vnode *vp, *dvp; 760 struct inode *ip; 761 int error; 762 763 vp = ap->a_vp; 764 dvp = ap->a_dvp; 765 ip = VTOI(vp); 766 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 767 if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) || 768 (VTOI(dvp)->i_flags & APPEND)) 769 error = EPERM; 770 else { 771 error = UFS_WAPBL_BEGIN(dvp->v_mount); 772 if (error == 0) { 773 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0); 774 UFS_WAPBL_END(dvp->v_mount); 775 } 776 } 777 VN_KNOTE(vp, NOTE_DELETE); 778 VN_KNOTE(dvp, NOTE_WRITE); 779 if (dvp == vp) 780 vrele(vp); 781 else 782 vput(vp); 783 vput(dvp); 784 fstrans_done(dvp->v_mount); 785 return (error); 786 } 787 788 /* 789 * ufs_link: create hard link. 790 */ 791 int 792 ufs_link(void *v) 793 { 794 struct vop_link_args /* { 795 struct vnode *a_dvp; 796 struct vnode *a_vp; 797 struct componentname *a_cnp; 798 } */ *ap = v; 799 struct vnode *dvp = ap->a_dvp; 800 struct vnode *vp = ap->a_vp; 801 struct componentname *cnp = ap->a_cnp; 802 struct inode *ip; 803 struct direct *newdir; 804 int error; 805 806 KASSERT(dvp != vp); 807 KASSERT(vp->v_type != VDIR); 808 KASSERT(dvp->v_mount == vp->v_mount); 809 810 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 811 error = vn_lock(vp, LK_EXCLUSIVE); 812 if (error) { 813 VOP_ABORTOP(dvp, cnp); 814 goto out2; 815 } 816 ip = VTOI(vp); 817 if ((nlink_t)ip->i_nlink >= LINK_MAX) { 818 VOP_ABORTOP(dvp, cnp); 819 error = EMLINK; 820 goto out1; 821 } 822 if (ip->i_flags & (IMMUTABLE | APPEND)) { 823 VOP_ABORTOP(dvp, cnp); 824 error = EPERM; 825 goto out1; 826 } 827 error = UFS_WAPBL_BEGIN(vp->v_mount); 828 if (error) { 829 VOP_ABORTOP(dvp, cnp); 830 goto out1; 831 } 832 ip->i_nlink++; 833 DIP_ASSIGN(ip, nlink, ip->i_nlink); 834 ip->i_flag |= IN_CHANGE; 835 error = UFS_UPDATE(vp, NULL, NULL, UPDATE_DIROP); 836 if (!error) { 837 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 838 ufs_makedirentry(ip, cnp, newdir); 839 error = ufs_direnter(dvp, vp, newdir, cnp, NULL); 840 pool_cache_put(ufs_direct_cache, newdir); 841 } 842 if (error) { 843 ip->i_nlink--; 844 DIP_ASSIGN(ip, nlink, ip->i_nlink); 845 ip->i_flag |= IN_CHANGE; 846 UFS_WAPBL_UPDATE(vp, NULL, NULL, UPDATE_DIROP); 847 } 848 UFS_WAPBL_END(vp->v_mount); 849 out1: 850 VOP_UNLOCK(vp); 851 out2: 852 VN_KNOTE(vp, NOTE_LINK); 853 VN_KNOTE(dvp, NOTE_WRITE); 854 vput(dvp); 855 fstrans_done(dvp->v_mount); 856 return (error); 857 } 858 859 /* 860 * whiteout vnode call 861 */ 862 int 863 ufs_whiteout(void *v) 864 { 865 struct vop_whiteout_args /* { 866 struct vnode *a_dvp; 867 struct componentname *a_cnp; 868 int a_flags; 869 } */ *ap = v; 870 struct vnode *dvp = ap->a_dvp; 871 struct componentname *cnp = ap->a_cnp; 872 struct direct *newdir; 873 int error; 874 struct ufsmount *ump = VFSTOUFS(dvp->v_mount); 875 876 error = 0; 877 switch (ap->a_flags) { 878 case LOOKUP: 879 /* 4.4 format directories support whiteout operations */ 880 if (ump->um_maxsymlinklen > 0) 881 return (0); 882 return (EOPNOTSUPP); 883 884 case CREATE: 885 /* create a new directory whiteout */ 886 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 887 #ifdef DIAGNOSTIC 888 if (ump->um_maxsymlinklen <= 0) 889 panic("ufs_whiteout: old format filesystem"); 890 #endif 891 892 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 893 newdir->d_ino = WINO; 894 newdir->d_namlen = cnp->cn_namelen; 895 memcpy(newdir->d_name, cnp->cn_nameptr, 896 (size_t)cnp->cn_namelen); 897 newdir->d_name[cnp->cn_namelen] = '\0'; 898 newdir->d_type = DT_WHT; 899 error = ufs_direnter(dvp, NULL, newdir, cnp, NULL); 900 pool_cache_put(ufs_direct_cache, newdir); 901 break; 902 903 case DELETE: 904 /* remove an existing directory whiteout */ 905 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 906 #ifdef DIAGNOSTIC 907 if (ump->um_maxsymlinklen <= 0) 908 panic("ufs_whiteout: old format filesystem"); 909 #endif 910 911 cnp->cn_flags &= ~DOWHITEOUT; 912 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0); 913 break; 914 default: 915 panic("ufs_whiteout: unknown op"); 916 /* NOTREACHED */ 917 } 918 fstrans_done(dvp->v_mount); 919 return (error); 920 } 921 922 923 /* 924 * Rename vnode operation 925 * rename("foo", "bar"); 926 * is essentially 927 * unlink("bar"); 928 * link("foo", "bar"); 929 * unlink("foo"); 930 * but ``atomically''. Can't do full commit without saving state in the 931 * inode on disk which isn't feasible at this time. Best we can do is 932 * always guarantee the target exists. 933 * 934 * Basic algorithm is: 935 * 936 * 1) Bump link count on source while we're linking it to the 937 * target. This also ensure the inode won't be deleted out 938 * from underneath us while we work (it may be truncated by 939 * a concurrent `trunc' or `open' for creation). 940 * 2) Link source to destination. If destination already exists, 941 * delete it first. 942 * 3) Unlink source reference to inode if still around. If a 943 * directory was moved and the parent of the destination 944 * is different from the source, patch the ".." entry in the 945 * directory. 946 */ 947 int 948 ufs_rename(void *v) 949 { 950 struct vop_rename_args /* { 951 struct vnode *a_fdvp; 952 struct vnode *a_fvp; 953 struct componentname *a_fcnp; 954 struct vnode *a_tdvp; 955 struct vnode *a_tvp; 956 struct componentname *a_tcnp; 957 } */ *ap = v; 958 struct vnode *tvp, *tdvp, *fvp, *fdvp; 959 struct componentname *tcnp, *fcnp; 960 struct inode *ip, *xp, *dp; 961 struct mount *mp; 962 struct direct *newdir; 963 int doingdirectory, oldparent, newparent, error; 964 965 #ifdef WAPBL 966 if (ap->a_tdvp->v_mount->mnt_wapbl) 967 return wapbl_ufs_rename(v); 968 #endif 969 970 tvp = ap->a_tvp; 971 tdvp = ap->a_tdvp; 972 fvp = ap->a_fvp; 973 fdvp = ap->a_fdvp; 974 tcnp = ap->a_tcnp; 975 fcnp = ap->a_fcnp; 976 doingdirectory = oldparent = newparent = error = 0; 977 978 /* 979 * Check for cross-device rename. 980 */ 981 if ((fvp->v_mount != tdvp->v_mount) || 982 (tvp && (fvp->v_mount != tvp->v_mount))) { 983 error = EXDEV; 984 abortit: 985 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */ 986 if (tdvp == tvp) 987 vrele(tdvp); 988 else 989 vput(tdvp); 990 if (tvp) 991 vput(tvp); 992 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */ 993 vrele(fdvp); 994 vrele(fvp); 995 return (error); 996 } 997 998 /* 999 * Check if just deleting a link name. 1000 */ 1001 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) || 1002 (VTOI(tdvp)->i_flags & APPEND))) { 1003 error = EPERM; 1004 goto abortit; 1005 } 1006 if (fvp == tvp) { 1007 if (fvp->v_type == VDIR) { 1008 error = EINVAL; 1009 goto abortit; 1010 } 1011 1012 /* Release destination completely. */ 1013 VOP_ABORTOP(tdvp, tcnp); 1014 vput(tdvp); 1015 vput(tvp); 1016 1017 /* Delete source. */ 1018 vrele(fvp); 1019 fcnp->cn_flags &= ~(MODMASK); 1020 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 1021 fcnp->cn_nameiop = DELETE; 1022 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 1023 if ((error = relookup(fdvp, &fvp, fcnp, 0))) { 1024 vput(fdvp); 1025 return (error); 1026 } 1027 return (VOP_REMOVE(fdvp, fvp, fcnp)); 1028 } 1029 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0) 1030 goto abortit; 1031 dp = VTOI(fdvp); 1032 ip = VTOI(fvp); 1033 if ((nlink_t) ip->i_nlink >= LINK_MAX) { 1034 VOP_UNLOCK(fvp); 1035 error = EMLINK; 1036 goto abortit; 1037 } 1038 if ((ip->i_flags & (IMMUTABLE | APPEND)) || 1039 (dp->i_flags & APPEND)) { 1040 VOP_UNLOCK(fvp); 1041 error = EPERM; 1042 goto abortit; 1043 } 1044 if ((ip->i_mode & IFMT) == IFDIR) { 1045 /* 1046 * Avoid ".", "..", and aliases of "." for obvious reasons. 1047 */ 1048 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || 1049 dp == ip || 1050 (fcnp->cn_flags & ISDOTDOT) || 1051 (tcnp->cn_flags & ISDOTDOT) || 1052 (ip->i_flag & IN_RENAME)) { 1053 VOP_UNLOCK(fvp); 1054 error = EINVAL; 1055 goto abortit; 1056 } 1057 ip->i_flag |= IN_RENAME; 1058 oldparent = dp->i_number; 1059 doingdirectory = 1; 1060 } 1061 VN_KNOTE(fdvp, NOTE_WRITE); /* XXXLUKEM/XXX: right place? */ 1062 1063 /* 1064 * When the target exists, both the directory 1065 * and target vnodes are returned locked. 1066 */ 1067 dp = VTOI(tdvp); 1068 xp = NULL; 1069 if (tvp) 1070 xp = VTOI(tvp); 1071 1072 mp = fdvp->v_mount; 1073 fstrans_start(mp, FSTRANS_SHARED); 1074 1075 /* 1076 * 1) Bump link count while we're moving stuff 1077 * around. If we crash somewhere before 1078 * completing our work, the link count 1079 * may be wrong, but correctable. 1080 */ 1081 ip->i_nlink++; 1082 DIP_ASSIGN(ip, nlink, ip->i_nlink); 1083 ip->i_flag |= IN_CHANGE; 1084 if ((error = UFS_UPDATE(fvp, NULL, NULL, UPDATE_DIROP)) != 0) { 1085 VOP_UNLOCK(fvp); 1086 goto bad; 1087 } 1088 1089 /* 1090 * If ".." must be changed (ie the directory gets a new 1091 * parent) then the source directory must not be in the 1092 * directory hierarchy above the target, as this would 1093 * orphan everything below the source directory. Also 1094 * the user must have write permission in the source so 1095 * as to be able to change "..". We must repeat the call 1096 * to namei, as the parent directory is unlocked by the 1097 * call to checkpath(). 1098 */ 1099 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred); 1100 VOP_UNLOCK(fvp); 1101 if (oldparent != dp->i_number) 1102 newparent = dp->i_number; 1103 if (doingdirectory && newparent) { 1104 if (error) /* write access check above */ 1105 goto bad; 1106 if (xp != NULL) 1107 vput(tvp); 1108 vref(tdvp); /* compensate for the ref checkpath loses */ 1109 if ((error = ufs_checkpath(ip, dp, tcnp->cn_cred)) != 0) { 1110 vrele(tdvp); 1111 goto out; 1112 } 1113 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY); 1114 error = relookup(tdvp, &tvp, tcnp, 0); 1115 if (error != 0) { 1116 vput(tdvp); 1117 goto out; 1118 } 1119 dp = VTOI(tdvp); 1120 xp = NULL; 1121 if (tvp) 1122 xp = VTOI(tvp); 1123 } 1124 /* 1125 * 2) If target doesn't exist, link the target 1126 * to the source and unlink the source. 1127 * Otherwise, rewrite the target directory 1128 * entry to reference the source inode and 1129 * expunge the original entry's existence. 1130 */ 1131 if (xp == NULL) { 1132 if (dp->i_dev != ip->i_dev) 1133 panic("rename: EXDEV"); 1134 /* 1135 * Account for ".." in new directory. 1136 * When source and destination have the same 1137 * parent we don't fool with the link count. 1138 */ 1139 if (doingdirectory && newparent) { 1140 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 1141 error = EMLINK; 1142 goto bad; 1143 } 1144 dp->i_nlink++; 1145 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1146 dp->i_flag |= IN_CHANGE; 1147 if ((error = UFS_UPDATE(tdvp, NULL, NULL, 1148 UPDATE_DIROP)) != 0) { 1149 dp->i_nlink--; 1150 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1151 dp->i_flag |= IN_CHANGE; 1152 goto bad; 1153 } 1154 } 1155 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 1156 ufs_makedirentry(ip, tcnp, newdir); 1157 error = ufs_direnter(tdvp, NULL, newdir, tcnp, NULL); 1158 pool_cache_put(ufs_direct_cache, newdir); 1159 if (error != 0) { 1160 if (doingdirectory && newparent) { 1161 dp->i_nlink--; 1162 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1163 dp->i_flag |= IN_CHANGE; 1164 (void)UFS_UPDATE(tdvp, NULL, NULL, 1165 UPDATE_WAIT|UPDATE_DIROP); 1166 } 1167 goto bad; 1168 } 1169 VN_KNOTE(tdvp, NOTE_WRITE); 1170 vput(tdvp); 1171 } else { 1172 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 1173 panic("rename: EXDEV"); 1174 /* 1175 * Short circuit rename(foo, foo). 1176 */ 1177 if (xp->i_number == ip->i_number) 1178 panic("rename: same file"); 1179 /* 1180 * If the parent directory is "sticky", then the user must 1181 * own the parent directory, or the destination of the rename, 1182 * otherwise the destination may not be changed (except by 1183 * root). This implements append-only directories. 1184 */ 1185 if ((dp->i_mode & S_ISTXT) && 1186 kauth_authorize_generic(tcnp->cn_cred, 1187 KAUTH_GENERIC_ISSUSER, NULL) != 0 && 1188 kauth_cred_geteuid(tcnp->cn_cred) != dp->i_uid && 1189 xp->i_uid != kauth_cred_geteuid(tcnp->cn_cred)) { 1190 error = EPERM; 1191 goto bad; 1192 } 1193 /* 1194 * Target must be empty if a directory and have no links 1195 * to it. Also, ensure source and target are compatible 1196 * (both directories, or both not directories). 1197 */ 1198 if ((xp->i_mode & IFMT) == IFDIR) { 1199 if (xp->i_nlink > 2 || 1200 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) { 1201 error = ENOTEMPTY; 1202 goto bad; 1203 } 1204 if (!doingdirectory) { 1205 error = ENOTDIR; 1206 goto bad; 1207 } 1208 cache_purge(tdvp); 1209 } else if (doingdirectory) { 1210 error = EISDIR; 1211 goto bad; 1212 } 1213 if ((error = ufs_dirrewrite(dp, xp, ip->i_number, 1214 IFTODT(ip->i_mode), doingdirectory && newparent ? 1215 newparent : doingdirectory, IN_CHANGE | IN_UPDATE)) != 0) 1216 goto bad; 1217 if (doingdirectory) { 1218 /* 1219 * Truncate inode. The only stuff left in the directory 1220 * is "." and "..". The "." reference is inconsequential 1221 * since we are quashing it. We have removed the "." 1222 * reference and the reference in the parent directory, 1223 * but there may be other hard links. 1224 */ 1225 if (!newparent) { 1226 dp->i_nlink--; 1227 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1228 dp->i_flag |= IN_CHANGE; 1229 } 1230 xp->i_nlink--; 1231 DIP_ASSIGN(xp, nlink, xp->i_nlink); 1232 xp->i_flag |= IN_CHANGE; 1233 if ((error = UFS_TRUNCATE(tvp, (off_t)0, IO_SYNC, 1234 tcnp->cn_cred))) 1235 goto bad; 1236 } 1237 VN_KNOTE(tdvp, NOTE_WRITE); 1238 vput(tdvp); 1239 VN_KNOTE(tvp, NOTE_DELETE); 1240 vput(tvp); 1241 xp = NULL; 1242 } 1243 1244 /* 1245 * 3) Unlink the source. 1246 */ 1247 fcnp->cn_flags &= ~(MODMASK); 1248 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 1249 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 1250 if ((error = relookup(fdvp, &fvp, fcnp, 0))) { 1251 vput(fdvp); 1252 vrele(ap->a_fvp); 1253 goto out2; 1254 } 1255 if (fvp != NULL) { 1256 xp = VTOI(fvp); 1257 dp = VTOI(fdvp); 1258 } else { 1259 /* 1260 * From name has disappeared. 1261 */ 1262 if (doingdirectory) 1263 panic("rename: lost dir entry"); 1264 vrele(ap->a_fvp); 1265 error = 0; 1266 goto out2; 1267 } 1268 /* 1269 * Ensure that the directory entry still exists and has not 1270 * changed while the new name has been entered. If the source is 1271 * a file then the entry may have been unlinked or renamed. In 1272 * either case there is no further work to be done. If the source 1273 * is a directory then it cannot have been rmdir'ed; The IRENAME 1274 * flag ensures that it cannot be moved by another rename or removed 1275 * by a rmdir. 1276 */ 1277 if (xp != ip) { 1278 if (doingdirectory) 1279 panic("rename: lost dir entry"); 1280 } else { 1281 /* 1282 * If the source is a directory with a 1283 * new parent, the link count of the old 1284 * parent directory must be decremented 1285 * and ".." set to point to the new parent. 1286 */ 1287 if (doingdirectory && newparent) { 1288 KASSERT(dp != NULL); 1289 xp->i_offset = mastertemplate.dot_reclen; 1290 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0, IN_CHANGE); 1291 cache_purge(fdvp); 1292 } 1293 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0); 1294 xp->i_flag &= ~IN_RENAME; 1295 } 1296 VN_KNOTE(fvp, NOTE_RENAME); 1297 if (dp) 1298 vput(fdvp); 1299 if (xp) 1300 vput(fvp); 1301 vrele(ap->a_fvp); 1302 goto out2; 1303 1304 /* exit routines from steps 1 & 2 */ 1305 bad: 1306 if (xp) 1307 vput(ITOV(xp)); 1308 vput(ITOV(dp)); 1309 out: 1310 if (doingdirectory) 1311 ip->i_flag &= ~IN_RENAME; 1312 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) { 1313 ip->i_nlink--; 1314 DIP_ASSIGN(ip, nlink, ip->i_nlink); 1315 ip->i_flag |= IN_CHANGE; 1316 ip->i_flag &= ~IN_RENAME; 1317 vput(fvp); 1318 } else 1319 vrele(fvp); 1320 vrele(fdvp); 1321 1322 /* exit routines from step 3 */ 1323 out2: 1324 fstrans_done(mp); 1325 return (error); 1326 } 1327 1328 int 1329 ufs_mkdir(void *v) 1330 { 1331 struct vop_mkdir_args /* { 1332 struct vnode *a_dvp; 1333 struct vnode **a_vpp; 1334 struct componentname *a_cnp; 1335 struct vattr *a_vap; 1336 } */ *ap = v; 1337 struct vnode *dvp = ap->a_dvp, *tvp; 1338 struct vattr *vap = ap->a_vap; 1339 struct componentname *cnp = ap->a_cnp; 1340 struct inode *ip, *dp = VTOI(dvp); 1341 struct buf *bp; 1342 struct dirtemplate dirtemplate; 1343 struct direct *newdir; 1344 int error, dmode; 1345 struct ufsmount *ump = dp->i_ump; 1346 int dirblksiz = ump->um_dirblksiz; 1347 1348 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 1349 1350 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 1351 error = EMLINK; 1352 goto out; 1353 } 1354 dmode = vap->va_mode & ACCESSPERMS; 1355 dmode |= IFDIR; 1356 /* 1357 * Must simulate part of ufs_makeinode here to acquire the inode, 1358 * but not have it entered in the parent directory. The entry is 1359 * made later after writing "." and ".." entries. 1360 */ 1361 if ((error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, ap->a_vpp)) != 0) 1362 goto out; 1363 error = UFS_WAPBL_BEGIN(ap->a_dvp->v_mount); 1364 if (error) 1365 goto out; 1366 tvp = *ap->a_vpp; 1367 ip = VTOI(tvp); 1368 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred); 1369 DIP_ASSIGN(ip, uid, ip->i_uid); 1370 ip->i_gid = dp->i_gid; 1371 DIP_ASSIGN(ip, gid, ip->i_gid); 1372 #if defined(QUOTA) || defined(QUOTA2) 1373 if ((error = chkiq(ip, 1, cnp->cn_cred, 0))) { 1374 UFS_VFREE(tvp, ip->i_number, dmode); 1375 UFS_WAPBL_END(dvp->v_mount); 1376 fstrans_done(dvp->v_mount); 1377 vput(tvp); 1378 vput(dvp); 1379 return (error); 1380 } 1381 #endif 1382 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1383 ip->i_mode = dmode; 1384 DIP_ASSIGN(ip, mode, dmode); 1385 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ 1386 ip->i_nlink = 2; 1387 DIP_ASSIGN(ip, nlink, 2); 1388 if (cnp->cn_flags & ISWHITEOUT) { 1389 ip->i_flags |= UF_OPAQUE; 1390 DIP_ASSIGN(ip, flags, ip->i_flags); 1391 } 1392 1393 /* 1394 * Bump link count in parent directory to reflect work done below. 1395 * Should be done before reference is created so cleanup is 1396 * possible if we crash. 1397 */ 1398 dp->i_nlink++; 1399 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1400 dp->i_flag |= IN_CHANGE; 1401 if ((error = UFS_UPDATE(dvp, NULL, NULL, UPDATE_DIROP)) != 0) 1402 goto bad; 1403 1404 /* 1405 * Initialize directory with "." and ".." from static template. 1406 */ 1407 dirtemplate = mastertemplate; 1408 dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen; 1409 dirtemplate.dot_ino = ufs_rw32(ip->i_number, UFS_MPNEEDSWAP(ump)); 1410 dirtemplate.dotdot_ino = ufs_rw32(dp->i_number, UFS_MPNEEDSWAP(ump)); 1411 dirtemplate.dot_reclen = ufs_rw16(dirtemplate.dot_reclen, 1412 UFS_MPNEEDSWAP(ump)); 1413 dirtemplate.dotdot_reclen = ufs_rw16(dirtemplate.dotdot_reclen, 1414 UFS_MPNEEDSWAP(ump)); 1415 if (ump->um_maxsymlinklen <= 0) { 1416 #if BYTE_ORDER == LITTLE_ENDIAN 1417 if (UFS_MPNEEDSWAP(ump) == 0) 1418 #else 1419 if (UFS_MPNEEDSWAP(ump) != 0) 1420 #endif 1421 { 1422 dirtemplate.dot_type = dirtemplate.dot_namlen; 1423 dirtemplate.dotdot_type = dirtemplate.dotdot_namlen; 1424 dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0; 1425 } else 1426 dirtemplate.dot_type = dirtemplate.dotdot_type = 0; 1427 } 1428 if ((error = UFS_BALLOC(tvp, (off_t)0, dirblksiz, cnp->cn_cred, 1429 B_CLRBUF, &bp)) != 0) 1430 goto bad; 1431 ip->i_size = dirblksiz; 1432 DIP_ASSIGN(ip, size, dirblksiz); 1433 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1434 uvm_vnp_setsize(tvp, ip->i_size); 1435 memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate); 1436 1437 /* 1438 * Directory set up, now install it's entry in the parent directory. 1439 * We must write out the buffer containing the new directory body 1440 * before entering the new name in the parent. 1441 */ 1442 if ((error = VOP_BWRITE(bp)) != 0) 1443 goto bad; 1444 if ((error = UFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0) { 1445 goto bad; 1446 } 1447 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 1448 ufs_makedirentry(ip, cnp, newdir); 1449 error = ufs_direnter(dvp, tvp, newdir, cnp, bp); 1450 pool_cache_put(ufs_direct_cache, newdir); 1451 bad: 1452 if (error == 0) { 1453 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); 1454 UFS_WAPBL_END(dvp->v_mount); 1455 } else { 1456 dp->i_nlink--; 1457 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1458 dp->i_flag |= IN_CHANGE; 1459 UFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP); 1460 /* 1461 * No need to do an explicit UFS_TRUNCATE here, vrele will 1462 * do this for us because we set the link count to 0. 1463 */ 1464 ip->i_nlink = 0; 1465 DIP_ASSIGN(ip, nlink, 0); 1466 ip->i_flag |= IN_CHANGE; 1467 /* If IN_ADIROP, account for it */ 1468 UFS_UNMARK_VNODE(tvp); 1469 UFS_WAPBL_UPDATE(tvp, NULL, NULL, UPDATE_DIROP); 1470 UFS_WAPBL_END(dvp->v_mount); 1471 vput(tvp); 1472 } 1473 out: 1474 fstrans_done(dvp->v_mount); 1475 vput(dvp); 1476 return (error); 1477 } 1478 1479 int 1480 ufs_rmdir(void *v) 1481 { 1482 struct vop_rmdir_args /* { 1483 struct vnode *a_dvp; 1484 struct vnode *a_vp; 1485 struct componentname *a_cnp; 1486 } */ *ap = v; 1487 struct vnode *vp, *dvp; 1488 struct componentname *cnp; 1489 struct inode *ip, *dp; 1490 int error; 1491 1492 vp = ap->a_vp; 1493 dvp = ap->a_dvp; 1494 cnp = ap->a_cnp; 1495 ip = VTOI(vp); 1496 dp = VTOI(dvp); 1497 /* 1498 * No rmdir "." or of mounted directories please. 1499 */ 1500 if (dp == ip || vp->v_mountedhere != NULL) { 1501 if (dp == ip) 1502 vrele(vp); 1503 else 1504 vput(vp); 1505 vput(vp); 1506 return (EINVAL); 1507 } 1508 1509 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 1510 1511 /* 1512 * Do not remove a directory that is in the process of being renamed. 1513 * Verify that the directory is empty (and valid). (Rmdir ".." won't 1514 * be valid since ".." will contain a reference to the current 1515 * directory and thus be non-empty.) 1516 */ 1517 error = 0; 1518 if (ip->i_flag & IN_RENAME) { 1519 error = EINVAL; 1520 goto out; 1521 } 1522 if (ip->i_nlink != 2 || 1523 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { 1524 error = ENOTEMPTY; 1525 goto out; 1526 } 1527 if ((dp->i_flags & APPEND) || 1528 (ip->i_flags & (IMMUTABLE | APPEND))) { 1529 error = EPERM; 1530 goto out; 1531 } 1532 error = UFS_WAPBL_BEGIN(dvp->v_mount); 1533 if (error) 1534 goto out; 1535 /* 1536 * Delete reference to directory before purging 1537 * inode. If we crash in between, the directory 1538 * will be reattached to lost+found, 1539 */ 1540 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1); 1541 if (error) { 1542 UFS_WAPBL_END(dvp->v_mount); 1543 goto out; 1544 } 1545 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); 1546 cache_purge(dvp); 1547 /* 1548 * Truncate inode. The only stuff left in the directory is "." and 1549 * "..". The "." reference is inconsequential since we're quashing 1550 * it. 1551 */ 1552 dp->i_nlink--; 1553 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1554 dp->i_flag |= IN_CHANGE; 1555 UFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP); 1556 ip->i_nlink--; 1557 DIP_ASSIGN(ip, nlink, ip->i_nlink); 1558 ip->i_flag |= IN_CHANGE; 1559 error = UFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred); 1560 cache_purge(vp); 1561 /* 1562 * Unlock the log while we still have reference to unlinked 1563 * directory vp so that it will not get locked for recycling 1564 */ 1565 UFS_WAPBL_END(dvp->v_mount); 1566 #ifdef UFS_DIRHASH 1567 if (ip->i_dirhash != NULL) 1568 ufsdirhash_free(ip); 1569 #endif 1570 out: 1571 VN_KNOTE(vp, NOTE_DELETE); 1572 vput(dvp); 1573 vput(vp); 1574 fstrans_done(dvp->v_mount); 1575 return (error); 1576 } 1577 1578 /* 1579 * symlink -- make a symbolic link 1580 */ 1581 int 1582 ufs_symlink(void *v) 1583 { 1584 struct vop_symlink_args /* { 1585 struct vnode *a_dvp; 1586 struct vnode **a_vpp; 1587 struct componentname *a_cnp; 1588 struct vattr *a_vap; 1589 char *a_target; 1590 } */ *ap = v; 1591 struct vnode *vp, **vpp; 1592 struct inode *ip; 1593 int len, error; 1594 1595 vpp = ap->a_vpp; 1596 /* 1597 * UFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful 1598 * ufs_makeinode 1599 */ 1600 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED); 1601 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, 1602 vpp, ap->a_cnp); 1603 if (error) 1604 goto out; 1605 VN_KNOTE(ap->a_dvp, NOTE_WRITE); 1606 vp = *vpp; 1607 len = strlen(ap->a_target); 1608 ip = VTOI(vp); 1609 if (len < ip->i_ump->um_maxsymlinklen) { 1610 memcpy((char *)SHORTLINK(ip), ap->a_target, len); 1611 ip->i_size = len; 1612 DIP_ASSIGN(ip, size, len); 1613 uvm_vnp_setsize(vp, ip->i_size); 1614 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1615 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 1616 } else 1617 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, 1618 UIO_SYSSPACE, IO_NODELOCKED | IO_JOURNALLOCKED, 1619 ap->a_cnp->cn_cred, NULL, NULL); 1620 UFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp); 1621 if (error) 1622 vput(vp); 1623 out: 1624 fstrans_done(ap->a_dvp->v_mount); 1625 return (error); 1626 } 1627 1628 /* 1629 * Vnode op for reading directories. 1630 * 1631 * This routine handles converting from the on-disk directory format 1632 * "struct direct" to the in-memory format "struct dirent" as well as 1633 * byte swapping the entries if necessary. 1634 */ 1635 int 1636 ufs_readdir(void *v) 1637 { 1638 struct vop_readdir_args /* { 1639 struct vnode *a_vp; 1640 struct uio *a_uio; 1641 kauth_cred_t a_cred; 1642 int *a_eofflag; 1643 off_t **a_cookies; 1644 int *ncookies; 1645 } */ *ap = v; 1646 struct vnode *vp = ap->a_vp; 1647 struct direct *cdp, *ecdp; 1648 struct dirent *ndp; 1649 char *cdbuf, *ndbuf, *endp; 1650 struct uio auio, *uio; 1651 struct iovec aiov; 1652 int error; 1653 size_t count, ccount, rcount; 1654 off_t off, *ccp; 1655 off_t startoff; 1656 size_t skipbytes; 1657 struct ufsmount *ump = VFSTOUFS(vp->v_mount); 1658 int nswap = UFS_MPNEEDSWAP(ump); 1659 #if BYTE_ORDER == LITTLE_ENDIAN 1660 int needswap = ump->um_maxsymlinklen <= 0 && nswap == 0; 1661 #else 1662 int needswap = ump->um_maxsymlinklen <= 0 && nswap != 0; 1663 #endif 1664 uio = ap->a_uio; 1665 count = uio->uio_resid; 1666 rcount = count - ((uio->uio_offset + count) & (ump->um_dirblksiz - 1)); 1667 1668 if (rcount < _DIRENT_MINSIZE(cdp) || count < _DIRENT_MINSIZE(ndp)) 1669 return EINVAL; 1670 1671 startoff = uio->uio_offset & ~(ump->um_dirblksiz - 1); 1672 skipbytes = uio->uio_offset - startoff; 1673 rcount += skipbytes; 1674 1675 auio.uio_iov = &aiov; 1676 auio.uio_iovcnt = 1; 1677 auio.uio_offset = startoff; 1678 auio.uio_resid = rcount; 1679 UIO_SETUP_SYSSPACE(&auio); 1680 auio.uio_rw = UIO_READ; 1681 cdbuf = malloc(rcount, M_TEMP, M_WAITOK); 1682 aiov.iov_base = cdbuf; 1683 aiov.iov_len = rcount; 1684 error = VOP_READ(vp, &auio, 0, ap->a_cred); 1685 if (error != 0) { 1686 free(cdbuf, M_TEMP); 1687 return error; 1688 } 1689 1690 rcount -= auio.uio_resid; 1691 1692 cdp = (struct direct *)(void *)cdbuf; 1693 ecdp = (struct direct *)(void *)&cdbuf[rcount]; 1694 1695 ndbuf = malloc(count, M_TEMP, M_WAITOK); 1696 ndp = (struct dirent *)(void *)ndbuf; 1697 endp = &ndbuf[count]; 1698 1699 off = uio->uio_offset; 1700 if (ap->a_cookies) { 1701 ccount = rcount / _DIRENT_RECLEN(cdp, 1); 1702 ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp), 1703 M_TEMP, M_WAITOK); 1704 } else { 1705 /* XXX: GCC */ 1706 ccount = 0; 1707 ccp = NULL; 1708 } 1709 1710 while (cdp < ecdp) { 1711 cdp->d_reclen = ufs_rw16(cdp->d_reclen, nswap); 1712 if (skipbytes > 0) { 1713 if (cdp->d_reclen <= skipbytes) { 1714 skipbytes -= cdp->d_reclen; 1715 cdp = _DIRENT_NEXT(cdp); 1716 continue; 1717 } 1718 /* 1719 * invalid cookie. 1720 */ 1721 error = EINVAL; 1722 goto out; 1723 } 1724 if (cdp->d_reclen == 0) { 1725 struct dirent *ondp = ndp; 1726 ndp->d_reclen = _DIRENT_MINSIZE(ndp); 1727 ndp = _DIRENT_NEXT(ndp); 1728 ondp->d_reclen = 0; 1729 cdp = ecdp; 1730 break; 1731 } 1732 if (needswap) { 1733 ndp->d_type = cdp->d_namlen; 1734 ndp->d_namlen = cdp->d_type; 1735 } else { 1736 ndp->d_type = cdp->d_type; 1737 ndp->d_namlen = cdp->d_namlen; 1738 } 1739 ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen); 1740 if ((char *)(void *)ndp + ndp->d_reclen + 1741 _DIRENT_MINSIZE(ndp) > endp) 1742 break; 1743 ndp->d_fileno = ufs_rw32(cdp->d_ino, nswap); 1744 (void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen); 1745 memset(&ndp->d_name[ndp->d_namlen], 0, 1746 ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen); 1747 off += cdp->d_reclen; 1748 if (ap->a_cookies) { 1749 KASSERT(ccp - *(ap->a_cookies) < ccount); 1750 *(ccp++) = off; 1751 } 1752 ndp = _DIRENT_NEXT(ndp); 1753 cdp = _DIRENT_NEXT(cdp); 1754 } 1755 1756 count = ((char *)(void *)ndp - ndbuf); 1757 error = uiomove(ndbuf, count, uio); 1758 out: 1759 if (ap->a_cookies) { 1760 if (error) { 1761 free(*(ap->a_cookies), M_TEMP); 1762 *(ap->a_cookies) = NULL; 1763 *(ap->a_ncookies) = 0; 1764 } else { 1765 *ap->a_ncookies = ccp - *(ap->a_cookies); 1766 } 1767 } 1768 uio->uio_offset = off; 1769 free(ndbuf, M_TEMP); 1770 free(cdbuf, M_TEMP); 1771 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset; 1772 return error; 1773 } 1774 1775 /* 1776 * Return target name of a symbolic link 1777 */ 1778 int 1779 ufs_readlink(void *v) 1780 { 1781 struct vop_readlink_args /* { 1782 struct vnode *a_vp; 1783 struct uio *a_uio; 1784 kauth_cred_t a_cred; 1785 } */ *ap = v; 1786 struct vnode *vp = ap->a_vp; 1787 struct inode *ip = VTOI(vp); 1788 struct ufsmount *ump = VFSTOUFS(vp->v_mount); 1789 int isize; 1790 1791 isize = ip->i_size; 1792 if (isize < ump->um_maxsymlinklen || 1793 (ump->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) { 1794 uiomove((char *)SHORTLINK(ip), isize, ap->a_uio); 1795 return (0); 1796 } 1797 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); 1798 } 1799 1800 /* 1801 * Calculate the logical to physical mapping if not done already, 1802 * then call the device strategy routine. 1803 */ 1804 int 1805 ufs_strategy(void *v) 1806 { 1807 struct vop_strategy_args /* { 1808 struct vnode *a_vp; 1809 struct buf *a_bp; 1810 } */ *ap = v; 1811 struct buf *bp; 1812 struct vnode *vp; 1813 struct inode *ip; 1814 struct mount *mp; 1815 int error; 1816 1817 bp = ap->a_bp; 1818 vp = ap->a_vp; 1819 ip = VTOI(vp); 1820 if (vp->v_type == VBLK || vp->v_type == VCHR) 1821 panic("ufs_strategy: spec"); 1822 KASSERT(bp->b_bcount != 0); 1823 if (bp->b_blkno == bp->b_lblkno) { 1824 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, 1825 NULL); 1826 if (error) { 1827 bp->b_error = error; 1828 biodone(bp); 1829 return (error); 1830 } 1831 if (bp->b_blkno == -1) /* no valid data */ 1832 clrbuf(bp); 1833 } 1834 if (bp->b_blkno < 0) { /* block is not on disk */ 1835 biodone(bp); 1836 return (0); 1837 } 1838 vp = ip->i_devvp; 1839 1840 error = VOP_STRATEGY(vp, bp); 1841 if (error) 1842 return error; 1843 1844 if (!BUF_ISREAD(bp)) 1845 return 0; 1846 1847 mp = wapbl_vptomp(vp); 1848 if (mp == NULL || mp->mnt_wapbl_replay == NULL || 1849 !WAPBL_REPLAY_ISOPEN(mp) || 1850 !WAPBL_REPLAY_CAN_READ(mp, bp->b_blkno, bp->b_bcount)) 1851 return 0; 1852 1853 error = biowait(bp); 1854 if (error) 1855 return error; 1856 1857 error = WAPBL_REPLAY_READ(mp, bp->b_data, bp->b_blkno, bp->b_bcount); 1858 if (error) { 1859 mutex_enter(&bufcache_lock); 1860 SET(bp->b_cflags, BC_INVAL); 1861 mutex_exit(&bufcache_lock); 1862 } 1863 return error; 1864 } 1865 1866 /* 1867 * Print out the contents of an inode. 1868 */ 1869 int 1870 ufs_print(void *v) 1871 { 1872 struct vop_print_args /* { 1873 struct vnode *a_vp; 1874 } */ *ap = v; 1875 struct vnode *vp; 1876 struct inode *ip; 1877 1878 vp = ap->a_vp; 1879 ip = VTOI(vp); 1880 printf("tag VT_UFS, ino %llu, on dev %llu, %llu", 1881 (unsigned long long)ip->i_number, 1882 (unsigned long long)major(ip->i_dev), 1883 (unsigned long long)minor(ip->i_dev)); 1884 printf(" flags 0x%x, nlink %d\n", 1885 ip->i_flag, ip->i_nlink); 1886 printf("\tmode 0%o, owner %d, group %d, size %qd", 1887 ip->i_mode, ip->i_uid, ip->i_gid, 1888 (long long)ip->i_size); 1889 if (vp->v_type == VFIFO) 1890 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v); 1891 printf("\n"); 1892 return (0); 1893 } 1894 1895 /* 1896 * Read wrapper for special devices. 1897 */ 1898 int 1899 ufsspec_read(void *v) 1900 { 1901 struct vop_read_args /* { 1902 struct vnode *a_vp; 1903 struct uio *a_uio; 1904 int a_ioflag; 1905 kauth_cred_t a_cred; 1906 } */ *ap = v; 1907 1908 /* 1909 * Set access flag. 1910 */ 1911 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0) 1912 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1913 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap)); 1914 } 1915 1916 /* 1917 * Write wrapper for special devices. 1918 */ 1919 int 1920 ufsspec_write(void *v) 1921 { 1922 struct vop_write_args /* { 1923 struct vnode *a_vp; 1924 struct uio *a_uio; 1925 int a_ioflag; 1926 kauth_cred_t a_cred; 1927 } */ *ap = v; 1928 1929 /* 1930 * Set update and change flags. 1931 */ 1932 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0) 1933 VTOI(ap->a_vp)->i_flag |= IN_MODIFY; 1934 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap)); 1935 } 1936 1937 /* 1938 * Close wrapper for special devices. 1939 * 1940 * Update the times on the inode then do device close. 1941 */ 1942 int 1943 ufsspec_close(void *v) 1944 { 1945 struct vop_close_args /* { 1946 struct vnode *a_vp; 1947 int a_fflag; 1948 kauth_cred_t a_cred; 1949 } */ *ap = v; 1950 struct vnode *vp; 1951 struct inode *ip; 1952 1953 vp = ap->a_vp; 1954 ip = VTOI(vp); 1955 if (vp->v_usecount > 1) 1956 UFS_ITIMES(vp, NULL, NULL, NULL); 1957 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap)); 1958 } 1959 1960 /* 1961 * Read wrapper for fifo's 1962 */ 1963 int 1964 ufsfifo_read(void *v) 1965 { 1966 struct vop_read_args /* { 1967 struct vnode *a_vp; 1968 struct uio *a_uio; 1969 int a_ioflag; 1970 kauth_cred_t a_cred; 1971 } */ *ap = v; 1972 1973 /* 1974 * Set access flag. 1975 */ 1976 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1977 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap)); 1978 } 1979 1980 /* 1981 * Write wrapper for fifo's. 1982 */ 1983 int 1984 ufsfifo_write(void *v) 1985 { 1986 struct vop_write_args /* { 1987 struct vnode *a_vp; 1988 struct uio *a_uio; 1989 int a_ioflag; 1990 kauth_cred_t a_cred; 1991 } */ *ap = v; 1992 1993 /* 1994 * Set update and change flags. 1995 */ 1996 VTOI(ap->a_vp)->i_flag |= IN_MODIFY; 1997 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap)); 1998 } 1999 2000 /* 2001 * Close wrapper for fifo's. 2002 * 2003 * Update the times on the inode then do device close. 2004 */ 2005 int 2006 ufsfifo_close(void *v) 2007 { 2008 struct vop_close_args /* { 2009 struct vnode *a_vp; 2010 int a_fflag; 2011 kauth_cred_t a_cred; 2012 } */ *ap = v; 2013 struct vnode *vp; 2014 struct inode *ip; 2015 2016 vp = ap->a_vp; 2017 ip = VTOI(vp); 2018 if (ap->a_vp->v_usecount > 1) 2019 UFS_ITIMES(vp, NULL, NULL, NULL); 2020 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap)); 2021 } 2022 2023 /* 2024 * Return POSIX pathconf information applicable to ufs filesystems. 2025 */ 2026 int 2027 ufs_pathconf(void *v) 2028 { 2029 struct vop_pathconf_args /* { 2030 struct vnode *a_vp; 2031 int a_name; 2032 register_t *a_retval; 2033 } */ *ap = v; 2034 2035 switch (ap->a_name) { 2036 case _PC_LINK_MAX: 2037 *ap->a_retval = LINK_MAX; 2038 return (0); 2039 case _PC_NAME_MAX: 2040 *ap->a_retval = NAME_MAX; 2041 return (0); 2042 case _PC_PATH_MAX: 2043 *ap->a_retval = PATH_MAX; 2044 return (0); 2045 case _PC_PIPE_BUF: 2046 *ap->a_retval = PIPE_BUF; 2047 return (0); 2048 case _PC_CHOWN_RESTRICTED: 2049 *ap->a_retval = 1; 2050 return (0); 2051 case _PC_NO_TRUNC: 2052 *ap->a_retval = 1; 2053 return (0); 2054 case _PC_SYNC_IO: 2055 *ap->a_retval = 1; 2056 return (0); 2057 case _PC_FILESIZEBITS: 2058 *ap->a_retval = 42; 2059 return (0); 2060 case _PC_SYMLINK_MAX: 2061 *ap->a_retval = MAXPATHLEN; 2062 return (0); 2063 case _PC_2_SYMLINKS: 2064 *ap->a_retval = 1; 2065 return (0); 2066 default: 2067 return (EINVAL); 2068 } 2069 /* NOTREACHED */ 2070 } 2071 2072 /* 2073 * Advisory record locking support 2074 */ 2075 int 2076 ufs_advlock(void *v) 2077 { 2078 struct vop_advlock_args /* { 2079 struct vnode *a_vp; 2080 void * a_id; 2081 int a_op; 2082 struct flock *a_fl; 2083 int a_flags; 2084 } */ *ap = v; 2085 struct inode *ip; 2086 2087 ip = VTOI(ap->a_vp); 2088 return lf_advlock(ap, &ip->i_lockf, ip->i_size); 2089 } 2090 2091 /* 2092 * Initialize the vnode associated with a new inode, handle aliased 2093 * vnodes. 2094 */ 2095 void 2096 ufs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *), 2097 struct vnode **vpp) 2098 { 2099 struct timeval tv; 2100 struct inode *ip; 2101 struct vnode *vp; 2102 dev_t rdev; 2103 struct ufsmount *ump; 2104 2105 vp = *vpp; 2106 ip = VTOI(vp); 2107 switch(vp->v_type = IFTOVT(ip->i_mode)) { 2108 case VCHR: 2109 case VBLK: 2110 vp->v_op = specops; 2111 ump = ip->i_ump; 2112 if (ump->um_fstype == UFS1) 2113 rdev = (dev_t)ufs_rw32(ip->i_ffs1_rdev, 2114 UFS_MPNEEDSWAP(ump)); 2115 else 2116 rdev = (dev_t)ufs_rw64(ip->i_ffs2_rdev, 2117 UFS_MPNEEDSWAP(ump)); 2118 spec_node_init(vp, rdev); 2119 break; 2120 case VFIFO: 2121 vp->v_op = fifoops; 2122 break; 2123 case VNON: 2124 case VBAD: 2125 case VSOCK: 2126 case VLNK: 2127 case VDIR: 2128 case VREG: 2129 break; 2130 } 2131 if (ip->i_number == ROOTINO) 2132 vp->v_vflag |= VV_ROOT; 2133 /* 2134 * Initialize modrev times 2135 */ 2136 getmicrouptime(&tv); 2137 ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32 2138 | tv.tv_usec * 4294u; 2139 *vpp = vp; 2140 } 2141 2142 /* 2143 * Allocate a new inode. 2144 */ 2145 int 2146 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp, 2147 struct componentname *cnp) 2148 { 2149 struct inode *ip, *pdir; 2150 struct direct *newdir; 2151 struct vnode *tvp; 2152 int error, ismember = 0; 2153 2154 UFS_WAPBL_JUNLOCK_ASSERT(dvp->v_mount); 2155 2156 pdir = VTOI(dvp); 2157 2158 if ((mode & IFMT) == 0) 2159 mode |= IFREG; 2160 2161 if ((error = UFS_VALLOC(dvp, mode, cnp->cn_cred, vpp)) != 0) { 2162 vput(dvp); 2163 return (error); 2164 } 2165 tvp = *vpp; 2166 ip = VTOI(tvp); 2167 ip->i_gid = pdir->i_gid; 2168 DIP_ASSIGN(ip, gid, ip->i_gid); 2169 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred); 2170 DIP_ASSIGN(ip, uid, ip->i_uid); 2171 error = UFS_WAPBL_BEGIN1(dvp->v_mount, dvp); 2172 if (error) { 2173 /* 2174 * Note, we can't VOP_VFREE(tvp) here like we should 2175 * because we can't write to the disk. Instead, we leave 2176 * the vnode dangling from the journal. 2177 */ 2178 vput(tvp); 2179 vput(dvp); 2180 return (error); 2181 } 2182 #if defined(QUOTA) || defined(QUOTA2) 2183 if ((error = chkiq(ip, 1, cnp->cn_cred, 0))) { 2184 UFS_VFREE(tvp, ip->i_number, mode); 2185 UFS_WAPBL_END1(dvp->v_mount, dvp); 2186 vput(tvp); 2187 vput(dvp); 2188 return (error); 2189 } 2190 #endif 2191 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 2192 ip->i_mode = mode; 2193 DIP_ASSIGN(ip, mode, mode); 2194 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ 2195 ip->i_nlink = 1; 2196 DIP_ASSIGN(ip, nlink, 1); 2197 if ((ip->i_mode & ISGID) && (kauth_cred_ismember_gid(cnp->cn_cred, 2198 ip->i_gid, &ismember) != 0 || !ismember) && 2199 kauth_authorize_generic(cnp->cn_cred, KAUTH_GENERIC_ISSUSER, NULL)) { 2200 ip->i_mode &= ~ISGID; 2201 DIP_ASSIGN(ip, mode, ip->i_mode); 2202 } 2203 2204 if (cnp->cn_flags & ISWHITEOUT) { 2205 ip->i_flags |= UF_OPAQUE; 2206 DIP_ASSIGN(ip, flags, ip->i_flags); 2207 } 2208 2209 /* 2210 * Make sure inode goes to disk before directory entry. 2211 */ 2212 if ((error = UFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0) 2213 goto bad; 2214 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 2215 ufs_makedirentry(ip, cnp, newdir); 2216 error = ufs_direnter(dvp, tvp, newdir, cnp, NULL); 2217 pool_cache_put(ufs_direct_cache, newdir); 2218 if (error) 2219 goto bad; 2220 vput(dvp); 2221 *vpp = tvp; 2222 return (0); 2223 2224 bad: 2225 /* 2226 * Write error occurred trying to update the inode 2227 * or the directory so must deallocate the inode. 2228 */ 2229 ip->i_nlink = 0; 2230 DIP_ASSIGN(ip, nlink, 0); 2231 ip->i_flag |= IN_CHANGE; 2232 /* If IN_ADIROP, account for it */ 2233 UFS_UNMARK_VNODE(tvp); 2234 UFS_WAPBL_UPDATE(tvp, NULL, NULL, 0); 2235 tvp->v_type = VNON; /* explodes later if VBLK */ 2236 UFS_WAPBL_END1(dvp->v_mount, dvp); 2237 vput(tvp); 2238 vput(dvp); 2239 return (error); 2240 } 2241 2242 /* 2243 * Allocate len bytes at offset off. 2244 */ 2245 int 2246 ufs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags, 2247 kauth_cred_t cred) 2248 { 2249 struct inode *ip = VTOI(vp); 2250 int error, delta, bshift, bsize; 2251 UVMHIST_FUNC("ufs_gop_alloc"); UVMHIST_CALLED(ubchist); 2252 2253 error = 0; 2254 bshift = vp->v_mount->mnt_fs_bshift; 2255 bsize = 1 << bshift; 2256 2257 delta = off & (bsize - 1); 2258 off -= delta; 2259 len += delta; 2260 2261 while (len > 0) { 2262 bsize = MIN(bsize, len); 2263 2264 error = UFS_BALLOC(vp, off, bsize, cred, flags, NULL); 2265 if (error) { 2266 goto out; 2267 } 2268 2269 /* 2270 * increase file size now, UFS_BALLOC() requires that 2271 * EOF be up-to-date before each call. 2272 */ 2273 2274 if (ip->i_size < off + bsize) { 2275 UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x", 2276 vp, ip->i_size, off + bsize, 0); 2277 ip->i_size = off + bsize; 2278 DIP_ASSIGN(ip, size, ip->i_size); 2279 } 2280 2281 off += bsize; 2282 len -= bsize; 2283 } 2284 2285 out: 2286 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 2287 return error; 2288 } 2289 2290 void 2291 ufs_gop_markupdate(struct vnode *vp, int flags) 2292 { 2293 u_int32_t mask = 0; 2294 2295 if ((flags & GOP_UPDATE_ACCESSED) != 0) { 2296 mask = IN_ACCESS; 2297 } 2298 if ((flags & GOP_UPDATE_MODIFIED) != 0) { 2299 if (vp->v_type == VREG) { 2300 mask |= IN_CHANGE | IN_UPDATE; 2301 } else { 2302 mask |= IN_MODIFY; 2303 } 2304 } 2305 if (mask) { 2306 struct inode *ip = VTOI(vp); 2307 2308 ip->i_flag |= mask; 2309 } 2310 } 2311