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