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