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