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