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); 1133 hammer2_mtx_ex(&ip->truncate_lock); 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 1239 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0); 1240 hammer2_inode_modify(ip); 1241 osize = ip->meta.size; 1242 ip->osize = osize; 1243 ip->meta.size = nsize; 1244 1245 /* 1246 * We must issue a chain_sync() when the DIRECTDATA state changes 1247 * to prevent confusion between the flush code and the in-memory 1248 * state. This is not perfect because we are doing it outside of 1249 * a sync/fsync operation, so it might not be fully synchronized 1250 * with the meta-data topology flush. 1251 */ 1252 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) { 1253 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); 1254 hammer2_inode_chain_sync(ip); 1255 } 1256 1257 hammer2_mtx_unlock(&ip->lock); 1258 if (ip->vp) { 1259 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL); 1260 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL); 1261 nvextendbuf(ip->vp, 1262 osize, nsize, 1263 oblksize, nblksize, 1264 -1, -1, 0); 1265 } 1266 hammer2_mtx_ex(&ip->lock); 1267 } 1268 1269 static 1270 int 1271 hammer2_vop_nresolve(struct vop_nresolve_args *ap) 1272 { 1273 hammer2_xop_nresolve_t *xop; 1274 hammer2_inode_t *ip; 1275 hammer2_inode_t *dip; 1276 struct namecache *ncp; 1277 struct vnode *vp; 1278 int error; 1279 1280 dip = VTOI(ap->a_dvp); 1281 xop = hammer2_xop_alloc(dip, 0); 1282 1283 ncp = ap->a_nch->ncp; 1284 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 1285 1286 /* 1287 * Note: In DragonFly the kernel handles '.' and '..'. 1288 */ 1289 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED); 1290 hammer2_xop_start(&xop->head, &hammer2_nresolve_desc); 1291 1292 error = hammer2_xop_collect(&xop->head, 0); 1293 error = hammer2_error_to_errno(error); 1294 if (error) { 1295 ip = NULL; 1296 } else { 1297 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 1298 } 1299 hammer2_inode_unlock(dip); 1300 1301 /* 1302 * Acquire the related vnode 1303 * 1304 * NOTE: For error processing, only ENOENT resolves the namecache 1305 * entry to NULL, otherwise we just return the error and 1306 * leave the namecache unresolved. 1307 * 1308 * NOTE: multiple hammer2_inode structures can be aliased to the 1309 * same chain element, for example for hardlinks. This 1310 * use case does not 'reattach' inode associations that 1311 * might already exist, but always allocates a new one. 1312 * 1313 * WARNING: inode structure is locked exclusively via inode_get 1314 * but chain was locked shared. inode_unlock() 1315 * will handle it properly. 1316 */ 1317 if (ip) { 1318 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */ 1319 if (error == 0) { 1320 vn_unlock(vp); 1321 cache_setvp(ap->a_nch, vp); 1322 } else if (error == ENOENT) { 1323 cache_setvp(ap->a_nch, NULL); 1324 } 1325 hammer2_inode_unlock(ip); 1326 1327 /* 1328 * The vp should not be released until after we've disposed 1329 * of our locks, because it might cause vop_inactive() to 1330 * be called. 1331 */ 1332 if (vp) 1333 vrele(vp); 1334 } else { 1335 error = ENOENT; 1336 cache_setvp(ap->a_nch, NULL); 1337 } 1338 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1339 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL, 1340 ("resolve error %d/%p ap %p\n", 1341 error, ap->a_nch->ncp->nc_vp, ap)); 1342 1343 return error; 1344 } 1345 1346 static 1347 int 1348 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) 1349 { 1350 hammer2_inode_t *dip; 1351 hammer2_tid_t inum; 1352 int error; 1353 1354 dip = VTOI(ap->a_dvp); 1355 inum = dip->meta.iparent; 1356 *ap->a_vpp = NULL; 1357 1358 if (inum) { 1359 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL, 1360 inum, ap->a_vpp); 1361 } else { 1362 error = ENOENT; 1363 } 1364 return error; 1365 } 1366 1367 static 1368 int 1369 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap) 1370 { 1371 hammer2_inode_t *dip; 1372 hammer2_inode_t *nip; 1373 struct namecache *ncp; 1374 const uint8_t *name; 1375 size_t name_len; 1376 hammer2_tid_t inum; 1377 int error; 1378 1379 dip = VTOI(ap->a_dvp); 1380 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1381 return (EROFS); 1382 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1383 return (ENOSPC); 1384 1385 ncp = ap->a_nch->ncp; 1386 name = ncp->nc_name; 1387 name_len = ncp->nc_nlen; 1388 1389 /*hammer2_pfs_memory_wait(dip->pmp);*/ 1390 hammer2_trans_init(dip->pmp, 0); 1391 1392 inum = hammer2_trans_newinum(dip->pmp); 1393 1394 /* 1395 * Create the actual inode as a hidden file in the iroot, then 1396 * create the directory entry. The creation of the actual inode 1397 * sets its nlinks to 1 which is the value we desire. 1398 * 1399 * dip must be locked before nip to avoid deadlock. 1400 */ 1401 hammer2_inode_lock(dip, 0); 1402 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1403 inum, &error); 1404 if (error) { 1405 error = hammer2_error_to_errno(error); 1406 } else { 1407 error = hammer2_dirent_create(dip, name, name_len, 1408 nip->meta.inum, nip->meta.type); 1409 /* returns UNIX error code */ 1410 } 1411 if (error) { 1412 if (nip) { 1413 hammer2_inode_unlink_finisher(nip, 0); 1414 hammer2_inode_unlock(nip); 1415 nip = NULL; 1416 } 1417 *ap->a_vpp = NULL; 1418 } else { 1419 /* 1420 * inode_depend() must occur before the igetv() because 1421 * the igetv() can temporarily release the inode lock. 1422 */ 1423 hammer2_inode_depend(dip, nip); /* before igetv */ 1424 *ap->a_vpp = hammer2_igetv(nip, &error); 1425 hammer2_inode_unlock(nip); 1426 } 1427 1428 /* 1429 * Update dip's mtime 1430 * 1431 * We can use a shared inode lock and allow the meta.mtime update 1432 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock. 1433 */ 1434 if (error == 0) { 1435 uint64_t mtime; 1436 1437 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1438 hammer2_update_time(&mtime); 1439 hammer2_inode_modify(dip); 1440 dip->meta.mtime = mtime; 1441 /*hammer2_inode_unlock(dip);*/ 1442 } 1443 hammer2_inode_unlock(dip); 1444 1445 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1446 1447 if (error == 0) { 1448 cache_setunresolved(ap->a_nch); 1449 cache_setvp(ap->a_nch, *ap->a_vpp); 1450 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 1451 } 1452 return error; 1453 } 1454 1455 static 1456 int 1457 hammer2_vop_open(struct vop_open_args *ap) 1458 { 1459 return vop_stdopen(ap); 1460 } 1461 1462 /* 1463 * hammer2_vop_advlock { vp, id, op, fl, flags } 1464 */ 1465 static 1466 int 1467 hammer2_vop_advlock(struct vop_advlock_args *ap) 1468 { 1469 hammer2_inode_t *ip = VTOI(ap->a_vp); 1470 hammer2_off_t size; 1471 1472 size = ip->meta.size; 1473 return (lf_advlock(ap, &ip->advlock, size)); 1474 } 1475 1476 static 1477 int 1478 hammer2_vop_close(struct vop_close_args *ap) 1479 { 1480 return vop_stdclose(ap); 1481 } 1482 1483 /* 1484 * hammer2_vop_nlink { nch, dvp, vp, cred } 1485 * 1486 * Create a hardlink from (vp) to {dvp, nch}. 1487 */ 1488 static 1489 int 1490 hammer2_vop_nlink(struct vop_nlink_args *ap) 1491 { 1492 hammer2_inode_t *tdip; /* target directory to create link in */ 1493 hammer2_inode_t *ip; /* inode we are hardlinking to */ 1494 struct namecache *ncp; 1495 const uint8_t *name; 1496 size_t name_len; 1497 int error; 1498 1499 if (ap->a_dvp->v_mount != ap->a_vp->v_mount) 1500 return(EXDEV); 1501 1502 tdip = VTOI(ap->a_dvp); 1503 if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG)) 1504 return (EROFS); 1505 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1) 1506 return (ENOSPC); 1507 1508 ncp = ap->a_nch->ncp; 1509 name = ncp->nc_name; 1510 name_len = ncp->nc_nlen; 1511 1512 /* 1513 * ip represents the file being hardlinked. The file could be a 1514 * normal file or a hardlink target if it has already been hardlinked. 1515 * (with the new semantics, it will almost always be a hardlink 1516 * target). 1517 * 1518 * Bump nlinks and potentially also create or move the hardlink 1519 * target in the parent directory common to (ip) and (tdip). The 1520 * consolidation code can modify ip->cluster. The returned cluster 1521 * is locked. 1522 */ 1523 ip = VTOI(ap->a_vp); 1524 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp)); 1525 /*hammer2_pfs_memory_wait(ip->pmp);*/ 1526 hammer2_trans_init(ip->pmp, 0); 1527 1528 /* 1529 * Target should be an indexed inode or there's no way we will ever 1530 * be able to find it! 1531 */ 1532 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0); 1533 1534 error = 0; 1535 1536 /* 1537 * Can return NULL and error == EXDEV if the common parent 1538 * crosses a directory with the xlink flag set. 1539 */ 1540 hammer2_inode_lock4(tdip, ip, NULL, NULL); 1541 1542 /* 1543 * Create the directory entry and bump nlinks. 1544 */ 1545 if (error == 0) { 1546 error = hammer2_dirent_create(tdip, name, name_len, 1547 ip->meta.inum, ip->meta.type); 1548 hammer2_inode_modify(ip); 1549 ++ip->meta.nlinks; 1550 } 1551 if (error == 0) { 1552 /* 1553 * Update dip's mtime 1554 */ 1555 uint64_t mtime; 1556 1557 hammer2_update_time(&mtime); 1558 hammer2_inode_modify(tdip); 1559 tdip->meta.mtime = mtime; 1560 1561 cache_setunresolved(ap->a_nch); 1562 cache_setvp(ap->a_nch, ap->a_vp); 1563 } 1564 hammer2_inode_unlock(ip); 1565 hammer2_inode_unlock(tdip); 1566 1567 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); 1568 hammer2_knote(ap->a_vp, NOTE_LINK); 1569 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1570 1571 return error; 1572 } 1573 1574 /* 1575 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap } 1576 * 1577 * The operating system has already ensured that the directory entry 1578 * does not exist and done all appropriate namespace locking. 1579 */ 1580 static 1581 int 1582 hammer2_vop_ncreate(struct vop_ncreate_args *ap) 1583 { 1584 hammer2_inode_t *dip; 1585 hammer2_inode_t *nip; 1586 struct namecache *ncp; 1587 const uint8_t *name; 1588 size_t name_len; 1589 hammer2_tid_t inum; 1590 int error; 1591 1592 dip = VTOI(ap->a_dvp); 1593 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1594 return (EROFS); 1595 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1596 return (ENOSPC); 1597 1598 ncp = ap->a_nch->ncp; 1599 name = ncp->nc_name; 1600 name_len = ncp->nc_nlen; 1601 /*hammer2_pfs_memory_wait(dip->pmp);*/ 1602 hammer2_trans_init(dip->pmp, 0); 1603 1604 inum = hammer2_trans_newinum(dip->pmp); 1605 1606 /* 1607 * Create the actual inode as a hidden file in the iroot, then 1608 * create the directory entry. The creation of the actual inode 1609 * sets its nlinks to 1 which is the value we desire. 1610 * 1611 * dip must be locked before nip to avoid deadlock. 1612 */ 1613 hammer2_inode_lock(dip, 0); 1614 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1615 inum, &error); 1616 1617 if (error) { 1618 error = hammer2_error_to_errno(error); 1619 } else { 1620 error = hammer2_dirent_create(dip, name, name_len, 1621 nip->meta.inum, nip->meta.type); 1622 } 1623 if (error) { 1624 if (nip) { 1625 hammer2_inode_unlink_finisher(nip, 0); 1626 hammer2_inode_unlock(nip); 1627 nip = NULL; 1628 } 1629 *ap->a_vpp = NULL; 1630 } else { 1631 hammer2_inode_depend(dip, nip); /* before igetv */ 1632 *ap->a_vpp = hammer2_igetv(nip, &error); 1633 hammer2_inode_unlock(nip); 1634 } 1635 1636 /* 1637 * Update dip's mtime 1638 */ 1639 if (error == 0) { 1640 uint64_t mtime; 1641 1642 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1643 hammer2_update_time(&mtime); 1644 hammer2_inode_modify(dip); 1645 dip->meta.mtime = mtime; 1646 /*hammer2_inode_unlock(dip);*/ 1647 } 1648 hammer2_inode_unlock(dip); 1649 1650 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1651 1652 if (error == 0) { 1653 cache_setunresolved(ap->a_nch); 1654 cache_setvp(ap->a_nch, *ap->a_vpp); 1655 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1656 } 1657 return error; 1658 } 1659 1660 /* 1661 * Make a device node (typically a fifo) 1662 */ 1663 static 1664 int 1665 hammer2_vop_nmknod(struct vop_nmknod_args *ap) 1666 { 1667 hammer2_inode_t *dip; 1668 hammer2_inode_t *nip; 1669 struct namecache *ncp; 1670 const uint8_t *name; 1671 size_t name_len; 1672 hammer2_tid_t inum; 1673 int error; 1674 1675 dip = VTOI(ap->a_dvp); 1676 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1677 return (EROFS); 1678 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1679 return (ENOSPC); 1680 1681 ncp = ap->a_nch->ncp; 1682 name = ncp->nc_name; 1683 name_len = ncp->nc_nlen; 1684 /*hammer2_pfs_memory_wait(dip->pmp);*/ 1685 hammer2_trans_init(dip->pmp, 0); 1686 1687 /* 1688 * Create the device inode and then create the directory entry. 1689 * 1690 * dip must be locked before nip to avoid deadlock. 1691 */ 1692 inum = hammer2_trans_newinum(dip->pmp); 1693 1694 hammer2_inode_lock(dip, 0); 1695 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1696 inum, &error); 1697 if (error == 0) { 1698 error = hammer2_dirent_create(dip, name, name_len, 1699 nip->meta.inum, nip->meta.type); 1700 } 1701 if (error) { 1702 if (nip) { 1703 hammer2_inode_unlink_finisher(nip, 0); 1704 hammer2_inode_unlock(nip); 1705 nip = NULL; 1706 } 1707 *ap->a_vpp = NULL; 1708 } else { 1709 hammer2_inode_depend(dip, nip); /* before igetv */ 1710 *ap->a_vpp = hammer2_igetv(nip, &error); 1711 hammer2_inode_unlock(nip); 1712 } 1713 1714 /* 1715 * Update dip's mtime 1716 */ 1717 if (error == 0) { 1718 uint64_t mtime; 1719 1720 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1721 hammer2_update_time(&mtime); 1722 hammer2_inode_modify(dip); 1723 dip->meta.mtime = mtime; 1724 /*hammer2_inode_unlock(dip);*/ 1725 } 1726 hammer2_inode_unlock(dip); 1727 1728 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1729 1730 if (error == 0) { 1731 cache_setunresolved(ap->a_nch); 1732 cache_setvp(ap->a_nch, *ap->a_vpp); 1733 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1734 } 1735 return error; 1736 } 1737 1738 /* 1739 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target } 1740 */ 1741 static 1742 int 1743 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap) 1744 { 1745 hammer2_inode_t *dip; 1746 hammer2_inode_t *nip; 1747 struct namecache *ncp; 1748 const uint8_t *name; 1749 size_t name_len; 1750 hammer2_tid_t inum; 1751 int error; 1752 1753 dip = VTOI(ap->a_dvp); 1754 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) 1755 return (EROFS); 1756 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1757 return (ENOSPC); 1758 1759 ncp = ap->a_nch->ncp; 1760 name = ncp->nc_name; 1761 name_len = ncp->nc_nlen; 1762 /*hammer2_pfs_memory_wait(dip->pmp);*/ 1763 hammer2_trans_init(dip->pmp, 0); 1764 1765 ap->a_vap->va_type = VLNK; /* enforce type */ 1766 1767 /* 1768 * Create the softlink as an inode and then create the directory 1769 * entry. 1770 * 1771 * dip must be locked before nip to avoid deadlock. 1772 */ 1773 inum = hammer2_trans_newinum(dip->pmp); 1774 1775 hammer2_inode_lock(dip, 0); 1776 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, 1777 inum, &error); 1778 if (error == 0) { 1779 error = hammer2_dirent_create(dip, name, name_len, 1780 nip->meta.inum, nip->meta.type); 1781 } 1782 if (error) { 1783 if (nip) { 1784 hammer2_inode_unlink_finisher(nip, 0); 1785 hammer2_inode_unlock(nip); 1786 nip = NULL; 1787 } 1788 *ap->a_vpp = NULL; 1789 hammer2_inode_unlock(dip); 1790 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1791 return error; 1792 } 1793 hammer2_inode_depend(dip, nip); /* before igetv */ 1794 *ap->a_vpp = hammer2_igetv(nip, &error); 1795 1796 /* 1797 * Build the softlink (~like file data) and finalize the namecache. 1798 */ 1799 if (error == 0) { 1800 size_t bytes; 1801 struct uio auio; 1802 struct iovec aiov; 1803 1804 bytes = strlen(ap->a_target); 1805 1806 hammer2_inode_unlock(nip); 1807 bzero(&auio, sizeof(auio)); 1808 bzero(&aiov, sizeof(aiov)); 1809 auio.uio_iov = &aiov; 1810 auio.uio_segflg = UIO_SYSSPACE; 1811 auio.uio_rw = UIO_WRITE; 1812 auio.uio_resid = bytes; 1813 auio.uio_iovcnt = 1; 1814 auio.uio_td = curthread; 1815 aiov.iov_base = ap->a_target; 1816 aiov.iov_len = bytes; 1817 error = hammer2_write_file(nip, &auio, IO_APPEND, 0); 1818 /* XXX handle error */ 1819 error = 0; 1820 } else { 1821 hammer2_inode_unlock(nip); 1822 } 1823 1824 /* 1825 * Update dip's mtime 1826 */ 1827 if (error == 0) { 1828 uint64_t mtime; 1829 1830 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1831 hammer2_update_time(&mtime); 1832 hammer2_inode_modify(dip); 1833 dip->meta.mtime = mtime; 1834 /*hammer2_inode_unlock(dip);*/ 1835 } 1836 hammer2_inode_unlock(dip); 1837 1838 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1839 1840 /* 1841 * Finalize namecache 1842 */ 1843 if (error == 0) { 1844 cache_setunresolved(ap->a_nch); 1845 cache_setvp(ap->a_nch, *ap->a_vpp); 1846 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1847 } 1848 return error; 1849 } 1850 1851 /* 1852 * hammer2_vop_nremove { nch, dvp, cred } 1853 */ 1854 static 1855 int 1856 hammer2_vop_nremove(struct vop_nremove_args *ap) 1857 { 1858 hammer2_xop_unlink_t *xop; 1859 hammer2_inode_t *dip; 1860 hammer2_inode_t *ip; 1861 struct namecache *ncp; 1862 int error; 1863 int isopen; 1864 1865 dip = VTOI(ap->a_dvp); 1866 if (dip->pmp->ronly) 1867 return (EROFS); 1868 #if 0 1869 /* allow removals, except user to also bulkfree */ 1870 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1871 return (ENOSPC); 1872 #endif 1873 1874 ncp = ap->a_nch->ncp; 1875 1876 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) { 1877 kprintf("hammer2: attempt to delete inside debug inode: %s\n", 1878 ncp->nc_name); 1879 while (hammer2_debug_inode && 1880 dip->meta.inum == hammer2_debug_inode) { 1881 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5); 1882 } 1883 } 1884 1885 /*hammer2_pfs_memory_wait(dip->pmp);*/ 1886 hammer2_trans_init(dip->pmp, 0); 1887 hammer2_inode_lock(dip, 0); 1888 1889 /* 1890 * The unlink XOP unlinks the path from the directory and 1891 * locates and returns the cluster associated with the real inode. 1892 * We have to handle nlinks here on the frontend. 1893 */ 1894 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 1895 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 1896 1897 /* 1898 * The namecache entry is locked so nobody can use this namespace. 1899 * Calculate isopen to determine if this namespace has an open vp 1900 * associated with it and resolve the vp only if it does. 1901 * 1902 * We try to avoid resolving the vnode if nobody has it open, but 1903 * note that the test is via this namespace only. 1904 */ 1905 isopen = cache_isopen(ap->a_nch); 1906 xop->isdir = 0; 1907 xop->dopermanent = 0; 1908 hammer2_xop_start(&xop->head, &hammer2_unlink_desc); 1909 1910 /* 1911 * Collect the real inode and adjust nlinks, destroy the real 1912 * inode if nlinks transitions to 0 and it was the real inode 1913 * (else it has already been removed). 1914 */ 1915 error = hammer2_xop_collect(&xop->head, 0); 1916 error = hammer2_error_to_errno(error); 1917 1918 if (error == 0) { 1919 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 1920 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1921 if (ip) { 1922 if (hammer2_debug_inode && 1923 ip->meta.inum == hammer2_debug_inode) { 1924 kprintf("hammer2: attempt to delete debug " 1925 "inode!\n"); 1926 while (hammer2_debug_inode && 1927 ip->meta.inum == hammer2_debug_inode) { 1928 tsleep(&hammer2_debug_inode, 0, 1929 "h2debug", hz*5); 1930 } 1931 } 1932 hammer2_inode_unlink_finisher(ip, isopen); 1933 hammer2_inode_depend(dip, ip); /* after modified */ 1934 hammer2_inode_unlock(ip); 1935 } 1936 } else { 1937 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1938 } 1939 1940 /* 1941 * Update dip's mtime 1942 */ 1943 if (error == 0) { 1944 uint64_t mtime; 1945 1946 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 1947 hammer2_update_time(&mtime); 1948 hammer2_inode_modify(dip); 1949 dip->meta.mtime = mtime; 1950 /*hammer2_inode_unlock(dip);*/ 1951 } 1952 hammer2_inode_unlock(dip); 1953 1954 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 1955 if (error == 0) { 1956 cache_unlink(ap->a_nch); 1957 hammer2_knote(ap->a_dvp, NOTE_WRITE); 1958 } 1959 return (error); 1960 } 1961 1962 /* 1963 * hammer2_vop_nrmdir { nch, dvp, cred } 1964 */ 1965 static 1966 int 1967 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap) 1968 { 1969 hammer2_xop_unlink_t *xop; 1970 hammer2_inode_t *dip; 1971 hammer2_inode_t *ip; 1972 struct namecache *ncp; 1973 int isopen; 1974 int error; 1975 1976 dip = VTOI(ap->a_dvp); 1977 if (dip->pmp->ronly) 1978 return (EROFS); 1979 #if 0 1980 /* allow removals, except user to also bulkfree */ 1981 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) 1982 return (ENOSPC); 1983 #endif 1984 1985 /*hammer2_pfs_memory_wait(dip->pmp);*/ 1986 hammer2_trans_init(dip->pmp, 0); 1987 hammer2_inode_lock(dip, 0); 1988 1989 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 1990 1991 ncp = ap->a_nch->ncp; 1992 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); 1993 isopen = cache_isopen(ap->a_nch); 1994 xop->isdir = 1; 1995 xop->dopermanent = 0; 1996 hammer2_xop_start(&xop->head, &hammer2_unlink_desc); 1997 1998 /* 1999 * Collect the real inode and adjust nlinks, destroy the real 2000 * inode if nlinks transitions to 0 and it was the real inode 2001 * (else it has already been removed). 2002 */ 2003 error = hammer2_xop_collect(&xop->head, 0); 2004 error = hammer2_error_to_errno(error); 2005 2006 if (error == 0) { 2007 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); 2008 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2009 if (ip) { 2010 hammer2_inode_unlink_finisher(ip, isopen); 2011 hammer2_inode_depend(dip, ip); /* after modified */ 2012 hammer2_inode_unlock(ip); 2013 } 2014 } else { 2015 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2016 } 2017 2018 /* 2019 * Update dip's mtime 2020 */ 2021 if (error == 0) { 2022 uint64_t mtime; 2023 2024 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/ 2025 hammer2_update_time(&mtime); 2026 hammer2_inode_modify(dip); 2027 dip->meta.mtime = mtime; 2028 /*hammer2_inode_unlock(dip);*/ 2029 } 2030 hammer2_inode_unlock(dip); 2031 2032 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); 2033 if (error == 0) { 2034 cache_unlink(ap->a_nch); 2035 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 2036 } 2037 return (error); 2038 } 2039 2040 /* 2041 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred } 2042 */ 2043 static 2044 int 2045 hammer2_vop_nrename(struct vop_nrename_args *ap) 2046 { 2047 struct namecache *fncp; 2048 struct namecache *tncp; 2049 hammer2_inode_t *fdip; /* source directory */ 2050 hammer2_inode_t *tdip; /* target directory */ 2051 hammer2_inode_t *ip; /* file being renamed */ 2052 hammer2_inode_t *tip; /* replaced target during rename or NULL */ 2053 const uint8_t *fname; 2054 size_t fname_len; 2055 const uint8_t *tname; 2056 size_t tname_len; 2057 int error; 2058 int update_tdip; 2059 int update_fdip; 2060 hammer2_key_t tlhc; 2061 2062 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) 2063 return(EXDEV); 2064 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount) 2065 return(EXDEV); 2066 2067 fdip = VTOI(ap->a_fdvp); /* source directory */ 2068 tdip = VTOI(ap->a_tdvp); /* target directory */ 2069 2070 if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG)) 2071 return (EROFS); 2072 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1) 2073 return (ENOSPC); 2074 2075 fncp = ap->a_fnch->ncp; /* entry name in source */ 2076 fname = fncp->nc_name; 2077 fname_len = fncp->nc_nlen; 2078 2079 tncp = ap->a_tnch->ncp; /* entry name in target */ 2080 tname = tncp->nc_name; 2081 tname_len = tncp->nc_nlen; 2082 2083 /*hammer2_pfs_memory_wait(tdip->pmp);*/ 2084 hammer2_trans_init(tdip->pmp, 0); 2085 2086 update_tdip = 0; 2087 update_fdip = 0; 2088 2089 ip = VTOI(fncp->nc_vp); 2090 hammer2_inode_ref(ip); /* extra ref */ 2091 2092 /* 2093 * Lookup the target name to determine if a directory entry 2094 * is being overwritten. We only hold related inode locks 2095 * temporarily, the operating system is expected to protect 2096 * against rename races. 2097 */ 2098 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL; 2099 if (tip) 2100 hammer2_inode_ref(tip); /* extra ref */ 2101 2102 /* 2103 * Can return NULL and error == EXDEV if the common parent 2104 * crosses a directory with the xlink flag set. 2105 * 2106 * For now try to avoid deadlocks with a simple pointer address 2107 * test. (tip) can be NULL. 2108 */ 2109 error = 0; 2110 { 2111 hammer2_inode_t *ip1 = fdip; 2112 hammer2_inode_t *ip2 = tdip; 2113 hammer2_inode_t *ip3 = ip; 2114 hammer2_inode_t *ip4 = tip; /* may be NULL */ 2115 2116 if (fdip > tdip) { 2117 ip1 = tdip; 2118 ip2 = fdip; 2119 } 2120 if (tip && ip > tip) { 2121 ip3 = tip; 2122 ip4 = ip; 2123 } 2124 hammer2_inode_lock4(ip1, ip2, ip3, ip4); 2125 } 2126 2127 /* 2128 * Resolve the collision space for (tdip, tname, tname_len) 2129 * 2130 * tdip must be held exclusively locked to prevent races since 2131 * multiple filenames can end up in the same collision space. 2132 */ 2133 { 2134 hammer2_xop_scanlhc_t *sxop; 2135 hammer2_tid_t lhcbase; 2136 2137 tlhc = hammer2_dirhash(tname, tname_len); 2138 lhcbase = tlhc; 2139 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING); 2140 sxop->lhc = tlhc; 2141 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc); 2142 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) { 2143 if (tlhc != sxop->head.cluster.focus->bref.key) 2144 break; 2145 ++tlhc; 2146 } 2147 error = hammer2_error_to_errno(error); 2148 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP); 2149 2150 if (error) { 2151 if (error != ENOENT) 2152 goto done2; 2153 ++tlhc; 2154 error = 0; 2155 } 2156 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) { 2157 error = ENOSPC; 2158 goto done2; 2159 } 2160 } 2161 2162 /* 2163 * Ready to go, issue the rename to the backend. Note that meta-data 2164 * updates to the related inodes occur separately from the rename 2165 * operation. 2166 * 2167 * NOTE: While it is not necessary to update ip->meta.name*, doing 2168 * so aids catastrophic recovery and debugging. 2169 */ 2170 if (error == 0) { 2171 hammer2_xop_nrename_t *xop4; 2172 2173 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING); 2174 xop4->lhc = tlhc; 2175 xop4->ip_key = ip->meta.name_key; 2176 hammer2_xop_setip2(&xop4->head, ip); 2177 hammer2_xop_setip3(&xop4->head, tdip); 2178 hammer2_xop_setname(&xop4->head, fname, fname_len); 2179 hammer2_xop_setname2(&xop4->head, tname, tname_len); 2180 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc); 2181 2182 error = hammer2_xop_collect(&xop4->head, 0); 2183 error = hammer2_error_to_errno(error); 2184 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP); 2185 2186 if (error == ENOENT) 2187 error = 0; 2188 2189 /* 2190 * Update inode meta-data. 2191 * 2192 * WARNING! The in-memory inode (ip) structure does not 2193 * maintain a copy of the inode's filename buffer. 2194 */ 2195 if (error == 0 && 2196 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) { 2197 hammer2_inode_modify(ip); 2198 ip->meta.name_len = tname_len; 2199 ip->meta.name_key = tlhc; 2200 } 2201 if (error == 0) { 2202 hammer2_inode_modify(ip); 2203 ip->meta.iparent = tdip->meta.inum; 2204 } 2205 update_fdip = 1; 2206 update_tdip = 1; 2207 } 2208 2209 done2: 2210 /* 2211 * If no error, the backend has replaced the target directory entry. 2212 * We must adjust nlinks on the original replace target if it exists. 2213 */ 2214 if (error == 0 && tip) { 2215 int isopen; 2216 2217 isopen = cache_isopen(ap->a_tnch); 2218 hammer2_inode_unlink_finisher(tip, isopen); 2219 } 2220 2221 /* 2222 * Update directory mtimes to represent the something changed. 2223 */ 2224 if (update_fdip || update_tdip) { 2225 uint64_t mtime; 2226 2227 hammer2_update_time(&mtime); 2228 if (update_fdip) { 2229 hammer2_inode_modify(fdip); 2230 fdip->meta.mtime = mtime; 2231 } 2232 if (update_tdip) { 2233 hammer2_inode_modify(tdip); 2234 tdip->meta.mtime = mtime; 2235 } 2236 } 2237 if (tip) { 2238 hammer2_inode_unlock(tip); 2239 hammer2_inode_drop(tip); 2240 } 2241 hammer2_inode_unlock(ip); 2242 hammer2_inode_unlock(tdip); 2243 hammer2_inode_unlock(fdip); 2244 hammer2_inode_drop(ip); 2245 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ); 2246 2247 /* 2248 * Issue the namecache update after unlocking all the internal 2249 * hammer2 structures, otherwise we might deadlock. 2250 * 2251 * WARNING! The target namespace must be updated atomically, 2252 * and we depend on cache_rename() to handle that for 2253 * us. Do not do a separate cache_unlink() because 2254 * that leaves a small window of opportunity for other 2255 * threads to allocate the target namespace before we 2256 * manage to complete our rename. 2257 * 2258 * WARNING! cache_rename() (and cache_unlink()) will properly 2259 * set VREF_FINALIZE on any attached vnode. Do not 2260 * call cache_setunresolved() manually before-hand as 2261 * this will prevent the flag from being set later via 2262 * cache_rename(). If VREF_FINALIZE is not properly set 2263 * and the inode is no longer in the topology, related 2264 * chains can remain dirty indefinitely. 2265 */ 2266 if (error == 0 && tip) { 2267 /*cache_unlink(ap->a_tnch); see above */ 2268 /*cache_setunresolved(ap->a_tnch); see above */ 2269 } 2270 if (error == 0) { 2271 cache_rename(ap->a_fnch, ap->a_tnch); 2272 hammer2_knote(ap->a_fdvp, NOTE_WRITE); 2273 hammer2_knote(ap->a_tdvp, NOTE_WRITE); 2274 hammer2_knote(fncp->nc_vp, NOTE_RENAME); 2275 } 2276 2277 return (error); 2278 } 2279 2280 /* 2281 * hammer2_vop_ioctl { vp, command, data, fflag, cred } 2282 */ 2283 static 2284 int 2285 hammer2_vop_ioctl(struct vop_ioctl_args *ap) 2286 { 2287 hammer2_inode_t *ip; 2288 int error; 2289 2290 ip = VTOI(ap->a_vp); 2291 2292 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data, 2293 ap->a_fflag, ap->a_cred); 2294 return (error); 2295 } 2296 2297 static 2298 int 2299 hammer2_vop_mountctl(struct vop_mountctl_args *ap) 2300 { 2301 struct mount *mp; 2302 hammer2_pfs_t *pmp; 2303 int rc; 2304 2305 switch (ap->a_op) { 2306 case (MOUNTCTL_SET_EXPORT): 2307 mp = ap->a_head.a_ops->head.vv_mount; 2308 pmp = MPTOPMP(mp); 2309 2310 if (ap->a_ctllen != sizeof(struct export_args)) 2311 rc = (EINVAL); 2312 else 2313 rc = vfs_export(mp, &pmp->export, 2314 (const struct export_args *)ap->a_ctl); 2315 break; 2316 default: 2317 rc = vop_stdmountctl(ap); 2318 break; 2319 } 2320 return (rc); 2321 } 2322 2323 /* 2324 * KQFILTER 2325 */ 2326 static void filt_hammer2detach(struct knote *kn); 2327 static int filt_hammer2read(struct knote *kn, long hint); 2328 static int filt_hammer2write(struct knote *kn, long hint); 2329 static int filt_hammer2vnode(struct knote *kn, long hint); 2330 2331 static struct filterops hammer2read_filtops = 2332 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2333 NULL, filt_hammer2detach, filt_hammer2read }; 2334 static struct filterops hammer2write_filtops = 2335 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2336 NULL, filt_hammer2detach, filt_hammer2write }; 2337 static struct filterops hammer2vnode_filtops = 2338 { FILTEROP_ISFD | FILTEROP_MPSAFE, 2339 NULL, filt_hammer2detach, filt_hammer2vnode }; 2340 2341 static 2342 int 2343 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap) 2344 { 2345 struct vnode *vp = ap->a_vp; 2346 struct knote *kn = ap->a_kn; 2347 2348 switch (kn->kn_filter) { 2349 case EVFILT_READ: 2350 kn->kn_fop = &hammer2read_filtops; 2351 break; 2352 case EVFILT_WRITE: 2353 kn->kn_fop = &hammer2write_filtops; 2354 break; 2355 case EVFILT_VNODE: 2356 kn->kn_fop = &hammer2vnode_filtops; 2357 break; 2358 default: 2359 return (EOPNOTSUPP); 2360 } 2361 2362 kn->kn_hook = (caddr_t)vp; 2363 2364 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 2365 2366 return(0); 2367 } 2368 2369 static void 2370 filt_hammer2detach(struct knote *kn) 2371 { 2372 struct vnode *vp = (void *)kn->kn_hook; 2373 2374 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 2375 } 2376 2377 static int 2378 filt_hammer2read(struct knote *kn, long hint) 2379 { 2380 struct vnode *vp = (void *)kn->kn_hook; 2381 hammer2_inode_t *ip = VTOI(vp); 2382 off_t off; 2383 2384 if (hint == NOTE_REVOKE) { 2385 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 2386 return(1); 2387 } 2388 off = ip->meta.size - kn->kn_fp->f_offset; 2389 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; 2390 if (kn->kn_sfflags & NOTE_OLDAPI) 2391 return(1); 2392 return (kn->kn_data != 0); 2393 } 2394 2395 2396 static int 2397 filt_hammer2write(struct knote *kn, long hint) 2398 { 2399 if (hint == NOTE_REVOKE) 2400 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 2401 kn->kn_data = 0; 2402 return (1); 2403 } 2404 2405 static int 2406 filt_hammer2vnode(struct knote *kn, long hint) 2407 { 2408 if (kn->kn_sfflags & hint) 2409 kn->kn_fflags |= hint; 2410 if (hint == NOTE_REVOKE) { 2411 kn->kn_flags |= (EV_EOF | EV_NODATA); 2412 return (1); 2413 } 2414 return (kn->kn_fflags != 0); 2415 } 2416 2417 /* 2418 * FIFO VOPS 2419 */ 2420 static 2421 int 2422 hammer2_vop_markatime(struct vop_markatime_args *ap) 2423 { 2424 hammer2_inode_t *ip; 2425 struct vnode *vp; 2426 2427 vp = ap->a_vp; 2428 ip = VTOI(vp); 2429 2430 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG)) 2431 return (EROFS); 2432 return(0); 2433 } 2434 2435 static 2436 int 2437 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap) 2438 { 2439 int error; 2440 2441 error = VOCALL(&fifo_vnode_vops, &ap->a_head); 2442 if (error) 2443 error = hammer2_vop_kqfilter(ap); 2444 return(error); 2445 } 2446 2447 /* 2448 * VOPS vector 2449 */ 2450 struct vop_ops hammer2_vnode_vops = { 2451 .vop_default = vop_defaultop, 2452 .vop_fsync = hammer2_vop_fsync, 2453 .vop_getpages = vop_stdgetpages, 2454 .vop_putpages = vop_stdputpages, 2455 .vop_access = hammer2_vop_access, 2456 .vop_advlock = hammer2_vop_advlock, 2457 .vop_close = hammer2_vop_close, 2458 .vop_nlink = hammer2_vop_nlink, 2459 .vop_ncreate = hammer2_vop_ncreate, 2460 .vop_nsymlink = hammer2_vop_nsymlink, 2461 .vop_nremove = hammer2_vop_nremove, 2462 .vop_nrmdir = hammer2_vop_nrmdir, 2463 .vop_nrename = hammer2_vop_nrename, 2464 .vop_getattr = hammer2_vop_getattr, 2465 .vop_setattr = hammer2_vop_setattr, 2466 .vop_readdir = hammer2_vop_readdir, 2467 .vop_readlink = hammer2_vop_readlink, 2468 .vop_read = hammer2_vop_read, 2469 .vop_write = hammer2_vop_write, 2470 .vop_open = hammer2_vop_open, 2471 .vop_inactive = hammer2_vop_inactive, 2472 .vop_reclaim = hammer2_vop_reclaim, 2473 .vop_nresolve = hammer2_vop_nresolve, 2474 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot, 2475 .vop_nmkdir = hammer2_vop_nmkdir, 2476 .vop_nmknod = hammer2_vop_nmknod, 2477 .vop_ioctl = hammer2_vop_ioctl, 2478 .vop_mountctl = hammer2_vop_mountctl, 2479 .vop_bmap = hammer2_vop_bmap, 2480 .vop_strategy = hammer2_vop_strategy, 2481 .vop_kqfilter = hammer2_vop_kqfilter 2482 }; 2483 2484 struct vop_ops hammer2_spec_vops = { 2485 .vop_default = vop_defaultop, 2486 .vop_fsync = hammer2_vop_fsync, 2487 .vop_read = vop_stdnoread, 2488 .vop_write = vop_stdnowrite, 2489 .vop_access = hammer2_vop_access, 2490 .vop_close = hammer2_vop_close, 2491 .vop_markatime = hammer2_vop_markatime, 2492 .vop_getattr = hammer2_vop_getattr, 2493 .vop_inactive = hammer2_vop_inactive, 2494 .vop_reclaim = hammer2_vop_reclaim, 2495 .vop_setattr = hammer2_vop_setattr 2496 }; 2497 2498 struct vop_ops hammer2_fifo_vops = { 2499 .vop_default = fifo_vnoperate, 2500 .vop_fsync = hammer2_vop_fsync, 2501 #if 0 2502 .vop_read = hammer2_vop_fiforead, 2503 .vop_write = hammer2_vop_fifowrite, 2504 #endif 2505 .vop_access = hammer2_vop_access, 2506 #if 0 2507 .vop_close = hammer2_vop_fifoclose, 2508 #endif 2509 .vop_markatime = hammer2_vop_markatime, 2510 .vop_getattr = hammer2_vop_getattr, 2511 .vop_inactive = hammer2_vop_inactive, 2512 .vop_reclaim = hammer2_vop_reclaim, 2513 .vop_setattr = hammer2_vop_setattr, 2514 .vop_kqfilter = hammer2_vop_fifokqfilter 2515 }; 2516 2517