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