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