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