1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 */ 27 28 /* Portions Copyright 2007 Jeremy Teo */ 29 /* Portions Copyright 2010 Robert Milkowski */ 30 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/systm.h> 35 #include <sys/sysmacros.h> 36 #include <sys/resource.h> 37 #include <sys/vfs.h> 38 #include <sys/vfs_opreg.h> 39 #include <sys/vnode.h> 40 #include <sys/file.h> 41 #include <sys/stat.h> 42 #include <sys/kmem.h> 43 #include <sys/taskq.h> 44 #include <sys/uio.h> 45 #include <sys/vmsystm.h> 46 #include <sys/atomic.h> 47 #include <sys/vm.h> 48 #include <vm/seg_vn.h> 49 #include <vm/pvn.h> 50 #include <vm/as.h> 51 #include <vm/kpm.h> 52 #include <vm/seg_kpm.h> 53 #include <sys/mman.h> 54 #include <sys/pathname.h> 55 #include <sys/cmn_err.h> 56 #include <sys/errno.h> 57 #include <sys/unistd.h> 58 #include <sys/zfs_dir.h> 59 #include <sys/zfs_acl.h> 60 #include <sys/zfs_ioctl.h> 61 #include <sys/fs/zfs.h> 62 #include <sys/dmu.h> 63 #include <sys/dmu_objset.h> 64 #include <sys/spa.h> 65 #include <sys/txg.h> 66 #include <sys/dbuf.h> 67 #include <sys/zap.h> 68 #include <sys/sa.h> 69 #include <sys/dirent.h> 70 #include <sys/policy.h> 71 #include <sys/sunddi.h> 72 #include <sys/filio.h> 73 #include <sys/sid.h> 74 #include "fs/fs_subr.h" 75 #include <sys/zfs_ctldir.h> 76 #include <sys/zfs_fuid.h> 77 #include <sys/zfs_sa.h> 78 #include <sys/dnlc.h> 79 #include <sys/zfs_rlock.h> 80 #include <sys/extdirent.h> 81 #include <sys/kidmap.h> 82 #include <sys/cred.h> 83 #include <sys/attr.h> 84 #include <sys/zfs_events.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 space 1766 */ 1767 dmu_tx_mark_netfree(tx); 1768 1769 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1770 if (error) { 1771 zfs_dirent_unlock(dl); 1772 VN_RELE(vp); 1773 if (xzp) 1774 VN_RELE(ZTOV(xzp)); 1775 if (error == ERESTART) { 1776 waited = B_TRUE; 1777 dmu_tx_wait(tx); 1778 dmu_tx_abort(tx); 1779 goto top; 1780 } 1781 if (realnmp) 1782 pn_free(realnmp); 1783 dmu_tx_abort(tx); 1784 ZFS_EXIT(zfsvfs); 1785 return (error); 1786 } 1787 1788 /* 1789 * Remove the directory entry. 1790 */ 1791 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1792 1793 if (error) { 1794 dmu_tx_commit(tx); 1795 goto out; 1796 } 1797 1798 if (unlinked) { 1799 /* 1800 * Hold z_lock so that we can make sure that the ACL obj 1801 * hasn't changed. Could have been deleted due to 1802 * zfs_sa_upgrade(). 1803 */ 1804 mutex_enter(&zp->z_lock); 1805 mutex_enter(&vp->v_lock); 1806 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1807 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 1808 delete_now = may_delete_now && !toobig && 1809 vp->v_count == 1 && !vn_has_cached_data(vp) && 1810 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 1811 acl_obj; 1812 mutex_exit(&vp->v_lock); 1813 } 1814 1815 txtype = TX_REMOVE; 1816 if (flags & FIGNORECASE) 1817 txtype |= TX_CI; 1818 rw_enter(&rz_zev_rwlock, RW_READER); 1819 if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_znode_remove) 1820 rz_zev_callbacks->rz_zev_znode_remove(dzp, zp, name, txtype); 1821 rw_exit(&rz_zev_rwlock); 1822 1823 if (delete_now) { 1824 if (xattr_obj_unlinked) { 1825 ASSERT3U(xzp->z_links, ==, 2); 1826 mutex_enter(&xzp->z_lock); 1827 xzp->z_unlinked = 1; 1828 xzp->z_links = 0; 1829 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 1830 &xzp->z_links, sizeof (xzp->z_links), tx); 1831 ASSERT3U(error, ==, 0); 1832 mutex_exit(&xzp->z_lock); 1833 zfs_unlinked_add(xzp, tx); 1834 1835 if (zp->z_is_sa) 1836 error = sa_remove(zp->z_sa_hdl, 1837 SA_ZPL_XATTR(zfsvfs), tx); 1838 else 1839 error = sa_update(zp->z_sa_hdl, 1840 SA_ZPL_XATTR(zfsvfs), &null_xattr, 1841 sizeof (uint64_t), tx); 1842 ASSERT0(error); 1843 } 1844 mutex_enter(&vp->v_lock); 1845 vp->v_count--; 1846 ASSERT0(vp->v_count); 1847 mutex_exit(&vp->v_lock); 1848 mutex_exit(&zp->z_lock); 1849 zfs_znode_delete(zp, tx); 1850 } else if (unlinked) { 1851 mutex_exit(&zp->z_lock); 1852 zfs_unlinked_add(zp, tx); 1853 } 1854 1855 txtype = TX_REMOVE; 1856 if (flags & FIGNORECASE) 1857 txtype |= TX_CI; 1858 zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 1859 1860 dmu_tx_commit(tx); 1861 out: 1862 if (realnmp) 1863 pn_free(realnmp); 1864 1865 zfs_dirent_unlock(dl); 1866 1867 if (!delete_now) 1868 VN_RELE(vp); 1869 if (xzp) 1870 VN_RELE(ZTOV(xzp)); 1871 1872 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1873 zil_commit(zilog, 0); 1874 1875 ZFS_EXIT(zfsvfs); 1876 return (error); 1877 } 1878 1879 /* 1880 * Create a new directory and insert it into dvp using the name 1881 * provided. Return a pointer to the inserted directory. 1882 * 1883 * IN: dvp - vnode of directory to add subdir to. 1884 * dirname - name of new directory. 1885 * vap - attributes of new directory. 1886 * cr - credentials of caller. 1887 * ct - caller context 1888 * flags - case flags 1889 * vsecp - ACL to be set 1890 * 1891 * OUT: vpp - vnode of created directory. 1892 * 1893 * RETURN: 0 on success, error code on failure. 1894 * 1895 * Timestamps: 1896 * dvp - ctime|mtime updated 1897 * vp - ctime|mtime|atime updated 1898 */ 1899 /*ARGSUSED*/ 1900 static int 1901 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 1902 caller_context_t *ct, int flags, vsecattr_t *vsecp) 1903 { 1904 znode_t *zp, *dzp = VTOZ(dvp); 1905 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1906 zilog_t *zilog; 1907 zfs_dirlock_t *dl; 1908 uint64_t txtype; 1909 dmu_tx_t *tx; 1910 int error; 1911 int zf = ZNEW; 1912 ksid_t *ksid; 1913 uid_t uid; 1914 gid_t gid = crgetgid(cr); 1915 zfs_acl_ids_t acl_ids; 1916 boolean_t fuid_dirtied; 1917 boolean_t waited = B_FALSE; 1918 1919 ASSERT(vap->va_type == VDIR); 1920 1921 /* 1922 * If we have an ephemeral id, ACL, or XVATTR then 1923 * make sure file system is at proper version 1924 */ 1925 1926 ksid = crgetsid(cr, KSID_OWNER); 1927 if (ksid) 1928 uid = ksid_getid(ksid); 1929 else 1930 uid = crgetuid(cr); 1931 if (zfsvfs->z_use_fuids == B_FALSE && 1932 (vsecp || (vap->va_mask & AT_XVATTR) || 1933 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1934 return (SET_ERROR(EINVAL)); 1935 1936 ZFS_ENTER(zfsvfs); 1937 ZFS_VERIFY_ZP(dzp); 1938 zilog = zfsvfs->z_log; 1939 1940 if (dzp->z_pflags & ZFS_XATTR) { 1941 ZFS_EXIT(zfsvfs); 1942 return (SET_ERROR(EINVAL)); 1943 } 1944 1945 if (zfsvfs->z_utf8 && u8_validate(dirname, 1946 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1947 ZFS_EXIT(zfsvfs); 1948 return (SET_ERROR(EILSEQ)); 1949 } 1950 if (flags & FIGNORECASE) 1951 zf |= ZCILOOK; 1952 1953 if (vap->va_mask & AT_XVATTR) { 1954 if ((error = secpolicy_xvattr((xvattr_t *)vap, 1955 crgetuid(cr), cr, vap->va_type)) != 0) { 1956 ZFS_EXIT(zfsvfs); 1957 return (error); 1958 } 1959 } 1960 1961 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 1962 vsecp, &acl_ids)) != 0) { 1963 ZFS_EXIT(zfsvfs); 1964 return (error); 1965 } 1966 /* 1967 * First make sure the new directory doesn't exist. 1968 * 1969 * Existence is checked first to make sure we don't return 1970 * EACCES instead of EEXIST which can cause some applications 1971 * to fail. 1972 */ 1973 top: 1974 *vpp = NULL; 1975 1976 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 1977 NULL, NULL)) { 1978 zfs_acl_ids_free(&acl_ids); 1979 ZFS_EXIT(zfsvfs); 1980 return (error); 1981 } 1982 1983 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 1984 zfs_acl_ids_free(&acl_ids); 1985 zfs_dirent_unlock(dl); 1986 ZFS_EXIT(zfsvfs); 1987 return (error); 1988 } 1989 1990 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 1991 zfs_acl_ids_free(&acl_ids); 1992 zfs_dirent_unlock(dl); 1993 ZFS_EXIT(zfsvfs); 1994 return (SET_ERROR(EDQUOT)); 1995 } 1996 1997 /* 1998 * Add a new entry to the directory. 1999 */ 2000 tx = dmu_tx_create(zfsvfs->z_os); 2001 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 2002 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 2003 fuid_dirtied = zfsvfs->z_fuid_dirty; 2004 if (fuid_dirtied) 2005 zfs_fuid_txhold(zfsvfs, tx); 2006 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 2007 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 2008 acl_ids.z_aclp->z_acl_bytes); 2009 } 2010 2011 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 2012 ZFS_SA_BASE_ATTR_SIZE); 2013 2014 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2015 if (error) { 2016 zfs_dirent_unlock(dl); 2017 if (error == ERESTART) { 2018 waited = B_TRUE; 2019 dmu_tx_wait(tx); 2020 dmu_tx_abort(tx); 2021 goto top; 2022 } 2023 zfs_acl_ids_free(&acl_ids); 2024 dmu_tx_abort(tx); 2025 ZFS_EXIT(zfsvfs); 2026 return (error); 2027 } 2028 2029 /* 2030 * Create new node. 2031 */ 2032 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 2033 2034 if (fuid_dirtied) 2035 zfs_fuid_sync(zfsvfs, tx); 2036 2037 /* 2038 * Now put new name in parent dir. 2039 */ 2040 (void) zfs_link_create(dl, zp, tx, ZNEW); 2041 2042 *vpp = ZTOV(zp); 2043 2044 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 2045 if (flags & FIGNORECASE) 2046 txtype |= TX_CI; 2047 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 2048 acl_ids.z_fuidp, vap); 2049 2050 zfs_acl_ids_free(&acl_ids); 2051 2052 dmu_tx_commit(tx); 2053 2054 zfs_dirent_unlock(dl); 2055 2056 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2057 zil_commit(zilog, 0); 2058 2059 ZFS_EXIT(zfsvfs); 2060 return (0); 2061 } 2062 2063 /* 2064 * Remove a directory subdir entry. If the current working 2065 * directory is the same as the subdir to be removed, the 2066 * remove will fail. 2067 * 2068 * IN: dvp - vnode of directory to remove from. 2069 * name - name of directory to be removed. 2070 * cwd - vnode of current working directory. 2071 * cr - credentials of caller. 2072 * ct - caller context 2073 * flags - case flags 2074 * 2075 * RETURN: 0 on success, error code on failure. 2076 * 2077 * Timestamps: 2078 * dvp - ctime|mtime updated 2079 */ 2080 /*ARGSUSED*/ 2081 static int 2082 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 2083 caller_context_t *ct, int flags) 2084 { 2085 znode_t *dzp = VTOZ(dvp); 2086 znode_t *zp; 2087 vnode_t *vp; 2088 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2089 zilog_t *zilog; 2090 zfs_dirlock_t *dl; 2091 dmu_tx_t *tx; 2092 int error; 2093 int zflg = ZEXISTS; 2094 boolean_t waited = B_FALSE; 2095 2096 ZFS_ENTER(zfsvfs); 2097 ZFS_VERIFY_ZP(dzp); 2098 zilog = zfsvfs->z_log; 2099 2100 if (flags & FIGNORECASE) 2101 zflg |= ZCILOOK; 2102 top: 2103 zp = NULL; 2104 2105 /* 2106 * Attempt to lock directory; fail if entry doesn't exist. 2107 */ 2108 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 2109 NULL, NULL)) { 2110 ZFS_EXIT(zfsvfs); 2111 return (error); 2112 } 2113 2114 vp = ZTOV(zp); 2115 2116 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 2117 goto out; 2118 } 2119 2120 if (vp->v_type != VDIR) { 2121 error = SET_ERROR(ENOTDIR); 2122 goto out; 2123 } 2124 2125 if (vp == cwd) { 2126 error = SET_ERROR(EINVAL); 2127 goto out; 2128 } 2129 2130 vnevent_rmdir(vp, dvp, name, ct); 2131 2132 /* 2133 * Grab a lock on the directory to make sure that noone is 2134 * trying to add (or lookup) entries while we are removing it. 2135 */ 2136 rw_enter(&zp->z_name_lock, RW_WRITER); 2137 2138 /* 2139 * Grab a lock on the parent pointer to make sure we play well 2140 * with the treewalk and directory rename code. 2141 */ 2142 rw_enter(&zp->z_parent_lock, RW_WRITER); 2143 2144 tx = dmu_tx_create(zfsvfs->z_os); 2145 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 2146 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 2147 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2148 zfs_sa_upgrade_txholds(tx, zp); 2149 zfs_sa_upgrade_txholds(tx, dzp); 2150 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2151 if (error) { 2152 rw_exit(&zp->z_parent_lock); 2153 rw_exit(&zp->z_name_lock); 2154 zfs_dirent_unlock(dl); 2155 VN_RELE(vp); 2156 if (error == ERESTART) { 2157 waited = B_TRUE; 2158 dmu_tx_wait(tx); 2159 dmu_tx_abort(tx); 2160 goto top; 2161 } 2162 dmu_tx_abort(tx); 2163 ZFS_EXIT(zfsvfs); 2164 return (error); 2165 } 2166 2167 error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 2168 2169 if (error == 0) { 2170 uint64_t txtype = TX_RMDIR; 2171 if (flags & FIGNORECASE) 2172 txtype |= TX_CI; 2173 2174 rw_enter(&rz_zev_rwlock, RW_READER); 2175 if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_znode_remove) 2176 rz_zev_callbacks->rz_zev_znode_remove(dzp, zp, 2177 name, txtype); 2178 rw_exit(&rz_zev_rwlock); 2179 2180 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 2181 } 2182 2183 dmu_tx_commit(tx); 2184 2185 rw_exit(&zp->z_parent_lock); 2186 rw_exit(&zp->z_name_lock); 2187 out: 2188 zfs_dirent_unlock(dl); 2189 2190 VN_RELE(vp); 2191 2192 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2193 zil_commit(zilog, 0); 2194 2195 ZFS_EXIT(zfsvfs); 2196 return (error); 2197 } 2198 2199 /* 2200 * Read as many directory entries as will fit into the provided 2201 * buffer from the given directory cursor position (specified in 2202 * the uio structure). 2203 * 2204 * IN: vp - vnode of directory to read. 2205 * uio - structure supplying read location, range info, 2206 * and return buffer. 2207 * cr - credentials of caller. 2208 * ct - caller context 2209 * flags - case flags 2210 * 2211 * OUT: uio - updated offset and range, buffer filled. 2212 * eofp - set to true if end-of-file detected. 2213 * 2214 * RETURN: 0 on success, error code on failure. 2215 * 2216 * Timestamps: 2217 * vp - atime updated 2218 * 2219 * Note that the low 4 bits of the cookie returned by zap is always zero. 2220 * This allows us to use the low range for "special" directory entries: 2221 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2222 * we use the offset 2 for the '.zfs' directory. 2223 */ 2224 /* ARGSUSED */ 2225 static int 2226 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 2227 caller_context_t *ct, int flags) 2228 { 2229 znode_t *zp = VTOZ(vp); 2230 iovec_t *iovp; 2231 edirent_t *eodp; 2232 dirent64_t *odp; 2233 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2234 objset_t *os; 2235 caddr_t outbuf; 2236 size_t bufsize; 2237 zap_cursor_t zc; 2238 zap_attribute_t zap; 2239 uint_t bytes_wanted; 2240 uint64_t offset; /* must be unsigned; checks for < 1 */ 2241 uint64_t parent; 2242 int local_eof; 2243 int outcount; 2244 int error; 2245 uint8_t prefetch; 2246 boolean_t check_sysattrs; 2247 2248 ZFS_ENTER(zfsvfs); 2249 ZFS_VERIFY_ZP(zp); 2250 2251 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 2252 &parent, sizeof (parent))) != 0) { 2253 ZFS_EXIT(zfsvfs); 2254 return (error); 2255 } 2256 2257 /* 2258 * If we are not given an eof variable, 2259 * use a local one. 2260 */ 2261 if (eofp == NULL) 2262 eofp = &local_eof; 2263 2264 /* 2265 * Check for valid iov_len. 2266 */ 2267 if (uio->uio_iov->iov_len <= 0) { 2268 ZFS_EXIT(zfsvfs); 2269 return (SET_ERROR(EINVAL)); 2270 } 2271 2272 /* 2273 * Quit if directory has been removed (posix) 2274 */ 2275 if ((*eofp = zp->z_unlinked) != 0) { 2276 ZFS_EXIT(zfsvfs); 2277 return (0); 2278 } 2279 2280 error = 0; 2281 os = zfsvfs->z_os; 2282 offset = uio->uio_loffset; 2283 prefetch = zp->z_zn_prefetch; 2284 2285 /* 2286 * Initialize the iterator cursor. 2287 */ 2288 if (offset <= 3) { 2289 /* 2290 * Start iteration from the beginning of the directory. 2291 */ 2292 zap_cursor_init(&zc, os, zp->z_id); 2293 } else { 2294 /* 2295 * The offset is a serialized cursor. 2296 */ 2297 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2298 } 2299 2300 /* 2301 * Get space to change directory entries into fs independent format. 2302 */ 2303 iovp = uio->uio_iov; 2304 bytes_wanted = iovp->iov_len; 2305 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2306 bufsize = bytes_wanted; 2307 outbuf = kmem_alloc(bufsize, KM_SLEEP); 2308 odp = (struct dirent64 *)outbuf; 2309 } else { 2310 bufsize = bytes_wanted; 2311 outbuf = NULL; 2312 odp = (struct dirent64 *)iovp->iov_base; 2313 } 2314 eodp = (struct edirent *)odp; 2315 2316 /* 2317 * If this VFS supports the system attribute view interface; and 2318 * we're looking at an extended attribute directory; and we care 2319 * about normalization conflicts on this vfs; then we must check 2320 * for normalization conflicts with the sysattr name space. 2321 */ 2322 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 2323 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 2324 (flags & V_RDDIR_ENTFLAGS); 2325 2326 /* 2327 * Transform to file-system independent format 2328 */ 2329 outcount = 0; 2330 while (outcount < bytes_wanted) { 2331 ino64_t objnum; 2332 ushort_t reclen; 2333 off64_t *next = NULL; 2334 2335 /* 2336 * Special case `.', `..', and `.zfs'. 2337 */ 2338 if (offset == 0) { 2339 (void) strcpy(zap.za_name, "."); 2340 zap.za_normalization_conflict = 0; 2341 objnum = zp->z_id; 2342 } else if (offset == 1) { 2343 (void) strcpy(zap.za_name, ".."); 2344 zap.za_normalization_conflict = 0; 2345 objnum = parent; 2346 } else if (offset == 2 && zfs_show_ctldir(zp)) { 2347 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 2348 zap.za_normalization_conflict = 0; 2349 objnum = ZFSCTL_INO_ROOT; 2350 } else { 2351 /* 2352 * Grab next entry. 2353 */ 2354 if (error = zap_cursor_retrieve(&zc, &zap)) { 2355 if ((*eofp = (error == ENOENT)) != 0) 2356 break; 2357 else 2358 goto update; 2359 } 2360 2361 if (zap.za_integer_length != 8 || 2362 zap.za_num_integers != 1) { 2363 cmn_err(CE_WARN, "zap_readdir: bad directory " 2364 "entry, obj = %lld, offset = %lld\n", 2365 (u_longlong_t)zp->z_id, 2366 (u_longlong_t)offset); 2367 error = SET_ERROR(ENXIO); 2368 goto update; 2369 } 2370 2371 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 2372 /* 2373 * MacOS X can extract the object type here such as: 2374 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 2375 */ 2376 2377 if (check_sysattrs && !zap.za_normalization_conflict) { 2378 zap.za_normalization_conflict = 2379 xattr_sysattr_casechk(zap.za_name); 2380 } 2381 } 2382 2383 if (flags & V_RDDIR_ACCFILTER) { 2384 /* 2385 * If we have no access at all, don't include 2386 * this entry in the returned information 2387 */ 2388 znode_t *ezp; 2389 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 2390 goto skip_entry; 2391 if (!zfs_has_access(ezp, cr)) { 2392 VN_RELE(ZTOV(ezp)); 2393 goto skip_entry; 2394 } 2395 VN_RELE(ZTOV(ezp)); 2396 } 2397 2398 if (flags & V_RDDIR_ENTFLAGS) 2399 reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 2400 else 2401 reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2402 2403 /* 2404 * Will this entry fit in the buffer? 2405 */ 2406 if (outcount + reclen > bufsize) { 2407 /* 2408 * Did we manage to fit anything in the buffer? 2409 */ 2410 if (!outcount) { 2411 error = SET_ERROR(EINVAL); 2412 goto update; 2413 } 2414 break; 2415 } 2416 if (flags & V_RDDIR_ENTFLAGS) { 2417 /* 2418 * Add extended flag entry: 2419 */ 2420 eodp->ed_ino = objnum; 2421 eodp->ed_reclen = reclen; 2422 /* NOTE: ed_off is the offset for the *next* entry */ 2423 next = &(eodp->ed_off); 2424 eodp->ed_eflags = zap.za_normalization_conflict ? 2425 ED_CASE_CONFLICT : 0; 2426 (void) strncpy(eodp->ed_name, zap.za_name, 2427 EDIRENT_NAMELEN(reclen)); 2428 eodp = (edirent_t *)((intptr_t)eodp + reclen); 2429 } else { 2430 /* 2431 * Add normal entry: 2432 */ 2433 odp->d_ino = objnum; 2434 odp->d_reclen = reclen; 2435 /* NOTE: d_off is the offset for the *next* entry */ 2436 next = &(odp->d_off); 2437 (void) strncpy(odp->d_name, zap.za_name, 2438 DIRENT64_NAMELEN(reclen)); 2439 odp = (dirent64_t *)((intptr_t)odp + reclen); 2440 } 2441 outcount += reclen; 2442 2443 ASSERT(outcount <= bufsize); 2444 2445 /* Prefetch znode */ 2446 if (prefetch) 2447 dmu_prefetch(os, objnum, 0, 0, 0, 2448 ZIO_PRIORITY_SYNC_READ); 2449 2450 skip_entry: 2451 /* 2452 * Move to the next entry, fill in the previous offset. 2453 */ 2454 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2455 zap_cursor_advance(&zc); 2456 offset = zap_cursor_serialize(&zc); 2457 } else { 2458 offset += 1; 2459 } 2460 if (next) 2461 *next = offset; 2462 } 2463 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2464 2465 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2466 iovp->iov_base += outcount; 2467 iovp->iov_len -= outcount; 2468 uio->uio_resid -= outcount; 2469 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2470 /* 2471 * Reset the pointer. 2472 */ 2473 offset = uio->uio_loffset; 2474 } 2475 2476 update: 2477 zap_cursor_fini(&zc); 2478 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2479 kmem_free(outbuf, bufsize); 2480 2481 if (error == ENOENT) 2482 error = 0; 2483 2484 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2485 2486 uio->uio_loffset = offset; 2487 ZFS_EXIT(zfsvfs); 2488 return (error); 2489 } 2490 2491 ulong_t zfs_fsync_sync_cnt = 4; 2492 2493 static int 2494 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2495 { 2496 znode_t *zp = VTOZ(vp); 2497 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2498 2499 /* 2500 * Regardless of whether this is required for standards conformance, 2501 * this is the logical behavior when fsync() is called on a file with 2502 * dirty pages. We use B_ASYNC since the ZIL transactions are already 2503 * going to be pushed out as part of the zil_commit(). 2504 */ 2505 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 2506 (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 2507 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 2508 2509 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 2510 2511 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 2512 ZFS_ENTER(zfsvfs); 2513 ZFS_VERIFY_ZP(zp); 2514 zil_commit(zfsvfs->z_log, zp->z_id); 2515 ZFS_EXIT(zfsvfs); 2516 } 2517 return (0); 2518 } 2519 2520 2521 /* 2522 * Get the requested file attributes and place them in the provided 2523 * vattr structure. 2524 * 2525 * IN: vp - vnode of file. 2526 * vap - va_mask identifies requested attributes. 2527 * If AT_XVATTR set, then optional attrs are requested 2528 * flags - ATTR_NOACLCHECK (CIFS server context) 2529 * cr - credentials of caller. 2530 * ct - caller context 2531 * 2532 * OUT: vap - attribute values. 2533 * 2534 * RETURN: 0 (always succeeds). 2535 */ 2536 /* ARGSUSED */ 2537 static int 2538 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2539 caller_context_t *ct) 2540 { 2541 znode_t *zp = VTOZ(vp); 2542 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2543 int error = 0; 2544 uint64_t links; 2545 uint64_t mtime[2], ctime[2]; 2546 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2547 xoptattr_t *xoap = NULL; 2548 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2549 sa_bulk_attr_t bulk[2]; 2550 int count = 0; 2551 2552 ZFS_ENTER(zfsvfs); 2553 ZFS_VERIFY_ZP(zp); 2554 2555 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2556 2557 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 2558 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 2559 2560 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 2561 ZFS_EXIT(zfsvfs); 2562 return (error); 2563 } 2564 2565 /* 2566 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 2567 * Also, if we are the owner don't bother, since owner should 2568 * always be allowed to read basic attributes of file. 2569 */ 2570 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && 2571 (vap->va_uid != crgetuid(cr))) { 2572 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 2573 skipaclchk, cr)) { 2574 ZFS_EXIT(zfsvfs); 2575 return (error); 2576 } 2577 } 2578 2579 /* 2580 * Return all attributes. It's cheaper to provide the answer 2581 * than to determine whether we were asked the question. 2582 */ 2583 2584 mutex_enter(&zp->z_lock); 2585 vap->va_type = vp->v_type; 2586 vap->va_mode = zp->z_mode & MODEMASK; 2587 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2588 vap->va_nodeid = zp->z_id; 2589 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 2590 links = zp->z_links + 1; 2591 else 2592 links = zp->z_links; 2593 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2594 vap->va_size = zp->z_size; 2595 vap->va_rdev = vp->v_rdev; 2596 vap->va_seq = zp->z_seq; 2597 2598 /* 2599 * Add in any requested optional attributes and the create time. 2600 * Also set the corresponding bits in the returned attribute bitmap. 2601 */ 2602 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 2603 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 2604 xoap->xoa_archive = 2605 ((zp->z_pflags & ZFS_ARCHIVE) != 0); 2606 XVA_SET_RTN(xvap, XAT_ARCHIVE); 2607 } 2608 2609 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 2610 xoap->xoa_readonly = 2611 ((zp->z_pflags & ZFS_READONLY) != 0); 2612 XVA_SET_RTN(xvap, XAT_READONLY); 2613 } 2614 2615 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 2616 xoap->xoa_system = 2617 ((zp->z_pflags & ZFS_SYSTEM) != 0); 2618 XVA_SET_RTN(xvap, XAT_SYSTEM); 2619 } 2620 2621 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 2622 xoap->xoa_hidden = 2623 ((zp->z_pflags & ZFS_HIDDEN) != 0); 2624 XVA_SET_RTN(xvap, XAT_HIDDEN); 2625 } 2626 2627 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2628 xoap->xoa_nounlink = 2629 ((zp->z_pflags & ZFS_NOUNLINK) != 0); 2630 XVA_SET_RTN(xvap, XAT_NOUNLINK); 2631 } 2632 2633 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2634 xoap->xoa_immutable = 2635 ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 2636 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 2637 } 2638 2639 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2640 xoap->xoa_appendonly = 2641 ((zp->z_pflags & ZFS_APPENDONLY) != 0); 2642 XVA_SET_RTN(xvap, XAT_APPENDONLY); 2643 } 2644 2645 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2646 xoap->xoa_nodump = 2647 ((zp->z_pflags & ZFS_NODUMP) != 0); 2648 XVA_SET_RTN(xvap, XAT_NODUMP); 2649 } 2650 2651 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 2652 xoap->xoa_opaque = 2653 ((zp->z_pflags & ZFS_OPAQUE) != 0); 2654 XVA_SET_RTN(xvap, XAT_OPAQUE); 2655 } 2656 2657 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2658 xoap->xoa_av_quarantined = 2659 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 2660 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 2661 } 2662 2663 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2664 xoap->xoa_av_modified = 2665 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 2666 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 2667 } 2668 2669 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 2670 vp->v_type == VREG) { 2671 zfs_sa_get_scanstamp(zp, xvap); 2672 } 2673 2674 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 2675 uint64_t times[2]; 2676 2677 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 2678 times, sizeof (times)); 2679 ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 2680 XVA_SET_RTN(xvap, XAT_CREATETIME); 2681 } 2682 2683 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2684 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 2685 XVA_SET_RTN(xvap, XAT_REPARSE); 2686 } 2687 if (XVA_ISSET_REQ(xvap, XAT_GEN)) { 2688 xoap->xoa_generation = zp->z_gen; 2689 XVA_SET_RTN(xvap, XAT_GEN); 2690 } 2691 2692 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 2693 xoap->xoa_offline = 2694 ((zp->z_pflags & ZFS_OFFLINE) != 0); 2695 XVA_SET_RTN(xvap, XAT_OFFLINE); 2696 } 2697 2698 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 2699 xoap->xoa_sparse = 2700 ((zp->z_pflags & ZFS_SPARSE) != 0); 2701 XVA_SET_RTN(xvap, XAT_SPARSE); 2702 } 2703 } 2704 2705 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 2706 ZFS_TIME_DECODE(&vap->va_mtime, mtime); 2707 ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2708 2709 mutex_exit(&zp->z_lock); 2710 2711 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); 2712 2713 if (zp->z_blksz == 0) { 2714 /* 2715 * Block size hasn't been set; suggest maximal I/O transfers. 2716 */ 2717 vap->va_blksize = zfsvfs->z_max_blksz; 2718 } 2719 2720 ZFS_EXIT(zfsvfs); 2721 return (0); 2722 } 2723 2724 /* 2725 * Set the file attributes to the values contained in the 2726 * vattr structure. 2727 * 2728 * IN: vp - vnode of file to be modified. 2729 * vap - new attribute values. 2730 * If AT_XVATTR set, then optional attrs are being set 2731 * flags - ATTR_UTIME set if non-default time values provided. 2732 * - ATTR_NOACLCHECK (CIFS context only). 2733 * cr - credentials of caller. 2734 * ct - caller context 2735 * 2736 * RETURN: 0 on success, error code on failure. 2737 * 2738 * Timestamps: 2739 * vp - ctime updated, mtime updated if size changed. 2740 */ 2741 /* ARGSUSED */ 2742 static int 2743 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2744 caller_context_t *ct) 2745 { 2746 znode_t *zp = VTOZ(vp); 2747 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2748 zilog_t *zilog; 2749 dmu_tx_t *tx; 2750 vattr_t oldva; 2751 xvattr_t tmpxvattr; 2752 uint_t mask = vap->va_mask; 2753 uint_t saved_mask = 0; 2754 int trim_mask = 0; 2755 uint64_t new_mode; 2756 uint64_t new_uid, new_gid; 2757 uint64_t xattr_obj; 2758 uint64_t mtime[2], ctime[2]; 2759 znode_t *attrzp; 2760 int need_policy = FALSE; 2761 int err, err2; 2762 zfs_fuid_info_t *fuidp = NULL; 2763 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2764 xoptattr_t *xoap; 2765 zfs_acl_t *aclp; 2766 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2767 boolean_t fuid_dirtied = B_FALSE; 2768 sa_bulk_attr_t bulk[7], xattr_bulk[7]; 2769 int count = 0, xattr_count = 0; 2770 2771 if (mask == 0) 2772 return (0); 2773 2774 if (mask & AT_NOSET) 2775 return (SET_ERROR(EINVAL)); 2776 2777 ZFS_ENTER(zfsvfs); 2778 ZFS_VERIFY_ZP(zp); 2779 2780 zilog = zfsvfs->z_log; 2781 2782 /* 2783 * Make sure that if we have ephemeral uid/gid or xvattr specified 2784 * that file system is at proper version level 2785 */ 2786 2787 if (zfsvfs->z_use_fuids == B_FALSE && 2788 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 2789 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 2790 (mask & AT_XVATTR))) { 2791 ZFS_EXIT(zfsvfs); 2792 return (SET_ERROR(EINVAL)); 2793 } 2794 2795 if (mask & AT_SIZE && vp->v_type == VDIR) { 2796 ZFS_EXIT(zfsvfs); 2797 return (SET_ERROR(EISDIR)); 2798 } 2799 2800 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 2801 ZFS_EXIT(zfsvfs); 2802 return (SET_ERROR(EINVAL)); 2803 } 2804 2805 /* 2806 * If this is an xvattr_t, then get a pointer to the structure of 2807 * optional attributes. If this is NULL, then we have a vattr_t. 2808 */ 2809 xoap = xva_getxoptattr(xvap); 2810 2811 xva_init(&tmpxvattr); 2812 2813 /* 2814 * Immutable files can only alter immutable bit and atime 2815 */ 2816 if ((zp->z_pflags & ZFS_IMMUTABLE) && 2817 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 2818 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 2819 ZFS_EXIT(zfsvfs); 2820 return (SET_ERROR(EPERM)); 2821 } 2822 2823 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 2824 ZFS_EXIT(zfsvfs); 2825 return (SET_ERROR(EPERM)); 2826 } 2827 2828 /* 2829 * Verify timestamps doesn't overflow 32 bits. 2830 * ZFS can handle large timestamps, but 32bit syscalls can't 2831 * handle times greater than 2039. This check should be removed 2832 * once large timestamps are fully supported. 2833 */ 2834 if (mask & (AT_ATIME | AT_MTIME)) { 2835 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 2836 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 2837 ZFS_EXIT(zfsvfs); 2838 return (SET_ERROR(EOVERFLOW)); 2839 } 2840 } 2841 2842 top: 2843 attrzp = NULL; 2844 aclp = NULL; 2845 2846 /* Can this be moved to before the top label? */ 2847 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2848 ZFS_EXIT(zfsvfs); 2849 return (SET_ERROR(EROFS)); 2850 } 2851 2852 /* 2853 * First validate permissions 2854 */ 2855 2856 if (mask & AT_SIZE) { 2857 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2858 if (err) { 2859 ZFS_EXIT(zfsvfs); 2860 return (err); 2861 } 2862 /* 2863 * XXX - Note, we are not providing any open 2864 * mode flags here (like FNDELAY), so we may 2865 * block if there are locks present... this 2866 * should be addressed in openat(). 2867 */ 2868 /* XXX - would it be OK to generate a log record here? */ 2869 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 2870 if (err) { 2871 ZFS_EXIT(zfsvfs); 2872 return (err); 2873 } 2874 2875 if (vap->va_size == 0) 2876 vnevent_truncate(ZTOV(zp), ct); 2877 } 2878 2879 if (mask & (AT_ATIME|AT_MTIME) || 2880 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 2881 XVA_ISSET_REQ(xvap, XAT_READONLY) || 2882 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 2883 XVA_ISSET_REQ(xvap, XAT_OFFLINE) || 2884 XVA_ISSET_REQ(xvap, XAT_SPARSE) || 2885 XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 2886 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 2887 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 2888 skipaclchk, cr); 2889 } 2890 2891 if (mask & (AT_UID|AT_GID)) { 2892 int idmask = (mask & (AT_UID|AT_GID)); 2893 int take_owner; 2894 int take_group; 2895 2896 /* 2897 * NOTE: even if a new mode is being set, 2898 * we may clear S_ISUID/S_ISGID bits. 2899 */ 2900 2901 if (!(mask & AT_MODE)) 2902 vap->va_mode = zp->z_mode; 2903 2904 /* 2905 * Take ownership or chgrp to group we are a member of 2906 */ 2907 2908 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2909 take_group = (mask & AT_GID) && 2910 zfs_groupmember(zfsvfs, vap->va_gid, cr); 2911 2912 /* 2913 * If both AT_UID and AT_GID are set then take_owner and 2914 * take_group must both be set in order to allow taking 2915 * ownership. 2916 * 2917 * Otherwise, send the check through secpolicy_vnode_setattr() 2918 * 2919 */ 2920 2921 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2922 ((idmask == AT_UID) && take_owner) || 2923 ((idmask == AT_GID) && take_group)) { 2924 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 2925 skipaclchk, cr) == 0) { 2926 /* 2927 * Remove setuid/setgid for non-privileged users 2928 */ 2929 secpolicy_setid_clear(vap, cr); 2930 trim_mask = (mask & (AT_UID|AT_GID)); 2931 } else { 2932 need_policy = TRUE; 2933 } 2934 } else { 2935 need_policy = TRUE; 2936 } 2937 } 2938 2939 mutex_enter(&zp->z_lock); 2940 oldva.va_mode = zp->z_mode; 2941 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 2942 if (mask & AT_XVATTR) { 2943 /* 2944 * Update xvattr mask to include only those attributes 2945 * that are actually changing. 2946 * 2947 * the bits will be restored prior to actually setting 2948 * the attributes so the caller thinks they were set. 2949 */ 2950 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2951 if (xoap->xoa_appendonly != 2952 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 2953 need_policy = TRUE; 2954 } else { 2955 XVA_CLR_REQ(xvap, XAT_APPENDONLY); 2956 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 2957 } 2958 } 2959 2960 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2961 if (xoap->xoa_nounlink != 2962 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 2963 need_policy = TRUE; 2964 } else { 2965 XVA_CLR_REQ(xvap, XAT_NOUNLINK); 2966 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 2967 } 2968 } 2969 2970 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2971 if (xoap->xoa_immutable != 2972 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 2973 need_policy = TRUE; 2974 } else { 2975 XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 2976 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 2977 } 2978 } 2979 2980 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2981 if (xoap->xoa_nodump != 2982 ((zp->z_pflags & ZFS_NODUMP) != 0)) { 2983 need_policy = TRUE; 2984 } else { 2985 XVA_CLR_REQ(xvap, XAT_NODUMP); 2986 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 2987 } 2988 } 2989 2990 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2991 if (xoap->xoa_av_modified != 2992 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 2993 need_policy = TRUE; 2994 } else { 2995 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 2996 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 2997 } 2998 } 2999 3000 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 3001 if ((vp->v_type != VREG && 3002 xoap->xoa_av_quarantined) || 3003 xoap->xoa_av_quarantined != 3004 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 3005 need_policy = TRUE; 3006 } else { 3007 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 3008 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 3009 } 3010 } 3011 3012 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 3013 mutex_exit(&zp->z_lock); 3014 ZFS_EXIT(zfsvfs); 3015 return (SET_ERROR(EPERM)); 3016 } 3017 3018 if (need_policy == FALSE && 3019 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 3020 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 3021 need_policy = TRUE; 3022 } 3023 } 3024 3025 mutex_exit(&zp->z_lock); 3026 3027 if (mask & AT_MODE) { 3028 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 3029 err = secpolicy_setid_setsticky_clear(vp, vap, 3030 &oldva, cr); 3031 if (err) { 3032 ZFS_EXIT(zfsvfs); 3033 return (err); 3034 } 3035 trim_mask |= AT_MODE; 3036 } else { 3037 need_policy = TRUE; 3038 } 3039 } 3040 3041 if (need_policy) { 3042 /* 3043 * If trim_mask is set then take ownership 3044 * has been granted or write_acl is present and user 3045 * has the ability to modify mode. In that case remove 3046 * UID|GID and or MODE from mask so that 3047 * secpolicy_vnode_setattr() doesn't revoke it. 3048 */ 3049 3050 if (trim_mask) { 3051 saved_mask = vap->va_mask; 3052 vap->va_mask &= ~trim_mask; 3053 } 3054 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 3055 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 3056 if (err) { 3057 ZFS_EXIT(zfsvfs); 3058 return (err); 3059 } 3060 3061 if (trim_mask) 3062 vap->va_mask |= saved_mask; 3063 } 3064 3065 /* 3066 * secpolicy_vnode_setattr, or take ownership may have 3067 * changed va_mask 3068 */ 3069 mask = vap->va_mask; 3070 3071 if ((mask & (AT_UID | AT_GID))) { 3072 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 3073 &xattr_obj, sizeof (xattr_obj)); 3074 3075 if (err == 0 && xattr_obj) { 3076 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 3077 if (err) 3078 goto out2; 3079 } 3080 if (mask & AT_UID) { 3081 new_uid = zfs_fuid_create(zfsvfs, 3082 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 3083 if (new_uid != zp->z_uid && 3084 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 3085 if (attrzp) 3086 VN_RELE(ZTOV(attrzp)); 3087 err = SET_ERROR(EDQUOT); 3088 goto out2; 3089 } 3090 } 3091 3092 if (mask & AT_GID) { 3093 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 3094 cr, ZFS_GROUP, &fuidp); 3095 if (new_gid != zp->z_gid && 3096 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 3097 if (attrzp) 3098 VN_RELE(ZTOV(attrzp)); 3099 err = SET_ERROR(EDQUOT); 3100 goto out2; 3101 } 3102 } 3103 } 3104 tx = dmu_tx_create(zfsvfs->z_os); 3105 3106 if (mask & AT_MODE) { 3107 uint64_t pmode = zp->z_mode; 3108 uint64_t acl_obj; 3109 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 3110 3111 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED && 3112 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) { 3113 err = SET_ERROR(EPERM); 3114 goto out; 3115 } 3116 3117 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 3118 goto out; 3119 3120 mutex_enter(&zp->z_lock); 3121 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 3122 /* 3123 * Are we upgrading ACL from old V0 format 3124 * to V1 format? 3125 */ 3126 if (zfsvfs->z_version >= ZPL_VERSION_FUID && 3127 zfs_znode_acl_version(zp) == 3128 ZFS_ACL_VERSION_INITIAL) { 3129 dmu_tx_hold_free(tx, acl_obj, 0, 3130 DMU_OBJECT_END); 3131 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3132 0, aclp->z_acl_bytes); 3133 } else { 3134 dmu_tx_hold_write(tx, acl_obj, 0, 3135 aclp->z_acl_bytes); 3136 } 3137 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3138 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3139 0, aclp->z_acl_bytes); 3140 } 3141 mutex_exit(&zp->z_lock); 3142 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3143 } else { 3144 if ((mask & AT_XVATTR) && 3145 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3146 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3147 else 3148 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3149 } 3150 3151 if (attrzp) { 3152 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 3153 } 3154 3155 fuid_dirtied = zfsvfs->z_fuid_dirty; 3156 if (fuid_dirtied) 3157 zfs_fuid_txhold(zfsvfs, tx); 3158 3159 zfs_sa_upgrade_txholds(tx, zp); 3160 3161 err = dmu_tx_assign(tx, TXG_WAIT); 3162 if (err) 3163 goto out; 3164 3165 count = 0; 3166 /* 3167 * Set each attribute requested. 3168 * We group settings according to the locks they need to acquire. 3169 * 3170 * Note: you cannot set ctime directly, although it will be 3171 * updated as a side-effect of calling this function. 3172 */ 3173 3174 3175 if (mask & (AT_UID|AT_GID|AT_MODE)) 3176 mutex_enter(&zp->z_acl_lock); 3177 mutex_enter(&zp->z_lock); 3178 3179 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 3180 &zp->z_pflags, sizeof (zp->z_pflags)); 3181 3182 if (attrzp) { 3183 if (mask & (AT_UID|AT_GID|AT_MODE)) 3184 mutex_enter(&attrzp->z_acl_lock); 3185 mutex_enter(&attrzp->z_lock); 3186 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3187 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 3188 sizeof (attrzp->z_pflags)); 3189 } 3190 3191 if (mask & (AT_UID|AT_GID)) { 3192 3193 if (mask & AT_UID) { 3194 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 3195 &new_uid, sizeof (new_uid)); 3196 zp->z_uid = new_uid; 3197 if (attrzp) { 3198 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3199 SA_ZPL_UID(zfsvfs), NULL, &new_uid, 3200 sizeof (new_uid)); 3201 attrzp->z_uid = new_uid; 3202 } 3203 } 3204 3205 if (mask & AT_GID) { 3206 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 3207 NULL, &new_gid, sizeof (new_gid)); 3208 zp->z_gid = new_gid; 3209 if (attrzp) { 3210 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3211 SA_ZPL_GID(zfsvfs), NULL, &new_gid, 3212 sizeof (new_gid)); 3213 attrzp->z_gid = new_gid; 3214 } 3215 } 3216 if (!(mask & AT_MODE)) { 3217 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 3218 NULL, &new_mode, sizeof (new_mode)); 3219 new_mode = zp->z_mode; 3220 } 3221 err = zfs_acl_chown_setattr(zp); 3222 ASSERT(err == 0); 3223 if (attrzp) { 3224 err = zfs_acl_chown_setattr(attrzp); 3225 ASSERT(err == 0); 3226 } 3227 } 3228 3229 if (mask & AT_MODE) { 3230 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 3231 &new_mode, sizeof (new_mode)); 3232 zp->z_mode = new_mode; 3233 ASSERT3U((uintptr_t)aclp, !=, NULL); 3234 err = zfs_aclset_common(zp, aclp, cr, tx); 3235 ASSERT0(err); 3236 if (zp->z_acl_cached) 3237 zfs_acl_free(zp->z_acl_cached); 3238 zp->z_acl_cached = aclp; 3239 aclp = NULL; 3240 } 3241 3242 3243 if (mask & AT_ATIME) { 3244 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 3245 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 3246 &zp->z_atime, sizeof (zp->z_atime)); 3247 } 3248 3249 if (mask & AT_MTIME) { 3250 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 3251 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 3252 mtime, sizeof (mtime)); 3253 } 3254 3255 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 3256 if (mask & AT_SIZE && !(mask & AT_MTIME)) { 3257 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 3258 NULL, mtime, sizeof (mtime)); 3259 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3260 &ctime, sizeof (ctime)); 3261 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 3262 B_TRUE); 3263 } else if (mask != 0) { 3264 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3265 &ctime, sizeof (ctime)); 3266 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 3267 B_TRUE); 3268 if (attrzp) { 3269 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3270 SA_ZPL_CTIME(zfsvfs), NULL, 3271 &ctime, sizeof (ctime)); 3272 zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 3273 mtime, ctime, B_TRUE); 3274 } 3275 } 3276 /* 3277 * Do this after setting timestamps to prevent timestamp 3278 * update from toggling bit 3279 */ 3280 3281 if (xoap && (mask & AT_XVATTR)) { 3282 3283 /* 3284 * restore trimmed off masks 3285 * so that return masks can be set for caller. 3286 */ 3287 3288 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 3289 XVA_SET_REQ(xvap, XAT_APPENDONLY); 3290 } 3291 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 3292 XVA_SET_REQ(xvap, XAT_NOUNLINK); 3293 } 3294 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 3295 XVA_SET_REQ(xvap, XAT_IMMUTABLE); 3296 } 3297 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 3298 XVA_SET_REQ(xvap, XAT_NODUMP); 3299 } 3300 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 3301 XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 3302 } 3303 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 3304 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 3305 } 3306 3307 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3308 ASSERT(vp->v_type == VREG); 3309 3310 zfs_xvattr_set(zp, xvap, tx); 3311 } 3312 3313 if (fuid_dirtied) 3314 zfs_fuid_sync(zfsvfs, tx); 3315 3316 if (mask != 0) 3317 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 3318 3319 mutex_exit(&zp->z_lock); 3320 if (mask & (AT_UID|AT_GID|AT_MODE)) 3321 mutex_exit(&zp->z_acl_lock); 3322 3323 if (attrzp) { 3324 if (mask & (AT_UID|AT_GID|AT_MODE)) 3325 mutex_exit(&attrzp->z_acl_lock); 3326 mutex_exit(&attrzp->z_lock); 3327 } 3328 out: 3329 if (err == 0 && attrzp) { 3330 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 3331 xattr_count, tx); 3332 ASSERT(err2 == 0); 3333 } 3334 3335 if (attrzp) 3336 VN_RELE(ZTOV(attrzp)); 3337 3338 if (aclp) 3339 zfs_acl_free(aclp); 3340 3341 if (fuidp) { 3342 zfs_fuid_info_free(fuidp); 3343 fuidp = NULL; 3344 } 3345 3346 if (err) { 3347 dmu_tx_abort(tx); 3348 if (err == ERESTART) 3349 goto top; 3350 } else { 3351 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 3352 dmu_tx_commit(tx); 3353 } 3354 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); 3358 rw_exit(&rz_zev_rwlock); 3359 3360 out2: 3361 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3362 zil_commit(zilog, 0); 3363 3364 ZFS_EXIT(zfsvfs); 3365 return (err); 3366 } 3367 3368 typedef struct zfs_zlock { 3369 krwlock_t *zl_rwlock; /* lock we acquired */ 3370 znode_t *zl_znode; /* znode we held */ 3371 struct zfs_zlock *zl_next; /* next in list */ 3372 } zfs_zlock_t; 3373 3374 /* 3375 * Drop locks and release vnodes that were held by zfs_rename_lock(). 3376 */ 3377 static void 3378 zfs_rename_unlock(zfs_zlock_t **zlpp) 3379 { 3380 zfs_zlock_t *zl; 3381 3382 while ((zl = *zlpp) != NULL) { 3383 if (zl->zl_znode != NULL) 3384 VN_RELE(ZTOV(zl->zl_znode)); 3385 rw_exit(zl->zl_rwlock); 3386 *zlpp = zl->zl_next; 3387 kmem_free(zl, sizeof (*zl)); 3388 } 3389 } 3390 3391 /* 3392 * Search back through the directory tree, using the ".." entries. 3393 * Lock each directory in the chain to prevent concurrent renames. 3394 * Fail any attempt to move a directory into one of its own descendants. 3395 * XXX - z_parent_lock can overlap with map or grow locks 3396 */ 3397 static int 3398 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3399 { 3400 zfs_zlock_t *zl; 3401 znode_t *zp = tdzp; 3402 uint64_t rootid = zp->z_zfsvfs->z_root; 3403 uint64_t oidp = zp->z_id; 3404 krwlock_t *rwlp = &szp->z_parent_lock; 3405 krw_t rw = RW_WRITER; 3406 3407 /* 3408 * First pass write-locks szp and compares to zp->z_id. 3409 * Later passes read-lock zp and compare to zp->z_parent. 3410 */ 3411 do { 3412 if (!rw_tryenter(rwlp, rw)) { 3413 /* 3414 * Another thread is renaming in this path. 3415 * Note that if we are a WRITER, we don't have any 3416 * parent_locks held yet. 3417 */ 3418 if (rw == RW_READER && zp->z_id > szp->z_id) { 3419 /* 3420 * Drop our locks and restart 3421 */ 3422 zfs_rename_unlock(&zl); 3423 *zlpp = NULL; 3424 zp = tdzp; 3425 oidp = zp->z_id; 3426 rwlp = &szp->z_parent_lock; 3427 rw = RW_WRITER; 3428 continue; 3429 } else { 3430 /* 3431 * Wait for other thread to drop its locks 3432 */ 3433 rw_enter(rwlp, rw); 3434 } 3435 } 3436 3437 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3438 zl->zl_rwlock = rwlp; 3439 zl->zl_znode = NULL; 3440 zl->zl_next = *zlpp; 3441 *zlpp = zl; 3442 3443 if (oidp == szp->z_id) /* We're a descendant of szp */ 3444 return (SET_ERROR(EINVAL)); 3445 3446 if (oidp == rootid) /* We've hit the top */ 3447 return (0); 3448 3449 if (rw == RW_READER) { /* i.e. not the first pass */ 3450 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3451 if (error) 3452 return (error); 3453 zl->zl_znode = zp; 3454 } 3455 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 3456 &oidp, sizeof (oidp)); 3457 rwlp = &zp->z_parent_lock; 3458 rw = RW_READER; 3459 3460 } while (zp->z_id != sdzp->z_id); 3461 3462 return (0); 3463 } 3464 3465 /* 3466 * Move an entry from the provided source directory to the target 3467 * directory. Change the entry name as indicated. 3468 * 3469 * IN: sdvp - Source directory containing the "old entry". 3470 * snm - Old entry name. 3471 * tdvp - Target directory to contain the "new entry". 3472 * tnm - New entry name. 3473 * cr - credentials of caller. 3474 * ct - caller context 3475 * flags - case flags 3476 * 3477 * RETURN: 0 on success, error code on failure. 3478 * 3479 * Timestamps: 3480 * sdvp,tdvp - ctime|mtime updated 3481 */ 3482 /*ARGSUSED*/ 3483 static int 3484 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 3485 caller_context_t *ct, int flags) 3486 { 3487 znode_t *tdzp, *szp, *tzp; 3488 znode_t *sdzp = VTOZ(sdvp); 3489 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 3490 zilog_t *zilog; 3491 vnode_t *realvp; 3492 zfs_dirlock_t *sdl, *tdl; 3493 dmu_tx_t *tx; 3494 zfs_zlock_t *zl; 3495 int cmp, serr, terr; 3496 int error = 0; 3497 int zflg = 0; 3498 boolean_t waited = B_FALSE; 3499 3500 ZFS_ENTER(zfsvfs); 3501 ZFS_VERIFY_ZP(sdzp); 3502 zilog = zfsvfs->z_log; 3503 3504 /* 3505 * Make sure we have the real vp for the target directory. 3506 */ 3507 if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3508 tdvp = realvp; 3509 3510 tdzp = VTOZ(tdvp); 3511 ZFS_VERIFY_ZP(tdzp); 3512 3513 /* 3514 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the 3515 * ctldir appear to have the same v_vfsp. 3516 */ 3517 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) { 3518 ZFS_EXIT(zfsvfs); 3519 return (SET_ERROR(EXDEV)); 3520 } 3521 3522 if (zfsvfs->z_utf8 && u8_validate(tnm, 3523 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3524 ZFS_EXIT(zfsvfs); 3525 return (SET_ERROR(EILSEQ)); 3526 } 3527 3528 if (flags & FIGNORECASE) 3529 zflg |= ZCILOOK; 3530 3531 top: 3532 szp = NULL; 3533 tzp = NULL; 3534 zl = NULL; 3535 3536 /* 3537 * This is to prevent the creation of links into attribute space 3538 * by renaming a linked file into/outof an attribute directory. 3539 * See the comment in zfs_link() for why this is considered bad. 3540 */ 3541 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3542 ZFS_EXIT(zfsvfs); 3543 return (SET_ERROR(EINVAL)); 3544 } 3545 3546 /* 3547 * Lock source and target directory entries. To prevent deadlock, 3548 * a lock ordering must be defined. We lock the directory with 3549 * the smallest object id first, or if it's a tie, the one with 3550 * the lexically first name. 3551 */ 3552 if (sdzp->z_id < tdzp->z_id) { 3553 cmp = -1; 3554 } else if (sdzp->z_id > tdzp->z_id) { 3555 cmp = 1; 3556 } else { 3557 /* 3558 * First compare the two name arguments without 3559 * considering any case folding. 3560 */ 3561 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 3562 3563 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 3564 ASSERT(error == 0 || !zfsvfs->z_utf8); 3565 if (cmp == 0) { 3566 /* 3567 * POSIX: "If the old argument and the new argument 3568 * both refer to links to the same existing file, 3569 * the rename() function shall return successfully 3570 * and perform no other action." 3571 */ 3572 ZFS_EXIT(zfsvfs); 3573 return (0); 3574 } 3575 /* 3576 * If the file system is case-folding, then we may 3577 * have some more checking to do. A case-folding file 3578 * system is either supporting mixed case sensitivity 3579 * access or is completely case-insensitive. Note 3580 * that the file system is always case preserving. 3581 * 3582 * In mixed sensitivity mode case sensitive behavior 3583 * is the default. FIGNORECASE must be used to 3584 * explicitly request case insensitive behavior. 3585 * 3586 * If the source and target names provided differ only 3587 * by case (e.g., a request to rename 'tim' to 'Tim'), 3588 * we will treat this as a special case in the 3589 * case-insensitive mode: as long as the source name 3590 * is an exact match, we will allow this to proceed as 3591 * a name-change request. 3592 */ 3593 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 3594 (zfsvfs->z_case == ZFS_CASE_MIXED && 3595 flags & FIGNORECASE)) && 3596 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 3597 &error) == 0) { 3598 /* 3599 * case preserving rename request, require exact 3600 * name matches 3601 */ 3602 zflg |= ZCIEXACT; 3603 zflg &= ~ZCILOOK; 3604 } 3605 } 3606 3607 /* 3608 * If the source and destination directories are the same, we should 3609 * grab the z_name_lock of that directory only once. 3610 */ 3611 if (sdzp == tdzp) { 3612 zflg |= ZHAVELOCK; 3613 rw_enter(&sdzp->z_name_lock, RW_READER); 3614 } 3615 3616 if (cmp < 0) { 3617 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 3618 ZEXISTS | zflg, NULL, NULL); 3619 terr = zfs_dirent_lock(&tdl, 3620 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3621 } else { 3622 terr = zfs_dirent_lock(&tdl, 3623 tdzp, tnm, &tzp, zflg, NULL, NULL); 3624 serr = zfs_dirent_lock(&sdl, 3625 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 3626 NULL, NULL); 3627 } 3628 3629 if (serr) { 3630 /* 3631 * Source entry invalid or not there. 3632 */ 3633 if (!terr) { 3634 zfs_dirent_unlock(tdl); 3635 if (tzp) 3636 VN_RELE(ZTOV(tzp)); 3637 } 3638 3639 if (sdzp == tdzp) 3640 rw_exit(&sdzp->z_name_lock); 3641 3642 if (strcmp(snm, "..") == 0) 3643 serr = SET_ERROR(EINVAL); 3644 ZFS_EXIT(zfsvfs); 3645 return (serr); 3646 } 3647 if (terr) { 3648 zfs_dirent_unlock(sdl); 3649 VN_RELE(ZTOV(szp)); 3650 3651 if (sdzp == tdzp) 3652 rw_exit(&sdzp->z_name_lock); 3653 3654 if (strcmp(tnm, "..") == 0) 3655 terr = SET_ERROR(EINVAL); 3656 ZFS_EXIT(zfsvfs); 3657 return (terr); 3658 } 3659 3660 /* 3661 * Must have write access at the source to remove the old entry 3662 * and write access at the target to create the new entry. 3663 * Note that if target and source are the same, this can be 3664 * done in a single check. 3665 */ 3666 3667 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3668 goto out; 3669 3670 if (ZTOV(szp)->v_type == VDIR) { 3671 /* 3672 * Check to make sure rename is valid. 3673 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3674 */ 3675 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3676 goto out; 3677 } 3678 3679 /* 3680 * Does target exist? 3681 */ 3682 if (tzp) { 3683 /* 3684 * Source and target must be the same type. 3685 */ 3686 if (ZTOV(szp)->v_type == VDIR) { 3687 if (ZTOV(tzp)->v_type != VDIR) { 3688 error = SET_ERROR(ENOTDIR); 3689 goto out; 3690 } 3691 } else { 3692 if (ZTOV(tzp)->v_type == VDIR) { 3693 error = SET_ERROR(EISDIR); 3694 goto out; 3695 } 3696 } 3697 /* 3698 * POSIX dictates that when the source and target 3699 * entries refer to the same file object, rename 3700 * must do nothing and exit without error. 3701 */ 3702 if (szp->z_id == tzp->z_id) { 3703 error = 0; 3704 goto out; 3705 } 3706 } 3707 3708 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3709 if (tzp) 3710 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 3711 3712 /* 3713 * notify the target directory if it is not the same 3714 * as source directory. 3715 */ 3716 if (tdvp != sdvp) { 3717 vnevent_rename_dest_dir(tdvp, ct); 3718 } 3719 3720 tx = dmu_tx_create(zfsvfs->z_os); 3721 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 3722 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 3723 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 3724 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 3725 if (sdzp != tdzp) { 3726 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 3727 zfs_sa_upgrade_txholds(tx, tdzp); 3728 } 3729 if (tzp) { 3730 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 3731 zfs_sa_upgrade_txholds(tx, tzp); 3732 } 3733 3734 zfs_sa_upgrade_txholds(tx, szp); 3735 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3736 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 3737 if (error) { 3738 if (zl != NULL) 3739 zfs_rename_unlock(&zl); 3740 zfs_dirent_unlock(sdl); 3741 zfs_dirent_unlock(tdl); 3742 3743 if (sdzp == tdzp) 3744 rw_exit(&sdzp->z_name_lock); 3745 3746 VN_RELE(ZTOV(szp)); 3747 if (tzp) 3748 VN_RELE(ZTOV(tzp)); 3749 if (error == ERESTART) { 3750 waited = B_TRUE; 3751 dmu_tx_wait(tx); 3752 dmu_tx_abort(tx); 3753 goto top; 3754 } 3755 dmu_tx_abort(tx); 3756 ZFS_EXIT(zfsvfs); 3757 return (error); 3758 } 3759 3760 if (tzp) /* Attempt to remove the existing target */ 3761 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3762 3763 if (error == 0) { 3764 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3765 if (error == 0) { 3766 szp->z_pflags |= ZFS_AV_MODIFIED; 3767 3768 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 3769 (void *)&szp->z_pflags, sizeof (uint64_t), tx); 3770 ASSERT0(error); 3771 3772 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3773 if (error == 0) { 3774 zfs_log_rename(zilog, tx, TX_RENAME | 3775 (flags & FIGNORECASE ? TX_CI : 0), sdzp, 3776 sdl->dl_name, tdzp, tdl->dl_name, szp); 3777 3778 /* 3779 * Update path information for the target vnode 3780 */ 3781 vn_renamepath(tdvp, ZTOV(szp), tnm, 3782 strlen(tnm)); 3783 } else { 3784 /* 3785 * At this point, we have successfully created 3786 * the target name, but have failed to remove 3787 * the source name. Since the create was done 3788 * with the ZRENAMING flag, there are 3789 * complications; for one, the link count is 3790 * wrong. The easiest way to deal with this 3791 * is to remove the newly created target, and 3792 * return the original error. This must 3793 * succeed; fortunately, it is very unlikely to 3794 * fail, since we just created it. 3795 */ 3796 VERIFY3U(zfs_link_destroy(tdl, szp, tx, 3797 ZRENAMING, NULL), ==, 0); 3798 } 3799 } 3800 } 3801 3802 dmu_tx_commit(tx); 3803 out: 3804 if (zl != NULL) 3805 zfs_rename_unlock(&zl); 3806 3807 zfs_dirent_unlock(sdl); 3808 zfs_dirent_unlock(tdl); 3809 3810 if (sdzp == tdzp) 3811 rw_exit(&sdzp->z_name_lock); 3812 3813 3814 VN_RELE(ZTOV(szp)); 3815 if (tzp) 3816 VN_RELE(ZTOV(tzp)); 3817 3818 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3819 zil_commit(zilog, 0); 3820 3821 ZFS_EXIT(zfsvfs); 3822 return (error); 3823 } 3824 3825 /* 3826 * Insert the indicated symbolic reference entry into the directory. 3827 * 3828 * IN: dvp - Directory to contain new symbolic link. 3829 * link - Name for new symlink entry. 3830 * vap - Attributes of new entry. 3831 * cr - credentials of caller. 3832 * ct - caller context 3833 * flags - case flags 3834 * 3835 * RETURN: 0 on success, error code on failure. 3836 * 3837 * Timestamps: 3838 * dvp - ctime|mtime updated 3839 */ 3840 /*ARGSUSED*/ 3841 static int 3842 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 3843 caller_context_t *ct, int flags) 3844 { 3845 znode_t *zp, *dzp = VTOZ(dvp); 3846 zfs_dirlock_t *dl; 3847 dmu_tx_t *tx; 3848 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 3849 zilog_t *zilog; 3850 uint64_t len = strlen(link); 3851 int error; 3852 int zflg = ZNEW; 3853 zfs_acl_ids_t acl_ids; 3854 boolean_t fuid_dirtied; 3855 uint64_t txtype = TX_SYMLINK; 3856 boolean_t waited = B_FALSE; 3857 3858 ASSERT(vap->va_type == VLNK); 3859 3860 ZFS_ENTER(zfsvfs); 3861 ZFS_VERIFY_ZP(dzp); 3862 zilog = zfsvfs->z_log; 3863 3864 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 3865 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3866 ZFS_EXIT(zfsvfs); 3867 return (SET_ERROR(EILSEQ)); 3868 } 3869 if (flags & FIGNORECASE) 3870 zflg |= ZCILOOK; 3871 3872 if (len > MAXPATHLEN) { 3873 ZFS_EXIT(zfsvfs); 3874 return (SET_ERROR(ENAMETOOLONG)); 3875 } 3876 3877 if ((error = zfs_acl_ids_create(dzp, 0, 3878 vap, cr, NULL, &acl_ids)) != 0) { 3879 ZFS_EXIT(zfsvfs); 3880 return (error); 3881 } 3882 top: 3883 /* 3884 * Attempt to lock directory; fail if entry already exists. 3885 */ 3886 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 3887 if (error) { 3888 zfs_acl_ids_free(&acl_ids); 3889 ZFS_EXIT(zfsvfs); 3890 return (error); 3891 } 3892 3893 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3894 zfs_acl_ids_free(&acl_ids); 3895 zfs_dirent_unlock(dl); 3896 ZFS_EXIT(zfsvfs); 3897 return (error); 3898 } 3899 3900 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 3901 zfs_acl_ids_free(&acl_ids); 3902 zfs_dirent_unlock(dl); 3903 ZFS_EXIT(zfsvfs); 3904 return (SET_ERROR(EDQUOT)); 3905 } 3906 tx = dmu_tx_create(zfsvfs->z_os); 3907 fuid_dirtied = zfsvfs->z_fuid_dirty; 3908 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3909 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3910 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 3911 ZFS_SA_BASE_ATTR_SIZE + len); 3912 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 3913 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3914 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 3915 acl_ids.z_aclp->z_acl_bytes); 3916 } 3917 if (fuid_dirtied) 3918 zfs_fuid_txhold(zfsvfs, tx); 3919 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 3920 if (error) { 3921 zfs_dirent_unlock(dl); 3922 if (error == ERESTART) { 3923 waited = B_TRUE; 3924 dmu_tx_wait(tx); 3925 dmu_tx_abort(tx); 3926 goto top; 3927 } 3928 zfs_acl_ids_free(&acl_ids); 3929 dmu_tx_abort(tx); 3930 ZFS_EXIT(zfsvfs); 3931 return (error); 3932 } 3933 3934 /* 3935 * Create a new object for the symlink. 3936 * for version 4 ZPL datsets the symlink will be an SA attribute 3937 */ 3938 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 3939 3940 if (fuid_dirtied) 3941 zfs_fuid_sync(zfsvfs, tx); 3942 3943 mutex_enter(&zp->z_lock); 3944 if (zp->z_is_sa) 3945 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 3946 link, len, tx); 3947 else 3948 zfs_sa_symlink(zp, link, len, tx); 3949 mutex_exit(&zp->z_lock); 3950 3951 zp->z_size = len; 3952 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 3953 &zp->z_size, sizeof (zp->z_size), tx); 3954 /* 3955 * Insert the new object into the directory. 3956 */ 3957 (void) zfs_link_create(dl, zp, tx, ZNEW); 3958 3959 if (flags & FIGNORECASE) 3960 txtype |= TX_CI; 3961 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 3962 3963 zfs_acl_ids_free(&acl_ids); 3964 3965 dmu_tx_commit(tx); 3966 3967 zfs_dirent_unlock(dl); 3968 3969 VN_RELE(ZTOV(zp)); 3970 3971 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3972 zil_commit(zilog, 0); 3973 3974 ZFS_EXIT(zfsvfs); 3975 return (error); 3976 } 3977 3978 /* 3979 * Return, in the buffer contained in the provided uio structure, 3980 * the symbolic path referred to by vp. 3981 * 3982 * IN: vp - vnode of symbolic link. 3983 * uio - structure to contain the link path. 3984 * cr - credentials of caller. 3985 * ct - caller context 3986 * 3987 * OUT: uio - structure containing the link path. 3988 * 3989 * RETURN: 0 on success, error code on failure. 3990 * 3991 * Timestamps: 3992 * vp - atime updated 3993 */ 3994 /* ARGSUSED */ 3995 static int 3996 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3997 { 3998 znode_t *zp = VTOZ(vp); 3999 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4000 int error; 4001 4002 ZFS_ENTER(zfsvfs); 4003 ZFS_VERIFY_ZP(zp); 4004 4005 mutex_enter(&zp->z_lock); 4006 if (zp->z_is_sa) 4007 error = sa_lookup_uio(zp->z_sa_hdl, 4008 SA_ZPL_SYMLINK(zfsvfs), uio); 4009 else 4010 error = zfs_sa_readlink(zp, uio); 4011 mutex_exit(&zp->z_lock); 4012 4013 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4014 4015 ZFS_EXIT(zfsvfs); 4016 return (error); 4017 } 4018 4019 /* 4020 * Insert a new entry into directory tdvp referencing svp. 4021 * 4022 * IN: tdvp - Directory to contain new entry. 4023 * svp - vnode of new entry. 4024 * name - name of new entry. 4025 * cr - credentials of caller. 4026 * ct - caller context 4027 * 4028 * RETURN: 0 on success, error code on failure. 4029 * 4030 * Timestamps: 4031 * tdvp - ctime|mtime updated 4032 * svp - ctime updated 4033 */ 4034 /* ARGSUSED */ 4035 static int 4036 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 4037 caller_context_t *ct, int flags) 4038 { 4039 znode_t *dzp = VTOZ(tdvp); 4040 znode_t *tzp, *szp; 4041 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4042 zilog_t *zilog; 4043 zfs_dirlock_t *dl; 4044 dmu_tx_t *tx; 4045 vnode_t *realvp; 4046 int error; 4047 int zf = ZNEW; 4048 uint64_t parent; 4049 uid_t owner; 4050 boolean_t waited = B_FALSE; 4051 4052 ASSERT(tdvp->v_type == VDIR); 4053 4054 ZFS_ENTER(zfsvfs); 4055 ZFS_VERIFY_ZP(dzp); 4056 zilog = zfsvfs->z_log; 4057 4058 if (VOP_REALVP(svp, &realvp, ct) == 0) 4059 svp = realvp; 4060 4061 /* 4062 * POSIX dictates that we return EPERM here. 4063 * Better choices include ENOTSUP or EISDIR. 4064 */ 4065 if (svp->v_type == VDIR) { 4066 ZFS_EXIT(zfsvfs); 4067 return (SET_ERROR(EPERM)); 4068 } 4069 4070 szp = VTOZ(svp); 4071 ZFS_VERIFY_ZP(szp); 4072 4073 /* 4074 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the 4075 * ctldir appear to have the same v_vfsp. 4076 */ 4077 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) { 4078 ZFS_EXIT(zfsvfs); 4079 return (SET_ERROR(EXDEV)); 4080 } 4081 4082 /* Prevent links to .zfs/shares files */ 4083 4084 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 4085 &parent, sizeof (uint64_t))) != 0) { 4086 ZFS_EXIT(zfsvfs); 4087 return (error); 4088 } 4089 if (parent == zfsvfs->z_shares_dir) { 4090 ZFS_EXIT(zfsvfs); 4091 return (SET_ERROR(EPERM)); 4092 } 4093 4094 if (zfsvfs->z_utf8 && u8_validate(name, 4095 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 4096 ZFS_EXIT(zfsvfs); 4097 return (SET_ERROR(EILSEQ)); 4098 } 4099 if (flags & FIGNORECASE) 4100 zf |= ZCILOOK; 4101 4102 /* 4103 * We do not support links between attributes and non-attributes 4104 * because of the potential security risk of creating links 4105 * into "normal" file space in order to circumvent restrictions 4106 * imposed in attribute space. 4107 */ 4108 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 4109 ZFS_EXIT(zfsvfs); 4110 return (SET_ERROR(EINVAL)); 4111 } 4112 4113 4114 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER); 4115 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) { 4116 ZFS_EXIT(zfsvfs); 4117 return (SET_ERROR(EPERM)); 4118 } 4119 4120 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 4121 ZFS_EXIT(zfsvfs); 4122 return (error); 4123 } 4124 4125 top: 4126 /* 4127 * Attempt to lock directory; fail if entry already exists. 4128 */ 4129 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 4130 if (error) { 4131 ZFS_EXIT(zfsvfs); 4132 return (error); 4133 } 4134 4135 tx = dmu_tx_create(zfsvfs->z_os); 4136 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 4137 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 4138 zfs_sa_upgrade_txholds(tx, szp); 4139 zfs_sa_upgrade_txholds(tx, dzp); 4140 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 4141 if (error) { 4142 zfs_dirent_unlock(dl); 4143 if (error == ERESTART) { 4144 waited = B_TRUE; 4145 dmu_tx_wait(tx); 4146 dmu_tx_abort(tx); 4147 goto top; 4148 } 4149 dmu_tx_abort(tx); 4150 ZFS_EXIT(zfsvfs); 4151 return (error); 4152 } 4153 4154 error = zfs_link_create(dl, szp, tx, 0); 4155 4156 if (error == 0) { 4157 uint64_t txtype = TX_LINK; 4158 if (flags & FIGNORECASE) 4159 txtype |= TX_CI; 4160 zfs_log_link(zilog, tx, txtype, dzp, szp, name); 4161 } 4162 4163 dmu_tx_commit(tx); 4164 4165 zfs_dirent_unlock(dl); 4166 4167 if (error == 0) { 4168 vnevent_link(svp, ct); 4169 } 4170 4171 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4172 zil_commit(zilog, 0); 4173 4174 ZFS_EXIT(zfsvfs); 4175 return (error); 4176 } 4177 4178 /* 4179 * zfs_null_putapage() is used when the file system has been force 4180 * unmounted. It just drops the pages. 4181 */ 4182 /* ARGSUSED */ 4183 static int 4184 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4185 size_t *lenp, int flags, cred_t *cr) 4186 { 4187 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 4188 return (0); 4189 } 4190 4191 /* 4192 * Push a page out to disk, klustering if possible. 4193 * 4194 * IN: vp - file to push page to. 4195 * pp - page to push. 4196 * flags - additional flags. 4197 * cr - credentials of caller. 4198 * 4199 * OUT: offp - start of range pushed. 4200 * lenp - len of range pushed. 4201 * 4202 * RETURN: 0 on success, error code on failure. 4203 * 4204 * NOTE: callers must have locked the page to be pushed. On 4205 * exit, the page (and all other pages in the kluster) must be 4206 * unlocked. 4207 */ 4208 /* ARGSUSED */ 4209 static int 4210 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4211 size_t *lenp, int flags, cred_t *cr) 4212 { 4213 znode_t *zp = VTOZ(vp); 4214 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4215 dmu_tx_t *tx; 4216 u_offset_t off, koff; 4217 size_t len, klen; 4218 int err; 4219 4220 off = pp->p_offset; 4221 len = PAGESIZE; 4222 /* 4223 * If our blocksize is bigger than the page size, try to kluster 4224 * multiple pages so that we write a full block (thus avoiding 4225 * a read-modify-write). 4226 */ 4227 if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 4228 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 4229 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 4230 ASSERT(koff <= zp->z_size); 4231 if (koff + klen > zp->z_size) 4232 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 4233 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 4234 } 4235 ASSERT3U(btop(len), ==, btopr(len)); 4236 4237 /* 4238 * Can't push pages past end-of-file. 4239 */ 4240 if (off >= zp->z_size) { 4241 /* ignore all pages */ 4242 err = 0; 4243 goto out; 4244 } else if (off + len > zp->z_size) { 4245 int npages = btopr(zp->z_size - off); 4246 page_t *trunc; 4247 4248 page_list_break(&pp, &trunc, npages); 4249 /* ignore pages past end of file */ 4250 if (trunc) 4251 pvn_write_done(trunc, flags); 4252 len = zp->z_size - off; 4253 } 4254 4255 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 4256 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 4257 err = SET_ERROR(EDQUOT); 4258 goto out; 4259 } 4260 tx = dmu_tx_create(zfsvfs->z_os); 4261 dmu_tx_hold_write(tx, zp->z_id, off, len); 4262 4263 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4264 zfs_sa_upgrade_txholds(tx, zp); 4265 err = dmu_tx_assign(tx, TXG_WAIT); 4266 if (err != 0) { 4267 dmu_tx_abort(tx); 4268 goto out; 4269 } 4270 4271 if (zp->z_blksz <= PAGESIZE) { 4272 caddr_t va = zfs_map_page(pp, S_READ); 4273 ASSERT3U(len, <=, PAGESIZE); 4274 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 4275 zfs_unmap_page(pp, va); 4276 } else { 4277 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 4278 } 4279 4280 if (err == 0) { 4281 uint64_t mtime[2], ctime[2]; 4282 sa_bulk_attr_t bulk[3]; 4283 int count = 0; 4284 4285 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 4286 &mtime, 16); 4287 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 4288 &ctime, 16); 4289 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 4290 &zp->z_pflags, 8); 4291 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 4292 B_TRUE); 4293 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 4294 } 4295 dmu_tx_commit(tx); 4296 4297 out: 4298 pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4299 if (offp) 4300 *offp = off; 4301 if (lenp) 4302 *lenp = len; 4303 4304 return (err); 4305 } 4306 4307 /* 4308 * Copy the portion of the file indicated from pages into the file. 4309 * The pages are stored in a page list attached to the files vnode. 4310 * 4311 * IN: vp - vnode of file to push page data to. 4312 * off - position in file to put data. 4313 * len - amount of data to write. 4314 * flags - flags to control the operation. 4315 * cr - credentials of caller. 4316 * ct - caller context. 4317 * 4318 * RETURN: 0 on success, error code on failure. 4319 * 4320 * Timestamps: 4321 * vp - ctime|mtime updated 4322 */ 4323 /*ARGSUSED*/ 4324 static int 4325 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 4326 caller_context_t *ct) 4327 { 4328 znode_t *zp = VTOZ(vp); 4329 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4330 page_t *pp; 4331 size_t io_len; 4332 u_offset_t io_off; 4333 uint_t blksz; 4334 rl_t *rl; 4335 int error = 0; 4336 4337 ZFS_ENTER(zfsvfs); 4338 ZFS_VERIFY_ZP(zp); 4339 4340 /* 4341 * There's nothing to do if no data is cached. 4342 */ 4343 if (!vn_has_cached_data(vp)) { 4344 ZFS_EXIT(zfsvfs); 4345 return (0); 4346 } 4347 4348 /* 4349 * Align this request to the file block size in case we kluster. 4350 * XXX - this can result in pretty aggresive locking, which can 4351 * impact simultanious read/write access. One option might be 4352 * to break up long requests (len == 0) into block-by-block 4353 * operations to get narrower locking. 4354 */ 4355 blksz = zp->z_blksz; 4356 if (ISP2(blksz)) 4357 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 4358 else 4359 io_off = 0; 4360 if (len > 0 && ISP2(blksz)) 4361 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 4362 else 4363 io_len = 0; 4364 4365 if (io_len == 0) { 4366 /* 4367 * Search the entire vp list for pages >= io_off. 4368 */ 4369 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 4370 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 4371 goto out; 4372 } 4373 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 4374 4375 if (off > zp->z_size) { 4376 /* past end of file */ 4377 zfs_range_unlock(rl); 4378 ZFS_EXIT(zfsvfs); 4379 return (0); 4380 } 4381 4382 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 4383 4384 for (off = io_off; io_off < off + len; io_off += io_len) { 4385 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 4386 pp = page_lookup(vp, io_off, 4387 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4388 } else { 4389 pp = page_lookup_nowait(vp, io_off, 4390 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4391 } 4392 4393 if (pp != NULL && pvn_getdirty(pp, flags)) { 4394 int err; 4395 4396 /* 4397 * Found a dirty page to push 4398 */ 4399 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 4400 if (err) 4401 error = err; 4402 } else { 4403 io_len = PAGESIZE; 4404 } 4405 } 4406 out: 4407 zfs_range_unlock(rl); 4408 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4409 zil_commit(zfsvfs->z_log, zp->z_id); 4410 ZFS_EXIT(zfsvfs); 4411 return (error); 4412 } 4413 4414 /*ARGSUSED*/ 4415 void 4416 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4417 { 4418 znode_t *zp = VTOZ(vp); 4419 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4420 int error; 4421 4422 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 4423 if (zp->z_sa_hdl == NULL) { 4424 /* 4425 * The fs has been unmounted, or we did a 4426 * suspend/resume and this file no longer exists. 4427 */ 4428 if (vn_has_cached_data(vp)) { 4429 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 4430 B_INVAL, cr); 4431 } 4432 4433 mutex_enter(&zp->z_lock); 4434 mutex_enter(&vp->v_lock); 4435 ASSERT(vp->v_count == 1); 4436 vp->v_count = 0; 4437 mutex_exit(&vp->v_lock); 4438 mutex_exit(&zp->z_lock); 4439 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4440 zfs_znode_free(zp); 4441 return; 4442 } 4443 4444 /* 4445 * Attempt to push any data in the page cache. If this fails 4446 * we will get kicked out later in zfs_zinactive(). 4447 */ 4448 if (vn_has_cached_data(vp)) { 4449 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 4450 cr); 4451 } 4452 4453 if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4454 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4455 4456 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4457 zfs_sa_upgrade_txholds(tx, zp); 4458 error = dmu_tx_assign(tx, TXG_WAIT); 4459 if (error) { 4460 dmu_tx_abort(tx); 4461 } else { 4462 mutex_enter(&zp->z_lock); 4463 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 4464 (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4465 zp->z_atime_dirty = 0; 4466 mutex_exit(&zp->z_lock); 4467 dmu_tx_commit(tx); 4468 } 4469 } 4470 4471 zfs_zinactive(zp); 4472 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4473 } 4474 4475 /* 4476 * Bounds-check the seek operation. 4477 * 4478 * IN: vp - vnode seeking within 4479 * ooff - old file offset 4480 * noffp - pointer to new file offset 4481 * ct - caller context 4482 * 4483 * RETURN: 0 on success, EINVAL if new offset invalid. 4484 */ 4485 /* ARGSUSED */ 4486 static int 4487 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 4488 caller_context_t *ct) 4489 { 4490 if (vp->v_type == VDIR) 4491 return (0); 4492 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4493 } 4494 4495 /* 4496 * Pre-filter the generic locking function to trap attempts to place 4497 * a mandatory lock on a memory mapped file. 4498 */ 4499 static int 4500 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 4501 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4502 { 4503 znode_t *zp = VTOZ(vp); 4504 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4505 4506 ZFS_ENTER(zfsvfs); 4507 ZFS_VERIFY_ZP(zp); 4508 4509 /* 4510 * We are following the UFS semantics with respect to mapcnt 4511 * here: If we see that the file is mapped already, then we will 4512 * return an error, but we don't worry about races between this 4513 * function and zfs_map(). 4514 */ 4515 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4516 ZFS_EXIT(zfsvfs); 4517 return (SET_ERROR(EAGAIN)); 4518 } 4519 ZFS_EXIT(zfsvfs); 4520 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4521 } 4522 4523 /* 4524 * If we can't find a page in the cache, we will create a new page 4525 * and fill it with file data. For efficiency, we may try to fill 4526 * multiple pages at once (klustering) to fill up the supplied page 4527 * list. Note that the pages to be filled are held with an exclusive 4528 * lock to prevent access by other threads while they are being filled. 4529 */ 4530 static int 4531 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4532 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4533 { 4534 znode_t *zp = VTOZ(vp); 4535 page_t *pp, *cur_pp; 4536 objset_t *os = zp->z_zfsvfs->z_os; 4537 u_offset_t io_off, total; 4538 size_t io_len; 4539 int err; 4540 4541 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 4542 /* 4543 * We only have a single page, don't bother klustering 4544 */ 4545 io_off = off; 4546 io_len = PAGESIZE; 4547 pp = page_create_va(vp, io_off, io_len, 4548 PG_EXCL | PG_WAIT, seg, addr); 4549 } else { 4550 /* 4551 * Try to find enough pages to fill the page list 4552 */ 4553 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 4554 &io_len, off, plsz, 0); 4555 } 4556 if (pp == NULL) { 4557 /* 4558 * The page already exists, nothing to do here. 4559 */ 4560 *pl = NULL; 4561 return (0); 4562 } 4563 4564 /* 4565 * Fill the pages in the kluster. 4566 */ 4567 cur_pp = pp; 4568 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 4569 caddr_t va; 4570 4571 ASSERT3U(io_off, ==, cur_pp->p_offset); 4572 va = zfs_map_page(cur_pp, S_WRITE); 4573 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 4574 DMU_READ_PREFETCH); 4575 zfs_unmap_page(cur_pp, va); 4576 if (err) { 4577 /* On error, toss the entire kluster */ 4578 pvn_read_done(pp, B_ERROR); 4579 /* convert checksum errors into IO errors */ 4580 if (err == ECKSUM) 4581 err = SET_ERROR(EIO); 4582 return (err); 4583 } 4584 cur_pp = cur_pp->p_next; 4585 } 4586 4587 /* 4588 * Fill in the page list array from the kluster starting 4589 * from the desired offset `off'. 4590 * NOTE: the page list will always be null terminated. 4591 */ 4592 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 4593 ASSERT(pl == NULL || (*pl)->p_offset == off); 4594 4595 return (0); 4596 } 4597 4598 /* 4599 * Return pointers to the pages for the file region [off, off + len] 4600 * in the pl array. If plsz is greater than len, this function may 4601 * also return page pointers from after the specified region 4602 * (i.e. the region [off, off + plsz]). These additional pages are 4603 * only returned if they are already in the cache, or were created as 4604 * part of a klustered read. 4605 * 4606 * IN: vp - vnode of file to get data from. 4607 * off - position in file to get data from. 4608 * len - amount of data to retrieve. 4609 * plsz - length of provided page list. 4610 * seg - segment to obtain pages for. 4611 * addr - virtual address of fault. 4612 * rw - mode of created pages. 4613 * cr - credentials of caller. 4614 * ct - caller context. 4615 * 4616 * OUT: protp - protection mode of created pages. 4617 * pl - list of pages created. 4618 * 4619 * RETURN: 0 on success, error code on failure. 4620 * 4621 * Timestamps: 4622 * vp - atime updated 4623 */ 4624 /* ARGSUSED */ 4625 static int 4626 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4627 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 4628 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4629 { 4630 znode_t *zp = VTOZ(vp); 4631 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4632 page_t **pl0 = pl; 4633 int err = 0; 4634 4635 /* we do our own caching, faultahead is unnecessary */ 4636 if (pl == NULL) 4637 return (0); 4638 else if (len > plsz) 4639 len = plsz; 4640 else 4641 len = P2ROUNDUP(len, PAGESIZE); 4642 ASSERT(plsz >= len); 4643 4644 ZFS_ENTER(zfsvfs); 4645 ZFS_VERIFY_ZP(zp); 4646 4647 if (protp) 4648 *protp = PROT_ALL; 4649 4650 /* 4651 * Loop through the requested range [off, off + len) looking 4652 * for pages. If we don't find a page, we will need to create 4653 * a new page and fill it with data from the file. 4654 */ 4655 while (len > 0) { 4656 if (*pl = page_lookup(vp, off, SE_SHARED)) 4657 *(pl+1) = NULL; 4658 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 4659 goto out; 4660 while (*pl) { 4661 ASSERT3U((*pl)->p_offset, ==, off); 4662 off += PAGESIZE; 4663 addr += PAGESIZE; 4664 if (len > 0) { 4665 ASSERT3U(len, >=, PAGESIZE); 4666 len -= PAGESIZE; 4667 } 4668 ASSERT3U(plsz, >=, PAGESIZE); 4669 plsz -= PAGESIZE; 4670 pl++; 4671 } 4672 } 4673 4674 /* 4675 * Fill out the page array with any pages already in the cache. 4676 */ 4677 while (plsz > 0 && 4678 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 4679 off += PAGESIZE; 4680 plsz -= PAGESIZE; 4681 } 4682 out: 4683 if (err) { 4684 /* 4685 * Release any pages we have previously locked. 4686 */ 4687 while (pl > pl0) 4688 page_unlock(*--pl); 4689 } else { 4690 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4691 } 4692 4693 *pl = NULL; 4694 4695 ZFS_EXIT(zfsvfs); 4696 return (err); 4697 } 4698 4699 /* 4700 * Request a memory map for a section of a file. This code interacts 4701 * with common code and the VM system as follows: 4702 * 4703 * - common code calls mmap(), which ends up in smmap_common() 4704 * - this calls VOP_MAP(), which takes you into (say) zfs 4705 * - zfs_map() calls as_map(), passing segvn_create() as the callback 4706 * - segvn_create() creates the new segment and calls VOP_ADDMAP() 4707 * - zfs_addmap() updates z_mapcnt 4708 */ 4709 /*ARGSUSED*/ 4710 static int 4711 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 4712 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 4713 caller_context_t *ct) 4714 { 4715 znode_t *zp = VTOZ(vp); 4716 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4717 segvn_crargs_t vn_a; 4718 int error; 4719 4720 ZFS_ENTER(zfsvfs); 4721 ZFS_VERIFY_ZP(zp); 4722 4723 if ((prot & PROT_WRITE) && (zp->z_pflags & 4724 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 4725 ZFS_EXIT(zfsvfs); 4726 return (SET_ERROR(EPERM)); 4727 } 4728 4729 if ((prot & (PROT_READ | PROT_EXEC)) && 4730 (zp->z_pflags & ZFS_AV_QUARANTINED)) { 4731 ZFS_EXIT(zfsvfs); 4732 return (SET_ERROR(EACCES)); 4733 } 4734 4735 if (vp->v_flag & VNOMAP) { 4736 ZFS_EXIT(zfsvfs); 4737 return (SET_ERROR(ENOSYS)); 4738 } 4739 4740 if (off < 0 || len > MAXOFFSET_T - off) { 4741 ZFS_EXIT(zfsvfs); 4742 return (SET_ERROR(ENXIO)); 4743 } 4744 4745 if (vp->v_type != VREG) { 4746 ZFS_EXIT(zfsvfs); 4747 return (SET_ERROR(ENODEV)); 4748 } 4749 4750 /* 4751 * If file is locked, disallow mapping. 4752 */ 4753 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 4754 ZFS_EXIT(zfsvfs); 4755 return (SET_ERROR(EAGAIN)); 4756 } 4757 4758 as_rangelock(as); 4759 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 4760 if (error != 0) { 4761 as_rangeunlock(as); 4762 ZFS_EXIT(zfsvfs); 4763 return (error); 4764 } 4765 4766 vn_a.vp = vp; 4767 vn_a.offset = (u_offset_t)off; 4768 vn_a.type = flags & MAP_TYPE; 4769 vn_a.prot = prot; 4770 vn_a.maxprot = maxprot; 4771 vn_a.cred = cr; 4772 vn_a.amp = NULL; 4773 vn_a.flags = flags & ~MAP_TYPE; 4774 vn_a.szc = 0; 4775 vn_a.lgrp_mem_policy_flags = 0; 4776 4777 error = as_map(as, *addrp, len, segvn_create, &vn_a); 4778 4779 as_rangeunlock(as); 4780 ZFS_EXIT(zfsvfs); 4781 return (error); 4782 } 4783 4784 /* ARGSUSED */ 4785 static int 4786 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 4787 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 4788 caller_context_t *ct) 4789 { 4790 uint64_t pages = btopr(len); 4791 4792 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4793 return (0); 4794 } 4795 4796 /* 4797 * The reason we push dirty pages as part of zfs_delmap() is so that we get a 4798 * more accurate mtime for the associated file. Since we don't have a way of 4799 * detecting when the data was actually modified, we have to resort to 4800 * heuristics. If an explicit msync() is done, then we mark the mtime when the 4801 * last page is pushed. The problem occurs when the msync() call is omitted, 4802 * which by far the most common case: 4803 * 4804 * open() 4805 * mmap() 4806 * <modify memory> 4807 * munmap() 4808 * close() 4809 * <time lapse> 4810 * putpage() via fsflush 4811 * 4812 * If we wait until fsflush to come along, we can have a modification time that 4813 * is some arbitrary point in the future. In order to prevent this in the 4814 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 4815 * torn down. 4816 */ 4817 /* ARGSUSED */ 4818 static int 4819 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 4820 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 4821 caller_context_t *ct) 4822 { 4823 uint64_t pages = btopr(len); 4824 4825 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 4826 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 4827 4828 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 4829 vn_has_cached_data(vp)) 4830 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 4831 4832 return (0); 4833 } 4834 4835 /* 4836 * Free or allocate space in a file. Currently, this function only 4837 * supports the `F_FREESP' command. However, this command is somewhat 4838 * misnamed, as its functionality includes the ability to allocate as 4839 * well as free space. 4840 * 4841 * IN: vp - vnode of file to free data in. 4842 * cmd - action to take (only F_FREESP supported). 4843 * bfp - section of file to free/alloc. 4844 * flag - current file open mode flags. 4845 * offset - current file offset. 4846 * cr - credentials of caller [UNUSED]. 4847 * ct - caller context. 4848 * 4849 * RETURN: 0 on success, error code on failure. 4850 * 4851 * Timestamps: 4852 * vp - ctime|mtime updated 4853 */ 4854 /* ARGSUSED */ 4855 static int 4856 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4857 offset_t offset, cred_t *cr, caller_context_t *ct) 4858 { 4859 znode_t *zp = VTOZ(vp); 4860 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4861 uint64_t off, len; 4862 int error; 4863 4864 ZFS_ENTER(zfsvfs); 4865 ZFS_VERIFY_ZP(zp); 4866 4867 if (cmd != F_FREESP) { 4868 ZFS_EXIT(zfsvfs); 4869 return (SET_ERROR(EINVAL)); 4870 } 4871 4872 /* 4873 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our 4874 * callers might not be able to detect properly that we are read-only, 4875 * so check it explicitly here. 4876 */ 4877 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 4878 ZFS_EXIT(zfsvfs); 4879 return (SET_ERROR(EROFS)); 4880 } 4881 4882 if (error = convoff(vp, bfp, 0, offset)) { 4883 ZFS_EXIT(zfsvfs); 4884 return (error); 4885 } 4886 4887 if (bfp->l_len < 0) { 4888 ZFS_EXIT(zfsvfs); 4889 return (SET_ERROR(EINVAL)); 4890 } 4891 4892 off = bfp->l_start; 4893 len = bfp->l_len; /* 0 means from off to end of file */ 4894 4895 error = zfs_freesp(zp, off, len, flag, TRUE); 4896 4897 if (error == 0 && off == 0 && len == 0) 4898 vnevent_truncate(ZTOV(zp), ct); 4899 4900 ZFS_EXIT(zfsvfs); 4901 return (error); 4902 } 4903 4904 /*ARGSUSED*/ 4905 static int 4906 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4907 { 4908 znode_t *zp = VTOZ(vp); 4909 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4910 uint32_t gen; 4911 uint64_t gen64; 4912 uint64_t object = zp->z_id; 4913 zfid_short_t *zfid; 4914 int size, i, error; 4915 4916 ZFS_ENTER(zfsvfs); 4917 ZFS_VERIFY_ZP(zp); 4918 4919 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 4920 &gen64, sizeof (uint64_t))) != 0) { 4921 ZFS_EXIT(zfsvfs); 4922 return (error); 4923 } 4924 4925 gen = (uint32_t)gen64; 4926 4927 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4928 if (fidp->fid_len < size) { 4929 fidp->fid_len = size; 4930 ZFS_EXIT(zfsvfs); 4931 return (SET_ERROR(ENOSPC)); 4932 } 4933 4934 zfid = (zfid_short_t *)fidp; 4935 4936 zfid->zf_len = size; 4937 4938 for (i = 0; i < sizeof (zfid->zf_object); i++) 4939 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4940 4941 /* Must have a non-zero generation number to distinguish from .zfs */ 4942 if (gen == 0) 4943 gen = 1; 4944 for (i = 0; i < sizeof (zfid->zf_gen); i++) 4945 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4946 4947 if (size == LONG_FID_LEN) { 4948 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4949 zfid_long_t *zlfid; 4950 4951 zlfid = (zfid_long_t *)fidp; 4952 4953 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4954 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4955 4956 /* XXX - this should be the generation number for the objset */ 4957 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4958 zlfid->zf_setgen[i] = 0; 4959 } 4960 4961 ZFS_EXIT(zfsvfs); 4962 return (0); 4963 } 4964 4965 static int 4966 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 4967 caller_context_t *ct) 4968 { 4969 znode_t *zp, *xzp; 4970 zfsvfs_t *zfsvfs; 4971 zfs_dirlock_t *dl; 4972 int error; 4973 4974 switch (cmd) { 4975 case _PC_LINK_MAX: 4976 *valp = ULONG_MAX; 4977 return (0); 4978 4979 case _PC_FILESIZEBITS: 4980 *valp = 64; 4981 return (0); 4982 4983 case _PC_XATTR_EXISTS: 4984 zp = VTOZ(vp); 4985 zfsvfs = zp->z_zfsvfs; 4986 ZFS_ENTER(zfsvfs); 4987 ZFS_VERIFY_ZP(zp); 4988 *valp = 0; 4989 error = zfs_dirent_lock(&dl, zp, "", &xzp, 4990 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4991 if (error == 0) { 4992 zfs_dirent_unlock(dl); 4993 if (!zfs_dirempty(xzp)) 4994 *valp = 1; 4995 VN_RELE(ZTOV(xzp)); 4996 } else if (error == ENOENT) { 4997 /* 4998 * If there aren't extended attributes, it's the 4999 * same as having zero of them. 5000 */ 5001 error = 0; 5002 } 5003 ZFS_EXIT(zfsvfs); 5004 return (error); 5005 5006 case _PC_SATTR_ENABLED: 5007 case _PC_SATTR_EXISTS: 5008 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 5009 (vp->v_type == VREG || vp->v_type == VDIR); 5010 return (0); 5011 5012 case _PC_ACCESS_FILTERING: 5013 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 5014 vp->v_type == VDIR; 5015 return (0); 5016 5017 case _PC_ACL_ENABLED: 5018 *valp = _ACL_ACE_ENABLED; 5019 return (0); 5020 5021 case _PC_MIN_HOLE_SIZE: 5022 *valp = (ulong_t)SPA_MINBLOCKSIZE; 5023 return (0); 5024 5025 case _PC_TIMESTAMP_RESOLUTION: 5026 /* nanosecond timestamp resolution */ 5027 *valp = 1L; 5028 return (0); 5029 5030 default: 5031 return (fs_pathconf(vp, cmd, valp, cr, ct)); 5032 } 5033 } 5034 5035 /*ARGSUSED*/ 5036 static int 5037 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 5038 caller_context_t *ct) 5039 { 5040 znode_t *zp = VTOZ(vp); 5041 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5042 int error; 5043 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 5044 5045 ZFS_ENTER(zfsvfs); 5046 ZFS_VERIFY_ZP(zp); 5047 error = zfs_getacl(zp, vsecp, skipaclchk, cr); 5048 ZFS_EXIT(zfsvfs); 5049 5050 return (error); 5051 } 5052 5053 /*ARGSUSED*/ 5054 static int 5055 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 5056 caller_context_t *ct) 5057 { 5058 znode_t *zp = VTOZ(vp); 5059 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5060 int error; 5061 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 5062 zilog_t *zilog = zfsvfs->z_log; 5063 5064 ZFS_ENTER(zfsvfs); 5065 ZFS_VERIFY_ZP(zp); 5066 5067 error = zfs_setacl(zp, vsecp, skipaclchk, cr); 5068 5069 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 5070 zil_commit(zilog, 0); 5071 5072 ZFS_EXIT(zfsvfs); 5073 return (error); 5074 } 5075 5076 /* 5077 * The smallest read we may consider to loan out an arcbuf. 5078 * This must be a power of 2. 5079 */ 5080 int zcr_blksz_min = (1 << 10); /* 1K */ 5081 /* 5082 * If set to less than the file block size, allow loaning out of an 5083 * arcbuf for a partial block read. This must be a power of 2. 5084 */ 5085 int zcr_blksz_max = (1 << 17); /* 128K */ 5086 5087 /*ARGSUSED*/ 5088 static int 5089 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 5090 caller_context_t *ct) 5091 { 5092 znode_t *zp = VTOZ(vp); 5093 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5094 int max_blksz = zfsvfs->z_max_blksz; 5095 uio_t *uio = &xuio->xu_uio; 5096 ssize_t size = uio->uio_resid; 5097 offset_t offset = uio->uio_loffset; 5098 int blksz; 5099 int fullblk, i; 5100 arc_buf_t *abuf; 5101 ssize_t maxsize; 5102 int preamble, postamble; 5103 5104 if (xuio->xu_type != UIOTYPE_ZEROCOPY) 5105 return (SET_ERROR(EINVAL)); 5106 5107 ZFS_ENTER(zfsvfs); 5108 ZFS_VERIFY_ZP(zp); 5109 switch (ioflag) { 5110 case UIO_WRITE: 5111 /* 5112 * Loan out an arc_buf for write if write size is bigger than 5113 * max_blksz, and the file's block size is also max_blksz. 5114 */ 5115 blksz = max_blksz; 5116 if (size < blksz || zp->z_blksz != blksz) { 5117 ZFS_EXIT(zfsvfs); 5118 return (SET_ERROR(EINVAL)); 5119 } 5120 /* 5121 * Caller requests buffers for write before knowing where the 5122 * write offset might be (e.g. NFS TCP write). 5123 */ 5124 if (offset == -1) { 5125 preamble = 0; 5126 } else { 5127 preamble = P2PHASE(offset, blksz); 5128 if (preamble) { 5129 preamble = blksz - preamble; 5130 size -= preamble; 5131 } 5132 } 5133 5134 postamble = P2PHASE(size, blksz); 5135 size -= postamble; 5136 5137 fullblk = size / blksz; 5138 (void) dmu_xuio_init(xuio, 5139 (preamble != 0) + fullblk + (postamble != 0)); 5140 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 5141 int, postamble, int, 5142 (preamble != 0) + fullblk + (postamble != 0)); 5143 5144 /* 5145 * Have to fix iov base/len for partial buffers. They 5146 * currently represent full arc_buf's. 5147 */ 5148 if (preamble) { 5149 /* data begins in the middle of the arc_buf */ 5150 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5151 blksz); 5152 ASSERT(abuf); 5153 (void) dmu_xuio_add(xuio, abuf, 5154 blksz - preamble, preamble); 5155 } 5156 5157 for (i = 0; i < fullblk; i++) { 5158 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5159 blksz); 5160 ASSERT(abuf); 5161 (void) dmu_xuio_add(xuio, abuf, 0, blksz); 5162 } 5163 5164 if (postamble) { 5165 /* data ends in the middle of the arc_buf */ 5166 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5167 blksz); 5168 ASSERT(abuf); 5169 (void) dmu_xuio_add(xuio, abuf, 0, postamble); 5170 } 5171 break; 5172 case UIO_READ: 5173 /* 5174 * Loan out an arc_buf for read if the read size is larger than 5175 * the current file block size. Block alignment is not 5176 * considered. Partial arc_buf will be loaned out for read. 5177 */ 5178 blksz = zp->z_blksz; 5179 if (blksz < zcr_blksz_min) 5180 blksz = zcr_blksz_min; 5181 if (blksz > zcr_blksz_max) 5182 blksz = zcr_blksz_max; 5183 /* avoid potential complexity of dealing with it */ 5184 if (blksz > max_blksz) { 5185 ZFS_EXIT(zfsvfs); 5186 return (SET_ERROR(EINVAL)); 5187 } 5188 5189 maxsize = zp->z_size - uio->uio_loffset; 5190 if (size > maxsize) 5191 size = maxsize; 5192 5193 if (size < blksz || vn_has_cached_data(vp)) { 5194 ZFS_EXIT(zfsvfs); 5195 return (SET_ERROR(EINVAL)); 5196 } 5197 break; 5198 default: 5199 ZFS_EXIT(zfsvfs); 5200 return (SET_ERROR(EINVAL)); 5201 } 5202 5203 uio->uio_extflg = UIO_XUIO; 5204 XUIO_XUZC_RW(xuio) = ioflag; 5205 ZFS_EXIT(zfsvfs); 5206 return (0); 5207 } 5208 5209 /*ARGSUSED*/ 5210 static int 5211 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 5212 { 5213 int i; 5214 arc_buf_t *abuf; 5215 int ioflag = XUIO_XUZC_RW(xuio); 5216 5217 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 5218 5219 i = dmu_xuio_cnt(xuio); 5220 while (i-- > 0) { 5221 abuf = dmu_xuio_arcbuf(xuio, i); 5222 /* 5223 * if abuf == NULL, it must be a write buffer 5224 * that has been returned in zfs_write(). 5225 */ 5226 if (abuf) 5227 dmu_return_arcbuf(abuf); 5228 ASSERT(abuf || ioflag == UIO_WRITE); 5229 } 5230 5231 dmu_xuio_fini(xuio); 5232 return (0); 5233 } 5234 5235 /* 5236 * Predeclare these here so that the compiler assumes that 5237 * this is an "old style" function declaration that does 5238 * not include arguments => we won't get type mismatch errors 5239 * in the initializations that follow. 5240 */ 5241 static int zfs_inval(); 5242 static int zfs_isdir(); 5243 5244 static int 5245 zfs_inval() 5246 { 5247 return (SET_ERROR(EINVAL)); 5248 } 5249 5250 static int 5251 zfs_isdir() 5252 { 5253 return (SET_ERROR(EISDIR)); 5254 } 5255 /* 5256 * Directory vnode operations template 5257 */ 5258 vnodeops_t *zfs_dvnodeops; 5259 const fs_operation_def_t zfs_dvnodeops_template[] = { 5260 VOPNAME_OPEN, { .vop_open = zfs_open }, 5261 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5262 VOPNAME_READ, { .error = zfs_isdir }, 5263 VOPNAME_WRITE, { .error = zfs_isdir }, 5264 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5265 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5266 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5267 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5268 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5269 VOPNAME_CREATE, { .vop_create = zfs_create }, 5270 VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 5271 VOPNAME_LINK, { .vop_link = zfs_link }, 5272 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5273 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 5274 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 5275 VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 5276 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 5277 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5278 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5279 VOPNAME_FID, { .vop_fid = zfs_fid }, 5280 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5281 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5282 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5283 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5284 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5285 NULL, NULL 5286 }; 5287 5288 /* 5289 * Regular file vnode operations template 5290 */ 5291 vnodeops_t *zfs_fvnodeops; 5292 const fs_operation_def_t zfs_fvnodeops_template[] = { 5293 VOPNAME_OPEN, { .vop_open = zfs_open }, 5294 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5295 VOPNAME_READ, { .vop_read = zfs_read }, 5296 VOPNAME_WRITE, { .vop_write = zfs_write }, 5297 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5298 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5299 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5300 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5301 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5302 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5303 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5304 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5305 VOPNAME_FID, { .vop_fid = zfs_fid }, 5306 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5307 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 5308 VOPNAME_SPACE, { .vop_space = zfs_space }, 5309 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 5310 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 5311 VOPNAME_MAP, { .vop_map = zfs_map }, 5312 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 5313 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 5314 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5315 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5316 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5317 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5318 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 5319 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 5320 NULL, NULL 5321 }; 5322 5323 /* 5324 * Symbolic link vnode operations template 5325 */ 5326 vnodeops_t *zfs_symvnodeops; 5327 const fs_operation_def_t zfs_symvnodeops_template[] = { 5328 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5329 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5330 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5331 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5332 VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 5333 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5334 VOPNAME_FID, { .vop_fid = zfs_fid }, 5335 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5336 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5337 NULL, NULL 5338 }; 5339 5340 /* 5341 * special share hidden files vnode operations template 5342 */ 5343 vnodeops_t *zfs_sharevnodeops; 5344 const fs_operation_def_t zfs_sharevnodeops_template[] = { 5345 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5346 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5347 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5348 VOPNAME_FID, { .vop_fid = zfs_fid }, 5349 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5350 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5351 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5352 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5353 NULL, NULL 5354 }; 5355 5356 /* 5357 * Extended attribute directory vnode operations template 5358 * 5359 * This template is identical to the directory vnodes 5360 * operation template except for restricted operations: 5361 * VOP_MKDIR() 5362 * VOP_SYMLINK() 5363 * 5364 * Note that there are other restrictions embedded in: 5365 * zfs_create() - restrict type to VREG 5366 * zfs_link() - no links into/out of attribute space 5367 * zfs_rename() - no moves into/out of attribute space 5368 */ 5369 vnodeops_t *zfs_xdvnodeops; 5370 const fs_operation_def_t zfs_xdvnodeops_template[] = { 5371 VOPNAME_OPEN, { .vop_open = zfs_open }, 5372 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5373 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5374 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5375 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5376 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5377 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5378 VOPNAME_CREATE, { .vop_create = zfs_create }, 5379 VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 5380 VOPNAME_LINK, { .vop_link = zfs_link }, 5381 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5382 VOPNAME_MKDIR, { .error = zfs_inval }, 5383 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 5384 VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 5385 VOPNAME_SYMLINK, { .error = zfs_inval }, 5386 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5387 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5388 VOPNAME_FID, { .vop_fid = zfs_fid }, 5389 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5390 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5391 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5392 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5393 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5394 NULL, NULL 5395 }; 5396 5397 /* 5398 * Error vnode operations template 5399 */ 5400 vnodeops_t *zfs_evnodeops; 5401 const fs_operation_def_t zfs_evnodeops_template[] = { 5402 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5403 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5404 NULL, NULL 5405 }; 5406