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 * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 * Copyright 2015 Joyent, Inc. 27 */ 28 29 /* Portions Copyright 2007 Jeremy Teo */ 30 /* Portions Copyright 2010 Robert Milkowski */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <sys/systm.h> 36 #include <sys/sysmacros.h> 37 #include <sys/resource.h> 38 #include <sys/vfs.h> 39 #include <sys/vfs_opreg.h> 40 #include <sys/vnode.h> 41 #include <sys/file.h> 42 #include <sys/stat.h> 43 #include <sys/kmem.h> 44 #include <sys/taskq.h> 45 #include <sys/uio.h> 46 #include <sys/vmsystm.h> 47 #include <sys/atomic.h> 48 #include <sys/vm.h> 49 #include <vm/seg_vn.h> 50 #include <vm/pvn.h> 51 #include <vm/as.h> 52 #include <vm/kpm.h> 53 #include <vm/seg_kpm.h> 54 #include <sys/mman.h> 55 #include <sys/pathname.h> 56 #include <sys/cmn_err.h> 57 #include <sys/errno.h> 58 #include <sys/unistd.h> 59 #include <sys/zfs_dir.h> 60 #include <sys/zfs_acl.h> 61 #include <sys/zfs_ioctl.h> 62 #include <sys/fs/zfs.h> 63 #include <sys/dmu.h> 64 #include <sys/dmu_objset.h> 65 #include <sys/spa.h> 66 #include <sys/txg.h> 67 #include <sys/dbuf.h> 68 #include <sys/zap.h> 69 #include <sys/sa.h> 70 #include <sys/dirent.h> 71 #include <sys/policy.h> 72 #include <sys/sunddi.h> 73 #include <sys/filio.h> 74 #include <sys/sid.h> 75 #include "fs/fs_subr.h" 76 #include <sys/zfs_ctldir.h> 77 #include <sys/zfs_fuid.h> 78 #include <sys/zfs_sa.h> 79 #include <sys/dnlc.h> 80 #include <sys/zfs_rlock.h> 81 #include <sys/extdirent.h> 82 #include <sys/kidmap.h> 83 #include <sys/cred.h> 84 #include <sys/attr.h> 85 86 /* 87 * Programming rules. 88 * 89 * Each vnode op performs some logical unit of work. To do this, the ZPL must 90 * properly lock its in-core state, create a DMU transaction, do the work, 91 * record this work in the intent log (ZIL), commit the DMU transaction, 92 * and wait for the intent log to commit if it is a synchronous operation. 93 * Moreover, the vnode ops must work in both normal and log replay context. 94 * The ordering of events is important to avoid deadlocks and references 95 * to freed memory. The example below illustrates the following Big Rules: 96 * 97 * (1) A check must be made in each zfs thread for a mounted file system. 98 * This is done avoiding races using ZFS_ENTER(zfsvfs). 99 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 100 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 101 * can return EIO from the calling function. 102 * 103 * (2) VN_RELE() should always be the last thing except for zil_commit() 104 * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 105 * First, if it's the last reference, the vnode/znode 106 * can be freed, so the zp may point to freed memory. Second, the last 107 * reference will call zfs_zinactive(), which may induce a lot of work -- 108 * pushing cached pages (which acquires range locks) and syncing out 109 * cached atime changes. Third, zfs_zinactive() may require a new tx, 110 * which could deadlock the system if you were already holding one. 111 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC(). 112 * 113 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 114 * as they can span dmu_tx_assign() calls. 115 * 116 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to 117 * dmu_tx_assign(). This is critical because we don't want to block 118 * while holding locks. 119 * 120 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This 121 * reduces lock contention and CPU usage when we must wait (note that if 122 * throughput is constrained by the storage, nearly every transaction 123 * must wait). 124 * 125 * Note, in particular, that if a lock is sometimes acquired before 126 * the tx assigns, and sometimes after (e.g. z_lock), then failing 127 * to use a non-blocking assign can deadlock the system. The scenario: 128 * 129 * Thread A has grabbed a lock before calling dmu_tx_assign(). 130 * Thread B is in an already-assigned tx, and blocks for this lock. 131 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 132 * forever, because the previous txg can't quiesce until B's tx commits. 133 * 134 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 135 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent 136 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT, 137 * to indicate that this operation has already called dmu_tx_wait(). 138 * This will ensure that we don't retry forever, waiting a short bit 139 * each time. 140 * 141 * (5) If the operation succeeded, generate the intent log entry for it 142 * before dropping locks. This ensures that the ordering of events 143 * in the intent log matches the order in which they actually occurred. 144 * During ZIL replay the zfs_log_* functions will update the sequence 145 * number to indicate the zil transaction has replayed. 146 * 147 * (6) At the end of each vnode op, the DMU tx must always commit, 148 * regardless of whether there were any errors. 149 * 150 * (7) After dropping all locks, invoke zil_commit(zilog, foid) 151 * to ensure that synchronous semantics are provided when necessary. 152 * 153 * In general, this is how things should be ordered in each vnode op: 154 * 155 * ZFS_ENTER(zfsvfs); // exit if unmounted 156 * top: 157 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 158 * rw_enter(...); // grab any other locks you need 159 * tx = dmu_tx_create(...); // get DMU tx 160 * dmu_tx_hold_*(); // hold each object you might modify 161 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 162 * if (error) { 163 * rw_exit(...); // drop locks 164 * zfs_dirent_unlock(dl); // unlock directory entry 165 * VN_RELE(...); // release held vnodes 166 * if (error == ERESTART) { 167 * waited = B_TRUE; 168 * dmu_tx_wait(tx); 169 * dmu_tx_abort(tx); 170 * goto top; 171 * } 172 * dmu_tx_abort(tx); // abort DMU tx 173 * ZFS_EXIT(zfsvfs); // finished in zfs 174 * return (error); // really out of space 175 * } 176 * error = do_real_work(); // do whatever this VOP does 177 * if (error == 0) 178 * zfs_log_*(...); // on success, make ZIL entry 179 * dmu_tx_commit(tx); // commit DMU tx -- error or not 180 * rw_exit(...); // drop locks 181 * zfs_dirent_unlock(dl); // unlock directory entry 182 * VN_RELE(...); // release held vnodes 183 * zil_commit(zilog, foid); // synchronous when necessary 184 * ZFS_EXIT(zfsvfs); // finished in zfs 185 * return (error); // done, report error 186 */ 187 188 /* ARGSUSED */ 189 static int 190 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 191 { 192 znode_t *zp = VTOZ(*vpp); 193 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 194 195 ZFS_ENTER(zfsvfs); 196 ZFS_VERIFY_ZP(zp); 197 198 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) && 199 ((flag & FAPPEND) == 0)) { 200 ZFS_EXIT(zfsvfs); 201 return (SET_ERROR(EPERM)); 202 } 203 204 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 205 ZTOV(zp)->v_type == VREG && 206 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) { 207 if (fs_vscan(*vpp, cr, 0) != 0) { 208 ZFS_EXIT(zfsvfs); 209 return (SET_ERROR(EACCES)); 210 } 211 } 212 213 /* Keep a count of the synchronous opens in the znode */ 214 if (flag & (FSYNC | FDSYNC)) 215 atomic_inc_32(&zp->z_sync_cnt); 216 217 ZFS_EXIT(zfsvfs); 218 return (0); 219 } 220 221 /* ARGSUSED */ 222 static int 223 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 224 caller_context_t *ct) 225 { 226 znode_t *zp = VTOZ(vp); 227 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 228 229 /* 230 * Clean up any locks held by this process on the vp. 231 */ 232 cleanlocks(vp, ddi_get_pid(), 0); 233 cleanshares(vp, ddi_get_pid()); 234 235 ZFS_ENTER(zfsvfs); 236 ZFS_VERIFY_ZP(zp); 237 238 /* Decrement the synchronous opens in the znode */ 239 if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 240 atomic_dec_32(&zp->z_sync_cnt); 241 242 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 243 ZTOV(zp)->v_type == VREG && 244 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) 245 VERIFY(fs_vscan(vp, cr, 1) == 0); 246 247 ZFS_EXIT(zfsvfs); 248 return (0); 249 } 250 251 /* 252 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 253 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 254 */ 255 static int 256 zfs_holey(vnode_t *vp, int cmd, offset_t *off) 257 { 258 znode_t *zp = VTOZ(vp); 259 uint64_t noff = (uint64_t)*off; /* new offset */ 260 uint64_t file_sz; 261 int error; 262 boolean_t hole; 263 264 file_sz = zp->z_size; 265 if (noff >= file_sz) { 266 return (SET_ERROR(ENXIO)); 267 } 268 269 if (cmd == _FIO_SEEK_HOLE) 270 hole = B_TRUE; 271 else 272 hole = B_FALSE; 273 274 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 275 276 if (error == ESRCH) 277 return (SET_ERROR(ENXIO)); 278 279 /* 280 * We could find a hole that begins after the logical end-of-file, 281 * because dmu_offset_next() only works on whole blocks. If the 282 * EOF falls mid-block, then indicate that the "virtual hole" 283 * at the end of the file begins at the logical EOF, rather than 284 * at the end of the last block. 285 */ 286 if (noff > file_sz) { 287 ASSERT(hole); 288 noff = file_sz; 289 } 290 291 if (noff < *off) 292 return (error); 293 *off = noff; 294 return (error); 295 } 296 297 /* ARGSUSED */ 298 static int 299 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 300 int *rvalp, caller_context_t *ct) 301 { 302 offset_t off; 303 offset_t ndata; 304 dmu_object_info_t doi; 305 int error; 306 zfsvfs_t *zfsvfs; 307 znode_t *zp; 308 309 switch (com) { 310 case _FIOFFS: 311 { 312 return (zfs_sync(vp->v_vfsp, 0, cred)); 313 314 /* 315 * The following two ioctls are used by bfu. Faking out, 316 * necessary to avoid bfu errors. 317 */ 318 } 319 case _FIOGDIO: 320 case _FIOSDIO: 321 { 322 return (0); 323 } 324 325 case _FIO_SEEK_DATA: 326 case _FIO_SEEK_HOLE: 327 { 328 if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 329 return (SET_ERROR(EFAULT)); 330 331 zp = VTOZ(vp); 332 zfsvfs = zp->z_zfsvfs; 333 ZFS_ENTER(zfsvfs); 334 ZFS_VERIFY_ZP(zp); 335 336 /* offset parameter is in/out */ 337 error = zfs_holey(vp, com, &off); 338 ZFS_EXIT(zfsvfs); 339 if (error) 340 return (error); 341 if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 342 return (SET_ERROR(EFAULT)); 343 return (0); 344 } 345 case _FIO_COUNT_FILLED: 346 { 347 /* 348 * _FIO_COUNT_FILLED adds a new ioctl command which 349 * exposes the number of filled blocks in a 350 * ZFS object. 351 */ 352 zp = VTOZ(vp); 353 zfsvfs = zp->z_zfsvfs; 354 ZFS_ENTER(zfsvfs); 355 ZFS_VERIFY_ZP(zp); 356 357 /* 358 * Wait for all dirty blocks for this object 359 * to get synced out to disk, and the DMU info 360 * updated. 361 */ 362 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id); 363 if (error) { 364 ZFS_EXIT(zfsvfs); 365 return (error); 366 } 367 368 /* 369 * Retrieve fill count from DMU object. 370 */ 371 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi); 372 if (error) { 373 ZFS_EXIT(zfsvfs); 374 return (error); 375 } 376 377 ndata = doi.doi_fill_count; 378 379 ZFS_EXIT(zfsvfs); 380 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag)) 381 return (SET_ERROR(EFAULT)); 382 return (0); 383 } 384 } 385 return (SET_ERROR(ENOTTY)); 386 } 387 388 /* 389 * Utility functions to map and unmap a single physical page. These 390 * are used to manage the mappable copies of ZFS file data, and therefore 391 * do not update ref/mod bits. 392 */ 393 caddr_t 394 zfs_map_page(page_t *pp, enum seg_rw rw) 395 { 396 if (kpm_enable) 397 return (hat_kpm_mapin(pp, 0)); 398 ASSERT(rw == S_READ || rw == S_WRITE); 399 return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0), 400 (caddr_t)-1)); 401 } 402 403 void 404 zfs_unmap_page(page_t *pp, caddr_t addr) 405 { 406 if (kpm_enable) { 407 hat_kpm_mapout(pp, 0, addr); 408 } else { 409 ppmapout(addr); 410 } 411 } 412 413 /* 414 * When a file is memory mapped, we must keep the IO data synchronized 415 * between the DMU cache and the memory mapped pages. What this means: 416 * 417 * On Write: If we find a memory mapped page, we write to *both* 418 * the page and the dmu buffer. 419 */ 420 static void 421 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid) 422 { 423 int64_t off; 424 425 off = start & PAGEOFFSET; 426 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 427 page_t *pp; 428 uint64_t nbytes = MIN(PAGESIZE - off, len); 429 430 if (pp = page_lookup(vp, start, SE_SHARED)) { 431 caddr_t va; 432 433 va = zfs_map_page(pp, S_WRITE); 434 (void) dmu_read(os, oid, start+off, nbytes, va+off, 435 DMU_READ_PREFETCH); 436 zfs_unmap_page(pp, va); 437 page_unlock(pp); 438 } 439 len -= nbytes; 440 off = 0; 441 } 442 } 443 444 /* 445 * When a file is memory mapped, we must keep the IO data synchronized 446 * between the DMU cache and the memory mapped pages. What this means: 447 * 448 * On Read: We "read" preferentially from memory mapped pages, 449 * else we default from the dmu buffer. 450 * 451 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 452 * the file is memory mapped. 453 */ 454 static int 455 mappedread(vnode_t *vp, int nbytes, uio_t *uio) 456 { 457 znode_t *zp = VTOZ(vp); 458 int64_t start, off; 459 int len = nbytes; 460 int error = 0; 461 462 start = uio->uio_loffset; 463 off = start & PAGEOFFSET; 464 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 465 page_t *pp; 466 uint64_t bytes = MIN(PAGESIZE - off, len); 467 468 if (pp = page_lookup(vp, start, SE_SHARED)) { 469 caddr_t va; 470 471 va = zfs_map_page(pp, S_READ); 472 error = uiomove(va + off, bytes, UIO_READ, uio); 473 zfs_unmap_page(pp, va); 474 page_unlock(pp); 475 } else { 476 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 477 uio, bytes); 478 } 479 len -= bytes; 480 off = 0; 481 if (error) 482 break; 483 } 484 return (error); 485 } 486 487 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 488 489 /* 490 * Read bytes from specified file into supplied buffer. 491 * 492 * IN: vp - vnode of file to be read from. 493 * uio - structure supplying read location, range info, 494 * and return buffer. 495 * ioflag - SYNC flags; used to provide FRSYNC semantics. 496 * cr - credentials of caller. 497 * ct - caller context 498 * 499 * OUT: uio - updated offset and range, buffer filled. 500 * 501 * RETURN: 0 on success, error code on failure. 502 * 503 * Side Effects: 504 * vp - atime updated if byte count > 0 505 */ 506 /* ARGSUSED */ 507 static int 508 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 509 { 510 znode_t *zp = VTOZ(vp); 511 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 512 ssize_t n, nbytes; 513 int error = 0; 514 rl_t *rl; 515 xuio_t *xuio = NULL; 516 517 ZFS_ENTER(zfsvfs); 518 ZFS_VERIFY_ZP(zp); 519 520 if (zp->z_pflags & ZFS_AV_QUARANTINED) { 521 ZFS_EXIT(zfsvfs); 522 return (SET_ERROR(EACCES)); 523 } 524 525 /* 526 * Validate file offset 527 */ 528 if (uio->uio_loffset < (offset_t)0) { 529 ZFS_EXIT(zfsvfs); 530 return (SET_ERROR(EINVAL)); 531 } 532 533 /* 534 * Fasttrack empty reads 535 */ 536 if (uio->uio_resid == 0) { 537 ZFS_EXIT(zfsvfs); 538 return (0); 539 } 540 541 /* 542 * Check for mandatory locks 543 */ 544 if (MANDMODE(zp->z_mode)) { 545 if (error = chklock(vp, FREAD, 546 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 547 ZFS_EXIT(zfsvfs); 548 return (error); 549 } 550 } 551 552 /* 553 * If we're in FRSYNC mode, sync out this znode before reading it. 554 */ 555 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 556 zil_commit(zfsvfs->z_log, zp->z_id); 557 558 /* 559 * Lock the range against changes. 560 */ 561 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 562 563 /* 564 * If we are reading past end-of-file we can skip 565 * to the end; but we might still need to set atime. 566 */ 567 if (uio->uio_loffset >= zp->z_size) { 568 error = 0; 569 goto out; 570 } 571 572 ASSERT(uio->uio_loffset < zp->z_size); 573 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset); 574 575 if ((uio->uio_extflg == UIO_XUIO) && 576 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) { 577 int nblk; 578 int blksz = zp->z_blksz; 579 uint64_t offset = uio->uio_loffset; 580 581 xuio = (xuio_t *)uio; 582 if ((ISP2(blksz))) { 583 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset, 584 blksz)) / blksz; 585 } else { 586 ASSERT(offset + n <= blksz); 587 nblk = 1; 588 } 589 (void) dmu_xuio_init(xuio, nblk); 590 591 if (vn_has_cached_data(vp)) { 592 /* 593 * For simplicity, we always allocate a full buffer 594 * even if we only expect to read a portion of a block. 595 */ 596 while (--nblk >= 0) { 597 (void) dmu_xuio_add(xuio, 598 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 599 blksz), 0, blksz); 600 } 601 } 602 } 603 604 while (n > 0) { 605 nbytes = MIN(n, zfs_read_chunk_size - 606 P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 607 608 if (vn_has_cached_data(vp)) { 609 error = mappedread(vp, nbytes, uio); 610 } else { 611 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 612 uio, nbytes); 613 } 614 if (error) { 615 /* convert checksum errors into IO errors */ 616 if (error == ECKSUM) 617 error = SET_ERROR(EIO); 618 break; 619 } 620 621 n -= nbytes; 622 } 623 out: 624 zfs_range_unlock(rl); 625 626 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 627 ZFS_EXIT(zfsvfs); 628 return (error); 629 } 630 631 /* 632 * Write the bytes to a file. 633 * 634 * IN: vp - vnode of file to be written to. 635 * uio - structure supplying write location, range info, 636 * and data buffer. 637 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is 638 * set if in append mode. 639 * cr - credentials of caller. 640 * ct - caller context (NFS/CIFS fem monitor only) 641 * 642 * OUT: uio - updated offset and range. 643 * 644 * RETURN: 0 on success, error code on failure. 645 * 646 * Timestamps: 647 * vp - ctime|mtime updated if byte count > 0 648 */ 649 650 /* ARGSUSED */ 651 static int 652 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 653 { 654 znode_t *zp = VTOZ(vp); 655 rlim64_t limit = uio->uio_llimit; 656 ssize_t start_resid = uio->uio_resid; 657 ssize_t tx_bytes; 658 uint64_t end_size; 659 dmu_tx_t *tx; 660 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 661 zilog_t *zilog; 662 offset_t woff; 663 ssize_t n, nbytes; 664 rl_t *rl; 665 int max_blksz = zfsvfs->z_max_blksz; 666 int error = 0; 667 arc_buf_t *abuf; 668 iovec_t *aiov = NULL; 669 xuio_t *xuio = NULL; 670 int i_iov = 0; 671 int iovcnt = uio->uio_iovcnt; 672 iovec_t *iovp = uio->uio_iov; 673 int write_eof; 674 int count = 0; 675 sa_bulk_attr_t bulk[4]; 676 uint64_t mtime[2], ctime[2]; 677 678 /* 679 * Fasttrack empty write 680 */ 681 n = start_resid; 682 if (n == 0) 683 return (0); 684 685 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 686 limit = MAXOFFSET_T; 687 688 ZFS_ENTER(zfsvfs); 689 ZFS_VERIFY_ZP(zp); 690 691 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 692 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 693 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 694 &zp->z_size, 8); 695 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 696 &zp->z_pflags, 8); 697 698 /* 699 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our 700 * callers might not be able to detect properly that we are read-only, 701 * so check it explicitly here. 702 */ 703 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 704 ZFS_EXIT(zfsvfs); 705 return (SET_ERROR(EROFS)); 706 } 707 708 /* 709 * If immutable or not appending then return EPERM 710 */ 711 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 712 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 713 (uio->uio_loffset < zp->z_size))) { 714 ZFS_EXIT(zfsvfs); 715 return (SET_ERROR(EPERM)); 716 } 717 718 zilog = zfsvfs->z_log; 719 720 /* 721 * Validate file offset 722 */ 723 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset; 724 if (woff < 0) { 725 ZFS_EXIT(zfsvfs); 726 return (SET_ERROR(EINVAL)); 727 } 728 729 /* 730 * Check for mandatory locks before calling zfs_range_lock() 731 * in order to prevent a deadlock with locks set via fcntl(). 732 */ 733 if (MANDMODE((mode_t)zp->z_mode) && 734 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 735 ZFS_EXIT(zfsvfs); 736 return (error); 737 } 738 739 /* 740 * Pre-fault the pages to ensure slow (eg NFS) pages 741 * don't hold up txg. 742 * Skip this if uio contains loaned arc_buf. 743 */ 744 if ((uio->uio_extflg == UIO_XUIO) && 745 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) 746 xuio = (xuio_t *)uio; 747 else 748 uio_prefaultpages(MIN(n, max_blksz), uio); 749 750 /* 751 * If in append mode, set the io offset pointer to eof. 752 */ 753 if (ioflag & FAPPEND) { 754 /* 755 * Obtain an appending range lock to guarantee file append 756 * semantics. We reset the write offset once we have the lock. 757 */ 758 rl = zfs_range_lock(zp, 0, n, RL_APPEND); 759 woff = rl->r_off; 760 if (rl->r_len == UINT64_MAX) { 761 /* 762 * We overlocked the file because this write will cause 763 * the file block size to increase. 764 * Note that zp_size cannot change with this lock held. 765 */ 766 woff = zp->z_size; 767 } 768 uio->uio_loffset = woff; 769 } else { 770 /* 771 * Note that if the file block size will change as a result of 772 * this write, then this range lock will lock the entire file 773 * so that we can re-write the block safely. 774 */ 775 rl = zfs_range_lock(zp, woff, n, RL_WRITER); 776 } 777 778 if (woff >= limit) { 779 zfs_range_unlock(rl); 780 ZFS_EXIT(zfsvfs); 781 return (SET_ERROR(EFBIG)); 782 } 783 784 if ((woff + n) > limit || woff > (limit - n)) 785 n = limit - woff; 786 787 /* Will this write extend the file length? */ 788 write_eof = (woff + n > zp->z_size); 789 790 end_size = MAX(zp->z_size, woff + n); 791 792 /* 793 * Write the file in reasonable size chunks. Each chunk is written 794 * in a separate transaction; this keeps the intent log records small 795 * and allows us to do more fine-grained space accounting. 796 */ 797 while (n > 0) { 798 abuf = NULL; 799 woff = uio->uio_loffset; 800 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 801 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 802 if (abuf != NULL) 803 dmu_return_arcbuf(abuf); 804 error = SET_ERROR(EDQUOT); 805 break; 806 } 807 808 if (xuio && abuf == NULL) { 809 ASSERT(i_iov < iovcnt); 810 aiov = &iovp[i_iov]; 811 abuf = dmu_xuio_arcbuf(xuio, i_iov); 812 dmu_xuio_clear(xuio, i_iov); 813 DTRACE_PROBE3(zfs_cp_write, int, i_iov, 814 iovec_t *, aiov, arc_buf_t *, abuf); 815 ASSERT((aiov->iov_base == abuf->b_data) || 816 ((char *)aiov->iov_base - (char *)abuf->b_data + 817 aiov->iov_len == arc_buf_size(abuf))); 818 i_iov++; 819 } else if (abuf == NULL && n >= max_blksz && 820 woff >= zp->z_size && 821 P2PHASE(woff, max_blksz) == 0 && 822 zp->z_blksz == max_blksz) { 823 /* 824 * This write covers a full block. "Borrow" a buffer 825 * from the dmu so that we can fill it before we enter 826 * a transaction. This avoids the possibility of 827 * holding up the transaction if the data copy hangs 828 * up on a pagefault (e.g., from an NFS server mapping). 829 */ 830 size_t cbytes; 831 832 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 833 max_blksz); 834 ASSERT(abuf != NULL); 835 ASSERT(arc_buf_size(abuf) == max_blksz); 836 if (error = uiocopy(abuf->b_data, max_blksz, 837 UIO_WRITE, uio, &cbytes)) { 838 dmu_return_arcbuf(abuf); 839 break; 840 } 841 ASSERT(cbytes == max_blksz); 842 } 843 844 /* 845 * Start a transaction. 846 */ 847 tx = dmu_tx_create(zfsvfs->z_os); 848 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 849 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 850 zfs_sa_upgrade_txholds(tx, zp); 851 error = dmu_tx_assign(tx, TXG_WAIT); 852 if (error) { 853 dmu_tx_abort(tx); 854 if (abuf != NULL) 855 dmu_return_arcbuf(abuf); 856 break; 857 } 858 859 /* 860 * If zfs_range_lock() over-locked we grow the blocksize 861 * and then reduce the lock range. This will only happen 862 * on the first iteration since zfs_range_reduce() will 863 * shrink down r_len to the appropriate size. 864 */ 865 if (rl->r_len == UINT64_MAX) { 866 uint64_t new_blksz; 867 868 if (zp->z_blksz > max_blksz) { 869 /* 870 * File's blocksize is already larger than the 871 * "recordsize" property. Only let it grow to 872 * the next power of 2. 873 */ 874 ASSERT(!ISP2(zp->z_blksz)); 875 new_blksz = MIN(end_size, 876 1 << highbit64(zp->z_blksz)); 877 } else { 878 new_blksz = MIN(end_size, max_blksz); 879 } 880 zfs_grow_blocksize(zp, new_blksz, tx); 881 zfs_range_reduce(rl, woff, n); 882 } 883 884 /* 885 * XXX - should we really limit each write to z_max_blksz? 886 * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 887 */ 888 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 889 890 if (abuf == NULL) { 891 tx_bytes = uio->uio_resid; 892 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 893 uio, nbytes, tx); 894 tx_bytes -= uio->uio_resid; 895 } else { 896 tx_bytes = nbytes; 897 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len); 898 /* 899 * If this is not a full block write, but we are 900 * extending the file past EOF and this data starts 901 * block-aligned, use assign_arcbuf(). Otherwise, 902 * write via dmu_write(). 903 */ 904 if (tx_bytes < max_blksz && (!write_eof || 905 aiov->iov_base != abuf->b_data)) { 906 ASSERT(xuio); 907 dmu_write(zfsvfs->z_os, zp->z_id, woff, 908 aiov->iov_len, aiov->iov_base, tx); 909 dmu_return_arcbuf(abuf); 910 xuio_stat_wbuf_copied(); 911 } else { 912 ASSERT(xuio || tx_bytes == max_blksz); 913 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl), 914 woff, abuf, tx); 915 } 916 ASSERT(tx_bytes <= uio->uio_resid); 917 uioskip(uio, tx_bytes); 918 } 919 if (tx_bytes && vn_has_cached_data(vp)) { 920 update_pages(vp, woff, 921 tx_bytes, zfsvfs->z_os, zp->z_id); 922 } 923 924 /* 925 * If we made no progress, we're done. If we made even 926 * partial progress, update the znode and ZIL accordingly. 927 */ 928 if (tx_bytes == 0) { 929 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 930 (void *)&zp->z_size, sizeof (uint64_t), tx); 931 dmu_tx_commit(tx); 932 ASSERT(error != 0); 933 break; 934 } 935 936 /* 937 * Clear Set-UID/Set-GID bits on successful write if not 938 * privileged and at least one of the excute bits is set. 939 * 940 * It would be nice to to this after all writes have 941 * been done, but that would still expose the ISUID/ISGID 942 * to another app after the partial write is committed. 943 * 944 * Note: we don't call zfs_fuid_map_id() here because 945 * user 0 is not an ephemeral uid. 946 */ 947 mutex_enter(&zp->z_acl_lock); 948 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | 949 (S_IXUSR >> 6))) != 0 && 950 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 951 secpolicy_vnode_setid_retain(cr, 952 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) { 953 uint64_t newmode; 954 zp->z_mode &= ~(S_ISUID | S_ISGID); 955 newmode = zp->z_mode; 956 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 957 (void *)&newmode, sizeof (uint64_t), tx); 958 } 959 mutex_exit(&zp->z_acl_lock); 960 961 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 962 B_TRUE); 963 964 /* 965 * Update the file size (zp_size) if it has changed; 966 * account for possible concurrent updates. 967 */ 968 while ((end_size = zp->z_size) < uio->uio_loffset) { 969 (void) atomic_cas_64(&zp->z_size, end_size, 970 uio->uio_loffset); 971 ASSERT(error == 0); 972 } 973 /* 974 * If we are replaying and eof is non zero then force 975 * the file size to the specified eof. Note, there's no 976 * concurrency during replay. 977 */ 978 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 979 zp->z_size = zfsvfs->z_replay_eof; 980 981 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 982 983 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 984 dmu_tx_commit(tx); 985 986 if (error != 0) 987 break; 988 ASSERT(tx_bytes == nbytes); 989 n -= nbytes; 990 991 if (!xuio && n > 0) 992 uio_prefaultpages(MIN(n, max_blksz), uio); 993 } 994 995 zfs_range_unlock(rl); 996 997 /* 998 * If we're in replay mode, or we made no progress, return error. 999 * Otherwise, it's at least a partial write, so it's successful. 1000 */ 1001 if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 1002 ZFS_EXIT(zfsvfs); 1003 return (error); 1004 } 1005 1006 if (ioflag & (FSYNC | FDSYNC) || 1007 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1008 zil_commit(zilog, zp->z_id); 1009 1010 ZFS_EXIT(zfsvfs); 1011 return (0); 1012 } 1013 1014 void 1015 zfs_get_done(zgd_t *zgd, int error) 1016 { 1017 znode_t *zp = zgd->zgd_private; 1018 objset_t *os = zp->z_zfsvfs->z_os; 1019 1020 if (zgd->zgd_db) 1021 dmu_buf_rele(zgd->zgd_db, zgd); 1022 1023 zfs_range_unlock(zgd->zgd_rl); 1024 1025 /* 1026 * Release the vnode asynchronously as we currently have the 1027 * txg stopped from syncing. 1028 */ 1029 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 1030 1031 if (error == 0 && zgd->zgd_bp) 1032 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1033 1034 kmem_free(zgd, sizeof (zgd_t)); 1035 } 1036 1037 #ifdef DEBUG 1038 static int zil_fault_io = 0; 1039 #endif 1040 1041 /* 1042 * Get data to generate a TX_WRITE intent log record. 1043 */ 1044 int 1045 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1046 { 1047 zfsvfs_t *zfsvfs = arg; 1048 objset_t *os = zfsvfs->z_os; 1049 znode_t *zp; 1050 uint64_t object = lr->lr_foid; 1051 uint64_t offset = lr->lr_offset; 1052 uint64_t size = lr->lr_length; 1053 blkptr_t *bp = &lr->lr_blkptr; 1054 dmu_buf_t *db; 1055 zgd_t *zgd; 1056 int error = 0; 1057 1058 ASSERT(zio != NULL); 1059 ASSERT(size != 0); 1060 1061 /* 1062 * Nothing to do if the file has been removed 1063 */ 1064 if (zfs_zget(zfsvfs, object, &zp) != 0) 1065 return (SET_ERROR(ENOENT)); 1066 if (zp->z_unlinked) { 1067 /* 1068 * Release the vnode asynchronously as we currently have the 1069 * txg stopped from syncing. 1070 */ 1071 VN_RELE_ASYNC(ZTOV(zp), 1072 dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 1073 return (SET_ERROR(ENOENT)); 1074 } 1075 1076 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 1077 zgd->zgd_zilog = zfsvfs->z_log; 1078 zgd->zgd_private = zp; 1079 1080 /* 1081 * Write records come in two flavors: immediate and indirect. 1082 * For small writes it's cheaper to store the data with the 1083 * log record (immediate); for large writes it's cheaper to 1084 * sync the data and get a pointer to it (indirect) so that 1085 * we don't have to write the data twice. 1086 */ 1087 if (buf != NULL) { /* immediate write */ 1088 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 1089 /* test for truncation needs to be done while range locked */ 1090 if (offset >= zp->z_size) { 1091 error = SET_ERROR(ENOENT); 1092 } else { 1093 error = dmu_read(os, object, offset, size, buf, 1094 DMU_READ_NO_PREFETCH); 1095 } 1096 ASSERT(error == 0 || error == ENOENT); 1097 } else { /* indirect write */ 1098 /* 1099 * Have to lock the whole block to ensure when it's 1100 * written out and it's checksum is being calculated 1101 * that no one can change the data. We need to re-check 1102 * blocksize after we get the lock in case it's changed! 1103 */ 1104 for (;;) { 1105 uint64_t blkoff; 1106 size = zp->z_blksz; 1107 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 1108 offset -= blkoff; 1109 zgd->zgd_rl = zfs_range_lock(zp, offset, size, 1110 RL_READER); 1111 if (zp->z_blksz == size) 1112 break; 1113 offset += blkoff; 1114 zfs_range_unlock(zgd->zgd_rl); 1115 } 1116 /* test for truncation needs to be done while range locked */ 1117 if (lr->lr_offset >= zp->z_size) 1118 error = SET_ERROR(ENOENT); 1119 #ifdef DEBUG 1120 if (zil_fault_io) { 1121 error = SET_ERROR(EIO); 1122 zil_fault_io = 0; 1123 } 1124 #endif 1125 if (error == 0) 1126 error = dmu_buf_hold(os, object, offset, zgd, &db, 1127 DMU_READ_NO_PREFETCH); 1128 1129 if (error == 0) { 1130 blkptr_t *obp = dmu_buf_get_blkptr(db); 1131 if (obp) { 1132 ASSERT(BP_IS_HOLE(bp)); 1133 *bp = *obp; 1134 } 1135 1136 zgd->zgd_db = db; 1137 zgd->zgd_bp = bp; 1138 1139 ASSERT(db->db_offset == offset); 1140 ASSERT(db->db_size == size); 1141 1142 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1143 zfs_get_done, zgd); 1144 ASSERT(error || lr->lr_length <= zp->z_blksz); 1145 1146 /* 1147 * On success, we need to wait for the write I/O 1148 * initiated by dmu_sync() to complete before we can 1149 * release this dbuf. We will finish everything up 1150 * in the zfs_get_done() callback. 1151 */ 1152 if (error == 0) 1153 return (0); 1154 1155 if (error == EALREADY) { 1156 lr->lr_common.lrc_txtype = TX_WRITE2; 1157 error = 0; 1158 } 1159 } 1160 } 1161 1162 zfs_get_done(zgd, error); 1163 1164 return (error); 1165 } 1166 1167 /*ARGSUSED*/ 1168 static int 1169 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 1170 caller_context_t *ct) 1171 { 1172 znode_t *zp = VTOZ(vp); 1173 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1174 int error; 1175 1176 ZFS_ENTER(zfsvfs); 1177 ZFS_VERIFY_ZP(zp); 1178 1179 if (flag & V_ACE_MASK) 1180 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 1181 else 1182 error = zfs_zaccess_rwx(zp, mode, flag, cr); 1183 1184 ZFS_EXIT(zfsvfs); 1185 return (error); 1186 } 1187 1188 /* 1189 * If vnode is for a device return a specfs vnode instead. 1190 */ 1191 static int 1192 specvp_check(vnode_t **vpp, cred_t *cr) 1193 { 1194 int error = 0; 1195 1196 if (IS_DEVVP(*vpp)) { 1197 struct vnode *svp; 1198 1199 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1200 VN_RELE(*vpp); 1201 if (svp == NULL) 1202 error = SET_ERROR(ENOSYS); 1203 *vpp = svp; 1204 } 1205 return (error); 1206 } 1207 1208 1209 /* 1210 * Lookup an entry in a directory, or an extended attribute directory. 1211 * If it exists, return a held vnode reference for it. 1212 * 1213 * IN: dvp - vnode of directory to search. 1214 * nm - name of entry to lookup. 1215 * pnp - full pathname to lookup [UNUSED]. 1216 * flags - LOOKUP_XATTR set if looking for an attribute. 1217 * rdir - root directory vnode [UNUSED]. 1218 * cr - credentials of caller. 1219 * ct - caller context 1220 * direntflags - directory lookup flags 1221 * realpnp - returned pathname. 1222 * 1223 * OUT: vpp - vnode of located entry, NULL if not found. 1224 * 1225 * RETURN: 0 on success, error code on failure. 1226 * 1227 * Timestamps: 1228 * NA 1229 */ 1230 /* ARGSUSED */ 1231 static int 1232 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 1233 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 1234 int *direntflags, pathname_t *realpnp) 1235 { 1236 znode_t *zdp = VTOZ(dvp); 1237 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 1238 int error = 0; 1239 1240 /* fast path */ 1241 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 1242 1243 if (dvp->v_type != VDIR) { 1244 return (SET_ERROR(ENOTDIR)); 1245 } else if (zdp->z_sa_hdl == NULL) { 1246 return (SET_ERROR(EIO)); 1247 } 1248 1249 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 1250 error = zfs_fastaccesschk_execute(zdp, cr); 1251 if (!error) { 1252 *vpp = dvp; 1253 VN_HOLD(*vpp); 1254 return (0); 1255 } 1256 return (error); 1257 } else { 1258 vnode_t *tvp = dnlc_lookup(dvp, nm); 1259 1260 if (tvp) { 1261 error = zfs_fastaccesschk_execute(zdp, cr); 1262 if (error) { 1263 VN_RELE(tvp); 1264 return (error); 1265 } 1266 if (tvp == DNLC_NO_VNODE) { 1267 VN_RELE(tvp); 1268 return (SET_ERROR(ENOENT)); 1269 } else { 1270 *vpp = tvp; 1271 return (specvp_check(vpp, cr)); 1272 } 1273 } 1274 } 1275 } 1276 1277 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1278 1279 ZFS_ENTER(zfsvfs); 1280 ZFS_VERIFY_ZP(zdp); 1281 1282 *vpp = NULL; 1283 1284 if (flags & LOOKUP_XATTR) { 1285 /* 1286 * If the xattr property is off, refuse the lookup request. 1287 */ 1288 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 1289 ZFS_EXIT(zfsvfs); 1290 return (SET_ERROR(EINVAL)); 1291 } 1292 1293 /* 1294 * We don't allow recursive attributes.. 1295 * Maybe someday we will. 1296 */ 1297 if (zdp->z_pflags & ZFS_XATTR) { 1298 ZFS_EXIT(zfsvfs); 1299 return (SET_ERROR(EINVAL)); 1300 } 1301 1302 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1303 ZFS_EXIT(zfsvfs); 1304 return (error); 1305 } 1306 1307 /* 1308 * Do we have permission to get into attribute directory? 1309 */ 1310 1311 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 1312 B_FALSE, cr)) { 1313 VN_RELE(*vpp); 1314 *vpp = NULL; 1315 } 1316 1317 ZFS_EXIT(zfsvfs); 1318 return (error); 1319 } 1320 1321 if (dvp->v_type != VDIR) { 1322 ZFS_EXIT(zfsvfs); 1323 return (SET_ERROR(ENOTDIR)); 1324 } 1325 1326 /* 1327 * Check accessibility of directory. 1328 */ 1329 1330 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1331 ZFS_EXIT(zfsvfs); 1332 return (error); 1333 } 1334 1335 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 1336 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1337 ZFS_EXIT(zfsvfs); 1338 return (SET_ERROR(EILSEQ)); 1339 } 1340 1341 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 1342 if (error == 0) 1343 error = specvp_check(vpp, cr); 1344 1345 ZFS_EXIT(zfsvfs); 1346 return (error); 1347 } 1348 1349 /* 1350 * Attempt to create a new entry in a directory. If the entry 1351 * already exists, truncate the file if permissible, else return 1352 * an error. Return the vp of the created or trunc'd file. 1353 * 1354 * IN: dvp - vnode of directory to put new file entry in. 1355 * name - name of new file entry. 1356 * vap - attributes of new file. 1357 * excl - flag indicating exclusive or non-exclusive mode. 1358 * mode - mode to open file with. 1359 * cr - credentials of caller. 1360 * flag - large file flag [UNUSED]. 1361 * ct - caller context 1362 * vsecp - ACL to be set 1363 * 1364 * OUT: vpp - vnode of created or trunc'd entry. 1365 * 1366 * RETURN: 0 on success, error code on failure. 1367 * 1368 * Timestamps: 1369 * dvp - ctime|mtime updated if new entry created 1370 * vp - ctime|mtime always, atime if new 1371 */ 1372 1373 /* ARGSUSED */ 1374 static int 1375 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1376 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 1377 vsecattr_t *vsecp) 1378 { 1379 znode_t *zp, *dzp = VTOZ(dvp); 1380 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1381 zilog_t *zilog; 1382 objset_t *os; 1383 zfs_dirlock_t *dl; 1384 dmu_tx_t *tx; 1385 int error; 1386 ksid_t *ksid; 1387 uid_t uid; 1388 gid_t gid = crgetgid(cr); 1389 zfs_acl_ids_t acl_ids; 1390 boolean_t fuid_dirtied; 1391 boolean_t have_acl = B_FALSE; 1392 boolean_t waited = B_FALSE; 1393 1394 /* 1395 * If we have an ephemeral id, ACL, or XVATTR then 1396 * make sure file system is at proper version 1397 */ 1398 1399 ksid = crgetsid(cr, KSID_OWNER); 1400 if (ksid) 1401 uid = ksid_getid(ksid); 1402 else 1403 uid = crgetuid(cr); 1404 1405 if (zfsvfs->z_use_fuids == B_FALSE && 1406 (vsecp || (vap->va_mask & AT_XVATTR) || 1407 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1408 return (SET_ERROR(EINVAL)); 1409 1410 ZFS_ENTER(zfsvfs); 1411 ZFS_VERIFY_ZP(dzp); 1412 os = zfsvfs->z_os; 1413 zilog = zfsvfs->z_log; 1414 1415 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 1416 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1417 ZFS_EXIT(zfsvfs); 1418 return (SET_ERROR(EILSEQ)); 1419 } 1420 1421 if (vap->va_mask & AT_XVATTR) { 1422 if ((error = secpolicy_xvattr((xvattr_t *)vap, 1423 crgetuid(cr), cr, vap->va_type)) != 0) { 1424 ZFS_EXIT(zfsvfs); 1425 return (error); 1426 } 1427 } 1428 top: 1429 *vpp = NULL; 1430 1431 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1432 vap->va_mode &= ~VSVTX; 1433 1434 if (*name == '\0') { 1435 /* 1436 * Null component name refers to the directory itself. 1437 */ 1438 VN_HOLD(dvp); 1439 zp = dzp; 1440 dl = NULL; 1441 error = 0; 1442 } else { 1443 /* possible VN_HOLD(zp) */ 1444 int zflg = 0; 1445 1446 if (flag & FIGNORECASE) 1447 zflg |= ZCILOOK; 1448 1449 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1450 NULL, NULL); 1451 if (error) { 1452 if (have_acl) 1453 zfs_acl_ids_free(&acl_ids); 1454 if (strcmp(name, "..") == 0) 1455 error = SET_ERROR(EISDIR); 1456 ZFS_EXIT(zfsvfs); 1457 return (error); 1458 } 1459 } 1460 1461 if (zp == NULL) { 1462 uint64_t txtype; 1463 1464 /* 1465 * Create a new file object and update the directory 1466 * to reference it. 1467 */ 1468 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1469 if (have_acl) 1470 zfs_acl_ids_free(&acl_ids); 1471 goto out; 1472 } 1473 1474 /* 1475 * We only support the creation of regular files in 1476 * extended attribute directories. 1477 */ 1478 1479 if ((dzp->z_pflags & ZFS_XATTR) && 1480 (vap->va_type != VREG)) { 1481 if (have_acl) 1482 zfs_acl_ids_free(&acl_ids); 1483 error = SET_ERROR(EINVAL); 1484 goto out; 1485 } 1486 1487 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 1488 cr, vsecp, &acl_ids)) != 0) 1489 goto out; 1490 have_acl = B_TRUE; 1491 1492 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 1493 zfs_acl_ids_free(&acl_ids); 1494 error = SET_ERROR(EDQUOT); 1495 goto out; 1496 } 1497 1498 tx = dmu_tx_create(os); 1499 1500 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1501 ZFS_SA_BASE_ATTR_SIZE); 1502 1503 fuid_dirtied = zfsvfs->z_fuid_dirty; 1504 if (fuid_dirtied) 1505 zfs_fuid_txhold(zfsvfs, tx); 1506 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1507 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 1508 if (!zfsvfs->z_use_sa && 1509 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1510 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1511 0, acl_ids.z_aclp->z_acl_bytes); 1512 } 1513 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1514 if (error) { 1515 zfs_dirent_unlock(dl); 1516 if (error == ERESTART) { 1517 waited = B_TRUE; 1518 dmu_tx_wait(tx); 1519 dmu_tx_abort(tx); 1520 goto top; 1521 } 1522 zfs_acl_ids_free(&acl_ids); 1523 dmu_tx_abort(tx); 1524 ZFS_EXIT(zfsvfs); 1525 return (error); 1526 } 1527 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1528 1529 if (fuid_dirtied) 1530 zfs_fuid_sync(zfsvfs, tx); 1531 1532 (void) zfs_link_create(dl, zp, tx, ZNEW); 1533 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 1534 if (flag & FIGNORECASE) 1535 txtype |= TX_CI; 1536 zfs_log_create(zilog, tx, txtype, dzp, zp, name, 1537 vsecp, acl_ids.z_fuidp, vap); 1538 zfs_acl_ids_free(&acl_ids); 1539 dmu_tx_commit(tx); 1540 } else { 1541 int aflags = (flag & FAPPEND) ? V_APPEND : 0; 1542 1543 if (have_acl) 1544 zfs_acl_ids_free(&acl_ids); 1545 have_acl = B_FALSE; 1546 1547 /* 1548 * A directory entry already exists for this name. 1549 */ 1550 /* 1551 * Can't truncate an existing file if in exclusive mode. 1552 */ 1553 if (excl == EXCL) { 1554 error = SET_ERROR(EEXIST); 1555 goto out; 1556 } 1557 /* 1558 * Can't open a directory for writing. 1559 */ 1560 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1561 error = SET_ERROR(EISDIR); 1562 goto out; 1563 } 1564 /* 1565 * Verify requested access to file. 1566 */ 1567 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1568 goto out; 1569 } 1570 1571 mutex_enter(&dzp->z_lock); 1572 dzp->z_seq++; 1573 mutex_exit(&dzp->z_lock); 1574 1575 /* 1576 * Truncate regular files if requested. 1577 */ 1578 if ((ZTOV(zp)->v_type == VREG) && 1579 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 1580 /* we can't hold any locks when calling zfs_freesp() */ 1581 zfs_dirent_unlock(dl); 1582 dl = NULL; 1583 error = zfs_freesp(zp, 0, 0, mode, TRUE); 1584 if (error == 0) { 1585 vnevent_create(ZTOV(zp), ct); 1586 } 1587 } 1588 } 1589 out: 1590 1591 if (dl) 1592 zfs_dirent_unlock(dl); 1593 1594 if (error) { 1595 if (zp) 1596 VN_RELE(ZTOV(zp)); 1597 } else { 1598 *vpp = ZTOV(zp); 1599 error = specvp_check(vpp, cr); 1600 } 1601 1602 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1603 zil_commit(zilog, 0); 1604 1605 ZFS_EXIT(zfsvfs); 1606 return (error); 1607 } 1608 1609 /* 1610 * Remove an entry from a directory. 1611 * 1612 * IN: dvp - vnode of directory to remove entry from. 1613 * name - name of entry to remove. 1614 * cr - credentials of caller. 1615 * ct - caller context 1616 * flags - case flags 1617 * 1618 * RETURN: 0 on success, error code on failure. 1619 * 1620 * Timestamps: 1621 * dvp - ctime|mtime 1622 * vp - ctime (if nlink > 0) 1623 */ 1624 1625 uint64_t null_xattr = 0; 1626 1627 /*ARGSUSED*/ 1628 static int 1629 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 1630 int flags) 1631 { 1632 znode_t *zp, *dzp = VTOZ(dvp); 1633 znode_t *xzp; 1634 vnode_t *vp; 1635 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1636 zilog_t *zilog; 1637 uint64_t acl_obj, xattr_obj; 1638 uint64_t xattr_obj_unlinked = 0; 1639 uint64_t obj = 0; 1640 zfs_dirlock_t *dl; 1641 dmu_tx_t *tx; 1642 boolean_t may_delete_now, delete_now = FALSE; 1643 boolean_t unlinked, toobig = FALSE; 1644 uint64_t txtype; 1645 pathname_t *realnmp = NULL; 1646 pathname_t realnm; 1647 int error; 1648 int zflg = ZEXISTS; 1649 boolean_t waited = B_FALSE; 1650 1651 ZFS_ENTER(zfsvfs); 1652 ZFS_VERIFY_ZP(dzp); 1653 zilog = zfsvfs->z_log; 1654 1655 if (flags & FIGNORECASE) { 1656 zflg |= ZCILOOK; 1657 pn_alloc(&realnm); 1658 realnmp = &realnm; 1659 } 1660 1661 top: 1662 xattr_obj = 0; 1663 xzp = NULL; 1664 /* 1665 * Attempt to lock directory; fail if entry doesn't exist. 1666 */ 1667 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1668 NULL, realnmp)) { 1669 if (realnmp) 1670 pn_free(realnmp); 1671 ZFS_EXIT(zfsvfs); 1672 return (error); 1673 } 1674 1675 vp = ZTOV(zp); 1676 1677 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1678 goto out; 1679 } 1680 1681 /* 1682 * Need to use rmdir for removing directories. 1683 */ 1684 if (vp->v_type == VDIR) { 1685 error = SET_ERROR(EPERM); 1686 goto out; 1687 } 1688 1689 vnevent_remove(vp, dvp, name, ct); 1690 1691 if (realnmp) 1692 dnlc_remove(dvp, realnmp->pn_buf); 1693 else 1694 dnlc_remove(dvp, name); 1695 1696 mutex_enter(&vp->v_lock); 1697 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1698 mutex_exit(&vp->v_lock); 1699 1700 /* 1701 * We may delete the znode now, or we may put it in the unlinked set; 1702 * it depends on whether we're the last link, and on whether there are 1703 * other holds on the vnode. So we dmu_tx_hold() the right things to 1704 * allow for either case. 1705 */ 1706 obj = zp->z_id; 1707 tx = dmu_tx_create(zfsvfs->z_os); 1708 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1709 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1710 zfs_sa_upgrade_txholds(tx, zp); 1711 zfs_sa_upgrade_txholds(tx, dzp); 1712 if (may_delete_now) { 1713 toobig = 1714 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 1715 /* if the file is too big, only hold_free a token amount */ 1716 dmu_tx_hold_free(tx, zp->z_id, 0, 1717 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 1718 } 1719 1720 /* are there any extended attributes? */ 1721 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1722 &xattr_obj, sizeof (xattr_obj)); 1723 if (error == 0 && xattr_obj) { 1724 error = zfs_zget(zfsvfs, xattr_obj, &xzp); 1725 ASSERT0(error); 1726 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 1727 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1728 } 1729 1730 mutex_enter(&zp->z_lock); 1731 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1732 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1733 mutex_exit(&zp->z_lock); 1734 1735 /* charge as an update -- would be nice not to charge at all */ 1736 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1737 1738 /* 1739 * Mark this transaction as typically resulting in a net free of space 1740 */ 1741 dmu_tx_mark_netfree(tx); 1742 1743 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1744 if (error) { 1745 zfs_dirent_unlock(dl); 1746 VN_RELE(vp); 1747 if (xzp) 1748 VN_RELE(ZTOV(xzp)); 1749 if (error == ERESTART) { 1750 waited = B_TRUE; 1751 dmu_tx_wait(tx); 1752 dmu_tx_abort(tx); 1753 goto top; 1754 } 1755 if (realnmp) 1756 pn_free(realnmp); 1757 dmu_tx_abort(tx); 1758 ZFS_EXIT(zfsvfs); 1759 return (error); 1760 } 1761 1762 /* 1763 * Remove the directory entry. 1764 */ 1765 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1766 1767 if (error) { 1768 dmu_tx_commit(tx); 1769 goto out; 1770 } 1771 1772 if (unlinked) { 1773 /* 1774 * Hold z_lock so that we can make sure that the ACL obj 1775 * hasn't changed. Could have been deleted due to 1776 * zfs_sa_upgrade(). 1777 */ 1778 mutex_enter(&zp->z_lock); 1779 mutex_enter(&vp->v_lock); 1780 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1781 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 1782 delete_now = may_delete_now && !toobig && 1783 vp->v_count == 1 && !vn_has_cached_data(vp) && 1784 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 1785 acl_obj; 1786 mutex_exit(&vp->v_lock); 1787 } 1788 1789 if (delete_now) { 1790 if (xattr_obj_unlinked) { 1791 ASSERT3U(xzp->z_links, ==, 2); 1792 mutex_enter(&xzp->z_lock); 1793 xzp->z_unlinked = 1; 1794 xzp->z_links = 0; 1795 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 1796 &xzp->z_links, sizeof (xzp->z_links), tx); 1797 ASSERT3U(error, ==, 0); 1798 mutex_exit(&xzp->z_lock); 1799 zfs_unlinked_add(xzp, tx); 1800 1801 if (zp->z_is_sa) 1802 error = sa_remove(zp->z_sa_hdl, 1803 SA_ZPL_XATTR(zfsvfs), tx); 1804 else 1805 error = sa_update(zp->z_sa_hdl, 1806 SA_ZPL_XATTR(zfsvfs), &null_xattr, 1807 sizeof (uint64_t), tx); 1808 ASSERT0(error); 1809 } 1810 mutex_enter(&vp->v_lock); 1811 vp->v_count--; 1812 ASSERT0(vp->v_count); 1813 mutex_exit(&vp->v_lock); 1814 mutex_exit(&zp->z_lock); 1815 zfs_znode_delete(zp, tx); 1816 } else if (unlinked) { 1817 mutex_exit(&zp->z_lock); 1818 zfs_unlinked_add(zp, tx); 1819 } 1820 1821 txtype = TX_REMOVE; 1822 if (flags & FIGNORECASE) 1823 txtype |= TX_CI; 1824 zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 1825 1826 dmu_tx_commit(tx); 1827 out: 1828 if (realnmp) 1829 pn_free(realnmp); 1830 1831 zfs_dirent_unlock(dl); 1832 1833 if (!delete_now) 1834 VN_RELE(vp); 1835 if (xzp) 1836 VN_RELE(ZTOV(xzp)); 1837 1838 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1839 zil_commit(zilog, 0); 1840 1841 ZFS_EXIT(zfsvfs); 1842 return (error); 1843 } 1844 1845 /* 1846 * Create a new directory and insert it into dvp using the name 1847 * provided. Return a pointer to the inserted directory. 1848 * 1849 * IN: dvp - vnode of directory to add subdir to. 1850 * dirname - name of new directory. 1851 * vap - attributes of new directory. 1852 * cr - credentials of caller. 1853 * ct - caller context 1854 * flags - case flags 1855 * vsecp - ACL to be set 1856 * 1857 * OUT: vpp - vnode of created directory. 1858 * 1859 * RETURN: 0 on success, error code on failure. 1860 * 1861 * Timestamps: 1862 * dvp - ctime|mtime updated 1863 * vp - ctime|mtime|atime updated 1864 */ 1865 /*ARGSUSED*/ 1866 static int 1867 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 1868 caller_context_t *ct, int flags, vsecattr_t *vsecp) 1869 { 1870 znode_t *zp, *dzp = VTOZ(dvp); 1871 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1872 zilog_t *zilog; 1873 zfs_dirlock_t *dl; 1874 uint64_t txtype; 1875 dmu_tx_t *tx; 1876 int error; 1877 int zf = ZNEW; 1878 ksid_t *ksid; 1879 uid_t uid; 1880 gid_t gid = crgetgid(cr); 1881 zfs_acl_ids_t acl_ids; 1882 boolean_t fuid_dirtied; 1883 boolean_t waited = B_FALSE; 1884 1885 ASSERT(vap->va_type == VDIR); 1886 1887 /* 1888 * If we have an ephemeral id, ACL, or XVATTR then 1889 * make sure file system is at proper version 1890 */ 1891 1892 ksid = crgetsid(cr, KSID_OWNER); 1893 if (ksid) 1894 uid = ksid_getid(ksid); 1895 else 1896 uid = crgetuid(cr); 1897 if (zfsvfs->z_use_fuids == B_FALSE && 1898 (vsecp || (vap->va_mask & AT_XVATTR) || 1899 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1900 return (SET_ERROR(EINVAL)); 1901 1902 ZFS_ENTER(zfsvfs); 1903 ZFS_VERIFY_ZP(dzp); 1904 zilog = zfsvfs->z_log; 1905 1906 if (dzp->z_pflags & ZFS_XATTR) { 1907 ZFS_EXIT(zfsvfs); 1908 return (SET_ERROR(EINVAL)); 1909 } 1910 1911 if (zfsvfs->z_utf8 && u8_validate(dirname, 1912 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1913 ZFS_EXIT(zfsvfs); 1914 return (SET_ERROR(EILSEQ)); 1915 } 1916 if (flags & FIGNORECASE) 1917 zf |= ZCILOOK; 1918 1919 if (vap->va_mask & AT_XVATTR) { 1920 if ((error = secpolicy_xvattr((xvattr_t *)vap, 1921 crgetuid(cr), cr, vap->va_type)) != 0) { 1922 ZFS_EXIT(zfsvfs); 1923 return (error); 1924 } 1925 } 1926 1927 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 1928 vsecp, &acl_ids)) != 0) { 1929 ZFS_EXIT(zfsvfs); 1930 return (error); 1931 } 1932 /* 1933 * First make sure the new directory doesn't exist. 1934 * 1935 * Existence is checked first to make sure we don't return 1936 * EACCES instead of EEXIST which can cause some applications 1937 * to fail. 1938 */ 1939 top: 1940 *vpp = NULL; 1941 1942 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 1943 NULL, NULL)) { 1944 zfs_acl_ids_free(&acl_ids); 1945 ZFS_EXIT(zfsvfs); 1946 return (error); 1947 } 1948 1949 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 1950 zfs_acl_ids_free(&acl_ids); 1951 zfs_dirent_unlock(dl); 1952 ZFS_EXIT(zfsvfs); 1953 return (error); 1954 } 1955 1956 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 1957 zfs_acl_ids_free(&acl_ids); 1958 zfs_dirent_unlock(dl); 1959 ZFS_EXIT(zfsvfs); 1960 return (SET_ERROR(EDQUOT)); 1961 } 1962 1963 /* 1964 * Add a new entry to the directory. 1965 */ 1966 tx = dmu_tx_create(zfsvfs->z_os); 1967 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 1968 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1969 fuid_dirtied = zfsvfs->z_fuid_dirty; 1970 if (fuid_dirtied) 1971 zfs_fuid_txhold(zfsvfs, tx); 1972 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1973 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1974 acl_ids.z_aclp->z_acl_bytes); 1975 } 1976 1977 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1978 ZFS_SA_BASE_ATTR_SIZE); 1979 1980 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1981 if (error) { 1982 zfs_dirent_unlock(dl); 1983 if (error == ERESTART) { 1984 waited = B_TRUE; 1985 dmu_tx_wait(tx); 1986 dmu_tx_abort(tx); 1987 goto top; 1988 } 1989 zfs_acl_ids_free(&acl_ids); 1990 dmu_tx_abort(tx); 1991 ZFS_EXIT(zfsvfs); 1992 return (error); 1993 } 1994 1995 /* 1996 * Create new node. 1997 */ 1998 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1999 2000 if (fuid_dirtied) 2001 zfs_fuid_sync(zfsvfs, tx); 2002 2003 /* 2004 * Now put new name in parent dir. 2005 */ 2006 (void) zfs_link_create(dl, zp, tx, ZNEW); 2007 2008 *vpp = ZTOV(zp); 2009 2010 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 2011 if (flags & FIGNORECASE) 2012 txtype |= TX_CI; 2013 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 2014 acl_ids.z_fuidp, vap); 2015 2016 zfs_acl_ids_free(&acl_ids); 2017 2018 dmu_tx_commit(tx); 2019 2020 zfs_dirent_unlock(dl); 2021 2022 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2023 zil_commit(zilog, 0); 2024 2025 ZFS_EXIT(zfsvfs); 2026 return (0); 2027 } 2028 2029 /* 2030 * Remove a directory subdir entry. If the current working 2031 * directory is the same as the subdir to be removed, the 2032 * remove will fail. 2033 * 2034 * IN: dvp - vnode of directory to remove from. 2035 * name - name of directory to be removed. 2036 * cwd - vnode of current working directory. 2037 * cr - credentials of caller. 2038 * ct - caller context 2039 * flags - case flags 2040 * 2041 * RETURN: 0 on success, error code on failure. 2042 * 2043 * Timestamps: 2044 * dvp - ctime|mtime updated 2045 */ 2046 /*ARGSUSED*/ 2047 static int 2048 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 2049 caller_context_t *ct, int flags) 2050 { 2051 znode_t *dzp = VTOZ(dvp); 2052 znode_t *zp; 2053 vnode_t *vp; 2054 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2055 zilog_t *zilog; 2056 zfs_dirlock_t *dl; 2057 dmu_tx_t *tx; 2058 int error; 2059 int zflg = ZEXISTS; 2060 boolean_t waited = B_FALSE; 2061 2062 ZFS_ENTER(zfsvfs); 2063 ZFS_VERIFY_ZP(dzp); 2064 zilog = zfsvfs->z_log; 2065 2066 if (flags & FIGNORECASE) 2067 zflg |= ZCILOOK; 2068 top: 2069 zp = NULL; 2070 2071 /* 2072 * Attempt to lock directory; fail if entry doesn't exist. 2073 */ 2074 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 2075 NULL, NULL)) { 2076 ZFS_EXIT(zfsvfs); 2077 return (error); 2078 } 2079 2080 vp = ZTOV(zp); 2081 2082 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 2083 goto out; 2084 } 2085 2086 if (vp->v_type != VDIR) { 2087 error = SET_ERROR(ENOTDIR); 2088 goto out; 2089 } 2090 2091 if (vp == cwd) { 2092 error = SET_ERROR(EINVAL); 2093 goto out; 2094 } 2095 2096 vnevent_rmdir(vp, dvp, name, ct); 2097 2098 /* 2099 * Grab a lock on the directory to make sure that noone is 2100 * trying to add (or lookup) entries while we are removing it. 2101 */ 2102 rw_enter(&zp->z_name_lock, RW_WRITER); 2103 2104 /* 2105 * Grab a lock on the parent pointer to make sure we play well 2106 * with the treewalk and directory rename code. 2107 */ 2108 rw_enter(&zp->z_parent_lock, RW_WRITER); 2109 2110 tx = dmu_tx_create(zfsvfs->z_os); 2111 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 2112 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 2113 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2114 zfs_sa_upgrade_txholds(tx, zp); 2115 zfs_sa_upgrade_txholds(tx, dzp); 2116 dmu_tx_mark_netfree(tx); 2117 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2118 if (error) { 2119 rw_exit(&zp->z_parent_lock); 2120 rw_exit(&zp->z_name_lock); 2121 zfs_dirent_unlock(dl); 2122 VN_RELE(vp); 2123 if (error == ERESTART) { 2124 waited = B_TRUE; 2125 dmu_tx_wait(tx); 2126 dmu_tx_abort(tx); 2127 goto top; 2128 } 2129 dmu_tx_abort(tx); 2130 ZFS_EXIT(zfsvfs); 2131 return (error); 2132 } 2133 2134 error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 2135 2136 if (error == 0) { 2137 uint64_t txtype = TX_RMDIR; 2138 if (flags & FIGNORECASE) 2139 txtype |= TX_CI; 2140 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 2141 } 2142 2143 dmu_tx_commit(tx); 2144 2145 rw_exit(&zp->z_parent_lock); 2146 rw_exit(&zp->z_name_lock); 2147 out: 2148 zfs_dirent_unlock(dl); 2149 2150 VN_RELE(vp); 2151 2152 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2153 zil_commit(zilog, 0); 2154 2155 ZFS_EXIT(zfsvfs); 2156 return (error); 2157 } 2158 2159 /* 2160 * Read as many directory entries as will fit into the provided 2161 * buffer from the given directory cursor position (specified in 2162 * the uio structure). 2163 * 2164 * IN: vp - vnode of directory to read. 2165 * uio - structure supplying read location, range info, 2166 * and return buffer. 2167 * cr - credentials of caller. 2168 * ct - caller context 2169 * flags - case flags 2170 * 2171 * OUT: uio - updated offset and range, buffer filled. 2172 * eofp - set to true if end-of-file detected. 2173 * 2174 * RETURN: 0 on success, error code on failure. 2175 * 2176 * Timestamps: 2177 * vp - atime updated 2178 * 2179 * Note that the low 4 bits of the cookie returned by zap is always zero. 2180 * This allows us to use the low range for "special" directory entries: 2181 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2182 * we use the offset 2 for the '.zfs' directory. 2183 */ 2184 /* ARGSUSED */ 2185 static int 2186 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 2187 caller_context_t *ct, int flags) 2188 { 2189 znode_t *zp = VTOZ(vp); 2190 iovec_t *iovp; 2191 edirent_t *eodp; 2192 dirent64_t *odp; 2193 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2194 objset_t *os; 2195 caddr_t outbuf; 2196 size_t bufsize; 2197 zap_cursor_t zc; 2198 zap_attribute_t zap; 2199 uint_t bytes_wanted; 2200 uint64_t offset; /* must be unsigned; checks for < 1 */ 2201 uint64_t parent; 2202 int local_eof; 2203 int outcount; 2204 int error; 2205 uint8_t prefetch; 2206 boolean_t check_sysattrs; 2207 2208 ZFS_ENTER(zfsvfs); 2209 ZFS_VERIFY_ZP(zp); 2210 2211 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 2212 &parent, sizeof (parent))) != 0) { 2213 ZFS_EXIT(zfsvfs); 2214 return (error); 2215 } 2216 2217 /* 2218 * If we are not given an eof variable, 2219 * use a local one. 2220 */ 2221 if (eofp == NULL) 2222 eofp = &local_eof; 2223 2224 /* 2225 * Check for valid iov_len. 2226 */ 2227 if (uio->uio_iov->iov_len <= 0) { 2228 ZFS_EXIT(zfsvfs); 2229 return (SET_ERROR(EINVAL)); 2230 } 2231 2232 /* 2233 * Quit if directory has been removed (posix) 2234 */ 2235 if ((*eofp = zp->z_unlinked) != 0) { 2236 ZFS_EXIT(zfsvfs); 2237 return (0); 2238 } 2239 2240 error = 0; 2241 os = zfsvfs->z_os; 2242 offset = uio->uio_loffset; 2243 prefetch = zp->z_zn_prefetch; 2244 2245 /* 2246 * Initialize the iterator cursor. 2247 */ 2248 if (offset <= 3) { 2249 /* 2250 * Start iteration from the beginning of the directory. 2251 */ 2252 zap_cursor_init(&zc, os, zp->z_id); 2253 } else { 2254 /* 2255 * The offset is a serialized cursor. 2256 */ 2257 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2258 } 2259 2260 /* 2261 * Get space to change directory entries into fs independent format. 2262 */ 2263 iovp = uio->uio_iov; 2264 bytes_wanted = iovp->iov_len; 2265 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2266 bufsize = bytes_wanted; 2267 outbuf = kmem_alloc(bufsize, KM_SLEEP); 2268 odp = (struct dirent64 *)outbuf; 2269 } else { 2270 bufsize = bytes_wanted; 2271 outbuf = NULL; 2272 odp = (struct dirent64 *)iovp->iov_base; 2273 } 2274 eodp = (struct edirent *)odp; 2275 2276 /* 2277 * If this VFS supports the system attribute view interface; and 2278 * we're looking at an extended attribute directory; and we care 2279 * about normalization conflicts on this vfs; then we must check 2280 * for normalization conflicts with the sysattr name space. 2281 */ 2282 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 2283 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 2284 (flags & V_RDDIR_ENTFLAGS); 2285 2286 /* 2287 * Transform to file-system independent format 2288 */ 2289 outcount = 0; 2290 while (outcount < bytes_wanted) { 2291 ino64_t objnum; 2292 ushort_t reclen; 2293 off64_t *next = NULL; 2294 2295 /* 2296 * Special case `.', `..', and `.zfs'. 2297 */ 2298 if (offset == 0) { 2299 (void) strcpy(zap.za_name, "."); 2300 zap.za_normalization_conflict = 0; 2301 objnum = zp->z_id; 2302 } else if (offset == 1) { 2303 (void) strcpy(zap.za_name, ".."); 2304 zap.za_normalization_conflict = 0; 2305 objnum = parent; 2306 } else if (offset == 2 && zfs_show_ctldir(zp)) { 2307 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 2308 zap.za_normalization_conflict = 0; 2309 objnum = ZFSCTL_INO_ROOT; 2310 } else { 2311 /* 2312 * Grab next entry. 2313 */ 2314 if (error = zap_cursor_retrieve(&zc, &zap)) { 2315 if ((*eofp = (error == ENOENT)) != 0) 2316 break; 2317 else 2318 goto update; 2319 } 2320 2321 if (zap.za_integer_length != 8 || 2322 zap.za_num_integers != 1) { 2323 cmn_err(CE_WARN, "zap_readdir: bad directory " 2324 "entry, obj = %lld, offset = %lld\n", 2325 (u_longlong_t)zp->z_id, 2326 (u_longlong_t)offset); 2327 error = SET_ERROR(ENXIO); 2328 goto update; 2329 } 2330 2331 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 2332 /* 2333 * MacOS X can extract the object type here such as: 2334 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 2335 */ 2336 2337 if (check_sysattrs && !zap.za_normalization_conflict) { 2338 zap.za_normalization_conflict = 2339 xattr_sysattr_casechk(zap.za_name); 2340 } 2341 } 2342 2343 if (flags & V_RDDIR_ACCFILTER) { 2344 /* 2345 * If we have no access at all, don't include 2346 * this entry in the returned information 2347 */ 2348 znode_t *ezp; 2349 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 2350 goto skip_entry; 2351 if (!zfs_has_access(ezp, cr)) { 2352 VN_RELE(ZTOV(ezp)); 2353 goto skip_entry; 2354 } 2355 VN_RELE(ZTOV(ezp)); 2356 } 2357 2358 if (flags & V_RDDIR_ENTFLAGS) 2359 reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 2360 else 2361 reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2362 2363 /* 2364 * Will this entry fit in the buffer? 2365 */ 2366 if (outcount + reclen > bufsize) { 2367 /* 2368 * Did we manage to fit anything in the buffer? 2369 */ 2370 if (!outcount) { 2371 error = SET_ERROR(EINVAL); 2372 goto update; 2373 } 2374 break; 2375 } 2376 if (flags & V_RDDIR_ENTFLAGS) { 2377 /* 2378 * Add extended flag entry: 2379 */ 2380 eodp->ed_ino = objnum; 2381 eodp->ed_reclen = reclen; 2382 /* NOTE: ed_off is the offset for the *next* entry */ 2383 next = &(eodp->ed_off); 2384 eodp->ed_eflags = zap.za_normalization_conflict ? 2385 ED_CASE_CONFLICT : 0; 2386 (void) strncpy(eodp->ed_name, zap.za_name, 2387 EDIRENT_NAMELEN(reclen)); 2388 eodp = (edirent_t *)((intptr_t)eodp + reclen); 2389 } else { 2390 /* 2391 * Add normal entry: 2392 */ 2393 odp->d_ino = objnum; 2394 odp->d_reclen = reclen; 2395 /* NOTE: d_off is the offset for the *next* entry */ 2396 next = &(odp->d_off); 2397 (void) strncpy(odp->d_name, zap.za_name, 2398 DIRENT64_NAMELEN(reclen)); 2399 odp = (dirent64_t *)((intptr_t)odp + reclen); 2400 } 2401 outcount += reclen; 2402 2403 ASSERT(outcount <= bufsize); 2404 2405 /* Prefetch znode */ 2406 if (prefetch) 2407 dmu_prefetch(os, objnum, 0, 0, 0, 2408 ZIO_PRIORITY_SYNC_READ); 2409 2410 skip_entry: 2411 /* 2412 * Move to the next entry, fill in the previous offset. 2413 */ 2414 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2415 zap_cursor_advance(&zc); 2416 offset = zap_cursor_serialize(&zc); 2417 } else { 2418 offset += 1; 2419 } 2420 if (next) 2421 *next = offset; 2422 } 2423 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2424 2425 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2426 iovp->iov_base += outcount; 2427 iovp->iov_len -= outcount; 2428 uio->uio_resid -= outcount; 2429 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2430 /* 2431 * Reset the pointer. 2432 */ 2433 offset = uio->uio_loffset; 2434 } 2435 2436 update: 2437 zap_cursor_fini(&zc); 2438 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2439 kmem_free(outbuf, bufsize); 2440 2441 if (error == ENOENT) 2442 error = 0; 2443 2444 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2445 2446 uio->uio_loffset = offset; 2447 ZFS_EXIT(zfsvfs); 2448 return (error); 2449 } 2450 2451 ulong_t zfs_fsync_sync_cnt = 4; 2452 2453 static int 2454 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2455 { 2456 znode_t *zp = VTOZ(vp); 2457 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2458 2459 /* 2460 * Regardless of whether this is required for standards conformance, 2461 * this is the logical behavior when fsync() is called on a file with 2462 * dirty pages. We use B_ASYNC since the ZIL transactions are already 2463 * going to be pushed out as part of the zil_commit(). 2464 */ 2465 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 2466 (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 2467 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 2468 2469 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 2470 2471 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 2472 ZFS_ENTER(zfsvfs); 2473 ZFS_VERIFY_ZP(zp); 2474 zil_commit(zfsvfs->z_log, zp->z_id); 2475 ZFS_EXIT(zfsvfs); 2476 } 2477 return (0); 2478 } 2479 2480 2481 /* 2482 * Get the requested file attributes and place them in the provided 2483 * vattr structure. 2484 * 2485 * IN: vp - vnode of file. 2486 * vap - va_mask identifies requested attributes. 2487 * If AT_XVATTR set, then optional attrs are requested 2488 * flags - ATTR_NOACLCHECK (CIFS server context) 2489 * cr - credentials of caller. 2490 * ct - caller context 2491 * 2492 * OUT: vap - attribute values. 2493 * 2494 * RETURN: 0 (always succeeds). 2495 */ 2496 /* ARGSUSED */ 2497 static int 2498 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2499 caller_context_t *ct) 2500 { 2501 znode_t *zp = VTOZ(vp); 2502 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2503 int error = 0; 2504 uint64_t links; 2505 uint64_t mtime[2], ctime[2]; 2506 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2507 xoptattr_t *xoap = NULL; 2508 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2509 sa_bulk_attr_t bulk[2]; 2510 int count = 0; 2511 2512 ZFS_ENTER(zfsvfs); 2513 ZFS_VERIFY_ZP(zp); 2514 2515 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2516 2517 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 2518 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 2519 2520 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 2521 ZFS_EXIT(zfsvfs); 2522 return (error); 2523 } 2524 2525 /* 2526 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 2527 * Also, if we are the owner don't bother, since owner should 2528 * always be allowed to read basic attributes of file. 2529 */ 2530 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && 2531 (vap->va_uid != crgetuid(cr))) { 2532 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 2533 skipaclchk, cr)) { 2534 ZFS_EXIT(zfsvfs); 2535 return (error); 2536 } 2537 } 2538 2539 /* 2540 * Return all attributes. It's cheaper to provide the answer 2541 * than to determine whether we were asked the question. 2542 */ 2543 2544 mutex_enter(&zp->z_lock); 2545 vap->va_type = vp->v_type; 2546 vap->va_mode = zp->z_mode & MODEMASK; 2547 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2548 vap->va_nodeid = zp->z_id; 2549 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 2550 links = zp->z_links + 1; 2551 else 2552 links = zp->z_links; 2553 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2554 vap->va_size = zp->z_size; 2555 vap->va_rdev = vp->v_rdev; 2556 vap->va_seq = zp->z_seq; 2557 2558 /* 2559 * Add in any requested optional attributes and the create time. 2560 * Also set the corresponding bits in the returned attribute bitmap. 2561 */ 2562 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 2563 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 2564 xoap->xoa_archive = 2565 ((zp->z_pflags & ZFS_ARCHIVE) != 0); 2566 XVA_SET_RTN(xvap, XAT_ARCHIVE); 2567 } 2568 2569 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 2570 xoap->xoa_readonly = 2571 ((zp->z_pflags & ZFS_READONLY) != 0); 2572 XVA_SET_RTN(xvap, XAT_READONLY); 2573 } 2574 2575 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 2576 xoap->xoa_system = 2577 ((zp->z_pflags & ZFS_SYSTEM) != 0); 2578 XVA_SET_RTN(xvap, XAT_SYSTEM); 2579 } 2580 2581 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 2582 xoap->xoa_hidden = 2583 ((zp->z_pflags & ZFS_HIDDEN) != 0); 2584 XVA_SET_RTN(xvap, XAT_HIDDEN); 2585 } 2586 2587 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2588 xoap->xoa_nounlink = 2589 ((zp->z_pflags & ZFS_NOUNLINK) != 0); 2590 XVA_SET_RTN(xvap, XAT_NOUNLINK); 2591 } 2592 2593 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2594 xoap->xoa_immutable = 2595 ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 2596 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 2597 } 2598 2599 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2600 xoap->xoa_appendonly = 2601 ((zp->z_pflags & ZFS_APPENDONLY) != 0); 2602 XVA_SET_RTN(xvap, XAT_APPENDONLY); 2603 } 2604 2605 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2606 xoap->xoa_nodump = 2607 ((zp->z_pflags & ZFS_NODUMP) != 0); 2608 XVA_SET_RTN(xvap, XAT_NODUMP); 2609 } 2610 2611 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 2612 xoap->xoa_opaque = 2613 ((zp->z_pflags & ZFS_OPAQUE) != 0); 2614 XVA_SET_RTN(xvap, XAT_OPAQUE); 2615 } 2616 2617 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2618 xoap->xoa_av_quarantined = 2619 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 2620 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 2621 } 2622 2623 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2624 xoap->xoa_av_modified = 2625 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 2626 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 2627 } 2628 2629 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 2630 vp->v_type == VREG) { 2631 zfs_sa_get_scanstamp(zp, xvap); 2632 } 2633 2634 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 2635 uint64_t times[2]; 2636 2637 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 2638 times, sizeof (times)); 2639 ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 2640 XVA_SET_RTN(xvap, XAT_CREATETIME); 2641 } 2642 2643 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2644 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 2645 XVA_SET_RTN(xvap, XAT_REPARSE); 2646 } 2647 if (XVA_ISSET_REQ(xvap, XAT_GEN)) { 2648 xoap->xoa_generation = zp->z_gen; 2649 XVA_SET_RTN(xvap, XAT_GEN); 2650 } 2651 2652 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 2653 xoap->xoa_offline = 2654 ((zp->z_pflags & ZFS_OFFLINE) != 0); 2655 XVA_SET_RTN(xvap, XAT_OFFLINE); 2656 } 2657 2658 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 2659 xoap->xoa_sparse = 2660 ((zp->z_pflags & ZFS_SPARSE) != 0); 2661 XVA_SET_RTN(xvap, XAT_SPARSE); 2662 } 2663 } 2664 2665 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 2666 ZFS_TIME_DECODE(&vap->va_mtime, mtime); 2667 ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2668 2669 mutex_exit(&zp->z_lock); 2670 2671 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); 2672 2673 if (zp->z_blksz == 0) { 2674 /* 2675 * Block size hasn't been set; suggest maximal I/O transfers. 2676 */ 2677 vap->va_blksize = zfsvfs->z_max_blksz; 2678 } 2679 2680 ZFS_EXIT(zfsvfs); 2681 return (0); 2682 } 2683 2684 /* 2685 * Set the file attributes to the values contained in the 2686 * vattr structure. 2687 * 2688 * IN: vp - vnode of file to be modified. 2689 * vap - new attribute values. 2690 * If AT_XVATTR set, then optional attrs are being set 2691 * flags - ATTR_UTIME set if non-default time values provided. 2692 * - ATTR_NOACLCHECK (CIFS context only). 2693 * cr - credentials of caller. 2694 * ct - caller context 2695 * 2696 * RETURN: 0 on success, error code on failure. 2697 * 2698 * Timestamps: 2699 * vp - ctime updated, mtime updated if size changed. 2700 */ 2701 /* ARGSUSED */ 2702 static int 2703 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2704 caller_context_t *ct) 2705 { 2706 znode_t *zp = VTOZ(vp); 2707 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2708 zilog_t *zilog; 2709 dmu_tx_t *tx; 2710 vattr_t oldva; 2711 xvattr_t tmpxvattr; 2712 uint_t mask = vap->va_mask; 2713 uint_t saved_mask = 0; 2714 int trim_mask = 0; 2715 uint64_t new_mode; 2716 uint64_t new_uid, new_gid; 2717 uint64_t xattr_obj; 2718 uint64_t mtime[2], ctime[2]; 2719 znode_t *attrzp; 2720 int need_policy = FALSE; 2721 int err, err2; 2722 zfs_fuid_info_t *fuidp = NULL; 2723 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2724 xoptattr_t *xoap; 2725 zfs_acl_t *aclp; 2726 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2727 boolean_t fuid_dirtied = B_FALSE; 2728 sa_bulk_attr_t bulk[7], xattr_bulk[7]; 2729 int count = 0, xattr_count = 0; 2730 2731 if (mask == 0) 2732 return (0); 2733 2734 if (mask & AT_NOSET) 2735 return (SET_ERROR(EINVAL)); 2736 2737 ZFS_ENTER(zfsvfs); 2738 ZFS_VERIFY_ZP(zp); 2739 2740 zilog = zfsvfs->z_log; 2741 2742 /* 2743 * Make sure that if we have ephemeral uid/gid or xvattr specified 2744 * that file system is at proper version level 2745 */ 2746 2747 if (zfsvfs->z_use_fuids == B_FALSE && 2748 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 2749 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 2750 (mask & AT_XVATTR))) { 2751 ZFS_EXIT(zfsvfs); 2752 return (SET_ERROR(EINVAL)); 2753 } 2754 2755 if (mask & AT_SIZE && vp->v_type == VDIR) { 2756 ZFS_EXIT(zfsvfs); 2757 return (SET_ERROR(EISDIR)); 2758 } 2759 2760 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 2761 ZFS_EXIT(zfsvfs); 2762 return (SET_ERROR(EINVAL)); 2763 } 2764 2765 /* 2766 * If this is an xvattr_t, then get a pointer to the structure of 2767 * optional attributes. If this is NULL, then we have a vattr_t. 2768 */ 2769 xoap = xva_getxoptattr(xvap); 2770 2771 xva_init(&tmpxvattr); 2772 2773 /* 2774 * Immutable files can only alter immutable bit and atime 2775 */ 2776 if ((zp->z_pflags & ZFS_IMMUTABLE) && 2777 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 2778 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 2779 ZFS_EXIT(zfsvfs); 2780 return (SET_ERROR(EPERM)); 2781 } 2782 2783 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 2784 ZFS_EXIT(zfsvfs); 2785 return (SET_ERROR(EPERM)); 2786 } 2787 2788 /* 2789 * Verify timestamps doesn't overflow 32 bits. 2790 * ZFS can handle large timestamps, but 32bit syscalls can't 2791 * handle times greater than 2039. This check should be removed 2792 * once large timestamps are fully supported. 2793 */ 2794 if (mask & (AT_ATIME | AT_MTIME)) { 2795 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 2796 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 2797 ZFS_EXIT(zfsvfs); 2798 return (SET_ERROR(EOVERFLOW)); 2799 } 2800 } 2801 2802 top: 2803 attrzp = NULL; 2804 aclp = NULL; 2805 2806 /* Can this be moved to before the top label? */ 2807 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2808 ZFS_EXIT(zfsvfs); 2809 return (SET_ERROR(EROFS)); 2810 } 2811 2812 /* 2813 * First validate permissions 2814 */ 2815 2816 if (mask & AT_SIZE) { 2817 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2818 if (err) { 2819 ZFS_EXIT(zfsvfs); 2820 return (err); 2821 } 2822 /* 2823 * XXX - Note, we are not providing any open 2824 * mode flags here (like FNDELAY), so we may 2825 * block if there are locks present... this 2826 * should be addressed in openat(). 2827 */ 2828 /* XXX - would it be OK to generate a log record here? */ 2829 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 2830 if (err) { 2831 ZFS_EXIT(zfsvfs); 2832 return (err); 2833 } 2834 2835 if (vap->va_size == 0) 2836 vnevent_truncate(ZTOV(zp), ct); 2837 } 2838 2839 if (mask & (AT_ATIME|AT_MTIME) || 2840 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 2841 XVA_ISSET_REQ(xvap, XAT_READONLY) || 2842 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 2843 XVA_ISSET_REQ(xvap, XAT_OFFLINE) || 2844 XVA_ISSET_REQ(xvap, XAT_SPARSE) || 2845 XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 2846 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 2847 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 2848 skipaclchk, cr); 2849 } 2850 2851 if (mask & (AT_UID|AT_GID)) { 2852 int idmask = (mask & (AT_UID|AT_GID)); 2853 int take_owner; 2854 int take_group; 2855 2856 /* 2857 * NOTE: even if a new mode is being set, 2858 * we may clear S_ISUID/S_ISGID bits. 2859 */ 2860 2861 if (!(mask & AT_MODE)) 2862 vap->va_mode = zp->z_mode; 2863 2864 /* 2865 * Take ownership or chgrp to group we are a member of 2866 */ 2867 2868 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2869 take_group = (mask & AT_GID) && 2870 zfs_groupmember(zfsvfs, vap->va_gid, cr); 2871 2872 /* 2873 * If both AT_UID and AT_GID are set then take_owner and 2874 * take_group must both be set in order to allow taking 2875 * ownership. 2876 * 2877 * Otherwise, send the check through secpolicy_vnode_setattr() 2878 * 2879 */ 2880 2881 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2882 ((idmask == AT_UID) && take_owner) || 2883 ((idmask == AT_GID) && take_group)) { 2884 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 2885 skipaclchk, cr) == 0) { 2886 /* 2887 * Remove setuid/setgid for non-privileged users 2888 */ 2889 secpolicy_setid_clear(vap, cr); 2890 trim_mask = (mask & (AT_UID|AT_GID)); 2891 } else { 2892 need_policy = TRUE; 2893 } 2894 } else { 2895 need_policy = TRUE; 2896 } 2897 } 2898 2899 mutex_enter(&zp->z_lock); 2900 oldva.va_mode = zp->z_mode; 2901 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 2902 if (mask & AT_XVATTR) { 2903 /* 2904 * Update xvattr mask to include only those attributes 2905 * that are actually changing. 2906 * 2907 * the bits will be restored prior to actually setting 2908 * the attributes so the caller thinks they were set. 2909 */ 2910 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2911 if (xoap->xoa_appendonly != 2912 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 2913 need_policy = TRUE; 2914 } else { 2915 XVA_CLR_REQ(xvap, XAT_APPENDONLY); 2916 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 2917 } 2918 } 2919 2920 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2921 if (xoap->xoa_nounlink != 2922 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 2923 need_policy = TRUE; 2924 } else { 2925 XVA_CLR_REQ(xvap, XAT_NOUNLINK); 2926 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 2927 } 2928 } 2929 2930 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2931 if (xoap->xoa_immutable != 2932 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 2933 need_policy = TRUE; 2934 } else { 2935 XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 2936 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 2937 } 2938 } 2939 2940 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2941 if (xoap->xoa_nodump != 2942 ((zp->z_pflags & ZFS_NODUMP) != 0)) { 2943 need_policy = TRUE; 2944 } else { 2945 XVA_CLR_REQ(xvap, XAT_NODUMP); 2946 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 2947 } 2948 } 2949 2950 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2951 if (xoap->xoa_av_modified != 2952 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 2953 need_policy = TRUE; 2954 } else { 2955 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 2956 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 2957 } 2958 } 2959 2960 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2961 if ((vp->v_type != VREG && 2962 xoap->xoa_av_quarantined) || 2963 xoap->xoa_av_quarantined != 2964 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 2965 need_policy = TRUE; 2966 } else { 2967 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 2968 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 2969 } 2970 } 2971 2972 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2973 mutex_exit(&zp->z_lock); 2974 ZFS_EXIT(zfsvfs); 2975 return (SET_ERROR(EPERM)); 2976 } 2977 2978 if (need_policy == FALSE && 2979 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 2980 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 2981 need_policy = TRUE; 2982 } 2983 } 2984 2985 mutex_exit(&zp->z_lock); 2986 2987 if (mask & AT_MODE) { 2988 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 2989 err = secpolicy_setid_setsticky_clear(vp, vap, 2990 &oldva, cr); 2991 if (err) { 2992 ZFS_EXIT(zfsvfs); 2993 return (err); 2994 } 2995 trim_mask |= AT_MODE; 2996 } else { 2997 need_policy = TRUE; 2998 } 2999 } 3000 3001 if (need_policy) { 3002 /* 3003 * If trim_mask is set then take ownership 3004 * has been granted or write_acl is present and user 3005 * has the ability to modify mode. In that case remove 3006 * UID|GID and or MODE from mask so that 3007 * secpolicy_vnode_setattr() doesn't revoke it. 3008 */ 3009 3010 if (trim_mask) { 3011 saved_mask = vap->va_mask; 3012 vap->va_mask &= ~trim_mask; 3013 } 3014 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 3015 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 3016 if (err) { 3017 ZFS_EXIT(zfsvfs); 3018 return (err); 3019 } 3020 3021 if (trim_mask) 3022 vap->va_mask |= saved_mask; 3023 } 3024 3025 /* 3026 * secpolicy_vnode_setattr, or take ownership may have 3027 * changed va_mask 3028 */ 3029 mask = vap->va_mask; 3030 3031 if ((mask & (AT_UID | AT_GID))) { 3032 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 3033 &xattr_obj, sizeof (xattr_obj)); 3034 3035 if (err == 0 && xattr_obj) { 3036 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 3037 if (err) 3038 goto out2; 3039 } 3040 if (mask & AT_UID) { 3041 new_uid = zfs_fuid_create(zfsvfs, 3042 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 3043 if (new_uid != zp->z_uid && 3044 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 3045 if (attrzp) 3046 VN_RELE(ZTOV(attrzp)); 3047 err = SET_ERROR(EDQUOT); 3048 goto out2; 3049 } 3050 } 3051 3052 if (mask & AT_GID) { 3053 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 3054 cr, ZFS_GROUP, &fuidp); 3055 if (new_gid != zp->z_gid && 3056 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 3057 if (attrzp) 3058 VN_RELE(ZTOV(attrzp)); 3059 err = SET_ERROR(EDQUOT); 3060 goto out2; 3061 } 3062 } 3063 } 3064 tx = dmu_tx_create(zfsvfs->z_os); 3065 3066 if (mask & AT_MODE) { 3067 uint64_t pmode = zp->z_mode; 3068 uint64_t acl_obj; 3069 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 3070 3071 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED && 3072 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) { 3073 err = SET_ERROR(EPERM); 3074 goto out; 3075 } 3076 3077 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 3078 goto out; 3079 3080 mutex_enter(&zp->z_lock); 3081 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 3082 /* 3083 * Are we upgrading ACL from old V0 format 3084 * to V1 format? 3085 */ 3086 if (zfsvfs->z_version >= ZPL_VERSION_FUID && 3087 zfs_znode_acl_version(zp) == 3088 ZFS_ACL_VERSION_INITIAL) { 3089 dmu_tx_hold_free(tx, acl_obj, 0, 3090 DMU_OBJECT_END); 3091 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3092 0, aclp->z_acl_bytes); 3093 } else { 3094 dmu_tx_hold_write(tx, acl_obj, 0, 3095 aclp->z_acl_bytes); 3096 } 3097 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3098 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3099 0, aclp->z_acl_bytes); 3100 } 3101 mutex_exit(&zp->z_lock); 3102 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3103 } else { 3104 if ((mask & AT_XVATTR) && 3105 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3106 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3107 else 3108 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3109 } 3110 3111 if (attrzp) { 3112 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 3113 } 3114 3115 fuid_dirtied = zfsvfs->z_fuid_dirty; 3116 if (fuid_dirtied) 3117 zfs_fuid_txhold(zfsvfs, tx); 3118 3119 zfs_sa_upgrade_txholds(tx, zp); 3120 3121 err = dmu_tx_assign(tx, TXG_WAIT); 3122 if (err) 3123 goto out; 3124 3125 count = 0; 3126 /* 3127 * Set each attribute requested. 3128 * We group settings according to the locks they need to acquire. 3129 * 3130 * Note: you cannot set ctime directly, although it will be 3131 * updated as a side-effect of calling this function. 3132 */ 3133 3134 3135 if (mask & (AT_UID|AT_GID|AT_MODE)) 3136 mutex_enter(&zp->z_acl_lock); 3137 mutex_enter(&zp->z_lock); 3138 3139 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 3140 &zp->z_pflags, sizeof (zp->z_pflags)); 3141 3142 if (attrzp) { 3143 if (mask & (AT_UID|AT_GID|AT_MODE)) 3144 mutex_enter(&attrzp->z_acl_lock); 3145 mutex_enter(&attrzp->z_lock); 3146 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3147 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 3148 sizeof (attrzp->z_pflags)); 3149 } 3150 3151 if (mask & (AT_UID|AT_GID)) { 3152 3153 if (mask & AT_UID) { 3154 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 3155 &new_uid, sizeof (new_uid)); 3156 zp->z_uid = new_uid; 3157 if (attrzp) { 3158 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3159 SA_ZPL_UID(zfsvfs), NULL, &new_uid, 3160 sizeof (new_uid)); 3161 attrzp->z_uid = new_uid; 3162 } 3163 } 3164 3165 if (mask & AT_GID) { 3166 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 3167 NULL, &new_gid, sizeof (new_gid)); 3168 zp->z_gid = new_gid; 3169 if (attrzp) { 3170 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3171 SA_ZPL_GID(zfsvfs), NULL, &new_gid, 3172 sizeof (new_gid)); 3173 attrzp->z_gid = new_gid; 3174 } 3175 } 3176 if (!(mask & AT_MODE)) { 3177 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 3178 NULL, &new_mode, sizeof (new_mode)); 3179 new_mode = zp->z_mode; 3180 } 3181 err = zfs_acl_chown_setattr(zp); 3182 ASSERT(err == 0); 3183 if (attrzp) { 3184 err = zfs_acl_chown_setattr(attrzp); 3185 ASSERT(err == 0); 3186 } 3187 } 3188 3189 if (mask & AT_MODE) { 3190 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 3191 &new_mode, sizeof (new_mode)); 3192 zp->z_mode = new_mode; 3193 ASSERT3U((uintptr_t)aclp, !=, NULL); 3194 err = zfs_aclset_common(zp, aclp, cr, tx); 3195 ASSERT0(err); 3196 if (zp->z_acl_cached) 3197 zfs_acl_free(zp->z_acl_cached); 3198 zp->z_acl_cached = aclp; 3199 aclp = NULL; 3200 } 3201 3202 3203 if (mask & AT_ATIME) { 3204 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 3205 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 3206 &zp->z_atime, sizeof (zp->z_atime)); 3207 } 3208 3209 if (mask & AT_MTIME) { 3210 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 3211 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 3212 mtime, sizeof (mtime)); 3213 } 3214 3215 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 3216 if (mask & AT_SIZE && !(mask & AT_MTIME)) { 3217 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 3218 NULL, mtime, sizeof (mtime)); 3219 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3220 &ctime, sizeof (ctime)); 3221 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 3222 B_TRUE); 3223 } else if (mask != 0) { 3224 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3225 &ctime, sizeof (ctime)); 3226 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 3227 B_TRUE); 3228 if (attrzp) { 3229 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3230 SA_ZPL_CTIME(zfsvfs), NULL, 3231 &ctime, sizeof (ctime)); 3232 zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 3233 mtime, ctime, B_TRUE); 3234 } 3235 } 3236 /* 3237 * Do this after setting timestamps to prevent timestamp 3238 * update from toggling bit 3239 */ 3240 3241 if (xoap && (mask & AT_XVATTR)) { 3242 3243 /* 3244 * restore trimmed off masks 3245 * so that return masks can be set for caller. 3246 */ 3247 3248 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 3249 XVA_SET_REQ(xvap, XAT_APPENDONLY); 3250 } 3251 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 3252 XVA_SET_REQ(xvap, XAT_NOUNLINK); 3253 } 3254 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 3255 XVA_SET_REQ(xvap, XAT_IMMUTABLE); 3256 } 3257 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 3258 XVA_SET_REQ(xvap, XAT_NODUMP); 3259 } 3260 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 3261 XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 3262 } 3263 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 3264 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 3265 } 3266 3267 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3268 ASSERT(vp->v_type == VREG); 3269 3270 zfs_xvattr_set(zp, xvap, tx); 3271 } 3272 3273 if (fuid_dirtied) 3274 zfs_fuid_sync(zfsvfs, tx); 3275 3276 if (mask != 0) 3277 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 3278 3279 mutex_exit(&zp->z_lock); 3280 if (mask & (AT_UID|AT_GID|AT_MODE)) 3281 mutex_exit(&zp->z_acl_lock); 3282 3283 if (attrzp) { 3284 if (mask & (AT_UID|AT_GID|AT_MODE)) 3285 mutex_exit(&attrzp->z_acl_lock); 3286 mutex_exit(&attrzp->z_lock); 3287 } 3288 out: 3289 if (err == 0 && attrzp) { 3290 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 3291 xattr_count, tx); 3292 ASSERT(err2 == 0); 3293 } 3294 3295 if (attrzp) 3296 VN_RELE(ZTOV(attrzp)); 3297 3298 if (aclp) 3299 zfs_acl_free(aclp); 3300 3301 if (fuidp) { 3302 zfs_fuid_info_free(fuidp); 3303 fuidp = NULL; 3304 } 3305 3306 if (err) { 3307 dmu_tx_abort(tx); 3308 if (err == ERESTART) 3309 goto top; 3310 } else { 3311 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 3312 dmu_tx_commit(tx); 3313 } 3314 3315 out2: 3316 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3317 zil_commit(zilog, 0); 3318 3319 ZFS_EXIT(zfsvfs); 3320 return (err); 3321 } 3322 3323 typedef struct zfs_zlock { 3324 krwlock_t *zl_rwlock; /* lock we acquired */ 3325 znode_t *zl_znode; /* znode we held */ 3326 struct zfs_zlock *zl_next; /* next in list */ 3327 } zfs_zlock_t; 3328 3329 /* 3330 * Drop locks and release vnodes that were held by zfs_rename_lock(). 3331 */ 3332 static void 3333 zfs_rename_unlock(zfs_zlock_t **zlpp) 3334 { 3335 zfs_zlock_t *zl; 3336 3337 while ((zl = *zlpp) != NULL) { 3338 if (zl->zl_znode != NULL) 3339 VN_RELE(ZTOV(zl->zl_znode)); 3340