zfs_vnops.c revision 288571
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) 2012, 2014 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/vm.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/atomic.h> 45#include <sys/namei.h> 46#include <sys/mman.h> 47#include <sys/cmn_err.h> 48#include <sys/errno.h> 49#include <sys/unistd.h> 50#include <sys/zfs_dir.h> 51#include <sys/zfs_ioctl.h> 52#include <sys/fs/zfs.h> 53#include <sys/dmu.h> 54#include <sys/dmu_objset.h> 55#include <sys/spa.h> 56#include <sys/txg.h> 57#include <sys/dbuf.h> 58#include <sys/zap.h> 59#include <sys/sa.h> 60#include <sys/dirent.h> 61#include <sys/policy.h> 62#include <sys/sunddi.h> 63#include <sys/filio.h> 64#include <sys/sid.h> 65#include <sys/zfs_ctldir.h> 66#include <sys/zfs_fuid.h> 67#include <sys/zfs_sa.h> 68#include <sys/dnlc.h> 69#include <sys/zfs_rlock.h> 70#include <sys/extdirent.h> 71#include <sys/kidmap.h> 72#include <sys/bio.h> 73#include <sys/buf.h> 74#include <sys/sched.h> 75#include <sys/acl.h> 76#include <vm/vm_param.h> 77#include <vm/vm_pageout.h> 78 79/* 80 * Programming rules. 81 * 82 * Each vnode op performs some logical unit of work. To do this, the ZPL must 83 * properly lock its in-core state, create a DMU transaction, do the work, 84 * record this work in the intent log (ZIL), commit the DMU transaction, 85 * and wait for the intent log to commit if it is a synchronous operation. 86 * Moreover, the vnode ops must work in both normal and log replay context. 87 * The ordering of events is important to avoid deadlocks and references 88 * to freed memory. The example below illustrates the following Big Rules: 89 * 90 * (1) A check must be made in each zfs thread for a mounted file system. 91 * This is done avoiding races using ZFS_ENTER(zfsvfs). 92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 94 * can return EIO from the calling function. 95 * 96 * (2) VN_RELE() should always be the last thing except for zil_commit() 97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 98 * First, if it's the last reference, the vnode/znode 99 * can be freed, so the zp may point to freed memory. Second, the last 100 * reference will call zfs_zinactive(), which may induce a lot of work -- 101 * pushing cached pages (which acquires range locks) and syncing out 102 * cached atime changes. Third, zfs_zinactive() may require a new tx, 103 * which could deadlock the system if you were already holding one. 104 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC(). 105 * 106 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 107 * as they can span dmu_tx_assign() calls. 108 * 109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to 110 * dmu_tx_assign(). This is critical because we don't want to block 111 * while holding locks. 112 * 113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This 114 * reduces lock contention and CPU usage when we must wait (note that if 115 * throughput is constrained by the storage, nearly every transaction 116 * must wait). 117 * 118 * Note, in particular, that if a lock is sometimes acquired before 119 * the tx assigns, and sometimes after (e.g. z_lock), then failing 120 * to use a non-blocking assign can deadlock the system. The scenario: 121 * 122 * Thread A has grabbed a lock before calling dmu_tx_assign(). 123 * Thread B is in an already-assigned tx, and blocks for this lock. 124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 125 * forever, because the previous txg can't quiesce until B's tx commits. 126 * 127 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent 129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT, 130 * to indicate that this operation has already called dmu_tx_wait(). 131 * This will ensure that we don't retry forever, waiting a short bit 132 * each time. 133 * 134 * (5) If the operation succeeded, generate the intent log entry for it 135 * before dropping locks. This ensures that the ordering of events 136 * in the intent log matches the order in which they actually occurred. 137 * During ZIL replay the zfs_log_* functions will update the sequence 138 * number to indicate the zil transaction has replayed. 139 * 140 * (6) At the end of each vnode op, the DMU tx must always commit, 141 * regardless of whether there were any errors. 142 * 143 * (7) After dropping all locks, invoke zil_commit(zilog, foid) 144 * to ensure that synchronous semantics are provided when necessary. 145 * 146 * In general, this is how things should be ordered in each vnode op: 147 * 148 * ZFS_ENTER(zfsvfs); // exit if unmounted 149 * top: 150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 151 * rw_enter(...); // grab any other locks you need 152 * tx = dmu_tx_create(...); // get DMU tx 153 * dmu_tx_hold_*(); // hold each object you might modify 154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 155 * if (error) { 156 * rw_exit(...); // drop locks 157 * zfs_dirent_unlock(dl); // unlock directory entry 158 * VN_RELE(...); // release held vnodes 159 * if (error == ERESTART) { 160 * waited = B_TRUE; 161 * dmu_tx_wait(tx); 162 * dmu_tx_abort(tx); 163 * goto top; 164 * } 165 * dmu_tx_abort(tx); // abort DMU tx 166 * ZFS_EXIT(zfsvfs); // finished in zfs 167 * return (error); // really out of space 168 * } 169 * error = do_real_work(); // do whatever this VOP does 170 * if (error == 0) 171 * zfs_log_*(...); // on success, make ZIL entry 172 * dmu_tx_commit(tx); // commit DMU tx -- error or not 173 * rw_exit(...); // drop locks 174 * zfs_dirent_unlock(dl); // unlock directory entry 175 * VN_RELE(...); // release held vnodes 176 * zil_commit(zilog, foid); // synchronous when necessary 177 * ZFS_EXIT(zfsvfs); // finished in zfs 178 * return (error); // done, report error 179 */ 180 181/* ARGSUSED */ 182static int 183zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 184{ 185 znode_t *zp = VTOZ(*vpp); 186 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 187 188 ZFS_ENTER(zfsvfs); 189 ZFS_VERIFY_ZP(zp); 190 191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) && 192 ((flag & FAPPEND) == 0)) { 193 ZFS_EXIT(zfsvfs); 194 return (SET_ERROR(EPERM)); 195 } 196 197 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 198 ZTOV(zp)->v_type == VREG && 199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) { 200 if (fs_vscan(*vpp, cr, 0) != 0) { 201 ZFS_EXIT(zfsvfs); 202 return (SET_ERROR(EACCES)); 203 } 204 } 205 206 /* Keep a count of the synchronous opens in the znode */ 207 if (flag & (FSYNC | FDSYNC)) 208 atomic_inc_32(&zp->z_sync_cnt); 209 210 ZFS_EXIT(zfsvfs); 211 return (0); 212} 213 214/* ARGSUSED */ 215static int 216zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 217 caller_context_t *ct) 218{ 219 znode_t *zp = VTOZ(vp); 220 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 221 222 /* 223 * Clean up any locks held by this process on the vp. 224 */ 225 cleanlocks(vp, ddi_get_pid(), 0); 226 cleanshares(vp, ddi_get_pid()); 227 228 ZFS_ENTER(zfsvfs); 229 ZFS_VERIFY_ZP(zp); 230 231 /* Decrement the synchronous opens in the znode */ 232 if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 233 atomic_dec_32(&zp->z_sync_cnt); 234 235 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 236 ZTOV(zp)->v_type == VREG && 237 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) 238 VERIFY(fs_vscan(vp, cr, 1) == 0); 239 240 ZFS_EXIT(zfsvfs); 241 return (0); 242} 243 244/* 245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 247 */ 248static int 249zfs_holey(vnode_t *vp, u_long cmd, offset_t *off) 250{ 251 znode_t *zp = VTOZ(vp); 252 uint64_t noff = (uint64_t)*off; /* new offset */ 253 uint64_t file_sz; 254 int error; 255 boolean_t hole; 256 257 file_sz = zp->z_size; 258 if (noff >= file_sz) { 259 return (SET_ERROR(ENXIO)); 260 } 261 262 if (cmd == _FIO_SEEK_HOLE) 263 hole = B_TRUE; 264 else 265 hole = B_FALSE; 266 267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 268 269 if (error == ESRCH) 270 return (SET_ERROR(ENXIO)); 271 272 /* 273 * We could find a hole that begins after the logical end-of-file, 274 * because dmu_offset_next() only works on whole blocks. If the 275 * EOF falls mid-block, then indicate that the "virtual hole" 276 * at the end of the file begins at the logical EOF, rather than 277 * at the end of the last block. 278 */ 279 if (noff > file_sz) { 280 ASSERT(hole); 281 noff = file_sz; 282 } 283 284 if (noff < *off) 285 return (error); 286 *off = noff; 287 return (error); 288} 289 290/* ARGSUSED */ 291static int 292zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred, 293 int *rvalp, caller_context_t *ct) 294{ 295 offset_t off; 296 int error; 297 zfsvfs_t *zfsvfs; 298 znode_t *zp; 299 300 switch (com) { 301 case _FIOFFS: 302 return (0); 303 304 /* 305 * The following two ioctls are used by bfu. Faking out, 306 * necessary to avoid bfu errors. 307 */ 308 case _FIOGDIO: 309 case _FIOSDIO: 310 return (0); 311 312 case _FIO_SEEK_DATA: 313 case _FIO_SEEK_HOLE: 314#ifdef sun 315 if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 316 return (SET_ERROR(EFAULT)); 317#else 318 off = *(offset_t *)data; 319#endif 320 zp = VTOZ(vp); 321 zfsvfs = zp->z_zfsvfs; 322 ZFS_ENTER(zfsvfs); 323 ZFS_VERIFY_ZP(zp); 324 325 /* offset parameter is in/out */ 326 error = zfs_holey(vp, com, &off); 327 ZFS_EXIT(zfsvfs); 328 if (error) 329 return (error); 330#ifdef sun 331 if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 332 return (SET_ERROR(EFAULT)); 333#else 334 *(offset_t *)data = off; 335#endif 336 return (0); 337 } 338 return (SET_ERROR(ENOTTY)); 339} 340 341static vm_page_t 342page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes) 343{ 344 vm_object_t obj; 345 vm_page_t pp; 346 int64_t end; 347 348 /* 349 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE 350 * aligned boundaries, if the range is not aligned. As a result a 351 * DEV_BSIZE subrange with partially dirty data may get marked as clean. 352 * It may happen that all DEV_BSIZE subranges are marked clean and thus 353 * the whole page would be considred clean despite have some dirty data. 354 * For this reason we should shrink the range to DEV_BSIZE aligned 355 * boundaries before calling vm_page_clear_dirty. 356 */ 357 end = rounddown2(off + nbytes, DEV_BSIZE); 358 off = roundup2(off, DEV_BSIZE); 359 nbytes = end - off; 360 361 obj = vp->v_object; 362 zfs_vmobject_assert_wlocked(obj); 363 364 for (;;) { 365 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL && 366 pp->valid) { 367 if (vm_page_xbusied(pp)) { 368 /* 369 * Reference the page before unlocking and 370 * sleeping so that the page daemon is less 371 * likely to reclaim it. 372 */ 373 vm_page_reference(pp); 374 vm_page_lock(pp); 375 zfs_vmobject_wunlock(obj); 376 vm_page_busy_sleep(pp, "zfsmwb"); 377 zfs_vmobject_wlock(obj); 378 continue; 379 } 380 vm_page_sbusy(pp); 381 } else if (pp == NULL) { 382 pp = vm_page_alloc(obj, OFF_TO_IDX(start), 383 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED | 384 VM_ALLOC_SBUSY); 385 } else { 386 ASSERT(pp != NULL && !pp->valid); 387 pp = NULL; 388 } 389 390 if (pp != NULL) { 391 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 392 vm_object_pip_add(obj, 1); 393 pmap_remove_write(pp); 394 if (nbytes != 0) 395 vm_page_clear_dirty(pp, off, nbytes); 396 } 397 break; 398 } 399 return (pp); 400} 401 402static void 403page_unbusy(vm_page_t pp) 404{ 405 406 vm_page_sunbusy(pp); 407 vm_object_pip_subtract(pp->object, 1); 408} 409 410static vm_page_t 411page_hold(vnode_t *vp, int64_t start) 412{ 413 vm_object_t obj; 414 vm_page_t pp; 415 416 obj = vp->v_object; 417 zfs_vmobject_assert_wlocked(obj); 418 419 for (;;) { 420 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL && 421 pp->valid) { 422 if (vm_page_xbusied(pp)) { 423 /* 424 * Reference the page before unlocking and 425 * sleeping so that the page daemon is less 426 * likely to reclaim it. 427 */ 428 vm_page_reference(pp); 429 vm_page_lock(pp); 430 zfs_vmobject_wunlock(obj); 431 vm_page_busy_sleep(pp, "zfsmwb"); 432 zfs_vmobject_wlock(obj); 433 continue; 434 } 435 436 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 437 vm_page_lock(pp); 438 vm_page_hold(pp); 439 vm_page_unlock(pp); 440 441 } else 442 pp = NULL; 443 break; 444 } 445 return (pp); 446} 447 448static void 449page_unhold(vm_page_t pp) 450{ 451 452 vm_page_lock(pp); 453 vm_page_unhold(pp); 454 vm_page_unlock(pp); 455} 456 457/* 458 * When a file is memory mapped, we must keep the IO data synchronized 459 * between the DMU cache and the memory mapped pages. What this means: 460 * 461 * On Write: If we find a memory mapped page, we write to *both* 462 * the page and the dmu buffer. 463 */ 464static void 465update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid, 466 int segflg, dmu_tx_t *tx) 467{ 468 vm_object_t obj; 469 struct sf_buf *sf; 470 caddr_t va; 471 int off; 472 473 ASSERT(segflg != UIO_NOCOPY); 474 ASSERT(vp->v_mount != NULL); 475 obj = vp->v_object; 476 ASSERT(obj != NULL); 477 478 off = start & PAGEOFFSET; 479 zfs_vmobject_wlock(obj); 480 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 481 vm_page_t pp; 482 int nbytes = imin(PAGESIZE - off, len); 483 484 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) { 485 zfs_vmobject_wunlock(obj); 486 487 va = zfs_map_page(pp, &sf); 488 (void) dmu_read(os, oid, start+off, nbytes, 489 va+off, DMU_READ_PREFETCH);; 490 zfs_unmap_page(sf); 491 492 zfs_vmobject_wlock(obj); 493 page_unbusy(pp); 494 } 495 len -= nbytes; 496 off = 0; 497 } 498 vm_object_pip_wakeupn(obj, 0); 499 zfs_vmobject_wunlock(obj); 500} 501 502/* 503 * Read with UIO_NOCOPY flag means that sendfile(2) requests 504 * ZFS to populate a range of page cache pages with data. 505 * 506 * NOTE: this function could be optimized to pre-allocate 507 * all pages in advance, drain exclusive busy on all of them, 508 * map them into contiguous KVA region and populate them 509 * in one single dmu_read() call. 510 */ 511static int 512mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio) 513{ 514 znode_t *zp = VTOZ(vp); 515 objset_t *os = zp->z_zfsvfs->z_os; 516 struct sf_buf *sf; 517 vm_object_t obj; 518 vm_page_t pp; 519 int64_t start; 520 caddr_t va; 521 int len = nbytes; 522 int off; 523 int error = 0; 524 525 ASSERT(uio->uio_segflg == UIO_NOCOPY); 526 ASSERT(vp->v_mount != NULL); 527 obj = vp->v_object; 528 ASSERT(obj != NULL); 529 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0); 530 531 zfs_vmobject_wlock(obj); 532 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) { 533 int bytes = MIN(PAGESIZE, len); 534 535 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY | 536 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY); 537 if (pp->valid == 0) { 538 zfs_vmobject_wunlock(obj); 539 va = zfs_map_page(pp, &sf); 540 error = dmu_read(os, zp->z_id, start, bytes, va, 541 DMU_READ_PREFETCH); 542 if (bytes != PAGESIZE && error == 0) 543 bzero(va + bytes, PAGESIZE - bytes); 544 zfs_unmap_page(sf); 545 zfs_vmobject_wlock(obj); 546 vm_page_sunbusy(pp); 547 vm_page_lock(pp); 548 if (error) { 549 if (pp->wire_count == 0 && pp->valid == 0 && 550 !vm_page_busied(pp)) 551 vm_page_free(pp); 552 } else { 553 pp->valid = VM_PAGE_BITS_ALL; 554 vm_page_activate(pp); 555 } 556 vm_page_unlock(pp); 557 } else { 558 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 559 vm_page_sunbusy(pp); 560 } 561 if (error) 562 break; 563 uio->uio_resid -= bytes; 564 uio->uio_offset += bytes; 565 len -= bytes; 566 } 567 zfs_vmobject_wunlock(obj); 568 return (error); 569} 570 571/* 572 * When a file is memory mapped, we must keep the IO data synchronized 573 * between the DMU cache and the memory mapped pages. What this means: 574 * 575 * On Read: We "read" preferentially from memory mapped pages, 576 * else we default from the dmu buffer. 577 * 578 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 579 * the file is memory mapped. 580 */ 581static int 582mappedread(vnode_t *vp, int nbytes, uio_t *uio) 583{ 584 znode_t *zp = VTOZ(vp); 585 vm_object_t obj; 586 int64_t start; 587 caddr_t va; 588 int len = nbytes; 589 int off; 590 int error = 0; 591 592 ASSERT(vp->v_mount != NULL); 593 obj = vp->v_object; 594 ASSERT(obj != NULL); 595 596 start = uio->uio_loffset; 597 off = start & PAGEOFFSET; 598 zfs_vmobject_wlock(obj); 599 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 600 vm_page_t pp; 601 uint64_t bytes = MIN(PAGESIZE - off, len); 602 603 if (pp = page_hold(vp, start)) { 604 struct sf_buf *sf; 605 caddr_t va; 606 607 zfs_vmobject_wunlock(obj); 608 va = zfs_map_page(pp, &sf); 609 error = uiomove(va + off, bytes, UIO_READ, uio); 610 zfs_unmap_page(sf); 611 zfs_vmobject_wlock(obj); 612 page_unhold(pp); 613 } else { 614 zfs_vmobject_wunlock(obj); 615 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 616 uio, bytes); 617 zfs_vmobject_wlock(obj); 618 } 619 len -= bytes; 620 off = 0; 621 if (error) 622 break; 623 } 624 zfs_vmobject_wunlock(obj); 625 return (error); 626} 627 628offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 629 630/* 631 * Read bytes from specified file into supplied buffer. 632 * 633 * IN: vp - vnode of file to be read from. 634 * uio - structure supplying read location, range info, 635 * and return buffer. 636 * ioflag - SYNC flags; used to provide FRSYNC semantics. 637 * cr - credentials of caller. 638 * ct - caller context 639 * 640 * OUT: uio - updated offset and range, buffer filled. 641 * 642 * RETURN: 0 on success, error code on failure. 643 * 644 * Side Effects: 645 * vp - atime updated if byte count > 0 646 */ 647/* ARGSUSED */ 648static int 649zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 650{ 651 znode_t *zp = VTOZ(vp); 652 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 653 ssize_t n, nbytes; 654 int error = 0; 655 rl_t *rl; 656 xuio_t *xuio = NULL; 657 658 ZFS_ENTER(zfsvfs); 659 ZFS_VERIFY_ZP(zp); 660 661 if (zp->z_pflags & ZFS_AV_QUARANTINED) { 662 ZFS_EXIT(zfsvfs); 663 return (SET_ERROR(EACCES)); 664 } 665 666 /* 667 * Validate file offset 668 */ 669 if (uio->uio_loffset < (offset_t)0) { 670 ZFS_EXIT(zfsvfs); 671 return (SET_ERROR(EINVAL)); 672 } 673 674 /* 675 * Fasttrack empty reads 676 */ 677 if (uio->uio_resid == 0) { 678 ZFS_EXIT(zfsvfs); 679 return (0); 680 } 681 682 /* 683 * Check for mandatory locks 684 */ 685 if (MANDMODE(zp->z_mode)) { 686 if (error = chklock(vp, FREAD, 687 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 688 ZFS_EXIT(zfsvfs); 689 return (error); 690 } 691 } 692 693 /* 694 * If we're in FRSYNC mode, sync out this znode before reading it. 695 */ 696 if (zfsvfs->z_log && 697 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)) 698 zil_commit(zfsvfs->z_log, zp->z_id); 699 700 /* 701 * Lock the range against changes. 702 */ 703 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 704 705 /* 706 * If we are reading past end-of-file we can skip 707 * to the end; but we might still need to set atime. 708 */ 709 if (uio->uio_loffset >= zp->z_size) { 710 error = 0; 711 goto out; 712 } 713 714 ASSERT(uio->uio_loffset < zp->z_size); 715 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset); 716 717#ifdef sun 718 if ((uio->uio_extflg == UIO_XUIO) && 719 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) { 720 int nblk; 721 int blksz = zp->z_blksz; 722 uint64_t offset = uio->uio_loffset; 723 724 xuio = (xuio_t *)uio; 725 if ((ISP2(blksz))) { 726 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset, 727 blksz)) / blksz; 728 } else { 729 ASSERT(offset + n <= blksz); 730 nblk = 1; 731 } 732 (void) dmu_xuio_init(xuio, nblk); 733 734 if (vn_has_cached_data(vp)) { 735 /* 736 * For simplicity, we always allocate a full buffer 737 * even if we only expect to read a portion of a block. 738 */ 739 while (--nblk >= 0) { 740 (void) dmu_xuio_add(xuio, 741 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 742 blksz), 0, blksz); 743 } 744 } 745 } 746#endif /* sun */ 747 748 while (n > 0) { 749 nbytes = MIN(n, zfs_read_chunk_size - 750 P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 751 752#ifdef __FreeBSD__ 753 if (uio->uio_segflg == UIO_NOCOPY) 754 error = mappedread_sf(vp, nbytes, uio); 755 else 756#endif /* __FreeBSD__ */ 757 if (vn_has_cached_data(vp)) { 758 error = mappedread(vp, nbytes, uio); 759 } else { 760 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 761 uio, nbytes); 762 } 763 if (error) { 764 /* convert checksum errors into IO errors */ 765 if (error == ECKSUM) 766 error = SET_ERROR(EIO); 767 break; 768 } 769 770 n -= nbytes; 771 } 772out: 773 zfs_range_unlock(rl); 774 775 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 776 ZFS_EXIT(zfsvfs); 777 return (error); 778} 779 780/* 781 * Write the bytes to a file. 782 * 783 * IN: vp - vnode of file to be written to. 784 * uio - structure supplying write location, range info, 785 * and data buffer. 786 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is 787 * set if in append mode. 788 * cr - credentials of caller. 789 * ct - caller context (NFS/CIFS fem monitor only) 790 * 791 * OUT: uio - updated offset and range. 792 * 793 * RETURN: 0 on success, error code on failure. 794 * 795 * Timestamps: 796 * vp - ctime|mtime updated if byte count > 0 797 */ 798 799/* ARGSUSED */ 800static int 801zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 802{ 803 znode_t *zp = VTOZ(vp); 804 rlim64_t limit = MAXOFFSET_T; 805 ssize_t start_resid = uio->uio_resid; 806 ssize_t tx_bytes; 807 uint64_t end_size; 808 dmu_tx_t *tx; 809 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 810 zilog_t *zilog; 811 offset_t woff; 812 ssize_t n, nbytes; 813 rl_t *rl; 814 int max_blksz = zfsvfs->z_max_blksz; 815 int error = 0; 816 arc_buf_t *abuf; 817 iovec_t *aiov = NULL; 818 xuio_t *xuio = NULL; 819 int i_iov = 0; 820 int iovcnt = uio->uio_iovcnt; 821 iovec_t *iovp = uio->uio_iov; 822 int write_eof; 823 int count = 0; 824 sa_bulk_attr_t bulk[4]; 825 uint64_t mtime[2], ctime[2]; 826 827 /* 828 * Fasttrack empty write 829 */ 830 n = start_resid; 831 if (n == 0) 832 return (0); 833 834 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 835 limit = MAXOFFSET_T; 836 837 ZFS_ENTER(zfsvfs); 838 ZFS_VERIFY_ZP(zp); 839 840 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 841 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 842 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 843 &zp->z_size, 8); 844 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 845 &zp->z_pflags, 8); 846 847 /* 848 * If immutable or not appending then return EPERM 849 */ 850 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 851 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 852 (uio->uio_loffset < zp->z_size))) { 853 ZFS_EXIT(zfsvfs); 854 return (SET_ERROR(EPERM)); 855 } 856 857 zilog = zfsvfs->z_log; 858 859 /* 860 * Validate file offset 861 */ 862 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset; 863 if (woff < 0) { 864 ZFS_EXIT(zfsvfs); 865 return (SET_ERROR(EINVAL)); 866 } 867 868 /* 869 * Check for mandatory locks before calling zfs_range_lock() 870 * in order to prevent a deadlock with locks set via fcntl(). 871 */ 872 if (MANDMODE((mode_t)zp->z_mode) && 873 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 874 ZFS_EXIT(zfsvfs); 875 return (error); 876 } 877 878#ifdef sun 879 /* 880 * Pre-fault the pages to ensure slow (eg NFS) pages 881 * don't hold up txg. 882 * Skip this if uio contains loaned arc_buf. 883 */ 884 if ((uio->uio_extflg == UIO_XUIO) && 885 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) 886 xuio = (xuio_t *)uio; 887 else 888 uio_prefaultpages(MIN(n, max_blksz), uio); 889#endif /* sun */ 890 891 /* 892 * If in append mode, set the io offset pointer to eof. 893 */ 894 if (ioflag & FAPPEND) { 895 /* 896 * Obtain an appending range lock to guarantee file append 897 * semantics. We reset the write offset once we have the lock. 898 */ 899 rl = zfs_range_lock(zp, 0, n, RL_APPEND); 900 woff = rl->r_off; 901 if (rl->r_len == UINT64_MAX) { 902 /* 903 * We overlocked the file because this write will cause 904 * the file block size to increase. 905 * Note that zp_size cannot change with this lock held. 906 */ 907 woff = zp->z_size; 908 } 909 uio->uio_loffset = woff; 910 } else { 911 /* 912 * Note that if the file block size will change as a result of 913 * this write, then this range lock will lock the entire file 914 * so that we can re-write the block safely. 915 */ 916 rl = zfs_range_lock(zp, woff, n, RL_WRITER); 917 } 918 919 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) { 920 zfs_range_unlock(rl); 921 ZFS_EXIT(zfsvfs); 922 return (EFBIG); 923 } 924 925 if (woff >= limit) { 926 zfs_range_unlock(rl); 927 ZFS_EXIT(zfsvfs); 928 return (SET_ERROR(EFBIG)); 929 } 930 931 if ((woff + n) > limit || woff > (limit - n)) 932 n = limit - woff; 933 934 /* Will this write extend the file length? */ 935 write_eof = (woff + n > zp->z_size); 936 937 end_size = MAX(zp->z_size, woff + n); 938 939 /* 940 * Write the file in reasonable size chunks. Each chunk is written 941 * in a separate transaction; this keeps the intent log records small 942 * and allows us to do more fine-grained space accounting. 943 */ 944 while (n > 0) { 945 abuf = NULL; 946 woff = uio->uio_loffset; 947 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 948 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 949 if (abuf != NULL) 950 dmu_return_arcbuf(abuf); 951 error = SET_ERROR(EDQUOT); 952 break; 953 } 954 955 if (xuio && abuf == NULL) { 956 ASSERT(i_iov < iovcnt); 957 aiov = &iovp[i_iov]; 958 abuf = dmu_xuio_arcbuf(xuio, i_iov); 959 dmu_xuio_clear(xuio, i_iov); 960 DTRACE_PROBE3(zfs_cp_write, int, i_iov, 961 iovec_t *, aiov, arc_buf_t *, abuf); 962 ASSERT((aiov->iov_base == abuf->b_data) || 963 ((char *)aiov->iov_base - (char *)abuf->b_data + 964 aiov->iov_len == arc_buf_size(abuf))); 965 i_iov++; 966 } else if (abuf == NULL && n >= max_blksz && 967 woff >= zp->z_size && 968 P2PHASE(woff, max_blksz) == 0 && 969 zp->z_blksz == max_blksz) { 970 /* 971 * This write covers a full block. "Borrow" a buffer 972 * from the dmu so that we can fill it before we enter 973 * a transaction. This avoids the possibility of 974 * holding up the transaction if the data copy hangs 975 * up on a pagefault (e.g., from an NFS server mapping). 976 */ 977 size_t cbytes; 978 979 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 980 max_blksz); 981 ASSERT(abuf != NULL); 982 ASSERT(arc_buf_size(abuf) == max_blksz); 983 if (error = uiocopy(abuf->b_data, max_blksz, 984 UIO_WRITE, uio, &cbytes)) { 985 dmu_return_arcbuf(abuf); 986 break; 987 } 988 ASSERT(cbytes == max_blksz); 989 } 990 991 /* 992 * Start a transaction. 993 */ 994 tx = dmu_tx_create(zfsvfs->z_os); 995 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 996 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 997 zfs_sa_upgrade_txholds(tx, zp); 998 error = dmu_tx_assign(tx, TXG_WAIT); 999 if (error) { 1000 dmu_tx_abort(tx); 1001 if (abuf != NULL) 1002 dmu_return_arcbuf(abuf); 1003 break; 1004 } 1005 1006 /* 1007 * If zfs_range_lock() over-locked we grow the blocksize 1008 * and then reduce the lock range. This will only happen 1009 * on the first iteration since zfs_range_reduce() will 1010 * shrink down r_len to the appropriate size. 1011 */ 1012 if (rl->r_len == UINT64_MAX) { 1013 uint64_t new_blksz; 1014 1015 if (zp->z_blksz > max_blksz) { 1016 /* 1017 * File's blocksize is already larger than the 1018 * "recordsize" property. Only let it grow to 1019 * the next power of 2. 1020 */ 1021 ASSERT(!ISP2(zp->z_blksz)); 1022 new_blksz = MIN(end_size, 1023 1 << highbit64(zp->z_blksz)); 1024 } else { 1025 new_blksz = MIN(end_size, max_blksz); 1026 } 1027 zfs_grow_blocksize(zp, new_blksz, tx); 1028 zfs_range_reduce(rl, woff, n); 1029 } 1030 1031 /* 1032 * XXX - should we really limit each write to z_max_blksz? 1033 * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 1034 */ 1035 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 1036 1037 if (woff + nbytes > zp->z_size) 1038 vnode_pager_setsize(vp, woff + nbytes); 1039 1040 if (abuf == NULL) { 1041 tx_bytes = uio->uio_resid; 1042 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 1043 uio, nbytes, tx); 1044 tx_bytes -= uio->uio_resid; 1045 } else { 1046 tx_bytes = nbytes; 1047 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len); 1048 /* 1049 * If this is not a full block write, but we are 1050 * extending the file past EOF and this data starts 1051 * block-aligned, use assign_arcbuf(). Otherwise, 1052 * write via dmu_write(). 1053 */ 1054 if (tx_bytes < max_blksz && (!write_eof || 1055 aiov->iov_base != abuf->b_data)) { 1056 ASSERT(xuio); 1057 dmu_write(zfsvfs->z_os, zp->z_id, woff, 1058 aiov->iov_len, aiov->iov_base, tx); 1059 dmu_return_arcbuf(abuf); 1060 xuio_stat_wbuf_copied(); 1061 } else { 1062 ASSERT(xuio || tx_bytes == max_blksz); 1063 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl), 1064 woff, abuf, tx); 1065 } 1066 ASSERT(tx_bytes <= uio->uio_resid); 1067 uioskip(uio, tx_bytes); 1068 } 1069 if (tx_bytes && vn_has_cached_data(vp)) { 1070 update_pages(vp, woff, tx_bytes, zfsvfs->z_os, 1071 zp->z_id, uio->uio_segflg, tx); 1072 } 1073 1074 /* 1075 * If we made no progress, we're done. If we made even 1076 * partial progress, update the znode and ZIL accordingly. 1077 */ 1078 if (tx_bytes == 0) { 1079 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 1080 (void *)&zp->z_size, sizeof (uint64_t), tx); 1081 dmu_tx_commit(tx); 1082 ASSERT(error != 0); 1083 break; 1084 } 1085 1086 /* 1087 * Clear Set-UID/Set-GID bits on successful write if not 1088 * privileged and at least one of the excute bits is set. 1089 * 1090 * It would be nice to to this after all writes have 1091 * been done, but that would still expose the ISUID/ISGID 1092 * to another app after the partial write is committed. 1093 * 1094 * Note: we don't call zfs_fuid_map_id() here because 1095 * user 0 is not an ephemeral uid. 1096 */ 1097 mutex_enter(&zp->z_acl_lock); 1098 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | 1099 (S_IXUSR >> 6))) != 0 && 1100 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 1101 secpolicy_vnode_setid_retain(vp, cr, 1102 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) { 1103 uint64_t newmode; 1104 zp->z_mode &= ~(S_ISUID | S_ISGID); 1105 newmode = zp->z_mode; 1106 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 1107 (void *)&newmode, sizeof (uint64_t), tx); 1108 } 1109 mutex_exit(&zp->z_acl_lock); 1110 1111 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 1112 B_TRUE); 1113 1114 /* 1115 * Update the file size (zp_size) if it has changed; 1116 * account for possible concurrent updates. 1117 */ 1118 while ((end_size = zp->z_size) < uio->uio_loffset) { 1119 (void) atomic_cas_64(&zp->z_size, end_size, 1120 uio->uio_loffset); 1121 ASSERT(error == 0); 1122 } 1123 /* 1124 * If we are replaying and eof is non zero then force 1125 * the file size to the specified eof. Note, there's no 1126 * concurrency during replay. 1127 */ 1128 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 1129 zp->z_size = zfsvfs->z_replay_eof; 1130 1131 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1132 1133 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 1134 dmu_tx_commit(tx); 1135 1136 if (error != 0) 1137 break; 1138 ASSERT(tx_bytes == nbytes); 1139 n -= nbytes; 1140 1141#ifdef sun 1142 if (!xuio && n > 0) 1143 uio_prefaultpages(MIN(n, max_blksz), uio); 1144#endif /* sun */ 1145 } 1146 1147 zfs_range_unlock(rl); 1148 1149 /* 1150 * If we're in replay mode, or we made no progress, return error. 1151 * Otherwise, it's at least a partial write, so it's successful. 1152 */ 1153 if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 1154 ZFS_EXIT(zfsvfs); 1155 return (error); 1156 } 1157 1158 if (ioflag & (FSYNC | FDSYNC) || 1159 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1160 zil_commit(zilog, zp->z_id); 1161 1162 ZFS_EXIT(zfsvfs); 1163 return (0); 1164} 1165 1166void 1167zfs_get_done(zgd_t *zgd, int error) 1168{ 1169 znode_t *zp = zgd->zgd_private; 1170 objset_t *os = zp->z_zfsvfs->z_os; 1171 1172 if (zgd->zgd_db) 1173 dmu_buf_rele(zgd->zgd_db, zgd); 1174 1175 zfs_range_unlock(zgd->zgd_rl); 1176 1177 /* 1178 * Release the vnode asynchronously as we currently have the 1179 * txg stopped from syncing. 1180 */ 1181 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 1182 1183 if (error == 0 && zgd->zgd_bp) 1184 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1185 1186 kmem_free(zgd, sizeof (zgd_t)); 1187} 1188 1189#ifdef DEBUG 1190static int zil_fault_io = 0; 1191#endif 1192 1193/* 1194 * Get data to generate a TX_WRITE intent log record. 1195 */ 1196int 1197zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1198{ 1199 zfsvfs_t *zfsvfs = arg; 1200 objset_t *os = zfsvfs->z_os; 1201 znode_t *zp; 1202 uint64_t object = lr->lr_foid; 1203 uint64_t offset = lr->lr_offset; 1204 uint64_t size = lr->lr_length; 1205 blkptr_t *bp = &lr->lr_blkptr; 1206 dmu_buf_t *db; 1207 zgd_t *zgd; 1208 int error = 0; 1209 1210 ASSERT(zio != NULL); 1211 ASSERT(size != 0); 1212 1213 /* 1214 * Nothing to do if the file has been removed 1215 */ 1216 if (zfs_zget(zfsvfs, object, &zp) != 0) 1217 return (SET_ERROR(ENOENT)); 1218 if (zp->z_unlinked) { 1219 /* 1220 * Release the vnode asynchronously as we currently have the 1221 * txg stopped from syncing. 1222 */ 1223 VN_RELE_ASYNC(ZTOV(zp), 1224 dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 1225 return (SET_ERROR(ENOENT)); 1226 } 1227 1228 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 1229 zgd->zgd_zilog = zfsvfs->z_log; 1230 zgd->zgd_private = zp; 1231 1232 /* 1233 * Write records come in two flavors: immediate and indirect. 1234 * For small writes it's cheaper to store the data with the 1235 * log record (immediate); for large writes it's cheaper to 1236 * sync the data and get a pointer to it (indirect) so that 1237 * we don't have to write the data twice. 1238 */ 1239 if (buf != NULL) { /* immediate write */ 1240 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 1241 /* test for truncation needs to be done while range locked */ 1242 if (offset >= zp->z_size) { 1243 error = SET_ERROR(ENOENT); 1244 } else { 1245 error = dmu_read(os, object, offset, size, buf, 1246 DMU_READ_NO_PREFETCH); 1247 } 1248 ASSERT(error == 0 || error == ENOENT); 1249 } else { /* indirect write */ 1250 /* 1251 * Have to lock the whole block to ensure when it's 1252 * written out and it's checksum is being calculated 1253 * that no one can change the data. We need to re-check 1254 * blocksize after we get the lock in case it's changed! 1255 */ 1256 for (;;) { 1257 uint64_t blkoff; 1258 size = zp->z_blksz; 1259 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 1260 offset -= blkoff; 1261 zgd->zgd_rl = zfs_range_lock(zp, offset, size, 1262 RL_READER); 1263 if (zp->z_blksz == size) 1264 break; 1265 offset += blkoff; 1266 zfs_range_unlock(zgd->zgd_rl); 1267 } 1268 /* test for truncation needs to be done while range locked */ 1269 if (lr->lr_offset >= zp->z_size) 1270 error = SET_ERROR(ENOENT); 1271#ifdef DEBUG 1272 if (zil_fault_io) { 1273 error = SET_ERROR(EIO); 1274 zil_fault_io = 0; 1275 } 1276#endif 1277 if (error == 0) 1278 error = dmu_buf_hold(os, object, offset, zgd, &db, 1279 DMU_READ_NO_PREFETCH); 1280 1281 if (error == 0) { 1282 blkptr_t *obp = dmu_buf_get_blkptr(db); 1283 if (obp) { 1284 ASSERT(BP_IS_HOLE(bp)); 1285 *bp = *obp; 1286 } 1287 1288 zgd->zgd_db = db; 1289 zgd->zgd_bp = bp; 1290 1291 ASSERT(db->db_offset == offset); 1292 ASSERT(db->db_size == size); 1293 1294 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1295 zfs_get_done, zgd); 1296 ASSERT(error || lr->lr_length <= zp->z_blksz); 1297 1298 /* 1299 * On success, we need to wait for the write I/O 1300 * initiated by dmu_sync() to complete before we can 1301 * release this dbuf. We will finish everything up 1302 * in the zfs_get_done() callback. 1303 */ 1304 if (error == 0) 1305 return (0); 1306 1307 if (error == EALREADY) { 1308 lr->lr_common.lrc_txtype = TX_WRITE2; 1309 error = 0; 1310 } 1311 } 1312 } 1313 1314 zfs_get_done(zgd, error); 1315 1316 return (error); 1317} 1318 1319/*ARGSUSED*/ 1320static int 1321zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 1322 caller_context_t *ct) 1323{ 1324 znode_t *zp = VTOZ(vp); 1325 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1326 int error; 1327 1328 ZFS_ENTER(zfsvfs); 1329 ZFS_VERIFY_ZP(zp); 1330 1331 if (flag & V_ACE_MASK) 1332 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 1333 else 1334 error = zfs_zaccess_rwx(zp, mode, flag, cr); 1335 1336 ZFS_EXIT(zfsvfs); 1337 return (error); 1338} 1339 1340/* 1341 * If vnode is for a device return a specfs vnode instead. 1342 */ 1343static int 1344specvp_check(vnode_t **vpp, cred_t *cr) 1345{ 1346 int error = 0; 1347 1348 if (IS_DEVVP(*vpp)) { 1349 struct vnode *svp; 1350 1351 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1352 VN_RELE(*vpp); 1353 if (svp == NULL) 1354 error = SET_ERROR(ENOSYS); 1355 *vpp = svp; 1356 } 1357 return (error); 1358} 1359 1360 1361/* 1362 * Lookup an entry in a directory, or an extended attribute directory. 1363 * If it exists, return a held vnode reference for it. 1364 * 1365 * IN: dvp - vnode of directory to search. 1366 * nm - name of entry to lookup. 1367 * pnp - full pathname to lookup [UNUSED]. 1368 * flags - LOOKUP_XATTR set if looking for an attribute. 1369 * rdir - root directory vnode [UNUSED]. 1370 * cr - credentials of caller. 1371 * ct - caller context 1372 * direntflags - directory lookup flags 1373 * realpnp - returned pathname. 1374 * 1375 * OUT: vpp - vnode of located entry, NULL if not found. 1376 * 1377 * RETURN: 0 on success, error code on failure. 1378 * 1379 * Timestamps: 1380 * NA 1381 */ 1382/* ARGSUSED */ 1383static int 1384zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp, 1385 int nameiop, cred_t *cr, kthread_t *td, int flags) 1386{ 1387 znode_t *zdp = VTOZ(dvp); 1388 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 1389 int error = 0; 1390 int *direntflags = NULL; 1391 void *realpnp = NULL; 1392 1393 /* fast path */ 1394 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 1395 1396 if (dvp->v_type != VDIR) { 1397 return (SET_ERROR(ENOTDIR)); 1398 } else if (zdp->z_sa_hdl == NULL) { 1399 return (SET_ERROR(EIO)); 1400 } 1401 1402 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 1403 error = zfs_fastaccesschk_execute(zdp, cr); 1404 if (!error) { 1405 *vpp = dvp; 1406 VN_HOLD(*vpp); 1407 return (0); 1408 } 1409 return (error); 1410 } else { 1411 vnode_t *tvp = dnlc_lookup(dvp, nm); 1412 1413 if (tvp) { 1414 error = zfs_fastaccesschk_execute(zdp, cr); 1415 if (error) { 1416 VN_RELE(tvp); 1417 return (error); 1418 } 1419 if (tvp == DNLC_NO_VNODE) { 1420 VN_RELE(tvp); 1421 return (SET_ERROR(ENOENT)); 1422 } else { 1423 *vpp = tvp; 1424 return (specvp_check(vpp, cr)); 1425 } 1426 } 1427 } 1428 } 1429 1430 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1431 1432 ZFS_ENTER(zfsvfs); 1433 ZFS_VERIFY_ZP(zdp); 1434 1435 *vpp = NULL; 1436 1437 if (flags & LOOKUP_XATTR) { 1438#ifdef TODO 1439 /* 1440 * If the xattr property is off, refuse the lookup request. 1441 */ 1442 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 1443 ZFS_EXIT(zfsvfs); 1444 return (SET_ERROR(EINVAL)); 1445 } 1446#endif 1447 1448 /* 1449 * We don't allow recursive attributes.. 1450 * Maybe someday we will. 1451 */ 1452 if (zdp->z_pflags & ZFS_XATTR) { 1453 ZFS_EXIT(zfsvfs); 1454 return (SET_ERROR(EINVAL)); 1455 } 1456 1457 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1458 ZFS_EXIT(zfsvfs); 1459 return (error); 1460 } 1461 1462 /* 1463 * Do we have permission to get into attribute directory? 1464 */ 1465 1466 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 1467 B_FALSE, cr)) { 1468 VN_RELE(*vpp); 1469 *vpp = NULL; 1470 } 1471 1472 ZFS_EXIT(zfsvfs); 1473 return (error); 1474 } 1475 1476 if (dvp->v_type != VDIR) { 1477 ZFS_EXIT(zfsvfs); 1478 return (SET_ERROR(ENOTDIR)); 1479 } 1480 1481 /* 1482 * Check accessibility of directory. 1483 */ 1484 1485 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1486 ZFS_EXIT(zfsvfs); 1487 return (error); 1488 } 1489 1490 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 1491 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1492 ZFS_EXIT(zfsvfs); 1493 return (SET_ERROR(EILSEQ)); 1494 } 1495 1496 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 1497 if (error == 0) 1498 error = specvp_check(vpp, cr); 1499 1500 /* Translate errors and add SAVENAME when needed. */ 1501 if (cnp->cn_flags & ISLASTCN) { 1502 switch (nameiop) { 1503 case CREATE: 1504 case RENAME: 1505 if (error == ENOENT) { 1506 error = EJUSTRETURN; 1507 cnp->cn_flags |= SAVENAME; 1508 break; 1509 } 1510 /* FALLTHROUGH */ 1511 case DELETE: 1512 if (error == 0) 1513 cnp->cn_flags |= SAVENAME; 1514 break; 1515 } 1516 } 1517 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) { 1518 int ltype = 0; 1519 1520 if (cnp->cn_flags & ISDOTDOT) { 1521 ltype = VOP_ISLOCKED(dvp); 1522 VOP_UNLOCK(dvp, 0); 1523 } 1524 ZFS_EXIT(zfsvfs); 1525 error = vn_lock(*vpp, cnp->cn_lkflags); 1526 if (cnp->cn_flags & ISDOTDOT) 1527 vn_lock(dvp, ltype | LK_RETRY); 1528 if (error != 0) { 1529 VN_RELE(*vpp); 1530 *vpp = NULL; 1531 return (error); 1532 } 1533 } else { 1534 ZFS_EXIT(zfsvfs); 1535 } 1536 1537#ifdef FREEBSD_NAMECACHE 1538 /* 1539 * Insert name into cache (as non-existent) if appropriate. 1540 */ 1541 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0) 1542 cache_enter(dvp, *vpp, cnp); 1543 /* 1544 * Insert name into cache if appropriate. 1545 */ 1546 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) { 1547 if (!(cnp->cn_flags & ISLASTCN) || 1548 (nameiop != DELETE && nameiop != RENAME)) { 1549 cache_enter(dvp, *vpp, cnp); 1550 } 1551 } 1552#endif 1553 1554 return (error); 1555} 1556 1557/* 1558 * Attempt to create a new entry in a directory. If the entry 1559 * already exists, truncate the file if permissible, else return 1560 * an error. Return the vp of the created or trunc'd file. 1561 * 1562 * IN: dvp - vnode of directory to put new file entry in. 1563 * name - name of new file entry. 1564 * vap - attributes of new file. 1565 * excl - flag indicating exclusive or non-exclusive mode. 1566 * mode - mode to open file with. 1567 * cr - credentials of caller. 1568 * flag - large file flag [UNUSED]. 1569 * ct - caller context 1570 * vsecp - ACL to be set 1571 * 1572 * OUT: vpp - vnode of created or trunc'd entry. 1573 * 1574 * RETURN: 0 on success, error code on failure. 1575 * 1576 * Timestamps: 1577 * dvp - ctime|mtime updated if new entry created 1578 * vp - ctime|mtime always, atime if new 1579 */ 1580 1581/* ARGSUSED */ 1582static int 1583zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode, 1584 vnode_t **vpp, cred_t *cr, kthread_t *td) 1585{ 1586 znode_t *zp, *dzp = VTOZ(dvp); 1587 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1588 zilog_t *zilog; 1589 objset_t *os; 1590 zfs_dirlock_t *dl; 1591 dmu_tx_t *tx; 1592 int error; 1593 ksid_t *ksid; 1594 uid_t uid; 1595 gid_t gid = crgetgid(cr); 1596 zfs_acl_ids_t acl_ids; 1597 boolean_t fuid_dirtied; 1598 boolean_t have_acl = B_FALSE; 1599 boolean_t waited = B_FALSE; 1600 void *vsecp = NULL; 1601 int flag = 0; 1602 1603 /* 1604 * If we have an ephemeral id, ACL, or XVATTR then 1605 * make sure file system is at proper version 1606 */ 1607 1608 ksid = crgetsid(cr, KSID_OWNER); 1609 if (ksid) 1610 uid = ksid_getid(ksid); 1611 else 1612 uid = crgetuid(cr); 1613 1614 if (zfsvfs->z_use_fuids == B_FALSE && 1615 (vsecp || (vap->va_mask & AT_XVATTR) || 1616 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1617 return (SET_ERROR(EINVAL)); 1618 1619 ZFS_ENTER(zfsvfs); 1620 ZFS_VERIFY_ZP(dzp); 1621 os = zfsvfs->z_os; 1622 zilog = zfsvfs->z_log; 1623 1624 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 1625 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1626 ZFS_EXIT(zfsvfs); 1627 return (SET_ERROR(EILSEQ)); 1628 } 1629 1630 if (vap->va_mask & AT_XVATTR) { 1631 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap, 1632 crgetuid(cr), cr, vap->va_type)) != 0) { 1633 ZFS_EXIT(zfsvfs); 1634 return (error); 1635 } 1636 } 1637 1638 getnewvnode_reserve(1); 1639 1640top: 1641 *vpp = NULL; 1642 1643 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr)) 1644 vap->va_mode &= ~S_ISVTX; 1645 1646 if (*name == '\0') { 1647 /* 1648 * Null component name refers to the directory itself. 1649 */ 1650 VN_HOLD(dvp); 1651 zp = dzp; 1652 dl = NULL; 1653 error = 0; 1654 } else { 1655 /* possible VN_HOLD(zp) */ 1656 int zflg = 0; 1657 1658 if (flag & FIGNORECASE) 1659 zflg |= ZCILOOK; 1660 1661 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1662 NULL, NULL); 1663 if (error) { 1664 if (have_acl) 1665 zfs_acl_ids_free(&acl_ids); 1666 if (strcmp(name, "..") == 0) 1667 error = SET_ERROR(EISDIR); 1668 getnewvnode_drop_reserve(); 1669 ZFS_EXIT(zfsvfs); 1670 return (error); 1671 } 1672 } 1673 1674 if (zp == NULL) { 1675 uint64_t txtype; 1676 1677 /* 1678 * Create a new file object and update the directory 1679 * to reference it. 1680 */ 1681 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1682 if (have_acl) 1683 zfs_acl_ids_free(&acl_ids); 1684 goto out; 1685 } 1686 1687 /* 1688 * We only support the creation of regular files in 1689 * extended attribute directories. 1690 */ 1691 1692 if ((dzp->z_pflags & ZFS_XATTR) && 1693 (vap->va_type != VREG)) { 1694 if (have_acl) 1695 zfs_acl_ids_free(&acl_ids); 1696 error = SET_ERROR(EINVAL); 1697 goto out; 1698 } 1699 1700 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 1701 cr, vsecp, &acl_ids)) != 0) 1702 goto out; 1703 have_acl = B_TRUE; 1704 1705 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 1706 zfs_acl_ids_free(&acl_ids); 1707 error = SET_ERROR(EDQUOT); 1708 goto out; 1709 } 1710 1711 tx = dmu_tx_create(os); 1712 1713 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1714 ZFS_SA_BASE_ATTR_SIZE); 1715 1716 fuid_dirtied = zfsvfs->z_fuid_dirty; 1717 if (fuid_dirtied) 1718 zfs_fuid_txhold(zfsvfs, tx); 1719 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1720 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 1721 if (!zfsvfs->z_use_sa && 1722 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1723 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1724 0, acl_ids.z_aclp->z_acl_bytes); 1725 } 1726 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1727 if (error) { 1728 zfs_dirent_unlock(dl); 1729 if (error == ERESTART) { 1730 waited = B_TRUE; 1731 dmu_tx_wait(tx); 1732 dmu_tx_abort(tx); 1733 goto top; 1734 } 1735 zfs_acl_ids_free(&acl_ids); 1736 dmu_tx_abort(tx); 1737 getnewvnode_drop_reserve(); 1738 ZFS_EXIT(zfsvfs); 1739 return (error); 1740 } 1741 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1742 1743 if (fuid_dirtied) 1744 zfs_fuid_sync(zfsvfs, tx); 1745 1746 (void) zfs_link_create(dl, zp, tx, ZNEW); 1747 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 1748 if (flag & FIGNORECASE) 1749 txtype |= TX_CI; 1750 zfs_log_create(zilog, tx, txtype, dzp, zp, name, 1751 vsecp, acl_ids.z_fuidp, vap); 1752 zfs_acl_ids_free(&acl_ids); 1753 dmu_tx_commit(tx); 1754 } else { 1755 int aflags = (flag & FAPPEND) ? V_APPEND : 0; 1756 1757 if (have_acl) 1758 zfs_acl_ids_free(&acl_ids); 1759 have_acl = B_FALSE; 1760 1761 /* 1762 * A directory entry already exists for this name. 1763 */ 1764 /* 1765 * Can't truncate an existing file if in exclusive mode. 1766 */ 1767 if (excl == EXCL) { 1768 error = SET_ERROR(EEXIST); 1769 goto out; 1770 } 1771 /* 1772 * Can't open a directory for writing. 1773 */ 1774 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1775 error = SET_ERROR(EISDIR); 1776 goto out; 1777 } 1778 /* 1779 * Verify requested access to file. 1780 */ 1781 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1782 goto out; 1783 } 1784 1785 mutex_enter(&dzp->z_lock); 1786 dzp->z_seq++; 1787 mutex_exit(&dzp->z_lock); 1788 1789 /* 1790 * Truncate regular files if requested. 1791 */ 1792 if ((ZTOV(zp)->v_type == VREG) && 1793 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 1794 /* we can't hold any locks when calling zfs_freesp() */ 1795 zfs_dirent_unlock(dl); 1796 dl = NULL; 1797 error = zfs_freesp(zp, 0, 0, mode, TRUE); 1798 if (error == 0) { 1799 vnevent_create(ZTOV(zp), ct); 1800 } 1801 } 1802 } 1803out: 1804 getnewvnode_drop_reserve(); 1805 if (dl) 1806 zfs_dirent_unlock(dl); 1807 1808 if (error) { 1809 if (zp) 1810 VN_RELE(ZTOV(zp)); 1811 } else { 1812 *vpp = ZTOV(zp); 1813 error = specvp_check(vpp, cr); 1814 } 1815 1816 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1817 zil_commit(zilog, 0); 1818 1819 ZFS_EXIT(zfsvfs); 1820 return (error); 1821} 1822 1823/* 1824 * Remove an entry from a directory. 1825 * 1826 * IN: dvp - vnode of directory to remove entry from. 1827 * name - name of entry to remove. 1828 * cr - credentials of caller. 1829 * ct - caller context 1830 * flags - case flags 1831 * 1832 * RETURN: 0 on success, error code on failure. 1833 * 1834 * Timestamps: 1835 * dvp - ctime|mtime 1836 * vp - ctime (if nlink > 0) 1837 */ 1838 1839uint64_t null_xattr = 0; 1840 1841/*ARGSUSED*/ 1842static int 1843zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 1844 int flags) 1845{ 1846 znode_t *zp, *dzp = VTOZ(dvp); 1847 znode_t *xzp; 1848 vnode_t *vp; 1849 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1850 zilog_t *zilog; 1851 uint64_t acl_obj, xattr_obj; 1852 uint64_t xattr_obj_unlinked = 0; 1853 uint64_t obj = 0; 1854 zfs_dirlock_t *dl; 1855 dmu_tx_t *tx; 1856 boolean_t may_delete_now, delete_now = FALSE; 1857 boolean_t unlinked, toobig = FALSE; 1858 uint64_t txtype; 1859 pathname_t *realnmp = NULL; 1860 pathname_t realnm; 1861 int error; 1862 int zflg = ZEXISTS; 1863 boolean_t waited = B_FALSE; 1864 1865 ZFS_ENTER(zfsvfs); 1866 ZFS_VERIFY_ZP(dzp); 1867 zilog = zfsvfs->z_log; 1868 1869 if (flags & FIGNORECASE) { 1870 zflg |= ZCILOOK; 1871 pn_alloc(&realnm); 1872 realnmp = &realnm; 1873 } 1874 1875top: 1876 xattr_obj = 0; 1877 xzp = NULL; 1878 /* 1879 * Attempt to lock directory; fail if entry doesn't exist. 1880 */ 1881 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1882 NULL, realnmp)) { 1883 if (realnmp) 1884 pn_free(realnmp); 1885 ZFS_EXIT(zfsvfs); 1886 return (error); 1887 } 1888 1889 vp = ZTOV(zp); 1890 1891 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1892 goto out; 1893 } 1894 1895 /* 1896 * Need to use rmdir for removing directories. 1897 */ 1898 if (vp->v_type == VDIR) { 1899 error = SET_ERROR(EPERM); 1900 goto out; 1901 } 1902 1903 vnevent_remove(vp, dvp, name, ct); 1904 1905 if (realnmp) 1906 dnlc_remove(dvp, realnmp->pn_buf); 1907 else 1908 dnlc_remove(dvp, name); 1909 1910 VI_LOCK(vp); 1911 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1912 VI_UNLOCK(vp); 1913 1914 /* 1915 * We may delete the znode now, or we may put it in the unlinked set; 1916 * it depends on whether we're the last link, and on whether there are 1917 * other holds on the vnode. So we dmu_tx_hold() the right things to 1918 * allow for either case. 1919 */ 1920 obj = zp->z_id; 1921 tx = dmu_tx_create(zfsvfs->z_os); 1922 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1923 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1924 zfs_sa_upgrade_txholds(tx, zp); 1925 zfs_sa_upgrade_txholds(tx, dzp); 1926 if (may_delete_now) { 1927 toobig = 1928 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 1929 /* if the file is too big, only hold_free a token amount */ 1930 dmu_tx_hold_free(tx, zp->z_id, 0, 1931 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 1932 } 1933 1934 /* are there any extended attributes? */ 1935 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1936 &xattr_obj, sizeof (xattr_obj)); 1937 if (error == 0 && xattr_obj) { 1938 error = zfs_zget(zfsvfs, xattr_obj, &xzp); 1939 ASSERT0(error); 1940 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 1941 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1942 } 1943 1944 mutex_enter(&zp->z_lock); 1945 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1946 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1947 mutex_exit(&zp->z_lock); 1948 1949 /* charge as an update -- would be nice not to charge at all */ 1950 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1951 1952 /* 1953 * Mark this transaction as typically resulting in a net free of 1954 * space, unless object removal will be delayed indefinitely 1955 * (due to active holds on the vnode due to the file being open). 1956 */ 1957 if (may_delete_now) 1958 dmu_tx_mark_netfree(tx); 1959 1960 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1961 if (error) { 1962 zfs_dirent_unlock(dl); 1963 VN_RELE(vp); 1964 if (xzp) 1965 VN_RELE(ZTOV(xzp)); 1966 if (error == ERESTART) { 1967 waited = B_TRUE; 1968 dmu_tx_wait(tx); 1969 dmu_tx_abort(tx); 1970 goto top; 1971 } 1972 if (realnmp) 1973 pn_free(realnmp); 1974 dmu_tx_abort(tx); 1975 ZFS_EXIT(zfsvfs); 1976 return (error); 1977 } 1978 1979 /* 1980 * Remove the directory entry. 1981 */ 1982 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1983 1984 if (error) { 1985 dmu_tx_commit(tx); 1986 goto out; 1987 } 1988 1989 if (unlinked) { 1990 /* 1991 * Hold z_lock so that we can make sure that the ACL obj 1992 * hasn't changed. Could have been deleted due to 1993 * zfs_sa_upgrade(). 1994 */ 1995 mutex_enter(&zp->z_lock); 1996 VI_LOCK(vp); 1997 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1998 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 1999 delete_now = may_delete_now && !toobig && 2000 vp->v_count == 1 && !vn_has_cached_data(vp) && 2001 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 2002 acl_obj; 2003 VI_UNLOCK(vp); 2004 } 2005 2006 if (delete_now) { 2007#ifdef __FreeBSD__ 2008 panic("zfs_remove: delete_now branch taken"); 2009#endif 2010 if (xattr_obj_unlinked) { 2011 ASSERT3U(xzp->z_links, ==, 2); 2012 mutex_enter(&xzp->z_lock); 2013 xzp->z_unlinked = 1; 2014 xzp->z_links = 0; 2015 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 2016 &xzp->z_links, sizeof (xzp->z_links), tx); 2017 ASSERT3U(error, ==, 0); 2018 mutex_exit(&xzp->z_lock); 2019 zfs_unlinked_add(xzp, tx); 2020 2021 if (zp->z_is_sa) 2022 error = sa_remove(zp->z_sa_hdl, 2023 SA_ZPL_XATTR(zfsvfs), tx); 2024 else 2025 error = sa_update(zp->z_sa_hdl, 2026 SA_ZPL_XATTR(zfsvfs), &null_xattr, 2027 sizeof (uint64_t), tx); 2028 ASSERT0(error); 2029 } 2030 VI_LOCK(vp); 2031 vp->v_count--; 2032 ASSERT0(vp->v_count); 2033 VI_UNLOCK(vp); 2034 mutex_exit(&zp->z_lock); 2035 zfs_znode_delete(zp, tx); 2036 } else if (unlinked) { 2037 mutex_exit(&zp->z_lock); 2038 zfs_unlinked_add(zp, tx); 2039#ifdef __FreeBSD__ 2040 vp->v_vflag |= VV_NOSYNC; 2041#endif 2042 } 2043 2044 txtype = TX_REMOVE; 2045 if (flags & FIGNORECASE) 2046 txtype |= TX_CI; 2047 zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 2048 2049 dmu_tx_commit(tx); 2050out: 2051 if (realnmp) 2052 pn_free(realnmp); 2053 2054 zfs_dirent_unlock(dl); 2055 2056 if (!delete_now) 2057 VN_RELE(vp); 2058 if (xzp) 2059 VN_RELE(ZTOV(xzp)); 2060 2061 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2062 zil_commit(zilog, 0); 2063 2064 ZFS_EXIT(zfsvfs); 2065 return (error); 2066} 2067 2068/* 2069 * Create a new directory and insert it into dvp using the name 2070 * provided. Return a pointer to the inserted directory. 2071 * 2072 * IN: dvp - vnode of directory to add subdir to. 2073 * dirname - name of new directory. 2074 * vap - attributes of new directory. 2075 * cr - credentials of caller. 2076 * ct - caller context 2077 * flags - case flags 2078 * vsecp - ACL to be set 2079 * 2080 * OUT: vpp - vnode of created directory. 2081 * 2082 * RETURN: 0 on success, error code on failure. 2083 * 2084 * Timestamps: 2085 * dvp - ctime|mtime updated 2086 * vp - ctime|mtime|atime updated 2087 */ 2088/*ARGSUSED*/ 2089static int 2090zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 2091 caller_context_t *ct, int flags, vsecattr_t *vsecp) 2092{ 2093 znode_t *zp, *dzp = VTOZ(dvp); 2094 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2095 zilog_t *zilog; 2096 zfs_dirlock_t *dl; 2097 uint64_t txtype; 2098 dmu_tx_t *tx; 2099 int error; 2100 int zf = ZNEW; 2101 ksid_t *ksid; 2102 uid_t uid; 2103 gid_t gid = crgetgid(cr); 2104 zfs_acl_ids_t acl_ids; 2105 boolean_t fuid_dirtied; 2106 boolean_t waited = B_FALSE; 2107 2108 ASSERT(vap->va_type == VDIR); 2109 2110 /* 2111 * If we have an ephemeral id, ACL, or XVATTR then 2112 * make sure file system is at proper version 2113 */ 2114 2115 ksid = crgetsid(cr, KSID_OWNER); 2116 if (ksid) 2117 uid = ksid_getid(ksid); 2118 else 2119 uid = crgetuid(cr); 2120 if (zfsvfs->z_use_fuids == B_FALSE && 2121 (vsecp || (vap->va_mask & AT_XVATTR) || 2122 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 2123 return (SET_ERROR(EINVAL)); 2124 2125 ZFS_ENTER(zfsvfs); 2126 ZFS_VERIFY_ZP(dzp); 2127 zilog = zfsvfs->z_log; 2128 2129 if (dzp->z_pflags & ZFS_XATTR) { 2130 ZFS_EXIT(zfsvfs); 2131 return (SET_ERROR(EINVAL)); 2132 } 2133 2134 if (zfsvfs->z_utf8 && u8_validate(dirname, 2135 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 2136 ZFS_EXIT(zfsvfs); 2137 return (SET_ERROR(EILSEQ)); 2138 } 2139 if (flags & FIGNORECASE) 2140 zf |= ZCILOOK; 2141 2142 if (vap->va_mask & AT_XVATTR) { 2143 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap, 2144 crgetuid(cr), cr, vap->va_type)) != 0) { 2145 ZFS_EXIT(zfsvfs); 2146 return (error); 2147 } 2148 } 2149 2150 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 2151 vsecp, &acl_ids)) != 0) { 2152 ZFS_EXIT(zfsvfs); 2153 return (error); 2154 } 2155 2156 getnewvnode_reserve(1); 2157 2158 /* 2159 * First make sure the new directory doesn't exist. 2160 * 2161 * Existence is checked first to make sure we don't return 2162 * EACCES instead of EEXIST which can cause some applications 2163 * to fail. 2164 */ 2165top: 2166 *vpp = NULL; 2167 2168 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 2169 NULL, NULL)) { 2170 zfs_acl_ids_free(&acl_ids); 2171 getnewvnode_drop_reserve(); 2172 ZFS_EXIT(zfsvfs); 2173 return (error); 2174 } 2175 2176 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 2177 zfs_acl_ids_free(&acl_ids); 2178 zfs_dirent_unlock(dl); 2179 getnewvnode_drop_reserve(); 2180 ZFS_EXIT(zfsvfs); 2181 return (error); 2182 } 2183 2184 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 2185 zfs_acl_ids_free(&acl_ids); 2186 zfs_dirent_unlock(dl); 2187 getnewvnode_drop_reserve(); 2188 ZFS_EXIT(zfsvfs); 2189 return (SET_ERROR(EDQUOT)); 2190 } 2191 2192 /* 2193 * Add a new entry to the directory. 2194 */ 2195 tx = dmu_tx_create(zfsvfs->z_os); 2196 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 2197 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 2198 fuid_dirtied = zfsvfs->z_fuid_dirty; 2199 if (fuid_dirtied) 2200 zfs_fuid_txhold(zfsvfs, tx); 2201 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 2202 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 2203 acl_ids.z_aclp->z_acl_bytes); 2204 } 2205 2206 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 2207 ZFS_SA_BASE_ATTR_SIZE); 2208 2209 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2210 if (error) { 2211 zfs_dirent_unlock(dl); 2212 if (error == ERESTART) { 2213 waited = B_TRUE; 2214 dmu_tx_wait(tx); 2215 dmu_tx_abort(tx); 2216 goto top; 2217 } 2218 zfs_acl_ids_free(&acl_ids); 2219 dmu_tx_abort(tx); 2220 getnewvnode_drop_reserve(); 2221 ZFS_EXIT(zfsvfs); 2222 return (error); 2223 } 2224 2225 /* 2226 * Create new node. 2227 */ 2228 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 2229 2230 if (fuid_dirtied) 2231 zfs_fuid_sync(zfsvfs, tx); 2232 2233 /* 2234 * Now put new name in parent dir. 2235 */ 2236 (void) zfs_link_create(dl, zp, tx, ZNEW); 2237 2238 *vpp = ZTOV(zp); 2239 2240 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 2241 if (flags & FIGNORECASE) 2242 txtype |= TX_CI; 2243 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 2244 acl_ids.z_fuidp, vap); 2245 2246 zfs_acl_ids_free(&acl_ids); 2247 2248 dmu_tx_commit(tx); 2249 2250 getnewvnode_drop_reserve(); 2251 2252 zfs_dirent_unlock(dl); 2253 2254 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2255 zil_commit(zilog, 0); 2256 2257 ZFS_EXIT(zfsvfs); 2258 return (0); 2259} 2260 2261/* 2262 * Remove a directory subdir entry. If the current working 2263 * directory is the same as the subdir to be removed, the 2264 * remove will fail. 2265 * 2266 * IN: dvp - vnode of directory to remove from. 2267 * name - name of directory to be removed. 2268 * cwd - vnode of current working directory. 2269 * cr - credentials of caller. 2270 * ct - caller context 2271 * flags - case flags 2272 * 2273 * RETURN: 0 on success, error code on failure. 2274 * 2275 * Timestamps: 2276 * dvp - ctime|mtime updated 2277 */ 2278/*ARGSUSED*/ 2279static int 2280zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 2281 caller_context_t *ct, int flags) 2282{ 2283 znode_t *dzp = VTOZ(dvp); 2284 znode_t *zp; 2285 vnode_t *vp; 2286 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2287 zilog_t *zilog; 2288 zfs_dirlock_t *dl; 2289 dmu_tx_t *tx; 2290 int error; 2291 int zflg = ZEXISTS; 2292 boolean_t waited = B_FALSE; 2293 2294 ZFS_ENTER(zfsvfs); 2295 ZFS_VERIFY_ZP(dzp); 2296 zilog = zfsvfs->z_log; 2297 2298 if (flags & FIGNORECASE) 2299 zflg |= ZCILOOK; 2300top: 2301 zp = NULL; 2302 2303 /* 2304 * Attempt to lock directory; fail if entry doesn't exist. 2305 */ 2306 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 2307 NULL, NULL)) { 2308 ZFS_EXIT(zfsvfs); 2309 return (error); 2310 } 2311 2312 vp = ZTOV(zp); 2313 2314 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 2315 goto out; 2316 } 2317 2318 if (vp->v_type != VDIR) { 2319 error = SET_ERROR(ENOTDIR); 2320 goto out; 2321 } 2322 2323 if (vp == cwd) { 2324 error = SET_ERROR(EINVAL); 2325 goto out; 2326 } 2327 2328 vnevent_rmdir(vp, dvp, name, ct); 2329 2330 /* 2331 * Grab a lock on the directory to make sure that noone is 2332 * trying to add (or lookup) entries while we are removing it. 2333 */ 2334 rw_enter(&zp->z_name_lock, RW_WRITER); 2335 2336 /* 2337 * Grab a lock on the parent pointer to make sure we play well 2338 * with the treewalk and directory rename code. 2339 */ 2340 rw_enter(&zp->z_parent_lock, RW_WRITER); 2341 2342 tx = dmu_tx_create(zfsvfs->z_os); 2343 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 2344 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 2345 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2346 zfs_sa_upgrade_txholds(tx, zp); 2347 zfs_sa_upgrade_txholds(tx, dzp); 2348 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2349 if (error) { 2350 rw_exit(&zp->z_parent_lock); 2351 rw_exit(&zp->z_name_lock); 2352 zfs_dirent_unlock(dl); 2353 VN_RELE(vp); 2354 if (error == ERESTART) { 2355 waited = B_TRUE; 2356 dmu_tx_wait(tx); 2357 dmu_tx_abort(tx); 2358 goto top; 2359 } 2360 dmu_tx_abort(tx); 2361 ZFS_EXIT(zfsvfs); 2362 return (error); 2363 } 2364 2365#ifdef FREEBSD_NAMECACHE 2366 cache_purge(dvp); 2367#endif 2368 2369 error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 2370 2371 if (error == 0) { 2372 uint64_t txtype = TX_RMDIR; 2373 if (flags & FIGNORECASE) 2374 txtype |= TX_CI; 2375 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 2376 } 2377 2378 dmu_tx_commit(tx); 2379 2380 rw_exit(&zp->z_parent_lock); 2381 rw_exit(&zp->z_name_lock); 2382#ifdef FREEBSD_NAMECACHE 2383 cache_purge(vp); 2384#endif 2385out: 2386 zfs_dirent_unlock(dl); 2387 2388 VN_RELE(vp); 2389 2390 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2391 zil_commit(zilog, 0); 2392 2393 ZFS_EXIT(zfsvfs); 2394 return (error); 2395} 2396 2397/* 2398 * Read as many directory entries as will fit into the provided 2399 * buffer from the given directory cursor position (specified in 2400 * the uio structure). 2401 * 2402 * IN: vp - vnode of directory to read. 2403 * uio - structure supplying read location, range info, 2404 * and return buffer. 2405 * cr - credentials of caller. 2406 * ct - caller context 2407 * flags - case flags 2408 * 2409 * OUT: uio - updated offset and range, buffer filled. 2410 * eofp - set to true if end-of-file detected. 2411 * 2412 * RETURN: 0 on success, error code on failure. 2413 * 2414 * Timestamps: 2415 * vp - atime updated 2416 * 2417 * Note that the low 4 bits of the cookie returned by zap is always zero. 2418 * This allows us to use the low range for "special" directory entries: 2419 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2420 * we use the offset 2 for the '.zfs' directory. 2421 */ 2422/* ARGSUSED */ 2423static int 2424zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies) 2425{ 2426 znode_t *zp = VTOZ(vp); 2427 iovec_t *iovp; 2428 edirent_t *eodp; 2429 dirent64_t *odp; 2430 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2431 objset_t *os; 2432 caddr_t outbuf; 2433 size_t bufsize; 2434 zap_cursor_t zc; 2435 zap_attribute_t zap; 2436 uint_t bytes_wanted; 2437 uint64_t offset; /* must be unsigned; checks for < 1 */ 2438 uint64_t parent; 2439 int local_eof; 2440 int outcount; 2441 int error; 2442 uint8_t prefetch; 2443 boolean_t check_sysattrs; 2444 uint8_t type; 2445 int ncooks; 2446 u_long *cooks = NULL; 2447 int flags = 0; 2448 2449 ZFS_ENTER(zfsvfs); 2450 ZFS_VERIFY_ZP(zp); 2451 2452 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 2453 &parent, sizeof (parent))) != 0) { 2454 ZFS_EXIT(zfsvfs); 2455 return (error); 2456 } 2457 2458 /* 2459 * If we are not given an eof variable, 2460 * use a local one. 2461 */ 2462 if (eofp == NULL) 2463 eofp = &local_eof; 2464 2465 /* 2466 * Check for valid iov_len. 2467 */ 2468 if (uio->uio_iov->iov_len <= 0) { 2469 ZFS_EXIT(zfsvfs); 2470 return (SET_ERROR(EINVAL)); 2471 } 2472 2473 /* 2474 * Quit if directory has been removed (posix) 2475 */ 2476 if ((*eofp = zp->z_unlinked) != 0) { 2477 ZFS_EXIT(zfsvfs); 2478 return (0); 2479 } 2480 2481 error = 0; 2482 os = zfsvfs->z_os; 2483 offset = uio->uio_loffset; 2484 prefetch = zp->z_zn_prefetch; 2485 2486 /* 2487 * Initialize the iterator cursor. 2488 */ 2489 if (offset <= 3) { 2490 /* 2491 * Start iteration from the beginning of the directory. 2492 */ 2493 zap_cursor_init(&zc, os, zp->z_id); 2494 } else { 2495 /* 2496 * The offset is a serialized cursor. 2497 */ 2498 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2499 } 2500 2501 /* 2502 * Get space to change directory entries into fs independent format. 2503 */ 2504 iovp = uio->uio_iov; 2505 bytes_wanted = iovp->iov_len; 2506 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2507 bufsize = bytes_wanted; 2508 outbuf = kmem_alloc(bufsize, KM_SLEEP); 2509 odp = (struct dirent64 *)outbuf; 2510 } else { 2511 bufsize = bytes_wanted; 2512 outbuf = NULL; 2513 odp = (struct dirent64 *)iovp->iov_base; 2514 } 2515 eodp = (struct edirent *)odp; 2516 2517 if (ncookies != NULL) { 2518 /* 2519 * Minimum entry size is dirent size and 1 byte for a file name. 2520 */ 2521 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1); 2522 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK); 2523 *cookies = cooks; 2524 *ncookies = ncooks; 2525 } 2526 /* 2527 * If this VFS supports the system attribute view interface; and 2528 * we're looking at an extended attribute directory; and we care 2529 * about normalization conflicts on this vfs; then we must check 2530 * for normalization conflicts with the sysattr name space. 2531 */ 2532#ifdef TODO 2533 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 2534 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 2535 (flags & V_RDDIR_ENTFLAGS); 2536#else 2537 check_sysattrs = 0; 2538#endif 2539 2540 /* 2541 * Transform to file-system independent format 2542 */ 2543 outcount = 0; 2544 while (outcount < bytes_wanted) { 2545 ino64_t objnum; 2546 ushort_t reclen; 2547 off64_t *next = NULL; 2548 2549 /* 2550 * Special case `.', `..', and `.zfs'. 2551 */ 2552 if (offset == 0) { 2553 (void) strcpy(zap.za_name, "."); 2554 zap.za_normalization_conflict = 0; 2555 objnum = zp->z_id; 2556 type = DT_DIR; 2557 } else if (offset == 1) { 2558 (void) strcpy(zap.za_name, ".."); 2559 zap.za_normalization_conflict = 0; 2560 objnum = parent; 2561 type = DT_DIR; 2562 } else if (offset == 2 && zfs_show_ctldir(zp)) { 2563 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 2564 zap.za_normalization_conflict = 0; 2565 objnum = ZFSCTL_INO_ROOT; 2566 type = DT_DIR; 2567 } else { 2568 /* 2569 * Grab next entry. 2570 */ 2571 if (error = zap_cursor_retrieve(&zc, &zap)) { 2572 if ((*eofp = (error == ENOENT)) != 0) 2573 break; 2574 else 2575 goto update; 2576 } 2577 2578 if (zap.za_integer_length != 8 || 2579 zap.za_num_integers != 1) { 2580 cmn_err(CE_WARN, "zap_readdir: bad directory " 2581 "entry, obj = %lld, offset = %lld\n", 2582 (u_longlong_t)zp->z_id, 2583 (u_longlong_t)offset); 2584 error = SET_ERROR(ENXIO); 2585 goto update; 2586 } 2587 2588 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 2589 /* 2590 * MacOS X can extract the object type here such as: 2591 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 2592 */ 2593 type = ZFS_DIRENT_TYPE(zap.za_first_integer); 2594 2595 if (check_sysattrs && !zap.za_normalization_conflict) { 2596#ifdef TODO 2597 zap.za_normalization_conflict = 2598 xattr_sysattr_casechk(zap.za_name); 2599#else 2600 panic("%s:%u: TODO", __func__, __LINE__); 2601#endif 2602 } 2603 } 2604 2605 if (flags & V_RDDIR_ACCFILTER) { 2606 /* 2607 * If we have no access at all, don't include 2608 * this entry in the returned information 2609 */ 2610 znode_t *ezp; 2611 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 2612 goto skip_entry; 2613 if (!zfs_has_access(ezp, cr)) { 2614 VN_RELE(ZTOV(ezp)); 2615 goto skip_entry; 2616 } 2617 VN_RELE(ZTOV(ezp)); 2618 } 2619 2620 if (flags & V_RDDIR_ENTFLAGS) 2621 reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 2622 else 2623 reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2624 2625 /* 2626 * Will this entry fit in the buffer? 2627 */ 2628 if (outcount + reclen > bufsize) { 2629 /* 2630 * Did we manage to fit anything in the buffer? 2631 */ 2632 if (!outcount) { 2633 error = SET_ERROR(EINVAL); 2634 goto update; 2635 } 2636 break; 2637 } 2638 if (flags & V_RDDIR_ENTFLAGS) { 2639 /* 2640 * Add extended flag entry: 2641 */ 2642 eodp->ed_ino = objnum; 2643 eodp->ed_reclen = reclen; 2644 /* NOTE: ed_off is the offset for the *next* entry */ 2645 next = &(eodp->ed_off); 2646 eodp->ed_eflags = zap.za_normalization_conflict ? 2647 ED_CASE_CONFLICT : 0; 2648 (void) strncpy(eodp->ed_name, zap.za_name, 2649 EDIRENT_NAMELEN(reclen)); 2650 eodp = (edirent_t *)((intptr_t)eodp + reclen); 2651 } else { 2652 /* 2653 * Add normal entry: 2654 */ 2655 odp->d_ino = objnum; 2656 odp->d_reclen = reclen; 2657 odp->d_namlen = strlen(zap.za_name); 2658 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1); 2659 odp->d_type = type; 2660 odp = (dirent64_t *)((intptr_t)odp + reclen); 2661 } 2662 outcount += reclen; 2663 2664 ASSERT(outcount <= bufsize); 2665 2666 /* Prefetch znode */ 2667 if (prefetch) 2668 dmu_prefetch(os, objnum, 0, 0, 0, 2669 ZIO_PRIORITY_SYNC_READ); 2670 2671 skip_entry: 2672 /* 2673 * Move to the next entry, fill in the previous offset. 2674 */ 2675 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2676 zap_cursor_advance(&zc); 2677 offset = zap_cursor_serialize(&zc); 2678 } else { 2679 offset += 1; 2680 } 2681 2682 if (cooks != NULL) { 2683 *cooks++ = offset; 2684 ncooks--; 2685 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks)); 2686 } 2687 } 2688 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2689 2690 /* Subtract unused cookies */ 2691 if (ncookies != NULL) 2692 *ncookies -= ncooks; 2693 2694 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2695 iovp->iov_base += outcount; 2696 iovp->iov_len -= outcount; 2697 uio->uio_resid -= outcount; 2698 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2699 /* 2700 * Reset the pointer. 2701 */ 2702 offset = uio->uio_loffset; 2703 } 2704 2705update: 2706 zap_cursor_fini(&zc); 2707 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2708 kmem_free(outbuf, bufsize); 2709 2710 if (error == ENOENT) 2711 error = 0; 2712 2713 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2714 2715 uio->uio_loffset = offset; 2716 ZFS_EXIT(zfsvfs); 2717 if (error != 0 && cookies != NULL) { 2718 free(*cookies, M_TEMP); 2719 *cookies = NULL; 2720 *ncookies = 0; 2721 } 2722 return (error); 2723} 2724 2725ulong_t zfs_fsync_sync_cnt = 4; 2726 2727static int 2728zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2729{ 2730 znode_t *zp = VTOZ(vp); 2731 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2732 2733 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 2734 2735 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 2736 ZFS_ENTER(zfsvfs); 2737 ZFS_VERIFY_ZP(zp); 2738 zil_commit(zfsvfs->z_log, zp->z_id); 2739 ZFS_EXIT(zfsvfs); 2740 } 2741 return (0); 2742} 2743 2744 2745/* 2746 * Get the requested file attributes and place them in the provided 2747 * vattr structure. 2748 * 2749 * IN: vp - vnode of file. 2750 * vap - va_mask identifies requested attributes. 2751 * If AT_XVATTR set, then optional attrs are requested 2752 * flags - ATTR_NOACLCHECK (CIFS server context) 2753 * cr - credentials of caller. 2754 * ct - caller context 2755 * 2756 * OUT: vap - attribute values. 2757 * 2758 * RETURN: 0 (always succeeds). 2759 */ 2760/* ARGSUSED */ 2761static int 2762zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2763 caller_context_t *ct) 2764{ 2765 znode_t *zp = VTOZ(vp); 2766 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2767 int error = 0; 2768 uint32_t blksize; 2769 u_longlong_t nblocks; 2770 uint64_t links; 2771 uint64_t mtime[2], ctime[2], crtime[2], rdev; 2772 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2773 xoptattr_t *xoap = NULL; 2774 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2775 sa_bulk_attr_t bulk[4]; 2776 int count = 0; 2777 2778 ZFS_ENTER(zfsvfs); 2779 ZFS_VERIFY_ZP(zp); 2780 2781 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2782 2783 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 2784 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 2785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16); 2786 if (vp->v_type == VBLK || vp->v_type == VCHR) 2787 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, 2788 &rdev, 8); 2789 2790 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 2791 ZFS_EXIT(zfsvfs); 2792 return (error); 2793 } 2794 2795 /* 2796 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 2797 * Also, if we are the owner don't bother, since owner should 2798 * always be allowed to read basic attributes of file. 2799 */ 2800 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && 2801 (vap->va_uid != crgetuid(cr))) { 2802 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 2803 skipaclchk, cr)) { 2804 ZFS_EXIT(zfsvfs); 2805 return (error); 2806 } 2807 } 2808 2809 /* 2810 * Return all attributes. It's cheaper to provide the answer 2811 * than to determine whether we were asked the question. 2812 */ 2813 2814 mutex_enter(&zp->z_lock); 2815 vap->va_type = IFTOVT(zp->z_mode); 2816 vap->va_mode = zp->z_mode & ~S_IFMT; 2817#ifdef sun 2818 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2819#else 2820 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 2821#endif 2822 vap->va_nodeid = zp->z_id; 2823 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 2824 links = zp->z_links + 1; 2825 else 2826 links = zp->z_links; 2827 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */ 2828 vap->va_size = zp->z_size; 2829#ifdef sun 2830 vap->va_rdev = vp->v_rdev; 2831#else 2832 if (vp->v_type == VBLK || vp->v_type == VCHR) 2833 vap->va_rdev = zfs_cmpldev(rdev); 2834#endif 2835 vap->va_seq = zp->z_seq; 2836 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */ 2837 vap->va_filerev = zp->z_seq; 2838 2839 /* 2840 * Add in any requested optional attributes and the create time. 2841 * Also set the corresponding bits in the returned attribute bitmap. 2842 */ 2843 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 2844 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 2845 xoap->xoa_archive = 2846 ((zp->z_pflags & ZFS_ARCHIVE) != 0); 2847 XVA_SET_RTN(xvap, XAT_ARCHIVE); 2848 } 2849 2850 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 2851 xoap->xoa_readonly = 2852 ((zp->z_pflags & ZFS_READONLY) != 0); 2853 XVA_SET_RTN(xvap, XAT_READONLY); 2854 } 2855 2856 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 2857 xoap->xoa_system = 2858 ((zp->z_pflags & ZFS_SYSTEM) != 0); 2859 XVA_SET_RTN(xvap, XAT_SYSTEM); 2860 } 2861 2862 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 2863 xoap->xoa_hidden = 2864 ((zp->z_pflags & ZFS_HIDDEN) != 0); 2865 XVA_SET_RTN(xvap, XAT_HIDDEN); 2866 } 2867 2868 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2869 xoap->xoa_nounlink = 2870 ((zp->z_pflags & ZFS_NOUNLINK) != 0); 2871 XVA_SET_RTN(xvap, XAT_NOUNLINK); 2872 } 2873 2874 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2875 xoap->xoa_immutable = 2876 ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 2877 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 2878 } 2879 2880 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2881 xoap->xoa_appendonly = 2882 ((zp->z_pflags & ZFS_APPENDONLY) != 0); 2883 XVA_SET_RTN(xvap, XAT_APPENDONLY); 2884 } 2885 2886 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2887 xoap->xoa_nodump = 2888 ((zp->z_pflags & ZFS_NODUMP) != 0); 2889 XVA_SET_RTN(xvap, XAT_NODUMP); 2890 } 2891 2892 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 2893 xoap->xoa_opaque = 2894 ((zp->z_pflags & ZFS_OPAQUE) != 0); 2895 XVA_SET_RTN(xvap, XAT_OPAQUE); 2896 } 2897 2898 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2899 xoap->xoa_av_quarantined = 2900 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 2901 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 2902 } 2903 2904 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2905 xoap->xoa_av_modified = 2906 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 2907 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 2908 } 2909 2910 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 2911 vp->v_type == VREG) { 2912 zfs_sa_get_scanstamp(zp, xvap); 2913 } 2914 2915 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 2916 uint64_t times[2]; 2917 2918 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 2919 times, sizeof (times)); 2920 ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 2921 XVA_SET_RTN(xvap, XAT_CREATETIME); 2922 } 2923 2924 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2925 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 2926 XVA_SET_RTN(xvap, XAT_REPARSE); 2927 } 2928 if (XVA_ISSET_REQ(xvap, XAT_GEN)) { 2929 xoap->xoa_generation = zp->z_gen; 2930 XVA_SET_RTN(xvap, XAT_GEN); 2931 } 2932 2933 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 2934 xoap->xoa_offline = 2935 ((zp->z_pflags & ZFS_OFFLINE) != 0); 2936 XVA_SET_RTN(xvap, XAT_OFFLINE); 2937 } 2938 2939 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 2940 xoap->xoa_sparse = 2941 ((zp->z_pflags & ZFS_SPARSE) != 0); 2942 XVA_SET_RTN(xvap, XAT_SPARSE); 2943 } 2944 } 2945 2946 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 2947 ZFS_TIME_DECODE(&vap->va_mtime, mtime); 2948 ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2949 ZFS_TIME_DECODE(&vap->va_birthtime, crtime); 2950 2951 mutex_exit(&zp->z_lock); 2952 2953 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks); 2954 vap->va_blksize = blksize; 2955 vap->va_bytes = nblocks << 9; /* nblocks * 512 */ 2956 2957 if (zp->z_blksz == 0) { 2958 /* 2959 * Block size hasn't been set; suggest maximal I/O transfers. 2960 */ 2961 vap->va_blksize = zfsvfs->z_max_blksz; 2962 } 2963 2964 ZFS_EXIT(zfsvfs); 2965 return (0); 2966} 2967 2968/* 2969 * Set the file attributes to the values contained in the 2970 * vattr structure. 2971 * 2972 * IN: vp - vnode of file to be modified. 2973 * vap - new attribute values. 2974 * If AT_XVATTR set, then optional attrs are being set 2975 * flags - ATTR_UTIME set if non-default time values provided. 2976 * - ATTR_NOACLCHECK (CIFS context only). 2977 * cr - credentials of caller. 2978 * ct - caller context 2979 * 2980 * RETURN: 0 on success, error code on failure. 2981 * 2982 * Timestamps: 2983 * vp - ctime updated, mtime updated if size changed. 2984 */ 2985/* ARGSUSED */ 2986static int 2987zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2988 caller_context_t *ct) 2989{ 2990 znode_t *zp = VTOZ(vp); 2991 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2992 zilog_t *zilog; 2993 dmu_tx_t *tx; 2994 vattr_t oldva; 2995 xvattr_t tmpxvattr; 2996 uint_t mask = vap->va_mask; 2997 uint_t saved_mask = 0; 2998 uint64_t saved_mode; 2999 int trim_mask = 0; 3000 uint64_t new_mode; 3001 uint64_t new_uid, new_gid; 3002 uint64_t xattr_obj; 3003 uint64_t mtime[2], ctime[2]; 3004 znode_t *attrzp; 3005 int need_policy = FALSE; 3006 int err, err2; 3007 zfs_fuid_info_t *fuidp = NULL; 3008 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 3009 xoptattr_t *xoap; 3010 zfs_acl_t *aclp; 3011 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 3012 boolean_t fuid_dirtied = B_FALSE; 3013 sa_bulk_attr_t bulk[7], xattr_bulk[7]; 3014 int count = 0, xattr_count = 0; 3015 3016 if (mask == 0) 3017 return (0); 3018 3019 if (mask & AT_NOSET) 3020 return (SET_ERROR(EINVAL)); 3021 3022 ZFS_ENTER(zfsvfs); 3023 ZFS_VERIFY_ZP(zp); 3024 3025 zilog = zfsvfs->z_log; 3026 3027 /* 3028 * Make sure that if we have ephemeral uid/gid or xvattr specified 3029 * that file system is at proper version level 3030 */ 3031 3032 if (zfsvfs->z_use_fuids == B_FALSE && 3033 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 3034 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 3035 (mask & AT_XVATTR))) { 3036 ZFS_EXIT(zfsvfs); 3037 return (SET_ERROR(EINVAL)); 3038 } 3039 3040 if (mask & AT_SIZE && vp->v_type == VDIR) { 3041 ZFS_EXIT(zfsvfs); 3042 return (SET_ERROR(EISDIR)); 3043 } 3044 3045 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 3046 ZFS_EXIT(zfsvfs); 3047 return (SET_ERROR(EINVAL)); 3048 } 3049 3050 /* 3051 * If this is an xvattr_t, then get a pointer to the structure of 3052 * optional attributes. If this is NULL, then we have a vattr_t. 3053 */ 3054 xoap = xva_getxoptattr(xvap); 3055 3056 xva_init(&tmpxvattr); 3057 3058 /* 3059 * Immutable files can only alter immutable bit and atime 3060 */ 3061 if ((zp->z_pflags & ZFS_IMMUTABLE) && 3062 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 3063 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 3064 ZFS_EXIT(zfsvfs); 3065 return (SET_ERROR(EPERM)); 3066 } 3067 3068 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 3069 ZFS_EXIT(zfsvfs); 3070 return (SET_ERROR(EPERM)); 3071 } 3072 3073 /* 3074 * Verify timestamps doesn't overflow 32 bits. 3075 * ZFS can handle large timestamps, but 32bit syscalls can't 3076 * handle times greater than 2039. This check should be removed 3077 * once large timestamps are fully supported. 3078 */ 3079 if (mask & (AT_ATIME | AT_MTIME)) { 3080 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 3081 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 3082 ZFS_EXIT(zfsvfs); 3083 return (SET_ERROR(EOVERFLOW)); 3084 } 3085 } 3086 3087top: 3088 attrzp = NULL; 3089 aclp = NULL; 3090 3091 /* Can this be moved to before the top label? */ 3092 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 3093 ZFS_EXIT(zfsvfs); 3094 return (SET_ERROR(EROFS)); 3095 } 3096 3097 /* 3098 * First validate permissions 3099 */ 3100 3101 if (mask & AT_SIZE) { 3102 /* 3103 * XXX - Note, we are not providing any open 3104 * mode flags here (like FNDELAY), so we may 3105 * block if there are locks present... this 3106 * should be addressed in openat(). 3107 */ 3108 /* XXX - would it be OK to generate a log record here? */ 3109 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 3110 if (err) { 3111 ZFS_EXIT(zfsvfs); 3112 return (err); 3113 } 3114 } 3115 3116 if (mask & (AT_ATIME|AT_MTIME) || 3117 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 3118 XVA_ISSET_REQ(xvap, XAT_READONLY) || 3119 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 3120 XVA_ISSET_REQ(xvap, XAT_OFFLINE) || 3121 XVA_ISSET_REQ(xvap, XAT_SPARSE) || 3122 XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 3123 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 3124 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 3125 skipaclchk, cr); 3126 } 3127 3128 if (mask & (AT_UID|AT_GID)) { 3129 int idmask = (mask & (AT_UID|AT_GID)); 3130 int take_owner; 3131 int take_group; 3132 3133 /* 3134 * NOTE: even if a new mode is being set, 3135 * we may clear S_ISUID/S_ISGID bits. 3136 */ 3137 3138 if (!(mask & AT_MODE)) 3139 vap->va_mode = zp->z_mode; 3140 3141 /* 3142 * Take ownership or chgrp to group we are a member of 3143 */ 3144 3145 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 3146 take_group = (mask & AT_GID) && 3147 zfs_groupmember(zfsvfs, vap->va_gid, cr); 3148 3149 /* 3150 * If both AT_UID and AT_GID are set then take_owner and 3151 * take_group must both be set in order to allow taking 3152 * ownership. 3153 * 3154 * Otherwise, send the check through secpolicy_vnode_setattr() 3155 * 3156 */ 3157 3158 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 3159 ((idmask == AT_UID) && take_owner) || 3160 ((idmask == AT_GID) && take_group)) { 3161 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 3162 skipaclchk, cr) == 0) { 3163 /* 3164 * Remove setuid/setgid for non-privileged users 3165 */ 3166 secpolicy_setid_clear(vap, vp, cr); 3167 trim_mask = (mask & (AT_UID|AT_GID)); 3168 } else { 3169 need_policy = TRUE; 3170 } 3171 } else { 3172 need_policy = TRUE; 3173 } 3174 } 3175 3176 mutex_enter(&zp->z_lock); 3177 oldva.va_mode = zp->z_mode; 3178 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 3179 if (mask & AT_XVATTR) { 3180 /* 3181 * Update xvattr mask to include only those attributes 3182 * that are actually changing. 3183 * 3184 * the bits will be restored prior to actually setting 3185 * the attributes so the caller thinks they were set. 3186 */ 3187 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 3188 if (xoap->xoa_appendonly != 3189 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 3190 need_policy = TRUE; 3191 } else { 3192 XVA_CLR_REQ(xvap, XAT_APPENDONLY); 3193 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 3194 } 3195 } 3196 3197 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 3198 if (xoap->xoa_nounlink != 3199 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 3200 need_policy = TRUE; 3201 } else { 3202 XVA_CLR_REQ(xvap, XAT_NOUNLINK); 3203 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 3204 } 3205 } 3206 3207 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 3208 if (xoap->xoa_immutable != 3209 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 3210 need_policy = TRUE; 3211 } else { 3212 XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 3213 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 3214 } 3215 } 3216 3217 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 3218 if (xoap->xoa_nodump != 3219 ((zp->z_pflags & ZFS_NODUMP) != 0)) { 3220 need_policy = TRUE; 3221 } else { 3222 XVA_CLR_REQ(xvap, XAT_NODUMP); 3223 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 3224 } 3225 } 3226 3227 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 3228 if (xoap->xoa_av_modified != 3229 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 3230 need_policy = TRUE; 3231 } else { 3232 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 3233 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 3234 } 3235 } 3236 3237 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 3238 if ((vp->v_type != VREG && 3239 xoap->xoa_av_quarantined) || 3240 xoap->xoa_av_quarantined != 3241 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 3242 need_policy = TRUE; 3243 } else { 3244 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 3245 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 3246 } 3247 } 3248 3249 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 3250 mutex_exit(&zp->z_lock); 3251 ZFS_EXIT(zfsvfs); 3252 return (SET_ERROR(EPERM)); 3253 } 3254 3255 if (need_policy == FALSE && 3256 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 3257 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 3258 need_policy = TRUE; 3259 } 3260 } 3261 3262 mutex_exit(&zp->z_lock); 3263 3264 if (mask & AT_MODE) { 3265 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 3266 err = secpolicy_setid_setsticky_clear(vp, vap, 3267 &oldva, cr); 3268 if (err) { 3269 ZFS_EXIT(zfsvfs); 3270 return (err); 3271 } 3272 trim_mask |= AT_MODE; 3273 } else { 3274 need_policy = TRUE; 3275 } 3276 } 3277 3278 if (need_policy) { 3279 /* 3280 * If trim_mask is set then take ownership 3281 * has been granted or write_acl is present and user 3282 * has the ability to modify mode. In that case remove 3283 * UID|GID and or MODE from mask so that 3284 * secpolicy_vnode_setattr() doesn't revoke it. 3285 */ 3286 3287 if (trim_mask) { 3288 saved_mask = vap->va_mask; 3289 vap->va_mask &= ~trim_mask; 3290 if (trim_mask & AT_MODE) { 3291 /* 3292 * Save the mode, as secpolicy_vnode_setattr() 3293 * will overwrite it with ova.va_mode. 3294 */ 3295 saved_mode = vap->va_mode; 3296 } 3297 } 3298 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 3299 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 3300 if (err) { 3301 ZFS_EXIT(zfsvfs); 3302 return (err); 3303 } 3304 3305 if (trim_mask) { 3306 vap->va_mask |= saved_mask; 3307 if (trim_mask & AT_MODE) { 3308 /* 3309 * Recover the mode after 3310 * secpolicy_vnode_setattr(). 3311 */ 3312 vap->va_mode = saved_mode; 3313 } 3314 } 3315 } 3316 3317 /* 3318 * secpolicy_vnode_setattr, or take ownership may have 3319 * changed va_mask 3320 */ 3321 mask = vap->va_mask; 3322 3323 if ((mask & (AT_UID | AT_GID))) { 3324 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 3325 &xattr_obj, sizeof (xattr_obj)); 3326 3327 if (err == 0 && xattr_obj) { 3328 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 3329 if (err) 3330 goto out2; 3331 } 3332 if (mask & AT_UID) { 3333 new_uid = zfs_fuid_create(zfsvfs, 3334 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 3335 if (new_uid != zp->z_uid && 3336 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 3337 if (attrzp) 3338 VN_RELE(ZTOV(attrzp)); 3339 err = SET_ERROR(EDQUOT); 3340 goto out2; 3341 } 3342 } 3343 3344 if (mask & AT_GID) { 3345 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 3346 cr, ZFS_GROUP, &fuidp); 3347 if (new_gid != zp->z_gid && 3348 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 3349 if (attrzp) 3350 VN_RELE(ZTOV(attrzp)); 3351 err = SET_ERROR(EDQUOT); 3352 goto out2; 3353 } 3354 } 3355 } 3356 tx = dmu_tx_create(zfsvfs->z_os); 3357 3358 if (mask & AT_MODE) { 3359 uint64_t pmode = zp->z_mode; 3360 uint64_t acl_obj; 3361 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 3362 3363 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED && 3364 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) { 3365 err = SET_ERROR(EPERM); 3366 goto out; 3367 } 3368 3369 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 3370 goto out; 3371 3372 mutex_enter(&zp->z_lock); 3373 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 3374 /* 3375 * Are we upgrading ACL from old V0 format 3376 * to V1 format? 3377 */ 3378 if (zfsvfs->z_version >= ZPL_VERSION_FUID && 3379 zfs_znode_acl_version(zp) == 3380 ZFS_ACL_VERSION_INITIAL) { 3381 dmu_tx_hold_free(tx, acl_obj, 0, 3382 DMU_OBJECT_END); 3383 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3384 0, aclp->z_acl_bytes); 3385 } else { 3386 dmu_tx_hold_write(tx, acl_obj, 0, 3387 aclp->z_acl_bytes); 3388 } 3389 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3390 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3391 0, aclp->z_acl_bytes); 3392 } 3393 mutex_exit(&zp->z_lock); 3394 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3395 } else { 3396 if ((mask & AT_XVATTR) && 3397 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3398 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3399 else 3400 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3401 } 3402 3403 if (attrzp) { 3404 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 3405 } 3406 3407 fuid_dirtied = zfsvfs->z_fuid_dirty; 3408 if (fuid_dirtied) 3409 zfs_fuid_txhold(zfsvfs, tx); 3410 3411 zfs_sa_upgrade_txholds(tx, zp); 3412 3413 err = dmu_tx_assign(tx, TXG_WAIT); 3414 if (err) 3415 goto out; 3416 3417 count = 0; 3418 /* 3419 * Set each attribute requested. 3420 * We group settings according to the locks they need to acquire. 3421 * 3422 * Note: you cannot set ctime directly, although it will be 3423 * updated as a side-effect of calling this function. 3424 */ 3425 3426 3427 if (mask & (AT_UID|AT_GID|AT_MODE)) 3428 mutex_enter(&zp->z_acl_lock); 3429 mutex_enter(&zp->z_lock); 3430 3431 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 3432 &zp->z_pflags, sizeof (zp->z_pflags)); 3433 3434 if (attrzp) { 3435 if (mask & (AT_UID|AT_GID|AT_MODE)) 3436 mutex_enter(&attrzp->z_acl_lock); 3437 mutex_enter(&attrzp->z_lock); 3438 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3439 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 3440 sizeof (attrzp->z_pflags)); 3441 } 3442 3443 if (mask & (AT_UID|AT_GID)) { 3444 3445 if (mask & AT_UID) { 3446 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 3447 &new_uid, sizeof (new_uid)); 3448 zp->z_uid = new_uid; 3449 if (attrzp) { 3450 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3451 SA_ZPL_UID(zfsvfs), NULL, &new_uid, 3452 sizeof (new_uid)); 3453 attrzp->z_uid = new_uid; 3454 } 3455 } 3456 3457 if (mask & AT_GID) { 3458 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 3459 NULL, &new_gid, sizeof (new_gid)); 3460 zp->z_gid = new_gid; 3461 if (attrzp) { 3462 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3463 SA_ZPL_GID(zfsvfs), NULL, &new_gid, 3464 sizeof (new_gid)); 3465 attrzp->z_gid = new_gid; 3466 } 3467 } 3468 if (!(mask & AT_MODE)) { 3469 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 3470 NULL, &new_mode, sizeof (new_mode)); 3471 new_mode = zp->z_mode; 3472 } 3473 err = zfs_acl_chown_setattr(zp); 3474 ASSERT(err == 0); 3475 if (attrzp) { 3476 err = zfs_acl_chown_setattr(attrzp); 3477 ASSERT(err == 0); 3478 } 3479 } 3480 3481 if (mask & AT_MODE) { 3482 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 3483 &new_mode, sizeof (new_mode)); 3484 zp->z_mode = new_mode; 3485 ASSERT3U((uintptr_t)aclp, !=, 0); 3486 err = zfs_aclset_common(zp, aclp, cr, tx); 3487 ASSERT0(err); 3488 if (zp->z_acl_cached) 3489 zfs_acl_free(zp->z_acl_cached); 3490 zp->z_acl_cached = aclp; 3491 aclp = NULL; 3492 } 3493 3494 3495 if (mask & AT_ATIME) { 3496 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 3497 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 3498 &zp->z_atime, sizeof (zp->z_atime)); 3499 } 3500 3501 if (mask & AT_MTIME) { 3502 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 3503 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 3504 mtime, sizeof (mtime)); 3505 } 3506 3507 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 3508 if (mask & AT_SIZE && !(mask & AT_MTIME)) { 3509 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 3510 NULL, mtime, sizeof (mtime)); 3511 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3512 &ctime, sizeof (ctime)); 3513 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 3514 B_TRUE); 3515 } else if (mask != 0) { 3516 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3517 &ctime, sizeof (ctime)); 3518 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 3519 B_TRUE); 3520 if (attrzp) { 3521 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3522 SA_ZPL_CTIME(zfsvfs), NULL, 3523 &ctime, sizeof (ctime)); 3524 zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 3525 mtime, ctime, B_TRUE); 3526 } 3527 } 3528 /* 3529 * Do this after setting timestamps to prevent timestamp 3530 * update from toggling bit 3531 */ 3532 3533 if (xoap && (mask & AT_XVATTR)) { 3534 3535 /* 3536 * restore trimmed off masks 3537 * so that return masks can be set for caller. 3538 */ 3539 3540 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 3541 XVA_SET_REQ(xvap, XAT_APPENDONLY); 3542 } 3543 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 3544 XVA_SET_REQ(xvap, XAT_NOUNLINK); 3545 } 3546 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 3547 XVA_SET_REQ(xvap, XAT_IMMUTABLE); 3548 } 3549 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 3550 XVA_SET_REQ(xvap, XAT_NODUMP); 3551 } 3552 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 3553 XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 3554 } 3555 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 3556 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 3557 } 3558 3559 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3560 ASSERT(vp->v_type == VREG); 3561 3562 zfs_xvattr_set(zp, xvap, tx); 3563 } 3564 3565 if (fuid_dirtied) 3566 zfs_fuid_sync(zfsvfs, tx); 3567 3568 if (mask != 0) 3569 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 3570 3571 mutex_exit(&zp->z_lock); 3572 if (mask & (AT_UID|AT_GID|AT_MODE)) 3573 mutex_exit(&zp->z_acl_lock); 3574 3575 if (attrzp) { 3576 if (mask & (AT_UID|AT_GID|AT_MODE)) 3577 mutex_exit(&attrzp->z_acl_lock); 3578 mutex_exit(&attrzp->z_lock); 3579 } 3580out: 3581 if (err == 0 && attrzp) { 3582 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 3583 xattr_count, tx); 3584 ASSERT(err2 == 0); 3585 } 3586 3587 if (attrzp) 3588 VN_RELE(ZTOV(attrzp)); 3589 3590 if (aclp) 3591 zfs_acl_free(aclp); 3592 3593 if (fuidp) { 3594 zfs_fuid_info_free(fuidp); 3595 fuidp = NULL; 3596 } 3597 3598 if (err) { 3599 dmu_tx_abort(tx); 3600 if (err == ERESTART) 3601 goto top; 3602 } else { 3603 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 3604 dmu_tx_commit(tx); 3605 } 3606 3607out2: 3608 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3609 zil_commit(zilog, 0); 3610 3611 ZFS_EXIT(zfsvfs); 3612 return (err); 3613} 3614 3615typedef struct zfs_zlock { 3616 krwlock_t *zl_rwlock; /* lock we acquired */ 3617 znode_t *zl_znode; /* znode we held */ 3618 struct zfs_zlock *zl_next; /* next in list */ 3619} zfs_zlock_t; 3620 3621/* 3622 * Drop locks and release vnodes that were held by zfs_rename_lock(). 3623 */ 3624static void 3625zfs_rename_unlock(zfs_zlock_t **zlpp) 3626{ 3627 zfs_zlock_t *zl; 3628 3629 while ((zl = *zlpp) != NULL) { 3630 if (zl->zl_znode != NULL) 3631 VN_RELE(ZTOV(zl->zl_znode)); 3632 rw_exit(zl->zl_rwlock); 3633 *zlpp = zl->zl_next; 3634 kmem_free(zl, sizeof (*zl)); 3635 } 3636} 3637 3638/* 3639 * Search back through the directory tree, using the ".." entries. 3640 * Lock each directory in the chain to prevent concurrent renames. 3641 * Fail any attempt to move a directory into one of its own descendants. 3642 * XXX - z_parent_lock can overlap with map or grow locks 3643 */ 3644static int 3645zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3646{ 3647 zfs_zlock_t *zl; 3648 znode_t *zp = tdzp; 3649 uint64_t rootid = zp->z_zfsvfs->z_root; 3650 uint64_t oidp = zp->z_id; 3651 krwlock_t *rwlp = &szp->z_parent_lock; 3652 krw_t rw = RW_WRITER; 3653 3654 /* 3655 * First pass write-locks szp and compares to zp->z_id. 3656 * Later passes read-lock zp and compare to zp->z_parent. 3657 */ 3658 do { 3659 if (!rw_tryenter(rwlp, rw)) { 3660 /* 3661 * Another thread is renaming in this path. 3662 * Note that if we are a WRITER, we don't have any 3663 * parent_locks held yet. 3664 */ 3665 if (rw == RW_READER && zp->z_id > szp->z_id) { 3666 /* 3667 * Drop our locks and restart 3668 */ 3669 zfs_rename_unlock(&zl); 3670 *zlpp = NULL; 3671 zp = tdzp; 3672 oidp = zp->z_id; 3673 rwlp = &szp->z_parent_lock; 3674 rw = RW_WRITER; 3675 continue; 3676 } else { 3677 /* 3678 * Wait for other thread to drop its locks 3679 */ 3680 rw_enter(rwlp, rw); 3681 } 3682 } 3683 3684 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3685 zl->zl_rwlock = rwlp; 3686 zl->zl_znode = NULL; 3687 zl->zl_next = *zlpp; 3688 *zlpp = zl; 3689 3690 if (oidp == szp->z_id) /* We're a descendant of szp */ 3691 return (SET_ERROR(EINVAL)); 3692 3693 if (oidp == rootid) /* We've hit the top */ 3694 return (0); 3695 3696 if (rw == RW_READER) { /* i.e. not the first pass */ 3697 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3698 if (error) 3699 return (error); 3700 zl->zl_znode = zp; 3701 } 3702 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 3703 &oidp, sizeof (oidp)); 3704 rwlp = &zp->z_parent_lock; 3705 rw = RW_READER; 3706 3707 } while (zp->z_id != sdzp->z_id); 3708 3709 return (0); 3710} 3711 3712/* 3713 * Move an entry from the provided source directory to the target 3714 * directory. Change the entry name as indicated. 3715 * 3716 * IN: sdvp - Source directory containing the "old entry". 3717 * snm - Old entry name. 3718 * tdvp - Target directory to contain the "new entry". 3719 * tnm - New entry name. 3720 * cr - credentials of caller. 3721 * ct - caller context 3722 * flags - case flags 3723 * 3724 * RETURN: 0 on success, error code on failure. 3725 * 3726 * Timestamps: 3727 * sdvp,tdvp - ctime|mtime updated 3728 */ 3729/*ARGSUSED*/ 3730static int 3731zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 3732 caller_context_t *ct, int flags) 3733{ 3734 znode_t *tdzp, *sdzp, *szp, *tzp; 3735 zfsvfs_t *zfsvfs; 3736 zilog_t *zilog; 3737 vnode_t *realvp; 3738 zfs_dirlock_t *sdl, *tdl; 3739 dmu_tx_t *tx; 3740 zfs_zlock_t *zl; 3741 int cmp, serr, terr; 3742 int error = 0; 3743 int zflg = 0; 3744 boolean_t waited = B_FALSE; 3745 3746 tdzp = VTOZ(tdvp); 3747 ZFS_VERIFY_ZP(tdzp); 3748 zfsvfs = tdzp->z_zfsvfs; 3749 ZFS_ENTER(zfsvfs); 3750 zilog = zfsvfs->z_log; 3751 sdzp = VTOZ(sdvp); 3752 3753 /* 3754 * In case sdzp is not valid, let's be sure to exit from the right 3755 * zfsvfs_t. 3756 */ 3757 if (sdzp->z_sa_hdl == NULL) { 3758 ZFS_EXIT(zfsvfs); 3759 return (SET_ERROR(EIO)); 3760 } 3761 3762 /* 3763 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the 3764 * ctldir appear to have the same v_vfsp. 3765 */ 3766 if (sdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) { 3767 ZFS_EXIT(zfsvfs); 3768 return (SET_ERROR(EXDEV)); 3769 } 3770 3771 if (zfsvfs->z_utf8 && u8_validate(tnm, 3772 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3773 ZFS_EXIT(zfsvfs); 3774 return (SET_ERROR(EILSEQ)); 3775 } 3776 3777 if (flags & FIGNORECASE) 3778 zflg |= ZCILOOK; 3779 3780top: 3781 szp = NULL; 3782 tzp = NULL; 3783 zl = NULL; 3784 3785 /* 3786 * This is to prevent the creation of links into attribute space 3787 * by renaming a linked file into/outof an attribute directory. 3788 * See the comment in zfs_link() for why this is considered bad. 3789 */ 3790 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3791 ZFS_EXIT(zfsvfs); 3792 return (SET_ERROR(EINVAL)); 3793 } 3794 3795 /* 3796 * Lock source and target directory entries. To prevent deadlock, 3797 * a lock ordering must be defined. We lock the directory with 3798 * the smallest object id first, or if it's a tie, the one with 3799 * the lexically first name. 3800 */ 3801 if (sdzp->z_id < tdzp->z_id) { 3802 cmp = -1; 3803 } else if (sdzp->z_id > tdzp->z_id) { 3804 cmp = 1; 3805 } else { 3806 /* 3807 * First compare the two name arguments without 3808 * considering any case folding. 3809 */ 3810 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 3811 3812 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 3813 ASSERT(error == 0 || !zfsvfs->z_utf8); 3814 if (cmp == 0) { 3815 /* 3816 * POSIX: "If the old argument and the new argument 3817 * both refer to links to the same existing file, 3818 * the rename() function shall return successfully 3819 * and perform no other action." 3820 */ 3821 ZFS_EXIT(zfsvfs); 3822 return (0); 3823 } 3824 /* 3825 * If the file system is case-folding, then we may 3826 * have some more checking to do. A case-folding file 3827 * system is either supporting mixed case sensitivity 3828 * access or is completely case-insensitive. Note 3829 * that the file system is always case preserving. 3830 * 3831 * In mixed sensitivity mode case sensitive behavior 3832 * is the default. FIGNORECASE must be used to 3833 * explicitly request case insensitive behavior. 3834 * 3835 * If the source and target names provided differ only 3836 * by case (e.g., a request to rename 'tim' to 'Tim'), 3837 * we will treat this as a special case in the 3838 * case-insensitive mode: as long as the source name 3839 * is an exact match, we will allow this to proceed as 3840 * a name-change request. 3841 */ 3842 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 3843 (zfsvfs->z_case == ZFS_CASE_MIXED && 3844 flags & FIGNORECASE)) && 3845 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 3846 &error) == 0) { 3847 /* 3848 * case preserving rename request, require exact 3849 * name matches 3850 */ 3851 zflg |= ZCIEXACT; 3852 zflg &= ~ZCILOOK; 3853 } 3854 } 3855 3856 /* 3857 * If the source and destination directories are the same, we should 3858 * grab the z_name_lock of that directory only once. 3859 */ 3860 if (sdzp == tdzp) { 3861 zflg |= ZHAVELOCK; 3862 rw_enter(&sdzp->z_name_lock, RW_READER); 3863 } 3864 3865 if (cmp < 0) { 3866 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 3867 ZEXISTS | zflg, NULL, NULL); 3868 terr = zfs_dirent_lock(&tdl, 3869 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3870 } else { 3871 terr = zfs_dirent_lock(&tdl, 3872 tdzp, tnm, &tzp, zflg, NULL, NULL); 3873 serr = zfs_dirent_lock(&sdl, 3874 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 3875 NULL, NULL); 3876 } 3877 3878 if (serr) { 3879 /* 3880 * Source entry invalid or not there. 3881 */ 3882 if (!terr) { 3883 zfs_dirent_unlock(tdl); 3884 if (tzp) 3885 VN_RELE(ZTOV(tzp)); 3886 } 3887 3888 if (sdzp == tdzp) 3889 rw_exit(&sdzp->z_name_lock); 3890 3891 /* 3892 * FreeBSD: In OpenSolaris they only check if rename source is 3893 * ".." here, because "." is handled in their lookup. This is 3894 * not the case for FreeBSD, so we check for "." explicitly. 3895 */ 3896 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0) 3897 serr = SET_ERROR(EINVAL); 3898 ZFS_EXIT(zfsvfs); 3899 return (serr); 3900 } 3901 if (terr) { 3902 zfs_dirent_unlock(sdl); 3903 VN_RELE(ZTOV(szp)); 3904 3905 if (sdzp == tdzp) 3906 rw_exit(&sdzp->z_name_lock); 3907 3908 if (strcmp(tnm, "..") == 0) 3909 terr = SET_ERROR(EINVAL); 3910 ZFS_EXIT(zfsvfs); 3911 return (terr); 3912 } 3913 3914 /* 3915 * Must have write access at the source to remove the old entry 3916 * and write access at the target to create the new entry. 3917 * Note that if target and source are the same, this can be 3918 * done in a single check. 3919 */ 3920 3921 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3922 goto out; 3923 3924 if (ZTOV(szp)->v_type == VDIR) { 3925 /* 3926 * Check to make sure rename is valid. 3927 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3928 */ 3929 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3930 goto out; 3931 } 3932 3933 /* 3934 * Does target exist? 3935 */ 3936 if (tzp) { 3937 /* 3938 * Source and target must be the same type. 3939 */ 3940 if (ZTOV(szp)->v_type == VDIR) { 3941 if (ZTOV(tzp)->v_type != VDIR) { 3942 error = SET_ERROR(ENOTDIR); 3943 goto out; 3944 } 3945 } else { 3946 if (ZTOV(tzp)->v_type == VDIR) { 3947 error = SET_ERROR(EISDIR); 3948 goto out; 3949 } 3950 } 3951 /* 3952 * POSIX dictates that when the source and target 3953 * entries refer to the same file object, rename 3954 * must do nothing and exit without error. 3955 */ 3956 if (szp->z_id == tzp->z_id) { 3957 error = 0; 3958 goto out; 3959 } 3960 } 3961 3962 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3963 if (tzp) 3964 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 3965 3966 /* 3967 * notify the target directory if it is not the same 3968 * as source directory. 3969 */ 3970 if (tdvp != sdvp) { 3971 vnevent_rename_dest_dir(tdvp, ct); 3972 } 3973 3974 tx = dmu_tx_create(zfsvfs->z_os); 3975 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 3976 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 3977 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 3978 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 3979 if (sdzp != tdzp) { 3980 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 3981 zfs_sa_upgrade_txholds(tx, tdzp); 3982 } 3983 if (tzp) { 3984 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 3985 zfs_sa_upgrade_txholds(tx, tzp); 3986 } 3987 3988 zfs_sa_upgrade_txholds(tx, szp); 3989 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3990 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 3991 if (error) { 3992 if (zl != NULL) 3993 zfs_rename_unlock(&zl); 3994 zfs_dirent_unlock(sdl); 3995 zfs_dirent_unlock(tdl); 3996 3997 if (sdzp == tdzp) 3998 rw_exit(&sdzp->z_name_lock); 3999 4000 VN_RELE(ZTOV(szp)); 4001 if (tzp) 4002 VN_RELE(ZTOV(tzp)); 4003 if (error == ERESTART) { 4004 waited = B_TRUE; 4005 dmu_tx_wait(tx); 4006 dmu_tx_abort(tx); 4007 goto top; 4008 } 4009 dmu_tx_abort(tx); 4010 ZFS_EXIT(zfsvfs); 4011 return (error); 4012 } 4013 4014 if (tzp) /* Attempt to remove the existing target */ 4015 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 4016 4017 if (error == 0) { 4018 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 4019 if (error == 0) { 4020 szp->z_pflags |= ZFS_AV_MODIFIED; 4021 4022 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 4023 (void *)&szp->z_pflags, sizeof (uint64_t), tx); 4024 ASSERT0(error); 4025 4026 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 4027 if (error == 0) { 4028 zfs_log_rename(zilog, tx, TX_RENAME | 4029 (flags & FIGNORECASE ? TX_CI : 0), sdzp, 4030 sdl->dl_name, tdzp, tdl->dl_name, szp); 4031 4032 /* 4033 * Update path information for the target vnode 4034 */ 4035 vn_renamepath(tdvp, ZTOV(szp), tnm, 4036 strlen(tnm)); 4037 } else { 4038 /* 4039 * At this point, we have successfully created 4040 * the target name, but have failed to remove 4041 * the source name. Since the create was done 4042 * with the ZRENAMING flag, there are 4043 * complications; for one, the link count is 4044 * wrong. The easiest way to deal with this 4045 * is to remove the newly created target, and 4046 * return the original error. This must 4047 * succeed; fortunately, it is very unlikely to 4048 * fail, since we just created it. 4049 */ 4050 VERIFY3U(zfs_link_destroy(tdl, szp, tx, 4051 ZRENAMING, NULL), ==, 0); 4052 } 4053 } 4054#ifdef FREEBSD_NAMECACHE 4055 if (error == 0) { 4056 cache_purge(sdvp); 4057 cache_purge(tdvp); 4058 cache_purge(ZTOV(szp)); 4059 if (tzp) 4060 cache_purge(ZTOV(tzp)); 4061 } 4062#endif 4063 } 4064 4065 dmu_tx_commit(tx); 4066out: 4067 if (zl != NULL) 4068 zfs_rename_unlock(&zl); 4069 4070 zfs_dirent_unlock(sdl); 4071 zfs_dirent_unlock(tdl); 4072 4073 if (sdzp == tdzp) 4074 rw_exit(&sdzp->z_name_lock); 4075 4076 4077 VN_RELE(ZTOV(szp)); 4078 if (tzp) 4079 VN_RELE(ZTOV(tzp)); 4080 4081 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4082 zil_commit(zilog, 0); 4083 4084 ZFS_EXIT(zfsvfs); 4085 4086 return (error); 4087} 4088 4089/* 4090 * Insert the indicated symbolic reference entry into the directory. 4091 * 4092 * IN: dvp - Directory to contain new symbolic link. 4093 * link - Name for new symlink entry. 4094 * vap - Attributes of new entry. 4095 * cr - credentials of caller. 4096 * ct - caller context 4097 * flags - case flags 4098 * 4099 * RETURN: 0 on success, error code on failure. 4100 * 4101 * Timestamps: 4102 * dvp - ctime|mtime updated 4103 */ 4104/*ARGSUSED*/ 4105static int 4106zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link, 4107 cred_t *cr, kthread_t *td) 4108{ 4109 znode_t *zp, *dzp = VTOZ(dvp); 4110 zfs_dirlock_t *dl; 4111 dmu_tx_t *tx; 4112 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4113 zilog_t *zilog; 4114 uint64_t len = strlen(link); 4115 int error; 4116 int zflg = ZNEW; 4117 zfs_acl_ids_t acl_ids; 4118 boolean_t fuid_dirtied; 4119 uint64_t txtype = TX_SYMLINK; 4120 boolean_t waited = B_FALSE; 4121 int flags = 0; 4122 4123 ASSERT(vap->va_type == VLNK); 4124 4125 ZFS_ENTER(zfsvfs); 4126 ZFS_VERIFY_ZP(dzp); 4127 zilog = zfsvfs->z_log; 4128 4129 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 4130 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 4131 ZFS_EXIT(zfsvfs); 4132 return (SET_ERROR(EILSEQ)); 4133 } 4134 if (flags & FIGNORECASE) 4135 zflg |= ZCILOOK; 4136 4137 if (len > MAXPATHLEN) { 4138 ZFS_EXIT(zfsvfs); 4139 return (SET_ERROR(ENAMETOOLONG)); 4140 } 4141 4142 if ((error = zfs_acl_ids_create(dzp, 0, 4143 vap, cr, NULL, &acl_ids)) != 0) { 4144 ZFS_EXIT(zfsvfs); 4145 return (error); 4146 } 4147 4148 getnewvnode_reserve(1); 4149 4150top: 4151 /* 4152 * Attempt to lock directory; fail if entry already exists. 4153 */ 4154 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 4155 if (error) { 4156 zfs_acl_ids_free(&acl_ids); 4157 getnewvnode_drop_reserve(); 4158 ZFS_EXIT(zfsvfs); 4159 return (error); 4160 } 4161 4162 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 4163 zfs_acl_ids_free(&acl_ids); 4164 zfs_dirent_unlock(dl); 4165 getnewvnode_drop_reserve(); 4166 ZFS_EXIT(zfsvfs); 4167 return (error); 4168 } 4169 4170 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 4171 zfs_acl_ids_free(&acl_ids); 4172 zfs_dirent_unlock(dl); 4173 getnewvnode_drop_reserve(); 4174 ZFS_EXIT(zfsvfs); 4175 return (SET_ERROR(EDQUOT)); 4176 } 4177 tx = dmu_tx_create(zfsvfs->z_os); 4178 fuid_dirtied = zfsvfs->z_fuid_dirty; 4179 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 4180 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 4181 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 4182 ZFS_SA_BASE_ATTR_SIZE + len); 4183 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 4184 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 4185 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 4186 acl_ids.z_aclp->z_acl_bytes); 4187 } 4188 if (fuid_dirtied) 4189 zfs_fuid_txhold(zfsvfs, tx); 4190 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 4191 if (error) { 4192 zfs_dirent_unlock(dl); 4193 if (error == ERESTART) { 4194 waited = B_TRUE; 4195 dmu_tx_wait(tx); 4196 dmu_tx_abort(tx); 4197 goto top; 4198 } 4199 zfs_acl_ids_free(&acl_ids); 4200 dmu_tx_abort(tx); 4201 getnewvnode_drop_reserve(); 4202 ZFS_EXIT(zfsvfs); 4203 return (error); 4204 } 4205 4206 /* 4207 * Create a new object for the symlink. 4208 * for version 4 ZPL datsets the symlink will be an SA attribute 4209 */ 4210 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 4211 4212 if (fuid_dirtied) 4213 zfs_fuid_sync(zfsvfs, tx); 4214 4215 mutex_enter(&zp->z_lock); 4216 if (zp->z_is_sa) 4217 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 4218 link, len, tx); 4219 else 4220 zfs_sa_symlink(zp, link, len, tx); 4221 mutex_exit(&zp->z_lock); 4222 4223 zp->z_size = len; 4224 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 4225 &zp->z_size, sizeof (zp->z_size), tx); 4226 /* 4227 * Insert the new object into the directory. 4228 */ 4229 (void) zfs_link_create(dl, zp, tx, ZNEW); 4230 4231 if (flags & FIGNORECASE) 4232 txtype |= TX_CI; 4233 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 4234 *vpp = ZTOV(zp); 4235 4236 zfs_acl_ids_free(&acl_ids); 4237 4238 dmu_tx_commit(tx); 4239 4240 getnewvnode_drop_reserve(); 4241 4242 zfs_dirent_unlock(dl); 4243 4244 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4245 zil_commit(zilog, 0); 4246 4247 ZFS_EXIT(zfsvfs); 4248 return (error); 4249} 4250 4251/* 4252 * Return, in the buffer contained in the provided uio structure, 4253 * the symbolic path referred to by vp. 4254 * 4255 * IN: vp - vnode of symbolic link. 4256 * uio - structure to contain the link path. 4257 * cr - credentials of caller. 4258 * ct - caller context 4259 * 4260 * OUT: uio - structure containing the link path. 4261 * 4262 * RETURN: 0 on success, error code on failure. 4263 * 4264 * Timestamps: 4265 * vp - atime updated 4266 */ 4267/* ARGSUSED */ 4268static int 4269zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 4270{ 4271 znode_t *zp = VTOZ(vp); 4272 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4273 int error; 4274 4275 ZFS_ENTER(zfsvfs); 4276 ZFS_VERIFY_ZP(zp); 4277 4278 mutex_enter(&zp->z_lock); 4279 if (zp->z_is_sa) 4280 error = sa_lookup_uio(zp->z_sa_hdl, 4281 SA_ZPL_SYMLINK(zfsvfs), uio); 4282 else 4283 error = zfs_sa_readlink(zp, uio); 4284 mutex_exit(&zp->z_lock); 4285 4286 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4287 4288 ZFS_EXIT(zfsvfs); 4289 return (error); 4290} 4291 4292/* 4293 * Insert a new entry into directory tdvp referencing svp. 4294 * 4295 * IN: tdvp - Directory to contain new entry. 4296 * svp - vnode of new entry. 4297 * name - name of new entry. 4298 * cr - credentials of caller. 4299 * ct - caller context 4300 * 4301 * RETURN: 0 on success, error code on failure. 4302 * 4303 * Timestamps: 4304 * tdvp - ctime|mtime updated 4305 * svp - ctime updated 4306 */ 4307/* ARGSUSED */ 4308static int 4309zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 4310 caller_context_t *ct, int flags) 4311{ 4312 znode_t *dzp = VTOZ(tdvp); 4313 znode_t *tzp, *szp; 4314 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4315 zilog_t *zilog; 4316 zfs_dirlock_t *dl; 4317 dmu_tx_t *tx; 4318 vnode_t *realvp; 4319 int error; 4320 int zf = ZNEW; 4321 uint64_t parent; 4322 uid_t owner; 4323 boolean_t waited = B_FALSE; 4324 4325 ASSERT(tdvp->v_type == VDIR); 4326 4327 ZFS_ENTER(zfsvfs); 4328 ZFS_VERIFY_ZP(dzp); 4329 zilog = zfsvfs->z_log; 4330 4331 if (VOP_REALVP(svp, &realvp, ct) == 0) 4332 svp = realvp; 4333 4334 /* 4335 * POSIX dictates that we return EPERM here. 4336 * Better choices include ENOTSUP or EISDIR. 4337 */ 4338 if (svp->v_type == VDIR) { 4339 ZFS_EXIT(zfsvfs); 4340 return (SET_ERROR(EPERM)); 4341 } 4342 4343 szp = VTOZ(svp); 4344 ZFS_VERIFY_ZP(szp); 4345 4346 /* 4347 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the 4348 * ctldir appear to have the same v_vfsp. 4349 */ 4350 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) { 4351 ZFS_EXIT(zfsvfs); 4352 return (SET_ERROR(EXDEV)); 4353 } 4354 4355 /* Prevent links to .zfs/shares files */ 4356 4357 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 4358 &parent, sizeof (uint64_t))) != 0) { 4359 ZFS_EXIT(zfsvfs); 4360 return (error); 4361 } 4362 if (parent == zfsvfs->z_shares_dir) { 4363 ZFS_EXIT(zfsvfs); 4364 return (SET_ERROR(EPERM)); 4365 } 4366 4367 if (zfsvfs->z_utf8 && u8_validate(name, 4368 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 4369 ZFS_EXIT(zfsvfs); 4370 return (SET_ERROR(EILSEQ)); 4371 } 4372 if (flags & FIGNORECASE) 4373 zf |= ZCILOOK; 4374 4375 /* 4376 * We do not support links between attributes and non-attributes 4377 * because of the potential security risk of creating links 4378 * into "normal" file space in order to circumvent restrictions 4379 * imposed in attribute space. 4380 */ 4381 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 4382 ZFS_EXIT(zfsvfs); 4383 return (SET_ERROR(EINVAL)); 4384 } 4385 4386 4387 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER); 4388 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) { 4389 ZFS_EXIT(zfsvfs); 4390 return (SET_ERROR(EPERM)); 4391 } 4392 4393 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 4394 ZFS_EXIT(zfsvfs); 4395 return (error); 4396 } 4397 4398top: 4399 /* 4400 * Attempt to lock directory; fail if entry already exists. 4401 */ 4402 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 4403 if (error) { 4404 ZFS_EXIT(zfsvfs); 4405 return (error); 4406 } 4407 4408 tx = dmu_tx_create(zfsvfs->z_os); 4409 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 4410 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 4411 zfs_sa_upgrade_txholds(tx, szp); 4412 zfs_sa_upgrade_txholds(tx, dzp); 4413 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 4414 if (error) { 4415 zfs_dirent_unlock(dl); 4416 if (error == ERESTART) { 4417 waited = B_TRUE; 4418 dmu_tx_wait(tx); 4419 dmu_tx_abort(tx); 4420 goto top; 4421 } 4422 dmu_tx_abort(tx); 4423 ZFS_EXIT(zfsvfs); 4424 return (error); 4425 } 4426 4427 error = zfs_link_create(dl, szp, tx, 0); 4428 4429 if (error == 0) { 4430 uint64_t txtype = TX_LINK; 4431 if (flags & FIGNORECASE) 4432 txtype |= TX_CI; 4433 zfs_log_link(zilog, tx, txtype, dzp, szp, name); 4434 } 4435 4436 dmu_tx_commit(tx); 4437 4438 zfs_dirent_unlock(dl); 4439 4440 if (error == 0) { 4441 vnevent_link(svp, ct); 4442 } 4443 4444 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4445 zil_commit(zilog, 0); 4446 4447 ZFS_EXIT(zfsvfs); 4448 return (error); 4449} 4450 4451#ifdef sun 4452/* 4453 * zfs_null_putapage() is used when the file system has been force 4454 * unmounted. It just drops the pages. 4455 */ 4456/* ARGSUSED */ 4457static int 4458zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4459 size_t *lenp, int flags, cred_t *cr) 4460{ 4461 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 4462 return (0); 4463} 4464 4465/* 4466 * Push a page out to disk, klustering if possible. 4467 * 4468 * IN: vp - file to push page to. 4469 * pp - page to push. 4470 * flags - additional flags. 4471 * cr - credentials of caller. 4472 * 4473 * OUT: offp - start of range pushed. 4474 * lenp - len of range pushed. 4475 * 4476 * RETURN: 0 on success, error code on failure. 4477 * 4478 * NOTE: callers must have locked the page to be pushed. On 4479 * exit, the page (and all other pages in the kluster) must be 4480 * unlocked. 4481 */ 4482/* ARGSUSED */ 4483static int 4484zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4485 size_t *lenp, int flags, cred_t *cr) 4486{ 4487 znode_t *zp = VTOZ(vp); 4488 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4489 dmu_tx_t *tx; 4490 u_offset_t off, koff; 4491 size_t len, klen; 4492 int err; 4493 4494 off = pp->p_offset; 4495 len = PAGESIZE; 4496 /* 4497 * If our blocksize is bigger than the page size, try to kluster 4498 * multiple pages so that we write a full block (thus avoiding 4499 * a read-modify-write). 4500 */ 4501 if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 4502 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 4503 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 4504 ASSERT(koff <= zp->z_size); 4505 if (koff + klen > zp->z_size) 4506 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 4507 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 4508 } 4509 ASSERT3U(btop(len), ==, btopr(len)); 4510 4511 /* 4512 * Can't push pages past end-of-file. 4513 */ 4514 if (off >= zp->z_size) { 4515 /* ignore all pages */ 4516 err = 0; 4517 goto out; 4518 } else if (off + len > zp->z_size) { 4519 int npages = btopr(zp->z_size - off); 4520 page_t *trunc; 4521 4522 page_list_break(&pp, &trunc, npages); 4523 /* ignore pages past end of file */ 4524 if (trunc) 4525 pvn_write_done(trunc, flags); 4526 len = zp->z_size - off; 4527 } 4528 4529 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 4530 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 4531 err = SET_ERROR(EDQUOT); 4532 goto out; 4533 } 4534 tx = dmu_tx_create(zfsvfs->z_os); 4535 dmu_tx_hold_write(tx, zp->z_id, off, len); 4536 4537 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4538 zfs_sa_upgrade_txholds(tx, zp); 4539 err = dmu_tx_assign(tx, TXG_WAIT); 4540 if (err != 0) { 4541 dmu_tx_abort(tx); 4542 goto out; 4543 } 4544 4545 if (zp->z_blksz <= PAGESIZE) { 4546 caddr_t va = zfs_map_page(pp, S_READ); 4547 ASSERT3U(len, <=, PAGESIZE); 4548 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 4549 zfs_unmap_page(pp, va); 4550 } else { 4551 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 4552 } 4553 4554 if (err == 0) { 4555 uint64_t mtime[2], ctime[2]; 4556 sa_bulk_attr_t bulk[3]; 4557 int count = 0; 4558 4559 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 4560 &mtime, 16); 4561 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 4562 &ctime, 16); 4563 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 4564 &zp->z_pflags, 8); 4565 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 4566 B_TRUE); 4567 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 4568 } 4569 dmu_tx_commit(tx); 4570 4571out: 4572 pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4573 if (offp) 4574 *offp = off; 4575 if (lenp) 4576 *lenp = len; 4577 4578 return (err); 4579} 4580 4581/* 4582 * Copy the portion of the file indicated from pages into the file. 4583 * The pages are stored in a page list attached to the files vnode. 4584 * 4585 * IN: vp - vnode of file to push page data to. 4586 * off - position in file to put data. 4587 * len - amount of data to write. 4588 * flags - flags to control the operation. 4589 * cr - credentials of caller. 4590 * ct - caller context. 4591 * 4592 * RETURN: 0 on success, error code on failure. 4593 * 4594 * Timestamps: 4595 * vp - ctime|mtime updated 4596 */ 4597/*ARGSUSED*/ 4598static int 4599zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 4600 caller_context_t *ct) 4601{ 4602 znode_t *zp = VTOZ(vp); 4603 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4604 page_t *pp; 4605 size_t io_len; 4606 u_offset_t io_off; 4607 uint_t blksz; 4608 rl_t *rl; 4609 int error = 0; 4610 4611 ZFS_ENTER(zfsvfs); 4612 ZFS_VERIFY_ZP(zp); 4613 4614 /* 4615 * Align this request to the file block size in case we kluster. 4616 * XXX - this can result in pretty aggresive locking, which can 4617 * impact simultanious read/write access. One option might be 4618 * to break up long requests (len == 0) into block-by-block 4619 * operations to get narrower locking. 4620 */ 4621 blksz = zp->z_blksz; 4622 if (ISP2(blksz)) 4623 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 4624 else 4625 io_off = 0; 4626 if (len > 0 && ISP2(blksz)) 4627 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 4628 else 4629 io_len = 0; 4630 4631 if (io_len == 0) { 4632 /* 4633 * Search the entire vp list for pages >= io_off. 4634 */ 4635 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 4636 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 4637 goto out; 4638 } 4639 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 4640 4641 if (off > zp->z_size) { 4642 /* past end of file */ 4643 zfs_range_unlock(rl); 4644 ZFS_EXIT(zfsvfs); 4645 return (0); 4646 } 4647 4648 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 4649 4650 for (off = io_off; io_off < off + len; io_off += io_len) { 4651 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 4652 pp = page_lookup(vp, io_off, 4653 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4654 } else { 4655 pp = page_lookup_nowait(vp, io_off, 4656 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4657 } 4658 4659 if (pp != NULL && pvn_getdirty(pp, flags)) { 4660 int err; 4661 4662 /* 4663 * Found a dirty page to push 4664 */ 4665 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 4666 if (err) 4667 error = err; 4668 } else { 4669 io_len = PAGESIZE; 4670 } 4671 } 4672out: 4673 zfs_range_unlock(rl); 4674 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4675 zil_commit(zfsvfs->z_log, zp->z_id); 4676 ZFS_EXIT(zfsvfs); 4677 return (error); 4678} 4679#endif /* sun */ 4680 4681/*ARGSUSED*/ 4682void 4683zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4684{ 4685 znode_t *zp = VTOZ(vp); 4686 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4687 int error; 4688 4689 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 4690 if (zp->z_sa_hdl == NULL) { 4691 /* 4692 * The fs has been unmounted, or we did a 4693 * suspend/resume and this file no longer exists. 4694 */ 4695 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4696 vrecycle(vp); 4697 return; 4698 } 4699 4700 mutex_enter(&zp->z_lock); 4701 if (zp->z_unlinked) { 4702 /* 4703 * Fast path to recycle a vnode of a removed file. 4704 */ 4705 mutex_exit(&zp->z_lock); 4706 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4707 vrecycle(vp); 4708 return; 4709 } 4710 mutex_exit(&zp->z_lock); 4711 4712 if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4713 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4714 4715 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4716 zfs_sa_upgrade_txholds(tx, zp); 4717 error = dmu_tx_assign(tx, TXG_WAIT); 4718 if (error) { 4719 dmu_tx_abort(tx); 4720 } else { 4721 mutex_enter(&zp->z_lock); 4722 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 4723 (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4724 zp->z_atime_dirty = 0; 4725 mutex_exit(&zp->z_lock); 4726 dmu_tx_commit(tx); 4727 } 4728 } 4729 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4730} 4731 4732#ifdef sun 4733/* 4734 * Bounds-check the seek operation. 4735 * 4736 * IN: vp - vnode seeking within 4737 * ooff - old file offset 4738 * noffp - pointer to new file offset 4739 * ct - caller context 4740 * 4741 * RETURN: 0 on success, EINVAL if new offset invalid. 4742 */ 4743/* ARGSUSED */ 4744static int 4745zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 4746 caller_context_t *ct) 4747{ 4748 if (vp->v_type == VDIR) 4749 return (0); 4750 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4751} 4752 4753/* 4754 * Pre-filter the generic locking function to trap attempts to place 4755 * a mandatory lock on a memory mapped file. 4756 */ 4757static int 4758zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 4759 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4760{ 4761 znode_t *zp = VTOZ(vp); 4762 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4763 4764 ZFS_ENTER(zfsvfs); 4765 ZFS_VERIFY_ZP(zp); 4766 4767 /* 4768 * We are following the UFS semantics with respect to mapcnt 4769 * here: If we see that the file is mapped already, then we will 4770 * return an error, but we don't worry about races between this 4771 * function and zfs_map(). 4772 */ 4773 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4774 ZFS_EXIT(zfsvfs); 4775 return (SET_ERROR(EAGAIN)); 4776 } 4777 ZFS_EXIT(zfsvfs); 4778 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4779} 4780 4781/* 4782 * If we can't find a page in the cache, we will create a new page 4783 * and fill it with file data. For efficiency, we may try to fill 4784 * multiple pages at once (klustering) to fill up the supplied page 4785 * list. Note that the pages to be filled are held with an exclusive 4786 * lock to prevent access by other threads while they are being filled. 4787 */ 4788static int 4789zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4790 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4791{ 4792 znode_t *zp = VTOZ(vp); 4793 page_t *pp, *cur_pp; 4794 objset_t *os = zp->z_zfsvfs->z_os; 4795 u_offset_t io_off, total; 4796 size_t io_len; 4797 int err; 4798 4799 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 4800 /* 4801 * We only have a single page, don't bother klustering 4802 */ 4803 io_off = off; 4804 io_len = PAGESIZE; 4805 pp = page_create_va(vp, io_off, io_len, 4806 PG_EXCL | PG_WAIT, seg, addr); 4807 } else { 4808 /* 4809 * Try to find enough pages to fill the page list 4810 */ 4811 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 4812 &io_len, off, plsz, 0); 4813 } 4814 if (pp == NULL) { 4815 /* 4816 * The page already exists, nothing to do here. 4817 */ 4818 *pl = NULL; 4819 return (0); 4820 } 4821 4822 /* 4823 * Fill the pages in the kluster. 4824 */ 4825 cur_pp = pp; 4826 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 4827 caddr_t va; 4828 4829 ASSERT3U(io_off, ==, cur_pp->p_offset); 4830 va = zfs_map_page(cur_pp, S_WRITE); 4831 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 4832 DMU_READ_PREFETCH); 4833 zfs_unmap_page(cur_pp, va); 4834 if (err) { 4835 /* On error, toss the entire kluster */ 4836 pvn_read_done(pp, B_ERROR); 4837 /* convert checksum errors into IO errors */ 4838 if (err == ECKSUM) 4839 err = SET_ERROR(EIO); 4840 return (err); 4841 } 4842 cur_pp = cur_pp->p_next; 4843 } 4844 4845 /* 4846 * Fill in the page list array from the kluster starting 4847 * from the desired offset `off'. 4848 * NOTE: the page list will always be null terminated. 4849 */ 4850 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 4851 ASSERT(pl == NULL || (*pl)->p_offset == off); 4852 4853 return (0); 4854} 4855 4856/* 4857 * Return pointers to the pages for the file region [off, off + len] 4858 * in the pl array. If plsz is greater than len, this function may 4859 * also return page pointers from after the specified region 4860 * (i.e. the region [off, off + plsz]). These additional pages are 4861 * only returned if they are already in the cache, or were created as 4862 * part of a klustered read. 4863 * 4864 * IN: vp - vnode of file to get data from. 4865 * off - position in file to get data from. 4866 * len - amount of data to retrieve. 4867 * plsz - length of provided page list. 4868 * seg - segment to obtain pages for. 4869 * addr - virtual address of fault. 4870 * rw - mode of created pages. 4871 * cr - credentials of caller. 4872 * ct - caller context. 4873 * 4874 * OUT: protp - protection mode of created pages. 4875 * pl - list of pages created. 4876 * 4877 * RETURN: 0 on success, error code on failure. 4878 * 4879 * Timestamps: 4880 * vp - atime updated 4881 */ 4882/* ARGSUSED */ 4883static int 4884zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4885 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 4886 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4887{ 4888 znode_t *zp = VTOZ(vp); 4889 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4890 page_t **pl0 = pl; 4891 int err = 0; 4892 4893 /* we do our own caching, faultahead is unnecessary */ 4894 if (pl == NULL) 4895 return (0); 4896 else if (len > plsz) 4897 len = plsz; 4898 else 4899 len = P2ROUNDUP(len, PAGESIZE); 4900 ASSERT(plsz >= len); 4901 4902 ZFS_ENTER(zfsvfs); 4903 ZFS_VERIFY_ZP(zp); 4904 4905 if (protp) 4906 *protp = PROT_ALL; 4907 4908 /* 4909 * Loop through the requested range [off, off + len) looking 4910 * for pages. If we don't find a page, we will need to create 4911 * a new page and fill it with data from the file. 4912 */ 4913 while (len > 0) { 4914 if (*pl = page_lookup(vp, off, SE_SHARED)) 4915 *(pl+1) = NULL; 4916 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 4917 goto out; 4918 while (*pl) { 4919 ASSERT3U((*pl)->p_offset, ==, off); 4920 off += PAGESIZE; 4921 addr += PAGESIZE; 4922 if (len > 0) { 4923 ASSERT3U(len, >=, PAGESIZE); 4924 len -= PAGESIZE; 4925 } 4926 ASSERT3U(plsz, >=, PAGESIZE); 4927 plsz -= PAGESIZE; 4928 pl++; 4929 } 4930 } 4931 4932 /* 4933 * Fill out the page array with any pages already in the cache. 4934 */ 4935 while (plsz > 0 && 4936 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 4937 off += PAGESIZE; 4938 plsz -= PAGESIZE; 4939 } 4940out: 4941 if (err) { 4942 /* 4943 * Release any pages we have previously locked. 4944 */ 4945 while (pl > pl0) 4946 page_unlock(*--pl); 4947 } else { 4948 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4949 } 4950 4951 *pl = NULL; 4952 4953 ZFS_EXIT(zfsvfs); 4954 return (err); 4955} 4956 4957/* 4958 * Request a memory map for a section of a file. This code interacts 4959 * with common code and the VM system as follows: 4960 * 4961 * - common code calls mmap(), which ends up in smmap_common() 4962 * - this calls VOP_MAP(), which takes you into (say) zfs 4963 * - zfs_map() calls as_map(), passing segvn_create() as the callback 4964 * - segvn_create() creates the new segment and calls VOP_ADDMAP() 4965 * - zfs_addmap() updates z_mapcnt 4966 */ 4967/*ARGSUSED*/ 4968static int 4969zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 4970 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 4971 caller_context_t *ct) 4972{ 4973 znode_t *zp = VTOZ(vp); 4974 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4975 segvn_crargs_t vn_a; 4976 int error; 4977 4978 ZFS_ENTER(zfsvfs); 4979 ZFS_VERIFY_ZP(zp); 4980 4981 if ((prot & PROT_WRITE) && (zp->z_pflags & 4982 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 4983 ZFS_EXIT(zfsvfs); 4984 return (SET_ERROR(EPERM)); 4985 } 4986 4987 if ((prot & (PROT_READ | PROT_EXEC)) && 4988 (zp->z_pflags & ZFS_AV_QUARANTINED)) { 4989 ZFS_EXIT(zfsvfs); 4990 return (SET_ERROR(EACCES)); 4991 } 4992 4993 if (vp->v_flag & VNOMAP) { 4994 ZFS_EXIT(zfsvfs); 4995 return (SET_ERROR(ENOSYS)); 4996 } 4997 4998 if (off < 0 || len > MAXOFFSET_T - off) { 4999 ZFS_EXIT(zfsvfs); 5000 return (SET_ERROR(ENXIO)); 5001 } 5002 5003 if (vp->v_type != VREG) { 5004 ZFS_EXIT(zfsvfs); 5005 return (SET_ERROR(ENODEV)); 5006 } 5007 5008 /* 5009 * If file is locked, disallow mapping. 5010 */ 5011 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 5012 ZFS_EXIT(zfsvfs); 5013 return (SET_ERROR(EAGAIN)); 5014 } 5015 5016 as_rangelock(as); 5017 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 5018 if (error != 0) { 5019 as_rangeunlock(as); 5020 ZFS_EXIT(zfsvfs); 5021 return (error); 5022 } 5023 5024 vn_a.vp = vp; 5025 vn_a.offset = (u_offset_t)off; 5026 vn_a.type = flags & MAP_TYPE; 5027 vn_a.prot = prot; 5028 vn_a.maxprot = maxprot; 5029 vn_a.cred = cr; 5030 vn_a.amp = NULL; 5031 vn_a.flags = flags & ~MAP_TYPE; 5032 vn_a.szc = 0; 5033 vn_a.lgrp_mem_policy_flags = 0; 5034 5035 error = as_map(as, *addrp, len, segvn_create, &vn_a); 5036 5037 as_rangeunlock(as); 5038 ZFS_EXIT(zfsvfs); 5039 return (error); 5040} 5041 5042/* ARGSUSED */ 5043static int 5044zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 5045 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 5046 caller_context_t *ct) 5047{ 5048 uint64_t pages = btopr(len); 5049 5050 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 5051 return (0); 5052} 5053 5054/* 5055 * The reason we push dirty pages as part of zfs_delmap() is so that we get a 5056 * more accurate mtime for the associated file. Since we don't have a way of 5057 * detecting when the data was actually modified, we have to resort to 5058 * heuristics. If an explicit msync() is done, then we mark the mtime when the 5059 * last page is pushed. The problem occurs when the msync() call is omitted, 5060 * which by far the most common case: 5061 * 5062 * open() 5063 * mmap() 5064 * <modify memory> 5065 * munmap() 5066 * close() 5067 * <time lapse> 5068 * putpage() via fsflush 5069 * 5070 * If we wait until fsflush to come along, we can have a modification time that 5071 * is some arbitrary point in the future. In order to prevent this in the 5072 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 5073 * torn down. 5074 */ 5075/* ARGSUSED */ 5076static int 5077zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 5078 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 5079 caller_context_t *ct) 5080{ 5081 uint64_t pages = btopr(len); 5082 5083 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 5084 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 5085 5086 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 5087 vn_has_cached_data(vp)) 5088 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 5089 5090 return (0); 5091} 5092 5093/* 5094 * Free or allocate space in a file. Currently, this function only 5095 * supports the `F_FREESP' command. However, this command is somewhat 5096 * misnamed, as its functionality includes the ability to allocate as 5097 * well as free space. 5098 * 5099 * IN: vp - vnode of file to free data in. 5100 * cmd - action to take (only F_FREESP supported). 5101 * bfp - section of file to free/alloc. 5102 * flag - current file open mode flags. 5103 * offset - current file offset. 5104 * cr - credentials of caller [UNUSED]. 5105 * ct - caller context. 5106 * 5107 * RETURN: 0 on success, error code on failure. 5108 * 5109 * Timestamps: 5110 * vp - ctime|mtime updated 5111 */ 5112/* ARGSUSED */ 5113static int 5114zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 5115 offset_t offset, cred_t *cr, caller_context_t *ct) 5116{ 5117 znode_t *zp = VTOZ(vp); 5118 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5119 uint64_t off, len; 5120 int error; 5121 5122 ZFS_ENTER(zfsvfs); 5123 ZFS_VERIFY_ZP(zp); 5124 5125 if (cmd != F_FREESP) { 5126 ZFS_EXIT(zfsvfs); 5127 return (SET_ERROR(EINVAL)); 5128 } 5129 5130 if (error = convoff(vp, bfp, 0, offset)) { 5131 ZFS_EXIT(zfsvfs); 5132 return (error); 5133 } 5134 5135 if (bfp->l_len < 0) { 5136 ZFS_EXIT(zfsvfs); 5137 return (SET_ERROR(EINVAL)); 5138 } 5139 5140 off = bfp->l_start; 5141 len = bfp->l_len; /* 0 means from off to end of file */ 5142 5143 error = zfs_freesp(zp, off, len, flag, TRUE); 5144 5145 ZFS_EXIT(zfsvfs); 5146 return (error); 5147} 5148#endif /* sun */ 5149 5150CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid)); 5151CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid)); 5152 5153/*ARGSUSED*/ 5154static int 5155zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 5156{ 5157 znode_t *zp = VTOZ(vp); 5158 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5159 uint32_t gen; 5160 uint64_t gen64; 5161 uint64_t object = zp->z_id; 5162 zfid_short_t *zfid; 5163 int size, i, error; 5164 5165 ZFS_ENTER(zfsvfs); 5166 ZFS_VERIFY_ZP(zp); 5167 5168 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 5169 &gen64, sizeof (uint64_t))) != 0) { 5170 ZFS_EXIT(zfsvfs); 5171 return (error); 5172 } 5173 5174 gen = (uint32_t)gen64; 5175 5176 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 5177 5178#ifdef illumos 5179 if (fidp->fid_len < size) { 5180 fidp->fid_len = size; 5181 ZFS_EXIT(zfsvfs); 5182 return (SET_ERROR(ENOSPC)); 5183 } 5184#else 5185 fidp->fid_len = size; 5186#endif 5187 5188 zfid = (zfid_short_t *)fidp; 5189 5190 zfid->zf_len = size; 5191 5192 for (i = 0; i < sizeof (zfid->zf_object); i++) 5193 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 5194 5195 /* Must have a non-zero generation number to distinguish from .zfs */ 5196 if (gen == 0) 5197 gen = 1; 5198 for (i = 0; i < sizeof (zfid->zf_gen); i++) 5199 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 5200 5201 if (size == LONG_FID_LEN) { 5202 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 5203 zfid_long_t *zlfid; 5204 5205 zlfid = (zfid_long_t *)fidp; 5206 5207 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 5208 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 5209 5210 /* XXX - this should be the generation number for the objset */ 5211 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 5212 zlfid->zf_setgen[i] = 0; 5213 } 5214 5215 ZFS_EXIT(zfsvfs); 5216 return (0); 5217} 5218 5219static int 5220zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 5221 caller_context_t *ct) 5222{ 5223 znode_t *zp, *xzp; 5224 zfsvfs_t *zfsvfs; 5225 zfs_dirlock_t *dl; 5226 int error; 5227 5228 switch (cmd) { 5229 case _PC_LINK_MAX: 5230 *valp = INT_MAX; 5231 return (0); 5232 5233 case _PC_FILESIZEBITS: 5234 *valp = 64; 5235 return (0); 5236#ifdef sun 5237 case _PC_XATTR_EXISTS: 5238 zp = VTOZ(vp); 5239 zfsvfs = zp->z_zfsvfs; 5240 ZFS_ENTER(zfsvfs); 5241 ZFS_VERIFY_ZP(zp); 5242 *valp = 0; 5243 error = zfs_dirent_lock(&dl, zp, "", &xzp, 5244 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 5245 if (error == 0) { 5246 zfs_dirent_unlock(dl); 5247 if (!zfs_dirempty(xzp)) 5248 *valp = 1; 5249 VN_RELE(ZTOV(xzp)); 5250 } else if (error == ENOENT) { 5251 /* 5252 * If there aren't extended attributes, it's the 5253 * same as having zero of them. 5254 */ 5255 error = 0; 5256 } 5257 ZFS_EXIT(zfsvfs); 5258 return (error); 5259 5260 case _PC_SATTR_ENABLED: 5261 case _PC_SATTR_EXISTS: 5262 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 5263 (vp->v_type == VREG || vp->v_type == VDIR); 5264 return (0); 5265 5266 case _PC_ACCESS_FILTERING: 5267 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 5268 vp->v_type == VDIR; 5269 return (0); 5270 5271 case _PC_ACL_ENABLED: 5272 *valp = _ACL_ACE_ENABLED; 5273 return (0); 5274#endif /* sun */ 5275 case _PC_MIN_HOLE_SIZE: 5276 *valp = (int)SPA_MINBLOCKSIZE; 5277 return (0); 5278#ifdef sun 5279 case _PC_TIMESTAMP_RESOLUTION: 5280 /* nanosecond timestamp resolution */ 5281 *valp = 1L; 5282 return (0); 5283#endif /* sun */ 5284 case _PC_ACL_EXTENDED: 5285 *valp = 0; 5286 return (0); 5287 5288 case _PC_ACL_NFS4: 5289 *valp = 1; 5290 return (0); 5291 5292 case _PC_ACL_PATH_MAX: 5293 *valp = ACL_MAX_ENTRIES; 5294 return (0); 5295 5296 default: 5297 return (EOPNOTSUPP); 5298 } 5299} 5300 5301/*ARGSUSED*/ 5302static int 5303zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 5304 caller_context_t *ct) 5305{ 5306 znode_t *zp = VTOZ(vp); 5307 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5308 int error; 5309 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 5310 5311 ZFS_ENTER(zfsvfs); 5312 ZFS_VERIFY_ZP(zp); 5313 error = zfs_getacl(zp, vsecp, skipaclchk, cr); 5314 ZFS_EXIT(zfsvfs); 5315 5316 return (error); 5317} 5318 5319/*ARGSUSED*/ 5320int 5321zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 5322 caller_context_t *ct) 5323{ 5324 znode_t *zp = VTOZ(vp); 5325 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5326 int error; 5327 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 5328 zilog_t *zilog = zfsvfs->z_log; 5329 5330 ZFS_ENTER(zfsvfs); 5331 ZFS_VERIFY_ZP(zp); 5332 5333 error = zfs_setacl(zp, vsecp, skipaclchk, cr); 5334 5335 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 5336 zil_commit(zilog, 0); 5337 5338 ZFS_EXIT(zfsvfs); 5339 return (error); 5340} 5341 5342#ifdef sun 5343/* 5344 * The smallest read we may consider to loan out an arcbuf. 5345 * This must be a power of 2. 5346 */ 5347int zcr_blksz_min = (1 << 10); /* 1K */ 5348/* 5349 * If set to less than the file block size, allow loaning out of an 5350 * arcbuf for a partial block read. This must be a power of 2. 5351 */ 5352int zcr_blksz_max = (1 << 17); /* 128K */ 5353 5354/*ARGSUSED*/ 5355static int 5356zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 5357 caller_context_t *ct) 5358{ 5359 znode_t *zp = VTOZ(vp); 5360 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5361 int max_blksz = zfsvfs->z_max_blksz; 5362 uio_t *uio = &xuio->xu_uio; 5363 ssize_t size = uio->uio_resid; 5364 offset_t offset = uio->uio_loffset; 5365 int blksz; 5366 int fullblk, i; 5367 arc_buf_t *abuf; 5368 ssize_t maxsize; 5369 int preamble, postamble; 5370 5371 if (xuio->xu_type != UIOTYPE_ZEROCOPY) 5372 return (SET_ERROR(EINVAL)); 5373 5374 ZFS_ENTER(zfsvfs); 5375 ZFS_VERIFY_ZP(zp); 5376 switch (ioflag) { 5377 case UIO_WRITE: 5378 /* 5379 * Loan out an arc_buf for write if write size is bigger than 5380 * max_blksz, and the file's block size is also max_blksz. 5381 */ 5382 blksz = max_blksz; 5383 if (size < blksz || zp->z_blksz != blksz) { 5384 ZFS_EXIT(zfsvfs); 5385 return (SET_ERROR(EINVAL)); 5386 } 5387 /* 5388 * Caller requests buffers for write before knowing where the 5389 * write offset might be (e.g. NFS TCP write). 5390 */ 5391 if (offset == -1) { 5392 preamble = 0; 5393 } else { 5394 preamble = P2PHASE(offset, blksz); 5395 if (preamble) { 5396 preamble = blksz - preamble; 5397 size -= preamble; 5398 } 5399 } 5400 5401 postamble = P2PHASE(size, blksz); 5402 size -= postamble; 5403 5404 fullblk = size / blksz; 5405 (void) dmu_xuio_init(xuio, 5406 (preamble != 0) + fullblk + (postamble != 0)); 5407 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 5408 int, postamble, int, 5409 (preamble != 0) + fullblk + (postamble != 0)); 5410 5411 /* 5412 * Have to fix iov base/len for partial buffers. They 5413 * currently represent full arc_buf's. 5414 */ 5415 if (preamble) { 5416 /* data begins in the middle of the arc_buf */ 5417 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5418 blksz); 5419 ASSERT(abuf); 5420 (void) dmu_xuio_add(xuio, abuf, 5421 blksz - preamble, preamble); 5422 } 5423 5424 for (i = 0; i < fullblk; i++) { 5425 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5426 blksz); 5427 ASSERT(abuf); 5428 (void) dmu_xuio_add(xuio, abuf, 0, blksz); 5429 } 5430 5431 if (postamble) { 5432 /* data ends in the middle of the arc_buf */ 5433 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5434 blksz); 5435 ASSERT(abuf); 5436 (void) dmu_xuio_add(xuio, abuf, 0, postamble); 5437 } 5438 break; 5439 case UIO_READ: 5440 /* 5441 * Loan out an arc_buf for read if the read size is larger than 5442 * the current file block size. Block alignment is not 5443 * considered. Partial arc_buf will be loaned out for read. 5444 */ 5445 blksz = zp->z_blksz; 5446 if (blksz < zcr_blksz_min) 5447 blksz = zcr_blksz_min; 5448 if (blksz > zcr_blksz_max) 5449 blksz = zcr_blksz_max; 5450 /* avoid potential complexity of dealing with it */ 5451 if (blksz > max_blksz) { 5452 ZFS_EXIT(zfsvfs); 5453 return (SET_ERROR(EINVAL)); 5454 } 5455 5456 maxsize = zp->z_size - uio->uio_loffset; 5457 if (size > maxsize) 5458 size = maxsize; 5459 5460 if (size < blksz || vn_has_cached_data(vp)) { 5461 ZFS_EXIT(zfsvfs); 5462 return (SET_ERROR(EINVAL)); 5463 } 5464 break; 5465 default: 5466 ZFS_EXIT(zfsvfs); 5467 return (SET_ERROR(EINVAL)); 5468 } 5469 5470 uio->uio_extflg = UIO_XUIO; 5471 XUIO_XUZC_RW(xuio) = ioflag; 5472 ZFS_EXIT(zfsvfs); 5473 return (0); 5474} 5475 5476/*ARGSUSED*/ 5477static int 5478zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 5479{ 5480 int i; 5481 arc_buf_t *abuf; 5482 int ioflag = XUIO_XUZC_RW(xuio); 5483 5484 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 5485 5486 i = dmu_xuio_cnt(xuio); 5487 while (i-- > 0) { 5488 abuf = dmu_xuio_arcbuf(xuio, i); 5489 /* 5490 * if abuf == NULL, it must be a write buffer 5491 * that has been returned in zfs_write(). 5492 */ 5493 if (abuf) 5494 dmu_return_arcbuf(abuf); 5495 ASSERT(abuf || ioflag == UIO_WRITE); 5496 } 5497 5498 dmu_xuio_fini(xuio); 5499 return (0); 5500} 5501 5502/* 5503 * Predeclare these here so that the compiler assumes that 5504 * this is an "old style" function declaration that does 5505 * not include arguments => we won't get type mismatch errors 5506 * in the initializations that follow. 5507 */ 5508static int zfs_inval(); 5509static int zfs_isdir(); 5510 5511static int 5512zfs_inval() 5513{ 5514 return (SET_ERROR(EINVAL)); 5515} 5516 5517static int 5518zfs_isdir() 5519{ 5520 return (SET_ERROR(EISDIR)); 5521} 5522/* 5523 * Directory vnode operations template 5524 */ 5525vnodeops_t *zfs_dvnodeops; 5526const fs_operation_def_t zfs_dvnodeops_template[] = { 5527 VOPNAME_OPEN, { .vop_open = zfs_open }, 5528 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5529 VOPNAME_READ, { .error = zfs_isdir }, 5530 VOPNAME_WRITE, { .error = zfs_isdir }, 5531 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5532 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5533 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5534 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5535 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5536 VOPNAME_CREATE, { .vop_create = zfs_create }, 5537 VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 5538 VOPNAME_LINK, { .vop_link = zfs_link }, 5539 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5540 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 5541 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 5542 VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 5543 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 5544 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5545 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5546 VOPNAME_FID, { .vop_fid = zfs_fid }, 5547 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5548 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5549 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5550 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5551 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5552 NULL, NULL 5553}; 5554 5555/* 5556 * Regular file vnode operations template 5557 */ 5558vnodeops_t *zfs_fvnodeops; 5559const fs_operation_def_t zfs_fvnodeops_template[] = { 5560 VOPNAME_OPEN, { .vop_open = zfs_open }, 5561 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5562 VOPNAME_READ, { .vop_read = zfs_read }, 5563 VOPNAME_WRITE, { .vop_write = zfs_write }, 5564 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5565 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5566 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5567 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5568 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5569 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5570 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5571 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5572 VOPNAME_FID, { .vop_fid = zfs_fid }, 5573 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5574 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 5575 VOPNAME_SPACE, { .vop_space = zfs_space }, 5576 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 5577 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 5578 VOPNAME_MAP, { .vop_map = zfs_map }, 5579 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 5580 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 5581 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5582 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5583 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5584 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5585 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 5586 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 5587 NULL, NULL 5588}; 5589 5590/* 5591 * Symbolic link vnode operations template 5592 */ 5593vnodeops_t *zfs_symvnodeops; 5594const fs_operation_def_t zfs_symvnodeops_template[] = { 5595 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5596 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5597 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5598 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5599 VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 5600 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5601 VOPNAME_FID, { .vop_fid = zfs_fid }, 5602 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5603 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5604 NULL, NULL 5605}; 5606 5607/* 5608 * special share hidden files vnode operations template 5609 */ 5610vnodeops_t *zfs_sharevnodeops; 5611const fs_operation_def_t zfs_sharevnodeops_template[] = { 5612 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5613 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5614 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5615 VOPNAME_FID, { .vop_fid = zfs_fid }, 5616 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5617 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5618 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5619 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5620 NULL, NULL 5621}; 5622 5623/* 5624 * Extended attribute directory vnode operations template 5625 * 5626 * This template is identical to the directory vnodes 5627 * operation template except for restricted operations: 5628 * VOP_MKDIR() 5629 * VOP_SYMLINK() 5630 * 5631 * Note that there are other restrictions embedded in: 5632 * zfs_create() - restrict type to VREG 5633 * zfs_link() - no links into/out of attribute space 5634 * zfs_rename() - no moves into/out of attribute space 5635 */ 5636vnodeops_t *zfs_xdvnodeops; 5637const fs_operation_def_t zfs_xdvnodeops_template[] = { 5638 VOPNAME_OPEN, { .vop_open = zfs_open }, 5639 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5640 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5641 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5642 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5643 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5644 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5645 VOPNAME_CREATE, { .vop_create = zfs_create }, 5646 VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 5647 VOPNAME_LINK, { .vop_link = zfs_link }, 5648 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5649 VOPNAME_MKDIR, { .error = zfs_inval }, 5650 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 5651 VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 5652 VOPNAME_SYMLINK, { .error = zfs_inval }, 5653 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5654 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5655 VOPNAME_FID, { .vop_fid = zfs_fid }, 5656 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5657 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5658 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5659 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5660 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5661 NULL, NULL 5662}; 5663 5664/* 5665 * Error vnode operations template 5666 */ 5667vnodeops_t *zfs_evnodeops; 5668const fs_operation_def_t zfs_evnodeops_template[] = { 5669 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5670 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5671 NULL, NULL 5672}; 5673#endif /* sun */ 5674 5675static int 5676ioflags(int ioflags) 5677{ 5678 int flags = 0; 5679 5680 if (ioflags & IO_APPEND) 5681 flags |= FAPPEND; 5682 if (ioflags & IO_NDELAY) 5683 flags |= FNONBLOCK; 5684 if (ioflags & IO_SYNC) 5685 flags |= (FSYNC | FDSYNC | FRSYNC); 5686 5687 return (flags); 5688} 5689 5690static int 5691zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage) 5692{ 5693 znode_t *zp = VTOZ(vp); 5694 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5695 objset_t *os = zp->z_zfsvfs->z_os; 5696 vm_page_t mfirst, mlast, mreq; 5697 vm_object_t object; 5698 caddr_t va; 5699 struct sf_buf *sf; 5700 off_t startoff, endoff; 5701 int i, error; 5702 vm_pindex_t reqstart, reqend; 5703 int pcount, lsize, reqsize, size; 5704 5705 ZFS_ENTER(zfsvfs); 5706 ZFS_VERIFY_ZP(zp); 5707 5708 pcount = OFF_TO_IDX(round_page(count)); 5709 mreq = m[reqpage]; 5710 object = mreq->object; 5711 error = 0; 5712 5713 KASSERT(vp->v_object == object, ("mismatching object")); 5714 5715 if (pcount > 1 && zp->z_blksz > PAGESIZE) { 5716 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz); 5717 reqstart = OFF_TO_IDX(round_page(startoff)); 5718 if (reqstart < m[0]->pindex) 5719 reqstart = 0; 5720 else 5721 reqstart = reqstart - m[0]->pindex; 5722 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE, 5723 zp->z_blksz); 5724 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1; 5725 if (reqend > m[pcount - 1]->pindex) 5726 reqend = m[pcount - 1]->pindex; 5727 reqsize = reqend - m[reqstart]->pindex + 1; 5728 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize, 5729 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds")); 5730 } else { 5731 reqstart = reqpage; 5732 reqsize = 1; 5733 } 5734 mfirst = m[reqstart]; 5735 mlast = m[reqstart + reqsize - 1]; 5736 5737 zfs_vmobject_wlock(object); 5738 5739 for (i = 0; i < reqstart; i++) { 5740 vm_page_lock(m[i]); 5741 vm_page_free(m[i]); 5742 vm_page_unlock(m[i]); 5743 } 5744 for (i = reqstart + reqsize; i < pcount; i++) { 5745 vm_page_lock(m[i]); 5746 vm_page_free(m[i]); 5747 vm_page_unlock(m[i]); 5748 } 5749 5750 if (mreq->valid && reqsize == 1) { 5751 if (mreq->valid != VM_PAGE_BITS_ALL) 5752 vm_page_zero_invalid(mreq, TRUE); 5753 zfs_vmobject_wunlock(object); 5754 ZFS_EXIT(zfsvfs); 5755 return (zfs_vm_pagerret_ok); 5756 } 5757 5758 PCPU_INC(cnt.v_vnodein); 5759 PCPU_ADD(cnt.v_vnodepgsin, reqsize); 5760 5761 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) { 5762 for (i = reqstart; i < reqstart + reqsize; i++) { 5763 if (i != reqpage) { 5764 vm_page_lock(m[i]); 5765 vm_page_free(m[i]); 5766 vm_page_unlock(m[i]); 5767 } 5768 } 5769 zfs_vmobject_wunlock(object); 5770 ZFS_EXIT(zfsvfs); 5771 return (zfs_vm_pagerret_bad); 5772 } 5773 5774 lsize = PAGE_SIZE; 5775 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size) 5776 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex); 5777 5778 zfs_vmobject_wunlock(object); 5779 5780 for (i = reqstart; i < reqstart + reqsize; i++) { 5781 size = PAGE_SIZE; 5782 if (i == (reqstart + reqsize - 1)) 5783 size = lsize; 5784 va = zfs_map_page(m[i], &sf); 5785 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex), 5786 size, va, DMU_READ_PREFETCH); 5787 if (size != PAGE_SIZE) 5788 bzero(va + size, PAGE_SIZE - size); 5789 zfs_unmap_page(sf); 5790 if (error != 0) 5791 break; 5792 } 5793 5794 zfs_vmobject_wlock(object); 5795 5796 for (i = reqstart; i < reqstart + reqsize; i++) { 5797 if (!error) 5798 m[i]->valid = VM_PAGE_BITS_ALL; 5799 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i])); 5800 if (i != reqpage) 5801 vm_page_readahead_finish(m[i]); 5802 } 5803 5804 zfs_vmobject_wunlock(object); 5805 5806 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 5807 ZFS_EXIT(zfsvfs); 5808 return (error ? zfs_vm_pagerret_error : zfs_vm_pagerret_ok); 5809} 5810 5811static int 5812zfs_freebsd_getpages(ap) 5813 struct vop_getpages_args /* { 5814 struct vnode *a_vp; 5815 vm_page_t *a_m; 5816 int a_count; 5817 int a_reqpage; 5818 vm_ooffset_t a_offset; 5819 } */ *ap; 5820{ 5821 5822 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage)); 5823} 5824 5825static int 5826zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags, 5827 int *rtvals) 5828{ 5829 znode_t *zp = VTOZ(vp); 5830 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5831 rl_t *rl; 5832 dmu_tx_t *tx; 5833 struct sf_buf *sf; 5834 vm_object_t object; 5835 vm_page_t m; 5836 caddr_t va; 5837 size_t tocopy; 5838 size_t lo_len; 5839 vm_ooffset_t lo_off; 5840 vm_ooffset_t off; 5841 uint_t blksz; 5842 int ncount; 5843 int pcount; 5844 int err; 5845 int i; 5846 5847 ZFS_ENTER(zfsvfs); 5848 ZFS_VERIFY_ZP(zp); 5849 5850 object = vp->v_object; 5851 pcount = btoc(len); 5852 ncount = pcount; 5853 5854 KASSERT(ma[0]->object == object, ("mismatching object")); 5855 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length")); 5856 5857 for (i = 0; i < pcount; i++) 5858 rtvals[i] = zfs_vm_pagerret_error; 5859 5860 off = IDX_TO_OFF(ma[0]->pindex); 5861 blksz = zp->z_blksz; 5862 lo_off = rounddown(off, blksz); 5863 lo_len = roundup(len + (off - lo_off), blksz); 5864 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER); 5865 5866 zfs_vmobject_wlock(object); 5867 if (len + off > object->un_pager.vnp.vnp_size) { 5868 if (object->un_pager.vnp.vnp_size > off) { 5869 int pgoff; 5870 5871 len = object->un_pager.vnp.vnp_size - off; 5872 ncount = btoc(len); 5873 if ((pgoff = (int)len & PAGE_MASK) != 0) { 5874 /* 5875 * If the object is locked and the following 5876 * conditions hold, then the page's dirty 5877 * field cannot be concurrently changed by a 5878 * pmap operation. 5879 */ 5880 m = ma[ncount - 1]; 5881 vm_page_assert_sbusied(m); 5882 KASSERT(!pmap_page_is_write_mapped(m), 5883 ("zfs_putpages: page %p is not read-only", m)); 5884 vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 5885 pgoff); 5886 } 5887 } else { 5888 len = 0; 5889 ncount = 0; 5890 } 5891 if (ncount < pcount) { 5892 for (i = ncount; i < pcount; i++) { 5893 rtvals[i] = zfs_vm_pagerret_bad; 5894 } 5895 } 5896 } 5897 zfs_vmobject_wunlock(object); 5898 5899 if (ncount == 0) 5900 goto out; 5901 5902 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 5903 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 5904 goto out; 5905 } 5906 5907top: 5908 tx = dmu_tx_create(zfsvfs->z_os); 5909 dmu_tx_hold_write(tx, zp->z_id, off, len); 5910 5911 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 5912 zfs_sa_upgrade_txholds(tx, zp); 5913 err = dmu_tx_assign(tx, TXG_NOWAIT); 5914 if (err != 0) { 5915 if (err == ERESTART) { 5916 dmu_tx_wait(tx); 5917 dmu_tx_abort(tx); 5918 goto top; 5919 } 5920 dmu_tx_abort(tx); 5921 goto out; 5922 } 5923 5924 if (zp->z_blksz < PAGE_SIZE) { 5925 i = 0; 5926 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) { 5927 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len; 5928 va = zfs_map_page(ma[i], &sf); 5929 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx); 5930 zfs_unmap_page(sf); 5931 } 5932 } else { 5933 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx); 5934 } 5935 5936 if (err == 0) { 5937 uint64_t mtime[2], ctime[2]; 5938 sa_bulk_attr_t bulk[3]; 5939 int count = 0; 5940 5941 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 5942 &mtime, 16); 5943 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 5944 &ctime, 16); 5945 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 5946 &zp->z_pflags, 8); 5947 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 5948 B_TRUE); 5949 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 5950 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 5951 5952 zfs_vmobject_wlock(object); 5953 for (i = 0; i < ncount; i++) { 5954 rtvals[i] = zfs_vm_pagerret_ok; 5955 vm_page_undirty(ma[i]); 5956 } 5957 zfs_vmobject_wunlock(object); 5958 PCPU_INC(cnt.v_vnodeout); 5959 PCPU_ADD(cnt.v_vnodepgsout, ncount); 5960 } 5961 dmu_tx_commit(tx); 5962 5963out: 5964 zfs_range_unlock(rl); 5965 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 || 5966 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 5967 zil_commit(zfsvfs->z_log, zp->z_id); 5968 ZFS_EXIT(zfsvfs); 5969 return (rtvals[0]); 5970} 5971 5972int 5973zfs_freebsd_putpages(ap) 5974 struct vop_putpages_args /* { 5975 struct vnode *a_vp; 5976 vm_page_t *a_m; 5977 int a_count; 5978 int a_sync; 5979 int *a_rtvals; 5980 vm_ooffset_t a_offset; 5981 } */ *ap; 5982{ 5983 5984 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync, 5985 ap->a_rtvals)); 5986} 5987 5988static int 5989zfs_freebsd_bmap(ap) 5990 struct vop_bmap_args /* { 5991 struct vnode *a_vp; 5992 daddr_t a_bn; 5993 struct bufobj **a_bop; 5994 daddr_t *a_bnp; 5995 int *a_runp; 5996 int *a_runb; 5997 } */ *ap; 5998{ 5999 6000 if (ap->a_bop != NULL) 6001 *ap->a_bop = &ap->a_vp->v_bufobj; 6002 if (ap->a_bnp != NULL) 6003 *ap->a_bnp = ap->a_bn; 6004 if (ap->a_runp != NULL) 6005 *ap->a_runp = 0; 6006 if (ap->a_runb != NULL) 6007 *ap->a_runb = 0; 6008 6009 return (0); 6010} 6011 6012static int 6013zfs_freebsd_open(ap) 6014 struct vop_open_args /* { 6015 struct vnode *a_vp; 6016 int a_mode; 6017 struct ucred *a_cred; 6018 struct thread *a_td; 6019 } */ *ap; 6020{ 6021 vnode_t *vp = ap->a_vp; 6022 znode_t *zp = VTOZ(vp); 6023 int error; 6024 6025 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL); 6026 if (error == 0) 6027 vnode_create_vobject(vp, zp->z_size, ap->a_td); 6028 return (error); 6029} 6030 6031static int 6032zfs_freebsd_close(ap) 6033 struct vop_close_args /* { 6034 struct vnode *a_vp; 6035 int a_fflag; 6036 struct ucred *a_cred; 6037 struct thread *a_td; 6038 } */ *ap; 6039{ 6040 6041 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL)); 6042} 6043 6044static int 6045zfs_freebsd_ioctl(ap) 6046 struct vop_ioctl_args /* { 6047 struct vnode *a_vp; 6048 u_long a_command; 6049 caddr_t a_data; 6050 int a_fflag; 6051 struct ucred *cred; 6052 struct thread *td; 6053 } */ *ap; 6054{ 6055 6056 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data, 6057 ap->a_fflag, ap->a_cred, NULL, NULL)); 6058} 6059 6060static int 6061zfs_freebsd_read(ap) 6062 struct vop_read_args /* { 6063 struct vnode *a_vp; 6064 struct uio *a_uio; 6065 int a_ioflag; 6066 struct ucred *a_cred; 6067 } */ *ap; 6068{ 6069 6070 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag), 6071 ap->a_cred, NULL)); 6072} 6073 6074static int 6075zfs_freebsd_write(ap) 6076 struct vop_write_args /* { 6077 struct vnode *a_vp; 6078 struct uio *a_uio; 6079 int a_ioflag; 6080 struct ucred *a_cred; 6081 } */ *ap; 6082{ 6083 6084 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag), 6085 ap->a_cred, NULL)); 6086} 6087 6088static int 6089zfs_freebsd_access(ap) 6090 struct vop_access_args /* { 6091 struct vnode *a_vp; 6092 accmode_t a_accmode; 6093 struct ucred *a_cred; 6094 struct thread *a_td; 6095 } */ *ap; 6096{ 6097 vnode_t *vp = ap->a_vp; 6098 znode_t *zp = VTOZ(vp); 6099 accmode_t accmode; 6100 int error = 0; 6101 6102 /* 6103 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND, 6104 */ 6105 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND); 6106 if (accmode != 0) 6107 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL); 6108 6109 /* 6110 * VADMIN has to be handled by vaccess(). 6111 */ 6112 if (error == 0) { 6113 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND); 6114 if (accmode != 0) { 6115 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid, 6116 zp->z_gid, accmode, ap->a_cred, NULL); 6117 } 6118 } 6119 6120 /* 6121 * For VEXEC, ensure that at least one execute bit is set for 6122 * non-directories. 6123 */ 6124 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR && 6125 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 6126 error = EACCES; 6127 } 6128 6129 return (error); 6130} 6131 6132static int 6133zfs_freebsd_lookup(ap) 6134 struct vop_lookup_args /* { 6135 struct vnode *a_dvp; 6136 struct vnode **a_vpp; 6137 struct componentname *a_cnp; 6138 } */ *ap; 6139{ 6140 struct componentname *cnp = ap->a_cnp; 6141 char nm[NAME_MAX + 1]; 6142 6143 ASSERT(cnp->cn_namelen < sizeof(nm)); 6144 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm))); 6145 6146 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop, 6147 cnp->cn_cred, cnp->cn_thread, 0)); 6148} 6149 6150static int 6151zfs_freebsd_create(ap) 6152 struct vop_create_args /* { 6153 struct vnode *a_dvp; 6154 struct vnode **a_vpp; 6155 struct componentname *a_cnp; 6156 struct vattr *a_vap; 6157 } */ *ap; 6158{ 6159 struct componentname *cnp = ap->a_cnp; 6160 vattr_t *vap = ap->a_vap; 6161 int error, mode; 6162 6163 ASSERT(cnp->cn_flags & SAVENAME); 6164 6165 vattr_init_mask(vap); 6166 mode = vap->va_mode & ALLPERMS; 6167 6168 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode, 6169 ap->a_vpp, cnp->cn_cred, cnp->cn_thread); 6170#ifdef FREEBSD_NAMECACHE 6171 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0) 6172 cache_enter(ap->a_dvp, *ap->a_vpp, cnp); 6173#endif 6174 return (error); 6175} 6176 6177static int 6178zfs_freebsd_remove(ap) 6179 struct vop_remove_args /* { 6180 struct vnode *a_dvp; 6181 struct vnode *a_vp; 6182 struct componentname *a_cnp; 6183 } */ *ap; 6184{ 6185 6186 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 6187 6188 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr, 6189 ap->a_cnp->cn_cred, NULL, 0)); 6190} 6191 6192static int 6193zfs_freebsd_mkdir(ap) 6194 struct vop_mkdir_args /* { 6195 struct vnode *a_dvp; 6196 struct vnode **a_vpp; 6197 struct componentname *a_cnp; 6198 struct vattr *a_vap; 6199 } */ *ap; 6200{ 6201 vattr_t *vap = ap->a_vap; 6202 6203 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 6204 6205 vattr_init_mask(vap); 6206 6207 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp, 6208 ap->a_cnp->cn_cred, NULL, 0, NULL)); 6209} 6210 6211static int 6212zfs_freebsd_rmdir(ap) 6213 struct vop_rmdir_args /* { 6214 struct vnode *a_dvp; 6215 struct vnode *a_vp; 6216 struct componentname *a_cnp; 6217 } */ *ap; 6218{ 6219 struct componentname *cnp = ap->a_cnp; 6220 6221 ASSERT(cnp->cn_flags & SAVENAME); 6222 6223 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0)); 6224} 6225 6226static int 6227zfs_freebsd_readdir(ap) 6228 struct vop_readdir_args /* { 6229 struct vnode *a_vp; 6230 struct uio *a_uio; 6231 struct ucred *a_cred; 6232 int *a_eofflag; 6233 int *a_ncookies; 6234 u_long **a_cookies; 6235 } */ *ap; 6236{ 6237 6238 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag, 6239 ap->a_ncookies, ap->a_cookies)); 6240} 6241 6242static int 6243zfs_freebsd_fsync(ap) 6244 struct vop_fsync_args /* { 6245 struct vnode *a_vp; 6246 int a_waitfor; 6247 struct thread *a_td; 6248 } */ *ap; 6249{ 6250 6251 vop_stdfsync(ap); 6252 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL)); 6253} 6254 6255static int 6256zfs_freebsd_getattr(ap) 6257 struct vop_getattr_args /* { 6258 struct vnode *a_vp; 6259 struct vattr *a_vap; 6260 struct ucred *a_cred; 6261 } */ *ap; 6262{ 6263 vattr_t *vap = ap->a_vap; 6264 xvattr_t xvap; 6265 u_long fflags = 0; 6266 int error; 6267 6268 xva_init(&xvap); 6269 xvap.xva_vattr = *vap; 6270 xvap.xva_vattr.va_mask |= AT_XVATTR; 6271 6272 /* Convert chflags into ZFS-type flags. */ 6273 /* XXX: what about SF_SETTABLE?. */ 6274 XVA_SET_REQ(&xvap, XAT_IMMUTABLE); 6275 XVA_SET_REQ(&xvap, XAT_APPENDONLY); 6276 XVA_SET_REQ(&xvap, XAT_NOUNLINK); 6277 XVA_SET_REQ(&xvap, XAT_NODUMP); 6278 XVA_SET_REQ(&xvap, XAT_READONLY); 6279 XVA_SET_REQ(&xvap, XAT_ARCHIVE); 6280 XVA_SET_REQ(&xvap, XAT_SYSTEM); 6281 XVA_SET_REQ(&xvap, XAT_HIDDEN); 6282 XVA_SET_REQ(&xvap, XAT_REPARSE); 6283 XVA_SET_REQ(&xvap, XAT_OFFLINE); 6284 XVA_SET_REQ(&xvap, XAT_SPARSE); 6285 6286 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL); 6287 if (error != 0) 6288 return (error); 6289 6290 /* Convert ZFS xattr into chflags. */ 6291#define FLAG_CHECK(fflag, xflag, xfield) do { \ 6292 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \ 6293 fflags |= (fflag); \ 6294} while (0) 6295 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE, 6296 xvap.xva_xoptattrs.xoa_immutable); 6297 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY, 6298 xvap.xva_xoptattrs.xoa_appendonly); 6299 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK, 6300 xvap.xva_xoptattrs.xoa_nounlink); 6301 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE, 6302 xvap.xva_xoptattrs.xoa_archive); 6303 FLAG_CHECK(UF_NODUMP, XAT_NODUMP, 6304 xvap.xva_xoptattrs.xoa_nodump); 6305 FLAG_CHECK(UF_READONLY, XAT_READONLY, 6306 xvap.xva_xoptattrs.xoa_readonly); 6307 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM, 6308 xvap.xva_xoptattrs.xoa_system); 6309 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN, 6310 xvap.xva_xoptattrs.xoa_hidden); 6311 FLAG_CHECK(UF_REPARSE, XAT_REPARSE, 6312 xvap.xva_xoptattrs.xoa_reparse); 6313 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE, 6314 xvap.xva_xoptattrs.xoa_offline); 6315 FLAG_CHECK(UF_SPARSE, XAT_SPARSE, 6316 xvap.xva_xoptattrs.xoa_sparse); 6317 6318#undef FLAG_CHECK 6319 *vap = xvap.xva_vattr; 6320 vap->va_flags = fflags; 6321 return (0); 6322} 6323 6324static int 6325zfs_freebsd_setattr(ap) 6326 struct vop_setattr_args /* { 6327 struct vnode *a_vp; 6328 struct vattr *a_vap; 6329 struct ucred *a_cred; 6330 } */ *ap; 6331{ 6332 vnode_t *vp = ap->a_vp; 6333 vattr_t *vap = ap->a_vap; 6334 cred_t *cred = ap->a_cred; 6335 xvattr_t xvap; 6336 u_long fflags; 6337 uint64_t zflags; 6338 6339 vattr_init_mask(vap); 6340 vap->va_mask &= ~AT_NOSET; 6341 6342 xva_init(&xvap); 6343 xvap.xva_vattr = *vap; 6344 6345 zflags = VTOZ(vp)->z_pflags; 6346 6347 if (vap->va_flags != VNOVAL) { 6348 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs; 6349 int error; 6350 6351 if (zfsvfs->z_use_fuids == B_FALSE) 6352 return (EOPNOTSUPP); 6353 6354 fflags = vap->va_flags; 6355 /* 6356 * XXX KDM 6357 * We need to figure out whether it makes sense to allow 6358 * UF_REPARSE through, since we don't really have other 6359 * facilities to handle reparse points and zfs_setattr() 6360 * doesn't currently allow setting that attribute anyway. 6361 */ 6362 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE| 6363 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE| 6364 UF_OFFLINE|UF_SPARSE)) != 0) 6365 return (EOPNOTSUPP); 6366 /* 6367 * Unprivileged processes are not permitted to unset system 6368 * flags, or modify flags if any system flags are set. 6369 * Privileged non-jail processes may not modify system flags 6370 * if securelevel > 0 and any existing system flags are set. 6371 * Privileged jail processes behave like privileged non-jail 6372 * processes if the security.jail.chflags_allowed sysctl is 6373 * is non-zero; otherwise, they behave like unprivileged 6374 * processes. 6375 */ 6376 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 || 6377 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) { 6378 if (zflags & 6379 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) { 6380 error = securelevel_gt(cred, 0); 6381 if (error != 0) 6382 return (error); 6383 } 6384 } else { 6385 /* 6386 * Callers may only modify the file flags on objects they 6387 * have VADMIN rights for. 6388 */ 6389 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0) 6390 return (error); 6391 if (zflags & 6392 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) { 6393 return (EPERM); 6394 } 6395 if (fflags & 6396 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) { 6397 return (EPERM); 6398 } 6399 } 6400 6401#define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \ 6402 if (((fflags & (fflag)) && !(zflags & (zflag))) || \ 6403 ((zflags & (zflag)) && !(fflags & (fflag)))) { \ 6404 XVA_SET_REQ(&xvap, (xflag)); \ 6405 (xfield) = ((fflags & (fflag)) != 0); \ 6406 } \ 6407} while (0) 6408 /* Convert chflags into ZFS-type flags. */ 6409 /* XXX: what about SF_SETTABLE?. */ 6410 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE, 6411 xvap.xva_xoptattrs.xoa_immutable); 6412 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY, 6413 xvap.xva_xoptattrs.xoa_appendonly); 6414 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK, 6415 xvap.xva_xoptattrs.xoa_nounlink); 6416 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE, 6417 xvap.xva_xoptattrs.xoa_archive); 6418 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP, 6419 xvap.xva_xoptattrs.xoa_nodump); 6420 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY, 6421 xvap.xva_xoptattrs.xoa_readonly); 6422 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM, 6423 xvap.xva_xoptattrs.xoa_system); 6424 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN, 6425 xvap.xva_xoptattrs.xoa_hidden); 6426 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE, 6427 xvap.xva_xoptattrs.xoa_hidden); 6428 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE, 6429 xvap.xva_xoptattrs.xoa_offline); 6430 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE, 6431 xvap.xva_xoptattrs.xoa_sparse); 6432#undef FLAG_CHANGE 6433 } 6434 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL)); 6435} 6436 6437static int 6438zfs_freebsd_rename(ap) 6439 struct vop_rename_args /* { 6440 struct vnode *a_fdvp; 6441 struct vnode *a_fvp; 6442 struct componentname *a_fcnp; 6443 struct vnode *a_tdvp; 6444 struct vnode *a_tvp; 6445 struct componentname *a_tcnp; 6446 } */ *ap; 6447{ 6448 vnode_t *fdvp = ap->a_fdvp; 6449 vnode_t *fvp = ap->a_fvp; 6450 vnode_t *tdvp = ap->a_tdvp; 6451 vnode_t *tvp = ap->a_tvp; 6452 int error; 6453 6454 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART)); 6455 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART)); 6456 6457 /* 6458 * Check for cross-device rename. 6459 */ 6460 if ((fdvp->v_mount != tdvp->v_mount) || 6461 (tvp && (fdvp->v_mount != tvp->v_mount))) 6462 error = EXDEV; 6463 else 6464 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp, 6465 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0); 6466 if (tdvp == tvp) 6467 VN_RELE(tdvp); 6468 else 6469 VN_URELE(tdvp); 6470 if (tvp) 6471 VN_URELE(tvp); 6472 VN_RELE(fdvp); 6473 VN_RELE(fvp); 6474 6475 return (error); 6476} 6477 6478static int 6479zfs_freebsd_symlink(ap) 6480 struct vop_symlink_args /* { 6481 struct vnode *a_dvp; 6482 struct vnode **a_vpp; 6483 struct componentname *a_cnp; 6484 struct vattr *a_vap; 6485 char *a_target; 6486 } */ *ap; 6487{ 6488 struct componentname *cnp = ap->a_cnp; 6489 vattr_t *vap = ap->a_vap; 6490 6491 ASSERT(cnp->cn_flags & SAVENAME); 6492 6493 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */ 6494 vattr_init_mask(vap); 6495 6496 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap, 6497 ap->a_target, cnp->cn_cred, cnp->cn_thread)); 6498} 6499 6500static int 6501zfs_freebsd_readlink(ap) 6502 struct vop_readlink_args /* { 6503 struct vnode *a_vp; 6504 struct uio *a_uio; 6505 struct ucred *a_cred; 6506 } */ *ap; 6507{ 6508 6509 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL)); 6510} 6511 6512static int 6513zfs_freebsd_link(ap) 6514 struct vop_link_args /* { 6515 struct vnode *a_tdvp; 6516 struct vnode *a_vp; 6517 struct componentname *a_cnp; 6518 } */ *ap; 6519{ 6520 struct componentname *cnp = ap->a_cnp; 6521 vnode_t *vp = ap->a_vp; 6522 vnode_t *tdvp = ap->a_tdvp; 6523 6524 if (tdvp->v_mount != vp->v_mount) 6525 return (EXDEV); 6526 6527 ASSERT(cnp->cn_flags & SAVENAME); 6528 6529 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0)); 6530} 6531 6532static int 6533zfs_freebsd_inactive(ap) 6534 struct vop_inactive_args /* { 6535 struct vnode *a_vp; 6536 struct thread *a_td; 6537 } */ *ap; 6538{ 6539 vnode_t *vp = ap->a_vp; 6540 6541 zfs_inactive(vp, ap->a_td->td_ucred, NULL); 6542 return (0); 6543} 6544 6545static int 6546zfs_freebsd_reclaim(ap) 6547 struct vop_reclaim_args /* { 6548 struct vnode *a_vp; 6549 struct thread *a_td; 6550 } */ *ap; 6551{ 6552 vnode_t *vp = ap->a_vp; 6553 znode_t *zp = VTOZ(vp); 6554 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 6555 6556 ASSERT(zp != NULL); 6557 6558 /* Destroy the vm object and flush associated pages. */ 6559 vnode_destroy_vobject(vp); 6560 6561 /* 6562 * z_teardown_inactive_lock protects from a race with 6563 * zfs_znode_dmu_fini in zfsvfs_teardown during 6564 * force unmount. 6565 */ 6566 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 6567 if (zp->z_sa_hdl == NULL) 6568 zfs_znode_free(zp); 6569 else 6570 zfs_zinactive(zp); 6571 rw_exit(&zfsvfs->z_teardown_inactive_lock); 6572 6573 vp->v_data = NULL; 6574 return (0); 6575} 6576 6577static int 6578zfs_freebsd_fid(ap) 6579 struct vop_fid_args /* { 6580 struct vnode *a_vp; 6581 struct fid *a_fid; 6582 } */ *ap; 6583{ 6584 6585 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL)); 6586} 6587 6588static int 6589zfs_freebsd_pathconf(ap) 6590 struct vop_pathconf_args /* { 6591 struct vnode *a_vp; 6592 int a_name; 6593 register_t *a_retval; 6594 } */ *ap; 6595{ 6596 ulong_t val; 6597 int error; 6598 6599 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL); 6600 if (error == 0) 6601 *ap->a_retval = val; 6602 else if (error == EOPNOTSUPP) 6603 error = vop_stdpathconf(ap); 6604 return (error); 6605} 6606 6607static int 6608zfs_freebsd_fifo_pathconf(ap) 6609 struct vop_pathconf_args /* { 6610 struct vnode *a_vp; 6611 int a_name; 6612 register_t *a_retval; 6613 } */ *ap; 6614{ 6615 6616 switch (ap->a_name) { 6617 case _PC_ACL_EXTENDED: 6618 case _PC_ACL_NFS4: 6619 case _PC_ACL_PATH_MAX: 6620 case _PC_MAC_PRESENT: 6621 return (zfs_freebsd_pathconf(ap)); 6622 default: 6623 return (fifo_specops.vop_pathconf(ap)); 6624 } 6625} 6626 6627/* 6628 * FreeBSD's extended attributes namespace defines file name prefix for ZFS' 6629 * extended attribute name: 6630 * 6631 * NAMESPACE PREFIX 6632 * system freebsd:system: 6633 * user (none, can be used to access ZFS fsattr(5) attributes 6634 * created on Solaris) 6635 */ 6636static int 6637zfs_create_attrname(int attrnamespace, const char *name, char *attrname, 6638 size_t size) 6639{ 6640 const char *namespace, *prefix, *suffix; 6641 6642 /* We don't allow '/' character in attribute name. */ 6643 if (strchr(name, '/') != NULL) 6644 return (EINVAL); 6645 /* We don't allow attribute names that start with "freebsd:" string. */ 6646 if (strncmp(name, "freebsd:", 8) == 0) 6647 return (EINVAL); 6648 6649 bzero(attrname, size); 6650 6651 switch (attrnamespace) { 6652 case EXTATTR_NAMESPACE_USER: 6653#if 0 6654 prefix = "freebsd:"; 6655 namespace = EXTATTR_NAMESPACE_USER_STRING; 6656 suffix = ":"; 6657#else 6658 /* 6659 * This is the default namespace by which we can access all 6660 * attributes created on Solaris. 6661 */ 6662 prefix = namespace = suffix = ""; 6663#endif 6664 break; 6665 case EXTATTR_NAMESPACE_SYSTEM: 6666 prefix = "freebsd:"; 6667 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING; 6668 suffix = ":"; 6669 break; 6670 case EXTATTR_NAMESPACE_EMPTY: 6671 default: 6672 return (EINVAL); 6673 } 6674 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix, 6675 name) >= size) { 6676 return (ENAMETOOLONG); 6677 } 6678 return (0); 6679} 6680 6681/* 6682 * Vnode operating to retrieve a named extended attribute. 6683 */ 6684static int 6685zfs_getextattr(struct vop_getextattr_args *ap) 6686/* 6687vop_getextattr { 6688 IN struct vnode *a_vp; 6689 IN int a_attrnamespace; 6690 IN const char *a_name; 6691 INOUT struct uio *a_uio; 6692 OUT size_t *a_size; 6693 IN struct ucred *a_cred; 6694 IN struct thread *a_td; 6695}; 6696*/ 6697{ 6698 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6699 struct thread *td = ap->a_td; 6700 struct nameidata nd; 6701 char attrname[255]; 6702 struct vattr va; 6703 vnode_t *xvp = NULL, *vp; 6704 int error, flags; 6705 6706 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6707 ap->a_cred, ap->a_td, VREAD); 6708 if (error != 0) 6709 return (error); 6710 6711 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 6712 sizeof(attrname)); 6713 if (error != 0) 6714 return (error); 6715 6716 ZFS_ENTER(zfsvfs); 6717 6718 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6719 LOOKUP_XATTR); 6720 if (error != 0) { 6721 ZFS_EXIT(zfsvfs); 6722 return (error); 6723 } 6724 6725 flags = FREAD; 6726 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, 6727 xvp, td); 6728 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL); 6729 vp = nd.ni_vp; 6730 NDFREE(&nd, NDF_ONLY_PNBUF); 6731 if (error != 0) { 6732 ZFS_EXIT(zfsvfs); 6733 if (error == ENOENT) 6734 error = ENOATTR; 6735 return (error); 6736 } 6737 6738 if (ap->a_size != NULL) { 6739 error = VOP_GETATTR(vp, &va, ap->a_cred); 6740 if (error == 0) 6741 *ap->a_size = (size_t)va.va_size; 6742 } else if (ap->a_uio != NULL) 6743 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred); 6744 6745 VOP_UNLOCK(vp, 0); 6746 vn_close(vp, flags, ap->a_cred, td); 6747 ZFS_EXIT(zfsvfs); 6748 6749 return (error); 6750} 6751 6752/* 6753 * Vnode operation to remove a named attribute. 6754 */ 6755int 6756zfs_deleteextattr(struct vop_deleteextattr_args *ap) 6757/* 6758vop_deleteextattr { 6759 IN struct vnode *a_vp; 6760 IN int a_attrnamespace; 6761 IN const char *a_name; 6762 IN struct ucred *a_cred; 6763 IN struct thread *a_td; 6764}; 6765*/ 6766{ 6767 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6768 struct thread *td = ap->a_td; 6769 struct nameidata nd; 6770 char attrname[255]; 6771 struct vattr va; 6772 vnode_t *xvp = NULL, *vp; 6773 int error, flags; 6774 6775 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6776 ap->a_cred, ap->a_td, VWRITE); 6777 if (error != 0) 6778 return (error); 6779 6780 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 6781 sizeof(attrname)); 6782 if (error != 0) 6783 return (error); 6784 6785 ZFS_ENTER(zfsvfs); 6786 6787 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6788 LOOKUP_XATTR); 6789 if (error != 0) { 6790 ZFS_EXIT(zfsvfs); 6791 return (error); 6792 } 6793 6794 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF, 6795 UIO_SYSSPACE, attrname, xvp, td); 6796 error = namei(&nd); 6797 vp = nd.ni_vp; 6798 if (error != 0) { 6799 ZFS_EXIT(zfsvfs); 6800 NDFREE(&nd, NDF_ONLY_PNBUF); 6801 if (error == ENOENT) 6802 error = ENOATTR; 6803 return (error); 6804 } 6805 6806 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd); 6807 NDFREE(&nd, NDF_ONLY_PNBUF); 6808 6809 vput(nd.ni_dvp); 6810 if (vp == nd.ni_dvp) 6811 vrele(vp); 6812 else 6813 vput(vp); 6814 ZFS_EXIT(zfsvfs); 6815 6816 return (error); 6817} 6818 6819/* 6820 * Vnode operation to set a named attribute. 6821 */ 6822static int 6823zfs_setextattr(struct vop_setextattr_args *ap) 6824/* 6825vop_setextattr { 6826 IN struct vnode *a_vp; 6827 IN int a_attrnamespace; 6828 IN const char *a_name; 6829 INOUT struct uio *a_uio; 6830 IN struct ucred *a_cred; 6831 IN struct thread *a_td; 6832}; 6833*/ 6834{ 6835 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6836 struct thread *td = ap->a_td; 6837 struct nameidata nd; 6838 char attrname[255]; 6839 struct vattr va; 6840 vnode_t *xvp = NULL, *vp; 6841 int error, flags; 6842 6843 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6844 ap->a_cred, ap->a_td, VWRITE); 6845 if (error != 0) 6846 return (error); 6847 6848 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 6849 sizeof(attrname)); 6850 if (error != 0) 6851 return (error); 6852 6853 ZFS_ENTER(zfsvfs); 6854 6855 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6856 LOOKUP_XATTR | CREATE_XATTR_DIR); 6857 if (error != 0) { 6858 ZFS_EXIT(zfsvfs); 6859 return (error); 6860 } 6861 6862 flags = FFLAGS(O_WRONLY | O_CREAT); 6863 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, 6864 xvp, td); 6865 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL); 6866 vp = nd.ni_vp; 6867 NDFREE(&nd, NDF_ONLY_PNBUF); 6868 if (error != 0) { 6869 ZFS_EXIT(zfsvfs); 6870 return (error); 6871 } 6872 6873 VATTR_NULL(&va); 6874 va.va_size = 0; 6875 error = VOP_SETATTR(vp, &va, ap->a_cred); 6876 if (error == 0) 6877 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred); 6878 6879 VOP_UNLOCK(vp, 0); 6880 vn_close(vp, flags, ap->a_cred, td); 6881 ZFS_EXIT(zfsvfs); 6882 6883 return (error); 6884} 6885 6886/* 6887 * Vnode operation to retrieve extended attributes on a vnode. 6888 */ 6889static int 6890zfs_listextattr(struct vop_listextattr_args *ap) 6891/* 6892vop_listextattr { 6893 IN struct vnode *a_vp; 6894 IN int a_attrnamespace; 6895 INOUT struct uio *a_uio; 6896 OUT size_t *a_size; 6897 IN struct ucred *a_cred; 6898 IN struct thread *a_td; 6899}; 6900*/ 6901{ 6902 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6903 struct thread *td = ap->a_td; 6904 struct nameidata nd; 6905 char attrprefix[16]; 6906 u_char dirbuf[sizeof(struct dirent)]; 6907 struct dirent *dp; 6908 struct iovec aiov; 6909 struct uio auio, *uio = ap->a_uio; 6910 size_t *sizep = ap->a_size; 6911 size_t plen; 6912 vnode_t *xvp = NULL, *vp; 6913 int done, error, eof, pos; 6914 6915 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6916 ap->a_cred, ap->a_td, VREAD); 6917 if (error != 0) 6918 return (error); 6919 6920 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix, 6921 sizeof(attrprefix)); 6922 if (error != 0) 6923 return (error); 6924 plen = strlen(attrprefix); 6925 6926 ZFS_ENTER(zfsvfs); 6927 6928 if (sizep != NULL) 6929 *sizep = 0; 6930 6931 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6932 LOOKUP_XATTR); 6933 if (error != 0) { 6934 ZFS_EXIT(zfsvfs); 6935 /* 6936 * ENOATTR means that the EA directory does not yet exist, 6937 * i.e. there are no extended attributes there. 6938 */ 6939 if (error == ENOATTR) 6940 error = 0; 6941 return (error); 6942 } 6943 6944 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED, 6945 UIO_SYSSPACE, ".", xvp, td); 6946 error = namei(&nd); 6947 vp = nd.ni_vp; 6948 NDFREE(&nd, NDF_ONLY_PNBUF); 6949 if (error != 0) { 6950 ZFS_EXIT(zfsvfs); 6951 return (error); 6952 } 6953 6954 auio.uio_iov = &aiov; 6955 auio.uio_iovcnt = 1; 6956 auio.uio_segflg = UIO_SYSSPACE; 6957 auio.uio_td = td; 6958 auio.uio_rw = UIO_READ; 6959 auio.uio_offset = 0; 6960 6961 do { 6962 u_char nlen; 6963 6964 aiov.iov_base = (void *)dirbuf; 6965 aiov.iov_len = sizeof(dirbuf); 6966 auio.uio_resid = sizeof(dirbuf); 6967 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL); 6968 done = sizeof(dirbuf) - auio.uio_resid; 6969 if (error != 0) 6970 break; 6971 for (pos = 0; pos < done;) { 6972 dp = (struct dirent *)(dirbuf + pos); 6973 pos += dp->d_reclen; 6974 /* 6975 * XXX: Temporarily we also accept DT_UNKNOWN, as this 6976 * is what we get when attribute was created on Solaris. 6977 */ 6978 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN) 6979 continue; 6980 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0) 6981 continue; 6982 else if (strncmp(dp->d_name, attrprefix, plen) != 0) 6983 continue; 6984 nlen = dp->d_namlen - plen; 6985 if (sizep != NULL) 6986 *sizep += 1 + nlen; 6987 else if (uio != NULL) { 6988 /* 6989 * Format of extattr name entry is one byte for 6990 * length and the rest for name. 6991 */ 6992 error = uiomove(&nlen, 1, uio->uio_rw, uio); 6993 if (error == 0) { 6994 error = uiomove(dp->d_name + plen, nlen, 6995 uio->uio_rw, uio); 6996 } 6997 if (error != 0) 6998 break; 6999 } 7000 } 7001 } while (!eof && error == 0); 7002 7003 vput(vp); 7004 ZFS_EXIT(zfsvfs); 7005 7006 return (error); 7007} 7008 7009int 7010zfs_freebsd_getacl(ap) 7011 struct vop_getacl_args /* { 7012 struct vnode *vp; 7013 acl_type_t type; 7014 struct acl *aclp; 7015 struct ucred *cred; 7016 struct thread *td; 7017 } */ *ap; 7018{ 7019 int error; 7020 vsecattr_t vsecattr; 7021 7022 if (ap->a_type != ACL_TYPE_NFS4) 7023 return (EINVAL); 7024 7025 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT; 7026 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL)) 7027 return (error); 7028 7029 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt); 7030 if (vsecattr.vsa_aclentp != NULL) 7031 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz); 7032 7033 return (error); 7034} 7035 7036int 7037zfs_freebsd_setacl(ap) 7038 struct vop_setacl_args /* { 7039 struct vnode *vp; 7040 acl_type_t type; 7041 struct acl *aclp; 7042 struct ucred *cred; 7043 struct thread *td; 7044 } */ *ap; 7045{ 7046 int error; 7047 vsecattr_t vsecattr; 7048 int aclbsize; /* size of acl list in bytes */ 7049 aclent_t *aaclp; 7050 7051 if (ap->a_type != ACL_TYPE_NFS4) 7052 return (EINVAL); 7053 7054 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES) 7055 return (EINVAL); 7056 7057 /* 7058 * With NFSv4 ACLs, chmod(2) may need to add additional entries, 7059 * splitting every entry into two and appending "canonical six" 7060 * entries at the end. Don't allow for setting an ACL that would 7061 * cause chmod(2) to run out of ACL entries. 7062 */ 7063 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES) 7064 return (ENOSPC); 7065 7066 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR); 7067 if (error != 0) 7068 return (error); 7069 7070 vsecattr.vsa_mask = VSA_ACE; 7071 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t); 7072 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP); 7073 aaclp = vsecattr.vsa_aclentp; 7074 vsecattr.vsa_aclentsz = aclbsize; 7075 7076 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp); 7077 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL); 7078 kmem_free(aaclp, aclbsize); 7079 7080 return (error); 7081} 7082 7083int 7084zfs_freebsd_aclcheck(ap) 7085 struct vop_aclcheck_args /* { 7086 struct vnode *vp; 7087 acl_type_t type; 7088 struct acl *aclp; 7089 struct ucred *cred; 7090 struct thread *td; 7091 } */ *ap; 7092{ 7093 7094 return (EOPNOTSUPP); 7095} 7096 7097struct vop_vector zfs_vnodeops; 7098struct vop_vector zfs_fifoops; 7099struct vop_vector zfs_shareops; 7100 7101struct vop_vector zfs_vnodeops = { 7102 .vop_default = &default_vnodeops, 7103 .vop_inactive = zfs_freebsd_inactive, 7104 .vop_reclaim = zfs_freebsd_reclaim, 7105 .vop_access = zfs_freebsd_access, 7106#ifdef FREEBSD_NAMECACHE 7107 .vop_lookup = vfs_cache_lookup, 7108 .vop_cachedlookup = zfs_freebsd_lookup, 7109#else 7110 .vop_lookup = zfs_freebsd_lookup, 7111#endif 7112 .vop_getattr = zfs_freebsd_getattr, 7113 .vop_setattr = zfs_freebsd_setattr, 7114 .vop_create = zfs_freebsd_create, 7115 .vop_mknod = zfs_freebsd_create, 7116 .vop_mkdir = zfs_freebsd_mkdir, 7117 .vop_readdir = zfs_freebsd_readdir, 7118 .vop_fsync = zfs_freebsd_fsync, 7119 .vop_open = zfs_freebsd_open, 7120 .vop_close = zfs_freebsd_close, 7121 .vop_rmdir = zfs_freebsd_rmdir, 7122 .vop_ioctl = zfs_freebsd_ioctl, 7123 .vop_link = zfs_freebsd_link, 7124 .vop_symlink = zfs_freebsd_symlink, 7125 .vop_readlink = zfs_freebsd_readlink, 7126 .vop_read = zfs_freebsd_read, 7127 .vop_write = zfs_freebsd_write, 7128 .vop_remove = zfs_freebsd_remove, 7129 .vop_rename = zfs_freebsd_rename, 7130 .vop_pathconf = zfs_freebsd_pathconf, 7131 .vop_bmap = zfs_freebsd_bmap, 7132 .vop_fid = zfs_freebsd_fid, 7133 .vop_getextattr = zfs_getextattr, 7134 .vop_deleteextattr = zfs_deleteextattr, 7135 .vop_setextattr = zfs_setextattr, 7136 .vop_listextattr = zfs_listextattr, 7137 .vop_getacl = zfs_freebsd_getacl, 7138 .vop_setacl = zfs_freebsd_setacl, 7139 .vop_aclcheck = zfs_freebsd_aclcheck, 7140 .vop_getpages = zfs_freebsd_getpages, 7141 .vop_putpages = zfs_freebsd_putpages, 7142}; 7143 7144struct vop_vector zfs_fifoops = { 7145 .vop_default = &fifo_specops, 7146 .vop_fsync = zfs_freebsd_fsync, 7147 .vop_access = zfs_freebsd_access, 7148 .vop_getattr = zfs_freebsd_getattr, 7149 .vop_inactive = zfs_freebsd_inactive, 7150 .vop_read = VOP_PANIC, 7151 .vop_reclaim = zfs_freebsd_reclaim, 7152 .vop_setattr = zfs_freebsd_setattr, 7153 .vop_write = VOP_PANIC, 7154 .vop_pathconf = zfs_freebsd_fifo_pathconf, 7155 .vop_fid = zfs_freebsd_fid, 7156 .vop_getacl = zfs_freebsd_getacl, 7157 .vop_setacl = zfs_freebsd_setacl, 7158 .vop_aclcheck = zfs_freebsd_aclcheck, 7159}; 7160 7161/* 7162 * special share hidden files vnode operations template 7163 */ 7164struct vop_vector zfs_shareops = { 7165 .vop_default = &default_vnodeops, 7166 .vop_access = zfs_freebsd_access, 7167 .vop_inactive = zfs_freebsd_inactive, 7168 .vop_reclaim = zfs_freebsd_reclaim, 7169 .vop_fid = zfs_freebsd_fid, 7170 .vop_pathconf = zfs_freebsd_pathconf, 7171}; 7172