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 2007 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_unlinked) { 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 * If the xattr property is off, refuse the lookup request. 1005 */ 1006 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 1007 ZFS_EXIT(zfsvfs); 1008 return (EINVAL); 1009 } 1010 1011 /* 1012 * We don't allow recursive attributes.. 1013 * Maybe someday we will. 1014 */ 1015 if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1016 ZFS_EXIT(zfsvfs); 1017 return (EINVAL); 1018 } 1019 1020 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1021 ZFS_EXIT(zfsvfs); 1022 return (error); 1023 } 1024 1025 /* 1026 * Do we have permission to get into attribute directory? 1027 */ 1028 1029 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 1030 VN_RELE(*vpp); 1031 } 1032 1033 ZFS_EXIT(zfsvfs); 1034 return (error); 1035 } 1036 1037 if (dvp->v_type != VDIR) { 1038 ZFS_EXIT(zfsvfs); 1039 return (ENOTDIR); 1040 } 1041 1042 /* 1043 * Check accessibility of directory. 1044 */ 1045 1046 if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 1047 ZFS_EXIT(zfsvfs); 1048 return (error); 1049 } 1050 1051 if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 1052 1053 /* 1054 * Convert device special files 1055 */ 1056 if (IS_DEVVP(*vpp)) { 1057 vnode_t *svp; 1058 1059 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1060 VN_RELE(*vpp); 1061 if (svp == NULL) 1062 error = ENOSYS; 1063 else 1064 *vpp = svp; 1065 } 1066 } 1067 1068 ZFS_EXIT(zfsvfs); 1069 return (error); 1070 } 1071 1072 /* 1073 * Attempt to create a new entry in a directory. If the entry 1074 * already exists, truncate the file if permissible, else return 1075 * an error. Return the vp of the created or trunc'd file. 1076 * 1077 * IN: dvp - vnode of directory to put new file entry in. 1078 * name - name of new file entry. 1079 * vap - attributes of new file. 1080 * excl - flag indicating exclusive or non-exclusive mode. 1081 * mode - mode to open file with. 1082 * cr - credentials of caller. 1083 * flag - large file flag [UNUSED]. 1084 * 1085 * OUT: vpp - vnode of created or trunc'd entry. 1086 * 1087 * RETURN: 0 if success 1088 * error code if failure 1089 * 1090 * Timestamps: 1091 * dvp - ctime|mtime updated if new entry created 1092 * vp - ctime|mtime always, atime if new 1093 */ 1094 /* ARGSUSED */ 1095 static int 1096 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1097 int mode, vnode_t **vpp, cred_t *cr, int flag) 1098 { 1099 znode_t *zp, *dzp = VTOZ(dvp); 1100 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1101 zilog_t *zilog = zfsvfs->z_log; 1102 objset_t *os = zfsvfs->z_os; 1103 zfs_dirlock_t *dl; 1104 dmu_tx_t *tx; 1105 int error; 1106 uint64_t zoid; 1107 1108 ZFS_ENTER(zfsvfs); 1109 1110 top: 1111 *vpp = NULL; 1112 1113 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1114 vap->va_mode &= ~VSVTX; 1115 1116 if (*name == '\0') { 1117 /* 1118 * Null component name refers to the directory itself. 1119 */ 1120 VN_HOLD(dvp); 1121 zp = dzp; 1122 dl = NULL; 1123 error = 0; 1124 } else { 1125 /* possible VN_HOLD(zp) */ 1126 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1127 if (strcmp(name, "..") == 0) 1128 error = EISDIR; 1129 ZFS_EXIT(zfsvfs); 1130 return (error); 1131 } 1132 } 1133 1134 zoid = zp ? zp->z_id : -1ULL; 1135 1136 if (zp == NULL) { 1137 /* 1138 * Create a new file object and update the directory 1139 * to reference it. 1140 */ 1141 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1142 goto out; 1143 } 1144 1145 /* 1146 * We only support the creation of regular files in 1147 * extended attribute directories. 1148 */ 1149 if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1150 (vap->va_type != VREG)) { 1151 error = EINVAL; 1152 goto out; 1153 } 1154 1155 tx = dmu_tx_create(os); 1156 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1157 dmu_tx_hold_bonus(tx, dzp->z_id); 1158 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1159 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1160 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1161 0, SPA_MAXBLOCKSIZE); 1162 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1163 if (error) { 1164 zfs_dirent_unlock(dl); 1165 if (error == ERESTART && 1166 zfsvfs->z_assign == TXG_NOWAIT) { 1167 dmu_tx_wait(tx); 1168 dmu_tx_abort(tx); 1169 goto top; 1170 } 1171 dmu_tx_abort(tx); 1172 ZFS_EXIT(zfsvfs); 1173 return (error); 1174 } 1175 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1176 ASSERT(zp->z_id == zoid); 1177 (void) zfs_link_create(dl, zp, tx, ZNEW); 1178 zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1179 dmu_tx_commit(tx); 1180 } else { 1181 /* 1182 * A directory entry already exists for this name. 1183 */ 1184 /* 1185 * Can't truncate an existing file if in exclusive mode. 1186 */ 1187 if (excl == EXCL) { 1188 error = EEXIST; 1189 goto out; 1190 } 1191 /* 1192 * Can't open a directory for writing. 1193 */ 1194 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1195 error = EISDIR; 1196 goto out; 1197 } 1198 /* 1199 * Verify requested access to file. 1200 */ 1201 if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1202 goto out; 1203 } 1204 1205 mutex_enter(&dzp->z_lock); 1206 dzp->z_seq++; 1207 mutex_exit(&dzp->z_lock); 1208 1209 /* 1210 * Truncate regular files if requested. 1211 */ 1212 if ((ZTOV(zp)->v_type == VREG) && 1213 (zp->z_phys->zp_size != 0) && 1214 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 1215 error = zfs_freesp(zp, 0, 0, mode, TRUE); 1216 if (error == ERESTART && 1217 zfsvfs->z_assign == TXG_NOWAIT) { 1218 /* NB: we already did dmu_tx_wait() */ 1219 zfs_dirent_unlock(dl); 1220 VN_RELE(ZTOV(zp)); 1221 goto top; 1222 } 1223 } 1224 } 1225 out: 1226 1227 if (dl) 1228 zfs_dirent_unlock(dl); 1229 1230 if (error) { 1231 if (zp) 1232 VN_RELE(ZTOV(zp)); 1233 } else { 1234 *vpp = ZTOV(zp); 1235 /* 1236 * If vnode is for a device return a specfs vnode instead. 1237 */ 1238 if (IS_DEVVP(*vpp)) { 1239 struct vnode *svp; 1240 1241 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1242 VN_RELE(*vpp); 1243 if (svp == NULL) { 1244 error = ENOSYS; 1245 } 1246 *vpp = svp; 1247 } 1248 } 1249 1250 ZFS_EXIT(zfsvfs); 1251 return (error); 1252 } 1253 1254 /* 1255 * Remove an entry from a directory. 1256 * 1257 * IN: dvp - vnode of directory to remove entry from. 1258 * name - name of entry to remove. 1259 * cr - credentials of caller. 1260 * 1261 * RETURN: 0 if success 1262 * error code if failure 1263 * 1264 * Timestamps: 1265 * dvp - ctime|mtime 1266 * vp - ctime (if nlink > 0) 1267 */ 1268 static int 1269 zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1270 { 1271 znode_t *zp, *dzp = VTOZ(dvp); 1272 znode_t *xzp = NULL; 1273 vnode_t *vp; 1274 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1275 zilog_t *zilog = zfsvfs->z_log; 1276 uint64_t acl_obj, xattr_obj; 1277 zfs_dirlock_t *dl; 1278 dmu_tx_t *tx; 1279 boolean_t may_delete_now, delete_now = FALSE; 1280 boolean_t unlinked; 1281 int error; 1282 1283 ZFS_ENTER(zfsvfs); 1284 1285 top: 1286 /* 1287 * Attempt to lock directory; fail if entry doesn't exist. 1288 */ 1289 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1290 ZFS_EXIT(zfsvfs); 1291 return (error); 1292 } 1293 1294 vp = ZTOV(zp); 1295 1296 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1297 goto out; 1298 } 1299 1300 /* 1301 * Need to use rmdir for removing directories. 1302 */ 1303 if (vp->v_type == VDIR) { 1304 error = EPERM; 1305 goto out; 1306 } 1307 1308 vnevent_remove(vp); 1309 1310 dnlc_remove(dvp, name); 1311 1312 mutex_enter(&vp->v_lock); 1313 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1314 mutex_exit(&vp->v_lock); 1315 1316 /* 1317 * We may delete the znode now, or we may put it in the unlinked set; 1318 * it depends on whether we're the last link, and on whether there are 1319 * other holds on the vnode. So we dmu_tx_hold() the right things to 1320 * allow for either case. 1321 */ 1322 tx = dmu_tx_create(zfsvfs->z_os); 1323 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1324 dmu_tx_hold_bonus(tx, zp->z_id); 1325 if (may_delete_now) 1326 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1327 1328 /* are there any extended attributes? */ 1329 if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1330 /* XXX - do we need this if we are deleting? */ 1331 dmu_tx_hold_bonus(tx, xattr_obj); 1332 } 1333 1334 /* are there any additional acls */ 1335 if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1336 may_delete_now) 1337 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1338 1339 /* charge as an update -- would be nice not to charge at all */ 1340 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1341 1342 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1343 if (error) { 1344 zfs_dirent_unlock(dl); 1345 VN_RELE(vp); 1346 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1347 dmu_tx_wait(tx); 1348 dmu_tx_abort(tx); 1349 goto top; 1350 } 1351 dmu_tx_abort(tx); 1352 ZFS_EXIT(zfsvfs); 1353 return (error); 1354 } 1355 1356 /* 1357 * Remove the directory entry. 1358 */ 1359 error = zfs_link_destroy(dl, zp, tx, 0, &unlinked); 1360 1361 if (error) { 1362 dmu_tx_commit(tx); 1363 goto out; 1364 } 1365 1366 if (unlinked) { 1367 mutex_enter(&vp->v_lock); 1368 delete_now = may_delete_now && 1369 vp->v_count == 1 && !vn_has_cached_data(vp) && 1370 zp->z_phys->zp_xattr == xattr_obj && 1371 zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1372 mutex_exit(&vp->v_lock); 1373 } 1374 1375 if (delete_now) { 1376 if (zp->z_phys->zp_xattr) { 1377 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1378 ASSERT3U(error, ==, 0); 1379 ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1380 dmu_buf_will_dirty(xzp->z_dbuf, tx); 1381 mutex_enter(&xzp->z_lock); 1382 xzp->z_unlinked = 1; 1383 xzp->z_phys->zp_links = 0; 1384 mutex_exit(&xzp->z_lock); 1385 zfs_unlinked_add(xzp, tx); 1386 zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1387 } 1388 mutex_enter(&zp->z_lock); 1389 mutex_enter(&vp->v_lock); 1390 vp->v_count--; 1391 ASSERT3U(vp->v_count, ==, 0); 1392 mutex_exit(&vp->v_lock); 1393 mutex_exit(&zp->z_lock); 1394 zfs_znode_delete(zp, tx); 1395 VFS_RELE(zfsvfs->z_vfs); 1396 } else if (unlinked) { 1397 zfs_unlinked_add(zp, tx); 1398 } 1399 1400 zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1401 1402 dmu_tx_commit(tx); 1403 out: 1404 zfs_dirent_unlock(dl); 1405 1406 if (!delete_now) { 1407 VN_RELE(vp); 1408 } else if (xzp) { 1409 /* this rele delayed to prevent nesting transactions */ 1410 VN_RELE(ZTOV(xzp)); 1411 } 1412 1413 ZFS_EXIT(zfsvfs); 1414 return (error); 1415 } 1416 1417 /* 1418 * Create a new directory and insert it into dvp using the name 1419 * provided. Return a pointer to the inserted directory. 1420 * 1421 * IN: dvp - vnode of directory to add subdir to. 1422 * dirname - name of new directory. 1423 * vap - attributes of new directory. 1424 * cr - credentials of caller. 1425 * 1426 * OUT: vpp - vnode of created directory. 1427 * 1428 * RETURN: 0 if success 1429 * error code if failure 1430 * 1431 * Timestamps: 1432 * dvp - ctime|mtime updated 1433 * vp - ctime|mtime|atime updated 1434 */ 1435 static int 1436 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1437 { 1438 znode_t *zp, *dzp = VTOZ(dvp); 1439 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1440 zilog_t *zilog = zfsvfs->z_log; 1441 zfs_dirlock_t *dl; 1442 uint64_t zoid = 0; 1443 dmu_tx_t *tx; 1444 int error; 1445 1446 ASSERT(vap->va_type == VDIR); 1447 1448 ZFS_ENTER(zfsvfs); 1449 1450 if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1451 ZFS_EXIT(zfsvfs); 1452 return (EINVAL); 1453 } 1454 top: 1455 *vpp = NULL; 1456 1457 /* 1458 * First make sure the new directory doesn't exist. 1459 */ 1460 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1461 ZFS_EXIT(zfsvfs); 1462 return (error); 1463 } 1464 1465 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 1466 zfs_dirent_unlock(dl); 1467 ZFS_EXIT(zfsvfs); 1468 return (error); 1469 } 1470 1471 /* 1472 * Add a new entry to the directory. 1473 */ 1474 tx = dmu_tx_create(zfsvfs->z_os); 1475 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 1476 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1477 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1478 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1479 0, SPA_MAXBLOCKSIZE); 1480 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1481 if (error) { 1482 zfs_dirent_unlock(dl); 1483 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1484 dmu_tx_wait(tx); 1485 dmu_tx_abort(tx); 1486 goto top; 1487 } 1488 dmu_tx_abort(tx); 1489 ZFS_EXIT(zfsvfs); 1490 return (error); 1491 } 1492 1493 /* 1494 * Create new node. 1495 */ 1496 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1497 1498 /* 1499 * Now put new name in parent dir. 1500 */ 1501 (void) zfs_link_create(dl, zp, tx, ZNEW); 1502 1503 *vpp = ZTOV(zp); 1504 1505 zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1506 dmu_tx_commit(tx); 1507 1508 zfs_dirent_unlock(dl); 1509 1510 ZFS_EXIT(zfsvfs); 1511 return (0); 1512 } 1513 1514 /* 1515 * Remove a directory subdir entry. If the current working 1516 * directory is the same as the subdir to be removed, the 1517 * remove will fail. 1518 * 1519 * IN: dvp - vnode of directory to remove from. 1520 * name - name of directory to be removed. 1521 * cwd - vnode of current working directory. 1522 * cr - credentials of caller. 1523 * 1524 * RETURN: 0 if success 1525 * error code if failure 1526 * 1527 * Timestamps: 1528 * dvp - ctime|mtime updated 1529 */ 1530 static int 1531 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1532 { 1533 znode_t *dzp = VTOZ(dvp); 1534 znode_t *zp; 1535 vnode_t *vp; 1536 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1537 zilog_t *zilog = zfsvfs->z_log; 1538 zfs_dirlock_t *dl; 1539 dmu_tx_t *tx; 1540 int error; 1541 1542 ZFS_ENTER(zfsvfs); 1543 1544 top: 1545 zp = NULL; 1546 1547 /* 1548 * Attempt to lock directory; fail if entry doesn't exist. 1549 */ 1550 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1551 ZFS_EXIT(zfsvfs); 1552 return (error); 1553 } 1554 1555 vp = ZTOV(zp); 1556 1557 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1558 goto out; 1559 } 1560 1561 if (vp->v_type != VDIR) { 1562 error = ENOTDIR; 1563 goto out; 1564 } 1565 1566 if (vp == cwd) { 1567 error = EINVAL; 1568 goto out; 1569 } 1570 1571 vnevent_rmdir(vp); 1572 1573 /* 1574 * Grab a lock on the parent pointer make sure we play well 1575 * with the treewalk and directory rename code. 1576 */ 1577 rw_enter(&zp->z_parent_lock, RW_WRITER); 1578 1579 tx = dmu_tx_create(zfsvfs->z_os); 1580 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1581 dmu_tx_hold_bonus(tx, zp->z_id); 1582 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1583 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1584 if (error) { 1585 rw_exit(&zp->z_parent_lock); 1586 zfs_dirent_unlock(dl); 1587 VN_RELE(vp); 1588 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1589 dmu_tx_wait(tx); 1590 dmu_tx_abort(tx); 1591 goto top; 1592 } 1593 dmu_tx_abort(tx); 1594 ZFS_EXIT(zfsvfs); 1595 return (error); 1596 } 1597 1598 error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1599 1600 if (error == 0) 1601 zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1602 1603 dmu_tx_commit(tx); 1604 1605 rw_exit(&zp->z_parent_lock); 1606 out: 1607 zfs_dirent_unlock(dl); 1608 1609 VN_RELE(vp); 1610 1611 ZFS_EXIT(zfsvfs); 1612 return (error); 1613 } 1614 1615 /* 1616 * Read as many directory entries as will fit into the provided 1617 * buffer from the given directory cursor position (specified in 1618 * the uio structure. 1619 * 1620 * IN: vp - vnode of directory to read. 1621 * uio - structure supplying read location, range info, 1622 * and return buffer. 1623 * cr - credentials of caller. 1624 * 1625 * OUT: uio - updated offset and range, buffer filled. 1626 * eofp - set to true if end-of-file detected. 1627 * 1628 * RETURN: 0 if success 1629 * error code if failure 1630 * 1631 * Timestamps: 1632 * vp - atime updated 1633 * 1634 * Note that the low 4 bits of the cookie returned by zap is always zero. 1635 * This allows us to use the low range for "special" directory entries: 1636 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1637 * we use the offset 2 for the '.zfs' directory. 1638 */ 1639 /* ARGSUSED */ 1640 static int 1641 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1642 { 1643 znode_t *zp = VTOZ(vp); 1644 iovec_t *iovp; 1645 dirent64_t *odp; 1646 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1647 objset_t *os; 1648 caddr_t outbuf; 1649 size_t bufsize; 1650 zap_cursor_t zc; 1651 zap_attribute_t zap; 1652 uint_t bytes_wanted; 1653 ushort_t this_reclen; 1654 uint64_t offset; /* must be unsigned; checks for < 1 */ 1655 off64_t *next; 1656 int local_eof; 1657 int outcount; 1658 int error; 1659 uint8_t prefetch; 1660 1661 ZFS_ENTER(zfsvfs); 1662 1663 /* 1664 * If we are not given an eof variable, 1665 * use a local one. 1666 */ 1667 if (eofp == NULL) 1668 eofp = &local_eof; 1669 1670 /* 1671 * Check for valid iov_len. 1672 */ 1673 if (uio->uio_iov->iov_len <= 0) { 1674 ZFS_EXIT(zfsvfs); 1675 return (EINVAL); 1676 } 1677 1678 /* 1679 * Quit if directory has been removed (posix) 1680 */ 1681 if ((*eofp = zp->z_unlinked) != 0) { 1682 ZFS_EXIT(zfsvfs); 1683 return (0); 1684 } 1685 1686 error = 0; 1687 os = zfsvfs->z_os; 1688 offset = uio->uio_loffset; 1689 prefetch = zp->z_zn_prefetch; 1690 1691 /* 1692 * Initialize the iterator cursor. 1693 */ 1694 if (offset <= 3) { 1695 /* 1696 * Start iteration from the beginning of the directory. 1697 */ 1698 zap_cursor_init(&zc, os, zp->z_id); 1699 } else { 1700 /* 1701 * The offset is a serialized cursor. 1702 */ 1703 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1704 } 1705 1706 /* 1707 * Get space to change directory entries into fs independent format. 1708 */ 1709 iovp = uio->uio_iov; 1710 bytes_wanted = iovp->iov_len; 1711 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1712 bufsize = bytes_wanted; 1713 outbuf = kmem_alloc(bufsize, KM_SLEEP); 1714 odp = (struct dirent64 *)outbuf; 1715 } else { 1716 bufsize = bytes_wanted; 1717 odp = (struct dirent64 *)iovp->iov_base; 1718 } 1719 1720 /* 1721 * Transform to file-system independent format 1722 */ 1723 outcount = 0; 1724 while (outcount < bytes_wanted) { 1725 /* 1726 * Special case `.', `..', and `.zfs'. 1727 */ 1728 if (offset == 0) { 1729 (void) strcpy(zap.za_name, "."); 1730 zap.za_first_integer = zp->z_id; 1731 this_reclen = DIRENT64_RECLEN(1); 1732 } else if (offset == 1) { 1733 (void) strcpy(zap.za_name, ".."); 1734 zap.za_first_integer = zp->z_phys->zp_parent; 1735 this_reclen = DIRENT64_RECLEN(2); 1736 } else if (offset == 2 && zfs_show_ctldir(zp)) { 1737 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1738 zap.za_first_integer = ZFSCTL_INO_ROOT; 1739 this_reclen = 1740 DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1); 1741 } else { 1742 /* 1743 * Grab next entry. 1744 */ 1745 if (error = zap_cursor_retrieve(&zc, &zap)) { 1746 if ((*eofp = (error == ENOENT)) != 0) 1747 break; 1748 else 1749 goto update; 1750 } 1751 1752 if (zap.za_integer_length != 8 || 1753 zap.za_num_integers != 1) { 1754 cmn_err(CE_WARN, "zap_readdir: bad directory " 1755 "entry, obj = %lld, offset = %lld\n", 1756 (u_longlong_t)zp->z_id, 1757 (u_longlong_t)offset); 1758 error = ENXIO; 1759 goto update; 1760 } 1761 this_reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1762 } 1763 1764 /* 1765 * Will this entry fit in the buffer? 1766 */ 1767 if (outcount + this_reclen > bufsize) { 1768 /* 1769 * Did we manage to fit anything in the buffer? 1770 */ 1771 if (!outcount) { 1772 error = EINVAL; 1773 goto update; 1774 } 1775 break; 1776 } 1777 /* 1778 * Add this entry: 1779 */ 1780 odp->d_ino = (ino64_t)zap.za_first_integer; 1781 odp->d_reclen = (ushort_t)this_reclen; 1782 /* NOTE: d_off is the offset for the *next* entry */ 1783 next = &(odp->d_off); 1784 (void) strncpy(odp->d_name, zap.za_name, 1785 DIRENT64_NAMELEN(this_reclen)); 1786 outcount += this_reclen; 1787 odp = (dirent64_t *)((intptr_t)odp + this_reclen); 1788 1789 ASSERT(outcount <= bufsize); 1790 1791 /* Prefetch znode */ 1792 if (prefetch) 1793 dmu_prefetch(os, zap.za_first_integer, 0, 0); 1794 1795 /* 1796 * Move to the next entry, fill in the previous offset. 1797 */ 1798 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1799 zap_cursor_advance(&zc); 1800 offset = zap_cursor_serialize(&zc); 1801 } else { 1802 offset += 1; 1803 } 1804 *next = offset; 1805 } 1806 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1807 1808 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1809 iovp->iov_base += outcount; 1810 iovp->iov_len -= outcount; 1811 uio->uio_resid -= outcount; 1812 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1813 /* 1814 * Reset the pointer. 1815 */ 1816 offset = uio->uio_loffset; 1817 } 1818 1819 update: 1820 zap_cursor_fini(&zc); 1821 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1822 kmem_free(outbuf, bufsize); 1823 1824 if (error == ENOENT) 1825 error = 0; 1826 1827 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1828 1829 uio->uio_loffset = offset; 1830 ZFS_EXIT(zfsvfs); 1831 return (error); 1832 } 1833 1834 static int 1835 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1836 { 1837 znode_t *zp = VTOZ(vp); 1838 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1839 1840 /* 1841 * Regardless of whether this is required for standards conformance, 1842 * this is the logical behavior when fsync() is called on a file with 1843 * dirty pages. We use B_ASYNC since the ZIL transactions are already 1844 * going to be pushed out as part of the zil_commit(). 1845 */ 1846 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 1847 (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 1848 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 1849 1850 ZFS_ENTER(zfsvfs); 1851 zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 1852 ZFS_EXIT(zfsvfs); 1853 return (0); 1854 } 1855 1856 /* 1857 * Get the requested file attributes and place them in the provided 1858 * vattr structure. 1859 * 1860 * IN: vp - vnode of file. 1861 * vap - va_mask identifies requested attributes. 1862 * flags - [UNUSED] 1863 * cr - credentials of caller. 1864 * 1865 * OUT: vap - attribute values. 1866 * 1867 * RETURN: 0 (always succeeds) 1868 */ 1869 /* ARGSUSED */ 1870 static int 1871 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1872 { 1873 znode_t *zp = VTOZ(vp); 1874 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1875 znode_phys_t *pzp = zp->z_phys; 1876 int error; 1877 1878 ZFS_ENTER(zfsvfs); 1879 1880 /* 1881 * Return all attributes. It's cheaper to provide the answer 1882 * than to determine whether we were asked the question. 1883 */ 1884 mutex_enter(&zp->z_lock); 1885 1886 vap->va_type = vp->v_type; 1887 vap->va_mode = pzp->zp_mode & MODEMASK; 1888 vap->va_uid = zp->z_phys->zp_uid; 1889 vap->va_gid = zp->z_phys->zp_gid; 1890 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1891 vap->va_nodeid = zp->z_id; 1892 vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX); /* nlink_t limit! */ 1893 vap->va_size = pzp->zp_size; 1894 vap->va_rdev = vp->v_rdev; 1895 vap->va_seq = zp->z_seq; 1896 1897 ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1898 ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1899 ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1900 1901 /* 1902 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1903 * Also, if we are the owner don't bother, since owner should 1904 * always be allowed to read basic attributes of file. 1905 */ 1906 if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1907 (zp->z_phys->zp_uid != crgetuid(cr))) { 1908 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1909 mutex_exit(&zp->z_lock); 1910 ZFS_EXIT(zfsvfs); 1911 return (error); 1912 } 1913 } 1914 1915 mutex_exit(&zp->z_lock); 1916 1917 dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1918 1919 if (zp->z_blksz == 0) { 1920 /* 1921 * Block size hasn't been set; suggest maximal I/O transfers. 1922 */ 1923 vap->va_blksize = zfsvfs->z_max_blksz; 1924 } 1925 1926 ZFS_EXIT(zfsvfs); 1927 return (0); 1928 } 1929 1930 /* 1931 * Set the file attributes to the values contained in the 1932 * vattr structure. 1933 * 1934 * IN: vp - vnode of file to be modified. 1935 * vap - new attribute values. 1936 * flags - ATTR_UTIME set if non-default time values provided. 1937 * cr - credentials of caller. 1938 * 1939 * RETURN: 0 if success 1940 * error code if failure 1941 * 1942 * Timestamps: 1943 * vp - ctime updated, mtime updated if size changed. 1944 */ 1945 /* ARGSUSED */ 1946 static int 1947 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1948 caller_context_t *ct) 1949 { 1950 struct znode *zp = VTOZ(vp); 1951 znode_phys_t *pzp = zp->z_phys; 1952 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1953 zilog_t *zilog = zfsvfs->z_log; 1954 dmu_tx_t *tx; 1955 vattr_t oldva; 1956 uint_t mask = vap->va_mask; 1957 uint_t saved_mask; 1958 int trim_mask = 0; 1959 uint64_t new_mode; 1960 znode_t *attrzp; 1961 int need_policy = FALSE; 1962 int err; 1963 1964 if (mask == 0) 1965 return (0); 1966 1967 if (mask & AT_NOSET) 1968 return (EINVAL); 1969 1970 if (mask & AT_SIZE && vp->v_type == VDIR) 1971 return (EISDIR); 1972 1973 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 1974 return (EINVAL); 1975 1976 ZFS_ENTER(zfsvfs); 1977 1978 top: 1979 attrzp = NULL; 1980 1981 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1982 ZFS_EXIT(zfsvfs); 1983 return (EROFS); 1984 } 1985 1986 /* 1987 * First validate permissions 1988 */ 1989 1990 if (mask & AT_SIZE) { 1991 err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1992 if (err) { 1993 ZFS_EXIT(zfsvfs); 1994 return (err); 1995 } 1996 /* 1997 * XXX - Note, we are not providing any open 1998 * mode flags here (like FNDELAY), so we may 1999 * block if there are locks present... this 2000 * should be addressed in openat(). 2001 */ 2002 do { 2003 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 2004 /* NB: we already did dmu_tx_wait() if necessary */ 2005 } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 2006 if (err) { 2007 ZFS_EXIT(zfsvfs); 2008 return (err); 2009 } 2010 } 2011 2012 if (mask & (AT_ATIME|AT_MTIME)) 2013 need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 2014 2015 if (mask & (AT_UID|AT_GID)) { 2016 int idmask = (mask & (AT_UID|AT_GID)); 2017 int take_owner; 2018 int take_group; 2019 2020 /* 2021 * NOTE: even if a new mode is being set, 2022 * we may clear S_ISUID/S_ISGID bits. 2023 */ 2024 2025 if (!(mask & AT_MODE)) 2026 vap->va_mode = pzp->zp_mode; 2027 2028 /* 2029 * Take ownership or chgrp to group we are a member of 2030 */ 2031 2032 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2033 take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 2034 2035 /* 2036 * If both AT_UID and AT_GID are set then take_owner and 2037 * take_group must both be set in order to allow taking 2038 * ownership. 2039 * 2040 * Otherwise, send the check through secpolicy_vnode_setattr() 2041 * 2042 */ 2043 2044 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2045 ((idmask == AT_UID) && take_owner) || 2046 ((idmask == AT_GID) && take_group)) { 2047 if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 2048 /* 2049 * Remove setuid/setgid for non-privileged users 2050 */ 2051 secpolicy_setid_clear(vap, cr); 2052 trim_mask = (mask & (AT_UID|AT_GID)); 2053 } else { 2054 need_policy = TRUE; 2055 } 2056 } else { 2057 need_policy = TRUE; 2058 } 2059 } 2060 2061 mutex_enter(&zp->z_lock); 2062 oldva.va_mode = pzp->zp_mode; 2063 oldva.va_uid = zp->z_phys->zp_uid; 2064 oldva.va_gid = zp->z_phys->zp_gid; 2065 mutex_exit(&zp->z_lock); 2066 2067 if (mask & AT_MODE) { 2068 if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) { 2069 err = secpolicy_setid_setsticky_clear(vp, vap, 2070 &oldva, cr); 2071 if (err) { 2072 ZFS_EXIT(zfsvfs); 2073 return (err); 2074 } 2075 trim_mask |= AT_MODE; 2076 } else { 2077 need_policy = TRUE; 2078 } 2079 } 2080 2081 if (need_policy) { 2082 /* 2083 * If trim_mask is set then take ownership 2084 * has been granted or write_acl is present and user 2085 * has the ability to modify mode. In that case remove 2086 * UID|GID and or MODE from mask so that 2087 * secpolicy_vnode_setattr() doesn't revoke it. 2088 */ 2089 2090 if (trim_mask) { 2091 saved_mask = vap->va_mask; 2092 vap->va_mask &= ~trim_mask; 2093 2094 } 2095 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2096 (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2097 if (err) { 2098 ZFS_EXIT(zfsvfs); 2099 return (err); 2100 } 2101 2102 if (trim_mask) 2103 vap->va_mask |= saved_mask; 2104 } 2105 2106 /* 2107 * secpolicy_vnode_setattr, or take ownership may have 2108 * changed va_mask 2109 */ 2110 mask = vap->va_mask; 2111 2112 tx = dmu_tx_create(zfsvfs->z_os); 2113 dmu_tx_hold_bonus(tx, zp->z_id); 2114 2115 if (mask & AT_MODE) { 2116 uint64_t pmode = pzp->zp_mode; 2117 2118 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2119 2120 if (zp->z_phys->zp_acl.z_acl_extern_obj) 2121 dmu_tx_hold_write(tx, 2122 pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2123 else 2124 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2125 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2126 } 2127 2128 if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 2129 err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 2130 if (err) { 2131 dmu_tx_abort(tx); 2132 ZFS_EXIT(zfsvfs); 2133 return (err); 2134 } 2135 dmu_tx_hold_bonus(tx, attrzp->z_id); 2136 } 2137 2138 err = dmu_tx_assign(tx, zfsvfs->z_assign); 2139 if (err) { 2140 if (attrzp) 2141 VN_RELE(ZTOV(attrzp)); 2142 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2143 dmu_tx_wait(tx); 2144 dmu_tx_abort(tx); 2145 goto top; 2146 } 2147 dmu_tx_abort(tx); 2148 ZFS_EXIT(zfsvfs); 2149 return (err); 2150 } 2151 2152 dmu_buf_will_dirty(zp->z_dbuf, tx); 2153 2154 /* 2155 * Set each attribute requested. 2156 * We group settings according to the locks they need to acquire. 2157 * 2158 * Note: you cannot set ctime directly, although it will be 2159 * updated as a side-effect of calling this function. 2160 */ 2161 2162 mutex_enter(&zp->z_lock); 2163 2164 if (mask & AT_MODE) { 2165 err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2166 ASSERT3U(err, ==, 0); 2167 } 2168 2169 if (attrzp) 2170 mutex_enter(&attrzp->z_lock); 2171 2172 if (mask & AT_UID) { 2173 zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 2174 if (attrzp) { 2175 attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 2176 } 2177 } 2178 2179 if (mask & AT_GID) { 2180 zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 2181 if (attrzp) 2182 attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 2183 } 2184 2185 if (attrzp) 2186 mutex_exit(&attrzp->z_lock); 2187 2188 if (mask & AT_ATIME) 2189 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2190 2191 if (mask & AT_MTIME) 2192 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2193 2194 if (mask & AT_SIZE) 2195 zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 2196 else if (mask != 0) 2197 zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2198 2199 if (mask != 0) 2200 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2201 2202 mutex_exit(&zp->z_lock); 2203 2204 if (attrzp) 2205 VN_RELE(ZTOV(attrzp)); 2206 2207 dmu_tx_commit(tx); 2208 2209 ZFS_EXIT(zfsvfs); 2210 return (err); 2211 } 2212 2213 typedef struct zfs_zlock { 2214 krwlock_t *zl_rwlock; /* lock we acquired */ 2215 znode_t *zl_znode; /* znode we held */ 2216 struct zfs_zlock *zl_next; /* next in list */ 2217 } zfs_zlock_t; 2218 2219 /* 2220 * Drop locks and release vnodes that were held by zfs_rename_lock(). 2221 */ 2222 static void 2223 zfs_rename_unlock(zfs_zlock_t **zlpp) 2224 { 2225 zfs_zlock_t *zl; 2226 2227 while ((zl = *zlpp) != NULL) { 2228 if (zl->zl_znode != NULL) 2229 VN_RELE(ZTOV(zl->zl_znode)); 2230 rw_exit(zl->zl_rwlock); 2231 *zlpp = zl->zl_next; 2232 kmem_free(zl, sizeof (*zl)); 2233 } 2234 } 2235 2236 /* 2237 * Search back through the directory tree, using the ".." entries. 2238 * Lock each directory in the chain to prevent concurrent renames. 2239 * Fail any attempt to move a directory into one of its own descendants. 2240 * XXX - z_parent_lock can overlap with map or grow locks 2241 */ 2242 static int 2243 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2244 { 2245 zfs_zlock_t *zl; 2246 znode_t *zp = tdzp; 2247 uint64_t rootid = zp->z_zfsvfs->z_root; 2248 uint64_t *oidp = &zp->z_id; 2249 krwlock_t *rwlp = &szp->z_parent_lock; 2250 krw_t rw = RW_WRITER; 2251 2252 /* 2253 * First pass write-locks szp and compares to zp->z_id. 2254 * Later passes read-lock zp and compare to zp->z_parent. 2255 */ 2256 do { 2257 if (!rw_tryenter(rwlp, rw)) { 2258 /* 2259 * Another thread is renaming in this path. 2260 * Note that if we are a WRITER, we don't have any 2261 * parent_locks held yet. 2262 */ 2263 if (rw == RW_READER && zp->z_id > szp->z_id) { 2264 /* 2265 * Drop our locks and restart 2266 */ 2267 zfs_rename_unlock(&zl); 2268 *zlpp = NULL; 2269 zp = tdzp; 2270 oidp = &zp->z_id; 2271 rwlp = &szp->z_parent_lock; 2272 rw = RW_WRITER; 2273 continue; 2274 } else { 2275 /* 2276 * Wait for other thread to drop its locks 2277 */ 2278 rw_enter(rwlp, rw); 2279 } 2280 } 2281 2282 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2283 zl->zl_rwlock = rwlp; 2284 zl->zl_znode = NULL; 2285 zl->zl_next = *zlpp; 2286 *zlpp = zl; 2287 2288 if (*oidp == szp->z_id) /* We're a descendant of szp */ 2289 return (EINVAL); 2290 2291 if (*oidp == rootid) /* We've hit the top */ 2292 return (0); 2293 2294 if (rw == RW_READER) { /* i.e. not the first pass */ 2295 int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2296 if (error) 2297 return (error); 2298 zl->zl_znode = zp; 2299 } 2300 oidp = &zp->z_phys->zp_parent; 2301 rwlp = &zp->z_parent_lock; 2302 rw = RW_READER; 2303 2304 } while (zp->z_id != sdzp->z_id); 2305 2306 return (0); 2307 } 2308 2309 /* 2310 * Move an entry from the provided source directory to the target 2311 * directory. Change the entry name as indicated. 2312 * 2313 * IN: sdvp - Source directory containing the "old entry". 2314 * snm - Old entry name. 2315 * tdvp - Target directory to contain the "new entry". 2316 * tnm - New entry name. 2317 * cr - credentials of caller. 2318 * 2319 * RETURN: 0 if success 2320 * error code if failure 2321 * 2322 * Timestamps: 2323 * sdvp,tdvp - ctime|mtime updated 2324 */ 2325 static int 2326 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2327 { 2328 znode_t *tdzp, *szp, *tzp; 2329 znode_t *sdzp = VTOZ(sdvp); 2330 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2331 zilog_t *zilog = zfsvfs->z_log; 2332 vnode_t *realvp; 2333 zfs_dirlock_t *sdl, *tdl; 2334 dmu_tx_t *tx; 2335 zfs_zlock_t *zl; 2336 int cmp, serr, terr, error; 2337 2338 ZFS_ENTER(zfsvfs); 2339 2340 /* 2341 * Make sure we have the real vp for the target directory. 2342 */ 2343 if (VOP_REALVP(tdvp, &realvp) == 0) 2344 tdvp = realvp; 2345 2346 if (tdvp->v_vfsp != sdvp->v_vfsp) { 2347 ZFS_EXIT(zfsvfs); 2348 return (EXDEV); 2349 } 2350 2351 tdzp = VTOZ(tdvp); 2352 top: 2353 szp = NULL; 2354 tzp = NULL; 2355 zl = NULL; 2356 2357 /* 2358 * This is to prevent the creation of links into attribute space 2359 * by renaming a linked file into/outof an attribute directory. 2360 * See the comment in zfs_link() for why this is considered bad. 2361 */ 2362 if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2363 (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2364 ZFS_EXIT(zfsvfs); 2365 return (EINVAL); 2366 } 2367 2368 /* 2369 * Lock source and target directory entries. To prevent deadlock, 2370 * a lock ordering must be defined. We lock the directory with 2371 * the smallest object id first, or if it's a tie, the one with 2372 * the lexically first name. 2373 */ 2374 if (sdzp->z_id < tdzp->z_id) { 2375 cmp = -1; 2376 } else if (sdzp->z_id > tdzp->z_id) { 2377 cmp = 1; 2378 } else { 2379 cmp = strcmp(snm, tnm); 2380 if (cmp == 0) { 2381 /* 2382 * POSIX: "If the old argument and the new argument 2383 * both refer to links to the same existing file, 2384 * the rename() function shall return successfully 2385 * and perform no other action." 2386 */ 2387 ZFS_EXIT(zfsvfs); 2388 return (0); 2389 } 2390 } 2391 if (cmp < 0) { 2392 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2393 terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2394 } else { 2395 terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2396 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2397 } 2398 2399 if (serr) { 2400 /* 2401 * Source entry invalid or not there. 2402 */ 2403 if (!terr) { 2404 zfs_dirent_unlock(tdl); 2405 if (tzp) 2406 VN_RELE(ZTOV(tzp)); 2407 } 2408 if (strcmp(snm, "..") == 0) 2409 serr = EINVAL; 2410 ZFS_EXIT(zfsvfs); 2411 return (serr); 2412 } 2413 if (terr) { 2414 zfs_dirent_unlock(sdl); 2415 VN_RELE(ZTOV(szp)); 2416 if (strcmp(tnm, "..") == 0) 2417 terr = EINVAL; 2418 ZFS_EXIT(zfsvfs); 2419 return (terr); 2420 } 2421 2422 /* 2423 * Must have write access at the source to remove the old entry 2424 * and write access at the target to create the new entry. 2425 * Note that if target and source are the same, this can be 2426 * done in a single check. 2427 */ 2428 2429 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2430 goto out; 2431 2432 if (ZTOV(szp)->v_type == VDIR) { 2433 /* 2434 * Check to make sure rename is valid. 2435 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2436 */ 2437 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2438 goto out; 2439 } 2440 2441 /* 2442 * Does target exist? 2443 */ 2444 if (tzp) { 2445 /* 2446 * Source and target must be the same type. 2447 */ 2448 if (ZTOV(szp)->v_type == VDIR) { 2449 if (ZTOV(tzp)->v_type != VDIR) { 2450 error = ENOTDIR; 2451 goto out; 2452 } 2453 } else { 2454 if (ZTOV(tzp)->v_type == VDIR) { 2455 error = EISDIR; 2456 goto out; 2457 } 2458 } 2459 /* 2460 * POSIX dictates that when the source and target 2461 * entries refer to the same file object, rename 2462 * must do nothing and exit without error. 2463 */ 2464 if (szp->z_id == tzp->z_id) { 2465 error = 0; 2466 goto out; 2467 } 2468 } 2469 2470 vnevent_rename_src(ZTOV(szp)); 2471 if (tzp) 2472 vnevent_rename_dest(ZTOV(tzp)); 2473 2474 tx = dmu_tx_create(zfsvfs->z_os); 2475 dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2476 dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 2477 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 2478 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 2479 if (sdzp != tdzp) 2480 dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 2481 if (tzp) 2482 dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 2483 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2484 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2485 if (error) { 2486 if (zl != NULL) 2487 zfs_rename_unlock(&zl); 2488 zfs_dirent_unlock(sdl); 2489 zfs_dirent_unlock(tdl); 2490 VN_RELE(ZTOV(szp)); 2491 if (tzp) 2492 VN_RELE(ZTOV(tzp)); 2493 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2494 dmu_tx_wait(tx); 2495 dmu_tx_abort(tx); 2496 goto top; 2497 } 2498 dmu_tx_abort(tx); 2499 ZFS_EXIT(zfsvfs); 2500 return (error); 2501 } 2502 2503 if (tzp) /* Attempt to remove the existing target */ 2504 error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2505 2506 if (error == 0) { 2507 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2508 if (error == 0) { 2509 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2510 ASSERT(error == 0); 2511 zfs_log_rename(zilog, tx, TX_RENAME, sdzp, 2512 sdl->dl_name, tdzp, tdl->dl_name, szp); 2513 } 2514 } 2515 2516 dmu_tx_commit(tx); 2517 out: 2518 if (zl != NULL) 2519 zfs_rename_unlock(&zl); 2520 2521 zfs_dirent_unlock(sdl); 2522 zfs_dirent_unlock(tdl); 2523 2524 VN_RELE(ZTOV(szp)); 2525 if (tzp) 2526 VN_RELE(ZTOV(tzp)); 2527 2528 ZFS_EXIT(zfsvfs); 2529 return (error); 2530 } 2531 2532 /* 2533 * Insert the indicated symbolic reference entry into the directory. 2534 * 2535 * IN: dvp - Directory to contain new symbolic link. 2536 * link - Name for new symlink entry. 2537 * vap - Attributes of new entry. 2538 * target - Target path of new symlink. 2539 * cr - credentials of caller. 2540 * 2541 * RETURN: 0 if success 2542 * error code if failure 2543 * 2544 * Timestamps: 2545 * dvp - ctime|mtime updated 2546 */ 2547 static int 2548 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2549 { 2550 znode_t *zp, *dzp = VTOZ(dvp); 2551 zfs_dirlock_t *dl; 2552 dmu_tx_t *tx; 2553 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2554 zilog_t *zilog = zfsvfs->z_log; 2555 uint64_t zoid; 2556 int len = strlen(link); 2557 int error; 2558 2559 ASSERT(vap->va_type == VLNK); 2560 2561 ZFS_ENTER(zfsvfs); 2562 top: 2563 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2564 ZFS_EXIT(zfsvfs); 2565 return (error); 2566 } 2567 2568 if (len > MAXPATHLEN) { 2569 ZFS_EXIT(zfsvfs); 2570 return (ENAMETOOLONG); 2571 } 2572 2573 /* 2574 * Attempt to lock directory; fail if entry already exists. 2575 */ 2576 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2577 ZFS_EXIT(zfsvfs); 2578 return (error); 2579 } 2580 2581 tx = dmu_tx_create(zfsvfs->z_os); 2582 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2583 dmu_tx_hold_bonus(tx, dzp->z_id); 2584 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2585 if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2586 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2587 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2588 if (error) { 2589 zfs_dirent_unlock(dl); 2590 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2591 dmu_tx_wait(tx); 2592 dmu_tx_abort(tx); 2593 goto top; 2594 } 2595 dmu_tx_abort(tx); 2596 ZFS_EXIT(zfsvfs); 2597 return (error); 2598 } 2599 2600 dmu_buf_will_dirty(dzp->z_dbuf, tx); 2601 2602 /* 2603 * Create a new object for the symlink. 2604 * Put the link content into bonus buffer if it will fit; 2605 * otherwise, store it just like any other file data. 2606 */ 2607 zoid = 0; 2608 if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2609 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2610 if (len != 0) 2611 bcopy(link, zp->z_phys + 1, len); 2612 } else { 2613 dmu_buf_t *dbp; 2614 2615 zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2616 2617 /* 2618 * Nothing can access the znode yet so no locking needed 2619 * for growing the znode's blocksize. 2620 */ 2621 zfs_grow_blocksize(zp, len, tx); 2622 2623 VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2624 dmu_buf_will_dirty(dbp, tx); 2625 2626 ASSERT3U(len, <=, dbp->db_size); 2627 bcopy(link, dbp->db_data, len); 2628 dmu_buf_rele(dbp, FTAG); 2629 } 2630 zp->z_phys->zp_size = len; 2631 2632 /* 2633 * Insert the new object into the directory. 2634 */ 2635 (void) zfs_link_create(dl, zp, tx, ZNEW); 2636 out: 2637 if (error == 0) 2638 zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link); 2639 2640 dmu_tx_commit(tx); 2641 2642 zfs_dirent_unlock(dl); 2643 2644 VN_RELE(ZTOV(zp)); 2645 2646 ZFS_EXIT(zfsvfs); 2647 return (error); 2648 } 2649 2650 /* 2651 * Return, in the buffer contained in the provided uio structure, 2652 * the symbolic path referred to by vp. 2653 * 2654 * IN: vp - vnode of symbolic link. 2655 * uoip - structure to contain the link path. 2656 * cr - credentials of caller. 2657 * 2658 * OUT: uio - structure to contain the link path. 2659 * 2660 * RETURN: 0 if success 2661 * error code if failure 2662 * 2663 * Timestamps: 2664 * vp - atime updated 2665 */ 2666 /* ARGSUSED */ 2667 static int 2668 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2669 { 2670 znode_t *zp = VTOZ(vp); 2671 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2672 size_t bufsz; 2673 int error; 2674 2675 ZFS_ENTER(zfsvfs); 2676 2677 bufsz = (size_t)zp->z_phys->zp_size; 2678 if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2679 error = uiomove(zp->z_phys + 1, 2680 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2681 } else { 2682 dmu_buf_t *dbp; 2683 error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 2684 if (error) { 2685 ZFS_EXIT(zfsvfs); 2686 return (error); 2687 } 2688 error = uiomove(dbp->db_data, 2689 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2690 dmu_buf_rele(dbp, FTAG); 2691 } 2692 2693 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2694 ZFS_EXIT(zfsvfs); 2695 return (error); 2696 } 2697 2698 /* 2699 * Insert a new entry into directory tdvp referencing svp. 2700 * 2701 * IN: tdvp - Directory to contain new entry. 2702 * svp - vnode of new entry. 2703 * name - name of new entry. 2704 * cr - credentials of caller. 2705 * 2706 * RETURN: 0 if success 2707 * error code if failure 2708 * 2709 * Timestamps: 2710 * tdvp - ctime|mtime updated 2711 * svp - ctime updated 2712 */ 2713 /* ARGSUSED */ 2714 static int 2715 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2716 { 2717 znode_t *dzp = VTOZ(tdvp); 2718 znode_t *tzp, *szp; 2719 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2720 zilog_t *zilog = zfsvfs->z_log; 2721 zfs_dirlock_t *dl; 2722 dmu_tx_t *tx; 2723 vnode_t *realvp; 2724 int error; 2725 2726 ASSERT(tdvp->v_type == VDIR); 2727 2728 ZFS_ENTER(zfsvfs); 2729 2730 if (VOP_REALVP(svp, &realvp) == 0) 2731 svp = realvp; 2732 2733 if (svp->v_vfsp != tdvp->v_vfsp) { 2734 ZFS_EXIT(zfsvfs); 2735 return (EXDEV); 2736 } 2737 2738 szp = VTOZ(svp); 2739 top: 2740 /* 2741 * We do not support links between attributes and non-attributes 2742 * because of the potential security risk of creating links 2743 * into "normal" file space in order to circumvent restrictions 2744 * imposed in attribute space. 2745 */ 2746 if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2747 (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2748 ZFS_EXIT(zfsvfs); 2749 return (EINVAL); 2750 } 2751 2752 /* 2753 * POSIX dictates that we return EPERM here. 2754 * Better choices include ENOTSUP or EISDIR. 2755 */ 2756 if (svp->v_type == VDIR) { 2757 ZFS_EXIT(zfsvfs); 2758 return (EPERM); 2759 } 2760 2761 if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2762 secpolicy_basic_link(cr) != 0) { 2763 ZFS_EXIT(zfsvfs); 2764 return (EPERM); 2765 } 2766 2767 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2768 ZFS_EXIT(zfsvfs); 2769 return (error); 2770 } 2771 2772 /* 2773 * Attempt to lock directory; fail if entry already exists. 2774 */ 2775 if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2776 ZFS_EXIT(zfsvfs); 2777 return (error); 2778 } 2779 2780 tx = dmu_tx_create(zfsvfs->z_os); 2781 dmu_tx_hold_bonus(tx, szp->z_id); 2782 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2783 error = dmu_tx_assign(tx, zfsvfs->z_assign); 2784 if (error) { 2785 zfs_dirent_unlock(dl); 2786 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2787 dmu_tx_wait(tx); 2788 dmu_tx_abort(tx); 2789 goto top; 2790 } 2791 dmu_tx_abort(tx); 2792 ZFS_EXIT(zfsvfs); 2793 return (error); 2794 } 2795 2796 error = zfs_link_create(dl, szp, tx, 0); 2797 2798 if (error == 0) 2799 zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2800 2801 dmu_tx_commit(tx); 2802 2803 zfs_dirent_unlock(dl); 2804 2805 ZFS_EXIT(zfsvfs); 2806 return (error); 2807 } 2808 2809 /* 2810 * zfs_null_putapage() is used when the file system has been force 2811 * unmounted. It just drops the pages. 2812 */ 2813 /* ARGSUSED */ 2814 static int 2815 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2816 size_t *lenp, int flags, cred_t *cr) 2817 { 2818 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2819 return (0); 2820 } 2821 2822 /* 2823 * Push a page out to disk, klustering if possible. 2824 * 2825 * IN: vp - file to push page to. 2826 * pp - page to push. 2827 * flags - additional flags. 2828 * cr - credentials of caller. 2829 * 2830 * OUT: offp - start of range pushed. 2831 * lenp - len of range pushed. 2832 * 2833 * RETURN: 0 if success 2834 * error code if failure 2835 * 2836 * NOTE: callers must have locked the page to be pushed. On 2837 * exit, the page (and all other pages in the kluster) must be 2838 * unlocked. 2839 */ 2840 /* ARGSUSED */ 2841 static int 2842 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2843 size_t *lenp, int flags, cred_t *cr) 2844 { 2845 znode_t *zp = VTOZ(vp); 2846 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2847 zilog_t *zilog = zfsvfs->z_log; 2848 dmu_tx_t *tx; 2849 rl_t *rl; 2850 u_offset_t off, koff; 2851 size_t len, klen; 2852 int err; 2853 2854 off = pp->p_offset; 2855 len = PAGESIZE; 2856 /* 2857 * If our blocksize is bigger than the page size, try to kluster 2858 * muiltiple pages so that we write a full block (thus avoiding 2859 * a read-modify-write). 2860 */ 2861 if (zp->z_blksz > PAGESIZE) { 2862 uint64_t filesz = zp->z_phys->zp_size; 2863 2864 if (!ISP2(zp->z_blksz)) { 2865 /* Only one block in the file. */ 2866 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 2867 koff = 0; 2868 } else { 2869 klen = zp->z_blksz; 2870 koff = P2ALIGN(off, (u_offset_t)klen); 2871 } 2872 ASSERT(koff <= filesz); 2873 if (koff + klen > filesz) 2874 klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 2875 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 2876 } 2877 ASSERT3U(btop(len), ==, btopr(len)); 2878 top: 2879 rl = zfs_range_lock(zp, off, len, RL_WRITER); 2880 /* 2881 * Can't push pages past end-of-file. 2882 */ 2883 if (off >= zp->z_phys->zp_size) { 2884 /* discard all pages */ 2885 flags |= B_INVAL; 2886 err = 0; 2887 goto out; 2888 } else if (off + len > zp->z_phys->zp_size) { 2889 int npages = btopr(zp->z_phys->zp_size - off); 2890 page_t *trunc; 2891 2892 page_list_break(&pp, &trunc, npages); 2893 /* discard pages past end of file */ 2894 if (trunc) 2895 pvn_write_done(trunc, B_INVAL | flags); 2896 len = zp->z_phys->zp_size - off; 2897 } 2898 2899 tx = dmu_tx_create(zfsvfs->z_os); 2900 dmu_tx_hold_write(tx, zp->z_id, off, len); 2901 dmu_tx_hold_bonus(tx, zp->z_id); 2902 err = dmu_tx_assign(tx, zfsvfs->z_assign); 2903 if (err != 0) { 2904 if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2905 zfs_range_unlock(rl); 2906 dmu_tx_wait(tx); 2907 dmu_tx_abort(tx); 2908 err = 0; 2909 goto top; 2910 } 2911 dmu_tx_abort(tx); 2912 goto out; 2913 } 2914 2915 if (zp->z_blksz <= PAGESIZE) { 2916 caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1); 2917 ASSERT3U(len, <=, PAGESIZE); 2918 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 2919 ppmapout(va); 2920 } else { 2921 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 2922 } 2923 2924 if (err == 0) { 2925 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 2926 (void) zfs_log_write( 2927 zilog, tx, TX_WRITE, zp, off, len, 0, NULL); 2928 dmu_tx_commit(tx); 2929 } 2930 2931 out: 2932 zfs_range_unlock(rl); 2933 pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags); 2934 if (offp) 2935 *offp = off; 2936 if (lenp) 2937 *lenp = len; 2938 2939 return (err); 2940 } 2941 2942 /* 2943 * Copy the portion of the file indicated from pages into the file. 2944 * The pages are stored in a page list attached to the files vnode. 2945 * 2946 * IN: vp - vnode of file to push page data to. 2947 * off - position in file to put data. 2948 * len - amount of data to write. 2949 * flags - flags to control the operation. 2950 * cr - credentials of caller. 2951 * 2952 * RETURN: 0 if success 2953 * error code if failure 2954 * 2955 * Timestamps: 2956 * vp - ctime|mtime updated 2957 */ 2958 static int 2959 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2960 { 2961 znode_t *zp = VTOZ(vp); 2962 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2963 page_t *pp; 2964 size_t io_len; 2965 u_offset_t io_off; 2966 uint64_t filesz; 2967 int error = 0; 2968 2969 ZFS_ENTER(zfsvfs); 2970 2971 ASSERT(zp->z_dbuf_held && zp->z_phys); 2972 2973 if (len == 0) { 2974 /* 2975 * Search the entire vp list for pages >= off. 2976 */ 2977 error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2978 flags, cr); 2979 goto out; 2980 } 2981 2982 filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 2983 if (off > filesz) { 2984 /* past end of file */ 2985 ZFS_EXIT(zfsvfs); 2986 return (0); 2987 } 2988 2989 len = MIN(len, filesz - off); 2990 2991 for (io_off = off; io_off < off + len; io_off += io_len) { 2992 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 2993 pp = page_lookup(vp, io_off, 2994 (flags & (B_INVAL | B_FREE)) ? 2995 SE_EXCL : SE_SHARED); 2996 } else { 2997 pp = page_lookup_nowait(vp, io_off, 2998 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2999 } 3000 3001 if (pp != NULL && pvn_getdirty(pp, flags)) { 3002 int err; 3003 3004 /* 3005 * Found a dirty page to push 3006 */ 3007 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 3008 if (err) 3009 error = err; 3010 } else { 3011 io_len = PAGESIZE; 3012 } 3013 } 3014 out: 3015 if ((flags & B_ASYNC) == 0) 3016 zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3017 ZFS_EXIT(zfsvfs); 3018 return (error); 3019 } 3020 3021 void 3022 zfs_inactive(vnode_t *vp, cred_t *cr) 3023 { 3024 znode_t *zp = VTOZ(vp); 3025 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3026 int error; 3027 3028 rw_enter(&zfsvfs->z_um_lock, RW_READER); 3029 if (zfsvfs->z_unmounted2) { 3030 ASSERT(zp->z_dbuf_held == 0); 3031 3032 if (vn_has_cached_data(vp)) { 3033 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3034 B_INVAL, cr); 3035 } 3036 3037 mutex_enter(&zp->z_lock); 3038 vp->v_count = 0; /* count arrives as 1 */ 3039 if (zp->z_dbuf == NULL) { 3040 mutex_exit(&zp->z_lock); 3041 zfs_znode_free(zp); 3042 } else { 3043 mutex_exit(&zp->z_lock); 3044 } 3045 rw_exit(&zfsvfs->z_um_lock); 3046 VFS_RELE(zfsvfs->z_vfs); 3047 return; 3048 } 3049 3050 /* 3051 * Attempt to push any data in the page cache. If this fails 3052 * we will get kicked out later in zfs_zinactive(). 3053 */ 3054 if (vn_has_cached_data(vp)) { 3055 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 3056 cr); 3057 } 3058 3059 if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3060 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3061 3062 dmu_tx_hold_bonus(tx, zp->z_id); 3063 error = dmu_tx_assign(tx, TXG_WAIT); 3064 if (error) { 3065 dmu_tx_abort(tx); 3066 } else { 3067 dmu_buf_will_dirty(zp->z_dbuf, tx); 3068 mutex_enter(&zp->z_lock); 3069 zp->z_atime_dirty = 0; 3070 mutex_exit(&zp->z_lock); 3071 dmu_tx_commit(tx); 3072 } 3073 } 3074 3075 zfs_zinactive(zp); 3076 rw_exit(&zfsvfs->z_um_lock); 3077 } 3078 3079 /* 3080 * Bounds-check the seek operation. 3081 * 3082 * IN: vp - vnode seeking within 3083 * ooff - old file offset 3084 * noffp - pointer to new file offset 3085 * 3086 * RETURN: 0 if success 3087 * EINVAL if new offset invalid 3088 */ 3089 /* ARGSUSED */ 3090 static int 3091 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 3092 { 3093 if (vp->v_type == VDIR) 3094 return (0); 3095 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3096 } 3097 3098 /* 3099 * Pre-filter the generic locking function to trap attempts to place 3100 * a mandatory lock on a memory mapped file. 3101 */ 3102 static int 3103 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 3104 flk_callback_t *flk_cbp, cred_t *cr) 3105 { 3106 znode_t *zp = VTOZ(vp); 3107 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3108 int error; 3109 3110 ZFS_ENTER(zfsvfs); 3111 3112 /* 3113 * We are following the UFS semantics with respect to mapcnt 3114 * here: If we see that the file is mapped already, then we will 3115 * return an error, but we don't worry about races between this 3116 * function and zfs_map(). 3117 */ 3118 if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3119 ZFS_EXIT(zfsvfs); 3120 return (EAGAIN); 3121 } 3122 error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 3123 ZFS_EXIT(zfsvfs); 3124 return (error); 3125 } 3126 3127 /* 3128 * If we can't find a page in the cache, we will create a new page 3129 * and fill it with file data. For efficiency, we may try to fill 3130 * multiple pages at once (klustering). 3131 */ 3132 static int 3133 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3134 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3135 { 3136 znode_t *zp = VTOZ(vp); 3137 page_t *pp, *cur_pp; 3138 objset_t *os = zp->z_zfsvfs->z_os; 3139 caddr_t va; 3140 u_offset_t io_off, total; 3141 uint64_t oid = zp->z_id; 3142 size_t io_len; 3143 uint64_t filesz; 3144 int err; 3145 3146 /* 3147 * If we are only asking for a single page don't bother klustering. 3148 */ 3149 filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 3150 if (off >= filesz) 3151 return (EFAULT); 3152 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3153 io_off = off; 3154 io_len = PAGESIZE; 3155 pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3156 } else { 3157 /* 3158 * Try to fill a kluster of pages (a blocks worth). 3159 */ 3160 size_t klen; 3161 u_offset_t koff; 3162 3163 if (!ISP2(zp->z_blksz)) { 3164 /* Only one block in the file. */ 3165 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3166 koff = 0; 3167 } else { 3168 /* 3169 * It would be ideal to align our offset to the 3170 * blocksize but doing so has resulted in some 3171 * strange application crashes. For now, we 3172 * leave the offset as is and only adjust the 3173 * length if we are off the end of the file. 3174 */ 3175 koff = off; 3176 klen = plsz; 3177 } 3178 ASSERT(koff <= filesz); 3179 if (koff + klen > filesz) 3180 klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 3181 ASSERT3U(off, >=, koff); 3182 ASSERT3U(off, <, koff + klen); 3183 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 3184 &io_len, koff, klen, 0); 3185 } 3186 if (pp == NULL) { 3187 /* 3188 * Some other thread entered the page before us. 3189 * Return to zfs_getpage to retry the lookup. 3190 */ 3191 *pl = NULL; 3192 return (0); 3193 } 3194 3195 /* 3196 * Fill the pages in the kluster. 3197 */ 3198 cur_pp = pp; 3199 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 3200 ASSERT3U(io_off, ==, cur_pp->p_offset); 3201 va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 3202 err = dmu_read(os, oid, io_off, PAGESIZE, va); 3203 ppmapout(va); 3204 if (err) { 3205 /* On error, toss the entire kluster */ 3206 pvn_read_done(pp, B_ERROR); 3207 return (err); 3208 } 3209 cur_pp = cur_pp->p_next; 3210 } 3211 out: 3212 /* 3213 * Fill in the page list array from the kluster. If 3214 * there are too many pages in the kluster, return 3215 * as many pages as possible starting from the desired 3216 * offset `off'. 3217 * NOTE: the page list will always be null terminated. 3218 */ 3219 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3220 3221 return (0); 3222 } 3223 3224 /* 3225 * Return pointers to the pages for the file region [off, off + len] 3226 * in the pl array. If plsz is greater than len, this function may 3227 * also return page pointers from before or after the specified 3228 * region (i.e. some region [off', off' + plsz]). These additional 3229 * pages are only returned if they are already in the cache, or were 3230 * created as part of a klustered read. 3231 * 3232 * IN: vp - vnode of file to get data from. 3233 * off - position in file to get data from. 3234 * len - amount of data to retrieve. 3235 * plsz - length of provided page list. 3236 * seg - segment to obtain pages for. 3237 * addr - virtual address of fault. 3238 * rw - mode of created pages. 3239 * cr - credentials of caller. 3240 * 3241 * OUT: protp - protection mode of created pages. 3242 * pl - list of pages created. 3243 * 3244 * RETURN: 0 if success 3245 * error code if failure 3246 * 3247 * Timestamps: 3248 * vp - atime updated 3249 */ 3250 /* ARGSUSED */ 3251 static int 3252 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3253 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3254 enum seg_rw rw, cred_t *cr) 3255 { 3256 znode_t *zp = VTOZ(vp); 3257 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3258 page_t *pp, **pl0 = pl; 3259 int need_unlock = 0, err = 0; 3260 offset_t orig_off; 3261 3262 ZFS_ENTER(zfsvfs); 3263 3264 if (protp) 3265 *protp = PROT_ALL; 3266 3267 ASSERT(zp->z_dbuf_held && zp->z_phys); 3268 3269 /* no faultahead (for now) */ 3270 if (pl == NULL) { 3271 ZFS_EXIT(zfsvfs); 3272 return (0); 3273 } 3274 3275 /* can't fault past EOF */ 3276 if (off >= zp->z_phys->zp_size) { 3277 ZFS_EXIT(zfsvfs); 3278 return (EFAULT); 3279 } 3280 orig_off = off; 3281 3282 /* 3283 * If we already own the lock, then we must be page faulting 3284 * in the middle of a write to this file (i.e., we are writing 3285 * to this file using data from a mapped region of the file). 3286 */ 3287 if (rw_owner(&zp->z_map_lock) != curthread) { 3288 rw_enter(&zp->z_map_lock, RW_WRITER); 3289 need_unlock = TRUE; 3290 } 3291 3292 /* 3293 * Loop through the requested range [off, off + len] looking 3294 * for pages. If we don't find a page, we will need to create 3295 * a new page and fill it with data from the file. 3296 */ 3297 while (len > 0) { 3298 if (plsz < PAGESIZE) 3299 break; 3300 if (pp = page_lookup(vp, off, SE_SHARED)) { 3301 *pl++ = pp; 3302 off += PAGESIZE; 3303 addr += PAGESIZE; 3304 len -= PAGESIZE; 3305 plsz -= PAGESIZE; 3306 } else { 3307 err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 3308 if (err) 3309 goto out; 3310 /* 3311 * klustering may have changed our region 3312 * to be block aligned. 3313 */ 3314 if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3315 int delta = off - pp->p_offset; 3316 len += delta; 3317 off -= delta; 3318 addr -= delta; 3319 } 3320 while (*pl) { 3321 pl++; 3322 off += PAGESIZE; 3323 addr += PAGESIZE; 3324 plsz -= PAGESIZE; 3325 if (len > PAGESIZE) 3326 len -= PAGESIZE; 3327 else 3328 len = 0; 3329 } 3330 } 3331 } 3332 3333 /* 3334 * Fill out the page array with any pages already in the cache. 3335 */ 3336 while (plsz > 0) { 3337 pp = page_lookup_nowait(vp, off, SE_SHARED); 3338 if (pp == NULL) 3339 break; 3340 *pl++ = pp; 3341 off += PAGESIZE; 3342 plsz -= PAGESIZE; 3343 } 3344 3345 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3346 out: 3347 /* 3348 * We can't grab the range lock for the page as reader which would 3349 * stop truncation as this leads to deadlock. So we need to recheck 3350 * the file size. 3351 */ 3352 if (orig_off >= zp->z_phys->zp_size) 3353 err = EFAULT; 3354 if (err) { 3355 /* 3356 * Release any pages we have previously locked. 3357 */ 3358 while (pl > pl0) 3359 page_unlock(*--pl); 3360 } 3361 3362 *pl = NULL; 3363 3364 if (need_unlock) 3365 rw_exit(&zp->z_map_lock); 3366 3367 ZFS_EXIT(zfsvfs); 3368 return (err); 3369 } 3370 3371 /* 3372 * Request a memory map for a section of a file. This code interacts 3373 * with common code and the VM system as follows: 3374 * 3375 * common code calls mmap(), which ends up in smmap_common() 3376 * 3377 * this calls VOP_MAP(), which takes you into (say) zfs 3378 * 3379 * zfs_map() calls as_map(), passing segvn_create() as the callback 3380 * 3381 * segvn_create() creates the new segment and calls VOP_ADDMAP() 3382 * 3383 * zfs_addmap() updates z_mapcnt 3384 */ 3385 static int 3386 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3387 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3388 { 3389 znode_t *zp = VTOZ(vp); 3390 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3391 segvn_crargs_t vn_a; 3392 int error; 3393 3394 ZFS_ENTER(zfsvfs); 3395 3396 if (vp->v_flag & VNOMAP) { 3397 ZFS_EXIT(zfsvfs); 3398 return (ENOSYS); 3399 } 3400 3401 if (off < 0 || len > MAXOFFSET_T - off) { 3402 ZFS_EXIT(zfsvfs); 3403 return (ENXIO); 3404 } 3405 3406 if (vp->v_type != VREG) { 3407 ZFS_EXIT(zfsvfs); 3408 return (ENODEV); 3409 } 3410 3411 /* 3412 * If file is locked, disallow mapping. 3413 */ 3414 if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 3415 ZFS_EXIT(zfsvfs); 3416 return (EAGAIN); 3417 } 3418 3419 as_rangelock(as); 3420 if ((flags & MAP_FIXED) == 0) { 3421 map_addr(addrp, len, off, 1, flags); 3422 if (*addrp == NULL) { 3423 as_rangeunlock(as); 3424 ZFS_EXIT(zfsvfs); 3425 return (ENOMEM); 3426 } 3427 } else { 3428 /* 3429 * User specified address - blow away any previous mappings 3430 */ 3431 (void) as_unmap(as, *addrp, len); 3432 } 3433 3434 vn_a.vp = vp; 3435 vn_a.offset = (u_offset_t)off; 3436 vn_a.type = flags & MAP_TYPE; 3437 vn_a.prot = prot; 3438 vn_a.maxprot = maxprot; 3439 vn_a.cred = cr; 3440 vn_a.amp = NULL; 3441 vn_a.flags = flags & ~MAP_TYPE; 3442 vn_a.szc = 0; 3443 vn_a.lgrp_mem_policy_flags = 0; 3444 3445 error = as_map(as, *addrp, len, segvn_create, &vn_a); 3446 3447 as_rangeunlock(as); 3448 ZFS_EXIT(zfsvfs); 3449 return (error); 3450 } 3451 3452 /* ARGSUSED */ 3453 static int 3454 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3455 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3456 { 3457 uint64_t pages = btopr(len); 3458 3459 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3460 return (0); 3461 } 3462 3463 /* 3464 * The reason we push dirty pages as part of zfs_delmap() is so that we get a 3465 * more accurate mtime for the associated file. Since we don't have a way of 3466 * detecting when the data was actually modified, we have to resort to 3467 * heuristics. If an explicit msync() is done, then we mark the mtime when the 3468 * last page is pushed. The problem occurs when the msync() call is omitted, 3469 * which by far the most common case: 3470 * 3471 * open() 3472 * mmap() 3473 * <modify memory> 3474 * munmap() 3475 * close() 3476 * <time lapse> 3477 * putpage() via fsflush 3478 * 3479 * If we wait until fsflush to come along, we can have a modification time that 3480 * is some arbitrary point in the future. In order to prevent this in the 3481 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 3482 * torn down. 3483 */ 3484 /* ARGSUSED */ 3485 static int 3486 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3487 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3488 { 3489 uint64_t pages = btopr(len); 3490 3491 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 3492 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 3493 3494 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 3495 vn_has_cached_data(vp)) 3496 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 3497 3498 return (0); 3499 } 3500 3501 /* 3502 * Free or allocate space in a file. Currently, this function only 3503 * supports the `F_FREESP' command. However, this command is somewhat 3504 * misnamed, as its functionality includes the ability to allocate as 3505 * well as free space. 3506 * 3507 * IN: vp - vnode of file to free data in. 3508 * cmd - action to take (only F_FREESP supported). 3509 * bfp - section of file to free/alloc. 3510 * flag - current file open mode flags. 3511 * offset - current file offset. 3512 * cr - credentials of caller [UNUSED]. 3513 * 3514 * RETURN: 0 if success 3515 * error code if failure 3516 * 3517 * Timestamps: 3518 * vp - ctime|mtime updated 3519 */ 3520 /* ARGSUSED */ 3521 static int 3522 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3523 offset_t offset, cred_t *cr, caller_context_t *ct) 3524 { 3525 znode_t *zp = VTOZ(vp); 3526 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3527 uint64_t off, len; 3528 int error; 3529 3530 ZFS_ENTER(zfsvfs); 3531 3532 top: 3533 if (cmd != F_FREESP) { 3534 ZFS_EXIT(zfsvfs); 3535 return (EINVAL); 3536 } 3537 3538 if (error = convoff(vp, bfp, 0, offset)) { 3539 ZFS_EXIT(zfsvfs); 3540 return (error); 3541 } 3542 3543 if (bfp->l_len < 0) { 3544 ZFS_EXIT(zfsvfs); 3545 return (EINVAL); 3546 } 3547 3548 off = bfp->l_start; 3549 len = bfp->l_len; /* 0 means from off to end of file */ 3550 3551 do { 3552 error = zfs_freesp(zp, off, len, flag, TRUE); 3553 /* NB: we already did dmu_tx_wait() if necessary */ 3554 } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3555 3556 ZFS_EXIT(zfsvfs); 3557 return (error); 3558 } 3559 3560 static int 3561 zfs_fid(vnode_t *vp, fid_t *fidp) 3562 { 3563 znode_t *zp = VTOZ(vp); 3564 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3565 uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3566 uint64_t object = zp->z_id; 3567 zfid_short_t *zfid; 3568 int size, i; 3569 3570 ZFS_ENTER(zfsvfs); 3571 3572 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3573 if (fidp->fid_len < size) { 3574 fidp->fid_len = size; 3575 ZFS_EXIT(zfsvfs); 3576 return (ENOSPC); 3577 } 3578 3579 zfid = (zfid_short_t *)fidp; 3580 3581 zfid->zf_len = size; 3582 3583 for (i = 0; i < sizeof (zfid->zf_object); i++) 3584 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3585 3586 /* Must have a non-zero generation number to distinguish from .zfs */ 3587 if (gen == 0) 3588 gen = 1; 3589 for (i = 0; i < sizeof (zfid->zf_gen); i++) 3590 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3591 3592 if (size == LONG_FID_LEN) { 3593 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3594 zfid_long_t *zlfid; 3595 3596 zlfid = (zfid_long_t *)fidp; 3597 3598 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3599 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3600 3601 /* XXX - this should be the generation number for the objset */ 3602 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3603 zlfid->zf_setgen[i] = 0; 3604 } 3605 3606 ZFS_EXIT(zfsvfs); 3607 return (0); 3608 } 3609 3610 static int 3611 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3612 { 3613 znode_t *zp, *xzp; 3614 zfsvfs_t *zfsvfs; 3615 zfs_dirlock_t *dl; 3616 int error; 3617 3618 switch (cmd) { 3619 case _PC_LINK_MAX: 3620 *valp = ULONG_MAX; 3621 return (0); 3622 3623 case _PC_FILESIZEBITS: 3624 *valp = 64; 3625 return (0); 3626 3627 case _PC_XATTR_EXISTS: 3628 zp = VTOZ(vp); 3629 zfsvfs = zp->z_zfsvfs; 3630 ZFS_ENTER(zfsvfs); 3631 *valp = 0; 3632 error = zfs_dirent_lock(&dl, zp, "", &xzp, 3633 ZXATTR | ZEXISTS | ZSHARED); 3634 if (error == 0) { 3635 zfs_dirent_unlock(dl); 3636 if (!zfs_dirempty(xzp)) 3637 *valp = 1; 3638 VN_RELE(ZTOV(xzp)); 3639 } else if (error == ENOENT) { 3640 /* 3641 * If there aren't extended attributes, it's the 3642 * same as having zero of them. 3643 */ 3644 error = 0; 3645 } 3646 ZFS_EXIT(zfsvfs); 3647 return (error); 3648 3649 case _PC_ACL_ENABLED: 3650 *valp = _ACL_ACE_ENABLED; 3651 return (0); 3652 3653 case _PC_MIN_HOLE_SIZE: 3654 *valp = (ulong_t)SPA_MINBLOCKSIZE; 3655 return (0); 3656 3657 default: 3658 return (fs_pathconf(vp, cmd, valp, cr)); 3659 } 3660 } 3661 3662 /*ARGSUSED*/ 3663 static int 3664 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3665 { 3666 znode_t *zp = VTOZ(vp); 3667 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3668 int error; 3669 3670 ZFS_ENTER(zfsvfs); 3671 error = zfs_getacl(zp, vsecp, cr); 3672 ZFS_EXIT(zfsvfs); 3673 3674 return (error); 3675 } 3676 3677 /*ARGSUSED*/ 3678 static int 3679 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3680 { 3681 znode_t *zp = VTOZ(vp); 3682 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3683 int error; 3684 3685 ZFS_ENTER(zfsvfs); 3686 error = zfs_setacl(zp, vsecp, cr); 3687 ZFS_EXIT(zfsvfs); 3688 return (error); 3689 } 3690 3691 /* 3692 * Predeclare these here so that the compiler assumes that 3693 * this is an "old style" function declaration that does 3694 * not include arguments => we won't get type mismatch errors 3695 * in the initializations that follow. 3696 */ 3697 static int zfs_inval(); 3698 static int zfs_isdir(); 3699 3700 static int 3701 zfs_inval() 3702 { 3703 return (EINVAL); 3704 } 3705 3706 static int 3707 zfs_isdir() 3708 { 3709 return (EISDIR); 3710 } 3711 /* 3712 * Directory vnode operations template 3713 */ 3714 vnodeops_t *zfs_dvnodeops; 3715 const fs_operation_def_t zfs_dvnodeops_template[] = { 3716 VOPNAME_OPEN, zfs_open, 3717 VOPNAME_CLOSE, zfs_close, 3718 VOPNAME_READ, zfs_isdir, 3719 VOPNAME_WRITE, zfs_isdir, 3720 VOPNAME_IOCTL, zfs_ioctl, 3721 VOPNAME_GETATTR, zfs_getattr, 3722 VOPNAME_SETATTR, zfs_setattr, 3723 VOPNAME_ACCESS, zfs_access, 3724 VOPNAME_LOOKUP, zfs_lookup, 3725 VOPNAME_CREATE, zfs_create, 3726 VOPNAME_REMOVE, zfs_remove, 3727 VOPNAME_LINK, zfs_link, 3728 VOPNAME_RENAME, zfs_rename, 3729 VOPNAME_MKDIR, zfs_mkdir, 3730 VOPNAME_RMDIR, zfs_rmdir, 3731 VOPNAME_READDIR, zfs_readdir, 3732 VOPNAME_SYMLINK, zfs_symlink, 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_PATHCONF, zfs_pathconf, 3738 VOPNAME_GETSECATTR, zfs_getsecattr, 3739 VOPNAME_SETSECATTR, zfs_setsecattr, 3740 NULL, NULL 3741 }; 3742 3743 /* 3744 * Regular file vnode operations template 3745 */ 3746 vnodeops_t *zfs_fvnodeops; 3747 const fs_operation_def_t zfs_fvnodeops_template[] = { 3748 VOPNAME_OPEN, zfs_open, 3749 VOPNAME_CLOSE, zfs_close, 3750 VOPNAME_READ, zfs_read, 3751 VOPNAME_WRITE, zfs_write, 3752 VOPNAME_IOCTL, zfs_ioctl, 3753 VOPNAME_GETATTR, zfs_getattr, 3754 VOPNAME_SETATTR, zfs_setattr, 3755 VOPNAME_ACCESS, zfs_access, 3756 VOPNAME_LOOKUP, zfs_lookup, 3757 VOPNAME_RENAME, zfs_rename, 3758 VOPNAME_FSYNC, zfs_fsync, 3759 VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive, 3760 VOPNAME_FID, zfs_fid, 3761 VOPNAME_SEEK, zfs_seek, 3762 VOPNAME_FRLOCK, zfs_frlock, 3763 VOPNAME_SPACE, zfs_space, 3764 VOPNAME_GETPAGE, zfs_getpage, 3765 VOPNAME_PUTPAGE, zfs_putpage, 3766 VOPNAME_MAP, (fs_generic_func_p) zfs_map, 3767 VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap, 3768 VOPNAME_DELMAP, zfs_delmap, 3769 VOPNAME_PATHCONF, zfs_pathconf, 3770 VOPNAME_GETSECATTR, zfs_getsecattr, 3771 VOPNAME_SETSECATTR, zfs_setsecattr, 3772 VOPNAME_VNEVENT, fs_vnevent_support, 3773 NULL, NULL 3774 }; 3775 3776 /* 3777 * Symbolic link vnode operations template 3778 */ 3779 vnodeops_t *zfs_symvnodeops; 3780 const fs_operation_def_t zfs_symvnodeops_template[] = { 3781 VOPNAME_GETATTR, zfs_getattr, 3782 VOPNAME_SETATTR, zfs_setattr, 3783 VOPNAME_ACCESS, zfs_access, 3784 VOPNAME_RENAME, zfs_rename, 3785 VOPNAME_READLINK, zfs_readlink, 3786 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3787 VOPNAME_FID, zfs_fid, 3788 VOPNAME_PATHCONF, zfs_pathconf, 3789 VOPNAME_VNEVENT, fs_vnevent_support, 3790 NULL, NULL 3791 }; 3792 3793 /* 3794 * Extended attribute directory vnode operations template 3795 * This template is identical to the directory vnodes 3796 * operation template except for restricted operations: 3797 * VOP_MKDIR() 3798 * VOP_SYMLINK() 3799 * Note that there are other restrictions embedded in: 3800 * zfs_create() - restrict type to VREG 3801 * zfs_link() - no links into/out of attribute space 3802 * zfs_rename() - no moves into/out of attribute space 3803 */ 3804 vnodeops_t *zfs_xdvnodeops; 3805 const fs_operation_def_t zfs_xdvnodeops_template[] = { 3806 VOPNAME_OPEN, zfs_open, 3807 VOPNAME_CLOSE, zfs_close, 3808 VOPNAME_IOCTL, zfs_ioctl, 3809 VOPNAME_GETATTR, zfs_getattr, 3810 VOPNAME_SETATTR, zfs_setattr, 3811 VOPNAME_ACCESS, zfs_access, 3812 VOPNAME_LOOKUP, zfs_lookup, 3813 VOPNAME_CREATE, zfs_create, 3814 VOPNAME_REMOVE, zfs_remove, 3815 VOPNAME_LINK, zfs_link, 3816 VOPNAME_RENAME, zfs_rename, 3817 VOPNAME_MKDIR, zfs_inval, 3818 VOPNAME_RMDIR, zfs_rmdir, 3819 VOPNAME_READDIR, zfs_readdir, 3820 VOPNAME_SYMLINK, zfs_inval, 3821 VOPNAME_FSYNC, zfs_fsync, 3822 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3823 VOPNAME_FID, zfs_fid, 3824 VOPNAME_SEEK, zfs_seek, 3825 VOPNAME_PATHCONF, zfs_pathconf, 3826 VOPNAME_GETSECATTR, zfs_getsecattr, 3827 VOPNAME_SETSECATTR, zfs_setsecattr, 3828 VOPNAME_VNEVENT, fs_vnevent_support, 3829 NULL, NULL 3830 }; 3831 3832 /* 3833 * Error vnode operations template 3834 */ 3835 vnodeops_t *zfs_evnodeops; 3836 const fs_operation_def_t zfs_evnodeops_template[] = { 3837 VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3838 VOPNAME_PATHCONF, zfs_pathconf, 3839 NULL, NULL 3840 }; 3841