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