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