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) { 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 uint64_t cmtime; 1577 1578 if (ap->a_dvp->v_mount != ap->a_vp->v_mount) 1579 return(EXDEV); 1580 1581 tdip = VTOI(ap->a_dvp); 1582 if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG)) 1583 return (EROFS); 1584 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1) 1585 return (ENOSPC); 1586 1587 ncp = ap->a_nch->ncp; 1588 name = ncp->nc_name; 1589 name_len = ncp->nc_nlen; 1590 1591 /* 1592 * ip represents the file being hardlinked. The file could be a 1593 * normal file or a hardlink target if it has already been hardlinked. 1594 * (with the new semantics, it will almost always be a hardlink 1595 * target). 1596 * 1597 * Bump nlinks and potentially also create or move the hardlink 1598 * target in the parent directory common to (ip) and (tdip). The 1599 * consolidation code can modify ip->cluster. The returned cluster 1600 * is locked. 1601 */ 1602 ip = VTOI(ap->a_vp); 1603 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp)); 1604 hammer2_trans_init(ip->pmp, 0); 1605 1606 /* 1607 * Target should be an indexed inode or there's no way we will ever 1608 * be able to find it! 1609 */ 1610 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0); 1611 1612 error = 0; 1613 1614 /* 1615 * Can return NULL and error == EXDEV if the common parent 1616 * crosses a directory with the xlink flag set. 1617 */ 1618 hammer2_inode_lock4(tdip, ip, NULL, NULL); 1619 1620 hammer2_update_time(&cmtime); 1621 1622 /* 1623 * Create the directory entry and bump nlinks. 1624 * Also update ip's ctime. 1625 */ 1626 if (error == 0) { 1627 error = hammer2_dirent_create(tdip, name, name_len, 1628 ip->meta.inum, ip->meta.type); 1629 hammer2_inode_modify(ip); 1630 ++ip->meta.nlinks; 1631 ip->meta.ctime = cmtime; 1632 } 1633 if (error == 0) { 1634 /* 1635 * Update dip's [cm]time 1636 */ 1637 hammer2_inode_modify(tdip); 1638 tdip->meta.mtime = cmtime; 1639 tdip->meta.ctime = cmtime; 1640 1641 cache_setunresolved(ap->a_nch); 1642 cache_setvp(ap->a_nch, ap->a_vp); 1643 } 1644 hammer2_inode_unlock(ip); 1645 hammer2_inode_unlock(tdip); 1646 1647 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); 1648 hammer2_knote(ap->a_vp, NOTE_LINK); 1649 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1650 1651 return error; 1652 } 1653 1654 /* 1655 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap } 1656 * 1657 * The operating system has already ensured that the directory entry 1658 * does not exist and done all appropriate namespace locking. 1659 */ 1660 static 1661 int 1662 hammer2_vop_ncreate(struct vop_ncreate_args *ap) 1663 { 1664 hammer2_inode_t *dip; 1665 hammer2_inode_t *nip; 1666 struct namecache *ncp; 1667 const uint8_t *name; 1668 size_t name_len; 1669 hammer2_tid_t inum; 1670 int error; 1671 1672 dip = VTOI(ap->a_dvp); 1673 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1674 return (EROFS); 1675 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1676 return (ENOSPC); 1677 1678 ncp = ap->a_nch->ncp; 1679 name = ncp->nc_name; 1680 name_len = ncp->nc_nlen; 1681 hammer2_trans_init(dip->pmp, 0); 1682 1683 inum = hammer2_trans_newinum(dip->pmp); 1684 1685 /* 1686 * Create the actual inode as a hidden file in the iroot, then 1687 * create the directory entry. The creation of the actual inode 1688 * sets its nlinks to 1 which is the value we desire. 1689 * 1690 * dip must be locked before nip to avoid deadlock. 1691 */ 1692 hammer2_inode_lock(dip, 0); 1693 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1694 inum, &error); 1695 1696 if (error) { 1697 error = hammer2_error_to_errno(error); 1698 } else { 1699 error = hammer2_dirent_create(dip, name, name_len, 1700 nip->meta.inum, nip->meta.type); 1701 } 1702 if (error) { 1703 if (nip) { 1704 hammer2_inode_unlink_finisher(nip, 0); 1705 hammer2_inode_unlock(nip); 1706 nip = NULL; 1707 } 1708 *ap->a_vpp = NULL; 1709 } else { 1710 hammer2_inode_depend(dip, nip); /* before igetv */ 1711 *ap->a_vpp = hammer2_igetv(nip, &error); 1712 hammer2_inode_unlock(nip); 1713 } 1714 1715 /* 1716 * Update dip's mtime 1717 */ 1718 if (error == 0) { 1719 uint64_t mtime; 1720 1721 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1722 hammer2_update_time(&mtime); 1723 hammer2_inode_modify(dip); 1724 dip->meta.mtime = mtime; 1725 /*hammer2_inode_unlock(dip);*/ 1726 } 1727 hammer2_inode_unlock(dip); 1728 1729 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1730 1731 if (error == 0) { 1732 cache_setunresolved(ap->a_nch); 1733 cache_setvp(ap->a_nch, *ap->a_vpp); 1734 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1735 } 1736 return error; 1737 } 1738 1739 /* 1740 * Make a device node (typically a fifo) 1741 */ 1742 static 1743 int 1744 hammer2_vop_nmknod(struct vop_nmknod_args *ap) 1745 { 1746 hammer2_inode_t *dip; 1747 hammer2_inode_t *nip; 1748 struct namecache *ncp; 1749 const uint8_t *name; 1750 size_t name_len; 1751 hammer2_tid_t inum; 1752 int error; 1753 1754 dip = VTOI(ap->a_dvp); 1755 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1756 return (EROFS); 1757 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1758 return (ENOSPC); 1759 1760 ncp = ap->a_nch->ncp; 1761 name = ncp->nc_name; 1762 name_len = ncp->nc_nlen; 1763 hammer2_trans_init(dip->pmp, 0); 1764 1765 /* 1766 * Create the device inode and then create the directory entry. 1767 * 1768 * dip must be locked before nip to avoid deadlock. 1769 */ 1770 inum = hammer2_trans_newinum(dip->pmp); 1771 1772 hammer2_inode_lock(dip, 0); 1773 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1774 inum, &error); 1775 if (error == 0) { 1776 error = hammer2_dirent_create(dip, name, name_len, 1777 nip->meta.inum, nip->meta.type); 1778 } 1779 if (error) { 1780 if (nip) { 1781 hammer2_inode_unlink_finisher(nip, 0); 1782 hammer2_inode_unlock(nip); 1783 nip = NULL; 1784 } 1785 *ap->a_vpp = NULL; 1786 } else { 1787 hammer2_inode_depend(dip, nip); /* before igetv */ 1788 *ap->a_vpp = hammer2_igetv(nip, &error); 1789 hammer2_inode_unlock(nip); 1790 } 1791 1792 /* 1793 * Update dip's mtime 1794 */ 1795 if (error == 0) { 1796 uint64_t mtime; 1797 1798 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1799 hammer2_update_time(&mtime); 1800 hammer2_inode_modify(dip); 1801 dip->meta.mtime = mtime; 1802 /*hammer2_inode_unlock(dip);*/ 1803 } 1804 hammer2_inode_unlock(dip); 1805 1806 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1807 1808 if (error == 0) { 1809 cache_setunresolved(ap->a_nch); 1810 cache_setvp(ap->a_nch, *ap->a_vpp); 1811 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1812 } 1813 return error; 1814 } 1815 1816 /* 1817 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target } 1818 */ 1819 static 1820 int 1821 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap) 1822 { 1823 hammer2_inode_t *dip; 1824 hammer2_inode_t *nip; 1825 struct namecache *ncp; 1826 const uint8_t *name; 1827 size_t name_len; 1828 hammer2_tid_t inum; 1829 int error; 1830 1831 dip = VTOI(ap->a_dvp); 1832 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1833 return (EROFS); 1834 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1835 return (ENOSPC); 1836 1837 ncp = ap->a_nch->ncp; 1838 name = ncp->nc_name; 1839 name_len = ncp->nc_nlen; 1840 hammer2_trans_init(dip->pmp, 0); 1841 1842 ap->a_vap->va_type = VLNK; /* enforce type */ 1843 1844 /* 1845 * Create the softlink as an inode and then create the directory 1846 * entry. 1847 * 1848 * dip must be locked before nip to avoid deadlock. 1849 */ 1850 inum = hammer2_trans_newinum(dip->pmp); 1851 1852 hammer2_inode_lock(dip, 0); 1853 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1854 inum, &error); 1855 if (error == 0) { 1856 error = hammer2_dirent_create(dip, name, name_len, 1857 nip->meta.inum, nip->meta.type); 1858 } 1859 if (error) { 1860 if (nip) { 1861 hammer2_inode_unlink_finisher(nip, 0); 1862 hammer2_inode_unlock(nip); 1863 nip = NULL; 1864 } 1865 *ap->a_vpp = NULL; 1866 hammer2_inode_unlock(dip); 1867 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1868 return error; 1869 } 1870 hammer2_inode_depend(dip, nip); /* before igetv */ 1871 *ap->a_vpp = hammer2_igetv(nip, &error); 1872 1873 /* 1874 * Build the softlink (~like file data) and finalize the namecache. 1875 */ 1876 if (error == 0) { 1877 size_t bytes; 1878 struct uio auio; 1879 struct iovec aiov; 1880 1881 bytes = strlen(ap->a_target); 1882 1883 hammer2_inode_unlock(nip); 1884 bzero(&auio, sizeof(auio)); 1885 bzero(&aiov, sizeof(aiov)); 1886 auio.uio_iov = &aiov; 1887 auio.uio_segflg = UIO_SYSSPACE; 1888 auio.uio_rw = UIO_WRITE; 1889 auio.uio_resid = bytes; 1890 auio.uio_iovcnt = 1; 1891 auio.uio_td = curthread; 1892 aiov.iov_base = ap->a_target; 1893 aiov.iov_len = bytes; 1894 error = hammer2_write_file(nip, &auio, IO_APPEND, 0); 1895 /* XXX handle error */ 1896 error = 0; 1897 } else { 1898 hammer2_inode_unlock(nip); 1899 } 1900 1901 /* 1902 * Update dip's mtime 1903 */ 1904 if (error == 0) { 1905 uint64_t mtime; 1906 1907 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1908 hammer2_update_time(&mtime); 1909 hammer2_inode_modify(dip); 1910 dip->meta.mtime = mtime; 1911 /*hammer2_inode_unlock(dip);*/ 1912 } 1913 hammer2_inode_unlock(dip); 1914 1915 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1916 1917 /* 1918 * Finalize namecache 1919 */ 1920 if (error == 0) { 1921 cache_setunresolved(ap->a_nch); 1922 cache_setvp(ap->a_nch, *ap->a_vpp); 1923 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1924 } 1925 return error; 1926 } 1927 1928 /* 1929 * hammer2_vop_nremove { nch, dvp, cred } 1930 */ 1931 static 1932 int 1933 hammer2_vop_nremove(struct vop_nremove_args *ap) 1934 { 1935 hammer2_xop_unlink_t *xop; 1936 hammer2_inode_t *dip; 1937 hammer2_inode_t *ip; 1938 struct namecache *ncp; 1939 int error; 1940 int isopen; 1941 1942 dip = VTOI(ap->a_dvp); 1943 if (dip->pmp->ronly) 1944 return (EROFS); 1945 #if 0 1946 /* allow removals, except user to also bulkfree */ 1947 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1948 return (ENOSPC); 1949 #endif 1950 1951 ncp = ap->a_nch->ncp; 1952 1953 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) { 1954 kprintf("hammer2: attempt to delete inside debug inode: %s\n", 1955 ncp->nc_name); 1956 while (hammer2_debug_inode && 1957 dip->meta.inum == hammer2_debug_inode) { 1958 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5); 1959 } 1960 } 1961 1962 hammer2_trans_init(dip->pmp, 0); 1963 hammer2_inode_lock(dip, 0); 1964 1965 /* 1966 * The unlink XOP unlinks the path from the directory and 1967 * locates and returns the cluster associated with the real inode. 1968 * We have to handle nlinks here on the frontend. 1969 */ 1970 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 1971 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 1972 1973 /* 1974 * The namecache entry is locked so nobody can use this namespace. 1975 * Calculate isopen to determine if this namespace has an open vp 1976 * associated with it and resolve the vp only if it does. 1977 * 1978 * We try to avoid resolving the vnode if nobody has it open, but 1979 * note that the test is via this namespace only. 1980 */ 1981 isopen = cache_isopen(ap->a_nch); 1982 xop->isdir = 0; 1983 xop->dopermanent = 0; 1984 hammer2_xop_start(&xop->head, &hammer2_unlink_desc); 1985 1986 /* 1987 * Collect the real inode and adjust nlinks, destroy the real 1988 * inode if nlinks transitions to 0 and it was the real inode 1989 * (else it has already been removed). 1990 */ 1991 error = hammer2_xop_collect(&xop->head, 0); 1992 error = hammer2_error_to_errno(error); 1993 1994 if (error == 0) { 1995 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 1996 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1997 if (ip) { 1998 if (hammer2_debug_inode && 1999 ip->meta.inum == hammer2_debug_inode) { 2000 kprintf("hammer2: attempt to delete debug " 2001 "inode!\n"); 2002 while (hammer2_debug_inode && 2003 ip->meta.inum == hammer2_debug_inode) { 2004 tsleep(&hammer2_debug_inode, 0, 2005 "h2debug", hz*5); 2006 } 2007 } 2008 hammer2_inode_unlink_finisher(ip, isopen); 2009 hammer2_inode_depend(dip, ip); /* after modified */ 2010 hammer2_inode_unlock(ip); 2011 } 2012 } else { 2013 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2014 } 2015 2016 /* 2017 * Update dip's mtime 2018 */ 2019 if (error == 0) { 2020 uint64_t mtime; 2021 2022 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 2023 hammer2_update_time(&mtime); 2024 hammer2_inode_modify(dip); 2025 dip->meta.mtime = mtime; 2026 /*hammer2_inode_unlock(dip);*/ 2027 } 2028 hammer2_inode_unlock(dip); 2029 2030 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 2031 if (error == 0) { 2032 cache_unlink(ap->a_nch); 2033 hammer2_knote(ap->a_dvp, NOTE_WRITE); 2034 } 2035 return (error); 2036 } 2037 2038 /* 2039 * hammer2_vop_nrmdir { nch, dvp, cred } 2040 */ 2041 static 2042 int 2043 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap) 2044 { 2045 hammer2_xop_unlink_t *xop; 2046 hammer2_inode_t *dip; 2047 hammer2_inode_t *ip; 2048 struct namecache *ncp; 2049 int isopen; 2050 int error; 2051 2052 dip = VTOI(ap->a_dvp); 2053 if (dip->pmp->ronly) 2054 return (EROFS); 2055 #if 0 2056 /* allow removals, except user to also bulkfree */ 2057 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 2058 return (ENOSPC); 2059 #endif 2060 2061 hammer2_trans_init(dip->pmp, 0); 2062 hammer2_inode_lock(dip, 0); 2063 2064 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 2065 2066 ncp = ap->a_nch->ncp; 2067 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 2068 isopen = cache_isopen(ap->a_nch); 2069 xop->isdir = 1; 2070 xop->dopermanent = 0; 2071 hammer2_xop_start(&xop->head, &hammer2_unlink_desc); 2072 2073 /* 2074 * Collect the real inode and adjust nlinks, destroy the real 2075 * inode if nlinks transitions to 0 and it was the real inode 2076 * (else it has already been removed). 2077 */ 2078 error = hammer2_xop_collect(&xop->head, 0); 2079 error = hammer2_error_to_errno(error); 2080 2081 if (error == 0) { 2082 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 2083 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2084 if (ip) { 2085 hammer2_inode_unlink_finisher(ip, isopen); 2086 hammer2_inode_depend(dip, ip); /* after modified */ 2087 hammer2_inode_unlock(ip); 2088 } 2089 } else { 2090 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2091 } 2092 2093 /* 2094 * Update dip's mtime 2095 */ 2096 if (error == 0) { 2097 uint64_t mtime; 2098 2099 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 2100 hammer2_update_time(&mtime); 2101 hammer2_inode_modify(dip); 2102 dip->meta.mtime = mtime; 2103 /*hammer2_inode_unlock(dip);*/ 2104 } 2105 hammer2_inode_unlock(dip); 2106 2107 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 2108 if (error == 0) { 2109 cache_unlink(ap->a_nch); 2110 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 2111 } 2112 return (error); 2113 } 2114 2115 /* 2116 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred } 2117 */ 2118 static 2119 int 2120 hammer2_vop_nrename(struct vop_nrename_args *ap) 2121 { 2122 struct namecache *fncp; 2123 struct namecache *tncp; 2124 hammer2_inode_t *fdip; /* source directory */ 2125 hammer2_inode_t *tdip; /* target directory */ 2126 hammer2_inode_t *ip; /* file being renamed */ 2127 hammer2_inode_t *tip; /* replaced target during rename or NULL */ 2128 const uint8_t *fname; 2129 size_t fname_len; 2130 const uint8_t *tname; 2131 size_t tname_len; 2132 int error; 2133 int update_tdip; 2134 int update_fdip; 2135 hammer2_key_t tlhc; 2136 2137 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) 2138 return(EXDEV); 2139 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount) 2140 return(EXDEV); 2141 2142 fdip = VTOI(ap->a_fdvp); /* source directory */ 2143 tdip = VTOI(ap->a_tdvp); /* target directory */ 2144 2145 if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG)) 2146 return (EROFS); 2147 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1) 2148 return (ENOSPC); 2149 2150 fncp = ap->a_fnch->ncp; /* entry name in source */ 2151 fname = fncp->nc_name; 2152 fname_len = fncp->nc_nlen; 2153 2154 tncp = ap->a_tnch->ncp; /* entry name in target */ 2155 tname = tncp->nc_name; 2156 tname_len = tncp->nc_nlen; 2157 2158 hammer2_trans_init(tdip->pmp, 0); 2159 2160 update_tdip = 0; 2161 update_fdip = 0; 2162 2163 ip = VTOI(fncp->nc_vp); 2164 hammer2_inode_ref(ip); /* extra ref */ 2165 2166 /* 2167 * Lookup the target name to determine if a directory entry 2168 * is being overwritten. We only hold related inode locks 2169 * temporarily, the operating system is expected to protect 2170 * against rename races. 2171 */ 2172 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL; 2173 if (tip) 2174 hammer2_inode_ref(tip); /* extra ref */ 2175 2176 /* 2177 * Can return NULL and error == EXDEV if the common parent 2178 * crosses a directory with the xlink flag set. 2179 * 2180 * For now try to avoid deadlocks with a simple pointer address 2181 * test. (tip) can be NULL. 2182 */ 2183 error = 0; 2184 { 2185 hammer2_inode_t *ip1 = fdip; 2186 hammer2_inode_t *ip2 = tdip; 2187 hammer2_inode_t *ip3 = ip; 2188 hammer2_inode_t *ip4 = tip; /* may be NULL */ 2189 2190 if (fdip > tdip) { 2191 ip1 = tdip; 2192 ip2 = fdip; 2193 } 2194 if (tip && ip > tip) { 2195 ip3 = tip; 2196 ip4 = ip; 2197 } 2198 hammer2_inode_lock4(ip1, ip2, ip3, ip4); 2199 } 2200 2201 /* 2202 * Resolve the collision space for (tdip, tname, tname_len) 2203 * 2204 * tdip must be held exclusively locked to prevent races since 2205 * multiple filenames can end up in the same collision space. 2206 */ 2207 { 2208 hammer2_xop_scanlhc_t *sxop; 2209 hammer2_tid_t lhcbase; 2210 2211 tlhc = hammer2_dirhash(tname, tname_len); 2212 lhcbase = tlhc; 2213 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING); 2214 sxop->lhc = tlhc; 2215 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc); 2216 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) { 2217 if (tlhc != sxop->head.cluster.focus->bref.key) 2218 break; 2219 ++tlhc; 2220 } 2221 error = hammer2_error_to_errno(error); 2222 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP); 2223 2224 if (error) { 2225 if (error != ENOENT) 2226 goto done2; 2227 ++tlhc; 2228 error = 0; 2229 } 2230 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) { 2231 error = ENOSPC; 2232 goto done2; 2233 } 2234 } 2235 2236 /* 2237 * Ready to go, issue the rename to the backend. Note that meta-data 2238 * updates to the related inodes occur separately from the rename 2239 * operation. 2240 * 2241 * NOTE: While it is not necessary to update ip->meta.name*, doing 2242 * so aids catastrophic recovery and debugging. 2243 */ 2244 if (error == 0) { 2245 hammer2_xop_nrename_t *xop4; 2246 2247 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING); 2248 xop4->lhc = tlhc; 2249 xop4->ip_key = ip->meta.name_key; 2250 hammer2_xop_setip2(&xop4->head, ip); 2251 hammer2_xop_setip3(&xop4->head, tdip); 2252 if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) 2253 hammer2_xop_setip4(&xop4->head, tip); 2254 hammer2_xop_setname(&xop4->head, fname, fname_len); 2255 hammer2_xop_setname2(&xop4->head, tname, tname_len); 2256 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc); 2257 2258 error = hammer2_xop_collect(&xop4->head, 0); 2259 error = hammer2_error_to_errno(error); 2260 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP); 2261 2262 if (error == ENOENT) 2263 error = 0; 2264 2265 /* 2266 * Update inode meta-data. 2267 * 2268 * WARNING! The in-memory inode (ip) structure does not 2269 * maintain a copy of the inode's filename buffer. 2270 */ 2271 if (error == 0 && 2272 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) { 2273 hammer2_inode_modify(ip); 2274 ip->meta.name_len = tname_len; 2275 ip->meta.name_key = tlhc; 2276 } 2277 if (error == 0) { 2278 hammer2_inode_modify(ip); 2279 ip->meta.iparent = tdip->meta.inum; 2280 } 2281 update_fdip = 1; 2282 update_tdip = 1; 2283 } 2284 2285 done2: 2286 /* 2287 * If no error, the backend has replaced the target directory entry. 2288 * We must adjust nlinks on the original replace target if it exists. 2289 */ 2290 if (error == 0 && tip) { 2291 int isopen; 2292 2293 isopen = cache_isopen(ap->a_tnch); 2294 hammer2_inode_unlink_finisher(tip, isopen); 2295 } 2296 2297 /* 2298 * Update directory mtimes to represent the something changed. 2299 */ 2300 if (update_fdip || update_tdip) { 2301 uint64_t mtime; 2302 2303 hammer2_update_time(&mtime); 2304 if (update_fdip) { 2305 hammer2_inode_modify(fdip); 2306 fdip->meta.mtime = mtime; 2307 } 2308 if (update_tdip) { 2309 hammer2_inode_modify(tdip); 2310 tdip->meta.mtime = mtime; 2311 } 2312 } 2313 if (tip) { 2314 hammer2_inode_unlock(tip); 2315 hammer2_inode_drop(tip); 2316 } 2317 hammer2_inode_unlock(ip); 2318 hammer2_inode_unlock(tdip); 2319 hammer2_inode_unlock(fdip); 2320 hammer2_inode_drop(ip); 2321 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ); 2322 2323 /* 2324 * Issue the namecache update after unlocking all the internal 2325 * hammer2 structures, otherwise we might deadlock. 2326 * 2327 * WARNING! The target namespace must be updated atomically, 2328 * and we depend on cache_rename() to handle that for 2329 * us. Do not do a separate cache_unlink() because 2330 * that leaves a small window of opportunity for other 2331 * threads to allocate the target namespace before we 2332 * manage to complete our rename. 2333 * 2334 * WARNING! cache_rename() (and cache_unlink()) will properly 2335 * set VREF_FINALIZE on any attached vnode. Do not 2336 * call cache_setunresolved() manually before-hand as 2337 * this will prevent the flag from being set later via 2338 * cache_rename(). If VREF_FINALIZE is not properly set 2339 * and the inode is no longer in the topology, related 2340 * chains can remain dirty indefinitely. 2341 */ 2342 if (error == 0 && tip) { 2343 /*cache_unlink(ap->a_tnch); see above */ 2344 /*cache_setunresolved(ap->a_tnch); see above */ 2345 } 2346 if (error == 0) { 2347 cache_rename(ap->a_fnch, ap->a_tnch); 2348 hammer2_knote(ap->a_fdvp, NOTE_WRITE); 2349 hammer2_knote(ap->a_tdvp, NOTE_WRITE); 2350 hammer2_knote(fncp->nc_vp, NOTE_RENAME); 2351 } 2352 2353 return (error); 2354 } 2355 2356 /* 2357 * hammer2_vop_ioctl { vp, command, data, fflag, cred } 2358 */ 2359 static 2360 int 2361 hammer2_vop_ioctl(struct vop_ioctl_args *ap) 2362 { 2363 hammer2_inode_t *ip; 2364 int error; 2365 2366 ip = VTOI(ap->a_vp); 2367 2368 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data, 2369 ap->a_fflag, ap->a_cred); 2370 return (error); 2371 } 2372 2373 static 2374 int 2375 hammer2_vop_mountctl(struct vop_mountctl_args *ap) 2376 { 2377 struct mount *mp; 2378 hammer2_pfs_t *pmp; 2379 int rc; 2380 2381 switch (ap->a_op) { 2382 case (MOUNTCTL_SET_EXPORT): 2383 mp = ap->a_head.a_ops->head.vv_mount; 2384 pmp = MPTOPMP(mp); 2385 2386 if (ap->a_ctllen != sizeof(struct export_args)) 2387 rc = (EINVAL); 2388 else 2389 rc = vfs_export(mp, &pmp->export, 2390 (const struct export_args *)ap->a_ctl); 2391 break; 2392 default: 2393 rc = vop_stdmountctl(ap); 2394 break; 2395 } 2396 return (rc); 2397 } 2398 2399 /* 2400 * KQFILTER 2401 */ 2402 static void filt_hammer2detach(struct knote *kn); 2403 static int filt_hammer2read(struct knote *kn, long hint); 2404 static int filt_hammer2write(struct knote *kn, long hint); 2405 static int filt_hammer2vnode(struct knote *kn, long hint); 2406 2407 static struct filterops hammer2read_filtops = 2408 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2409 NULL, filt_hammer2detach, filt_hammer2read }; 2410 static struct filterops hammer2write_filtops = 2411 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2412 NULL, filt_hammer2detach, filt_hammer2write }; 2413 static struct filterops hammer2vnode_filtops = 2414 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2415 NULL, filt_hammer2detach, filt_hammer2vnode }; 2416 2417 static 2418 int 2419 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap) 2420 { 2421 struct vnode *vp = ap->a_vp; 2422 struct knote *kn = ap->a_kn; 2423 2424 switch (kn->kn_filter) { 2425 case EVFILT_READ: 2426 kn->kn_fop = &hammer2read_filtops; 2427 break; 2428 case EVFILT_WRITE: 2429 kn->kn_fop = &hammer2write_filtops; 2430 break; 2431 case EVFILT_VNODE: 2432 kn->kn_fop = &hammer2vnode_filtops; 2433 break; 2434 default: 2435 return (EOPNOTSUPP); 2436 } 2437 2438 kn->kn_hook = (caddr_t)vp; 2439 2440 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 2441 2442 return(0); 2443 } 2444 2445 static void 2446 filt_hammer2detach(struct knote *kn) 2447 { 2448 struct vnode *vp = (void *)kn->kn_hook; 2449 2450 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 2451 } 2452 2453 static int 2454 filt_hammer2read(struct knote *kn, long hint) 2455 { 2456 struct vnode *vp = (void *)kn->kn_hook; 2457 hammer2_inode_t *ip = VTOI(vp); 2458 off_t off; 2459 2460 if (hint == NOTE_REVOKE) { 2461 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 2462 return(1); 2463 } 2464 off = ip->meta.size - kn->kn_fp->f_offset; 2465 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; 2466 if (kn->kn_sfflags & NOTE_OLDAPI) 2467 return(1); 2468 return (kn->kn_data != 0); 2469 } 2470 2471 2472 static int 2473 filt_hammer2write(struct knote *kn, long hint) 2474 { 2475 if (hint == NOTE_REVOKE) 2476 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 2477 kn->kn_data = 0; 2478 return (1); 2479 } 2480 2481 static int 2482 filt_hammer2vnode(struct knote *kn, long hint) 2483 { 2484 if (kn->kn_sfflags & hint) 2485 kn->kn_fflags |= hint; 2486 if (hint == NOTE_REVOKE) { 2487 kn->kn_flags |= (EV_EOF | EV_NODATA); 2488 return (1); 2489 } 2490 return (kn->kn_fflags != 0); 2491 } 2492 2493 /* 2494 * FIFO VOPS 2495 */ 2496 static 2497 int 2498 hammer2_vop_markatime(struct vop_markatime_args *ap) 2499 { 2500 hammer2_inode_t *ip; 2501 struct vnode *vp; 2502 2503 vp = ap->a_vp; 2504 ip = VTOI(vp); 2505 2506 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG)) 2507 return (EROFS); 2508 return(0); 2509 } 2510 2511 static 2512 int 2513 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap) 2514 { 2515 int error; 2516 2517 error = VOCALL(&fifo_vnode_vops, &ap->a_head); 2518 if (error) 2519 error = hammer2_vop_kqfilter(ap); 2520 return(error); 2521 } 2522 2523 /* 2524 * VOPS vector 2525 */ 2526 struct vop_ops hammer2_vnode_vops = { 2527 .vop_default = vop_defaultop, 2528 .vop_fsync = hammer2_vop_fsync, 2529 .vop_getpages = vop_stdgetpages, 2530 .vop_putpages = vop_stdputpages, 2531 .vop_access = hammer2_vop_access, 2532 .vop_advlock = hammer2_vop_advlock, 2533 .vop_close = hammer2_vop_close, 2534 .vop_nlink = hammer2_vop_nlink, 2535 .vop_ncreate = hammer2_vop_ncreate, 2536 .vop_nsymlink = hammer2_vop_nsymlink, 2537 .vop_nremove = hammer2_vop_nremove, 2538 .vop_nrmdir = hammer2_vop_nrmdir, 2539 .vop_nrename = hammer2_vop_nrename, 2540 .vop_getattr = hammer2_vop_getattr, 2541 .vop_getattr_lite = hammer2_vop_getattr_lite, 2542 .vop_setattr = hammer2_vop_setattr, 2543 .vop_readdir = hammer2_vop_readdir, 2544 .vop_readlink = hammer2_vop_readlink, 2545 .vop_read = hammer2_vop_read, 2546 .vop_write = hammer2_vop_write, 2547 .vop_open = hammer2_vop_open, 2548 .vop_inactive = hammer2_vop_inactive, 2549 .vop_reclaim = hammer2_vop_reclaim, 2550 .vop_nresolve = hammer2_vop_nresolve, 2551 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot, 2552 .vop_nmkdir = hammer2_vop_nmkdir, 2553 .vop_nmknod = hammer2_vop_nmknod, 2554 .vop_ioctl = hammer2_vop_ioctl, 2555 .vop_mountctl = hammer2_vop_mountctl, 2556 .vop_bmap = hammer2_vop_bmap, 2557 .vop_strategy = hammer2_vop_strategy, 2558 .vop_kqfilter = hammer2_vop_kqfilter 2559 }; 2560 2561 struct vop_ops hammer2_spec_vops = { 2562 .vop_default = vop_defaultop, 2563 .vop_fsync = hammer2_vop_fsync, 2564 .vop_read = vop_stdnoread, 2565 .vop_write = vop_stdnowrite, 2566 .vop_access = hammer2_vop_access, 2567 .vop_close = hammer2_vop_close, 2568 .vop_markatime = hammer2_vop_markatime, 2569 .vop_getattr = hammer2_vop_getattr, 2570 .vop_inactive = hammer2_vop_inactive, 2571 .vop_reclaim = hammer2_vop_reclaim, 2572 .vop_setattr = hammer2_vop_setattr 2573 }; 2574 2575 struct vop_ops hammer2_fifo_vops = { 2576 .vop_default = fifo_vnoperate, 2577 .vop_fsync = hammer2_vop_fsync, 2578 #if 0 2579 .vop_read = hammer2_vop_fiforead, 2580 .vop_write = hammer2_vop_fifowrite, 2581 #endif 2582 .vop_access = hammer2_vop_access, 2583 #if 0 2584 .vop_close = hammer2_vop_fifoclose, 2585 #endif 2586 .vop_markatime = hammer2_vop_markatime, 2587 .vop_getattr = hammer2_vop_getattr, 2588 .vop_inactive = hammer2_vop_inactive, 2589 .vop_reclaim = hammer2_vop_reclaim, 2590 .vop_setattr = hammer2_vop_setattr, 2591 .vop_kqfilter = hammer2_vop_fifokqfilter 2592 }; 2593 2594