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