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