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