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