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