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