1 /* 2 * Copyright (c) 2011-2018 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@dragonflybsd.org> 6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 7 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 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 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 3. Neither the name of The DragonFly Project nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific, prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 /* 37 * Kernel Filesystem interface 38 * 39 * NOTE! local ipdata pointers must be reloaded on any modifying operation 40 * to the inode as its underlying chain may have changed. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/fcntl.h> 47 #include <sys/buf.h> 48 #include <sys/proc.h> 49 #include <sys/mount.h> 50 #include <sys/vnode.h> 51 #include <sys/mountctl.h> 52 #include <sys/dirent.h> 53 #include <sys/uio.h> 54 #include <sys/objcache.h> 55 #include <sys/event.h> 56 #include <sys/file.h> 57 #include <vfs/fifofs/fifo.h> 58 59 #include "hammer2.h" 60 61 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, 62 int seqcount); 63 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio, 64 int ioflag, int seqcount); 65 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize); 66 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize); 67 68 /* 69 * Last reference to a vnode is going away but it is still cached. 70 */ 71 static 72 int 73 hammer2_vop_inactive(struct vop_inactive_args *ap) 74 { 75 hammer2_inode_t *ip; 76 struct vnode *vp; 77 78 vp = ap->a_vp; 79 ip = VTOI(vp); 80 81 /* 82 * Degenerate case 83 */ 84 if (ip == NULL) { 85 vrecycle(vp); 86 return (0); 87 } 88 89 /* 90 * Aquire the inode lock to interlock against vp updates via 91 * the inode path and file deletions and such (which can be 92 * namespace-only operations that might not hold the vnode). 93 */ 94 hammer2_inode_lock(ip, 0); 95 if (ip->flags & HAMMER2_INODE_ISUNLINKED) { 96 hammer2_key_t lbase; 97 int nblksize; 98 99 /* 100 * If the inode has been unlinked we can throw away all 101 * buffers (dirty or not) and clean the file out. 102 * 103 * Because vrecycle() calls are not guaranteed, try to 104 * dispose of the inode as much as possible right here. 105 */ 106 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL); 107 nvtruncbuf(vp, 0, nblksize, 0, 0); 108 109 /* 110 * Delete the file on-media. 111 */ 112 if ((ip->flags & HAMMER2_INODE_DELETING) == 0) { 113 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING); 114 hammer2_inode_delayed_sideq(ip); 115 } 116 hammer2_inode_unlock(ip); 117 118 /* 119 * Recycle immediately if possible 120 */ 121 vrecycle(vp); 122 } else { 123 hammer2_inode_unlock(ip); 124 } 125 return (0); 126 } 127 128 /* 129 * Reclaim a vnode so that it can be reused; after the inode is 130 * disassociated, the filesystem must manage it alone. 131 */ 132 static 133 int 134 hammer2_vop_reclaim(struct vop_reclaim_args *ap) 135 { 136 hammer2_inode_t *ip; 137 hammer2_pfs_t *pmp; 138 struct vnode *vp; 139 140 vp = ap->a_vp; 141 ip = VTOI(vp); 142 if (ip == NULL) 143 return(0); 144 145 pmp = ip->pmp; 146 147 /* 148 * NOTE! We do not attempt to flush chains here, flushing is 149 * really fragile and could also deadlock. 150 */ 151 vclrisdirty(vp); 152 153 /* 154 * The inode lock is required to disconnect it. 155 */ 156 hammer2_inode_lock(ip, 0); 157 vp->v_data = NULL; 158 ip->vp = NULL; 159 160 /* 161 * Delete the file on-media. This should have been handled by the 162 * inactivation. The operation is likely still queued on the inode 163 * though so only complain if the stars don't align. 164 */ 165 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED | HAMMER2_INODE_DELETING)) == 166 HAMMER2_INODE_ISUNLINKED) 167 { 168 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING); 169 hammer2_inode_delayed_sideq(ip); 170 kprintf("hammer2: vp=%p ip=%p unlinked but not disposed\n", 171 vp, ip); 172 } 173 hammer2_inode_unlock(ip); 174 175 /* 176 * Modified inodes will already be on SIDEQ or SYNCQ, no further 177 * action is needed. 178 * 179 * We cannot safely synchronize the inode from inside the reclaim 180 * due to potentially deep locks held as-of when the reclaim occurs. 181 * Interactions and potential deadlocks abound. We also can't do it 182 * here without desynchronizing from the related directory entrie(s). 183 */ 184 hammer2_inode_drop(ip); /* vp ref */ 185 186 /* 187 * XXX handle background sync when ip dirty, kernel will no longer 188 * notify us regarding this inode because there is no longer a 189 * vnode attached to it. 190 */ 191 192 return (0); 193 } 194 195 /* 196 * Currently this function synchronizes the front-end inode state to the 197 * backend chain topology, then flushes the inode's chain and sub-topology 198 * to backend media. This function does not flush the root topology down to 199 * the inode. 200 */ 201 static 202 int 203 hammer2_vop_fsync(struct vop_fsync_args *ap) 204 { 205 hammer2_inode_t *ip; 206 struct vnode *vp; 207 int error1; 208 int error2; 209 210 vp = ap->a_vp; 211 ip = VTOI(vp); 212 error1 = 0; 213 214 hammer2_trans_init(ip->pmp, 0); 215 216 /* 217 * Flush dirty buffers in the file's logical buffer cache. 218 * It is best to wait for the strategy code to commit the 219 * buffers to the device's backing buffer cache before 220 * then trying to flush the inode. 221 * 222 * This should be quick, but certain inode modifications cached 223 * entirely in the hammer2_inode structure may not trigger a 224 * buffer read until the flush so the fsync can wind up also 225 * doing scattered reads. 226 */ 227 vfsync(vp, ap->a_waitfor, 1, NULL, NULL); 228 bio_track_wait(&vp->v_track_write, 0, 0); 229 230 /* 231 * Flush any inode changes 232 */ 233 hammer2_inode_lock(ip, 0); 234 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED)) 235 error1 = hammer2_inode_chain_sync(ip); 236 237 /* 238 * Flush dirty chains related to the inode. 239 * 240 * NOTE! We are not in a flush transaction. The inode remains on 241 * the sideq so the filesystem syncer can synchronize it to 242 * the volume root. 243 */ 244 error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP); 245 if (error2) 246 error1 = error2; 247 248 /* 249 * We may be able to clear the vnode dirty flag. 250 */ 251 if ((ip->flags & (HAMMER2_INODE_MODIFIED | 252 HAMMER2_INODE_RESIZED | 253 HAMMER2_INODE_DIRTYDATA)) == 0 && 254 RB_EMPTY(&vp->v_rbdirty_tree) && 255 !bio_track_active(&vp->v_track_write)) { 256 vclrisdirty(vp); 257 } 258 hammer2_inode_unlock(ip); 259 hammer2_trans_done(ip->pmp, 0); 260 261 return (error1); 262 } 263 264 /* 265 * No lock needed, just handle ip->update 266 */ 267 static 268 int 269 hammer2_vop_access(struct vop_access_args *ap) 270 { 271 hammer2_inode_t *ip = VTOI(ap->a_vp); 272 uid_t uid; 273 gid_t gid; 274 mode_t mode; 275 uint32_t uflags; 276 int error; 277 int update; 278 279 retry: 280 update = spin_access_start(&ip->cluster_spin); 281 282 /*hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);*/ 283 uid = hammer2_to_unix_xid(&ip->meta.uid); 284 gid = hammer2_to_unix_xid(&ip->meta.gid); 285 mode = ip->meta.mode; 286 uflags = ip->meta.uflags; 287 /*hammer2_inode_unlock(ip);*/ 288 289 if (__predict_false(spin_access_end(&ip->cluster_spin, update))) 290 goto retry; 291 292 error = vop_helper_access(ap, uid, gid, mode, uflags); 293 294 return (error); 295 } 296 297 static 298 int 299 hammer2_vop_getattr(struct vop_getattr_args *ap) 300 { 301 hammer2_pfs_t *pmp; 302 hammer2_inode_t *ip; 303 struct vnode *vp; 304 struct vattr *vap; 305 int update; 306 307 vp = ap->a_vp; 308 vap = ap->a_vap; 309 310 ip = VTOI(vp); 311 pmp = ip->pmp; 312 313 retry: 314 update = spin_access_start(&ip->cluster_spin); 315 316 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0]; 317 vap->va_fileid = ip->meta.inum; 318 vap->va_mode = ip->meta.mode; 319 vap->va_nlink = ip->meta.nlinks; 320 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid); 321 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid); 322 vap->va_rmajor = 0; 323 vap->va_rminor = 0; 324 vap->va_size = ip->meta.size; /* protected by shared lock */ 325 vap->va_blocksize = HAMMER2_PBUFSIZE; 326 vap->va_flags = ip->meta.uflags; 327 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime); 328 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime); 329 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime); 330 vap->va_gen = 1; 331 vap->va_bytes = 0; 332 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) { 333 /* 334 * Can't really calculate directory use sans the files under 335 * it, just assume one block for now. 336 */ 337 vap->va_bytes += HAMMER2_INODE_BYTES; 338 } else { 339 vap->va_bytes = hammer2_inode_data_count(ip); 340 } 341 vap->va_type = hammer2_get_vtype(ip->meta.type); 342 vap->va_filerev = 0; 343 vap->va_uid_uuid = ip->meta.uid; 344 vap->va_gid_uuid = ip->meta.gid; 345 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | 346 VA_FSID_UUID_VALID; 347 348 if (__predict_false(spin_access_end(&ip->cluster_spin, update))) 349 goto retry; 350 351 return (0); 352 } 353 354 static 355 int 356 hammer2_vop_getattr_lite(struct vop_getattr_lite_args *ap) 357 { 358 hammer2_pfs_t *pmp; 359 hammer2_inode_t *ip; 360 struct vnode *vp; 361 struct vattr_lite *lvap; 362 int update; 363 364 vp = ap->a_vp; 365 lvap = ap->a_lvap; 366 367 ip = VTOI(vp); 368 pmp = ip->pmp; 369 370 retry: 371 update = spin_access_start(&ip->cluster_spin); 372 373 #if 0 374 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0]; 375 vap->va_fileid = ip->meta.inum; 376 #endif 377 lvap->va_mode = ip->meta.mode; 378 lvap->va_nlink = ip->meta.nlinks; 379 lvap->va_uid = hammer2_to_unix_xid(&ip->meta.uid); 380 lvap->va_gid = hammer2_to_unix_xid(&ip->meta.gid); 381 #if 0 382 vap->va_rmajor = 0; 383 vap->va_rminor = 0; 384 #endif 385 lvap->va_size = ip->meta.size; 386 #if 0 387 vap->va_blocksize = HAMMER2_PBUFSIZE; 388 #endif 389 lvap->va_flags = ip->meta.uflags; 390 lvap->va_type = hammer2_get_vtype(ip->meta.type); 391 #if 0 392 vap->va_filerev = 0; 393 vap->va_uid_uuid = ip->meta.uid; 394 vap->va_gid_uuid = ip->meta.gid; 395 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | 396 VA_FSID_UUID_VALID; 397 #endif 398 399 if (__predict_false(spin_access_end(&ip->cluster_spin, update))) 400 goto retry; 401 402 return (0); 403 } 404 405 static 406 int 407 hammer2_vop_setattr(struct vop_setattr_args *ap) 408 { 409 hammer2_inode_t *ip; 410 struct vnode *vp; 411 struct vattr *vap; 412 int error; 413 int kflags = 0; 414 uint64_t ctime; 415 416 vp = ap->a_vp; 417 vap = ap->a_vap; 418 hammer2_update_time(&ctime); 419 420 ip = VTOI(vp); 421 422 if (ip->pmp->ronly) 423 return (EROFS); 424 425 /* 426 * Normally disallow setattr if there is no space, unless we 427 * are in emergency mode (might be needed to chflags -R noschg 428 * files prior to removal). 429 */ 430 if ((ip->pmp->flags & HAMMER2_PMPF_EMERG) == 0 && 431 hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1) { 432 return (ENOSPC); 433 } 434 435 hammer2_trans_init(ip->pmp, 0); 436 hammer2_inode_lock(ip, 0); 437 error = 0; 438 439 if (vap->va_flags != VNOVAL) { 440 uint32_t flags; 441 442 flags = ip->meta.uflags; 443 error = vop_helper_setattr_flags(&flags, vap->va_flags, 444 hammer2_to_unix_xid(&ip->meta.uid), 445 ap->a_cred); 446 if (error == 0) { 447 if (ip->meta.uflags != flags) { 448 hammer2_inode_modify(ip); 449 hammer2_spin_lock_update(&ip->cluster_spin); 450 ip->meta.uflags = flags; 451 ip->meta.ctime = ctime; 452 hammer2_spin_unlock_update(&ip->cluster_spin); 453 kflags |= NOTE_ATTRIB; 454 } 455 if (ip->meta.uflags & (IMMUTABLE | APPEND)) { 456 error = 0; 457 goto done; 458 } 459 } 460 goto done; 461 } 462 if (ip->meta.uflags & (IMMUTABLE | APPEND)) { 463 error = EPERM; 464 goto done; 465 } 466 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 467 mode_t cur_mode = ip->meta.mode; 468 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid); 469 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid); 470 uuid_t uuid_uid; 471 uuid_t uuid_gid; 472 473 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, 474 ap->a_cred, 475 &cur_uid, &cur_gid, &cur_mode); 476 if (error == 0) { 477 hammer2_guid_to_uuid(&uuid_uid, cur_uid); 478 hammer2_guid_to_uuid(&uuid_gid, cur_gid); 479 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) || 480 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) || 481 ip->meta.mode != cur_mode 482 ) { 483 hammer2_inode_modify(ip); 484 hammer2_spin_lock_update(&ip->cluster_spin); 485 ip->meta.uid = uuid_uid; 486 ip->meta.gid = uuid_gid; 487 ip->meta.mode = cur_mode; 488 ip->meta.ctime = ctime; 489 hammer2_spin_unlock_update(&ip->cluster_spin); 490 } 491 kflags |= NOTE_ATTRIB; 492 } 493 } 494 495 /* 496 * Resize the file 497 */ 498 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) { 499 switch(vp->v_type) { 500 case VREG: 501 if (vap->va_size == ip->meta.size) 502 break; 503 if (vap->va_size < ip->meta.size) { 504 hammer2_mtx_ex(&ip->truncate_lock); 505 hammer2_truncate_file(ip, vap->va_size); 506 hammer2_mtx_unlock(&ip->truncate_lock); 507 kflags |= NOTE_WRITE; 508 } else { 509 hammer2_extend_file(ip, vap->va_size); 510 kflags |= NOTE_WRITE | NOTE_EXTEND; 511 } 512 hammer2_inode_modify(ip); 513 ip->meta.mtime = ctime; 514 vclrflags(vp, VLASTWRITETS); 515 break; 516 default: 517 error = EINVAL; 518 goto done; 519 } 520 } 521 #if 0 522 /* atime not supported */ 523 if (vap->va_atime.tv_sec != VNOVAL) { 524 hammer2_inode_modify(ip); 525 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime); 526 kflags |= NOTE_ATTRIB; 527 } 528 #endif 529 if (vap->va_mode != (mode_t)VNOVAL) { 530 mode_t cur_mode = ip->meta.mode; 531 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid); 532 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid); 533 534 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, 535 cur_uid, cur_gid, &cur_mode); 536 if (error == 0) { 537 hammer2_inode_modify(ip); 538 hammer2_spin_lock_update(&ip->cluster_spin); 539 ip->meta.mode = cur_mode; 540 ip->meta.ctime = ctime; 541 hammer2_spin_unlock_update(&ip->cluster_spin); 542 kflags |= NOTE_ATTRIB; 543 } 544 } 545 546 if (vap->va_mtime.tv_sec != VNOVAL) { 547 hammer2_inode_modify(ip); 548 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime); 549 kflags |= NOTE_ATTRIB; 550 vclrflags(vp, VLASTWRITETS); 551 } 552 553 done: 554 /* 555 * If a truncation occurred we must call chain_sync() now in order 556 * to trim the related data chains, otherwise a later expansion can 557 * cause havoc. 558 * 559 * If an extend occured that changed the DIRECTDATA state, we must 560 * call inode_chain_sync now in order to prepare the inode's indirect 561 * block table. 562 * 563 * WARNING! This means we are making an adjustment to the inode's 564 * chain outside of sync/fsync, and not just to inode->meta, which 565 * may result in some consistency issues if a crash were to occur 566 * at just the wrong time. 567 */ 568 if (ip->flags & HAMMER2_INODE_RESIZED) 569 hammer2_inode_chain_sync(ip); 570 571 /* 572 * Cleanup. 573 */ 574 hammer2_inode_unlock(ip); 575 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); 576 hammer2_knote(ip->vp, kflags); 577 578 return (error); 579 } 580 581 static 582 int 583 hammer2_vop_readdir(struct vop_readdir_args *ap) 584 { 585 hammer2_xop_readdir_t *xop; 586 hammer2_blockref_t bref; 587 hammer2_inode_t *ip; 588 hammer2_tid_t inum; 589 hammer2_key_t lkey; 590 struct uio *uio; 591 off_t *cookies; 592 off_t saveoff; 593 int cookie_index; 594 int ncookies; 595 int error; 596 int eofflag; 597 int r; 598 599 ip = VTOI(ap->a_vp); 600 uio = ap->a_uio; 601 saveoff = uio->uio_offset; 602 eofflag = 0; 603 error = 0; 604 605 /* 606 * Setup cookies directory entry cookies if requested 607 */ 608 if (ap->a_ncookies) { 609 ncookies = uio->uio_resid / 16 + 1; 610 if (ncookies > 1024) 611 ncookies = 1024; 612 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); 613 } else { 614 ncookies = -1; 615 cookies = NULL; 616 } 617 cookie_index = 0; 618 619 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED); 620 621 /* 622 * Handle artificial entries. To ensure that only positive 64 bit 623 * quantities are returned to userland we always strip off bit 63. 624 * The hash code is designed such that codes 0x0000-0x7FFF are not 625 * used, allowing us to use these codes for articial entries. 626 * 627 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not 628 * allow '..' to cross the mount point into (e.g.) the super-root. 629 */ 630 if (saveoff == 0) { 631 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK; 632 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, "."); 633 if (r) 634 goto done; 635 if (cookies) 636 cookies[cookie_index] = saveoff; 637 ++saveoff; 638 ++cookie_index; 639 if (cookie_index == ncookies) 640 goto done; 641 } 642 643 if (saveoff == 1) { 644 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK; 645 if (ip != ip->pmp->iroot) 646 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK; 647 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, ".."); 648 if (r) 649 goto done; 650 if (cookies) 651 cookies[cookie_index] = saveoff; 652 ++saveoff; 653 ++cookie_index; 654 if (cookie_index == ncookies) 655 goto done; 656 } 657 658 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE; 659 if (hammer2_debug & 0x0020) 660 kprintf("readdir: lkey %016jx\n", lkey); 661 if (error) 662 goto done; 663 664 /* 665 * Use XOP for cluster scan. 666 * 667 * parent is the inode cluster, already locked for us. Don't 668 * double lock shared locks as this will screw up upgrades. 669 */ 670 xop = hammer2_xop_alloc(ip, 0); 671 xop->lkey = lkey; 672 hammer2_xop_start(&xop->head, &hammer2_readdir_desc); 673 674 for (;;) { 675 const hammer2_inode_data_t *ripdata; 676 const char *dname; 677 int dtype; 678 679 error = hammer2_xop_collect(&xop->head, 0); 680 error = hammer2_error_to_errno(error); 681 if (error) { 682 break; 683 } 684 if (cookie_index == ncookies) 685 break; 686 if (hammer2_debug & 0x0020) 687 kprintf("cluster chain %p %p\n", 688 xop->head.cluster.focus, 689 (xop->head.cluster.focus ? 690 xop->head.cluster.focus->data : (void *)-1)); 691 hammer2_cluster_bref(&xop->head.cluster, &bref); 692 693 if (bref.type == HAMMER2_BREF_TYPE_INODE) { 694 ripdata = &hammer2_xop_gdata(&xop->head)->ipdata; 695 dtype = hammer2_get_dtype(ripdata->meta.type); 696 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK; 697 r = vop_write_dirent(&error, uio, 698 ripdata->meta.inum & 699 HAMMER2_DIRHASH_USERMSK, 700 dtype, 701 ripdata->meta.name_len, 702 ripdata->filename); 703 hammer2_xop_pdata(&xop->head); 704 if (r) 705 break; 706 if (cookies) 707 cookies[cookie_index] = saveoff; 708 ++cookie_index; 709 } else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) { 710 uint16_t namlen; 711 712 dtype = hammer2_get_dtype(bref.embed.dirent.type); 713 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK; 714 namlen = bref.embed.dirent.namlen; 715 if (namlen <= sizeof(bref.check.buf)) { 716 dname = bref.check.buf; 717 } else { 718 dname = hammer2_xop_gdata(&xop->head)->buf; 719 } 720 r = vop_write_dirent(&error, uio, 721 bref.embed.dirent.inum, dtype, 722 namlen, dname); 723 if (namlen > sizeof(bref.check.buf)) 724 hammer2_xop_pdata(&xop->head); 725 if (r) 726 break; 727 if (cookies) 728 cookies[cookie_index] = saveoff; 729 ++cookie_index; 730 } else { 731 /* XXX chain error */ 732 kprintf("bad chain type readdir %d\n", bref.type); 733 } 734 } 735 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 736 if (error == ENOENT) { 737 error = 0; 738 eofflag = 1; 739 saveoff = (hammer2_key_t)-1; 740 } else { 741 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK; 742 } 743 done: 744 hammer2_inode_unlock(ip); 745 if (ap->a_eofflag) 746 *ap->a_eofflag = eofflag; 747 if (hammer2_debug & 0x0020) 748 kprintf("readdir: done at %016jx\n", saveoff); 749 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE; 750 if (error && cookie_index == 0) { 751 if (cookies) { 752 kfree(cookies, M_TEMP); 753 *ap->a_ncookies = 0; 754 *ap->a_cookies = NULL; 755 } 756 } else { 757 if (cookies) { 758 *ap->a_ncookies = cookie_index; 759 *ap->a_cookies = cookies; 760 } 761 } 762 return (error); 763 } 764 765 /* 766 * hammer2_vop_readlink { vp, uio, cred } 767 */ 768 static 769 int 770 hammer2_vop_readlink(struct vop_readlink_args *ap) 771 { 772 struct vnode *vp; 773 hammer2_inode_t *ip; 774 int error; 775 776 vp = ap->a_vp; 777 if (vp->v_type != VLNK) 778 return (EINVAL); 779 ip = VTOI(vp); 780 781 error = hammer2_read_file(ip, ap->a_uio, 0); 782 return (error); 783 } 784 785 static 786 int 787 hammer2_vop_read(struct vop_read_args *ap) 788 { 789 struct vnode *vp; 790 hammer2_inode_t *ip; 791 struct uio *uio; 792 int error; 793 int seqcount; 794 795 /* 796 * Read operations supported on this vnode? 797 */ 798 vp = ap->a_vp; 799 if (vp->v_type == VDIR) 800 return (EISDIR); 801 if (vp->v_type != VREG) 802 return (EINVAL); 803 804 /* 805 * Misc 806 */ 807 ip = VTOI(vp); 808 uio = ap->a_uio; 809 error = 0; 810 811 seqcount = ap->a_ioflag >> IO_SEQSHIFT; 812 813 error = hammer2_read_file(ip, uio, seqcount); 814 return (error); 815 } 816 817 static 818 int 819 hammer2_vop_write(struct vop_write_args *ap) 820 { 821 hammer2_inode_t *ip; 822 thread_t td; 823 struct vnode *vp; 824 struct uio *uio; 825 int error; 826 int seqcount; 827 int ioflag; 828 829 /* 830 * Read operations supported on this vnode? 831 */ 832 vp = ap->a_vp; 833 if (vp->v_type != VREG) 834 return (EINVAL); 835 836 /* 837 * Misc 838 */ 839 ip = VTOI(vp); 840 ioflag = ap->a_ioflag; 841 uio = ap->a_uio; 842 error = 0; 843 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG)) 844 return (EROFS); 845 switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) { 846 case 2: 847 return (ENOSPC); 848 case 1: 849 ioflag |= IO_DIRECT; /* semi-synchronous */ 850 /* fall through */ 851 default: 852 break; 853 } 854 855 seqcount = ioflag >> IO_SEQSHIFT; 856 857 /* 858 * Check resource limit 859 */ 860 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc && 861 uio->uio_offset + uio->uio_resid > 862 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 863 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ); 864 return (EFBIG); 865 } 866 867 /* 868 * The transaction interlocks against flush initiations 869 * (note: but will run concurrently with the actual flush). 870 * 871 * To avoid deadlocking against the VM system, we must flag any 872 * transaction related to the buffer cache or other direct 873 * VM page manipulation. 874 */ 875 if (uio->uio_segflg == UIO_NOCOPY) { 876 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE); 877 } else { 878 hammer2_trans_init(ip->pmp, 0); 879 } 880 error = hammer2_write_file(ip, uio, ioflag, seqcount); 881 if (uio->uio_segflg == UIO_NOCOPY) 882 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE | 883 HAMMER2_TRANS_SIDEQ); 884 else 885 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); 886 887 return (error); 888 } 889 890 /* 891 * Perform read operations on a file or symlink given an UNLOCKED 892 * inode and uio. 893 * 894 * The passed ip is not locked. 895 */ 896 static 897 int 898 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount) 899 { 900 hammer2_off_t size; 901 struct buf *bp; 902 int error; 903 904 error = 0; 905 906 /* 907 * UIO read loop. 908 * 909 * WARNING! Assumes that the kernel interlocks size changes at the 910 * vnode level. 911 */ 912 hammer2_mtx_sh(&ip->lock); 913 hammer2_mtx_sh(&ip->truncate_lock); 914 size = ip->meta.size; 915 hammer2_mtx_unlock(&ip->lock); 916 917 while (uio->uio_resid > 0 && uio->uio_offset < size) { 918 hammer2_key_t lbase; 919 hammer2_key_t leof; 920 int lblksize; 921 int loff; 922 int n; 923 924 lblksize = hammer2_calc_logical(ip, uio->uio_offset, 925 &lbase, &leof); 926 927 #if 1 928 bp = NULL; 929 error = cluster_readx(ip->vp, leof, lbase, lblksize, 930 B_NOTMETA | B_KVABIO, 931 uio->uio_resid, 932 seqcount * MAXBSIZE, 933 &bp); 934 #else 935 if (uio->uio_segflg == UIO_NOCOPY) { 936 bp = getblk(ip->vp, lbase, lblksize, 937 GETBLK_BHEAVY | GETBLK_KVABIO, 0); 938 if (bp->b_flags & B_CACHE) { 939 int i; 940 int j = 0; 941 if (bp->b_xio.xio_npages != 16) 942 kprintf("NPAGES BAD\n"); 943 for (i = 0; i < bp->b_xio.xio_npages; ++i) { 944 vm_page_t m; 945 m = bp->b_xio.xio_pages[i]; 946 if (m == NULL || m->valid == 0) { 947 kprintf("bp %016jx %016jx pg %d inv", 948 lbase, leof, i); 949 if (m) 950 kprintf("m->object %p/%p", m->object, ip->vp->v_object); 951 kprintf("\n"); 952 j = 1; 953 } 954 } 955 if (j) 956 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error); 957 } 958 bqrelse(bp); 959 } 960 error = bread_kvabio(ip->vp, lbase, lblksize, &bp); 961 #endif 962 if (error) { 963 brelse(bp); 964 break; 965 } 966 bkvasync(bp); 967 loff = (int)(uio->uio_offset - lbase); 968 n = lblksize - loff; 969 if (n > uio->uio_resid) 970 n = uio->uio_resid; 971 if (n > size - uio->uio_offset) 972 n = (int)(size - uio->uio_offset); 973 bp->b_flags |= B_AGE; 974 uiomovebp(bp, (char *)bp->b_data + loff, n, uio); 975 bqrelse(bp); 976 } 977 hammer2_mtx_unlock(&ip->truncate_lock); 978 979 return (error); 980 } 981 982 /* 983 * Write to the file represented by the inode via the logical buffer cache. 984 * The inode may represent a regular file or a symlink. 985 * 986 * The inode must not be locked. 987 */ 988 static 989 int 990 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio, 991 int ioflag, int seqcount) 992 { 993 hammer2_key_t old_eof; 994 hammer2_key_t new_eof; 995 struct buf *bp; 996 int kflags; 997 int error; 998 int modified; 999 1000 /* 1001 * Setup if append 1002 * 1003 * WARNING! Assumes that the kernel interlocks size changes at the 1004 * vnode level. 1005 */ 1006 hammer2_mtx_ex(&ip->lock); 1007 hammer2_mtx_sh(&ip->truncate_lock); 1008 if (ioflag & IO_APPEND) 1009 uio->uio_offset = ip->meta.size; 1010 old_eof = ip->meta.size; 1011 1012 /* 1013 * Extend the file if necessary. If the write fails at some point 1014 * we will truncate it back down to cover as much as we were able 1015 * to write. 1016 * 1017 * Doing this now makes it easier to calculate buffer sizes in 1018 * the loop. 1019 */ 1020 kflags = 0; 1021 error = 0; 1022 modified = 0; 1023 1024 if (uio->uio_offset + uio->uio_resid > old_eof) { 1025 new_eof = uio->uio_offset + uio->uio_resid; 1026 modified = 1; 1027 hammer2_extend_file(ip, new_eof); 1028 kflags |= NOTE_EXTEND; 1029 } else { 1030 new_eof = old_eof; 1031 } 1032 hammer2_mtx_unlock(&ip->lock); 1033 1034 /* 1035 * UIO write loop 1036 */ 1037 while (uio->uio_resid > 0) { 1038 hammer2_key_t lbase; 1039 int trivial; 1040 int endofblk; 1041 int lblksize; 1042 int loff; 1043 int n; 1044 1045 /* 1046 * Don't allow the buffer build to blow out the buffer 1047 * cache. 1048 */ 1049 if ((ioflag & IO_RECURSE) == 0) 1050 bwillwrite(HAMMER2_PBUFSIZE); 1051 1052 /* 1053 * This nominally tells us how much we can cluster and 1054 * what the logical buffer size needs to be. Currently 1055 * we don't try to cluster the write and just handle one 1056 * block at a time. 1057 */ 1058 lblksize = hammer2_calc_logical(ip, uio->uio_offset, 1059 &lbase, NULL); 1060 loff = (int)(uio->uio_offset - lbase); 1061 1062 KKASSERT(lblksize <= MAXBSIZE); 1063 1064 /* 1065 * Calculate bytes to copy this transfer and whether the 1066 * copy completely covers the buffer or not. 1067 */ 1068 trivial = 0; 1069 n = lblksize - loff; 1070 if (n > uio->uio_resid) { 1071 n = uio->uio_resid; 1072 if (loff == lbase && uio->uio_offset + n == new_eof) 1073 trivial = 1; 1074 endofblk = 0; 1075 } else { 1076 if (loff == 0) 1077 trivial = 1; 1078 endofblk = 1; 1079 } 1080 if (lbase >= new_eof) 1081 trivial = 1; 1082 1083 /* 1084 * Get the buffer 1085 */ 1086 if (uio->uio_segflg == UIO_NOCOPY) { 1087 /* 1088 * Issuing a write with the same data backing the 1089 * buffer. Instantiate the buffer to collect the 1090 * backing vm pages, then read-in any missing bits. 1091 * 1092 * This case is used by vop_stdputpages(). 1093 */ 1094 bp = getblk(ip->vp, lbase, lblksize, 1095 GETBLK_BHEAVY | GETBLK_KVABIO, 0); 1096 if ((bp->b_flags & B_CACHE) == 0) { 1097 bqrelse(bp); 1098 error = bread_kvabio(ip->vp, lbase, 1099 lblksize, &bp); 1100 } 1101 } else if (trivial) { 1102 /* 1103 * Even though we are entirely overwriting the buffer 1104 * we may still have to zero it out to avoid a 1105 * mmap/write visibility issue. 1106 */ 1107 bp = getblk(ip->vp, lbase, lblksize, 1108 GETBLK_BHEAVY | GETBLK_KVABIO, 0); 1109 if ((bp->b_flags & B_CACHE) == 0) 1110 vfs_bio_clrbuf(bp); 1111 } else { 1112 /* 1113 * Partial overwrite, read in any missing bits then 1114 * replace the portion being written. 1115 * 1116 * (The strategy code will detect zero-fill physical 1117 * blocks for this case). 1118 */ 1119 error = bread_kvabio(ip->vp, lbase, lblksize, &bp); 1120 if (error == 0) 1121 bheavy(bp); 1122 } 1123 1124 if (error) { 1125 brelse(bp); 1126 break; 1127 } 1128 1129 /* 1130 * Ok, copy the data in 1131 */ 1132 bkvasync(bp); 1133 error = uiomovebp(bp, bp->b_data + loff, n, uio); 1134 kflags |= NOTE_WRITE; 1135 modified = 1; 1136 if (error) { 1137 brelse(bp); 1138 break; 1139 } 1140 1141 /* 1142 * WARNING: Pageout daemon will issue UIO_NOCOPY writes 1143 * with IO_SYNC or IO_ASYNC set. These writes 1144 * must be handled as the pageout daemon expects. 1145 * 1146 * NOTE! H2 relies on cluster_write() here because it 1147 * cannot preallocate disk blocks at the logical 1148 * level due to not knowing what the compression 1149 * size will be at this time. 1150 * 1151 * We must use cluster_write() here and we depend 1152 * on the write-behind feature to flush buffers 1153 * appropriately. If we let the buffer daemons do 1154 * it the block allocations will be all over the 1155 * map. 1156 */ 1157 if (ioflag & IO_SYNC) { 1158 bwrite(bp); 1159 } else if ((ioflag & IO_DIRECT) && endofblk) { 1160 bawrite(bp); 1161 } else if (ioflag & IO_ASYNC) { 1162 bawrite(bp); 1163 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) { 1164 bdwrite(bp); 1165 } else { 1166 #if 1 1167 bp->b_flags |= B_CLUSTEROK; 1168 cluster_write(bp, new_eof, lblksize, seqcount); 1169 #else 1170 bp->b_flags |= B_CLUSTEROK; 1171 bdwrite(bp); 1172 #endif 1173 } 1174 } 1175 1176 /* 1177 * Cleanup. If we extended the file EOF but failed to write through 1178 * the entire write is a failure and we have to back-up. 1179 */ 1180 if (error && new_eof != old_eof) { 1181 hammer2_mtx_unlock(&ip->truncate_lock); 1182 hammer2_mtx_ex(&ip->lock); /* note lock order */ 1183 hammer2_mtx_ex(&ip->truncate_lock); /* note lock order */ 1184 hammer2_truncate_file(ip, old_eof); 1185 if (ip->flags & HAMMER2_INODE_MODIFIED) 1186 hammer2_inode_chain_sync(ip); 1187 hammer2_mtx_unlock(&ip->lock); 1188 } else if (modified) { 1189 struct vnode *vp = ip->vp; 1190 1191 hammer2_mtx_ex(&ip->lock); 1192 hammer2_inode_modify(ip); 1193 if (uio->uio_segflg == UIO_NOCOPY) { 1194 if (vp->v_flag & VLASTWRITETS) { 1195 ip->meta.mtime = 1196 (unsigned long)vp->v_lastwrite_ts.tv_sec * 1197 1000000 + 1198 vp->v_lastwrite_ts.tv_nsec / 1000; 1199 } 1200 } else { 1201 hammer2_update_time(&ip->meta.mtime); 1202 vclrflags(vp, VLASTWRITETS); 1203 } 1204 1205 #if 0 1206 /* 1207 * REMOVED - handled by hammer2_extend_file(). Do not issue 1208 * a chain_sync() outside of a sync/fsync except for DIRECTDATA 1209 * state changes. 1210 * 1211 * Under normal conditions we only issue a chain_sync if 1212 * the inode's DIRECTDATA state changed. 1213 */ 1214 if (ip->flags & HAMMER2_INODE_RESIZED) 1215 hammer2_inode_chain_sync(ip); 1216 #endif 1217 hammer2_mtx_unlock(&ip->lock); 1218 hammer2_knote(ip->vp, kflags); 1219 } 1220 hammer2_trans_assert_strategy(ip->pmp); 1221 hammer2_mtx_unlock(&ip->truncate_lock); 1222 1223 return error; 1224 } 1225 1226 /* 1227 * Truncate the size of a file. The inode must be locked. 1228 * 1229 * We must unconditionally set HAMMER2_INODE_RESIZED to properly 1230 * ensure that any on-media data beyond the new file EOF has been destroyed. 1231 * 1232 * WARNING: nvtruncbuf() can only be safely called without the inode lock 1233 * held due to the way our write thread works. If the truncation 1234 * occurs in the middle of a buffer, nvtruncbuf() is responsible 1235 * for dirtying that buffer and zeroing out trailing bytes. 1236 * 1237 * WARNING! Assumes that the kernel interlocks size changes at the 1238 * vnode level. 1239 * 1240 * WARNING! Caller assumes responsibility for removing dead blocks 1241 * if INODE_RESIZED is set. 1242 */ 1243 static 1244 void 1245 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize) 1246 { 1247 hammer2_key_t lbase; 1248 int nblksize; 1249 1250 hammer2_mtx_unlock(&ip->lock); 1251 if (ip->vp) { 1252 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL); 1253 nvtruncbuf(ip->vp, nsize, 1254 nblksize, (int)nsize & (nblksize - 1), 1255 0); 1256 } 1257 hammer2_mtx_ex(&ip->lock); 1258 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0); 1259 ip->osize = ip->meta.size; 1260 ip->meta.size = nsize; 1261 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); 1262 hammer2_inode_modify(ip); 1263 } 1264 1265 /* 1266 * Extend the size of a file. The inode must be locked. 1267 * 1268 * Even though the file size is changing, we do not have to set the 1269 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES 1270 * boundary. When this occurs a hammer2_inode_chain_sync() is required 1271 * to prepare the inode cluster's indirect block table, otherwise 1272 * async execution of the strategy code will implode on us. 1273 * 1274 * WARNING! Assumes that the kernel interlocks size changes at the 1275 * vnode level. 1276 * 1277 * WARNING! Caller assumes responsibility for transitioning out 1278 * of the inode DIRECTDATA mode if INODE_RESIZED is set. 1279 */ 1280 static 1281 void 1282 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize) 1283 { 1284 hammer2_key_t lbase; 1285 hammer2_key_t osize; 1286 int oblksize; 1287 int nblksize; 1288 int error; 1289 1290 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0); 1291 hammer2_inode_modify(ip); 1292 osize = ip->meta.size; 1293 ip->osize = osize; 1294 ip->meta.size = nsize; 1295 1296 /* 1297 * We must issue a chain_sync() when the DIRECTDATA state changes 1298 * to prevent confusion between the flush code and the in-memory 1299 * state. This is not perfect because we are doing it outside of 1300 * a sync/fsync operation, so it might not be fully synchronized 1301 * with the meta-data topology flush. 1302 * 1303 * We must retain and re-dirty the buffer cache buffer containing 1304 * the direct data so it can be written to a real block. It should 1305 * not be possible for a bread error to occur since the original data 1306 * is extracted from the inode structure directly. 1307 */ 1308 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) { 1309 if (osize) { 1310 struct buf *bp; 1311 1312 oblksize = hammer2_calc_logical(ip, 0, NULL, NULL); 1313 error = bread_kvabio(ip->vp, 0, oblksize, &bp); 1314 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); 1315 hammer2_inode_chain_sync(ip); 1316 if (error == 0) { 1317 bheavy(bp); 1318 bdwrite(bp); 1319 } else { 1320 brelse(bp); 1321 } 1322 } else { 1323 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); 1324 hammer2_inode_chain_sync(ip); 1325 } 1326 } 1327 hammer2_mtx_unlock(&ip->lock); 1328 if (ip->vp) { 1329 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL); 1330 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL); 1331 nvextendbuf(ip->vp, 1332 osize, nsize, 1333 oblksize, nblksize, 1334 -1, -1, 0); 1335 } 1336 hammer2_mtx_ex(&ip->lock); 1337 } 1338 1339 static 1340 int 1341 hammer2_vop_nresolve(struct vop_nresolve_args *ap) 1342 { 1343 hammer2_xop_nresolve_t *xop; 1344 hammer2_inode_t *ip; 1345 hammer2_inode_t *dip; 1346 struct namecache *ncp; 1347 struct vnode *vp; 1348 int error; 1349 1350 dip = VTOI(ap->a_dvp); 1351 xop = hammer2_xop_alloc(dip, 0); 1352 1353 ncp = ap->a_nch->ncp; 1354 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 1355 1356 /* 1357 * Note: In DragonFly the kernel handles '.' and '..'. 1358 */ 1359 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED); 1360 hammer2_xop_start(&xop->head, &hammer2_nresolve_desc); 1361 1362 error = hammer2_xop_collect(&xop->head, 0); 1363 error = hammer2_error_to_errno(error); 1364 if (error) { 1365 ip = NULL; 1366 } else { 1367 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 1368 } 1369 hammer2_inode_unlock(dip); 1370 1371 /* 1372 * Acquire the related vnode 1373 * 1374 * NOTE: For error processing, only ENOENT resolves the namecache 1375 * entry to NULL, otherwise we just return the error and 1376 * leave the namecache unresolved. 1377 * 1378 * NOTE: multiple hammer2_inode structures can be aliased to the 1379 * same chain element, for example for hardlinks. This 1380 * use case does not 'reattach' inode associations that 1381 * might already exist, but always allocates a new one. 1382 * 1383 * WARNING: inode structure is locked exclusively via inode_get 1384 * but chain was locked shared. inode_unlock() 1385 * will handle it properly. 1386 */ 1387 if (ip) { 1388 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */ 1389 if (error == 0) { 1390 vn_unlock(vp); 1391 cache_setvp(ap->a_nch, vp); 1392 } else if (error == ENOENT) { 1393 cache_setvp(ap->a_nch, NULL); 1394 } 1395 hammer2_inode_unlock(ip); 1396 1397 /* 1398 * The vp should not be released until after we've disposed 1399 * of our locks, because it might cause vop_inactive() to 1400 * be called. 1401 */ 1402 if (vp) 1403 vrele(vp); 1404 } else { 1405 error = ENOENT; 1406 cache_setvp(ap->a_nch, NULL); 1407 } 1408 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1409 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL, 1410 ("resolve error %d/%p ap %p\n", 1411 error, ap->a_nch->ncp->nc_vp, ap)); 1412 1413 return error; 1414 } 1415 1416 static 1417 int 1418 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) 1419 { 1420 hammer2_inode_t *dip; 1421 hammer2_tid_t inum; 1422 int error; 1423 1424 dip = VTOI(ap->a_dvp); 1425 inum = dip->meta.iparent; 1426 *ap->a_vpp = NULL; 1427 1428 if (inum) { 1429 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL, 1430 inum, ap->a_vpp); 1431 } else { 1432 error = ENOENT; 1433 } 1434 return error; 1435 } 1436 1437 static 1438 int 1439 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap) 1440 { 1441 hammer2_inode_t *dip; 1442 hammer2_inode_t *nip; 1443 struct namecache *ncp; 1444 const uint8_t *name; 1445 size_t name_len; 1446 hammer2_tid_t inum; 1447 int error; 1448 1449 dip = VTOI(ap->a_dvp); 1450 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1451 return (EROFS); 1452 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1453 return (ENOSPC); 1454 1455 ncp = ap->a_nch->ncp; 1456 name = ncp->nc_name; 1457 name_len = ncp->nc_nlen; 1458 1459 hammer2_trans_init(dip->pmp, 0); 1460 1461 inum = hammer2_trans_newinum(dip->pmp); 1462 1463 /* 1464 * Create the actual inode as a hidden file in the iroot, then 1465 * create the directory entry. The creation of the actual inode 1466 * sets its nlinks to 1 which is the value we desire. 1467 * 1468 * dip must be locked before nip to avoid deadlock. 1469 */ 1470 hammer2_inode_lock(dip, 0); 1471 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1472 inum, &error); 1473 if (error) { 1474 error = hammer2_error_to_errno(error); 1475 } else { 1476 error = hammer2_dirent_create(dip, name, name_len, 1477 nip->meta.inum, nip->meta.type); 1478 /* returns UNIX error code */ 1479 } 1480 if (error) { 1481 if (nip) { 1482 hammer2_inode_unlink_finisher(nip, NULL); 1483 hammer2_inode_unlock(nip); 1484 nip = NULL; 1485 } 1486 *ap->a_vpp = NULL; 1487 } else { 1488 /* 1489 * inode_depend() must occur before the igetv() because 1490 * the igetv() can temporarily release the inode lock. 1491 */ 1492 hammer2_inode_depend(dip, nip); /* before igetv */ 1493 *ap->a_vpp = hammer2_igetv(nip, &error); 1494 hammer2_inode_unlock(nip); 1495 } 1496 1497 /* 1498 * Update dip's mtime 1499 * 1500 * We can use a shared inode lock and allow the meta.mtime update 1501 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock. 1502 */ 1503 if (error == 0) { 1504 uint64_t mtime; 1505 1506 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1507 hammer2_update_time(&mtime); 1508 hammer2_inode_modify(dip); 1509 dip->meta.mtime = mtime; 1510 /*hammer2_inode_unlock(dip);*/ 1511 } 1512 hammer2_inode_unlock(dip); 1513 1514 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1515 1516 if (error == 0) { 1517 cache_setunresolved(ap->a_nch); 1518 cache_setvp(ap->a_nch, *ap->a_vpp); 1519 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 1520 } 1521 return error; 1522 } 1523 1524 static 1525 int 1526 hammer2_vop_open(struct vop_open_args *ap) 1527 { 1528 return vop_stdopen(ap); 1529 } 1530 1531 /* 1532 * hammer2_vop_advlock { vp, id, op, fl, flags } 1533 */ 1534 static 1535 int 1536 hammer2_vop_advlock(struct vop_advlock_args *ap) 1537 { 1538 hammer2_inode_t *ip = VTOI(ap->a_vp); 1539 hammer2_off_t size; 1540 1541 size = ip->meta.size; 1542 return (lf_advlock(ap, &ip->advlock, size)); 1543 } 1544 1545 static 1546 int 1547 hammer2_vop_close(struct vop_close_args *ap) 1548 { 1549 return vop_stdclose(ap); 1550 } 1551 1552 /* 1553 * hammer2_vop_nlink { nch, dvp, vp, cred } 1554 * 1555 * Create a hardlink from (vp) to {dvp, nch}. 1556 */ 1557 static 1558 int 1559 hammer2_vop_nlink(struct vop_nlink_args *ap) 1560 { 1561 hammer2_inode_t *tdip; /* target directory to create link in */ 1562 hammer2_inode_t *ip; /* inode we are hardlinking to */ 1563 struct namecache *ncp; 1564 const uint8_t *name; 1565 size_t name_len; 1566 int error; 1567 uint64_t cmtime; 1568 1569 if (ap->a_dvp->v_mount != ap->a_vp->v_mount) 1570 return(EXDEV); 1571 1572 tdip = VTOI(ap->a_dvp); 1573 if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG)) 1574 return (EROFS); 1575 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1) 1576 return (ENOSPC); 1577 1578 ncp = ap->a_nch->ncp; 1579 name = ncp->nc_name; 1580 name_len = ncp->nc_nlen; 1581 1582 /* 1583 * ip represents the file being hardlinked. The file could be a 1584 * normal file or a hardlink target if it has already been hardlinked. 1585 * (with the new semantics, it will almost always be a hardlink 1586 * target). 1587 * 1588 * Bump nlinks and potentially also create or move the hardlink 1589 * target in the parent directory common to (ip) and (tdip). The 1590 * consolidation code can modify ip->cluster. The returned cluster 1591 * is locked. 1592 */ 1593 ip = VTOI(ap->a_vp); 1594 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp)); 1595 hammer2_trans_init(ip->pmp, 0); 1596 1597 /* 1598 * Target should be an indexed inode or there's no way we will ever 1599 * be able to find it! 1600 */ 1601 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0); 1602 1603 error = 0; 1604 1605 /* 1606 * Can return NULL and error == EXDEV if the common parent 1607 * crosses a directory with the xlink flag set. 1608 */ 1609 hammer2_inode_lock4(tdip, ip, NULL, NULL); 1610 1611 hammer2_update_time(&cmtime); 1612 1613 /* 1614 * Create the directory entry and bump nlinks. 1615 * Also update ip's ctime. 1616 */ 1617 if (error == 0) { 1618 error = hammer2_dirent_create(tdip, name, name_len, 1619 ip->meta.inum, ip->meta.type); 1620 hammer2_inode_modify(ip); 1621 ++ip->meta.nlinks; 1622 ip->meta.ctime = cmtime; 1623 } 1624 if (error == 0) { 1625 /* 1626 * Update dip's [cm]time 1627 */ 1628 hammer2_inode_modify(tdip); 1629 tdip->meta.mtime = cmtime; 1630 tdip->meta.ctime = cmtime; 1631 1632 cache_setunresolved(ap->a_nch); 1633 cache_setvp(ap->a_nch, ap->a_vp); 1634 } 1635 hammer2_inode_unlock(ip); 1636 hammer2_inode_unlock(tdip); 1637 1638 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); 1639 hammer2_knote(ap->a_vp, NOTE_LINK); 1640 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1641 1642 return error; 1643 } 1644 1645 /* 1646 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap } 1647 * 1648 * The operating system has already ensured that the directory entry 1649 * does not exist and done all appropriate namespace locking. 1650 */ 1651 static 1652 int 1653 hammer2_vop_ncreate(struct vop_ncreate_args *ap) 1654 { 1655 hammer2_inode_t *dip; 1656 hammer2_inode_t *nip; 1657 struct namecache *ncp; 1658 const uint8_t *name; 1659 size_t name_len; 1660 hammer2_tid_t inum; 1661 int error; 1662 1663 dip = VTOI(ap->a_dvp); 1664 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1665 return (EROFS); 1666 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1667 return (ENOSPC); 1668 1669 ncp = ap->a_nch->ncp; 1670 name = ncp->nc_name; 1671 name_len = ncp->nc_nlen; 1672 hammer2_trans_init(dip->pmp, 0); 1673 1674 inum = hammer2_trans_newinum(dip->pmp); 1675 1676 /* 1677 * Create the actual inode as a hidden file in the iroot, then 1678 * create the directory entry. The creation of the actual inode 1679 * sets its nlinks to 1 which is the value we desire. 1680 * 1681 * dip must be locked before nip to avoid deadlock. 1682 */ 1683 hammer2_inode_lock(dip, 0); 1684 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1685 inum, &error); 1686 1687 if (error) { 1688 error = hammer2_error_to_errno(error); 1689 } else { 1690 error = hammer2_dirent_create(dip, name, name_len, 1691 nip->meta.inum, nip->meta.type); 1692 } 1693 if (error) { 1694 if (nip) { 1695 hammer2_inode_unlink_finisher(nip, NULL); 1696 hammer2_inode_unlock(nip); 1697 nip = NULL; 1698 } 1699 *ap->a_vpp = NULL; 1700 } else { 1701 hammer2_inode_depend(dip, nip); /* before igetv */ 1702 *ap->a_vpp = hammer2_igetv(nip, &error); 1703 hammer2_inode_unlock(nip); 1704 } 1705 1706 /* 1707 * Update dip's mtime 1708 */ 1709 if (error == 0) { 1710 uint64_t mtime; 1711 1712 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1713 hammer2_update_time(&mtime); 1714 hammer2_inode_modify(dip); 1715 dip->meta.mtime = mtime; 1716 /*hammer2_inode_unlock(dip);*/ 1717 } 1718 hammer2_inode_unlock(dip); 1719 1720 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1721 1722 if (error == 0) { 1723 cache_setunresolved(ap->a_nch); 1724 cache_setvp(ap->a_nch, *ap->a_vpp); 1725 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1726 } 1727 return error; 1728 } 1729 1730 /* 1731 * Make a device node (typically a fifo) 1732 */ 1733 static 1734 int 1735 hammer2_vop_nmknod(struct vop_nmknod_args *ap) 1736 { 1737 hammer2_inode_t *dip; 1738 hammer2_inode_t *nip; 1739 struct namecache *ncp; 1740 const uint8_t *name; 1741 size_t name_len; 1742 hammer2_tid_t inum; 1743 int error; 1744 1745 dip = VTOI(ap->a_dvp); 1746 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1747 return (EROFS); 1748 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1749 return (ENOSPC); 1750 1751 ncp = ap->a_nch->ncp; 1752 name = ncp->nc_name; 1753 name_len = ncp->nc_nlen; 1754 hammer2_trans_init(dip->pmp, 0); 1755 1756 /* 1757 * Create the device inode and then create the directory entry. 1758 * 1759 * dip must be locked before nip to avoid deadlock. 1760 */ 1761 inum = hammer2_trans_newinum(dip->pmp); 1762 1763 hammer2_inode_lock(dip, 0); 1764 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1765 inum, &error); 1766 if (error == 0) { 1767 error = hammer2_dirent_create(dip, name, name_len, 1768 nip->meta.inum, nip->meta.type); 1769 } 1770 if (error) { 1771 if (nip) { 1772 hammer2_inode_unlink_finisher(nip, NULL); 1773 hammer2_inode_unlock(nip); 1774 nip = NULL; 1775 } 1776 *ap->a_vpp = NULL; 1777 } else { 1778 hammer2_inode_depend(dip, nip); /* before igetv */ 1779 *ap->a_vpp = hammer2_igetv(nip, &error); 1780 hammer2_inode_unlock(nip); 1781 } 1782 1783 /* 1784 * Update dip's mtime 1785 */ 1786 if (error == 0) { 1787 uint64_t mtime; 1788 1789 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1790 hammer2_update_time(&mtime); 1791 hammer2_inode_modify(dip); 1792 dip->meta.mtime = mtime; 1793 /*hammer2_inode_unlock(dip);*/ 1794 } 1795 hammer2_inode_unlock(dip); 1796 1797 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1798 1799 if (error == 0) { 1800 cache_setunresolved(ap->a_nch); 1801 cache_setvp(ap->a_nch, *ap->a_vpp); 1802 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1803 } 1804 return error; 1805 } 1806 1807 /* 1808 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target } 1809 */ 1810 static 1811 int 1812 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap) 1813 { 1814 hammer2_inode_t *dip; 1815 hammer2_inode_t *nip; 1816 struct namecache *ncp; 1817 const uint8_t *name; 1818 size_t name_len; 1819 hammer2_tid_t inum; 1820 int error; 1821 1822 dip = VTOI(ap->a_dvp); 1823 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1824 return (EROFS); 1825 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1826 return (ENOSPC); 1827 1828 ncp = ap->a_nch->ncp; 1829 name = ncp->nc_name; 1830 name_len = ncp->nc_nlen; 1831 hammer2_trans_init(dip->pmp, 0); 1832 1833 ap->a_vap->va_type = VLNK; /* enforce type */ 1834 1835 /* 1836 * Create the softlink as an inode and then create the directory 1837 * entry. 1838 * 1839 * dip must be locked before nip to avoid deadlock. 1840 */ 1841 inum = hammer2_trans_newinum(dip->pmp); 1842 1843 hammer2_inode_lock(dip, 0); 1844 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1845 inum, &error); 1846 if (error == 0) { 1847 error = hammer2_dirent_create(dip, name, name_len, 1848 nip->meta.inum, nip->meta.type); 1849 } 1850 if (error) { 1851 if (nip) { 1852 hammer2_inode_unlink_finisher(nip, NULL); 1853 hammer2_inode_unlock(nip); 1854 nip = NULL; 1855 } 1856 *ap->a_vpp = NULL; 1857 hammer2_inode_unlock(dip); 1858 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1859 return error; 1860 } 1861 hammer2_inode_depend(dip, nip); /* before igetv */ 1862 *ap->a_vpp = hammer2_igetv(nip, &error); 1863 1864 /* 1865 * Build the softlink (~like file data) and finalize the namecache. 1866 */ 1867 if (error == 0) { 1868 size_t bytes; 1869 struct uio auio; 1870 struct iovec aiov; 1871 1872 bytes = strlen(ap->a_target); 1873 1874 hammer2_inode_unlock(nip); 1875 bzero(&auio, sizeof(auio)); 1876 bzero(&aiov, sizeof(aiov)); 1877 auio.uio_iov = &aiov; 1878 auio.uio_segflg = UIO_SYSSPACE; 1879 auio.uio_rw = UIO_WRITE; 1880 auio.uio_resid = bytes; 1881 auio.uio_iovcnt = 1; 1882 auio.uio_td = curthread; 1883 aiov.iov_base = ap->a_target; 1884 aiov.iov_len = bytes; 1885 error = hammer2_write_file(nip, &auio, IO_APPEND, 0); 1886 /* XXX handle error */ 1887 error = 0; 1888 } else { 1889 hammer2_inode_unlock(nip); 1890 } 1891 1892 /* 1893 * Update dip's mtime 1894 */ 1895 if (error == 0) { 1896 uint64_t mtime; 1897 1898 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1899 hammer2_update_time(&mtime); 1900 hammer2_inode_modify(dip); 1901 dip->meta.mtime = mtime; 1902 /*hammer2_inode_unlock(dip);*/ 1903 } 1904 hammer2_inode_unlock(dip); 1905 1906 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1907 1908 /* 1909 * Finalize namecache 1910 */ 1911 if (error == 0) { 1912 cache_setunresolved(ap->a_nch); 1913 cache_setvp(ap->a_nch, *ap->a_vpp); 1914 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1915 } 1916 return error; 1917 } 1918 1919 /* 1920 * hammer2_vop_nremove { nch, dvp, cred } 1921 */ 1922 static 1923 int 1924 hammer2_vop_nremove(struct vop_nremove_args *ap) 1925 { 1926 hammer2_xop_unlink_t *xop; 1927 hammer2_inode_t *dip; 1928 hammer2_inode_t *ip; 1929 struct vnode *vprecycle; 1930 struct namecache *ncp; 1931 int error; 1932 1933 dip = VTOI(ap->a_dvp); 1934 if (dip->pmp->ronly) 1935 return (EROFS); 1936 #if 0 1937 /* allow removals, except user to also bulkfree */ 1938 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1939 return (ENOSPC); 1940 #endif 1941 1942 ncp = ap->a_nch->ncp; 1943 1944 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) { 1945 kprintf("hammer2: attempt to delete inside debug inode: %s\n", 1946 ncp->nc_name); 1947 while (hammer2_debug_inode && 1948 dip->meta.inum == hammer2_debug_inode) { 1949 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5); 1950 } 1951 } 1952 1953 hammer2_trans_init(dip->pmp, 0); 1954 hammer2_inode_lock(dip, 0); 1955 1956 /* 1957 * The unlink XOP unlinks the path from the directory and 1958 * locates and returns the cluster associated with the real inode. 1959 * We have to handle nlinks here on the frontend. 1960 */ 1961 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 1962 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 1963 1964 xop->isdir = 0; 1965 xop->dopermanent = 0; 1966 hammer2_xop_start(&xop->head, &hammer2_unlink_desc); 1967 1968 /* 1969 * Collect the real inode and adjust nlinks, destroy the real 1970 * inode if nlinks transitions to 0 and it was the real inode 1971 * (else it has already been removed). 1972 */ 1973 error = hammer2_xop_collect(&xop->head, 0); 1974 error = hammer2_error_to_errno(error); 1975 vprecycle = NULL; 1976 1977 if (error == 0) { 1978 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 1979 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1980 if (ip) { 1981 if (hammer2_debug_inode && 1982 ip->meta.inum == hammer2_debug_inode) { 1983 kprintf("hammer2: attempt to delete debug " 1984 "inode!\n"); 1985 while (hammer2_debug_inode && 1986 ip->meta.inum == hammer2_debug_inode) { 1987 tsleep(&hammer2_debug_inode, 0, 1988 "h2debug", hz*5); 1989 } 1990 } 1991 hammer2_inode_unlink_finisher(ip, &vprecycle); 1992 hammer2_inode_depend(dip, ip); /* after modified */ 1993 hammer2_inode_unlock(ip); 1994 } 1995 } else { 1996 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1997 } 1998 1999 /* 2000 * Update dip's mtime 2001 */ 2002 if (error == 0) { 2003 uint64_t mtime; 2004 2005 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 2006 hammer2_update_time(&mtime); 2007 hammer2_inode_modify(dip); 2008 dip->meta.mtime = mtime; 2009 /*hammer2_inode_unlock(dip);*/ 2010 } 2011 hammer2_inode_unlock(dip); 2012 2013 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 2014 if (error == 0) { 2015 cache_unlink(ap->a_nch); 2016 hammer2_knote(ap->a_dvp, NOTE_WRITE); 2017 } 2018 if (vprecycle) 2019 hammer2_inode_vprecycle(vprecycle); 2020 2021 return (error); 2022 } 2023 2024 /* 2025 * hammer2_vop_nrmdir { nch, dvp, cred } 2026 */ 2027 static 2028 int 2029 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap) 2030 { 2031 hammer2_xop_unlink_t *xop; 2032 hammer2_inode_t *dip; 2033 hammer2_inode_t *ip; 2034 struct namecache *ncp; 2035 struct vnode *vprecycle; 2036 int error; 2037 2038 dip = VTOI(ap->a_dvp); 2039 if (dip->pmp->ronly) 2040 return (EROFS); 2041 #if 0 2042 /* allow removals, except user to also bulkfree */ 2043 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 2044 return (ENOSPC); 2045 #endif 2046 2047 hammer2_trans_init(dip->pmp, 0); 2048 hammer2_inode_lock(dip, 0); 2049 2050 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 2051 2052 ncp = ap->a_nch->ncp; 2053 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 2054 xop->isdir = 1; 2055 xop->dopermanent = 0; 2056 hammer2_xop_start(&xop->head, &hammer2_unlink_desc); 2057 2058 /* 2059 * Collect the real inode and adjust nlinks, destroy the real 2060 * inode if nlinks transitions to 0 and it was the real inode 2061 * (else it has already been removed). 2062 */ 2063 error = hammer2_xop_collect(&xop->head, 0); 2064 error = hammer2_error_to_errno(error); 2065 vprecycle = NULL; 2066 2067 if (error == 0) { 2068 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 2069 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2070 if (ip) { 2071 hammer2_inode_unlink_finisher(ip, &vprecycle); 2072 hammer2_inode_depend(dip, ip); /* after modified */ 2073 hammer2_inode_unlock(ip); 2074 } 2075 } else { 2076 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2077 } 2078 2079 /* 2080 * Update dip's mtime 2081 */ 2082 if (error == 0) { 2083 uint64_t mtime; 2084 2085 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 2086 hammer2_update_time(&mtime); 2087 hammer2_inode_modify(dip); 2088 dip->meta.mtime = mtime; 2089 /*hammer2_inode_unlock(dip);*/ 2090 } 2091 hammer2_inode_unlock(dip); 2092 2093 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 2094 if (error == 0) { 2095 cache_unlink(ap->a_nch); 2096 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 2097 } 2098 if (vprecycle) 2099 hammer2_inode_vprecycle(vprecycle); 2100 return (error); 2101 } 2102 2103 /* 2104 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred } 2105 */ 2106 static 2107 int 2108 hammer2_vop_nrename(struct vop_nrename_args *ap) 2109 { 2110 struct namecache *fncp; 2111 struct namecache *tncp; 2112 hammer2_inode_t *fdip; /* source directory */ 2113 hammer2_inode_t *tdip; /* target directory */ 2114 hammer2_inode_t *ip; /* file being renamed */ 2115 hammer2_inode_t *tip; /* replaced target during rename or NULL */ 2116 struct vnode *vprecycle; 2117 const uint8_t *fname; 2118 size_t fname_len; 2119 const uint8_t *tname; 2120 size_t tname_len; 2121 int error; 2122 int update_tdip; 2123 int update_fdip; 2124 hammer2_key_t tlhc; 2125 2126 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) 2127 return(EXDEV); 2128 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount) 2129 return(EXDEV); 2130 2131 fdip = VTOI(ap->a_fdvp); /* source directory */ 2132 tdip = VTOI(ap->a_tdvp); /* target directory */ 2133 2134 if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG)) 2135 return (EROFS); 2136 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1) 2137 return (ENOSPC); 2138 2139 fncp = ap->a_fnch->ncp; /* entry name in source */ 2140 fname = fncp->nc_name; 2141 fname_len = fncp->nc_nlen; 2142 2143 tncp = ap->a_tnch->ncp; /* entry name in target */ 2144 tname = tncp->nc_name; 2145 tname_len = tncp->nc_nlen; 2146 2147 hammer2_trans_init(tdip->pmp, 0); 2148 2149 update_tdip = 0; 2150 update_fdip = 0; 2151 2152 ip = VTOI(fncp->nc_vp); 2153 hammer2_inode_ref(ip); /* extra ref */ 2154 2155 /* 2156 * Lookup the target name to determine if a directory entry 2157 * is being overwritten. We only hold related inode locks 2158 * temporarily, the operating system is expected to protect 2159 * against rename races. 2160 */ 2161 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL; 2162 if (tip) 2163 hammer2_inode_ref(tip); /* extra ref */ 2164 2165 /* 2166 * Can return NULL and error == EXDEV if the common parent 2167 * crosses a directory with the xlink flag set. 2168 * 2169 * For now try to avoid deadlocks with a simple pointer address 2170 * test. (tip) can be NULL. 2171 */ 2172 error = 0; 2173 { 2174 hammer2_inode_t *ip1 = fdip; 2175 hammer2_inode_t *ip2 = tdip; 2176 hammer2_inode_t *ip3 = ip; 2177 hammer2_inode_t *ip4 = tip; /* may be NULL */ 2178 2179 if (fdip > tdip) { 2180 ip1 = tdip; 2181 ip2 = fdip; 2182 } 2183 if (tip && ip > tip) { 2184 ip3 = tip; 2185 ip4 = ip; 2186 } 2187 hammer2_inode_lock4(ip1, ip2, ip3, ip4); 2188 } 2189 2190 /* 2191 * Resolve the collision space for (tdip, tname, tname_len) 2192 * 2193 * tdip must be held exclusively locked to prevent races since 2194 * multiple filenames can end up in the same collision space. 2195 */ 2196 { 2197 hammer2_xop_scanlhc_t *sxop; 2198 hammer2_tid_t lhcbase; 2199 2200 tlhc = hammer2_dirhash(tname, tname_len); 2201 lhcbase = tlhc; 2202 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING); 2203 sxop->lhc = tlhc; 2204 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc); 2205 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) { 2206 if (tlhc != sxop->head.cluster.focus->bref.key) 2207 break; 2208 ++tlhc; 2209 } 2210 error = hammer2_error_to_errno(error); 2211 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP); 2212 2213 if (error) { 2214 if (error != ENOENT) 2215 goto done2; 2216 ++tlhc; 2217 error = 0; 2218 } 2219 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) { 2220 error = ENOSPC; 2221 goto done2; 2222 } 2223 } 2224 2225 /* 2226 * Ready to go, issue the rename to the backend. Note that meta-data 2227 * updates to the related inodes occur separately from the rename 2228 * operation. 2229 * 2230 * NOTE: While it is not necessary to update ip->meta.name*, doing 2231 * so aids catastrophic recovery and debugging. 2232 */ 2233 if (error == 0) { 2234 hammer2_xop_nrename_t *xop4; 2235 2236 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING); 2237 xop4->lhc = tlhc; 2238 xop4->ip_key = ip->meta.name_key; 2239 hammer2_xop_setip2(&xop4->head, ip); 2240 hammer2_xop_setip3(&xop4->head, tdip); 2241 if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) 2242 hammer2_xop_setip4(&xop4->head, tip); 2243 hammer2_xop_setname(&xop4->head, fname, fname_len); 2244 hammer2_xop_setname2(&xop4->head, tname, tname_len); 2245 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc); 2246 2247 error = hammer2_xop_collect(&xop4->head, 0); 2248 error = hammer2_error_to_errno(error); 2249 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP); 2250 2251 if (error == ENOENT) 2252 error = 0; 2253 2254 /* 2255 * Update inode meta-data. 2256 * 2257 * WARNING! The in-memory inode (ip) structure does not 2258 * maintain a copy of the inode's filename buffer. 2259 */ 2260 if (error == 0 && 2261 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) { 2262 hammer2_inode_modify(ip); 2263 ip->meta.name_len = tname_len; 2264 ip->meta.name_key = tlhc; 2265 } 2266 if (error == 0) { 2267 hammer2_inode_modify(ip); 2268 ip->meta.iparent = tdip->meta.inum; 2269 } 2270 update_fdip = 1; 2271 update_tdip = 1; 2272 } 2273 2274 done2: 2275 /* 2276 * If no error, the backend has replaced the target directory entry. 2277 * We must adjust nlinks on the original replace target if it exists. 2278 */ 2279 vprecycle = NULL; 2280 if (error == 0 && tip) { 2281 hammer2_inode_unlink_finisher(tip, &vprecycle); 2282 } 2283 2284 /* 2285 * Update directory mtimes to represent the something changed. 2286 */ 2287 if (update_fdip || update_tdip) { 2288 uint64_t mtime; 2289 2290 hammer2_update_time(&mtime); 2291 if (update_fdip) { 2292 hammer2_inode_modify(fdip); 2293 fdip->meta.mtime = mtime; 2294 } 2295 if (update_tdip) { 2296 hammer2_inode_modify(tdip); 2297 tdip->meta.mtime = mtime; 2298 } 2299 } 2300 if (tip) { 2301 hammer2_inode_unlock(tip); 2302 hammer2_inode_drop(tip); 2303 } 2304 hammer2_inode_unlock(ip); 2305 hammer2_inode_unlock(tdip); 2306 hammer2_inode_unlock(fdip); 2307 hammer2_inode_drop(ip); 2308 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ); 2309 2310 /* 2311 * Issue the namecache update after unlocking all the internal 2312 * hammer2 structures, otherwise we might deadlock. 2313 * 2314 * WARNING! The target namespace must be updated atomically, 2315 * and we depend on cache_rename() to handle that for 2316 * us. Do not do a separate cache_unlink() because 2317 * that leaves a small window of opportunity for other 2318 * threads to allocate the target namespace before we 2319 * manage to complete our rename. 2320 * 2321 * WARNING! cache_rename() (and cache_unlink()) will properly 2322 * set VREF_FINALIZE on any attached vnode. Do not 2323 * call cache_setunresolved() manually before-hand as 2324 * this will prevent the flag from being set later via 2325 * cache_rename(). If VREF_FINALIZE is not properly set 2326 * and the inode is no longer in the topology, related 2327 * chains can remain dirty indefinitely. 2328 */ 2329 if (error == 0 && tip) { 2330 /*cache_unlink(ap->a_tnch); see above */ 2331 /*cache_setunresolved(ap->a_tnch); see above */ 2332 } 2333 if (error == 0) { 2334 cache_rename(ap->a_fnch, ap->a_tnch); 2335 hammer2_knote(ap->a_fdvp, NOTE_WRITE); 2336 hammer2_knote(ap->a_tdvp, NOTE_WRITE); 2337 hammer2_knote(fncp->nc_vp, NOTE_RENAME); 2338 } 2339 if (vprecycle) 2340 hammer2_inode_vprecycle(vprecycle); 2341 2342 return (error); 2343 } 2344 2345 /* 2346 * hammer2_vop_ioctl { vp, command, data, fflag, cred } 2347 */ 2348 static 2349 int 2350 hammer2_vop_ioctl(struct vop_ioctl_args *ap) 2351 { 2352 hammer2_inode_t *ip; 2353 int error; 2354 2355 ip = VTOI(ap->a_vp); 2356 2357 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data, 2358 ap->a_fflag, ap->a_cred); 2359 return (error); 2360 } 2361 2362 static 2363 int 2364 hammer2_vop_mountctl(struct vop_mountctl_args *ap) 2365 { 2366 struct mount *mp; 2367 hammer2_pfs_t *pmp; 2368 int rc; 2369 2370 switch (ap->a_op) { 2371 case (MOUNTCTL_SET_EXPORT): 2372 mp = ap->a_head.a_ops->head.vv_mount; 2373 pmp = MPTOPMP(mp); 2374 2375 if (ap->a_ctllen != sizeof(struct export_args)) 2376 rc = (EINVAL); 2377 else 2378 rc = vfs_export(mp, &pmp->export, 2379 (const struct export_args *)ap->a_ctl); 2380 break; 2381 default: 2382 rc = vop_stdmountctl(ap); 2383 break; 2384 } 2385 return (rc); 2386 } 2387 2388 /* 2389 * KQFILTER 2390 */ 2391 static void filt_hammer2detach(struct knote *kn); 2392 static int filt_hammer2read(struct knote *kn, long hint); 2393 static int filt_hammer2write(struct knote *kn, long hint); 2394 static int filt_hammer2vnode(struct knote *kn, long hint); 2395 2396 static struct filterops hammer2read_filtops = 2397 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2398 NULL, filt_hammer2detach, filt_hammer2read }; 2399 static struct filterops hammer2write_filtops = 2400 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2401 NULL, filt_hammer2detach, filt_hammer2write }; 2402 static struct filterops hammer2vnode_filtops = 2403 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2404 NULL, filt_hammer2detach, filt_hammer2vnode }; 2405 2406 static 2407 int 2408 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap) 2409 { 2410 struct vnode *vp = ap->a_vp; 2411 struct knote *kn = ap->a_kn; 2412 2413 switch (kn->kn_filter) { 2414 case EVFILT_READ: 2415 kn->kn_fop = &hammer2read_filtops; 2416 break; 2417 case EVFILT_WRITE: 2418 kn->kn_fop = &hammer2write_filtops; 2419 break; 2420 case EVFILT_VNODE: 2421 kn->kn_fop = &hammer2vnode_filtops; 2422 break; 2423 default: 2424 return (EOPNOTSUPP); 2425 } 2426 2427 kn->kn_hook = (caddr_t)vp; 2428 2429 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 2430 2431 return(0); 2432 } 2433 2434 static void 2435 filt_hammer2detach(struct knote *kn) 2436 { 2437 struct vnode *vp = (void *)kn->kn_hook; 2438 2439 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 2440 } 2441 2442 static int 2443 filt_hammer2read(struct knote *kn, long hint) 2444 { 2445 struct vnode *vp = (void *)kn->kn_hook; 2446 hammer2_inode_t *ip = VTOI(vp); 2447 off_t off; 2448 2449 if (hint == NOTE_REVOKE) { 2450 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 2451 return(1); 2452 } 2453 off = ip->meta.size - kn->kn_fp->f_offset; 2454 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; 2455 if (kn->kn_sfflags & NOTE_OLDAPI) 2456 return(1); 2457 return (kn->kn_data != 0); 2458 } 2459 2460 2461 static int 2462 filt_hammer2write(struct knote *kn, long hint) 2463 { 2464 if (hint == NOTE_REVOKE) 2465 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 2466 kn->kn_data = 0; 2467 return (1); 2468 } 2469 2470 static int 2471 filt_hammer2vnode(struct knote *kn, long hint) 2472 { 2473 if (kn->kn_sfflags & hint) 2474 kn->kn_fflags |= hint; 2475 if (hint == NOTE_REVOKE) { 2476 kn->kn_flags |= (EV_EOF | EV_NODATA); 2477 return (1); 2478 } 2479 return (kn->kn_fflags != 0); 2480 } 2481 2482 /* 2483 * FIFO VOPS 2484 */ 2485 static 2486 int 2487 hammer2_vop_markatime(struct vop_markatime_args *ap) 2488 { 2489 hammer2_inode_t *ip; 2490 struct vnode *vp; 2491 2492 vp = ap->a_vp; 2493 ip = VTOI(vp); 2494 2495 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG)) 2496 return (EROFS); 2497 return(0); 2498 } 2499 2500 static 2501 int 2502 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap) 2503 { 2504 int error; 2505 2506 error = VOCALL(&fifo_vnode_vops, &ap->a_head); 2507 if (error) 2508 error = hammer2_vop_kqfilter(ap); 2509 return(error); 2510 } 2511 2512 /* 2513 * VOPS vector 2514 */ 2515 struct vop_ops hammer2_vnode_vops = { 2516 .vop_default = vop_defaultop, 2517 .vop_fsync = hammer2_vop_fsync, 2518 .vop_getpages = vop_stdgetpages, 2519 .vop_putpages = vop_stdputpages, 2520 .vop_access = hammer2_vop_access, 2521 .vop_advlock = hammer2_vop_advlock, 2522 .vop_close = hammer2_vop_close, 2523 .vop_nlink = hammer2_vop_nlink, 2524 .vop_ncreate = hammer2_vop_ncreate, 2525 .vop_nsymlink = hammer2_vop_nsymlink, 2526 .vop_nremove = hammer2_vop_nremove, 2527 .vop_nrmdir = hammer2_vop_nrmdir, 2528 .vop_nrename = hammer2_vop_nrename, 2529 .vop_getattr = hammer2_vop_getattr, 2530 .vop_getattr_lite = hammer2_vop_getattr_lite, 2531 .vop_setattr = hammer2_vop_setattr, 2532 .vop_readdir = hammer2_vop_readdir, 2533 .vop_readlink = hammer2_vop_readlink, 2534 .vop_read = hammer2_vop_read, 2535 .vop_write = hammer2_vop_write, 2536 .vop_open = hammer2_vop_open, 2537 .vop_inactive = hammer2_vop_inactive, 2538 .vop_reclaim = hammer2_vop_reclaim, 2539 .vop_nresolve = hammer2_vop_nresolve, 2540 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot, 2541 .vop_nmkdir = hammer2_vop_nmkdir, 2542 .vop_nmknod = hammer2_vop_nmknod, 2543 .vop_ioctl = hammer2_vop_ioctl, 2544 .vop_mountctl = hammer2_vop_mountctl, 2545 .vop_bmap = hammer2_vop_bmap, 2546 .vop_strategy = hammer2_vop_strategy, 2547 .vop_kqfilter = hammer2_vop_kqfilter 2548 }; 2549 2550 struct vop_ops hammer2_spec_vops = { 2551 .vop_default = vop_defaultop, 2552 .vop_fsync = hammer2_vop_fsync, 2553 .vop_read = vop_stdnoread, 2554 .vop_write = vop_stdnowrite, 2555 .vop_access = hammer2_vop_access, 2556 .vop_close = hammer2_vop_close, 2557 .vop_markatime = hammer2_vop_markatime, 2558 .vop_getattr = hammer2_vop_getattr, 2559 .vop_inactive = hammer2_vop_inactive, 2560 .vop_reclaim = hammer2_vop_reclaim, 2561 .vop_setattr = hammer2_vop_setattr 2562 }; 2563 2564 struct vop_ops hammer2_fifo_vops = { 2565 .vop_default = fifo_vnoperate, 2566 .vop_fsync = hammer2_vop_fsync, 2567 #if 0 2568 .vop_read = hammer2_vop_fiforead, 2569 .vop_write = hammer2_vop_fifowrite, 2570 #endif 2571 .vop_access = hammer2_vop_access, 2572 #if 0 2573 .vop_close = hammer2_vop_fifoclose, 2574 #endif 2575 .vop_markatime = hammer2_vop_markatime, 2576 .vop_getattr = hammer2_vop_getattr, 2577 .vop_inactive = hammer2_vop_inactive, 2578 .vop_reclaim = hammer2_vop_reclaim, 2579 .vop_setattr = hammer2_vop_setattr, 2580 .vop_kqfilter = hammer2_vop_fifokqfilter 2581 }; 2582 2583