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