1 /* 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/mountctl.h> 36 #include <sys/namecache.h> 37 #include <sys/buf2.h> 38 #include <vfs/fifofs/fifo.h> 39 40 #include "hammer.h" 41 42 /* 43 * USERFS VNOPS 44 */ 45 static int hammer_vop_fsync(struct vop_fsync_args *); 46 static int hammer_vop_read(struct vop_read_args *); 47 static int hammer_vop_write(struct vop_write_args *); 48 static int hammer_vop_access(struct vop_access_args *); 49 static int hammer_vop_advlock(struct vop_advlock_args *); 50 static int hammer_vop_close(struct vop_close_args *); 51 static int hammer_vop_ncreate(struct vop_ncreate_args *); 52 static int hammer_vop_getattr(struct vop_getattr_args *); 53 static int hammer_vop_nresolve(struct vop_nresolve_args *); 54 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *); 55 static int hammer_vop_nlink(struct vop_nlink_args *); 56 static int hammer_vop_nmkdir(struct vop_nmkdir_args *); 57 static int hammer_vop_nmknod(struct vop_nmknod_args *); 58 static int hammer_vop_open(struct vop_open_args *); 59 static int hammer_vop_print(struct vop_print_args *); 60 static int hammer_vop_readdir(struct vop_readdir_args *); 61 static int hammer_vop_readlink(struct vop_readlink_args *); 62 static int hammer_vop_nremove(struct vop_nremove_args *); 63 static int hammer_vop_nrename(struct vop_nrename_args *); 64 static int hammer_vop_nrmdir(struct vop_nrmdir_args *); 65 static int hammer_vop_markatime(struct vop_markatime_args *); 66 static int hammer_vop_setattr(struct vop_setattr_args *); 67 static int hammer_vop_strategy(struct vop_strategy_args *); 68 static int hammer_vop_bmap(struct vop_bmap_args *ap); 69 static int hammer_vop_nsymlink(struct vop_nsymlink_args *); 70 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *); 71 static int hammer_vop_ioctl(struct vop_ioctl_args *); 72 static int hammer_vop_mountctl(struct vop_mountctl_args *); 73 static int hammer_vop_kqfilter (struct vop_kqfilter_args *); 74 75 static int hammer_vop_fifoclose (struct vop_close_args *); 76 static int hammer_vop_fiforead (struct vop_read_args *); 77 static int hammer_vop_fifowrite (struct vop_write_args *); 78 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *); 79 80 struct vop_ops hammer_vnode_vops = { 81 .vop_default = vop_defaultop, 82 .vop_fsync = hammer_vop_fsync, 83 .vop_getpages = vop_stdgetpages, 84 .vop_putpages = vop_stdputpages, 85 .vop_read = hammer_vop_read, 86 .vop_write = hammer_vop_write, 87 .vop_access = hammer_vop_access, 88 .vop_advlock = hammer_vop_advlock, 89 .vop_close = hammer_vop_close, 90 .vop_ncreate = hammer_vop_ncreate, 91 .vop_getattr = hammer_vop_getattr, 92 .vop_inactive = hammer_vop_inactive, 93 .vop_reclaim = hammer_vop_reclaim, 94 .vop_nresolve = hammer_vop_nresolve, 95 .vop_nlookupdotdot = hammer_vop_nlookupdotdot, 96 .vop_nlink = hammer_vop_nlink, 97 .vop_nmkdir = hammer_vop_nmkdir, 98 .vop_nmknod = hammer_vop_nmknod, 99 .vop_open = hammer_vop_open, 100 .vop_pathconf = vop_stdpathconf, 101 .vop_print = hammer_vop_print, 102 .vop_readdir = hammer_vop_readdir, 103 .vop_readlink = hammer_vop_readlink, 104 .vop_nremove = hammer_vop_nremove, 105 .vop_nrename = hammer_vop_nrename, 106 .vop_nrmdir = hammer_vop_nrmdir, 107 .vop_markatime = hammer_vop_markatime, 108 .vop_setattr = hammer_vop_setattr, 109 .vop_bmap = hammer_vop_bmap, 110 .vop_strategy = hammer_vop_strategy, 111 .vop_nsymlink = hammer_vop_nsymlink, 112 .vop_nwhiteout = hammer_vop_nwhiteout, 113 .vop_ioctl = hammer_vop_ioctl, 114 .vop_mountctl = hammer_vop_mountctl, 115 .vop_kqfilter = hammer_vop_kqfilter 116 }; 117 118 struct vop_ops hammer_spec_vops = { 119 .vop_default = vop_defaultop, 120 .vop_fsync = hammer_vop_fsync, 121 .vop_read = vop_stdnoread, 122 .vop_write = vop_stdnowrite, 123 .vop_access = hammer_vop_access, 124 .vop_close = hammer_vop_close, 125 .vop_markatime = hammer_vop_markatime, 126 .vop_getattr = hammer_vop_getattr, 127 .vop_inactive = hammer_vop_inactive, 128 .vop_reclaim = hammer_vop_reclaim, 129 .vop_setattr = hammer_vop_setattr 130 }; 131 132 struct vop_ops hammer_fifo_vops = { 133 .vop_default = fifo_vnoperate, 134 .vop_fsync = hammer_vop_fsync, 135 .vop_read = hammer_vop_fiforead, 136 .vop_write = hammer_vop_fifowrite, 137 .vop_access = hammer_vop_access, 138 .vop_close = hammer_vop_fifoclose, 139 .vop_markatime = hammer_vop_markatime, 140 .vop_getattr = hammer_vop_getattr, 141 .vop_inactive = hammer_vop_inactive, 142 .vop_reclaim = hammer_vop_reclaim, 143 .vop_setattr = hammer_vop_setattr, 144 .vop_kqfilter = hammer_vop_fifokqfilter 145 }; 146 147 static __inline 148 void 149 hammer_knote(struct vnode *vp, int flags) 150 { 151 if (flags) 152 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags); 153 } 154 155 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch, 156 struct vnode *dvp, struct ucred *cred, 157 int flags, int isdir); 158 static int hammer_vop_strategy_read(struct vop_strategy_args *ap); 159 static int hammer_vop_strategy_write(struct vop_strategy_args *ap); 160 161 /* 162 * hammer_vop_fsync { vp, waitfor } 163 * 164 * fsync() an inode to disk and wait for it to be completely committed 165 * such that the information would not be undone if a crash occured after 166 * return. 167 * 168 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement 169 * a REDO log. A sysctl is provided to relax HAMMER's fsync() 170 * operation. 171 * 172 * Ultimately the combination of a REDO log and use of fast storage 173 * to front-end cluster caches will make fsync fast, but it aint 174 * here yet. And, in anycase, we need real transactional 175 * all-or-nothing features which are not restricted to a single file. 176 */ 177 static 178 int 179 hammer_vop_fsync(struct vop_fsync_args *ap) 180 { 181 hammer_inode_t ip = VTOI(ap->a_vp); 182 hammer_mount_t hmp = ip->hmp; 183 int waitfor = ap->a_waitfor; 184 int mode; 185 186 lwkt_gettoken(&hmp->fs_token); 187 188 /* 189 * Fsync rule relaxation (default is either full synchronous flush 190 * or REDO semantics with synchronous flush). 191 */ 192 if (ap->a_flags & VOP_FSYNC_SYSCALL) { 193 switch(hammer_fsync_mode) { 194 case 0: 195 mode0: 196 /* no REDO, full synchronous flush */ 197 goto skip; 198 case 1: 199 mode1: 200 /* no REDO, full asynchronous flush */ 201 if (waitfor == MNT_WAIT) 202 waitfor = MNT_NOWAIT; 203 goto skip; 204 case 2: 205 /* REDO semantics, synchronous flush */ 206 if (hmp->version < HAMMER_VOL_VERSION_FOUR) 207 goto mode0; 208 mode = HAMMER_FLUSH_UNDOS_AUTO; 209 break; 210 case 3: 211 /* REDO semantics, relaxed asynchronous flush */ 212 if (hmp->version < HAMMER_VOL_VERSION_FOUR) 213 goto mode1; 214 mode = HAMMER_FLUSH_UNDOS_RELAXED; 215 if (waitfor == MNT_WAIT) 216 waitfor = MNT_NOWAIT; 217 break; 218 case 4: 219 /* ignore the fsync() system call */ 220 lwkt_reltoken(&hmp->fs_token); 221 return(0); 222 default: 223 /* we have to do something */ 224 mode = HAMMER_FLUSH_UNDOS_RELAXED; 225 if (waitfor == MNT_WAIT) 226 waitfor = MNT_NOWAIT; 227 break; 228 } 229 230 /* 231 * Fast fsync only needs to flush the UNDO/REDO fifo if 232 * HAMMER_INODE_REDO is non-zero and the only modifications 233 * made to the file are write or write-extends. 234 */ 235 if ((ip->flags & HAMMER_INODE_REDO) && 236 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0) { 237 ++hammer_count_fsyncs; 238 hammer_flusher_flush_undos(hmp, mode); 239 ip->redo_count = 0; 240 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0) 241 vclrisdirty(ip->vp); 242 lwkt_reltoken(&hmp->fs_token); 243 return(0); 244 } 245 246 /* 247 * REDO is enabled by fsync(), the idea being we really only 248 * want to lay down REDO records when programs are using 249 * fsync() heavily. The first fsync() on the file starts 250 * the gravy train going and later fsync()s keep it hot by 251 * resetting the redo_count. 252 * 253 * We weren't running REDOs before now so we have to fall 254 * through and do a full fsync of what we have. 255 */ 256 if (hmp->version >= HAMMER_VOL_VERSION_FOUR && 257 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) { 258 ip->flags |= HAMMER_INODE_REDO; 259 ip->redo_count = 0; 260 } 261 } 262 skip: 263 264 /* 265 * Do a full flush sequence. 266 * 267 * Attempt to release the vnode while waiting for the inode to 268 * finish flushing. This can really mess up inactive->reclaim 269 * sequences so only do it if the vnode is active. 270 * 271 * WARNING! The VX lock functions must be used. vn_lock() will 272 * fail when this is part of a VOP_RECLAIM sequence. 273 */ 274 ++hammer_count_fsyncs; 275 vfsync(ap->a_vp, waitfor, 1, NULL, NULL); 276 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); 277 if (waitfor == MNT_WAIT) { 278 int dorelock; 279 280 if ((ap->a_vp->v_flag & VRECLAIMED) == 0) { 281 vx_unlock(ap->a_vp); 282 dorelock = 1; 283 } else { 284 dorelock = 0; 285 } 286 hammer_wait_inode(ip); 287 if (dorelock) 288 vx_lock(ap->a_vp); 289 } 290 if (ip->vp && (ip->flags & HAMMER_INODE_MODMASK) == 0) 291 vclrisdirty(ip->vp); 292 lwkt_reltoken(&hmp->fs_token); 293 return (ip->error); 294 } 295 296 /* 297 * hammer_vop_read { vp, uio, ioflag, cred } 298 * 299 * MPSAFE (for the cache safe does not require fs_token) 300 */ 301 static 302 int 303 hammer_vop_read(struct vop_read_args *ap) 304 { 305 struct hammer_transaction trans; 306 hammer_inode_t ip; 307 hammer_mount_t hmp; 308 off_t offset; 309 struct buf *bp; 310 struct uio *uio; 311 int error; 312 int n; 313 int seqcount; 314 int ioseqcount; 315 int blksize; 316 int bigread; 317 int got_trans; 318 size_t resid; 319 320 if (ap->a_vp->v_type != VREG) 321 return (EINVAL); 322 ip = VTOI(ap->a_vp); 323 hmp = ip->hmp; 324 error = 0; 325 got_trans = 0; 326 uio = ap->a_uio; 327 328 /* 329 * Attempt to shortcut directly to the VM object using lwbufs. 330 * This is much faster than instantiating buffer cache buffers. 331 */ 332 resid = uio->uio_resid; 333 error = vop_helper_read_shortcut(ap); 334 hammer_stats_file_read += resid - uio->uio_resid; 335 if (error) 336 return (error); 337 if (uio->uio_resid == 0) 338 goto finished; 339 340 /* 341 * Allow the UIO's size to override the sequential heuristic. 342 */ 343 blksize = hammer_blocksize(uio->uio_offset); 344 seqcount = (uio->uio_resid + (MAXBSIZE - 1)) / MAXBSIZE; 345 ioseqcount = (ap->a_ioflag >> 16); 346 if (seqcount < ioseqcount) 347 seqcount = ioseqcount; 348 349 /* 350 * If reading or writing a huge amount of data we have to break 351 * atomicy and allow the operation to be interrupted by a signal 352 * or it can DOS the machine. 353 */ 354 bigread = (uio->uio_resid > 100 * 1024 * 1024); 355 356 /* 357 * Access the data typically in HAMMER_BUFSIZE blocks via the 358 * buffer cache, but HAMMER may use a variable block size based 359 * on the offset. 360 * 361 * XXX Temporary hack, delay the start transaction while we remain 362 * MPSAFE. NOTE: ino_data.size cannot change while vnode is 363 * locked-shared. 364 */ 365 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) { 366 int64_t base_offset; 367 int64_t file_limit; 368 369 blksize = hammer_blocksize(uio->uio_offset); 370 offset = (int)uio->uio_offset & (blksize - 1); 371 base_offset = uio->uio_offset - offset; 372 373 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0) 374 break; 375 376 /* 377 * MPSAFE 378 */ 379 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0); 380 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) { 381 bp->b_flags &= ~B_AGE; 382 error = 0; 383 goto skip; 384 } 385 if (ap->a_ioflag & IO_NRDELAY) { 386 bqrelse(bp); 387 return (EWOULDBLOCK); 388 } 389 390 /* 391 * MPUNSAFE 392 */ 393 if (got_trans == 0) { 394 hammer_start_transaction(&trans, ip->hmp); 395 got_trans = 1; 396 } 397 398 /* 399 * NOTE: A valid bp has already been acquired, but was not 400 * B_CACHE. 401 */ 402 if (hammer_cluster_enable) { 403 /* 404 * Use file_limit to prevent cluster_read() from 405 * creating buffers of the wrong block size past 406 * the demarc. 407 */ 408 file_limit = ip->ino_data.size; 409 if (base_offset < HAMMER_XDEMARC && 410 file_limit > HAMMER_XDEMARC) { 411 file_limit = HAMMER_XDEMARC; 412 } 413 error = cluster_readx(ap->a_vp, 414 file_limit, base_offset, 415 blksize, B_NOTMETA, 416 uio->uio_resid, 417 seqcount * MAXBSIZE, 418 &bp); 419 } else { 420 error = breadnx(ap->a_vp, base_offset, 421 blksize, B_NOTMETA, 422 NULL, NULL, 0, &bp); 423 } 424 if (error) { 425 brelse(bp); 426 break; 427 } 428 skip: 429 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IOISSUED)) { 430 hdkprintf("zone2_offset %016jx read file %016jx@%016jx\n", 431 (intmax_t)bp->b_bio2.bio_offset, 432 (intmax_t)ip->obj_id, 433 (intmax_t)bp->b_loffset); 434 } 435 bp->b_flags &= ~B_IOISSUED; 436 if (blksize == HAMMER_XBUFSIZE) 437 bp->b_flags |= B_CLUSTEROK; 438 439 n = blksize - offset; 440 if (n > uio->uio_resid) 441 n = uio->uio_resid; 442 if (n > ip->ino_data.size - uio->uio_offset) 443 n = (int)(ip->ino_data.size - uio->uio_offset); 444 445 /* 446 * Set B_AGE, data has a lower priority than meta-data. 447 * 448 * Use a hold/unlock/drop sequence to run the uiomove 449 * with the buffer unlocked, avoiding deadlocks against 450 * read()s on mmap()'d spaces. 451 */ 452 bp->b_flags |= B_AGE; 453 error = uiomovebp(bp, (char *)bp->b_data + offset, n, uio); 454 bqrelse(bp); 455 456 if (error) 457 break; 458 hammer_stats_file_read += n; 459 } 460 461 finished: 462 463 /* 464 * Try to update the atime with just the inode lock for maximum 465 * concurrency. If we can't shortcut it we have to get the full 466 * blown transaction. 467 */ 468 if (got_trans == 0 && hammer_update_atime_quick(ip) < 0) { 469 hammer_start_transaction(&trans, ip->hmp); 470 got_trans = 1; 471 } 472 473 if (got_trans) { 474 if ((ip->flags & HAMMER_INODE_RO) == 0 && 475 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) { 476 lwkt_gettoken(&hmp->fs_token); 477 ip->ino_data.atime = trans.time; 478 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME); 479 hammer_done_transaction(&trans); 480 lwkt_reltoken(&hmp->fs_token); 481 } else { 482 hammer_done_transaction(&trans); 483 } 484 } 485 return (error); 486 } 487 488 /* 489 * hammer_vop_write { vp, uio, ioflag, cred } 490 */ 491 static 492 int 493 hammer_vop_write(struct vop_write_args *ap) 494 { 495 struct hammer_transaction trans; 496 hammer_inode_t ip; 497 hammer_mount_t hmp; 498 thread_t td; 499 struct vnode *vp; 500 struct uio *uio; 501 int offset; 502 off_t base_offset; 503 int64_t cluster_eof; 504 struct buf *bp; 505 int kflags; 506 int error; 507 int n; 508 int flags; 509 int seqcount; 510 int bigwrite; 511 512 vp = ap->a_vp; 513 if (vp->v_type != VREG) 514 return (EINVAL); 515 ip = VTOI(ap->a_vp); 516 hmp = ip->hmp; 517 error = 0; 518 kflags = 0; 519 seqcount = ap->a_ioflag >> 16; 520 521 if (ip->flags & HAMMER_INODE_RO) 522 return (EROFS); 523 524 /* 525 * Create a transaction to cover the operations we perform. 526 */ 527 hammer_start_transaction(&trans, hmp); 528 uio = ap->a_uio; 529 530 /* 531 * Use v_lastwrite_ts if file not open for writing 532 * (i.e. a late msync) 533 */ 534 if (vp->v_writecount == 0) { 535 trans.time = vp->v_lastwrite_ts.tv_sec * 1000000 + 536 vp->v_lastwrite_ts.tv_nsec / 1000; 537 } 538 539 540 /* 541 * Check append mode 542 */ 543 if (ap->a_ioflag & IO_APPEND) 544 uio->uio_offset = ip->ino_data.size; 545 546 /* 547 * Check for illegal write offsets. Valid range is 0...2^63-1. 548 * 549 * NOTE: the base_off assignment is required to work around what 550 * I consider to be a GCC-4 optimization bug. 551 */ 552 if (uio->uio_offset < 0) { 553 hammer_done_transaction(&trans); 554 return (EFBIG); 555 } 556 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */ 557 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) { 558 hammer_done_transaction(&trans); 559 return (EFBIG); 560 } 561 562 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc && 563 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 564 hammer_done_transaction(&trans); 565 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ); 566 return (EFBIG); 567 } 568 569 /* 570 * If reading or writing a huge amount of data we have to break 571 * atomicy and allow the operation to be interrupted by a signal 572 * or it can DOS the machine. 573 * 574 * Preset redo_count so we stop generating REDOs earlier if the 575 * limit is exceeded. 576 * 577 * redo_count is heuristical, SMP races are ok 578 */ 579 bigwrite = (uio->uio_resid > 100 * 1024 * 1024); 580 if ((ip->flags & HAMMER_INODE_REDO) && 581 ip->redo_count < hammer_limit_redo) { 582 ip->redo_count += uio->uio_resid; 583 } 584 585 /* 586 * Access the data typically in HAMMER_BUFSIZE blocks via the 587 * buffer cache, but HAMMER may use a variable block size based 588 * on the offset. 589 */ 590 while (uio->uio_resid > 0) { 591 int fixsize = 0; 592 int blksize; 593 int blkmask; 594 int trivial; 595 int endofblk; 596 off_t nsize; 597 598 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0) 599 break; 600 if (bigwrite && (error = hammer_signal_check(hmp)) != 0) 601 break; 602 603 blksize = hammer_blocksize(uio->uio_offset); 604 605 /* 606 * Control the number of pending records associated with 607 * this inode. If too many have accumulated start a 608 * flush. Try to maintain a pipeline with the flusher. 609 * 610 * NOTE: It is possible for other sources to grow the 611 * records but not necessarily issue another flush, 612 * so use a timeout and ensure that a re-flush occurs. 613 */ 614 if (ip->rsv_recs >= hammer_limit_inode_recs) { 615 lwkt_gettoken(&hmp->fs_token); 616 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); 617 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) { 618 ip->flags |= HAMMER_INODE_RECSW; 619 tsleep(&ip->rsv_recs, 0, "hmrwww", hz); 620 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); 621 } 622 lwkt_reltoken(&hmp->fs_token); 623 } 624 625 /* 626 * Do not allow HAMMER to blow out the buffer cache. Very 627 * large UIOs can lockout other processes due to bwillwrite() 628 * mechanics. 629 * 630 * The hammer inode is not locked during these operations. 631 * The vnode is locked which can interfere with the pageout 632 * daemon for non-UIO_NOCOPY writes but should not interfere 633 * with the buffer cache. Even so, we cannot afford to 634 * allow the pageout daemon to build up too many dirty buffer 635 * cache buffers. 636 * 637 * Only call this if we aren't being recursively called from 638 * a virtual disk device (vn), else we may deadlock. 639 */ 640 if ((ap->a_ioflag & IO_RECURSE) == 0) 641 bwillwrite(blksize); 642 643 /* 644 * Calculate the blocksize at the current offset and figure 645 * out how much we can actually write. 646 */ 647 blkmask = blksize - 1; 648 offset = (int)uio->uio_offset & blkmask; 649 base_offset = uio->uio_offset & ~(int64_t)blkmask; 650 n = blksize - offset; 651 if (n > uio->uio_resid) { 652 n = uio->uio_resid; 653 endofblk = 0; 654 } else { 655 endofblk = 1; 656 } 657 nsize = uio->uio_offset + n; 658 if (nsize > ip->ino_data.size) { 659 if (uio->uio_offset > ip->ino_data.size) 660 trivial = 0; 661 else 662 trivial = 1; 663 nvextendbuf(ap->a_vp, 664 ip->ino_data.size, 665 nsize, 666 hammer_blocksize(ip->ino_data.size), 667 hammer_blocksize(nsize), 668 hammer_blockoff(ip->ino_data.size), 669 hammer_blockoff(nsize), 670 trivial); 671 fixsize = 1; 672 kflags |= NOTE_EXTEND; 673 } 674 675 if (uio->uio_segflg == UIO_NOCOPY) { 676 /* 677 * Issuing a write with the same data backing the 678 * buffer. Instantiate the buffer to collect the 679 * backing vm pages, then read-in any missing bits. 680 * 681 * This case is used by vop_stdputpages(). 682 */ 683 bp = getblk(ap->a_vp, base_offset, 684 blksize, GETBLK_BHEAVY, 0); 685 if ((bp->b_flags & B_CACHE) == 0) { 686 bqrelse(bp); 687 error = bread(ap->a_vp, base_offset, 688 blksize, &bp); 689 } 690 } else if (offset == 0 && uio->uio_resid >= blksize) { 691 /* 692 * Even though we are entirely overwriting the buffer 693 * we may still have to zero it out to avoid a 694 * mmap/write visibility issue. 695 */ 696 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0); 697 if ((bp->b_flags & B_CACHE) == 0) 698 vfs_bio_clrbuf(bp); 699 } else if (base_offset >= ip->ino_data.size) { 700 /* 701 * If the base offset of the buffer is beyond the 702 * file EOF, we don't have to issue a read. 703 */ 704 bp = getblk(ap->a_vp, base_offset, 705 blksize, GETBLK_BHEAVY, 0); 706 vfs_bio_clrbuf(bp); 707 } else { 708 /* 709 * Partial overwrite, read in any missing bits then 710 * replace the portion being written. 711 */ 712 error = bread(ap->a_vp, base_offset, blksize, &bp); 713 if (error == 0) 714 bheavy(bp); 715 } 716 if (error == 0) 717 error = uiomovebp(bp, bp->b_data + offset, n, uio); 718 719 lwkt_gettoken(&hmp->fs_token); 720 721 /* 722 * Generate REDO records if enabled and redo_count will not 723 * exceeded the limit. 724 * 725 * If redo_count exceeds the limit we stop generating records 726 * and clear HAMMER_INODE_REDO. This will cause the next 727 * fsync() to do a full meta-data sync instead of just an 728 * UNDO/REDO fifo update. 729 * 730 * When clearing HAMMER_INODE_REDO any pre-existing REDOs 731 * will still be tracked. The tracks will be terminated 732 * when the related meta-data (including possible data 733 * modifications which are not tracked via REDO) is 734 * flushed. 735 */ 736 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) { 737 if (ip->redo_count < hammer_limit_redo) { 738 bp->b_flags |= B_VFSFLAG1; 739 error = hammer_generate_redo(&trans, ip, 740 base_offset + offset, 741 HAMMER_REDO_WRITE, 742 bp->b_data + offset, 743 (size_t)n); 744 } else { 745 ip->flags &= ~HAMMER_INODE_REDO; 746 } 747 } 748 749 /* 750 * If we screwed up we have to undo any VM size changes we 751 * made. 752 */ 753 if (error) { 754 brelse(bp); 755 if (fixsize) { 756 nvtruncbuf(ap->a_vp, ip->ino_data.size, 757 hammer_blocksize(ip->ino_data.size), 758 hammer_blockoff(ip->ino_data.size), 759 0); 760 } 761 lwkt_reltoken(&hmp->fs_token); 762 break; 763 } 764 kflags |= NOTE_WRITE; 765 hammer_stats_file_write += n; 766 if (blksize == HAMMER_XBUFSIZE) 767 bp->b_flags |= B_CLUSTEROK; 768 if (ip->ino_data.size < uio->uio_offset) { 769 ip->ino_data.size = uio->uio_offset; 770 flags = HAMMER_INODE_SDIRTY; 771 } else { 772 flags = 0; 773 } 774 ip->ino_data.mtime = trans.time; 775 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS; 776 hammer_modify_inode(&trans, ip, flags); 777 778 /* 779 * Once we dirty the buffer any cached zone-X offset 780 * becomes invalid. HAMMER NOTE: no-history mode cannot 781 * allow overwriting over the same data sector unless 782 * we provide UNDOs for the old data, which we don't. 783 */ 784 bp->b_bio2.bio_offset = NOOFFSET; 785 786 lwkt_reltoken(&hmp->fs_token); 787 788 /* 789 * Final buffer disposition. 790 * 791 * Because meta-data updates are deferred, HAMMER is 792 * especially sensitive to excessive bdwrite()s because 793 * the I/O stream is not broken up by disk reads. So the 794 * buffer cache simply cannot keep up. 795 * 796 * WARNING! blksize is variable. cluster_write() is 797 * expected to not blow up if it encounters 798 * buffers that do not match the passed blksize. 799 * 800 * NOTE! Hammer shouldn't need to bawrite()/cluster_write(). 801 * The ip->rsv_recs check should burst-flush the data. 802 * If we queue it immediately the buf could be left 803 * locked on the device queue for a very long time. 804 * 805 * However, failing to flush a dirty buffer out when 806 * issued from the pageout daemon can result in a low 807 * memory deadlock against bio_page_alloc(), so we 808 * have to bawrite() on IO_ASYNC as well. 809 * 810 * NOTE! To avoid degenerate stalls due to mismatched block 811 * sizes we only honor IO_DIRECT on the write which 812 * abuts the end of the buffer. However, we must 813 * honor IO_SYNC in case someone is silly enough to 814 * configure a HAMMER file as swap, or when HAMMER 815 * is serving NFS (for commits). Ick ick. 816 */ 817 bp->b_flags |= B_AGE; 818 if (blksize == HAMMER_XBUFSIZE) 819 bp->b_flags |= B_CLUSTEROK; 820 821 if (ap->a_ioflag & IO_SYNC) { 822 bwrite(bp); 823 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) { 824 bawrite(bp); 825 } else if (ap->a_ioflag & IO_ASYNC) { 826 bawrite(bp); 827 } else if (hammer_cluster_enable && 828 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) { 829 if (base_offset < HAMMER_XDEMARC) 830 cluster_eof = hammer_blockdemarc(base_offset, 831 ip->ino_data.size); 832 else 833 cluster_eof = ip->ino_data.size; 834 cluster_write(bp, cluster_eof, blksize, seqcount); 835 } else { 836 bdwrite(bp); 837 } 838 } 839 hammer_done_transaction(&trans); 840 hammer_knote(ap->a_vp, kflags); 841 842 return (error); 843 } 844 845 /* 846 * hammer_vop_access { vp, mode, cred } 847 * 848 * MPSAFE - does not require fs_token 849 */ 850 static 851 int 852 hammer_vop_access(struct vop_access_args *ap) 853 { 854 hammer_inode_t ip = VTOI(ap->a_vp); 855 uid_t uid; 856 gid_t gid; 857 int error; 858 859 uid = hammer_to_unix_xid(&ip->ino_data.uid); 860 gid = hammer_to_unix_xid(&ip->ino_data.gid); 861 862 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode, 863 ip->ino_data.uflags); 864 return (error); 865 } 866 867 /* 868 * hammer_vop_advlock { vp, id, op, fl, flags } 869 * 870 * MPSAFE - does not require fs_token 871 */ 872 static 873 int 874 hammer_vop_advlock(struct vop_advlock_args *ap) 875 { 876 hammer_inode_t ip = VTOI(ap->a_vp); 877 878 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size)); 879 } 880 881 /* 882 * hammer_vop_close { vp, fflag } 883 * 884 * We can only sync-on-close for normal closes. XXX disabled for now. 885 */ 886 static 887 int 888 hammer_vop_close(struct vop_close_args *ap) 889 { 890 #if 0 891 struct vnode *vp = ap->a_vp; 892 hammer_inode_t ip = VTOI(vp); 893 int waitfor; 894 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) { 895 if (vn_islocked(vp) == LK_EXCLUSIVE && 896 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) { 897 if (ip->flags & HAMMER_INODE_CLOSESYNC) 898 waitfor = MNT_WAIT; 899 else 900 waitfor = MNT_NOWAIT; 901 ip->flags &= ~(HAMMER_INODE_CLOSESYNC | 902 HAMMER_INODE_CLOSEASYNC); 903 VOP_FSYNC(vp, MNT_NOWAIT, waitfor); 904 } 905 } 906 #endif 907 return (vop_stdclose(ap)); 908 } 909 910 /* 911 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap } 912 * 913 * The operating system has already ensured that the directory entry 914 * does not exist and done all appropriate namespace locking. 915 */ 916 static 917 int 918 hammer_vop_ncreate(struct vop_ncreate_args *ap) 919 { 920 struct hammer_transaction trans; 921 hammer_inode_t dip; 922 hammer_inode_t nip; 923 struct nchandle *nch; 924 hammer_mount_t hmp; 925 int error; 926 927 nch = ap->a_nch; 928 dip = VTOI(ap->a_dvp); 929 hmp = dip->hmp; 930 931 if (dip->flags & HAMMER_INODE_RO) 932 return (EROFS); 933 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) 934 return (error); 935 936 /* 937 * Create a transaction to cover the operations we perform. 938 */ 939 lwkt_gettoken(&hmp->fs_token); 940 hammer_start_transaction(&trans, hmp); 941 942 /* 943 * Create a new filesystem object of the requested type. The 944 * returned inode will be referenced and shared-locked to prevent 945 * it from being moved to the flusher. 946 */ 947 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, 948 dip, nch->ncp->nc_name, nch->ncp->nc_nlen, 949 NULL, &nip); 950 if (error) { 951 hkprintf("hammer_create_inode error %d\n", error); 952 hammer_done_transaction(&trans); 953 *ap->a_vpp = NULL; 954 lwkt_reltoken(&hmp->fs_token); 955 return (error); 956 } 957 958 /* 959 * Add the new filesystem object to the directory. This will also 960 * bump the inode's link count. 961 */ 962 error = hammer_ip_add_direntry(&trans, dip, 963 nch->ncp->nc_name, nch->ncp->nc_nlen, 964 nip); 965 if (error) 966 hkprintf("hammer_ip_add_direntry error %d\n", error); 967 968 /* 969 * Finish up. 970 */ 971 if (error) { 972 hammer_rel_inode(nip, 0); 973 hammer_done_transaction(&trans); 974 *ap->a_vpp = NULL; 975 } else { 976 error = hammer_get_vnode(nip, ap->a_vpp); 977 hammer_done_transaction(&trans); 978 hammer_rel_inode(nip, 0); 979 if (error == 0) { 980 cache_setunresolved(ap->a_nch); 981 cache_setvp(ap->a_nch, *ap->a_vpp); 982 } 983 hammer_knote(ap->a_dvp, NOTE_WRITE); 984 } 985 lwkt_reltoken(&hmp->fs_token); 986 return (error); 987 } 988 989 /* 990 * hammer_vop_getattr { vp, vap } 991 * 992 * Retrieve an inode's attribute information. When accessing inodes 993 * historically we fake the atime field to ensure consistent results. 994 * The atime field is stored in the B-Tree element and allowed to be 995 * updated without cycling the element. 996 * 997 * MPSAFE - does not require fs_token 998 */ 999 static 1000 int 1001 hammer_vop_getattr(struct vop_getattr_args *ap) 1002 { 1003 hammer_inode_t ip = VTOI(ap->a_vp); 1004 struct vattr *vap = ap->a_vap; 1005 1006 /* 1007 * We want the fsid to be different when accessing a filesystem 1008 * with different as-of's so programs like diff don't think 1009 * the files are the same. 1010 * 1011 * We also want the fsid to be the same when comparing snapshots, 1012 * or when comparing mirrors (which might be backed by different 1013 * physical devices). HAMMER fsids are based on the PFS's 1014 * shared_uuid field. 1015 * 1016 * XXX there is a chance of collision here. The va_fsid reported 1017 * by stat is different from the more involved fsid used in the 1018 * mount structure. 1019 */ 1020 hammer_lock_sh(&ip->lock); 1021 vap->va_fsid = ip->pfsm->fsid_udev ^ (uint32_t)ip->obj_asof ^ 1022 (uint32_t)(ip->obj_asof >> 32); 1023 1024 vap->va_fileid = ip->ino_leaf.base.obj_id; 1025 vap->va_mode = ip->ino_data.mode; 1026 vap->va_nlink = ip->ino_data.nlinks; 1027 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid); 1028 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid); 1029 vap->va_rmajor = 0; 1030 vap->va_rminor = 0; 1031 vap->va_size = ip->ino_data.size; 1032 1033 /* 1034 * Special case for @@PFS softlinks. The actual size of the 1035 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes. 1036 * or for MAX_TID is "@@-1:%05d" == 10 bytes. 1037 * 1038 * Note that userspace hammer command does not allow users to 1039 * create a @@PFS softlink under an existing other PFS (id!=0) 1040 * so the ip localization here for @@PFS softlink is always 0. 1041 */ 1042 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK && 1043 ip->ino_data.size == 10 && 1044 ip->obj_asof == HAMMER_MAX_TID && 1045 ip->obj_localization == HAMMER_DEF_LOCALIZATION && 1046 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) { 1047 if (hammer_is_pfs_slave(&ip->pfsm->pfsd)) 1048 vap->va_size = 26; 1049 else 1050 vap->va_size = 10; 1051 } 1052 1053 /* 1054 * We must provide a consistent atime and mtime for snapshots 1055 * so people can do a 'tar cf - ... | md5' on them and get 1056 * consistent results. 1057 */ 1058 if (ip->flags & HAMMER_INODE_RO) { 1059 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime); 1060 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime); 1061 } else { 1062 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime); 1063 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime); 1064 } 1065 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime); 1066 vap->va_flags = ip->ino_data.uflags; 1067 vap->va_gen = 1; /* hammer inums are unique for all time */ 1068 vap->va_blocksize = HAMMER_BUFSIZE; 1069 if (ip->ino_data.size >= HAMMER_XDEMARC) { 1070 vap->va_bytes = HAMMER_XBUFSIZE64_DOALIGN(ip->ino_data.size); 1071 } else if (ip->ino_data.size > HAMMER_HBUFSIZE) { 1072 vap->va_bytes = HAMMER_BUFSIZE64_DOALIGN(ip->ino_data.size); 1073 } else { 1074 vap->va_bytes = HAMMER_DATA_DOALIGN(ip->ino_data.size); 1075 } 1076 1077 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type); 1078 vap->va_filerev = 0; /* XXX */ 1079 vap->va_uid_uuid = ip->ino_data.uid; 1080 vap->va_gid_uuid = ip->ino_data.gid; 1081 vap->va_fsid_uuid = ip->hmp->fsid; 1082 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | 1083 VA_FSID_UUID_VALID; 1084 1085 switch (ip->ino_data.obj_type) { 1086 case HAMMER_OBJTYPE_CDEV: 1087 case HAMMER_OBJTYPE_BDEV: 1088 vap->va_rmajor = ip->ino_data.rmajor; 1089 vap->va_rminor = ip->ino_data.rminor; 1090 break; 1091 default: 1092 break; 1093 } 1094 hammer_unlock(&ip->lock); 1095 return(0); 1096 } 1097 1098 /* 1099 * hammer_vop_nresolve { nch, dvp, cred } 1100 * 1101 * Locate the requested directory entry. 1102 */ 1103 static 1104 int 1105 hammer_vop_nresolve(struct vop_nresolve_args *ap) 1106 { 1107 struct hammer_transaction trans; 1108 struct namecache *ncp; 1109 hammer_mount_t hmp; 1110 hammer_inode_t dip; 1111 hammer_inode_t ip; 1112 hammer_tid_t asof; 1113 struct hammer_cursor cursor; 1114 struct vnode *vp; 1115 int64_t namekey; 1116 int error; 1117 int i; 1118 int nlen; 1119 int flags; 1120 int ispfs; 1121 int64_t obj_id; 1122 uint32_t localization; 1123 uint32_t max_iterations; 1124 1125 /* 1126 * Misc initialization, plus handle as-of name extensions. Look for 1127 * the '@@' extension. Note that as-of files and directories cannot 1128 * be modified. 1129 */ 1130 dip = VTOI(ap->a_dvp); 1131 ncp = ap->a_nch->ncp; 1132 asof = dip->obj_asof; 1133 localization = dip->obj_localization; /* for code consistency */ 1134 nlen = ncp->nc_nlen; 1135 flags = dip->flags & HAMMER_INODE_RO; 1136 ispfs = 0; 1137 hmp = dip->hmp; 1138 1139 lwkt_gettoken(&hmp->fs_token); 1140 hammer_simple_transaction(&trans, hmp); 1141 1142 for (i = 0; i < nlen; ++i) { 1143 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') { 1144 error = hammer_str_to_tid(ncp->nc_name + i + 2, 1145 &ispfs, &asof, &localization); 1146 if (error != 0) { 1147 i = nlen; 1148 break; 1149 } 1150 if (asof != HAMMER_MAX_TID) 1151 flags |= HAMMER_INODE_RO; 1152 break; 1153 } 1154 } 1155 nlen = i; 1156 1157 /* 1158 * If this is a PFS we dive into the PFS root inode 1159 */ 1160 if (ispfs && nlen == 0) { 1161 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT, 1162 asof, localization, 1163 flags, &error); 1164 if (error == 0) { 1165 error = hammer_get_vnode(ip, &vp); 1166 hammer_rel_inode(ip, 0); 1167 } else { 1168 vp = NULL; 1169 } 1170 if (error == 0) { 1171 vn_unlock(vp); 1172 cache_setvp(ap->a_nch, vp); 1173 vrele(vp); 1174 } 1175 goto done; 1176 } 1177 1178 /* 1179 * If there is no path component the time extension is relative to dip. 1180 * e.g. "fubar/@@<snapshot>" 1181 * 1182 * "." is handled by the kernel, but ".@@<snapshot>" is not. 1183 * e.g. "fubar/.@@<snapshot>" 1184 * 1185 * ".." is handled by the kernel. We do not currently handle 1186 * "..@<snapshot>". 1187 */ 1188 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) { 1189 ip = hammer_get_inode(&trans, dip, dip->obj_id, 1190 asof, dip->obj_localization, 1191 flags, &error); 1192 if (error == 0) { 1193 error = hammer_get_vnode(ip, &vp); 1194 hammer_rel_inode(ip, 0); 1195 } else { 1196 vp = NULL; 1197 } 1198 if (error == 0) { 1199 vn_unlock(vp); 1200 cache_setvp(ap->a_nch, vp); 1201 vrele(vp); 1202 } 1203 goto done; 1204 } 1205 1206 /* 1207 * Calculate the namekey and setup the key range for the scan. This 1208 * works kinda like a chained hash table where the lower 32 bits 1209 * of the namekey synthesize the chain. 1210 * 1211 * The key range is inclusive of both key_beg and key_end. 1212 */ 1213 namekey = hammer_direntry_namekey(dip, ncp->nc_name, nlen, 1214 &max_iterations); 1215 1216 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip); 1217 cursor.key_beg.localization = dip->obj_localization | 1218 hammer_dir_localization(dip); 1219 cursor.key_beg.obj_id = dip->obj_id; 1220 cursor.key_beg.key = namekey; 1221 cursor.key_beg.create_tid = 0; 1222 cursor.key_beg.delete_tid = 0; 1223 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; 1224 cursor.key_beg.obj_type = 0; 1225 1226 cursor.key_end = cursor.key_beg; 1227 cursor.key_end.key += max_iterations; 1228 cursor.asof = asof; 1229 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; 1230 1231 /* 1232 * Scan all matching records (the chain), locate the one matching 1233 * the requested path component. 1234 * 1235 * The hammer_ip_*() functions merge in-memory records with on-disk 1236 * records for the purposes of the search. 1237 */ 1238 obj_id = 0; 1239 localization = HAMMER_DEF_LOCALIZATION; 1240 1241 if (error == 0) { 1242 error = hammer_ip_first(&cursor); 1243 while (error == 0) { 1244 error = hammer_ip_resolve_data(&cursor); 1245 if (error) 1246 break; 1247 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF && 1248 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) { 1249 obj_id = cursor.data->entry.obj_id; 1250 localization = cursor.data->entry.localization; 1251 break; 1252 } 1253 error = hammer_ip_next(&cursor); 1254 } 1255 } 1256 hammer_done_cursor(&cursor); 1257 1258 /* 1259 * Lookup the obj_id. This should always succeed. If it does not 1260 * the filesystem may be damaged and we return a dummy inode. 1261 */ 1262 if (error == 0) { 1263 ip = hammer_get_inode(&trans, dip, obj_id, 1264 asof, localization, 1265 flags, &error); 1266 if (error == ENOENT) { 1267 hkprintf("WARNING: Missing inode for dirent \"%s\"\n" 1268 "\tobj_id = %016jx, asof=%016jx, lo=%08x\n", 1269 ncp->nc_name, 1270 (intmax_t)obj_id, (intmax_t)asof, 1271 localization); 1272 error = 0; 1273 ip = hammer_get_dummy_inode(&trans, dip, obj_id, 1274 asof, localization, 1275 flags, &error); 1276 } 1277 if (error == 0) { 1278 error = hammer_get_vnode(ip, &vp); 1279 hammer_rel_inode(ip, 0); 1280 } else { 1281 vp = NULL; 1282 } 1283 if (error == 0) { 1284 vn_unlock(vp); 1285 cache_setvp(ap->a_nch, vp); 1286 vrele(vp); 1287 } 1288 } else if (error == ENOENT) { 1289 cache_setvp(ap->a_nch, NULL); 1290 } 1291 done: 1292 hammer_done_transaction(&trans); 1293 lwkt_reltoken(&hmp->fs_token); 1294 return (error); 1295 } 1296 1297 /* 1298 * hammer_vop_nlookupdotdot { dvp, vpp, cred } 1299 * 1300 * Locate the parent directory of a directory vnode. 1301 * 1302 * dvp is referenced but not locked. *vpp must be returned referenced and 1303 * locked. A parent_obj_id of 0 indicates that we are at the root. 1304 * 1305 * NOTE: as-of sequences are not linked into the directory structure. If 1306 * we are at the root with a different asof then the mount point, reload 1307 * the same directory with the mount point's asof. I'm not sure what this 1308 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not 1309 * get confused, but it hasn't been tested. 1310 */ 1311 static 1312 int 1313 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) 1314 { 1315 struct hammer_transaction trans; 1316 hammer_inode_t dip; 1317 hammer_inode_t ip; 1318 hammer_mount_t hmp; 1319 int64_t parent_obj_id; 1320 uint32_t parent_obj_localization; 1321 hammer_tid_t asof; 1322 int error; 1323 1324 dip = VTOI(ap->a_dvp); 1325 asof = dip->obj_asof; 1326 hmp = dip->hmp; 1327 1328 /* 1329 * Whos are parent? This could be the root of a pseudo-filesystem 1330 * whos parent is in another localization domain. 1331 */ 1332 lwkt_gettoken(&hmp->fs_token); 1333 parent_obj_id = dip->ino_data.parent_obj_id; 1334 if (dip->obj_id == HAMMER_OBJID_ROOT) 1335 parent_obj_localization = HAMMER_DEF_LOCALIZATION; 1336 else 1337 parent_obj_localization = dip->obj_localization; 1338 1339 /* 1340 * It's probably a PFS root when dip->ino_data.parent_obj_id is 0. 1341 */ 1342 if (parent_obj_id == 0) { 1343 if (dip->obj_id == HAMMER_OBJID_ROOT && 1344 asof != hmp->asof) { 1345 parent_obj_id = dip->obj_id; 1346 asof = hmp->asof; 1347 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK); 1348 ksnprintf(*ap->a_fakename, 19, "0x%016jx", 1349 (intmax_t)dip->obj_asof); 1350 } else { 1351 *ap->a_vpp = NULL; 1352 lwkt_reltoken(&hmp->fs_token); 1353 return ENOENT; 1354 } 1355 } 1356 1357 hammer_simple_transaction(&trans, hmp); 1358 1359 ip = hammer_get_inode(&trans, dip, parent_obj_id, 1360 asof, parent_obj_localization, 1361 dip->flags, &error); 1362 if (ip) { 1363 error = hammer_get_vnode(ip, ap->a_vpp); 1364 hammer_rel_inode(ip, 0); 1365 } else { 1366 *ap->a_vpp = NULL; 1367 } 1368 hammer_done_transaction(&trans); 1369 lwkt_reltoken(&hmp->fs_token); 1370 return (error); 1371 } 1372 1373 /* 1374 * hammer_vop_nlink { nch, dvp, vp, cred } 1375 */ 1376 static 1377 int 1378 hammer_vop_nlink(struct vop_nlink_args *ap) 1379 { 1380 struct hammer_transaction trans; 1381 hammer_inode_t dip; 1382 hammer_inode_t ip; 1383 struct nchandle *nch; 1384 hammer_mount_t hmp; 1385 int error; 1386 1387 if (ap->a_dvp->v_mount != ap->a_vp->v_mount) 1388 return(EXDEV); 1389 1390 nch = ap->a_nch; 1391 dip = VTOI(ap->a_dvp); 1392 ip = VTOI(ap->a_vp); 1393 hmp = dip->hmp; 1394 1395 if (dip->obj_localization != ip->obj_localization) 1396 return(EXDEV); 1397 1398 if (dip->flags & HAMMER_INODE_RO) 1399 return (EROFS); 1400 if (ip->flags & HAMMER_INODE_RO) 1401 return (EROFS); 1402 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) 1403 return (error); 1404 1405 /* 1406 * Create a transaction to cover the operations we perform. 1407 */ 1408 lwkt_gettoken(&hmp->fs_token); 1409 hammer_start_transaction(&trans, hmp); 1410 1411 /* 1412 * Add the filesystem object to the directory. Note that neither 1413 * dip nor ip are referenced or locked, but their vnodes are 1414 * referenced. This function will bump the inode's link count. 1415 */ 1416 error = hammer_ip_add_direntry(&trans, dip, 1417 nch->ncp->nc_name, nch->ncp->nc_nlen, 1418 ip); 1419 1420 /* 1421 * Finish up. 1422 */ 1423 if (error == 0) { 1424 cache_setunresolved(nch); 1425 cache_setvp(nch, ap->a_vp); 1426 } 1427 hammer_done_transaction(&trans); 1428 hammer_knote(ap->a_vp, NOTE_LINK); 1429 hammer_knote(ap->a_dvp, NOTE_WRITE); 1430 lwkt_reltoken(&hmp->fs_token); 1431 return (error); 1432 } 1433 1434 /* 1435 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap } 1436 * 1437 * The operating system has already ensured that the directory entry 1438 * does not exist and done all appropriate namespace locking. 1439 */ 1440 static 1441 int 1442 hammer_vop_nmkdir(struct vop_nmkdir_args *ap) 1443 { 1444 struct hammer_transaction trans; 1445 hammer_inode_t dip; 1446 hammer_inode_t nip; 1447 struct nchandle *nch; 1448 hammer_mount_t hmp; 1449 int error; 1450 1451 nch = ap->a_nch; 1452 dip = VTOI(ap->a_dvp); 1453 hmp = dip->hmp; 1454 1455 if (dip->flags & HAMMER_INODE_RO) 1456 return (EROFS); 1457 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) 1458 return (error); 1459 1460 /* 1461 * Create a transaction to cover the operations we perform. 1462 */ 1463 lwkt_gettoken(&hmp->fs_token); 1464 hammer_start_transaction(&trans, hmp); 1465 1466 /* 1467 * Create a new filesystem object of the requested type. The 1468 * returned inode will be referenced but not locked. 1469 */ 1470 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, 1471 dip, nch->ncp->nc_name, nch->ncp->nc_nlen, 1472 NULL, &nip); 1473 if (error) { 1474 hammer_done_transaction(&trans); 1475 *ap->a_vpp = NULL; 1476 lwkt_reltoken(&hmp->fs_token); 1477 return (error); 1478 } 1479 /* 1480 * Add the new filesystem object to the directory. This will also 1481 * bump the inode's link count. 1482 */ 1483 error = hammer_ip_add_direntry(&trans, dip, 1484 nch->ncp->nc_name, nch->ncp->nc_nlen, 1485 nip); 1486 if (error) 1487 hkprintf("hammer_mkdir (add) error %d\n", error); 1488 1489 /* 1490 * Finish up. 1491 */ 1492 if (error) { 1493 hammer_rel_inode(nip, 0); 1494 *ap->a_vpp = NULL; 1495 } else { 1496 error = hammer_get_vnode(nip, ap->a_vpp); 1497 hammer_rel_inode(nip, 0); 1498 if (error == 0) { 1499 cache_setunresolved(ap->a_nch); 1500 cache_setvp(ap->a_nch, *ap->a_vpp); 1501 } 1502 } 1503 hammer_done_transaction(&trans); 1504 if (error == 0) 1505 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 1506 lwkt_reltoken(&hmp->fs_token); 1507 return (error); 1508 } 1509 1510 /* 1511 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap } 1512 * 1513 * The operating system has already ensured that the directory entry 1514 * does not exist and done all appropriate namespace locking. 1515 */ 1516 static 1517 int 1518 hammer_vop_nmknod(struct vop_nmknod_args *ap) 1519 { 1520 struct hammer_transaction trans; 1521 hammer_inode_t dip; 1522 hammer_inode_t nip; 1523 struct nchandle *nch; 1524 hammer_mount_t hmp; 1525 int error; 1526 1527 nch = ap->a_nch; 1528 dip = VTOI(ap->a_dvp); 1529 hmp = dip->hmp; 1530 1531 if (dip->flags & HAMMER_INODE_RO) 1532 return (EROFS); 1533 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) 1534 return (error); 1535 1536 /* 1537 * Create a transaction to cover the operations we perform. 1538 */ 1539 lwkt_gettoken(&hmp->fs_token); 1540 hammer_start_transaction(&trans, hmp); 1541 1542 /* 1543 * Create a new filesystem object of the requested type. The 1544 * returned inode will be referenced but not locked. 1545 * 1546 * If mknod specifies a directory a pseudo-fs is created. 1547 */ 1548 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, 1549 dip, nch->ncp->nc_name, nch->ncp->nc_nlen, 1550 NULL, &nip); 1551 if (error) { 1552 hammer_done_transaction(&trans); 1553 *ap->a_vpp = NULL; 1554 lwkt_reltoken(&hmp->fs_token); 1555 return (error); 1556 } 1557 1558 /* 1559 * Add the new filesystem object to the directory. This will also 1560 * bump the inode's link count. 1561 */ 1562 error = hammer_ip_add_direntry(&trans, dip, 1563 nch->ncp->nc_name, nch->ncp->nc_nlen, 1564 nip); 1565 1566 /* 1567 * Finish up. 1568 */ 1569 if (error) { 1570 hammer_rel_inode(nip, 0); 1571 *ap->a_vpp = NULL; 1572 } else { 1573 error = hammer_get_vnode(nip, ap->a_vpp); 1574 hammer_rel_inode(nip, 0); 1575 if (error == 0) { 1576 cache_setunresolved(ap->a_nch); 1577 cache_setvp(ap->a_nch, *ap->a_vpp); 1578 } 1579 } 1580 hammer_done_transaction(&trans); 1581 if (error == 0) 1582 hammer_knote(ap->a_dvp, NOTE_WRITE); 1583 lwkt_reltoken(&hmp->fs_token); 1584 return (error); 1585 } 1586 1587 /* 1588 * hammer_vop_open { vp, mode, cred, fp } 1589 * 1590 * MPSAFE (does not require fs_token) 1591 */ 1592 static 1593 int 1594 hammer_vop_open(struct vop_open_args *ap) 1595 { 1596 hammer_inode_t ip; 1597 1598 ip = VTOI(ap->a_vp); 1599 1600 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO)) 1601 return (EROFS); 1602 return(vop_stdopen(ap)); 1603 } 1604 1605 /* 1606 * hammer_vop_print { vp } 1607 */ 1608 static 1609 int 1610 hammer_vop_print(struct vop_print_args *ap) 1611 { 1612 return EOPNOTSUPP; 1613 } 1614 1615 /* 1616 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies } 1617 */ 1618 static 1619 int 1620 hammer_vop_readdir(struct vop_readdir_args *ap) 1621 { 1622 struct hammer_transaction trans; 1623 struct hammer_cursor cursor; 1624 hammer_inode_t ip; 1625 hammer_mount_t hmp; 1626 struct uio *uio; 1627 hammer_base_elm_t base; 1628 int error; 1629 int cookie_index; 1630 int ncookies; 1631 off_t *cookies; 1632 off_t saveoff; 1633 int r; 1634 int dtype; 1635 1636 ip = VTOI(ap->a_vp); 1637 uio = ap->a_uio; 1638 saveoff = uio->uio_offset; 1639 hmp = ip->hmp; 1640 1641 if (ap->a_ncookies) { 1642 ncookies = uio->uio_resid / 16 + 1; 1643 if (ncookies > 1024) 1644 ncookies = 1024; 1645 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); 1646 cookie_index = 0; 1647 } else { 1648 ncookies = -1; 1649 cookies = NULL; 1650 cookie_index = 0; 1651 } 1652 1653 lwkt_gettoken(&hmp->fs_token); 1654 hammer_simple_transaction(&trans, hmp); 1655 1656 /* 1657 * Handle artificial entries 1658 * 1659 * It should be noted that the minimum value for a directory 1660 * hash key on-media is 0x0000000100000000, so we can use anything 1661 * less then that to represent our 'special' key space. 1662 */ 1663 error = 0; 1664 if (saveoff == 0) { 1665 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, "."); 1666 if (r) 1667 goto done; 1668 if (cookies) 1669 cookies[cookie_index] = saveoff; 1670 ++saveoff; 1671 ++cookie_index; 1672 if (cookie_index == ncookies) 1673 goto done; 1674 } 1675 if (saveoff == 1) { 1676 if (ip->ino_data.parent_obj_id) { 1677 r = vop_write_dirent(&error, uio, 1678 ip->ino_data.parent_obj_id, 1679 DT_DIR, 2, ".."); 1680 } else { 1681 r = vop_write_dirent(&error, uio, 1682 ip->obj_id, DT_DIR, 2, ".."); 1683 } 1684 if (r) 1685 goto done; 1686 if (cookies) 1687 cookies[cookie_index] = saveoff; 1688 ++saveoff; 1689 ++cookie_index; 1690 if (cookie_index == ncookies) 1691 goto done; 1692 } 1693 1694 /* 1695 * Key range (begin and end inclusive) to scan. Directory keys 1696 * directly translate to a 64 bit 'seek' position. 1697 */ 1698 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); 1699 cursor.key_beg.localization = ip->obj_localization | 1700 hammer_dir_localization(ip); 1701 cursor.key_beg.obj_id = ip->obj_id; 1702 cursor.key_beg.create_tid = 0; 1703 cursor.key_beg.delete_tid = 0; 1704 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; 1705 cursor.key_beg.obj_type = 0; 1706 cursor.key_beg.key = saveoff; 1707 1708 cursor.key_end = cursor.key_beg; 1709 cursor.key_end.key = HAMMER_MAX_KEY; 1710 cursor.asof = ip->obj_asof; 1711 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; 1712 1713 error = hammer_ip_first(&cursor); 1714 1715 while (error == 0) { 1716 error = hammer_ip_resolve_data(&cursor); 1717 if (error) 1718 break; 1719 base = &cursor.leaf->base; 1720 saveoff = base->key; 1721 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF); 1722 1723 if (base->obj_id != ip->obj_id) 1724 hpanic("bad record at %p", cursor.node); 1725 1726 dtype = hammer_get_dtype(cursor.leaf->base.obj_type); 1727 r = vop_write_dirent( 1728 &error, uio, cursor.data->entry.obj_id, 1729 dtype, 1730 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF , 1731 (void *)cursor.data->entry.name); 1732 if (r) 1733 break; 1734 ++saveoff; 1735 if (cookies) 1736 cookies[cookie_index] = base->key; 1737 ++cookie_index; 1738 if (cookie_index == ncookies) 1739 break; 1740 error = hammer_ip_next(&cursor); 1741 } 1742 hammer_done_cursor(&cursor); 1743 1744 done: 1745 hammer_done_transaction(&trans); 1746 1747 if (ap->a_eofflag) 1748 *ap->a_eofflag = (error == ENOENT); 1749 uio->uio_offset = saveoff; 1750 if (error && cookie_index == 0) { 1751 if (error == ENOENT) 1752 error = 0; 1753 if (cookies) { 1754 kfree(cookies, M_TEMP); 1755 *ap->a_ncookies = 0; 1756 *ap->a_cookies = NULL; 1757 } 1758 } else { 1759 if (error == ENOENT) 1760 error = 0; 1761 if (cookies) { 1762 *ap->a_ncookies = cookie_index; 1763 *ap->a_cookies = cookies; 1764 } 1765 } 1766 lwkt_reltoken(&hmp->fs_token); 1767 return(error); 1768 } 1769 1770 /* 1771 * hammer_vop_readlink { vp, uio, cred } 1772 */ 1773 static 1774 int 1775 hammer_vop_readlink(struct vop_readlink_args *ap) 1776 { 1777 struct hammer_transaction trans; 1778 struct hammer_cursor cursor; 1779 hammer_inode_t ip; 1780 hammer_mount_t hmp; 1781 char buf[32]; 1782 uint32_t localization; 1783 hammer_pseudofs_inmem_t pfsm; 1784 int error; 1785 1786 ip = VTOI(ap->a_vp); 1787 hmp = ip->hmp; 1788 1789 lwkt_gettoken(&hmp->fs_token); 1790 1791 /* 1792 * Shortcut if the symlink data was stuffed into ino_data. 1793 * 1794 * Also expand special "@@PFS%05d" softlinks (expansion only 1795 * occurs for non-historical (current) accesses made from the 1796 * primary filesystem). 1797 * 1798 * Note that userspace hammer command does not allow users to 1799 * create a @@PFS softlink under an existing other PFS (id!=0) 1800 * so the ip localization here for @@PFS softlink is always 0. 1801 */ 1802 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) { 1803 char *ptr; 1804 int bytes; 1805 1806 ptr = ip->ino_data.ext.symlink; 1807 bytes = (int)ip->ino_data.size; 1808 if (bytes == 10 && 1809 ip->obj_asof == HAMMER_MAX_TID && 1810 ip->obj_localization == HAMMER_DEF_LOCALIZATION && 1811 strncmp(ptr, "@@PFS", 5) == 0) { 1812 hammer_simple_transaction(&trans, hmp); 1813 bcopy(ptr + 5, buf, 5); 1814 buf[5] = 0; 1815 localization = pfs_to_lo(strtoul(buf, NULL, 10)); 1816 pfsm = hammer_load_pseudofs(&trans, localization, 1817 &error); 1818 if (error == 0) { 1819 if (hammer_is_pfs_slave(&pfsm->pfsd)) { 1820 /* vap->va_size == 26 */ 1821 ksnprintf(buf, sizeof(buf), 1822 "@@0x%016jx:%05d", 1823 (intmax_t)pfsm->pfsd.sync_end_tid, 1824 lo_to_pfs(localization)); 1825 } else { 1826 /* vap->va_size == 10 */ 1827 ksnprintf(buf, sizeof(buf), 1828 "@@-1:%05d", 1829 lo_to_pfs(localization)); 1830 } 1831 ptr = buf; 1832 bytes = strlen(buf); 1833 } 1834 if (pfsm) 1835 hammer_rel_pseudofs(hmp, pfsm); 1836 hammer_done_transaction(&trans); 1837 } 1838 error = uiomove(ptr, bytes, ap->a_uio); 1839 lwkt_reltoken(&hmp->fs_token); 1840 return(error); 1841 } 1842 1843 /* 1844 * Long version 1845 */ 1846 hammer_simple_transaction(&trans, hmp); 1847 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); 1848 1849 /* 1850 * Key range (begin and end inclusive) to scan. Directory keys 1851 * directly translate to a 64 bit 'seek' position. 1852 */ 1853 cursor.key_beg.localization = ip->obj_localization | 1854 HAMMER_LOCALIZE_MISC; 1855 cursor.key_beg.obj_id = ip->obj_id; 1856 cursor.key_beg.create_tid = 0; 1857 cursor.key_beg.delete_tid = 0; 1858 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX; 1859 cursor.key_beg.obj_type = 0; 1860 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK; 1861 cursor.asof = ip->obj_asof; 1862 cursor.flags |= HAMMER_CURSOR_ASOF; 1863 1864 error = hammer_ip_lookup(&cursor); 1865 if (error == 0) { 1866 error = hammer_ip_resolve_data(&cursor); 1867 if (error == 0) { 1868 KKASSERT(cursor.leaf->data_len >= 1869 HAMMER_SYMLINK_NAME_OFF); 1870 error = uiomove(cursor.data->symlink.name, 1871 cursor.leaf->data_len - 1872 HAMMER_SYMLINK_NAME_OFF, 1873 ap->a_uio); 1874 } 1875 } 1876 hammer_done_cursor(&cursor); 1877 hammer_done_transaction(&trans); 1878 lwkt_reltoken(&hmp->fs_token); 1879 return(error); 1880 } 1881 1882 /* 1883 * hammer_vop_nremove { nch, dvp, cred } 1884 */ 1885 static 1886 int 1887 hammer_vop_nremove(struct vop_nremove_args *ap) 1888 { 1889 struct hammer_transaction trans; 1890 hammer_inode_t dip; 1891 hammer_mount_t hmp; 1892 int error; 1893 1894 dip = VTOI(ap->a_dvp); 1895 hmp = dip->hmp; 1896 1897 if (hammer_nohistory(dip) == 0 && 1898 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) { 1899 return (error); 1900 } 1901 1902 lwkt_gettoken(&hmp->fs_token); 1903 hammer_start_transaction(&trans, hmp); 1904 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0); 1905 hammer_done_transaction(&trans); 1906 if (error == 0) 1907 hammer_knote(ap->a_dvp, NOTE_WRITE); 1908 lwkt_reltoken(&hmp->fs_token); 1909 return (error); 1910 } 1911 1912 /* 1913 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred } 1914 */ 1915 static 1916 int 1917 hammer_vop_nrename(struct vop_nrename_args *ap) 1918 { 1919 struct hammer_transaction trans; 1920 struct namecache *fncp; 1921 struct namecache *tncp; 1922 hammer_inode_t fdip; 1923 hammer_inode_t tdip; 1924 hammer_inode_t ip; 1925 hammer_mount_t hmp; 1926 struct hammer_cursor cursor; 1927 int64_t namekey; 1928 uint32_t max_iterations; 1929 int nlen, error; 1930 1931 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) 1932 return(EXDEV); 1933 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount) 1934 return(EXDEV); 1935 1936 fdip = VTOI(ap->a_fdvp); 1937 tdip = VTOI(ap->a_tdvp); 1938 fncp = ap->a_fnch->ncp; 1939 tncp = ap->a_tnch->ncp; 1940 ip = VTOI(fncp->nc_vp); 1941 KKASSERT(ip != NULL); 1942 1943 hmp = ip->hmp; 1944 1945 if (fdip->obj_localization != tdip->obj_localization) 1946 return(EXDEV); 1947 if (fdip->obj_localization != ip->obj_localization) 1948 return(EXDEV); 1949 1950 if (fdip->flags & HAMMER_INODE_RO) 1951 return (EROFS); 1952 if (tdip->flags & HAMMER_INODE_RO) 1953 return (EROFS); 1954 if (ip->flags & HAMMER_INODE_RO) 1955 return (EROFS); 1956 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) 1957 return (error); 1958 1959 lwkt_gettoken(&hmp->fs_token); 1960 hammer_start_transaction(&trans, hmp); 1961 1962 /* 1963 * Remove tncp from the target directory and then link ip as 1964 * tncp. XXX pass trans to dounlink 1965 * 1966 * Force the inode sync-time to match the transaction so it is 1967 * in-sync with the creation of the target directory entry. 1968 */ 1969 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, 1970 ap->a_cred, 0, -1); 1971 if (error == 0 || error == ENOENT) { 1972 error = hammer_ip_add_direntry(&trans, tdip, 1973 tncp->nc_name, tncp->nc_nlen, 1974 ip); 1975 if (error == 0) { 1976 ip->ino_data.parent_obj_id = tdip->obj_id; 1977 ip->ino_data.ctime = trans.time; 1978 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY); 1979 } 1980 } 1981 if (error) 1982 goto failed; /* XXX */ 1983 1984 /* 1985 * Locate the record in the originating directory and remove it. 1986 * 1987 * Calculate the namekey and setup the key range for the scan. This 1988 * works kinda like a chained hash table where the lower 32 bits 1989 * of the namekey synthesize the chain. 1990 * 1991 * The key range is inclusive of both key_beg and key_end. 1992 */ 1993 namekey = hammer_direntry_namekey(fdip, fncp->nc_name, fncp->nc_nlen, 1994 &max_iterations); 1995 retry: 1996 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip); 1997 cursor.key_beg.localization = fdip->obj_localization | 1998 hammer_dir_localization(fdip); 1999 cursor.key_beg.obj_id = fdip->obj_id; 2000 cursor.key_beg.key = namekey; 2001 cursor.key_beg.create_tid = 0; 2002 cursor.key_beg.delete_tid = 0; 2003 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; 2004 cursor.key_beg.obj_type = 0; 2005 2006 cursor.key_end = cursor.key_beg; 2007 cursor.key_end.key += max_iterations; 2008 cursor.asof = fdip->obj_asof; 2009 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; 2010 2011 /* 2012 * Scan all matching records (the chain), locate the one matching 2013 * the requested path component. 2014 * 2015 * The hammer_ip_*() functions merge in-memory records with on-disk 2016 * records for the purposes of the search. 2017 */ 2018 error = hammer_ip_first(&cursor); 2019 while (error == 0) { 2020 if (hammer_ip_resolve_data(&cursor) != 0) 2021 break; 2022 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF; 2023 KKASSERT(nlen > 0); 2024 if (fncp->nc_nlen == nlen && 2025 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) { 2026 break; 2027 } 2028 error = hammer_ip_next(&cursor); 2029 } 2030 2031 /* 2032 * If all is ok we have to get the inode so we can adjust nlinks. 2033 * 2034 * WARNING: hammer_ip_del_direntry() may have to terminate the 2035 * cursor to avoid a recursion. It's ok to call hammer_done_cursor() 2036 * twice. 2037 */ 2038 if (error == 0) 2039 error = hammer_ip_del_direntry(&trans, &cursor, fdip, ip); 2040 2041 /* 2042 * XXX A deadlock here will break rename's atomicy for the purposes 2043 * of crash recovery. 2044 */ 2045 if (error == EDEADLK) { 2046 hammer_done_cursor(&cursor); 2047 goto retry; 2048 } 2049 2050 /* 2051 * Cleanup and tell the kernel that the rename succeeded. 2052 * 2053 * NOTE: ip->vp, if non-NULL, cannot be directly referenced 2054 * without formally acquiring the vp since the vp might 2055 * have zero refs on it, or in the middle of a reclaim, 2056 * etc. 2057 */ 2058 hammer_done_cursor(&cursor); 2059 if (error == 0) { 2060 cache_rename(ap->a_fnch, ap->a_tnch); 2061 hammer_knote(ap->a_fdvp, NOTE_WRITE); 2062 hammer_knote(ap->a_tdvp, NOTE_WRITE); 2063 while (ip->vp) { 2064 struct vnode *vp; 2065 2066 error = hammer_get_vnode(ip, &vp); 2067 if (error == 0 && vp) { 2068 vn_unlock(vp); 2069 hammer_knote(ip->vp, NOTE_RENAME); 2070 vrele(vp); 2071 break; 2072 } 2073 hdkprintf("ip/vp race2 avoided\n"); 2074 } 2075 } 2076 2077 failed: 2078 hammer_done_transaction(&trans); 2079 lwkt_reltoken(&hmp->fs_token); 2080 return (error); 2081 } 2082 2083 /* 2084 * hammer_vop_nrmdir { nch, dvp, cred } 2085 */ 2086 static 2087 int 2088 hammer_vop_nrmdir(struct vop_nrmdir_args *ap) 2089 { 2090 struct hammer_transaction trans; 2091 hammer_inode_t dip; 2092 hammer_mount_t hmp; 2093 int error; 2094 2095 dip = VTOI(ap->a_dvp); 2096 hmp = dip->hmp; 2097 2098 if (hammer_nohistory(dip) == 0 && 2099 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) { 2100 return (error); 2101 } 2102 2103 lwkt_gettoken(&hmp->fs_token); 2104 hammer_start_transaction(&trans, hmp); 2105 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1); 2106 hammer_done_transaction(&trans); 2107 if (error == 0) 2108 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); 2109 lwkt_reltoken(&hmp->fs_token); 2110 return (error); 2111 } 2112 2113 /* 2114 * hammer_vop_markatime { vp, cred } 2115 */ 2116 static 2117 int 2118 hammer_vop_markatime(struct vop_markatime_args *ap) 2119 { 2120 struct hammer_transaction trans; 2121 hammer_inode_t ip; 2122 hammer_mount_t hmp; 2123 2124 ip = VTOI(ap->a_vp); 2125 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) 2126 return (EROFS); 2127 if (ip->flags & HAMMER_INODE_RO) 2128 return (EROFS); 2129 hmp = ip->hmp; 2130 if (hmp->mp->mnt_flag & MNT_NOATIME) 2131 return (0); 2132 lwkt_gettoken(&hmp->fs_token); 2133 hammer_start_transaction(&trans, hmp); 2134 2135 ip->ino_data.atime = trans.time; 2136 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME); 2137 hammer_done_transaction(&trans); 2138 hammer_knote(ap->a_vp, NOTE_ATTRIB); 2139 lwkt_reltoken(&hmp->fs_token); 2140 return (0); 2141 } 2142 2143 /* 2144 * hammer_vop_setattr { vp, vap, cred } 2145 */ 2146 static 2147 int 2148 hammer_vop_setattr(struct vop_setattr_args *ap) 2149 { 2150 struct hammer_transaction trans; 2151 hammer_inode_t ip; 2152 struct vattr *vap; 2153 hammer_mount_t hmp; 2154 int modflags; 2155 int error; 2156 int truncating; 2157 int blksize; 2158 int kflags; 2159 #if 0 2160 int64_t aligned_size; 2161 #endif 2162 uint32_t flags; 2163 2164 vap = ap->a_vap; 2165 ip = ap->a_vp->v_data; 2166 modflags = 0; 2167 kflags = 0; 2168 hmp = ip->hmp; 2169 2170 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) 2171 return(EROFS); 2172 if (ip->flags & HAMMER_INODE_RO) 2173 return (EROFS); 2174 if (hammer_nohistory(ip) == 0 && 2175 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) { 2176 return (error); 2177 } 2178 2179 lwkt_gettoken(&hmp->fs_token); 2180 hammer_start_transaction(&trans, hmp); 2181 error = 0; 2182 2183 if (vap->va_flags != VNOVAL) { 2184 flags = ip->ino_data.uflags; 2185 error = vop_helper_setattr_flags(&flags, vap->va_flags, 2186 hammer_to_unix_xid(&ip->ino_data.uid), 2187 ap->a_cred); 2188 if (error == 0) { 2189 if (ip->ino_data.uflags != flags) { 2190 ip->ino_data.uflags = flags; 2191 ip->ino_data.ctime = trans.time; 2192 modflags |= HAMMER_INODE_DDIRTY; 2193 kflags |= NOTE_ATTRIB; 2194 } 2195 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) { 2196 error = 0; 2197 goto done; 2198 } 2199 } 2200 goto done; 2201 } 2202 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) { 2203 error = EPERM; 2204 goto done; 2205 } 2206 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 2207 mode_t cur_mode = ip->ino_data.mode; 2208 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid); 2209 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid); 2210 hammer_uuid_t uuid_uid; 2211 hammer_uuid_t uuid_gid; 2212 2213 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, 2214 ap->a_cred, 2215 &cur_uid, &cur_gid, &cur_mode); 2216 if (error == 0) { 2217 hammer_guid_to_uuid(&uuid_uid, cur_uid); 2218 hammer_guid_to_uuid(&uuid_gid, cur_gid); 2219 if (kuuid_compare(&uuid_uid, &ip->ino_data.uid) || 2220 kuuid_compare(&uuid_gid, &ip->ino_data.gid) || 2221 ip->ino_data.mode != cur_mode) { 2222 ip->ino_data.uid = uuid_uid; 2223 ip->ino_data.gid = uuid_gid; 2224 ip->ino_data.mode = cur_mode; 2225 ip->ino_data.ctime = trans.time; 2226 modflags |= HAMMER_INODE_DDIRTY; 2227 } 2228 kflags |= NOTE_ATTRIB; 2229 } 2230 } 2231 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) { 2232 switch(ap->a_vp->v_type) { 2233 case VREG: 2234 if (vap->va_size == ip->ino_data.size) 2235 break; 2236 2237 /* 2238 * Log the operation if in fast-fsync mode or if 2239 * there are unterminated redo write records present. 2240 * 2241 * The second check is needed so the recovery code 2242 * properly truncates write redos even if nominal 2243 * REDO operations is turned off due to excessive 2244 * writes, because the related records might be 2245 * destroyed and never lay down a TERM_WRITE. 2246 */ 2247 if ((ip->flags & HAMMER_INODE_REDO) || 2248 (ip->flags & HAMMER_INODE_RDIRTY)) { 2249 error = hammer_generate_redo(&trans, ip, 2250 vap->va_size, 2251 HAMMER_REDO_TRUNC, 2252 NULL, 0); 2253 } 2254 blksize = hammer_blocksize(vap->va_size); 2255 2256 /* 2257 * XXX break atomicy, we can deadlock the backend 2258 * if we do not release the lock. Probably not a 2259 * big deal here. 2260 */ 2261 if (vap->va_size < ip->ino_data.size) { 2262 nvtruncbuf(ap->a_vp, vap->va_size, 2263 blksize, 2264 hammer_blockoff(vap->va_size), 2265 0); 2266 truncating = 1; 2267 kflags |= NOTE_WRITE; 2268 } else { 2269 nvextendbuf(ap->a_vp, 2270 ip->ino_data.size, 2271 vap->va_size, 2272 hammer_blocksize(ip->ino_data.size), 2273 hammer_blocksize(vap->va_size), 2274 hammer_blockoff(ip->ino_data.size), 2275 hammer_blockoff(vap->va_size), 2276 0); 2277 truncating = 0; 2278 kflags |= NOTE_WRITE | NOTE_EXTEND; 2279 } 2280 ip->ino_data.size = vap->va_size; 2281 ip->ino_data.mtime = trans.time; 2282 /* XXX safe to use SDIRTY instead of DDIRTY here? */ 2283 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY; 2284 2285 /* 2286 * On-media truncation is cached in the inode until 2287 * the inode is synchronized. We must immediately 2288 * handle any frontend records. 2289 */ 2290 if (truncating) { 2291 hammer_ip_frontend_trunc(ip, vap->va_size); 2292 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) { 2293 ip->flags |= HAMMER_INODE_TRUNCATED; 2294 ip->trunc_off = vap->va_size; 2295 hammer_inode_dirty(ip); 2296 } else if (ip->trunc_off > vap->va_size) { 2297 ip->trunc_off = vap->va_size; 2298 } 2299 } 2300 2301 #if 0 2302 /* 2303 * When truncating, nvtruncbuf() may have cleaned out 2304 * a portion of the last block on-disk in the buffer 2305 * cache. We must clean out any frontend records 2306 * for blocks beyond the new last block. 2307 */ 2308 aligned_size = (vap->va_size + (blksize - 1)) & 2309 ~(int64_t)(blksize - 1); 2310 if (truncating && vap->va_size < aligned_size) { 2311 aligned_size -= blksize; 2312 hammer_ip_frontend_trunc(ip, aligned_size); 2313 } 2314 #endif 2315 break; 2316 case VDATABASE: 2317 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) { 2318 ip->flags |= HAMMER_INODE_TRUNCATED; 2319 ip->trunc_off = vap->va_size; 2320 hammer_inode_dirty(ip); 2321 } else if (ip->trunc_off > vap->va_size) { 2322 ip->trunc_off = vap->va_size; 2323 } 2324 hammer_ip_frontend_trunc(ip, vap->va_size); 2325 ip->ino_data.size = vap->va_size; 2326 ip->ino_data.mtime = trans.time; 2327 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY; 2328 kflags |= NOTE_ATTRIB; 2329 break; 2330 default: 2331 error = EINVAL; 2332 goto done; 2333 } 2334 break; 2335 } 2336 if (vap->va_atime.tv_sec != VNOVAL) { 2337 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime); 2338 modflags |= HAMMER_INODE_ATIME; 2339 kflags |= NOTE_ATTRIB; 2340 } 2341 if (vap->va_mtime.tv_sec != VNOVAL) { 2342 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime); 2343 modflags |= HAMMER_INODE_MTIME; 2344 kflags |= NOTE_ATTRIB; 2345 } 2346 if (vap->va_mode != (mode_t)VNOVAL) { 2347 mode_t cur_mode = ip->ino_data.mode; 2348 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid); 2349 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid); 2350 2351 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, 2352 cur_uid, cur_gid, &cur_mode); 2353 if (error == 0 && ip->ino_data.mode != cur_mode) { 2354 ip->ino_data.mode = cur_mode; 2355 ip->ino_data.ctime = trans.time; 2356 modflags |= HAMMER_INODE_DDIRTY; 2357 kflags |= NOTE_ATTRIB; 2358 } 2359 } 2360 done: 2361 if (error == 0) 2362 hammer_modify_inode(&trans, ip, modflags); 2363 hammer_done_transaction(&trans); 2364 hammer_knote(ap->a_vp, kflags); 2365 lwkt_reltoken(&hmp->fs_token); 2366 return (error); 2367 } 2368 2369 /* 2370 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target } 2371 */ 2372 static 2373 int 2374 hammer_vop_nsymlink(struct vop_nsymlink_args *ap) 2375 { 2376 struct hammer_transaction trans; 2377 hammer_inode_t dip; 2378 hammer_inode_t nip; 2379 hammer_record_t record; 2380 struct nchandle *nch; 2381 hammer_mount_t hmp; 2382 int error; 2383 int bytes; 2384 2385 ap->a_vap->va_type = VLNK; 2386 2387 nch = ap->a_nch; 2388 dip = VTOI(ap->a_dvp); 2389 hmp = dip->hmp; 2390 2391 if (dip->flags & HAMMER_INODE_RO) 2392 return (EROFS); 2393 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) 2394 return (error); 2395 2396 /* 2397 * Create a transaction to cover the operations we perform. 2398 */ 2399 lwkt_gettoken(&hmp->fs_token); 2400 hammer_start_transaction(&trans, hmp); 2401 2402 /* 2403 * Create a new filesystem object of the requested type. The 2404 * returned inode will be referenced but not locked. 2405 */ 2406 2407 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, 2408 dip, nch->ncp->nc_name, nch->ncp->nc_nlen, 2409 NULL, &nip); 2410 if (error) { 2411 hammer_done_transaction(&trans); 2412 *ap->a_vpp = NULL; 2413 lwkt_reltoken(&hmp->fs_token); 2414 return (error); 2415 } 2416 2417 /* 2418 * Add a record representing the symlink. symlink stores the link 2419 * as pure data, not a string, and is no \0 terminated. 2420 */ 2421 if (error == 0) { 2422 bytes = strlen(ap->a_target); 2423 2424 if (bytes <= HAMMER_INODE_BASESYMLEN) { 2425 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes); 2426 } else { 2427 record = hammer_alloc_mem_record(nip, bytes); 2428 record->type = HAMMER_MEM_RECORD_GENERAL; 2429 2430 record->leaf.base.localization = nip->obj_localization | 2431 HAMMER_LOCALIZE_MISC; 2432 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK; 2433 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX; 2434 record->leaf.data_len = bytes; 2435 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0); 2436 bcopy(ap->a_target, record->data->symlink.name, bytes); 2437 error = hammer_ip_add_record(&trans, record); 2438 } 2439 2440 /* 2441 * Set the file size to the length of the link. 2442 */ 2443 if (error == 0) { 2444 nip->ino_data.size = bytes; 2445 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY); 2446 } 2447 } 2448 if (error == 0) 2449 error = hammer_ip_add_direntry(&trans, dip, nch->ncp->nc_name, 2450 nch->ncp->nc_nlen, nip); 2451 2452 /* 2453 * Finish up. 2454 */ 2455 if (error) { 2456 hammer_rel_inode(nip, 0); 2457 *ap->a_vpp = NULL; 2458 } else { 2459 error = hammer_get_vnode(nip, ap->a_vpp); 2460 hammer_rel_inode(nip, 0); 2461 if (error == 0) { 2462 cache_setunresolved(ap->a_nch); 2463 cache_setvp(ap->a_nch, *ap->a_vpp); 2464 hammer_knote(ap->a_dvp, NOTE_WRITE); 2465 } 2466 } 2467 hammer_done_transaction(&trans); 2468 lwkt_reltoken(&hmp->fs_token); 2469 return (error); 2470 } 2471 2472 /* 2473 * hammer_vop_nwhiteout { nch, dvp, cred, flags } 2474 */ 2475 static 2476 int 2477 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap) 2478 { 2479 struct hammer_transaction trans; 2480 hammer_inode_t dip; 2481 hammer_mount_t hmp; 2482 int error; 2483 2484 dip = VTOI(ap->a_dvp); 2485 hmp = dip->hmp; 2486 2487 if (hammer_nohistory(dip) == 0 && 2488 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) { 2489 return (error); 2490 } 2491 2492 lwkt_gettoken(&hmp->fs_token); 2493 hammer_start_transaction(&trans, hmp); 2494 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, 2495 ap->a_cred, ap->a_flags, -1); 2496 hammer_done_transaction(&trans); 2497 lwkt_reltoken(&hmp->fs_token); 2498 2499 return (error); 2500 } 2501 2502 /* 2503 * hammer_vop_ioctl { vp, command, data, fflag, cred } 2504 */ 2505 static 2506 int 2507 hammer_vop_ioctl(struct vop_ioctl_args *ap) 2508 { 2509 hammer_inode_t ip = ap->a_vp->v_data; 2510 hammer_mount_t hmp = ip->hmp; 2511 int error; 2512 2513 lwkt_gettoken(&hmp->fs_token); 2514 error = hammer_ioctl(ip, ap->a_command, ap->a_data, 2515 ap->a_fflag, ap->a_cred); 2516 lwkt_reltoken(&hmp->fs_token); 2517 return (error); 2518 } 2519 2520 static 2521 int 2522 hammer_vop_mountctl(struct vop_mountctl_args *ap) 2523 { 2524 static const struct mountctl_opt extraopt[] = { 2525 { HMNT_NOHISTORY, "nohistory" }, 2526 { HMNT_MASTERID, "master" }, 2527 { HMNT_NOMIRROR, "nomirror" }, 2528 { 0, NULL} 2529 2530 }; 2531 hammer_mount_t hmp; 2532 struct mount *mp; 2533 int usedbytes; 2534 int error; 2535 2536 error = 0; 2537 usedbytes = 0; 2538 mp = ap->a_head.a_ops->head.vv_mount; 2539 KKASSERT(mp->mnt_data != NULL); 2540 hmp = (hammer_mount_t)mp->mnt_data; 2541 2542 lwkt_gettoken(&hmp->fs_token); 2543 2544 switch(ap->a_op) { 2545 case MOUNTCTL_SET_EXPORT: 2546 if (ap->a_ctllen != sizeof(struct export_args)) 2547 error = EINVAL; 2548 else 2549 error = hammer_vfs_export(mp, ap->a_op, 2550 (const struct export_args *)ap->a_ctl); 2551 break; 2552 case MOUNTCTL_MOUNTFLAGS: 2553 /* 2554 * Call standard mountctl VOP function 2555 * so we get user mount flags. 2556 */ 2557 error = vop_stdmountctl(ap); 2558 if (error) 2559 break; 2560 2561 usedbytes = *ap->a_res; 2562 2563 if (usedbytes > 0 && usedbytes < ap->a_buflen) { 2564 usedbytes += vfs_flagstostr(hmp->hflags, extraopt, 2565 ap->a_buf, 2566 ap->a_buflen - usedbytes, 2567 &error); 2568 } 2569 2570 *ap->a_res += usedbytes; 2571 break; 2572 default: 2573 error = vop_stdmountctl(ap); 2574 break; 2575 } 2576 lwkt_reltoken(&hmp->fs_token); 2577 return(error); 2578 } 2579 2580 /* 2581 * hammer_vop_strategy { vp, bio } 2582 * 2583 * Strategy call, used for regular file read & write only. Note that the 2584 * bp may represent a cluster. 2585 * 2586 * To simplify operation and allow better optimizations in the future, 2587 * this code does not make any assumptions with regards to buffer alignment 2588 * or size. 2589 */ 2590 static 2591 int 2592 hammer_vop_strategy(struct vop_strategy_args *ap) 2593 { 2594 struct buf *bp; 2595 int error; 2596 2597 bp = ap->a_bio->bio_buf; 2598 2599 switch(bp->b_cmd) { 2600 case BUF_CMD_READ: 2601 error = hammer_vop_strategy_read(ap); 2602 break; 2603 case BUF_CMD_WRITE: 2604 error = hammer_vop_strategy_write(ap); 2605 break; 2606 default: 2607 bp->b_error = error = EINVAL; 2608 bp->b_flags |= B_ERROR; 2609 biodone(ap->a_bio); 2610 break; 2611 } 2612 2613 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */ 2614 2615 return (error); 2616 } 2617 2618 /* 2619 * Read from a regular file. Iterate the related records and fill in the 2620 * BIO/BUF. Gaps are zero-filled. 2621 * 2622 * The support code in hammer_object.c should be used to deal with mixed 2623 * in-memory and on-disk records. 2624 * 2625 * NOTE: Can be called from the cluster code with an oversized buf. 2626 * 2627 * XXX atime update 2628 */ 2629 static 2630 int 2631 hammer_vop_strategy_read(struct vop_strategy_args *ap) 2632 { 2633 struct hammer_transaction trans; 2634 hammer_inode_t ip; 2635 hammer_inode_t dip; 2636 hammer_mount_t hmp; 2637 struct hammer_cursor cursor; 2638 hammer_base_elm_t base; 2639 hammer_off_t disk_offset; 2640 struct bio *bio; 2641 struct bio *nbio; 2642 struct buf *bp; 2643 int64_t rec_offset; 2644 int64_t ran_end; 2645 int64_t tmp64; 2646 int error; 2647 int boff; 2648 int roff; 2649 int n; 2650 int isdedupable; 2651 2652 bio = ap->a_bio; 2653 bp = bio->bio_buf; 2654 ip = ap->a_vp->v_data; 2655 hmp = ip->hmp; 2656 2657 /* 2658 * The zone-2 disk offset may have been set by the cluster code via 2659 * a BMAP operation, or else should be NOOFFSET. 2660 * 2661 * Checking the high bits for a match against zone-2 should suffice. 2662 * 2663 * In cases where a lot of data duplication is present it may be 2664 * more beneficial to drop through and doubule-buffer through the 2665 * device. 2666 */ 2667 nbio = push_bio(bio); 2668 if (hammer_is_zone_large_data(nbio->bio_offset)) { 2669 if (hammer_double_buffer == 0) { 2670 lwkt_gettoken(&hmp->fs_token); 2671 error = hammer_io_direct_read(hmp, nbio, NULL); 2672 lwkt_reltoken(&hmp->fs_token); 2673 return (error); 2674 } 2675 2676 /* 2677 * Try to shortcut requests for double_buffer mode too. 2678 * Since this mode runs through the device buffer cache 2679 * only compatible buffer sizes (meaning those generated 2680 * by normal filesystem buffers) are legal. 2681 */ 2682 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) { 2683 lwkt_gettoken(&hmp->fs_token); 2684 error = hammer_io_indirect_read(hmp, nbio, NULL); 2685 lwkt_reltoken(&hmp->fs_token); 2686 return (error); 2687 } 2688 } 2689 2690 /* 2691 * Well, that sucked. Do it the hard way. If all the stars are 2692 * aligned we may still be able to issue a direct-read. 2693 */ 2694 lwkt_gettoken(&hmp->fs_token); 2695 hammer_simple_transaction(&trans, hmp); 2696 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); 2697 2698 /* 2699 * Key range (begin and end inclusive) to scan. Note that the key's 2700 * stored in the actual records represent BASE+LEN, not BASE. The 2701 * first record containing bio_offset will have a key > bio_offset. 2702 */ 2703 cursor.key_beg.localization = ip->obj_localization | 2704 HAMMER_LOCALIZE_MISC; 2705 cursor.key_beg.obj_id = ip->obj_id; 2706 cursor.key_beg.create_tid = 0; 2707 cursor.key_beg.delete_tid = 0; 2708 cursor.key_beg.obj_type = 0; 2709 cursor.key_beg.key = bio->bio_offset + 1; 2710 cursor.asof = ip->obj_asof; 2711 cursor.flags |= HAMMER_CURSOR_ASOF; 2712 2713 cursor.key_end = cursor.key_beg; 2714 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE); 2715 #if 0 2716 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) { 2717 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB; 2718 cursor.key_end.rec_type = HAMMER_RECTYPE_DB; 2719 cursor.key_end.key = HAMMER_MAX_KEY; 2720 } else 2721 #endif 2722 { 2723 ran_end = bio->bio_offset + bp->b_bufsize; 2724 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA; 2725 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA; 2726 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */ 2727 if (tmp64 < ran_end) 2728 cursor.key_end.key = HAMMER_MAX_KEY; 2729 else 2730 cursor.key_end.key = ran_end + MAXPHYS + 1; 2731 } 2732 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; 2733 2734 /* 2735 * Set NOSWAPCACHE for cursor data extraction if double buffering 2736 * is disabled or (if the file is not marked cacheable via chflags 2737 * and vm.swapcache_use_chflags is enabled). 2738 */ 2739 if (hammer_double_buffer == 0 || 2740 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 && 2741 vm_swapcache_use_chflags)) { 2742 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE; 2743 } 2744 2745 error = hammer_ip_first(&cursor); 2746 boff = 0; 2747 2748 while (error == 0) { 2749 /* 2750 * Get the base file offset of the record. The key for 2751 * data records is (base + bytes) rather then (base). 2752 */ 2753 base = &cursor.leaf->base; 2754 rec_offset = base->key - cursor.leaf->data_len; 2755 2756 /* 2757 * Calculate the gap, if any, and zero-fill it. 2758 * 2759 * n is the offset of the start of the record verses our 2760 * current seek offset in the bio. 2761 */ 2762 n = (int)(rec_offset - (bio->bio_offset + boff)); 2763 if (n > 0) { 2764 if (n > bp->b_bufsize - boff) 2765 n = bp->b_bufsize - boff; 2766 bzero((char *)bp->b_data + boff, n); 2767 boff += n; 2768 n = 0; 2769 } 2770 2771 /* 2772 * Calculate the data offset in the record and the number 2773 * of bytes we can copy. 2774 * 2775 * There are two degenerate cases. First, boff may already 2776 * be at bp->b_bufsize. Secondly, the data offset within 2777 * the record may exceed the record's size. 2778 */ 2779 roff = -n; 2780 rec_offset += roff; 2781 n = cursor.leaf->data_len - roff; 2782 if (n <= 0) { 2783 hdkprintf("bad n=%d roff=%d\n", n, roff); 2784 n = 0; 2785 } else if (n > bp->b_bufsize - boff) { 2786 n = bp->b_bufsize - boff; 2787 } 2788 2789 /* 2790 * Deal with cached truncations. This cool bit of code 2791 * allows truncate()/ftruncate() to avoid having to sync 2792 * the file. 2793 * 2794 * If the frontend is truncated then all backend records are 2795 * subject to the frontend's truncation. 2796 * 2797 * If the backend is truncated then backend records on-disk 2798 * (but not in-memory) are subject to the backend's 2799 * truncation. In-memory records owned by the backend 2800 * represent data written after the truncation point on the 2801 * backend and must not be truncated. 2802 * 2803 * Truncate operations deal with frontend buffer cache 2804 * buffers and frontend-owned in-memory records synchronously. 2805 */ 2806 if (ip->flags & HAMMER_INODE_TRUNCATED) { 2807 if (hammer_cursor_ondisk(&cursor)/* || 2808 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) { 2809 if (ip->trunc_off <= rec_offset) 2810 n = 0; 2811 else if (ip->trunc_off < rec_offset + n) 2812 n = (int)(ip->trunc_off - rec_offset); 2813 } 2814 } 2815 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) { 2816 if (hammer_cursor_ondisk(&cursor)) { 2817 if (ip->sync_trunc_off <= rec_offset) 2818 n = 0; 2819 else if (ip->sync_trunc_off < rec_offset + n) 2820 n = (int)(ip->sync_trunc_off - rec_offset); 2821 } 2822 } 2823 2824 /* 2825 * Try to issue a direct read into our bio if possible, 2826 * otherwise resolve the element data into a hammer_buffer 2827 * and copy. 2828 * 2829 * The buffer on-disk should be zerod past any real 2830 * truncation point, but may not be for any synthesized 2831 * truncation point from above. 2832 * 2833 * NOTE: disk_offset is only valid if the cursor data is 2834 * on-disk. 2835 */ 2836 disk_offset = cursor.leaf->data_offset + roff; 2837 isdedupable = (boff == 0 && n == bp->b_bufsize && 2838 hammer_cursor_ondisk(&cursor) && 2839 ((int)disk_offset & HAMMER_BUFMASK) == 0); 2840 2841 if (isdedupable && hammer_double_buffer == 0) { 2842 /* 2843 * Direct read case 2844 */ 2845 KKASSERT(hammer_is_zone_large_data(disk_offset)); 2846 nbio->bio_offset = disk_offset; 2847 error = hammer_io_direct_read(hmp, nbio, cursor.leaf); 2848 if (hammer_live_dedup && error == 0) 2849 hammer_dedup_cache_add(ip, cursor.leaf); 2850 goto done; 2851 } else if (isdedupable) { 2852 /* 2853 * Async I/O case for reading from backing store 2854 * and copying the data to the filesystem buffer. 2855 * live-dedup has to verify the data anyway if it 2856 * gets a hit later so we can just add the entry 2857 * now. 2858 */ 2859 KKASSERT(hammer_is_zone_large_data(disk_offset)); 2860 nbio->bio_offset = disk_offset; 2861 if (hammer_live_dedup) 2862 hammer_dedup_cache_add(ip, cursor.leaf); 2863 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf); 2864 goto done; 2865 } else if (n) { 2866 error = hammer_ip_resolve_data(&cursor); 2867 if (error == 0) { 2868 if (hammer_live_dedup && isdedupable) 2869 hammer_dedup_cache_add(ip, cursor.leaf); 2870 bcopy((char *)cursor.data + roff, 2871 (char *)bp->b_data + boff, n); 2872 } 2873 } 2874 if (error) 2875 break; 2876 2877 /* 2878 * We have to be sure that the only elements added to the 2879 * dedup cache are those which are already on-media. 2880 */ 2881 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor)) 2882 hammer_dedup_cache_add(ip, cursor.leaf); 2883 2884 /* 2885 * Iterate until we have filled the request. 2886 */ 2887 boff += n; 2888 if (boff == bp->b_bufsize) 2889 break; 2890 error = hammer_ip_next(&cursor); 2891 } 2892 2893 /* 2894 * There may have been a gap after the last record 2895 */ 2896 if (error == ENOENT) 2897 error = 0; 2898 if (error == 0 && boff != bp->b_bufsize) { 2899 KKASSERT(boff < bp->b_bufsize); 2900 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff); 2901 /* boff = bp->b_bufsize; */ 2902 } 2903 2904 /* 2905 * Disallow swapcache operation on the vnode buffer if double 2906 * buffering is enabled, the swapcache will get the data via 2907 * the block device buffer. 2908 */ 2909 if (hammer_double_buffer) 2910 bp->b_flags |= B_NOTMETA; 2911 2912 /* 2913 * Cleanup 2914 */ 2915 bp->b_resid = 0; 2916 bp->b_error = error; 2917 if (error) 2918 bp->b_flags |= B_ERROR; 2919 biodone(ap->a_bio); 2920 2921 done: 2922 /* 2923 * Cache the b-tree node for the last data read in cache[1]. 2924 * 2925 * If we hit the file EOF then also cache the node in the 2926 * governing directory's cache[3], it will be used to initialize 2927 * the new inode's cache[1] for any inodes looked up via the directory. 2928 * 2929 * This doesn't reduce disk accesses since the B-Tree chain is 2930 * likely cached, but it does reduce cpu overhead when looking 2931 * up file offsets for cpdup/tar/cpio style iterations. 2932 */ 2933 if (cursor.node) 2934 hammer_cache_node(&ip->cache[1], cursor.node); 2935 if (ran_end >= ip->ino_data.size) { 2936 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id, 2937 ip->obj_asof, ip->obj_localization); 2938 if (dip) { 2939 hammer_cache_node(&dip->cache[3], cursor.node); 2940 hammer_rel_inode(dip, 0); 2941 } 2942 } 2943 hammer_done_cursor(&cursor); 2944 hammer_done_transaction(&trans); 2945 lwkt_reltoken(&hmp->fs_token); 2946 return(error); 2947 } 2948 2949 /* 2950 * BMAP operation - used to support cluster_read() only. 2951 * 2952 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb) 2953 * 2954 * This routine may return EOPNOTSUPP if the opration is not supported for 2955 * the specified offset. The contents of the pointer arguments do not 2956 * need to be initialized in that case. 2957 * 2958 * If a disk address is available and properly aligned return 0 with 2959 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately 2960 * to the run-length relative to that offset. Callers may assume that 2961 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently 2962 * large, so return EOPNOTSUPP if it is not sufficiently large. 2963 */ 2964 static 2965 int 2966 hammer_vop_bmap(struct vop_bmap_args *ap) 2967 { 2968 struct hammer_transaction trans; 2969 hammer_inode_t ip; 2970 hammer_mount_t hmp; 2971 struct hammer_cursor cursor; 2972 hammer_base_elm_t base; 2973 int64_t rec_offset; 2974 int64_t ran_end; 2975 int64_t tmp64; 2976 int64_t base_offset; 2977 int64_t base_disk_offset; 2978 int64_t last_offset; 2979 hammer_off_t last_disk_offset; 2980 hammer_off_t disk_offset; 2981 int rec_len; 2982 int error; 2983 int blksize; 2984 2985 ip = ap->a_vp->v_data; 2986 hmp = ip->hmp; 2987 2988 /* 2989 * We can only BMAP regular files. We can't BMAP database files, 2990 * directories, etc. 2991 */ 2992 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE) 2993 return(EOPNOTSUPP); 2994 2995 /* 2996 * bmap is typically called with runp/runb both NULL when used 2997 * for writing. We do not support BMAP for writing atm. 2998 */ 2999 if (ap->a_cmd != BUF_CMD_READ) 3000 return(EOPNOTSUPP); 3001 3002 /* 3003 * Scan the B-Tree to acquire blockmap addresses, then translate 3004 * to raw addresses. 3005 */ 3006 lwkt_gettoken(&hmp->fs_token); 3007 hammer_simple_transaction(&trans, hmp); 3008 3009 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); 3010 3011 /* 3012 * Key range (begin and end inclusive) to scan. Note that the key's 3013 * stored in the actual records represent BASE+LEN, not BASE. The 3014 * first record containing bio_offset will have a key > bio_offset. 3015 */ 3016 cursor.key_beg.localization = ip->obj_localization | 3017 HAMMER_LOCALIZE_MISC; 3018 cursor.key_beg.obj_id = ip->obj_id; 3019 cursor.key_beg.create_tid = 0; 3020 cursor.key_beg.delete_tid = 0; 3021 cursor.key_beg.obj_type = 0; 3022 if (ap->a_runb) 3023 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1; 3024 else 3025 cursor.key_beg.key = ap->a_loffset + 1; 3026 if (cursor.key_beg.key < 0) 3027 cursor.key_beg.key = 0; 3028 cursor.asof = ip->obj_asof; 3029 cursor.flags |= HAMMER_CURSOR_ASOF; 3030 3031 cursor.key_end = cursor.key_beg; 3032 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE); 3033 3034 ran_end = ap->a_loffset + MAXPHYS; 3035 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA; 3036 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA; 3037 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */ 3038 if (tmp64 < ran_end) 3039 cursor.key_end.key = HAMMER_MAX_KEY; 3040 else 3041 cursor.key_end.key = ran_end + MAXPHYS + 1; 3042 3043 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; 3044 3045 error = hammer_ip_first(&cursor); 3046 base_offset = last_offset = 0; 3047 base_disk_offset = last_disk_offset = 0; 3048 3049 while (error == 0) { 3050 /* 3051 * Get the base file offset of the record. The key for 3052 * data records is (base + bytes) rather then (base). 3053 * 3054 * NOTE: rec_offset + rec_len may exceed the end-of-file. 3055 * The extra bytes should be zero on-disk and the BMAP op 3056 * should still be ok. 3057 */ 3058 base = &cursor.leaf->base; 3059 rec_offset = base->key - cursor.leaf->data_len; 3060 rec_len = cursor.leaf->data_len; 3061 3062 /* 3063 * Incorporate any cached truncation. 3064 * 3065 * NOTE: Modifications to rec_len based on synthesized 3066 * truncation points remove the guarantee that any extended 3067 * data on disk is zero (since the truncations may not have 3068 * taken place on-media yet). 3069 */ 3070 if (ip->flags & HAMMER_INODE_TRUNCATED) { 3071 if (hammer_cursor_ondisk(&cursor) || 3072 cursor.iprec->flush_state == HAMMER_FST_FLUSH) { 3073 if (ip->trunc_off <= rec_offset) 3074 rec_len = 0; 3075 else if (ip->trunc_off < rec_offset + rec_len) 3076 rec_len = (int)(ip->trunc_off - rec_offset); 3077 } 3078 } 3079 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) { 3080 if (hammer_cursor_ondisk(&cursor)) { 3081 if (ip->sync_trunc_off <= rec_offset) 3082 rec_len = 0; 3083 else if (ip->sync_trunc_off < rec_offset + rec_len) 3084 rec_len = (int)(ip->sync_trunc_off - rec_offset); 3085 } 3086 } 3087 3088 /* 3089 * Accumulate information. If we have hit a discontiguous 3090 * block reset base_offset unless we are already beyond the 3091 * requested offset. If we are, that's it, we stop. 3092 */ 3093 if (error) 3094 break; 3095 if (hammer_cursor_ondisk(&cursor)) { 3096 disk_offset = cursor.leaf->data_offset; 3097 if (rec_offset != last_offset || 3098 disk_offset != last_disk_offset) { 3099 if (rec_offset > ap->a_loffset) 3100 break; 3101 base_offset = rec_offset; 3102 base_disk_offset = disk_offset; 3103 } 3104 last_offset = rec_offset + rec_len; 3105 last_disk_offset = disk_offset + rec_len; 3106 3107 if (hammer_live_dedup) 3108 hammer_dedup_cache_add(ip, cursor.leaf); 3109 } 3110 3111 error = hammer_ip_next(&cursor); 3112 } 3113 3114 if (cursor.node) 3115 hammer_cache_node(&ip->cache[1], cursor.node); 3116 3117 hammer_done_cursor(&cursor); 3118 hammer_done_transaction(&trans); 3119 lwkt_reltoken(&hmp->fs_token); 3120 3121 /* 3122 * If we couldn't find any records or the records we did find were 3123 * all behind the requested offset, return failure. A forward 3124 * truncation can leave a hole w/ no on-disk records. 3125 */ 3126 if (last_offset == 0 || last_offset < ap->a_loffset) 3127 return (EOPNOTSUPP); 3128 3129 /* 3130 * Figure out the block size at the requested offset and adjust 3131 * our limits so the cluster_read() does not create inappropriately 3132 * sized buffer cache buffers. 3133 */ 3134 blksize = hammer_blocksize(ap->a_loffset); 3135 if (hammer_blocksize(base_offset) != blksize) { 3136 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset); 3137 } 3138 if (last_offset != ap->a_loffset && 3139 hammer_blocksize(last_offset - 1) != blksize) { 3140 last_offset = hammer_blockdemarc(ap->a_loffset, 3141 last_offset - 1); 3142 } 3143 3144 /* 3145 * Returning EOPNOTSUPP simply prevents the direct-IO optimization 3146 * from occuring. 3147 */ 3148 disk_offset = base_disk_offset + (ap->a_loffset - base_offset); 3149 3150 if (!hammer_is_zone_large_data(disk_offset)) { 3151 /* 3152 * Only large-data zones can be direct-IOd 3153 */ 3154 error = EOPNOTSUPP; 3155 } else if ((disk_offset & HAMMER_BUFMASK) || 3156 (last_offset - ap->a_loffset) < blksize) { 3157 /* 3158 * doffsetp is not aligned or the forward run size does 3159 * not cover a whole buffer, disallow the direct I/O. 3160 */ 3161 error = EOPNOTSUPP; 3162 } else { 3163 /* 3164 * We're good. 3165 */ 3166 *ap->a_doffsetp = disk_offset; 3167 if (ap->a_runb) { 3168 *ap->a_runb = ap->a_loffset - base_offset; 3169 KKASSERT(*ap->a_runb >= 0); 3170 } 3171 if (ap->a_runp) { 3172 *ap->a_runp = last_offset - ap->a_loffset; 3173 KKASSERT(*ap->a_runp >= 0); 3174 } 3175 error = 0; 3176 } 3177 return(error); 3178 } 3179 3180 /* 3181 * Write to a regular file. Because this is a strategy call the OS is 3182 * trying to actually get data onto the media. 3183 */ 3184 static 3185 int 3186 hammer_vop_strategy_write(struct vop_strategy_args *ap) 3187 { 3188 hammer_record_t record; 3189 hammer_mount_t hmp; 3190 hammer_inode_t ip; 3191 struct bio *bio; 3192 struct buf *bp; 3193 int blksize __debugvar; 3194 int bytes; 3195 int error; 3196 3197 bio = ap->a_bio; 3198 bp = bio->bio_buf; 3199 ip = ap->a_vp->v_data; 3200 hmp = ip->hmp; 3201 3202 blksize = hammer_blocksize(bio->bio_offset); 3203 KKASSERT(bp->b_bufsize == blksize); 3204 3205 if (ip->flags & HAMMER_INODE_RO) { 3206 bp->b_error = EROFS; 3207 bp->b_flags |= B_ERROR; 3208 biodone(ap->a_bio); 3209 return(EROFS); 3210 } 3211 3212 lwkt_gettoken(&hmp->fs_token); 3213 3214 /* 3215 * Disallow swapcache operation on the vnode buffer if double 3216 * buffering is enabled, the swapcache will get the data via 3217 * the block device buffer. 3218 */ 3219 if (hammer_double_buffer) 3220 bp->b_flags |= B_NOTMETA; 3221 3222 /* 3223 * Interlock with inode destruction (no in-kernel or directory 3224 * topology visibility). If we queue new IO while trying to 3225 * destroy the inode we can deadlock the vtrunc call in 3226 * hammer_inode_unloadable_check(). 3227 * 3228 * Besides, there's no point flushing a bp associated with an 3229 * inode that is being destroyed on-media and has no kernel 3230 * references. 3231 */ 3232 if ((ip->flags | ip->sync_flags) & 3233 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) { 3234 bp->b_resid = 0; 3235 biodone(ap->a_bio); 3236 lwkt_reltoken(&hmp->fs_token); 3237 return(0); 3238 } 3239 3240 /* 3241 * Reserve space and issue a direct-write from the front-end. 3242 * NOTE: The direct_io code will hammer_bread/bcopy smaller 3243 * allocations. 3244 * 3245 * An in-memory record will be installed to reference the storage 3246 * until the flusher can get to it. 3247 * 3248 * Since we own the high level bio the front-end will not try to 3249 * do a direct-read until the write completes. 3250 * 3251 * NOTE: The only time we do not reserve a full-sized buffers 3252 * worth of data is if the file is small. We do not try to 3253 * allocate a fragment (from the small-data zone) at the end of 3254 * an otherwise large file as this can lead to wildly separated 3255 * data. 3256 */ 3257 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0); 3258 KKASSERT(bio->bio_offset < ip->ino_data.size); 3259 if (bio->bio_offset || ip->ino_data.size > HAMMER_HBUFSIZE) 3260 bytes = bp->b_bufsize; 3261 else 3262 bytes = HAMMER_DATA_DOALIGN_WITH(int, ip->ino_data.size); 3263 3264 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data, 3265 bytes, &error); 3266 3267 /* 3268 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated 3269 * in hammer_vop_write(). We must flag the record so the proper 3270 * REDO_TERM_WRITE entry is generated during the flush. 3271 */ 3272 if (record) { 3273 if (bp->b_flags & B_VFSFLAG1) { 3274 record->flags |= HAMMER_RECF_REDO; 3275 bp->b_flags &= ~B_VFSFLAG1; 3276 } 3277 if (record->flags & HAMMER_RECF_DEDUPED) { 3278 bp->b_resid = 0; 3279 hammer_ip_replace_bulk(hmp, record); 3280 biodone(ap->a_bio); 3281 } else { 3282 hammer_io_direct_write(hmp, bio, record); 3283 } 3284 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs) 3285 hammer_flush_inode(ip, 0); 3286 } else { 3287 bp->b_bio2.bio_offset = NOOFFSET; 3288 bp->b_error = error; 3289 bp->b_flags |= B_ERROR; 3290 biodone(ap->a_bio); 3291 } 3292 lwkt_reltoken(&hmp->fs_token); 3293 return(error); 3294 } 3295 3296 /* 3297 * dounlink - disconnect a directory entry 3298 * 3299 * XXX whiteout support not really in yet 3300 */ 3301 static int 3302 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch, 3303 struct vnode *dvp, struct ucred *cred, 3304 int flags, int isdir) 3305 { 3306 struct namecache *ncp; 3307 hammer_inode_t dip; 3308 hammer_inode_t ip; 3309 hammer_mount_t hmp; 3310 struct hammer_cursor cursor; 3311 int64_t namekey; 3312 uint32_t max_iterations; 3313 int nlen, error; 3314 3315 /* 3316 * Calculate the namekey and setup the key range for the scan. This 3317 * works kinda like a chained hash table where the lower 32 bits 3318 * of the namekey synthesize the chain. 3319 * 3320 * The key range is inclusive of both key_beg and key_end. 3321 */ 3322 dip = VTOI(dvp); 3323 ncp = nch->ncp; 3324 hmp = dip->hmp; 3325 3326 if (dip->flags & HAMMER_INODE_RO) 3327 return (EROFS); 3328 3329 namekey = hammer_direntry_namekey(dip, ncp->nc_name, ncp->nc_nlen, 3330 &max_iterations); 3331 retry: 3332 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip); 3333 cursor.key_beg.localization = dip->obj_localization | 3334 hammer_dir_localization(dip); 3335 cursor.key_beg.obj_id = dip->obj_id; 3336 cursor.key_beg.key = namekey; 3337 cursor.key_beg.create_tid = 0; 3338 cursor.key_beg.delete_tid = 0; 3339 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; 3340 cursor.key_beg.obj_type = 0; 3341 3342 cursor.key_end = cursor.key_beg; 3343 cursor.key_end.key += max_iterations; 3344 cursor.asof = dip->obj_asof; 3345 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; 3346 3347 /* 3348 * Scan all matching records (the chain), locate the one matching 3349 * the requested path component. info->last_error contains the 3350 * error code on search termination and could be 0, ENOENT, or 3351 * something else. 3352 * 3353 * The hammer_ip_*() functions merge in-memory records with on-disk 3354 * records for the purposes of the search. 3355 */ 3356 error = hammer_ip_first(&cursor); 3357 3358 while (error == 0) { 3359 error = hammer_ip_resolve_data(&cursor); 3360 if (error) 3361 break; 3362 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF; 3363 KKASSERT(nlen > 0); 3364 if (ncp->nc_nlen == nlen && 3365 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) { 3366 break; 3367 } 3368 error = hammer_ip_next(&cursor); 3369 } 3370 3371 /* 3372 * If all is ok we have to get the inode so we can adjust nlinks. 3373 * To avoid a deadlock with the flusher we must release the inode 3374 * lock on the directory when acquiring the inode for the entry. 3375 * 3376 * If the target is a directory, it must be empty. 3377 */ 3378 if (error == 0) { 3379 hammer_unlock(&cursor.ip->lock); 3380 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id, 3381 hmp->asof, 3382 cursor.data->entry.localization, 3383 0, &error); 3384 hammer_lock_sh(&cursor.ip->lock); 3385 if (error == ENOENT) { 3386 hkprintf("WARNING: Removing dirent w/missing inode " 3387 "\"%s\"\n" 3388 "\tobj_id = %016jx\n", 3389 ncp->nc_name, 3390 (intmax_t)cursor.data->entry.obj_id); 3391 error = 0; 3392 } 3393 3394 /* 3395 * If isdir >= 0 we validate that the entry is or is not a 3396 * directory. If isdir < 0 we don't care. 3397 */ 3398 if (error == 0 && isdir >= 0 && ip) { 3399 if (isdir && 3400 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) { 3401 error = ENOTDIR; 3402 } else if (isdir == 0 && 3403 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) { 3404 error = EISDIR; 3405 } 3406 } 3407 3408 /* 3409 * If we are trying to remove a directory the directory must 3410 * be empty. 3411 * 3412 * The check directory code can loop and deadlock/retry. Our 3413 * own cursor's node locks must be released to avoid a 3-way 3414 * deadlock with the flusher if the check directory code 3415 * blocks. 3416 * 3417 * If any changes whatsoever have been made to the cursor 3418 * set EDEADLK and retry. 3419 * 3420 * WARNING: See warnings in hammer_unlock_cursor() 3421 * function. 3422 */ 3423 if (error == 0 && ip && ip->ino_data.obj_type == 3424 HAMMER_OBJTYPE_DIRECTORY) { 3425 hammer_unlock_cursor(&cursor); 3426 error = hammer_ip_check_directory_empty(trans, ip); 3427 hammer_lock_cursor(&cursor); 3428 if (cursor.flags & HAMMER_CURSOR_RETEST) { 3429 hkprintf("Warning: avoided deadlock " 3430 "on rmdir '%s'\n", 3431 ncp->nc_name); 3432 error = EDEADLK; 3433 } 3434 } 3435 3436 /* 3437 * Delete the directory entry. 3438 * 3439 * WARNING: hammer_ip_del_direntry() may have to terminate 3440 * the cursor to avoid a deadlock. It is ok to call 3441 * hammer_done_cursor() twice. 3442 */ 3443 if (error == 0) { 3444 error = hammer_ip_del_direntry(trans, &cursor, 3445 dip, ip); 3446 } 3447 hammer_done_cursor(&cursor); 3448 if (error == 0) { 3449 /* 3450 * Tell the namecache that we are now unlinked. 3451 */ 3452 cache_unlink(nch); 3453 3454 /* 3455 * NOTE: ip->vp, if non-NULL, cannot be directly 3456 * referenced without formally acquiring the 3457 * vp since the vp might have zero refs on it, 3458 * or in the middle of a reclaim, etc. 3459 * 3460 * NOTE: The cache_setunresolved() can rip the vp 3461 * out from under us since the vp may not have 3462 * any refs, in which case ip->vp will be NULL 3463 * from the outset. 3464 */ 3465 while (ip && ip->vp) { 3466 struct vnode *vp; 3467 3468 error = hammer_get_vnode(ip, &vp); 3469 if (error == 0 && vp) { 3470 vn_unlock(vp); 3471 hammer_knote(ip->vp, NOTE_DELETE); 3472 #if 0 3473 /* 3474 * Don't do this, it can deadlock 3475 * on concurrent rm's of hardlinks. 3476 * Shouldn't be needed any more. 3477 */ 3478 cache_inval_vp(ip->vp, CINV_DESTROY); 3479 #endif 3480 vrele(vp); 3481 break; 3482 } 3483 hdkprintf("ip/vp race1 avoided\n"); 3484 } 3485 } 3486 if (ip) 3487 hammer_rel_inode(ip, 0); 3488 } else { 3489 hammer_done_cursor(&cursor); 3490 } 3491 if (error == EDEADLK) 3492 goto retry; 3493 3494 return (error); 3495 } 3496 3497 /************************************************************************ 3498 * FIFO AND SPECFS OPS * 3499 ************************************************************************ 3500 * 3501 */ 3502 static int 3503 hammer_vop_fifoclose (struct vop_close_args *ap) 3504 { 3505 /* XXX update itimes */ 3506 return (VOCALL(&fifo_vnode_vops, &ap->a_head)); 3507 } 3508 3509 static int 3510 hammer_vop_fiforead (struct vop_read_args *ap) 3511 { 3512 int error; 3513 3514 error = VOCALL(&fifo_vnode_vops, &ap->a_head); 3515 /* XXX update access time */ 3516 return (error); 3517 } 3518 3519 static int 3520 hammer_vop_fifowrite (struct vop_write_args *ap) 3521 { 3522 int error; 3523 3524 error = VOCALL(&fifo_vnode_vops, &ap->a_head); 3525 /* XXX update access time */ 3526 return (error); 3527 } 3528 3529 static 3530 int 3531 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap) 3532 { 3533 int error; 3534 3535 error = VOCALL(&fifo_vnode_vops, &ap->a_head); 3536 if (error) 3537 error = hammer_vop_kqfilter(ap); 3538 return(error); 3539 } 3540 3541 /************************************************************************ 3542 * KQFILTER OPS * 3543 ************************************************************************ 3544 * 3545 */ 3546 static void filt_hammerdetach(struct knote *kn); 3547 static int filt_hammerread(struct knote *kn, long hint); 3548 static int filt_hammerwrite(struct knote *kn, long hint); 3549 static int filt_hammervnode(struct knote *kn, long hint); 3550 3551 static struct filterops hammerread_filtops = 3552 { FILTEROP_ISFD | FILTEROP_MPSAFE, 3553 NULL, filt_hammerdetach, filt_hammerread }; 3554 static struct filterops hammerwrite_filtops = 3555 { FILTEROP_ISFD | FILTEROP_MPSAFE, 3556 NULL, filt_hammerdetach, filt_hammerwrite }; 3557 static struct filterops hammervnode_filtops = 3558 { FILTEROP_ISFD | FILTEROP_MPSAFE, 3559 NULL, filt_hammerdetach, filt_hammervnode }; 3560 3561 static 3562 int 3563 hammer_vop_kqfilter(struct vop_kqfilter_args *ap) 3564 { 3565 struct vnode *vp = ap->a_vp; 3566 struct knote *kn = ap->a_kn; 3567 3568 switch (kn->kn_filter) { 3569 case EVFILT_READ: 3570 kn->kn_fop = &hammerread_filtops; 3571 break; 3572 case EVFILT_WRITE: 3573 kn->kn_fop = &hammerwrite_filtops; 3574 break; 3575 case EVFILT_VNODE: 3576 kn->kn_fop = &hammervnode_filtops; 3577 break; 3578 default: 3579 return (EOPNOTSUPP); 3580 } 3581 3582 kn->kn_hook = (caddr_t)vp; 3583 3584 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 3585 3586 return(0); 3587 } 3588 3589 static void 3590 filt_hammerdetach(struct knote *kn) 3591 { 3592 struct vnode *vp = (void *)kn->kn_hook; 3593 3594 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); 3595 } 3596 3597 static int 3598 filt_hammerread(struct knote *kn, long hint) 3599 { 3600 struct vnode *vp = (void *)kn->kn_hook; 3601 hammer_inode_t ip = VTOI(vp); 3602 hammer_mount_t hmp = ip->hmp; 3603 off_t off; 3604 3605 if (hint == NOTE_REVOKE) { 3606 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 3607 return(1); 3608 } 3609 lwkt_gettoken(&hmp->fs_token); /* XXX use per-ip-token */ 3610 off = ip->ino_data.size - kn->kn_fp->f_offset; 3611 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; 3612 lwkt_reltoken(&hmp->fs_token); 3613 if (kn->kn_sfflags & NOTE_OLDAPI) 3614 return(1); 3615 return (kn->kn_data != 0); 3616 } 3617 3618 static int 3619 filt_hammerwrite(struct knote *kn, long hint) 3620 { 3621 if (hint == NOTE_REVOKE) 3622 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); 3623 kn->kn_data = 0; 3624 return (1); 3625 } 3626 3627 static int 3628 filt_hammervnode(struct knote *kn, long hint) 3629 { 3630 if (kn->kn_sfflags & hint) 3631 kn->kn_fflags |= hint; 3632 if (hint == NOTE_REVOKE) { 3633 kn->kn_flags |= (EV_EOF | EV_NODATA); 3634 return (1); 3635 } 3636 return (kn->kn_fflags != 0); 3637 } 3638 3639