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