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