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