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