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.
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.
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]
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.
27 /* Portions Copyright 2007 Jeremy Teo */
28 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
38 #include <sys/vnode.h>
42 #include <sys/taskq.h>
44 #include <sys/atomic.h>
45 #include <sys/namei.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>
54 #include <sys/dmu_objset.h>
60 #include <sys/dirent.h>
61 #include <sys/policy.h>
62 #include <sys/sunddi.h>
63 #include <sys/filio.h>
65 #include <sys/zfs_ctldir.h>
66 #include <sys/zfs_fuid.h>
67 #include <sys/zfs_sa.h>
69 #include <sys/zfs_rlock.h>
70 #include <sys/extdirent.h>
71 #include <sys/kidmap.h>
74 #include <sys/sched.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_pageout.h>
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:
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.
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().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
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.
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
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:
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.
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
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.
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
146 * In general, this is how things should be ordered in each vnode op:
148 * ZFS_ENTER(zfsvfs); // exit if unmounted
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);
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * VN_RELE(...); // release held vnodes
159 * if (error == ERESTART) {
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zfsvfs); // finished in zfs
167 * return (error); // really out of space
169 * error = do_real_work(); // do whatever this VOP does
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
183 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
185 znode_t *zp = VTOZ(*vpp);
186 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
192 ((flag & FAPPEND) == 0)) {
194 return (SET_ERROR(EPERM));
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) {
202 return (SET_ERROR(EACCES));
206 /* Keep a count of the synchronous opens in the znode */
207 if (flag & (FSYNC | FDSYNC))
208 atomic_inc_32(&zp->z_sync_cnt);
216 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
217 caller_context_t *ct)
219 znode_t *zp = VTOZ(vp);
220 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
223 * Clean up any locks held by this process on the vp.
225 cleanlocks(vp, ddi_get_pid(), 0);
226 cleanshares(vp, ddi_get_pid());
231 /* Decrement the synchronous opens in the znode */
232 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
233 atomic_dec_32(&zp->z_sync_cnt);
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);
245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
251 znode_t *zp = VTOZ(vp);
252 uint64_t noff = (uint64_t)*off; /* new offset */
257 file_sz = zp->z_size;
258 if (noff >= file_sz) {
259 return (SET_ERROR(ENXIO));
262 if (cmd == _FIO_SEEK_HOLE)
267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
270 return (SET_ERROR(ENXIO));
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.
279 if (noff > file_sz) {
292 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
293 int *rvalp, caller_context_t *ct)
305 * The following two ioctls are used by bfu. Faking out,
306 * necessary to avoid bfu errors.
315 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
316 return (SET_ERROR(EFAULT));
318 off = *(offset_t *)data;
321 zfsvfs = zp->z_zfsvfs;
325 /* offset parameter is in/out */
326 error = zfs_holey(vp, com, &off);
331 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
332 return (SET_ERROR(EFAULT));
334 *(offset_t *)data = off;
338 return (SET_ERROR(ENOTTY));
342 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
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.
357 end = rounddown2(off + nbytes, DEV_BSIZE);
358 off = roundup2(off, DEV_BSIZE);
362 zfs_vmobject_assert_wlocked(obj);
365 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
367 if (vm_page_xbusied(pp)) {
369 * Reference the page before unlocking and
370 * sleeping so that the page daemon is less
371 * likely to reclaim it.
373 vm_page_reference(pp);
375 zfs_vmobject_wunlock(obj);
376 vm_page_busy_sleep(pp, "zfsmwb");
377 zfs_vmobject_wlock(obj);
381 } else if (pp == NULL) {
382 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
383 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
386 ASSERT(pp != NULL && !pp->valid);
391 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
392 vm_object_pip_add(obj, 1);
393 pmap_remove_write(pp);
395 vm_page_clear_dirty(pp, off, nbytes);
403 page_unbusy(vm_page_t pp)
407 vm_object_pip_subtract(pp->object, 1);
411 page_hold(vnode_t *vp, int64_t start)
417 zfs_vmobject_assert_wlocked(obj);
420 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
422 if (vm_page_xbusied(pp)) {
424 * Reference the page before unlocking and
425 * sleeping so that the page daemon is less
426 * likely to reclaim it.
428 vm_page_reference(pp);
430 zfs_vmobject_wunlock(obj);
431 vm_page_busy_sleep(pp, "zfsmwb");
432 zfs_vmobject_wlock(obj);
436 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
449 page_unhold(vm_page_t pp)
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:
461 * On Write: If we find a memory mapped page, we write to *both*
462 * the page and the dmu buffer.
465 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
466 int segflg, dmu_tx_t *tx)
473 ASSERT(segflg != UIO_NOCOPY);
474 ASSERT(vp->v_mount != NULL);
478 off = start & PAGEOFFSET;
479 zfs_vmobject_wlock(obj);
480 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
482 int nbytes = imin(PAGESIZE - off, len);
484 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
485 zfs_vmobject_wunlock(obj);
487 va = zfs_map_page(pp, &sf);
488 (void) dmu_read(os, oid, start+off, nbytes,
489 va+off, DMU_READ_PREFETCH);;
492 zfs_vmobject_wlock(obj);
498 vm_object_pip_wakeupn(obj, 0);
499 zfs_vmobject_wunlock(obj);
503 * Read with UIO_NOCOPY flag means that sendfile(2) requests
504 * ZFS to populate a range of page cache pages with data.
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.
512 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
514 znode_t *zp = VTOZ(vp);
515 objset_t *os = zp->z_zfsvfs->z_os;
525 ASSERT(uio->uio_segflg == UIO_NOCOPY);
526 ASSERT(vp->v_mount != NULL);
529 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
531 zfs_vmobject_wlock(obj);
532 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
533 int bytes = MIN(PAGESIZE, len);
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,
542 if (bytes != PAGESIZE && error == 0)
543 bzero(va + bytes, PAGESIZE - bytes);
545 zfs_vmobject_wlock(obj);
549 if (pp->wire_count == 0 && pp->valid == 0 &&
553 pp->valid = VM_PAGE_BITS_ALL;
554 vm_page_activate(pp);
558 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
563 uio->uio_resid -= bytes;
564 uio->uio_offset += bytes;
567 zfs_vmobject_wunlock(obj);
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:
575 * On Read: We "read" preferentially from memory mapped pages,
576 * else we default from the dmu buffer.
578 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
579 * the file is memory mapped.
582 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
584 znode_t *zp = VTOZ(vp);
592 ASSERT(vp->v_mount != NULL);
596 start = uio->uio_loffset;
597 off = start & PAGEOFFSET;
598 zfs_vmobject_wlock(obj);
599 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
601 uint64_t bytes = MIN(PAGESIZE - off, len);
603 if (pp = page_hold(vp, start)) {
607 zfs_vmobject_wunlock(obj);
608 va = zfs_map_page(pp, &sf);
609 error = uiomove(va + off, bytes, UIO_READ, uio);
611 zfs_vmobject_wlock(obj);
614 zfs_vmobject_wunlock(obj);
615 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
617 zfs_vmobject_wlock(obj);
624 zfs_vmobject_wunlock(obj);
628 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
631 * Read bytes from specified file into supplied buffer.
633 * IN: vp - vnode of file to be read from.
634 * uio - structure supplying read location, range info,
636 * ioflag - SYNC flags; used to provide FRSYNC semantics.
637 * cr - credentials of caller.
638 * ct - caller context
640 * OUT: uio - updated offset and range, buffer filled.
642 * RETURN: 0 on success, error code on failure.
645 * vp - atime updated if byte count > 0
649 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
651 znode_t *zp = VTOZ(vp);
652 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
661 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
663 return (SET_ERROR(EACCES));
667 * Validate file offset
669 if (uio->uio_loffset < (offset_t)0) {
671 return (SET_ERROR(EINVAL));
675 * Fasttrack empty reads
677 if (uio->uio_resid == 0) {
683 * Check for mandatory locks
685 if (MANDMODE(zp->z_mode)) {
686 if (error = chklock(vp, FREAD,
687 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
694 * If we're in FRSYNC mode, sync out this znode before reading it.
697 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
698 zil_commit(zfsvfs->z_log, zp->z_id);
701 * Lock the range against changes.
703 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
706 * If we are reading past end-of-file we can skip
707 * to the end; but we might still need to set atime.
709 if (uio->uio_loffset >= zp->z_size) {
714 ASSERT(uio->uio_loffset < zp->z_size);
715 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
718 if ((uio->uio_extflg == UIO_XUIO) &&
719 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
721 int blksz = zp->z_blksz;
722 uint64_t offset = uio->uio_loffset;
724 xuio = (xuio_t *)uio;
726 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
729 ASSERT(offset + n <= blksz);
732 (void) dmu_xuio_init(xuio, nblk);
734 if (vn_has_cached_data(vp)) {
736 * For simplicity, we always allocate a full buffer
737 * even if we only expect to read a portion of a block.
739 while (--nblk >= 0) {
740 (void) dmu_xuio_add(xuio,
741 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
749 nbytes = MIN(n, zfs_read_chunk_size -
750 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
753 if (uio->uio_segflg == UIO_NOCOPY)
754 error = mappedread_sf(vp, nbytes, uio);
756 #endif /* __FreeBSD__ */
757 if (vn_has_cached_data(vp)) {
758 error = mappedread(vp, nbytes, uio);
760 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
764 /* convert checksum errors into IO errors */
766 error = SET_ERROR(EIO);
773 zfs_range_unlock(rl);
775 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
781 * Write the bytes to a file.
783 * IN: vp - vnode of file to be written to.
784 * uio - structure supplying write location, range info,
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)
791 * OUT: uio - updated offset and range.
793 * RETURN: 0 on success, error code on failure.
796 * vp - ctime|mtime updated if byte count > 0
801 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
803 znode_t *zp = VTOZ(vp);
804 rlim64_t limit = MAXOFFSET_T;
805 ssize_t start_resid = uio->uio_resid;
809 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
814 int max_blksz = zfsvfs->z_max_blksz;
817 iovec_t *aiov = NULL;
820 int iovcnt = uio->uio_iovcnt;
821 iovec_t *iovp = uio->uio_iov;
824 sa_bulk_attr_t bulk[4];
825 uint64_t mtime[2], ctime[2];
828 * Fasttrack empty write
834 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
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,
844 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
848 * If immutable or not appending then return EPERM
850 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
851 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
852 (uio->uio_loffset < zp->z_size))) {
854 return (SET_ERROR(EPERM));
857 zilog = zfsvfs->z_log;
860 * Validate file offset
862 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
865 return (SET_ERROR(EINVAL));
869 * Check for mandatory locks before calling zfs_range_lock()
870 * in order to prevent a deadlock with locks set via fcntl().
872 if (MANDMODE((mode_t)zp->z_mode) &&
873 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
880 * Pre-fault the pages to ensure slow (eg NFS) pages
882 * Skip this if uio contains loaned arc_buf.
884 if ((uio->uio_extflg == UIO_XUIO) &&
885 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
886 xuio = (xuio_t *)uio;
888 uio_prefaultpages(MIN(n, max_blksz), uio);
892 * If in append mode, set the io offset pointer to eof.
894 if (ioflag & FAPPEND) {
896 * Obtain an appending range lock to guarantee file append
897 * semantics. We reset the write offset once we have the lock.
899 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
901 if (rl->r_len == UINT64_MAX) {
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.
909 uio->uio_loffset = woff;
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.
916 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
919 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
920 zfs_range_unlock(rl);
926 zfs_range_unlock(rl);
928 return (SET_ERROR(EFBIG));
931 if ((woff + n) > limit || woff > (limit - n))
934 /* Will this write extend the file length? */
935 write_eof = (woff + n > zp->z_size);
937 end_size = MAX(zp->z_size, woff + n);
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.
946 woff = uio->uio_loffset;
947 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
948 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
950 dmu_return_arcbuf(abuf);
951 error = SET_ERROR(EDQUOT);
955 if (xuio && abuf == NULL) {
956 ASSERT(i_iov < iovcnt);
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)));
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) {
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).
979 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
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);
988 ASSERT(cbytes == max_blksz);
992 * Start a transaction.
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);
1002 dmu_return_arcbuf(abuf);
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.
1012 if (rl->r_len == UINT64_MAX) {
1015 if (zp->z_blksz > max_blksz) {
1017 * File's blocksize is already larger than the
1018 * "recordsize" property. Only let it grow to
1019 * the next power of 2.
1021 ASSERT(!ISP2(zp->z_blksz));
1022 new_blksz = MIN(end_size,
1023 1 << highbit64(zp->z_blksz));
1025 new_blksz = MIN(end_size, max_blksz);
1027 zfs_grow_blocksize(zp, new_blksz, tx);
1028 zfs_range_reduce(rl, woff, n);
1032 * XXX - should we really limit each write to z_max_blksz?
1033 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1035 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1037 if (woff + nbytes > zp->z_size)
1038 vnode_pager_setsize(vp, woff + nbytes);
1041 tx_bytes = uio->uio_resid;
1042 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1044 tx_bytes -= uio->uio_resid;
1047 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1049 * If this is not a full block write, but we are
1050 * extending the file past EOF and this data starts
1051 * block-aligned, use assign_arcbuf(). Otherwise,
1052 * write via dmu_write().
1054 if (tx_bytes < max_blksz && (!write_eof ||
1055 aiov->iov_base != abuf->b_data)) {
1057 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1058 aiov->iov_len, aiov->iov_base, tx);
1059 dmu_return_arcbuf(abuf);
1060 xuio_stat_wbuf_copied();
1062 ASSERT(xuio || tx_bytes == max_blksz);
1063 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1066 ASSERT(tx_bytes <= uio->uio_resid);
1067 uioskip(uio, tx_bytes);
1069 if (tx_bytes && vn_has_cached_data(vp)) {
1070 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1071 zp->z_id, uio->uio_segflg, tx);
1075 * If we made no progress, we're done. If we made even
1076 * partial progress, update the znode and ZIL accordingly.
1078 if (tx_bytes == 0) {
1079 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1080 (void *)&zp->z_size, sizeof (uint64_t), tx);
1087 * Clear Set-UID/Set-GID bits on successful write if not
1088 * privileged and at least one of the excute bits is set.
1090 * It would be nice to to this after all writes have
1091 * been done, but that would still expose the ISUID/ISGID
1092 * to another app after the partial write is committed.
1094 * Note: we don't call zfs_fuid_map_id() here because
1095 * user 0 is not an ephemeral uid.
1097 mutex_enter(&zp->z_acl_lock);
1098 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1099 (S_IXUSR >> 6))) != 0 &&
1100 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1101 secpolicy_vnode_setid_retain(vp, cr,
1102 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1104 zp->z_mode &= ~(S_ISUID | S_ISGID);
1105 newmode = zp->z_mode;
1106 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1107 (void *)&newmode, sizeof (uint64_t), tx);
1109 mutex_exit(&zp->z_acl_lock);
1111 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1115 * Update the file size (zp_size) if it has changed;
1116 * account for possible concurrent updates.
1118 while ((end_size = zp->z_size) < uio->uio_loffset) {
1119 (void) atomic_cas_64(&zp->z_size, end_size,
1124 * If we are replaying and eof is non zero then force
1125 * the file size to the specified eof. Note, there's no
1126 * concurrency during replay.
1128 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1129 zp->z_size = zfsvfs->z_replay_eof;
1131 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1133 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1138 ASSERT(tx_bytes == nbytes);
1143 uio_prefaultpages(MIN(n, max_blksz), uio);
1147 zfs_range_unlock(rl);
1150 * If we're in replay mode, or we made no progress, return error.
1151 * Otherwise, it's at least a partial write, so it's successful.
1153 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1158 if (ioflag & (FSYNC | FDSYNC) ||
1159 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1160 zil_commit(zilog, zp->z_id);
1167 zfs_get_done(zgd_t *zgd, int error)
1169 znode_t *zp = zgd->zgd_private;
1170 objset_t *os = zp->z_zfsvfs->z_os;
1173 dmu_buf_rele(zgd->zgd_db, zgd);
1175 zfs_range_unlock(zgd->zgd_rl);
1178 * Release the vnode asynchronously as we currently have the
1179 * txg stopped from syncing.
1181 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1183 if (error == 0 && zgd->zgd_bp)
1184 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1186 kmem_free(zgd, sizeof (zgd_t));
1190 static int zil_fault_io = 0;
1194 * Get data to generate a TX_WRITE intent log record.
1197 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1199 zfsvfs_t *zfsvfs = arg;
1200 objset_t *os = zfsvfs->z_os;
1202 uint64_t object = lr->lr_foid;
1203 uint64_t offset = lr->lr_offset;
1204 uint64_t size = lr->lr_length;
1205 blkptr_t *bp = &lr->lr_blkptr;
1210 ASSERT(zio != NULL);
1214 * Nothing to do if the file has been removed
1216 if (zfs_zget(zfsvfs, object, &zp) != 0)
1217 return (SET_ERROR(ENOENT));
1218 if (zp->z_unlinked) {
1220 * Release the vnode asynchronously as we currently have the
1221 * txg stopped from syncing.
1223 VN_RELE_ASYNC(ZTOV(zp),
1224 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1225 return (SET_ERROR(ENOENT));
1228 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1229 zgd->zgd_zilog = zfsvfs->z_log;
1230 zgd->zgd_private = zp;
1233 * Write records come in two flavors: immediate and indirect.
1234 * For small writes it's cheaper to store the data with the
1235 * log record (immediate); for large writes it's cheaper to
1236 * sync the data and get a pointer to it (indirect) so that
1237 * we don't have to write the data twice.
1239 if (buf != NULL) { /* immediate write */
1240 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1241 /* test for truncation needs to be done while range locked */
1242 if (offset >= zp->z_size) {
1243 error = SET_ERROR(ENOENT);
1245 error = dmu_read(os, object, offset, size, buf,
1246 DMU_READ_NO_PREFETCH);
1248 ASSERT(error == 0 || error == ENOENT);
1249 } else { /* indirect write */
1251 * Have to lock the whole block to ensure when it's
1252 * written out and it's checksum is being calculated
1253 * that no one can change the data. We need to re-check
1254 * blocksize after we get the lock in case it's changed!
1259 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1261 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1263 if (zp->z_blksz == size)
1266 zfs_range_unlock(zgd->zgd_rl);
1268 /* test for truncation needs to be done while range locked */
1269 if (lr->lr_offset >= zp->z_size)
1270 error = SET_ERROR(ENOENT);
1273 error = SET_ERROR(EIO);
1278 error = dmu_buf_hold(os, object, offset, zgd, &db,
1279 DMU_READ_NO_PREFETCH);
1282 blkptr_t *obp = dmu_buf_get_blkptr(db);
1284 ASSERT(BP_IS_HOLE(bp));
1291 ASSERT(db->db_offset == offset);
1292 ASSERT(db->db_size == size);
1294 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1296 ASSERT(error || lr->lr_length <= zp->z_blksz);
1299 * On success, we need to wait for the write I/O
1300 * initiated by dmu_sync() to complete before we can
1301 * release this dbuf. We will finish everything up
1302 * in the zfs_get_done() callback.
1307 if (error == EALREADY) {
1308 lr->lr_common.lrc_txtype = TX_WRITE2;
1314 zfs_get_done(zgd, error);
1321 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1322 caller_context_t *ct)
1324 znode_t *zp = VTOZ(vp);
1325 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1331 if (flag & V_ACE_MASK)
1332 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1334 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1341 * If vnode is for a device return a specfs vnode instead.
1344 specvp_check(vnode_t **vpp, cred_t *cr)
1348 if (IS_DEVVP(*vpp)) {
1351 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1354 error = SET_ERROR(ENOSYS);
1362 * Lookup an entry in a directory, or an extended attribute directory.
1363 * If it exists, return a held vnode reference for it.
1365 * IN: dvp - vnode of directory to search.
1366 * nm - name of entry to lookup.
1367 * pnp - full pathname to lookup [UNUSED].
1368 * flags - LOOKUP_XATTR set if looking for an attribute.
1369 * rdir - root directory vnode [UNUSED].
1370 * cr - credentials of caller.
1371 * ct - caller context
1372 * direntflags - directory lookup flags
1373 * realpnp - returned pathname.
1375 * OUT: vpp - vnode of located entry, NULL if not found.
1377 * RETURN: 0 on success, error code on failure.
1384 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1385 int nameiop, cred_t *cr, kthread_t *td, int flags)
1387 znode_t *zdp = VTOZ(dvp);
1388 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1390 int *direntflags = NULL;
1391 void *realpnp = NULL;
1394 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1396 if (dvp->v_type != VDIR) {
1397 return (SET_ERROR(ENOTDIR));
1398 } else if (zdp->z_sa_hdl == NULL) {
1399 return (SET_ERROR(EIO));
1402 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1403 error = zfs_fastaccesschk_execute(zdp, cr);
1411 vnode_t *tvp = dnlc_lookup(dvp, nm);
1414 error = zfs_fastaccesschk_execute(zdp, cr);
1419 if (tvp == DNLC_NO_VNODE) {
1421 return (SET_ERROR(ENOENT));
1424 return (specvp_check(vpp, cr));
1430 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1437 if (flags & LOOKUP_XATTR) {
1440 * If the xattr property is off, refuse the lookup request.
1442 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1444 return (SET_ERROR(EINVAL));
1449 * We don't allow recursive attributes..
1450 * Maybe someday we will.
1452 if (zdp->z_pflags & ZFS_XATTR) {
1454 return (SET_ERROR(EINVAL));
1457 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1463 * Do we have permission to get into attribute directory?
1466 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1476 if (dvp->v_type != VDIR) {
1478 return (SET_ERROR(ENOTDIR));
1482 * Check accessibility of directory.
1485 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1490 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1491 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1493 return (SET_ERROR(EILSEQ));
1496 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1498 error = specvp_check(vpp, cr);
1500 /* Translate errors and add SAVENAME when needed. */
1501 if (cnp->cn_flags & ISLASTCN) {
1505 if (error == ENOENT) {
1506 error = EJUSTRETURN;
1507 cnp->cn_flags |= SAVENAME;
1513 cnp->cn_flags |= SAVENAME;
1517 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1520 if (cnp->cn_flags & ISDOTDOT) {
1521 ltype = VOP_ISLOCKED(dvp);
1525 error = vn_lock(*vpp, cnp->cn_lkflags);
1526 if (cnp->cn_flags & ISDOTDOT)
1527 vn_lock(dvp, ltype | LK_RETRY);
1537 #ifdef FREEBSD_NAMECACHE
1539 * Insert name into cache (as non-existent) if appropriate.
1541 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1542 cache_enter(dvp, *vpp, cnp);
1544 * Insert name into cache if appropriate.
1546 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1547 if (!(cnp->cn_flags & ISLASTCN) ||
1548 (nameiop != DELETE && nameiop != RENAME)) {
1549 cache_enter(dvp, *vpp, cnp);
1558 * Attempt to create a new entry in a directory. If the entry
1559 * already exists, truncate the file if permissible, else return
1560 * an error. Return the vp of the created or trunc'd file.
1562 * IN: dvp - vnode of directory to put new file entry in.
1563 * name - name of new file entry.
1564 * vap - attributes of new file.
1565 * excl - flag indicating exclusive or non-exclusive mode.
1566 * mode - mode to open file with.
1567 * cr - credentials of caller.
1568 * flag - large file flag [UNUSED].
1569 * ct - caller context
1570 * vsecp - ACL to be set
1572 * OUT: vpp - vnode of created or trunc'd entry.
1574 * RETURN: 0 on success, error code on failure.
1577 * dvp - ctime|mtime updated if new entry created
1578 * vp - ctime|mtime always, atime if new
1583 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1584 vnode_t **vpp, cred_t *cr, kthread_t *td)
1586 znode_t *zp, *dzp = VTOZ(dvp);
1587 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1595 gid_t gid = crgetgid(cr);
1596 zfs_acl_ids_t acl_ids;
1597 boolean_t fuid_dirtied;
1598 boolean_t have_acl = B_FALSE;
1599 boolean_t waited = B_FALSE;
1604 * If we have an ephemeral id, ACL, or XVATTR then
1605 * make sure file system is at proper version
1608 ksid = crgetsid(cr, KSID_OWNER);
1610 uid = ksid_getid(ksid);
1614 if (zfsvfs->z_use_fuids == B_FALSE &&
1615 (vsecp || (vap->va_mask & AT_XVATTR) ||
1616 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1617 return (SET_ERROR(EINVAL));
1622 zilog = zfsvfs->z_log;
1624 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1625 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1627 return (SET_ERROR(EILSEQ));
1630 if (vap->va_mask & AT_XVATTR) {
1631 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1632 crgetuid(cr), cr, vap->va_type)) != 0) {
1638 getnewvnode_reserve(1);
1643 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1644 vap->va_mode &= ~S_ISVTX;
1646 if (*name == '\0') {
1648 * Null component name refers to the directory itself.
1655 /* possible VN_HOLD(zp) */
1658 if (flag & FIGNORECASE)
1661 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1665 zfs_acl_ids_free(&acl_ids);
1666 if (strcmp(name, "..") == 0)
1667 error = SET_ERROR(EISDIR);
1668 getnewvnode_drop_reserve();
1678 * Create a new file object and update the directory
1681 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1683 zfs_acl_ids_free(&acl_ids);
1688 * We only support the creation of regular files in
1689 * extended attribute directories.
1692 if ((dzp->z_pflags & ZFS_XATTR) &&
1693 (vap->va_type != VREG)) {
1695 zfs_acl_ids_free(&acl_ids);
1696 error = SET_ERROR(EINVAL);
1700 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1701 cr, vsecp, &acl_ids)) != 0)
1705 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1706 zfs_acl_ids_free(&acl_ids);
1707 error = SET_ERROR(EDQUOT);
1711 tx = dmu_tx_create(os);
1713 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1714 ZFS_SA_BASE_ATTR_SIZE);
1716 fuid_dirtied = zfsvfs->z_fuid_dirty;
1718 zfs_fuid_txhold(zfsvfs, tx);
1719 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1720 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1721 if (!zfsvfs->z_use_sa &&
1722 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1723 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1724 0, acl_ids.z_aclp->z_acl_bytes);
1726 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1728 zfs_dirent_unlock(dl);
1729 if (error == ERESTART) {
1735 zfs_acl_ids_free(&acl_ids);
1737 getnewvnode_drop_reserve();
1741 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1744 zfs_fuid_sync(zfsvfs, tx);
1746 (void) zfs_link_create(dl, zp, tx, ZNEW);
1747 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1748 if (flag & FIGNORECASE)
1750 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1751 vsecp, acl_ids.z_fuidp, vap);
1752 zfs_acl_ids_free(&acl_ids);
1755 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1758 zfs_acl_ids_free(&acl_ids);
1762 * A directory entry already exists for this name.
1765 * Can't truncate an existing file if in exclusive mode.
1768 error = SET_ERROR(EEXIST);
1772 * Can't open a directory for writing.
1774 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1775 error = SET_ERROR(EISDIR);
1779 * Verify requested access to file.
1781 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1785 mutex_enter(&dzp->z_lock);
1787 mutex_exit(&dzp->z_lock);
1790 * Truncate regular files if requested.
1792 if ((ZTOV(zp)->v_type == VREG) &&
1793 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1794 /* we can't hold any locks when calling zfs_freesp() */
1795 zfs_dirent_unlock(dl);
1797 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1799 vnevent_create(ZTOV(zp), ct);
1804 getnewvnode_drop_reserve();
1806 zfs_dirent_unlock(dl);
1813 error = specvp_check(vpp, cr);
1816 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1817 zil_commit(zilog, 0);
1824 * Remove an entry from a directory.
1826 * IN: dvp - vnode of directory to remove entry from.
1827 * name - name of entry to remove.
1828 * cr - credentials of caller.
1829 * ct - caller context
1830 * flags - case flags
1832 * RETURN: 0 on success, error code on failure.
1836 * vp - ctime (if nlink > 0)
1839 uint64_t null_xattr = 0;
1843 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1846 znode_t *zp, *dzp = VTOZ(dvp);
1849 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1851 uint64_t acl_obj, xattr_obj;
1852 uint64_t xattr_obj_unlinked = 0;
1856 boolean_t may_delete_now, delete_now = FALSE;
1857 boolean_t unlinked, toobig = FALSE;
1859 pathname_t *realnmp = NULL;
1863 boolean_t waited = B_FALSE;
1867 zilog = zfsvfs->z_log;
1869 if (flags & FIGNORECASE) {
1879 * Attempt to lock directory; fail if entry doesn't exist.
1881 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1891 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1896 * Need to use rmdir for removing directories.
1898 if (vp->v_type == VDIR) {
1899 error = SET_ERROR(EPERM);
1903 vnevent_remove(vp, dvp, name, ct);
1906 dnlc_remove(dvp, realnmp->pn_buf);
1908 dnlc_remove(dvp, name);
1911 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1915 * We may delete the znode now, or we may put it in the unlinked set;
1916 * it depends on whether we're the last link, and on whether there are
1917 * other holds on the vnode. So we dmu_tx_hold() the right things to
1918 * allow for either case.
1921 tx = dmu_tx_create(zfsvfs->z_os);
1922 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1923 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1924 zfs_sa_upgrade_txholds(tx, zp);
1925 zfs_sa_upgrade_txholds(tx, dzp);
1926 if (may_delete_now) {
1928 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1929 /* if the file is too big, only hold_free a token amount */
1930 dmu_tx_hold_free(tx, zp->z_id, 0,
1931 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1934 /* are there any extended attributes? */
1935 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1936 &xattr_obj, sizeof (xattr_obj));
1937 if (error == 0 && xattr_obj) {
1938 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1940 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1941 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1944 mutex_enter(&zp->z_lock);
1945 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1946 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1947 mutex_exit(&zp->z_lock);
1949 /* charge as an update -- would be nice not to charge at all */
1950 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1953 * Mark this transaction as typically resulting in a net free of
1954 * space, unless object removal will be delayed indefinitely
1955 * (due to active holds on the vnode due to the file being open).
1958 dmu_tx_mark_netfree(tx);
1960 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1962 zfs_dirent_unlock(dl);
1966 if (error == ERESTART) {
1980 * Remove the directory entry.
1982 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1991 * Hold z_lock so that we can make sure that the ACL obj
1992 * hasn't changed. Could have been deleted due to
1995 mutex_enter(&zp->z_lock);
1997 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1998 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1999 delete_now = may_delete_now && !toobig &&
2000 vp->v_count == 1 && !vn_has_cached_data(vp) &&
2001 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
2008 panic("zfs_remove: delete_now branch taken");
2010 if (xattr_obj_unlinked) {
2011 ASSERT3U(xzp->z_links, ==, 2);
2012 mutex_enter(&xzp->z_lock);
2013 xzp->z_unlinked = 1;
2015 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
2016 &xzp->z_links, sizeof (xzp->z_links), tx);
2017 ASSERT3U(error, ==, 0);
2018 mutex_exit(&xzp->z_lock);
2019 zfs_unlinked_add(xzp, tx);
2022 error = sa_remove(zp->z_sa_hdl,
2023 SA_ZPL_XATTR(zfsvfs), tx);
2025 error = sa_update(zp->z_sa_hdl,
2026 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2027 sizeof (uint64_t), tx);
2032 ASSERT0(vp->v_count);
2034 mutex_exit(&zp->z_lock);
2035 zfs_znode_delete(zp, tx);
2036 } else if (unlinked) {
2037 mutex_exit(&zp->z_lock);
2038 zfs_unlinked_add(zp, tx);
2040 vp->v_vflag |= VV_NOSYNC;
2045 if (flags & FIGNORECASE)
2047 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2054 zfs_dirent_unlock(dl);
2061 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2062 zil_commit(zilog, 0);
2069 * Create a new directory and insert it into dvp using the name
2070 * provided. Return a pointer to the inserted directory.
2072 * IN: dvp - vnode of directory to add subdir to.
2073 * dirname - name of new directory.
2074 * vap - attributes of new directory.
2075 * cr - credentials of caller.
2076 * ct - caller context
2077 * flags - case flags
2078 * vsecp - ACL to be set
2080 * OUT: vpp - vnode of created directory.
2082 * RETURN: 0 on success, error code on failure.
2085 * dvp - ctime|mtime updated
2086 * vp - ctime|mtime|atime updated
2090 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2091 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2093 znode_t *zp, *dzp = VTOZ(dvp);
2094 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2103 gid_t gid = crgetgid(cr);
2104 zfs_acl_ids_t acl_ids;
2105 boolean_t fuid_dirtied;
2106 boolean_t waited = B_FALSE;
2108 ASSERT(vap->va_type == VDIR);
2111 * If we have an ephemeral id, ACL, or XVATTR then
2112 * make sure file system is at proper version
2115 ksid = crgetsid(cr, KSID_OWNER);
2117 uid = ksid_getid(ksid);
2120 if (zfsvfs->z_use_fuids == B_FALSE &&
2121 (vsecp || (vap->va_mask & AT_XVATTR) ||
2122 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2123 return (SET_ERROR(EINVAL));
2127 zilog = zfsvfs->z_log;
2129 if (dzp->z_pflags & ZFS_XATTR) {
2131 return (SET_ERROR(EINVAL));
2134 if (zfsvfs->z_utf8 && u8_validate(dirname,
2135 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2137 return (SET_ERROR(EILSEQ));
2139 if (flags & FIGNORECASE)
2142 if (vap->va_mask & AT_XVATTR) {
2143 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2144 crgetuid(cr), cr, vap->va_type)) != 0) {
2150 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2151 vsecp, &acl_ids)) != 0) {
2156 getnewvnode_reserve(1);
2159 * First make sure the new directory doesn't exist.
2161 * Existence is checked first to make sure we don't return
2162 * EACCES instead of EEXIST which can cause some applications
2168 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2170 zfs_acl_ids_free(&acl_ids);
2171 getnewvnode_drop_reserve();
2176 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2177 zfs_acl_ids_free(&acl_ids);
2178 zfs_dirent_unlock(dl);
2179 getnewvnode_drop_reserve();
2184 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2185 zfs_acl_ids_free(&acl_ids);
2186 zfs_dirent_unlock(dl);
2187 getnewvnode_drop_reserve();
2189 return (SET_ERROR(EDQUOT));
2193 * Add a new entry to the directory.
2195 tx = dmu_tx_create(zfsvfs->z_os);
2196 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2197 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2198 fuid_dirtied = zfsvfs->z_fuid_dirty;
2200 zfs_fuid_txhold(zfsvfs, tx);
2201 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2202 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2203 acl_ids.z_aclp->z_acl_bytes);
2206 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2207 ZFS_SA_BASE_ATTR_SIZE);
2209 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2211 zfs_dirent_unlock(dl);
2212 if (error == ERESTART) {
2218 zfs_acl_ids_free(&acl_ids);
2220 getnewvnode_drop_reserve();
2228 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2231 zfs_fuid_sync(zfsvfs, tx);
2234 * Now put new name in parent dir.
2236 (void) zfs_link_create(dl, zp, tx, ZNEW);
2240 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2241 if (flags & FIGNORECASE)
2243 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2244 acl_ids.z_fuidp, vap);
2246 zfs_acl_ids_free(&acl_ids);
2250 getnewvnode_drop_reserve();
2252 zfs_dirent_unlock(dl);
2254 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2255 zil_commit(zilog, 0);
2262 * Remove a directory subdir entry. If the current working
2263 * directory is the same as the subdir to be removed, the
2266 * IN: dvp - vnode of directory to remove from.
2267 * name - name of directory to be removed.
2268 * cwd - vnode of current working directory.
2269 * cr - credentials of caller.
2270 * ct - caller context
2271 * flags - case flags
2273 * RETURN: 0 on success, error code on failure.
2276 * dvp - ctime|mtime updated
2280 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2281 caller_context_t *ct, int flags)
2283 znode_t *dzp = VTOZ(dvp);
2286 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2292 boolean_t waited = B_FALSE;
2296 zilog = zfsvfs->z_log;
2298 if (flags & FIGNORECASE)
2304 * Attempt to lock directory; fail if entry doesn't exist.
2306 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2314 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2318 if (vp->v_type != VDIR) {
2319 error = SET_ERROR(ENOTDIR);
2324 error = SET_ERROR(EINVAL);
2328 vnevent_rmdir(vp, dvp, name, ct);
2331 * Grab a lock on the directory to make sure that noone is
2332 * trying to add (or lookup) entries while we are removing it.
2334 rw_enter(&zp->z_name_lock, RW_WRITER);
2337 * Grab a lock on the parent pointer to make sure we play well
2338 * with the treewalk and directory rename code.
2340 rw_enter(&zp->z_parent_lock, RW_WRITER);
2342 tx = dmu_tx_create(zfsvfs->z_os);
2343 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2344 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2345 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2346 zfs_sa_upgrade_txholds(tx, zp);
2347 zfs_sa_upgrade_txholds(tx, dzp);
2348 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2350 rw_exit(&zp->z_parent_lock);
2351 rw_exit(&zp->z_name_lock);
2352 zfs_dirent_unlock(dl);
2354 if (error == ERESTART) {
2365 #ifdef FREEBSD_NAMECACHE
2369 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2372 uint64_t txtype = TX_RMDIR;
2373 if (flags & FIGNORECASE)
2375 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2380 rw_exit(&zp->z_parent_lock);
2381 rw_exit(&zp->z_name_lock);
2382 #ifdef FREEBSD_NAMECACHE
2386 zfs_dirent_unlock(dl);
2390 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2391 zil_commit(zilog, 0);
2398 * Read as many directory entries as will fit into the provided
2399 * buffer from the given directory cursor position (specified in
2400 * the uio structure).
2402 * IN: vp - vnode of directory to read.
2403 * uio - structure supplying read location, range info,
2404 * and return buffer.
2405 * cr - credentials of caller.
2406 * ct - caller context
2407 * flags - case flags
2409 * OUT: uio - updated offset and range, buffer filled.
2410 * eofp - set to true if end-of-file detected.
2412 * RETURN: 0 on success, error code on failure.
2415 * vp - atime updated
2417 * Note that the low 4 bits of the cookie returned by zap is always zero.
2418 * This allows us to use the low range for "special" directory entries:
2419 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2420 * we use the offset 2 for the '.zfs' directory.
2424 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2426 znode_t *zp = VTOZ(vp);
2430 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2435 zap_attribute_t zap;
2436 uint_t bytes_wanted;
2437 uint64_t offset; /* must be unsigned; checks for < 1 */
2443 boolean_t check_sysattrs;
2446 u_long *cooks = NULL;
2452 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2453 &parent, sizeof (parent))) != 0) {
2459 * If we are not given an eof variable,
2466 * Check for valid iov_len.
2468 if (uio->uio_iov->iov_len <= 0) {
2470 return (SET_ERROR(EINVAL));
2474 * Quit if directory has been removed (posix)
2476 if ((*eofp = zp->z_unlinked) != 0) {
2483 offset = uio->uio_loffset;
2484 prefetch = zp->z_zn_prefetch;
2487 * Initialize the iterator cursor.
2491 * Start iteration from the beginning of the directory.
2493 zap_cursor_init(&zc, os, zp->z_id);
2496 * The offset is a serialized cursor.
2498 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2502 * Get space to change directory entries into fs independent format.
2504 iovp = uio->uio_iov;
2505 bytes_wanted = iovp->iov_len;
2506 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2507 bufsize = bytes_wanted;
2508 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2509 odp = (struct dirent64 *)outbuf;
2511 bufsize = bytes_wanted;
2513 odp = (struct dirent64 *)iovp->iov_base;
2515 eodp = (struct edirent *)odp;
2517 if (ncookies != NULL) {
2519 * Minimum entry size is dirent size and 1 byte for a file name.
2521 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2522 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2527 * If this VFS supports the system attribute view interface; and
2528 * we're looking at an extended attribute directory; and we care
2529 * about normalization conflicts on this vfs; then we must check
2530 * for normalization conflicts with the sysattr name space.
2533 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2534 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2535 (flags & V_RDDIR_ENTFLAGS);
2541 * Transform to file-system independent format
2544 while (outcount < bytes_wanted) {
2547 off64_t *next = NULL;
2550 * Special case `.', `..', and `.zfs'.
2553 (void) strcpy(zap.za_name, ".");
2554 zap.za_normalization_conflict = 0;
2557 } else if (offset == 1) {
2558 (void) strcpy(zap.za_name, "..");
2559 zap.za_normalization_conflict = 0;
2562 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2563 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2564 zap.za_normalization_conflict = 0;
2565 objnum = ZFSCTL_INO_ROOT;
2571 if (error = zap_cursor_retrieve(&zc, &zap)) {
2572 if ((*eofp = (error == ENOENT)) != 0)
2578 if (zap.za_integer_length != 8 ||
2579 zap.za_num_integers != 1) {
2580 cmn_err(CE_WARN, "zap_readdir: bad directory "
2581 "entry, obj = %lld, offset = %lld\n",
2582 (u_longlong_t)zp->z_id,
2583 (u_longlong_t)offset);
2584 error = SET_ERROR(ENXIO);
2588 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2590 * MacOS X can extract the object type here such as:
2591 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2593 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2595 if (check_sysattrs && !zap.za_normalization_conflict) {
2597 zap.za_normalization_conflict =
2598 xattr_sysattr_casechk(zap.za_name);
2600 panic("%s:%u: TODO", __func__, __LINE__);
2605 if (flags & V_RDDIR_ACCFILTER) {
2607 * If we have no access at all, don't include
2608 * this entry in the returned information
2611 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2613 if (!zfs_has_access(ezp, cr)) {
2620 if (flags & V_RDDIR_ENTFLAGS)
2621 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2623 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2626 * Will this entry fit in the buffer?
2628 if (outcount + reclen > bufsize) {
2630 * Did we manage to fit anything in the buffer?
2633 error = SET_ERROR(EINVAL);
2638 if (flags & V_RDDIR_ENTFLAGS) {
2640 * Add extended flag entry:
2642 eodp->ed_ino = objnum;
2643 eodp->ed_reclen = reclen;
2644 /* NOTE: ed_off is the offset for the *next* entry */
2645 next = &(eodp->ed_off);
2646 eodp->ed_eflags = zap.za_normalization_conflict ?
2647 ED_CASE_CONFLICT : 0;
2648 (void) strncpy(eodp->ed_name, zap.za_name,
2649 EDIRENT_NAMELEN(reclen));
2650 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2655 odp->d_ino = objnum;
2656 odp->d_reclen = reclen;
2657 odp->d_namlen = strlen(zap.za_name);
2658 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2660 odp = (dirent64_t *)((intptr_t)odp + reclen);
2664 ASSERT(outcount <= bufsize);
2666 /* Prefetch znode */
2668 dmu_prefetch(os, objnum, 0, 0);
2672 * Move to the next entry, fill in the previous offset.
2674 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2675 zap_cursor_advance(&zc);
2676 offset = zap_cursor_serialize(&zc);
2681 if (cooks != NULL) {
2684 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2687 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2689 /* Subtract unused cookies */
2690 if (ncookies != NULL)
2691 *ncookies -= ncooks;
2693 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2694 iovp->iov_base += outcount;
2695 iovp->iov_len -= outcount;
2696 uio->uio_resid -= outcount;
2697 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2699 * Reset the pointer.
2701 offset = uio->uio_loffset;
2705 zap_cursor_fini(&zc);
2706 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2707 kmem_free(outbuf, bufsize);
2709 if (error == ENOENT)
2712 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2714 uio->uio_loffset = offset;
2716 if (error != 0 && cookies != NULL) {
2717 free(*cookies, M_TEMP);
2724 ulong_t zfs_fsync_sync_cnt = 4;
2727 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2729 znode_t *zp = VTOZ(vp);
2730 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2732 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2734 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2737 zil_commit(zfsvfs->z_log, zp->z_id);
2745 * Get the requested file attributes and place them in the provided
2748 * IN: vp - vnode of file.
2749 * vap - va_mask identifies requested attributes.
2750 * If AT_XVATTR set, then optional attrs are requested
2751 * flags - ATTR_NOACLCHECK (CIFS server context)
2752 * cr - credentials of caller.
2753 * ct - caller context
2755 * OUT: vap - attribute values.
2757 * RETURN: 0 (always succeeds).
2761 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2762 caller_context_t *ct)
2764 znode_t *zp = VTOZ(vp);
2765 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2768 u_longlong_t nblocks;
2770 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2771 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2772 xoptattr_t *xoap = NULL;
2773 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2774 sa_bulk_attr_t bulk[4];
2780 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2782 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2783 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2784 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2785 if (vp->v_type == VBLK || vp->v_type == VCHR)
2786 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2789 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2795 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2796 * Also, if we are the owner don't bother, since owner should
2797 * always be allowed to read basic attributes of file.
2799 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2800 (vap->va_uid != crgetuid(cr))) {
2801 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2809 * Return all attributes. It's cheaper to provide the answer
2810 * than to determine whether we were asked the question.
2813 mutex_enter(&zp->z_lock);
2814 vap->va_type = IFTOVT(zp->z_mode);
2815 vap->va_mode = zp->z_mode & ~S_IFMT;
2817 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2819 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2821 vap->va_nodeid = zp->z_id;
2822 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2823 links = zp->z_links + 1;
2825 links = zp->z_links;
2826 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2827 vap->va_size = zp->z_size;
2829 vap->va_rdev = vp->v_rdev;
2831 if (vp->v_type == VBLK || vp->v_type == VCHR)
2832 vap->va_rdev = zfs_cmpldev(rdev);
2834 vap->va_seq = zp->z_seq;
2835 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2836 vap->va_filerev = zp->z_seq;
2839 * Add in any requested optional attributes and the create time.
2840 * Also set the corresponding bits in the returned attribute bitmap.
2842 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2843 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2845 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2846 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2849 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2850 xoap->xoa_readonly =
2851 ((zp->z_pflags & ZFS_READONLY) != 0);
2852 XVA_SET_RTN(xvap, XAT_READONLY);
2855 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2857 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2858 XVA_SET_RTN(xvap, XAT_SYSTEM);
2861 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2863 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2864 XVA_SET_RTN(xvap, XAT_HIDDEN);
2867 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2868 xoap->xoa_nounlink =
2869 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2870 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2873 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2874 xoap->xoa_immutable =
2875 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2876 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2879 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2880 xoap->xoa_appendonly =
2881 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2882 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2885 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2887 ((zp->z_pflags & ZFS_NODUMP) != 0);
2888 XVA_SET_RTN(xvap, XAT_NODUMP);
2891 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2893 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2894 XVA_SET_RTN(xvap, XAT_OPAQUE);
2897 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2898 xoap->xoa_av_quarantined =
2899 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2900 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2903 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2904 xoap->xoa_av_modified =
2905 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2906 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2909 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2910 vp->v_type == VREG) {
2911 zfs_sa_get_scanstamp(zp, xvap);
2914 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2917 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2918 times, sizeof (times));
2919 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2920 XVA_SET_RTN(xvap, XAT_CREATETIME);
2923 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2924 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2925 XVA_SET_RTN(xvap, XAT_REPARSE);
2927 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2928 xoap->xoa_generation = zp->z_gen;
2929 XVA_SET_RTN(xvap, XAT_GEN);
2932 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2934 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2935 XVA_SET_RTN(xvap, XAT_OFFLINE);
2938 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2940 ((zp->z_pflags & ZFS_SPARSE) != 0);
2941 XVA_SET_RTN(xvap, XAT_SPARSE);
2945 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2946 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2947 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2948 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2950 mutex_exit(&zp->z_lock);
2952 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2953 vap->va_blksize = blksize;
2954 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2956 if (zp->z_blksz == 0) {
2958 * Block size hasn't been set; suggest maximal I/O transfers.
2960 vap->va_blksize = zfsvfs->z_max_blksz;
2968 * Set the file attributes to the values contained in the
2971 * IN: vp - vnode of file to be modified.
2972 * vap - new attribute values.
2973 * If AT_XVATTR set, then optional attrs are being set
2974 * flags - ATTR_UTIME set if non-default time values provided.
2975 * - ATTR_NOACLCHECK (CIFS context only).
2976 * cr - credentials of caller.
2977 * ct - caller context
2979 * RETURN: 0 on success, error code on failure.
2982 * vp - ctime updated, mtime updated if size changed.
2986 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2987 caller_context_t *ct)
2989 znode_t *zp = VTOZ(vp);
2990 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2995 uint_t mask = vap->va_mask;
2996 uint_t saved_mask = 0;
2997 uint64_t saved_mode;
3000 uint64_t new_uid, new_gid;
3002 uint64_t mtime[2], ctime[2];
3004 int need_policy = FALSE;
3006 zfs_fuid_info_t *fuidp = NULL;
3007 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
3010 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3011 boolean_t fuid_dirtied = B_FALSE;
3012 sa_bulk_attr_t bulk[7], xattr_bulk[7];
3013 int count = 0, xattr_count = 0;
3018 if (mask & AT_NOSET)
3019 return (SET_ERROR(EINVAL));
3024 zilog = zfsvfs->z_log;
3027 * Make sure that if we have ephemeral uid/gid or xvattr specified
3028 * that file system is at proper version level
3031 if (zfsvfs->z_use_fuids == B_FALSE &&
3032 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3033 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3034 (mask & AT_XVATTR))) {
3036 return (SET_ERROR(EINVAL));
3039 if (mask & AT_SIZE && vp->v_type == VDIR) {
3041 return (SET_ERROR(EISDIR));
3044 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3046 return (SET_ERROR(EINVAL));
3050 * If this is an xvattr_t, then get a pointer to the structure of
3051 * optional attributes. If this is NULL, then we have a vattr_t.
3053 xoap = xva_getxoptattr(xvap);
3055 xva_init(&tmpxvattr);
3058 * Immutable files can only alter immutable bit and atime
3060 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3061 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3062 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3064 return (SET_ERROR(EPERM));
3067 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3069 return (SET_ERROR(EPERM));
3073 * Verify timestamps doesn't overflow 32 bits.
3074 * ZFS can handle large timestamps, but 32bit syscalls can't
3075 * handle times greater than 2039. This check should be removed
3076 * once large timestamps are fully supported.
3078 if (mask & (AT_ATIME | AT_MTIME)) {
3079 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3080 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3082 return (SET_ERROR(EOVERFLOW));
3090 /* Can this be moved to before the top label? */
3091 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3093 return (SET_ERROR(EROFS));
3097 * First validate permissions
3100 if (mask & AT_SIZE) {
3102 * XXX - Note, we are not providing any open
3103 * mode flags here (like FNDELAY), so we may
3104 * block if there are locks present... this
3105 * should be addressed in openat().
3107 /* XXX - would it be OK to generate a log record here? */
3108 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3115 if (mask & (AT_ATIME|AT_MTIME) ||
3116 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3117 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3118 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3119 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3120 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3121 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3122 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3123 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3127 if (mask & (AT_UID|AT_GID)) {
3128 int idmask = (mask & (AT_UID|AT_GID));
3133 * NOTE: even if a new mode is being set,
3134 * we may clear S_ISUID/S_ISGID bits.
3137 if (!(mask & AT_MODE))
3138 vap->va_mode = zp->z_mode;
3141 * Take ownership or chgrp to group we are a member of
3144 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3145 take_group = (mask & AT_GID) &&
3146 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3149 * If both AT_UID and AT_GID are set then take_owner and
3150 * take_group must both be set in order to allow taking
3153 * Otherwise, send the check through secpolicy_vnode_setattr()
3157 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3158 ((idmask == AT_UID) && take_owner) ||
3159 ((idmask == AT_GID) && take_group)) {
3160 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3161 skipaclchk, cr) == 0) {
3163 * Remove setuid/setgid for non-privileged users
3165 secpolicy_setid_clear(vap, vp, cr);
3166 trim_mask = (mask & (AT_UID|AT_GID));
3175 mutex_enter(&zp->z_lock);
3176 oldva.va_mode = zp->z_mode;
3177 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3178 if (mask & AT_XVATTR) {
3180 * Update xvattr mask to include only those attributes
3181 * that are actually changing.
3183 * the bits will be restored prior to actually setting
3184 * the attributes so the caller thinks they were set.
3186 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3187 if (xoap->xoa_appendonly !=
3188 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3191 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3192 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3196 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3197 if (xoap->xoa_nounlink !=
3198 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3201 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3202 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3206 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3207 if (xoap->xoa_immutable !=
3208 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3211 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3212 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3216 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3217 if (xoap->xoa_nodump !=
3218 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3221 XVA_CLR_REQ(xvap, XAT_NODUMP);
3222 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3226 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3227 if (xoap->xoa_av_modified !=
3228 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3231 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3232 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3236 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3237 if ((vp->v_type != VREG &&
3238 xoap->xoa_av_quarantined) ||
3239 xoap->xoa_av_quarantined !=
3240 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3243 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3244 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3248 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3249 mutex_exit(&zp->z_lock);
3251 return (SET_ERROR(EPERM));
3254 if (need_policy == FALSE &&
3255 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3256 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3261 mutex_exit(&zp->z_lock);
3263 if (mask & AT_MODE) {
3264 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3265 err = secpolicy_setid_setsticky_clear(vp, vap,
3271 trim_mask |= AT_MODE;
3279 * If trim_mask is set then take ownership
3280 * has been granted or write_acl is present and user
3281 * has the ability to modify mode. In that case remove
3282 * UID|GID and or MODE from mask so that
3283 * secpolicy_vnode_setattr() doesn't revoke it.
3287 saved_mask = vap->va_mask;
3288 vap->va_mask &= ~trim_mask;
3289 if (trim_mask & AT_MODE) {
3291 * Save the mode, as secpolicy_vnode_setattr()
3292 * will overwrite it with ova.va_mode.
3294 saved_mode = vap->va_mode;
3297 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3298 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3305 vap->va_mask |= saved_mask;
3306 if (trim_mask & AT_MODE) {
3308 * Recover the mode after
3309 * secpolicy_vnode_setattr().
3311 vap->va_mode = saved_mode;
3317 * secpolicy_vnode_setattr, or take ownership may have
3320 mask = vap->va_mask;
3322 if ((mask & (AT_UID | AT_GID))) {
3323 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3324 &xattr_obj, sizeof (xattr_obj));
3326 if (err == 0 && xattr_obj) {
3327 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3331 if (mask & AT_UID) {
3332 new_uid = zfs_fuid_create(zfsvfs,
3333 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3334 if (new_uid != zp->z_uid &&
3335 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3337 VN_RELE(ZTOV(attrzp));
3338 err = SET_ERROR(EDQUOT);
3343 if (mask & AT_GID) {
3344 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3345 cr, ZFS_GROUP, &fuidp);
3346 if (new_gid != zp->z_gid &&
3347 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3349 VN_RELE(ZTOV(attrzp));
3350 err = SET_ERROR(EDQUOT);
3355 tx = dmu_tx_create(zfsvfs->z_os);
3357 if (mask & AT_MODE) {
3358 uint64_t pmode = zp->z_mode;
3360 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3362 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3363 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3364 err = SET_ERROR(EPERM);
3368 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3371 mutex_enter(&zp->z_lock);
3372 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3374 * Are we upgrading ACL from old V0 format
3377 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3378 zfs_znode_acl_version(zp) ==
3379 ZFS_ACL_VERSION_INITIAL) {
3380 dmu_tx_hold_free(tx, acl_obj, 0,
3382 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3383 0, aclp->z_acl_bytes);
3385 dmu_tx_hold_write(tx, acl_obj, 0,
3388 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3389 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3390 0, aclp->z_acl_bytes);
3392 mutex_exit(&zp->z_lock);
3393 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3395 if ((mask & AT_XVATTR) &&
3396 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3397 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3399 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3403 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3406 fuid_dirtied = zfsvfs->z_fuid_dirty;
3408 zfs_fuid_txhold(zfsvfs, tx);
3410 zfs_sa_upgrade_txholds(tx, zp);
3412 err = dmu_tx_assign(tx, TXG_WAIT);
3418 * Set each attribute requested.
3419 * We group settings according to the locks they need to acquire.
3421 * Note: you cannot set ctime directly, although it will be
3422 * updated as a side-effect of calling this function.
3426 if (mask & (AT_UID|AT_GID|AT_MODE))
3427 mutex_enter(&zp->z_acl_lock);
3428 mutex_enter(&zp->z_lock);
3430 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3431 &zp->z_pflags, sizeof (zp->z_pflags));
3434 if (mask & (AT_UID|AT_GID|AT_MODE))
3435 mutex_enter(&attrzp->z_acl_lock);
3436 mutex_enter(&attrzp->z_lock);
3437 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3438 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3439 sizeof (attrzp->z_pflags));
3442 if (mask & (AT_UID|AT_GID)) {
3444 if (mask & AT_UID) {
3445 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3446 &new_uid, sizeof (new_uid));
3447 zp->z_uid = new_uid;
3449 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3450 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3452 attrzp->z_uid = new_uid;
3456 if (mask & AT_GID) {
3457 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3458 NULL, &new_gid, sizeof (new_gid));
3459 zp->z_gid = new_gid;
3461 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3462 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3464 attrzp->z_gid = new_gid;
3467 if (!(mask & AT_MODE)) {
3468 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3469 NULL, &new_mode, sizeof (new_mode));
3470 new_mode = zp->z_mode;
3472 err = zfs_acl_chown_setattr(zp);
3475 err = zfs_acl_chown_setattr(attrzp);
3480 if (mask & AT_MODE) {
3481 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3482 &new_mode, sizeof (new_mode));
3483 zp->z_mode = new_mode;
3484 ASSERT3U((uintptr_t)aclp, !=, 0);
3485 err = zfs_aclset_common(zp, aclp, cr, tx);
3487 if (zp->z_acl_cached)
3488 zfs_acl_free(zp->z_acl_cached);
3489 zp->z_acl_cached = aclp;
3494 if (mask & AT_ATIME) {
3495 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3496 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3497 &zp->z_atime, sizeof (zp->z_atime));
3500 if (mask & AT_MTIME) {
3501 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3502 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3503 mtime, sizeof (mtime));
3506 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3507 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3508 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3509 NULL, mtime, sizeof (mtime));
3510 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3511 &ctime, sizeof (ctime));
3512 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3514 } else if (mask != 0) {
3515 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3516 &ctime, sizeof (ctime));
3517 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3520 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3521 SA_ZPL_CTIME(zfsvfs), NULL,
3522 &ctime, sizeof (ctime));
3523 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3524 mtime, ctime, B_TRUE);
3528 * Do this after setting timestamps to prevent timestamp
3529 * update from toggling bit
3532 if (xoap && (mask & AT_XVATTR)) {
3535 * restore trimmed off masks
3536 * so that return masks can be set for caller.
3539 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3540 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3542 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3543 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3545 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3546 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3548 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3549 XVA_SET_REQ(xvap, XAT_NODUMP);
3551 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3552 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3554 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3555 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3558 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3559 ASSERT(vp->v_type == VREG);
3561 zfs_xvattr_set(zp, xvap, tx);
3565 zfs_fuid_sync(zfsvfs, tx);
3568 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3570 mutex_exit(&zp->z_lock);
3571 if (mask & (AT_UID|AT_GID|AT_MODE))
3572 mutex_exit(&zp->z_acl_lock);
3575 if (mask & (AT_UID|AT_GID|AT_MODE))
3576 mutex_exit(&attrzp->z_acl_lock);
3577 mutex_exit(&attrzp->z_lock);
3580 if (err == 0 && attrzp) {
3581 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3587 VN_RELE(ZTOV(attrzp));
3593 zfs_fuid_info_free(fuidp);
3599 if (err == ERESTART)
3602 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3607 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3608 zil_commit(zilog, 0);
3614 typedef struct zfs_zlock {
3615 krwlock_t *zl_rwlock; /* lock we acquired */
3616 znode_t *zl_znode; /* znode we held */
3617 struct zfs_zlock *zl_next; /* next in list */
3621 * Drop locks and release vnodes that were held by zfs_rename_lock().
3624 zfs_rename_unlock(zfs_zlock_t **zlpp)
3628 while ((zl = *zlpp) != NULL) {
3629 if (zl->zl_znode != NULL)
3630 VN_RELE(ZTOV(zl->zl_znode));
3631 rw_exit(zl->zl_rwlock);
3632 *zlpp = zl->zl_next;
3633 kmem_free(zl, sizeof (*zl));
3638 * Search back through the directory tree, using the ".." entries.
3639 * Lock each directory in the chain to prevent concurrent renames.
3640 * Fail any attempt to move a directory into one of its own descendants.
3641 * XXX - z_parent_lock can overlap with map or grow locks
3644 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3648 uint64_t rootid = zp->z_zfsvfs->z_root;
3649 uint64_t oidp = zp->z_id;
3650 krwlock_t *rwlp = &szp->z_parent_lock;
3651 krw_t rw = RW_WRITER;
3654 * First pass write-locks szp and compares to zp->z_id.
3655 * Later passes read-lock zp and compare to zp->z_parent.
3658 if (!rw_tryenter(rwlp, rw)) {
3660 * Another thread is renaming in this path.
3661 * Note that if we are a WRITER, we don't have any
3662 * parent_locks held yet.
3664 if (rw == RW_READER && zp->z_id > szp->z_id) {
3666 * Drop our locks and restart
3668 zfs_rename_unlock(&zl);
3672 rwlp = &szp->z_parent_lock;
3677 * Wait for other thread to drop its locks
3683 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3684 zl->zl_rwlock = rwlp;
3685 zl->zl_znode = NULL;
3686 zl->zl_next = *zlpp;
3689 if (oidp == szp->z_id) /* We're a descendant of szp */
3690 return (SET_ERROR(EINVAL));
3692 if (oidp == rootid) /* We've hit the top */
3695 if (rw == RW_READER) { /* i.e. not the first pass */
3696 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3701 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3702 &oidp, sizeof (oidp));
3703 rwlp = &zp->z_parent_lock;
3706 } while (zp->z_id != sdzp->z_id);
3712 * Move an entry from the provided source directory to the target
3713 * directory. Change the entry name as indicated.
3715 * IN: sdvp - Source directory containing the "old entry".
3716 * snm - Old entry name.
3717 * tdvp - Target directory to contain the "new entry".
3718 * tnm - New entry name.
3719 * cr - credentials of caller.
3720 * ct - caller context
3721 * flags - case flags
3723 * RETURN: 0 on success, error code on failure.
3726 * sdvp,tdvp - ctime|mtime updated
3730 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3731 caller_context_t *ct, int flags)
3733 znode_t *tdzp, *sdzp, *szp, *tzp;
3737 zfs_dirlock_t *sdl, *tdl;
3740 int cmp, serr, terr;
3743 boolean_t waited = B_FALSE;
3746 ZFS_VERIFY_ZP(tdzp);
3747 zfsvfs = tdzp->z_zfsvfs;
3749 zilog = zfsvfs->z_log;
3753 * In case sdzp is not valid, let's be sure to exit from the right
3756 if (sdzp->z_sa_hdl == NULL) {
3758 return (SET_ERROR(EIO));
3762 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3763 * ctldir appear to have the same v_vfsp.
3765 if (sdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3767 return (SET_ERROR(EXDEV));
3770 if (zfsvfs->z_utf8 && u8_validate(tnm,
3771 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3773 return (SET_ERROR(EILSEQ));
3776 if (flags & FIGNORECASE)
3785 * This is to prevent the creation of links into attribute space
3786 * by renaming a linked file into/outof an attribute directory.
3787 * See the comment in zfs_link() for why this is considered bad.
3789 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3791 return (SET_ERROR(EINVAL));
3795 * Lock source and target directory entries. To prevent deadlock,
3796 * a lock ordering must be defined. We lock the directory with
3797 * the smallest object id first, or if it's a tie, the one with
3798 * the lexically first name.
3800 if (sdzp->z_id < tdzp->z_id) {
3802 } else if (sdzp->z_id > tdzp->z_id) {
3806 * First compare the two name arguments without
3807 * considering any case folding.
3809 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3811 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3812 ASSERT(error == 0 || !zfsvfs->z_utf8);
3815 * POSIX: "If the old argument and the new argument
3816 * both refer to links to the same existing file,
3817 * the rename() function shall return successfully
3818 * and perform no other action."
3824 * If the file system is case-folding, then we may
3825 * have some more checking to do. A case-folding file
3826 * system is either supporting mixed case sensitivity
3827 * access or is completely case-insensitive. Note
3828 * that the file system is always case preserving.
3830 * In mixed sensitivity mode case sensitive behavior
3831 * is the default. FIGNORECASE must be used to
3832 * explicitly request case insensitive behavior.
3834 * If the source and target names provided differ only
3835 * by case (e.g., a request to rename 'tim' to 'Tim'),
3836 * we will treat this as a special case in the
3837 * case-insensitive mode: as long as the source name
3838 * is an exact match, we will allow this to proceed as
3839 * a name-change request.
3841 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3842 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3843 flags & FIGNORECASE)) &&
3844 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3847 * case preserving rename request, require exact
3856 * If the source and destination directories are the same, we should
3857 * grab the z_name_lock of that directory only once.
3861 rw_enter(&sdzp->z_name_lock, RW_READER);
3865 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3866 ZEXISTS | zflg, NULL, NULL);
3867 terr = zfs_dirent_lock(&tdl,
3868 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3870 terr = zfs_dirent_lock(&tdl,
3871 tdzp, tnm, &tzp, zflg, NULL, NULL);
3872 serr = zfs_dirent_lock(&sdl,
3873 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3879 * Source entry invalid or not there.
3882 zfs_dirent_unlock(tdl);
3888 rw_exit(&sdzp->z_name_lock);
3891 * FreeBSD: In OpenSolaris they only check if rename source is
3892 * ".." here, because "." is handled in their lookup. This is
3893 * not the case for FreeBSD, so we check for "." explicitly.
3895 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3896 serr = SET_ERROR(EINVAL);
3901 zfs_dirent_unlock(sdl);
3905 rw_exit(&sdzp->z_name_lock);
3907 if (strcmp(tnm, "..") == 0)
3908 terr = SET_ERROR(EINVAL);
3914 * Must have write access at the source to remove the old entry
3915 * and write access at the target to create the new entry.
3916 * Note that if target and source are the same, this can be
3917 * done in a single check.
3920 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3923 if (ZTOV(szp)->v_type == VDIR) {
3925 * Check to make sure rename is valid.
3926 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3928 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3933 * Does target exist?
3937 * Source and target must be the same type.
3939 if (ZTOV(szp)->v_type == VDIR) {
3940 if (ZTOV(tzp)->v_type != VDIR) {
3941 error = SET_ERROR(ENOTDIR);
3945 if (ZTOV(tzp)->v_type == VDIR) {
3946 error = SET_ERROR(EISDIR);
3951 * POSIX dictates that when the source and target
3952 * entries refer to the same file object, rename
3953 * must do nothing and exit without error.
3955 if (szp->z_id == tzp->z_id) {
3961 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3963 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3966 * notify the target directory if it is not the same
3967 * as source directory.
3970 vnevent_rename_dest_dir(tdvp, ct);
3973 tx = dmu_tx_create(zfsvfs->z_os);
3974 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3975 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3976 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3977 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3979 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3980 zfs_sa_upgrade_txholds(tx, tdzp);
3983 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3984 zfs_sa_upgrade_txholds(tx, tzp);
3987 zfs_sa_upgrade_txholds(tx, szp);
3988 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3989 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3992 zfs_rename_unlock(&zl);
3993 zfs_dirent_unlock(sdl);
3994 zfs_dirent_unlock(tdl);
3997 rw_exit(&sdzp->z_name_lock);
4002 if (error == ERESTART) {
4013 if (tzp) /* Attempt to remove the existing target */
4014 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
4017 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
4019 szp->z_pflags |= ZFS_AV_MODIFIED;
4021 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4022 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4025 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
4027 zfs_log_rename(zilog, tx, TX_RENAME |
4028 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4029 sdl->dl_name, tdzp, tdl->dl_name, szp);
4032 * Update path information for the target vnode
4034 vn_renamepath(tdvp, ZTOV(szp), tnm,
4038 * At this point, we have successfully created
4039 * the target name, but have failed to remove
4040 * the source name. Since the create was done
4041 * with the ZRENAMING flag, there are
4042 * complications; for one, the link count is
4043 * wrong. The easiest way to deal with this
4044 * is to remove the newly created target, and
4045 * return the original error. This must
4046 * succeed; fortunately, it is very unlikely to
4047 * fail, since we just created it.
4049 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4050 ZRENAMING, NULL), ==, 0);
4053 #ifdef FREEBSD_NAMECACHE
4057 cache_purge(ZTOV(szp));
4059 cache_purge(ZTOV(tzp));
4067 zfs_rename_unlock(&zl);
4069 zfs_dirent_unlock(sdl);
4070 zfs_dirent_unlock(tdl);
4073 rw_exit(&sdzp->z_name_lock);
4080 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4081 zil_commit(zilog, 0);
4089 * Insert the indicated symbolic reference entry into the directory.
4091 * IN: dvp - Directory to contain new symbolic link.
4092 * link - Name for new symlink entry.
4093 * vap - Attributes of new entry.
4094 * cr - credentials of caller.
4095 * ct - caller context
4096 * flags - case flags
4098 * RETURN: 0 on success, error code on failure.
4101 * dvp - ctime|mtime updated
4105 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4106 cred_t *cr, kthread_t *td)
4108 znode_t *zp, *dzp = VTOZ(dvp);
4111 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4113 uint64_t len = strlen(link);
4116 zfs_acl_ids_t acl_ids;
4117 boolean_t fuid_dirtied;
4118 uint64_t txtype = TX_SYMLINK;
4119 boolean_t waited = B_FALSE;
4122 ASSERT(vap->va_type == VLNK);
4126 zilog = zfsvfs->z_log;
4128 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4129 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4131 return (SET_ERROR(EILSEQ));
4133 if (flags & FIGNORECASE)
4136 if (len > MAXPATHLEN) {
4138 return (SET_ERROR(ENAMETOOLONG));
4141 if ((error = zfs_acl_ids_create(dzp, 0,
4142 vap, cr, NULL, &acl_ids)) != 0) {
4147 getnewvnode_reserve(1);
4151 * Attempt to lock directory; fail if entry already exists.
4153 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4155 zfs_acl_ids_free(&acl_ids);
4156 getnewvnode_drop_reserve();
4161 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4162 zfs_acl_ids_free(&acl_ids);
4163 zfs_dirent_unlock(dl);
4164 getnewvnode_drop_reserve();
4169 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4170 zfs_acl_ids_free(&acl_ids);
4171 zfs_dirent_unlock(dl);
4172 getnewvnode_drop_reserve();
4174 return (SET_ERROR(EDQUOT));
4176 tx = dmu_tx_create(zfsvfs->z_os);
4177 fuid_dirtied = zfsvfs->z_fuid_dirty;
4178 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4179 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4180 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4181 ZFS_SA_BASE_ATTR_SIZE + len);
4182 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4183 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4184 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4185 acl_ids.z_aclp->z_acl_bytes);
4188 zfs_fuid_txhold(zfsvfs, tx);
4189 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4191 zfs_dirent_unlock(dl);
4192 if (error == ERESTART) {
4198 zfs_acl_ids_free(&acl_ids);
4200 getnewvnode_drop_reserve();
4206 * Create a new object for the symlink.
4207 * for version 4 ZPL datsets the symlink will be an SA attribute
4209 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4212 zfs_fuid_sync(zfsvfs, tx);
4214 mutex_enter(&zp->z_lock);
4216 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4219 zfs_sa_symlink(zp, link, len, tx);
4220 mutex_exit(&zp->z_lock);
4223 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4224 &zp->z_size, sizeof (zp->z_size), tx);
4226 * Insert the new object into the directory.
4228 (void) zfs_link_create(dl, zp, tx, ZNEW);
4230 if (flags & FIGNORECASE)
4232 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4235 zfs_acl_ids_free(&acl_ids);
4239 getnewvnode_drop_reserve();
4241 zfs_dirent_unlock(dl);
4243 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4244 zil_commit(zilog, 0);
4251 * Return, in the buffer contained in the provided uio structure,
4252 * the symbolic path referred to by vp.
4254 * IN: vp - vnode of symbolic link.
4255 * uio - structure to contain the link path.
4256 * cr - credentials of caller.
4257 * ct - caller context
4259 * OUT: uio - structure containing the link path.
4261 * RETURN: 0 on success, error code on failure.
4264 * vp - atime updated
4268 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4270 znode_t *zp = VTOZ(vp);
4271 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4277 mutex_enter(&zp->z_lock);
4279 error = sa_lookup_uio(zp->z_sa_hdl,
4280 SA_ZPL_SYMLINK(zfsvfs), uio);
4282 error = zfs_sa_readlink(zp, uio);
4283 mutex_exit(&zp->z_lock);
4285 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4292 * Insert a new entry into directory tdvp referencing svp.
4294 * IN: tdvp - Directory to contain new entry.
4295 * svp - vnode of new entry.
4296 * name - name of new entry.
4297 * cr - credentials of caller.
4298 * ct - caller context
4300 * RETURN: 0 on success, error code on failure.
4303 * tdvp - ctime|mtime updated
4304 * svp - ctime updated
4308 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4309 caller_context_t *ct, int flags)
4311 znode_t *dzp = VTOZ(tdvp);
4313 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4322 boolean_t waited = B_FALSE;
4324 ASSERT(tdvp->v_type == VDIR);
4328 zilog = zfsvfs->z_log;
4330 if (VOP_REALVP(svp, &realvp, ct) == 0)
4334 * POSIX dictates that we return EPERM here.
4335 * Better choices include ENOTSUP or EISDIR.
4337 if (svp->v_type == VDIR) {
4339 return (SET_ERROR(EPERM));
4346 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4347 * ctldir appear to have the same v_vfsp.
4349 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4351 return (SET_ERROR(EXDEV));
4354 /* Prevent links to .zfs/shares files */
4356 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4357 &parent, sizeof (uint64_t))) != 0) {
4361 if (parent == zfsvfs->z_shares_dir) {
4363 return (SET_ERROR(EPERM));
4366 if (zfsvfs->z_utf8 && u8_validate(name,
4367 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4369 return (SET_ERROR(EILSEQ));
4371 if (flags & FIGNORECASE)
4375 * We do not support links between attributes and non-attributes
4376 * because of the potential security risk of creating links
4377 * into "normal" file space in order to circumvent restrictions
4378 * imposed in attribute space.
4380 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4382 return (SET_ERROR(EINVAL));
4386 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4387 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4389 return (SET_ERROR(EPERM));
4392 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4399 * Attempt to lock directory; fail if entry already exists.
4401 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4407 tx = dmu_tx_create(zfsvfs->z_os);
4408 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4409 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4410 zfs_sa_upgrade_txholds(tx, szp);
4411 zfs_sa_upgrade_txholds(tx, dzp);
4412 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4414 zfs_dirent_unlock(dl);
4415 if (error == ERESTART) {
4426 error = zfs_link_create(dl, szp, tx, 0);
4429 uint64_t txtype = TX_LINK;
4430 if (flags & FIGNORECASE)
4432 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4437 zfs_dirent_unlock(dl);
4440 vnevent_link(svp, ct);
4443 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4444 zil_commit(zilog, 0);
4452 * zfs_null_putapage() is used when the file system has been force
4453 * unmounted. It just drops the pages.
4457 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4458 size_t *lenp, int flags, cred_t *cr)
4460 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4465 * Push a page out to disk, klustering if possible.
4467 * IN: vp - file to push page to.
4468 * pp - page to push.
4469 * flags - additional flags.
4470 * cr - credentials of caller.
4472 * OUT: offp - start of range pushed.
4473 * lenp - len of range pushed.
4475 * RETURN: 0 on success, error code on failure.
4477 * NOTE: callers must have locked the page to be pushed. On
4478 * exit, the page (and all other pages in the kluster) must be
4483 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4484 size_t *lenp, int flags, cred_t *cr)
4486 znode_t *zp = VTOZ(vp);
4487 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4489 u_offset_t off, koff;
4496 * If our blocksize is bigger than the page size, try to kluster
4497 * multiple pages so that we write a full block (thus avoiding
4498 * a read-modify-write).
4500 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4501 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4502 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4503 ASSERT(koff <= zp->z_size);
4504 if (koff + klen > zp->z_size)
4505 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4506 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4508 ASSERT3U(btop(len), ==, btopr(len));
4511 * Can't push pages past end-of-file.
4513 if (off >= zp->z_size) {
4514 /* ignore all pages */
4517 } else if (off + len > zp->z_size) {
4518 int npages = btopr(zp->z_size - off);
4521 page_list_break(&pp, &trunc, npages);
4522 /* ignore pages past end of file */
4524 pvn_write_done(trunc, flags);
4525 len = zp->z_size - off;
4528 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4529 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4530 err = SET_ERROR(EDQUOT);
4533 tx = dmu_tx_create(zfsvfs->z_os);
4534 dmu_tx_hold_write(tx, zp->z_id, off, len);
4536 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4537 zfs_sa_upgrade_txholds(tx, zp);
4538 err = dmu_tx_assign(tx, TXG_WAIT);
4544 if (zp->z_blksz <= PAGESIZE) {
4545 caddr_t va = zfs_map_page(pp, S_READ);
4546 ASSERT3U(len, <=, PAGESIZE);
4547 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4548 zfs_unmap_page(pp, va);
4550 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4554 uint64_t mtime[2], ctime[2];
4555 sa_bulk_attr_t bulk[3];
4558 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4560 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4562 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4564 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4566 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4571 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4581 * Copy the portion of the file indicated from pages into the file.
4582 * The pages are stored in a page list attached to the files vnode.
4584 * IN: vp - vnode of file to push page data to.
4585 * off - position in file to put data.
4586 * len - amount of data to write.
4587 * flags - flags to control the operation.
4588 * cr - credentials of caller.
4589 * ct - caller context.
4591 * RETURN: 0 on success, error code on failure.
4594 * vp - ctime|mtime updated
4598 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4599 caller_context_t *ct)
4601 znode_t *zp = VTOZ(vp);
4602 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4614 * Align this request to the file block size in case we kluster.
4615 * XXX - this can result in pretty aggresive locking, which can
4616 * impact simultanious read/write access. One option might be
4617 * to break up long requests (len == 0) into block-by-block
4618 * operations to get narrower locking.
4620 blksz = zp->z_blksz;
4622 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4625 if (len > 0 && ISP2(blksz))
4626 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4632 * Search the entire vp list for pages >= io_off.
4634 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4635 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4638 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4640 if (off > zp->z_size) {
4641 /* past end of file */
4642 zfs_range_unlock(rl);
4647 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4649 for (off = io_off; io_off < off + len; io_off += io_len) {
4650 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4651 pp = page_lookup(vp, io_off,
4652 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4654 pp = page_lookup_nowait(vp, io_off,
4655 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4658 if (pp != NULL && pvn_getdirty(pp, flags)) {
4662 * Found a dirty page to push
4664 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4672 zfs_range_unlock(rl);
4673 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4674 zil_commit(zfsvfs->z_log, zp->z_id);
4682 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4684 znode_t *zp = VTOZ(vp);
4685 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4688 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4689 if (zp->z_sa_hdl == NULL) {
4691 * The fs has been unmounted, or we did a
4692 * suspend/resume and this file no longer exists.
4694 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4699 mutex_enter(&zp->z_lock);
4700 if (zp->z_unlinked) {
4702 * Fast path to recycle a vnode of a removed file.
4704 mutex_exit(&zp->z_lock);
4705 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4709 mutex_exit(&zp->z_lock);
4711 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4712 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4714 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4715 zfs_sa_upgrade_txholds(tx, zp);
4716 error = dmu_tx_assign(tx, TXG_WAIT);
4720 mutex_enter(&zp->z_lock);
4721 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4722 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4723 zp->z_atime_dirty = 0;
4724 mutex_exit(&zp->z_lock);
4728 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4733 * Bounds-check the seek operation.
4735 * IN: vp - vnode seeking within
4736 * ooff - old file offset
4737 * noffp - pointer to new file offset
4738 * ct - caller context
4740 * RETURN: 0 on success, EINVAL if new offset invalid.
4744 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4745 caller_context_t *ct)
4747 if (vp->v_type == VDIR)
4749 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4753 * Pre-filter the generic locking function to trap attempts to place
4754 * a mandatory lock on a memory mapped file.
4757 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4758 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4760 znode_t *zp = VTOZ(vp);
4761 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4767 * We are following the UFS semantics with respect to mapcnt
4768 * here: If we see that the file is mapped already, then we will
4769 * return an error, but we don't worry about races between this
4770 * function and zfs_map().
4772 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4774 return (SET_ERROR(EAGAIN));
4777 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4781 * If we can't find a page in the cache, we will create a new page
4782 * and fill it with file data. For efficiency, we may try to fill
4783 * multiple pages at once (klustering) to fill up the supplied page
4784 * list. Note that the pages to be filled are held with an exclusive
4785 * lock to prevent access by other threads while they are being filled.
4788 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4789 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4791 znode_t *zp = VTOZ(vp);
4792 page_t *pp, *cur_pp;
4793 objset_t *os = zp->z_zfsvfs->z_os;
4794 u_offset_t io_off, total;
4798 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4800 * We only have a single page, don't bother klustering
4804 pp = page_create_va(vp, io_off, io_len,
4805 PG_EXCL | PG_WAIT, seg, addr);
4808 * Try to find enough pages to fill the page list
4810 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4811 &io_len, off, plsz, 0);
4815 * The page already exists, nothing to do here.
4822 * Fill the pages in the kluster.
4825 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4828 ASSERT3U(io_off, ==, cur_pp->p_offset);
4829 va = zfs_map_page(cur_pp, S_WRITE);
4830 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4832 zfs_unmap_page(cur_pp, va);
4834 /* On error, toss the entire kluster */
4835 pvn_read_done(pp, B_ERROR);
4836 /* convert checksum errors into IO errors */
4838 err = SET_ERROR(EIO);
4841 cur_pp = cur_pp->p_next;
4845 * Fill in the page list array from the kluster starting
4846 * from the desired offset `off'.
4847 * NOTE: the page list will always be null terminated.
4849 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4850 ASSERT(pl == NULL || (*pl)->p_offset == off);
4856 * Return pointers to the pages for the file region [off, off + len]
4857 * in the pl array. If plsz is greater than len, this function may
4858 * also return page pointers from after the specified region
4859 * (i.e. the region [off, off + plsz]). These additional pages are
4860 * only returned if they are already in the cache, or were created as
4861 * part of a klustered read.
4863 * IN: vp - vnode of file to get data from.
4864 * off - position in file to get data from.
4865 * len - amount of data to retrieve.
4866 * plsz - length of provided page list.
4867 * seg - segment to obtain pages for.
4868 * addr - virtual address of fault.
4869 * rw - mode of created pages.
4870 * cr - credentials of caller.
4871 * ct - caller context.
4873 * OUT: protp - protection mode of created pages.
4874 * pl - list of pages created.
4876 * RETURN: 0 on success, error code on failure.
4879 * vp - atime updated
4883 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4884 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4885 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4887 znode_t *zp = VTOZ(vp);
4888 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4892 /* we do our own caching, faultahead is unnecessary */
4895 else if (len > plsz)
4898 len = P2ROUNDUP(len, PAGESIZE);
4899 ASSERT(plsz >= len);
4908 * Loop through the requested range [off, off + len) looking
4909 * for pages. If we don't find a page, we will need to create
4910 * a new page and fill it with data from the file.
4913 if (*pl = page_lookup(vp, off, SE_SHARED))
4915 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4918 ASSERT3U((*pl)->p_offset, ==, off);
4922 ASSERT3U(len, >=, PAGESIZE);
4925 ASSERT3U(plsz, >=, PAGESIZE);
4932 * Fill out the page array with any pages already in the cache.
4935 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4942 * Release any pages we have previously locked.
4947 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4957 * Request a memory map for a section of a file. This code interacts
4958 * with common code and the VM system as follows:
4960 * - common code calls mmap(), which ends up in smmap_common()
4961 * - this calls VOP_MAP(), which takes you into (say) zfs
4962 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4963 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4964 * - zfs_addmap() updates z_mapcnt
4968 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4969 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4970 caller_context_t *ct)
4972 znode_t *zp = VTOZ(vp);
4973 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4974 segvn_crargs_t vn_a;
4980 if ((prot & PROT_WRITE) && (zp->z_pflags &
4981 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4983 return (SET_ERROR(EPERM));
4986 if ((prot & (PROT_READ | PROT_EXEC)) &&
4987 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4989 return (SET_ERROR(EACCES));
4992 if (vp->v_flag & VNOMAP) {
4994 return (SET_ERROR(ENOSYS));
4997 if (off < 0 || len > MAXOFFSET_T - off) {
4999 return (SET_ERROR(ENXIO));
5002 if (vp->v_type != VREG) {
5004 return (SET_ERROR(ENODEV));
5008 * If file is locked, disallow mapping.
5010 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
5012 return (SET_ERROR(EAGAIN));
5016 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5024 vn_a.offset = (u_offset_t)off;
5025 vn_a.type = flags & MAP_TYPE;
5027 vn_a.maxprot = maxprot;
5030 vn_a.flags = flags & ~MAP_TYPE;
5032 vn_a.lgrp_mem_policy_flags = 0;
5034 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5043 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5044 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5045 caller_context_t *ct)
5047 uint64_t pages = btopr(len);
5049 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5054 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5055 * more accurate mtime for the associated file. Since we don't have a way of
5056 * detecting when the data was actually modified, we have to resort to
5057 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5058 * last page is pushed. The problem occurs when the msync() call is omitted,
5059 * which by far the most common case:
5067 * putpage() via fsflush
5069 * If we wait until fsflush to come along, we can have a modification time that
5070 * is some arbitrary point in the future. In order to prevent this in the
5071 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5076 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5077 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5078 caller_context_t *ct)
5080 uint64_t pages = btopr(len);
5082 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5083 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5085 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5086 vn_has_cached_data(vp))
5087 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5093 * Free or allocate space in a file. Currently, this function only
5094 * supports the `F_FREESP' command. However, this command is somewhat
5095 * misnamed, as its functionality includes the ability to allocate as
5096 * well as free space.
5098 * IN: vp - vnode of file to free data in.
5099 * cmd - action to take (only F_FREESP supported).
5100 * bfp - section of file to free/alloc.
5101 * flag - current file open mode flags.
5102 * offset - current file offset.
5103 * cr - credentials of caller [UNUSED].
5104 * ct - caller context.
5106 * RETURN: 0 on success, error code on failure.
5109 * vp - ctime|mtime updated
5113 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5114 offset_t offset, cred_t *cr, caller_context_t *ct)
5116 znode_t *zp = VTOZ(vp);
5117 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5124 if (cmd != F_FREESP) {
5126 return (SET_ERROR(EINVAL));
5129 if (error = convoff(vp, bfp, 0, offset)) {
5134 if (bfp->l_len < 0) {
5136 return (SET_ERROR(EINVAL));
5140 len = bfp->l_len; /* 0 means from off to end of file */
5142 error = zfs_freesp(zp, off, len, flag, TRUE);
5149 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5150 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5154 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5156 znode_t *zp = VTOZ(vp);
5157 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5160 uint64_t object = zp->z_id;
5167 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5168 &gen64, sizeof (uint64_t))) != 0) {
5173 gen = (uint32_t)gen64;
5175 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5178 if (fidp->fid_len < size) {
5179 fidp->fid_len = size;
5181 return (SET_ERROR(ENOSPC));
5184 fidp->fid_len = size;
5187 zfid = (zfid_short_t *)fidp;
5189 zfid->zf_len = size;
5191 for (i = 0; i < sizeof (zfid->zf_object); i++)
5192 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5194 /* Must have a non-zero generation number to distinguish from .zfs */
5197 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5198 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5200 if (size == LONG_FID_LEN) {
5201 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5204 zlfid = (zfid_long_t *)fidp;
5206 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5207 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5209 /* XXX - this should be the generation number for the objset */
5210 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5211 zlfid->zf_setgen[i] = 0;
5219 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5220 caller_context_t *ct)
5232 case _PC_FILESIZEBITS:
5236 case _PC_XATTR_EXISTS:
5238 zfsvfs = zp->z_zfsvfs;
5242 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5243 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5245 zfs_dirent_unlock(dl);
5246 if (!zfs_dirempty(xzp))
5249 } else if (error == ENOENT) {
5251 * If there aren't extended attributes, it's the
5252 * same as having zero of them.
5259 case _PC_SATTR_ENABLED:
5260 case _PC_SATTR_EXISTS:
5261 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5262 (vp->v_type == VREG || vp->v_type == VDIR);
5265 case _PC_ACCESS_FILTERING:
5266 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5270 case _PC_ACL_ENABLED:
5271 *valp = _ACL_ACE_ENABLED;
5274 case _PC_MIN_HOLE_SIZE:
5275 *valp = (int)SPA_MINBLOCKSIZE;
5278 case _PC_TIMESTAMP_RESOLUTION:
5279 /* nanosecond timestamp resolution */
5283 case _PC_ACL_EXTENDED:
5291 case _PC_ACL_PATH_MAX:
5292 *valp = ACL_MAX_ENTRIES;
5296 return (EOPNOTSUPP);
5302 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5303 caller_context_t *ct)
5305 znode_t *zp = VTOZ(vp);
5306 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5308 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5312 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5320 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5321 caller_context_t *ct)
5323 znode_t *zp = VTOZ(vp);
5324 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5326 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5327 zilog_t *zilog = zfsvfs->z_log;
5332 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5334 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5335 zil_commit(zilog, 0);
5343 * The smallest read we may consider to loan out an arcbuf.
5344 * This must be a power of 2.
5346 int zcr_blksz_min = (1 << 10); /* 1K */
5348 * If set to less than the file block size, allow loaning out of an
5349 * arcbuf for a partial block read. This must be a power of 2.
5351 int zcr_blksz_max = (1 << 17); /* 128K */
5355 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5356 caller_context_t *ct)
5358 znode_t *zp = VTOZ(vp);
5359 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5360 int max_blksz = zfsvfs->z_max_blksz;
5361 uio_t *uio = &xuio->xu_uio;
5362 ssize_t size = uio->uio_resid;
5363 offset_t offset = uio->uio_loffset;
5368 int preamble, postamble;
5370 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5371 return (SET_ERROR(EINVAL));
5378 * Loan out an arc_buf for write if write size is bigger than
5379 * max_blksz, and the file's block size is also max_blksz.
5382 if (size < blksz || zp->z_blksz != blksz) {
5384 return (SET_ERROR(EINVAL));
5387 * Caller requests buffers for write before knowing where the
5388 * write offset might be (e.g. NFS TCP write).
5393 preamble = P2PHASE(offset, blksz);
5395 preamble = blksz - preamble;
5400 postamble = P2PHASE(size, blksz);
5403 fullblk = size / blksz;
5404 (void) dmu_xuio_init(xuio,
5405 (preamble != 0) + fullblk + (postamble != 0));
5406 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5407 int, postamble, int,
5408 (preamble != 0) + fullblk + (postamble != 0));
5411 * Have to fix iov base/len for partial buffers. They
5412 * currently represent full arc_buf's.
5415 /* data begins in the middle of the arc_buf */
5416 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5419 (void) dmu_xuio_add(xuio, abuf,
5420 blksz - preamble, preamble);
5423 for (i = 0; i < fullblk; i++) {
5424 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5427 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5431 /* data ends in the middle of the arc_buf */
5432 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5435 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5440 * Loan out an arc_buf for read if the read size is larger than
5441 * the current file block size. Block alignment is not
5442 * considered. Partial arc_buf will be loaned out for read.
5444 blksz = zp->z_blksz;
5445 if (blksz < zcr_blksz_min)
5446 blksz = zcr_blksz_min;
5447 if (blksz > zcr_blksz_max)
5448 blksz = zcr_blksz_max;
5449 /* avoid potential complexity of dealing with it */
5450 if (blksz > max_blksz) {
5452 return (SET_ERROR(EINVAL));
5455 maxsize = zp->z_size - uio->uio_loffset;
5459 if (size < blksz || vn_has_cached_data(vp)) {
5461 return (SET_ERROR(EINVAL));
5466 return (SET_ERROR(EINVAL));
5469 uio->uio_extflg = UIO_XUIO;
5470 XUIO_XUZC_RW(xuio) = ioflag;
5477 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5481 int ioflag = XUIO_XUZC_RW(xuio);
5483 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5485 i = dmu_xuio_cnt(xuio);
5487 abuf = dmu_xuio_arcbuf(xuio, i);
5489 * if abuf == NULL, it must be a write buffer
5490 * that has been returned in zfs_write().
5493 dmu_return_arcbuf(abuf);
5494 ASSERT(abuf || ioflag == UIO_WRITE);
5497 dmu_xuio_fini(xuio);
5502 * Predeclare these here so that the compiler assumes that
5503 * this is an "old style" function declaration that does
5504 * not include arguments => we won't get type mismatch errors
5505 * in the initializations that follow.
5507 static int zfs_inval();
5508 static int zfs_isdir();
5513 return (SET_ERROR(EINVAL));
5519 return (SET_ERROR(EISDIR));
5522 * Directory vnode operations template
5524 vnodeops_t *zfs_dvnodeops;
5525 const fs_operation_def_t zfs_dvnodeops_template[] = {
5526 VOPNAME_OPEN, { .vop_open = zfs_open },
5527 VOPNAME_CLOSE, { .vop_close = zfs_close },
5528 VOPNAME_READ, { .error = zfs_isdir },
5529 VOPNAME_WRITE, { .error = zfs_isdir },
5530 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5531 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5532 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5533 VOPNAME_ACCESS, { .vop_access = zfs_access },
5534 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5535 VOPNAME_CREATE, { .vop_create = zfs_create },
5536 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5537 VOPNAME_LINK, { .vop_link = zfs_link },
5538 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5539 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5540 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5541 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5542 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5543 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5544 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5545 VOPNAME_FID, { .vop_fid = zfs_fid },
5546 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5547 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5548 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5549 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5550 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5555 * Regular file vnode operations template
5557 vnodeops_t *zfs_fvnodeops;
5558 const fs_operation_def_t zfs_fvnodeops_template[] = {
5559 VOPNAME_OPEN, { .vop_open = zfs_open },
5560 VOPNAME_CLOSE, { .vop_close = zfs_close },
5561 VOPNAME_READ, { .vop_read = zfs_read },
5562 VOPNAME_WRITE, { .vop_write = zfs_write },
5563 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5564 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5565 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5566 VOPNAME_ACCESS, { .vop_access = zfs_access },
5567 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5568 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5569 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5570 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5571 VOPNAME_FID, { .vop_fid = zfs_fid },
5572 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5573 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5574 VOPNAME_SPACE, { .vop_space = zfs_space },
5575 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5576 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5577 VOPNAME_MAP, { .vop_map = zfs_map },
5578 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5579 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5580 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5581 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5582 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5583 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5584 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5585 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5590 * Symbolic link vnode operations template
5592 vnodeops_t *zfs_symvnodeops;
5593 const fs_operation_def_t zfs_symvnodeops_template[] = {
5594 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5595 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5596 VOPNAME_ACCESS, { .vop_access = zfs_access },
5597 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5598 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5599 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5600 VOPNAME_FID, { .vop_fid = zfs_fid },
5601 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5602 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5607 * special share hidden files vnode operations template
5609 vnodeops_t *zfs_sharevnodeops;
5610 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5611 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5612 VOPNAME_ACCESS, { .vop_access = zfs_access },
5613 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5614 VOPNAME_FID, { .vop_fid = zfs_fid },
5615 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5616 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5617 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5618 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5623 * Extended attribute directory vnode operations template
5625 * This template is identical to the directory vnodes
5626 * operation template except for restricted operations:
5630 * Note that there are other restrictions embedded in:
5631 * zfs_create() - restrict type to VREG
5632 * zfs_link() - no links into/out of attribute space
5633 * zfs_rename() - no moves into/out of attribute space
5635 vnodeops_t *zfs_xdvnodeops;
5636 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5637 VOPNAME_OPEN, { .vop_open = zfs_open },
5638 VOPNAME_CLOSE, { .vop_close = zfs_close },
5639 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5640 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5641 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5642 VOPNAME_ACCESS, { .vop_access = zfs_access },
5643 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5644 VOPNAME_CREATE, { .vop_create = zfs_create },
5645 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5646 VOPNAME_LINK, { .vop_link = zfs_link },
5647 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5648 VOPNAME_MKDIR, { .error = zfs_inval },
5649 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5650 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5651 VOPNAME_SYMLINK, { .error = zfs_inval },
5652 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5653 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5654 VOPNAME_FID, { .vop_fid = zfs_fid },
5655 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5656 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5657 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5658 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5659 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5664 * Error vnode operations template
5666 vnodeops_t *zfs_evnodeops;
5667 const fs_operation_def_t zfs_evnodeops_template[] = {
5668 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5669 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5675 ioflags(int ioflags)
5679 if (ioflags & IO_APPEND)
5681 if (ioflags & IO_NDELAY)
5683 if (ioflags & IO_SYNC)
5684 flags |= (FSYNC | FDSYNC | FRSYNC);
5690 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5692 znode_t *zp = VTOZ(vp);
5693 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5694 objset_t *os = zp->z_zfsvfs->z_os;
5695 vm_page_t mfirst, mlast, mreq;
5699 off_t startoff, endoff;
5701 vm_pindex_t reqstart, reqend;
5702 int pcount, lsize, reqsize, size;
5707 pcount = OFF_TO_IDX(round_page(count));
5709 object = mreq->object;
5712 KASSERT(vp->v_object == object, ("mismatching object"));
5714 if (pcount > 1 && zp->z_blksz > PAGESIZE) {
5715 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz);
5716 reqstart = OFF_TO_IDX(round_page(startoff));
5717 if (reqstart < m[0]->pindex)
5720 reqstart = reqstart - m[0]->pindex;
5721 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE,
5723 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1;
5724 if (reqend > m[pcount - 1]->pindex)
5725 reqend = m[pcount - 1]->pindex;
5726 reqsize = reqend - m[reqstart]->pindex + 1;
5727 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize,
5728 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds"));
5733 mfirst = m[reqstart];
5734 mlast = m[reqstart + reqsize - 1];
5736 zfs_vmobject_wlock(object);
5738 for (i = 0; i < reqstart; i++) {
5741 vm_page_unlock(m[i]);
5743 for (i = reqstart + reqsize; i < pcount; i++) {
5746 vm_page_unlock(m[i]);
5749 if (mreq->valid && reqsize == 1) {
5750 if (mreq->valid != VM_PAGE_BITS_ALL)
5751 vm_page_zero_invalid(mreq, TRUE);
5752 zfs_vmobject_wunlock(object);
5754 return (zfs_vm_pagerret_ok);
5757 PCPU_INC(cnt.v_vnodein);
5758 PCPU_ADD(cnt.v_vnodepgsin, reqsize);
5760 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5761 for (i = reqstart; i < reqstart + reqsize; i++) {
5765 vm_page_unlock(m[i]);
5768 zfs_vmobject_wunlock(object);
5770 return (zfs_vm_pagerret_bad);
5774 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
5775 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex);
5777 zfs_vmobject_wunlock(object);
5779 for (i = reqstart; i < reqstart + reqsize; i++) {
5781 if (i == (reqstart + reqsize - 1))
5783 va = zfs_map_page(m[i], &sf);
5784 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
5785 size, va, DMU_READ_PREFETCH);
5786 if (size != PAGE_SIZE)
5787 bzero(va + size, PAGE_SIZE - size);
5793 zfs_vmobject_wlock(object);
5795 for (i = reqstart; i < reqstart + reqsize; i++) {
5797 m[i]->valid = VM_PAGE_BITS_ALL;
5798 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i]));
5800 vm_page_readahead_finish(m[i]);
5803 zfs_vmobject_wunlock(object);
5805 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5807 return (error ? zfs_vm_pagerret_error : zfs_vm_pagerret_ok);
5811 zfs_freebsd_getpages(ap)
5812 struct vop_getpages_args /* {
5817 vm_ooffset_t a_offset;
5821 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5825 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
5828 znode_t *zp = VTOZ(vp);
5829 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5838 vm_ooffset_t lo_off;
5849 object = vp->v_object;
5853 KASSERT(ma[0]->object == object, ("mismatching object"));
5854 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
5856 for (i = 0; i < pcount; i++)
5857 rtvals[i] = zfs_vm_pagerret_error;
5859 off = IDX_TO_OFF(ma[0]->pindex);
5860 blksz = zp->z_blksz;
5861 lo_off = rounddown(off, blksz);
5862 lo_len = roundup(len + (off - lo_off), blksz);
5863 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
5865 zfs_vmobject_wlock(object);
5866 if (len + off > object->un_pager.vnp.vnp_size) {
5867 if (object->un_pager.vnp.vnp_size > off) {
5870 len = object->un_pager.vnp.vnp_size - off;
5872 if ((pgoff = (int)len & PAGE_MASK) != 0) {
5874 * If the object is locked and the following
5875 * conditions hold, then the page's dirty
5876 * field cannot be concurrently changed by a
5880 vm_page_assert_sbusied(m);
5881 KASSERT(!pmap_page_is_write_mapped(m),
5882 ("zfs_putpages: page %p is not read-only", m));
5883 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5890 if (ncount < pcount) {
5891 for (i = ncount; i < pcount; i++) {
5892 rtvals[i] = zfs_vm_pagerret_bad;
5896 zfs_vmobject_wunlock(object);
5901 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
5902 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
5907 tx = dmu_tx_create(zfsvfs->z_os);
5908 dmu_tx_hold_write(tx, zp->z_id, off, len);
5910 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5911 zfs_sa_upgrade_txholds(tx, zp);
5912 err = dmu_tx_assign(tx, TXG_NOWAIT);
5914 if (err == ERESTART) {
5923 if (zp->z_blksz < PAGE_SIZE) {
5925 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5926 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5927 va = zfs_map_page(ma[i], &sf);
5928 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
5932 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
5936 uint64_t mtime[2], ctime[2];
5937 sa_bulk_attr_t bulk[3];
5940 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
5942 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
5944 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
5946 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
5948 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
5949 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
5951 zfs_vmobject_wlock(object);
5952 for (i = 0; i < ncount; i++) {
5953 rtvals[i] = zfs_vm_pagerret_ok;
5954 vm_page_undirty(ma[i]);
5956 zfs_vmobject_wunlock(object);
5957 PCPU_INC(cnt.v_vnodeout);
5958 PCPU_ADD(cnt.v_vnodepgsout, ncount);
5963 zfs_range_unlock(rl);
5964 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
5965 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5966 zil_commit(zfsvfs->z_log, zp->z_id);
5972 zfs_freebsd_putpages(ap)
5973 struct vop_putpages_args /* {
5979 vm_ooffset_t a_offset;
5983 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
5988 zfs_freebsd_bmap(ap)
5989 struct vop_bmap_args /* {
5992 struct bufobj **a_bop;
5999 if (ap->a_bop != NULL)
6000 *ap->a_bop = &ap->a_vp->v_bufobj;
6001 if (ap->a_bnp != NULL)
6002 *ap->a_bnp = ap->a_bn;
6003 if (ap->a_runp != NULL)
6005 if (ap->a_runb != NULL)
6012 zfs_freebsd_open(ap)
6013 struct vop_open_args /* {
6016 struct ucred *a_cred;
6017 struct thread *a_td;
6020 vnode_t *vp = ap->a_vp;
6021 znode_t *zp = VTOZ(vp);
6024 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
6026 vnode_create_vobject(vp, zp->z_size, ap->a_td);
6031 zfs_freebsd_close(ap)
6032 struct vop_close_args /* {
6035 struct ucred *a_cred;
6036 struct thread *a_td;
6040 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
6044 zfs_freebsd_ioctl(ap)
6045 struct vop_ioctl_args /* {
6055 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
6056 ap->a_fflag, ap->a_cred, NULL, NULL));
6060 zfs_freebsd_read(ap)
6061 struct vop_read_args /* {
6065 struct ucred *a_cred;
6069 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6074 zfs_freebsd_write(ap)
6075 struct vop_write_args /* {
6079 struct ucred *a_cred;
6083 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6088 zfs_freebsd_access(ap)
6089 struct vop_access_args /* {
6091 accmode_t a_accmode;
6092 struct ucred *a_cred;
6093 struct thread *a_td;
6096 vnode_t *vp = ap->a_vp;
6097 znode_t *zp = VTOZ(vp);
6102 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
6104 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
6106 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
6109 * VADMIN has to be handled by vaccess().
6112 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
6114 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
6115 zp->z_gid, accmode, ap->a_cred, NULL);
6120 * For VEXEC, ensure that at least one execute bit is set for
6123 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
6124 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
6132 zfs_freebsd_lookup(ap)
6133 struct vop_lookup_args /* {
6134 struct vnode *a_dvp;
6135 struct vnode **a_vpp;
6136 struct componentname *a_cnp;
6139 struct componentname *cnp = ap->a_cnp;
6140 char nm[NAME_MAX + 1];
6142 ASSERT(cnp->cn_namelen < sizeof(nm));
6143 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
6145 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
6146 cnp->cn_cred, cnp->cn_thread, 0));
6150 zfs_freebsd_create(ap)
6151 struct vop_create_args /* {
6152 struct vnode *a_dvp;
6153 struct vnode **a_vpp;
6154 struct componentname *a_cnp;
6155 struct vattr *a_vap;
6158 struct componentname *cnp = ap->a_cnp;
6159 vattr_t *vap = ap->a_vap;
6162 ASSERT(cnp->cn_flags & SAVENAME);
6164 vattr_init_mask(vap);
6165 mode = vap->va_mode & ALLPERMS;
6167 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
6168 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
6169 #ifdef FREEBSD_NAMECACHE
6170 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
6171 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
6177 zfs_freebsd_remove(ap)
6178 struct vop_remove_args /* {
6179 struct vnode *a_dvp;
6181 struct componentname *a_cnp;
6185 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6187 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
6188 ap->a_cnp->cn_cred, NULL, 0));
6192 zfs_freebsd_mkdir(ap)
6193 struct vop_mkdir_args /* {
6194 struct vnode *a_dvp;
6195 struct vnode **a_vpp;
6196 struct componentname *a_cnp;
6197 struct vattr *a_vap;
6200 vattr_t *vap = ap->a_vap;
6202 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6204 vattr_init_mask(vap);
6206 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
6207 ap->a_cnp->cn_cred, NULL, 0, NULL));
6211 zfs_freebsd_rmdir(ap)
6212 struct vop_rmdir_args /* {
6213 struct vnode *a_dvp;
6215 struct componentname *a_cnp;
6218 struct componentname *cnp = ap->a_cnp;
6220 ASSERT(cnp->cn_flags & SAVENAME);
6222 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
6226 zfs_freebsd_readdir(ap)
6227 struct vop_readdir_args /* {
6230 struct ucred *a_cred;
6237 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
6238 ap->a_ncookies, ap->a_cookies));
6242 zfs_freebsd_fsync(ap)
6243 struct vop_fsync_args /* {
6246 struct thread *a_td;
6251 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
6255 zfs_freebsd_getattr(ap)
6256 struct vop_getattr_args /* {
6258 struct vattr *a_vap;
6259 struct ucred *a_cred;
6262 vattr_t *vap = ap->a_vap;
6268 xvap.xva_vattr = *vap;
6269 xvap.xva_vattr.va_mask |= AT_XVATTR;
6271 /* Convert chflags into ZFS-type flags. */
6272 /* XXX: what about SF_SETTABLE?. */
6273 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
6274 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6275 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6276 XVA_SET_REQ(&xvap, XAT_NODUMP);
6277 XVA_SET_REQ(&xvap, XAT_READONLY);
6278 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
6279 XVA_SET_REQ(&xvap, XAT_SYSTEM);
6280 XVA_SET_REQ(&xvap, XAT_HIDDEN);
6281 XVA_SET_REQ(&xvap, XAT_REPARSE);
6282 XVA_SET_REQ(&xvap, XAT_OFFLINE);
6283 XVA_SET_REQ(&xvap, XAT_SPARSE);
6285 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6289 /* Convert ZFS xattr into chflags. */
6290 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6291 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6292 fflags |= (fflag); \
6294 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6295 xvap.xva_xoptattrs.xoa_immutable);
6296 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6297 xvap.xva_xoptattrs.xoa_appendonly);
6298 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6299 xvap.xva_xoptattrs.xoa_nounlink);
6300 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
6301 xvap.xva_xoptattrs.xoa_archive);
6302 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6303 xvap.xva_xoptattrs.xoa_nodump);
6304 FLAG_CHECK(UF_READONLY, XAT_READONLY,
6305 xvap.xva_xoptattrs.xoa_readonly);
6306 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
6307 xvap.xva_xoptattrs.xoa_system);
6308 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
6309 xvap.xva_xoptattrs.xoa_hidden);
6310 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
6311 xvap.xva_xoptattrs.xoa_reparse);
6312 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
6313 xvap.xva_xoptattrs.xoa_offline);
6314 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
6315 xvap.xva_xoptattrs.xoa_sparse);
6318 *vap = xvap.xva_vattr;
6319 vap->va_flags = fflags;
6324 zfs_freebsd_setattr(ap)
6325 struct vop_setattr_args /* {
6327 struct vattr *a_vap;
6328 struct ucred *a_cred;
6331 vnode_t *vp = ap->a_vp;
6332 vattr_t *vap = ap->a_vap;
6333 cred_t *cred = ap->a_cred;
6338 vattr_init_mask(vap);
6339 vap->va_mask &= ~AT_NOSET;
6342 xvap.xva_vattr = *vap;
6344 zflags = VTOZ(vp)->z_pflags;
6346 if (vap->va_flags != VNOVAL) {
6347 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6350 if (zfsvfs->z_use_fuids == B_FALSE)
6351 return (EOPNOTSUPP);
6353 fflags = vap->va_flags;
6356 * We need to figure out whether it makes sense to allow
6357 * UF_REPARSE through, since we don't really have other
6358 * facilities to handle reparse points and zfs_setattr()
6359 * doesn't currently allow setting that attribute anyway.
6361 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
6362 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
6363 UF_OFFLINE|UF_SPARSE)) != 0)
6364 return (EOPNOTSUPP);
6366 * Unprivileged processes are not permitted to unset system
6367 * flags, or modify flags if any system flags are set.
6368 * Privileged non-jail processes may not modify system flags
6369 * if securelevel > 0 and any existing system flags are set.
6370 * Privileged jail processes behave like privileged non-jail
6371 * processes if the security.jail.chflags_allowed sysctl is
6372 * is non-zero; otherwise, they behave like unprivileged
6375 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6376 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6378 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6379 error = securelevel_gt(cred, 0);
6385 * Callers may only modify the file flags on objects they
6386 * have VADMIN rights for.
6388 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6391 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6395 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6400 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6401 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6402 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6403 XVA_SET_REQ(&xvap, (xflag)); \
6404 (xfield) = ((fflags & (fflag)) != 0); \
6407 /* Convert chflags into ZFS-type flags. */
6408 /* XXX: what about SF_SETTABLE?. */
6409 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6410 xvap.xva_xoptattrs.xoa_immutable);
6411 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6412 xvap.xva_xoptattrs.xoa_appendonly);
6413 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6414 xvap.xva_xoptattrs.xoa_nounlink);
6415 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
6416 xvap.xva_xoptattrs.xoa_archive);
6417 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6418 xvap.xva_xoptattrs.xoa_nodump);
6419 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
6420 xvap.xva_xoptattrs.xoa_readonly);
6421 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
6422 xvap.xva_xoptattrs.xoa_system);
6423 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
6424 xvap.xva_xoptattrs.xoa_hidden);
6425 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
6426 xvap.xva_xoptattrs.xoa_hidden);
6427 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
6428 xvap.xva_xoptattrs.xoa_offline);
6429 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
6430 xvap.xva_xoptattrs.xoa_sparse);
6433 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6437 zfs_freebsd_rename(ap)
6438 struct vop_rename_args /* {
6439 struct vnode *a_fdvp;
6440 struct vnode *a_fvp;
6441 struct componentname *a_fcnp;
6442 struct vnode *a_tdvp;
6443 struct vnode *a_tvp;
6444 struct componentname *a_tcnp;
6447 vnode_t *fdvp = ap->a_fdvp;
6448 vnode_t *fvp = ap->a_fvp;
6449 vnode_t *tdvp = ap->a_tdvp;
6450 vnode_t *tvp = ap->a_tvp;
6453 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6454 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6457 * Check for cross-device rename.
6459 if ((fdvp->v_mount != tdvp->v_mount) ||
6460 (tvp && (fdvp->v_mount != tvp->v_mount)))
6463 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6464 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6478 zfs_freebsd_symlink(ap)
6479 struct vop_symlink_args /* {
6480 struct vnode *a_dvp;
6481 struct vnode **a_vpp;
6482 struct componentname *a_cnp;
6483 struct vattr *a_vap;
6487 struct componentname *cnp = ap->a_cnp;
6488 vattr_t *vap = ap->a_vap;
6490 ASSERT(cnp->cn_flags & SAVENAME);
6492 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6493 vattr_init_mask(vap);
6495 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6496 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6500 zfs_freebsd_readlink(ap)
6501 struct vop_readlink_args /* {
6504 struct ucred *a_cred;
6508 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6512 zfs_freebsd_link(ap)
6513 struct vop_link_args /* {
6514 struct vnode *a_tdvp;
6516 struct componentname *a_cnp;
6519 struct componentname *cnp = ap->a_cnp;
6520 vnode_t *vp = ap->a_vp;
6521 vnode_t *tdvp = ap->a_tdvp;
6523 if (tdvp->v_mount != vp->v_mount)
6526 ASSERT(cnp->cn_flags & SAVENAME);
6528 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6532 zfs_freebsd_inactive(ap)
6533 struct vop_inactive_args /* {
6535 struct thread *a_td;
6538 vnode_t *vp = ap->a_vp;
6540 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6545 zfs_freebsd_reclaim(ap)
6546 struct vop_reclaim_args /* {
6548 struct thread *a_td;
6551 vnode_t *vp = ap->a_vp;
6552 znode_t *zp = VTOZ(vp);
6553 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6557 /* Destroy the vm object and flush associated pages. */
6558 vnode_destroy_vobject(vp);
6561 * z_teardown_inactive_lock protects from a race with
6562 * zfs_znode_dmu_fini in zfsvfs_teardown during
6565 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6566 if (zp->z_sa_hdl == NULL)
6570 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6578 struct vop_fid_args /* {
6584 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6588 zfs_freebsd_pathconf(ap)
6589 struct vop_pathconf_args /* {
6592 register_t *a_retval;
6598 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6600 *ap->a_retval = val;
6601 else if (error == EOPNOTSUPP)
6602 error = vop_stdpathconf(ap);
6607 zfs_freebsd_fifo_pathconf(ap)
6608 struct vop_pathconf_args /* {
6611 register_t *a_retval;
6615 switch (ap->a_name) {
6616 case _PC_ACL_EXTENDED:
6618 case _PC_ACL_PATH_MAX:
6619 case _PC_MAC_PRESENT:
6620 return (zfs_freebsd_pathconf(ap));
6622 return (fifo_specops.vop_pathconf(ap));
6627 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6628 * extended attribute name:
6631 * system freebsd:system:
6632 * user (none, can be used to access ZFS fsattr(5) attributes
6633 * created on Solaris)
6636 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6639 const char *namespace, *prefix, *suffix;
6641 /* We don't allow '/' character in attribute name. */
6642 if (strchr(name, '/') != NULL)
6644 /* We don't allow attribute names that start with "freebsd:" string. */
6645 if (strncmp(name, "freebsd:", 8) == 0)
6648 bzero(attrname, size);
6650 switch (attrnamespace) {
6651 case EXTATTR_NAMESPACE_USER:
6653 prefix = "freebsd:";
6654 namespace = EXTATTR_NAMESPACE_USER_STRING;
6658 * This is the default namespace by which we can access all
6659 * attributes created on Solaris.
6661 prefix = namespace = suffix = "";
6664 case EXTATTR_NAMESPACE_SYSTEM:
6665 prefix = "freebsd:";
6666 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6669 case EXTATTR_NAMESPACE_EMPTY:
6673 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6675 return (ENAMETOOLONG);
6681 * Vnode operating to retrieve a named extended attribute.
6684 zfs_getextattr(struct vop_getextattr_args *ap)
6687 IN struct vnode *a_vp;
6688 IN int a_attrnamespace;
6689 IN const char *a_name;
6690 INOUT struct uio *a_uio;
6692 IN struct ucred *a_cred;
6693 IN struct thread *a_td;
6697 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6698 struct thread *td = ap->a_td;
6699 struct nameidata nd;
6702 vnode_t *xvp = NULL, *vp;
6705 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6706 ap->a_cred, ap->a_td, VREAD);
6710 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6717 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6725 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6727 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6729 NDFREE(&nd, NDF_ONLY_PNBUF);
6732 if (error == ENOENT)
6737 if (ap->a_size != NULL) {
6738 error = VOP_GETATTR(vp, &va, ap->a_cred);
6740 *ap->a_size = (size_t)va.va_size;
6741 } else if (ap->a_uio != NULL)
6742 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6745 vn_close(vp, flags, ap->a_cred, td);
6752 * Vnode operation to remove a named attribute.
6755 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6758 IN struct vnode *a_vp;
6759 IN int a_attrnamespace;
6760 IN const char *a_name;
6761 IN struct ucred *a_cred;
6762 IN struct thread *a_td;
6766 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6767 struct thread *td = ap->a_td;
6768 struct nameidata nd;
6771 vnode_t *xvp = NULL, *vp;
6774 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6775 ap->a_cred, ap->a_td, VWRITE);
6779 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6786 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6793 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
6794 UIO_SYSSPACE, attrname, xvp, td);
6799 NDFREE(&nd, NDF_ONLY_PNBUF);
6800 if (error == ENOENT)
6805 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6806 NDFREE(&nd, NDF_ONLY_PNBUF);
6809 if (vp == nd.ni_dvp)
6819 * Vnode operation to set a named attribute.
6822 zfs_setextattr(struct vop_setextattr_args *ap)
6825 IN struct vnode *a_vp;
6826 IN int a_attrnamespace;
6827 IN const char *a_name;
6828 INOUT struct uio *a_uio;
6829 IN struct ucred *a_cred;
6830 IN struct thread *a_td;
6834 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6835 struct thread *td = ap->a_td;
6836 struct nameidata nd;
6839 vnode_t *xvp = NULL, *vp;
6842 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6843 ap->a_cred, ap->a_td, VWRITE);
6847 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6854 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6855 LOOKUP_XATTR | CREATE_XATTR_DIR);
6861 flags = FFLAGS(O_WRONLY | O_CREAT);
6862 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6864 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6866 NDFREE(&nd, NDF_ONLY_PNBUF);
6874 error = VOP_SETATTR(vp, &va, ap->a_cred);
6876 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6879 vn_close(vp, flags, ap->a_cred, td);
6886 * Vnode operation to retrieve extended attributes on a vnode.
6889 zfs_listextattr(struct vop_listextattr_args *ap)
6892 IN struct vnode *a_vp;
6893 IN int a_attrnamespace;
6894 INOUT struct uio *a_uio;
6896 IN struct ucred *a_cred;
6897 IN struct thread *a_td;
6901 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6902 struct thread *td = ap->a_td;
6903 struct nameidata nd;
6904 char attrprefix[16];
6905 u_char dirbuf[sizeof(struct dirent)];
6908 struct uio auio, *uio = ap->a_uio;
6909 size_t *sizep = ap->a_size;
6911 vnode_t *xvp = NULL, *vp;
6912 int done, error, eof, pos;
6914 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6915 ap->a_cred, ap->a_td, VREAD);
6919 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6920 sizeof(attrprefix));
6923 plen = strlen(attrprefix);
6930 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6935 * ENOATTR means that the EA directory does not yet exist,
6936 * i.e. there are no extended attributes there.
6938 if (error == ENOATTR)
6943 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
6944 UIO_SYSSPACE, ".", xvp, td);
6947 NDFREE(&nd, NDF_ONLY_PNBUF);
6953 auio.uio_iov = &aiov;
6954 auio.uio_iovcnt = 1;
6955 auio.uio_segflg = UIO_SYSSPACE;
6957 auio.uio_rw = UIO_READ;
6958 auio.uio_offset = 0;
6963 aiov.iov_base = (void *)dirbuf;
6964 aiov.iov_len = sizeof(dirbuf);
6965 auio.uio_resid = sizeof(dirbuf);
6966 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
6967 done = sizeof(dirbuf) - auio.uio_resid;
6970 for (pos = 0; pos < done;) {
6971 dp = (struct dirent *)(dirbuf + pos);
6972 pos += dp->d_reclen;
6974 * XXX: Temporarily we also accept DT_UNKNOWN, as this
6975 * is what we get when attribute was created on Solaris.
6977 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
6979 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
6981 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
6983 nlen = dp->d_namlen - plen;
6986 else if (uio != NULL) {
6988 * Format of extattr name entry is one byte for
6989 * length and the rest for name.
6991 error = uiomove(&nlen, 1, uio->uio_rw, uio);
6993 error = uiomove(dp->d_name + plen, nlen,
7000 } while (!eof && error == 0);
7009 zfs_freebsd_getacl(ap)
7010 struct vop_getacl_args /* {
7019 vsecattr_t vsecattr;
7021 if (ap->a_type != ACL_TYPE_NFS4)
7024 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
7025 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
7028 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
7029 if (vsecattr.vsa_aclentp != NULL)
7030 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
7036 zfs_freebsd_setacl(ap)
7037 struct vop_setacl_args /* {
7046 vsecattr_t vsecattr;
7047 int aclbsize; /* size of acl list in bytes */
7050 if (ap->a_type != ACL_TYPE_NFS4)
7053 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
7057 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
7058 * splitting every entry into two and appending "canonical six"
7059 * entries at the end. Don't allow for setting an ACL that would
7060 * cause chmod(2) to run out of ACL entries.
7062 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
7065 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
7069 vsecattr.vsa_mask = VSA_ACE;
7070 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
7071 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
7072 aaclp = vsecattr.vsa_aclentp;
7073 vsecattr.vsa_aclentsz = aclbsize;
7075 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
7076 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
7077 kmem_free(aaclp, aclbsize);
7083 zfs_freebsd_aclcheck(ap)
7084 struct vop_aclcheck_args /* {
7093 return (EOPNOTSUPP);
7096 struct vop_vector zfs_vnodeops;
7097 struct vop_vector zfs_fifoops;
7098 struct vop_vector zfs_shareops;
7100 struct vop_vector zfs_vnodeops = {
7101 .vop_default = &default_vnodeops,
7102 .vop_inactive = zfs_freebsd_inactive,
7103 .vop_reclaim = zfs_freebsd_reclaim,
7104 .vop_access = zfs_freebsd_access,
7105 #ifdef FREEBSD_NAMECACHE
7106 .vop_lookup = vfs_cache_lookup,
7107 .vop_cachedlookup = zfs_freebsd_lookup,
7109 .vop_lookup = zfs_freebsd_lookup,
7111 .vop_getattr = zfs_freebsd_getattr,
7112 .vop_setattr = zfs_freebsd_setattr,
7113 .vop_create = zfs_freebsd_create,
7114 .vop_mknod = zfs_freebsd_create,
7115 .vop_mkdir = zfs_freebsd_mkdir,
7116 .vop_readdir = zfs_freebsd_readdir,
7117 .vop_fsync = zfs_freebsd_fsync,
7118 .vop_open = zfs_freebsd_open,
7119 .vop_close = zfs_freebsd_close,
7120 .vop_rmdir = zfs_freebsd_rmdir,
7121 .vop_ioctl = zfs_freebsd_ioctl,
7122 .vop_link = zfs_freebsd_link,
7123 .vop_symlink = zfs_freebsd_symlink,
7124 .vop_readlink = zfs_freebsd_readlink,
7125 .vop_read = zfs_freebsd_read,
7126 .vop_write = zfs_freebsd_write,
7127 .vop_remove = zfs_freebsd_remove,
7128 .vop_rename = zfs_freebsd_rename,
7129 .vop_pathconf = zfs_freebsd_pathconf,
7130 .vop_bmap = zfs_freebsd_bmap,
7131 .vop_fid = zfs_freebsd_fid,
7132 .vop_getextattr = zfs_getextattr,
7133 .vop_deleteextattr = zfs_deleteextattr,
7134 .vop_setextattr = zfs_setextattr,
7135 .vop_listextattr = zfs_listextattr,
7136 .vop_getacl = zfs_freebsd_getacl,
7137 .vop_setacl = zfs_freebsd_setacl,
7138 .vop_aclcheck = zfs_freebsd_aclcheck,
7139 .vop_getpages = zfs_freebsd_getpages,
7140 .vop_putpages = zfs_freebsd_putpages,
7143 struct vop_vector zfs_fifoops = {
7144 .vop_default = &fifo_specops,
7145 .vop_fsync = zfs_freebsd_fsync,
7146 .vop_access = zfs_freebsd_access,
7147 .vop_getattr = zfs_freebsd_getattr,
7148 .vop_inactive = zfs_freebsd_inactive,
7149 .vop_read = VOP_PANIC,
7150 .vop_reclaim = zfs_freebsd_reclaim,
7151 .vop_setattr = zfs_freebsd_setattr,
7152 .vop_write = VOP_PANIC,
7153 .vop_pathconf = zfs_freebsd_fifo_pathconf,
7154 .vop_fid = zfs_freebsd_fid,
7155 .vop_getacl = zfs_freebsd_getacl,
7156 .vop_setacl = zfs_freebsd_setacl,
7157 .vop_aclcheck = zfs_freebsd_aclcheck,
7161 * special share hidden files vnode operations template
7163 struct vop_vector zfs_shareops = {
7164 .vop_default = &default_vnodeops,
7165 .vop_access = zfs_freebsd_access,
7166 .vop_inactive = zfs_freebsd_inactive,
7167 .vop_reclaim = zfs_freebsd_reclaim,
7168 .vop_fid = zfs_freebsd_fid,
7169 .vop_pathconf = zfs_freebsd_pathconf,