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 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/systm.h> 32 #include <sys/sysmacros.h> 33 #include <sys/resource.h> 34 #include <sys/vfs.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 <sys/mman.h> 48 #include <sys/pathname.h> 49 #include <sys/cmn_err.h> 50 #include <sys/errno.h> 51 #include <sys/unistd.h> 52 #include <sys/zfs_vfsops.h> 53 #include <sys/zfs_dir.h> 54 #include <sys/zfs_acl.h> 55 #include <sys/zfs_ioctl.h> 56 #include <sys/fs/zfs.h> 57 #include <sys/dmu.h> 58 #include <sys/spa.h> 59 #include <sys/txg.h> 60 #include <sys/dbuf.h> 61 #include <sys/zap.h> 62 #include <sys/dirent.h> 63 #include <sys/policy.h> 64 #include <sys/sunddi.h> 65 #include <sys/filio.h> 66 #include "fs/fs_subr.h" 67 #include <sys/zfs_ctldir.h> 68 #include <sys/dnlc.h> 69 #include <sys/zfs_rlock.h> 70 71 /* 72 * Programming rules. 73 * 74 * Each vnode op performs some logical unit of work. To do this, the ZPL must 75 * properly lock its in-core state, create a DMU transaction, do the work, 76 * record this work in the intent log (ZIL), commit the DMU transaction, 77 * and wait the the intent log to commit if it's is a synchronous operation. 78 * Morover, the vnode ops must work in both normal and log replay context. 79 * The ordering of events is important to avoid deadlocks and references 80 * to freed memory. The example below illustrates the following Big Rules: 81 * 82 * (1) A check must be made in each zfs thread for a mounted file system. 83 * This is done avoiding races using ZFS_ENTER(zfsvfs). 84 * A ZFS_EXIT(zfsvfs) is needed before all returns. 85 * 86 * (2) VN_RELE() should always be the last thing except for zil_commit() 87 * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 88 * First, if it's the last reference, the vnode/znode 89 * can be freed, so the zp may point to freed memory. Second, the last 90 * reference will call zfs_zinactive(), which may induce a lot of work -- 91 * pushing cached pages (which acquires range locks) and syncing out 92 * cached atime changes. Third, zfs_zinactive() may require a new tx, 93 * which could deadlock the system if you were already holding one. 94 * 95 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 96 * as they can span dmu_tx_assign() calls. 97 * 98 * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign(). 99 * In normal operation, this will be TXG_NOWAIT. During ZIL replay, 100 * it will be a specific txg. Either way, dmu_tx_assign() never blocks. 101 * This is critical because we don't want to block while holding locks. 102 * Note, in particular, that if a lock is sometimes acquired before 103 * the tx assigns, and sometimes after (e.g. z_lock), then failing to 104 * use a non-blocking assign can deadlock the system. The scenario: 105 * 106 * Thread A has grabbed a lock before calling dmu_tx_assign(). 107 * Thread B is in an already-assigned tx, and blocks for this lock. 108 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 109 * forever, because the previous txg can't quiesce until B's tx commits. 110 * 111 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 112 * then drop all locks, call dmu_tx_wait(), and try again. 113 * 114 * (5) If the operation succeeded, generate the intent log entry for it 115 * before dropping locks. This ensures that the ordering of events 116 * in the intent log matches the order in which they actually occurred. 117 * 118 * (6) At the end of each vnode op, the DMU tx must always commit, 119 * regardless of whether there were any errors. 120 * 121 * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 122 * to ensure that synchronous semantics are provided when necessary. 123 * 124 * In general, this is how things should be ordered in each vnode op: 125 * 126 * ZFS_ENTER(zfsvfs); // exit if unmounted 127 * top: 128 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 129 * rw_enter(...); // grab any other locks you need 130 * tx = dmu_tx_create(...); // get DMU tx 131 * dmu_tx_hold_*(); // hold each object you might modify 132 * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign 133 * if (error) { 134 * rw_exit(...); // drop locks 135 * zfs_dirent_unlock(dl); // unlock directory entry 136 * VN_RELE(...); // release held vnodes 137 * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 138 * dmu_tx_wait(tx); 139 * dmu_tx_abort(tx); 140 * goto top; 141 * } 142 * dmu_tx_abort(tx); // abort DMU tx 143 * ZFS_EXIT(zfsvfs); // finished in zfs 144 * return (error); // really out of space 145 * } 146 * error = do_real_work(); // do whatever this VOP does 147 * if (error == 0) 148 * zfs_log_*(...); // on success, make ZIL entry 149 * dmu_tx_commit(tx); // commit DMU tx -- error or not 150 * rw_exit(...); // drop locks 151 * zfs_dirent_unlock(dl); // unlock directory entry 152 * VN_RELE(...); // release held vnodes 153 * zil_commit(zilog, seq, foid); // synchronous when necessary 154 * ZFS_EXIT(zfsvfs); // finished in zfs 155 * return (error); // done, report error 156 */ 157 /* ARGSUSED */ 158 static int 159 zfs_open(vnode_t **vpp, int flag, cred_t *cr) 160 { 161 znode_t *zp = VTOZ(*vpp); 162 163 /* Keep a count of the synchronous opens in the znode */ 164 if (flag & (FSYNC | FDSYNC)) 165 atomic_inc_32(&zp->z_sync_cnt); 166 return (0); 167 } 168 169 /* ARGSUSED */ 170 static int 171 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 172 { 173 znode_t *zp = VTOZ(vp); 174 175 /* Decrement the synchronous opens in the znode */ 176 if (flag & (FSYNC | FDSYNC)) 177 atomic_dec_32(&zp->z_sync_cnt); 178 179 /* 180 * Clean up any locks held by this process on the vp. 181 */ 182 cleanlocks(vp, ddi_get_pid(), 0); 183 cleanshares(vp, ddi_get_pid()); 184 185 return (0); 186 } 187 188 /* 189 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 190 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 191 */ 192 static int 193 zfs_holey(vnode_t *vp, int cmd, offset_t *off) 194 { 195 znode_t *zp = VTOZ(vp); 196 uint64_t noff = (uint64_t)*off; /* new offset */ 197 uint64_t file_sz; 198 int error; 199 boolean_t hole; 200 201 file_sz = zp->z_phys->zp_size; 202 if (noff >= file_sz) { 203 return (ENXIO); 204 } 205 206 if (cmd == _FIO_SEEK_HOLE) 207 hole = B_TRUE; 208 else 209 hole = B_FALSE; 210 211 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 212 213 /* end of file? */ 214 if ((error == ESRCH) || (noff > file_sz)) { 215 /* 216 * Handle the virtual hole at the end of file. 217 */ 218 if (hole) { 219 *off = file_sz; 220 return (0); 221 } 222 return (ENXIO); 223 } 224 225 if (noff < *off) 226 return (error); 227 *off = noff; 228 return (error); 229 } 230 231 /* ARGSUSED */ 232 static int 233 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 234 int *rvalp) 235 { 236 offset_t off; 237 int error; 238 zfsvfs_t *zfsvfs; 239 240 switch (com) { 241 case _FIOFFS: 242 return (zfs_sync(vp->v_vfsp, 0, cred)); 243 244 /* 245 * The following two ioctls are used by bfu. Faking out, 246 * necessary to avoid bfu errors. 247 */ 248 case _FIOGDIO: 249 case _FIOSDIO: 250 return (0); 251 252 case _FIO_SEEK_DATA: 253 case _FIO_SEEK_HOLE: 254 if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 255 return (EFAULT); 256 257 zfsvfs = VTOZ(vp)->z_zfsvfs; 258 ZFS_ENTER(zfsvfs); 259 260 /* offset parameter is in/out */ 261 error = zfs_holey(vp, com, &off); 262 ZFS_EXIT(zfsvfs); 263 if (error) 264 return (error); 265 if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 266 return (EFAULT); 267 return (0); 268 } 269 return (ENOTTY); 270 } 271 272 /* 273 * When a file is memory mapped, we must keep the IO data synchronized 274 * between the DMU cache and the memory mapped pages. What this means: 275 * 276 * On Write: If we find a memory mapped page, we write to *both* 277 * the page and the dmu buffer. 278 * 279 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 280 * the file is memory mapped. 281 */ 282 static int 283 mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx) 284 { 285 znode_t *zp = VTOZ(vp); 286 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 287 int64_t start, off; 288 int len = nbytes; 289 int error = 0; 290 291 start = uio->uio_loffset; 292 off = start & PAGEOFFSET; 293 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 294 page_t *pp; 295 uint64_t bytes = MIN(PAGESIZE - off, len); 296 297 /* 298 * We don't want a new page to "appear" in the middle of 299 * the file update (because it may not get the write 300 * update data), so we grab a lock to block 301 * zfs_getpage(). 302 */ 303 rw_enter(&zp->z_map_lock, RW_WRITER); 304 if (pp = page_lookup(vp, start, SE_SHARED)) { 305 caddr_t va; 306 307 rw_exit(&zp->z_map_lock); 308 va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 309 error = uiomove(va+off, bytes, UIO_WRITE, uio); 310 if (error == 0) { 311 dmu_write(zfsvfs->z_os, zp->z_id, 312 woff, bytes, va+off, tx); 313 } 314 ppmapout(va); 315 page_unlock(pp); 316 } else { 317 error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 318 woff, bytes, uio, tx); 319 rw_exit(&zp->z_map_lock); 320 } 321 len -= bytes; 322 woff += bytes; 323 off = 0; 324 if (error) 325 break; 326 } 327 return (error); 328 } 329 330 /* 331 * When a file is memory mapped, we must keep the IO data synchronized 332 * between the DMU cache and the memory mapped pages. What this means: 333 * 334 * On Read: We "read" preferentially from memory mapped pages, 335 * else we default from the dmu buffer. 336 * 337 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 338 * the file is memory mapped. 339 */ 340 static int 341 mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio) 342 { 343 int64_t start, off, bytes; 344 int len = nbytes; 345 int error = 0; 346 347 start = uio->uio_loffset; 348 off = start & PAGEOFFSET; 349 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 350 page_t *pp; 351 352 bytes = MIN(PAGESIZE - off, len); 353 if (pp = page_lookup(vp, start, SE_SHARED)) { 354 caddr_t va; 355 356 va = ppmapin(pp, PROT_READ, (caddr_t)-1L); 357 error = uiomove(va + off, bytes, UIO_READ, uio); 358 ppmapout(va); 359 page_unlock(pp); 360 } else { 361 /* XXX use dmu_read here? */ 362 error = uiomove(addr, bytes, UIO_READ, uio); 363 } 364 len -= bytes; 365 addr += bytes; 366 off = 0; 367 if (error) 368 break; 369 } 370 return (error); 371 } 372 373 uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 374 375 /* 376 * Read bytes from specified file into supplied buffer. 377 * 378 * IN: vp - vnode of file to be read from. 379 * uio - structure supplying read location, range info, 380 * and return buffer. 381 * ioflag - SYNC flags; used to provide FRSYNC semantics. 382 * cr - credentials of caller. 383 * 384 * OUT: uio - updated offset and range, buffer filled. 385 * 386 * RETURN: 0 if success 387 * error code if failure 388 * 389 * Side Effects: 390 * vp - atime updated if byte count > 0 391 */ 392 /* ARGSUSED */ 393 static int 394 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 395 { 396 znode_t *zp = VTOZ(vp); 397 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 398 uint64_t delta; 399 ssize_t n, size, cnt, ndone; 400 int error, i, numbufs; 401 dmu_buf_t *dbp, **dbpp; 402 rl_t *rl; 403 404 ZFS_ENTER(zfsvfs); 405 406 /* 407 * Validate file offset 408 */ 409 if (uio->uio_loffset < (offset_t)0) { 410 ZFS_EXIT(zfsvfs); 411 return (EINVAL); 412 } 413 414 /* 415 * Fasttrack empty reads 416 */ 417 if (uio->uio_resid == 0) { 418 ZFS_EXIT(zfsvfs); 419 return (0); 420 } 421 422 /* 423 * Check for mandatory locks 424 */ 425 if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 426 if (error = chklock(vp, FREAD, 427 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 428 ZFS_EXIT(zfsvfs); 429 return (error); 430 } 431 } 432 433 /* 434 * If we're in FRSYNC mode, sync out this znode before reading it. 435 */ 436 if (ioflag & FRSYNC) 437 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 438 439 /* 440 * Lock the range against changes. 441 */ 442 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 443 444 /* 445 * If we are reading past end-of-file we can skip 446 * to the end; but we might still need to set atime. 447 */ 448 if (uio->uio_loffset >= zp->z_phys->zp_size) { 449 cnt = 0; 450 error = 0; 451 goto out; 452 } 453 454 cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 455 456 for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) { 457 ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 458 n = MIN(zfs_read_chunk_size, 459 zp->z_phys->zp_size - uio->uio_loffset); 460 n = MIN(n, cnt); 461 error = dmu_buf_hold_array_by_bonus(zp->z_dbuf, 462 uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp); 463 if (error) 464 goto out; 465 /* 466 * Compute the adjustment to align the dmu buffers 467 * with the uio buffer. 468 */ 469 delta = uio->uio_loffset - dbpp[0]->db_offset; 470 471 for (i = 0; i < numbufs; i++) { 472 if (n < 0) 473 break; 474 dbp = dbpp[i]; 475 size = dbp->db_size - delta; 476 /* 477 * XXX -- this is correct, but may be suboptimal. 478 * If the pages are all clean, we don't need to 479 * go through mappedread(). Maybe the VMODSORT 480 * stuff can help us here. 481 */ 482 if (vn_has_cached_data(vp)) { 483 error = mappedread(vp, (caddr_t)dbp->db_data + 484 delta, (n < size ? n : size), uio); 485 } else { 486 error = uiomove((caddr_t)dbp->db_data + delta, 487 (n < size ? n : size), UIO_READ, uio); 488 } 489 if (error) { 490 dmu_buf_rele_array(dbpp, numbufs, FTAG); 491 goto out; 492 } 493 n -= dbp->db_size; 494 if (delta) { 495 n += delta; 496 delta = 0; 497 } 498 } 499 dmu_buf_rele_array(dbpp, numbufs, FTAG); 500 } 501 out: 502 zfs_range_unlock(rl); 503 504 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 505 ZFS_EXIT(zfsvfs); 506 return (error); 507 } 508 509 /* 510 * Fault in the pages of the first n bytes specified by the uio structure. 511 * 1 byte in each page is touched and the uio struct is unmodified. 512 * Any error will exit this routine as this is only a best 513 * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 514 */ 515 static void 516 zfs_prefault_write(ssize_t n, struct uio *uio) 517 { 518 struct iovec *iov; 519 ulong_t cnt, incr; 520 caddr_t p; 521 uint8_t tmp; 522 523 iov = uio->uio_iov; 524 525 while (n) { 526 cnt = MIN(iov->iov_len, n); 527 if (cnt == 0) { 528 /* empty iov entry */ 529 iov++; 530 continue; 531 } 532 n -= cnt; 533 /* 534 * touch each page in this segment. 535 */ 536 p = iov->iov_base; 537 while (cnt) { 538 switch (uio->uio_segflg) { 539 case UIO_USERSPACE: 540 case UIO_USERISPACE: 541 if (fuword8(p, &tmp)) 542 return; 543 break; 544 case UIO_SYSSPACE: 545 if (kcopy(p, &tmp, 1)) 546 return; 547 break; 548 } 549 incr = MIN(cnt, PAGESIZE); 550 p += incr; 551 cnt -= incr; 552 } 553 /* 554 * touch the last byte in case it straddles a page. 555 */ 556 p--; 557 switch (uio->uio_segflg) { 558 case UIO_USERSPACE: 559 case UIO_USERISPACE: 560 if (fuword8(p, &tmp)) 561 return; 562 break; 563 case UIO_SYSSPACE: 564 if (kcopy(p, &tmp, 1)) 565 return; 566 break; 567 } 568 iov++; 569 } 570 } 571 572 /* 573 * Write the bytes to a file. 574 * 575 * IN: vp - vnode of file to be written to. 576 * uio - structure supplying write location, range info, 577 * and data buffer. 578 * ioflag - FAPPEND flag set if in append mode. 579 * cr - credentials of caller. 580 * 581 * OUT: uio - updated offset and range. 582 * 583 * RETURN: 0 if success 584 * error code if failure 585 * 586 * Timestamps: 587 * vp - ctime|mtime updated if byte count > 0 588 */ 589 /* ARGSUSED */ 590 static int 591 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 592 { 593 znode_t *zp = VTOZ(vp); 594 rlim64_t limit = uio->uio_llimit; 595 ssize_t start_resid = uio->uio_resid; 596 ssize_t tx_bytes; 597 uint64_t end_size; 598 dmu_tx_t *tx; 599 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 600 zilog_t *zilog = zfsvfs->z_log; 601 offset_t woff; 602 ssize_t n, nbytes; 603 rl_t *rl; 604 int max_blksz = zfsvfs->z_max_blksz; 605 int error; 606 607 /* 608 * Fasttrack empty write 609 */ 610 n = start_resid; 611 if (n == 0) 612 return (0); 613 614 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 615 limit = MAXOFFSET_T; 616 617 ZFS_ENTER(zfsvfs); 618 619 /* 620 * Pre-fault the pages to ensure slow (eg NFS) pages 621 * don't hold up txg. 622 */ 623 zfs_prefault_write(n, uio); 624 625 /* 626 * If in append mode, set the io offset pointer to eof. 627 */ 628 if (ioflag & FAPPEND) { 629 /* 630 * Range lock for a file append: 631 * The value for the start of range will be determined by 632 * zfs_range_lock() (to guarantee append semantics). 633 * If this write will cause the block size to increase, 634 * zfs_range_lock() will lock the entire file, so we must 635 * later reduce the range after we grow the block size. 636 */ 637 rl = zfs_range_lock(zp, 0, n, RL_APPEND); 638 if (rl->r_len == UINT64_MAX) { 639 /* overlocked, zp_size can't change */ 640 woff = uio->uio_loffset = zp->z_phys->zp_size; 641 } else { 642 woff = uio->uio_loffset = rl->r_off; 643 } 644 } else { 645 woff = uio->uio_loffset; 646 /* 647 * Validate file offset 648 */ 649 if (woff < 0) { 650 ZFS_EXIT(zfsvfs); 651 return (EINVAL); 652 } 653 654 /* 655 * If we need to grow the block size then zfs_range_lock() 656 * will lock a wider range than we request here. 657 * Later after growing the block size we reduce the range. 658 */ 659 rl = zfs_range_lock(zp, woff, n, RL_WRITER); 660 } 661 662 if (woff >= limit) { 663 error = EFBIG; 664 goto no_tx_done; 665 } 666 667 if ((woff + n) > limit || woff > (limit - n)) 668 n = limit - woff; 669 670 /* 671 * Check for mandatory locks 672 */ 673 if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 674 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) 675 goto no_tx_done; 676 end_size = MAX(zp->z_phys->zp_size, woff + n); 677 top: 678 tx = dmu_tx_create(zfsvfs->z_os); 679 dmu_tx_hold_bonus(tx, zp->z_id); 680 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 681 error = dmu_tx_assign(tx, zfsvfs->z_assign); 682 if (error) { 683 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 684 dmu_tx_wait(tx); 685 dmu_tx_abort(tx); 686 goto top; 687 } 688 dmu_tx_abort(tx); 689 goto no_tx_done; 690 } 691 692 /* 693 * If zfs_range_lock() over-locked we grow the blocksize 694 * and then reduce the lock range. 695 */ 696 if (rl->r_len == UINT64_MAX) { 697 uint64_t new_blksz; 698 699 if (zp->z_blksz > max_blksz) { 700 ASSERT(!ISP2(zp->z_blksz)); 701 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 702 } else { 703 new_blksz = MIN(end_size, max_blksz); 704 } 705 zfs_grow_blocksize(zp, new_blksz, tx); 706 zfs_range_reduce(rl, woff, n); 707 } 708 709 /* 710 * The file data does not fit in the znode "cache", so we 711 * will be writing to the file block data buffers. 712 * Each buffer will be written in a separate transaction; 713 * this keeps the intent log records small and allows us 714 * to do more fine-grained space accounting. 715 */ 716 while (n > 0) { 717 /* 718 * XXX - should we really limit each write to z_max_blksz? 719 * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 720 */ 721 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 722 rw_enter(&zp->z_map_lock, RW_READER); 723 724 tx_bytes = uio->uio_resid; 725 if (vn_has_cached_data(vp)) { 726 rw_exit(&zp->z_map_lock); 727 error = mappedwrite(vp, woff, nbytes, uio, tx); 728 } else { 729 error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 730 woff, nbytes, uio, tx); 731 rw_exit(&zp->z_map_lock); 732 } 733 tx_bytes -= uio->uio_resid; 734 735 if (error) { 736 /* XXX - do we need to "clean up" the dmu buffer? */ 737 break; 738 } 739 740 ASSERT(tx_bytes == nbytes); 741 742 /* 743 * Clear Set-UID/Set-GID bits on successful write if not 744 * privileged and at least one of the excute bits is set. 745 * 746 * It would be nice to to this after all writes have 747 * been done, but that would still expose the ISUID/ISGID 748 * to another app after the partial write is committed. 749 */ 750 751 mutex_enter(&zp->z_acl_lock); 752 if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 753 (S_IXUSR >> 6))) != 0 && 754 (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 755 secpolicy_vnode_setid_retain(cr, 756 (zp->z_phys->zp_mode & S_ISUID) != 0 && 757 zp->z_phys->zp_uid == 0) != 0) { 758 zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 759 } 760 mutex_exit(&zp->z_acl_lock); 761 762 n -= nbytes; 763 if (n <= 0) 764 break; 765 766 /* 767 * We have more work ahead of us, so wrap up this transaction 768 * and start another. Exact same logic as tx_done below. 769 */ 770 while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 771 dmu_buf_will_dirty(zp->z_dbuf, tx); 772 (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 773 uio->uio_loffset); 774 } 775 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 776 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 777 ioflag, uio); 778 dmu_tx_commit(tx); 779 780 /* 781 * Start another transaction. 782 */ 783 woff = uio->uio_loffset; 784 tx = dmu_tx_create(zfsvfs->z_os); 785 dmu_tx_hold_bonus(tx, zp->z_id); 786 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 787 error = dmu_tx_assign(tx, zfsvfs->z_assign); 788 if (error) { 789 if (error == ERESTART && 790 zfsvfs->z_assign == TXG_NOWAIT) { 791 dmu_tx_wait(tx); 792 dmu_tx_abort(tx); 793 goto top; 794 } 795 dmu_tx_abort(tx); 796 goto no_tx_done; 797 } 798 } 799 800 tx_done: 801 802 if (tx_bytes != 0) { 803 /* 804 * Update the file size if it has changed; account 805 * for possible concurrent updates. 806 */ 807 while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 808 dmu_buf_will_dirty(zp->z_dbuf, tx); 809 (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 810 uio->uio_loffset); 811 } 812 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 813 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 814 ioflag, uio); 815 } 816 dmu_tx_commit(tx); 817 818 819 no_tx_done: 820 821 zfs_range_unlock(rl); 822 823 /* 824 * If we're in replay mode, or we made no progress, return error. 825 * Otherwise, it's at least a partial write, so it's successful. 826 */ 827 if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 828 ZFS_EXIT(zfsvfs); 829 return (error); 830 } 831 832 if (ioflag & (FSYNC | FDSYNC)) 833 zil_commit(zilog, zp->z_last_itx, zp->z_id); 834 835 ZFS_EXIT(zfsvfs); 836 return (0); 837 } 838 839 void 840 zfs_get_done(dmu_buf_t *db, void *vzgd) 841 { 842 zgd_t *zgd = (zgd_t *)vzgd; 843 rl_t *rl = zgd->zgd_rl; 844 vnode_t *vp = ZTOV(rl->r_zp); 845 846 dmu_buf_rele(db, vzgd); 847 zfs_range_unlock(rl); 848 VN_RELE(vp); 849 zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp))); 850 kmem_free(zgd, sizeof (zgd_t)); 851 } 852 853 /* 854 * Get data to generate a TX_WRITE intent log record. 855 */ 856 int 857 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 858 { 859 zfsvfs_t *zfsvfs = arg; 860 objset_t *os = zfsvfs->z_os; 861 znode_t *zp; 862 uint64_t off = lr->lr_offset; 863 dmu_buf_t *db; 864 rl_t *rl; 865 zgd_t *zgd; 866 int dlen = lr->lr_length; /* length of user data */ 867 int error = 0; 868 869 ASSERT(zio); 870 ASSERT(dlen != 0); 871 872 /* 873 * Nothing to do if the file has been removed 874 */ 875 if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 876 return (ENOENT); 877 if (zp->z_reap) { 878 VN_RELE(ZTOV(zp)); 879 return (ENOENT); 880 } 881 882 /* 883 * Write records come in two flavors: immediate and indirect. 884 * For small writes it's cheaper to store the data with the 885 * log record (immediate); for large writes it's cheaper to 886 * sync the data and get a pointer to it (indirect) so that 887 * we don't have to write the data twice. 888 */ 889 if (buf != NULL) { /* immediate write */ 890 rl = zfs_range_lock(zp, off, dlen, RL_READER); 891 /* test for truncation needs to be done while range locked */ 892 if (off >= zp->z_phys->zp_size) { 893 error = ENOENT; 894 goto out; 895 } 896 VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 897 } else { /* indirect write */ 898 uint64_t boff; /* block starting offset */ 899 900 ASSERT3U(dlen, <=, zp->z_blksz); 901 /* 902 * Have to lock the whole block to ensure when it's 903 * written out and it's checksum is being calculated 904 * that no one can change the data. We need to re-check 905 * blocksize after we get the lock in case it's changed! 906 */ 907 for (;;) { 908 if (ISP2(zp->z_blksz)) { 909 boff = P2ALIGN_TYPED(off, zp->z_blksz, 910 uint64_t); 911 } else { 912 boff = 0; 913 } 914 dlen = zp->z_blksz; 915 rl = zfs_range_lock(zp, boff, dlen, RL_READER); 916 if (zp->z_blksz == dlen) 917 break; 918 zfs_range_unlock(rl); 919 } 920 /* test for truncation needs to be done while range locked */ 921 if (off >= zp->z_phys->zp_size) { 922 error = ENOENT; 923 goto out; 924 } 925 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 926 zgd->zgd_rl = rl; 927 zgd->zgd_zilog = zfsvfs->z_log; 928 zgd->zgd_bp = &lr->lr_blkptr; 929 VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 930 ASSERT(boff == db->db_offset); 931 lr->lr_blkoff = off - boff; 932 error = dmu_sync(zio, db, &lr->lr_blkptr, 933 lr->lr_common.lrc_txg, zfs_get_done, zgd); 934 if (error == 0) { 935 zil_add_vdev(zfsvfs->z_log, 936 DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr))); 937 } 938 /* 939 * If we get EINPROGRESS, then we need to wait for a 940 * write IO initiated by dmu_sync() to complete before 941 * we can release this dbuf. We will finish everything 942 * up in the zfs_get_done() callback. 943 */ 944 if (error == EINPROGRESS) 945 return (0); 946 dmu_buf_rele(db, zgd); 947 kmem_free(zgd, sizeof (zgd_t)); 948 } 949 out: 950 zfs_range_unlock(rl); 951 VN_RELE(ZTOV(zp)); 952 return (error); 953 } 954 955 /*ARGSUSED*/ 956 static int 957 zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr) 958 { 959 znode_t *zp = VTOZ(vp); 960 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 961 int error; 962 963 ZFS_ENTER(zfsvfs); 964 error = zfs_zaccess_rwx(zp, mode, cr); 965 ZFS_EXIT(zfsvfs); 966 return (error); 967 } 968 969 /* 970 * Lookup an entry in a directory, or an extended attribute directory. 971 * If it exists, return a held vnode reference for it. 972 * 973 * IN: dvp - vnode of directory to search. 974 * nm - name of entry to lookup. 975 * pnp - full pathname to lookup [UNUSED]. 976 * flags - LOOKUP_XATTR set if looking for an attribute. 977 * rdir - root directory vnode [UNUSED]. 978 * cr - credentials of caller. 979 * 980 * OUT: vpp - vnode of located entry, NULL if not found. 981 * 982 * RETURN: 0 if success 983 * error code if failure 984 * 985 * Timestamps: 986 * NA 987 */ 988 /* ARGSUSED */ 989 static int 990 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 991 int flags, vnode_t *rdir, cred_t *cr) 992 { 993 994 znode_t *zdp = VTOZ(dvp); 995 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 996 int error; 997 998 ZFS_ENTER(zfsvfs); 999 1000 *vpp = NULL; 1001 1002 if (flags & LOOKUP_XATTR) { 1003 /* 1004 * We don't allow recursive attributes.. 1005 * Maybe someday we will. 1006 */ 1007 if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1008 ZFS_EXIT(zfsvfs); 1009 return (EINVAL); 1010 } 1011 1012 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) { 1013 ZFS_EXIT(zfsvfs); 1014 return (error); 1015 } 1016 1017 /* 1018 * Do we have permission to get into attribute directory? 1019 */ 1020 1021 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 1022 VN_RELE(*vpp); 1023 } 1024 1025 ZFS_EXIT(zfsvfs); 1026 return (error); 1027 } 1028 1029 if (dvp->v_type != VDIR) { 1030 ZFS_EXIT(zfsvfs); 1031 return (ENOTDIR); 1032 } 1033 1034 /* 1035 * Check accessibility of directory. 1036 */ 1037 1038 if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 1039 ZFS_EXIT(zfsvfs); 1040 return (error); 1041 } 1042 1043 if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 1044 1045 /* 1046 * Convert device special files 1047 */ 1048 if (IS_DEVVP(*vpp)) { 1049 vnode_t *svp; 1050 1051 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1052 VN_RELE(*vpp); 1053 if (svp == NULL) 1054 error = ENOSYS; 1055 else 1056 *vpp = svp; 1057 } 1058 } 1059 1060 ZFS_EXIT(zfsvfs); 1061 return (error); 1062 } 1063 1064 /* 1065 * Attempt to create a new entry in a directory. If the entry 1066 * already exists, truncate the file if permissible, else return 1067 * an error. Return the vp of the created or trunc'd file. 1068 * 1069 * IN: dvp - vnode of directory to put new file entry in. 1070 * name - name of new file entry. 1071 * vap - attributes of new file. 1072 * excl - flag indicating exclusive or non-exclusive mode. 1073 * mode - mode to open file with. 1074 * cr - credentials of caller. 1075 * flag - large file flag [UNUSED]. 1076 * 1077 * OUT: vpp - vnode of created or trunc'd entry. 1078 * 1079 * RETURN: 0 if success 1080 * error code if failure 1081 * 1082 * Timestamps: 1083 * dvp - ctime|mtime updated if new entry created 1084 * vp - ctime|mtime always, atime if new 1085 */ 1086 /* ARGSUSED */ 1087 static int 1088 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1089 int mode, vnode_t **vpp, cred_t *cr, int flag) 1090 { 1091 znode_t *zp, *dzp = VTOZ(dvp); 1092 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1093 zilog_t *zilog = zfsvfs->z_log; 1094 objset_t *os = zfsvfs->z_os; 1095 zfs_dirlock_t *dl; 1096 dmu_tx_t *tx; 1097 int error; 1098 uint64_t zoid; 1099 1100 ZFS_ENTER(zfsvfs); 1101 1102 top: 1103 *vpp = NULL; 1104 1105 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1106 vap->va_mode &= ~VSVTX; 1107 1108 if (*name == '\0') { 1109 /* 1110 * Null component name refers to the directory itself. 1111 */ 1112 VN_HOLD(dvp); 1113 zp = dzp; 1114 dl = NULL; 1115 error = 0; 1116 } else { 1117 /* possible VN_HOLD(zp) */ 1118 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1119 if (strcmp(name, "..") == 0) 1120 error = EISDIR; 1121 ZFS_EXIT(zfsvfs); 1122 return (error); 1123 } 1124 } 1125 1126 zoid = zp ? zp->z_id : -1ULL; 1127 1128 if (zp == NULL) { 1129 /* 1130 * Create a new file object and update the directory 1131 * to reference it. 1132 */ 1133 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1134 goto out; 1135 } 1136 1137 /* 1138 * We only support the creation of regular files in 1139 * extended attribute directories. 1140 */ 1141 if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1142 (vap->va_type != VREG)) { 1143 error = EINVAL; 1144 goto out; 1145 } 1146 1147 tx = dmu_tx_create(os); 1148 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1149 dmu_tx_hold_bonus(tx, dzp->z_id); 1150 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1151 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1152 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1153 0, SPA_MAXBLOCKSIZE); 1154 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1155 if (error) { 1156 zfs_dirent_unlock(dl); 1157 if (error == ERESTART && 1158 zfsvfs->z_assign == TXG_NOWAIT) { 1159 dmu_tx_wait(tx); 1160 dmu_tx_abort(tx); 1161 goto top; 1162 } 1163 dmu_tx_abort(tx); 1164 ZFS_EXIT(zfsvfs); 1165 return (error); 1166 } 1167 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1168 ASSERT(zp->z_id == zoid); 1169 (void) zfs_link_create(dl, zp, tx, ZNEW); 1170 zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1171 dmu_tx_commit(tx); 1172 } else { 1173 /* 1174 * A directory entry already exists for this name. 1175 */ 1176 /* 1177 * Can't truncate an existing file if in exclusive mode. 1178 */ 1179 if (excl == EXCL) { 1180 error = EEXIST; 1181 goto out; 1182 } 1183 /* 1184 * Can't open a directory for writing. 1185 */ 1186 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1187 error = EISDIR; 1188 goto out; 1189 } 1190 /* 1191 * Verify requested access to file. 1192 */ 1193 if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1194 goto out; 1195 } 1196 1197 mutex_enter(&dzp->z_lock); 1198 dzp->z_seq++; 1199 mutex_exit(&dzp->z_lock); 1200 1201 /* 1202 * Truncate regular files if requested. 1203 */ 1204 if ((ZTOV(zp)->v_type == VREG) && 1205 (zp->z_phys->zp_size != 0) && 1206 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 1207 error = zfs_freesp(zp, 0, 0, mode, TRUE); 1208 if (error == ERESTART && 1209 zfsvfs->z_assign == TXG_NOWAIT) { 1210 /* NB: we already did dmu_tx_wait() */ 1211 zfs_dirent_unlock(dl); 1212 VN_RELE(ZTOV(zp)); 1213 goto top; 1214 } 1215 } 1216 } 1217 out: 1218 1219 if (dl) 1220 zfs_dirent_unlock(dl); 1221 1222 if (error) { 1223 if (zp) 1224 VN_RELE(ZTOV(zp)); 1225 } else { 1226 *vpp = ZTOV(zp); 1227 /* 1228 * If vnode is for a device return a specfs vnode instead. 1229 */ 1230 if (IS_DEVVP(*vpp)) { 1231 struct vnode *svp; 1232 1233 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1234 VN_RELE(*vpp); 1235 if (svp == NULL) { 1236 error = ENOSYS; 1237 } 1238 *vpp = svp; 1239 } 1240 } 1241 1242 ZFS_EXIT(zfsvfs); 1243 return (error); 1244 } 1245 1246 /* 1247 * Remove an entry from a directory. 1248 * 1249 * IN: dvp - vnode of directory to remove entry from. 1250 * name - name of entry to remove. 1251 * cr - credentials of caller. 1252 * 1253 * RETURN: 0 if success 1254 * error code if failure 1255 * 1256 * Timestamps: 1257 * dvp - ctime|mtime 1258 * vp - ctime (if nlink > 0) 1259 */ 1260 static int 1261 zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1262 { 1263 znode_t *zp, *dzp = VTOZ(dvp); 1264 znode_t *xzp = NULL; 1265 vnode_t *vp; 1266 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1267 zilog_t *zilog = zfsvfs->z_log; 1268 uint64_t acl_obj, xattr_obj; 1269 zfs_dirlock_t *dl; 1270 dmu_tx_t *tx; 1271 int may_delete_now, delete_now = FALSE; 1272 int reaped; 1273 int error; 1274 1275 ZFS_ENTER(zfsvfs); 1276 1277 top: 1278 /* 1279 * Attempt to lock directory; fail if entry doesn't exist. 1280 */ 1281 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1282 ZFS_EXIT(zfsvfs); 1283 return (error); 1284 } 1285 1286 vp = ZTOV(zp); 1287 1288 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1289 goto out; 1290 } 1291 1292 /* 1293 * Need to use rmdir for removing directories. 1294 */ 1295 if (vp->v_type == VDIR) { 1296 error = EPERM; 1297 goto out; 1298 } 1299 1300 vnevent_remove(vp); 1301 1302 dnlc_remove(dvp, name); 1303 1304 mutex_enter(&vp->v_lock); 1305 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1306 mutex_exit(&vp->v_lock); 1307 1308 /* 1309 * We may delete the znode now, or we may put it on the delete queue; 1310 * it depends on whether we're the last link, and on whether there are 1311 * other holds on the vnode. So we dmu_tx_hold() the right things to 1312 * allow for either case. 1313 */ 1314 tx = dmu_tx_create(zfsvfs->z_os); 1315 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1316 dmu_tx_hold_bonus(tx, zp->z_id); 1317 if (may_delete_now) 1318 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1319 1320 /* are there any extended attributes? */ 1321 if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1322 /* 1323 * XXX - There is a possibility that the delete 1324 * of the parent file could succeed, but then we get 1325 * an ENOSPC when we try to delete the xattrs... 1326 * so we would need to re-try the deletes periodically 1327 */ 1328 /* XXX - do we need this if we are deleting? */ 1329 dmu_tx_hold_bonus(tx, xattr_obj); 1330 } 1331 1332 /* are there any additional acls */ 1333 if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1334 may_delete_now) 1335 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1336 1337 /* charge as an update -- would be nice not to charge at all */ 1338 dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1339 1340 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1341 if (error) { 1342 zfs_dirent_unlock(dl); 1343 VN_RELE(vp); 1344 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1345 dmu_tx_wait(tx); 1346 dmu_tx_abort(tx); 1347 goto top; 1348 } 1349 dmu_tx_abort(tx); 1350 ZFS_EXIT(zfsvfs); 1351 return (error); 1352 } 1353 1354 /* 1355 * Remove the directory entry. 1356 */ 1357 error = zfs_link_destroy(dl, zp, tx, 0, &reaped); 1358 1359 if (error) { 1360 dmu_tx_commit(tx); 1361 goto out; 1362 } 1363 1364 if (reaped) { 1365 mutex_enter(&vp->v_lock); 1366 delete_now = may_delete_now && 1367 vp->v_count == 1 && !vn_has_cached_data(vp) && 1368 zp->z_phys->zp_xattr == xattr_obj && 1369 zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1370 mutex_exit(&vp->v_lock); 1371 } 1372 1373 if (delete_now) { 1374 if (zp->z_phys->zp_xattr) { 1375 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1376 ASSERT3U(error, ==, 0); 1377 ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1378 dmu_buf_will_dirty(xzp->z_dbuf, tx); 1379 mutex_enter(&xzp->z_lock); 1380 xzp->z_reap = 1; 1381 xzp->z_phys->zp_links = 0; 1382 mutex_exit(&xzp->z_lock); 1383 zfs_dq_add(xzp, tx); 1384 zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1385 } 1386 mutex_enter(&zp->z_lock); 1387 mutex_enter(&vp->v_lock); 1388 vp->v_count--; 1389 ASSERT3U(vp->v_count, ==, 0); 1390 mutex_exit(&vp->v_lock); 1391 mutex_exit(&zp->z_lock); 1392 zfs_znode_delete(zp, tx); 1393 VFS_RELE(zfsvfs->z_vfs); 1394 } else if (reaped) { 1395 zfs_dq_add(zp, tx); 1396 } 1397 1398 zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1399 1400 dmu_tx_commit(tx); 1401 out: 1402 zfs_dirent_unlock(dl); 1403 1404 if (!delete_now) { 1405 VN_RELE(vp); 1406 } else if (xzp) { 1407 /* this rele delayed to prevent nesting transactions */ 1408 VN_RELE(ZTOV(xzp)); 1409 } 1410 1411 ZFS_EXIT(zfsvfs); 1412 return (error); 1413 } 1414 1415 /* 1416 * Create a new directory and insert it into dvp using the name 1417 * provided. Return a pointer to the inserted directory. 1418 * 1419 * IN: dvp - vnode of directory to add subdir to. 1420 * dirname - name of new directory. 1421 * vap - attributes of new directory. 1422 * cr - credentials of caller. 1423 * 1424 * OUT: vpp - vnode of created directory. 1425 * 1426 * RETURN: 0 if success 1427 * error code if failure 1428 * 1429 * Timestamps: 1430 * dvp - ctime|mtime updated 1431 * vp - ctime|mtime|atime updated 1432 */ 1433 static int 1434 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1435 { 1436 znode_t *zp, *dzp = VTOZ(dvp); 1437 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1438 zilog_t *zilog = zfsvfs->z_log; 1439 zfs_dirlock_t *dl; 1440 uint64_t zoid = 0; 1441 dmu_tx_t *tx; 1442 int error; 1443 1444 ASSERT(vap->va_type == VDIR); 1445 1446 ZFS_ENTER(zfsvfs); 1447 1448 if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1449 ZFS_EXIT(zfsvfs); 1450 return (EINVAL); 1451 } 1452 top: 1453 *vpp = NULL; 1454 1455 /* 1456 * First make sure the new directory doesn't exist. 1457 */ 1458 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1459 ZFS_EXIT(zfsvfs); 1460 return (error); 1461 } 1462 1463 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 1464 zfs_dirent_unlock(dl); 1465 ZFS_EXIT(zfsvfs); 1466 return (error); 1467 } 1468 1469 /* 1470 * Add a new entry to the directory. 1471 */ 1472 tx = dmu_tx_create(zfsvfs->z_os); 1473 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 1474 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1475 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1476 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1477 0, SPA_MAXBLOCKSIZE); 1478 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1479 if (error) { 1480 zfs_dirent_unlock(dl); 1481 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1482 dmu_tx_wait(tx); 1483 dmu_tx_abort(tx); 1484 goto top; 1485 } 1486 dmu_tx_abort(tx); 1487 ZFS_EXIT(zfsvfs); 1488 return (error); 1489 } 1490 1491 /* 1492 * Create new node. 1493 */ 1494 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1495 1496 /* 1497 * Now put new name in parent dir. 1498 */ 1499 (void) zfs_link_create(dl, zp, tx, ZNEW); 1500 1501 *vpp = ZTOV(zp); 1502 1503 zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1504 dmu_tx_commit(tx); 1505 1506 zfs_dirent_unlock(dl); 1507 1508 ZFS_EXIT(zfsvfs); 1509 return (0); 1510 } 1511 1512 /* 1513 * Remove a directory subdir entry. If the current working 1514 * directory is the same as the subdir to be removed, the 1515 * remove will fail. 1516 * 1517 * IN: dvp - vnode of directory to remove from. 1518 * name - name of directory to be removed. 1519 * cwd - vnode of current working directory. 1520 * cr - credentials of caller. 1521 * 1522 * RETURN: 0 if success 1523 * error code if failure 1524 * 1525 * Timestamps: 1526 * dvp - ctime|mtime updated 1527 */ 1528 static int 1529 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1530 { 1531 znode_t *dzp = VTOZ(dvp); 1532 znode_t *zp; 1533 vnode_t *vp; 1534 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1535 zilog_t *zilog = zfsvfs->z_log; 1536 zfs_dirlock_t *dl; 1537 dmu_tx_t *tx; 1538 int error; 1539 1540 ZFS_ENTER(zfsvfs); 1541 1542 top: 1543 zp = NULL; 1544 1545 /* 1546 * Attempt to lock directory; fail if entry doesn't exist. 1547 */ 1548 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1549 ZFS_EXIT(zfsvfs); 1550 return (error); 1551 } 1552 1553 vp = ZTOV(zp); 1554 1555 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1556 goto out; 1557 } 1558 1559 if (vp->v_type != VDIR) { 1560 error = ENOTDIR; 1561 goto out; 1562 } 1563 1564 if (vp == cwd) { 1565 error = EINVAL; 1566 goto out; 1567 } 1568 1569 vnevent_rmdir(vp); 1570 1571 /* 1572 * Grab a lock on the parent pointer make sure we play well 1573 * with the treewalk and directory rename code. 1574 */ 1575 rw_enter(&zp->z_parent_lock, RW_WRITER); 1576 1577 tx = dmu_tx_create(zfsvfs->z_os); 1578 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1579 dmu_tx_hold_bonus(tx, zp->z_id); 1580 dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1581 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1582 if (error) { 1583 rw_exit(&zp->z_parent_lock); 1584 zfs_dirent_unlock(dl); 1585 VN_RELE(vp); 1586 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1587 dmu_tx_wait(tx); 1588 dmu_tx_abort(tx); 1589 goto top; 1590 } 1591 dmu_tx_abort(tx); 1592 ZFS_EXIT(zfsvfs); 1593 return (error); 1594 } 1595 1596 error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1597 1598 if (error == 0) 1599 zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1600 1601 dmu_tx_commit(tx); 1602 1603 rw_exit(&zp->z_parent_lock); 1604 out: 1605 zfs_dirent_unlock(dl); 1606 1607 VN_RELE(vp); 1608 1609 ZFS_EXIT(zfsvfs); 1610 return (error); 1611 } 1612 1613 /* 1614 * Read as many directory entries as will fit into the provided 1615 * buffer from the given directory cursor position (specified in 1616 * the uio structure. 1617 * 1618 * IN: vp - vnode of directory to read. 1619 * uio - structure supplying read location, range info, 1620 * and return buffer. 1621 * cr - credentials of caller. 1622 * 1623 * OUT: uio - updated offset and range, buffer filled. 1624 * eofp - set to true if end-of-file detected. 1625 * 1626 * RETURN: 0 if success 1627 * error code if failure 1628 * 1629 * Timestamps: 1630 * vp - atime updated 1631 * 1632 * Note that the low 4 bits of the cookie returned by zap is always zero. 1633 * This allows us to use the low range for "special" directory entries: 1634 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1635 * we use the offset 2 for the '.zfs' directory. 1636 */ 1637 /* ARGSUSED */ 1638 static int 1639 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1640 { 1641 znode_t *zp = VTOZ(vp); 1642 iovec_t *iovp; 1643 dirent64_t *odp; 1644 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1645 objset_t *os; 1646 caddr_t outbuf; 1647 size_t bufsize; 1648 zap_cursor_t zc; 1649 zap_attribute_t zap; 1650 uint_t bytes_wanted; 1651 ushort_t this_reclen; 1652 uint64_t offset; /* must be unsigned; checks for < 1 */ 1653 off64_t *next; 1654 int local_eof; 1655 int outcount; 1656 int error; 1657 uint8_t prefetch; 1658 1659 ZFS_ENTER(zfsvfs); 1660 1661 /* 1662 * If we are not given an eof variable, 1663 * use a local one. 1664 */ 1665 if (eofp == NULL) 1666 eofp = &local_eof; 1667 1668 /* 1669 * Check for valid iov_len. 1670 */ 1671 if (uio->uio_iov->iov_len <= 0) { 1672 ZFS_EXIT(zfsvfs); 1673 return (EINVAL); 1674 } 1675 1676 /* 1677 * Quit if directory has been removed (posix) 1678 */ 1679 if ((*eofp = zp->z_reap) != 0) { 1680 ZFS_EXIT(zfsvfs); 1681 return (0); 1682 } 1683 1684 error = 0; 1685 os = zfsvfs->z_os; 1686 offset = uio->uio_loffset; 1687 prefetch = zp->z_zn_prefetch; 1688 1689 /* 1690 * Initialize the iterator cursor. 1691 */ 1692 if (offset <= 3) { 1693 /* 1694 * Start iteration from the beginning of the directory. 1695 */ 1696 zap_cursor_init(&zc, os, zp->z_id); 1697 } else { 1698 /* 1699 * The offset is a serialized cursor. 1700 */ 1701 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1702 } 1703 1704 /* 1705 * Get space to change directory entries into fs independent format. 1706 */ 1707 iovp = uio->uio_iov; 1708 bytes_wanted = iovp->iov_len; 1709 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1710 bufsize = bytes_wanted; 1711 outbuf = kmem_alloc(bufsize, KM_SLEEP); 1712 odp = (struct dirent64 *)outbuf; 1713 } else { 1714 bufsize = bytes_wanted; 1715 odp = (struct dirent64 *)iovp->iov_base; 1716 } 1717 1718 /* 1719 * Transform to file-system independent format 1720 */ 1721 outcount = 0; 1722 while (outcount < bytes_wanted) { 1723 /* 1724 * Special case `.', `..', and `.zfs'. 1725 */ 1726 if (offset == 0) { 1727 (void) strcpy(zap.za_name, "."); 1728 zap.za_first_integer = zp->z_id; 1729 this_reclen = DIRENT64_RECLEN(1); 1730 } else if (offset == 1) { 1731 (void) strcpy(zap.za_name, ".."); 1732 zap.za_first_integer = zp->z_phys->zp_parent; 1733 this_reclen = DIRENT64_RECLEN(2); 1734 } else if (offset == 2 && zfs_show_ctldir(zp)) { 1735 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1736 zap.za_first_integer = ZFSCTL_INO_ROOT; 1737 this_reclen = 1738 DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1); 1739 } else { 1740 /* 1741 * Grab next entry. 1742 */ 1743 if (error = zap_cursor_retrieve(&zc, &zap)) { 1744 if ((*eofp = (error == ENOENT)) != 0) 1745 break; 1746 else 1747 goto update; 1748 } 1749 1750 if (zap.za_integer_length != 8 || 1751 zap.za_num_integers != 1) { 1752 cmn_err(CE_WARN, "zap_readdir: bad directory " 1753 "entry, obj = %lld, offset = %lld\n", 1754 (u_longlong_t)zp->z_id, 1755 (u_longlong_t)offset); 1756 error = ENXIO; 1757 goto update; 1758 } 1759 this_reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1760 } 1761 1762 /* 1763 * Will this entry fit in the buffer? 1764 */ 1765 if (outcount + this_reclen > bufsize) { 1766 /* 1767 * Did we manage to fit anything in the buffer? 1768 */ 1769 if (!outcount) { 1770 error = EINVAL; 1771 goto update; 1772 } 1773 break; 1774 } 1775 /* 1776 * Add this entry: 1777 */ 1778 odp->d_ino = (ino64_t)zap.za_first_integer; 1779 odp->d_reclen = (ushort_t)this_reclen; 1780 /* NOTE: d_off is the offset for the *next* entry */ 1781 next = &(odp->d_off); 1782 (void) strncpy(odp->d_name, zap.za_name, 1783 DIRENT64_NAMELEN(this_reclen)); 1784 outcount += this_reclen; 1785 odp = (dirent64_t *)((intptr_t)odp + this_reclen); 1786 1787 ASSERT(outcount <= bufsize); 1788 1789 /* Prefetch znode */ 1790 if (prefetch) 1791 dmu_prefetch(os, zap.za_first_integer, 0, 0); 1792 1793 /* 1794 * Move to the next entry, fill in the previous offset. 1795 */ 1796 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1797 zap_cursor_advance(&zc); 1798 offset = zap_cursor_serialize(&zc); 1799 } else { 1800 offset += 1; 1801 } 1802 *next = offset; 1803 } 1804 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1805 1806 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1807 iovp->iov_base += outcount; 1808 iovp->iov_len -= outcount; 1809 uio->uio_resid -= outcount; 1810 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1811 /* 1812 * Reset the pointer. 1813 */ 1814 offset = uio->uio_loffset; 1815 } 1816 1817 update: 1818 zap_cursor_fini(&zc); 1819 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1820 kmem_free(outbuf, bufsize); 1821 1822 if (error == ENOENT) 1823 error = 0; 1824 1825 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1826 1827 uio->uio_loffset = offset; 1828 ZFS_EXIT(zfsvfs); 1829 return (error); 1830 } 1831 1832 static int 1833 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1834 { 1835 znode_t *zp = VTOZ(vp); 1836 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1837 1838 /* 1839 * Regardless of whether this is required for standards conformance, 1840 * this is the logical behavior when fsync() is called on a file with 1841 * dirty pages. We use B_ASYNC since the ZIL transactions are already 1842 * going to be pushed out as part of the zil_commit(). 1843 */ 1844 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 1845 (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 1846 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 1847 1848 ZFS_ENTER(zfsvfs); 1849 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 1850 ZFS_EXIT(zfsvfs); 1851 return (0); 1852 } 1853 1854 /* 1855 * Get the requested file attributes and place them in the provided 1856 * vattr structure. 1857 * 1858 * IN: vp - vnode of file. 1859 * vap - va_mask identifies requested attributes. 1860 * flags - [UNUSED] 1861 * cr - credentials of caller. 1862 * 1863 * OUT: vap - attribute values. 1864 * 1865 * RETURN: 0 (always succeeds) 1866 */ 1867 /* ARGSUSED */ 1868 static int 1869 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1870 { 1871 znode_t *zp = VTOZ(vp); 1872 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1873 znode_phys_t *pzp = zp->z_phys; 1874 int error; 1875 1876 ZFS_ENTER(zfsvfs); 1877 1878 /* 1879 * Return all attributes. It's cheaper to provide the answer 1880 * than to determine whether we were asked the question. 1881 */ 1882 mutex_enter(&zp->z_lock); 1883 1884 vap->va_type = vp->v_type; 1885 vap->va_mode = pzp->zp_mode & MODEMASK; 1886 vap->va_uid = zp->z_phys->zp_uid; 1887 vap->va_gid = zp->z_phys->zp_gid; 1888 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1889 vap->va_nodeid = zp->z_id; 1890 vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX); /* nlink_t limit! */ 1891 vap->va_size = pzp->zp_size; 1892 vap->va_rdev = vp->v_rdev; 1893 vap->va_seq = zp->z_seq; 1894 1895 ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1896 ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1897 ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1898 1899 /* 1900 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1901 * Also, if we are the owner don't bother, since owner should 1902 * always be allowed to read basic attributes of file. 1903 */ 1904 if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1905 (zp->z_phys->zp_uid != crgetuid(cr))) { 1906 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1907 mutex_exit(&zp->z_lock); 1908 ZFS_EXIT(zfsvfs); 1909 return (error); 1910 } 1911 } 1912 1913 mutex_exit(&zp->z_lock); 1914 1915 dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1916 1917 if (zp->z_blksz == 0) { 1918 /* 1919 * Block size hasn't been set; suggest maximal I/O transfers. 1920 */ 1921 vap->va_blksize = zfsvfs->z_max_blksz; 1922 } 1923 1924 ZFS_EXIT(zfsvfs); 1925 return (0); 1926 } 1927 1928 /* 1929 * Set the file attributes to the values contained in the 1930 * vattr structure. 1931 * 1932 * IN: vp - vnode of file to be modified. 1933 * vap - new attribute values. 1934 * flags - ATTR_UTIME set if non-default time values provided. 1935 * cr - credentials of caller. 1936 * 1937 * RETURN: 0 if success 1938 * error code if failure 1939 * 1940 * Timestamps: 1941 * vp - ctime updated, mtime updated if size changed. 1942 */ 1943 /* ARGSUSED */ 1944 static int 1945 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1946 caller_context_t *ct) 1947 { 1948 struct znode *zp = VTOZ(vp); 1949 znode_phys_t *pzp = zp->z_phys; 1950 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1951 zilog_t *zilog = zfsvfs->z_log; 1952 dmu_tx_t *tx; 1953 vattr_t oldva; 1954 uint_t mask = vap->va_mask; 1955 uint_t saved_mask; 1956 int trim_mask = 0; 1957 uint64_t new_mode; 1958 znode_t *attrzp; 1959 int need_policy = FALSE; 1960 int err; 1961 1962 if (mask == 0) 1963 return (0); 1964 1965 if (mask & AT_NOSET) 1966 return (EINVAL); 1967 1968 if (mask & AT_SIZE && vp->v_type == VDIR) 1969 return (EISDIR); 1970 1971 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 1972 return (EINVAL); 1973 1974 ZFS_ENTER(zfsvfs); 1975 1976 top: 1977 attrzp = NULL; 1978 1979 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1980 ZFS_EXIT(zfsvfs); 1981 return (EROFS); 1982 } 1983 1984 /* 1985 * First validate permissions 1986 */ 1987 1988 if (mask & AT_SIZE) { 1989 err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1990 if (err) { 1991 ZFS_EXIT(zfsvfs); 1992 return (err); 1993 } 1994 /* 1995 * XXX - Note, we are not providing any open 1996 * mode flags here (like FNDELAY), so we may 1997 * block if there are locks present... this 1998 * should be addressed in openat(). 1999 */ 2000 do { 2001 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 2002 /* NB: we already did dmu_tx_wait() if necessary */ 2003 } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 2004 if (err) { 2005 ZFS_EXIT(zfsvfs); 2006 return (err); 2007 } 2008 } 2009 2010 if (mask & (AT_ATIME|AT_MTIME)) 2011 need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 2012 2013 if (mask & (AT_UID|AT_GID)) { 2014 int idmask = (mask & (AT_UID|AT_GID)); 2015 int take_owner; 2016 int take_group; 2017 2018 /* 2019 * NOTE: even if a new mode is being set, 2020 * we may clear S_ISUID/S_ISGID bits. 2021 */ 2022 2023 if (!(mask & AT_MODE)) 2024 vap->va_mode = pzp->zp_mode; 2025 2026 /* 2027 * Take ownership or chgrp to group we are a member of 2028 */ 2029 2030 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2031 take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 2032 2033 /* 2034 * If both AT_UID and AT_GID are set then take_owner and 2035 * take_group must both be set in order to allow taking 2036 * ownership. 2037 * 2038 * Otherwise, send the check through secpolicy_vnode_setattr() 2039 * 2040 */ 2041 2042 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2043 ((idmask == AT_UID) && take_owner) || 2044 ((idmask == AT_GID) && take_group)) { 2045 if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 2046 /* 2047 * Remove setuid/setgid for non-privileged users 2048 */ 2049 secpolicy_setid_clear(vap, cr); 2050 trim_mask = (mask & (AT_UID|AT_GID)); 2051 } else { 2052 need_policy = TRUE; 2053 } 2054 } else { 2055 need_policy = TRUE; 2056 } 2057 } 2058 2059 mutex_enter(&zp->z_lock); 2060 oldva.va_mode = pzp->zp_mode; 2061 oldva.va_uid = zp->z_phys->zp_uid; 2062 oldva.va_gid = zp->z_phys->zp_gid; 2063 mutex_exit(&zp->z_lock); 2064 2065 if (mask & AT_MODE) { 2066 if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) { 2067 err = secpolicy_setid_setsticky_clear(vp, vap, 2068 &oldva, cr); 2069 if (err) { 2070 ZFS_EXIT(zfsvfs); 2071 return (err); 2072 } 2073 trim_mask |= AT_MODE; 2074 } else { 2075 need_policy = TRUE; 2076 } 2077 } 2078 2079 if (need_policy) { 2080 /* 2081 * If trim_mask is set then take ownership 2082 * has been granted or write_acl is present and user 2083 * has the ability to modify mode. In that case remove 2084 * UID|GID and or MODE from mask so that 2085 * secpolicy_vnode_setattr() doesn't revoke it. 2086 */ 2087 2088 if (trim_mask) { 2089 saved_mask = vap->va_mask; 2090 vap->va_mask &= ~trim_mask; 2091 2092 } 2093 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2094 (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2095 if (err) { 2096 ZFS_EXIT(zfsvfs); 2097 return (err); 2098 } 2099 2100 if (trim_mask) 2101 vap->va_mask |= saved_mask; 2102 } 2103 2104 /* 2105 * secpolicy_vnode_setattr, or take ownership may have 2106 * changed va_mask 2107 */ 2108 mask = vap->va_mask; 2109 2110 tx = dmu_tx_create(zfsvfs->z_os); 2111 dmu_tx_hold_bonus(tx, zp->z_id); 2112 2113 if (mask & AT_MODE) { 2114 uint64_t pmode = pzp->zp_mode; 2115 2116 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2117 2118 if (zp->z_phys->zp_acl.z_acl_extern_obj) 2119 dmu_tx_hold_write(tx, 2120 pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2121 else 2122 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2123 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2124 } 2125 2126 if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 2127 err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 2128 if (err) { 2129 dmu_tx_abort(tx); 2130 ZFS_EXIT(zfsvfs); 2131 return (err); 2132 } 2133 dmu_tx_hold_bonus(tx, attrzp->z_id); 2134 } 2135 2136 err = dmu_tx_assign(tx, zfsvfs->z_assign); 2137 if (err) { 2138 if (attrzp) 2139 VN_RELE(ZTOV(attrzp)); 2140 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2141 dmu_tx_wait(tx); 2142 dmu_tx_abort(tx); 2143 goto top; 2144 } 2145 dmu_tx_abort(tx); 2146 ZFS_EXIT(zfsvfs); 2147 return (err); 2148 } 2149 2150 dmu_buf_will_dirty(zp->z_dbuf, tx); 2151 2152 /* 2153 * Set each attribute requested. 2154 * We group settings according to the locks they need to acquire. 2155 * 2156 * Note: you cannot set ctime directly, although it will be 2157 * updated as a side-effect of calling this function. 2158 */ 2159 2160 mutex_enter(&zp->z_lock); 2161 2162 if (mask & AT_MODE) { 2163 err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2164 ASSERT3U(err, ==, 0); 2165 } 2166 2167 if (attrzp) 2168 mutex_enter(&attrzp->z_lock); 2169 2170 if (mask & AT_UID) { 2171 zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 2172 if (attrzp) { 2173 attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 2174 } 2175 } 2176 2177 if (mask & AT_GID) { 2178 zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 2179 if (attrzp) 2180 attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 2181 } 2182 2183 if (attrzp) 2184 mutex_exit(&attrzp->z_lock); 2185 2186 if (mask & AT_ATIME) 2187 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2188 2189 if (mask & AT_MTIME) 2190 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2191 2192 if (mask & AT_SIZE) 2193 zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 2194 else if (mask != 0) 2195 zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2196 2197 if (mask != 0) 2198 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2199 2200 mutex_exit(&zp->z_lock); 2201 2202 if (attrzp) 2203 VN_RELE(ZTOV(attrzp)); 2204 2205 dmu_tx_commit(tx); 2206 2207 ZFS_EXIT(zfsvfs); 2208 return (err); 2209 } 2210 2211 /* 2212 * Search back through the directory tree, using the ".." entries. 2213 * Lock each directory in the chain to prevent concurrent renames. 2214 * Fail any attempt to move a directory into one of its own descendants. 2215 * XXX - z_parent_lock can overlap with map or grow locks 2216 */ 2217 typedef struct zfs_zlock { 2218 krwlock_t *zl_rwlock; /* lock we acquired */ 2219 znode_t *zl_znode; /* znode we held */ 2220 struct zfs_zlock *zl_next; /* next in list */ 2221 } zfs_zlock_t; 2222 2223 static int 2224 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2225 { 2226 zfs_zlock_t *zl; 2227 znode_t *zp = tdzp; 2228 uint64_t rootid = zp->z_zfsvfs->z_root; 2229 uint64_t *oidp = &zp->z_id; 2230 krwlock_t *rwlp = &szp->z_parent_lock; 2231 krw_t rw = RW_WRITER; 2232 2233 /* 2234 * First pass write-locks szp and compares to zp->z_id. 2235 * Later passes read-lock zp and compare to zp->z_parent. 2236 */ 2237 do { 2238 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2239 zl->zl_rwlock = rwlp; 2240 zl->zl_znode = NULL; 2241 zl->zl_next = *zlpp; 2242 *zlpp = zl; 2243 2244 rw_enter(rwlp, rw); 2245 2246 if (*oidp == szp->z_id) /* We're a descendant of szp */ 2247 return (EINVAL); 2248 2249 if (*oidp == rootid) /* We've hit the top */ 2250 return (0); 2251 2252 if (rw == RW_READER) { /* i.e. not the first pass */ 2253 int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2254 if (error) 2255 return (error); 2256 zl->zl_znode = zp; 2257 } 2258 oidp = &zp->z_phys->zp_parent; 2259 rwlp = &zp->z_parent_lock; 2260 rw = RW_READER; 2261 2262 } while (zp->z_id != sdzp->z_id); 2263 2264 return (0); 2265 } 2266 2267 /* 2268 * Drop locks and release vnodes that were held by zfs_rename_lock(). 2269 */ 2270 static void 2271 zfs_rename_unlock(zfs_zlock_t **zlpp) 2272 { 2273 zfs_zlock_t *zl; 2274 2275 while ((zl = *zlpp) != NULL) { 2276 if (zl->zl_znode != NULL) 2277 VN_RELE(ZTOV(zl->zl_znode)); 2278 rw_exit(zl->zl_rwlock); 2279 *zlpp = zl->zl_next; 2280 kmem_free(zl, sizeof (*zl)); 2281 } 2282 } 2283 2284 /* 2285 * Move an entry from the provided source directory to the target 2286 * directory. Change the entry name as indicated. 2287 * 2288 * IN: sdvp - Source directory containing the "old entry". 2289 * snm - Old entry name. 2290 * tdvp - Target directory to contain the "new entry". 2291 * tnm - New entry name. 2292 * cr - credentials of caller. 2293 * 2294 * RETURN: 0 if success 2295 * error code if failure 2296 * 2297 * Timestamps: 2298 * sdvp,tdvp - ctime|mtime updated 2299 */ 2300 static int 2301 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2302 { 2303 znode_t *tdzp, *szp, *tzp; 2304 znode_t *sdzp = VTOZ(sdvp); 2305 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2306 zilog_t *zilog = zfsvfs->z_log; 2307 vnode_t *realvp; 2308 zfs_dirlock_t *sdl, *tdl; 2309 dmu_tx_t *tx; 2310 zfs_zlock_t *zl; 2311 int cmp, serr, terr, error; 2312 2313 ZFS_ENTER(zfsvfs); 2314 2315 /* 2316 * Make sure we have the real vp for the target directory. 2317 */ 2318 if (VOP_REALVP(tdvp, &realvp) == 0) 2319 tdvp = realvp; 2320 2321 if (tdvp->v_vfsp != sdvp->v_vfsp) { 2322 ZFS_EXIT(zfsvfs); 2323 return (EXDEV); 2324 } 2325 2326 tdzp = VTOZ(tdvp); 2327 top: 2328 szp = NULL; 2329 tzp = NULL; 2330 zl = NULL; 2331 2332 /* 2333 * This is to prevent the creation of links into attribute space 2334 * by renaming a linked file into/outof an attribute directory. 2335 * See the comment in zfs_link() for why this is considered bad. 2336 */ 2337 if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2338 (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2339 ZFS_EXIT(zfsvfs); 2340 return (EINVAL); 2341 } 2342 2343 /* 2344 * Lock source and target directory entries. To prevent deadlock, 2345 * a lock ordering must be defined. We lock the directory with 2346 * the smallest object id first, or if it's a tie, the one with 2347 * the lexically first name. 2348 */ 2349 if (sdzp->z_id < tdzp->z_id) { 2350 cmp = -1; 2351 } else if (sdzp->z_id > tdzp->z_id) { 2352 cmp = 1; 2353 } else { 2354 cmp = strcmp(snm, tnm); 2355 if (cmp == 0) { 2356 /* 2357 * POSIX: "If the old argument and the new argument 2358 * both refer to links to the same existing file, 2359 * the rename() function shall return successfully 2360 * and perform no other action." 2361 */ 2362 ZFS_EXIT(zfsvfs); 2363 return (0); 2364 } 2365 } 2366 if (cmp < 0) { 2367 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2368 terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2369 } else { 2370 terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2371 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2372 } 2373 2374 if (serr) { 2375 /* 2376 * Source entry invalid or not there. 2377 */ 2378 if (!terr) { 2379 zfs_dirent_unlock(tdl); 2380 if (tzp) 2381 VN_RELE(ZTOV(tzp)); 2382 } 2383 if (strcmp(snm, "..") == 0) 2384 serr = EINVAL; 2385 ZFS_EXIT(zfsvfs); 2386 return (serr); 2387 } 2388 if (terr) { 2389 zfs_dirent_unlock(sdl); 2390 VN_RELE(ZTOV(szp)); 2391 if (strcmp(tnm, "..") == 0) 2392 terr = EINVAL; 2393 ZFS_EXIT(zfsvfs); 2394 return (terr); 2395 } 2396 2397 /* 2398 * Must have write access at the source to remove the old entry 2399 * and write access at the target to create the new entry. 2400 * Note that if target and source are the same, this can be 2401 * done in a single check. 2402 */ 2403 2404 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2405 goto out; 2406 2407 if (ZTOV(szp)->v_type == VDIR) { 2408 /* 2409 * Check to make sure rename is valid. 2410 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2411 */ 2412 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2413 goto out; 2414 } 2415 2416 /* 2417 * Does target exist? 2418 */ 2419 if (tzp) { 2420 /* 2421 * Source and target must be the same type. 2422 */ 2423 if (ZTOV(szp)->v_type == VDIR) { 2424 if (ZTOV(tzp)->v_type != VDIR) { 2425 error = ENOTDIR; 2426 goto out; 2427 } 2428 } else { 2429 if (ZTOV(tzp)->v_type == VDIR) { 2430 error = EISDIR; 2431 goto out; 2432 } 2433 } 2434 /* 2435 * POSIX dictates that when the source and target 2436 * entries refer to the same file object, rename 2437 * must do nothing and exit without error. 2438 */ 2439 if (szp->z_id == tzp->z_id) { 2440 error = 0; 2441 goto out; 2442 } 2443 } 2444 2445 vnevent_rename_src(ZTOV(szp)); 2446 if (tzp) 2447 vnevent_rename_dest(ZTOV(tzp)); 2448 2449 tx = dmu_tx_create(zfsvfs->z_os); 2450 dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2451 dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 2452 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 2453 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 2454 if (sdzp != tdzp) 2455 dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 2456 if (tzp) 2457 dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 2458 dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 2459 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2460 if (error) { 2461 if (zl != NULL) 2462 zfs_rename_unlock(&zl); 2463 zfs_dirent_unlock(sdl); 2464 zfs_dirent_unlock(tdl); 2465 VN_RELE(ZTOV(szp)); 2466 if (tzp) 2467 VN_RELE(ZTOV(tzp)); 2468 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2469 dmu_tx_wait(tx); 2470 dmu_tx_abort(tx); 2471 goto top; 2472 } 2473 dmu_tx_abort(tx); 2474 ZFS_EXIT(zfsvfs); 2475 return (error); 2476 } 2477 2478 if (tzp) /* Attempt to remove the existing target */ 2479 error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2480 2481 if (error == 0) { 2482 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2483 if (error == 0) { 2484 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2485 ASSERT(error == 0); 2486 zfs_log_rename(zilog, tx, TX_RENAME, sdzp, 2487 sdl->dl_name, tdzp, tdl->dl_name, szp); 2488 } 2489 } 2490 2491 dmu_tx_commit(tx); 2492 out: 2493 if (zl != NULL) 2494 zfs_rename_unlock(&zl); 2495 2496 zfs_dirent_unlock(sdl); 2497 zfs_dirent_unlock(tdl); 2498 2499 VN_RELE(ZTOV(szp)); 2500 if (tzp) 2501 VN_RELE(ZTOV(tzp)); 2502 2503 ZFS_EXIT(zfsvfs); 2504 return (error); 2505 } 2506 2507 /* 2508 * Insert the indicated symbolic reference entry into the directory. 2509 * 2510 * IN: dvp - Directory to contain new symbolic link. 2511 * link - Name for new symlink entry. 2512 * vap - Attributes of new entry. 2513 * target - Target path of new symlink. 2514 * cr - credentials of caller. 2515 * 2516 * RETURN: 0 if success 2517 * error code if failure 2518 * 2519 * Timestamps: 2520 * dvp - ctime|mtime updated 2521 */ 2522 static int 2523 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2524 { 2525 znode_t *zp, *dzp = VTOZ(dvp); 2526 zfs_dirlock_t *dl; 2527 dmu_tx_t *tx; 2528 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2529 zilog_t *zilog = zfsvfs->z_log; 2530 uint64_t zoid; 2531 int len = strlen(link); 2532 int error; 2533 2534 ASSERT(vap->va_type == VLNK); 2535 2536 ZFS_ENTER(zfsvfs); 2537 top: 2538 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2539 ZFS_EXIT(zfsvfs); 2540 return (error); 2541 } 2542 2543 if (len > MAXPATHLEN) { 2544 ZFS_EXIT(zfsvfs); 2545 return (ENAMETOOLONG); 2546 } 2547 2548 /* 2549 * Attempt to lock directory; fail if entry already exists. 2550 */ 2551 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2552 ZFS_EXIT(zfsvfs); 2553 return (error); 2554 } 2555 2556 tx = dmu_tx_create(zfsvfs->z_os); 2557 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2558 dmu_tx_hold_bonus(tx, dzp->z_id); 2559 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2560 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2561 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2562 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2563 if (error) { 2564 zfs_dirent_unlock(dl); 2565 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2566 dmu_tx_wait(tx); 2567 dmu_tx_abort(tx); 2568 goto top; 2569 } 2570 dmu_tx_abort(tx); 2571 ZFS_EXIT(zfsvfs); 2572 return (error); 2573 } 2574 2575 dmu_buf_will_dirty(dzp->z_dbuf, tx); 2576 2577 /* 2578 * Create a new object for the symlink. 2579 * Put the link content into bonus buffer if it will fit; 2580 * otherwise, store it just like any other file data. 2581 */ 2582 zoid = 0; 2583 if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2584 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2585 if (len != 0) 2586 bcopy(link, zp->z_phys + 1, len); 2587 } else { 2588 dmu_buf_t *dbp; 2589 2590 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2591 2592 /* 2593 * Nothing can access the znode yet so no locking needed 2594 * for growing the znode's blocksize. 2595 */ 2596 zfs_grow_blocksize(zp, len, tx); 2597 2598 VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2599 dmu_buf_will_dirty(dbp, tx); 2600 2601 ASSERT3U(len, <=, dbp->db_size); 2602 bcopy(link, dbp->db_data, len); 2603 dmu_buf_rele(dbp, FTAG); 2604 } 2605 zp->z_phys->zp_size = len; 2606 2607 /* 2608 * Insert the new object into the directory. 2609 */ 2610 (void) zfs_link_create(dl, zp, tx, ZNEW); 2611 out: 2612 if (error == 0) 2613 zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link); 2614 2615 dmu_tx_commit(tx); 2616 2617 zfs_dirent_unlock(dl); 2618 2619 VN_RELE(ZTOV(zp)); 2620 2621 ZFS_EXIT(zfsvfs); 2622 return (error); 2623 } 2624 2625 /* 2626 * Return, in the buffer contained in the provided uio structure, 2627 * the symbolic path referred to by vp. 2628 * 2629 * IN: vp - vnode of symbolic link. 2630 * uoip - structure to contain the link path. 2631 * cr - credentials of caller. 2632 * 2633 * OUT: uio - structure to contain the link path. 2634 * 2635 * RETURN: 0 if success 2636 * error code if failure 2637 * 2638 * Timestamps: 2639 * vp - atime updated 2640 */ 2641 /* ARGSUSED */ 2642 static int 2643 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2644 { 2645 znode_t *zp = VTOZ(vp); 2646 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2647 size_t bufsz; 2648 int error; 2649 2650 ZFS_ENTER(zfsvfs); 2651 2652 bufsz = (size_t)zp->z_phys->zp_size; 2653 if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2654 error = uiomove(zp->z_phys + 1, 2655 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2656 } else { 2657 dmu_buf_t *dbp; 2658 error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 2659 if (error) { 2660 ZFS_EXIT(zfsvfs); 2661 return (error); 2662 } 2663 error = uiomove(dbp->db_data, 2664 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2665 dmu_buf_rele(dbp, FTAG); 2666 } 2667 2668 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2669 ZFS_EXIT(zfsvfs); 2670 return (error); 2671 } 2672 2673 /* 2674 * Insert a new entry into directory tdvp referencing svp. 2675 * 2676 * IN: tdvp - Directory to contain new entry. 2677 * svp - vnode of new entry. 2678 * name - name of new entry. 2679 * cr - credentials of caller. 2680 * 2681 * RETURN: 0 if success 2682 * error code if failure 2683 * 2684 * Timestamps: 2685 * tdvp - ctime|mtime updated 2686 * svp - ctime updated 2687 */ 2688 /* ARGSUSED */ 2689 static int 2690 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2691 { 2692 znode_t *dzp = VTOZ(tdvp); 2693 znode_t *tzp, *szp; 2694 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2695 zilog_t *zilog = zfsvfs->z_log; 2696 zfs_dirlock_t *dl; 2697 dmu_tx_t *tx; 2698 vnode_t *realvp; 2699 int error; 2700 2701 ASSERT(tdvp->v_type == VDIR); 2702 2703 ZFS_ENTER(zfsvfs); 2704 2705 if (VOP_REALVP(svp, &realvp) == 0) 2706 svp = realvp; 2707 2708 if (svp->v_vfsp != tdvp->v_vfsp) { 2709 ZFS_EXIT(zfsvfs); 2710 return (EXDEV); 2711 } 2712 2713 szp = VTOZ(svp); 2714 top: 2715 /* 2716 * We do not support links between attributes and non-attributes 2717 * because of the potential security risk of creating links 2718 * into "normal" file space in order to circumvent restrictions 2719 * imposed in attribute space. 2720 */ 2721 if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2722 (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2723 ZFS_EXIT(zfsvfs); 2724 return (EINVAL); 2725 } 2726 2727 /* 2728 * POSIX dictates that we return EPERM here. 2729 * Better choices include ENOTSUP or EISDIR. 2730 */ 2731 if (svp->v_type == VDIR) { 2732 ZFS_EXIT(zfsvfs); 2733 return (EPERM); 2734 } 2735 2736 if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2737 secpolicy_basic_link(cr) != 0) { 2738 ZFS_EXIT(zfsvfs); 2739 return (EPERM); 2740 } 2741 2742 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2743 ZFS_EXIT(zfsvfs); 2744 return (error); 2745 } 2746 2747 /* 2748 * Attempt to lock directory; fail if entry already exists. 2749 */ 2750 if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2751 ZFS_EXIT(zfsvfs); 2752 return (error); 2753 } 2754 2755 tx = dmu_tx_create(zfsvfs->z_os); 2756 dmu_tx_hold_bonus(tx, szp->z_id); 2757 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2758 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2759 if (error) { 2760 zfs_dirent_unlock(dl); 2761 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2762 dmu_tx_wait(tx); 2763 dmu_tx_abort(tx); 2764 goto top; 2765 } 2766 dmu_tx_abort(tx); 2767 ZFS_EXIT(zfsvfs); 2768 return (error); 2769 } 2770 2771 error = zfs_link_create(dl, szp, tx, 0); 2772 2773 if (error == 0) 2774 zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2775 2776 dmu_tx_commit(tx); 2777 2778 zfs_dirent_unlock(dl); 2779 2780 ZFS_EXIT(zfsvfs); 2781 return (error); 2782 } 2783 2784 /* 2785 * zfs_null_putapage() is used when the file system has been force 2786 * unmounted. It just drops the pages. 2787 */ 2788 /* ARGSUSED */ 2789 static int 2790 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2791 size_t *lenp, int flags, cred_t *cr) 2792 { 2793 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2794 return (0); 2795 } 2796 2797 /* 2798 * Push a page out to disk, klustering if possible. 2799 * 2800 * IN: vp - file to push page to. 2801 * pp - page to push. 2802 * flags - additional flags. 2803 * cr - credentials of caller. 2804 * 2805 * OUT: offp - start of range pushed. 2806 * lenp - len of range pushed. 2807 * 2808 * RETURN: 0 if success 2809 * error code if failure 2810 * 2811 * NOTE: callers must have locked the page to be pushed. On 2812 * exit, the page (and all other pages in the kluster) must be 2813 * unlocked. 2814 */ 2815 /* ARGSUSED */ 2816 static int 2817 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2818 size_t *lenp, int flags, cred_t *cr) 2819 { 2820 znode_t *zp = VTOZ(vp); 2821 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2822 zilog_t *zilog = zfsvfs->z_log; 2823 dmu_tx_t *tx; 2824 rl_t *rl; 2825 u_offset_t off, koff; 2826 size_t len, klen; 2827 int err; 2828 2829 off = pp->p_offset; 2830 len = PAGESIZE; 2831 /* 2832 * If our blocksize is bigger than the page size, try to kluster 2833 * muiltiple pages so that we write a full block (thus avoiding 2834 * a read-modify-write). 2835 */ 2836 if (zp->z_blksz > PAGESIZE) { 2837 uint64_t filesz = zp->z_phys->zp_size; 2838 2839 if (!ISP2(zp->z_blksz)) { 2840 /* Only one block in the file. */ 2841 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 2842 koff = 0; 2843 } else { 2844 klen = zp->z_blksz; 2845 koff = P2ALIGN(off, (u_offset_t)klen); 2846 } 2847 ASSERT(koff <= filesz); 2848 if (koff + klen > filesz) 2849 klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 2850 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 2851 } 2852 ASSERT3U(btop(len), ==, btopr(len)); 2853 top: 2854 rl = zfs_range_lock(zp, off, len, RL_WRITER); 2855 /* 2856 * Can't push pages past end-of-file. 2857 */ 2858 if (off >= zp->z_phys->zp_size) { 2859 /* discard all pages */ 2860 flags |= B_INVAL; 2861 err = 0; 2862 goto out; 2863 } else if (off + len > zp->z_phys->zp_size) { 2864 int npages = btopr(zp->z_phys->zp_size - off); 2865 page_t *trunc; 2866 2867 page_list_break(&pp, &trunc, npages); 2868 /* discard pages past end of file */ 2869 if (trunc) 2870 pvn_write_done(trunc, B_INVAL | flags); 2871 len = zp->z_phys->zp_size - off; 2872 } 2873 2874 tx = dmu_tx_create(zfsvfs->z_os); 2875 dmu_tx_hold_write(tx, zp->z_id, off, len); 2876 dmu_tx_hold_bonus(tx, zp->z_id); 2877 err = dmu_tx_assign(tx, zfsvfs->z_assign); 2878 if (err != 0) { 2879 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2880 zfs_range_unlock(rl); 2881 dmu_tx_wait(tx); 2882 dmu_tx_abort(tx); 2883 err = 0; 2884 goto top; 2885 } 2886 dmu_tx_abort(tx); 2887 goto out; 2888 } 2889 2890 if (zp->z_blksz <= PAGESIZE) { 2891 caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1); 2892 ASSERT3U(len, <=, PAGESIZE); 2893 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 2894 ppmapout(va); 2895 } else { 2896 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 2897 } 2898 2899 if (err == 0) { 2900 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 2901 (void) zfs_log_write( 2902 zilog, tx, TX_WRITE, zp, off, len, 0, NULL); 2903 dmu_tx_commit(tx); 2904 } 2905 2906 out: 2907 zfs_range_unlock(rl); 2908 pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags); 2909 if (offp) 2910 *offp = off; 2911 if (lenp) 2912 *lenp = len; 2913 2914 return (err); 2915 } 2916 2917 /* 2918 * Copy the portion of the file indicated from pages into the file. 2919 * The pages are stored in a page list attached to the files vnode. 2920 * 2921 * IN: vp - vnode of file to push page data to. 2922 * off - position in file to put data. 2923 * len - amount of data to write. 2924 * flags - flags to control the operation. 2925 * cr - credentials of caller. 2926 * 2927 * RETURN: 0 if success 2928 * error code if failure 2929 * 2930 * Timestamps: 2931 * vp - ctime|mtime updated 2932 */ 2933 static int 2934 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2935 { 2936 znode_t *zp = VTOZ(vp); 2937 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2938 page_t *pp; 2939 size_t io_len; 2940 u_offset_t io_off; 2941 uint64_t filesz; 2942 int error = 0; 2943 2944 ZFS_ENTER(zfsvfs); 2945 2946 ASSERT(zp->z_dbuf_held && zp->z_phys); 2947 2948 if (len == 0) { 2949 /* 2950 * Search the entire vp list for pages >= off. 2951 */ 2952 error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2953 flags, cr); 2954 goto out; 2955 } 2956 2957 filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 2958 if (off > filesz) { 2959 /* past end of file */ 2960 ZFS_EXIT(zfsvfs); 2961 return (0); 2962 } 2963 2964 len = MIN(len, filesz - off); 2965 2966 for (io_off = off; io_off < off + len; io_off += io_len) { 2967 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 2968 pp = page_lookup(vp, io_off, 2969 (flags & (B_INVAL | B_FREE)) ? 2970 SE_EXCL : SE_SHARED); 2971 } else { 2972 pp = page_lookup_nowait(vp, io_off, 2973 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2974 } 2975 2976 if (pp != NULL && pvn_getdirty(pp, flags)) { 2977 int err; 2978 2979 /* 2980 * Found a dirty page to push 2981 */ 2982 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 2983 if (err) 2984 error = err; 2985 } else { 2986 io_len = PAGESIZE; 2987 } 2988 } 2989 out: 2990 if ((flags & B_ASYNC) == 0) 2991 zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 2992 ZFS_EXIT(zfsvfs); 2993 return (error); 2994 } 2995 2996 void 2997 zfs_inactive(vnode_t *vp, cred_t *cr) 2998 { 2999 znode_t *zp = VTOZ(vp); 3000 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3001 int error; 3002 3003 rw_enter(&zfsvfs->z_um_lock, RW_READER); 3004 if (zfsvfs->z_unmounted2) { 3005 ASSERT(zp->z_dbuf_held == 0); 3006 3007 if (vn_has_cached_data(vp)) { 3008 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3009 B_INVAL, cr); 3010 } 3011 3012 mutex_enter(&zp->z_lock); 3013 vp->v_count = 0; /* count arrives as 1 */ 3014 if (zp->z_dbuf == NULL) { 3015 mutex_exit(&zp->z_lock); 3016 zfs_znode_free(zp); 3017 } else { 3018 mutex_exit(&zp->z_lock); 3019 } 3020 rw_exit(&zfsvfs->z_um_lock); 3021 VFS_RELE(zfsvfs->z_vfs); 3022 return; 3023 } 3024 3025 /* 3026 * Attempt to push any data in the page cache. If this fails 3027 * we will get kicked out later in zfs_zinactive(). 3028 */ 3029 if (vn_has_cached_data(vp)) { 3030 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 3031 cr); 3032 } 3033 3034 if (zp->z_atime_dirty && zp->z_reap == 0) { 3035 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3036 3037 dmu_tx_hold_bonus(tx, zp->z_id); 3038 error = dmu_tx_assign(tx, TXG_WAIT); 3039 if (error) { 3040 dmu_tx_abort(tx); 3041 } else { 3042 dmu_buf_will_dirty(zp->z_dbuf, tx); 3043 mutex_enter(&zp->z_lock); 3044 zp->z_atime_dirty = 0; 3045 mutex_exit(&zp->z_lock); 3046 dmu_tx_commit(tx); 3047 } 3048 } 3049 3050 zfs_zinactive(zp); 3051 rw_exit(&zfsvfs->z_um_lock); 3052 } 3053 3054 /* 3055 * Bounds-check the seek operation. 3056 * 3057 * IN: vp - vnode seeking within 3058 * ooff - old file offset 3059 * noffp - pointer to new file offset 3060 * 3061 * RETURN: 0 if success 3062 * EINVAL if new offset invalid 3063 */ 3064 /* ARGSUSED */ 3065 static int 3066 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 3067 { 3068 if (vp->v_type == VDIR) 3069 return (0); 3070 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3071 } 3072 3073 /* 3074 * Pre-filter the generic locking function to trap attempts to place 3075 * a mandatory lock on a memory mapped file. 3076 */ 3077 static int 3078 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 3079 flk_callback_t *flk_cbp, cred_t *cr) 3080 { 3081 znode_t *zp = VTOZ(vp); 3082 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3083 int error; 3084 3085 ZFS_ENTER(zfsvfs); 3086 3087 /* 3088 * We are following the UFS semantics with respect to mapcnt 3089 * here: If we see that the file is mapped already, then we will 3090 * return an error, but we don't worry about races between this 3091 * function and zfs_map(). 3092 */ 3093 if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3094 ZFS_EXIT(zfsvfs); 3095 return (EAGAIN); 3096 } 3097 error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 3098 ZFS_EXIT(zfsvfs); 3099 return (error); 3100 } 3101 3102 /* 3103 * If we can't find a page in the cache, we will create a new page 3104 * and fill it with file data. For efficiency, we may try to fill 3105 * multiple pages at once (klustering). 3106 */ 3107 static int 3108 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3109 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3110 { 3111 znode_t *zp = VTOZ(vp); 3112 page_t *pp, *cur_pp; 3113 objset_t *os = zp->z_zfsvfs->z_os; 3114 caddr_t va; 3115 u_offset_t io_off, total; 3116 uint64_t oid = zp->z_id; 3117 size_t io_len; 3118 uint64_t filesz; 3119 int err; 3120 3121 /* 3122 * If we are only asking for a single page don't bother klustering. 3123 */ 3124 filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 3125 if (off >= filesz) 3126 return (EFAULT); 3127 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3128 io_off = off; 3129 io_len = PAGESIZE; 3130 pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3131 } else { 3132 /* 3133 * Try to fill a kluster of pages (a blocks worth). 3134 */ 3135 size_t klen; 3136 u_offset_t koff; 3137 3138 if (!ISP2(zp->z_blksz)) { 3139 /* Only one block in the file. */ 3140 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3141 koff = 0; 3142 } else { 3143 /* 3144 * It would be ideal to align our offset to the 3145 * blocksize but doing so has resulted in some 3146 * strange application crashes. For now, we 3147 * leave the offset as is and only adjust the 3148 * length if we are off the end of the file. 3149 */ 3150 koff = off; 3151 klen = plsz; 3152 } 3153 ASSERT(koff <= filesz); 3154 if (koff + klen > filesz) 3155 klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 3156 ASSERT3U(off, >=, koff); 3157 ASSERT3U(off, <, koff + klen); 3158 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 3159 &io_len, koff, klen, 0); 3160 } 3161 if (pp == NULL) { 3162 /* 3163 * Some other thread entered the page before us. 3164 * Return to zfs_getpage to retry the lookup. 3165 */ 3166 *pl = NULL; 3167 return (0); 3168 } 3169 3170 /* 3171 * Fill the pages in the kluster. 3172 */ 3173 cur_pp = pp; 3174 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 3175 ASSERT3U(io_off, ==, cur_pp->p_offset); 3176 va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 3177 err = dmu_read(os, oid, io_off, PAGESIZE, va); 3178 ppmapout(va); 3179 if (err) { 3180 /* On error, toss the entire kluster */ 3181 pvn_read_done(pp, B_ERROR); 3182 return (err); 3183 } 3184 cur_pp = cur_pp->p_next; 3185 } 3186 out: 3187 /* 3188 * Fill in the page list array from the kluster. If 3189 * there are too many pages in the kluster, return 3190 * as many pages as possible starting from the desired 3191 * offset `off'. 3192 * NOTE: the page list will always be null terminated. 3193 */ 3194 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3195 3196 return (0); 3197 } 3198 3199 /* 3200 * Return pointers to the pages for the file region [off, off + len] 3201 * in the pl array. If plsz is greater than len, this function may 3202 * also return page pointers from before or after the specified 3203 * region (i.e. some region [off', off' + plsz]). These additional 3204 * pages are only returned if they are already in the cache, or were 3205 * created as part of a klustered read. 3206 * 3207 * IN: vp - vnode of file to get data from. 3208 * off - position in file to get data from. 3209 * len - amount of data to retrieve. 3210 * plsz - length of provided page list. 3211 * seg - segment to obtain pages for. 3212 * addr - virtual address of fault. 3213 * rw - mode of created pages. 3214 * cr - credentials of caller. 3215 * 3216 * OUT: protp - protection mode of created pages. 3217 * pl - list of pages created. 3218 * 3219 * RETURN: 0 if success 3220 * error code if failure 3221 * 3222 * Timestamps: 3223 * vp - atime updated 3224 */ 3225 /* ARGSUSED */ 3226 static int 3227 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3228 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3229 enum seg_rw rw, cred_t *cr) 3230 { 3231 znode_t *zp = VTOZ(vp); 3232 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3233 page_t *pp, **pl0 = pl; 3234 int need_unlock = 0, err = 0; 3235 offset_t orig_off; 3236 3237 ZFS_ENTER(zfsvfs); 3238 3239 if (protp) 3240 *protp = PROT_ALL; 3241 3242 ASSERT(zp->z_dbuf_held && zp->z_phys); 3243 3244 /* no faultahead (for now) */ 3245 if (pl == NULL) { 3246 ZFS_EXIT(zfsvfs); 3247 return (0); 3248 } 3249 3250 /* can't fault past EOF */ 3251 if (off >= zp->z_phys->zp_size) { 3252 ZFS_EXIT(zfsvfs); 3253 return (EFAULT); 3254 } 3255 orig_off = off; 3256 3257 /* 3258 * If we already own the lock, then we must be page faulting 3259 * in the middle of a write to this file (i.e., we are writing 3260 * to this file using data from a mapped region of the file). 3261 */ 3262 if (rw_owner(&zp->z_map_lock) != curthread) { 3263 rw_enter(&zp->z_map_lock, RW_WRITER); 3264 need_unlock = TRUE; 3265 } 3266 3267 /* 3268 * Loop through the requested range [off, off + len] looking 3269 * for pages. If we don't find a page, we will need to create 3270 * a new page and fill it with data from the file. 3271 */ 3272 while (len > 0) { 3273 if (plsz < PAGESIZE) 3274 break; 3275 if (pp = page_lookup(vp, off, SE_SHARED)) { 3276 *pl++ = pp; 3277 off += PAGESIZE; 3278 addr += PAGESIZE; 3279 len -= PAGESIZE; 3280 plsz -= PAGESIZE; 3281 } else { 3282 err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 3283 if (err) 3284 goto out; 3285 /* 3286 * klustering may have changed our region 3287 * to be block aligned. 3288 */ 3289 if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3290 int delta = off - pp->p_offset; 3291 len += delta; 3292 off -= delta; 3293 addr -= delta; 3294 } 3295 while (*pl) { 3296 pl++; 3297 off += PAGESIZE; 3298 addr += PAGESIZE; 3299 plsz -= PAGESIZE; 3300 if (len > PAGESIZE) 3301 len -= PAGESIZE; 3302 else 3303 len = 0; 3304 } 3305 } 3306 } 3307 3308 /* 3309 * Fill out the page array with any pages already in the cache. 3310 */ 3311 while (plsz > 0) { 3312 pp = page_lookup_nowait(vp, off, SE_SHARED); 3313 if (pp == NULL) 3314 break; 3315 *pl++ = pp; 3316 off += PAGESIZE; 3317 plsz -= PAGESIZE; 3318 } 3319 3320 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3321 out: 3322 /* 3323 * We can't grab the range lock for the page as reader which would 3324 * stop truncation as this leads to deadlock. So we need to recheck 3325 * the file size. 3326 */ 3327 if (orig_off >= zp->z_phys->zp_size) 3328 err = EFAULT; 3329 if (err) { 3330 /* 3331 * Release any pages we have previously locked. 3332 */ 3333 while (pl > pl0) 3334 page_unlock(*--pl); 3335 } 3336 3337 *pl = NULL; 3338 3339 if (need_unlock) 3340 rw_exit(&zp->z_map_lock); 3341 3342 ZFS_EXIT(zfsvfs); 3343 return (err); 3344 } 3345 3346 /* 3347 * Request a memory map for a section of a file. This code interacts 3348 * with common code and the VM system as follows: 3349 * 3350 * common code calls mmap(), which ends up in smmap_common() 3351 * 3352 * this calls VOP_MAP(), which takes you into (say) zfs 3353 * 3354 * zfs_map() calls as_map(), passing segvn_create() as the callback 3355 * 3356 * segvn_create() creates the new segment and calls VOP_ADDMAP() 3357 * 3358 * zfs_addmap() updates z_mapcnt 3359 */ 3360 static int 3361 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3362 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3363 { 3364 znode_t *zp = VTOZ(vp); 3365 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3366 segvn_crargs_t vn_a; 3367 int error; 3368 3369 ZFS_ENTER(zfsvfs); 3370 3371 if (vp->v_flag & VNOMAP) { 3372 ZFS_EXIT(zfsvfs); 3373 return (ENOSYS); 3374 } 3375 3376 if (off < 0 || len > MAXOFFSET_T - off) { 3377 ZFS_EXIT(zfsvfs); 3378 return (ENXIO); 3379 } 3380 3381 if (vp->v_type != VREG) { 3382 ZFS_EXIT(zfsvfs); 3383 return (ENODEV); 3384 } 3385 3386 /* 3387 * If file is locked, disallow mapping. 3388 */ 3389 if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 3390 ZFS_EXIT(zfsvfs); 3391 return (EAGAIN); 3392 } 3393 3394 as_rangelock(as); 3395 if ((flags & MAP_FIXED) == 0) { 3396 map_addr(addrp, len, off, 1, flags); 3397 if (*addrp == NULL) { 3398 as_rangeunlock(as); 3399 ZFS_EXIT(zfsvfs); 3400 return (ENOMEM); 3401 } 3402 } else { 3403 /* 3404 * User specified address - blow away any previous mappings 3405 */ 3406 (void) as_unmap(as, *addrp, len); 3407 } 3408 3409 vn_a.vp = vp; 3410 vn_a.offset = (u_offset_t)off; 3411 vn_a.type = flags & MAP_TYPE; 3412 vn_a.prot = prot; 3413 vn_a.maxprot = maxprot; 3414 vn_a.cred = cr; 3415 vn_a.amp = NULL; 3416 vn_a.flags = flags & ~MAP_TYPE; 3417 vn_a.szc = 0; 3418 vn_a.lgrp_mem_policy_flags = 0; 3419 3420 error = as_map(as, *addrp, len, segvn_create, &vn_a); 3421 3422 as_rangeunlock(as); 3423 ZFS_EXIT(zfsvfs); 3424 return (error); 3425 } 3426 3427 /* ARGSUSED */ 3428 static int 3429 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3430 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3431 { 3432 uint64_t pages = btopr(len); 3433 3434 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3435 return (0); 3436 } 3437 3438 /* 3439 * The reason we push dirty pages as part of zfs_delmap() is so that we get a 3440 * more accurate mtime for the associated file. Since we don't have a way of 3441 * detecting when the data was actually modified, we have to resort to 3442 * heuristics. If an explicit msync() is done, then we mark the mtime when the 3443 * last page is pushed. The problem occurs when the msync() call is omitted, 3444 * which by far the most common case: 3445 * 3446 * open() 3447 * mmap() 3448 * <modify memory> 3449 * munmap() 3450 * close() 3451 * <time lapse> 3452 * putpage() via fsflush 3453 * 3454 * If we wait until fsflush to come along, we can have a modification time that 3455 * is some arbitrary point in the future. In order to prevent this in the 3456 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 3457 * torn down. 3458 */ 3459 /* ARGSUSED */ 3460 static int 3461 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3462 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3463 { 3464 uint64_t pages = btopr(len); 3465 3466 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 3467 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 3468 3469 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 3470 vn_has_cached_data(vp)) 3471 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 3472 3473 return (0); 3474 } 3475 3476 /* 3477 * Free or allocate space in a file. Currently, this function only 3478 * supports the `F_FREESP' command. However, this command is somewhat 3479 * misnamed, as its functionality includes the ability to allocate as 3480 * well as free space. 3481 * 3482 * IN: vp - vnode of file to free data in. 3483 * cmd - action to take (only F_FREESP supported). 3484 * bfp - section of file to free/alloc. 3485 * flag - current file open mode flags. 3486 * offset - current file offset. 3487 * cr - credentials of caller [UNUSED]. 3488 * 3489 * RETURN: 0 if success 3490 * error code if failure 3491 * 3492 * Timestamps: 3493 * vp - ctime|mtime updated 3494 */ 3495 /* ARGSUSED */ 3496 static int 3497 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3498 offset_t offset, cred_t *cr, caller_context_t *ct) 3499 { 3500 znode_t *zp = VTOZ(vp); 3501 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3502 uint64_t off, len; 3503 int error; 3504 3505 ZFS_ENTER(zfsvfs); 3506 3507 top: 3508 if (cmd != F_FREESP) { 3509 ZFS_EXIT(zfsvfs); 3510 return (EINVAL); 3511 } 3512 3513 if (error = convoff(vp, bfp, 0, offset)) { 3514 ZFS_EXIT(zfsvfs); 3515 return (error); 3516 } 3517 3518 if (bfp->l_len < 0) { 3519 ZFS_EXIT(zfsvfs); 3520 return (EINVAL); 3521 } 3522 3523 off = bfp->l_start; 3524 len = bfp->l_len; /* 0 means from off to end of file */ 3525 3526 do { 3527 error = zfs_freesp(zp, off, len, flag, TRUE); 3528 /* NB: we already did dmu_tx_wait() if necessary */ 3529 } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3530 3531 ZFS_EXIT(zfsvfs); 3532 return (error); 3533 } 3534 3535 static int 3536 zfs_fid(vnode_t *vp, fid_t *fidp) 3537 { 3538 znode_t *zp = VTOZ(vp); 3539 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3540 uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3541 uint64_t object = zp->z_id; 3542 zfid_short_t *zfid; 3543 int size, i; 3544 3545 ZFS_ENTER(zfsvfs); 3546 3547 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3548 if (fidp->fid_len < size) { 3549 fidp->fid_len = size; 3550 ZFS_EXIT(zfsvfs); 3551 return (ENOSPC); 3552 } 3553 3554 zfid = (zfid_short_t *)fidp; 3555 3556 zfid->zf_len = size; 3557 3558 for (i = 0; i < sizeof (zfid->zf_object); i++) 3559 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3560 3561 /* Must have a non-zero generation number to distinguish from .zfs */ 3562 if (gen == 0) 3563 gen = 1; 3564 for (i = 0; i < sizeof (zfid->zf_gen); i++) 3565 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3566 3567 if (size == LONG_FID_LEN) { 3568 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3569 zfid_long_t *zlfid; 3570 3571 zlfid = (zfid_long_t *)fidp; 3572 3573 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3574 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3575 3576 /* XXX - this should be the generation number for the objset */ 3577 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3578 zlfid->zf_setgen[i] = 0; 3579 } 3580 3581 ZFS_EXIT(zfsvfs); 3582 return (0); 3583 } 3584 3585 static int 3586 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3587 { 3588 znode_t *zp, *xzp; 3589 zfsvfs_t *zfsvfs; 3590 zfs_dirlock_t *dl; 3591 int error; 3592 3593 switch (cmd) { 3594 case _PC_LINK_MAX: 3595 *valp = ULONG_MAX; 3596 return (0); 3597 3598 case _PC_FILESIZEBITS: 3599 *valp = 64; 3600 return (0); 3601 3602 case _PC_XATTR_EXISTS: 3603 zp = VTOZ(vp); 3604 zfsvfs = zp->z_zfsvfs; 3605 ZFS_ENTER(zfsvfs); 3606 *valp = 0; 3607 error = zfs_dirent_lock(&dl, zp, "", &xzp, 3608 ZXATTR | ZEXISTS | ZSHARED); 3609 if (error == 0) { 3610 zfs_dirent_unlock(dl); 3611 if (!zfs_dirempty(xzp)) 3612 *valp = 1; 3613 VN_RELE(ZTOV(xzp)); 3614 } else if (error == ENOENT) { 3615 /* 3616 * If there aren't extended attributes, it's the 3617 * same as having zero of them. 3618 */ 3619 error = 0; 3620 } 3621 ZFS_EXIT(zfsvfs); 3622 return (error); 3623 3624 case _PC_ACL_ENABLED: 3625 *valp = _ACL_ACE_ENABLED; 3626 return (0); 3627 3628 case _PC_MIN_HOLE_SIZE: 3629 *valp = (ulong_t)SPA_MINBLOCKSIZE; 3630 return (0); 3631 3632 default: 3633 return (fs_pathconf(vp, cmd, valp, cr)); 3634 } 3635 } 3636 3637 /*ARGSUSED*/ 3638 static int 3639 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3640 { 3641 znode_t *zp = VTOZ(vp); 3642 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3643 int error; 3644 3645 ZFS_ENTER(zfsvfs); 3646 error = zfs_getacl(zp, vsecp, cr); 3647 ZFS_EXIT(zfsvfs); 3648 3649 return (error); 3650 } 3651 3652 /*ARGSUSED*/ 3653 static int 3654 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3655 { 3656 znode_t *zp = VTOZ(vp); 3657 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3658 int error; 3659 3660 ZFS_ENTER(zfsvfs); 3661 error = zfs_setacl(zp, vsecp, cr); 3662 ZFS_EXIT(zfsvfs); 3663 return (error); 3664 } 3665 3666 /* 3667 * Predeclare these here so that the compiler assumes that 3668 * this is an "old style" function declaration that does 3669 * not include arguments => we won't get type mismatch errors 3670 * in the initializations that follow. 3671 */ 3672 static int zfs_inval(); 3673 static int zfs_isdir(); 3674 3675 static int 3676 zfs_inval() 3677 { 3678 return (EINVAL); 3679 } 3680 3681 static int 3682 zfs_isdir() 3683 { 3684 return (EISDIR); 3685 } 3686 /* 3687 * Directory vnode operations template 3688 */ 3689 vnodeops_t *zfs_dvnodeops; 3690 const fs_operation_def_t zfs_dvnodeops_template[] = { 3691 VOPNAME_OPEN, zfs_open, 3692 VOPNAME_CLOSE, zfs_close, 3693 VOPNAME_READ, zfs_isdir, 3694 VOPNAME_WRITE, zfs_isdir, 3695 VOPNAME_IOCTL, zfs_ioctl, 3696 VOPNAME_GETATTR, zfs_getattr, 3697 VOPNAME_SETATTR, zfs_setattr, 3698 VOPNAME_ACCESS, zfs_access, 3699 VOPNAME_LOOKUP, zfs_lookup, 3700 VOPNAME_CREATE, zfs_create, 3701 VOPNAME_REMOVE, zfs_remove, 3702 VOPNAME_LINK, zfs_link, 3703 VOPNAME_RENAME, zfs_rename, 3704 VOPNAME_MKDIR, zfs_mkdir, 3705 VOPNAME_RMDIR, zfs_rmdir, 3706 VOPNAME_READDIR, zfs_readdir, 3707 VOPNAME_SYMLINK, zfs_symlink, 3708 VOPNAME_FSYNC, zfs_fsync, 3709 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3710 VOPNAME_FID, zfs_fid, 3711 VOPNAME_SEEK, zfs_seek, 3712 VOPNAME_PATHCONF, zfs_pathconf, 3713 VOPNAME_GETSECATTR, zfs_getsecattr, 3714 VOPNAME_SETSECATTR, zfs_setsecattr, 3715 NULL, NULL 3716 }; 3717 3718 /* 3719 * Regular file vnode operations template 3720 */ 3721 vnodeops_t *zfs_fvnodeops; 3722 const fs_operation_def_t zfs_fvnodeops_template[] = { 3723 VOPNAME_OPEN, zfs_open, 3724 VOPNAME_CLOSE, zfs_close, 3725 VOPNAME_READ, zfs_read, 3726 VOPNAME_WRITE, zfs_write, 3727 VOPNAME_IOCTL, zfs_ioctl, 3728 VOPNAME_GETATTR, zfs_getattr, 3729 VOPNAME_SETATTR, zfs_setattr, 3730 VOPNAME_ACCESS, zfs_access, 3731 VOPNAME_LOOKUP, zfs_lookup, 3732 VOPNAME_RENAME, zfs_rename, 3733 VOPNAME_FSYNC, zfs_fsync, 3734 VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive, 3735 VOPNAME_FID, zfs_fid, 3736 VOPNAME_SEEK, zfs_seek, 3737 VOPNAME_FRLOCK, zfs_frlock, 3738 VOPNAME_SPACE, zfs_space, 3739 VOPNAME_GETPAGE, zfs_getpage, 3740 VOPNAME_PUTPAGE, zfs_putpage, 3741 VOPNAME_MAP, (fs_generic_func_p) zfs_map, 3742 VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap, 3743 VOPNAME_DELMAP, zfs_delmap, 3744 VOPNAME_PATHCONF, zfs_pathconf, 3745 VOPNAME_GETSECATTR, zfs_getsecattr, 3746 VOPNAME_SETSECATTR, zfs_setsecattr, 3747 VOPNAME_VNEVENT, fs_vnevent_support, 3748 NULL, NULL 3749 }; 3750 3751 /* 3752 * Symbolic link vnode operations template 3753 */ 3754 vnodeops_t *zfs_symvnodeops; 3755 const fs_operation_def_t zfs_symvnodeops_template[] = { 3756 VOPNAME_GETATTR, zfs_getattr, 3757 VOPNAME_SETATTR, zfs_setattr, 3758 VOPNAME_ACCESS, zfs_access, 3759 VOPNAME_RENAME, zfs_rename, 3760 VOPNAME_READLINK, zfs_readlink, 3761 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3762 VOPNAME_FID, zfs_fid, 3763 VOPNAME_PATHCONF, zfs_pathconf, 3764 VOPNAME_VNEVENT, fs_vnevent_support, 3765 NULL, NULL 3766 }; 3767 3768 /* 3769 * Extended attribute directory vnode operations template 3770 * This template is identical to the directory vnodes 3771 * operation template except for restricted operations: 3772 * VOP_MKDIR() 3773 * VOP_SYMLINK() 3774 * Note that there are other restrictions embedded in: 3775 * zfs_create() - restrict type to VREG 3776 * zfs_link() - no links into/out of attribute space 3777 * zfs_rename() - no moves into/out of attribute space 3778 */ 3779 vnodeops_t *zfs_xdvnodeops; 3780 const fs_operation_def_t zfs_xdvnodeops_template[] = { 3781 VOPNAME_OPEN, zfs_open, 3782 VOPNAME_CLOSE, zfs_close, 3783 VOPNAME_IOCTL, zfs_ioctl, 3784 VOPNAME_GETATTR, zfs_getattr, 3785 VOPNAME_SETATTR, zfs_setattr, 3786 VOPNAME_ACCESS, zfs_access, 3787 VOPNAME_LOOKUP, zfs_lookup, 3788 VOPNAME_CREATE, zfs_create, 3789 VOPNAME_REMOVE, zfs_remove, 3790 VOPNAME_LINK, zfs_link, 3791 VOPNAME_RENAME, zfs_rename, 3792 VOPNAME_MKDIR, zfs_inval, 3793 VOPNAME_RMDIR, zfs_rmdir, 3794 VOPNAME_READDIR, zfs_readdir, 3795 VOPNAME_SYMLINK, zfs_inval, 3796 VOPNAME_FSYNC, zfs_fsync, 3797 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3798 VOPNAME_FID, zfs_fid, 3799 VOPNAME_SEEK, zfs_seek, 3800 VOPNAME_PATHCONF, zfs_pathconf, 3801 VOPNAME_GETSECATTR, zfs_getsecattr, 3802 VOPNAME_SETSECATTR, zfs_setsecattr, 3803 VOPNAME_VNEVENT, fs_vnevent_support, 3804 NULL, NULL 3805 }; 3806 3807 /* 3808 * Error vnode operations template 3809 */ 3810 vnodeops_t *zfs_evnodeops; 3811 const fs_operation_def_t zfs_evnodeops_template[] = { 3812 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3813 VOPNAME_PATHCONF, zfs_pathconf, 3814 NULL, NULL 3815 }; 3816