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