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