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