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