1 /* $NetBSD: ufs_vnops.c,v 1.221 2014/05/25 13:46:16 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.221 2014/05/25 13:46:16 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/kmem.h> 88 #include <sys/malloc.h> 89 #include <sys/dirent.h> 90 #include <sys/lockf.h> 91 #include <sys/kauth.h> 92 #include <sys/wapbl.h> 93 #include <sys/fstrans.h> 94 95 #include <miscfs/specfs/specdev.h> 96 #include <miscfs/fifofs/fifo.h> 97 #include <miscfs/genfs/genfs.h> 98 99 #include <ufs/ufs/inode.h> 100 #include <ufs/ufs/dir.h> 101 #include <ufs/ufs/ufsmount.h> 102 #include <ufs/ufs/ufs_bswap.h> 103 #include <ufs/ufs/ufs_extern.h> 104 #include <ufs/ufs/ufs_wapbl.h> 105 #ifdef UFS_DIRHASH 106 #include <ufs/ufs/dirhash.h> 107 #endif 108 #include <ufs/ext2fs/ext2fs_extern.h> 109 #include <ufs/ext2fs/ext2fs_dir.h> 110 #include <ufs/ffs/ffs_extern.h> 111 #include <ufs/lfs/lfs_extern.h> 112 #include <ufs/lfs/lfs.h> 113 114 #include <uvm/uvm.h> 115 116 __CTASSERT(EXT2FS_MAXNAMLEN == FFS_MAXNAMLEN); 117 __CTASSERT(LFS_MAXNAMLEN == FFS_MAXNAMLEN); 118 119 static int ufs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *); 120 static int ufs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, 121 struct lwp *); 122 123 /* 124 * A virgin directory (no blushing please). 125 */ 126 static const struct dirtemplate mastertemplate = { 127 0, 12, DT_DIR, 1, ".", 128 0, UFS_DIRBLKSIZ - 12, DT_DIR, 2, ".." 129 }; 130 131 /* 132 * Create a regular file 133 */ 134 int 135 ufs_create(void *v) 136 { 137 struct vop_create_v3_args /* { 138 struct vnode *a_dvp; 139 struct vnode **a_vpp; 140 struct componentname *a_cnp; 141 struct vattr *a_vap; 142 } */ *ap = v; 143 int error; 144 struct vnode *dvp = ap->a_dvp; 145 struct ufs_lookup_results *ulr; 146 147 /* XXX should handle this material another way */ 148 ulr = &VTOI(dvp)->i_crap; 149 UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); 150 151 /* 152 * UFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful 153 * ufs_makeinode 154 */ 155 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 156 error = 157 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), 158 dvp, ulr, ap->a_vpp, ap->a_cnp); 159 if (error) { 160 fstrans_done(dvp->v_mount); 161 return (error); 162 } 163 UFS_WAPBL_END1(dvp->v_mount, dvp); 164 fstrans_done(dvp->v_mount); 165 VN_KNOTE(dvp, NOTE_WRITE); 166 VOP_UNLOCK(*ap->a_vpp); 167 return (0); 168 } 169 170 /* 171 * Mknod vnode call 172 */ 173 /* ARGSUSED */ 174 int 175 ufs_mknod(void *v) 176 { 177 struct vop_mknod_v3_args /* { 178 struct vnode *a_dvp; 179 struct vnode **a_vpp; 180 struct componentname *a_cnp; 181 struct vattr *a_vap; 182 } */ *ap = v; 183 struct vattr *vap; 184 struct vnode **vpp; 185 struct inode *ip; 186 int error; 187 struct mount *mp; 188 ino_t ino; 189 struct ufs_lookup_results *ulr; 190 191 vap = ap->a_vap; 192 vpp = ap->a_vpp; 193 194 /* XXX should handle this material another way */ 195 ulr = &VTOI(ap->a_dvp)->i_crap; 196 UFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp)); 197 198 /* 199 * UFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful 200 * ufs_makeinode 201 */ 202 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED); 203 if ((error = 204 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), 205 ap->a_dvp, ulr, vpp, ap->a_cnp)) != 0) 206 goto out; 207 VN_KNOTE(ap->a_dvp, NOTE_WRITE); 208 ip = VTOI(*vpp); 209 mp = (*vpp)->v_mount; 210 ino = ip->i_number; 211 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 212 if (vap->va_rdev != VNOVAL) { 213 struct ufsmount *ump = ip->i_ump; 214 /* 215 * Want to be able to use this to make badblock 216 * inodes, so don't truncate the dev number. 217 */ 218 if (ump->um_fstype == UFS1) 219 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev, 220 UFS_MPNEEDSWAP(ump)); 221 else 222 ip->i_ffs2_rdev = ufs_rw64(vap->va_rdev, 223 UFS_MPNEEDSWAP(ump)); 224 } 225 UFS_WAPBL_UPDATE(*vpp, NULL, NULL, 0); 226 UFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp); 227 /* 228 * Remove inode so that it will be reloaded by vcache_get and 229 * checked to see if it is an alias of an existing entry in 230 * the inode cache. 231 */ 232 (*vpp)->v_type = VNON; 233 VOP_UNLOCK(*vpp); 234 vgone(*vpp); 235 error = vcache_get(mp, &ino, sizeof(ino), vpp); 236 out: 237 fstrans_done(ap->a_dvp->v_mount); 238 if (error != 0) { 239 *vpp = NULL; 240 return (error); 241 } 242 return (0); 243 } 244 245 /* 246 * Open called. 247 * 248 * Nothing to do. 249 */ 250 /* ARGSUSED */ 251 int 252 ufs_open(void *v) 253 { 254 struct vop_open_args /* { 255 struct vnode *a_vp; 256 int a_mode; 257 kauth_cred_t a_cred; 258 } */ *ap = v; 259 260 /* 261 * Files marked append-only must be opened for appending. 262 */ 263 if ((VTOI(ap->a_vp)->i_flags & APPEND) && 264 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 265 return (EPERM); 266 return (0); 267 } 268 269 /* 270 * Close called. 271 * 272 * Update the times on the inode. 273 */ 274 /* ARGSUSED */ 275 int 276 ufs_close(void *v) 277 { 278 struct vop_close_args /* { 279 struct vnode *a_vp; 280 int a_fflag; 281 kauth_cred_t a_cred; 282 } */ *ap = v; 283 struct vnode *vp; 284 285 vp = ap->a_vp; 286 fstrans_start(vp->v_mount, FSTRANS_SHARED); 287 if (vp->v_usecount > 1) 288 UFS_ITIMES(vp, NULL, NULL, NULL); 289 fstrans_done(vp->v_mount); 290 return (0); 291 } 292 293 static int 294 ufs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode, 295 kauth_cred_t cred) 296 { 297 #if defined(QUOTA) || defined(QUOTA2) 298 int error; 299 #endif 300 301 /* 302 * Disallow write attempts on read-only file systems; 303 * unless the file is a socket, fifo, or a block or 304 * character device resident on the file system. 305 */ 306 if (mode & VWRITE) { 307 switch (vp->v_type) { 308 case VDIR: 309 case VLNK: 310 case VREG: 311 if (vp->v_mount->mnt_flag & MNT_RDONLY) 312 return (EROFS); 313 #if defined(QUOTA) || defined(QUOTA2) 314 fstrans_start(vp->v_mount, FSTRANS_SHARED); 315 error = chkdq(ip, 0, cred, 0); 316 fstrans_done(vp->v_mount); 317 if (error != 0) 318 return error; 319 #endif 320 break; 321 case VBAD: 322 case VBLK: 323 case VCHR: 324 case VSOCK: 325 case VFIFO: 326 case VNON: 327 default: 328 break; 329 } 330 } 331 332 /* If it is a snapshot, nobody gets access to it. */ 333 if ((ip->i_flags & SF_SNAPSHOT)) 334 return (EPERM); 335 /* If immutable bit set, nobody gets to write it. */ 336 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE)) 337 return (EPERM); 338 339 return 0; 340 } 341 342 static int 343 ufs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode, 344 kauth_cred_t cred) 345 { 346 347 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type, 348 ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type, 349 ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred)); 350 } 351 352 int 353 ufs_access(void *v) 354 { 355 struct vop_access_args /* { 356 struct vnode *a_vp; 357 int a_mode; 358 kauth_cred_t a_cred; 359 } */ *ap = v; 360 struct vnode *vp; 361 struct inode *ip; 362 mode_t mode; 363 int error; 364 365 vp = ap->a_vp; 366 ip = VTOI(vp); 367 mode = ap->a_mode; 368 369 error = ufs_check_possible(vp, ip, mode, ap->a_cred); 370 if (error) 371 return error; 372 373 error = ufs_check_permitted(vp, ip, mode, ap->a_cred); 374 375 return error; 376 } 377 378 /* ARGSUSED */ 379 int 380 ufs_getattr(void *v) 381 { 382 struct vop_getattr_args /* { 383 struct vnode *a_vp; 384 struct vattr *a_vap; 385 kauth_cred_t a_cred; 386 } */ *ap = v; 387 struct vnode *vp; 388 struct inode *ip; 389 struct vattr *vap; 390 391 vp = ap->a_vp; 392 ip = VTOI(vp); 393 vap = ap->a_vap; 394 fstrans_start(vp->v_mount, FSTRANS_SHARED); 395 UFS_ITIMES(vp, NULL, NULL, NULL); 396 397 /* 398 * Copy from inode table 399 */ 400 vap->va_fsid = ip->i_dev; 401 vap->va_fileid = ip->i_number; 402 vap->va_mode = ip->i_mode & ALLPERMS; 403 vap->va_nlink = ip->i_nlink; 404 vap->va_uid = ip->i_uid; 405 vap->va_gid = ip->i_gid; 406 vap->va_size = vp->v_size; 407 if (ip->i_ump->um_fstype == UFS1) { 408 vap->va_rdev = (dev_t)ufs_rw32(ip->i_ffs1_rdev, 409 UFS_MPNEEDSWAP(ip->i_ump)); 410 vap->va_atime.tv_sec = ip->i_ffs1_atime; 411 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec; 412 vap->va_mtime.tv_sec = ip->i_ffs1_mtime; 413 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec; 414 vap->va_ctime.tv_sec = ip->i_ffs1_ctime; 415 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec; 416 vap->va_birthtime.tv_sec = 0; 417 vap->va_birthtime.tv_nsec = 0; 418 vap->va_bytes = dbtob((u_quad_t)ip->i_ffs1_blocks); 419 } else { 420 vap->va_rdev = (dev_t)ufs_rw64(ip->i_ffs2_rdev, 421 UFS_MPNEEDSWAP(ip->i_ump)); 422 vap->va_atime.tv_sec = ip->i_ffs2_atime; 423 vap->va_atime.tv_nsec = ip->i_ffs2_atimensec; 424 vap->va_mtime.tv_sec = ip->i_ffs2_mtime; 425 vap->va_mtime.tv_nsec = ip->i_ffs2_mtimensec; 426 vap->va_ctime.tv_sec = ip->i_ffs2_ctime; 427 vap->va_ctime.tv_nsec = ip->i_ffs2_ctimensec; 428 vap->va_birthtime.tv_sec = ip->i_ffs2_birthtime; 429 vap->va_birthtime.tv_nsec = ip->i_ffs2_birthnsec; 430 vap->va_bytes = dbtob(ip->i_ffs2_blocks); 431 } 432 vap->va_gen = ip->i_gen; 433 vap->va_flags = ip->i_flags; 434 435 /* this doesn't belong here */ 436 if (vp->v_type == VBLK) 437 vap->va_blocksize = BLKDEV_IOSIZE; 438 else if (vp->v_type == VCHR) 439 vap->va_blocksize = MAXBSIZE; 440 else 441 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; 442 vap->va_type = vp->v_type; 443 vap->va_filerev = ip->i_modrev; 444 fstrans_done(vp->v_mount); 445 return (0); 446 } 447 448 /* 449 * Set attribute vnode op. called from several syscalls 450 */ 451 int 452 ufs_setattr(void *v) 453 { 454 struct vop_setattr_args /* { 455 struct vnode *a_vp; 456 struct vattr *a_vap; 457 kauth_cred_t a_cred; 458 } */ *ap = v; 459 struct vattr *vap; 460 struct vnode *vp; 461 struct inode *ip; 462 kauth_cred_t cred; 463 struct lwp *l; 464 int error; 465 kauth_action_t action; 466 bool changing_sysflags; 467 468 vap = ap->a_vap; 469 vp = ap->a_vp; 470 ip = VTOI(vp); 471 cred = ap->a_cred; 472 l = curlwp; 473 action = KAUTH_VNODE_WRITE_FLAGS; 474 changing_sysflags = false; 475 476 /* 477 * Check for unsettable attributes. 478 */ 479 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 480 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 481 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 482 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 483 return (EINVAL); 484 } 485 486 fstrans_start(vp->v_mount, FSTRANS_SHARED); 487 488 if (vap->va_flags != VNOVAL) { 489 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 490 error = EROFS; 491 goto out; 492 } 493 494 /* Snapshot flag cannot be set or cleared */ 495 if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) != 496 (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) { 497 error = EPERM; 498 goto out; 499 } 500 501 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) { 502 action |= KAUTH_VNODE_HAS_SYSFLAGS; 503 } 504 505 if ((vap->va_flags & SF_SETTABLE) != 506 (ip->i_flags & SF_SETTABLE)) { 507 action |= KAUTH_VNODE_WRITE_SYSFLAGS; 508 changing_sysflags = true; 509 } 510 511 error = kauth_authorize_vnode(cred, action, vp, NULL, 512 genfs_can_chflags(cred, vp->v_type, ip->i_uid, 513 changing_sysflags)); 514 if (error) 515 goto out; 516 517 if (changing_sysflags) { 518 error = UFS_WAPBL_BEGIN(vp->v_mount); 519 if (error) 520 goto out; 521 ip->i_flags = vap->va_flags; 522 DIP_ASSIGN(ip, flags, ip->i_flags); 523 } else { 524 error = UFS_WAPBL_BEGIN(vp->v_mount); 525 if (error) 526 goto out; 527 ip->i_flags &= SF_SETTABLE; 528 ip->i_flags |= (vap->va_flags & UF_SETTABLE); 529 DIP_ASSIGN(ip, flags, ip->i_flags); 530 } 531 ip->i_flag |= IN_CHANGE; 532 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 533 UFS_WAPBL_END(vp->v_mount); 534 if (vap->va_flags & (IMMUTABLE | APPEND)) { 535 error = 0; 536 goto out; 537 } 538 } 539 if (ip->i_flags & (IMMUTABLE | APPEND)) { 540 error = EPERM; 541 goto out; 542 } 543 /* 544 * Go through the fields and update iff not VNOVAL. 545 */ 546 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 547 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 548 error = EROFS; 549 goto out; 550 } 551 error = UFS_WAPBL_BEGIN(vp->v_mount); 552 if (error) 553 goto out; 554 error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, l); 555 UFS_WAPBL_END(vp->v_mount); 556 if (error) 557 goto out; 558 } 559 if (vap->va_size != VNOVAL) { 560 /* 561 * Disallow write attempts on read-only file systems; 562 * unless the file is a socket, fifo, or a block or 563 * character device resident on the file system. 564 */ 565 switch (vp->v_type) { 566 case VDIR: 567 error = EISDIR; 568 goto out; 569 case VCHR: 570 case VBLK: 571 case VFIFO: 572 break; 573 case VREG: 574 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 575 error = EROFS; 576 goto out; 577 } 578 if ((ip->i_flags & SF_SNAPSHOT) != 0) { 579 error = EPERM; 580 goto out; 581 } 582 error = UFS_WAPBL_BEGIN(vp->v_mount); 583 if (error) 584 goto out; 585 /* 586 * When journaling, only truncate one indirect block 587 * at a time. 588 */ 589 if (vp->v_mount->mnt_wapbl) { 590 uint64_t incr = MNINDIR(ip->i_ump) << 591 vp->v_mount->mnt_fs_bshift; /* Power of 2 */ 592 uint64_t base = UFS_NDADDR << 593 vp->v_mount->mnt_fs_bshift; 594 while (!error && ip->i_size > base + incr && 595 ip->i_size > vap->va_size + incr) { 596 /* 597 * round down to next full indirect 598 * block boundary. 599 */ 600 uint64_t nsize = base + 601 ((ip->i_size - base - 1) & 602 ~(incr - 1)); 603 error = UFS_TRUNCATE(vp, nsize, 0, 604 cred); 605 if (error == 0) { 606 UFS_WAPBL_END(vp->v_mount); 607 error = 608 UFS_WAPBL_BEGIN(vp->v_mount); 609 } 610 } 611 } 612 if (!error) 613 error = UFS_TRUNCATE(vp, vap->va_size, 0, cred); 614 UFS_WAPBL_END(vp->v_mount); 615 if (error) 616 goto out; 617 break; 618 default: 619 error = EOPNOTSUPP; 620 goto out; 621 } 622 } 623 ip = VTOI(vp); 624 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL || 625 vap->va_birthtime.tv_sec != VNOVAL) { 626 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 627 error = EROFS; 628 goto out; 629 } 630 if ((ip->i_flags & SF_SNAPSHOT) != 0) { 631 error = EPERM; 632 goto out; 633 } 634 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, 635 NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred)); 636 if (error) 637 goto out; 638 error = UFS_WAPBL_BEGIN(vp->v_mount); 639 if (error) 640 goto out; 641 if (vap->va_atime.tv_sec != VNOVAL) 642 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) 643 ip->i_flag |= IN_ACCESS; 644 if (vap->va_mtime.tv_sec != VNOVAL) { 645 ip->i_flag |= IN_CHANGE | IN_UPDATE; 646 if (vp->v_mount->mnt_flag & MNT_RELATIME) 647 ip->i_flag |= IN_ACCESS; 648 } 649 if (vap->va_birthtime.tv_sec != VNOVAL && 650 ip->i_ump->um_fstype == UFS2) { 651 ip->i_ffs2_birthtime = vap->va_birthtime.tv_sec; 652 ip->i_ffs2_birthnsec = vap->va_birthtime.tv_nsec; 653 } 654 error = UFS_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 0); 655 UFS_WAPBL_END(vp->v_mount); 656 if (error) 657 goto out; 658 } 659 error = 0; 660 if (vap->va_mode != (mode_t)VNOVAL) { 661 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 662 error = EROFS; 663 goto out; 664 } 665 if ((ip->i_flags & SF_SNAPSHOT) != 0 && 666 (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | 667 S_IXOTH | S_IWOTH))) { 668 error = EPERM; 669 goto out; 670 } 671 error = UFS_WAPBL_BEGIN(vp->v_mount); 672 if (error) 673 goto out; 674 error = ufs_chmod(vp, (int)vap->va_mode, cred, l); 675 UFS_WAPBL_END(vp->v_mount); 676 } 677 VN_KNOTE(vp, NOTE_ATTRIB); 678 out: 679 fstrans_done(vp->v_mount); 680 return (error); 681 } 682 683 /* 684 * Change the mode on a file. 685 * Inode must be locked before calling. 686 */ 687 static int 688 ufs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l) 689 { 690 struct inode *ip; 691 int error; 692 693 UFS_WAPBL_JLOCK_ASSERT(vp->v_mount); 694 695 ip = VTOI(vp); 696 697 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp, 698 NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode)); 699 if (error) 700 return (error); 701 702 fstrans_start(vp->v_mount, FSTRANS_SHARED); 703 ip->i_mode &= ~ALLPERMS; 704 ip->i_mode |= (mode & ALLPERMS); 705 ip->i_flag |= IN_CHANGE; 706 DIP_ASSIGN(ip, mode, ip->i_mode); 707 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 708 fstrans_done(vp->v_mount); 709 return (0); 710 } 711 712 /* 713 * Perform chown operation on inode ip; 714 * inode must be locked prior to call. 715 */ 716 static int 717 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred, 718 struct lwp *l) 719 { 720 struct inode *ip; 721 int error = 0; 722 #if defined(QUOTA) || defined(QUOTA2) 723 uid_t ouid; 724 gid_t ogid; 725 int64_t change; 726 #endif 727 ip = VTOI(vp); 728 error = 0; 729 730 if (uid == (uid_t)VNOVAL) 731 uid = ip->i_uid; 732 if (gid == (gid_t)VNOVAL) 733 gid = ip->i_gid; 734 735 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp, 736 NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid)); 737 if (error) 738 return (error); 739 740 fstrans_start(vp->v_mount, FSTRANS_SHARED); 741 #if defined(QUOTA) || defined(QUOTA2) 742 ogid = ip->i_gid; 743 ouid = ip->i_uid; 744 change = DIP(ip, blocks); 745 (void) chkdq(ip, -change, cred, 0); 746 (void) chkiq(ip, -1, cred, 0); 747 #endif 748 ip->i_gid = gid; 749 DIP_ASSIGN(ip, gid, gid); 750 ip->i_uid = uid; 751 DIP_ASSIGN(ip, uid, uid); 752 #if defined(QUOTA) || defined(QUOTA2) 753 if ((error = chkdq(ip, change, cred, 0)) == 0) { 754 if ((error = chkiq(ip, 1, cred, 0)) == 0) 755 goto good; 756 else 757 (void) chkdq(ip, -change, cred, FORCE); 758 } 759 ip->i_gid = ogid; 760 DIP_ASSIGN(ip, gid, ogid); 761 ip->i_uid = ouid; 762 DIP_ASSIGN(ip, uid, ouid); 763 (void) chkdq(ip, change, cred, FORCE); 764 (void) chkiq(ip, 1, cred, FORCE); 765 fstrans_done(vp->v_mount); 766 return (error); 767 good: 768 #endif /* QUOTA || QUOTA2 */ 769 ip->i_flag |= IN_CHANGE; 770 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 771 fstrans_done(vp->v_mount); 772 return (0); 773 } 774 775 int 776 ufs_remove(void *v) 777 { 778 struct vop_remove_args /* { 779 struct vnode *a_dvp; 780 struct vnode *a_vp; 781 struct componentname *a_cnp; 782 } */ *ap = v; 783 struct vnode *vp, *dvp; 784 struct inode *ip; 785 int error; 786 struct ufs_lookup_results *ulr; 787 788 vp = ap->a_vp; 789 dvp = ap->a_dvp; 790 ip = VTOI(vp); 791 792 /* XXX should handle this material another way */ 793 ulr = &VTOI(dvp)->i_crap; 794 UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); 795 796 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 797 if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) || 798 (VTOI(dvp)->i_flags & APPEND)) 799 error = EPERM; 800 else { 801 error = UFS_WAPBL_BEGIN(dvp->v_mount); 802 if (error == 0) { 803 error = ufs_dirremove(dvp, ulr, 804 ip, ap->a_cnp->cn_flags, 0); 805 UFS_WAPBL_END(dvp->v_mount); 806 } 807 } 808 VN_KNOTE(vp, NOTE_DELETE); 809 VN_KNOTE(dvp, NOTE_WRITE); 810 if (dvp == vp) 811 vrele(vp); 812 else 813 vput(vp); 814 vput(dvp); 815 fstrans_done(dvp->v_mount); 816 return (error); 817 } 818 819 /* 820 * ufs_link: create hard link. 821 */ 822 int 823 ufs_link(void *v) 824 { 825 struct vop_link_args /* { 826 struct vnode *a_dvp; 827 struct vnode *a_vp; 828 struct componentname *a_cnp; 829 } */ *ap = v; 830 struct vnode *dvp = ap->a_dvp; 831 struct vnode *vp = ap->a_vp; 832 struct componentname *cnp = ap->a_cnp; 833 struct inode *ip; 834 struct direct *newdir; 835 int error; 836 struct ufs_lookup_results *ulr; 837 838 KASSERT(dvp != vp); 839 KASSERT(vp->v_type != VDIR); 840 KASSERT(dvp->v_mount == vp->v_mount); 841 842 /* XXX should handle this material another way */ 843 ulr = &VTOI(dvp)->i_crap; 844 UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); 845 846 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 847 error = vn_lock(vp, LK_EXCLUSIVE); 848 if (error) { 849 VOP_ABORTOP(dvp, cnp); 850 goto out2; 851 } 852 ip = VTOI(vp); 853 if ((nlink_t)ip->i_nlink >= LINK_MAX) { 854 VOP_ABORTOP(dvp, cnp); 855 error = EMLINK; 856 goto out1; 857 } 858 if (ip->i_flags & (IMMUTABLE | APPEND)) { 859 VOP_ABORTOP(dvp, cnp); 860 error = EPERM; 861 goto out1; 862 } 863 error = UFS_WAPBL_BEGIN(vp->v_mount); 864 if (error) { 865 VOP_ABORTOP(dvp, cnp); 866 goto out1; 867 } 868 ip->i_nlink++; 869 DIP_ASSIGN(ip, nlink, ip->i_nlink); 870 ip->i_flag |= IN_CHANGE; 871 error = UFS_UPDATE(vp, NULL, NULL, UPDATE_DIROP); 872 if (!error) { 873 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 874 ufs_makedirentry(ip, cnp, newdir); 875 error = ufs_direnter(dvp, ulr, vp, newdir, cnp, NULL); 876 pool_cache_put(ufs_direct_cache, newdir); 877 } 878 if (error) { 879 ip->i_nlink--; 880 DIP_ASSIGN(ip, nlink, ip->i_nlink); 881 ip->i_flag |= IN_CHANGE; 882 UFS_WAPBL_UPDATE(vp, NULL, NULL, UPDATE_DIROP); 883 } 884 UFS_WAPBL_END(vp->v_mount); 885 out1: 886 VOP_UNLOCK(vp); 887 out2: 888 VN_KNOTE(vp, NOTE_LINK); 889 VN_KNOTE(dvp, NOTE_WRITE); 890 vput(dvp); 891 fstrans_done(dvp->v_mount); 892 return (error); 893 } 894 895 /* 896 * whiteout vnode call 897 */ 898 int 899 ufs_whiteout(void *v) 900 { 901 struct vop_whiteout_args /* { 902 struct vnode *a_dvp; 903 struct componentname *a_cnp; 904 int a_flags; 905 } */ *ap = v; 906 struct vnode *dvp = ap->a_dvp; 907 struct componentname *cnp = ap->a_cnp; 908 struct direct *newdir; 909 int error; 910 struct ufsmount *ump = VFSTOUFS(dvp->v_mount); 911 struct ufs_lookup_results *ulr; 912 913 /* XXX should handle this material another way */ 914 ulr = &VTOI(dvp)->i_crap; 915 UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); 916 917 error = 0; 918 switch (ap->a_flags) { 919 case LOOKUP: 920 /* 4.4 format directories support whiteout operations */ 921 if (ump->um_maxsymlinklen > 0) 922 return (0); 923 return (EOPNOTSUPP); 924 925 case CREATE: 926 /* create a new directory whiteout */ 927 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 928 error = UFS_WAPBL_BEGIN(dvp->v_mount); 929 if (error) 930 break; 931 #ifdef DIAGNOSTIC 932 if (ump->um_maxsymlinklen <= 0) 933 panic("ufs_whiteout: old format filesystem"); 934 #endif 935 936 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 937 newdir->d_ino = UFS_WINO; 938 newdir->d_namlen = cnp->cn_namelen; 939 memcpy(newdir->d_name, cnp->cn_nameptr, 940 (size_t)cnp->cn_namelen); 941 newdir->d_name[cnp->cn_namelen] = '\0'; 942 newdir->d_type = DT_WHT; 943 error = ufs_direnter(dvp, ulr, NULL, newdir, cnp, NULL); 944 pool_cache_put(ufs_direct_cache, newdir); 945 break; 946 947 case DELETE: 948 /* remove an existing directory whiteout */ 949 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 950 error = UFS_WAPBL_BEGIN(dvp->v_mount); 951 if (error) 952 break; 953 #ifdef DIAGNOSTIC 954 if (ump->um_maxsymlinklen <= 0) 955 panic("ufs_whiteout: old format filesystem"); 956 #endif 957 958 cnp->cn_flags &= ~DOWHITEOUT; 959 error = ufs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0); 960 break; 961 default: 962 panic("ufs_whiteout: unknown op"); 963 /* NOTREACHED */ 964 } 965 UFS_WAPBL_END(dvp->v_mount); 966 fstrans_done(dvp->v_mount); 967 return (error); 968 } 969 970 int 971 ufs_mkdir(void *v) 972 { 973 struct vop_mkdir_v3_args /* { 974 struct vnode *a_dvp; 975 struct vnode **a_vpp; 976 struct componentname *a_cnp; 977 struct vattr *a_vap; 978 } */ *ap = v; 979 struct vnode *dvp = ap->a_dvp, *tvp; 980 struct vattr *vap = ap->a_vap; 981 struct componentname *cnp = ap->a_cnp; 982 struct inode *ip, *dp = VTOI(dvp); 983 struct buf *bp; 984 struct dirtemplate dirtemplate; 985 struct direct *newdir; 986 int error, dmode; 987 struct ufsmount *ump = dp->i_ump; 988 int dirblksiz = ump->um_dirblksiz; 989 struct ufs_lookup_results *ulr; 990 991 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 992 993 /* XXX should handle this material another way */ 994 ulr = &dp->i_crap; 995 UFS_CHECK_CRAPCOUNTER(dp); 996 997 if ((nlink_t)dp->i_nlink >= LINK_MAX) { 998 error = EMLINK; 999 goto out; 1000 } 1001 dmode = vap->va_mode & ACCESSPERMS; 1002 dmode |= IFDIR; 1003 /* 1004 * Must simulate part of ufs_makeinode here to acquire the inode, 1005 * but not have it entered in the parent directory. The entry is 1006 * made later after writing "." and ".." entries. 1007 */ 1008 if ((error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, ap->a_vpp)) != 0) 1009 goto out; 1010 1011 tvp = *ap->a_vpp; 1012 ip = VTOI(tvp); 1013 1014 error = UFS_WAPBL_BEGIN(ap->a_dvp->v_mount); 1015 if (error) { 1016 UFS_VFREE(tvp, ip->i_number, dmode); 1017 vput(tvp); 1018 goto out; 1019 } 1020 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred); 1021 DIP_ASSIGN(ip, uid, ip->i_uid); 1022 ip->i_gid = dp->i_gid; 1023 DIP_ASSIGN(ip, gid, ip->i_gid); 1024 #if defined(QUOTA) || defined(QUOTA2) 1025 if ((error = chkiq(ip, 1, cnp->cn_cred, 0))) { 1026 UFS_VFREE(tvp, ip->i_number, dmode); 1027 UFS_WAPBL_END(dvp->v_mount); 1028 fstrans_done(dvp->v_mount); 1029 vput(tvp); 1030 return (error); 1031 } 1032 #endif 1033 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1034 ip->i_mode = dmode; 1035 DIP_ASSIGN(ip, mode, dmode); 1036 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ 1037 ip->i_nlink = 2; 1038 DIP_ASSIGN(ip, nlink, 2); 1039 if (cnp->cn_flags & ISWHITEOUT) { 1040 ip->i_flags |= UF_OPAQUE; 1041 DIP_ASSIGN(ip, flags, ip->i_flags); 1042 } 1043 1044 /* 1045 * Bump link count in parent directory to reflect work done below. 1046 * Should be done before reference is created so cleanup is 1047 * possible if we crash. 1048 */ 1049 dp->i_nlink++; 1050 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1051 dp->i_flag |= IN_CHANGE; 1052 if ((error = UFS_UPDATE(dvp, NULL, NULL, UPDATE_DIROP)) != 0) 1053 goto bad; 1054 1055 /* 1056 * Initialize directory with "." and ".." from static template. 1057 */ 1058 dirtemplate = mastertemplate; 1059 dirtemplate.dotdot_reclen = dirblksiz - dirtemplate.dot_reclen; 1060 dirtemplate.dot_ino = ufs_rw32(ip->i_number, UFS_MPNEEDSWAP(ump)); 1061 dirtemplate.dotdot_ino = ufs_rw32(dp->i_number, UFS_MPNEEDSWAP(ump)); 1062 dirtemplate.dot_reclen = ufs_rw16(dirtemplate.dot_reclen, 1063 UFS_MPNEEDSWAP(ump)); 1064 dirtemplate.dotdot_reclen = ufs_rw16(dirtemplate.dotdot_reclen, 1065 UFS_MPNEEDSWAP(ump)); 1066 if (ump->um_maxsymlinklen <= 0) { 1067 #if BYTE_ORDER == LITTLE_ENDIAN 1068 if (UFS_MPNEEDSWAP(ump) == 0) 1069 #else 1070 if (UFS_MPNEEDSWAP(ump) != 0) 1071 #endif 1072 { 1073 dirtemplate.dot_type = dirtemplate.dot_namlen; 1074 dirtemplate.dotdot_type = dirtemplate.dotdot_namlen; 1075 dirtemplate.dot_namlen = dirtemplate.dotdot_namlen = 0; 1076 } else 1077 dirtemplate.dot_type = dirtemplate.dotdot_type = 0; 1078 } 1079 if ((error = UFS_BALLOC(tvp, (off_t)0, dirblksiz, cnp->cn_cred, 1080 B_CLRBUF, &bp)) != 0) 1081 goto bad; 1082 ip->i_size = dirblksiz; 1083 DIP_ASSIGN(ip, size, dirblksiz); 1084 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1085 uvm_vnp_setsize(tvp, ip->i_size); 1086 memcpy((void *)bp->b_data, (void *)&dirtemplate, sizeof dirtemplate); 1087 1088 /* 1089 * Directory set up, now install it's entry in the parent directory. 1090 * We must write out the buffer containing the new directory body 1091 * before entering the new name in the parent. 1092 */ 1093 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) 1094 goto bad; 1095 if ((error = UFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0) { 1096 goto bad; 1097 } 1098 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 1099 ufs_makedirentry(ip, cnp, newdir); 1100 error = ufs_direnter(dvp, ulr, tvp, newdir, cnp, bp); 1101 pool_cache_put(ufs_direct_cache, newdir); 1102 bad: 1103 if (error == 0) { 1104 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); 1105 VOP_UNLOCK(tvp); 1106 UFS_WAPBL_END(dvp->v_mount); 1107 } else { 1108 dp->i_nlink--; 1109 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1110 dp->i_flag |= IN_CHANGE; 1111 UFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP); 1112 /* 1113 * No need to do an explicit UFS_TRUNCATE here, vrele will 1114 * do this for us because we set the link count to 0. 1115 */ 1116 ip->i_nlink = 0; 1117 DIP_ASSIGN(ip, nlink, 0); 1118 ip->i_flag |= IN_CHANGE; 1119 UFS_WAPBL_UPDATE(tvp, NULL, NULL, UPDATE_DIROP); 1120 UFS_WAPBL_END(dvp->v_mount); 1121 vput(tvp); 1122 } 1123 out: 1124 fstrans_done(dvp->v_mount); 1125 return (error); 1126 } 1127 1128 int 1129 ufs_rmdir(void *v) 1130 { 1131 struct vop_rmdir_args /* { 1132 struct vnode *a_dvp; 1133 struct vnode *a_vp; 1134 struct componentname *a_cnp; 1135 } */ *ap = v; 1136 struct vnode *vp, *dvp; 1137 struct componentname *cnp; 1138 struct inode *ip, *dp; 1139 int error; 1140 struct ufs_lookup_results *ulr; 1141 1142 vp = ap->a_vp; 1143 dvp = ap->a_dvp; 1144 cnp = ap->a_cnp; 1145 ip = VTOI(vp); 1146 dp = VTOI(dvp); 1147 1148 /* XXX should handle this material another way */ 1149 ulr = &dp->i_crap; 1150 UFS_CHECK_CRAPCOUNTER(dp); 1151 1152 /* 1153 * No rmdir "." or of mounted directories please. 1154 */ 1155 if (dp == ip || vp->v_mountedhere != NULL) { 1156 if (dp == ip) 1157 vrele(dvp); 1158 else 1159 vput(dvp); 1160 vput(vp); 1161 return (EINVAL); 1162 } 1163 1164 fstrans_start(dvp->v_mount, FSTRANS_SHARED); 1165 1166 /* 1167 * Do not remove a directory that is in the process of being renamed. 1168 * Verify that the directory is empty (and valid). (Rmdir ".." won't 1169 * be valid since ".." will contain a reference to the current 1170 * directory and thus be non-empty.) 1171 */ 1172 error = 0; 1173 if (ip->i_nlink != 2 || 1174 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { 1175 error = ENOTEMPTY; 1176 goto out; 1177 } 1178 if ((dp->i_flags & APPEND) || 1179 (ip->i_flags & (IMMUTABLE | APPEND))) { 1180 error = EPERM; 1181 goto out; 1182 } 1183 error = UFS_WAPBL_BEGIN(dvp->v_mount); 1184 if (error) 1185 goto out; 1186 /* 1187 * Delete reference to directory before purging 1188 * inode. If we crash in between, the directory 1189 * will be reattached to lost+found, 1190 */ 1191 error = ufs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1); 1192 if (error) { 1193 UFS_WAPBL_END(dvp->v_mount); 1194 goto out; 1195 } 1196 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); 1197 cache_purge(dvp); 1198 /* 1199 * Truncate inode. The only stuff left in the directory is "." and 1200 * "..". The "." reference is inconsequential since we're quashing 1201 * it. 1202 */ 1203 dp->i_nlink--; 1204 DIP_ASSIGN(dp, nlink, dp->i_nlink); 1205 dp->i_flag |= IN_CHANGE; 1206 UFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP); 1207 ip->i_nlink--; 1208 DIP_ASSIGN(ip, nlink, ip->i_nlink); 1209 ip->i_flag |= IN_CHANGE; 1210 error = UFS_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred); 1211 cache_purge(vp); 1212 /* 1213 * Unlock the log while we still have reference to unlinked 1214 * directory vp so that it will not get locked for recycling 1215 */ 1216 UFS_WAPBL_END(dvp->v_mount); 1217 #ifdef UFS_DIRHASH 1218 if (ip->i_dirhash != NULL) 1219 ufsdirhash_free(ip); 1220 #endif 1221 out: 1222 VN_KNOTE(vp, NOTE_DELETE); 1223 vput(vp); 1224 fstrans_done(dvp->v_mount); 1225 vput(dvp); 1226 return (error); 1227 } 1228 1229 /* 1230 * symlink -- make a symbolic link 1231 */ 1232 int 1233 ufs_symlink(void *v) 1234 { 1235 struct vop_symlink_v3_args /* { 1236 struct vnode *a_dvp; 1237 struct vnode **a_vpp; 1238 struct componentname *a_cnp; 1239 struct vattr *a_vap; 1240 char *a_target; 1241 } */ *ap = v; 1242 struct vnode *vp, **vpp; 1243 struct inode *ip; 1244 int len, error; 1245 struct ufs_lookup_results *ulr; 1246 1247 vpp = ap->a_vpp; 1248 1249 /* XXX should handle this material another way */ 1250 ulr = &VTOI(ap->a_dvp)->i_crap; 1251 UFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp)); 1252 1253 /* 1254 * UFS_WAPBL_BEGIN1(dvp->v_mount, dvp) performed by successful 1255 * ufs_makeinode 1256 */ 1257 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED); 1258 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, ulr, 1259 vpp, ap->a_cnp); 1260 if (error) 1261 goto out; 1262 VN_KNOTE(ap->a_dvp, NOTE_WRITE); 1263 vp = *vpp; 1264 len = strlen(ap->a_target); 1265 ip = VTOI(vp); 1266 /* 1267 * This test is off by one. um_maxsymlinklen contains the 1268 * number of bytes available, and we aren't storing a \0, so 1269 * the test should properly be <=. However, it cannot be 1270 * changed as this would break compatibility with existing fs 1271 * images -- see the way ufs_readlink() works. 1272 */ 1273 if (len < ip->i_ump->um_maxsymlinklen) { 1274 memcpy((char *)SHORTLINK(ip), ap->a_target, len); 1275 ip->i_size = len; 1276 DIP_ASSIGN(ip, size, len); 1277 uvm_vnp_setsize(vp, ip->i_size); 1278 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1279 if (vp->v_mount->mnt_flag & MNT_RELATIME) 1280 ip->i_flag |= IN_ACCESS; 1281 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 1282 } else 1283 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, 1284 UIO_SYSSPACE, IO_NODELOCKED | IO_JOURNALLOCKED, 1285 ap->a_cnp->cn_cred, NULL, NULL); 1286 UFS_WAPBL_END1(ap->a_dvp->v_mount, ap->a_dvp); 1287 VOP_UNLOCK(vp); 1288 if (error) 1289 vrele(vp); 1290 out: 1291 fstrans_done(ap->a_dvp->v_mount); 1292 return (error); 1293 } 1294 1295 /* 1296 * Vnode op for reading directories. 1297 * 1298 * This routine handles converting from the on-disk directory format 1299 * "struct direct" to the in-memory format "struct dirent" as well as 1300 * byte swapping the entries if necessary. 1301 */ 1302 int 1303 ufs_readdir(void *v) 1304 { 1305 struct vop_readdir_args /* { 1306 struct vnode *a_vp; 1307 struct uio *a_uio; 1308 kauth_cred_t a_cred; 1309 int *a_eofflag; 1310 off_t **a_cookies; 1311 int *ncookies; 1312 } */ *ap = v; 1313 struct vnode *vp = ap->a_vp; 1314 struct direct *cdp, *ecdp; 1315 struct dirent *ndp; 1316 char *cdbuf, *ndbuf, *endp; 1317 struct uio auio, *uio; 1318 struct iovec aiov; 1319 int error; 1320 size_t count, ccount, rcount, cdbufsz, ndbufsz; 1321 off_t off, *ccp; 1322 off_t startoff; 1323 size_t skipbytes; 1324 struct ufsmount *ump = VFSTOUFS(vp->v_mount); 1325 int nswap = UFS_MPNEEDSWAP(ump); 1326 #if BYTE_ORDER == LITTLE_ENDIAN 1327 int needswap = ump->um_maxsymlinklen <= 0 && nswap == 0; 1328 #else 1329 int needswap = ump->um_maxsymlinklen <= 0 && nswap != 0; 1330 #endif 1331 uio = ap->a_uio; 1332 count = uio->uio_resid; 1333 rcount = count - ((uio->uio_offset + count) & (ump->um_dirblksiz - 1)); 1334 1335 if (rcount < _DIRENT_MINSIZE(cdp) || count < _DIRENT_MINSIZE(ndp)) 1336 return EINVAL; 1337 1338 startoff = uio->uio_offset & ~(ump->um_dirblksiz - 1); 1339 skipbytes = uio->uio_offset - startoff; 1340 rcount += skipbytes; 1341 1342 auio.uio_iov = &aiov; 1343 auio.uio_iovcnt = 1; 1344 auio.uio_offset = startoff; 1345 auio.uio_resid = rcount; 1346 UIO_SETUP_SYSSPACE(&auio); 1347 auio.uio_rw = UIO_READ; 1348 cdbufsz = rcount; 1349 cdbuf = kmem_alloc(cdbufsz, KM_SLEEP); 1350 aiov.iov_base = cdbuf; 1351 aiov.iov_len = rcount; 1352 error = VOP_READ(vp, &auio, 0, ap->a_cred); 1353 if (error != 0) { 1354 kmem_free(cdbuf, cdbufsz); 1355 return error; 1356 } 1357 1358 rcount -= auio.uio_resid; 1359 1360 cdp = (struct direct *)(void *)cdbuf; 1361 ecdp = (struct direct *)(void *)&cdbuf[rcount]; 1362 1363 ndbufsz = count; 1364 ndbuf = kmem_alloc(ndbufsz, KM_SLEEP); 1365 ndp = (struct dirent *)(void *)ndbuf; 1366 endp = &ndbuf[count]; 1367 1368 off = uio->uio_offset; 1369 if (ap->a_cookies) { 1370 ccount = rcount / _DIRENT_RECLEN(cdp, 1); 1371 ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp), 1372 M_TEMP, M_WAITOK); 1373 } else { 1374 /* XXX: GCC */ 1375 ccount = 0; 1376 ccp = NULL; 1377 } 1378 1379 while (cdp < ecdp) { 1380 cdp->d_reclen = ufs_rw16(cdp->d_reclen, nswap); 1381 if (skipbytes > 0) { 1382 if (cdp->d_reclen <= skipbytes) { 1383 skipbytes -= cdp->d_reclen; 1384 cdp = _DIRENT_NEXT(cdp); 1385 continue; 1386 } 1387 /* 1388 * invalid cookie. 1389 */ 1390 error = EINVAL; 1391 goto out; 1392 } 1393 if (cdp->d_reclen == 0) { 1394 struct dirent *ondp = ndp; 1395 ndp->d_reclen = _DIRENT_MINSIZE(ndp); 1396 ndp = _DIRENT_NEXT(ndp); 1397 ondp->d_reclen = 0; 1398 cdp = ecdp; 1399 break; 1400 } 1401 if (needswap) { 1402 ndp->d_type = cdp->d_namlen; 1403 ndp->d_namlen = cdp->d_type; 1404 } else { 1405 ndp->d_type = cdp->d_type; 1406 ndp->d_namlen = cdp->d_namlen; 1407 } 1408 ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen); 1409 if ((char *)(void *)ndp + ndp->d_reclen + 1410 _DIRENT_MINSIZE(ndp) > endp) 1411 break; 1412 ndp->d_fileno = ufs_rw32(cdp->d_ino, nswap); 1413 (void)memcpy(ndp->d_name, cdp->d_name, ndp->d_namlen); 1414 memset(&ndp->d_name[ndp->d_namlen], 0, 1415 ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen); 1416 off += cdp->d_reclen; 1417 if (ap->a_cookies) { 1418 KASSERT(ccp - *(ap->a_cookies) < ccount); 1419 *(ccp++) = off; 1420 } 1421 ndp = _DIRENT_NEXT(ndp); 1422 cdp = _DIRENT_NEXT(cdp); 1423 } 1424 1425 count = ((char *)(void *)ndp - ndbuf); 1426 error = uiomove(ndbuf, count, uio); 1427 out: 1428 if (ap->a_cookies) { 1429 if (error) { 1430 free(*(ap->a_cookies), M_TEMP); 1431 *(ap->a_cookies) = NULL; 1432 *(ap->a_ncookies) = 0; 1433 } else { 1434 *ap->a_ncookies = ccp - *(ap->a_cookies); 1435 } 1436 } 1437 uio->uio_offset = off; 1438 kmem_free(ndbuf, ndbufsz); 1439 kmem_free(cdbuf, cdbufsz); 1440 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset; 1441 return error; 1442 } 1443 1444 /* 1445 * Return target name of a symbolic link 1446 */ 1447 int 1448 ufs_readlink(void *v) 1449 { 1450 struct vop_readlink_args /* { 1451 struct vnode *a_vp; 1452 struct uio *a_uio; 1453 kauth_cred_t a_cred; 1454 } */ *ap = v; 1455 struct vnode *vp = ap->a_vp; 1456 struct inode *ip = VTOI(vp); 1457 struct ufsmount *ump = VFSTOUFS(vp->v_mount); 1458 int isize; 1459 1460 /* 1461 * The test against um_maxsymlinklen is off by one; it should 1462 * theoretically be <=, not <. However, it cannot be changed 1463 * as that would break compatibility with existing fs images. 1464 */ 1465 1466 isize = ip->i_size; 1467 if (isize < ump->um_maxsymlinklen || 1468 (ump->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) { 1469 uiomove((char *)SHORTLINK(ip), isize, ap->a_uio); 1470 return (0); 1471 } 1472 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); 1473 } 1474 1475 /* 1476 * Calculate the logical to physical mapping if not done already, 1477 * then call the device strategy routine. 1478 */ 1479 int 1480 ufs_strategy(void *v) 1481 { 1482 struct vop_strategy_args /* { 1483 struct vnode *a_vp; 1484 struct buf *a_bp; 1485 } */ *ap = v; 1486 struct buf *bp; 1487 struct vnode *vp; 1488 struct inode *ip; 1489 struct mount *mp; 1490 int error; 1491 1492 bp = ap->a_bp; 1493 vp = ap->a_vp; 1494 ip = VTOI(vp); 1495 if (vp->v_type == VBLK || vp->v_type == VCHR) 1496 panic("ufs_strategy: spec"); 1497 KASSERT(bp->b_bcount != 0); 1498 if (bp->b_blkno == bp->b_lblkno) { 1499 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, 1500 NULL); 1501 if (error) { 1502 bp->b_error = error; 1503 biodone(bp); 1504 return (error); 1505 } 1506 if (bp->b_blkno == -1) /* no valid data */ 1507 clrbuf(bp); 1508 } 1509 if (bp->b_blkno < 0) { /* block is not on disk */ 1510 biodone(bp); 1511 return (0); 1512 } 1513 vp = ip->i_devvp; 1514 1515 error = VOP_STRATEGY(vp, bp); 1516 if (error) 1517 return error; 1518 1519 if (!BUF_ISREAD(bp)) 1520 return 0; 1521 1522 mp = wapbl_vptomp(vp); 1523 if (mp == NULL || mp->mnt_wapbl_replay == NULL || 1524 !WAPBL_REPLAY_ISOPEN(mp) || 1525 !WAPBL_REPLAY_CAN_READ(mp, bp->b_blkno, bp->b_bcount)) 1526 return 0; 1527 1528 error = biowait(bp); 1529 if (error) 1530 return error; 1531 1532 error = WAPBL_REPLAY_READ(mp, bp->b_data, bp->b_blkno, bp->b_bcount); 1533 if (error) { 1534 mutex_enter(&bufcache_lock); 1535 SET(bp->b_cflags, BC_INVAL); 1536 mutex_exit(&bufcache_lock); 1537 } 1538 return error; 1539 } 1540 1541 /* 1542 * Print out the contents of an inode. 1543 */ 1544 int 1545 ufs_print(void *v) 1546 { 1547 struct vop_print_args /* { 1548 struct vnode *a_vp; 1549 } */ *ap = v; 1550 struct vnode *vp; 1551 struct inode *ip; 1552 1553 vp = ap->a_vp; 1554 ip = VTOI(vp); 1555 printf("tag VT_UFS, ino %llu, on dev %llu, %llu", 1556 (unsigned long long)ip->i_number, 1557 (unsigned long long)major(ip->i_dev), 1558 (unsigned long long)minor(ip->i_dev)); 1559 printf(" flags 0x%x, nlink %d\n", 1560 ip->i_flag, ip->i_nlink); 1561 printf("\tmode 0%o, owner %d, group %d, size %qd", 1562 ip->i_mode, ip->i_uid, ip->i_gid, 1563 (long long)ip->i_size); 1564 if (vp->v_type == VFIFO) 1565 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v); 1566 printf("\n"); 1567 return (0); 1568 } 1569 1570 /* 1571 * Read wrapper for special devices. 1572 */ 1573 int 1574 ufsspec_read(void *v) 1575 { 1576 struct vop_read_args /* { 1577 struct vnode *a_vp; 1578 struct uio *a_uio; 1579 int a_ioflag; 1580 kauth_cred_t a_cred; 1581 } */ *ap = v; 1582 1583 /* 1584 * Set access flag. 1585 */ 1586 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0) 1587 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1588 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap)); 1589 } 1590 1591 /* 1592 * Write wrapper for special devices. 1593 */ 1594 int 1595 ufsspec_write(void *v) 1596 { 1597 struct vop_write_args /* { 1598 struct vnode *a_vp; 1599 struct uio *a_uio; 1600 int a_ioflag; 1601 kauth_cred_t a_cred; 1602 } */ *ap = v; 1603 1604 /* 1605 * Set update and change flags. 1606 */ 1607 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0) 1608 VTOI(ap->a_vp)->i_flag |= IN_MODIFY; 1609 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap)); 1610 } 1611 1612 /* 1613 * Close wrapper for special devices. 1614 * 1615 * Update the times on the inode then do device close. 1616 */ 1617 int 1618 ufsspec_close(void *v) 1619 { 1620 struct vop_close_args /* { 1621 struct vnode *a_vp; 1622 int a_fflag; 1623 kauth_cred_t a_cred; 1624 } */ *ap = v; 1625 struct vnode *vp; 1626 1627 vp = ap->a_vp; 1628 if (vp->v_usecount > 1) 1629 UFS_ITIMES(vp, NULL, NULL, NULL); 1630 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap)); 1631 } 1632 1633 /* 1634 * Read wrapper for fifo's 1635 */ 1636 int 1637 ufsfifo_read(void *v) 1638 { 1639 struct vop_read_args /* { 1640 struct vnode *a_vp; 1641 struct uio *a_uio; 1642 int a_ioflag; 1643 kauth_cred_t a_cred; 1644 } */ *ap = v; 1645 1646 /* 1647 * Set access flag. 1648 */ 1649 VTOI(ap->a_vp)->i_flag |= IN_ACCESS; 1650 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap)); 1651 } 1652 1653 /* 1654 * Write wrapper for fifo's. 1655 */ 1656 int 1657 ufsfifo_write(void *v) 1658 { 1659 struct vop_write_args /* { 1660 struct vnode *a_vp; 1661 struct uio *a_uio; 1662 int a_ioflag; 1663 kauth_cred_t a_cred; 1664 } */ *ap = v; 1665 1666 /* 1667 * Set update and change flags. 1668 */ 1669 VTOI(ap->a_vp)->i_flag |= IN_MODIFY; 1670 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap)); 1671 } 1672 1673 /* 1674 * Close wrapper for fifo's. 1675 * 1676 * Update the times on the inode then do device close. 1677 */ 1678 int 1679 ufsfifo_close(void *v) 1680 { 1681 struct vop_close_args /* { 1682 struct vnode *a_vp; 1683 int a_fflag; 1684 kauth_cred_t a_cred; 1685 } */ *ap = v; 1686 struct vnode *vp; 1687 1688 vp = ap->a_vp; 1689 if (ap->a_vp->v_usecount > 1) 1690 UFS_ITIMES(vp, NULL, NULL, NULL); 1691 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap)); 1692 } 1693 1694 /* 1695 * Return POSIX pathconf information applicable to ufs filesystems. 1696 */ 1697 int 1698 ufs_pathconf(void *v) 1699 { 1700 struct vop_pathconf_args /* { 1701 struct vnode *a_vp; 1702 int a_name; 1703 register_t *a_retval; 1704 } */ *ap = v; 1705 1706 switch (ap->a_name) { 1707 case _PC_LINK_MAX: 1708 *ap->a_retval = LINK_MAX; 1709 return (0); 1710 case _PC_NAME_MAX: 1711 *ap->a_retval = FFS_MAXNAMLEN; 1712 return (0); 1713 case _PC_PATH_MAX: 1714 *ap->a_retval = PATH_MAX; 1715 return (0); 1716 case _PC_PIPE_BUF: 1717 *ap->a_retval = PIPE_BUF; 1718 return (0); 1719 case _PC_CHOWN_RESTRICTED: 1720 *ap->a_retval = 1; 1721 return (0); 1722 case _PC_NO_TRUNC: 1723 *ap->a_retval = 1; 1724 return (0); 1725 case _PC_SYNC_IO: 1726 *ap->a_retval = 1; 1727 return (0); 1728 case _PC_FILESIZEBITS: 1729 *ap->a_retval = 42; 1730 return (0); 1731 case _PC_SYMLINK_MAX: 1732 *ap->a_retval = MAXPATHLEN; 1733 return (0); 1734 case _PC_2_SYMLINKS: 1735 *ap->a_retval = 1; 1736 return (0); 1737 default: 1738 return (EINVAL); 1739 } 1740 /* NOTREACHED */ 1741 } 1742 1743 /* 1744 * Advisory record locking support 1745 */ 1746 int 1747 ufs_advlock(void *v) 1748 { 1749 struct vop_advlock_args /* { 1750 struct vnode *a_vp; 1751 void * a_id; 1752 int a_op; 1753 struct flock *a_fl; 1754 int a_flags; 1755 } */ *ap = v; 1756 struct inode *ip; 1757 1758 ip = VTOI(ap->a_vp); 1759 return lf_advlock(ap, &ip->i_lockf, ip->i_size); 1760 } 1761 1762 /* 1763 * Initialize the vnode associated with a new inode, handle aliased 1764 * vnodes. 1765 */ 1766 void 1767 ufs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *), 1768 struct vnode **vpp) 1769 { 1770 struct timeval tv; 1771 struct inode *ip; 1772 struct vnode *vp; 1773 dev_t rdev; 1774 struct ufsmount *ump; 1775 1776 vp = *vpp; 1777 ip = VTOI(vp); 1778 switch(vp->v_type = IFTOVT(ip->i_mode)) { 1779 case VCHR: 1780 case VBLK: 1781 vp->v_op = specops; 1782 ump = ip->i_ump; 1783 if (ump->um_fstype == UFS1) 1784 rdev = (dev_t)ufs_rw32(ip->i_ffs1_rdev, 1785 UFS_MPNEEDSWAP(ump)); 1786 else 1787 rdev = (dev_t)ufs_rw64(ip->i_ffs2_rdev, 1788 UFS_MPNEEDSWAP(ump)); 1789 spec_node_init(vp, rdev); 1790 break; 1791 case VFIFO: 1792 vp->v_op = fifoops; 1793 break; 1794 case VNON: 1795 case VBAD: 1796 case VSOCK: 1797 case VLNK: 1798 case VDIR: 1799 case VREG: 1800 break; 1801 } 1802 if (ip->i_number == UFS_ROOTINO) 1803 vp->v_vflag |= VV_ROOT; 1804 /* 1805 * Initialize modrev times 1806 */ 1807 getmicrouptime(&tv); 1808 ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32 1809 | tv.tv_usec * 4294u; 1810 *vpp = vp; 1811 } 1812 1813 /* 1814 * Allocate a new inode. 1815 */ 1816 int 1817 ufs_makeinode(int mode, struct vnode *dvp, const struct ufs_lookup_results *ulr, 1818 struct vnode **vpp, struct componentname *cnp) 1819 { 1820 struct inode *ip, *pdir; 1821 struct direct *newdir; 1822 struct vnode *tvp; 1823 int error; 1824 1825 UFS_WAPBL_JUNLOCK_ASSERT(dvp->v_mount); 1826 1827 pdir = VTOI(dvp); 1828 1829 if ((mode & IFMT) == 0) 1830 mode |= IFREG; 1831 1832 if ((error = UFS_VALLOC(dvp, mode, cnp->cn_cred, vpp)) != 0) { 1833 return (error); 1834 } 1835 tvp = *vpp; 1836 ip = VTOI(tvp); 1837 ip->i_gid = pdir->i_gid; 1838 DIP_ASSIGN(ip, gid, ip->i_gid); 1839 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred); 1840 DIP_ASSIGN(ip, uid, ip->i_uid); 1841 error = UFS_WAPBL_BEGIN1(dvp->v_mount, dvp); 1842 if (error) { 1843 /* 1844 * Note, we can't VOP_VFREE(tvp) here like we should 1845 * because we can't write to the disk. Instead, we leave 1846 * the vnode dangling from the journal. 1847 */ 1848 vput(tvp); 1849 return (error); 1850 } 1851 #if defined(QUOTA) || defined(QUOTA2) 1852 if ((error = chkiq(ip, 1, cnp->cn_cred, 0))) { 1853 UFS_VFREE(tvp, ip->i_number, mode); 1854 UFS_WAPBL_END1(dvp->v_mount, dvp); 1855 vput(tvp); 1856 return (error); 1857 } 1858 #endif 1859 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1860 ip->i_mode = mode; 1861 DIP_ASSIGN(ip, mode, mode); 1862 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ 1863 ip->i_nlink = 1; 1864 DIP_ASSIGN(ip, nlink, 1); 1865 1866 /* Authorize setting SGID if needed. */ 1867 if (ip->i_mode & ISGID) { 1868 error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY, 1869 tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid, 1870 ip->i_gid, mode)); 1871 if (error) { 1872 ip->i_mode &= ~ISGID; 1873 DIP_ASSIGN(ip, mode, ip->i_mode); 1874 } 1875 } 1876 1877 if (cnp->cn_flags & ISWHITEOUT) { 1878 ip->i_flags |= UF_OPAQUE; 1879 DIP_ASSIGN(ip, flags, ip->i_flags); 1880 } 1881 1882 /* 1883 * Make sure inode goes to disk before directory entry. 1884 */ 1885 if ((error = UFS_UPDATE(tvp, NULL, NULL, UPDATE_DIROP)) != 0) 1886 goto bad; 1887 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK); 1888 ufs_makedirentry(ip, cnp, newdir); 1889 error = ufs_direnter(dvp, ulr, tvp, newdir, cnp, NULL); 1890 pool_cache_put(ufs_direct_cache, newdir); 1891 if (error) 1892 goto bad; 1893 *vpp = tvp; 1894 return (0); 1895 1896 bad: 1897 /* 1898 * Write error occurred trying to update the inode 1899 * or the directory so must deallocate the inode. 1900 */ 1901 ip->i_nlink = 0; 1902 DIP_ASSIGN(ip, nlink, 0); 1903 ip->i_flag |= IN_CHANGE; 1904 UFS_WAPBL_UPDATE(tvp, NULL, NULL, 0); 1905 tvp->v_type = VNON; /* explodes later if VBLK */ 1906 UFS_WAPBL_END1(dvp->v_mount, dvp); 1907 vput(tvp); 1908 return (error); 1909 } 1910 1911 /* 1912 * Allocate len bytes at offset off. 1913 */ 1914 int 1915 ufs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags, 1916 kauth_cred_t cred) 1917 { 1918 struct inode *ip = VTOI(vp); 1919 int error, delta, bshift, bsize; 1920 UVMHIST_FUNC("ufs_gop_alloc"); UVMHIST_CALLED(ubchist); 1921 1922 error = 0; 1923 bshift = vp->v_mount->mnt_fs_bshift; 1924 bsize = 1 << bshift; 1925 1926 delta = off & (bsize - 1); 1927 off -= delta; 1928 len += delta; 1929 1930 while (len > 0) { 1931 bsize = MIN(bsize, len); 1932 1933 error = UFS_BALLOC(vp, off, bsize, cred, flags, NULL); 1934 if (error) { 1935 goto out; 1936 } 1937 1938 /* 1939 * increase file size now, UFS_BALLOC() requires that 1940 * EOF be up-to-date before each call. 1941 */ 1942 1943 if (ip->i_size < off + bsize) { 1944 UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x", 1945 vp, ip->i_size, off + bsize, 0); 1946 ip->i_size = off + bsize; 1947 DIP_ASSIGN(ip, size, ip->i_size); 1948 } 1949 1950 off += bsize; 1951 len -= bsize; 1952 } 1953 1954 out: 1955 UFS_WAPBL_UPDATE(vp, NULL, NULL, 0); 1956 return error; 1957 } 1958 1959 void 1960 ufs_gop_markupdate(struct vnode *vp, int flags) 1961 { 1962 u_int32_t mask = 0; 1963 1964 if ((flags & GOP_UPDATE_ACCESSED) != 0) { 1965 mask = IN_ACCESS; 1966 } 1967 if ((flags & GOP_UPDATE_MODIFIED) != 0) { 1968 if (vp->v_type == VREG) { 1969 mask |= IN_CHANGE | IN_UPDATE; 1970 } else { 1971 mask |= IN_MODIFY; 1972 } 1973 } 1974 if (mask) { 1975 struct inode *ip = VTOI(vp); 1976 1977 ip->i_flag |= mask; 1978 } 1979 } 1980