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