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