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