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