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