1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/systm.h> 32 #include <sys/sysmacros.h> 33 #include <sys/resource.h> 34 #include <sys/vfs.h> 35 #include <sys/vnode.h> 36 #include <sys/file.h> 37 #include <sys/stat.h> 38 #include <sys/kmem.h> 39 #include <sys/taskq.h> 40 #include <sys/uio.h> 41 #include <sys/vmsystm.h> 42 #include <sys/atomic.h> 43 #include <sys/vm.h> 44 #include <vm/seg_vn.h> 45 #include <vm/pvn.h> 46 #include <vm/as.h> 47 #include <sys/mman.h> 48 #include <sys/pathname.h> 49 #include <sys/cmn_err.h> 50 #include <sys/errno.h> 51 #include <sys/unistd.h> 52 #include <sys/zfs_vfsops.h> 53 #include <sys/zfs_dir.h> 54 #include <sys/zfs_acl.h> 55 #include <sys/zfs_ioctl.h> 56 #include <sys/fs/zfs.h> 57 #include <sys/dmu.h> 58 #include <sys/spa.h> 59 #include <sys/txg.h> 60 #include <sys/dbuf.h> 61 #include <sys/zap.h> 62 #include <sys/dirent.h> 63 #include <sys/policy.h> 64 #include <sys/sunddi.h> 65 #include <sys/filio.h> 66 #include "fs/fs_subr.h" 67 #include <sys/zfs_ctldir.h> 68 #include <sys/dnlc.h> 69 #include <sys/zfs_rlock.h> 70 71 /* 72 * Programming rules. 73 * 74 * Each vnode op performs some logical unit of work. To do this, the ZPL must 75 * properly lock its in-core state, create a DMU transaction, do the work, 76 * record this work in the intent log (ZIL), commit the DMU transaction, 77 * and wait the the intent log to commit if it's is a synchronous operation. 78 * Morover, the vnode ops must work in both normal and log replay context. 79 * The ordering of events is important to avoid deadlocks and references 80 * to freed memory. The example below illustrates the following Big Rules: 81 * 82 * (1) A check must be made in each zfs thread for a mounted file system. 83 * This is done avoiding races using ZFS_ENTER(zfsvfs). 84 * A ZFS_EXIT(zfsvfs) is needed before all returns. 85 * 86 * (2) VN_RELE() should always be the last thing except for zil_commit() 87 * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 88 * First, if it's the last reference, the vnode/znode 89 * can be freed, so the zp may point to freed memory. Second, the last 90 * reference will call zfs_zinactive(), which may induce a lot of work -- 91 * pushing cached pages (which acquires range locks) and syncing out 92 * cached atime changes. Third, zfs_zinactive() may require a new tx, 93 * which could deadlock the system if you were already holding one. 94 * 95 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 96 * as they can span dmu_tx_assign() calls. 97 * 98 * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign(). 99 * In normal operation, this will be TXG_NOWAIT. During ZIL replay, 100 * it will be a specific txg. Either way, dmu_tx_assign() never blocks. 101 * This is critical because we don't want to block while holding locks. 102 * Note, in particular, that if a lock is sometimes acquired before 103 * the tx assigns, and sometimes after (e.g. z_lock), then failing to 104 * use a non-blocking assign can deadlock the system. The scenario: 105 * 106 * Thread A has grabbed a lock before calling dmu_tx_assign(). 107 * Thread B is in an already-assigned tx, and blocks for this lock. 108 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 109 * forever, because the previous txg can't quiesce until B's tx commits. 110 * 111 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 112 * then drop all locks, call dmu_tx_wait(), and try again. 113 * 114 * (5) If the operation succeeded, generate the intent log entry for it 115 * before dropping locks. This ensures that the ordering of events 116 * in the intent log matches the order in which they actually occurred. 117 * 118 * (6) At the end of each vnode op, the DMU tx must always commit, 119 * regardless of whether there were any errors. 120 * 121 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 122 * to ensure that synchronous semantics are provided when necessary. 123 * 124 * In general, this is how things should be ordered in each vnode op: 125 * 126 * ZFS_ENTER(zfsvfs); // exit if unmounted 127 * top: 128 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 129 * rw_enter(...); // grab any other locks you need 130 * tx = dmu_tx_create(...); // get DMU tx 131 * dmu_tx_hold_*(); // hold each object you might modify 132 * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign 133 * if (error) { 134 * rw_exit(...); // drop locks 135 * zfs_dirent_unlock(dl); // unlock directory entry 136 * VN_RELE(...); // release held vnodes 137 * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 138 * dmu_tx_wait(tx); 139 * dmu_tx_abort(tx); 140 * goto top; 141 * } 142 * dmu_tx_abort(tx); // abort DMU tx 143 * ZFS_EXIT(zfsvfs); // finished in zfs 144 * return (error); // really out of space 145 * } 146 * error = do_real_work(); // do whatever this VOP does 147 * if (error == 0) 148 * zfs_log_*(...); // on success, make ZIL entry 149 * dmu_tx_commit(tx); // commit DMU tx -- error or not 150 * rw_exit(...); // drop locks 151 * zfs_dirent_unlock(dl); // unlock directory entry 152 * VN_RELE(...); // release held vnodes 153 * zil_commit(zilog, seq, foid); // synchronous when necessary 154 * ZFS_EXIT(zfsvfs); // finished in zfs 155 * return (error); // done, report error 156 */ 157 /* ARGSUSED */ 158 static int 159 zfs_open(vnode_t **vpp, int flag, cred_t *cr) 160 { 161 znode_t *zp = VTOZ(*vpp); 162 163 /* Keep a count of the synchronous opens in the znode */ 164 if (flag & (FSYNC | FDSYNC)) 165 atomic_inc_32(&zp->z_sync_cnt); 166 return (0); 167 } 168 169 /* ARGSUSED */ 170 static int 171 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 172 { 173 znode_t *zp = VTOZ(vp); 174 175 /* Decrement the synchronous opens in the znode */ 176 if (flag & (FSYNC | FDSYNC)) 177 atomic_dec_32(&zp->z_sync_cnt); 178 179 /* 180 * Clean up any locks held by this process on the vp. 181 */ 182 cleanlocks(vp, ddi_get_pid(), 0); 183 cleanshares(vp, ddi_get_pid()); 184 185 return (0); 186 } 187 188 /* 189 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 190 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 191 */ 192 static int 193 zfs_holey(vnode_t *vp, int cmd, offset_t *off) 194 { 195 znode_t *zp = VTOZ(vp); 196 uint64_t noff = (uint64_t)*off; /* new offset */ 197 uint64_t file_sz; 198 int error; 199 boolean_t hole; 200 201 file_sz = zp->z_phys->zp_size; 202 if (noff >= file_sz) { 203 return (ENXIO); 204 } 205 206 if (cmd == _FIO_SEEK_HOLE) 207 hole = B_TRUE; 208 else 209 hole = B_FALSE; 210 211 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 212 213 /* end of file? */ 214 if ((error == ESRCH) || (noff > file_sz)) { 215 /* 216 * Handle the virtual hole at the end of file. 217 */ 218 if (hole) { 219 *off = file_sz; 220 return (0); 221 } 222 return (ENXIO); 223 } 224 225 if (noff < *off) 226 return (error); 227 *off = noff; 228 return (error); 229 } 230 231 /* ARGSUSED */ 232 static int 233 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 234 int *rvalp) 235 { 236 offset_t off; 237 int error; 238 zfsvfs_t *zfsvfs; 239 240 switch (com) { 241 case _FIOFFS: 242 return (zfs_sync(vp->v_vfsp, 0, cred)); 243 244 /* 245 * The following two ioctls are used by bfu. Faking out, 246 * necessary to avoid bfu errors. 247 */ 248 case _FIOGDIO: 249 case _FIOSDIO: 250 return (0); 251 252 case _FIO_SEEK_DATA: 253 case _FIO_SEEK_HOLE: 254 if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 255 return (EFAULT); 256 257 zfsvfs = VTOZ(vp)->z_zfsvfs; 258 ZFS_ENTER(zfsvfs); 259 260 /* offset parameter is in/out */ 261 error = zfs_holey(vp, com, &off); 262 ZFS_EXIT(zfsvfs); 263 if (error) 264 return (error); 265 if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 266 return (EFAULT); 267 return (0); 268 } 269 return (ENOTTY); 270 } 271 272 /* 273 * When a file is memory mapped, we must keep the IO data synchronized 274 * between the DMU cache and the memory mapped pages. What this means: 275 * 276 * On Write: If we find a memory mapped page, we write to *both* 277 * the page and the dmu buffer. 278 * 279 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 280 * the file is memory mapped. 281 */ 282 static int 283 mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx) 284 { 285 znode_t *zp = VTOZ(vp); 286 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 287 int64_t start, off; 288 int len = nbytes; 289 int error = 0; 290 291 start = uio->uio_loffset; 292 off = start & PAGEOFFSET; 293 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 294 page_t *pp; 295 uint64_t bytes = MIN(PAGESIZE - off, len); 296 uint64_t woff = uio->uio_loffset; 297 298 /* 299 * We don't want a new page to "appear" in the middle of 300 * the file update (because it may not get the write 301 * update data), so we grab a lock to block 302 * zfs_getpage(). 303 */ 304 rw_enter(&zp->z_map_lock, RW_WRITER); 305 if (pp = page_lookup(vp, start, SE_SHARED)) { 306 caddr_t va; 307 308 rw_exit(&zp->z_map_lock); 309 va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 310 error = uiomove(va+off, bytes, UIO_WRITE, uio); 311 if (error == 0) { 312 dmu_write(zfsvfs->z_os, zp->z_id, 313 woff, bytes, va+off, tx); 314 } 315 ppmapout(va); 316 page_unlock(pp); 317 } else { 318 error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 319 uio, bytes, tx); 320 rw_exit(&zp->z_map_lock); 321 } 322 len -= bytes; 323 off = 0; 324 if (error) 325 break; 326 } 327 return (error); 328 } 329 330 /* 331 * When a file is memory mapped, we must keep the IO data synchronized 332 * between the DMU cache and the memory mapped pages. What this means: 333 * 334 * On Read: We "read" preferentially from memory mapped pages, 335 * else we default from the dmu buffer. 336 * 337 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 338 * the file is memory mapped. 339 */ 340 static int 341 mappedread(vnode_t *vp, int nbytes, uio_t *uio) 342 { 343 znode_t *zp = VTOZ(vp); 344 objset_t *os = zp->z_zfsvfs->z_os; 345 int64_t start, off; 346 int len = nbytes; 347 int error = 0; 348 349 start = uio->uio_loffset; 350 off = start & PAGEOFFSET; 351 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 352 page_t *pp; 353 uint64_t bytes = MIN(PAGESIZE - off, len); 354 355 if (pp = page_lookup(vp, start, SE_SHARED)) { 356 caddr_t va; 357 358 va = ppmapin(pp, PROT_READ, (caddr_t)-1L); 359 error = uiomove(va + off, bytes, UIO_READ, uio); 360 ppmapout(va); 361 page_unlock(pp); 362 } else { 363 error = dmu_read_uio(os, zp->z_id, uio, bytes); 364 } 365 len -= bytes; 366 off = 0; 367 if (error) 368 break; 369 } 370 return (error); 371 } 372 373 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 374 375 /* 376 * Read bytes from specified file into supplied buffer. 377 * 378 * IN: vp - vnode of file to be read from. 379 * uio - structure supplying read location, range info, 380 * and return buffer. 381 * ioflag - SYNC flags; used to provide FRSYNC semantics. 382 * cr - credentials of caller. 383 * 384 * OUT: uio - updated offset and range, buffer filled. 385 * 386 * RETURN: 0 if success 387 * error code if failure 388 * 389 * Side Effects: 390 * vp - atime updated if byte count > 0 391 */ 392 /* ARGSUSED */ 393 static int 394 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 395 { 396 znode_t *zp = VTOZ(vp); 397 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 398 objset_t *os = zfsvfs->z_os; 399 ssize_t n, nbytes; 400 int error; 401 rl_t *rl; 402 403 ZFS_ENTER(zfsvfs); 404 405 /* 406 * Validate file offset 407 */ 408 if (uio->uio_loffset < (offset_t)0) { 409 ZFS_EXIT(zfsvfs); 410 return (EINVAL); 411 } 412 413 /* 414 * Fasttrack empty reads 415 */ 416 if (uio->uio_resid == 0) { 417 ZFS_EXIT(zfsvfs); 418 return (0); 419 } 420 421 /* 422 * Check for mandatory locks 423 */ 424 if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 425 if (error = chklock(vp, FREAD, 426 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 427 ZFS_EXIT(zfsvfs); 428 return (error); 429 } 430 } 431 432 /* 433 * If we're in FRSYNC mode, sync out this znode before reading it. 434 */ 435 if (ioflag & FRSYNC) 436 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 437 438 /* 439 * Lock the range against changes. 440 */ 441 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 442 443 /* 444 * If we are reading past end-of-file we can skip 445 * to the end; but we might still need to set atime. 446 */ 447 if (uio->uio_loffset >= zp->z_phys->zp_size) { 448 error = 0; 449 goto out; 450 } 451 452 ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 453 n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 454 455 while (n > 0) { 456 nbytes = MIN(n, zfs_read_chunk_size - 457 P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 458 459 if (vn_has_cached_data(vp)) 460 error = mappedread(vp, nbytes, uio); 461 else 462 error = dmu_read_uio(os, zp->z_id, uio, nbytes); 463 if (error) 464 break; 465 466 n -= nbytes; 467 } 468 469 out: 470 zfs_range_unlock(rl); 471 472 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 473 ZFS_EXIT(zfsvfs); 474 return (error); 475 } 476 477 /* 478 * Fault in the pages of the first n bytes specified by the uio structure. 479 * 1 byte in each page is touched and the uio struct is unmodified. 480 * Any error will exit this routine as this is only a best 481 * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 482 */ 483 static void 484 zfs_prefault_write(ssize_t n, struct uio *uio) 485 { 486 struct iovec *iov; 487 ulong_t cnt, incr; 488 caddr_t p; 489 uint8_t tmp; 490 491 iov = uio->uio_iov; 492 493 while (n) { 494 cnt = MIN(iov->iov_len, n); 495 if (cnt == 0) { 496 /* empty iov entry */ 497 iov++; 498 continue; 499 } 500 n -= cnt; 501 /* 502 * touch each page in this segment. 503 */ 504 p = iov->iov_base; 505 while (cnt) { 506 switch (uio->uio_segflg) { 507 case UIO_USERSPACE: 508 case UIO_USERISPACE: 509 if (fuword8(p, &tmp)) 510 return; 511 break; 512 case UIO_SYSSPACE: 513 if (kcopy(p, &tmp, 1)) 514 return; 515 break; 516 } 517 incr = MIN(cnt, PAGESIZE); 518 p += incr; 519 cnt -= incr; 520 } 521 /* 522 * touch the last byte in case it straddles a page. 523 */ 524 p--; 525 switch (uio->uio_segflg) { 526 case UIO_USERSPACE: 527 case UIO_USERISPACE: 528 if (fuword8(p, &tmp)) 529 return; 530 break; 531 case UIO_SYSSPACE: 532 if (kcopy(p, &tmp, 1)) 533 return; 534 break; 535 } 536 iov++; 537 } 538 } 539 540 /* 541 * Write the bytes to a file. 542 * 543 * IN: vp - vnode of file to be written to. 544 * uio - structure supplying write location, range info, 545 * and data buffer. 546 * ioflag - FAPPEND flag set if in append mode. 547 * cr - credentials of caller. 548 * 549 * OUT: uio - updated offset and range. 550 * 551 * RETURN: 0 if success 552 * error code if failure 553 * 554 * Timestamps: 555 * vp - ctime|mtime updated if byte count > 0 556 */ 557 /* ARGSUSED */ 558 static int 559 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 560 { 561 znode_t *zp = VTOZ(vp); 562 rlim64_t limit = uio->uio_llimit; 563 ssize_t start_resid = uio->uio_resid; 564 ssize_t tx_bytes; 565 uint64_t end_size; 566 dmu_tx_t *tx; 567 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 568 zilog_t *zilog = zfsvfs->z_log; 569 offset_t woff; 570 ssize_t n, nbytes; 571 rl_t *rl; 572 int max_blksz = zfsvfs->z_max_blksz; 573 int error; 574 575 /* 576 * Fasttrack empty write 577 */ 578 n = start_resid; 579 if (n == 0) 580 return (0); 581 582 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 583 limit = MAXOFFSET_T; 584 585 ZFS_ENTER(zfsvfs); 586 587 /* 588 * Pre-fault the pages to ensure slow (eg NFS) pages 589 * don't hold up txg. 590 */ 591 zfs_prefault_write(n, uio); 592 593 /* 594 * If in append mode, set the io offset pointer to eof. 595 */ 596 if (ioflag & FAPPEND) { 597 /* 598 * Range lock for a file append: 599 * The value for the start of range will be determined by 600 * zfs_range_lock() (to guarantee append semantics). 601 * If this write will cause the block size to increase, 602 * zfs_range_lock() will lock the entire file, so we must 603 * later reduce the range after we grow the block size. 604 */ 605 rl = zfs_range_lock(zp, 0, n, RL_APPEND); 606 if (rl->r_len == UINT64_MAX) { 607 /* overlocked, zp_size can't change */ 608 woff = uio->uio_loffset = zp->z_phys->zp_size; 609 } else { 610 woff = uio->uio_loffset = rl->r_off; 611 } 612 } else { 613 woff = uio->uio_loffset; 614 /* 615 * Validate file offset 616 */ 617 if (woff < 0) { 618 ZFS_EXIT(zfsvfs); 619 return (EINVAL); 620 } 621 622 /* 623 * If we need to grow the block size then zfs_range_lock() 624 * will lock a wider range than we request here. 625 * Later after growing the block size we reduce the range. 626 */ 627 rl = zfs_range_lock(zp, woff, n, RL_WRITER); 628 } 629 630 if (woff >= limit) { 631 zfs_range_unlock(rl); 632 ZFS_EXIT(zfsvfs); 633 return (EFBIG); 634 } 635 636 if ((woff + n) > limit || woff > (limit - n)) 637 n = limit - woff; 638 639 /* 640 * Check for mandatory locks 641 */ 642 if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 643 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 644 zfs_range_unlock(rl); 645 ZFS_EXIT(zfsvfs); 646 return (error); 647 } 648 end_size = MAX(zp->z_phys->zp_size, woff + n); 649 650 /* 651 * Write the file in reasonable size chunks. Each chunk is written 652 * in a separate transaction; this keeps the intent log records small 653 * and allows us to do more fine-grained space accounting. 654 */ 655 while (n > 0) { 656 /* 657 * Start a transaction. 658 */ 659 woff = uio->uio_loffset; 660 tx = dmu_tx_create(zfsvfs->z_os); 661 dmu_tx_hold_bonus(tx, zp->z_id); 662 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 663 error = dmu_tx_assign(tx, zfsvfs->z_assign); 664 if (error) { 665 if (error == ERESTART && 666 zfsvfs->z_assign == TXG_NOWAIT) { 667 dmu_tx_wait(tx); 668 dmu_tx_abort(tx); 669 continue; 670 } 671 dmu_tx_abort(tx); 672 break; 673 } 674 675 /* 676 * If zfs_range_lock() over-locked we grow the blocksize 677 * and then reduce the lock range. This will only happen 678 * on the first iteration since zfs_range_reduce() will 679 * shrink down r_len to the appropriate size. 680 */ 681 if (rl->r_len == UINT64_MAX) { 682 uint64_t new_blksz; 683 684 if (zp->z_blksz > max_blksz) { 685 ASSERT(!ISP2(zp->z_blksz)); 686 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 687 } else { 688 new_blksz = MIN(end_size, max_blksz); 689 } 690 zfs_grow_blocksize(zp, new_blksz, tx); 691 zfs_range_reduce(rl, woff, n); 692 } 693 694 /* 695 * XXX - should we really limit each write to z_max_blksz? 696 * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 697 */ 698 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 699 rw_enter(&zp->z_map_lock, RW_READER); 700 701 tx_bytes = uio->uio_resid; 702 if (vn_has_cached_data(vp)) { 703 rw_exit(&zp->z_map_lock); 704 error = mappedwrite(vp, nbytes, uio, tx); 705 } else { 706 error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 707 uio, nbytes, tx); 708 rw_exit(&zp->z_map_lock); 709 } 710 tx_bytes -= uio->uio_resid; 711 712 /* 713 * If we made no progress, we're done. If we made even 714 * partial progress, update the znode and ZIL accordingly. 715 */ 716 if (tx_bytes == 0) { 717 ASSERT(error != 0); 718 break; 719 } 720 721 /* 722 * Clear Set-UID/Set-GID bits on successful write if not 723 * privileged and at least one of the excute bits is set. 724 * 725 * It would be nice to to this after all writes have 726 * been done, but that would still expose the ISUID/ISGID 727 * to another app after the partial write is committed. 728 */ 729 mutex_enter(&zp->z_acl_lock); 730 if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 731 (S_IXUSR >> 6))) != 0 && 732 (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 733 secpolicy_vnode_setid_retain(cr, 734 (zp->z_phys->zp_mode & S_ISUID) != 0 && 735 zp->z_phys->zp_uid == 0) != 0) { 736 zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 737 } 738 mutex_exit(&zp->z_acl_lock); 739 740 /* 741 * Update time stamp. NOTE: This marks the bonus buffer as 742 * dirty, so we don't have to do it again for zp_size. 743 */ 744 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 745 746 /* 747 * Update the file size (zp_size) if it has changed; 748 * account for possible concurrent updates. 749 */ 750 while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 751 (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 752 uio->uio_loffset); 753 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 754 dmu_tx_commit(tx); 755 756 if (error != 0) 757 break; 758 ASSERT(tx_bytes == nbytes); 759 n -= nbytes; 760 } 761 762 zfs_range_unlock(rl); 763 764 /* 765 * If we're in replay mode, or we made no progress, return error. 766 * Otherwise, it's at least a partial write, so it's successful. 767 */ 768 if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 769 ZFS_EXIT(zfsvfs); 770 return (error); 771 } 772 773 if (ioflag & (FSYNC | FDSYNC)) 774 zil_commit(zilog, zp->z_last_itx, zp->z_id); 775 776 ZFS_EXIT(zfsvfs); 777 return (0); 778 } 779 780 void 781 zfs_get_done(dmu_buf_t *db, void *vzgd) 782 { 783 zgd_t *zgd = (zgd_t *)vzgd; 784 rl_t *rl = zgd->zgd_rl; 785 vnode_t *vp = ZTOV(rl->r_zp); 786 787 dmu_buf_rele(db, vzgd); 788 zfs_range_unlock(rl); 789 VN_RELE(vp); 790 zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp))); 791 kmem_free(zgd, sizeof (zgd_t)); 792 } 793 794 /* 795 * Get data to generate a TX_WRITE intent log record. 796 */ 797 int 798 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 799 { 800 zfsvfs_t *zfsvfs = arg; 801 objset_t *os = zfsvfs->z_os; 802 znode_t *zp; 803 uint64_t off = lr->lr_offset; 804 dmu_buf_t *db; 805 rl_t *rl; 806 zgd_t *zgd; 807 int dlen = lr->lr_length; /* length of user data */ 808 int error = 0; 809 810 ASSERT(zio); 811 ASSERT(dlen != 0); 812 813 /* 814 * Nothing to do if the file has been removed 815 */ 816 if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 817 return (ENOENT); 818 if (zp->z_unlinked) { 819 VN_RELE(ZTOV(zp)); 820 return (ENOENT); 821 } 822 823 /* 824 * Write records come in two flavors: immediate and indirect. 825 * For small writes it's cheaper to store the data with the 826 * log record (immediate); for large writes it's cheaper to 827 * sync the data and get a pointer to it (indirect) so that 828 * we don't have to write the data twice. 829 */ 830 if (buf != NULL) { /* immediate write */ 831 rl = zfs_range_lock(zp, off, dlen, RL_READER); 832 /* test for truncation needs to be done while range locked */ 833 if (off >= zp->z_phys->zp_size) { 834 error = ENOENT; 835 goto out; 836 } 837 VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 838 } else { /* indirect write */ 839 uint64_t boff; /* block starting offset */ 840 841 ASSERT3U(dlen, <=, zp->z_blksz); 842 /* 843 * Have to lock the whole block to ensure when it's 844 * written out and it's checksum is being calculated 845 * that no one can change the data. We need to re-check 846 * blocksize after we get the lock in case it's changed! 847 */ 848 for (;;) { 849 if (ISP2(zp->z_blksz)) { 850 boff = P2ALIGN_TYPED(off, zp->z_blksz, 851 uint64_t); 852 } else { 853 boff = 0; 854 } 855 dlen = zp->z_blksz; 856 rl = zfs_range_lock(zp, boff, dlen, RL_READER); 857 if (zp->z_blksz == dlen) 858 break; 859 zfs_range_unlock(rl); 860 } 861 /* test for truncation needs to be done while range locked */ 862 if (off >= zp->z_phys->zp_size) { 863 error = ENOENT; 864 goto out; 865 } 866 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 867 zgd->zgd_rl = rl; 868 zgd->zgd_zilog = zfsvfs->z_log; 869 zgd->zgd_bp = &lr->lr_blkptr; 870 VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 871 ASSERT(boff == db->db_offset); 872 lr->lr_blkoff = off - boff; 873 error = dmu_sync(zio, db, &lr->lr_blkptr, 874 lr->lr_common.lrc_txg, zfs_get_done, zgd); 875 if (error == 0) { 876 zil_add_vdev(zfsvfs->z_log, 877 DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr))); 878 } 879 /* 880 * If we get EINPROGRESS, then we need to wait for a 881 * write IO initiated by dmu_sync() to complete before 882 * we can release this dbuf. We will finish everything 883 * up in the zfs_get_done() callback. 884 */ 885 if (error == EINPROGRESS) 886 return (0); 887 dmu_buf_rele(db, zgd); 888 kmem_free(zgd, sizeof (zgd_t)); 889 } 890 out: 891 zfs_range_unlock(rl); 892 VN_RELE(ZTOV(zp)); 893 return (error); 894 } 895 896 /*ARGSUSED*/ 897 static int 898 zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr) 899 { 900 znode_t *zp = VTOZ(vp); 901 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 902 int error; 903 904 ZFS_ENTER(zfsvfs); 905 error = zfs_zaccess_rwx(zp, mode, cr); 906 ZFS_EXIT(zfsvfs); 907 return (error); 908 } 909 910 /* 911 * Lookup an entry in a directory, or an extended attribute directory. 912 * If it exists, return a held vnode reference for it. 913 * 914 * IN: dvp - vnode of directory to search. 915 * nm - name of entry to lookup. 916 * pnp - full pathname to lookup [UNUSED]. 917 * flags - LOOKUP_XATTR set if looking for an attribute. 918 * rdir - root directory vnode [UNUSED]. 919 * cr - credentials of caller. 920 * 921 * OUT: vpp - vnode of located entry, NULL if not found. 922 * 923 * RETURN: 0 if success 924 * error code if failure 925 * 926 * Timestamps: 927 * NA 928 */ 929 /* ARGSUSED */ 930 static int 931 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 932 int flags, vnode_t *rdir, cred_t *cr) 933 { 934 935 znode_t *zdp = VTOZ(dvp); 936 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 937 int error; 938 939 ZFS_ENTER(zfsvfs); 940 941 *vpp = NULL; 942 943 if (flags & LOOKUP_XATTR) { 944 /* 945 * If the xattr property is off, refuse the lookup request. 946 */ 947 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 948 ZFS_EXIT(zfsvfs); 949 return (EINVAL); 950 } 951 952 /* 953 * We don't allow recursive attributes.. 954 * Maybe someday we will. 955 */ 956 if (zdp->z_phys->zp_flags & ZFS_XATTR) { 957 ZFS_EXIT(zfsvfs); 958 return (EINVAL); 959 } 960 961 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 962 ZFS_EXIT(zfsvfs); 963 return (error); 964 } 965 966 /* 967 * Do we have permission to get into attribute directory? 968 */ 969 970 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 971 VN_RELE(*vpp); 972 } 973 974 ZFS_EXIT(zfsvfs); 975 return (error); 976 } 977 978 if (dvp->v_type != VDIR) { 979 ZFS_EXIT(zfsvfs); 980 return (ENOTDIR); 981 } 982 983 /* 984 * Check accessibility of directory. 985 */ 986 987 if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 988 ZFS_EXIT(zfsvfs); 989 return (error); 990 } 991 992 if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 993 994 /* 995 * Convert device special files 996 */ 997 if (IS_DEVVP(*vpp)) { 998 vnode_t *svp; 999 1000 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1001 VN_RELE(*vpp); 1002 if (svp == NULL) 1003 error = ENOSYS; 1004 else 1005 *vpp = svp; 1006 } 1007 } 1008 1009 ZFS_EXIT(zfsvfs); 1010 return (error); 1011 } 1012 1013 /* 1014 * Attempt to create a new entry in a directory. If the entry 1015 * already exists, truncate the file if permissible, else return 1016 * an error. Return the vp of the created or trunc'd file. 1017 * 1018 * IN: dvp - vnode of directory to put new file entry in. 1019 * name - name of new file entry. 1020 * vap - attributes of new file. 1021 * excl - flag indicating exclusive or non-exclusive mode. 1022 * mode - mode to open file with. 1023 * cr - credentials of caller. 1024 * flag - large file flag [UNUSED]. 1025 * 1026 * OUT: vpp - vnode of created or trunc'd entry. 1027 * 1028 * RETURN: 0 if success 1029 * error code if failure 1030 * 1031 * Timestamps: 1032 * dvp - ctime|mtime updated if new entry created 1033 * vp - ctime|mtime always, atime if new 1034 */ 1035 /* ARGSUSED */ 1036 static int 1037 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1038 int mode, vnode_t **vpp, cred_t *cr, int flag) 1039 { 1040 znode_t *zp, *dzp = VTOZ(dvp); 1041 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1042 zilog_t *zilog = zfsvfs->z_log; 1043 objset_t *os = zfsvfs->z_os; 1044 zfs_dirlock_t *dl; 1045 dmu_tx_t *tx; 1046 int error; 1047 uint64_t zoid; 1048 1049 ZFS_ENTER(zfsvfs); 1050 1051 top: 1052 *vpp = NULL; 1053 1054 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1055 vap->va_mode &= ~VSVTX; 1056 1057 if (*name == '\0') { 1058 /* 1059 * Null component name refers to the directory itself. 1060 */ 1061 VN_HOLD(dvp); 1062 zp = dzp; 1063 dl = NULL; 1064 error = 0; 1065 } else { 1066 /* possible VN_HOLD(zp) */ 1067 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1068 if (strcmp(name, "..") == 0) 1069 error = EISDIR; 1070 ZFS_EXIT(zfsvfs); 1071 return (error); 1072 } 1073 } 1074 1075 zoid = zp ? zp->z_id : -1ULL; 1076 1077 if (zp == NULL) { 1078 /* 1079 * Create a new file object and update the directory 1080 * to reference it. 1081 */ 1082 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1083 goto out; 1084 } 1085 1086 /* 1087 * We only support the creation of regular files in 1088 * extended attribute directories. 1089 */ 1090 if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1091 (vap->va_type != VREG)) { 1092 error = EINVAL; 1093 goto out; 1094 } 1095 1096 tx = dmu_tx_create(os); 1097 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1098 dmu_tx_hold_bonus(tx, dzp->z_id); 1099 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1100 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1101 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1102 0, SPA_MAXBLOCKSIZE); 1103 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1104 if (error) { 1105 zfs_dirent_unlock(dl); 1106 if (error == ERESTART && 1107 zfsvfs->z_assign == TXG_NOWAIT) { 1108 dmu_tx_wait(tx); 1109 dmu_tx_abort(tx); 1110 goto top; 1111 } 1112 dmu_tx_abort(tx); 1113 ZFS_EXIT(zfsvfs); 1114 return (error); 1115 } 1116 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1117 ASSERT(zp->z_id == zoid); 1118 (void) zfs_link_create(dl, zp, tx, ZNEW); 1119 zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1120 dmu_tx_commit(tx); 1121 } else { 1122 /* 1123 * A directory entry already exists for this name. 1124 */ 1125 /* 1126 * Can't truncate an existing file if in exclusive mode. 1127 */ 1128 if (excl == EXCL) { 1129 error = EEXIST; 1130 goto out; 1131 } 1132 /* 1133 * Can't open a directory for writing. 1134 */ 1135 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1136 error = EISDIR; 1137 goto out; 1138 } 1139 /* 1140 * Verify requested access to file. 1141 */ 1142 if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1143 goto out; 1144 } 1145 1146 mutex_enter(&dzp->z_lock); 1147 dzp->z_seq++; 1148 mutex_exit(&dzp->z_lock); 1149 1150 /* 1151 * Truncate regular files if requested. 1152 */ 1153 if ((ZTOV(zp)->v_type == VREG) && 1154 (zp->z_phys->zp_size != 0) && 1155 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 1156 error = zfs_freesp(zp, 0, 0, mode, TRUE); 1157 if (error == ERESTART && 1158 zfsvfs->z_assign == TXG_NOWAIT) { 1159 /* NB: we already did dmu_tx_wait() */ 1160 zfs_dirent_unlock(dl); 1161 VN_RELE(ZTOV(zp)); 1162 goto top; 1163 } 1164 } 1165 } 1166 out: 1167 1168 if (dl) 1169 zfs_dirent_unlock(dl); 1170 1171 if (error) { 1172 if (zp) 1173 VN_RELE(ZTOV(zp)); 1174 } else { 1175 *vpp = ZTOV(zp); 1176 /* 1177 * If vnode is for a device return a specfs vnode instead. 1178 */ 1179 if (IS_DEVVP(*vpp)) { 1180 struct vnode *svp; 1181 1182 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1183 VN_RELE(*vpp); 1184 if (svp == NULL) { 1185 error = ENOSYS; 1186 } 1187 *vpp = svp; 1188 } 1189 } 1190 1191 ZFS_EXIT(zfsvfs); 1192 return (error); 1193 } 1194 1195 /* 1196 * Remove an entry from a directory. 1197 * 1198 * IN: dvp - vnode of directory to remove entry from. 1199 * name - name of entry to remove. 1200 * cr - credentials of caller. 1201 * 1202 * RETURN: 0 if success 1203 * error code if failure 1204 * 1205 * Timestamps: 1206 * dvp - ctime|mtime 1207 * vp - ctime (if nlink > 0) 1208 */ 1209 static int 1210 zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1211 { 1212 znode_t *zp, *dzp = VTOZ(dvp); 1213 znode_t *xzp = NULL; 1214 vnode_t *vp; 1215 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1216 zilog_t *zilog = zfsvfs->z_log; 1217 uint64_t acl_obj, xattr_obj; 1218 zfs_dirlock_t *dl; 1219 dmu_tx_t *tx; 1220 boolean_t may_delete_now, delete_now = FALSE; 1221 boolean_t unlinked; 1222 int error; 1223 1224 ZFS_ENTER(zfsvfs); 1225 1226 top: 1227 /* 1228 * Attempt to lock directory; fail if entry doesn't exist. 1229 */ 1230 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1231 ZFS_EXIT(zfsvfs); 1232 return (error); 1233 } 1234 1235 vp = ZTOV(zp); 1236 1237 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1238 goto out; 1239 } 1240 1241 /* 1242 * Need to use rmdir for removing directories. 1243 */ 1244 if (vp->v_type == VDIR) { 1245 error = EPERM; 1246 goto out; 1247 } 1248 1249 vnevent_remove(vp); 1250 1251 dnlc_remove(dvp, name); 1252 1253 mutex_enter(&vp->v_lock); 1254 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1255 mutex_exit(&vp->v_lock); 1256 1257 /* 1258 * We may delete the znode now, or we may put it in the unlinked set; 1259 * it depends on whether we're the last link, and on whether there are 1260 * other holds on the vnode. So we dmu_tx_hold() the right things to 1261 * allow for either case. 1262 */ 1263 tx = dmu_tx_create(zfsvfs->z_os); 1264 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1265 dmu_tx_hold_bonus(tx, zp->z_id); 1266 if (may_delete_now) 1267 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1268 1269 /* are there any extended attributes? */ 1270 if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1271 /* XXX - do we need this if we are deleting? */ 1272 dmu_tx_hold_bonus(tx, xattr_obj); 1273 } 1274 1275 /* are there any additional acls */ 1276 if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1277 may_delete_now) 1278 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1279 1280 /* charge as an update -- would be nice not to charge at all */ 1281 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1282 1283 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1284 if (error) { 1285 zfs_dirent_unlock(dl); 1286 VN_RELE(vp); 1287 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1288 dmu_tx_wait(tx); 1289 dmu_tx_abort(tx); 1290 goto top; 1291 } 1292 dmu_tx_abort(tx); 1293 ZFS_EXIT(zfsvfs); 1294 return (error); 1295 } 1296 1297 /* 1298 * Remove the directory entry. 1299 */ 1300 error = zfs_link_destroy(dl, zp, tx, 0, &unlinked); 1301 1302 if (error) { 1303 dmu_tx_commit(tx); 1304 goto out; 1305 } 1306 1307 if (unlinked) { 1308 mutex_enter(&vp->v_lock); 1309 delete_now = may_delete_now && 1310 vp->v_count == 1 && !vn_has_cached_data(vp) && 1311 zp->z_phys->zp_xattr == xattr_obj && 1312 zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1313 mutex_exit(&vp->v_lock); 1314 } 1315 1316 if (delete_now) { 1317 if (zp->z_phys->zp_xattr) { 1318 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1319 ASSERT3U(error, ==, 0); 1320 ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1321 dmu_buf_will_dirty(xzp->z_dbuf, tx); 1322 mutex_enter(&xzp->z_lock); 1323 xzp->z_unlinked = 1; 1324 xzp->z_phys->zp_links = 0; 1325 mutex_exit(&xzp->z_lock); 1326 zfs_unlinked_add(xzp, tx); 1327 zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1328 } 1329 mutex_enter(&zp->z_lock); 1330 mutex_enter(&vp->v_lock); 1331 vp->v_count--; 1332 ASSERT3U(vp->v_count, ==, 0); 1333 mutex_exit(&vp->v_lock); 1334 mutex_exit(&zp->z_lock); 1335 zfs_znode_delete(zp, tx); 1336 VFS_RELE(zfsvfs->z_vfs); 1337 } else if (unlinked) { 1338 zfs_unlinked_add(zp, tx); 1339 } 1340 1341 zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1342 1343 dmu_tx_commit(tx); 1344 out: 1345 zfs_dirent_unlock(dl); 1346 1347 if (!delete_now) { 1348 VN_RELE(vp); 1349 } else if (xzp) { 1350 /* this rele delayed to prevent nesting transactions */ 1351 VN_RELE(ZTOV(xzp)); 1352 } 1353 1354 ZFS_EXIT(zfsvfs); 1355 return (error); 1356 } 1357 1358 /* 1359 * Create a new directory and insert it into dvp using the name 1360 * provided. Return a pointer to the inserted directory. 1361 * 1362 * IN: dvp - vnode of directory to add subdir to. 1363 * dirname - name of new directory. 1364 * vap - attributes of new directory. 1365 * cr - credentials of caller. 1366 * 1367 * OUT: vpp - vnode of created directory. 1368 * 1369 * RETURN: 0 if success 1370 * error code if failure 1371 * 1372 * Timestamps: 1373 * dvp - ctime|mtime updated 1374 * vp - ctime|mtime|atime updated 1375 */ 1376 static int 1377 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1378 { 1379 znode_t *zp, *dzp = VTOZ(dvp); 1380 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1381 zilog_t *zilog = zfsvfs->z_log; 1382 zfs_dirlock_t *dl; 1383 uint64_t zoid = 0; 1384 dmu_tx_t *tx; 1385 int error; 1386 1387 ASSERT(vap->va_type == VDIR); 1388 1389 ZFS_ENTER(zfsvfs); 1390 1391 if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1392 ZFS_EXIT(zfsvfs); 1393 return (EINVAL); 1394 } 1395 top: 1396 *vpp = NULL; 1397 1398 /* 1399 * First make sure the new directory doesn't exist. 1400 */ 1401 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1402 ZFS_EXIT(zfsvfs); 1403 return (error); 1404 } 1405 1406 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 1407 zfs_dirent_unlock(dl); 1408 ZFS_EXIT(zfsvfs); 1409 return (error); 1410 } 1411 1412 /* 1413 * Add a new entry to the directory. 1414 */ 1415 tx = dmu_tx_create(zfsvfs->z_os); 1416 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 1417 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1418 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1419 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1420 0, SPA_MAXBLOCKSIZE); 1421 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1422 if (error) { 1423 zfs_dirent_unlock(dl); 1424 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1425 dmu_tx_wait(tx); 1426 dmu_tx_abort(tx); 1427 goto top; 1428 } 1429 dmu_tx_abort(tx); 1430 ZFS_EXIT(zfsvfs); 1431 return (error); 1432 } 1433 1434 /* 1435 * Create new node. 1436 */ 1437 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1438 1439 /* 1440 * Now put new name in parent dir. 1441 */ 1442 (void) zfs_link_create(dl, zp, tx, ZNEW); 1443 1444 *vpp = ZTOV(zp); 1445 1446 zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1447 dmu_tx_commit(tx); 1448 1449 zfs_dirent_unlock(dl); 1450 1451 ZFS_EXIT(zfsvfs); 1452 return (0); 1453 } 1454 1455 /* 1456 * Remove a directory subdir entry. If the current working 1457 * directory is the same as the subdir to be removed, the 1458 * remove will fail. 1459 * 1460 * IN: dvp - vnode of directory to remove from. 1461 * name - name of directory to be removed. 1462 * cwd - vnode of current working directory. 1463 * cr - credentials of caller. 1464 * 1465 * RETURN: 0 if success 1466 * error code if failure 1467 * 1468 * Timestamps: 1469 * dvp - ctime|mtime updated 1470 */ 1471 static int 1472 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1473 { 1474 znode_t *dzp = VTOZ(dvp); 1475 znode_t *zp; 1476 vnode_t *vp; 1477 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1478 zilog_t *zilog = zfsvfs->z_log; 1479 zfs_dirlock_t *dl; 1480 dmu_tx_t *tx; 1481 int error; 1482 1483 ZFS_ENTER(zfsvfs); 1484 1485 top: 1486 zp = NULL; 1487 1488 /* 1489 * Attempt to lock directory; fail if entry doesn't exist. 1490 */ 1491 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1492 ZFS_EXIT(zfsvfs); 1493 return (error); 1494 } 1495 1496 vp = ZTOV(zp); 1497 1498 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1499 goto out; 1500 } 1501 1502 if (vp->v_type != VDIR) { 1503 error = ENOTDIR; 1504 goto out; 1505 } 1506 1507 if (vp == cwd) { 1508 error = EINVAL; 1509 goto out; 1510 } 1511 1512 vnevent_rmdir(vp); 1513 1514 /* 1515 * Grab a lock on the parent pointer make sure we play well 1516 * with the treewalk and directory rename code. 1517 */ 1518 rw_enter(&zp->z_parent_lock, RW_WRITER); 1519 1520 tx = dmu_tx_create(zfsvfs->z_os); 1521 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1522 dmu_tx_hold_bonus(tx, zp->z_id); 1523 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1524 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1525 if (error) { 1526 rw_exit(&zp->z_parent_lock); 1527 zfs_dirent_unlock(dl); 1528 VN_RELE(vp); 1529 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1530 dmu_tx_wait(tx); 1531 dmu_tx_abort(tx); 1532 goto top; 1533 } 1534 dmu_tx_abort(tx); 1535 ZFS_EXIT(zfsvfs); 1536 return (error); 1537 } 1538 1539 error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1540 1541 if (error == 0) 1542 zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1543 1544 dmu_tx_commit(tx); 1545 1546 rw_exit(&zp->z_parent_lock); 1547 out: 1548 zfs_dirent_unlock(dl); 1549 1550 VN_RELE(vp); 1551 1552 ZFS_EXIT(zfsvfs); 1553 return (error); 1554 } 1555 1556 /* 1557 * Read as many directory entries as will fit into the provided 1558 * buffer from the given directory cursor position (specified in 1559 * the uio structure. 1560 * 1561 * IN: vp - vnode of directory to read. 1562 * uio - structure supplying read location, range info, 1563 * and return buffer. 1564 * cr - credentials of caller. 1565 * 1566 * OUT: uio - updated offset and range, buffer filled. 1567 * eofp - set to true if end-of-file detected. 1568 * 1569 * RETURN: 0 if success 1570 * error code if failure 1571 * 1572 * Timestamps: 1573 * vp - atime updated 1574 * 1575 * Note that the low 4 bits of the cookie returned by zap is always zero. 1576 * This allows us to use the low range for "special" directory entries: 1577 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1578 * we use the offset 2 for the '.zfs' directory. 1579 */ 1580 /* ARGSUSED */ 1581 static int 1582 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1583 { 1584 znode_t *zp = VTOZ(vp); 1585 iovec_t *iovp; 1586 dirent64_t *odp; 1587 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1588 objset_t *os; 1589 caddr_t outbuf; 1590 size_t bufsize; 1591 zap_cursor_t zc; 1592 zap_attribute_t zap; 1593 uint_t bytes_wanted; 1594 ushort_t this_reclen; 1595 uint64_t offset; /* must be unsigned; checks for < 1 */ 1596 off64_t *next; 1597 int local_eof; 1598 int outcount; 1599 int error; 1600 uint8_t prefetch; 1601 1602 ZFS_ENTER(zfsvfs); 1603 1604 /* 1605 * If we are not given an eof variable, 1606 * use a local one. 1607 */ 1608 if (eofp == NULL) 1609 eofp = &local_eof; 1610 1611 /* 1612 * Check for valid iov_len. 1613 */ 1614 if (uio->uio_iov->iov_len <= 0) { 1615 ZFS_EXIT(zfsvfs); 1616 return (EINVAL); 1617 } 1618 1619 /* 1620 * Quit if directory has been removed (posix) 1621 */ 1622 if ((*eofp = zp->z_unlinked) != 0) { 1623 ZFS_EXIT(zfsvfs); 1624 return (0); 1625 } 1626 1627 error = 0; 1628 os = zfsvfs->z_os; 1629 offset = uio->uio_loffset; 1630 prefetch = zp->z_zn_prefetch; 1631 1632 /* 1633 * Initialize the iterator cursor. 1634 */ 1635 if (offset <= 3) { 1636 /* 1637 * Start iteration from the beginning of the directory. 1638 */ 1639 zap_cursor_init(&zc, os, zp->z_id); 1640 } else { 1641 /* 1642 * The offset is a serialized cursor. 1643 */ 1644 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1645 } 1646 1647 /* 1648 * Get space to change directory entries into fs independent format. 1649 */ 1650 iovp = uio->uio_iov; 1651 bytes_wanted = iovp->iov_len; 1652 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1653 bufsize = bytes_wanted; 1654 outbuf = kmem_alloc(bufsize, KM_SLEEP); 1655 odp = (struct dirent64 *)outbuf; 1656 } else { 1657 bufsize = bytes_wanted; 1658 odp = (struct dirent64 *)iovp->iov_base; 1659 } 1660 1661 /* 1662 * Transform to file-system independent format 1663 */ 1664 outcount = 0; 1665 while (outcount < bytes_wanted) { 1666 /* 1667 * Special case `.', `..', and `.zfs'. 1668 */ 1669 if (offset == 0) { 1670 (void) strcpy(zap.za_name, "."); 1671 zap.za_first_integer = zp->z_id; 1672 this_reclen = DIRENT64_RECLEN(1); 1673 } else if (offset == 1) { 1674 (void) strcpy(zap.za_name, ".."); 1675 zap.za_first_integer = zp->z_phys->zp_parent; 1676 this_reclen = DIRENT64_RECLEN(2); 1677 } else if (offset == 2 && zfs_show_ctldir(zp)) { 1678 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1679 zap.za_first_integer = ZFSCTL_INO_ROOT; 1680 this_reclen = 1681 DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1); 1682 } else { 1683 /* 1684 * Grab next entry. 1685 */ 1686 if (error = zap_cursor_retrieve(&zc, &zap)) { 1687 if ((*eofp = (error == ENOENT)) != 0) 1688 break; 1689 else 1690 goto update; 1691 } 1692 1693 if (zap.za_integer_length != 8 || 1694 zap.za_num_integers != 1) { 1695 cmn_err(CE_WARN, "zap_readdir: bad directory " 1696 "entry, obj = %lld, offset = %lld\n", 1697 (u_longlong_t)zp->z_id, 1698 (u_longlong_t)offset); 1699 error = ENXIO; 1700 goto update; 1701 } 1702 this_reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1703 } 1704 1705 /* 1706 * Will this entry fit in the buffer? 1707 */ 1708 if (outcount + this_reclen > bufsize) { 1709 /* 1710 * Did we manage to fit anything in the buffer? 1711 */ 1712 if (!outcount) { 1713 error = EINVAL; 1714 goto update; 1715 } 1716 break; 1717 } 1718 /* 1719 * Add this entry: 1720 */ 1721 odp->d_ino = (ino64_t)zap.za_first_integer; 1722 odp->d_reclen = (ushort_t)this_reclen; 1723 /* NOTE: d_off is the offset for the *next* entry */ 1724 next = &(odp->d_off); 1725 (void) strncpy(odp->d_name, zap.za_name, 1726 DIRENT64_NAMELEN(this_reclen)); 1727 outcount += this_reclen; 1728 odp = (dirent64_t *)((intptr_t)odp + this_reclen); 1729 1730 ASSERT(outcount <= bufsize); 1731 1732 /* Prefetch znode */ 1733 if (prefetch) 1734 dmu_prefetch(os, zap.za_first_integer, 0, 0); 1735 1736 /* 1737 * Move to the next entry, fill in the previous offset. 1738 */ 1739 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1740 zap_cursor_advance(&zc); 1741 offset = zap_cursor_serialize(&zc); 1742 } else { 1743 offset += 1; 1744 } 1745 *next = offset; 1746 } 1747 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1748 1749 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1750 iovp->iov_base += outcount; 1751 iovp->iov_len -= outcount; 1752 uio->uio_resid -= outcount; 1753 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1754 /* 1755 * Reset the pointer. 1756 */ 1757 offset = uio->uio_loffset; 1758 } 1759 1760 update: 1761 zap_cursor_fini(&zc); 1762 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1763 kmem_free(outbuf, bufsize); 1764 1765 if (error == ENOENT) 1766 error = 0; 1767 1768 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1769 1770 uio->uio_loffset = offset; 1771 ZFS_EXIT(zfsvfs); 1772 return (error); 1773 } 1774 1775 static int 1776 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1777 { 1778 znode_t *zp = VTOZ(vp); 1779 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1780 1781 /* 1782 * Regardless of whether this is required for standards conformance, 1783 * this is the logical behavior when fsync() is called on a file with 1784 * dirty pages. We use B_ASYNC since the ZIL transactions are already 1785 * going to be pushed out as part of the zil_commit(). 1786 */ 1787 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 1788 (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 1789 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 1790 1791 ZFS_ENTER(zfsvfs); 1792 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 1793 ZFS_EXIT(zfsvfs); 1794 return (0); 1795 } 1796 1797 /* 1798 * Get the requested file attributes and place them in the provided 1799 * vattr structure. 1800 * 1801 * IN: vp - vnode of file. 1802 * vap - va_mask identifies requested attributes. 1803 * flags - [UNUSED] 1804 * cr - credentials of caller. 1805 * 1806 * OUT: vap - attribute values. 1807 * 1808 * RETURN: 0 (always succeeds) 1809 */ 1810 /* ARGSUSED */ 1811 static int 1812 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1813 { 1814 znode_t *zp = VTOZ(vp); 1815 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1816 znode_phys_t *pzp = zp->z_phys; 1817 int error; 1818 1819 ZFS_ENTER(zfsvfs); 1820 1821 /* 1822 * Return all attributes. It's cheaper to provide the answer 1823 * than to determine whether we were asked the question. 1824 */ 1825 mutex_enter(&zp->z_lock); 1826 1827 vap->va_type = vp->v_type; 1828 vap->va_mode = pzp->zp_mode & MODEMASK; 1829 vap->va_uid = zp->z_phys->zp_uid; 1830 vap->va_gid = zp->z_phys->zp_gid; 1831 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1832 vap->va_nodeid = zp->z_id; 1833 vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX); /* nlink_t limit! */ 1834 vap->va_size = pzp->zp_size; 1835 vap->va_rdev = vp->v_rdev; 1836 vap->va_seq = zp->z_seq; 1837 1838 ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1839 ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1840 ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1841 1842 /* 1843 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1844 * Also, if we are the owner don't bother, since owner should 1845 * always be allowed to read basic attributes of file. 1846 */ 1847 if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1848 (zp->z_phys->zp_uid != crgetuid(cr))) { 1849 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1850 mutex_exit(&zp->z_lock); 1851 ZFS_EXIT(zfsvfs); 1852 return (error); 1853 } 1854 } 1855 1856 mutex_exit(&zp->z_lock); 1857 1858 dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1859 1860 if (zp->z_blksz == 0) { 1861 /* 1862 * Block size hasn't been set; suggest maximal I/O transfers. 1863 */ 1864 vap->va_blksize = zfsvfs->z_max_blksz; 1865 } 1866 1867 ZFS_EXIT(zfsvfs); 1868 return (0); 1869 } 1870 1871 /* 1872 * Set the file attributes to the values contained in the 1873 * vattr structure. 1874 * 1875 * IN: vp - vnode of file to be modified. 1876 * vap - new attribute values. 1877 * flags - ATTR_UTIME set if non-default time values provided. 1878 * cr - credentials of caller. 1879 * 1880 * RETURN: 0 if success 1881 * error code if failure 1882 * 1883 * Timestamps: 1884 * vp - ctime updated, mtime updated if size changed. 1885 */ 1886 /* ARGSUSED */ 1887 static int 1888 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1889 caller_context_t *ct) 1890 { 1891 struct znode *zp = VTOZ(vp); 1892 znode_phys_t *pzp = zp->z_phys; 1893 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1894 zilog_t *zilog = zfsvfs->z_log; 1895 dmu_tx_t *tx; 1896 vattr_t oldva; 1897 uint_t mask = vap->va_mask; 1898 uint_t saved_mask; 1899 int trim_mask = 0; 1900 uint64_t new_mode; 1901 znode_t *attrzp; 1902 int need_policy = FALSE; 1903 int err; 1904 1905 if (mask == 0) 1906 return (0); 1907 1908 if (mask & AT_NOSET) 1909 return (EINVAL); 1910 1911 if (mask & AT_SIZE && vp->v_type == VDIR) 1912 return (EISDIR); 1913 1914 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 1915 return (EINVAL); 1916 1917 ZFS_ENTER(zfsvfs); 1918 1919 top: 1920 attrzp = NULL; 1921 1922 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1923 ZFS_EXIT(zfsvfs); 1924 return (EROFS); 1925 } 1926 1927 /* 1928 * First validate permissions 1929 */ 1930 1931 if (mask & AT_SIZE) { 1932 err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1933 if (err) { 1934 ZFS_EXIT(zfsvfs); 1935 return (err); 1936 } 1937 /* 1938 * XXX - Note, we are not providing any open 1939 * mode flags here (like FNDELAY), so we may 1940 * block if there are locks present... this 1941 * should be addressed in openat(). 1942 */ 1943 do { 1944 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 1945 /* NB: we already did dmu_tx_wait() if necessary */ 1946 } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 1947 if (err) { 1948 ZFS_EXIT(zfsvfs); 1949 return (err); 1950 } 1951 } 1952 1953 if (mask & (AT_ATIME|AT_MTIME)) 1954 need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 1955 1956 if (mask & (AT_UID|AT_GID)) { 1957 int idmask = (mask & (AT_UID|AT_GID)); 1958 int take_owner; 1959 int take_group; 1960 1961 /* 1962 * NOTE: even if a new mode is being set, 1963 * we may clear S_ISUID/S_ISGID bits. 1964 */ 1965 1966 if (!(mask & AT_MODE)) 1967 vap->va_mode = pzp->zp_mode; 1968 1969 /* 1970 * Take ownership or chgrp to group we are a member of 1971 */ 1972 1973 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 1974 take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 1975 1976 /* 1977 * If both AT_UID and AT_GID are set then take_owner and 1978 * take_group must both be set in order to allow taking 1979 * ownership. 1980 * 1981 * Otherwise, send the check through secpolicy_vnode_setattr() 1982 * 1983 */ 1984 1985 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 1986 ((idmask == AT_UID) && take_owner) || 1987 ((idmask == AT_GID) && take_group)) { 1988 if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 1989 /* 1990 * Remove setuid/setgid for non-privileged users 1991 */ 1992 secpolicy_setid_clear(vap, cr); 1993 trim_mask = (mask & (AT_UID|AT_GID)); 1994 } else { 1995 need_policy = TRUE; 1996 } 1997 } else { 1998 need_policy = TRUE; 1999 } 2000 } 2001 2002 mutex_enter(&zp->z_lock); 2003 oldva.va_mode = pzp->zp_mode; 2004 oldva.va_uid = zp->z_phys->zp_uid; 2005 oldva.va_gid = zp->z_phys->zp_gid; 2006 mutex_exit(&zp->z_lock); 2007 2008 if (mask & AT_MODE) { 2009 if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) { 2010 err = secpolicy_setid_setsticky_clear(vp, vap, 2011 &oldva, cr); 2012 if (err) { 2013 ZFS_EXIT(zfsvfs); 2014 return (err); 2015 } 2016 trim_mask |= AT_MODE; 2017 } else { 2018 need_policy = TRUE; 2019 } 2020 } 2021 2022 if (need_policy) { 2023 /* 2024 * If trim_mask is set then take ownership 2025 * has been granted or write_acl is present and user 2026 * has the ability to modify mode. In that case remove 2027 * UID|GID and or MODE from mask so that 2028 * secpolicy_vnode_setattr() doesn't revoke it. 2029 */ 2030 2031 if (trim_mask) { 2032 saved_mask = vap->va_mask; 2033 vap->va_mask &= ~trim_mask; 2034 2035 } 2036 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2037 (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2038 if (err) { 2039 ZFS_EXIT(zfsvfs); 2040 return (err); 2041 } 2042 2043 if (trim_mask) 2044 vap->va_mask |= saved_mask; 2045 } 2046 2047 /* 2048 * secpolicy_vnode_setattr, or take ownership may have 2049 * changed va_mask 2050 */ 2051 mask = vap->va_mask; 2052 2053 tx = dmu_tx_create(zfsvfs->z_os); 2054 dmu_tx_hold_bonus(tx, zp->z_id); 2055 2056 if (mask & AT_MODE) { 2057 uint64_t pmode = pzp->zp_mode; 2058 2059 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2060 2061 if (zp->z_phys->zp_acl.z_acl_extern_obj) 2062 dmu_tx_hold_write(tx, 2063 pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2064 else 2065 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2066 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2067 } 2068 2069 if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 2070 err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 2071 if (err) { 2072 dmu_tx_abort(tx); 2073 ZFS_EXIT(zfsvfs); 2074 return (err); 2075 } 2076 dmu_tx_hold_bonus(tx, attrzp->z_id); 2077 } 2078 2079 err = dmu_tx_assign(tx, zfsvfs->z_assign); 2080 if (err) { 2081 if (attrzp) 2082 VN_RELE(ZTOV(attrzp)); 2083 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2084 dmu_tx_wait(tx); 2085 dmu_tx_abort(tx); 2086 goto top; 2087 } 2088 dmu_tx_abort(tx); 2089 ZFS_EXIT(zfsvfs); 2090 return (err); 2091 } 2092 2093 dmu_buf_will_dirty(zp->z_dbuf, tx); 2094 2095 /* 2096 * Set each attribute requested. 2097 * We group settings according to the locks they need to acquire. 2098 * 2099 * Note: you cannot set ctime directly, although it will be 2100 * updated as a side-effect of calling this function. 2101 */ 2102 2103 mutex_enter(&zp->z_lock); 2104 2105 if (mask & AT_MODE) { 2106 err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2107 ASSERT3U(err, ==, 0); 2108 } 2109 2110 if (attrzp) 2111 mutex_enter(&attrzp->z_lock); 2112 2113 if (mask & AT_UID) { 2114 zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 2115 if (attrzp) { 2116 attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 2117 } 2118 } 2119 2120 if (mask & AT_GID) { 2121 zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 2122 if (attrzp) 2123 attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 2124 } 2125 2126 if (attrzp) 2127 mutex_exit(&attrzp->z_lock); 2128 2129 if (mask & AT_ATIME) 2130 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2131 2132 if (mask & AT_MTIME) 2133 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2134 2135 if (mask & AT_SIZE) 2136 zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 2137 else if (mask != 0) 2138 zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2139 2140 if (mask != 0) 2141 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2142 2143 mutex_exit(&zp->z_lock); 2144 2145 if (attrzp) 2146 VN_RELE(ZTOV(attrzp)); 2147 2148 dmu_tx_commit(tx); 2149 2150 ZFS_EXIT(zfsvfs); 2151 return (err); 2152 } 2153 2154 typedef struct zfs_zlock { 2155 krwlock_t *zl_rwlock; /* lock we acquired */ 2156 znode_t *zl_znode; /* znode we held */ 2157 struct zfs_zlock *zl_next; /* next in list */ 2158 } zfs_zlock_t; 2159 2160 /* 2161 * Drop locks and release vnodes that were held by zfs_rename_lock(). 2162 */ 2163 static void 2164 zfs_rename_unlock(zfs_zlock_t **zlpp) 2165 { 2166 zfs_zlock_t *zl; 2167 2168 while ((zl = *zlpp) != NULL) { 2169 if (zl->zl_znode != NULL) 2170 VN_RELE(ZTOV(zl->zl_znode)); 2171 rw_exit(zl->zl_rwlock); 2172 *zlpp = zl->zl_next; 2173 kmem_free(zl, sizeof (*zl)); 2174 } 2175 } 2176 2177 /* 2178 * Search back through the directory tree, using the ".." entries. 2179 * Lock each directory in the chain to prevent concurrent renames. 2180 * Fail any attempt to move a directory into one of its own descendants. 2181 * XXX - z_parent_lock can overlap with map or grow locks 2182 */ 2183 static int 2184 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2185 { 2186 zfs_zlock_t *zl; 2187 znode_t *zp = tdzp; 2188 uint64_t rootid = zp->z_zfsvfs->z_root; 2189 uint64_t *oidp = &zp->z_id; 2190 krwlock_t *rwlp = &szp->z_parent_lock; 2191 krw_t rw = RW_WRITER; 2192 2193 /* 2194 * First pass write-locks szp and compares to zp->z_id. 2195 * Later passes read-lock zp and compare to zp->z_parent. 2196 */ 2197 do { 2198 if (!rw_tryenter(rwlp, rw)) { 2199 /* 2200 * Another thread is renaming in this path. 2201 * Note that if we are a WRITER, we don't have any 2202 * parent_locks held yet. 2203 */ 2204 if (rw == RW_READER && zp->z_id > szp->z_id) { 2205 /* 2206 * Drop our locks and restart 2207 */ 2208 zfs_rename_unlock(&zl); 2209 *zlpp = NULL; 2210 zp = tdzp; 2211 oidp = &zp->z_id; 2212 rwlp = &szp->z_parent_lock; 2213 rw = RW_WRITER; 2214 continue; 2215 } else { 2216 /* 2217 * Wait for other thread to drop its locks 2218 */ 2219 rw_enter(rwlp, rw); 2220 } 2221 } 2222 2223 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2224 zl->zl_rwlock = rwlp; 2225 zl->zl_znode = NULL; 2226 zl->zl_next = *zlpp; 2227 *zlpp = zl; 2228 2229 if (*oidp == szp->z_id) /* We're a descendant of szp */ 2230 return (EINVAL); 2231 2232 if (*oidp == rootid) /* We've hit the top */ 2233 return (0); 2234 2235 if (rw == RW_READER) { /* i.e. not the first pass */ 2236 int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2237 if (error) 2238 return (error); 2239 zl->zl_znode = zp; 2240 } 2241 oidp = &zp->z_phys->zp_parent; 2242 rwlp = &zp->z_parent_lock; 2243 rw = RW_READER; 2244 2245 } while (zp->z_id != sdzp->z_id); 2246 2247 return (0); 2248 } 2249 2250 /* 2251 * Move an entry from the provided source directory to the target 2252 * directory. Change the entry name as indicated. 2253 * 2254 * IN: sdvp - Source directory containing the "old entry". 2255 * snm - Old entry name. 2256 * tdvp - Target directory to contain the "new entry". 2257 * tnm - New entry name. 2258 * cr - credentials of caller. 2259 * 2260 * RETURN: 0 if success 2261 * error code if failure 2262 * 2263 * Timestamps: 2264 * sdvp,tdvp - ctime|mtime updated 2265 */ 2266 static int 2267 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2268 { 2269 znode_t *tdzp, *szp, *tzp; 2270 znode_t *sdzp = VTOZ(sdvp); 2271 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2272 zilog_t *zilog = zfsvfs->z_log; 2273 vnode_t *realvp; 2274 zfs_dirlock_t *sdl, *tdl; 2275 dmu_tx_t *tx; 2276 zfs_zlock_t *zl; 2277 int cmp, serr, terr, error; 2278 2279 ZFS_ENTER(zfsvfs); 2280 2281 /* 2282 * Make sure we have the real vp for the target directory. 2283 */ 2284 if (VOP_REALVP(tdvp, &realvp) == 0) 2285 tdvp = realvp; 2286 2287 if (tdvp->v_vfsp != sdvp->v_vfsp) { 2288 ZFS_EXIT(zfsvfs); 2289 return (EXDEV); 2290 } 2291 2292 tdzp = VTOZ(tdvp); 2293 top: 2294 szp = NULL; 2295 tzp = NULL; 2296 zl = NULL; 2297 2298 /* 2299 * This is to prevent the creation of links into attribute space 2300 * by renaming a linked file into/outof an attribute directory. 2301 * See the comment in zfs_link() for why this is considered bad. 2302 */ 2303 if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2304 (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2305 ZFS_EXIT(zfsvfs); 2306 return (EINVAL); 2307 } 2308 2309 /* 2310 * Lock source and target directory entries. To prevent deadlock, 2311 * a lock ordering must be defined. We lock the directory with 2312 * the smallest object id first, or if it's a tie, the one with 2313 * the lexically first name. 2314 */ 2315 if (sdzp->z_id < tdzp->z_id) { 2316 cmp = -1; 2317 } else if (sdzp->z_id > tdzp->z_id) { 2318 cmp = 1; 2319 } else { 2320 cmp = strcmp(snm, tnm); 2321 if (cmp == 0) { 2322 /* 2323 * POSIX: "If the old argument and the new argument 2324 * both refer to links to the same existing file, 2325 * the rename() function shall return successfully 2326 * and perform no other action." 2327 */ 2328 ZFS_EXIT(zfsvfs); 2329 return (0); 2330 } 2331 } 2332 if (cmp < 0) { 2333 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2334 terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2335 } else { 2336 terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2337 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2338 } 2339 2340 if (serr) { 2341 /* 2342 * Source entry invalid or not there. 2343 */ 2344 if (!terr) { 2345 zfs_dirent_unlock(tdl); 2346 if (tzp) 2347 VN_RELE(ZTOV(tzp)); 2348 } 2349 if (strcmp(snm, "..") == 0) 2350 serr = EINVAL; 2351 ZFS_EXIT(zfsvfs); 2352 return (serr); 2353 } 2354 if (terr) { 2355 zfs_dirent_unlock(sdl); 2356 VN_RELE(ZTOV(szp)); 2357 if (strcmp(tnm, "..") == 0) 2358 terr = EINVAL; 2359 ZFS_EXIT(zfsvfs); 2360 return (terr); 2361 } 2362 2363 /* 2364 * Must have write access at the source to remove the old entry 2365 * and write access at the target to create the new entry. 2366 * Note that if target and source are the same, this can be 2367 * done in a single check. 2368 */ 2369 2370 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2371 goto out; 2372 2373 if (ZTOV(szp)->v_type == VDIR) { 2374 /* 2375 * Check to make sure rename is valid. 2376 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2377 */ 2378 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2379 goto out; 2380 } 2381 2382 /* 2383 * Does target exist? 2384 */ 2385 if (tzp) { 2386 /* 2387 * Source and target must be the same type. 2388 */ 2389 if (ZTOV(szp)->v_type == VDIR) { 2390 if (ZTOV(tzp)->v_type != VDIR) { 2391 error = ENOTDIR; 2392 goto out; 2393 } 2394 } else { 2395 if (ZTOV(tzp)->v_type == VDIR) { 2396 error = EISDIR; 2397 goto out; 2398 } 2399 } 2400 /* 2401 * POSIX dictates that when the source and target 2402 * entries refer to the same file object, rename 2403 * must do nothing and exit without error. 2404 */ 2405 if (szp->z_id == tzp->z_id) { 2406 error = 0; 2407 goto out; 2408 } 2409 } 2410 2411 vnevent_rename_src(ZTOV(szp)); 2412 if (tzp) 2413 vnevent_rename_dest(ZTOV(tzp)); 2414 2415 tx = dmu_tx_create(zfsvfs->z_os); 2416 dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2417 dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 2418 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 2419 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 2420 if (sdzp != tdzp) 2421 dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 2422 if (tzp) 2423 dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 2424 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2425 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2426 if (error) { 2427 if (zl != NULL) 2428 zfs_rename_unlock(&zl); 2429 zfs_dirent_unlock(sdl); 2430 zfs_dirent_unlock(tdl); 2431 VN_RELE(ZTOV(szp)); 2432 if (tzp) 2433 VN_RELE(ZTOV(tzp)); 2434 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2435 dmu_tx_wait(tx); 2436 dmu_tx_abort(tx); 2437 goto top; 2438 } 2439 dmu_tx_abort(tx); 2440 ZFS_EXIT(zfsvfs); 2441 return (error); 2442 } 2443 2444 if (tzp) /* Attempt to remove the existing target */ 2445 error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2446 2447 if (error == 0) { 2448 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2449 if (error == 0) { 2450 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2451 ASSERT(error == 0); 2452 zfs_log_rename(zilog, tx, TX_RENAME, sdzp, 2453 sdl->dl_name, tdzp, tdl->dl_name, szp); 2454 } 2455 } 2456 2457 dmu_tx_commit(tx); 2458 out: 2459 if (zl != NULL) 2460 zfs_rename_unlock(&zl); 2461 2462 zfs_dirent_unlock(sdl); 2463 zfs_dirent_unlock(tdl); 2464 2465 VN_RELE(ZTOV(szp)); 2466 if (tzp) 2467 VN_RELE(ZTOV(tzp)); 2468 2469 ZFS_EXIT(zfsvfs); 2470 return (error); 2471 } 2472 2473 /* 2474 * Insert the indicated symbolic reference entry into the directory. 2475 * 2476 * IN: dvp - Directory to contain new symbolic link. 2477 * link - Name for new symlink entry. 2478 * vap - Attributes of new entry. 2479 * target - Target path of new symlink. 2480 * cr - credentials of caller. 2481 * 2482 * RETURN: 0 if success 2483 * error code if failure 2484 * 2485 * Timestamps: 2486 * dvp - ctime|mtime updated 2487 */ 2488 static int 2489 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2490 { 2491 znode_t *zp, *dzp = VTOZ(dvp); 2492 zfs_dirlock_t *dl; 2493 dmu_tx_t *tx; 2494 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2495 zilog_t *zilog = zfsvfs->z_log; 2496 uint64_t zoid; 2497 int len = strlen(link); 2498 int error; 2499 2500 ASSERT(vap->va_type == VLNK); 2501 2502 ZFS_ENTER(zfsvfs); 2503 top: 2504 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2505 ZFS_EXIT(zfsvfs); 2506 return (error); 2507 } 2508 2509 if (len > MAXPATHLEN) { 2510 ZFS_EXIT(zfsvfs); 2511 return (ENAMETOOLONG); 2512 } 2513 2514 /* 2515 * Attempt to lock directory; fail if entry already exists. 2516 */ 2517 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2518 ZFS_EXIT(zfsvfs); 2519 return (error); 2520 } 2521 2522 tx = dmu_tx_create(zfsvfs->z_os); 2523 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2524 dmu_tx_hold_bonus(tx, dzp->z_id); 2525 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2526 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2527 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2528 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2529 if (error) { 2530 zfs_dirent_unlock(dl); 2531 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2532 dmu_tx_wait(tx); 2533 dmu_tx_abort(tx); 2534 goto top; 2535 } 2536 dmu_tx_abort(tx); 2537 ZFS_EXIT(zfsvfs); 2538 return (error); 2539 } 2540 2541 dmu_buf_will_dirty(dzp->z_dbuf, tx); 2542 2543 /* 2544 * Create a new object for the symlink. 2545 * Put the link content into bonus buffer if it will fit; 2546 * otherwise, store it just like any other file data. 2547 */ 2548 zoid = 0; 2549 if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2550 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2551 if (len != 0) 2552 bcopy(link, zp->z_phys + 1, len); 2553 } else { 2554 dmu_buf_t *dbp; 2555 2556 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2557 2558 /* 2559 * Nothing can access the znode yet so no locking needed 2560 * for growing the znode's blocksize. 2561 */ 2562 zfs_grow_blocksize(zp, len, tx); 2563 2564 VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2565 dmu_buf_will_dirty(dbp, tx); 2566 2567 ASSERT3U(len, <=, dbp->db_size); 2568 bcopy(link, dbp->db_data, len); 2569 dmu_buf_rele(dbp, FTAG); 2570 } 2571 zp->z_phys->zp_size = len; 2572 2573 /* 2574 * Insert the new object into the directory. 2575 */ 2576 (void) zfs_link_create(dl, zp, tx, ZNEW); 2577 out: 2578 if (error == 0) 2579 zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link); 2580 2581 dmu_tx_commit(tx); 2582 2583 zfs_dirent_unlock(dl); 2584 2585 VN_RELE(ZTOV(zp)); 2586 2587 ZFS_EXIT(zfsvfs); 2588 return (error); 2589 } 2590 2591 /* 2592 * Return, in the buffer contained in the provided uio structure, 2593 * the symbolic path referred to by vp. 2594 * 2595 * IN: vp - vnode of symbolic link. 2596 * uoip - structure to contain the link path. 2597 * cr - credentials of caller. 2598 * 2599 * OUT: uio - structure to contain the link path. 2600 * 2601 * RETURN: 0 if success 2602 * error code if failure 2603 * 2604 * Timestamps: 2605 * vp - atime updated 2606 */ 2607 /* ARGSUSED */ 2608 static int 2609 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2610 { 2611 znode_t *zp = VTOZ(vp); 2612 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2613 size_t bufsz; 2614 int error; 2615 2616 ZFS_ENTER(zfsvfs); 2617 2618 bufsz = (size_t)zp->z_phys->zp_size; 2619 if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2620 error = uiomove(zp->z_phys + 1, 2621 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2622 } else { 2623 dmu_buf_t *dbp; 2624 error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 2625 if (error) { 2626 ZFS_EXIT(zfsvfs); 2627 return (error); 2628 } 2629 error = uiomove(dbp->db_data, 2630 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2631 dmu_buf_rele(dbp, FTAG); 2632 } 2633 2634 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2635 ZFS_EXIT(zfsvfs); 2636 return (error); 2637 } 2638 2639 /* 2640 * Insert a new entry into directory tdvp referencing svp. 2641 * 2642 * IN: tdvp - Directory to contain new entry. 2643 * svp - vnode of new entry. 2644 * name - name of new entry. 2645 * cr - credentials of caller. 2646 * 2647 * RETURN: 0 if success 2648 * error code if failure 2649 * 2650 * Timestamps: 2651 * tdvp - ctime|mtime updated 2652 * svp - ctime updated 2653 */ 2654 /* ARGSUSED */ 2655 static int 2656 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2657 { 2658 znode_t *dzp = VTOZ(tdvp); 2659 znode_t *tzp, *szp; 2660 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2661 zilog_t *zilog = zfsvfs->z_log; 2662 zfs_dirlock_t *dl; 2663 dmu_tx_t *tx; 2664 vnode_t *realvp; 2665 int error; 2666 2667 ASSERT(tdvp->v_type == VDIR); 2668 2669 ZFS_ENTER(zfsvfs); 2670 2671 if (VOP_REALVP(svp, &realvp) == 0) 2672 svp = realvp; 2673 2674 if (svp->v_vfsp != tdvp->v_vfsp) { 2675 ZFS_EXIT(zfsvfs); 2676 return (EXDEV); 2677 } 2678 2679 szp = VTOZ(svp); 2680 top: 2681 /* 2682 * We do not support links between attributes and non-attributes 2683 * because of the potential security risk of creating links 2684 * into "normal" file space in order to circumvent restrictions 2685 * imposed in attribute space. 2686 */ 2687 if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2688 (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2689 ZFS_EXIT(zfsvfs); 2690 return (EINVAL); 2691 } 2692 2693 /* 2694 * POSIX dictates that we return EPERM here. 2695 * Better choices include ENOTSUP or EISDIR. 2696 */ 2697 if (svp->v_type == VDIR) { 2698 ZFS_EXIT(zfsvfs); 2699 return (EPERM); 2700 } 2701 2702 if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2703 secpolicy_basic_link(cr) != 0) { 2704 ZFS_EXIT(zfsvfs); 2705 return (EPERM); 2706 } 2707 2708 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2709 ZFS_EXIT(zfsvfs); 2710 return (error); 2711 } 2712 2713 /* 2714 * Attempt to lock directory; fail if entry already exists. 2715 */ 2716 if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2717 ZFS_EXIT(zfsvfs); 2718 return (error); 2719 } 2720 2721 tx = dmu_tx_create(zfsvfs->z_os); 2722 dmu_tx_hold_bonus(tx, szp->z_id); 2723 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2724 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2725 if (error) { 2726 zfs_dirent_unlock(dl); 2727 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2728 dmu_tx_wait(tx); 2729 dmu_tx_abort(tx); 2730 goto top; 2731 } 2732 dmu_tx_abort(tx); 2733 ZFS_EXIT(zfsvfs); 2734 return (error); 2735 } 2736 2737 error = zfs_link_create(dl, szp, tx, 0); 2738 2739 if (error == 0) 2740 zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2741 2742 dmu_tx_commit(tx); 2743 2744 zfs_dirent_unlock(dl); 2745 2746 ZFS_EXIT(zfsvfs); 2747 return (error); 2748 } 2749 2750 /* 2751 * zfs_null_putapage() is used when the file system has been force 2752 * unmounted. It just drops the pages. 2753 */ 2754 /* ARGSUSED */ 2755 static int 2756 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2757 size_t *lenp, int flags, cred_t *cr) 2758 { 2759 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2760 return (0); 2761 } 2762 2763 /* 2764 * Push a page out to disk, klustering if possible. 2765 * 2766 * IN: vp - file to push page to. 2767 * pp - page to push. 2768 * flags - additional flags. 2769 * cr - credentials of caller. 2770 * 2771 * OUT: offp - start of range pushed. 2772 * lenp - len of range pushed. 2773 * 2774 * RETURN: 0 if success 2775 * error code if failure 2776 * 2777 * NOTE: callers must have locked the page to be pushed. On 2778 * exit, the page (and all other pages in the kluster) must be 2779 * unlocked. 2780 */ 2781 /* ARGSUSED */ 2782 static int 2783 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2784 size_t *lenp, int flags, cred_t *cr) 2785 { 2786 znode_t *zp = VTOZ(vp); 2787 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2788 zilog_t *zilog = zfsvfs->z_log; 2789 dmu_tx_t *tx; 2790 rl_t *rl; 2791 u_offset_t off, koff; 2792 size_t len, klen; 2793 int err; 2794 2795 off = pp->p_offset; 2796 len = PAGESIZE; 2797 /* 2798 * If our blocksize is bigger than the page size, try to kluster 2799 * muiltiple pages so that we write a full block (thus avoiding 2800 * a read-modify-write). 2801 */ 2802 if (zp->z_blksz > PAGESIZE) { 2803 uint64_t filesz = zp->z_phys->zp_size; 2804 2805 if (!ISP2(zp->z_blksz)) { 2806 /* Only one block in the file. */ 2807 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 2808 koff = 0; 2809 } else { 2810 klen = zp->z_blksz; 2811 koff = P2ALIGN(off, (u_offset_t)klen); 2812 } 2813 ASSERT(koff <= filesz); 2814 if (koff + klen > filesz) 2815 klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 2816 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 2817 } 2818 ASSERT3U(btop(len), ==, btopr(len)); 2819 top: 2820 rl = zfs_range_lock(zp, off, len, RL_WRITER); 2821 /* 2822 * Can't push pages past end-of-file. 2823 */ 2824 if (off >= zp->z_phys->zp_size) { 2825 /* discard all pages */ 2826 flags |= B_INVAL; 2827 err = 0; 2828 goto out; 2829 } else if (off + len > zp->z_phys->zp_size) { 2830 int npages = btopr(zp->z_phys->zp_size - off); 2831 page_t *trunc; 2832 2833 page_list_break(&pp, &trunc, npages); 2834 /* discard pages past end of file */ 2835 if (trunc) 2836 pvn_write_done(trunc, B_INVAL | flags); 2837 len = zp->z_phys->zp_size - off; 2838 } 2839 2840 tx = dmu_tx_create(zfsvfs->z_os); 2841 dmu_tx_hold_write(tx, zp->z_id, off, len); 2842 dmu_tx_hold_bonus(tx, zp->z_id); 2843 err = dmu_tx_assign(tx, zfsvfs->z_assign); 2844 if (err != 0) { 2845 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2846 zfs_range_unlock(rl); 2847 dmu_tx_wait(tx); 2848 dmu_tx_abort(tx); 2849 err = 0; 2850 goto top; 2851 } 2852 dmu_tx_abort(tx); 2853 goto out; 2854 } 2855 2856 if (zp->z_blksz <= PAGESIZE) { 2857 caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1); 2858 ASSERT3U(len, <=, PAGESIZE); 2859 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 2860 ppmapout(va); 2861 } else { 2862 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 2863 } 2864 2865 if (err == 0) { 2866 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 2867 zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0); 2868 dmu_tx_commit(tx); 2869 } 2870 2871 out: 2872 zfs_range_unlock(rl); 2873 pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags); 2874 if (offp) 2875 *offp = off; 2876 if (lenp) 2877 *lenp = len; 2878 2879 return (err); 2880 } 2881 2882 /* 2883 * Copy the portion of the file indicated from pages into the file. 2884 * The pages are stored in a page list attached to the files vnode. 2885 * 2886 * IN: vp - vnode of file to push page data to. 2887 * off - position in file to put data. 2888 * len - amount of data to write. 2889 * flags - flags to control the operation. 2890 * cr - credentials of caller. 2891 * 2892 * RETURN: 0 if success 2893 * error code if failure 2894 * 2895 * Timestamps: 2896 * vp - ctime|mtime updated 2897 */ 2898 static int 2899 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2900 { 2901 znode_t *zp = VTOZ(vp); 2902 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2903 page_t *pp; 2904 size_t io_len; 2905 u_offset_t io_off; 2906 uint64_t filesz; 2907 int error = 0; 2908 2909 ZFS_ENTER(zfsvfs); 2910 2911 ASSERT(zp->z_dbuf_held && zp->z_phys); 2912 2913 if (len == 0) { 2914 /* 2915 * Search the entire vp list for pages >= off. 2916 */ 2917 error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2918 flags, cr); 2919 goto out; 2920 } 2921 2922 filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 2923 if (off > filesz) { 2924 /* past end of file */ 2925 ZFS_EXIT(zfsvfs); 2926 return (0); 2927 } 2928 2929 len = MIN(len, filesz - off); 2930 2931 for (io_off = off; io_off < off + len; io_off += io_len) { 2932 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 2933 pp = page_lookup(vp, io_off, 2934 (flags & (B_INVAL | B_FREE)) ? 2935 SE_EXCL : SE_SHARED); 2936 } else { 2937 pp = page_lookup_nowait(vp, io_off, 2938 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2939 } 2940 2941 if (pp != NULL && pvn_getdirty(pp, flags)) { 2942 int err; 2943 2944 /* 2945 * Found a dirty page to push 2946 */ 2947 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 2948 if (err) 2949 error = err; 2950 } else { 2951 io_len = PAGESIZE; 2952 } 2953 } 2954 out: 2955 if ((flags & B_ASYNC) == 0) 2956 zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 2957 ZFS_EXIT(zfsvfs); 2958 return (error); 2959 } 2960 2961 void 2962 zfs_inactive(vnode_t *vp, cred_t *cr) 2963 { 2964 znode_t *zp = VTOZ(vp); 2965 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2966 int error; 2967 2968 rw_enter(&zfsvfs->z_um_lock, RW_READER); 2969 if (zfsvfs->z_unmounted2) { 2970 ASSERT(zp->z_dbuf_held == 0); 2971 2972 if (vn_has_cached_data(vp)) { 2973 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 2974 B_INVAL, cr); 2975 } 2976 2977 mutex_enter(&zp->z_lock); 2978 vp->v_count = 0; /* count arrives as 1 */ 2979 if (zp->z_dbuf == NULL) { 2980 mutex_exit(&zp->z_lock); 2981 zfs_znode_free(zp); 2982 } else { 2983 mutex_exit(&zp->z_lock); 2984 } 2985 rw_exit(&zfsvfs->z_um_lock); 2986 VFS_RELE(zfsvfs->z_vfs); 2987 return; 2988 } 2989 2990 /* 2991 * Attempt to push any data in the page cache. If this fails 2992 * we will get kicked out later in zfs_zinactive(). 2993 */ 2994 if (vn_has_cached_data(vp)) { 2995 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 2996 cr); 2997 } 2998 2999 if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3000 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3001 3002 dmu_tx_hold_bonus(tx, zp->z_id); 3003 error = dmu_tx_assign(tx, TXG_WAIT); 3004 if (error) { 3005 dmu_tx_abort(tx); 3006 } else { 3007 dmu_buf_will_dirty(zp->z_dbuf, tx); 3008 mutex_enter(&zp->z_lock); 3009 zp->z_atime_dirty = 0; 3010 mutex_exit(&zp->z_lock); 3011 dmu_tx_commit(tx); 3012 } 3013 } 3014 3015 zfs_zinactive(zp); 3016 rw_exit(&zfsvfs->z_um_lock); 3017 } 3018 3019 /* 3020 * Bounds-check the seek operation. 3021 * 3022 * IN: vp - vnode seeking within 3023 * ooff - old file offset 3024 * noffp - pointer to new file offset 3025 * 3026 * RETURN: 0 if success 3027 * EINVAL if new offset invalid 3028 */ 3029 /* ARGSUSED */ 3030 static int 3031 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 3032 { 3033 if (vp->v_type == VDIR) 3034 return (0); 3035 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3036 } 3037 3038 /* 3039 * Pre-filter the generic locking function to trap attempts to place 3040 * a mandatory lock on a memory mapped file. 3041 */ 3042 static int 3043 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 3044 flk_callback_t *flk_cbp, cred_t *cr) 3045 { 3046 znode_t *zp = VTOZ(vp); 3047 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3048 int error; 3049 3050 ZFS_ENTER(zfsvfs); 3051 3052 /* 3053 * We are following the UFS semantics with respect to mapcnt 3054 * here: If we see that the file is mapped already, then we will 3055 * return an error, but we don't worry about races between this 3056 * function and zfs_map(). 3057 */ 3058 if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3059 ZFS_EXIT(zfsvfs); 3060 return (EAGAIN); 3061 } 3062 error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 3063 ZFS_EXIT(zfsvfs); 3064 return (error); 3065 } 3066 3067 /* 3068 * If we can't find a page in the cache, we will create a new page 3069 * and fill it with file data. For efficiency, we may try to fill 3070 * multiple pages at once (klustering). 3071 */ 3072 static int 3073 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3074 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3075 { 3076 znode_t *zp = VTOZ(vp); 3077 page_t *pp, *cur_pp; 3078 objset_t *os = zp->z_zfsvfs->z_os; 3079 caddr_t va; 3080 u_offset_t io_off, total; 3081 uint64_t oid = zp->z_id; 3082 size_t io_len; 3083 uint64_t filesz; 3084 int err; 3085 3086 /* 3087 * If we are only asking for a single page don't bother klustering. 3088 */ 3089 filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 3090 if (off >= filesz) 3091 return (EFAULT); 3092 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3093 io_off = off; 3094 io_len = PAGESIZE; 3095 pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3096 } else { 3097 /* 3098 * Try to fill a kluster of pages (a blocks worth). 3099 */ 3100 size_t klen; 3101 u_offset_t koff; 3102 3103 if (!ISP2(zp->z_blksz)) { 3104 /* Only one block in the file. */ 3105 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3106 koff = 0; 3107 } else { 3108 /* 3109 * It would be ideal to align our offset to the 3110 * blocksize but doing so has resulted in some 3111 * strange application crashes. For now, we 3112 * leave the offset as is and only adjust the 3113 * length if we are off the end of the file. 3114 */ 3115 koff = off; 3116 klen = plsz; 3117 } 3118 ASSERT(koff <= filesz); 3119 if (koff + klen > filesz) 3120 klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 3121 ASSERT3U(off, >=, koff); 3122 ASSERT3U(off, <, koff + klen); 3123 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 3124 &io_len, koff, klen, 0); 3125 } 3126 if (pp == NULL) { 3127 /* 3128 * Some other thread entered the page before us. 3129 * Return to zfs_getpage to retry the lookup. 3130 */ 3131 *pl = NULL; 3132 return (0); 3133 } 3134 3135 /* 3136 * Fill the pages in the kluster. 3137 */ 3138 cur_pp = pp; 3139 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 3140 ASSERT3U(io_off, ==, cur_pp->p_offset); 3141 va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 3142 err = dmu_read(os, oid, io_off, PAGESIZE, va); 3143 ppmapout(va); 3144 if (err) { 3145 /* On error, toss the entire kluster */ 3146 pvn_read_done(pp, B_ERROR); 3147 return (err); 3148 } 3149 cur_pp = cur_pp->p_next; 3150 } 3151 out: 3152 /* 3153 * Fill in the page list array from the kluster. If 3154 * there are too many pages in the kluster, return 3155 * as many pages as possible starting from the desired 3156 * offset `off'. 3157 * NOTE: the page list will always be null terminated. 3158 */ 3159 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3160 3161 return (0); 3162 } 3163 3164 /* 3165 * Return pointers to the pages for the file region [off, off + len] 3166 * in the pl array. If plsz is greater than len, this function may 3167 * also return page pointers from before or after the specified 3168 * region (i.e. some region [off', off' + plsz]). These additional 3169 * pages are only returned if they are already in the cache, or were 3170 * created as part of a klustered read. 3171 * 3172 * IN: vp - vnode of file to get data from. 3173 * off - position in file to get data from. 3174 * len - amount of data to retrieve. 3175 * plsz - length of provided page list. 3176 * seg - segment to obtain pages for. 3177 * addr - virtual address of fault. 3178 * rw - mode of created pages. 3179 * cr - credentials of caller. 3180 * 3181 * OUT: protp - protection mode of created pages. 3182 * pl - list of pages created. 3183 * 3184 * RETURN: 0 if success 3185 * error code if failure 3186 * 3187 * Timestamps: 3188 * vp - atime updated 3189 */ 3190 /* ARGSUSED */ 3191 static int 3192 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3193 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3194 enum seg_rw rw, cred_t *cr) 3195 { 3196 znode_t *zp = VTOZ(vp); 3197 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3198 page_t *pp, **pl0 = pl; 3199 int need_unlock = 0, err = 0; 3200 offset_t orig_off; 3201 3202 ZFS_ENTER(zfsvfs); 3203 3204 if (protp) 3205 *protp = PROT_ALL; 3206 3207 ASSERT(zp->z_dbuf_held && zp->z_phys); 3208 3209 /* no faultahead (for now) */ 3210 if (pl == NULL) { 3211 ZFS_EXIT(zfsvfs); 3212 return (0); 3213 } 3214 3215 /* can't fault past EOF */ 3216 if (off >= zp->z_phys->zp_size) { 3217 ZFS_EXIT(zfsvfs); 3218 return (EFAULT); 3219 } 3220 orig_off = off; 3221 3222 /* 3223 * If we already own the lock, then we must be page faulting 3224 * in the middle of a write to this file (i.e., we are writing 3225 * to this file using data from a mapped region of the file). 3226 */ 3227 if (rw_owner(&zp->z_map_lock) != curthread) { 3228 rw_enter(&zp->z_map_lock, RW_WRITER); 3229 need_unlock = TRUE; 3230 } 3231 3232 /* 3233 * Loop through the requested range [off, off + len] looking 3234 * for pages. If we don't find a page, we will need to create 3235 * a new page and fill it with data from the file. 3236 */ 3237 while (len > 0) { 3238 if (plsz < PAGESIZE) 3239 break; 3240 if (pp = page_lookup(vp, off, SE_SHARED)) { 3241 *pl++ = pp; 3242 off += PAGESIZE; 3243 addr += PAGESIZE; 3244 len -= PAGESIZE; 3245 plsz -= PAGESIZE; 3246 } else { 3247 err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 3248 if (err) 3249 goto out; 3250 /* 3251 * klustering may have changed our region 3252 * to be block aligned. 3253 */ 3254 if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3255 int delta = off - pp->p_offset; 3256 len += delta; 3257 off -= delta; 3258 addr -= delta; 3259 } 3260 while (*pl) { 3261 pl++; 3262 off += PAGESIZE; 3263 addr += PAGESIZE; 3264 plsz -= PAGESIZE; 3265 if (len > PAGESIZE) 3266 len -= PAGESIZE; 3267 else 3268 len = 0; 3269 } 3270 } 3271 } 3272 3273 /* 3274 * Fill out the page array with any pages already in the cache. 3275 */ 3276 while (plsz > 0) { 3277 pp = page_lookup_nowait(vp, off, SE_SHARED); 3278 if (pp == NULL) 3279 break; 3280 *pl++ = pp; 3281 off += PAGESIZE; 3282 plsz -= PAGESIZE; 3283 } 3284 3285 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3286 out: 3287 /* 3288 * We can't grab the range lock for the page as reader which would 3289 * stop truncation as this leads to deadlock. So we need to recheck 3290 * the file size. 3291 */ 3292 if (orig_off >= zp->z_phys->zp_size) 3293 err = EFAULT; 3294 if (err) { 3295 /* 3296 * Release any pages we have previously locked. 3297 */ 3298 while (pl > pl0) 3299 page_unlock(*--pl); 3300 } 3301 3302 *pl = NULL; 3303 3304 if (need_unlock) 3305 rw_exit(&zp->z_map_lock); 3306 3307 ZFS_EXIT(zfsvfs); 3308 return (err); 3309 } 3310 3311 /* 3312 * Request a memory map for a section of a file. This code interacts 3313 * with common code and the VM system as follows: 3314 * 3315 * common code calls mmap(), which ends up in smmap_common() 3316 * 3317 * this calls VOP_MAP(), which takes you into (say) zfs 3318 * 3319 * zfs_map() calls as_map(), passing segvn_create() as the callback 3320 * 3321 * segvn_create() creates the new segment and calls VOP_ADDMAP() 3322 * 3323 * zfs_addmap() updates z_mapcnt 3324 */ 3325 static int 3326 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3327 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3328 { 3329 znode_t *zp = VTOZ(vp); 3330 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3331 segvn_crargs_t vn_a; 3332 int error; 3333 3334 ZFS_ENTER(zfsvfs); 3335 3336 if (vp->v_flag & VNOMAP) { 3337 ZFS_EXIT(zfsvfs); 3338 return (ENOSYS); 3339 } 3340 3341 if (off < 0 || len > MAXOFFSET_T - off) { 3342 ZFS_EXIT(zfsvfs); 3343 return (ENXIO); 3344 } 3345 3346 if (vp->v_type != VREG) { 3347 ZFS_EXIT(zfsvfs); 3348 return (ENODEV); 3349 } 3350 3351 /* 3352 * If file is locked, disallow mapping. 3353 */ 3354 if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 3355 ZFS_EXIT(zfsvfs); 3356 return (EAGAIN); 3357 } 3358 3359 as_rangelock(as); 3360 if ((flags & MAP_FIXED) == 0) { 3361 map_addr(addrp, len, off, 1, flags); 3362 if (*addrp == NULL) { 3363 as_rangeunlock(as); 3364 ZFS_EXIT(zfsvfs); 3365 return (ENOMEM); 3366 } 3367 } else { 3368 /* 3369 * User specified address - blow away any previous mappings 3370 */ 3371 (void) as_unmap(as, *addrp, len); 3372 } 3373 3374 vn_a.vp = vp; 3375 vn_a.offset = (u_offset_t)off; 3376 vn_a.type = flags & MAP_TYPE; 3377 vn_a.prot = prot; 3378 vn_a.maxprot = maxprot; 3379 vn_a.cred = cr; 3380 vn_a.amp = NULL; 3381 vn_a.flags = flags & ~MAP_TYPE; 3382 vn_a.szc = 0; 3383 vn_a.lgrp_mem_policy_flags = 0; 3384 3385 error = as_map(as, *addrp, len, segvn_create, &vn_a); 3386 3387 as_rangeunlock(as); 3388 ZFS_EXIT(zfsvfs); 3389 return (error); 3390 } 3391 3392 /* ARGSUSED */ 3393 static int 3394 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3395 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3396 { 3397 uint64_t pages = btopr(len); 3398 3399 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3400 return (0); 3401 } 3402 3403 /* 3404 * The reason we push dirty pages as part of zfs_delmap() is so that we get a 3405 * more accurate mtime for the associated file. Since we don't have a way of 3406 * detecting when the data was actually modified, we have to resort to 3407 * heuristics. If an explicit msync() is done, then we mark the mtime when the 3408 * last page is pushed. The problem occurs when the msync() call is omitted, 3409 * which by far the most common case: 3410 * 3411 * open() 3412 * mmap() 3413 * <modify memory> 3414 * munmap() 3415 * close() 3416 * <time lapse> 3417 * putpage() via fsflush 3418 * 3419 * If we wait until fsflush to come along, we can have a modification time that 3420 * is some arbitrary point in the future. In order to prevent this in the 3421 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 3422 * torn down. 3423 */ 3424 /* ARGSUSED */ 3425 static int 3426 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3427 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3428 { 3429 uint64_t pages = btopr(len); 3430 3431 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 3432 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 3433 3434 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 3435 vn_has_cached_data(vp)) 3436 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 3437 3438 return (0); 3439 } 3440 3441 /* 3442 * Free or allocate space in a file. Currently, this function only 3443 * supports the `F_FREESP' command. However, this command is somewhat 3444 * misnamed, as its functionality includes the ability to allocate as 3445 * well as free space. 3446 * 3447 * IN: vp - vnode of file to free data in. 3448 * cmd - action to take (only F_FREESP supported). 3449 * bfp - section of file to free/alloc. 3450 * flag - current file open mode flags. 3451 * offset - current file offset. 3452 * cr - credentials of caller [UNUSED]. 3453 * 3454 * RETURN: 0 if success 3455 * error code if failure 3456 * 3457 * Timestamps: 3458 * vp - ctime|mtime updated 3459 */ 3460 /* ARGSUSED */ 3461 static int 3462 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3463 offset_t offset, cred_t *cr, caller_context_t *ct) 3464 { 3465 znode_t *zp = VTOZ(vp); 3466 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3467 uint64_t off, len; 3468 int error; 3469 3470 ZFS_ENTER(zfsvfs); 3471 3472 top: 3473 if (cmd != F_FREESP) { 3474 ZFS_EXIT(zfsvfs); 3475 return (EINVAL); 3476 } 3477 3478 if (error = convoff(vp, bfp, 0, offset)) { 3479 ZFS_EXIT(zfsvfs); 3480 return (error); 3481 } 3482 3483 if (bfp->l_len < 0) { 3484 ZFS_EXIT(zfsvfs); 3485 return (EINVAL); 3486 } 3487 3488 off = bfp->l_start; 3489 len = bfp->l_len; /* 0 means from off to end of file */ 3490 3491 do { 3492 error = zfs_freesp(zp, off, len, flag, TRUE); 3493 /* NB: we already did dmu_tx_wait() if necessary */ 3494 } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3495 3496 ZFS_EXIT(zfsvfs); 3497 return (error); 3498 } 3499 3500 static int 3501 zfs_fid(vnode_t *vp, fid_t *fidp) 3502 { 3503 znode_t *zp = VTOZ(vp); 3504 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3505 uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3506 uint64_t object = zp->z_id; 3507 zfid_short_t *zfid; 3508 int size, i; 3509 3510 ZFS_ENTER(zfsvfs); 3511 3512 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3513 if (fidp->fid_len < size) { 3514 fidp->fid_len = size; 3515 ZFS_EXIT(zfsvfs); 3516 return (ENOSPC); 3517 } 3518 3519 zfid = (zfid_short_t *)fidp; 3520 3521 zfid->zf_len = size; 3522 3523 for (i = 0; i < sizeof (zfid->zf_object); i++) 3524 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3525 3526 /* Must have a non-zero generation number to distinguish from .zfs */ 3527 if (gen == 0) 3528 gen = 1; 3529 for (i = 0; i < sizeof (zfid->zf_gen); i++) 3530 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3531 3532 if (size == LONG_FID_LEN) { 3533 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3534 zfid_long_t *zlfid; 3535 3536 zlfid = (zfid_long_t *)fidp; 3537 3538 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3539 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3540 3541 /* XXX - this should be the generation number for the objset */ 3542 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3543 zlfid->zf_setgen[i] = 0; 3544 } 3545 3546 ZFS_EXIT(zfsvfs); 3547 return (0); 3548 } 3549 3550 static int 3551 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3552 { 3553 znode_t *zp, *xzp; 3554 zfsvfs_t *zfsvfs; 3555 zfs_dirlock_t *dl; 3556 int error; 3557 3558 switch (cmd) { 3559 case _PC_LINK_MAX: 3560 *valp = ULONG_MAX; 3561 return (0); 3562 3563 case _PC_FILESIZEBITS: 3564 *valp = 64; 3565 return (0); 3566 3567 case _PC_XATTR_EXISTS: 3568 zp = VTOZ(vp); 3569 zfsvfs = zp->z_zfsvfs; 3570 ZFS_ENTER(zfsvfs); 3571 *valp = 0; 3572 error = zfs_dirent_lock(&dl, zp, "", &xzp, 3573 ZXATTR | ZEXISTS | ZSHARED); 3574 if (error == 0) { 3575 zfs_dirent_unlock(dl); 3576 if (!zfs_dirempty(xzp)) 3577 *valp = 1; 3578 VN_RELE(ZTOV(xzp)); 3579 } else if (error == ENOENT) { 3580 /* 3581 * If there aren't extended attributes, it's the 3582 * same as having zero of them. 3583 */ 3584 error = 0; 3585 } 3586 ZFS_EXIT(zfsvfs); 3587 return (error); 3588 3589 case _PC_ACL_ENABLED: 3590 *valp = _ACL_ACE_ENABLED; 3591 return (0); 3592 3593 case _PC_MIN_HOLE_SIZE: 3594 *valp = (ulong_t)SPA_MINBLOCKSIZE; 3595 return (0); 3596 3597 default: 3598 return (fs_pathconf(vp, cmd, valp, cr)); 3599 } 3600 } 3601 3602 /*ARGSUSED*/ 3603 static int 3604 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3605 { 3606 znode_t *zp = VTOZ(vp); 3607 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3608 int error; 3609 3610 ZFS_ENTER(zfsvfs); 3611 error = zfs_getacl(zp, vsecp, cr); 3612 ZFS_EXIT(zfsvfs); 3613 3614 return (error); 3615 } 3616 3617 /*ARGSUSED*/ 3618 static int 3619 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3620 { 3621 znode_t *zp = VTOZ(vp); 3622 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3623 int error; 3624 3625 ZFS_ENTER(zfsvfs); 3626 error = zfs_setacl(zp, vsecp, cr); 3627 ZFS_EXIT(zfsvfs); 3628 return (error); 3629 } 3630 3631 /* 3632 * Predeclare these here so that the compiler assumes that 3633 * this is an "old style" function declaration that does 3634 * not include arguments => we won't get type mismatch errors 3635 * in the initializations that follow. 3636 */ 3637 static int zfs_inval(); 3638 static int zfs_isdir(); 3639 3640 static int 3641 zfs_inval() 3642 { 3643 return (EINVAL); 3644 } 3645 3646 static int 3647 zfs_isdir() 3648 { 3649 return (EISDIR); 3650 } 3651 /* 3652 * Directory vnode operations template 3653 */ 3654 vnodeops_t *zfs_dvnodeops; 3655 const fs_operation_def_t zfs_dvnodeops_template[] = { 3656 VOPNAME_OPEN, zfs_open, 3657 VOPNAME_CLOSE, zfs_close, 3658 VOPNAME_READ, zfs_isdir, 3659 VOPNAME_WRITE, zfs_isdir, 3660 VOPNAME_IOCTL, zfs_ioctl, 3661 VOPNAME_GETATTR, zfs_getattr, 3662 VOPNAME_SETATTR, zfs_setattr, 3663 VOPNAME_ACCESS, zfs_access, 3664 VOPNAME_LOOKUP, zfs_lookup, 3665 VOPNAME_CREATE, zfs_create, 3666 VOPNAME_REMOVE, zfs_remove, 3667 VOPNAME_LINK, zfs_link, 3668 VOPNAME_RENAME, zfs_rename, 3669 VOPNAME_MKDIR, zfs_mkdir, 3670 VOPNAME_RMDIR, zfs_rmdir, 3671 VOPNAME_READDIR, zfs_readdir, 3672 VOPNAME_SYMLINK, zfs_symlink, 3673 VOPNAME_FSYNC, zfs_fsync, 3674 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3675 VOPNAME_FID, zfs_fid, 3676 VOPNAME_SEEK, zfs_seek, 3677 VOPNAME_PATHCONF, zfs_pathconf, 3678 VOPNAME_GETSECATTR, zfs_getsecattr, 3679 VOPNAME_SETSECATTR, zfs_setsecattr, 3680 NULL, NULL 3681 }; 3682 3683 /* 3684 * Regular file vnode operations template 3685 */ 3686 vnodeops_t *zfs_fvnodeops; 3687 const fs_operation_def_t zfs_fvnodeops_template[] = { 3688 VOPNAME_OPEN, zfs_open, 3689 VOPNAME_CLOSE, zfs_close, 3690 VOPNAME_READ, zfs_read, 3691 VOPNAME_WRITE, zfs_write, 3692 VOPNAME_IOCTL, zfs_ioctl, 3693 VOPNAME_GETATTR, zfs_getattr, 3694 VOPNAME_SETATTR, zfs_setattr, 3695 VOPNAME_ACCESS, zfs_access, 3696 VOPNAME_LOOKUP, zfs_lookup, 3697 VOPNAME_RENAME, zfs_rename, 3698 VOPNAME_FSYNC, zfs_fsync, 3699 VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive, 3700 VOPNAME_FID, zfs_fid, 3701 VOPNAME_SEEK, zfs_seek, 3702 VOPNAME_FRLOCK, zfs_frlock, 3703 VOPNAME_SPACE, zfs_space, 3704 VOPNAME_GETPAGE, zfs_getpage, 3705 VOPNAME_PUTPAGE, zfs_putpage, 3706 VOPNAME_MAP, (fs_generic_func_p) zfs_map, 3707 VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap, 3708 VOPNAME_DELMAP, zfs_delmap, 3709 VOPNAME_PATHCONF, zfs_pathconf, 3710 VOPNAME_GETSECATTR, zfs_getsecattr, 3711 VOPNAME_SETSECATTR, zfs_setsecattr, 3712 VOPNAME_VNEVENT, fs_vnevent_support, 3713 NULL, NULL 3714 }; 3715 3716 /* 3717 * Symbolic link vnode operations template 3718 */ 3719 vnodeops_t *zfs_symvnodeops; 3720 const fs_operation_def_t zfs_symvnodeops_template[] = { 3721 VOPNAME_GETATTR, zfs_getattr, 3722 VOPNAME_SETATTR, zfs_setattr, 3723 VOPNAME_ACCESS, zfs_access, 3724 VOPNAME_RENAME, zfs_rename, 3725 VOPNAME_READLINK, zfs_readlink, 3726 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3727 VOPNAME_FID, zfs_fid, 3728 VOPNAME_PATHCONF, zfs_pathconf, 3729 VOPNAME_VNEVENT, fs_vnevent_support, 3730 NULL, NULL 3731 }; 3732 3733 /* 3734 * Extended attribute directory vnode operations template 3735 * This template is identical to the directory vnodes 3736 * operation template except for restricted operations: 3737 * VOP_MKDIR() 3738 * VOP_SYMLINK() 3739 * Note that there are other restrictions embedded in: 3740 * zfs_create() - restrict type to VREG 3741 * zfs_link() - no links into/out of attribute space 3742 * zfs_rename() - no moves into/out of attribute space 3743 */ 3744 vnodeops_t *zfs_xdvnodeops; 3745 const fs_operation_def_t zfs_xdvnodeops_template[] = { 3746 VOPNAME_OPEN, zfs_open, 3747 VOPNAME_CLOSE, zfs_close, 3748 VOPNAME_IOCTL, zfs_ioctl, 3749 VOPNAME_GETATTR, zfs_getattr, 3750 VOPNAME_SETATTR, zfs_setattr, 3751 VOPNAME_ACCESS, zfs_access, 3752 VOPNAME_LOOKUP, zfs_lookup, 3753 VOPNAME_CREATE, zfs_create, 3754 VOPNAME_REMOVE, zfs_remove, 3755 VOPNAME_LINK, zfs_link, 3756 VOPNAME_RENAME, zfs_rename, 3757 VOPNAME_MKDIR, zfs_inval, 3758 VOPNAME_RMDIR, zfs_rmdir, 3759 VOPNAME_READDIR, zfs_readdir, 3760 VOPNAME_SYMLINK, zfs_inval, 3761 VOPNAME_FSYNC, zfs_fsync, 3762 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3763 VOPNAME_FID, zfs_fid, 3764 VOPNAME_SEEK, zfs_seek, 3765 VOPNAME_PATHCONF, zfs_pathconf, 3766 VOPNAME_GETSECATTR, zfs_getsecattr, 3767 VOPNAME_SETSECATTR, zfs_setsecattr, 3768 VOPNAME_VNEVENT, fs_vnevent_support, 3769 NULL, NULL 3770 }; 3771 3772 /* 3773 * Error vnode operations template 3774 */ 3775 vnodeops_t *zfs_evnodeops; 3776 const fs_operation_def_t zfs_evnodeops_template[] = { 3777 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3778 VOPNAME_PATHCONF, zfs_pathconf, 3779 NULL, NULL 3780 }; 3781